![]() |
2010-05-25
, 12:08
|
Posts: 882 |
Thanked: 1,310 times |
Joined on Mar 2007
|
#2
|
The Following User Says Thank You to ukki For This Useful Post: | ||
![]() |
2010-05-25
, 12:10
|
|
Posts: 1,637 |
Thanked: 4,424 times |
Joined on Apr 2009
@ Germany
|
#3
|
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.
The Following User Says Thank You to nicolai For This Useful Post: | ||
![]() |
2010-05-25
, 12:14
|
|
Posts: 4,274 |
Thanked: 5,358 times |
Joined on Sep 2007
@ Looking at y'all and sighing
|
#4
|
![]() |
2010-05-26
, 11:55
|
Posts: 13 |
Thanked: 0 times |
Joined on May 2010
|
#5
|
![]() |
2010-05-26
, 13:53
|
|
Posts: 4,274 |
Thanked: 5,358 times |
Joined on Sep 2007
@ Looking at y'all and sighing
|
#6
|
Fine, is there also a way to detect when power charger is plugged/unplugged?
/* 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 (value, CHARGER_CONNECTED);
libhal_free_string (value);
(void) print_is_connected ();
}
static void on_property_modified (LibHalContext *ctx, const char *udi, const char *key, dbus_bool_t is_removed G_GNUC_UNUSED, dbus_bool_t is_added G_GNUC_UNUSED)
{
if (!g_str_equal (udi, BME_UDI) || !g_str_equal (key, CONNECTION_STATUS_PROP))
return;
set_connected_to_charger (libhal_device_get_property_string (hal_ctx, BME_UDI, CONNECTION_STATUS_PROP, NULL));
}
int main (void)
{
loop = g_main_loop_new (NULL, FALSE);
signal (SIGINT, sig_handler);
signal (SIGQUIT, sig_handler);
signal (SIGTERM, sig_handler);
osso_ctx = osso_initialize ("charger_test", "666", FALSE, NULL);
hal_ctx = libhal_ctx_new ();
libhal_ctx_set_dbus_connection (hal_ctx, (DBusConnection*) osso_get_sys_dbus_connection (osso_ctx));
libhal_ctx_init (hal_ctx, NULL);
/* Get initial state */
set_connected_to_charger (libhal_device_get_property_string (hal_ctx, BME_UDI, CONNECTION_STATUS_PROP, NULL));
/* Watch for changes */
libhal_ctx_set_device_property_modified (hal_ctx, on_property_modified);
libhal_device_add_property_watch (hal_ctx, BME_UDI, NULL);
g_main_loop_run (loop);
g_main_loop_unref (loop);
libhal_device_remove_property_watch (hal_ctx, BME_UDI, NULL);
libhal_ctx_shutdown (hal_ctx, NULL);
libhal_ctx_free (hal_ctx);
osso_deinitialize (osso_ctx);
return EXIT_SUCCESS;
}
bool
enough_battery_p (void)
{
LibHalContext *hal;
int i;
char **devs;
int n_devs;
hal = libhal_ctx_new ();
libhal_ctx_set_dbus_connection (hal, dbus_bus_get (DBUS_BUS_SYSTEM, NULL));
devs = libhal_find_device_by_capability (hal, "battery", &n_devs, NULL);
if (devs)
{
for (i = 0; i < n_devs; i++)
{
DBusError error;
dbus_error_init (&error);
dbus_bool_t charging = libhal_device_get_property_bool
(hal, devs[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
(hal, devs[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 (hal, NULL);
return devs == NULL || i < n_devs;
}
The Following User Says Thank You to qwerty12 For This Useful Post: | ||
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.