maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   General (https://talk.maemo.org/forumdisplay.php?f=7)
-   -   Getting Screen State to Save Power (https://talk.maemo.org/showthread.php?t=53580)

jarmniku 2010-05-25 11:53

Getting Screen State to Save Power
 
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.

ukki 2010-05-25 12:08

Re: Getting Screen State to Save Power
 
http://wiki.maemo.org/Documentation/...state_messages

nicolai 2010-05-25 12:10

Re: Getting Screen State to Save Power
 
Quote:

Originally Posted by jarmniku (Post 675586)
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

qwerty12 2010-05-25 12:14

Re: Getting Screen State to Save Power
 
osso_hw_set_display_event_cb: http://maemo.org/api_refs/5.0/5.0-fi...aa9891b0ec3977

jarmniku 2010-05-26 11:55

Re: Getting Screen State to Save Power
 
Fine, is there also a way to detect when power charger is plugged/unplugged?

qwerty12 2010-05-26 13:53

Re: Getting Screen State to Save Power
 
Quote:

Originally Posted by jarmniku (Post 679091)
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;




All times are GMT. The time now is 04:27.

vBulletin® Version 3.8.8