Jump to content

Battery Status

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
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.

#2
CallBackGuy

CallBackGuy

    Newbie

  • Members
  • PipPip
  • 29 posts
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++?

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
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
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...

#4
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Neat, how did you do it in C++?

#5
theonejb

theonejb

    Learning Programmer

  • Members
  • PipPipPip
  • 52 posts
Here's the code snippet for connecting to the DBus and registering a call back. It uses the PowerManagement app. for the callback:

 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