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 (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; }
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 (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; }
|