I'm trying to program an application that activates when ever the charging status of my laptop changes (from charging to discharging or vice versa). Is there any way in which I can make my application get notified of the event?
I'd like to program in C++, but if thats not possible, I'd be happy to do so in some other language as well.
Battery Status
Started by theonejb, Jul 16 2009 06:52 PM
4 replies to this topic
#1
Posted 16 July 2009 - 06:52 PM
|
|
|
#2
Posted 27 August 2009 - 03:03 AM
Here some python code that uses dbus/HAL to connect to your laptops ac_adapter and receives a signal when it's unplugged/plugged in.
Maybe you can try to port it to C++, I'm sure there is dbus/gobject header files for C++?
EDIT: Oh. A bit late of a reply, hopefully this is still helpful for him or someone else.
Maybe you can try to port it to C++, I'm sure there is dbus/gobject header files for C++?
import gobject
import dbus, dbus.service, dbus.mainloop.glib
def on_device_event (arg, dbarry):
if dev.GetProperty ('ac_adapter.present'):
print "Battery is charging"
else:
print "Battery is discharging"
dbus.mainloop.glib.DBusGMainLoop (set_as_default=True)
bus = dbus.SystemBus ()
hal_obj = bus.get_object ('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager')
hal = dbus.Interface (hal_obj, 'org.freedesktop.Hal.Manager')
dev_obj = bus.get_object("org.freedesktop.Hal", hal.FindDeviceByCapability("ac_adapter")[0])
dev = dbus.Interface(dev_obj, "org.freedesktop.Hal.Device")
dev.connect_to_signal("PropertyModified", on_device_event)
mainloop = gobject.MainLoop()
mainloop.run()
EDIT: Oh. A bit late of a reply, hopefully this is still helpful for him or someone else.
#3
Posted 27 August 2009 - 08:19 AM
Thanks for the reply, I however have already solved the problem. I used the EXACT same technique as you, just in C++. :thumbup:
Still, thnx...
Still, thnx...
#4
Posted 27 August 2009 - 09:24 AM
Neat, how did you do it in C++?
#5
Posted 27 August 2009 - 09:56 AM
Here's the code snippet for connecting to the DBus and registering a call back. It uses the PowerManagement app. for the callback:
Heres the link to the complete application that I needed to implement the DBus connection in:
~surfer-a1/wateen-manager/origin : contents of AC.c at revision 1
g_type_init();
// connect to the DBus
DBusGConnection *connection;
GError *error;
error = NULL;
connection = dbus_g_bus_get(DBUS_BUS_SESSION, &error);
// if failed, display error and quit
if (connection == NULL) {
g_printerr("ERROR: Unable to connect to DBus.\n");
g_error_free(error);
exit(1);
} else {
g_print("Success: Connection to DBus succeeded. Proceeding...\n");
}
// connect to the PowerManager
DBusGProxy *proxy;
const char *name = "org.freedesktop.PowerManagement";
const char *path = "/org/freedesktop/PowerManagement";
const char *interface = "org.freedesktop.PowerManagement";
error = NULL;
proxy = dbus_g_proxy_new_for_name(connection, name, path, interface);
// check for errors
if (proxy == NULL) {
g_printerr("ERROR: Unable to connect to the PowerManagement Applet.\n");
g_error_free(error);
exit(1);
} else {
g_print("Success: Connected to PowerManagement Applet. Proceeding...\n");
}
// connect to the OnBatteryChanged signal
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
const char *sName = "OnBatteryChanged";
dbus_g_proxy_add_signal(proxy, sName,G_TYPE_BOOLEAN, G_TYPE_INVALID);
dbus_g_proxy_connect_signal(proxy, sName, G_CALLBACK(onBatCh), NULL, NULL);
g_print("Success: Connected callback to signal.\n");
Heres the link to the complete application that I needed to implement the DBus connection in:
~surfer-a1/wateen-manager/origin : contents of AC.c at revision 1


Sign In
Create Account


Back to top









