From 65ab8e5387aa25f86c868c243a90b4b5174b6532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Jard=C3=B3n?= Date: Mon, 27 Jun 2011 16:45:26 +0100 Subject: Implement power dbus service interface --- Makefile.am | 20 +++++ src/dbus-shared-names.h | 2 +- src/power-service-dbus.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++ src/power-service-dbus.h | 57 ++++++++++++++ src/power-service.c | 7 +- src/power-service.xml | 11 +++ 6 files changed, 280 insertions(+), 5 deletions(-) create mode 100644 src/power-service-dbus.c create mode 100644 src/power-service-dbus.h create mode 100644 src/power-service.xml diff --git a/Makefile.am b/Makefile.am index b45f92d..2812ec6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -23,6 +23,8 @@ libexec_PROGRAMS = indicator-power-service powerlibdir = $(INDICATORDIR) powerlib_LTLIBRARIES = libpower.la libpower_la_SOURCES = \ + src/dbus-shared-names.h \ + src/gen-power-service.xml.h \ src/indicator-power.c libpower_la_CFLAGS = \ $(INDICATOR_CFLAGS) \ @@ -39,6 +41,9 @@ libpower_la_LDFLAGS = \ ################ indicator_power_service_SOURCES = \ + src/gen-power-service.xml.c \ + src/power-service-dbus.h \ + src/power-service-dbus.c \ src/power-service.c indicator_power_service_CFLAGS = \ $(POWERSERVICE_CFLAGS) \ @@ -48,6 +53,21 @@ indicator_power_service_CFLAGS = \ indicator_power_service_LDADD = \ $(POWERSERVICE_LIBS) +src/gen-%.xml.c: src/%.xml + @echo "Building $@ from $<" + @echo "const char * _$(subst -,_,$(subst .,_,$(notdir $(basename $<)))) = " > $@ + @sed -e "s:\":\\\\\":g" -e s:^:\": -e s:\$$:\\\\n\": $< >> $@ + @echo ";" >> $@ + +src/gen-%.xml.h: src/%.xml + @echo "Building $@ from $<" + @echo "extern const char * _$(subst -,_,$(subst .,_,$(notdir $(basename $<))));" > $@ + +BUILT_SOURCES = \ + src/gen-power-service.xml.c \ + src/gen-power-service.xml.h + + ############################################################ EXTRA_DIST = autogen.sh diff --git a/src/dbus-shared-names.h b/src/dbus-shared-names.h index 8401083..e383737 100644 --- a/src/dbus-shared-names.h +++ b/src/dbus-shared-names.h @@ -22,7 +22,7 @@ with this program. If not, see . #ifndef __DBUS_SHARED_NAMES_H__ -#define __DBUS_SHARED_NAMES_H__ 1 +#define __DBUS_SHARED_NAMES_H__ #define INDICATOR_POWER_DBUS_NAME "com.canonical.indicator.power" #define INDICATOR_POWER_DBUS_VERSION 1 diff --git a/src/power-service-dbus.c b/src/power-service-dbus.c new file mode 100644 index 0000000..7a42740 --- /dev/null +++ b/src/power-service-dbus.c @@ -0,0 +1,188 @@ +/* +An indicator to power related information in the menubar. + +Copyright 2011 Codethink Ltd. + +Authors: + Javier Jardon + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "power-service-dbus.h" + +#include + +#include "dbus-shared-names.h" +#include "gen-power-service.xml.h" + +struct _PowerServiceDbusPrivate +{ + GDBusConnection *bus; + GCancellable *bus_cancel; + guint dbus_registration; +}; + +/* GDBus Stuff */ +static GDBusNodeInfo *node_info = NULL; +static GDBusInterfaceInfo *interface_info = NULL; + +static void power_service_dbus_class_init (PowerServiceDbusClass *klass); +static void power_service_dbus_init (PowerServiceDbus *self); +static void power_service_dbus_dispose (GObject *object); +static void power_service_dbus_finalize (GObject *object); +static void bus_get_cb (GObject *object, + GAsyncResult *res, + gpointer user_data); + + +G_DEFINE_TYPE (PowerServiceDbus, power_service_dbus, G_TYPE_OBJECT); + +static void +bus_get_cb (GObject *object, + GAsyncResult *res, + gpointer user_data) +{ + PowerServiceDbus *self = POWER_SERVICE_DBUS (user_data); + PowerServiceDbusPrivate *priv = self->priv; + GError *error = NULL; + GDBusConnection *connection = g_bus_get_finish (res, &error); + + if (error != NULL) + { + g_error("OMG! Unable to get a connection to DBus: %s", error->message); + g_error_free(error); + + return; + } + + priv->bus = connection; + + if (priv->bus_cancel != NULL) + { + g_object_unref (priv->bus_cancel); + priv->bus_cancel = NULL; + } + + /* Now register our object on our new connection */ + priv->dbus_registration = g_dbus_connection_register_object (priv->bus, + INDICATOR_POWER_SERVICE_DBUS_OBJECT, + interface_info, + NULL, + user_data, + NULL, + &error); + if (error != NULL) + { + g_error ("Unable to register the object to DBus: %s", error->message); + g_error_free(error); + + return; + } +} + +static void +power_service_dbus_class_init (PowerServiceDbusClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = power_service_dbus_dispose; + object_class->finalize = power_service_dbus_finalize; + + /* Setting up the DBus interfaces */ + if (node_info == NULL) + { + GError * error = NULL; + + node_info = g_dbus_node_info_new_for_xml (_power_service, &error); + if (error != NULL) + { + g_error ("Unable to parse Power Service Dbus description: %s", error->message); + g_error_free (error); + } + } + + if (interface_info == NULL) + { + interface_info = g_dbus_node_info_lookup_interface (node_info, INDICATOR_POWER_SERVICE_DBUS_INTERFACE); + + if (interface_info == NULL) + { + g_error ("Unable to find interface '" INDICATOR_POWER_SERVICE_DBUS_INTERFACE "'"); + } + } + + g_type_class_add_private (klass, sizeof (PowerServiceDbusPrivate)); +} + +static void +power_service_dbus_init (PowerServiceDbus *self) +{ + PowerServiceDbusPrivate *priv; + + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, + POWER_SERVICE_DBUS_TYPE, + PowerServiceDbusPrivate); + priv = self->priv; + + priv->bus = NULL; + priv->bus_cancel = NULL; + priv->dbus_registration = 0; + + self->priv->bus_cancel = g_cancellable_new (); + g_bus_get (G_BUS_TYPE_SESSION, + priv->bus_cancel, + bus_get_cb, + self); +} + +static void +power_service_dbus_dispose (GObject *object) +{ + PowerServiceDbus *self = POWER_SERVICE_DBUS (object); + PowerServiceDbusPrivate *priv = self->priv; + + if (priv->dbus_registration != 0) + { + g_dbus_connection_unregister_object (priv->bus, + priv->dbus_registration); + /* Don't care if it fails, there's nothing we can do */ + priv->dbus_registration = 0; + } + + if (priv->bus != NULL) + { + g_object_unref (priv->bus); + priv->bus = NULL; + } + + if (priv->bus_cancel != NULL) + { + g_cancellable_cancel (priv->bus_cancel); + g_object_unref (priv->bus_cancel); + priv->bus_cancel = NULL; + } + + G_OBJECT_CLASS (power_service_dbus_parent_class)->dispose (object); +} + +static void +power_service_dbus_finalize (GObject *object) +{ + G_OBJECT_CLASS (power_service_dbus_parent_class)->finalize (object); +} + diff --git a/src/power-service-dbus.h b/src/power-service-dbus.h new file mode 100644 index 0000000..49757a7 --- /dev/null +++ b/src/power-service-dbus.h @@ -0,0 +1,57 @@ +/* +An indicator to time and date related information in the menubar. + +Copyright 2011 Codethink Ltd. + +Authors: + Javier Jardon + +This program is free software: you can redistribute it and/or modify it +under the terms of the GNU General Public License version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranties of +MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR +PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along +with this program. If not, see . +*/ + +#ifndef __POWER_SERVICE_DBUS_H__ +#define __POWER_SERVICE_DBUS_H__ + +#include +#include + +G_BEGIN_DECLS + +#define POWER_SERVICE_DBUS_TYPE (power_service_dbus_get_type ()) +#define POWER_SERVICE_DBUS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), POWER_SERVICE_DBUS_TYPE, PowerServiceDbus)) +#define POWER_SERVICE_DBUS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), POWER_SERVICE_DBUS_TYPE, PowerServiceDbusClass)) +#define IS_POWER_SERVICE_DBUS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), POWER_SERVICE_DBUS_TYPE)) +#define IS_POWER_SERVICE_DBUS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), POWER_SERVICE_DBUS_TYPE)) +#define POWER_SERVICE_DBUS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), POWER_SERVICE_DBUS_TYPE, PowerServiceDbusClass)) + +typedef struct _PowerServiceDbus PowerServiceDbus; +typedef struct _PowerServiceDbusClass PowerServiceDbusClass; +typedef struct _PowerServiceDbusPrivate PowerServiceDbusPrivate; + +struct _PowerServiceDbus +{ + GObject parent_instance; + + PowerServiceDbusPrivate *priv; +}; + +struct _PowerServiceDbusClass +{ + GObjectClass parent_class; +}; + +GType power_service_dbus_get_type (void) G_GNUC_CONST; + +G_END_DECLS + +#endif /* __POWER_SERVICE_DBUS_H__ */ diff --git a/src/power-service.c b/src/power-service.c index bca079b..daaea41 100644 --- a/src/power-service.c +++ b/src/power-service.c @@ -35,13 +35,13 @@ with this program. If not, see . #include +#include "power-service-dbus.h" #include "dbus-shared-names.h" static IndicatorService *service = NULL; static GMainLoop *mainloop = NULL; static DbusmenuServer *server = NULL; -/*TODO Do we need this?*/ -/*static PowerServiceDbus *dbus_interface = NULL;*/ +static PowerServiceDbus *dbus_interface = NULL; /* Repsonds to the service object saying it's time to shutdown. It stops the mainloop. */ @@ -133,8 +133,7 @@ main (gint argc, build_menus (root_menuitem); /* Setup dbus interface */ - /*TODO*/ - /*dbus_interface = g_object_new (POWER_SERVICE_DBUS_TYPE, NULL);*/ + dbus_interface = g_object_new (POWER_SERVICE_DBUS_TYPE, NULL); mainloop = g_main_loop_new (NULL, FALSE); g_main_loop_run(mainloop); diff --git a/src/power-service.xml b/src/power-service.xml new file mode 100644 index 0000000..d63fe25 --- /dev/null +++ b/src/power-service.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + -- cgit v1.2.3