Active Topics

 


Reply
Thread Tools
Posts: 13 | Thanked: 0 times | Joined on May 2010
#1
Hi!

I would like to stop my computation intensive animation when screen is powered down to save battery.

This is a very common need and mentioned as an example use of D-Bus, but I can't find an example how to actually do the trick. Actually, I can't find any examples of how to generally listen events from D-Bus, though there are a lot of web-pages concerning D-Bus. LibOSSO should also provide some kind of interface to the same thing, but I had no luck in that side, too.

The 'beef' is somehow hidden for me... I would appreciate any help.
 
Posts: 882 | Thanked: 1,310 times | Joined on Mar 2007
#2
 

The Following User Says Thank You to ukki For This Useful Post:
nicolai's Avatar
Posts: 1,637 | Thanked: 4,424 times | Joined on Apr 2009 @ Germany
#3
Originally Posted by jarmniku View Post
Hi!

I would like to stop my computation intensive animation when screen is powered down to save battery.

This is a very common need and mentioned as an example use of D-Bus, but I can't find an example how to actually do the trick. Actually, I can't find any examples of how to generally listen events from D-Bus, though there are a lot of web-pages concerning D-Bus. LibOSSO should also provide some kind of interface to the same thing, but I had no luck in that side, too.

The 'beef' is somehow hidden for me... I would appreciate any help.
Maybe you should post in Development rather than General.
What programming language do you want to use? (And for which
platform, I assume maemo5)
If C, here you can find some API-Dokumentation.
For screen-state look at mce-dev dbus-interface


nicolai
 

The Following User Says Thank You to nicolai For This Useful Post:
qwerty12's Avatar
Posts: 4,274 | Thanked: 5,358 times | Joined on Sep 2007 @ Looking at y'all and sighing
#4
 

The Following 2 Users Say Thank You to qwerty12 For This Useful Post:
Posts: 13 | Thanked: 0 times | Joined on May 2010
#5
Fine, is there also a way to detect when power charger is plugged/unplugged?
 
qwerty12's Avatar
Posts: 4,274 | Thanked: 5,358 times | Joined on Sep 2007 @ Looking at y'all and sighing
#6
Originally Posted by jarmniku View Post
Fine, is there also a way to detect when power charger is plugged/unplugged?
PHP Code:
/* gcc pimp.c `pkg-config --cflags --libs glib-2.0 libosso hal` -Wall */

#include <stdlib.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <libosso.h>
#include <libhal.h>

/* Subject to change. */
#define BME_UDI "/org/freedesktop/Hal/devices/bme"
#define CONNECTION_STATUS_PROP "maemo.charger.connection_status"
#define CHARGER_CONNECTED "connected"

static GMainLoop *loop NULL;
static 
osso_context_t *osso_ctx NULL;
static 
LibHalContext *hal_ctx NULL;
static 
gboolean connected_to_charger FALSE;

static 
void sig_handler (int sig G_GNUC_UNUSED)
{
    if  (
loop != NULL && g_main_loop_is_running (loop))
        
g_main_loop_quit (loop);
}

static 
inline gint print_is_connected (void)
{
    return 
g_printf ("%s\n"connected_to_charger "On charger" "Not on charger");
}

static 
void set_connected_to_charger (char *value)
{
    
g_return_if_fail (value);

    
connected_to_charger g_str_equal (valueCHARGER_CONNECTED);
    
libhal_free_string (value);

    (
voidprint_is_connected ();
}

static 
void on_property_modified (LibHalContext *ctx, const char *udi, const char *keydbus_bool_t is_removed G_GNUC_UNUSEDdbus_bool_t is_added G_GNUC_UNUSED)
{
    if (!
g_str_equal (udiBME_UDI) || !g_str_equal (keyCONNECTION_STATUS_PROP))
        return;

    
set_connected_to_charger (libhal_device_get_property_string (hal_ctxBME_UDICONNECTION_STATUS_PROPNULL));
}

int main (void)
{
    
loop g_main_loop_new (NULLFALSE);

    
signal (SIGINTsig_handler);
    
signal (SIGQUITsig_handler);
    
signal (SIGTERMsig_handler);

    
osso_ctx osso_initialize ("charger_test""666"FALSENULL);

    
hal_ctx libhal_ctx_new ();

    
libhal_ctx_set_dbus_connection (hal_ctx, (DBusConnection*) osso_get_sys_dbus_connection (osso_ctx));
    
libhal_ctx_init (hal_ctxNULL);

    
/* Get initial state */
    
set_connected_to_charger (libhal_device_get_property_string (hal_ctxBME_UDICONNECTION_STATUS_PROPNULL));

    
/* Watch for changes */
    
libhal_ctx_set_device_property_modified (hal_ctxon_property_modified);
    
libhal_device_add_property_watch (hal_ctxBME_UDINULL);

    
g_main_loop_run (loop);
    
g_main_loop_unref (loop);

    
libhal_device_remove_property_watch (hal_ctxBME_UDINULL);
    
libhal_ctx_shutdown (hal_ctxNULL);
    
libhal_ctx_free (hal_ctx);
    
osso_deinitialize (osso_ctx);

    return 
EXIT_SUCCESS;

Since there is no official way (AFAIK), here's an unofficial way. Make sure to add error checks and, as I deal in DBus-Glib and not libdbus directly...

Oh, I guess I was on the right lines.

Here's a snippet from dbus.cc, found in hildon-application-manager:
PHP Code:
bool
enough_battery_p 
(void)
{
  
LibHalContext *hal;

  
int i;
  
char **devs;
  
int n_devs;

  
hal libhal_ctx_new ();
  
libhal_ctx_set_dbus_connection (haldbus_bus_get (DBUS_BUS_SYSTEMNULL));
  
devs libhal_find_device_by_capability (hal"battery", &n_devsNULL);

  if (
devs)
    {
      for (
0n_devsi++)
    {
      
DBusError error;

      
dbus_error_init (&error);
      
dbus_bool_t charging libhal_device_get_property_bool
        
(haldevs[i], "battery.rechargeable.is_charging", &error);

      if (
dbus_error_is_set (&error))
        
dbus_error_free (&error);
      else
        {
          if (
charging)
        break;
        }

      
dbus_error_init (&error);
      
dbus_int32_t percentage libhal_device_get_property_int
        
(haldevs[i], "battery.charge_level.percentage", &error);

      if (
dbus_error_is_set (&error))
        {
          
dbus_error_free (&error);
          break;
        }
      else
        {
          if (
percentage 50)
        break;
        }
    }
    }

  
libhal_ctx_shutdown (halNULL);

  return 
devs == NULL || n_devs;


Last edited by qwerty12; 2010-05-27 at 18:05.
 

The Following User Says Thank You to qwerty12 For This Useful Post:
Reply


 
Forum Jump


All times are GMT. The time now is 14:18.