From 54da4e72561253c50b7bdb165657b39067c32a42 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 12 Oct 2009 19:41:34 -0400 Subject: [PATCH 08/21] Introduce Inhibitors This introduces the Inhibitor interface. --- doc/dbus/ConsoleKit.xml.in | 2 + doc/dbus/Makefile.am | 3 + src/Makefile.am | 6 + src/ck-inhibitor.c | 428 ++++++++++++++++++++++++++ src/ck-inhibitor.h | 91 ++++++ src/org.freedesktop.ConsoleKit.Inhibitor.xml | 24 ++ 6 files changed, 554 insertions(+), 0 deletions(-) create mode 100644 src/ck-inhibitor.c create mode 100644 src/ck-inhibitor.h create mode 100644 src/org.freedesktop.ConsoleKit.Inhibitor.xml diff --git a/doc/dbus/ConsoleKit.xml.in b/doc/dbus/ConsoleKit.xml.in index 3ce7aec..9cac3e4 100644 --- a/doc/dbus/ConsoleKit.xml.in +++ b/doc/dbus/ConsoleKit.xml.in @@ -3,6 +3,7 @@ + @@ -62,6 +63,7 @@ &dbus-Manager; &dbus-Seat; &dbus-Session; + &dbus-Inhibitor; diff --git a/doc/dbus/Makefile.am b/doc/dbus/Makefile.am index 4f8f3e7..6758fb5 100644 --- a/doc/dbus/Makefile.am +++ b/doc/dbus/Makefile.am @@ -8,6 +8,7 @@ SPEC_XML_FILES = \ org.freedesktop.ConsoleKit.Manager.ref.xml \ org.freedesktop.ConsoleKit.Seat.ref.xml \ org.freedesktop.ConsoleKit.Session.ref.xml \ + org.freedesktop.ConsoleKit.Inhibitor.ref.xml \ $(NULL) if DOCBOOK_DOCS_ENABLED @@ -26,6 +27,8 @@ org.freedesktop.ConsoleKit.Seat.ref.xml : $(top_srcdir)/src/org.freedesktop.Cons $(XSLTPROC) $(top_srcdir)/doc/dbus/spec-to-docbook.xsl $< | tail -n +2 > $@ org.freedesktop.ConsoleKit.Session.ref.xml : $(top_srcdir)/src/org.freedesktop.ConsoleKit.Session.xml spec-to-docbook.xsl $(XSLTPROC) $(top_srcdir)/doc/dbus/spec-to-docbook.xsl $< | tail -n +2 > $@ +org.freedesktop.ConsoleKit.Inhibitor.ref.xml : $(top_srcdir)/src/org.freedesktop.ConsoleKit.Inhibitor.xml spec-to-docbook.xsl + $(XSLTPROC) $(top_srcdir)/doc/dbus/spec-to-docbook.xsl $< | tail -n +2 > $@ EXTRA_DIST = \ spec-to-docbook.xsl \ diff --git a/src/Makefile.am b/src/Makefile.am index 6ab05c8..b83b9e3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -71,6 +71,7 @@ BUILT_SOURCES = \ ck-manager-glue.h \ ck-seat-glue.h \ ck-session-glue.h \ + ck-inhibitor-glue.h \ ck-marshal.c \ ck-marshal.h \ $(NULL) @@ -80,6 +81,7 @@ dbusif_DATA = \ org.freedesktop.ConsoleKit.Manager.xml \ org.freedesktop.ConsoleKit.Seat.xml \ org.freedesktop.ConsoleKit.Session.xml \ + org.freedesktop.ConsoleKit.Inhibitor.xml \ $(NULL) ck-manager-glue.h: org.freedesktop.ConsoleKit.Manager.xml Makefile.am @@ -88,6 +90,8 @@ ck-seat-glue.h: org.freedesktop.ConsoleKit.Seat.xml Makefile.am dbus-binding-tool --prefix=ck_seat --mode=glib-server --output=ck-seat-glue.h org.freedesktop.ConsoleKit.Seat.xml ck-session-glue.h: org.freedesktop.ConsoleKit.Session.xml Makefile.am dbus-binding-tool --prefix=ck_session --mode=glib-server --output=ck-session-glue.h org.freedesktop.ConsoleKit.Session.xml +ck-inhibitor-glue.h: org.freedesktop.ConsoleKit.Inhibitor.xml Makefile.am + dbus-binding-tool --prefix=ck_inhibitor --mode=glib-server --output=ck-inhibitor-glue.h org.freedesktop.ConsoleKit.Inhibitor.xml ck-marshal.c: ck-marshal.list echo "#include \"ck-marshal.h\"" > $@ && \ @@ -119,6 +123,8 @@ console_kit_daemon_SOURCES = \ ck-run-programs.h \ ck-event-logger.c \ ck-event-logger.h \ + ck-inhibitor.c \ + ck-inhibitor.h \ $(BUILT_SOURCES) \ $(NULL) diff --git a/src/ck-inhibitor.c b/src/ck-inhibitor.c new file mode 100644 index 0000000..5579f53 --- /dev/null +++ b/src/ck-inhibitor.c @@ -0,0 +1,428 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2009 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + */ + +#include "config.h" + +#include + +#include "ck-inhibitor.h" +#include "ck-inhibitor-glue.h" + +static guint32 inhibitor_serial = 1; + +#define CK_INHIBITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_INHIBITOR, CkInhibitorPrivate)) + +struct CkInhibitorPrivate +{ + char *id; + char *session_id; + char *reason; + char *bus_name; + guint flags; + guint cookie; + DBusGConnection *connection; +}; + +enum { + PROP_0, + PROP_REASON, + PROP_FLAGS, + PROP_COOKIE, + PROP_SESSION_ID, + PROP_BUS_NAME +}; + +G_DEFINE_TYPE (CkInhibitor, ck_inhibitor, G_TYPE_OBJECT) + +GQuark +ck_inhibitor_error_quark (void) +{ + static GQuark ret = 0; + if (ret == 0) { + ret = g_quark_from_static_string ("ck_inhibitor_error"); + } + + return ret; +} + +#define ENUM_ENTRY(NAME, DESC) { NAME, "" #NAME "", DESC } + +GType +ck_inhibitor_error_get_type (void) +{ + static GType etype = 0; + + if (etype == 0) { + static const GEnumValue values[] = { + ENUM_ENTRY (CK_INHIBITOR_ERROR_GENERAL, "GeneralError"), + ENUM_ENTRY (CK_INHIBITOR_ERROR_NOT_SET, "NotSet"), + { 0, 0, 0 } + }; + + g_assert (CK_INHIBITOR_NUM_ERRORS == G_N_ELEMENTS (values) - 1); + + etype = g_enum_register_static ("CkInhibitorError", values); + } + + return etype; +} + +static guint32 +get_next_inhibitor_serial (void) +{ + guint32 serial; + + serial = inhibitor_serial++; + + if ((gint32)inhibitor_serial < 0) { + inhibitor_serial = 1; + } + + return serial; +} + +static gboolean +register_inhibitor (CkInhibitor *inhibitor) +{ + GError *error; + + error = NULL; + inhibitor->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); + if (inhibitor->priv->connection == NULL) { + if (error != NULL) { + g_critical ("error getting system bus: %s", error->message); + g_error_free (error); + } + return FALSE; + } + + dbus_g_connection_register_g_object (inhibitor->priv->connection, inhibitor->priv->id, G_OBJECT (inhibitor)); + + return TRUE; +} + +static GObject * +ck_inhibitor_constructor (GType type, + guint n_construct_properties, + GObjectConstructParam *construct_properties) +{ + CkInhibitor *inhibitor; + gboolean res; + + inhibitor = CK_INHIBITOR (G_OBJECT_CLASS (ck_inhibitor_parent_class)->constructor (type, + n_construct_properties, + construct_properties)); + + g_free (inhibitor->priv->id); + inhibitor->priv->id = g_strdup_printf ("/org/freedesktop/ConsoleKit/Inhibitor%u", get_next_inhibitor_serial ()); + res = register_inhibitor (inhibitor); + if (! res) { + g_warning ("Unable to register inhibitor with system bus"); + } + + return G_OBJECT (inhibitor); +} + +static void +ck_inhibitor_init (CkInhibitor *inhibitor) +{ + inhibitor->priv = CK_INHIBITOR_GET_PRIVATE (inhibitor); +} + +static void +ck_inhibitor_set_reason (CkInhibitor *inhibitor, + const char *reason) +{ + g_return_if_fail (CK_IS_INHIBITOR (inhibitor)); + + g_free (inhibitor->priv->reason); + + if (reason != NULL) { + inhibitor->priv->reason = g_strdup (reason); + } else { + inhibitor->priv->reason = g_strdup (""); + } + g_object_notify (G_OBJECT (inhibitor), "reason"); +} + +static void +ck_inhibitor_set_session_id (CkInhibitor *inhibitor, + const char *session_id) +{ + g_return_if_fail (CK_IS_INHIBITOR (inhibitor)); + + g_free (inhibitor->priv->session_id); + inhibitor->priv->session_id = g_strdup (session_id); + g_object_notify (G_OBJECT (inhibitor), "session-id"); +} + +static void +ck_inhibitor_set_bus_name (CkInhibitor *inhibitor, + const char *bus_name) +{ + g_return_if_fail (CK_IS_INHIBITOR (inhibitor)); + + g_free (inhibitor->priv->bus_name); + inhibitor->priv->bus_name = g_strdup (bus_name); + g_object_notify (G_OBJECT (inhibitor), "session-id"); +} + +static void +ck_inhibitor_set_cookie (CkInhibitor *inhibitor, + guint cookie) +{ + g_return_if_fail (CK_IS_INHIBITOR (inhibitor)); + + if (inhibitor->priv->cookie != cookie) { + inhibitor->priv->cookie = cookie; + g_object_notify (G_OBJECT (inhibitor), "cookie"); + } +} + +static void +ck_inhibitor_set_flags (CkInhibitor *inhibitor, + guint flags) +{ + g_return_if_fail (CK_IS_INHIBITOR (inhibitor)); + + if (inhibitor->priv->flags != flags) { + inhibitor->priv->flags = flags; + g_object_notify (G_OBJECT (inhibitor), "flags"); + } +} + +const char * +ck_inhibitor_peek_id (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), NULL); + + return inhibitor->priv->id; +} + +const char * +ck_inhibitor_peek_session_id (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), NULL); + + return inhibitor->priv->session_id; +} + +const char * +ck_inhibitor_peek_reason (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), NULL); + + return inhibitor->priv->reason; +} + +const char * +ck_inhibitor_peek_bus_name (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), NULL); + + return inhibitor->priv->bus_name; +} + +guint +ck_inhibitor_peek_flags (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), 0); + + return inhibitor->priv->flags; +} + +guint +ck_inhibitor_peek_cookie (CkInhibitor *inhibitor) +{ + g_return_val_if_fail (CK_IS_INHIBITOR (inhibitor), 0); + + return inhibitor->priv->cookie; +} + +static void +ck_inhibitor_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + CkInhibitor *self; + + self = CK_INHIBITOR (object); + + switch (prop_id) { + case PROP_REASON: + ck_inhibitor_set_reason (self, g_value_get_string (value)); + break; + case PROP_SESSION_ID: + ck_inhibitor_set_session_id (self, g_value_get_string (value)); + break; + case PROP_BUS_NAME: + ck_inhibitor_set_bus_name (self, g_value_get_string (value)); + break; + case PROP_FLAGS: + ck_inhibitor_set_flags (self, g_value_get_uint (value)); + break; + case PROP_COOKIE: + ck_inhibitor_set_cookie (self, g_value_get_uint (value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +ck_inhibitor_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + CkInhibitor *self; + + self = CK_INHIBITOR (object); + + switch (prop_id) { + case PROP_REASON: + g_value_set_string (value, self->priv->reason); + break; + case PROP_SESSION_ID: + g_value_set_string (value, self->priv->session_id); + break; + case PROP_BUS_NAME: + g_value_set_string (value, self->priv->bus_name); + break; + case PROP_FLAGS: + g_value_set_uint (value, self->priv->flags); + break; + case PROP_COOKIE: + g_value_set_uint (value, self->priv->cookie); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +ck_inhibitor_finalize (GObject *object) +{ + CkInhibitor *inhibitor = (CkInhibitor *) object; + + g_free (inhibitor->priv->id); + g_free (inhibitor->priv->session_id); + g_free (inhibitor->priv->reason); + g_free (inhibitor->priv->bus_name); + + G_OBJECT_CLASS (ck_inhibitor_parent_class)->finalize (object); +} + +static void +ck_inhibitor_class_init (CkInhibitorClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->finalize = ck_inhibitor_finalize; + object_class->constructor = ck_inhibitor_constructor; + object_class->get_property = ck_inhibitor_get_property; + object_class->set_property = ck_inhibitor_set_property; + + g_object_class_install_property (object_class, + PROP_REASON, + g_param_spec_string ("reason", + "reason", + "reason", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, + PROP_SESSION_ID, + g_param_spec_string ("session-id", + "session-id", + "session-id", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, + PROP_BUS_NAME, + g_param_spec_string ("bus-name", + "bus-name", + "bus-name", + "", + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, + PROP_FLAGS, + g_param_spec_uint ("flags", + "flags", + "flags", + 0, + G_MAXINT, + 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + g_object_class_install_property (object_class, + PROP_COOKIE, + g_param_spec_uint ("cookie", + "cookie", + "cookie", + 0, + G_MAXINT, + 0, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); + + dbus_g_object_type_install_info (CK_TYPE_INHIBITOR, &dbus_glib_ck_inhibitor_object_info); + dbus_g_error_domain_register (CK_INHIBITOR_ERROR, NULL, CK_INHIBITOR_TYPE_ERROR); + g_type_class_add_private (klass, sizeof (CkInhibitorPrivate)); +} + +CkInhibitor * +ck_inhibitor_new (const char *bus_name, + guint flags, + const char *reason, + guint cookie) +{ + CkInhibitor *inhibitor; + + inhibitor = g_object_new (CK_TYPE_INHIBITOR, + "bus-name", bus_name, + "flags", flags, + "reason", reason, + "cookie", cookie, + NULL); + + return inhibitor; +} + +CkInhibitor * +ck_inhibitor_new_for_session (const char *session_id, + const char *bus_name, + guint flags, + const char *reason, + guint cookie) +{ + CkInhibitor *inhibitor; + + inhibitor = g_object_new (CK_TYPE_INHIBITOR, + "session-id", session_id, + "bus-name", bus_name, + "flags", flags, + "reason", reason, + "cookie", cookie, + NULL); + + return inhibitor; +} + diff --git a/src/ck-inhibitor.h b/src/ck-inhibitor.h new file mode 100644 index 0000000..57dc7cb --- /dev/null +++ b/src/ck-inhibitor.h @@ -0,0 +1,91 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- + * + * Copyright (C) 2009 Red Hat, Inc + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +#ifndef __CK_INHIBITOR_H +#define __CK_INHIBITOR_H + + +#include + + +#define CK_TYPE_INHIBITOR (ck_inhibitor_get_type ()) +#define CK_INHIBITOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CK_TYPE_INHIBITOR, CkInhibitor)) +#define CK_INHIBITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CK_TYPE_INHIBITOR, CkInhibitorClass)) +#define CK_IS_INHIBITOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CK_TYPE_INHIBITOR)) +#define CK_IS_INHIBITOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CK_TYPE_INHIBITOR)) +#define CK_INHIBITOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CK_TYPE_INHIBITOR, CkInhibitorClass)) + +typedef struct _CkInhibitor CkInhibitor; +typedef struct _CkInhibitorClass CkInhibitorClass; + +typedef struct CkInhibitorPrivate CkInhibitorPrivate; + +struct _CkInhibitor +{ + GObject parent; + CkInhibitorPrivate *priv; +}; + +struct _CkInhibitorClass +{ + GObjectClass parent_class; +}; + +typedef enum { + CK_INHIBITOR_FLAG_SHUTDOWN = 1 << 0 +} CkInhibitorFlag; + +typedef enum +{ + CK_INHIBITOR_ERROR_GENERAL = 0, + CK_INHIBITOR_ERROR_NOT_SET, + CK_INHIBITOR_NUM_ERRORS +} CkInhibitorError; + +#define CK_INHIBITOR_ERROR ck_inhibitor_error_quark () +GType ck_inhibitor_error_get_type (void); +#define CK_INHIBITOR_TYPE_ERROR (ck_inhibitor_error_get_type ()) + +GQuark ck_inhibitor_error_quark (void); + +GType ck_inhibitor_get_type (void) G_GNUC_CONST; + +CkInhibitor * ck_inhibitor_new (const char *bus_name, + guint flags, + const char *reason, + guint cookie); +CkInhibitor * ck_inhibitor_new_for_session (const char *bus_name, + const char *session_id, + guint flags, + const char *reason, + guint cookie); + +const char * ck_inhibitor_peek_id (CkInhibitor *inhibitor); +const char * ck_inhibitor_peek_bus_name (CkInhibitor *inhibitor); +const char * ck_inhibitor_peek_session_id (CkInhibitor *inhibitor); +const char * ck_inhibitor_peek_reason (CkInhibitor *inhibitor); +guint ck_inhibitor_peek_cookie (CkInhibitor *inhibitor); +guint ck_inhibitor_peek_flags (CkInhibitor *inhibitor); + + +G_END_DECLS + +#endif /* __CK_INHIBITOR_H__ */ + diff --git a/src/org.freedesktop.ConsoleKit.Inhibitor.xml b/src/org.freedesktop.ConsoleKit.Inhibitor.xml new file mode 100644 index 0000000..7bdc449 --- /dev/null +++ b/src/org.freedesktop.ConsoleKit.Inhibitor.xml @@ -0,0 +1,24 @@ + + + + + + + The reason for the inhibit. Note that this will be either a string that comes from the creator of the inhibit, or the fixed string "Not responding". Callers should check for "Not responding" and translate the string before presenting it to the user + + + + + The flags that determine the scope of the inhibit + + + + + The session ID associated with this inhibit, if any. + + + + + -- 1.6.5.rc2