summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2008-08-04 17:31:52 +0100
committerRichard Hughes <richard@hughsie.com>2008-08-04 17:31:52 +0100
commit9dca67f115ff8a71c1f633356918a9e056d24cac (patch)
treee5891f9501a6f7757c0db1c4fd1c5601badf9396 /tools
parent53d3e2bf255fba4f73cab0120e487d16214242af (diff)
add a new ninary to get the battery power state
Diffstat (limited to 'tools')
-rw-r--r--tools/Makefile.am19
-rw-r--r--tools/dkp-battery-power.c67
2 files changed, 83 insertions, 3 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 0f8143f..6dfe157 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -30,13 +30,12 @@ dkp-marshal.c: $(top_srcdir)/src/dkp-marshal.list
dkp-daemon-glue.h: $(top_srcdir)/src/org.freedesktop.DeviceKit.Power.xml Makefile.am
dbus-binding-tool --prefix=devkit_power_daemon --mode=glib-client --output=dkp-daemon-glue.h $(top_srcdir)/src/org.freedesktop.DeviceKit.Power.xml
-bin_PROGRAMS = devkit-power
+bin_PROGRAMS = devkit-power devkit-battery-power
devkit_power_SOURCES = \
- dkp-tool.c \
+ dkp-tool.c \
$(BUILT_SOURCES)
-
devkit_power_CPPFLAGS = \
-DG_LOG_DOMAIN=\"devkit-power\" \
$(DISABLE_DEPRECATED) \
@@ -47,6 +46,20 @@ devkit_power_LDADD = \
$(DEVKIT_POWER_LIBS) \
$(POLKIT_DBUS_LIBS)
+devkit_battery_power_SOURCES = \
+ dkp-battery-power.c \
+ $(BUILT_SOURCES)
+
+devkit_battery_power_CPPFLAGS = \
+ -DG_LOG_DOMAIN=\"devkit-power\" \
+ $(DISABLE_DEPRECATED) \
+ $(AM_CPPFLAGS)
+
+devkit_battery_power_LDADD = \
+ $(DBUS_GLIB_LIBS) \
+ $(DEVKIT_POWER_LIBS) \
+ $(POLKIT_DBUS_LIBS)
+
CLEANFILES = $(BUILT_SOURCES)
clean-local :
diff --git a/tools/dkp-battery-power.c b/tools/dkp-battery-power.c
new file mode 100644
index 0000000..49be2ca
--- /dev/null
+++ b/tools/dkp-battery-power.c
@@ -0,0 +1,67 @@
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include "dkp-debug.h"
+
+/**
+ * main:
+ **/
+int
+main (int argc, char **argv)
+{
+ gint retval = 1;
+ gboolean ret;
+ gboolean on_battery;
+ GError *error = NULL;
+ DBusGConnection *bus = NULL;
+ DBusGProxy *proxy = NULL;
+ gboolean verbose = FALSE;
+ GOptionContext *context;
+
+ const GOptionEntry entries[] = {
+ { "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Show extra debugging information", NULL },
+ { NULL }
+ };
+
+ g_type_init ();
+
+ context = g_option_context_new ("devkit-battery-power");
+ g_option_context_add_main_entries (context, entries, NULL);
+ g_option_context_parse (context, &argc, &argv, NULL);
+ g_option_context_free (context);
+ dkp_debug_init (verbose);
+
+ bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
+ if (bus == NULL) {
+ dkp_warning ("Couldn't connect to system bus: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ proxy = dbus_g_proxy_new_for_name (bus, "org.freedesktop.DeviceKit.Power",
+ "/", "org.freedesktop.DeviceKit.Power");
+ if (proxy == NULL) {
+ dkp_warning ("Couldn't connect to DeviceKit-power");
+ goto out;
+ }
+
+ ret = dbus_g_proxy_call (proxy, "GetOnBattery", &error,
+ G_TYPE_INVALID,
+ G_TYPE_BOOLEAN, &on_battery,
+ G_TYPE_INVALID);
+ if (!ret) {
+ dkp_debug ("GetOnBattery failed: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ g_print ("on-battery: %s\n", on_battery ? "yes" : "no");
+ retval = 0;
+
+out:
+ if (proxy != NULL)
+ g_object_unref (proxy);
+ if (bus != NULL)
+ dbus_g_connection_unref (bus);
+ return retval;
+}
+