From da1c209be7cb975020ca1f8e31dbb9599346171f Mon Sep 17 00:00:00 2001 From: Ali Abdallah <ali.slackware@gmail.com> Date: Fri, 10 Apr 2009 14:00:29 +0000 Subject: [PATCH] Connect to the session using libxfcegui4 and properly shutdown when the session dies (Old svn revision: 7155) --- ChangeLog | 3 + src/Makefile.am | 2 + src/xfpm-main.c | 13 ++++ src/xfpm-manager.c | 17 +++++ src/xfpm-session.c | 169 +++++++++++++++++++++++++++++++++++++++++++++ src/xfpm-session.h | 57 +++++++++++++++ 6 files changed, 261 insertions(+) create mode 100644 src/xfpm-session.c create mode 100644 src/xfpm-session.h diff --git a/ChangeLog b/ChangeLog index a3b3f38e..42e82218 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ +2009-04-10 16:00 Ali aliov@xfce.org + * : Connect to the session using libxfcegui4 and properly shutdown when the session dies + 2009-04-10 12:24 Ali aliov@xfce.org * : Provide a wake up signal to avoid possible multiple sleep/hibernate request diff --git a/src/Makefile.am b/src/Makefile.am index 74504982..766c9efe 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,6 +15,8 @@ xfce4_power_manager_SOURCES = \ xfpm-manager.h \ xfpm-engine.c \ xfpm-engine.h \ + xfpm-session.c \ + xfpm-session.h \ xfpm-xfconf.c \ xfpm-xfconf.h \ xfpm-supply.c \ diff --git a/src/xfpm-main.c b/src/xfpm-main.c index 7573a756..e5663189 100644 --- a/src/xfpm-main.c +++ b/src/xfpm-main.c @@ -41,6 +41,7 @@ #include "xfce-power-manager-dbus-client.h" #include "xfpm-manager.h" +#include "xfpm-session.h" static void show_version() @@ -64,6 +65,7 @@ int main(int argc, char **argv) gboolean version = FALSE; gboolean no_daemon = FALSE; gboolean reload = FALSE; + gchar *client_id = NULL; xfce_textdomain (GETTEXT_PACKAGE, LOCALEDIR, "UTF-8"); @@ -75,6 +77,7 @@ int main(int argc, char **argv) { "customize", 'c', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &config, N_("Show the configuration dialog"), NULL }, { "quit", 'q', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &quit, N_("Quit any running xfce power manager"), NULL }, { "version", 'V', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &version, N_("Version information"), NULL }, + { "sm-client-id", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &client_id, NULL, NULL }, { NULL, }, }; @@ -227,14 +230,24 @@ int main(int argc, char **argv) else { TRACE("Starting the power manager\n"); + XfpmSession *session; + session = xfpm_session_new (); + + if ( client_id != NULL ) + xfpm_session_set_client_id (session, client_id); + if ( no_daemon == FALSE && daemon(0,0) ) { g_critical ("Could not daemonize"); } + XfpmManager *manager; manager = xfpm_manager_new(bus); xfpm_manager_start(manager); gtk_main(); + + g_object_unref (session); } + return EXIT_SUCCESS; } diff --git a/src/xfpm-manager.c b/src/xfpm-manager.c index c1208949..5d37ec24 100644 --- a/src/xfpm-manager.c +++ b/src/xfpm-manager.c @@ -44,6 +44,7 @@ #include "xfpm-manager.h" #include "xfpm-engine.h" +#include "xfpm-session.h" /* Init */ static void xfpm_manager_class_init (XfpmManagerClass *klass); @@ -60,6 +61,7 @@ static gboolean xfpm_manager_quit (XfpmManager *manager); struct XfpmManagerPrivate { + XfpmSession *session; XfpmEngine *engine; HalMonitor *monitor; @@ -106,6 +108,8 @@ xfpm_manager_init(XfpmManager *manager) manager->priv->engine = NULL; manager->priv->monitor = NULL; + manager->priv->session = xfpm_session_new (); + notify_init ("xfce4-power-manager"); } @@ -121,6 +125,8 @@ xfpm_manager_finalize(GObject *object) if ( manager->priv->engine ) g_object_unref (manager->priv->engine); + + g_object_unref (manager->priv->session); g_object_unref (manager->priv->monitor); @@ -166,6 +172,12 @@ xfpm_manager_reserve_names (XfpmManager *manager) } } +static void +xfpm_manager_session_die_cb (XfpmSession *session, XfpmManager *manager) +{ + xfpm_manager_quit (manager); +} + XfpmManager * xfpm_manager_new (DBusGConnection *bus) { @@ -200,6 +212,11 @@ void xfpm_manager_start (XfpmManager *manager) } manager->priv->engine = xfpm_engine_new (); + manager->priv->session = xfpm_session_new (); + + g_signal_connect (manager->priv->session, "session-die", + G_CALLBACK (xfpm_manager_session_die_cb), manager); + out: ; } diff --git a/src/xfpm-session.c b/src/xfpm-session.c new file mode 100644 index 00000000..e9417b9d --- /dev/null +++ b/src/xfpm-session.c @@ -0,0 +1,169 @@ +/* + * * Copyright (C) 2009 Ali <aliov@xfce.org> + * + * Licensed under the GNU General Public License Version 2 + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <glib.h> +#include <gtk/gtk.h> + +#include <libxfce4util/libxfce4util.h> +#include <libxfcegui4/libxfcegui4.h> + +#include "xfpm-session.h" + +/* Init */ +static void xfpm_session_class_init (XfpmSessionClass *klass); +static void xfpm_session_init (XfpmSession *session); +static void xfpm_session_finalize (GObject *object); + +#define XFPM_SESSION_GET_PRIVATE(o) \ +(G_TYPE_INSTANCE_GET_PRIVATE ((o), XFPM_TYPE_SESSION, XfpmSessionPrivate)) + +struct XfpmSessionPrivate +{ + SessionClient *client; + gboolean managed; +}; + +enum +{ + SESSION_DIE, + LAST_SIGNAL +}; + +static guint signals [LAST_SIGNAL] = { 0 }; + +static gpointer xfpm_session_object = NULL; + +G_DEFINE_TYPE (XfpmSession, xfpm_session, G_TYPE_OBJECT) + +static void +xfpm_session_die (gpointer client_data) +{ + XfpmSession *session; + + if ( G_UNLIKELY (xfpm_session_object == NULL ) ) + return; + + session = XFPM_SESSION (xfpm_session_object); + if ( G_UNLIKELY (session->priv->managed == FALSE) ) + return; + + TRACE ("Session disconnected signal\n"); + g_signal_emit (G_OBJECT (session), signals [SESSION_DIE], 0); +} + +static void +xfpm_session_class_init (XfpmSessionClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + signals[SESSION_DIE] = + g_signal_new("session-die", + XFPM_TYPE_SESSION, + G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET(XfpmSessionClass, session_die), + NULL, NULL, + g_cclosure_marshal_VOID__VOID, + G_TYPE_NONE, 0, G_TYPE_NONE); + + object_class->finalize = xfpm_session_finalize; + + g_type_class_add_private (klass, sizeof (XfpmSessionPrivate)); +} + +static void +xfpm_session_init (XfpmSession *session) +{ + gchar **restart_command; + + session->priv = XFPM_SESSION_GET_PRIVATE (session); + + session->priv->client = NULL; + + restart_command = g_new (gchar *, 3); + restart_command[0] = g_strdup ("xfce4-power-manager"); + restart_command[1] = g_strdup ("--restart"); + restart_command[2] = NULL; + + session->priv->client = client_session_new_full (NULL, + SESSION_RESTART_IMMEDIATELY, + 40, + NULL, + (gchar *) PACKAGE_NAME, + NULL, + restart_command, + g_strdupv (restart_command), + NULL, + NULL, + NULL); + if ( G_UNLIKELY (session->priv->client == NULL ) ) + { + g_warning ("Failed to connect to session manager"); + return; + } + + session->priv->managed = session_init (session->priv->client); + session->priv->client->die = xfpm_session_die; +} + +static void +xfpm_session_finalize (GObject *object) +{ + XfpmSession *session; + + session = XFPM_SESSION (object); + + if ( session->priv->client != NULL ) + client_session_free (session->priv->client); + + G_OBJECT_CLASS (xfpm_session_parent_class)->finalize (object); +} + +XfpmSession * +xfpm_session_new (void) +{ + if ( xfpm_session_object != NULL ) + { + g_object_ref (xfpm_session_object); + } + else + { + xfpm_session_object = g_object_new (XFPM_TYPE_SESSION, NULL); + g_object_add_weak_pointer (xfpm_session_object, &xfpm_session_object); + } + return XFPM_SESSION (xfpm_session_object); +} + +void xfpm_session_set_client_id (XfpmSession *session, const gchar *client_id) +{ + g_return_if_fail (XFPM_IS_SESSION (session)); + + if ( G_UNLIKELY (session->priv->client == NULL) ) + return; + + client_session_set_client_id (session->priv->client, client_id); +} diff --git a/src/xfpm-session.h b/src/xfpm-session.h new file mode 100644 index 00000000..be4d01d7 --- /dev/null +++ b/src/xfpm-session.h @@ -0,0 +1,57 @@ +/* + * * Copyright (C) 2009 Ali <aliov@xfce.org> + * + * Licensed under the GNU General Public License Version 2 + * + * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __XFPM_SESSION_H +#define __XFPM_SESSION_H + +#include <glib-object.h> + +G_BEGIN_DECLS + +#define XFPM_TYPE_SESSION (xfpm_session_get_type () ) +#define XFPM_SESSION(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), XFPM_TYPE_SESSION, XfpmSession)) +#define XFPM_IS_SESSION(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), XFPM_TYPE_SESSION)) + +typedef struct XfpmSessionPrivate XfpmSessionPrivate; + +typedef struct +{ + GObject parent; + XfpmSessionPrivate *priv; + +} XfpmSession; + +typedef struct +{ + GObjectClass parent_class; + + void (*session_die) (XfpmSession *session); + +} XfpmSessionClass; + +GType xfpm_session_get_type (void) G_GNUC_CONST; +XfpmSession *xfpm_session_new (void); + +void xfpm_session_set_client_id (XfpmSession *session, + const gchar *client_id); + +G_END_DECLS + +#endif /* __XFPM_SESSION_H */ -- GitLab