summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPerberos <[email protected]>2011-11-04 22:16:15 -0300
committerPerberos <[email protected]>2011-11-04 22:16:15 -0300
commitfff4ecc82f2bcfa7427596e7ad9c3769fcab040b (patch)
treeac4f1812a7991609c9c32c776daede2d8492f7b5 /src
downloadmate-screensaver-fff4ecc82f2bcfa7427596e7ad9c3769fcab040b.tar.bz2
mate-screensaver-fff4ecc82f2bcfa7427596e7ad9c3769fcab040b.tar.xz
first commit
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am268
-rw-r--r--src/copy-theme-dialog.c545
-rw-r--r--src/copy-theme-dialog.c.orig545
-rw-r--r--src/copy-theme-dialog.h56
-rw-r--r--src/copy-theme-dialog.h.orig56
-rwxr-xr-xsrc/debug-screensaver.sh42
-rw-r--r--src/gs-auth-helper.c213
-rw-r--r--src/gs-auth-helper.c.orig213
-rw-r--r--src/gs-auth-pam.c843
-rw-r--r--src/gs-auth-pam.c.orig843
-rw-r--r--src/gs-auth-pwent.c291
-rw-r--r--src/gs-auth-pwent.c.orig291
-rw-r--r--src/gs-auth.h67
-rw-r--r--src/gs-auth.h.orig65
-rw-r--r--src/gs-debug.c163
-rw-r--r--src/gs-debug.c.orig163
-rw-r--r--src/gs-debug.h71
-rw-r--r--src/gs-fade.c987
-rw-r--r--src/gs-fade.c.orig987
-rw-r--r--src/gs-fade.h77
-rw-r--r--src/gs-fade.h.orig77
-rw-r--r--src/gs-grab-x11.c672
-rw-r--r--src/gs-grab-x11.c.orig672
-rw-r--r--src/gs-grab.h79
-rw-r--r--src/gs-grab.h.orig79
-rw-r--r--src/gs-job.c567
-rw-r--r--src/gs-job.c.orig567
-rw-r--r--src/gs-job.h69
-rw-r--r--src/gs-job.h.orig69
-rw-r--r--src/gs-listener-dbus.c2272
-rw-r--r--src/gs-listener-dbus.c.orig2272
-rw-r--r--src/gs-listener-dbus.h89
-rw-r--r--src/gs-listener-dbus.h.orig89
-rw-r--r--src/gs-lock-plug.c2050
-rw-r--r--src/gs-lock-plug.c.orig2050
-rw-r--r--src/gs-lock-plug.h82
-rw-r--r--src/gs-lock-plug.h.orig82
-rw-r--r--src/gs-manager.c2030
-rw-r--r--src/gs-manager.c.orig2030
-rw-r--r--src/gs-manager.h107
-rw-r--r--src/gs-manager.h.orig107
-rw-r--r--src/gs-marshal.list4
-rw-r--r--src/gs-monitor.c528
-rw-r--r--src/gs-monitor.c.orig528
-rw-r--r--src/gs-monitor.h57
-rw-r--r--src/gs-monitor.h.orig57
-rw-r--r--src/gs-prefs.c995
-rw-r--r--src/gs-prefs.c.orig995
-rw-r--r--src/gs-prefs.h85
-rw-r--r--src/gs-prefs.h.orig85
-rw-r--r--src/gs-theme-manager.c453
-rw-r--r--src/gs-theme-manager.c.orig453
-rw-r--r--src/gs-theme-manager.h65
-rw-r--r--src/gs-theme-manager.h.orig65
-rw-r--r--src/gs-visual-gl.c116
-rw-r--r--src/gs-visual-gl.c.orig116
-rw-r--r--src/gs-visual-gl.h32
-rw-r--r--src/gs-watcher-x11.c680
-rw-r--r--src/gs-watcher-x11.c.orig680
-rw-r--r--src/gs-watcher.h65
-rw-r--r--src/gs-watcher.h.orig65
-rw-r--r--src/gs-window-x11.c2647
-rw-r--r--src/gs-window-x11.c.orig2647
-rw-r--r--src/gs-window.h106
-rw-r--r--src/gs-window.h.orig106
-rw-r--r--src/mate-screensaver-command.c551
-rw-r--r--src/mate-screensaver-command.c.orig551
-rw-r--r--src/mate-screensaver-dialog.c665
-rw-r--r--src/mate-screensaver-dialog.c.orig665
-rw-r--r--src/mate-screensaver-gl-helper.c83
-rw-r--r--src/mate-screensaver-gl-helper.c.orig83
-rw-r--r--src/mate-screensaver-preferences.c1781
-rw-r--r--src/mate-screensaver-preferences.c.orig1781
-rw-r--r--src/mate-screensaver.c132
-rw-r--r--src/mate-screensaver.c.orig132
-rw-r--r--src/mate-screensaver.desktop.in.in14
-rw-r--r--src/mate-screensaver.h32
-rw-r--r--src/setuid.c247
-rw-r--r--src/setuid.c.orig247
-rw-r--r--src/setuid.h25
-rw-r--r--src/setuid.h.orig25
-rw-r--r--src/subprocs.c166
-rw-r--r--src/subprocs.c.orig166
-rw-r--r--src/subprocs.h39
-rw-r--r--src/subprocs.h.orig39
-rw-r--r--src/test-fade.c134
-rw-r--r--src/test-fade.c.orig134
-rw-r--r--src/test-passwd.c288
-rw-r--r--src/test-passwd.c.orig288
-rw-r--r--src/test-watcher.c106
-rw-r--r--src/test-watcher.c.orig106
-rw-r--r--src/test-window.c180
-rw-r--r--src/test-window.c.orig180
93 files changed, 43367 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..1de819f
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,268 @@
+## We require new-style dependency handling.
+AUTOMAKE_OPTIONS = 1.7
+
+NULL =
+
+saverdir = $(libexecdir)/mate-screensaver
+themesdir = $(pkgdatadir)/themes
+
+INCLUDES = \
+ -I. \
+ -I$(srcdir) \
+ -DMATEMENU_I_KNOW_THIS_IS_UNSTABLE \
+ $(MATE_SCREENSAVER_CFLAGS) \
+ $(MATE_SCREENSAVER_DIALOG_CFLAGS) \
+ $(MATE_SCREENSAVER_CAPPLET_CFLAGS) \
+ $(DISABLE_DEPRECATED_CFLAGS) \
+ -DPREFIX=\""$(prefix)"\" \
+ -DBINDIR=\""$(bindir)"\" \
+ -DLIBDIR=\""$(libdir)"\" \
+ -DLIBEXECDIR=\""$(libexecdir)"\" \
+ -DDATADIR=\""$(datadir)"\" \
+ -DSYSCONFDIR=\""$(sysconfdir)"\" \
+ -DMATELOCALEDIR=\""$(datadir)/locale"\" \
+ -DSAVERDIR=\""$(saverdir)"\" \
+ -DTHEMESDIR=\""$(themesdir)"\" \
+ -DGTKBUILDERDIR=\"$(pkgdatadir)\" \
+ -DPAM_SERVICE_NAME=\""mate-screensaver"\" \
+ $(WARN_CFLAGS) \
+ $(AUTH_CFLAGS) \
+ $(DEBUG_CFLAGS) \
+ $(DBUS_CFLAGS) \
+ $(LIBMATEKBDUI_CFLAGS) \
+ $(LIBMATENOTIFY_CFLAGS) \
+ $(NULL)
+
+bin_PROGRAMS = \
+ mate-screensaver \
+ mate-screensaver-command \
+ mate-screensaver-preferences \
+ $(NULL)
+
+libexec_PROGRAMS = \
+ mate-screensaver-dialog \
+ mate-screensaver-gl-helper \
+ $(NULL)
+
+noinst_PROGRAMS = \
+ test-fade \
+ test-passwd \
+ test-watcher \
+ test-window \
+ $(NULL)
+
+desktopdir = $(sysconfdir)/xdg/autostart
+desktop_in_files = mate-screensaver.desktop.in
+desktop_DATA = $(desktop_in_files:.desktop.in=.desktop)
+
+mate_screensaver_command_SOURCES = \
+ mate-screensaver-command.c \
+ $(NULL)
+
+mate_screensaver_command_LDADD = \
+ $(MATE_SCREENSAVER_COMMAND_LIBS) \
+ $(NULL)
+
+AUTH_SOURCES = \
+ gs-auth.h \
+ $(NULL)
+
+test_fade_SOURCES = \
+ test-fade.c \
+ gs-fade.c \
+ gs-fade.h \
+ gs-debug.c \
+ gs-debug.h \
+ $(NULL)
+
+test_fade_LDADD = \
+ $(MATE_SCREENSAVER_LIBS) \
+ $(SAVER_LIBS) \
+ $(NULL)
+
+test_passwd_SOURCES = \
+ test-passwd.c \
+ $(AUTH_SOURCES) \
+ setuid.c \
+ setuid.h \
+ subprocs.c \
+ subprocs.h \
+ $(NULL)
+
+test_passwd_LDADD = \
+ $(MATE_SCREENSAVER_DIALOG_LIBS)\
+ $(AUTH_LIBS) \
+ $(NULL)
+
+test_watcher_SOURCES = \
+ test-watcher.c \
+ gs-watcher.h \
+ gs-watcher-x11.c \
+ gs-marshal.c \
+ gs-marshal.h \
+ gs-debug.c \
+ gs-debug.h \
+ $(NULL)
+
+test_watcher_LDADD = \
+ $(MATE_SCREENSAVER_LIBS) \
+ $(SAVER_LIBS) \
+ $(NULL)
+
+test_window_SOURCES = \
+ test-window.c \
+ gs-window.h \
+ gs-window-x11.c \
+ gs-grab-x11.c \
+ gs-grab.h \
+ gs-marshal.c \
+ gs-marshal.h \
+ gs-debug.c \
+ gs-debug.h \
+ subprocs.c \
+ subprocs.h \
+ $(NULL)
+
+test_window_LDADD = \
+ $(MATE_SCREENSAVER_LIBS) \
+ $(SAVER_LIBS) \
+ $(NULL)
+
+mate_screensaver_dialog_SOURCES = \
+ mate-screensaver-dialog.c \
+ gs-lock-plug.c \
+ gs-lock-plug.h \
+ gs-debug.c \
+ gs-debug.h \
+ setuid.c \
+ setuid.h \
+ subprocs.c \
+ subprocs.h \
+ $(AUTH_SOURCES) \
+ $(NULL)
+
+EXTRA_mate_screensaver_dialog_SOURCES = \
+ gs-auth-pam.c \
+ gs-auth-helper.c \
+ gs-auth-pwent.c \
+ $(NULL)
+
+mate_screensaver_dialog_LDADD = \
+ $(MATE_SCREENSAVER_DIALOG_LIBS)\
+ $(SAVER_LIBS) \
+ $(AUTH_LIBS) \
+ $(LIBMATEKBDUI_LIBS) \
+ $(LIBMATENOTIFY_LIBS) \
+ $(NULL)
+
+BUILT_SOURCES = \
+ gs-marshal.c \
+ gs-marshal.h \
+ $(NULL)
+
+gs-marshal.c: gs-marshal.list
+ echo "#include \"gs-marshal.h\"" > [email protected] && \
+ @[email protected] $< --prefix=gs_marshal --body >> [email protected]
+
+gs-marshal.h: gs-marshal.list
+ @[email protected] $< --prefix=gs_marshal --header > [email protected]
+
+mate_screensaver_SOURCES = \
+ mate-screensaver.c \
+ mate-screensaver.h \
+ gs-monitor.c \
+ gs-monitor.h \
+ gs-watcher-x11.c \
+ gs-watcher.h \
+ gs-listener-dbus.c \
+ gs-listener-dbus.h \
+ gs-manager.c \
+ gs-manager.h \
+ gs-window-x11.c \
+ gs-window.h \
+ gs-prefs.c \
+ gs-prefs.h \
+ gs-theme-manager.c \
+ gs-theme-manager.h \
+ gs-job.c \
+ gs-job.h \
+ gs-debug.c \
+ gs-debug.h \
+ subprocs.c \
+ subprocs.h \
+ gs-grab-x11.c \
+ gs-grab.h \
+ gs-fade.c \
+ gs-fade.h \
+ $(BUILT_SOURCES) \
+ $(NULL)
+
+mate_screensaver_gl_helper_LDADD = \
+ $(MATE_SCREENSAVER_LIBS) \
+ $(GL_LIBS) \
+ $(NULL)
+
+mate_screensaver_gl_helper_SOURCES = \
+ mate-screensaver-gl-helper.c \
+ gs-visual-gl.c \
+ gs-visual-gl.h \
+ $(NULL)
+
+mate_screensaver_LDADD = \
+ $(MATE_SCREENSAVER_LIBS) \
+ $(SAVER_LIBS) \
+ $(NULL)
+
+mate_screensaver_LDFLAGS = -export-dynamic
+
+mate_screensaver_preferences_SOURCES = \
+ mate-screensaver-preferences.c \
+ copy-theme-dialog.c \
+ copy-theme-dialog.h \
+ gs-theme-manager.c \
+ gs-theme-manager.h \
+ gs-job.c \
+ gs-job.h \
+ gs-debug.c \
+ gs-debug.h \
+ subprocs.c \
+ subprocs.h \
+ $(NULL)
+
+mate_screensaver_preferences_LDADD = \
+ $(MATE_SCREENSAVER_CAPPLET_LIBS) \
+ $(NULL)
+
+EXTRA_DIST = \
+ debug-screensaver.sh \
+ gs-marshal.list \
+ mate-screensaver.desktop.in \
+ $(NULL)
+
+CLEANFILES = \
+ $(desktop_DATA) \
+ mate-screensaver.desktop.in \
+ $(BUILT_SOURCES)
+
+MAINTAINERCLEANFILES = \
+ *~ \
+ Makefile.in
+
+install-exec-hook:
+ @if [ "[email protected][email protected]" = "xyes" ]; then \
+ echo "***" ; \
+ echo "*** Warning: mate-screensaver has been compiled with support for" ; \
+ echo "*** shadow passwords. If your system actually uses shadow" ; \
+ echo "*** passwords then it must be installed as a setuid root" ; \
+ echo "*** program in order for locking to work. To do this, you" ; \
+ echo "*** must run:" ; \
+ echo "***" ; \
+ echo "*** chown root $(DESTDIR)$(libexecdir)/mate-screensaver-dialog" ; \
+ echo "*** chmod +s $(DESTDIR)$(libexecdir)/mate-screensaver-dialog" ; \
+ echo "***" ; \
+ echo "*** For now, it will be installed non-setuid, which" ; \
+ echo "*** means that locking might not work." ; \
+ echo "***" ; \
+ fi
diff --git a/src/copy-theme-dialog.c b/src/copy-theme-dialog.c
new file mode 100644
index 0000000..ed7808d
--- /dev/null
+++ b/src/copy-theme-dialog.c
@@ -0,0 +1,545 @@
+/* copy-theme-dialog.c
+ * Copyright (C) 2008 John Millikin <[email protected]>
+ *
+ * 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, 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.
+**/
+
+#include "config.h"
+
+#include <limits.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+#include "copy-theme-dialog.h"
+
+static void
+copy_theme_dialog_class_init (CopyThemeDialogClass *klass);
+static void
+copy_theme_dialog_init (CopyThemeDialog *dlg);
+static void
+add_file_to_dialog (gpointer data, gpointer user_data);
+static void
+single_copy_complete (GObject *source_object, GAsyncResult *res,
+ gpointer user_data);
+static void
+copy_theme_dialog_copy_next (CopyThemeDialog *dialog);
+static void
+copy_theme_dialog_cancel (CopyThemeDialog *dialog);
+static void
+copy_theme_dialog_finalize (GObject *obj);
+static void
+copy_theme_dialog_update_num_files (CopyThemeDialog *dlg);
+static void
+copy_theme_dialog_response (GtkDialog *dialog, gint response_id);
+static void
+eel_gtk_label_make_bold (GtkLabel *label);
+static void
+create_titled_label (GtkTable *table,
+ int row,
+ GtkWidget **title_widget,
+ GtkWidget **label_text_widget);
+
+static GObjectClass *parent_class = NULL;
+
+enum
+{
+ CANCELLED = 0,
+ COMPLETE,
+ SIGNAL_COUNT
+};
+
+struct _CopyThemeDialogPrivate
+{
+ GtkWidget *progress;
+ GtkWidget *status;
+ GtkWidget *current;
+ GtkWidget *from;
+ GtkWidget *to;
+
+ GFile *theme_dir;
+ GSList *all_files, *file;
+ GSList *all_basenames, *basename;
+ guint index;
+ guint total_files;
+ GCancellable *cancellable;
+};
+
+guint signals[SIGNAL_COUNT] = {0, 0};
+
+GType
+copy_theme_dialog_get_type (void)
+{
+ static GType copy_theme_dialog_type = 0;
+
+ if (!copy_theme_dialog_type)
+ {
+ static GTypeInfo copy_theme_dialog_info =
+ {
+ sizeof (CopyThemeDialogClass),
+ NULL, /* GBaseInitFunc */
+ NULL, /* GBaseFinalizeFunc */
+ (GClassInitFunc) copy_theme_dialog_class_init,
+ NULL, /* GClassFinalizeFunc */
+ NULL, /* data */
+ sizeof (CopyThemeDialog),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) copy_theme_dialog_init,
+ NULL
+ };
+
+ copy_theme_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
+ "CopyThemeDialog",
+ &copy_theme_dialog_info,
+ 0);
+ }
+
+ return copy_theme_dialog_type;
+}
+
+static void
+copy_theme_dialog_class_init (CopyThemeDialogClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (CopyThemeDialogPrivate));
+
+ klass->cancelled = copy_theme_dialog_cancel;
+ object_class->finalize = copy_theme_dialog_finalize;
+
+ GTK_DIALOG_CLASS (klass)->response = copy_theme_dialog_response;
+
+ signals[CANCELLED] = g_signal_new ("cancelled",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (CopyThemeDialogClass, cancelled),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ signals[COMPLETE] = g_signal_new ("complete",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (CopyThemeDialogClass, complete),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ parent_class = g_type_class_peek_parent (klass);
+}
+
+GtkWidget*
+copy_theme_dialog_new (GList *files)
+{
+ GtkWidget *dialog;
+ CopyThemeDialogPrivate *priv;
+
+ dialog = GTK_WIDGET (g_object_new (COPY_THEME_DIALOG_TYPE, NULL));
+ priv = COPY_THEME_DIALOG (dialog)->priv;
+ priv->index = 0;
+ priv->total_files = 0;
+ priv->all_files = NULL;
+ priv->all_basenames = NULL;
+
+ g_list_foreach (files, add_file_to_dialog, dialog);
+
+ priv->file = priv->all_files;
+ priv->basename = priv->all_basenames;
+
+ return dialog;
+}
+
+static gboolean
+copy_finished (CopyThemeDialog *dialog)
+{
+ return (g_cancellable_is_cancelled (dialog->priv->cancellable) ||
+ dialog->priv->file == NULL);
+}
+
+static void
+copy_theme_dialog_init (CopyThemeDialog *dlg)
+{
+ GtkWidget *vbox;
+ GtkWidget *hbox;
+ GtkWidget *progress_vbox;
+ GtkWidget *table;
+ GtkWidget *label;
+ char *markup;
+ gchar *theme_dir_path;
+
+ dlg->priv = G_TYPE_INSTANCE_GET_PRIVATE (dlg, COPY_THEME_DIALOG_TYPE,
+ CopyThemeDialogPrivate);
+
+ /* Find and, if needed, create the directory for storing themes */
+ theme_dir_path = g_build_filename (g_get_user_data_dir (),
+ "applications", "screensavers",
+ NULL);
+ dlg->priv->theme_dir = g_file_new_for_path (theme_dir_path);
+ g_mkdir_with_parents (theme_dir_path, S_IRWXU);
+ g_free (theme_dir_path);
+
+ /* For cancelling async I/O operations */
+ dlg->priv->cancellable = g_cancellable_new ();
+
+ /* GUI settings */
+ gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dlg)->vbox),
+ 4);
+ gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dlg)->vbox), 4);
+
+ vbox = gtk_vbox_new (FALSE, 6);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
+ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
+
+ dlg->priv->status = gtk_label_new ("");
+ markup = g_strdup_printf ("<big><b>%s</b></big>", _("Copying files"));
+ gtk_label_set_markup (GTK_LABEL (dlg->priv->status), markup);
+ g_free (markup);
+
+ gtk_misc_set_alignment (GTK_MISC (dlg->priv->status), 0.0, 0.0);
+
+ gtk_box_pack_start (GTK_BOX (vbox), dlg->priv->status, FALSE, FALSE, 0);
+
+ hbox = gtk_hbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
+
+ table = gtk_table_new (2, 2, FALSE);
+ gtk_table_set_row_spacings (GTK_TABLE (table), 4);
+ gtk_table_set_col_spacings (GTK_TABLE (table), 4);
+
+ create_titled_label (GTK_TABLE (table), 0,
+ &label,
+ &dlg->priv->from);
+ gtk_label_set_text (GTK_LABEL (label), _("From:"));
+ create_titled_label (GTK_TABLE (table), 1,
+ &label,
+ &dlg->priv->to);
+ gtk_label_set_text (GTK_LABEL (label), _("To:"));
+
+ gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (table), FALSE, FALSE, 0);
+
+ progress_vbox = gtk_vbox_new (TRUE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), progress_vbox, FALSE, FALSE, 0);
+
+ dlg->priv->progress = gtk_progress_bar_new ();
+ gtk_box_pack_start (GTK_BOX (progress_vbox),
+ dlg->priv->progress, FALSE, FALSE, 0);
+
+ dlg->priv->current = gtk_label_new ("");
+ gtk_box_pack_start (GTK_BOX (progress_vbox),
+ dlg->priv->current, FALSE, FALSE, 0);
+ gtk_misc_set_alignment (GTK_MISC (dlg->priv->current), 0.0, 0.5);
+
+ gtk_dialog_add_button (GTK_DIALOG (dlg),
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
+
+ gtk_window_set_title (GTK_WINDOW (dlg),
+ _("Copying themes"));
+ gtk_dialog_set_has_separator (GTK_DIALOG (dlg), FALSE);
+ gtk_container_set_border_width (GTK_CONTAINER (dlg), 6);
+
+ gtk_widget_show_all (GTK_DIALOG (dlg)->vbox);
+}
+
+static void
+add_file_to_dialog (gpointer data, gpointer user_data)
+{
+ CopyThemeDialogPrivate *priv;
+ GFile *file;
+ gchar *basename = NULL, *raw_basename;
+
+ priv = COPY_THEME_DIALOG (user_data)->priv;
+ file = G_FILE (data);
+
+ raw_basename = g_file_get_basename (file);
+ if (g_str_has_suffix (raw_basename, ".desktop"))
+ {
+ /* FIXME: validate key file? */
+ basename = g_strndup (raw_basename,
+ /* 8 = strlen (".desktop") */
+ strlen (raw_basename) - 8);
+ }
+ g_free (raw_basename);
+
+ if (basename)
+ {
+ g_object_ref (file);
+ priv->all_files = g_slist_append (priv->all_files, file);
+ priv->all_basenames = g_slist_append (priv->all_basenames, basename);
+ priv->total_files++;
+ }
+
+ else
+ {
+ GtkWidget *dialog;
+ gchar *uri;
+
+ dialog = gtk_message_dialog_new (GTK_WINDOW (user_data),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_OK,
+ _("Invalid screensaver theme"));
+ uri = g_file_get_uri (file);
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ _("%s does not appear to be a valid screensaver theme."),
+ uri);
+ g_free (uri);
+ gtk_window_set_title (GTK_WINDOW (dialog), "");
+ gtk_window_set_icon_name (GTK_WINDOW (dialog), "preferences-desktop-screensaver");
+
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ }
+}
+
+static void
+single_copy_complete (GObject *source_object, GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ gboolean should_continue = FALSE;
+ CopyThemeDialog *dialog = COPY_THEME_DIALOG (user_data);
+
+ if (g_file_copy_finish (G_FILE (source_object), res, &error))
+ {
+ should_continue = TRUE;
+ }
+
+ else
+ {
+ /* If the file already exists, generate a new random name
+ * and try again.
+ **/
+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+ {
+ GFile *file, *destination;
+ gchar *basename, *full_basename;
+ g_error_free (error);
+
+ file = G_FILE (dialog->priv->file->data);
+ basename = (gchar *) (dialog->priv->basename->data);
+
+ g_return_if_fail (file != NULL);
+ g_return_if_fail (basename != NULL);
+
+ full_basename = g_strdup_printf ("%s-%u.desktop",
+ basename,
+ g_random_int ());
+ destination = g_file_get_child (dialog->priv->theme_dir,
+ full_basename);
+ g_free (full_basename);
+
+ g_file_copy_async (file, destination, G_FILE_COPY_NONE,
+ G_PRIORITY_DEFAULT,
+ dialog->priv->cancellable,
+ NULL, NULL,
+ single_copy_complete, dialog);
+ }
+
+ else
+ {
+ if (g_error_matches (error, G_IO_ERROR,
+ G_IO_ERROR_CANCELLED))
+ {
+ /* User has cancelled the theme copy */
+ g_signal_emit (G_OBJECT (dialog),
+ signals[CANCELLED],
+ 0, NULL);
+ }
+
+ else
+ {
+ /* Some other error occurred, ignore and
+ * try to copy remaining files
+ **/
+ should_continue = TRUE;
+ }
+
+ g_error_free (error);
+ }
+ }
+
+ /* Update informational widgets and, if needed, signal
+ * copy completion.
+ **/
+ if (should_continue)
+ {
+ dialog->priv->index++;
+ dialog->priv->file = dialog->priv->file->next;
+ dialog->priv->basename = dialog->priv->basename->next;
+ copy_theme_dialog_update_num_files (dialog);
+ copy_theme_dialog_copy_next (dialog);
+ }
+}
+
+/* Try to copy the theme file to the user's screensaver directory.
+ * If a file with the given name already exists, the error will be
+ * caught later and the copy re-attempted with a random value
+ * appended to the filename.
+**/
+static void
+copy_theme_dialog_copy_next (CopyThemeDialog *dialog)
+{
+ GFile *file, *destination;
+ gchar *basename, *full_basename;
+
+ if (copy_finished (dialog))
+ {
+ g_signal_emit (G_OBJECT (dialog), signals[COMPLETE],
+ 0, NULL);
+ return;
+ }
+
+ file = G_FILE (dialog->priv->file->data);
+ basename = (gchar *) (dialog->priv->basename->data);
+
+ g_return_if_fail (file != NULL);
+ g_return_if_fail (basename != NULL);
+
+ full_basename = g_strdup_printf ("%s.desktop", basename);
+ destination = g_file_get_child (dialog->priv->theme_dir, full_basename);
+ g_free (full_basename);
+
+ g_file_copy_async (file, destination, G_FILE_COPY_NONE,
+ G_PRIORITY_DEFAULT, dialog->priv->cancellable,
+ NULL, NULL, single_copy_complete, dialog);
+}
+
+static gboolean
+timeout_display_dialog (gpointer data)
+{
+ if (IS_COPY_THEME_DIALOG (data))
+ {
+ CopyThemeDialog *dialog = COPY_THEME_DIALOG (data);
+ if (!copy_finished (dialog))
+ {
+ gtk_widget_show (GTK_WIDGET (dialog));
+
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (copy_theme_dialog_response),
+ dialog);
+ }
+ }
+ return FALSE;
+}
+
+void
+copy_theme_dialog_begin (CopyThemeDialog *dialog)
+{
+ gtk_widget_hide (GTK_WIDGET (dialog));
+
+ /* If the copy operation takes more than half a second to
+ * complete, display the dialog.
+ **/
+ g_timeout_add (500, timeout_display_dialog, dialog);
+
+ copy_theme_dialog_copy_next (dialog);
+}
+
+static void
+copy_theme_dialog_cancel (CopyThemeDialog *dialog)
+{
+ g_cancellable_cancel (dialog->priv->cancellable);
+}
+
+static void
+copy_theme_dialog_finalize (GObject *obj)
+{
+ CopyThemeDialog *dlg = COPY_THEME_DIALOG (obj);
+
+ g_object_unref (dlg->priv->theme_dir);
+ g_slist_foreach (dlg->priv->all_files, (GFunc) (g_object_unref), NULL);
+ g_slist_free (dlg->priv->all_files);
+ g_slist_foreach (dlg->priv->all_basenames, (GFunc) (g_free), NULL);
+ g_slist_free (dlg->priv->all_basenames);
+ g_object_unref (dlg->priv->cancellable);
+
+ if (parent_class->finalize)
+ parent_class->finalize (G_OBJECT (dlg));
+}
+
+static void
+copy_theme_dialog_update_num_files (CopyThemeDialog *dlg)
+{
+ gchar *str = g_strdup_printf (_("Copying file: %u of %u"),
+ dlg->priv->index, dlg->priv->total_files);
+ gtk_progress_bar_set_text (GTK_PROGRESS_BAR (dlg->priv->progress), str);
+ g_free (str);
+}
+
+static void
+copy_theme_dialog_response (GtkDialog *dialog, gint response_id)
+{
+ g_cancellable_cancel (COPY_THEME_DIALOG (dialog)->priv->cancellable);
+}
+
+/**
+ * eel_gtk_label_make_bold.
+ *
+ * Switches the font of label to a bold equivalent.
+ * @label: The label.
+ **/
+static void
+eel_gtk_label_make_bold (GtkLabel *label)
+{
+ PangoFontDescription *font_desc;
+
+ font_desc = pango_font_description_new ();
+
+ pango_font_description_set_weight (font_desc,
+ PANGO_WEIGHT_BOLD);
+
+ /* This will only affect the weight of the font, the rest is
+ * from the current state of the widget, which comes from the
+ * theme or user prefs, since the font desc only has the
+ * weight flag turned on.
+ */
+ gtk_widget_modify_font (GTK_WIDGET (label), font_desc);
+
+ pango_font_description_free (font_desc);
+}
+
+/* from caja */
+static void
+create_titled_label (GtkTable *table,
+ int row,
+ GtkWidget **title_widget,
+ GtkWidget **label_text_widget)
+{
+ *title_widget = gtk_label_new ("");
+ eel_gtk_label_make_bold (GTK_LABEL (*title_widget));
+ gtk_misc_set_alignment (GTK_MISC (*title_widget), 1, 0);
+ gtk_table_attach (table, *title_widget,
+ 0, 1,
+ row, row + 1,
+ GTK_FILL, 0,
+ 0, 0);
+ gtk_widget_show (*title_widget);
+
+ *label_text_widget = gtk_label_new ("");
+ gtk_label_set_ellipsize (GTK_LABEL (*label_text_widget), PANGO_ELLIPSIZE_END);
+ gtk_table_attach (table, *label_text_widget,
+ 1, 2,
+ row, row + 1,
+ GTK_FILL | GTK_EXPAND, 0,
+ 0, 0);
+ gtk_widget_show (*label_text_widget);
+ gtk_misc_set_alignment (GTK_MISC (*label_text_widget), 0, 0);
+}
diff --git a/src/copy-theme-dialog.c.orig b/src/copy-theme-dialog.c.orig
new file mode 100644
index 0000000..d8150eb
--- /dev/null
+++ b/src/copy-theme-dialog.c.orig
@@ -0,0 +1,545 @@
+/* copy-theme-dialog.c
+ * Copyright (C) 2008 John Millikin <[email protected]>
+ *
+ * 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, 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.
+**/
+
+#include "config.h"
+
+#include <limits.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+#include "copy-theme-dialog.h"
+
+static void
+copy_theme_dialog_class_init (CopyThemeDialogClass *klass);
+static void
+copy_theme_dialog_init (CopyThemeDialog *dlg);
+static void
+add_file_to_dialog (gpointer data, gpointer user_data);
+static void
+single_copy_complete (GObject *source_object, GAsyncResult *res,
+ gpointer user_data);
+static void
+copy_theme_dialog_copy_next (CopyThemeDialog *dialog);
+static void
+copy_theme_dialog_cancel (CopyThemeDialog *dialog);
+static void
+copy_theme_dialog_finalize (GObject *obj);
+static void
+copy_theme_dialog_update_num_files (CopyThemeDialog *dlg);
+static void
+copy_theme_dialog_response (GtkDialog *dialog, gint response_id);
+static void
+eel_gtk_label_make_bold (GtkLabel *label);
+static void
+create_titled_label (GtkTable *table,
+ int row,
+ GtkWidget **title_widget,
+ GtkWidget **label_text_widget);
+
+static GObjectClass *parent_class = NULL;
+
+enum
+{
+ CANCELLED = 0,
+ COMPLETE,
+ SIGNAL_COUNT
+};
+
+struct _CopyThemeDialogPrivate
+{
+ GtkWidget *progress;
+ GtkWidget *status;
+ GtkWidget *current;
+ GtkWidget *from;
+ GtkWidget *to;
+
+ GFile *theme_dir;
+ GSList *all_files, *file;
+ GSList *all_basenames, *basename;
+ guint index;
+ guint total_files;
+ GCancellable *cancellable;
+};
+
+guint signals[SIGNAL_COUNT] = {0, 0};
+
+GType
+copy_theme_dialog_get_type (void)
+{
+ static GType copy_theme_dialog_type = 0;
+
+ if (!copy_theme_dialog_type)
+ {
+ static GTypeInfo copy_theme_dialog_info =
+ {
+ sizeof (CopyThemeDialogClass),
+ NULL, /* GBaseInitFunc */
+ NULL, /* GBaseFinalizeFunc */
+ (GClassInitFunc) copy_theme_dialog_class_init,
+ NULL, /* GClassFinalizeFunc */
+ NULL, /* data */
+ sizeof (CopyThemeDialog),
+ 0, /* n_preallocs */
+ (GInstanceInitFunc) copy_theme_dialog_init,
+ NULL
+ };
+
+ copy_theme_dialog_type = g_type_register_static (GTK_TYPE_DIALOG,
+ "CopyThemeDialog",
+ &copy_theme_dialog_info,
+ 0);
+ }
+
+ return copy_theme_dialog_type;
+}
+
+static void
+copy_theme_dialog_class_init (CopyThemeDialogClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ g_type_class_add_private (klass, sizeof (CopyThemeDialogPrivate));
+
+ klass->cancelled = copy_theme_dialog_cancel;
+ object_class->finalize = copy_theme_dialog_finalize;
+
+ GTK_DIALOG_CLASS (klass)->response = copy_theme_dialog_response;
+
+ signals[CANCELLED] = g_signal_new ("cancelled",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (CopyThemeDialogClass, cancelled),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ signals[COMPLETE] = g_signal_new ("complete",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (CopyThemeDialogClass, complete),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ parent_class = g_type_class_peek_parent (klass);
+}
+
+GtkWidget*
+copy_theme_dialog_new (GList *files)
+{
+ GtkWidget *dialog;
+ CopyThemeDialogPrivate *priv;
+
+ dialog = GTK_WIDGET (g_object_new (COPY_THEME_DIALOG_TYPE, NULL));
+ priv = COPY_THEME_DIALOG (dialog)->priv;
+ priv->index = 0;
+ priv->total_files = 0;
+ priv->all_files = NULL;
+ priv->all_basenames = NULL;
+
+ g_list_foreach (files, add_file_to_dialog, dialog);
+
+ priv->file = priv->all_files;
+ priv->basename = priv->all_basenames;
+
+ return dialog;
+}
+
+static gboolean
+copy_finished (CopyThemeDialog *dialog)
+{
+ return (g_cancellable_is_cancelled (dialog->priv->cancellable) ||
+ dialog->priv->file == NULL);
+}
+
+static void
+copy_theme_dialog_init (CopyThemeDialog *dlg)
+{
+ GtkWidget *vbox;
+ GtkWidget *hbox;
+ GtkWidget *progress_vbox;
+ GtkWidget *table;
+ GtkWidget *label;
+ char *markup;
+ gchar *theme_dir_path;
+
+ dlg->priv = G_TYPE_INSTANCE_GET_PRIVATE (dlg, COPY_THEME_DIALOG_TYPE,
+ CopyThemeDialogPrivate);
+
+ /* Find and, if needed, create the directory for storing themes */
+ theme_dir_path = g_build_filename (g_get_user_data_dir (),
+ "applications", "screensavers",
+ NULL);
+ dlg->priv->theme_dir = g_file_new_for_path (theme_dir_path);
+ g_mkdir_with_parents (theme_dir_path, S_IRWXU);
+ g_free (theme_dir_path);
+
+ /* For cancelling async I/O operations */
+ dlg->priv->cancellable = g_cancellable_new ();
+
+ /* GUI settings */
+ gtk_container_set_border_width (GTK_CONTAINER (GTK_DIALOG (dlg)->vbox),
+ 4);
+ gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dlg)->vbox), 4);
+
+ vbox = gtk_vbox_new (FALSE, 6);
+ gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
+ gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox, TRUE, TRUE, 0);
+
+ dlg->priv->status = gtk_label_new ("");
+ markup = g_strdup_printf ("<big><b>%s</b></big>", _("Copying files"));
+ gtk_label_set_markup (GTK_LABEL (dlg->priv->status), markup);
+ g_free (markup);
+
+ gtk_misc_set_alignment (GTK_MISC (dlg->priv->status), 0.0, 0.0);
+
+ gtk_box_pack_start (GTK_BOX (vbox), dlg->priv->status, FALSE, FALSE, 0);
+
+ hbox = gtk_hbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), hbox, TRUE, TRUE, 0);
+
+ table = gtk_table_new (2, 2, FALSE);
+ gtk_table_set_row_spacings (GTK_TABLE (table), 4);
+ gtk_table_set_col_spacings (GTK_TABLE (table), 4);
+
+ create_titled_label (GTK_TABLE (table), 0,
+ &label,
+ &dlg->priv->from);
+ gtk_label_set_text (GTK_LABEL (label), _("From:"));
+ create_titled_label (GTK_TABLE (table), 1,
+ &label,
+ &dlg->priv->to);
+ gtk_label_set_text (GTK_LABEL (label), _("To:"));
+
+ gtk_box_pack_start (GTK_BOX (vbox), GTK_WIDGET (table), FALSE, FALSE, 0);
+
+ progress_vbox = gtk_vbox_new (TRUE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), progress_vbox, FALSE, FALSE, 0);
+
+ dlg->priv->progress = gtk_progress_bar_new ();
+ gtk_box_pack_start (GTK_BOX (progress_vbox),
+ dlg->priv->progress, FALSE, FALSE, 0);
+
+ dlg->priv->current = gtk_label_new ("");
+ gtk_box_pack_start (GTK_BOX (progress_vbox),
+ dlg->priv->current, FALSE, FALSE, 0);
+ gtk_misc_set_alignment (GTK_MISC (dlg->priv->current), 0.0, 0.5);
+
+ gtk_dialog_add_button (GTK_DIALOG (dlg),
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
+
+ gtk_window_set_title (GTK_WINDOW (dlg),
+ _("Copying themes"));
+ gtk_dialog_set_has_separator (GTK_DIALOG (dlg), FALSE);
+ gtk_container_set_border_width (GTK_CONTAINER (dlg), 6);
+
+ gtk_widget_show_all (GTK_DIALOG (dlg)->vbox);
+}
+
+static void
+add_file_to_dialog (gpointer data, gpointer user_data)
+{
+ CopyThemeDialogPrivate *priv;
+ GFile *file;
+ gchar *basename = NULL, *raw_basename;
+
+ priv = COPY_THEME_DIALOG (user_data)->priv;
+ file = G_FILE (data);
+
+ raw_basename = g_file_get_basename (file);
+ if (g_str_has_suffix (raw_basename, ".desktop"))
+ {
+ /* FIXME: validate key file? */
+ basename = g_strndup (raw_basename,
+ /* 8 = strlen (".desktop") */
+ strlen (raw_basename) - 8);
+ }
+ g_free (raw_basename);
+
+ if (basename)
+ {
+ g_object_ref (file);
+ priv->all_files = g_slist_append (priv->all_files, file);
+ priv->all_basenames = g_slist_append (priv->all_basenames, basename);
+ priv->total_files++;
+ }
+
+ else
+ {
+ GtkWidget *dialog;
+ gchar *uri;
+
+ dialog = gtk_message_dialog_new (GTK_WINDOW (user_data),
+ GTK_DIALOG_MODAL,
+ GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_OK,
+ _("Invalid screensaver theme"));
+ uri = g_file_get_uri (file);
+ gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+ _("%s does not appear to be a valid screensaver theme."),
+ uri);
+ g_free (uri);
+ gtk_window_set_title (GTK_WINDOW (dialog), "");
+ gtk_window_set_icon_name (GTK_WINDOW (dialog), "preferences-desktop-screensaver");
+
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ }
+}
+
+static void
+single_copy_complete (GObject *source_object, GAsyncResult *res,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ gboolean should_continue = FALSE;
+ CopyThemeDialog *dialog = COPY_THEME_DIALOG (user_data);
+
+ if (g_file_copy_finish (G_FILE (source_object), res, &error))
+ {
+ should_continue = TRUE;
+ }
+
+ else
+ {
+ /* If the file already exists, generate a new random name
+ * and try again.
+ **/
+ if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
+ {
+ GFile *file, *destination;
+ gchar *basename, *full_basename;
+ g_error_free (error);
+
+ file = G_FILE (dialog->priv->file->data);
+ basename = (gchar *) (dialog->priv->basename->data);
+
+ g_return_if_fail (file != NULL);
+ g_return_if_fail (basename != NULL);
+
+ full_basename = g_strdup_printf ("%s-%u.desktop",
+ basename,
+ g_random_int ());
+ destination = g_file_get_child (dialog->priv->theme_dir,
+ full_basename);
+ g_free (full_basename);
+
+ g_file_copy_async (file, destination, G_FILE_COPY_NONE,
+ G_PRIORITY_DEFAULT,
+ dialog->priv->cancellable,
+ NULL, NULL,
+ single_copy_complete, dialog);
+ }
+
+ else
+ {
+ if (g_error_matches (error, G_IO_ERROR,
+ G_IO_ERROR_CANCELLED))
+ {
+ /* User has cancelled the theme copy */
+ g_signal_emit (G_OBJECT (dialog),
+ signals[CANCELLED],
+ 0, NULL);
+ }
+
+ else
+ {
+ /* Some other error occurred, ignore and
+ * try to copy remaining files
+ **/
+ should_continue = TRUE;
+ }
+
+ g_error_free (error);
+ }
+ }
+
+ /* Update informational widgets and, if needed, signal
+ * copy completion.
+ **/
+ if (should_continue)
+ {
+ dialog->priv->index++;
+ dialog->priv->file = dialog->priv->file->next;
+ dialog->priv->basename = dialog->priv->basename->next;
+ copy_theme_dialog_update_num_files (dialog);
+ copy_theme_dialog_copy_next (dialog);
+ }
+}
+
+/* Try to copy the theme file to the user's screensaver directory.
+ * If a file with the given name already exists, the error will be
+ * caught later and the copy re-attempted with a random value
+ * appended to the filename.
+**/
+static void
+copy_theme_dialog_copy_next (CopyThemeDialog *dialog)
+{
+ GFile *file, *destination;
+ gchar *basename, *full_basename;
+
+ if (copy_finished (dialog))
+ {
+ g_signal_emit (G_OBJECT (dialog), signals[COMPLETE],
+ 0, NULL);
+ return;
+ }
+
+ file = G_FILE (dialog->priv->file->data);
+ basename = (gchar *) (dialog->priv->basename->data);
+
+ g_return_if_fail (file != NULL);
+ g_return_if_fail (basename != NULL);
+
+ full_basename = g_strdup_printf ("%s.desktop", basename);
+ destination = g_file_get_child (dialog->priv->theme_dir, full_basename);
+ g_free (full_basename);
+
+ g_file_copy_async (file, destination, G_FILE_COPY_NONE,
+ G_PRIORITY_DEFAULT, dialog->priv->cancellable,
+ NULL, NULL, single_copy_complete, dialog);
+}
+
+static gboolean
+timeout_display_dialog (gpointer data)
+{
+ if (IS_COPY_THEME_DIALOG (data))
+ {
+ CopyThemeDialog *dialog = COPY_THEME_DIALOG (data);
+ if (!copy_finished (dialog))
+ {
+ gtk_widget_show (GTK_WIDGET (dialog));
+
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (copy_theme_dialog_response),
+ dialog);
+ }
+ }
+ return FALSE;
+}
+
+void
+copy_theme_dialog_begin (CopyThemeDialog *dialog)
+{
+ gtk_widget_hide (GTK_WIDGET (dialog));
+
+ /* If the copy operation takes more than half a second to
+ * complete, display the dialog.
+ **/
+ g_timeout_add (500, timeout_display_dialog, dialog);
+
+ copy_theme_dialog_copy_next (dialog);
+}
+
+static void
+copy_theme_dialog_cancel (CopyThemeDialog *dialog)
+{
+ g_cancellable_cancel (dialog->priv->cancellable);
+}
+
+static void
+copy_theme_dialog_finalize (GObject *obj)
+{
+ CopyThemeDialog *dlg = COPY_THEME_DIALOG (obj);
+
+ g_object_unref (dlg->priv->theme_dir);
+ g_slist_foreach (dlg->priv->all_files, (GFunc) (g_object_unref), NULL);
+ g_slist_free (dlg->priv->all_files);
+ g_slist_foreach (dlg->priv->all_basenames, (GFunc) (g_free), NULL);
+ g_slist_free (dlg->priv->all_basenames);
+ g_object_unref (dlg->priv->cancellable);
+
+ if (parent_class->finalize)
+ parent_class->finalize (G_OBJECT (dlg));
+}
+
+static void
+copy_theme_dialog_update_num_files (CopyThemeDialog *dlg)
+{
+ gchar *str = g_strdup_printf (_("Copying file: %u of %u"),
+ dlg->priv->index, dlg->priv->total_files);
+ gtk_progress_bar_set_text (GTK_PROGRESS_BAR (dlg->priv->progress), str);
+ g_free (str);
+}
+
+static void
+copy_theme_dialog_response (GtkDialog *dialog, gint response_id)
+{
+ g_cancellable_cancel (COPY_THEME_DIALOG (dialog)->priv->cancellable);
+}
+
+/**
+ * eel_gtk_label_make_bold.
+ *
+ * Switches the font of label to a bold equivalent.
+ * @label: The label.
+ **/
+static void
+eel_gtk_label_make_bold (GtkLabel *label)
+{
+ PangoFontDescription *font_desc;
+
+ font_desc = pango_font_description_new ();
+
+ pango_font_description_set_weight (font_desc,
+ PANGO_WEIGHT_BOLD);
+
+ /* This will only affect the weight of the font, the rest is
+ * from the current state of the widget, which comes from the
+ * theme or user prefs, since the font desc only has the
+ * weight flag turned on.
+ */
+ gtk_widget_modify_font (GTK_WIDGET (label), font_desc);
+
+ pango_font_description_free (font_desc);
+}
+
+/* from caja */
+static void
+create_titled_label (GtkTable *table,
+ int row,
+ GtkWidget **title_widget,
+ GtkWidget **label_text_widget)
+{
+ *title_widget = gtk_label_new ("");
+ eel_gtk_label_make_bold (GTK_LABEL (*title_widget));
+ gtk_misc_set_alignment (GTK_MISC (*title_widget), 1, 0);
+ gtk_table_attach (table, *title_widget,
+ 0, 1,
+ row, row + 1,
+ GTK_FILL, 0,
+ 0, 0);
+ gtk_widget_show (*title_widget);
+
+ *label_text_widget = gtk_label_new ("");
+ gtk_label_set_ellipsize (GTK_LABEL (*label_text_widget), PANGO_ELLIPSIZE_END);
+ gtk_table_attach (table, *label_text_widget,
+ 1, 2,
+ row, row + 1,
+ GTK_FILL | GTK_EXPAND, 0,
+ 0, 0);
+ gtk_widget_show (*label_text_widget);
+ gtk_misc_set_alignment (GTK_MISC (*label_text_widget), 0, 0);
+}
diff --git a/src/copy-theme-dialog.h b/src/copy-theme-dialog.h
new file mode 100644
index 0000000..6e72017
--- /dev/null
+++ b/src/copy-theme-dialog.h
@@ -0,0 +1,56 @@
+/* copy-theme-dialog.h
+ * Copyright (C) 2008 John Millikin <[email protected]>
+ *
+ * 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, 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 __COPY_THEME_DIALOG_H__
+#define __COPY_THEME_DIALOG_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define COPY_THEME_DIALOG_TYPE copy_theme_dialog_get_type ()
+#define COPY_THEME_DIALOG(obj) G_TYPE_CHECK_INSTANCE_CAST (obj, COPY_THEME_DIALOG_TYPE, CopyThemeDialog)
+#define COPY_THEME_DIALOG_CLASS(klass) G_TYPE_CHECK_CLASS_CAST (klass, COPY_THEME_DIALOG_TYPE, CopyThemeDialogClass)
+#define IS_COPY_THEME_DIALOG(obj) G_TYPE_CHECK_INSTANCE_TYPE (obj, COPY_THEME_DIALOG_TYPE)
+
+typedef struct _CopyThemeDialog CopyThemeDialog;
+typedef struct _CopyThemeDialogClass CopyThemeDialogClass;
+typedef struct _CopyThemeDialogPrivate CopyThemeDialogPrivate;
+
+struct _CopyThemeDialog
+{
+ GtkDialog dialog;
+ CopyThemeDialogPrivate *priv;
+};
+
+struct _CopyThemeDialogClass
+{
+ GtkDialogClass parent_class;
+
+ void (*cancelled) (CopyThemeDialog *dialog);
+ void (*complete) (CopyThemeDialog *dialog);
+};
+
+GType copy_theme_dialog_get_type (void);
+GtkWidget *copy_theme_dialog_new (GList *files);
+void copy_theme_dialog_begin (CopyThemeDialog *dialog);
+
+G_END_DECLS
+
+#endif /* __COPY_THEME_DIALOG_H__ */
diff --git a/src/copy-theme-dialog.h.orig b/src/copy-theme-dialog.h.orig
new file mode 100644
index 0000000..b1e66fd
--- /dev/null
+++ b/src/copy-theme-dialog.h.orig
@@ -0,0 +1,56 @@
+/* copy-theme-dialog.h
+ * Copyright (C) 2008 John Millikin <[email protected]>
+ *
+ * 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, 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 __COPY_THEME_DIALOG_H__
+#define __COPY_THEME_DIALOG_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define COPY_THEME_DIALOG_TYPE copy_theme_dialog_get_type ()
+#define COPY_THEME_DIALOG(obj) G_TYPE_CHECK_INSTANCE_CAST (obj, COPY_THEME_DIALOG_TYPE, CopyThemeDialog)
+#define COPY_THEME_DIALOG_CLASS(klass) G_TYPE_CHECK_CLASS_CAST (klass, COPY_THEME_DIALOG_TYPE, CopyThemeDialogClass)
+#define IS_COPY_THEME_DIALOG(obj) G_TYPE_CHECK_INSTANCE_TYPE (obj, COPY_THEME_DIALOG_TYPE)
+
+typedef struct _CopyThemeDialog CopyThemeDialog;
+typedef struct _CopyThemeDialogClass CopyThemeDialogClass;
+typedef struct _CopyThemeDialogPrivate CopyThemeDialogPrivate;
+
+struct _CopyThemeDialog
+{
+ GtkDialog dialog;
+ CopyThemeDialogPrivate *priv;
+};
+
+struct _CopyThemeDialogClass
+{
+ GtkDialogClass parent_class;
+
+ void (*cancelled) (CopyThemeDialog *dialog);
+ void (*complete) (CopyThemeDialog *dialog);
+};
+
+GType copy_theme_dialog_get_type (void);
+GtkWidget *copy_theme_dialog_new (GList *files);
+void copy_theme_dialog_begin (CopyThemeDialog *dialog);
+
+G_END_DECLS
+
+#endif /* __COPY_THEME_DIALOG_H__ */
diff --git a/src/debug-screensaver.sh b/src/debug-screensaver.sh
new file mode 100755
index 0000000..c8b1a93
--- /dev/null
+++ b/src/debug-screensaver.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+# This is probably linux only at the moment
+
+if [ -z "${DBUS_SESSION_BUS_ADDRESS}" ]; then
+ pid=`pgrep -u $USER "mate-session|x-session-manager"`
+ if [ "x$pid" != "x" ]; then
+ env_address=`(cat /proc/$pid/environ; echo) | tr "\000" "\n" | grep '^DBUS_SESSION_BUS_ADDRESS='`
+ env_display=`(cat /proc/$pid/environ; echo) | tr "\000" "\n" | grep '^DISPLAY='`
+ env_xdg_cookie=`(cat /proc/$pid/environ; echo) | tr "\000" "\n" | grep '^XDG_SESSION_COOKIE='`
+ env_path=`(cat /proc/$pid/environ; echo) | tr "\000" "\n" | grep '^PATH='`
+ if [ "x$env_address" != "x" ]; then
+ echo "Setting $env_address"
+ echo "Setting $env_display"
+ echo "Setting $env_path"
+ echo "Setting $env_xdg_cookie"
+ eval "export $env_address"
+ eval "export $env_display"
+ eval "export $env_path"
+ eval "export $env_xdg_cookie"
+ fi
+ fi
+fi
+
+if [ -z "${DBUS_SESSION_BUS_ADDRESS}" ]; then
+ echo "Could not determine DBUS_SESSION_BUS_ADDRESS"
+ exit 1
+fi
+
+export G_DEBUG=fatal_criticals
+
+# kill the existing daemon
+mate-screensaver-command --exit
+
+# run the daemon in the debugger
+#gdb --args mate-screensaver --no-daemon --debug --sync
+
+# or if that isn't helpful just get the debug output
+#mate-screensaver --no-daemon --debug > /tmp/gs-debug-log.txt 2>&1
+
+# or just run it with debugging on
+mate-screensaver --no-daemon --debug
diff --git a/src/gs-auth-helper.c b/src/gs-auth-helper.c
new file mode 100644
index 0000000..ea9aefe
--- /dev/null
+++ b/src/gs-auth-helper.c
@@ -0,0 +1,213 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * written by Olaf Kirch <[email protected]>
+ * xscreensaver, Copyright (c) 1993-2004 Jamie Zawinski <[email protected]>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation. No representations are made about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ */
+
+/* The idea here is to be able to run mate-screensaver-dialog without any setuid bits.
+ * Password verification happens through an external program that you feed
+ * your password to on stdin. The external command is invoked with a user
+ * name argument.
+ *
+ * The external helper does whatever authentication is necessary. Currently,
+ * SuSE uses "unix2_chkpwd", which is a variation of "unix_chkpwd" from the
+ * PAM distribution.
+ *
+ * Normally, the password helper should just authenticate the calling user
+ * (i.e. based on the caller's real uid). This is in order to prevent
+ * brute-forcing passwords in a shadow environment. A less restrictive
+ * approach would be to allow verifying other passwords as well, but always
+ * with a 2 second delay or so. (Not sure what SuSE's "unix2_chkpwd"
+ * currently does.)
+ * -- Olaf Kirch <[email protected]>, 16-Dec-2003
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/wait.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "gs-auth.h"
+#include "subprocs.h"
+
+static gboolean verbose_enabled = FALSE;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static gboolean
+ext_run (const char *user,
+ const char *typed_passwd,
+ gboolean verbose)
+{
+ int pfd[2], status;
+ pid_t pid;
+
+ if (pipe (pfd) < 0)
+ {
+ return 0;
+ }
+
+ if (verbose)
+ {
+ g_message ("ext_run (%s, %s)",
+ PASSWD_HELPER_PROGRAM, user);
+ }
+
+ block_sigchld ();
+
+ if ((pid = fork ()) < 0)
+ {
+ close (pfd [0]);
+ close (pfd [1]);
+ return FALSE;
+ }
+
+ if (pid == 0)
+ {
+ close (pfd [1]);
+ if (pfd [0] != 0)
+ {
+ dup2 (pfd [0], 0);
+ }
+
+ /* Helper is invoked as helper service-name [user] */
+ execlp (PASSWD_HELPER_PROGRAM, PASSWD_HELPER_PROGRAM, "mate-screensaver", user, NULL);
+ if (verbose)
+ {
+ g_message ("%s: %s", PASSWD_HELPER_PROGRAM, g_strerror (errno));
+ }
+
+ exit (1);
+ }
+
+ close (pfd [0]);
+
+ /* Write out password to helper process */
+ if (!typed_passwd)
+ {
+ typed_passwd = "";
+ }
+ write (pfd [1], typed_passwd, strlen (typed_passwd));
+ close (pfd [1]);
+
+ while (waitpid (pid, &status, 0) < 0)
+ {
+ if (errno == EINTR)
+ {
+ continue;
+ }
+
+ if (verbose)
+ {
+ g_message ("ext_run: waitpid failed: %s\n",
+ g_strerror (errno));
+ }
+
+ unblock_sigchld ();
+ return FALSE;
+ }
+
+ unblock_sigchld ();
+
+ if (! WIFEXITED (status) || WEXITSTATUS (status) != 0)
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ gboolean res = FALSE;
+ char *password;
+
+ password = NULL;
+
+ /* ask for the password for user */
+ if (func != NULL)
+ {
+ func (GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ "Password: ",
+ &password,
+ data);
+ }
+
+ if (password == NULL)
+ {
+ return FALSE;
+ }
+
+ res = ext_run (username, password, gs_auth_get_verbose ());
+
+ return res;
+}
+
+gboolean
+gs_auth_init (void)
+{
+ return TRUE;
+}
+
+gboolean
+gs_auth_priv_init (void)
+{
+ /* Make sure the passwd helper exists */
+ if (g_access (PASSWD_HELPER_PROGRAM, X_OK) < 0)
+ {
+ g_warning ("%s does not exist. "
+ "password authentication via "
+ "external helper will not work.",
+ PASSWD_HELPER_PROGRAM);
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/src/gs-auth-helper.c.orig b/src/gs-auth-helper.c.orig
new file mode 100644
index 0000000..946aef3
--- /dev/null
+++ b/src/gs-auth-helper.c.orig
@@ -0,0 +1,213 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * written by Olaf Kirch <[email protected]>
+ * xscreensaver, Copyright (c) 1993-2004 Jamie Zawinski <[email protected]>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation. No representations are made about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ */
+
+/* The idea here is to be able to run mate-screensaver-dialog without any setuid bits.
+ * Password verification happens through an external program that you feed
+ * your password to on stdin. The external command is invoked with a user
+ * name argument.
+ *
+ * The external helper does whatever authentication is necessary. Currently,
+ * SuSE uses "unix2_chkpwd", which is a variation of "unix_chkpwd" from the
+ * PAM distribution.
+ *
+ * Normally, the password helper should just authenticate the calling user
+ * (i.e. based on the caller's real uid). This is in order to prevent
+ * brute-forcing passwords in a shadow environment. A less restrictive
+ * approach would be to allow verifying other passwords as well, but always
+ * with a 2 second delay or so. (Not sure what SuSE's "unix2_chkpwd"
+ * currently does.)
+ * -- Olaf Kirch <[email protected]>, 16-Dec-2003
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <errno.h>
+#include <sys/wait.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "gs-auth.h"
+#include "subprocs.h"
+
+static gboolean verbose_enabled = FALSE;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static gboolean
+ext_run (const char *user,
+ const char *typed_passwd,
+ gboolean verbose)
+{
+ int pfd[2], status;
+ pid_t pid;
+
+ if (pipe (pfd) < 0)
+ {
+ return 0;
+ }
+
+ if (verbose)
+ {
+ g_message ("ext_run (%s, %s)",
+ PASSWD_HELPER_PROGRAM, user);
+ }
+
+ block_sigchld ();
+
+ if ((pid = fork ()) < 0)
+ {
+ close (pfd [0]);
+ close (pfd [1]);
+ return FALSE;
+ }
+
+ if (pid == 0)
+ {
+ close (pfd [1]);
+ if (pfd [0] != 0)
+ {
+ dup2 (pfd [0], 0);
+ }
+
+ /* Helper is invoked as helper service-name [user] */
+ execlp (PASSWD_HELPER_PROGRAM, PASSWD_HELPER_PROGRAM, "mate-screensaver", user, NULL);
+ if (verbose)
+ {
+ g_message ("%s: %s", PASSWD_HELPER_PROGRAM, g_strerror (errno));
+ }
+
+ exit (1);
+ }
+
+ close (pfd [0]);
+
+ /* Write out password to helper process */
+ if (!typed_passwd)
+ {
+ typed_passwd = "";
+ }
+ write (pfd [1], typed_passwd, strlen (typed_passwd));
+ close (pfd [1]);
+
+ while (waitpid (pid, &status, 0) < 0)
+ {
+ if (errno == EINTR)
+ {
+ continue;
+ }
+
+ if (verbose)
+ {
+ g_message ("ext_run: waitpid failed: %s\n",
+ g_strerror (errno));
+ }
+
+ unblock_sigchld ();
+ return FALSE;
+ }
+
+ unblock_sigchld ();
+
+ if (! WIFEXITED (status) || WEXITSTATUS (status) != 0)
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ gboolean res = FALSE;
+ char *password;
+
+ password = NULL;
+
+ /* ask for the password for user */
+ if (func != NULL)
+ {
+ func (GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ "Password: ",
+ &password,
+ data);
+ }
+
+ if (password == NULL)
+ {
+ return FALSE;
+ }
+
+ res = ext_run (username, password, gs_auth_get_verbose ());
+
+ return res;
+}
+
+gboolean
+gs_auth_init (void)
+{
+ return TRUE;
+}
+
+gboolean
+gs_auth_priv_init (void)
+{
+ /* Make sure the passwd helper exists */
+ if (g_access (PASSWD_HELPER_PROGRAM, X_OK) < 0)
+ {
+ g_warning ("%s does not exist. "
+ "password authentication via "
+ "external helper will not work.",
+ PASSWD_HELPER_PROGRAM);
+ return FALSE;
+ }
+
+ return TRUE;
+}
diff --git a/src/gs-auth-pam.c b/src/gs-auth-pam.c
new file mode 100644
index 0000000..44c8aca
--- /dev/null
+++ b/src/gs-auth-pam.c
@@ -0,0 +1,843 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ * Copyright (C) 2006 Ray Strode <[email protected]>
+ * Copyright (C) 2003 Bill Nottingham <[email protected]>
+ * Copyright (c) 1993-2003 Jamie Zawinski <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+#include <security/pam_appl.h>
+#include <signal.h>
+#include <errno.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "gs-auth.h"
+
+#include "subprocs.h"
+
+/* Some time between Red Hat 4.2 and 7.0, the words were transposed
+ in the various PAM_x_CRED macro names. Yay!
+*/
+#ifndef PAM_REFRESH_CRED
+# define PAM_REFRESH_CRED PAM_CRED_REFRESH
+#endif
+
+#ifdef HAVE_PAM_FAIL_DELAY
+/* We handle delays ourself.*/
+/* Don't set this to 0 (Linux bug workaround.) */
+# define PAM_NO_DELAY(pamh) pam_fail_delay ((pamh), 1)
+#else /* !HAVE_PAM_FAIL_DELAY */
+# define PAM_NO_DELAY(pamh) /* */
+#endif /* !HAVE_PAM_FAIL_DELAY */
+
+
+/* On SunOS 5.6, and on Linux with PAM 0.64, pam_strerror() takes two args.
+ On some other Linux systems with some other version of PAM (e.g.,
+ whichever Debian release comes with a 2.2.5 kernel) it takes one arg.
+ I can't tell which is more "recent" or "correct" behavior, so configure
+ figures out which is in use for us. Shoot me!
+*/
+#ifdef PAM_STRERROR_TWO_ARGS
+# define PAM_STRERROR(pamh, status) pam_strerror((pamh), (status))
+#else /* !PAM_STRERROR_TWO_ARGS */
+# define PAM_STRERROR(pamh, status) pam_strerror((status))
+#endif /* !PAM_STRERROR_TWO_ARGS */
+
+static gboolean verbose_enabled = FALSE;
+static pam_handle_t *pam_handle = NULL;
+static gboolean did_we_ask_for_password = FALSE;
+
+struct pam_closure
+{
+ const char *username;
+ GSAuthMessageFunc cb_func;
+ gpointer cb_data;
+ int signal_fd;
+ int result;
+};
+
+typedef struct
+{
+ struct pam_closure *closure;
+ GSAuthMessageStyle style;
+ const char *msg;
+ char **resp;
+ gboolean should_interrupt_stack;
+} GsAuthMessageHandlerData;
+
+static GCond *message_handled_condition;
+static GMutex *message_handler_mutex;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static GSAuthMessageStyle
+pam_style_to_gs_style (int pam_style)
+{
+ GSAuthMessageStyle style;
+
+ switch (pam_style)
+ {
+ case PAM_PROMPT_ECHO_ON:
+ style = GS_AUTH_MESSAGE_PROMPT_ECHO_ON;
+ break;
+ case PAM_PROMPT_ECHO_OFF:
+ style = GS_AUTH_MESSAGE_PROMPT_ECHO_OFF;
+ break;
+ case PAM_ERROR_MSG:
+ style = GS_AUTH_MESSAGE_ERROR_MSG;
+ break;
+ case PAM_TEXT_INFO:
+ style = GS_AUTH_MESSAGE_TEXT_INFO;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return style;
+}
+
+static gboolean
+auth_message_handler (GSAuthMessageStyle style,
+ const char *msg,
+ char **response,
+ gpointer data)
+{
+ gboolean ret;
+
+ ret = TRUE;
+ *response = NULL;
+
+ switch (style)
+ {
+ case GS_AUTH_MESSAGE_PROMPT_ECHO_ON:
+ break;
+ case GS_AUTH_MESSAGE_PROMPT_ECHO_OFF:
+ if (msg != NULL && g_str_has_prefix (msg, _("Password:")))
+ {
+ did_we_ask_for_password = TRUE;
+ }
+ break;
+ case GS_AUTH_MESSAGE_ERROR_MSG:
+ break;
+ case GS_AUTH_MESSAGE_TEXT_INFO:
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ return ret;
+}
+
+static gboolean
+gs_auth_queued_message_handler (GsAuthMessageHandlerData *data)
+{
+ gboolean res;
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for lock");
+ }
+
+ g_mutex_lock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for response");
+ }
+
+ res = data->closure->cb_func (data->style,
+ data->msg,
+ data->resp,
+ data->closure->cb_data);
+
+ data->should_interrupt_stack = res == FALSE;
+
+ g_cond_signal (message_handled_condition);
+ g_mutex_unlock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Got response");
+ }
+
+ return FALSE;
+}
+
+static gboolean
+gs_auth_run_message_handler (struct pam_closure *c,
+ GSAuthMessageStyle style,
+ const char *msg,
+ char **resp)
+{
+ GsAuthMessageHandlerData data;
+
+ data.closure = c;
+ data.style = style;
+ data.msg = msg;
+ data.resp = resp;
+ data.should_interrupt_stack = TRUE;
+
+ g_mutex_lock (message_handler_mutex);
+
+ /* Queue the callback in the gui (the main) thread
+ */
+ g_idle_add ((GSourceFunc) gs_auth_queued_message_handler, &data);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for respose to message style %d: '%s'", style, msg);
+ }
+
+ /* Wait for the response
+ */
+ g_cond_wait (message_handled_condition,
+ message_handler_mutex);
+ g_mutex_unlock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Got respose to message style %d: interrupt:%d", style, data.should_interrupt_stack);
+ }
+
+ return data.should_interrupt_stack == FALSE;
+}
+
+static int
+pam_conversation (int nmsgs,
+ const struct pam_message **msg,
+ struct pam_response **resp,
+ void *closure)
+{
+ int replies = 0;
+ struct pam_response *reply = NULL;
+ struct pam_closure *c = (struct pam_closure *) closure;
+ gboolean res;
+ int ret;
+
+ reply = (struct pam_response *) calloc (nmsgs, sizeof (*reply));
+
+ if (reply == NULL)
+ {
+ return PAM_CONV_ERR;
+ }
+
+ res = TRUE;
+ ret = PAM_SUCCESS;
+
+ for (replies = 0; replies < nmsgs && ret == PAM_SUCCESS; replies++)
+ {
+ GSAuthMessageStyle style;
+ char *utf8_msg;
+
+ style = pam_style_to_gs_style (msg [replies]->msg_style);
+
+ utf8_msg = g_locale_to_utf8 (msg [replies]->msg,
+ -1,
+ NULL,
+ NULL,
+ NULL);
+
+ /* if we couldn't convert text from locale then
+ * assume utf-8 and hope for the best */
+ if (utf8_msg == NULL)
+ {
+ char *p;
+ char *q;
+
+ utf8_msg = g_strdup (msg [replies]->msg);
+
+ p = utf8_msg;
+ while (*p != '\0' && !g_utf8_validate ((const char *)p, -1, (const char **)&q))
+ {
+ *q = '?';
+ p = q + 1;
+ }
+ }
+
+ /* handle message locally first */
+ auth_message_handler (style,
+ utf8_msg,
+ &reply [replies].resp,
+ NULL);
+
+ if (c->cb_func != NULL)
+ {
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Handling message style %d: '%s'", style, utf8_msg);
+ }
+
+ /* blocks until the gui responds
+ */
+ res = gs_auth_run_message_handler (c,
+ style,
+ utf8_msg,
+ &reply [replies].resp);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Msg handler returned %d", res);
+ }
+
+ /* If the handler returns FALSE - interrupt the PAM stack */
+ if (res)
+ {
+ reply [replies].resp_retcode = PAM_SUCCESS;
+ }
+ else
+ {
+ int i;
+ for (i = 0; i <= replies; i++)
+ {
+ free (reply [i].resp);
+ }
+ free (reply);
+ reply = NULL;
+ ret = PAM_CONV_ERR;
+ }
+ }
+
+ g_free (utf8_msg);
+ }
+
+ *resp = reply;
+
+ return ret;
+}
+
+static gboolean
+close_pam_handle (int status)
+{
+
+ if (pam_handle != NULL)
+ {
+ int status2;
+
+ status2 = pam_end (pam_handle, status);
+ pam_handle = NULL;
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_end (...) ==> %d (%s)",
+ status2,
+ (status2 == PAM_SUCCESS ? "Success" : "Failure"));
+ }
+ }
+
+ if (message_handled_condition != NULL)
+ {
+ g_cond_free (message_handled_condition);
+ message_handled_condition = NULL;
+ }
+
+ if (message_handler_mutex != NULL)
+ {
+ g_mutex_free (message_handler_mutex);
+ message_handler_mutex = NULL;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+create_pam_handle (const char *username,
+ const char *display,
+ struct pam_conv *conv,
+ int *status_code)
+{
+ int status;
+ const char *service = PAM_SERVICE_NAME;
+ char *disp;
+ gboolean ret;
+
+ if (pam_handle != NULL)
+ {
+ g_warning ("create_pam_handle: Stale pam handle around, cleaning up");
+ close_pam_handle (PAM_SUCCESS);
+ }
+
+ /* init things */
+ pam_handle = NULL;
+ status = -1;
+ disp = NULL;
+ ret = TRUE;
+
+ /* Initialize a PAM session for the user */
+ if ((status = pam_start (service, username, conv, &pam_handle)) != PAM_SUCCESS)
+ {
+ pam_handle = NULL;
+ g_warning (_("Unable to establish service %s: %s\n"),
+ service,
+ PAM_STRERROR (NULL, status));
+
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ ret = FALSE;
+ goto out;
+ }
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("pam_start (\"%s\", \"%s\", ...) ==> %d (%s)",
+ service,
+ username,
+ status,
+ PAM_STRERROR (pam_handle, status));
+ }
+
+ disp = g_strdup (display);
+ if (disp == NULL)
+ {
+ disp = g_strdup (":0.0");
+ }
+
+ if ((status = pam_set_item (pam_handle, PAM_TTY, disp)) != PAM_SUCCESS)
+ {
+ g_warning (_("Can't set PAM_TTY=%s"), display);
+
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ ret = FALSE;
+ goto out;
+ }
+
+ ret = TRUE;
+ message_handled_condition = g_cond_new ();
+ message_handler_mutex = g_mutex_new ();
+
+out:
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ g_free (disp);
+
+ return ret;
+}
+
+static void
+set_pam_error (GError **error,
+ int status)
+{
+ if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN)
+ {
+ char *msg;
+
+ if (did_we_ask_for_password)
+ {
+ msg = g_strdup (_("Incorrect password."));
+ }
+ else
+ {
+ msg = g_strdup (_("Authentication failed."));
+ }
+
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_ERROR,
+ "%s",
+ msg);
+ g_free (msg);
+ }
+ else if (status == PAM_PERM_DENIED)
+ {
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_DENIED,
+ "%s",
+ _("Not permitted to gain access at this time."));
+ }
+ else if (status == PAM_ACCT_EXPIRED)
+ {
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_DENIED,
+ "%s",
+ _("No longer permitted to access the system."));
+ }
+
+}
+
+static int
+gs_auth_thread_func (int auth_operation_fd)
+{
+ static const int flags = 0;
+ int status;
+ int status2;
+ struct timespec timeout;
+ sigset_t set;
+ const void *p;
+
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 1;
+
+ set = block_sigchld ();
+
+ status = pam_authenticate (pam_handle, flags);
+
+ sigtimedwait (&set, NULL, &timeout);
+ unblock_sigchld ();
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_authenticate (...) ==> %d (%s)",
+ status,
+ PAM_STRERROR (pam_handle, status));
+ }
+
+ if (status != PAM_SUCCESS)
+ {
+ goto done;
+ }
+
+ if ((status = pam_get_item (pam_handle, PAM_USER, &p)) != PAM_SUCCESS)
+ {
+ /* is not really an auth problem, but it will
+ pretty much look as such, it shouldn't really
+ happen */
+ goto done;
+ }
+
+ /* We don't actually care if the account modules fail or succeed,
+ * but we need to run them anyway because certain pam modules
+ * depend on side effects of the account modules getting run.
+ */
+ status2 = pam_acct_mgmt (pam_handle, 0);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("pam_acct_mgmt (...) ==> %d (%s)\n",
+ status2,
+ PAM_STRERROR (pam_handle, status2));
+ }
+
+ /* FIXME: should we handle these? */
+ switch (status2)
+ {
+ case PAM_SUCCESS:
+ break;
+ case PAM_NEW_AUTHTOK_REQD:
+ break;
+ case PAM_AUTHINFO_UNAVAIL:
+ break;
+ case PAM_ACCT_EXPIRED:
+ break;
+ case PAM_PERM_DENIED:
+ break;
+ default :
+ break;
+ }
+
+ /* Each time we successfully authenticate, refresh credentials,
+ for Kerberos/AFS/DCE/etc. If this fails, just ignore that
+ failure and blunder along; it shouldn't matter.
+
+ Note: this used to be PAM_REFRESH_CRED instead of
+ PAM_REINITIALIZE_CRED, but Jason Heiss <[email protected]>
+ says that the Linux PAM library ignores that one, and only refreshes
+ credentials when using PAM_REINITIALIZE_CRED.
+ */
+ status2 = pam_setcred (pam_handle, PAM_REINITIALIZE_CRED);
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_setcred (...) ==> %d (%s)",
+ status2,
+ PAM_STRERROR (pam_handle, status2));
+ }
+
+done:
+ /* we're done, close the fd and wake up the main
+ * loop
+ */
+ close (auth_operation_fd);
+
+ return status;
+}
+
+static gboolean
+gs_auth_loop_quit (GIOChannel *source,
+ GIOCondition condition,
+ gboolean *thread_done)
+{
+ *thread_done = TRUE;
+ gtk_main_quit ();
+ return FALSE;
+}
+
+static gboolean
+gs_auth_pam_verify_user (pam_handle_t *handle,
+ int *status)
+{
+ GThread *auth_thread;
+ GIOChannel *channel;
+ guint watch_id;
+ int auth_operation_fds[2];
+ int auth_status;
+ gboolean thread_done;
+
+ channel = NULL;
+ watch_id = 0;
+ auth_status = PAM_AUTH_ERR;
+
+ /* This pipe gives us a set of fds we can hook into
+ * the event loop to be notified when our helper thread
+ * is ready to be reaped.
+ */
+ if (pipe (auth_operation_fds) < 0)
+ {
+ goto out;
+ }
+
+ if (fcntl (auth_operation_fds[0], F_SETFD, FD_CLOEXEC) < 0)
+ {
+ close (auth_operation_fds[0]);
+ close (auth_operation_fds[1]);
+ goto out;
+ }
+
+ if (fcntl (auth_operation_fds[1], F_SETFD, FD_CLOEXEC) < 0)
+ {
+ close (auth_operation_fds[0]);
+ close (auth_operation_fds[1]);
+ goto out;
+ }
+
+ channel = g_io_channel_unix_new (auth_operation_fds[0]);
+
+ /* we use a recursive main loop to process ui events
+ * while we wait on a thread to handle the blocking parts
+ * of pam authentication.
+ */
+ thread_done = FALSE;
+ watch_id = g_io_add_watch (channel, G_IO_ERR | G_IO_HUP,
+ (GIOFunc) gs_auth_loop_quit, &thread_done);
+
+ auth_thread = g_thread_create ((GThreadFunc) gs_auth_thread_func,
+ GINT_TO_POINTER (auth_operation_fds[1]),
+ TRUE, NULL);
+
+ if (auth_thread == NULL)
+ {
+ goto out;
+ }
+
+ gtk_main ();
+
+ /* if the event loop was quit before the thread is done then we can't
+ * reap the thread without blocking on it finishing. The
+ * thread may not ever finish though if the pam module is blocking.
+ *
+ * The only time the event loop is going to stop when the thread isn't
+ * done, however, is if the dialog quits early (from, e.g., "cancel"),
+ * so we can just exit. An alternative option would be to switch to
+ * using pthreads directly and calling pthread_cancel.
+ */
+ if (!thread_done)
+ {
+ raise (SIGTERM);
+ }
+
+ auth_status = GPOINTER_TO_INT (g_thread_join (auth_thread));
+
+out:
+ if (watch_id != 0)
+ {
+ g_source_remove (watch_id);
+ }
+
+ if (channel != NULL)
+ {
+ g_io_channel_unref (channel);
+ }
+
+ if (status)
+ {
+ *status = auth_status;
+ }
+
+ return auth_status == PAM_SUCCESS;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ int status = -1;
+ struct pam_conv conv;
+ struct pam_closure c;
+ struct passwd *pwent;
+
+ pwent = getpwnam (username);
+ if (pwent == NULL)
+ {
+ return FALSE;
+ }
+
+ c.username = username;
+ c.cb_func = func;
+ c.cb_data = data;
+
+ conv.conv = &pam_conversation;
+ conv.appdata_ptr = (void *) &c;
+
+ /* Initialize PAM. */
+ create_pam_handle (username, display, &conv, &status);
+ if (status != PAM_SUCCESS)
+ {
+ goto done;
+ }
+
+ pam_set_item (pam_handle, PAM_USER_PROMPT, _("Username:"));
+
+ PAM_NO_DELAY(pam_handle);
+
+ did_we_ask_for_password = FALSE;
+ if (! gs_auth_pam_verify_user (pam_handle, &status))
+ {
+ goto done;
+ }
+
+done:
+ if (status != PAM_SUCCESS)
+ {
+ set_pam_error (error, status);
+ }
+
+ close_pam_handle (status);
+
+ return (status == PAM_SUCCESS ? TRUE : FALSE);
+}
+
+gboolean
+gs_auth_init (void)
+{
+ return TRUE;
+}
+
+gboolean
+gs_auth_priv_init (void)
+{
+ /* We have nothing to do at init-time.
+ However, we might as well do some error checking.
+ If "/etc/pam.d" exists and is a directory, but "/etc/pam.d/xlock"
+ does not exist, warn that PAM probably isn't going to work.
+
+ This is a priv-init instead of a non-priv init in case the directory
+ is unreadable or something (don't know if that actually happens.)
+ */
+ const char dir [] = "/etc/pam.d";
+ const char file [] = "/etc/pam.d/" PAM_SERVICE_NAME;
+ const char file2 [] = "/etc/pam.conf";
+ struct stat st;
+
+ if (g_stat (dir, &st) == 0 && st.st_mode & S_IFDIR)
+ {
+ if (g_stat (file, &st) != 0)
+ {
+ g_warning ("%s does not exist.\n"
+ "Authentication via PAM is unlikely to work.",
+ file);
+ }
+ }
+ else if (g_stat (file2, &st) == 0)
+ {
+ FILE *f = g_fopen (file2, "r");
+ if (f)
+ {
+ gboolean ok = FALSE;
+ char buf[255];
+ while (fgets (buf, sizeof(buf), f))
+ {
+ if (strstr (buf, PAM_SERVICE_NAME))
+ {
+ ok = TRUE;
+ break;
+ }
+ }
+
+ fclose (f);
+ if (!ok)
+ {
+ g_warning ("%s does not list the `%s' service.\n"
+ "Authentication via PAM is unlikely to work.",
+ file2, PAM_SERVICE_NAME);
+ }
+ }
+ /* else warn about file2 existing but being unreadable? */
+ }
+ else
+ {
+ g_warning ("Neither %s nor %s exist.\n"
+ "Authentication via PAM is unlikely to work.",
+ file2, file);
+ }
+
+ /* Return true anyway, just in case. */
+ return TRUE;
+}
diff --git a/src/gs-auth-pam.c.orig b/src/gs-auth-pam.c.orig
new file mode 100644
index 0000000..88852d9
--- /dev/null
+++ b/src/gs-auth-pam.c.orig
@@ -0,0 +1,843 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ * Copyright (C) 2006 Ray Strode <[email protected]>
+ * Copyright (C) 2003 Bill Nottingham <[email protected]>
+ * Copyright (c) 1993-2003 Jamie Zawinski <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+#include <security/pam_appl.h>
+#include <signal.h>
+#include <errno.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "gs-auth.h"
+
+#include "subprocs.h"
+
+/* Some time between Red Hat 4.2 and 7.0, the words were transposed
+ in the various PAM_x_CRED macro names. Yay!
+*/
+#ifndef PAM_REFRESH_CRED
+# define PAM_REFRESH_CRED PAM_CRED_REFRESH
+#endif
+
+#ifdef HAVE_PAM_FAIL_DELAY
+/* We handle delays ourself.*/
+/* Don't set this to 0 (Linux bug workaround.) */
+# define PAM_NO_DELAY(pamh) pam_fail_delay ((pamh), 1)
+#else /* !HAVE_PAM_FAIL_DELAY */
+# define PAM_NO_DELAY(pamh) /* */
+#endif /* !HAVE_PAM_FAIL_DELAY */
+
+
+/* On SunOS 5.6, and on Linux with PAM 0.64, pam_strerror() takes two args.
+ On some other Linux systems with some other version of PAM (e.g.,
+ whichever Debian release comes with a 2.2.5 kernel) it takes one arg.
+ I can't tell which is more "recent" or "correct" behavior, so configure
+ figures out which is in use for us. Shoot me!
+*/
+#ifdef PAM_STRERROR_TWO_ARGS
+# define PAM_STRERROR(pamh, status) pam_strerror((pamh), (status))
+#else /* !PAM_STRERROR_TWO_ARGS */
+# define PAM_STRERROR(pamh, status) pam_strerror((status))
+#endif /* !PAM_STRERROR_TWO_ARGS */
+
+static gboolean verbose_enabled = FALSE;
+static pam_handle_t *pam_handle = NULL;
+static gboolean did_we_ask_for_password = FALSE;
+
+struct pam_closure
+{
+ const char *username;
+ GSAuthMessageFunc cb_func;
+ gpointer cb_data;
+ int signal_fd;
+ int result;
+};
+
+typedef struct
+{
+ struct pam_closure *closure;
+ GSAuthMessageStyle style;
+ const char *msg;
+ char **resp;
+ gboolean should_interrupt_stack;
+} GsAuthMessageHandlerData;
+
+static GCond *message_handled_condition;
+static GMutex *message_handler_mutex;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static GSAuthMessageStyle
+pam_style_to_gs_style (int pam_style)
+{
+ GSAuthMessageStyle style;
+
+ switch (pam_style)
+ {
+ case PAM_PROMPT_ECHO_ON:
+ style = GS_AUTH_MESSAGE_PROMPT_ECHO_ON;
+ break;
+ case PAM_PROMPT_ECHO_OFF:
+ style = GS_AUTH_MESSAGE_PROMPT_ECHO_OFF;
+ break;
+ case PAM_ERROR_MSG:
+ style = GS_AUTH_MESSAGE_ERROR_MSG;
+ break;
+ case PAM_TEXT_INFO:
+ style = GS_AUTH_MESSAGE_TEXT_INFO;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return style;
+}
+
+static gboolean
+auth_message_handler (GSAuthMessageStyle style,
+ const char *msg,
+ char **response,
+ gpointer data)
+{
+ gboolean ret;
+
+ ret = TRUE;
+ *response = NULL;
+
+ switch (style)
+ {
+ case GS_AUTH_MESSAGE_PROMPT_ECHO_ON:
+ break;
+ case GS_AUTH_MESSAGE_PROMPT_ECHO_OFF:
+ if (msg != NULL && g_str_has_prefix (msg, _("Password:")))
+ {
+ did_we_ask_for_password = TRUE;
+ }
+ break;
+ case GS_AUTH_MESSAGE_ERROR_MSG:
+ break;
+ case GS_AUTH_MESSAGE_TEXT_INFO:
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ return ret;
+}
+
+static gboolean
+gs_auth_queued_message_handler (GsAuthMessageHandlerData *data)
+{
+ gboolean res;
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for lock");
+ }
+
+ g_mutex_lock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for response");
+ }
+
+ res = data->closure->cb_func (data->style,
+ data->msg,
+ data->resp,
+ data->closure->cb_data);
+
+ data->should_interrupt_stack = res == FALSE;
+
+ g_cond_signal (message_handled_condition);
+ g_mutex_unlock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Got response");
+ }
+
+ return FALSE;
+}
+
+static gboolean
+gs_auth_run_message_handler (struct pam_closure *c,
+ GSAuthMessageStyle style,
+ const char *msg,
+ char **resp)
+{
+ GsAuthMessageHandlerData data;
+
+ data.closure = c;
+ data.style = style;
+ data.msg = msg;
+ data.resp = resp;
+ data.should_interrupt_stack = TRUE;
+
+ g_mutex_lock (message_handler_mutex);
+
+ /* Queue the callback in the gui (the main) thread
+ */
+ g_idle_add ((GSourceFunc) gs_auth_queued_message_handler, &data);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Waiting for respose to message style %d: '%s'", style, msg);
+ }
+
+ /* Wait for the response
+ */
+ g_cond_wait (message_handled_condition,
+ message_handler_mutex);
+ g_mutex_unlock (message_handler_mutex);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Got respose to message style %d: interrupt:%d", style, data.should_interrupt_stack);
+ }
+
+ return data.should_interrupt_stack == FALSE;
+}
+
+static int
+pam_conversation (int nmsgs,
+ const struct pam_message **msg,
+ struct pam_response **resp,
+ void *closure)
+{
+ int replies = 0;
+ struct pam_response *reply = NULL;
+ struct pam_closure *c = (struct pam_closure *) closure;
+ gboolean res;
+ int ret;
+
+ reply = (struct pam_response *) calloc (nmsgs, sizeof (*reply));
+
+ if (reply == NULL)
+ {
+ return PAM_CONV_ERR;
+ }
+
+ res = TRUE;
+ ret = PAM_SUCCESS;
+
+ for (replies = 0; replies < nmsgs && ret == PAM_SUCCESS; replies++)
+ {
+ GSAuthMessageStyle style;
+ char *utf8_msg;
+
+ style = pam_style_to_gs_style (msg [replies]->msg_style);
+
+ utf8_msg = g_locale_to_utf8 (msg [replies]->msg,
+ -1,
+ NULL,
+ NULL,
+ NULL);
+
+ /* if we couldn't convert text from locale then
+ * assume utf-8 and hope for the best */
+ if (utf8_msg == NULL)
+ {
+ char *p;
+ char *q;
+
+ utf8_msg = g_strdup (msg [replies]->msg);
+
+ p = utf8_msg;
+ while (*p != '\0' && !g_utf8_validate ((const char *)p, -1, (const char **)&q))
+ {
+ *q = '?';
+ p = q + 1;
+ }
+ }
+
+ /* handle message locally first */
+ auth_message_handler (style,
+ utf8_msg,
+ &reply [replies].resp,
+ NULL);
+
+ if (c->cb_func != NULL)
+ {
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Handling message style %d: '%s'", style, utf8_msg);
+ }
+
+ /* blocks until the gui responds
+ */
+ res = gs_auth_run_message_handler (c,
+ style,
+ utf8_msg,
+ &reply [replies].resp);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("Msg handler returned %d", res);
+ }
+
+ /* If the handler returns FALSE - interrupt the PAM stack */
+ if (res)
+ {
+ reply [replies].resp_retcode = PAM_SUCCESS;
+ }
+ else
+ {
+ int i;
+ for (i = 0; i <= replies; i++)
+ {
+ free (reply [i].resp);
+ }
+ free (reply);
+ reply = NULL;
+ ret = PAM_CONV_ERR;
+ }
+ }
+
+ g_free (utf8_msg);
+ }
+
+ *resp = reply;
+
+ return ret;
+}
+
+static gboolean
+close_pam_handle (int status)
+{
+
+ if (pam_handle != NULL)
+ {
+ int status2;
+
+ status2 = pam_end (pam_handle, status);
+ pam_handle = NULL;
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_end (...) ==> %d (%s)",
+ status2,
+ (status2 == PAM_SUCCESS ? "Success" : "Failure"));
+ }
+ }
+
+ if (message_handled_condition != NULL)
+ {
+ g_cond_free (message_handled_condition);
+ message_handled_condition = NULL;
+ }
+
+ if (message_handler_mutex != NULL)
+ {
+ g_mutex_free (message_handler_mutex);
+ message_handler_mutex = NULL;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+create_pam_handle (const char *username,
+ const char *display,
+ struct pam_conv *conv,
+ int *status_code)
+{
+ int status;
+ const char *service = PAM_SERVICE_NAME;
+ char *disp;
+ gboolean ret;
+
+ if (pam_handle != NULL)
+ {
+ g_warning ("create_pam_handle: Stale pam handle around, cleaning up");
+ close_pam_handle (PAM_SUCCESS);
+ }
+
+ /* init things */
+ pam_handle = NULL;
+ status = -1;
+ disp = NULL;
+ ret = TRUE;
+
+ /* Initialize a PAM session for the user */
+ if ((status = pam_start (service, username, conv, &pam_handle)) != PAM_SUCCESS)
+ {
+ pam_handle = NULL;
+ g_warning (_("Unable to establish service %s: %s\n"),
+ service,
+ PAM_STRERROR (NULL, status));
+
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ ret = FALSE;
+ goto out;
+ }
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("pam_start (\"%s\", \"%s\", ...) ==> %d (%s)",
+ service,
+ username,
+ status,
+ PAM_STRERROR (pam_handle, status));
+ }
+
+ disp = g_strdup (display);
+ if (disp == NULL)
+ {
+ disp = g_strdup (":0.0");
+ }
+
+ if ((status = pam_set_item (pam_handle, PAM_TTY, disp)) != PAM_SUCCESS)
+ {
+ g_warning (_("Can't set PAM_TTY=%s"), display);
+
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ ret = FALSE;
+ goto out;
+ }
+
+ ret = TRUE;
+ message_handled_condition = g_cond_new ();
+ message_handler_mutex = g_mutex_new ();
+
+out:
+ if (status_code != NULL)
+ {
+ *status_code = status;
+ }
+
+ g_free (disp);
+
+ return ret;
+}
+
+static void
+set_pam_error (GError **error,
+ int status)
+{
+ if (status == PAM_AUTH_ERR || status == PAM_USER_UNKNOWN)
+ {
+ char *msg;
+
+ if (did_we_ask_for_password)
+ {
+ msg = g_strdup (_("Incorrect password."));
+ }
+ else
+ {
+ msg = g_strdup (_("Authentication failed."));
+ }
+
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_ERROR,
+ "%s",
+ msg);
+ g_free (msg);
+ }
+ else if (status == PAM_PERM_DENIED)
+ {
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_DENIED,
+ "%s",
+ _("Not permitted to gain access at this time."));
+ }
+ else if (status == PAM_ACCT_EXPIRED)
+ {
+ g_set_error (error,
+ GS_AUTH_ERROR,
+ GS_AUTH_ERROR_AUTH_DENIED,
+ "%s",
+ _("No longer permitted to access the system."));
+ }
+
+}
+
+static int
+gs_auth_thread_func (int auth_operation_fd)
+{
+ static const int flags = 0;
+ int status;
+ int status2;
+ struct timespec timeout;
+ sigset_t set;
+ const void *p;
+
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 1;
+
+ set = block_sigchld ();
+
+ status = pam_authenticate (pam_handle, flags);
+
+ sigtimedwait (&set, NULL, &timeout);
+ unblock_sigchld ();
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_authenticate (...) ==> %d (%s)",
+ status,
+ PAM_STRERROR (pam_handle, status));
+ }
+
+ if (status != PAM_SUCCESS)
+ {
+ goto done;
+ }
+
+ if ((status = pam_get_item (pam_handle, PAM_USER, &p)) != PAM_SUCCESS)
+ {
+ /* is not really an auth problem, but it will
+ pretty much look as such, it shouldn't really
+ happen */
+ goto done;
+ }
+
+ /* We don't actually care if the account modules fail or succeed,
+ * but we need to run them anyway because certain pam modules
+ * depend on side effects of the account modules getting run.
+ */
+ status2 = pam_acct_mgmt (pam_handle, 0);
+
+ if (gs_auth_get_verbose ())
+ {
+ g_message ("pam_acct_mgmt (...) ==> %d (%s)\n",
+ status2,
+ PAM_STRERROR (pam_handle, status2));
+ }
+
+ /* FIXME: should we handle these? */
+ switch (status2)
+ {
+ case PAM_SUCCESS:
+ break;
+ case PAM_NEW_AUTHTOK_REQD:
+ break;
+ case PAM_AUTHINFO_UNAVAIL:
+ break;
+ case PAM_ACCT_EXPIRED:
+ break;
+ case PAM_PERM_DENIED:
+ break;
+ default :
+ break;
+ }
+
+ /* Each time we successfully authenticate, refresh credentials,
+ for Kerberos/AFS/DCE/etc. If this fails, just ignore that
+ failure and blunder along; it shouldn't matter.
+
+ Note: this used to be PAM_REFRESH_CRED instead of
+ PAM_REINITIALIZE_CRED, but Jason Heiss <[email protected]>
+ says that the Linux PAM library ignores that one, and only refreshes
+ credentials when using PAM_REINITIALIZE_CRED.
+ */
+ status2 = pam_setcred (pam_handle, PAM_REINITIALIZE_CRED);
+ if (gs_auth_get_verbose ())
+ {
+ g_message (" pam_setcred (...) ==> %d (%s)",
+ status2,
+ PAM_STRERROR (pam_handle, status2));
+ }
+
+done:
+ /* we're done, close the fd and wake up the main
+ * loop
+ */
+ close (auth_operation_fd);
+
+ return status;
+}
+
+static gboolean
+gs_auth_loop_quit (GIOChannel *source,
+ GIOCondition condition,
+ gboolean *thread_done)
+{
+ *thread_done = TRUE;
+ gtk_main_quit ();
+ return FALSE;
+}
+
+static gboolean
+gs_auth_pam_verify_user (pam_handle_t *handle,
+ int *status)
+{
+ GThread *auth_thread;
+ GIOChannel *channel;
+ guint watch_id;
+ int auth_operation_fds[2];
+ int auth_status;
+ gboolean thread_done;
+
+ channel = NULL;
+ watch_id = 0;
+ auth_status = PAM_AUTH_ERR;
+
+ /* This pipe gives us a set of fds we can hook into
+ * the event loop to be notified when our helper thread
+ * is ready to be reaped.
+ */
+ if (pipe (auth_operation_fds) < 0)
+ {
+ goto out;
+ }
+
+ if (fcntl (auth_operation_fds[0], F_SETFD, FD_CLOEXEC) < 0)
+ {
+ close (auth_operation_fds[0]);
+ close (auth_operation_fds[1]);
+ goto out;
+ }
+
+ if (fcntl (auth_operation_fds[1], F_SETFD, FD_CLOEXEC) < 0)
+ {
+ close (auth_operation_fds[0]);
+ close (auth_operation_fds[1]);
+ goto out;
+ }
+
+ channel = g_io_channel_unix_new (auth_operation_fds[0]);
+
+ /* we use a recursive main loop to process ui events
+ * while we wait on a thread to handle the blocking parts
+ * of pam authentication.
+ */
+ thread_done = FALSE;
+ watch_id = g_io_add_watch (channel, G_IO_ERR | G_IO_HUP,
+ (GIOFunc) gs_auth_loop_quit, &thread_done);
+
+ auth_thread = g_thread_create ((GThreadFunc) gs_auth_thread_func,
+ GINT_TO_POINTER (auth_operation_fds[1]),
+ TRUE, NULL);
+
+ if (auth_thread == NULL)
+ {
+ goto out;
+ }
+
+ gtk_main ();
+
+ /* if the event loop was quit before the thread is done then we can't
+ * reap the thread without blocking on it finishing. The
+ * thread may not ever finish though if the pam module is blocking.
+ *
+ * The only time the event loop is going to stop when the thread isn't
+ * done, however, is if the dialog quits early (from, e.g., "cancel"),
+ * so we can just exit. An alternative option would be to switch to
+ * using pthreads directly and calling pthread_cancel.
+ */
+ if (!thread_done)
+ {
+ raise (SIGTERM);
+ }
+
+ auth_status = GPOINTER_TO_INT (g_thread_join (auth_thread));
+
+out:
+ if (watch_id != 0)
+ {
+ g_source_remove (watch_id);
+ }
+
+ if (channel != NULL)
+ {
+ g_io_channel_unref (channel);
+ }
+
+ if (status)
+ {
+ *status = auth_status;
+ }
+
+ return auth_status == PAM_SUCCESS;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ int status = -1;
+ struct pam_conv conv;
+ struct pam_closure c;
+ struct passwd *pwent;
+
+ pwent = getpwnam (username);
+ if (pwent == NULL)
+ {
+ return FALSE;
+ }
+
+ c.username = username;
+ c.cb_func = func;
+ c.cb_data = data;
+
+ conv.conv = &pam_conversation;
+ conv.appdata_ptr = (void *) &c;
+
+ /* Initialize PAM. */
+ create_pam_handle (username, display, &conv, &status);
+ if (status != PAM_SUCCESS)
+ {
+ goto done;
+ }
+
+ pam_set_item (pam_handle, PAM_USER_PROMPT, _("Username:"));
+
+ PAM_NO_DELAY(pam_handle);
+
+ did_we_ask_for_password = FALSE;
+ if (! gs_auth_pam_verify_user (pam_handle, &status))
+ {
+ goto done;
+ }
+
+done:
+ if (status != PAM_SUCCESS)
+ {
+ set_pam_error (error, status);
+ }
+
+ close_pam_handle (status);
+
+ return (status == PAM_SUCCESS ? TRUE : FALSE);
+}
+
+gboolean
+gs_auth_init (void)
+{
+ return TRUE;
+}
+
+gboolean
+gs_auth_priv_init (void)
+{
+ /* We have nothing to do at init-time.
+ However, we might as well do some error checking.
+ If "/etc/pam.d" exists and is a directory, but "/etc/pam.d/xlock"
+ does not exist, warn that PAM probably isn't going to work.
+
+ This is a priv-init instead of a non-priv init in case the directory
+ is unreadable or something (don't know if that actually happens.)
+ */
+ const char dir [] = "/etc/pam.d";
+ const char file [] = "/etc/pam.d/" PAM_SERVICE_NAME;
+ const char file2 [] = "/etc/pam.conf";
+ struct stat st;
+
+ if (g_stat (dir, &st) == 0 && st.st_mode & S_IFDIR)
+ {
+ if (g_stat (file, &st) != 0)
+ {
+ g_warning ("%s does not exist.\n"
+ "Authentication via PAM is unlikely to work.",
+ file);
+ }
+ }
+ else if (g_stat (file2, &st) == 0)
+ {
+ FILE *f = g_fopen (file2, "r");
+ if (f)
+ {
+ gboolean ok = FALSE;
+ char buf[255];
+ while (fgets (buf, sizeof(buf), f))
+ {
+ if (strstr (buf, PAM_SERVICE_NAME))
+ {
+ ok = TRUE;
+ break;
+ }
+ }
+
+ fclose (f);
+ if (!ok)
+ {
+ g_warning ("%s does not list the `%s' service.\n"
+ "Authentication via PAM is unlikely to work.",
+ file2, PAM_SERVICE_NAME);
+ }
+ }
+ /* else warn about file2 existing but being unreadable? */
+ }
+ else
+ {
+ g_warning ("Neither %s nor %s exist.\n"
+ "Authentication via PAM is unlikely to work.",
+ file2, file);
+ }
+
+ /* Return true anyway, just in case. */
+ return TRUE;
+}
diff --git a/src/gs-auth-pwent.c b/src/gs-auth-pwent.c
new file mode 100644
index 0000000..3fe1b22
--- /dev/null
+++ b/src/gs-auth-pwent.c
@@ -0,0 +1,291 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (c) 1993-1998 Jamie Zawinski <[email protected]>
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_CRYPT_H
+# include <crypt.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+
+#ifdef __bsdi__
+# include <sys/param.h>
+# if _BSDI_VERSION >= 199608
+# define BSD_AUTH
+# endif
+#endif /* __bsdi__ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#if defined(HAVE_SHADOW_PASSWD) /* passwds live in /etc/shadow */
+
+# include <shadow.h>
+# define PWTYPE struct spwd *
+# define PWPSLOT sp_pwdp
+# define GETPW getspnam
+
+#elif defined(HAVE_ENHANCED_PASSWD) /* passwds live in /tcb/files/auth/ */
+/* M.Matsumoto <[email protected]> */
+# include <sys/security.h>
+# include <prot.h>
+
+# define PWTYPE struct pr_passwd *
+# define PWPSLOT ufld.fd_encrypt
+# define GETPW getprpwnam
+
+#elif defined(HAVE_ADJUNCT_PASSWD)
+
+# include <sys/label.h>
+# include <sys/audit.h>
+# include <pwdadj.h>
+
+# define PWTYPE struct passwd_adjunct *
+# define PWPSLOT pwa_passwd
+# define GETPW getpwanam
+
+#elif defined(HAVE_HPUX_PASSWD)
+
+# include <hpsecurity.h>
+# include <prot.h>
+
+# define PWTYPE struct s_passwd *
+# define PWPSLOT pw_passwd
+# define GETPW getspwnam
+
+# define HAVE_BIGCRYPT
+
+#endif
+
+#include "gs-auth.h"
+
+static gboolean verbose_enabled = FALSE;
+
+static char *encrypted_user_passwd = NULL;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static gboolean
+passwd_known (const char *pw)
+{
+ return (pw &&
+ pw[0] != '*' && /* This would be sensible... */
+ strlen (pw) > 4); /* ...but this is what Solaris does. */
+}
+
+static char *
+get_encrypted_passwd (const char *user)
+{
+ char *result = NULL;
+
+#ifdef PWTYPE
+ if (user && *user && !result)
+ {
+ /* First check the shadow passwords. */
+ PWTYPE p = GETPW ((char *) user);
+ if (p && passwd_known (p->PWPSLOT))
+ {
+ result = g_strdup (p->PWPSLOT);
+ }
+ }
+#endif /* PWTYPE */
+
+ if (user && *user && !result)
+ {
+ /* Check non-shadow passwords too. */
+ struct passwd *p = getpwnam (user);
+ if (p && passwd_known (p->pw_passwd))
+ {
+ result = g_strdup (p->pw_passwd);
+ }
+ }
+
+ /* The manual for passwd(4) on HPUX 10.10 says:
+
+ Password aging is put in effect for a particular user if his
+ encrypted password in the password file is followed by a comma and
+ a nonnull string of characters from the above alphabet. This
+ string defines the "age" needed to implement password aging.
+
+ So this means that passwd->pw_passwd isn't simply a string of cyphertext,
+ it might have trailing junk. So, if there is a comma in the string, and
+ that comma is beyond position 13, terminate the string before the comma.
+ */
+ if (result && strlen (result) > 13)
+ {
+ char *s = strchr (result + 13, ',');
+ if (s)
+ {
+ *s = 0;
+ }
+ }
+
+#ifndef HAVE_PAM
+ /* We only issue this warning if not compiled with support for PAM.
+ If we're using PAM, it's not unheard of that normal pwent passwords
+ would be unavailable. */
+
+ if (!result)
+ {
+ g_warning ("Couldn't get password of \"%s\"",
+ (user ? user : "(null)"));
+ }
+
+#endif /* !HAVE_PAM */
+
+ return result;
+}
+
+/* This has to be called before we've changed our effective user ID,
+ because it might need privileges to get at the encrypted passwords.
+ Returns false if we weren't able to get any passwords, and therefore,
+ locking isn't possible. (It will also have written to stderr.)
+*/
+
+gboolean
+gs_auth_priv_init (void)
+{
+ const char *u;
+
+ u = g_get_user_name ();
+
+ encrypted_user_passwd = get_encrypted_passwd (u);
+
+ if (encrypted_user_passwd != NULL)
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+gboolean
+gs_auth_init (void)
+{
+ if (encrypted_user_passwd != NULL)
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+static gboolean
+passwds_match (const char *cleartext,
+ const char *ciphertext)
+{
+ char *s = NULL; /* note that on some systems, crypt() may return null */
+
+ s = (char *) crypt (cleartext, ciphertext);
+ if (s && !strcmp (s, ciphertext))
+ {
+ return TRUE;
+ }
+
+#ifdef HAVE_BIGCRYPT
+ /* There seems to be no way to tell at runtime if an HP machine is in
+ "trusted" mode, and thereby, which of crypt() or bigcrypt() we should
+ be calling to compare passwords. So call them both, and see which
+ one works. */
+
+ s = (char *) bigcrypt (cleartext, ciphertext);
+ if (s && !strcmp (s, ciphertext))
+ {
+ return TRUE;
+ }
+
+#endif /* HAVE_BIGCRYPT */
+
+ return FALSE;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ char *password;
+
+ password = NULL;
+
+ /* ask for the password for user */
+ if (func != NULL)
+ {
+ func (GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ "Password: ",
+ &password,
+ data);
+ }
+
+ if (password == NULL)
+ {
+ return FALSE;
+ }
+
+ if (encrypted_user_passwd && passwds_match (password, encrypted_user_passwd))
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
diff --git a/src/gs-auth-pwent.c.orig b/src/gs-auth-pwent.c.orig
new file mode 100644
index 0000000..21abbee
--- /dev/null
+++ b/src/gs-auth-pwent.c.orig
@@ -0,0 +1,291 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (c) 1993-1998 Jamie Zawinski <[email protected]>
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_CRYPT_H
+# include <crypt.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+
+#ifdef __bsdi__
+# include <sys/param.h>
+# if _BSDI_VERSION >= 199608
+# define BSD_AUTH
+# endif
+#endif /* __bsdi__ */
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#if defined(HAVE_SHADOW_PASSWD) /* passwds live in /etc/shadow */
+
+# include <shadow.h>
+# define PWTYPE struct spwd *
+# define PWPSLOT sp_pwdp
+# define GETPW getspnam
+
+#elif defined(HAVE_ENHANCED_PASSWD) /* passwds live in /tcb/files/auth/ */
+/* M.Matsumoto <[email protected]> */
+# include <sys/security.h>
+# include <prot.h>
+
+# define PWTYPE struct pr_passwd *
+# define PWPSLOT ufld.fd_encrypt
+# define GETPW getprpwnam
+
+#elif defined(HAVE_ADJUNCT_PASSWD)
+
+# include <sys/label.h>
+# include <sys/audit.h>
+# include <pwdadj.h>
+
+# define PWTYPE struct passwd_adjunct *
+# define PWPSLOT pwa_passwd
+# define GETPW getpwanam
+
+#elif defined(HAVE_HPUX_PASSWD)
+
+# include <hpsecurity.h>
+# include <prot.h>
+
+# define PWTYPE struct s_passwd *
+# define PWPSLOT pw_passwd
+# define GETPW getspwnam
+
+# define HAVE_BIGCRYPT
+
+#endif
+
+#include "gs-auth.h"
+
+static gboolean verbose_enabled = FALSE;
+
+static char *encrypted_user_passwd = NULL;
+
+GQuark
+gs_auth_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (! quark)
+ {
+ quark = g_quark_from_static_string ("gs_auth_error");
+ }
+
+ return quark;
+}
+
+void
+gs_auth_set_verbose (gboolean enabled)
+{
+ verbose_enabled = enabled;
+}
+
+gboolean
+gs_auth_get_verbose (void)
+{
+ return verbose_enabled;
+}
+
+static gboolean
+passwd_known (const char *pw)
+{
+ return (pw &&
+ pw[0] != '*' && /* This would be sensible... */
+ strlen (pw) > 4); /* ...but this is what Solaris does. */
+}
+
+static char *
+get_encrypted_passwd (const char *user)
+{
+ char *result = NULL;
+
+#ifdef PWTYPE
+ if (user && *user && !result)
+ {
+ /* First check the shadow passwords. */
+ PWTYPE p = GETPW ((char *) user);
+ if (p && passwd_known (p->PWPSLOT))
+ {
+ result = g_strdup (p->PWPSLOT);
+ }
+ }
+#endif /* PWTYPE */
+
+ if (user && *user && !result)
+ {
+ /* Check non-shadow passwords too. */
+ struct passwd *p = getpwnam (user);
+ if (p && passwd_known (p->pw_passwd))
+ {
+ result = g_strdup (p->pw_passwd);
+ }
+ }
+
+ /* The manual for passwd(4) on HPUX 10.10 says:
+
+ Password aging is put in effect for a particular user if his
+ encrypted password in the password file is followed by a comma and
+ a nonnull string of characters from the above alphabet. This
+ string defines the "age" needed to implement password aging.
+
+ So this means that passwd->pw_passwd isn't simply a string of cyphertext,
+ it might have trailing junk. So, if there is a comma in the string, and
+ that comma is beyond position 13, terminate the string before the comma.
+ */
+ if (result && strlen (result) > 13)
+ {
+ char *s = strchr (result + 13, ',');
+ if (s)
+ {
+ *s = 0;
+ }
+ }
+
+#ifndef HAVE_PAM
+ /* We only issue this warning if not compiled with support for PAM.
+ If we're using PAM, it's not unheard of that normal pwent passwords
+ would be unavailable. */
+
+ if (!result)
+ {
+ g_warning ("Couldn't get password of \"%s\"",
+ (user ? user : "(null)"));
+ }
+
+#endif /* !HAVE_PAM */
+
+ return result;
+}
+
+/* This has to be called before we've changed our effective user ID,
+ because it might need privileges to get at the encrypted passwords.
+ Returns false if we weren't able to get any passwords, and therefore,
+ locking isn't possible. (It will also have written to stderr.)
+*/
+
+gboolean
+gs_auth_priv_init (void)
+{
+ const char *u;
+
+ u = g_get_user_name ();
+
+ encrypted_user_passwd = get_encrypted_passwd (u);
+
+ if (encrypted_user_passwd != NULL)
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+
+gboolean
+gs_auth_init (void)
+{
+ if (encrypted_user_passwd != NULL)
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+static gboolean
+passwds_match (const char *cleartext,
+ const char *ciphertext)
+{
+ char *s = NULL; /* note that on some systems, crypt() may return null */
+
+ s = (char *) crypt (cleartext, ciphertext);
+ if (s && !strcmp (s, ciphertext))
+ {
+ return TRUE;
+ }
+
+#ifdef HAVE_BIGCRYPT
+ /* There seems to be no way to tell at runtime if an HP machine is in
+ "trusted" mode, and thereby, which of crypt() or bigcrypt() we should
+ be calling to compare passwords. So call them both, and see which
+ one works. */
+
+ s = (char *) bigcrypt (cleartext, ciphertext);
+ if (s && !strcmp (s, ciphertext))
+ {
+ return TRUE;
+ }
+
+#endif /* HAVE_BIGCRYPT */
+
+ return FALSE;
+}
+
+gboolean
+gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error)
+{
+ char *password;
+
+ password = NULL;
+
+ /* ask for the password for user */
+ if (func != NULL)
+ {
+ func (GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ "Password: ",
+ &password,
+ data);
+ }
+
+ if (password == NULL)
+ {
+ return FALSE;
+ }
+
+ if (encrypted_user_passwd && passwds_match (password, encrypted_user_passwd))
+ {
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
diff --git a/src/gs-auth.h b/src/gs-auth.h
new file mode 100644
index 0000000..503e92d
--- /dev/null
+++ b/src/gs-auth.h
@@ -0,0 +1,67 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ *
+ * 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 __GS_AUTH_H
+#define __GS_AUTH_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef enum
+{
+ GS_AUTH_MESSAGE_PROMPT_ECHO_ON,
+ GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ GS_AUTH_MESSAGE_ERROR_MSG,
+ GS_AUTH_MESSAGE_TEXT_INFO
+} GSAuthMessageStyle;
+
+typedef enum
+{
+ GS_AUTH_ERROR_GENERAL,
+ GS_AUTH_ERROR_AUTH_ERROR,
+ GS_AUTH_ERROR_USER_UNKNOWN,
+ GS_AUTH_ERROR_AUTH_DENIED
+} GSAuthError;
+
+typedef gboolean (* GSAuthMessageFunc) (GSAuthMessageStyle style,
+ const char *msg,
+ char **response,
+ gpointer data);
+
+#define GS_AUTH_ERROR gs_auth_error_quark ()
+
+GQuark gs_auth_error_quark (void);
+
+void gs_auth_set_verbose (gboolean verbose);
+gboolean gs_auth_get_verbose (void);
+
+gboolean gs_auth_priv_init (void);
+gboolean gs_auth_init (void);
+gboolean gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* __GS_AUTH_H */
diff --git a/src/gs-auth.h.orig b/src/gs-auth.h.orig
new file mode 100644
index 0000000..25e5271
--- /dev/null
+++ b/src/gs-auth.h.orig
@@ -0,0 +1,65 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2006 William Jon McCann <[email protected]>
+ *
+ * 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 __GS_AUTH_H
+#define __GS_AUTH_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+typedef enum {
+ GS_AUTH_MESSAGE_PROMPT_ECHO_ON,
+ GS_AUTH_MESSAGE_PROMPT_ECHO_OFF,
+ GS_AUTH_MESSAGE_ERROR_MSG,
+ GS_AUTH_MESSAGE_TEXT_INFO
+} GSAuthMessageStyle;
+
+typedef enum {
+ GS_AUTH_ERROR_GENERAL,
+ GS_AUTH_ERROR_AUTH_ERROR,
+ GS_AUTH_ERROR_USER_UNKNOWN,
+ GS_AUTH_ERROR_AUTH_DENIED
+} GSAuthError;
+
+typedef gboolean (* GSAuthMessageFunc) (GSAuthMessageStyle style,
+ const char *msg,
+ char **response,
+ gpointer data);
+
+#define GS_AUTH_ERROR gs_auth_error_quark ()
+
+GQuark gs_auth_error_quark (void);
+
+void gs_auth_set_verbose (gboolean verbose);
+gboolean gs_auth_get_verbose (void);
+
+gboolean gs_auth_priv_init (void);
+gboolean gs_auth_init (void);
+gboolean gs_auth_verify_user (const char *username,
+ const char *display,
+ GSAuthMessageFunc func,
+ gpointer data,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* __GS_AUTH_H */
diff --git a/src/gs-debug.c b/src/gs-debug.c
new file mode 100644
index 0000000..52fbc12
--- /dev/null
+++ b/src/gs-debug.c
@@ -0,0 +1,163 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "gs-debug.h"
+
+static gboolean debugging = FALSE;
+static FILE *debug_out = NULL;
+
+/* Based on rhythmbox/lib/rb-debug.c */
+/* Our own funky debugging function, should only be used when something
+ * is not going wrong, if something *is* wrong use g_warning.
+ */
+void
+gs_debug_real (const char *func,
+ const char *file,
+ const int line,
+ const char *format, ...)
+{
+ va_list args;
+ char buffer [1025];
+ char *str_time;
+ time_t the_time;
+
+ if (debugging == FALSE)
+ return;
+
+ va_start (args, format);
+
+ g_vsnprintf (buffer, 1024, format, args);
+
+ va_end (args);
+
+ time (&the_time);
+ str_time = g_new0 (char, 255);
+ strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
+
+ fprintf ((debug_out ? debug_out : stderr),
+ "[%s] %s:%d (%s):\t %s\n",
+ func, file, line, str_time, buffer);
+
+ if (debug_out)
+ fflush (debug_out);
+
+ g_free (str_time);
+}
+
+gboolean
+gs_debug_enabled (void)
+{
+ return debugging;
+}
+
+void
+gs_debug_init (gboolean debug,
+ gboolean to_file)
+{
+ /* return if already initialized */
+ if (debugging == TRUE)
+ {
+ return;
+ }
+
+ debugging = debug;
+
+ if (debug && to_file)
+ {
+ const char path [50] = "mate_screensaver_debug_XXXXXX";
+ int fd;
+
+ fd = g_file_open_tmp (path, NULL, NULL);
+
+ if (fd >= 0)
+ {
+ debug_out = fdopen (fd, "a");
+ }
+ }
+
+ gs_debug ("Debugging %s", (debug) ? "enabled" : "disabled");
+}
+
+void
+gs_debug_shutdown (void)
+{
+ if (! debugging)
+ return;
+
+ gs_debug ("Shutting down debugging");
+
+ debugging = FALSE;
+
+ if (debug_out != NULL)
+ {
+ fclose (debug_out);
+ debug_out = NULL;
+ }
+}
+
+void
+_gs_profile_log (const char *func,
+ const char *note,
+ const char *format,
+ ...)
+{
+ va_list args;
+ char *str;
+ char *formatted;
+
+ if (format == NULL)
+ {
+ formatted = g_strdup ("");
+ }
+ else
+ {
+ va_start (args, format);
+ formatted = g_strdup_vprintf (format, args);
+ va_end (args);
+ }
+
+ if (func != NULL)
+ {
+ str = g_strdup_printf ("MARK: %s %s: %s %s", g_get_prgname(), func, note ? note : "", formatted);
+ }
+ else
+ {
+ str = g_strdup_printf ("MARK: %s: %s %s", g_get_prgname(), note ? note : "", formatted);
+ }
+
+ g_free (formatted);
+
+ g_access (str, F_OK);
+ g_free (str);
+}
diff --git a/src/gs-debug.c.orig b/src/gs-debug.c.orig
new file mode 100644
index 0000000..ff80c0c
--- /dev/null
+++ b/src/gs-debug.c.orig
@@ -0,0 +1,163 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+#include <signal.h>
+#include <time.h>
+#include <unistd.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include "gs-debug.h"
+
+static gboolean debugging = FALSE;
+static FILE *debug_out = NULL;
+
+/* Based on rhythmbox/lib/rb-debug.c */
+/* Our own funky debugging function, should only be used when something
+ * is not going wrong, if something *is* wrong use g_warning.
+ */
+void
+gs_debug_real (const char *func,
+ const char *file,
+ const int line,
+ const char *format, ...)
+{
+ va_list args;
+ char buffer [1025];
+ char *str_time;
+ time_t the_time;
+
+ if (debugging == FALSE)
+ return;
+
+ va_start (args, format);
+
+ g_vsnprintf (buffer, 1024, format, args);
+
+ va_end (args);
+
+ time (&the_time);
+ str_time = g_new0 (char, 255);
+ strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
+
+ fprintf ((debug_out ? debug_out : stderr),
+ "[%s] %s:%d (%s):\t %s\n",
+ func, file, line, str_time, buffer);
+
+ if (debug_out)
+ fflush (debug_out);
+
+ g_free (str_time);
+}
+
+gboolean
+gs_debug_enabled (void)
+{
+ return debugging;
+}
+
+void
+gs_debug_init (gboolean debug,
+ gboolean to_file)
+{
+ /* return if already initialized */
+ if (debugging == TRUE)
+ {
+ return;
+ }
+
+ debugging = debug;
+
+ if (debug && to_file)
+ {
+ const char path [50] = "mate_screensaver_debug_XXXXXX";
+ int fd;
+
+ fd = g_file_open_tmp (path, NULL, NULL);
+
+ if (fd >= 0)
+ {
+ debug_out = fdopen (fd, "a");
+ }
+ }
+
+ gs_debug ("Debugging %s", (debug) ? "enabled" : "disabled");
+}
+
+void
+gs_debug_shutdown (void)
+{
+ if (! debugging)
+ return;
+
+ gs_debug ("Shutting down debugging");
+
+ debugging = FALSE;
+
+ if (debug_out != NULL)
+ {
+ fclose (debug_out);
+ debug_out = NULL;
+ }
+}
+
+void
+_gs_profile_log (const char *func,
+ const char *note,
+ const char *format,
+ ...)
+{
+ va_list args;
+ char *str;
+ char *formatted;
+
+ if (format == NULL)
+ {
+ formatted = g_strdup ("");
+ }
+ else
+ {
+ va_start (args, format);
+ formatted = g_strdup_vprintf (format, args);
+ va_end (args);
+ }
+
+ if (func != NULL)
+ {
+ str = g_strdup_printf ("MARK: %s %s: %s %s", g_get_prgname(), func, note ? note : "", formatted);
+ }
+ else
+ {
+ str = g_strdup_printf ("MARK: %s: %s %s", g_get_prgname(), note ? note : "", formatted);
+ }
+
+ g_free (formatted);
+
+ g_access (str, F_OK);
+ g_free (str);
+}
diff --git a/src/gs-debug.h b/src/gs-debug.h
new file mode 100644
index 0000000..fdf9fd8
--- /dev/null
+++ b/src/gs-debug.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_DEBUG_H
+#define __GS_DEBUG_H
+
+#include <stdarg.h>
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+#define gs_debug(...) gs_debug_real (__func__, __FILE__, __LINE__, __VA_ARGS__)
+#elif defined(__GNUC__) && __GNUC__ >= 3
+#define gs_debug(...) gs_debug_real (__FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
+#else
+#define gs_debug(...)
+#endif
+
+void gs_debug_init (gboolean debug,
+ gboolean to_file);
+gboolean gs_debug_enabled (void);
+void gs_debug_shutdown (void);
+void gs_debug_real (const char *func,
+ const char *file,
+ int line,
+ const char *format, ...);
+
+#ifdef ENABLE_PROFILING
+#ifdef G_HAVE_ISO_VARARGS
+#define gs_profile_start(...) _gs_profile_log (G_STRFUNC, "start", __VA_ARGS__)
+#define gs_profile_end(...) _gs_profile_log (G_STRFUNC, "end", __VA_ARGS__)
+#define gs_profile_msg(...) _gs_profile_log (NULL, NULL, __VA_ARGS__)
+#elif defined(G_HAVE_GNUC_VARARGS)
+#define gs_profile_start(format...) _gs_profile_log (G_STRFUNC, "start", format)
+#define gs_profile_end(format...) _gs_profile_log (G_STRFUNC, "end", format)
+#define gs_profile_msg(format...) _gs_profile_log (NULL, NULL, format)
+#endif
+#else
+#define gs_profile_start(...)
+#define gs_profile_end(...)
+#define gs_profile_msg(...)
+#endif
+
+void _gs_profile_log (const char *func,
+ const char *note,
+ const char *format,
+ ...) G_GNUC_PRINTF (3, 4);
+
+G_END_DECLS
+
+#endif /* __GS_DEBUG_H */
diff --git a/src/gs-fade.c b/src/gs-fade.c
new file mode 100644
index 0000000..ba04b67
--- /dev/null
+++ b/src/gs-fade.c
@@ -0,0 +1,987 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2009 William Jon McCann <[email protected]>
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <errno.h>
+
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+
+#include "gs-fade.h"
+#include "gs-debug.h"
+
+#define MATE_DESKTOP_USE_UNSTABLE_API
+
+#include "libmateui/mate-rr.h"
+
+/* XFree86 4.x+ Gamma fading */
+
+
+#ifdef HAVE_XF86VMODE_GAMMA
+
+#include <X11/extensions/xf86vmode.h>
+
+#define XF86_MIN_GAMMA 0.1
+
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void gs_fade_class_init (GSFadeClass *klass);
+static void gs_fade_init (GSFade *fade);
+static void gs_fade_finalize (GObject *object);
+
+#define GS_FADE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_FADE, GSFadePrivate))
+
+struct GSGammaInfo
+{
+ int size;
+ unsigned short *r;
+ unsigned short *g;
+ unsigned short *b;
+};
+
+struct GSFadeScreenPrivate
+{
+ int fade_type;
+ int num_ramps;
+ /* one per crtc in randr mode */
+ struct GSGammaInfo *info;
+ /* one per screen in theory */
+ MateRRScreen *rrscreen;
+#ifdef HAVE_XF86VMODE_GAMMA
+ /* one per screen also */
+ XF86VidModeGamma vmg;
+#endif /* HAVE_XF86VMODE_GAMMA */
+ gboolean (*fade_setup) (GSFade *fade,
+ int screen);
+ gboolean (*fade_set_alpha_gamma) (GSFade *fade,
+ int screen,
+ gdouble alpha);
+ void (*fade_finish) (GSFade *fade,
+ int screen);
+};
+
+struct GSFadePrivate
+{
+ guint enabled : 1;
+ guint active : 1;
+
+ guint timeout;
+
+ guint step;
+ guint num_steps;
+ guint timer_id;
+
+ gdouble alpha_per_iter;
+ gdouble current_alpha;
+
+ int num_screens;
+
+ struct GSFadeScreenPrivate *screen_priv;
+};
+
+enum
+{
+ FADED,
+ LAST_SIGNAL
+};
+
+enum
+{
+ FADE_TYPE_NONE,
+ FADE_TYPE_GAMMA_NUMBER,
+ FADE_TYPE_GAMMA_RAMP,
+ FADE_TYPE_XRANDR,
+};
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSFade, gs_fade, G_TYPE_OBJECT)
+
+static gpointer fade_object = NULL;
+
+#ifdef HAVE_XF86VMODE_GAMMA
+
+/* This is needed because the VidMode extension doesn't work
+ on remote displays -- but if the remote display has the extension
+ at all, XF86VidModeQueryExtension returns true, and then
+ XF86VidModeQueryVersion dies with an X error.
+*/
+
+static gboolean error_handler_hit = FALSE;
+
+static int
+ignore_all_errors_ehandler (Display *dpy,
+ XErrorEvent *error)
+{
+ error_handler_hit = TRUE;
+
+ return 0;
+}
+
+static Bool
+safe_XF86VidModeQueryVersion (Display *dpy,
+ int *majP,
+ int *minP)
+{
+ Bool result;
+ XErrorHandler old_handler;
+
+ XSync (dpy, False);
+ error_handler_hit = FALSE;
+ old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
+
+ result = XF86VidModeQueryVersion (dpy, majP, minP);
+
+ XSync (dpy, False);
+ XSetErrorHandler (old_handler);
+ XSync (dpy, False);
+
+ return (error_handler_hit
+ ? False
+ : result);
+}
+
+static gboolean
+xf86_whack_gamma (int screen,
+ struct GSFadeScreenPrivate *screen_priv,
+ float ratio)
+{
+ Bool status;
+ struct GSGammaInfo *gamma_info;
+
+ gamma_info = screen_priv->info;
+
+ if (!gamma_info)
+ return FALSE;
+
+ if (ratio < 0)
+ {
+ ratio = 0;
+ }
+ if (ratio > 1)
+ {
+ ratio = 1;
+ }
+
+ if (gamma_info->size == 0)
+ {
+ /* we only have a gamma number, not a ramp. */
+
+ XF86VidModeGamma g2;
+
+ g2.red = screen_priv->vmg.red * ratio;
+ g2.green = screen_priv->vmg.green * ratio;
+ g2.blue = screen_priv->vmg.blue * ratio;
+
+ if (g2.red < XF86_MIN_GAMMA)
+ {
+ g2.red = XF86_MIN_GAMMA;
+ }
+ if (g2.green < XF86_MIN_GAMMA)
+ {
+ g2.green = XF86_MIN_GAMMA;
+ }
+ if (g2.blue < XF86_MIN_GAMMA)
+ {
+ g2.blue = XF86_MIN_GAMMA;
+ }
+
+ status = XF86VidModeSetGamma (GDK_DISPLAY (), screen, &g2);
+ }
+ else
+ {
+
+# ifdef HAVE_XF86VMODE_GAMMA_RAMP
+ unsigned short *r, *g, *b;
+ int i;
+
+ r = g_new0 (unsigned short, gamma_info->size);
+ g = g_new0 (unsigned short, gamma_info->size);
+ b = g_new0 (unsigned short, gamma_info->size);
+
+ for (i = 0; i < gamma_info->size; i++)
+ {
+ r[i] = gamma_info->r[i] * ratio;
+ g[i] = gamma_info->g[i] * ratio;
+ b[i] = gamma_info->b[i] * ratio;
+ }
+
+ status = XF86VidModeSetGammaRamp (GDK_DISPLAY (), screen, gamma_info->size, r, g, b);
+
+ g_free (r);
+ g_free (g);
+ g_free (b);
+
+# else /* !HAVE_XF86VMODE_GAMMA_RAMP */
+ abort ();
+# endif /* !HAVE_XF86VMODE_GAMMA_RAMP */
+ }
+
+ gdk_flush ();
+
+ return status;
+}
+
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+/* VidModeExtension version 2.0 or better is needed to do gamma.
+ 2.0 added gamma values; 2.1 added gamma ramps.
+*/
+# define XF86_VIDMODE_GAMMA_MIN_MAJOR 2
+# define XF86_VIDMODE_GAMMA_MIN_MINOR 0
+# define XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR 2
+# define XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR 1
+
+
+gboolean
+gs_fade_get_enabled (GSFade *fade)
+{
+ g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
+
+ return fade->priv->enabled;
+}
+
+void
+gs_fade_set_enabled (GSFade *fade,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ if (fade->priv->enabled != enabled)
+ {
+ fade->priv->enabled = enabled;
+ }
+}
+
+#ifdef HAVE_XF86VMODE_GAMMA
+static gboolean
+gamma_fade_setup (GSFade *fade, int screen_idx)
+{
+ gboolean res;
+ struct GSFadeScreenPrivate *screen_priv;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (screen_priv->info)
+ return TRUE;
+
+# ifndef HAVE_XF86VMODE_GAMMA_RAMP
+ if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
+ {
+ /* server is newer than client! */
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ }
+# endif
+
+# ifdef HAVE_XF86VMODE_GAMMA_RAMP
+
+ screen_priv->info = g_new0(struct GSGammaInfo, 1);
+ screen_priv->num_ramps = 1;
+
+ if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
+ {
+ /* have ramps */
+
+ res = XF86VidModeGetGammaRampSize (GDK_DISPLAY (), screen_idx, &screen_priv->info->size);
+ if (!res || screen_priv->info->size <= 0)
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+
+ screen_priv->info->r = g_new0 (unsigned short, screen_priv->info->size);
+ screen_priv->info->g = g_new0 (unsigned short, screen_priv->info->size);
+ screen_priv->info->b = g_new0 (unsigned short, screen_priv->info->size);
+
+ if (! (screen_priv->info->r && screen_priv->info->g && screen_priv->info->b))
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+
+ res = XF86VidModeGetGammaRamp (GDK_DISPLAY (),
+ screen_idx,
+ screen_priv->info->size,
+ screen_priv->info->r,
+ screen_priv->info->g,
+ screen_priv->info->b);
+ if (! res)
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+ gs_debug ("Initialized gamma ramp fade");
+ }
+# endif /* HAVE_XF86VMODE_GAMMA_RAMP */
+
+test_number:
+ if (FADE_TYPE_GAMMA_NUMBER == screen_priv->fade_type)
+ {
+ /* only have gamma parameter, not ramps. */
+
+ res = XF86VidModeGetGamma (GDK_DISPLAY (), screen_idx, &screen_priv->vmg);
+ if (! res)
+ {
+ screen_priv->fade_type = FADE_TYPE_NONE;
+ goto test_none;
+ }
+ gs_debug ("Initialized gamma fade for screen %d: %f %f %f",
+ screen_idx,
+ screen_priv->vmg.red,
+ screen_priv->vmg.green,
+ screen_priv->vmg.blue);
+ }
+
+test_none:
+ if (FADE_TYPE_NONE == screen_priv->fade_type)
+ {
+ goto FAIL;
+ }
+
+ return TRUE;
+FAIL:
+
+ return FALSE;
+}
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void
+screen_fade_finish (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ int i;
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (!screen_priv->info)
+ return;
+
+ for (i = 0; i < screen_priv->num_ramps; i++)
+ {
+ if (screen_priv->info[i].r)
+ g_free (screen_priv->info[i].r);
+ if (screen_priv->info[i].g)
+ g_free (screen_priv->info[i].g);
+ if (screen_priv->info[i].b)
+ g_free (screen_priv->info[i].b);
+ }
+
+ g_free (screen_priv->info);
+ screen_priv->info = NULL;
+ screen_priv->num_ramps = 0;
+}
+
+#ifdef HAVE_XF86VMODE_GAMMA
+static gboolean
+gamma_fade_set_alpha_gamma (GSFade *fade,
+ int screen_idx,
+ gdouble alpha)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ gboolean res;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+ res = xf86_whack_gamma (screen_idx, screen_priv, alpha);
+
+ return TRUE;
+}
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void
+check_gamma_extension (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+#ifdef HAVE_XF86VMODE_GAMMA
+ int event;
+ int error;
+ int major;
+ int minor;
+ gboolean res;
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+#ifdef HAVE_XF86VMODE_GAMMA
+ res = XF86VidModeQueryExtension (GDK_DISPLAY (), &event, &error);
+ if (! res)
+ goto fade_none;
+
+ res = safe_XF86VidModeQueryVersion (GDK_DISPLAY (), &major, &minor);
+ if (! res)
+ goto fade_none;
+
+ if (major < XF86_VIDMODE_GAMMA_MIN_MAJOR ||
+ (major == XF86_VIDMODE_GAMMA_MIN_MAJOR &&
+ minor < XF86_VIDMODE_GAMMA_MIN_MINOR))
+ goto fade_none;
+
+ screen_priv->fade_setup = gamma_fade_setup;
+ screen_priv->fade_finish = screen_fade_finish;
+ screen_priv->fade_set_alpha_gamma = gamma_fade_set_alpha_gamma;
+
+ if (major < XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR ||
+ (major == XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR &&
+ minor < XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR))
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ return;
+ }
+
+ /* Copacetic */
+ screen_priv->fade_type = FADE_TYPE_GAMMA_RAMP;
+ return;
+fade_none:
+#endif
+ screen_priv->fade_type = FADE_TYPE_NONE;
+}
+
+/* Xrandr support */
+
+static gboolean xrandr_fade_setup (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ MateRRCrtc *crtc;
+ MateRRCrtc **crtcs;
+ int crtc_count = 0;
+ struct GSGammaInfo *info;
+ gboolean res;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (screen_priv->info)
+ return TRUE;
+
+ /* refresh the screen info */
+ mate_rr_screen_refresh (screen_priv->rrscreen, NULL);
+
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ while (*crtcs)
+ {
+ crtc_count++;
+ crtcs++;
+ };
+
+ screen_priv->info = g_new0 (struct GSGammaInfo, crtc_count);
+ screen_priv->num_ramps = crtc_count;
+
+ crtc_count = 0;
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ while (*crtcs)
+ {
+ crtc = *crtcs;
+
+ info = &screen_priv->info[crtc_count];
+
+ /* if no mode ignore crtc */
+ if (!mate_rr_crtc_get_current_mode (crtc))
+ {
+ info->size = 0;
+ info->r = NULL;
+ info->g = NULL;
+ info->b = NULL;
+ }
+ else
+ {
+ res = mate_rr_crtc_get_gamma (crtc, &info->size,
+ &info->r, &info->g,
+ &info->b);
+ if (res == FALSE)
+ goto fail;
+ }
+
+ crtcs++;
+ crtc_count++;
+ }
+ return TRUE;
+fail:
+ return FALSE;
+}
+
+static void xrandr_crtc_whack_gamma (MateRRCrtc *crtc,
+ struct GSGammaInfo *gamma_info,
+ float ratio)
+{
+ unsigned short *r, *g, *b;
+ int i;
+
+ if (gamma_info->size == 0)
+ return;
+
+ if (ratio < 0)
+ {
+ ratio = 0;
+ }
+ if (ratio > 1)
+ {
+ ratio = 1;
+ }
+
+ r = g_new0 (unsigned short, gamma_info->size);
+ g = g_new0 (unsigned short, gamma_info->size);
+ b = g_new0 (unsigned short, gamma_info->size);
+
+ for (i = 0; i < gamma_info->size; i++)
+ {
+ r[i] = gamma_info->r[i] * ratio;
+ g[i] = gamma_info->g[i] * ratio;
+ b[i] = gamma_info->b[i] * ratio;
+ }
+
+ mate_rr_crtc_set_gamma (crtc, gamma_info->size,
+ r, g, b);
+ g_free (r);
+ g_free (g);
+ g_free (b);
+}
+
+static gboolean xrandr_fade_set_alpha_gamma (GSFade *fade,
+ int screen_idx,
+ gdouble alpha)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ struct GSGammaInfo *info;
+ MateRRCrtc **crtcs;
+ int i;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (!screen_priv->info)
+ return FALSE;
+
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ i = 0;
+
+ while (*crtcs)
+ {
+ info = &screen_priv->info[i];
+ xrandr_crtc_whack_gamma (*crtcs, info, alpha);
+ i++;
+ crtcs++;
+ }
+ return TRUE;
+}
+
+static void
+check_randr_extension (GSFade *fade, int screen_idx)
+{
+ GdkDisplay *display = gdk_display_get_default ();
+ GdkScreen *screen = gdk_display_get_screen (display, screen_idx);
+ struct GSFadeScreenPrivate *screen_priv;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ screen_priv->rrscreen = mate_rr_screen_new (screen,
+ NULL,
+ NULL,
+ NULL);
+ if (!screen_priv->rrscreen)
+ {
+ screen_priv->fade_type = FADE_TYPE_NONE;
+ return;
+ }
+
+ screen_priv->fade_type = FADE_TYPE_XRANDR;
+ screen_priv->fade_setup = xrandr_fade_setup;
+ screen_priv->fade_finish = screen_fade_finish;
+ screen_priv->fade_set_alpha_gamma = xrandr_fade_set_alpha_gamma;
+}
+
+static gboolean
+gs_fade_set_alpha (GSFade *fade,
+ gdouble alpha)
+{
+ gboolean ret = FALSE;
+ int i;
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ switch (fade->priv->screen_priv[i].fade_type)
+ {
+ case FADE_TYPE_GAMMA_RAMP:
+ case FADE_TYPE_GAMMA_NUMBER:
+ case FADE_TYPE_XRANDR:
+ ret = fade->priv->screen_priv[i].fade_set_alpha_gamma (fade, i, alpha);
+ break;
+ case FADE_TYPE_NONE:
+ ret = FALSE;
+ break;
+ default:
+ g_warning ("Unknown fade type");
+ ret = FALSE;
+ break;
+ }
+ }
+ return ret;
+}
+
+static gboolean
+gs_fade_out_iter (GSFade *fade)
+{
+ gboolean ret;
+
+ if (fade->priv->current_alpha < 0.01)
+ {
+ return FALSE;
+ }
+
+ fade->priv->current_alpha -= fade->priv->alpha_per_iter;
+
+ ret = gs_fade_set_alpha (fade, fade->priv->current_alpha);
+
+ return ret;
+}
+
+static gboolean
+gs_fade_stop (GSFade *fade)
+{
+ if (fade->priv->timer_id > 0)
+ {
+ g_source_remove (fade->priv->timer_id);
+ fade->priv->timer_id = 0;
+ }
+
+ fade->priv->step = 0;
+ fade->priv->active = FALSE;
+
+ return TRUE;
+}
+
+void
+gs_fade_finish (GSFade *fade)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ if (! fade->priv->active)
+ {
+ return;
+ }
+
+ gs_fade_stop (fade);
+
+ g_signal_emit (fade, signals [FADED], 0);
+
+ fade->priv->active = FALSE;
+}
+
+static gboolean
+fade_out_timer (GSFade *fade)
+{
+ gboolean res;
+
+ res = gs_fade_out_iter (fade);
+
+ /* if failed then fade is complete */
+ if (! res)
+ {
+ gs_fade_finish (fade);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_fade_get_active (GSFade *fade)
+{
+ g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
+
+ return fade->priv->active;
+}
+
+static void
+gs_fade_set_timeout (GSFade *fade,
+ guint timeout)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ fade->priv->timeout = timeout;
+}
+
+static void
+gs_fade_start (GSFade *fade,
+ guint timeout)
+{
+ guint steps_per_sec = 30;
+ guint msecs_per_step;
+ struct GSFadeScreenPrivate *screen_priv;
+ gboolean active_fade, res;
+ int i;
+
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ screen_priv = &fade->priv->screen_priv[i];
+ if (screen_priv->fade_type != FADE_TYPE_NONE)
+ {
+ res = screen_priv->fade_setup (fade, i);
+ if (res == FALSE)
+ return;
+ }
+ }
+
+ if (fade->priv->timer_id > 0)
+ {
+ gs_fade_stop (fade);
+ }
+
+ fade->priv->active = TRUE;
+
+ gs_fade_set_timeout (fade, timeout);
+
+ active_fade = FALSE;
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ screen_priv = &fade->priv->screen_priv[i];
+ if (screen_priv->fade_type != FADE_TYPE_NONE)
+ active_fade = TRUE;
+ }
+ if (active_fade)
+ {
+ guint num_steps;
+
+ num_steps = (fade->priv->timeout / 1000) * steps_per_sec;
+ msecs_per_step = 1000 / steps_per_sec;
+ fade->priv->alpha_per_iter = 1.0 / (gdouble)num_steps;
+
+ fade->priv->timer_id = g_timeout_add (msecs_per_step, (GSourceFunc)fade_out_timer, fade);
+ }
+ else
+ {
+ gs_fade_finish (fade);
+ }
+}
+
+typedef struct
+{
+ GSFadeDoneFunc done_cb;
+ gpointer data;
+} FadedCallbackData;
+
+static void
+gs_fade_async_callback (GSFade *fade,
+ FadedCallbackData *cdata)
+{
+ g_signal_handlers_disconnect_by_func (fade,
+ gs_fade_async_callback,
+ cdata);
+
+ if (cdata->done_cb)
+ {
+ cdata->done_cb (fade, cdata->data);
+ }
+
+ g_free (cdata);
+}
+
+void
+gs_fade_async (GSFade *fade,
+ guint timeout,
+ GSFadeDoneFunc func,
+ gpointer data)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ /* if fade is active then pause it */
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ if (func)
+ {
+ FadedCallbackData *cb_data;
+
+ cb_data = g_new0 (FadedCallbackData, 1);
+ cb_data->done_cb = func;
+ cb_data->data = data;
+
+ g_signal_connect (fade, "faded",
+ G_CALLBACK (gs_fade_async_callback),
+ cb_data);
+ }
+
+ gs_fade_start (fade, timeout);
+}
+
+static void
+gs_fade_sync_callback (GSFade *fade,
+ int *flag)
+{
+ *flag = TRUE;
+ g_signal_handlers_disconnect_by_func (fade,
+ gs_fade_sync_callback,
+ flag);
+}
+
+void
+gs_fade_sync (GSFade *fade,
+ guint timeout)
+{
+ int flag = FALSE;
+
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ /* if fade is active then pause it */
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ g_signal_connect (fade, "faded",
+ G_CALLBACK (gs_fade_sync_callback),
+ &flag);
+
+ gs_fade_start (fade, timeout);
+
+ while (! flag)
+ {
+ gtk_main_iteration ();
+ }
+}
+
+void
+gs_fade_reset (GSFade *fade)
+{
+ int i;
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ gs_debug ("Resetting fade");
+
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ fade->priv->current_alpha = 1.0;
+
+ gs_fade_set_alpha (fade, fade->priv->current_alpha);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ fade->priv->screen_priv[i].fade_finish (fade, i);
+}
+
+static void
+gs_fade_class_init (GSFadeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_fade_finalize;
+
+ signals [FADED] =
+ g_signal_new ("faded",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSFadeClass, faded),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0, G_TYPE_NONE);
+
+ g_type_class_add_private (klass, sizeof (GSFadePrivate));
+}
+
+static void
+gs_fade_init (GSFade *fade)
+{
+ GdkDisplay *display;
+ int i;
+
+ fade->priv = GS_FADE_GET_PRIVATE (fade);
+
+ fade->priv->timeout = 1000;
+ fade->priv->current_alpha = 1.0;
+
+ display = gdk_display_get_default ();
+ fade->priv->num_screens = gdk_display_get_n_screens (display);
+
+ fade->priv->screen_priv = g_new0 (struct GSFadeScreenPrivate, fade->priv->num_screens);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ check_randr_extension (fade, i);
+ if (!fade->priv->screen_priv[i].fade_type)
+ check_gamma_extension (fade, i);
+ gs_debug ("Fade type: %d", fade->priv->screen_priv[i].fade_type);
+ }
+}
+
+static void
+gs_fade_finalize (GObject *object)
+{
+ GSFade *fade;
+ int i;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_FADE (object));
+
+ fade = GS_FADE (object);
+
+ g_return_if_fail (fade->priv != NULL);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ fade->priv->screen_priv[i].fade_finish(fade, i);
+
+ if (fade->priv->screen_priv)
+ {
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ if (!fade->priv->screen_priv[i].rrscreen)
+ continue;
+ mate_rr_screen_destroy (fade->priv->screen_priv[i].rrscreen);
+ }
+
+ g_free (fade->priv->screen_priv);
+ fade->priv->screen_priv = NULL;
+ }
+
+ G_OBJECT_CLASS (gs_fade_parent_class)->finalize (object);
+}
+
+GSFade *
+gs_fade_new (void)
+{
+ if (fade_object)
+ {
+ g_object_ref (fade_object);
+ }
+ else
+ {
+ fade_object = g_object_new (GS_TYPE_FADE, NULL);
+ g_object_add_weak_pointer (fade_object,
+ (gpointer *) &fade_object);
+ }
+
+ return GS_FADE (fade_object);
+}
diff --git a/src/gs-fade.c.orig b/src/gs-fade.c.orig
new file mode 100644
index 0000000..3b46004
--- /dev/null
+++ b/src/gs-fade.c.orig
@@ -0,0 +1,987 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2009 William Jon McCann <[email protected]>
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <errno.h>
+
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+
+#include "gs-fade.h"
+#include "gs-debug.h"
+
+#define MATE_DESKTOP_USE_UNSTABLE_API
+
+#include "libmateui/mate-rr.h"
+
+/* XFree86 4.x+ Gamma fading */
+
+
+#ifdef HAVE_XF86VMODE_GAMMA
+
+#include <X11/extensions/xf86vmode.h>
+
+#define XF86_MIN_GAMMA 0.1
+
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void gs_fade_class_init (GSFadeClass *klass);
+static void gs_fade_init (GSFade *fade);
+static void gs_fade_finalize (GObject *object);
+
+#define GS_FADE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_FADE, GSFadePrivate))
+
+struct GSGammaInfo
+{
+ int size;
+ unsigned short *r;
+ unsigned short *g;
+ unsigned short *b;
+};
+
+struct GSFadeScreenPrivate
+{
+ int fade_type;
+ int num_ramps;
+ /* one per crtc in randr mode */
+ struct GSGammaInfo *info;
+ /* one per screen in theory */
+ MateRRScreen *rrscreen;
+#ifdef HAVE_XF86VMODE_GAMMA
+ /* one per screen also */
+ XF86VidModeGamma vmg;
+#endif /* HAVE_XF86VMODE_GAMMA */
+ gboolean (*fade_setup) (GSFade *fade,
+ int screen);
+ gboolean (*fade_set_alpha_gamma) (GSFade *fade,
+ int screen,
+ gdouble alpha);
+ void (*fade_finish) (GSFade *fade,
+ int screen);
+};
+
+struct GSFadePrivate
+{
+ guint enabled : 1;
+ guint active : 1;
+
+ guint timeout;
+
+ guint step;
+ guint num_steps;
+ guint timer_id;
+
+ gdouble alpha_per_iter;
+ gdouble current_alpha;
+
+ int num_screens;
+
+ struct GSFadeScreenPrivate *screen_priv;
+};
+
+enum
+{
+ FADED,
+ LAST_SIGNAL
+};
+
+enum
+{
+ FADE_TYPE_NONE,
+ FADE_TYPE_GAMMA_NUMBER,
+ FADE_TYPE_GAMMA_RAMP,
+ FADE_TYPE_XRANDR,
+};
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSFade, gs_fade, G_TYPE_OBJECT)
+
+static gpointer fade_object = NULL;
+
+#ifdef HAVE_XF86VMODE_GAMMA
+
+/* This is needed because the VidMode extension doesn't work
+ on remote displays -- but if the remote display has the extension
+ at all, XF86VidModeQueryExtension returns true, and then
+ XF86VidModeQueryVersion dies with an X error.
+*/
+
+static gboolean error_handler_hit = FALSE;
+
+static int
+ignore_all_errors_ehandler (Display *dpy,
+ XErrorEvent *error)
+{
+ error_handler_hit = TRUE;
+
+ return 0;
+}
+
+static Bool
+safe_XF86VidModeQueryVersion (Display *dpy,
+ int *majP,
+ int *minP)
+{
+ Bool result;
+ XErrorHandler old_handler;
+
+ XSync (dpy, False);
+ error_handler_hit = FALSE;
+ old_handler = XSetErrorHandler (ignore_all_errors_ehandler);
+
+ result = XF86VidModeQueryVersion (dpy, majP, minP);
+
+ XSync (dpy, False);
+ XSetErrorHandler (old_handler);
+ XSync (dpy, False);
+
+ return (error_handler_hit
+ ? False
+ : result);
+}
+
+static gboolean
+xf86_whack_gamma (int screen,
+ struct GSFadeScreenPrivate *screen_priv,
+ float ratio)
+{
+ Bool status;
+ struct GSGammaInfo *gamma_info;
+
+ gamma_info = screen_priv->info;
+
+ if (!gamma_info)
+ return FALSE;
+
+ if (ratio < 0)
+ {
+ ratio = 0;
+ }
+ if (ratio > 1)
+ {
+ ratio = 1;
+ }
+
+ if (gamma_info->size == 0)
+ {
+ /* we only have a gamma number, not a ramp. */
+
+ XF86VidModeGamma g2;
+
+ g2.red = screen_priv->vmg.red * ratio;
+ g2.green = screen_priv->vmg.green * ratio;
+ g2.blue = screen_priv->vmg.blue * ratio;
+
+ if (g2.red < XF86_MIN_GAMMA)
+ {
+ g2.red = XF86_MIN_GAMMA;
+ }
+ if (g2.green < XF86_MIN_GAMMA)
+ {
+ g2.green = XF86_MIN_GAMMA;
+ }
+ if (g2.blue < XF86_MIN_GAMMA)
+ {
+ g2.blue = XF86_MIN_GAMMA;
+ }
+
+ status = XF86VidModeSetGamma (GDK_DISPLAY (), screen, &g2);
+ }
+ else
+ {
+
+# ifdef HAVE_XF86VMODE_GAMMA_RAMP
+ unsigned short *r, *g, *b;
+ int i;
+
+ r = g_new0 (unsigned short, gamma_info->size);
+ g = g_new0 (unsigned short, gamma_info->size);
+ b = g_new0 (unsigned short, gamma_info->size);
+
+ for (i = 0; i < gamma_info->size; i++)
+ {
+ r[i] = gamma_info->r[i] * ratio;
+ g[i] = gamma_info->g[i] * ratio;
+ b[i] = gamma_info->b[i] * ratio;
+ }
+
+ status = XF86VidModeSetGammaRamp (GDK_DISPLAY (), screen, gamma_info->size, r, g, b);
+
+ g_free (r);
+ g_free (g);
+ g_free (b);
+
+# else /* !HAVE_XF86VMODE_GAMMA_RAMP */
+ abort ();
+# endif /* !HAVE_XF86VMODE_GAMMA_RAMP */
+ }
+
+ gdk_flush ();
+
+ return status;
+}
+
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+/* VidModeExtension version 2.0 or better is needed to do gamma.
+ 2.0 added gamma values; 2.1 added gamma ramps.
+*/
+# define XF86_VIDMODE_GAMMA_MIN_MAJOR 2
+# define XF86_VIDMODE_GAMMA_MIN_MINOR 0
+# define XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR 2
+# define XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR 1
+
+
+gboolean
+gs_fade_get_enabled (GSFade *fade)
+{
+ g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
+
+ return fade->priv->enabled;
+}
+
+void
+gs_fade_set_enabled (GSFade *fade,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ if (fade->priv->enabled != enabled)
+ {
+ fade->priv->enabled = enabled;
+ }
+}
+
+#ifdef HAVE_XF86VMODE_GAMMA
+static gboolean
+gamma_fade_setup (GSFade *fade, int screen_idx)
+{
+ gboolean res;
+ struct GSFadeScreenPrivate *screen_priv;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (screen_priv->info)
+ return TRUE;
+
+# ifndef HAVE_XF86VMODE_GAMMA_RAMP
+ if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
+ {
+ /* server is newer than client! */
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ }
+# endif
+
+# ifdef HAVE_XF86VMODE_GAMMA_RAMP
+
+ screen_priv->info = g_new0(struct GSGammaInfo, 1);
+ screen_priv->num_ramps = 1;
+
+ if (FADE_TYPE_GAMMA_RAMP == screen_priv->fade_type)
+ {
+ /* have ramps */
+
+ res = XF86VidModeGetGammaRampSize (GDK_DISPLAY (), screen_idx, &screen_priv->info->size);
+ if (!res || screen_priv->info->size <= 0)
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+
+ screen_priv->info->r = g_new0 (unsigned short, screen_priv->info->size);
+ screen_priv->info->g = g_new0 (unsigned short, screen_priv->info->size);
+ screen_priv->info->b = g_new0 (unsigned short, screen_priv->info->size);
+
+ if (! (screen_priv->info->r && screen_priv->info->g && screen_priv->info->b))
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+
+ res = XF86VidModeGetGammaRamp (GDK_DISPLAY (),
+ screen_idx,
+ screen_priv->info->size,
+ screen_priv->info->r,
+ screen_priv->info->g,
+ screen_priv->info->b);
+ if (! res)
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ goto test_number;
+ }
+ gs_debug ("Initialized gamma ramp fade");
+ }
+# endif /* HAVE_XF86VMODE_GAMMA_RAMP */
+
+test_number:
+ if (FADE_TYPE_GAMMA_NUMBER == screen_priv->fade_type)
+ {
+ /* only have gamma parameter, not ramps. */
+
+ res = XF86VidModeGetGamma (GDK_DISPLAY (), screen_idx, &screen_priv->vmg);
+ if (! res)
+ {
+ screen_priv->fade_type = FADE_TYPE_NONE;
+ goto test_none;
+ }
+ gs_debug ("Initialized gamma fade for screen %d: %f %f %f",
+ screen_idx,
+ screen_priv->vmg.red,
+ screen_priv->vmg.green,
+ screen_priv->vmg.blue);
+ }
+
+test_none:
+ if (FADE_TYPE_NONE == screen_priv->fade_type)
+ {
+ goto FAIL;
+ }
+
+ return TRUE;
+FAIL:
+
+ return FALSE;
+}
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void
+screen_fade_finish (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ int i;
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (!screen_priv->info)
+ return;
+
+ for (i = 0; i < screen_priv->num_ramps; i++)
+ {
+ if (screen_priv->info[i].r)
+ g_free (screen_priv->info[i].r);
+ if (screen_priv->info[i].g)
+ g_free (screen_priv->info[i].g);
+ if (screen_priv->info[i].b)
+ g_free (screen_priv->info[i].b);
+ }
+
+ g_free (screen_priv->info);
+ screen_priv->info = NULL;
+ screen_priv->num_ramps = 0;
+}
+
+#ifdef HAVE_XF86VMODE_GAMMA
+static gboolean
+gamma_fade_set_alpha_gamma (GSFade *fade,
+ int screen_idx,
+ gdouble alpha)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ gboolean res;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+ res = xf86_whack_gamma (screen_idx, screen_priv, alpha);
+
+ return TRUE;
+}
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+static void
+check_gamma_extension (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+#ifdef HAVE_XF86VMODE_GAMMA
+ int event;
+ int error;
+ int major;
+ int minor;
+ gboolean res;
+#endif /* HAVE_XF86VMODE_GAMMA */
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+#ifdef HAVE_XF86VMODE_GAMMA
+ res = XF86VidModeQueryExtension (GDK_DISPLAY (), &event, &error);
+ if (! res)
+ goto fade_none;
+
+ res = safe_XF86VidModeQueryVersion (GDK_DISPLAY (), &major, &minor);
+ if (! res)
+ goto fade_none;
+
+ if (major < XF86_VIDMODE_GAMMA_MIN_MAJOR ||
+ (major == XF86_VIDMODE_GAMMA_MIN_MAJOR &&
+ minor < XF86_VIDMODE_GAMMA_MIN_MINOR))
+ goto fade_none;
+
+ screen_priv->fade_setup = gamma_fade_setup;
+ screen_priv->fade_finish = screen_fade_finish;
+ screen_priv->fade_set_alpha_gamma = gamma_fade_set_alpha_gamma;
+
+ if (major < XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR ||
+ (major == XF86_VIDMODE_GAMMA_RAMP_MIN_MAJOR &&
+ minor < XF86_VIDMODE_GAMMA_RAMP_MIN_MINOR))
+ {
+ screen_priv->fade_type = FADE_TYPE_GAMMA_NUMBER;
+ return;
+ }
+
+ /* Copacetic */
+ screen_priv->fade_type = FADE_TYPE_GAMMA_RAMP;
+ return;
+fade_none:
+#endif
+ screen_priv->fade_type = FADE_TYPE_NONE;
+}
+
+/* Xrandr support */
+
+static gboolean xrandr_fade_setup (GSFade *fade, int screen_idx)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ MateRRCrtc *crtc;
+ MateRRCrtc **crtcs;
+ int crtc_count = 0;
+ struct GSGammaInfo *info;
+ gboolean res;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (screen_priv->info)
+ return TRUE;
+
+ /* refresh the screen info */
+ mate_rr_screen_refresh (screen_priv->rrscreen, NULL);
+
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ while (*crtcs)
+ {
+ crtc_count++;
+ crtcs++;
+ };
+
+ screen_priv->info = g_new0 (struct GSGammaInfo, crtc_count);
+ screen_priv->num_ramps = crtc_count;
+
+ crtc_count = 0;
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ while (*crtcs)
+ {
+ crtc = *crtcs;
+
+ info = &screen_priv->info[crtc_count];
+
+ /* if no mode ignore crtc */
+ if (!mate_rr_crtc_get_current_mode (crtc))
+ {
+ info->size = 0;
+ info->r = NULL;
+ info->g = NULL;
+ info->b = NULL;
+ }
+ else
+ {
+ res = mate_rr_crtc_get_gamma (crtc, &info->size,
+ &info->r, &info->g,
+ &info->b);
+ if (res == FALSE)
+ goto fail;
+ }
+
+ crtcs++;
+ crtc_count++;
+ }
+ return TRUE;
+fail:
+ return FALSE;
+}
+
+static void xrandr_crtc_whack_gamma (MateRRCrtc *crtc,
+ struct GSGammaInfo *gamma_info,
+ float ratio)
+{
+ unsigned short *r, *g, *b;
+ int i;
+
+ if (gamma_info->size == 0)
+ return;
+
+ if (ratio < 0)
+ {
+ ratio = 0;
+ }
+ if (ratio > 1)
+ {
+ ratio = 1;
+ }
+
+ r = g_new0 (unsigned short, gamma_info->size);
+ g = g_new0 (unsigned short, gamma_info->size);
+ b = g_new0 (unsigned short, gamma_info->size);
+
+ for (i = 0; i < gamma_info->size; i++)
+ {
+ r[i] = gamma_info->r[i] * ratio;
+ g[i] = gamma_info->g[i] * ratio;
+ b[i] = gamma_info->b[i] * ratio;
+ }
+
+ mate_rr_crtc_set_gamma (crtc, gamma_info->size,
+ r, g, b);
+ g_free (r);
+ g_free (g);
+ g_free (b);
+}
+
+static gboolean xrandr_fade_set_alpha_gamma (GSFade *fade,
+ int screen_idx,
+ gdouble alpha)
+{
+ struct GSFadeScreenPrivate *screen_priv;
+ struct GSGammaInfo *info;
+ MateRRCrtc **crtcs;
+ int i;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ if (!screen_priv->info)
+ return FALSE;
+
+ crtcs = mate_rr_screen_list_crtcs (screen_priv->rrscreen);
+ i = 0;
+
+ while (*crtcs)
+ {
+ info = &screen_priv->info[i];
+ xrandr_crtc_whack_gamma (*crtcs, info, alpha);
+ i++;
+ crtcs++;
+ }
+ return TRUE;
+}
+
+static void
+check_randr_extension (GSFade *fade, int screen_idx)
+{
+ GdkDisplay *display = gdk_display_get_default ();
+ GdkScreen *screen = gdk_display_get_screen (display, screen_idx);
+ struct GSFadeScreenPrivate *screen_priv;
+
+ screen_priv = &fade->priv->screen_priv[screen_idx];
+
+ screen_priv->rrscreen = mate_rr_screen_new (screen,
+ NULL,
+ NULL,
+ NULL);
+ if (!screen_priv->rrscreen)
+ {
+ screen_priv->fade_type = FADE_TYPE_NONE;
+ return;
+ }
+
+ screen_priv->fade_type = FADE_TYPE_XRANDR;
+ screen_priv->fade_setup = xrandr_fade_setup;
+ screen_priv->fade_finish = screen_fade_finish;
+ screen_priv->fade_set_alpha_gamma = xrandr_fade_set_alpha_gamma;
+}
+
+static gboolean
+gs_fade_set_alpha (GSFade *fade,
+ gdouble alpha)
+{
+ gboolean ret = FALSE;
+ int i;
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ switch (fade->priv->screen_priv[i].fade_type)
+ {
+ case FADE_TYPE_GAMMA_RAMP:
+ case FADE_TYPE_GAMMA_NUMBER:
+ case FADE_TYPE_XRANDR:
+ ret = fade->priv->screen_priv[i].fade_set_alpha_gamma (fade, i, alpha);
+ break;
+ case FADE_TYPE_NONE:
+ ret = FALSE;
+ break;
+ default:
+ g_warning ("Unknown fade type");
+ ret = FALSE;
+ break;
+ }
+ }
+ return ret;
+}
+
+static gboolean
+gs_fade_out_iter (GSFade *fade)
+{
+ gboolean ret;
+
+ if (fade->priv->current_alpha < 0.01)
+ {
+ return FALSE;
+ }
+
+ fade->priv->current_alpha -= fade->priv->alpha_per_iter;
+
+ ret = gs_fade_set_alpha (fade, fade->priv->current_alpha);
+
+ return ret;
+}
+
+static gboolean
+gs_fade_stop (GSFade *fade)
+{
+ if (fade->priv->timer_id > 0)
+ {
+ g_source_remove (fade->priv->timer_id);
+ fade->priv->timer_id = 0;
+ }
+
+ fade->priv->step = 0;
+ fade->priv->active = FALSE;
+
+ return TRUE;
+}
+
+void
+gs_fade_finish (GSFade *fade)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ if (! fade->priv->active)
+ {
+ return;
+ }
+
+ gs_fade_stop (fade);
+
+ g_signal_emit (fade, signals [FADED], 0);
+
+ fade->priv->active = FALSE;
+}
+
+static gboolean
+fade_out_timer (GSFade *fade)
+{
+ gboolean res;
+
+ res = gs_fade_out_iter (fade);
+
+ /* if failed then fade is complete */
+ if (! res)
+ {
+ gs_fade_finish (fade);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_fade_get_active (GSFade *fade)
+{
+ g_return_val_if_fail (GS_IS_FADE (fade), FALSE);
+
+ return fade->priv->active;
+}
+
+static void
+gs_fade_set_timeout (GSFade *fade,
+ guint timeout)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ fade->priv->timeout = timeout;
+}
+
+static void
+gs_fade_start (GSFade *fade,
+ guint timeout)
+{
+ guint steps_per_sec = 30;
+ guint msecs_per_step;
+ struct GSFadeScreenPrivate *screen_priv;
+ gboolean active_fade, res;
+ int i;
+
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ screen_priv = &fade->priv->screen_priv[i];
+ if (screen_priv->fade_type != FADE_TYPE_NONE)
+ {
+ res = screen_priv->fade_setup (fade, i);
+ if (res == FALSE)
+ return;
+ }
+ }
+
+ if (fade->priv->timer_id > 0)
+ {
+ gs_fade_stop (fade);
+ }
+
+ fade->priv->active = TRUE;
+
+ gs_fade_set_timeout (fade, timeout);
+
+ active_fade = FALSE;
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ screen_priv = &fade->priv->screen_priv[i];
+ if (screen_priv->fade_type != FADE_TYPE_NONE)
+ active_fade = TRUE;
+ }
+ if (active_fade)
+ {
+ guint num_steps;
+
+ num_steps = (fade->priv->timeout / 1000) * steps_per_sec;
+ msecs_per_step = 1000 / steps_per_sec;
+ fade->priv->alpha_per_iter = 1.0 / (gdouble)num_steps;
+
+ fade->priv->timer_id = g_timeout_add (msecs_per_step, (GSourceFunc)fade_out_timer, fade);
+ }
+ else
+ {
+ gs_fade_finish (fade);
+ }
+}
+
+typedef struct
+{
+ GSFadeDoneFunc done_cb;
+ gpointer data;
+} FadedCallbackData;
+
+static void
+gs_fade_async_callback (GSFade *fade,
+ FadedCallbackData *cdata)
+{
+ g_signal_handlers_disconnect_by_func (fade,
+ gs_fade_async_callback,
+ cdata);
+
+ if (cdata->done_cb)
+ {
+ cdata->done_cb (fade, cdata->data);
+ }
+
+ g_free (cdata);
+}
+
+void
+gs_fade_async (GSFade *fade,
+ guint timeout,
+ GSFadeDoneFunc func,
+ gpointer data)
+{
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ /* if fade is active then pause it */
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ if (func)
+ {
+ FadedCallbackData *cb_data;
+
+ cb_data = g_new0 (FadedCallbackData, 1);
+ cb_data->done_cb = func;
+ cb_data->data = data;
+
+ g_signal_connect (fade, "faded",
+ G_CALLBACK (gs_fade_async_callback),
+ cb_data);
+ }
+
+ gs_fade_start (fade, timeout);
+}
+
+static void
+gs_fade_sync_callback (GSFade *fade,
+ int *flag)
+{
+ *flag = TRUE;
+ g_signal_handlers_disconnect_by_func (fade,
+ gs_fade_sync_callback,
+ flag);
+}
+
+void
+gs_fade_sync (GSFade *fade,
+ guint timeout)
+{
+ int flag = FALSE;
+
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ /* if fade is active then pause it */
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ g_signal_connect (fade, "faded",
+ G_CALLBACK (gs_fade_sync_callback),
+ &flag);
+
+ gs_fade_start (fade, timeout);
+
+ while (! flag)
+ {
+ gtk_main_iteration ();
+ }
+}
+
+void
+gs_fade_reset (GSFade *fade)
+{
+ int i;
+ g_return_if_fail (GS_IS_FADE (fade));
+
+ gs_debug ("Resetting fade");
+
+ if (fade->priv->active)
+ {
+ gs_fade_stop (fade);
+ }
+
+ fade->priv->current_alpha = 1.0;
+
+ gs_fade_set_alpha (fade, fade->priv->current_alpha);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ fade->priv->screen_priv[i].fade_finish (fade, i);
+}
+
+static void
+gs_fade_class_init (GSFadeClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_fade_finalize;
+
+ signals [FADED] =
+ g_signal_new ("faded",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSFadeClass, faded),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0, G_TYPE_NONE);
+
+ g_type_class_add_private (klass, sizeof (GSFadePrivate));
+}
+
+static void
+gs_fade_init (GSFade *fade)
+{
+ GdkDisplay *display;
+ int i;
+
+ fade->priv = GS_FADE_GET_PRIVATE (fade);
+
+ fade->priv->timeout = 1000;
+ fade->priv->current_alpha = 1.0;
+
+ display = gdk_display_get_default ();
+ fade->priv->num_screens = gdk_display_get_n_screens (display);
+
+ fade->priv->screen_priv = g_new0 (struct GSFadeScreenPrivate, fade->priv->num_screens);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ check_randr_extension (fade, i);
+ if (!fade->priv->screen_priv[i].fade_type)
+ check_gamma_extension (fade, i);
+ gs_debug ("Fade type: %d", fade->priv->screen_priv[i].fade_type);
+ }
+}
+
+static void
+gs_fade_finalize (GObject *object)
+{
+ GSFade *fade;
+ int i;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_FADE (object));
+
+ fade = GS_FADE (object);
+
+ g_return_if_fail (fade->priv != NULL);
+
+ for (i = 0; i < fade->priv->num_screens; i++)
+ fade->priv->screen_priv[i].fade_finish(fade, i);
+
+ if (fade->priv->screen_priv)
+ {
+ for (i = 0; i < fade->priv->num_screens; i++)
+ {
+ if (!fade->priv->screen_priv[i].rrscreen)
+ continue;
+ mate_rr_screen_destroy (fade->priv->screen_priv[i].rrscreen);
+ }
+
+ g_free (fade->priv->screen_priv);
+ fade->priv->screen_priv = NULL;
+ }
+
+ G_OBJECT_CLASS (gs_fade_parent_class)->finalize (object);
+}
+
+GSFade *
+gs_fade_new (void)
+{
+ if (fade_object)
+ {
+ g_object_ref (fade_object);
+ }
+ else
+ {
+ fade_object = g_object_new (GS_TYPE_FADE, NULL);
+ g_object_add_weak_pointer (fade_object,
+ (gpointer *) &fade_object);
+ }
+
+ return GS_FADE (fade_object);
+}
diff --git a/src/gs-fade.h b/src/gs-fade.h
new file mode 100644
index 0000000..65f3e7f
--- /dev/null
+++ b/src/gs-fade.h
@@ -0,0 +1,77 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_FADE_H
+#define __GS_FADE_H
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_FADE (gs_fade_get_type ())
+#define GS_FADE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_FADE, GSFade))
+#define GS_FADE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_FADE, GSFadeClass))
+#define GS_IS_FADE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_FADE))
+#define GS_IS_FADE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_FADE))
+#define GS_FADE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_FADE, GSFadeClass))
+
+
+typedef struct GSFadePrivate GSFadePrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSFadePrivate *priv;
+} GSFade;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+ void (* faded) (GSFade *fade);
+} GSFadeClass;
+
+typedef void (* GSFadeDoneFunc) (GSFade *fade,
+ gpointer data);
+
+
+GType gs_fade_get_type (void);
+
+GSFade * gs_fade_new (void);
+
+void gs_fade_async (GSFade *fade,
+ guint timeout,
+ GSFadeDoneFunc done_cb,
+ gpointer data);
+void gs_fade_sync (GSFade *fade,
+ guint timeout);
+
+void gs_fade_finish (GSFade *fade);
+void gs_fade_reset (GSFade *fade);
+
+gboolean gs_fade_get_active (GSFade *fade);
+
+gboolean gs_fade_get_enabled (GSFade *fade);
+void gs_fade_set_enabled (GSFade *fade,
+ gboolean enabled);
+
+G_END_DECLS
+
+#endif /* __GS_FADE_H */
diff --git a/src/gs-fade.h.orig b/src/gs-fade.h.orig
new file mode 100644
index 0000000..bfa7c2a
--- /dev/null
+++ b/src/gs-fade.h.orig
@@ -0,0 +1,77 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_FADE_H
+#define __GS_FADE_H
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_FADE (gs_fade_get_type ())
+#define GS_FADE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_FADE, GSFade))
+#define GS_FADE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_FADE, GSFadeClass))
+#define GS_IS_FADE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_FADE))
+#define GS_IS_FADE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_FADE))
+#define GS_FADE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_FADE, GSFadeClass))
+
+
+typedef struct GSFadePrivate GSFadePrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSFadePrivate *priv;
+} GSFade;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+ void (* faded) (GSFade *fade);
+} GSFadeClass;
+
+typedef void (* GSFadeDoneFunc) (GSFade *fade,
+ gpointer data);
+
+
+GType gs_fade_get_type (void);
+
+GSFade * gs_fade_new (void);
+
+void gs_fade_async (GSFade *fade,
+ guint timeout,
+ GSFadeDoneFunc done_cb,
+ gpointer data);
+void gs_fade_sync (GSFade *fade,
+ guint timeout);
+
+void gs_fade_finish (GSFade *fade);
+void gs_fade_reset (GSFade *fade);
+
+gboolean gs_fade_get_active (GSFade *fade);
+
+gboolean gs_fade_get_enabled (GSFade *fade);
+void gs_fade_set_enabled (GSFade *fade,
+ gboolean enabled);
+
+G_END_DECLS
+
+#endif /* __GS_FADE_H */
diff --git a/src/gs-grab-x11.c b/src/gs-grab-x11.c
new file mode 100644
index 0000000..9eca1ec
--- /dev/null
+++ b/src/gs-grab-x11.c
@@ -0,0 +1,672 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+
+#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
+# include <X11/extensions/xf86misc.h>
+#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
+
+#include "gs-window.h"
+#include "gs-grab.h"
+#include "gs-debug.h"
+
+static void gs_grab_class_init (GSGrabClass *klass);
+static void gs_grab_init (GSGrab *grab);
+static void gs_grab_finalize (GObject *object);
+
+#define GS_GRAB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_GRAB, GSGrabPrivate))
+
+G_DEFINE_TYPE (GSGrab, gs_grab, G_TYPE_OBJECT)
+
+static gpointer grab_object = NULL;
+
+struct GSGrabPrivate
+{
+ guint mouse_hide_cursor : 1;
+ GdkWindow *mouse_grab_window;
+ GdkWindow *keyboard_grab_window;
+ GdkScreen *mouse_grab_screen;
+ GdkScreen *keyboard_grab_screen;
+
+ GtkWidget *invisible;
+};
+
+static GdkCursor *
+get_cursor (void)
+{
+ GdkBitmap *empty_bitmap;
+ GdkCursor *cursor;
+ GdkColor useless;
+ char invisible_cursor_bits [] = { 0x0 };
+
+ useless.red = useless.green = useless.blue = 0;
+ useless.pixel = 0;
+
+ empty_bitmap = gdk_bitmap_create_from_data (NULL,
+ invisible_cursor_bits,
+ 1, 1);
+
+ cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
+ empty_bitmap,
+ &useless,
+ &useless, 0, 0);
+
+ g_object_unref (empty_bitmap);
+
+ return cursor;
+}
+
+static const char *
+grab_string (int status)
+{
+ switch (status)
+ {
+ case GDK_GRAB_SUCCESS:
+ return "GrabSuccess";
+ case GDK_GRAB_ALREADY_GRABBED:
+ return "AlreadyGrabbed";
+ case GDK_GRAB_INVALID_TIME:
+ return "GrabInvalidTime";
+ case GDK_GRAB_NOT_VIEWABLE:
+ return "GrabNotViewable";
+ case GDK_GRAB_FROZEN:
+ return "GrabFrozen";
+ default:
+ {
+ static char foo [255];
+ sprintf (foo, "unknown status: %d", status);
+ return foo;
+ }
+ }
+}
+
+#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
+/* This function enables and disables the Ctrl-Alt-KP_star and
+ Ctrl-Alt-KP_slash hot-keys, which (in XFree86 4.2) break any
+ grabs and/or kill the grabbing client. That would effectively
+ unlock the screen, so we don't like that.
+
+ The Ctrl-Alt-KP_star and Ctrl-Alt-KP_slash hot-keys only exist
+ if AllowDeactivateGrabs and/or AllowClosedownGrabs are turned on
+ in XF86Config. I believe they are disabled by default.
+
+ This does not affect any other keys (specifically Ctrl-Alt-BS or
+ Ctrl-Alt-F1) but I wish it did. Maybe it will someday.
+ */
+static void
+xorg_lock_smasher_set_active (GSGrab *grab,
+ gboolean active)
+{
+ int status, event, error;
+
+ if (!XF86MiscQueryExtension (GDK_DISPLAY (), &event, &error))
+ {
+ gs_debug ("No XFree86-Misc extension present");
+ return;
+ }
+
+ if (active)
+ {
+ gs_debug ("Enabling the x.org grab smasher");
+ }
+ else
+ {
+ gs_debug ("Disabling the x.org grab smasher");
+ }
+
+ gdk_error_trap_push ();
+
+ status = XF86MiscSetGrabKeysState (GDK_DISPLAY (), active);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_error_trap_pop ();
+
+ if (active && status == MiscExtGrabStateAlready)
+ {
+ /* shut up, consider this success */
+ status = MiscExtGrabStateSuccess;
+ }
+
+ gs_debug ("XF86MiscSetGrabKeysState(%s) returned %s\n",
+ active ? "on" : "off",
+ (status == MiscExtGrabStateSuccess ? "MiscExtGrabStateSuccess" :
+ status == MiscExtGrabStateLocked ? "MiscExtGrabStateLocked" :
+ status == MiscExtGrabStateAlready ? "MiscExtGrabStateAlready" :
+ "unknown value"));
+}
+#else
+static void
+xorg_lock_smasher_set_active (GSGrab *grab,
+ gboolean active)
+{
+}
+#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
+
+static int
+gs_grab_get_keyboard (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen)
+{
+ GdkGrabStatus status;
+
+ g_return_val_if_fail (window != NULL, FALSE);
+ g_return_val_if_fail (screen != NULL, FALSE);
+
+ gs_debug ("Grabbing keyboard widget=%X", (guint32) GDK_WINDOW_XID (window));
+ status = gdk_keyboard_grab (window, FALSE, GDK_CURRENT_TIME);
+
+ if (status == GDK_GRAB_SUCCESS)
+ {
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+ }
+ grab->priv->keyboard_grab_window = window;
+
+ g_object_add_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+
+ grab->priv->keyboard_grab_screen = screen;
+ }
+ else
+ {
+ gs_debug ("Couldn't grab keyboard! (%s)", grab_string (status));
+ }
+
+ return status;
+}
+
+static int
+gs_grab_get_mouse (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ GdkGrabStatus status;
+ GdkCursor *cursor;
+
+ g_return_val_if_fail (window != NULL, FALSE);
+ g_return_val_if_fail (screen != NULL, FALSE);
+
+ cursor = get_cursor ();
+
+ gs_debug ("Grabbing mouse widget=%X", (guint32) GDK_WINDOW_XID (window));
+ status = gdk_pointer_grab (window, TRUE, 0, NULL,
+ (hide_cursor ? cursor : NULL),
+ GDK_CURRENT_TIME);
+
+ if (status == GDK_GRAB_SUCCESS)
+ {
+ if (grab->priv->mouse_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+ }
+ grab->priv->mouse_grab_window = window;
+
+ g_object_add_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+
+ grab->priv->mouse_grab_screen = screen;
+ grab->priv->mouse_hide_cursor = hide_cursor;
+ }
+
+ gdk_cursor_unref (cursor);
+
+ return status;
+}
+
+void
+gs_grab_keyboard_reset (GSGrab *grab)
+{
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+ }
+ grab->priv->keyboard_grab_window = NULL;
+ grab->priv->keyboard_grab_screen = NULL;
+}
+
+static gboolean
+gs_grab_release_keyboard (GSGrab *grab)
+{
+ gs_debug ("Ungrabbing keyboard");
+ gdk_keyboard_ungrab (GDK_CURRENT_TIME);
+
+ gs_grab_keyboard_reset (grab);
+
+ return TRUE;
+}
+
+void
+gs_grab_mouse_reset (GSGrab *grab)
+{
+ if (grab->priv->mouse_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+ }
+
+ grab->priv->mouse_grab_window = NULL;
+ grab->priv->mouse_grab_screen = NULL;
+}
+
+gboolean
+gs_grab_release_mouse (GSGrab *grab)
+{
+ gs_debug ("Ungrabbing pointer");
+ gdk_pointer_ungrab (GDK_CURRENT_TIME);
+
+ gs_grab_mouse_reset (grab);
+
+ return TRUE;
+}
+
+static gboolean
+gs_grab_move_mouse (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean result;
+ GdkWindow *old_window;
+ GdkScreen *old_screen;
+ gboolean old_hide_cursor;
+
+ /* if the pointer is not grabbed and we have a
+ mouse_grab_window defined then we lost the grab */
+ if (! gdk_pointer_is_grabbed ())
+ {
+ gs_grab_mouse_reset (grab);
+ }
+
+ if (grab->priv->mouse_grab_window == window)
+ {
+ gs_debug ("Window %X is already grabbed, skipping",
+ (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window));
+ return TRUE;
+ }
+
+#if 0
+ gs_debug ("Intentionally skipping move pointer grabs");
+ /* FIXME: GTK doesn't like having the pointer grabbed */
+ return TRUE;
+#else
+ if (grab->priv->mouse_grab_window)
+ {
+ gs_debug ("Moving pointer grab from %X to %X",
+ (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window),
+ (guint32) GDK_WINDOW_XID (window));
+ }
+ else
+ {
+ gs_debug ("Getting pointer grab on %X",
+ (guint32) GDK_WINDOW_XID (window));
+ }
+#endif
+
+ gs_debug ("*** doing X server grab");
+ gdk_x11_grab_server ();
+
+ old_window = grab->priv->mouse_grab_window;
+ old_screen = grab->priv->mouse_grab_screen;
+ old_hide_cursor = grab->priv->mouse_hide_cursor;
+
+ if (old_window)
+ {
+ gs_grab_release_mouse (grab);
+ }
+
+ result = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+
+ if (result != GDK_GRAB_SUCCESS)
+ {
+ sleep (1);
+ result = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+ }
+
+ if ((result != GDK_GRAB_SUCCESS) && old_window)
+ {
+ gs_debug ("Could not grab mouse for new window. Resuming previous grab.");
+ gs_grab_get_mouse (grab, old_window, old_screen, old_hide_cursor);
+ }
+
+ gs_debug ("*** releasing X server grab");
+ gdk_x11_ungrab_server ();
+ gdk_flush ();
+
+ return (result == GDK_GRAB_SUCCESS);
+}
+
+static gboolean
+gs_grab_move_keyboard (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen)
+{
+ gboolean result;
+ GdkWindow *old_window;
+ GdkScreen *old_screen;
+
+ if (grab->priv->keyboard_grab_window == window)
+ {
+ gs_debug ("Window %X is already grabbed, skipping",
+ (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window));
+ return TRUE;
+ }
+
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ gs_debug ("Moving keyboard grab from %X to %X",
+ (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window),
+ (guint32) GDK_WINDOW_XID (window));
+ }
+ else
+ {
+ gs_debug ("Getting keyboard grab on %X",
+ (guint32) GDK_WINDOW_XID (window));
+
+ }
+
+ gs_debug ("*** doing X server grab");
+ gdk_x11_grab_server ();
+
+ old_window = grab->priv->keyboard_grab_window;
+ old_screen = grab->priv->keyboard_grab_screen;
+
+ if (old_window)
+ {
+ gs_grab_release_keyboard (grab);
+ }
+
+ result = gs_grab_get_keyboard (grab, window, screen);
+
+ if (result != GDK_GRAB_SUCCESS)
+ {
+ sleep (1);
+ result = gs_grab_get_keyboard (grab, window, screen);
+ }
+
+ if ((result != GDK_GRAB_SUCCESS) && old_window)
+ {
+ gs_debug ("Could not grab keyboard for new window. Resuming previous grab.");
+ gs_grab_get_keyboard (grab, old_window, old_screen);
+ }
+
+ gs_debug ("*** releasing X server grab");
+ gdk_x11_ungrab_server ();
+ gdk_flush ();
+
+ return (result == GDK_GRAB_SUCCESS);
+}
+
+static void
+gs_grab_nuke_focus (void)
+{
+ Window focus = 0;
+ int rev = 0;
+
+ gs_debug ("Nuking focus");
+
+ gdk_error_trap_push ();
+
+ XGetInputFocus (GDK_DISPLAY (), &focus, &rev);
+
+ XSetInputFocus (GDK_DISPLAY (), None, RevertToNone, CurrentTime);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_error_trap_pop ();
+}
+
+void
+gs_grab_release (GSGrab *grab)
+{
+ gs_debug ("Releasing all grabs");
+
+ gs_grab_release_mouse (grab);
+ gs_grab_release_keyboard (grab);
+
+ /* FIXME: is it right to enable this ? */
+ xorg_lock_smasher_set_active (grab, TRUE);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_flush ();
+}
+
+gboolean
+gs_grab_grab_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean mstatus = FALSE;
+ gboolean kstatus = FALSE;
+ int i;
+ int retries = 4;
+ gboolean focus_fuckus = FALSE;
+
+AGAIN:
+
+ for (i = 0; i < retries; i++)
+ {
+ kstatus = gs_grab_get_keyboard (grab, window, screen);
+ if (kstatus == GDK_GRAB_SUCCESS)
+ {
+ break;
+ }
+
+ /* else, wait a second and try to grab again. */
+ sleep (1);
+ }
+
+ if (kstatus != GDK_GRAB_SUCCESS)
+ {
+ if (!focus_fuckus)
+ {
+ focus_fuckus = TRUE;
+ gs_grab_nuke_focus ();
+ goto AGAIN;
+ }
+ }
+
+ for (i = 0; i < retries; i++)
+ {
+ mstatus = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+ if (mstatus == GDK_GRAB_SUCCESS)
+ {
+ break;
+ }
+
+ /* else, wait a second and try to grab again. */
+ sleep (1);
+ }
+
+ if (mstatus != GDK_GRAB_SUCCESS)
+ {
+ gs_debug ("Couldn't grab pointer! (%s)",
+ grab_string (mstatus));
+ }
+
+#if 0
+ /* FIXME: release the pointer grab so GTK will work */
+ gs_grab_release_mouse (grab);
+#endif
+
+ /* When should we allow blanking to proceed? The current theory
+ is that both a keyboard grab and a mouse grab are mandatory
+
+ - If we don't have a keyboard grab, then we won't be able to
+ read a password to unlock, so the kbd grab is manditory.
+
+ - If we don't have a mouse grab, then we might not see mouse
+ clicks as a signal to unblank, on-screen widgets won't work ideally,
+ and gs_grab_move_to_window() will spin forever when it gets called.
+ */
+
+ if (kstatus != GDK_GRAB_SUCCESS || mstatus != GDK_GRAB_SUCCESS)
+ {
+ /* Do not blank without a keyboard and mouse grabs. */
+
+ /* Release keyboard or mouse which was grabbed. */
+ if (kstatus == GDK_GRAB_SUCCESS)
+ {
+ gs_grab_release_keyboard (grab);
+ }
+ if (mstatus == GDK_GRAB_SUCCESS)
+ {
+ gs_grab_release_mouse (grab);
+ }
+
+ return FALSE;
+ }
+
+ /* Grab is good, go ahead and blank. */
+ return TRUE;
+}
+
+/* this is used to grab the keyboard and mouse to the root */
+gboolean
+gs_grab_grab_root (GSGrab *grab,
+ gboolean hide_cursor)
+{
+ GdkDisplay *display;
+ GdkWindow *root;
+ GdkScreen *screen;
+ gboolean res;
+
+ gs_debug ("Grabbing the root window");
+
+ display = gdk_display_get_default ();
+ gdk_display_get_pointer (display, &screen, NULL, NULL, NULL);
+ root = gdk_screen_get_root_window (screen);
+
+ res = gs_grab_grab_window (grab, root, screen, hide_cursor);
+
+ return res;
+}
+
+/* this is used to grab the keyboard and mouse to an offscreen window */
+gboolean
+gs_grab_grab_offscreen (GSGrab *grab,
+ gboolean hide_cursor)
+{
+ GdkScreen *screen;
+ gboolean res;
+
+ gs_debug ("Grabbing an offscreen window");
+
+ screen = gtk_invisible_get_screen (GTK_INVISIBLE (grab->priv->invisible));
+ res = gs_grab_grab_window (grab, grab->priv->invisible->window, screen, hide_cursor);
+
+ return res;
+}
+
+/* This is similar to gs_grab_grab_window but doesn't fail */
+void
+gs_grab_move_to_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean result = FALSE;
+
+ g_return_if_fail (GS_IS_GRAB (grab));
+
+ xorg_lock_smasher_set_active (grab, FALSE);
+
+ do
+ {
+ result = gs_grab_move_keyboard (grab, window, screen);
+ gdk_flush ();
+ }
+ while (!result);
+
+ do
+ {
+ result = gs_grab_move_mouse (grab, window, screen, hide_cursor);
+ gdk_flush ();
+ }
+ while (!result);
+}
+
+static void
+gs_grab_class_init (GSGrabClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_grab_finalize;
+
+ g_type_class_add_private (klass, sizeof (GSGrabPrivate));
+}
+
+static void
+gs_grab_init (GSGrab *grab)
+{
+ grab->priv = GS_GRAB_GET_PRIVATE (grab);
+
+ grab->priv->mouse_hide_cursor = FALSE;
+ grab->priv->invisible = gtk_invisible_new ();
+ gtk_widget_show (grab->priv->invisible);
+}
+
+static void
+gs_grab_finalize (GObject *object)
+{
+ GSGrab *grab;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_GRAB (object));
+
+ grab = GS_GRAB (object);
+
+ g_return_if_fail (grab->priv != NULL);
+
+ gtk_widget_destroy (grab->priv->invisible);
+
+ G_OBJECT_CLASS (gs_grab_parent_class)->finalize (object);
+}
+
+GSGrab *
+gs_grab_new (void)
+{
+ if (grab_object)
+ {
+ g_object_ref (grab_object);
+ }
+ else
+ {
+ grab_object = g_object_new (GS_TYPE_GRAB, NULL);
+ g_object_add_weak_pointer (grab_object,
+ (gpointer *) &grab_object);
+ }
+
+ return GS_GRAB (grab_object);
+}
diff --git a/src/gs-grab-x11.c.orig b/src/gs-grab-x11.c.orig
new file mode 100644
index 0000000..e6e7f22
--- /dev/null
+++ b/src/gs-grab-x11.c.orig
@@ -0,0 +1,672 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+#include <gtk/gtk.h>
+
+#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
+# include <X11/extensions/xf86misc.h>
+#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
+
+#include "gs-window.h"
+#include "gs-grab.h"
+#include "gs-debug.h"
+
+static void gs_grab_class_init (GSGrabClass *klass);
+static void gs_grab_init (GSGrab *grab);
+static void gs_grab_finalize (GObject *object);
+
+#define GS_GRAB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_GRAB, GSGrabPrivate))
+
+G_DEFINE_TYPE (GSGrab, gs_grab, G_TYPE_OBJECT)
+
+static gpointer grab_object = NULL;
+
+struct GSGrabPrivate
+{
+ guint mouse_hide_cursor : 1;
+ GdkWindow *mouse_grab_window;
+ GdkWindow *keyboard_grab_window;
+ GdkScreen *mouse_grab_screen;
+ GdkScreen *keyboard_grab_screen;
+
+ GtkWidget *invisible;
+};
+
+static GdkCursor *
+get_cursor (void)
+{
+ GdkBitmap *empty_bitmap;
+ GdkCursor *cursor;
+ GdkColor useless;
+ char invisible_cursor_bits [] = { 0x0 };
+
+ useless.red = useless.green = useless.blue = 0;
+ useless.pixel = 0;
+
+ empty_bitmap = gdk_bitmap_create_from_data (NULL,
+ invisible_cursor_bits,
+ 1, 1);
+
+ cursor = gdk_cursor_new_from_pixmap (empty_bitmap,
+ empty_bitmap,
+ &useless,
+ &useless, 0, 0);
+
+ g_object_unref (empty_bitmap);
+
+ return cursor;
+}
+
+static const char *
+grab_string (int status)
+{
+ switch (status)
+ {
+ case GDK_GRAB_SUCCESS:
+ return "GrabSuccess";
+ case GDK_GRAB_ALREADY_GRABBED:
+ return "AlreadyGrabbed";
+ case GDK_GRAB_INVALID_TIME:
+ return "GrabInvalidTime";
+ case GDK_GRAB_NOT_VIEWABLE:
+ return "GrabNotViewable";
+ case GDK_GRAB_FROZEN:
+ return "GrabFrozen";
+ default:
+ {
+ static char foo [255];
+ sprintf (foo, "unknown status: %d", status);
+ return foo;
+ }
+ }
+}
+
+#ifdef HAVE_XF86MISCSETGRABKEYSSTATE
+/* This function enables and disables the Ctrl-Alt-KP_star and
+ Ctrl-Alt-KP_slash hot-keys, which (in XFree86 4.2) break any
+ grabs and/or kill the grabbing client. That would effectively
+ unlock the screen, so we don't like that.
+
+ The Ctrl-Alt-KP_star and Ctrl-Alt-KP_slash hot-keys only exist
+ if AllowDeactivateGrabs and/or AllowClosedownGrabs are turned on
+ in XF86Config. I believe they are disabled by default.
+
+ This does not affect any other keys (specifically Ctrl-Alt-BS or
+ Ctrl-Alt-F1) but I wish it did. Maybe it will someday.
+ */
+static void
+xorg_lock_smasher_set_active (GSGrab *grab,
+ gboolean active)
+{
+ int status, event, error;
+
+ if (!XF86MiscQueryExtension (GDK_DISPLAY (), &event, &error))
+ {
+ gs_debug ("No XFree86-Misc extension present");
+ return;
+ }
+
+ if (active)
+ {
+ gs_debug ("Enabling the x.org grab smasher");
+ }
+ else
+ {
+ gs_debug ("Disabling the x.org grab smasher");
+ }
+
+ gdk_error_trap_push ();
+
+ status = XF86MiscSetGrabKeysState (GDK_DISPLAY (), active);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_error_trap_pop ();
+
+ if (active && status == MiscExtGrabStateAlready)
+ {
+ /* shut up, consider this success */
+ status = MiscExtGrabStateSuccess;
+ }
+
+ gs_debug ("XF86MiscSetGrabKeysState(%s) returned %s\n",
+ active ? "on" : "off",
+ (status == MiscExtGrabStateSuccess ? "MiscExtGrabStateSuccess" :
+ status == MiscExtGrabStateLocked ? "MiscExtGrabStateLocked" :
+ status == MiscExtGrabStateAlready ? "MiscExtGrabStateAlready" :
+ "unknown value"));
+}
+#else
+static void
+xorg_lock_smasher_set_active (GSGrab *grab,
+ gboolean active)
+{
+}
+#endif /* HAVE_XF86MISCSETGRABKEYSSTATE */
+
+static int
+gs_grab_get_keyboard (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen)
+{
+ GdkGrabStatus status;
+
+ g_return_val_if_fail (window != NULL, FALSE);
+ g_return_val_if_fail (screen != NULL, FALSE);
+
+ gs_debug ("Grabbing keyboard widget=%X", (guint32) GDK_WINDOW_XID (window));
+ status = gdk_keyboard_grab (window, FALSE, GDK_CURRENT_TIME);
+
+ if (status == GDK_GRAB_SUCCESS)
+ {
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+ }
+ grab->priv->keyboard_grab_window = window;
+
+ g_object_add_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+
+ grab->priv->keyboard_grab_screen = screen;
+ }
+ else
+ {
+ gs_debug ("Couldn't grab keyboard! (%s)", grab_string (status));
+ }
+
+ return status;
+}
+
+static int
+gs_grab_get_mouse (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ GdkGrabStatus status;
+ GdkCursor *cursor;
+
+ g_return_val_if_fail (window != NULL, FALSE);
+ g_return_val_if_fail (screen != NULL, FALSE);
+
+ cursor = get_cursor ();
+
+ gs_debug ("Grabbing mouse widget=%X", (guint32) GDK_WINDOW_XID (window));
+ status = gdk_pointer_grab (window, TRUE, 0, NULL,
+ (hide_cursor ? cursor : NULL),
+ GDK_CURRENT_TIME);
+
+ if (status == GDK_GRAB_SUCCESS)
+ {
+ if (grab->priv->mouse_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+ }
+ grab->priv->mouse_grab_window = window;
+
+ g_object_add_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+
+ grab->priv->mouse_grab_screen = screen;
+ grab->priv->mouse_hide_cursor = hide_cursor;
+ }
+
+ gdk_cursor_unref (cursor);
+
+ return status;
+}
+
+void
+gs_grab_keyboard_reset (GSGrab *grab)
+{
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->keyboard_grab_window),
+ (gpointer *) &grab->priv->keyboard_grab_window);
+ }
+ grab->priv->keyboard_grab_window = NULL;
+ grab->priv->keyboard_grab_screen = NULL;
+}
+
+static gboolean
+gs_grab_release_keyboard (GSGrab *grab)
+{
+ gs_debug ("Ungrabbing keyboard");
+ gdk_keyboard_ungrab (GDK_CURRENT_TIME);
+
+ gs_grab_keyboard_reset (grab);
+
+ return TRUE;
+}
+
+void
+gs_grab_mouse_reset (GSGrab *grab)
+{
+ if (grab->priv->mouse_grab_window != NULL)
+ {
+ g_object_remove_weak_pointer (G_OBJECT (grab->priv->mouse_grab_window),
+ (gpointer *) &grab->priv->mouse_grab_window);
+ }
+
+ grab->priv->mouse_grab_window = NULL;
+ grab->priv->mouse_grab_screen = NULL;
+}
+
+gboolean
+gs_grab_release_mouse (GSGrab *grab)
+{
+ gs_debug ("Ungrabbing pointer");
+ gdk_pointer_ungrab (GDK_CURRENT_TIME);
+
+ gs_grab_mouse_reset (grab);
+
+ return TRUE;
+}
+
+static gboolean
+gs_grab_move_mouse (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean result;
+ GdkWindow *old_window;
+ GdkScreen *old_screen;
+ gboolean old_hide_cursor;
+
+ /* if the pointer is not grabbed and we have a
+ mouse_grab_window defined then we lost the grab */
+ if (! gdk_pointer_is_grabbed ())
+ {
+ gs_grab_mouse_reset (grab);
+ }
+
+ if (grab->priv->mouse_grab_window == window)
+ {
+ gs_debug ("Window %X is already grabbed, skipping",
+ (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window));
+ return TRUE;
+ }
+
+#if 0
+ gs_debug ("Intentionally skipping move pointer grabs");
+ /* FIXME: GTK doesn't like having the pointer grabbed */
+ return TRUE;
+#else
+ if (grab->priv->mouse_grab_window)
+ {
+ gs_debug ("Moving pointer grab from %X to %X",
+ (guint32) GDK_WINDOW_XID (grab->priv->mouse_grab_window),
+ (guint32) GDK_WINDOW_XID (window));
+ }
+ else
+ {
+ gs_debug ("Getting pointer grab on %X",
+ (guint32) GDK_WINDOW_XID (window));
+ }
+#endif
+
+ gs_debug ("*** doing X server grab");
+ gdk_x11_grab_server ();
+
+ old_window = grab->priv->mouse_grab_window;
+ old_screen = grab->priv->mouse_grab_screen;
+ old_hide_cursor = grab->priv->mouse_hide_cursor;
+
+ if (old_window)
+ {
+ gs_grab_release_mouse (grab);
+ }
+
+ result = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+
+ if (result != GDK_GRAB_SUCCESS)
+ {
+ sleep (1);
+ result = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+ }
+
+ if ((result != GDK_GRAB_SUCCESS) && old_window)
+ {
+ gs_debug ("Could not grab mouse for new window. Resuming previous grab.");
+ gs_grab_get_mouse (grab, old_window, old_screen, old_hide_cursor);
+ }
+
+ gs_debug ("*** releasing X server grab");
+ gdk_x11_ungrab_server ();
+ gdk_flush ();
+
+ return (result == GDK_GRAB_SUCCESS);
+}
+
+static gboolean
+gs_grab_move_keyboard (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen)
+{
+ gboolean result;
+ GdkWindow *old_window;
+ GdkScreen *old_screen;
+
+ if (grab->priv->keyboard_grab_window == window)
+ {
+ gs_debug ("Window %X is already grabbed, skipping",
+ (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window));
+ return TRUE;
+ }
+
+ if (grab->priv->keyboard_grab_window != NULL)
+ {
+ gs_debug ("Moving keyboard grab from %X to %X",
+ (guint32) GDK_WINDOW_XID (grab->priv->keyboard_grab_window),
+ (guint32) GDK_WINDOW_XID (window));
+ }
+ else
+ {
+ gs_debug ("Getting keyboard grab on %X",
+ (guint32) GDK_WINDOW_XID (window));
+
+ }
+
+ gs_debug ("*** doing X server grab");
+ gdk_x11_grab_server ();
+
+ old_window = grab->priv->keyboard_grab_window;
+ old_screen = grab->priv->keyboard_grab_screen;
+
+ if (old_window)
+ {
+ gs_grab_release_keyboard (grab);
+ }
+
+ result = gs_grab_get_keyboard (grab, window, screen);
+
+ if (result != GDK_GRAB_SUCCESS)
+ {
+ sleep (1);
+ result = gs_grab_get_keyboard (grab, window, screen);
+ }
+
+ if ((result != GDK_GRAB_SUCCESS) && old_window)
+ {
+ gs_debug ("Could not grab keyboard for new window. Resuming previous grab.");
+ gs_grab_get_keyboard (grab, old_window, old_screen);
+ }
+
+ gs_debug ("*** releasing X server grab");
+ gdk_x11_ungrab_server ();
+ gdk_flush ();
+
+ return (result == GDK_GRAB_SUCCESS);
+}
+
+static void
+gs_grab_nuke_focus (void)
+{
+ Window focus = 0;
+ int rev = 0;
+
+ gs_debug ("Nuking focus");
+
+ gdk_error_trap_push ();
+
+ XGetInputFocus (GDK_DISPLAY (), &focus, &rev);
+
+ XSetInputFocus (GDK_DISPLAY (), None, RevertToNone, CurrentTime);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_error_trap_pop ();
+}
+
+void
+gs_grab_release (GSGrab *grab)
+{
+ gs_debug ("Releasing all grabs");
+
+ gs_grab_release_mouse (grab);
+ gs_grab_release_keyboard (grab);
+
+ /* FIXME: is it right to enable this ? */
+ xorg_lock_smasher_set_active (grab, TRUE);
+
+ gdk_display_sync (gdk_display_get_default ());
+ gdk_flush ();
+}
+
+gboolean
+gs_grab_grab_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean mstatus = FALSE;
+ gboolean kstatus = FALSE;
+ int i;
+ int retries = 4;
+ gboolean focus_fuckus = FALSE;
+
+AGAIN:
+
+ for (i = 0; i < retries; i++)
+ {
+ kstatus = gs_grab_get_keyboard (grab, window, screen);
+ if (kstatus == GDK_GRAB_SUCCESS)
+ {
+ break;
+ }
+
+ /* else, wait a second and try to grab again. */
+ sleep (1);
+ }
+
+ if (kstatus != GDK_GRAB_SUCCESS)
+ {
+ if (!focus_fuckus)
+ {
+ focus_fuckus = TRUE;
+ gs_grab_nuke_focus ();
+ goto AGAIN;
+ }
+ }
+
+ for (i = 0; i < retries; i++)
+ {
+ mstatus = gs_grab_get_mouse (grab, window, screen, hide_cursor);
+ if (mstatus == GDK_GRAB_SUCCESS)
+ {
+ break;
+ }
+
+ /* else, wait a second and try to grab again. */
+ sleep (1);
+ }
+
+ if (mstatus != GDK_GRAB_SUCCESS)
+ {
+ gs_debug ("Couldn't grab pointer! (%s)",
+ grab_string (mstatus));
+ }
+
+#if 0
+ /* FIXME: release the pointer grab so GTK will work */
+ gs_grab_release_mouse (grab);
+#endif
+
+ /* When should we allow blanking to proceed? The current theory
+ is that both a keyboard grab and a mouse grab are mandatory
+
+ - If we don't have a keyboard grab, then we won't be able to
+ read a password to unlock, so the kbd grab is manditory.
+
+ - If we don't have a mouse grab, then we might not see mouse
+ clicks as a signal to unblank, on-screen widgets won't work ideally,
+ and gs_grab_move_to_window() will spin forever when it gets called.
+ */
+
+ if (kstatus != GDK_GRAB_SUCCESS || mstatus != GDK_GRAB_SUCCESS)
+ {
+ /* Do not blank without a keyboard and mouse grabs. */
+
+ /* Release keyboard or mouse which was grabbed. */
+ if (kstatus == GDK_GRAB_SUCCESS)
+ {
+ gs_grab_release_keyboard (grab);
+ }
+ if (mstatus == GDK_GRAB_SUCCESS)
+ {
+ gs_grab_release_mouse (grab);
+ }
+
+ return FALSE;
+ }
+
+ /* Grab is good, go ahead and blank. */
+ return TRUE;
+}
+
+/* this is used to grab the keyboard and mouse to the root */
+gboolean
+gs_grab_grab_root (GSGrab *grab,
+ gboolean hide_cursor)
+{
+ GdkDisplay *display;
+ GdkWindow *root;
+ GdkScreen *screen;
+ gboolean res;
+
+ gs_debug ("Grabbing the root window");
+
+ display = gdk_display_get_default ();
+ gdk_display_get_pointer (display, &screen, NULL, NULL, NULL);
+ root = gdk_screen_get_root_window (screen);
+
+ res = gs_grab_grab_window (grab, root, screen, hide_cursor);
+
+ return res;
+}
+
+/* this is used to grab the keyboard and mouse to an offscreen window */
+gboolean
+gs_grab_grab_offscreen (GSGrab *grab,
+ gboolean hide_cursor)
+{
+ GdkScreen *screen;
+ gboolean res;
+
+ gs_debug ("Grabbing an offscreen window");
+
+ screen = gtk_invisible_get_screen (GTK_INVISIBLE (grab->priv->invisible));
+ res = gs_grab_grab_window (grab, grab->priv->invisible->window, screen, hide_cursor);
+
+ return res;
+}
+
+/* This is similar to gs_grab_grab_window but doesn't fail */
+void
+gs_grab_move_to_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor)
+{
+ gboolean result = FALSE;
+
+ g_return_if_fail (GS_IS_GRAB (grab));
+
+ xorg_lock_smasher_set_active (grab, FALSE);
+
+ do
+ {
+ result = gs_grab_move_keyboard (grab, window, screen);
+ gdk_flush ();
+ }
+ while (!result);
+
+ do
+ {
+ result = gs_grab_move_mouse (grab, window, screen, hide_cursor);
+ gdk_flush ();
+ }
+ while (!result);
+}
+
+static void
+gs_grab_class_init (GSGrabClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_grab_finalize;
+
+ g_type_class_add_private (klass, sizeof (GSGrabPrivate));
+}
+
+static void
+gs_grab_init (GSGrab *grab)
+{
+ grab->priv = GS_GRAB_GET_PRIVATE (grab);
+
+ grab->priv->mouse_hide_cursor = FALSE;
+ grab->priv->invisible = gtk_invisible_new ();
+ gtk_widget_show (grab->priv->invisible);
+}
+
+static void
+gs_grab_finalize (GObject *object)
+{
+ GSGrab *grab;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_GRAB (object));
+
+ grab = GS_GRAB (object);
+
+ g_return_if_fail (grab->priv != NULL);
+
+ gtk_widget_destroy (grab->priv->invisible);
+
+ G_OBJECT_CLASS (gs_grab_parent_class)->finalize (object);
+}
+
+GSGrab *
+gs_grab_new (void)
+{
+ if (grab_object)
+ {
+ g_object_ref (grab_object);
+ }
+ else
+ {
+ grab_object = g_object_new (GS_TYPE_GRAB, NULL);
+ g_object_add_weak_pointer (grab_object,
+ (gpointer *) &grab_object);
+ }
+
+ return GS_GRAB (grab_object);
+}
diff --git a/src/gs-grab.h b/src/gs-grab.h
new file mode 100644
index 0000000..4717f49
--- /dev/null
+++ b/src/gs-grab.h
@@ -0,0 +1,79 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_GRAB_H
+#define __GS_GRAB_H
+
+#include <glib.h>
+#include <gdk/gdk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_GRAB (gs_grab_get_type ())
+#define GS_GRAB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_GRAB, GSGrab))
+#define GS_GRAB_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_GRAB, GSGrabClass))
+#define GS_IS_GRAB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_GRAB))
+#define GS_IS_GRAB_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_GRAB))
+#define GS_GRAB_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_GRAB, GSGrabClass))
+
+typedef struct GSGrabPrivate GSGrabPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSGrabPrivate *priv;
+} GSGrab;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+} GSGrabClass;
+
+GType gs_grab_get_type (void);
+
+GSGrab * gs_grab_new (void);
+
+void gs_grab_release (GSGrab *grab);
+gboolean gs_grab_release_mouse (GSGrab *grab);
+
+gboolean gs_grab_grab_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor);
+
+gboolean gs_grab_grab_root (GSGrab *grab,
+ gboolean hide_cursor);
+gboolean gs_grab_grab_offscreen (GSGrab *grab,
+ gboolean hide_cursor);
+
+void gs_grab_move_to_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor);
+
+void gs_grab_mouse_reset (GSGrab *grab);
+void gs_grab_keyboard_reset (GSGrab *grab);
+
+G_END_DECLS
+
+#endif /* __GS_GRAB_H */
diff --git a/src/gs-grab.h.orig b/src/gs-grab.h.orig
new file mode 100644
index 0000000..a910403
--- /dev/null
+++ b/src/gs-grab.h.orig
@@ -0,0 +1,79 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_GRAB_H
+#define __GS_GRAB_H
+
+#include <glib.h>
+#include <gdk/gdk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_GRAB (gs_grab_get_type ())
+#define GS_GRAB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_GRAB, GSGrab))
+#define GS_GRAB_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_GRAB, GSGrabClass))
+#define GS_IS_GRAB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_GRAB))
+#define GS_IS_GRAB_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_GRAB))
+#define GS_GRAB_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_GRAB, GSGrabClass))
+
+typedef struct GSGrabPrivate GSGrabPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSGrabPrivate *priv;
+} GSGrab;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+} GSGrabClass;
+
+GType gs_grab_get_type (void);
+
+GSGrab * gs_grab_new (void);
+
+void gs_grab_release (GSGrab *grab);
+gboolean gs_grab_release_mouse (GSGrab *grab);
+
+gboolean gs_grab_grab_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor);
+
+gboolean gs_grab_grab_root (GSGrab *grab,
+ gboolean hide_cursor);
+gboolean gs_grab_grab_offscreen (GSGrab *grab,
+ gboolean hide_cursor);
+
+void gs_grab_move_to_window (GSGrab *grab,
+ GdkWindow *window,
+ GdkScreen *screen,
+ gboolean hide_cursor);
+
+void gs_grab_mouse_reset (GSGrab *grab);
+void gs_grab_keyboard_reset (GSGrab *grab);
+
+G_END_DECLS
+
+#endif /* __GS_GRAB_H */
diff --git a/src/gs-job.c b/src/gs-job.c
new file mode 100644
index 0000000..30d1f34
--- /dev/null
+++ b/src/gs-job.c
@@ -0,0 +1,567 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <string.h>
+
+#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
+#include <sys/resource.h>
+#endif
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+
+#include "gs-debug.h"
+#include "gs-job.h"
+
+#include "subprocs.h"
+
+static void gs_job_class_init (GSJobClass *klass);
+static void gs_job_init (GSJob *job);
+static void gs_job_finalize (GObject *object);
+
+#define GS_JOB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_JOB, GSJobPrivate))
+
+typedef enum
+{
+ GS_JOB_INVALID,
+ GS_JOB_RUNNING,
+ GS_JOB_STOPPED,
+ GS_JOB_KILLED,
+ GS_JOB_DEAD
+} GSJobStatus;
+
+struct GSJobPrivate
+{
+ GtkWidget *widget;
+
+ GSJobStatus status;
+ gint pid;
+ guint watch_id;
+
+ char *command;
+};
+
+G_DEFINE_TYPE (GSJob, gs_job, G_TYPE_OBJECT)
+
+static char *
+widget_get_id_string (GtkWidget *widget)
+{
+ char *id = NULL;
+
+ g_return_val_if_fail (widget != NULL, NULL);
+
+ id = g_strdup_printf ("0x%X",
+ (guint32)GDK_WINDOW_XID (widget->window));
+ return id;
+}
+
+static void
+gs_job_class_init (GSJobClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_job_finalize;
+
+ g_type_class_add_private (klass, sizeof (GSJobPrivate));
+}
+
+static void
+gs_job_init (GSJob *job)
+{
+ job->priv = GS_JOB_GET_PRIVATE (job);
+}
+
+/* adapted from gspawn.c */
+static int
+wait_on_child (int pid)
+{
+ int status;
+
+wait_again:
+ if (waitpid (pid, &status, 0) < 0)
+ {
+ if (errno == EINTR)
+ {
+ goto wait_again;
+ }
+ else if (errno == ECHILD)
+ {
+ ; /* do nothing, child already reaped */
+ }
+ else
+ {
+ gs_debug ("waitpid () should not fail in 'GSJob'");
+ }
+ }
+
+ return status;
+}
+
+static void
+gs_job_died (GSJob *job)
+{
+ if (job->priv->pid > 0)
+ {
+ int exit_status;
+
+ gs_debug ("Waiting on process %d", job->priv->pid);
+ exit_status = wait_on_child (job->priv->pid);
+
+ job->priv->status = GS_JOB_DEAD;
+
+ if (WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) != 0))
+ {
+ gs_debug ("Wait on child process failed");
+ }
+ else
+ {
+ /* exited normally */
+ }
+ }
+ g_spawn_close_pid (job->priv->pid);
+ job->priv->pid = 0;
+
+ gs_debug ("Job died");
+}
+
+static void
+gs_job_finalize (GObject *object)
+{
+ GSJob *job;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_JOB (object));
+
+ job = GS_JOB (object);
+
+ g_return_if_fail (job->priv != NULL);
+
+ if (job->priv->pid > 0)
+ {
+ signal_pid (job->priv->pid, SIGTERM);
+ gs_job_died (job);
+ }
+
+ g_free (job->priv->command);
+ job->priv->command = NULL;
+
+ G_OBJECT_CLASS (gs_job_parent_class)->finalize (object);
+}
+
+void
+gs_job_set_widget (GSJob *job,
+ GtkWidget *widget)
+{
+ g_return_if_fail (job != NULL);
+ g_return_if_fail (GS_IS_JOB (job));
+
+ if (widget != job->priv->widget)
+ {
+ job->priv->widget = widget;
+
+ /* restart job */
+ if (gs_job_is_running (job))
+ {
+ gs_job_stop (job);
+ gs_job_start (job);
+ }
+ }
+}
+
+gboolean
+gs_job_set_command (GSJob *job,
+ const char *command)
+{
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("Setting command for job: '%s'",
+ command != NULL ? command : "NULL");
+
+ g_free (job->priv->command);
+ job->priv->command = g_strdup (command);
+
+ return TRUE;
+}
+
+GSJob *
+gs_job_new (void)
+{
+ GObject *job;
+
+ job = g_object_new (GS_TYPE_JOB, NULL);
+
+ return GS_JOB (job);
+}
+
+GSJob *
+gs_job_new_for_widget (GtkWidget *widget)
+{
+ GObject *job;
+
+ job = g_object_new (GS_TYPE_JOB, NULL);
+
+ gs_job_set_widget (GS_JOB (job), widget);
+
+ return GS_JOB (job);
+}
+
+static void
+nice_process (int pid,
+ int nice_level)
+{
+ g_return_if_fail (pid > 0);
+
+ if (nice_level == 0)
+ {
+ return;
+ }
+
+#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
+ gs_debug ("Setting child process priority to: %d", nice_level);
+ if (setpriority (PRIO_PROCESS, pid, nice_level) != 0)
+ {
+ gs_debug ("setpriority(PRIO_PROCESS, %lu, %d) failed",
+ (unsigned long) pid, nice_level);
+ }
+#else
+ gs_debug ("don't know how to change process priority on this system.");
+#endif
+}
+
+static GPtrArray *
+get_env_vars (GtkWidget *widget)
+{
+ GPtrArray *env;
+ char *str;
+ int i;
+ static const char *allowed_env_vars [] =
+ {
+ "PATH",
+ "SESSION_MANAGER",
+ "XAUTHORITY",
+ "XAUTHLOCALHOSTNAME",
+ "LANG",
+ "LANGUAGE",
+ "DBUS_SESSION_BUS_ADDRESS"
+ };
+
+ env = g_ptr_array_new ();
+
+ str = gdk_screen_make_display_name (gtk_widget_get_screen (widget));
+ g_ptr_array_add (env, g_strdup_printf ("DISPLAY=%s", str));
+ g_free (str);
+
+ g_ptr_array_add (env, g_strdup_printf ("HOME=%s",
+ g_get_home_dir ()));
+
+ for (i = 0; i < G_N_ELEMENTS (allowed_env_vars); i++)
+ {
+ const char *var;
+ const char *val;
+ var = allowed_env_vars [i];
+ val = g_getenv (var);
+ if (val != NULL)
+ {
+ g_ptr_array_add (env, g_strdup_printf ("%s=%s",
+ var,
+ val));
+ }
+ }
+
+ str = widget_get_id_string (widget);
+ g_ptr_array_add (env, g_strdup_printf ("XSCREENSAVER_WINDOW=%s", str));
+ g_free (str);
+
+ g_ptr_array_add (env, NULL);
+
+ return env;
+}
+
+static gboolean
+spawn_on_widget (GtkWidget *widget,
+ const char *command,
+ int *pid,
+ GIOFunc watch_func,
+ gpointer user_data,
+ guint *watch_id)
+{
+ char **argv;
+ GPtrArray *env;
+ gboolean result;
+ GIOChannel *channel;
+ GError *error = NULL;
+ int standard_error;
+ int child_pid;
+ int id;
+ int i;
+
+ if (command == NULL)
+ {
+ return FALSE;
+ }
+
+ if (! g_shell_parse_argv (command, NULL, &argv, &error))
+ {
+ gs_debug ("Could not parse command: %s", error->message);
+ g_error_free (error);
+ return FALSE;
+ }
+
+ env = get_env_vars (widget);
+
+ error = NULL;
+ result = gdk_spawn_on_screen_with_pipes (gtk_widget_get_screen (widget),
+ NULL,
+ argv,
+ (char **)env->pdata,
+ G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
+ NULL,
+ NULL,
+ &child_pid,
+ NULL,
+ NULL,
+ &standard_error,
+ &error);
+ for (i = 0; i < env->len; i++)
+ {
+ g_free (g_ptr_array_index (env, i));
+ }
+ g_ptr_array_free (env, TRUE);
+
+ if (! result)
+ {
+ gs_debug ("Could not start command '%s': %s", command, error->message);
+ g_error_free (error);
+ g_strfreev (argv);
+ return FALSE;
+ }
+
+ g_strfreev (argv);
+
+ nice_process (child_pid, 10);
+
+ if (pid != NULL)
+ {
+ *pid = child_pid;
+ }
+ else
+ {
+ g_spawn_close_pid (child_pid);
+ }
+
+ channel = g_io_channel_unix_new (standard_error);
+ g_io_channel_set_close_on_unref (channel, TRUE);
+ g_io_channel_set_flags (channel,
+ g_io_channel_get_flags (channel) | G_IO_FLAG_NONBLOCK,
+ NULL);
+ id = g_io_add_watch (channel,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ watch_func,
+ user_data);
+ if (watch_id != NULL)
+ {
+ *watch_id = id;
+ }
+
+ g_io_channel_unref (channel);
+
+ return result;
+}
+
+static gboolean
+command_watch (GIOChannel *source,
+ GIOCondition condition,
+ GSJob *job)
+{
+ GIOStatus status;
+ GError *error = NULL;
+ gboolean done = FALSE;
+
+ g_return_val_if_fail (job != NULL, FALSE);
+
+ if (condition & G_IO_IN)
+ {
+ char *str;
+
+ status = g_io_channel_read_line (source, &str, NULL, NULL, &error);
+
+ if (status == G_IO_STATUS_NORMAL)
+ {
+ gs_debug ("command output: %s", str);
+
+ }
+ else if (status == G_IO_STATUS_EOF)
+ {
+ done = TRUE;
+
+ }
+ else if (error != NULL)
+ {
+ gs_debug ("command error: %s", error->message);
+ g_error_free (error);
+ }
+
+ g_free (str);
+ }
+ else if (condition & G_IO_HUP)
+ {
+ done = TRUE;
+ }
+
+ if (done)
+ {
+ gs_job_died (job);
+
+ job->priv->watch_id = 0;
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_job_is_running (GSJob *job)
+{
+ gboolean running;
+
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ running = (job->priv->pid > 0);
+
+ return running;
+}
+
+gboolean
+gs_job_start (GSJob *job)
+{
+ gboolean result;
+
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("starting job");
+
+ if (job->priv->pid != 0)
+ {
+ gs_debug ("Cannot restart active job.");
+ return FALSE;
+ }
+
+ if (job->priv->widget == NULL)
+ {
+ gs_debug ("Could not start job: screensaver window is not set.");
+ return FALSE;
+ }
+
+ if (job->priv->command == NULL)
+ {
+ /* no warning here because a NULL command is interpreted
+ as a no-op job */
+ gs_debug ("No command set for job.");
+ return FALSE;
+ }
+
+ result = spawn_on_widget (job->priv->widget,
+ job->priv->command,
+ &job->priv->pid,
+ (GIOFunc)command_watch,
+ job,
+ &job->priv->watch_id);
+
+ if (result)
+ {
+ job->priv->status = GS_JOB_RUNNING;
+ }
+
+ return result;
+}
+
+static void
+remove_command_watch (GSJob *job)
+{
+ if (job->priv->watch_id != 0)
+ {
+ g_source_remove (job->priv->watch_id);
+ job->priv->watch_id = 0;
+ }
+}
+
+gboolean
+gs_job_stop (GSJob *job)
+{
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("stopping job");
+
+ if (job->priv->pid == 0)
+ {
+ gs_debug ("Could not stop job: pid not defined");
+ return FALSE;
+ }
+
+ if (job->priv->status == GS_JOB_STOPPED)
+ {
+ gs_job_suspend (job, FALSE);
+ }
+
+ remove_command_watch (job);
+
+ signal_pid (job->priv->pid, SIGTERM);
+
+ job->priv->status = GS_JOB_KILLED;
+
+ gs_job_died (job);
+
+ return TRUE;
+}
+
+gboolean
+gs_job_suspend (GSJob *job,
+ gboolean suspend)
+{
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("suspending job");
+
+ if (job->priv->pid == 0)
+ {
+ return FALSE;
+ }
+
+ signal_pid (job->priv->pid, (suspend ? SIGSTOP : SIGCONT));
+
+ job->priv->status = (suspend ? GS_JOB_STOPPED : GS_JOB_RUNNING);
+
+ return TRUE;
+}
diff --git a/src/gs-job.c.orig b/src/gs-job.c.orig
new file mode 100644
index 0000000..896addc
--- /dev/null
+++ b/src/gs-job.c.orig
@@ -0,0 +1,567 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]du>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <errno.h>
+#include <string.h>
+
+#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
+#include <sys/resource.h>
+#endif
+
+#include <glib.h>
+#include <glib/gstdio.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+
+#include "gs-debug.h"
+#include "gs-job.h"
+
+#include "subprocs.h"
+
+static void gs_job_class_init (GSJobClass *klass);
+static void gs_job_init (GSJob *job);
+static void gs_job_finalize (GObject *object);
+
+#define GS_JOB_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_JOB, GSJobPrivate))
+
+typedef enum
+{
+ GS_JOB_INVALID,
+ GS_JOB_RUNNING,
+ GS_JOB_STOPPED,
+ GS_JOB_KILLED,
+ GS_JOB_DEAD
+} GSJobStatus;
+
+struct GSJobPrivate
+{
+ GtkWidget *widget;
+
+ GSJobStatus status;
+ gint pid;
+ guint watch_id;
+
+ char *command;
+};
+
+G_DEFINE_TYPE (GSJob, gs_job, G_TYPE_OBJECT)
+
+static char *
+widget_get_id_string (GtkWidget *widget)
+{
+ char *id = NULL;
+
+ g_return_val_if_fail (widget != NULL, NULL);
+
+ id = g_strdup_printf ("0x%X",
+ (guint32)GDK_WINDOW_XID (widget->window));
+ return id;
+}
+
+static void
+gs_job_class_init (GSJobClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_job_finalize;
+
+ g_type_class_add_private (klass, sizeof (GSJobPrivate));
+}
+
+static void
+gs_job_init (GSJob *job)
+{
+ job->priv = GS_JOB_GET_PRIVATE (job);
+}
+
+/* adapted from gspawn.c */
+static int
+wait_on_child (int pid)
+{
+ int status;
+
+wait_again:
+ if (waitpid (pid, &status, 0) < 0)
+ {
+ if (errno == EINTR)
+ {
+ goto wait_again;
+ }
+ else if (errno == ECHILD)
+ {
+ ; /* do nothing, child already reaped */
+ }
+ else
+ {
+ gs_debug ("waitpid () should not fail in 'GSJob'");
+ }
+ }
+
+ return status;
+}
+
+static void
+gs_job_died (GSJob *job)
+{
+ if (job->priv->pid > 0)
+ {
+ int exit_status;
+
+ gs_debug ("Waiting on process %d", job->priv->pid);
+ exit_status = wait_on_child (job->priv->pid);
+
+ job->priv->status = GS_JOB_DEAD;
+
+ if (WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) != 0))
+ {
+ gs_debug ("Wait on child process failed");
+ }
+ else
+ {
+ /* exited normally */
+ }
+ }
+ g_spawn_close_pid (job->priv->pid);
+ job->priv->pid = 0;
+
+ gs_debug ("Job died");
+}
+
+static void
+gs_job_finalize (GObject *object)
+{
+ GSJob *job;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_JOB (object));
+
+ job = GS_JOB (object);
+
+ g_return_if_fail (job->priv != NULL);
+
+ if (job->priv->pid > 0)
+ {
+ signal_pid (job->priv->pid, SIGTERM);
+ gs_job_died (job);
+ }
+
+ g_free (job->priv->command);
+ job->priv->command = NULL;
+
+ G_OBJECT_CLASS (gs_job_parent_class)->finalize (object);
+}
+
+void
+gs_job_set_widget (GSJob *job,
+ GtkWidget *widget)
+{
+ g_return_if_fail (job != NULL);
+ g_return_if_fail (GS_IS_JOB (job));
+
+ if (widget != job->priv->widget)
+ {
+ job->priv->widget = widget;
+
+ /* restart job */
+ if (gs_job_is_running (job))
+ {
+ gs_job_stop (job);
+ gs_job_start (job);
+ }
+ }
+}
+
+gboolean
+gs_job_set_command (GSJob *job,
+ const char *command)
+{
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("Setting command for job: '%s'",
+ command != NULL ? command : "NULL");
+
+ g_free (job->priv->command);
+ job->priv->command = g_strdup (command);
+
+ return TRUE;
+}
+
+GSJob *
+gs_job_new (void)
+{
+ GObject *job;
+
+ job = g_object_new (GS_TYPE_JOB, NULL);
+
+ return GS_JOB (job);
+}
+
+GSJob *
+gs_job_new_for_widget (GtkWidget *widget)
+{
+ GObject *job;
+
+ job = g_object_new (GS_TYPE_JOB, NULL);
+
+ gs_job_set_widget (GS_JOB (job), widget);
+
+ return GS_JOB (job);
+}
+
+static void
+nice_process (int pid,
+ int nice_level)
+{
+ g_return_if_fail (pid > 0);
+
+ if (nice_level == 0)
+ {
+ return;
+ }
+
+#if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
+ gs_debug ("Setting child process priority to: %d", nice_level);
+ if (setpriority (PRIO_PROCESS, pid, nice_level) != 0)
+ {
+ gs_debug ("setpriority(PRIO_PROCESS, %lu, %d) failed",
+ (unsigned long) pid, nice_level);
+ }
+#else
+ gs_debug ("don't know how to change process priority on this system.");
+#endif
+}
+
+static GPtrArray *
+get_env_vars (GtkWidget *widget)
+{
+ GPtrArray *env;
+ char *str;
+ int i;
+ static const char *allowed_env_vars [] =
+ {
+ "PATH",
+ "SESSION_MANAGER",
+ "XAUTHORITY",
+ "XAUTHLOCALHOSTNAME",
+ "LANG",
+ "LANGUAGE",
+ "DBUS_SESSION_BUS_ADDRESS"
+ };
+
+ env = g_ptr_array_new ();
+
+ str = gdk_screen_make_display_name (gtk_widget_get_screen (widget));
+ g_ptr_array_add (env, g_strdup_printf ("DISPLAY=%s", str));
+ g_free (str);
+
+ g_ptr_array_add (env, g_strdup_printf ("HOME=%s",
+ g_get_home_dir ()));
+
+ for (i = 0; i < G_N_ELEMENTS (allowed_env_vars); i++)
+ {
+ const char *var;
+ const char *val;
+ var = allowed_env_vars [i];
+ val = g_getenv (var);
+ if (val != NULL)
+ {
+ g_ptr_array_add (env, g_strdup_printf ("%s=%s",
+ var,
+ val));
+ }
+ }
+
+ str = widget_get_id_string (widget);
+ g_ptr_array_add (env, g_strdup_printf ("XSCREENSAVER_WINDOW=%s", str));
+ g_free (str);
+
+ g_ptr_array_add (env, NULL);
+
+ return env;
+}
+
+static gboolean
+spawn_on_widget (GtkWidget *widget,
+ const char *command,
+ int *pid,
+ GIOFunc watch_func,
+ gpointer user_data,
+ guint *watch_id)
+{
+ char **argv;
+ GPtrArray *env;
+ gboolean result;
+ GIOChannel *channel;
+ GError *error = NULL;
+ int standard_error;
+ int child_pid;
+ int id;
+ int i;
+
+ if (command == NULL)
+ {
+ return FALSE;
+ }
+
+ if (! g_shell_parse_argv (command, NULL, &argv, &error))
+ {
+ gs_debug ("Could not parse command: %s", error->message);
+ g_error_free (error);
+ return FALSE;
+ }
+
+ env = get_env_vars (widget);
+
+ error = NULL;
+ result = gdk_spawn_on_screen_with_pipes (gtk_widget_get_screen (widget),
+ NULL,
+ argv,
+ (char **)env->pdata,
+ G_SPAWN_SEARCH_PATH | G_SPAWN_DO_NOT_REAP_CHILD,
+ NULL,
+ NULL,
+ &child_pid,
+ NULL,
+ NULL,
+ &standard_error,
+ &error);
+ for (i = 0; i < env->len; i++)
+ {
+ g_free (g_ptr_array_index (env, i));
+ }
+ g_ptr_array_free (env, TRUE);
+
+ if (! result)
+ {
+ gs_debug ("Could not start command '%s': %s", command, error->message);
+ g_error_free (error);
+ g_strfreev (argv);
+ return FALSE;
+ }
+
+ g_strfreev (argv);
+
+ nice_process (child_pid, 10);
+
+ if (pid != NULL)
+ {
+ *pid = child_pid;
+ }
+ else
+ {
+ g_spawn_close_pid (child_pid);
+ }
+
+ channel = g_io_channel_unix_new (standard_error);
+ g_io_channel_set_close_on_unref (channel, TRUE);
+ g_io_channel_set_flags (channel,
+ g_io_channel_get_flags (channel) | G_IO_FLAG_NONBLOCK,
+ NULL);
+ id = g_io_add_watch (channel,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ watch_func,
+ user_data);
+ if (watch_id != NULL)
+ {
+ *watch_id = id;
+ }
+
+ g_io_channel_unref (channel);
+
+ return result;
+}
+
+static gboolean
+command_watch (GIOChannel *source,
+ GIOCondition condition,
+ GSJob *job)
+{
+ GIOStatus status;
+ GError *error = NULL;
+ gboolean done = FALSE;
+
+ g_return_val_if_fail (job != NULL, FALSE);
+
+ if (condition & G_IO_IN)
+ {
+ char *str;
+
+ status = g_io_channel_read_line (source, &str, NULL, NULL, &error);
+
+ if (status == G_IO_STATUS_NORMAL)
+ {
+ gs_debug ("command output: %s", str);
+
+ }
+ else if (status == G_IO_STATUS_EOF)
+ {
+ done = TRUE;
+
+ }
+ else if (error != NULL)
+ {
+ gs_debug ("command error: %s", error->message);
+ g_error_free (error);
+ }
+
+ g_free (str);
+ }
+ else if (condition & G_IO_HUP)
+ {
+ done = TRUE;
+ }
+
+ if (done)
+ {
+ gs_job_died (job);
+
+ job->priv->watch_id = 0;
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+gboolean
+gs_job_is_running (GSJob *job)
+{
+ gboolean running;
+
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ running = (job->priv->pid > 0);
+
+ return running;
+}
+
+gboolean
+gs_job_start (GSJob *job)
+{
+ gboolean result;
+
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("starting job");
+
+ if (job->priv->pid != 0)
+ {
+ gs_debug ("Cannot restart active job.");
+ return FALSE;
+ }
+
+ if (job->priv->widget == NULL)
+ {
+ gs_debug ("Could not start job: screensaver window is not set.");
+ return FALSE;
+ }
+
+ if (job->priv->command == NULL)
+ {
+ /* no warning here because a NULL command is interpreted
+ as a no-op job */
+ gs_debug ("No command set for job.");
+ return FALSE;
+ }
+
+ result = spawn_on_widget (job->priv->widget,
+ job->priv->command,
+ &job->priv->pid,
+ (GIOFunc)command_watch,
+ job,
+ &job->priv->watch_id);
+
+ if (result)
+ {
+ job->priv->status = GS_JOB_RUNNING;
+ }
+
+ return result;
+}
+
+static void
+remove_command_watch (GSJob *job)
+{
+ if (job->priv->watch_id != 0)
+ {
+ g_source_remove (job->priv->watch_id);
+ job->priv->watch_id = 0;
+ }
+}
+
+gboolean
+gs_job_stop (GSJob *job)
+{
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("stopping job");
+
+ if (job->priv->pid == 0)
+ {
+ gs_debug ("Could not stop job: pid not defined");
+ return FALSE;
+ }
+
+ if (job->priv->status == GS_JOB_STOPPED)
+ {
+ gs_job_suspend (job, FALSE);
+ }
+
+ remove_command_watch (job);
+
+ signal_pid (job->priv->pid, SIGTERM);
+
+ job->priv->status = GS_JOB_KILLED;
+
+ gs_job_died (job);
+
+ return TRUE;
+}
+
+gboolean
+gs_job_suspend (GSJob *job,
+ gboolean suspend)
+{
+ g_return_val_if_fail (job != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_JOB (job), FALSE);
+
+ gs_debug ("suspending job");
+
+ if (job->priv->pid == 0)
+ {
+ return FALSE;
+ }
+
+ signal_pid (job->priv->pid, (suspend ? SIGSTOP : SIGCONT));
+
+ job->priv->status = (suspend ? GS_JOB_STOPPED : GS_JOB_RUNNING);
+
+ return TRUE;
+}
diff --git a/src/gs-job.h b/src/gs-job.h
new file mode 100644
index 0000000..ba7084e
--- /dev/null
+++ b/src/gs-job.h
@@ -0,0 +1,69 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_JOB_H
+#define __GS_JOB_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_JOB (gs_job_get_type ())
+#define GS_JOB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_JOB, GSJob))
+#define GS_JOB_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_JOB, GSJobClass))
+#define GS_IS_JOB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_JOB))
+#define GS_IS_JOB_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_JOB))
+#define GS_JOB_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_JOB, GSJobClass))
+
+typedef struct GSJobPrivate GSJobPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSJobPrivate *priv;
+} GSJob;
+
+typedef struct
+{
+ GObjectClass parent_class;
+} GSJobClass;
+
+GType gs_job_get_type (void);
+
+GSJob *gs_job_new (void);
+GSJob *gs_job_new_for_widget (GtkWidget *widget);
+
+gboolean gs_job_is_running (GSJob *job);
+gboolean gs_job_start (GSJob *job);
+gboolean gs_job_stop (GSJob *job);
+gboolean gs_job_suspend (GSJob *job,
+ gboolean suspend);
+
+void gs_job_set_widget (GSJob *job,
+ GtkWidget *widget);
+
+gboolean gs_job_set_command (GSJob *job,
+ const char *command);
+
+G_END_DECLS
+
+#endif /* __GS_JOB_H */
diff --git a/src/gs-job.h.orig b/src/gs-job.h.orig
new file mode 100644
index 0000000..b4fa3dc
--- /dev/null
+++ b/src/gs-job.h.orig
@@ -0,0 +1,69 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_JOB_H
+#define __GS_JOB_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_JOB (gs_job_get_type ())
+#define GS_JOB(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_JOB, GSJob))
+#define GS_JOB_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_JOB, GSJobClass))
+#define GS_IS_JOB(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_JOB))
+#define GS_IS_JOB_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_JOB))
+#define GS_JOB_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_JOB, GSJobClass))
+
+typedef struct GSJobPrivate GSJobPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSJobPrivate *priv;
+} GSJob;
+
+typedef struct
+{
+ GObjectClass parent_class;
+} GSJobClass;
+
+GType gs_job_get_type (void);
+
+GSJob *gs_job_new (void);
+GSJob *gs_job_new_for_widget (GtkWidget *widget);
+
+gboolean gs_job_is_running (GSJob *job);
+gboolean gs_job_start (GSJob *job);
+gboolean gs_job_stop (GSJob *job);
+gboolean gs_job_suspend (GSJob *job,
+ gboolean suspend);
+
+void gs_job_set_widget (GSJob *job,
+ GtkWidget *widget);
+
+gboolean gs_job_set_command (GSJob *job,
+ const char *command);
+
+G_END_DECLS
+
+#endif /* __GS_JOB_H */
diff --git a/src/gs-listener-dbus.c b/src/gs-listener-dbus.c
new file mode 100644
index 0000000..601689c
--- /dev/null
+++ b/src/gs-listener-dbus.c
@@ -0,0 +1,2272 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+
+#include <glib/gi18n.h>
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include "gs-listener-dbus.h"
+#include "gs-marshal.h"
+#include "gs-debug.h"
+
+/* this is for dbus < 0.3 */
+#if ((DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 30))
+#define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
+#define dbus_bus_request_name(connection, name, flags, err) dbus_bus_acquire_service(connection, name, flags, err)
+#endif
+
+static void gs_listener_class_init (GSListenerClass *klass);
+static void gs_listener_init (GSListener *listener);
+static void gs_listener_finalize (GObject *object);
+
+static void gs_listener_unregister_handler (DBusConnection *connection,
+ void *data);
+
+static DBusHandlerResult gs_listener_message_handler (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data);
+
+#define GS_LISTENER_SERVICE "org.mate.ScreenSaver"
+#define GS_LISTENER_PATH "/org/mate/ScreenSaver"
+#define GS_LISTENER_INTERFACE "org.mate.ScreenSaver"
+
+#define HAL_DEVICE_INTERFACE "org.freedesktop.Hal.Device"
+
+#define CK_NAME "org.freedesktop.ConsoleKit"
+#define CK_MANAGER_PATH "/org/freedesktop/ConsoleKit/Manager"
+#define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
+#define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
+
+#define SESSION_NAME "org.mate.SessionManager"
+#define SESSION_PATH "/org/mate/SessionManager"
+#define SESSION_INTERFACE "org.mate.SessionManager"
+
+#define TYPE_MISMATCH_ERROR GS_LISTENER_INTERFACE ".TypeMismatch"
+
+#define GS_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_LISTENER, GSListenerPrivate))
+
+struct GSListenerPrivate
+{
+ DBusConnection *connection;
+ DBusConnection *system_connection;
+
+ guint session_idle : 1;
+ guint active : 1;
+ guint activation_enabled : 1;
+ guint throttled : 1;
+ GHashTable *inhibitors;
+ GHashTable *throttlers;
+ time_t active_start;
+ time_t session_idle_start;
+ char *session_id;
+
+ guint32 ck_throttle_cookie;
+};
+
+typedef struct
+{
+ int entry_type;
+ char *application;
+ char *reason;
+ char *connection;
+ guint32 cookie;
+ guint32 foreign_cookie;
+ GTimeVal since;
+} GSListenerRefEntry;
+
+enum
+{
+ LOCK,
+ CYCLE,
+ QUIT,
+ SIMULATE_USER_ACTIVITY,
+ ACTIVE_CHANGED,
+ THROTTLE_CHANGED,
+ SHOW_MESSAGE,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_ACTIVE,
+ PROP_SESSION_IDLE,
+ PROP_ACTIVATION_ENABLED,
+};
+
+enum
+{
+ REF_ENTRY_TYPE_INHIBIT,
+ REF_ENTRY_TYPE_THROTTLE
+};
+
+static DBusObjectPathVTable
+gs_listener_vtable = { &gs_listener_unregister_handler,
+ &gs_listener_message_handler,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSListener, gs_listener, G_TYPE_OBJECT)
+
+GQuark
+gs_listener_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (!quark)
+ {
+ quark = g_quark_from_static_string ("gs_listener_error");
+ }
+
+ return quark;
+}
+
+static void
+gs_listener_ref_entry_free (GSListenerRefEntry *entry)
+{
+ g_free (entry->connection);
+ g_free (entry->application);
+ g_free (entry->reason);
+ g_free (entry);
+ entry = NULL;
+}
+
+static void
+gs_listener_unregister_handler (DBusConnection *connection,
+ void *data)
+{
+}
+
+static gboolean
+send_dbus_message (DBusConnection *connection,
+ DBusMessage *message)
+{
+ gboolean is_connected;
+ gboolean sent;
+
+ g_return_val_if_fail (message != NULL, FALSE);
+
+ if (! connection)
+ {
+ gs_debug ("There is no valid connection to the message bus");
+ return FALSE;
+ }
+
+ is_connected = dbus_connection_get_is_connected (connection);
+ if (! is_connected)
+ {
+ gs_debug ("Not connected to the message bus");
+ return FALSE;
+ }
+
+ sent = dbus_connection_send (connection, message, NULL);
+
+ return sent;
+}
+
+static void
+send_dbus_boolean_signal (GSListener *listener,
+ const char *name,
+ gboolean value)
+{
+ DBusMessage *message;
+ DBusMessageIter iter;
+
+ g_return_if_fail (listener != NULL);
+
+ message = dbus_message_new_signal (GS_LISTENER_PATH,
+ GS_LISTENER_SERVICE,
+ name);
+
+ dbus_message_iter_init_append (message, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value);
+
+ if (! send_dbus_message (listener->priv->connection, message))
+ {
+ gs_debug ("Could not send %s signal", name);
+ }
+
+ dbus_message_unref (message);
+}
+
+static void
+gs_listener_send_signal_active_changed (GSListener *listener)
+{
+ g_return_if_fail (listener != NULL);
+
+ gs_debug ("Sending the ActiveChanged(%s) signal on the session bus",
+ listener->priv->active ? "TRUE" : "FALSE");
+
+ send_dbus_boolean_signal (listener, "ActiveChanged", listener->priv->active);
+}
+
+static const char *
+get_name_for_entry_type (int entry_type)
+{
+ const char *name;
+
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ name = "inhibitor";
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ name = "throttler";
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return name;
+}
+
+static GHashTable *
+get_hash_for_entry_type (GSListener *listener,
+ int entry_type)
+{
+ GHashTable *hash;
+
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ hash = listener->priv->inhibitors;
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ hash = listener->priv->throttlers;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return hash;
+}
+
+static void
+list_ref_entry (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ GSListenerRefEntry *entry;
+
+ entry = (GSListenerRefEntry *)value;
+
+ gs_debug ("%s: %s for reason: %s",
+ get_name_for_entry_type (entry->entry_type),
+ entry->application,
+ entry->reason);
+}
+
+static gboolean
+listener_ref_entry_is_present (GSListener *listener,
+ int entry_type)
+{
+ guint n_entries;
+ gboolean is_set;
+ GHashTable *hash;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ /* if we aren't inhibited then activate */
+ n_entries = 0;
+ if (hash != NULL)
+ {
+ n_entries = g_hash_table_size (hash);
+
+ g_hash_table_foreach (hash, list_ref_entry, NULL);
+ }
+
+ is_set = (n_entries > 0);
+
+ return is_set;
+}
+
+static gboolean
+listener_check_activation (GSListener *listener)
+{
+ gboolean inhibited;
+ gboolean res;
+
+ gs_debug ("Checking for activation");
+
+ if (! listener->priv->activation_enabled)
+ {
+ return TRUE;
+ }
+
+ if (! listener->priv->session_idle)
+ {
+ return TRUE;
+ }
+
+ /* if we aren't inhibited then activate */
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ res = FALSE;
+ if (! inhibited)
+ {
+ gs_debug ("Trying to activate");
+ res = gs_listener_set_active (listener, TRUE);
+ }
+
+ return res;
+}
+
+static void
+gs_listener_set_throttle (GSListener *listener,
+ gboolean throttled)
+{
+ g_return_if_fail (GS_IS_LISTENER (listener));
+
+ if (listener->priv->throttled != throttled)
+ {
+ gs_debug ("Changing throttle status: %d", throttled);
+
+ listener->priv->throttled = throttled;
+
+ g_signal_emit (listener, signals [THROTTLE_CHANGED], 0, throttled);
+ }
+}
+
+static gboolean
+listener_check_throttle (GSListener *listener)
+{
+ gboolean throttled;
+
+ gs_debug ("Checking for throttle");
+
+ throttled = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_THROTTLE);
+
+ if (throttled != listener->priv->throttled)
+ {
+ gs_listener_set_throttle (listener, throttled);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+listener_set_session_idle_internal (GSListener *listener,
+ gboolean idle)
+{
+ listener->priv->session_idle = idle;
+
+ if (idle)
+ {
+ listener->priv->session_idle_start = time (NULL);
+ }
+ else
+ {
+ listener->priv->session_idle_start = 0;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+listener_set_active_internal (GSListener *listener,
+ gboolean active)
+{
+ listener->priv->active = active;
+
+ /* if idle not in sync with active, change it */
+ if (listener->priv->session_idle != active)
+ {
+ listener_set_session_idle_internal (listener, active);
+ }
+
+ if (active)
+ {
+ listener->priv->active_start = time (NULL);
+ }
+ else
+ {
+ listener->priv->active_start = 0;
+ }
+
+ gs_listener_send_signal_active_changed (listener);
+
+ return TRUE;
+}
+
+gboolean
+gs_listener_set_active (GSListener *listener,
+ gboolean active)
+{
+ gboolean res;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ if (listener->priv->active == active)
+ {
+ gs_debug ("Trying to set active state when already: %s",
+ active ? "active" : "inactive");
+ return FALSE;
+ }
+
+ res = FALSE;
+ g_signal_emit (listener, signals [ACTIVE_CHANGED], 0, active, &res);
+ if (! res)
+ {
+ /* if the signal is not handled then we haven't changed state */
+ gs_debug ("Active-changed signal not handled");
+
+ /* clear the idle state */
+ if (active)
+ {
+ listener_set_session_idle_internal (listener, FALSE);
+ }
+
+ return FALSE;
+ }
+
+ listener_set_active_internal (listener, active);
+
+ return TRUE;
+}
+
+gboolean
+gs_listener_set_session_idle (GSListener *listener,
+ gboolean idle)
+{
+ gboolean res;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ gs_debug ("Setting session idle: %d", idle);
+
+ if (listener->priv->session_idle == idle)
+ {
+ gs_debug ("Trying to set idle state when already %s",
+ idle ? "idle" : "not idle");
+ return FALSE;
+ }
+
+ if (idle)
+ {
+ gboolean inhibited;
+
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ /* if we are inhibited then do nothing */
+ if (inhibited)
+ {
+ return FALSE;
+ }
+ }
+
+ listener->priv->session_idle = idle;
+ res = listener_check_activation (listener);
+
+ /* if activation fails then don't set idle */
+ if (res)
+ {
+ listener_set_session_idle_internal (listener, idle);
+ }
+ else
+ {
+ gs_debug ("Idle activation failed");
+ listener->priv->session_idle = !idle;
+ }
+
+ return res;
+}
+
+gboolean
+gs_listener_get_activation_enabled (GSListener *listener)
+{
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ return listener->priv->activation_enabled;
+}
+
+gboolean
+gs_listener_is_inhibited (GSListener *listener)
+{
+ gboolean inhibited;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ return inhibited;
+}
+
+void
+gs_listener_set_activation_enabled (GSListener *listener,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_LISTENER (listener));
+
+ if (listener->priv->activation_enabled != enabled)
+ {
+ listener->priv->activation_enabled = enabled;
+ }
+}
+
+static dbus_bool_t
+listener_property_set_bool (GSListener *listener,
+ guint prop_id,
+ dbus_bool_t value)
+{
+ dbus_bool_t ret;
+
+ ret = FALSE;
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ gs_listener_set_active (listener, value);
+ ret = TRUE;
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}
+
+static void
+raise_error (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *error_name,
+ char *format, ...)
+{
+ char buf[512];
+ DBusMessage *reply;
+
+ va_list args;
+ va_start (args, format);
+ vsnprintf (buf, sizeof (buf), format, args);
+ va_end (args);
+
+ gs_debug (buf);
+ reply = dbus_message_new_error (in_reply_to, error_name, buf);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+}
+
+static void
+raise_syntax (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *method_name)
+{
+ raise_error (connection, in_reply_to,
+ GS_LISTENER_SERVICE ".SyntaxError",
+ "There is a syntax error in the invocation of the method %s",
+ method_name);
+}
+
+static guint32
+generate_cookie (void)
+{
+ guint32 cookie;
+
+ cookie = (guint32)g_random_int_range (1, G_MAXINT32);
+
+ return cookie;
+}
+
+static guint32
+listener_generate_unique_key (GSListener *listener,
+ int entry_type)
+{
+ guint32 cookie;
+ GHashTable *hash;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ do
+ {
+ cookie = generate_cookie ();
+ }
+ while (g_hash_table_lookup (hash, &cookie) != NULL);
+
+ return cookie;
+}
+
+static void
+listener_ref_entry_check (GSListener *listener,
+ int entry_type)
+{
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ listener_check_activation (listener);
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ listener_check_throttle (listener);
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+}
+
+static void
+add_session_inhibit (GSListener *listener,
+ GSListenerRefEntry *entry)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusMessageIter reply_iter;
+ DBusError error;
+ guint xid;
+ guint flags;
+
+ g_return_if_fail (listener != NULL);
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (SESSION_NAME,
+ SESSION_PATH,
+ SESSION_INTERFACE,
+ "Inhibit");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return;
+ }
+
+ dbus_message_iter_init_append (message, &iter);
+ xid = 0;
+ flags = 8;
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &entry->application);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &xid);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &entry->reason);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &flags);
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->connection,
+ message,
+ -1,
+ &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return;
+ }
+
+ dbus_message_iter_init (reply, &reply_iter);
+ dbus_message_iter_get_basic (&reply_iter, &entry->foreign_cookie);
+
+ dbus_message_unref (reply);
+}
+
+static void
+remove_session_inhibit (GSListener *listener,
+ GSListenerRefEntry *entry)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusError error;
+
+ g_return_if_fail (listener != NULL);
+
+ if (entry->foreign_cookie == 0)
+ {
+ gs_debug ("Can't remove inhibitor from session: Session cookie not set");
+ return;
+ }
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (SESSION_NAME,
+ SESSION_PATH,
+ SESSION_INTERFACE,
+ "Uninhibit");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return;
+ }
+
+ dbus_message_iter_init_append (message, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &entry->foreign_cookie);
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->connection,
+ message,
+ -1,
+ &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return;
+ }
+
+ dbus_message_unref (reply);
+}
+
+static void
+listener_add_ref_entry (GSListener *listener,
+ int entry_type,
+ GSListenerRefEntry *entry)
+{
+ GHashTable *hash;
+
+ gs_debug ("adding %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+ g_hash_table_insert (hash, &entry->cookie, entry);
+
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* proxy inhibit over to mate session */
+ add_session_inhibit (listener, entry);
+ }
+
+ listener_ref_entry_check (listener, entry_type);
+}
+
+static gboolean
+listener_remove_ref_entry (GSListener *listener,
+ int entry_type,
+ guint32 cookie)
+{
+ GHashTable *hash;
+ gboolean removed;
+ GSListenerRefEntry *entry;
+
+ removed = FALSE;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ entry = g_hash_table_lookup (hash, &cookie);
+ if (entry == NULL)
+ {
+ goto out;
+ }
+
+ gs_debug ("removing %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* remove inhibit from mate session */
+ remove_session_inhibit (listener, entry);
+ }
+
+ removed = g_hash_table_remove (hash, &cookie);
+out:
+ if (removed)
+ {
+ listener_ref_entry_check (listener, entry_type);
+ }
+ else
+ {
+ gs_debug ("Cookie %u was not in the list!", cookie);
+ }
+
+ return removed;
+}
+
+#if GLIB_CHECK_VERSION(2,12,0)
+#define _g_time_val_to_iso8601(t) g_time_val_to_iso8601(t)
+#else
+/* copied from GLib */
+static gchar *
+_g_time_val_to_iso8601 (GTimeVal *time_)
+{
+ gchar *retval;
+
+ g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
+
+#define ISO_8601_LEN 21
+#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
+ retval = g_new0 (gchar, ISO_8601_LEN + 1);
+
+ strftime (retval, ISO_8601_LEN,
+ ISO_8601_FORMAT,
+ gmtime (&(time_->tv_sec)));
+
+ return retval;
+}
+#endif
+
+static void
+accumulate_ref_entry (gpointer key,
+ GSListenerRefEntry *entry,
+ DBusMessageIter *iter)
+{
+ char *description;
+ char *time;
+
+ time = _g_time_val_to_iso8601 (&entry->since);
+
+ description = g_strdup_printf ("Application=\"%s\"; Since=\"%s\"; Reason=\"%s\";",
+ entry->application,
+ time,
+ entry->reason);
+
+ dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &description);
+
+ g_free (description);
+ g_free (time);
+}
+
+static DBusHandlerResult
+listener_dbus_get_ref_entries (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ GHashTable *hash;
+ DBusMessageIter iter;
+ DBusMessageIter iter_array;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_iter_init_append (reply, &iter);
+ dbus_message_iter_open_container (&iter,
+ DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING,
+ &iter_array);
+
+ if (hash != NULL)
+ {
+ g_hash_table_foreach (hash,
+ (GHFunc)accumulate_ref_entry,
+ &iter_array);
+ }
+
+ dbus_message_iter_close_container (&iter, &iter_array);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static void
+listener_add_ck_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint32 *cookiep)
+{
+ GSListenerRefEntry *entry;
+
+ entry = g_new0 (GSListenerRefEntry, 1);
+ entry->entry_type = entry_type;
+ entry->connection = g_strdup (dbus_message_get_sender (message));
+ entry->cookie = listener_generate_unique_key (listener, entry_type);
+ entry->application = g_strdup ("ConsoleKit");
+ entry->reason = g_strdup ("Session is not active");
+ g_get_current_time (&entry->since);
+
+ /* takes ownership of entry */
+ listener_add_ref_entry (listener, entry_type, entry);
+
+ if (cookiep != NULL)
+ {
+ *cookiep = entry->cookie;
+ }
+}
+
+static void
+listener_remove_ck_ref_entry (GSListener *listener,
+ int entry_type,
+ guint32 cookie)
+{
+ listener_remove_ref_entry (listener, entry_type, cookie);
+}
+
+static DBusHandlerResult
+listener_dbus_add_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ DBusError error;
+ const char *application;
+ const char *reason;
+ GSListenerRefEntry *entry;
+ DBusMessageIter iter;
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &application,
+ DBUS_TYPE_STRING, &reason,
+ DBUS_TYPE_INVALID))
+ {
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ raise_syntax (connection, message, "Inhibit");
+ }
+ else if (entry_type == REF_ENTRY_TYPE_THROTTLE)
+ {
+ raise_syntax (connection, message, "Throttle");
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ entry = g_new0 (GSListenerRefEntry, 1);
+ entry->entry_type = entry_type;
+ entry->connection = g_strdup (dbus_message_get_sender (message));
+ entry->cookie = listener_generate_unique_key (listener, entry_type);
+ entry->application = g_strdup (application);
+ entry->reason = g_strdup (reason);
+ g_get_current_time (&entry->since);
+
+ listener_add_ref_entry (listener, entry_type, entry);
+
+ dbus_message_iter_init_append (reply, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &entry->cookie);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_remove_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ DBusError error;
+ const char *sender;
+ guint32 cookie;
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_UINT32, &cookie,
+ DBUS_TYPE_INVALID))
+ {
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ raise_syntax (connection, message, "UnInhibit");
+ }
+ else if (entry_type == REF_ENTRY_TYPE_THROTTLE)
+ {
+ raise_syntax (connection, message, "UnThrottle");
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ g_error ("No memory");
+
+ /* FIXME: check sender is from same connection as entry */
+ sender = dbus_message_get_sender (message);
+
+ listener_remove_ref_entry (listener, entry_type, cookie);
+
+ /* FIXME: Pointless? */
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static gboolean
+listener_ref_entry_remove_for_connection (GSListener *listener,
+ int entry_type,
+ const char *connection)
+{
+ gboolean removed;
+ GHashTable *hash;
+ GHashTableIter iter;
+ GSListenerRefEntry *entry;
+
+ if (connection == NULL)
+ return FALSE;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ removed = FALSE;
+ g_hash_table_iter_init (&iter, hash);
+ while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&entry))
+ {
+ if (entry->connection != NULL &&
+ strcmp (connection, entry->connection) == 0)
+ {
+ gs_debug ("removing %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry->entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ if (entry->entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* remove inhibit from mate session */
+ remove_session_inhibit (listener, entry);
+ }
+
+ g_hash_table_iter_remove (&iter);
+ removed = TRUE;
+ }
+ }
+
+ return removed;
+}
+
+static void
+listener_service_deleted (GSListener *listener,
+ DBusMessage *message)
+{
+ const char *old_service_name;
+ const char *new_service_name;
+ gboolean removed;
+
+ if (! dbus_message_get_args (message, NULL,
+ DBUS_TYPE_STRING, &old_service_name,
+ DBUS_TYPE_STRING, &new_service_name,
+ DBUS_TYPE_INVALID))
+ {
+ g_error ("Invalid NameOwnerChanged signal from bus!");
+ return;
+ }
+
+ gs_debug ("DBUS service deleted: %s", new_service_name);
+
+ removed = listener_ref_entry_remove_for_connection (listener, REF_ENTRY_TYPE_THROTTLE, new_service_name);
+ if (removed)
+ {
+ listener_ref_entry_check (listener, REF_ENTRY_TYPE_THROTTLE);
+ }
+
+ removed = listener_ref_entry_remove_for_connection (listener, REF_ENTRY_TYPE_INHIBIT, new_service_name);
+ if (removed)
+ {
+ listener_ref_entry_check (listener, REF_ENTRY_TYPE_INHIBIT);
+ }
+
+}
+
+static void
+raise_property_type_error (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *device_id)
+{
+ char buf [512];
+ DBusMessage *reply;
+
+ snprintf (buf, 511,
+ "Type mismatch setting property with id %s",
+ device_id);
+ gs_debug (buf);
+
+ reply = dbus_message_new_error (in_reply_to,
+ TYPE_MISMATCH_ERROR,
+ buf);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+}
+
+static DBusHandlerResult
+listener_set_property (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint prop_id)
+{
+ const char *path;
+ int type;
+ gboolean rc;
+ DBusMessageIter iter;
+ DBusMessage *reply;
+
+ path = dbus_message_get_path (message);
+
+ dbus_message_iter_init (message, &iter);
+ type = dbus_message_iter_get_arg_type (&iter);
+ rc = FALSE;
+
+ switch (type)
+ {
+ case DBUS_TYPE_BOOLEAN:
+ {
+ dbus_bool_t v;
+ dbus_message_iter_get_basic (&iter, &v);
+ rc = listener_property_set_bool (listener, prop_id, v);
+ break;
+ }
+ default:
+ gs_debug ("Unsupported property type %d", type);
+ break;
+ }
+
+ if (! rc)
+ {
+ raise_property_type_error (connection, message, path);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_get_property (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint prop_id)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ g_error ("No memory");
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ {
+ dbus_bool_t b;
+ b = listener->priv->active;
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &b);
+ }
+ break;
+ default:
+ gs_debug ("Unsupported property id %u", prop_id);
+ break;
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_get_active_time (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+ dbus_uint32_t secs;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (listener->priv->active)
+ {
+ time_t now = time (NULL);
+
+ if (now < listener->priv->active_start)
+ {
+ /* shouldn't happen */
+ gs_debug ("Active start time is in the future");
+ secs = 0;
+ }
+ else if (listener->priv->active_start <= 0)
+ {
+ /* shouldn't happen */
+ gs_debug ("Active start time was not set");
+ secs = 0;
+ }
+ else
+ {
+ secs = now - listener->priv->active_start;
+ }
+ }
+ else
+ {
+ secs = 0;
+ }
+
+ gs_debug ("Returning screensaver active for %u seconds", secs);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &secs);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_show_message (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+ DBusError error;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (listener->priv->active)
+ {
+ char *summary;
+ char *body;
+ char *icon;
+
+ /* if we're not active we ignore the request */
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &summary,
+ DBUS_TYPE_STRING, &body,
+ DBUS_TYPE_STRING, &icon,
+ DBUS_TYPE_INVALID))
+ {
+ raise_syntax (connection, message, "ShowMessage");
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ g_signal_emit (listener, signals [SHOW_MESSAGE], 0, summary, body, icon);
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+do_introspect (DBusConnection *connection,
+ DBusMessage *message,
+ dbus_bool_t local_interface)
+{
+ DBusMessage *reply;
+ GString *xml;
+ char *xml_string;
+
+ /* standard header */
+ xml = g_string_new ("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
+ "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
+ "<node>\n"
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
+ " <method name=\"Introspect\">\n"
+ " <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
+ " </method>\n"
+ " </interface>\n");
+
+ /* ScreenSaver interface */
+ xml = g_string_append (xml,
+ " <interface name=\"org.mate.ScreenSaver\">\n"
+ " <method name=\"Lock\">\n"
+ " </method>\n"
+ " <method name=\"Cycle\">\n"
+ " </method>\n"
+ " <method name=\"SimulateUserActivity\">\n"
+ " </method>\n"
+ " <method name=\"Inhibit\">\n"
+ " <arg name=\"application_name\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"reason\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"cookie\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"UnInhibit\">\n"
+ " <arg name=\"cookie\" direction=\"in\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"GetInhibitors\">\n"
+ " <arg name=\"list\" direction=\"out\" type=\"as\"/>\n"
+ " </method>\n"
+ " <method name=\"Throttle\">\n"
+ " <arg name=\"application_name\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"reason\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"cookie\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"UnThrottle\">\n"
+ " <arg name=\"cookie\" direction=\"in\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"GetActive\">\n"
+ " <arg name=\"value\" direction=\"out\" type=\"b\"/>\n"
+ " </method>\n"
+ " <method name=\"GetActiveTime\">\n"
+ " <arg name=\"seconds\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"SetActive\">\n"
+ " <arg name=\"value\" direction=\"in\" type=\"b\"/>\n"
+ " </method>\n"
+ " <method name=\"ShowMessage\">\n"
+ " <arg name=\"summary\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"body\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"icon\" direction=\"in\" type=\"s\"/>\n"
+ " </method>\n"
+ " <signal name=\"ActiveChanged\">\n"
+ " <arg name=\"new_value\" type=\"b\"/>\n"
+ " </signal>\n"
+ " </interface>\n");
+
+ reply = dbus_message_new_method_return (message);
+
+ xml = g_string_append (xml, "</node>\n");
+ xml_string = g_string_free (xml, FALSE);
+
+ dbus_message_append_args (reply,
+ DBUS_TYPE_STRING, &xml_string,
+ DBUS_TYPE_INVALID);
+
+ g_free (xml_string);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_handle_session_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data,
+ dbus_bool_t local_interface)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+
+#if 0
+ g_message ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Lock"))
+ {
+ g_signal_emit (listener, signals [LOCK], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Quit"))
+ {
+ g_signal_emit (listener, signals [QUIT], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Cycle"))
+ {
+ g_signal_emit (listener, signals [CYCLE], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Inhibit"))
+ {
+ return listener_dbus_add_ref_entry (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "UnInhibit"))
+ {
+ return listener_dbus_remove_ref_entry (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetInhibitors"))
+ {
+ return listener_dbus_get_ref_entries (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Throttle"))
+ {
+ return listener_dbus_add_ref_entry (listener, REF_ENTRY_TYPE_THROTTLE, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "UnThrottle"))
+ {
+ return listener_dbus_remove_ref_entry (listener, REF_ENTRY_TYPE_THROTTLE, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "SetActive"))
+ {
+ return listener_set_property (listener, connection, message, PROP_ACTIVE);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetActive"))
+ {
+ return listener_get_property (listener, connection, message, PROP_ACTIVE);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetActiveTime"))
+ {
+ return listener_get_active_time (listener, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "ShowMessage"))
+ {
+ return listener_show_message (listener, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "SimulateUserActivity"))
+ {
+ g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, "org.freedesktop.DBus.Introspectable", "Introspect"))
+ {
+ return do_introspect (connection, message, local_interface);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static gboolean
+_listener_message_path_is_our_session (GSListener *listener,
+ DBusMessage *message)
+{
+ const char *ssid;
+ gboolean ours;
+
+ ours = FALSE;
+
+ ssid = dbus_message_get_path (message);
+ if (ssid != NULL
+ && listener->priv->session_id != NULL
+ && strcmp (ssid, listener->priv->session_id) == 0)
+ {
+ ours = TRUE;
+ }
+
+ return ours;
+}
+
+static DBusHandlerResult
+listener_dbus_handle_system_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data,
+ dbus_bool_t local_interface)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+#if 1
+ gs_debug ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ if (dbus_message_is_signal (message, HAL_DEVICE_INTERFACE, "Condition"))
+ {
+ DBusError error;
+ const char *event;
+ const char *keyname;
+
+ dbus_error_init (&error);
+ if (dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &event,
+ DBUS_TYPE_STRING, &keyname,
+ DBUS_TYPE_INVALID))
+ {
+ if ((event && strcmp (event, "ButtonPressed") == 0) &&
+ (keyname && strcmp (keyname, "coffee") == 0))
+ {
+ gs_debug ("Coffee key was pressed - locking");
+ g_signal_emit (listener, signals [LOCK], 0);
+ }
+ }
+
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Unlock"))
+ {
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ gs_debug ("Console kit requested session unlock");
+ gs_listener_set_active (listener, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Lock"))
+ {
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ gs_debug ("ConsoleKit requested session lock");
+ g_signal_emit (listener, signals [LOCK], 0);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "ActiveChanged"))
+ {
+ /* NB that `ActiveChanged' refers to the active
+ * session in ConsoleKit terminology - ie which
+ * session is currently displayed on the screen.
+ * mate-screensaver uses `active' to mean `is the
+ * screensaver active' (ie, is the screen locked) but
+ * that's not what we're referring to here.
+ */
+
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ DBusError error;
+ dbus_bool_t new_active;
+
+ dbus_error_init (&error);
+ if (dbus_message_get_args (message, &error,
+ DBUS_TYPE_BOOLEAN, &new_active,
+ DBUS_TYPE_INVALID))
+ {
+ gs_debug ("ConsoleKit notified ActiveChanged %d", new_active);
+
+ /* when we aren't active add an implicit throttle from CK
+ * when we become active remove the throttle and poke the lock */
+ if (new_active)
+ {
+ if (listener->priv->ck_throttle_cookie != 0)
+ {
+ listener_remove_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ listener->priv->ck_throttle_cookie);
+ listener->priv->ck_throttle_cookie = 0;
+ }
+
+ g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0);
+ }
+ else
+ {
+ if (listener->priv->ck_throttle_cookie != 0)
+ {
+ g_warning ("ConsoleKit throttle already set");
+ listener_remove_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ listener->priv->ck_throttle_cookie);
+ listener->priv->ck_throttle_cookie = 0;
+ }
+
+ listener_add_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ connection,
+ message,
+ &listener->priv->ck_throttle_cookie);
+ }
+ }
+
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static DBusHandlerResult
+gs_listener_message_handler (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+#if 0
+ g_message ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ if (dbus_message_is_method_call (message, "org.freedesktop.DBus", "AddMatch"))
+ {
+ DBusMessage *reply;
+
+ reply = dbus_message_new_method_return (message);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
+ strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0)
+ {
+ dbus_connection_unref (connection);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else
+ {
+ return listener_dbus_handle_session_message (connection, message, user_data, TRUE);
+ }
+}
+
+static gboolean
+gs_listener_dbus_init (GSListener *listener)
+{
+ DBusError error;
+
+ dbus_error_init (&error);
+
+ if (listener->priv->connection == NULL)
+ {
+ listener->priv->connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
+ if (listener->priv->connection == NULL)
+ {
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("couldn't connect to session bus: %s",
+ error.message);
+ dbus_error_free (&error);
+ }
+ return FALSE;
+ }
+
+ dbus_connection_setup_with_g_main (listener->priv->connection, NULL);
+ dbus_connection_set_exit_on_disconnect (listener->priv->connection, FALSE);
+ }
+
+ if (listener->priv->system_connection == NULL)
+ {
+ listener->priv->system_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+ if (listener->priv->system_connection == NULL)
+ {
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("couldn't connect to system bus: %s",
+ error.message);
+ dbus_error_free (&error);
+ }
+ return FALSE;
+ }
+
+ dbus_connection_setup_with_g_main (listener->priv->system_connection, NULL);
+ dbus_connection_set_exit_on_disconnect (listener->priv->system_connection, FALSE);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+reinit_dbus (GSListener *listener)
+{
+ gboolean initialized;
+ gboolean try_again;
+
+ initialized = gs_listener_dbus_init (listener);
+
+ /* if we didn't initialize then try again */
+ /* FIXME: Should we keep trying forever? If we fail more than
+ once or twice then the session bus may have died. The
+ problem is that if it is restarted it will likely have a
+ different bus address and we won't be able to find it */
+ try_again = !initialized;
+
+ return try_again;
+}
+
+static DBusHandlerResult
+listener_dbus_filter_function (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+ const char *path;
+
+ path = dbus_message_get_path (message);
+
+ /*
+ g_message ("obj_path=%s interface=%s method=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message));
+ */
+
+ if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")
+ && strcmp (path, DBUS_PATH_LOCAL) == 0)
+ {
+
+ g_message ("Got disconnected from the session message bus; "
+ "retrying to reconnect every 10 seconds");
+
+ dbus_connection_unref (connection);
+ listener->priv->connection = NULL;
+
+ g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener);
+ }
+ else if (dbus_message_is_signal (message,
+ DBUS_INTERFACE_DBUS,
+ "NameOwnerChanged"))
+ {
+
+ if (listener->priv->inhibitors != NULL)
+ {
+ listener_service_deleted (listener, message);
+ }
+ }
+ else
+ {
+ return listener_dbus_handle_session_message (connection, message, user_data, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_system_filter_function (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+ const char *path;
+
+ path = dbus_message_get_path (message);
+
+ if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")
+ && strcmp (path, DBUS_PATH_LOCAL) == 0)
+ {
+
+ g_message ("Got disconnected from the system message bus; "
+ "retrying to reconnect every 10 seconds");
+
+ dbus_connection_unref (connection);
+ listener->priv->system_connection = NULL;
+
+ g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener);
+ }
+ else
+ {
+ return listener_dbus_handle_system_message (connection, message, user_data, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static void
+gs_listener_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSListener *self;
+
+ self = GS_LISTENER (object);
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ gs_listener_set_active (self, g_value_get_boolean (value));
+ break;
+ case PROP_SESSION_IDLE:
+ gs_listener_set_session_idle (self, g_value_get_boolean (value));
+ break;
+ case PROP_ACTIVATION_ENABLED:
+ gs_listener_set_activation_enabled (self, g_value_get_boolean (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_listener_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSListener *self;
+
+ self = GS_LISTENER (object);
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ g_value_set_boolean (value, self->priv->active);
+ break;
+ case PROP_SESSION_IDLE:
+ g_value_set_boolean (value, self->priv->session_idle);
+ break;
+ case PROP_ACTIVATION_ENABLED:
+ g_value_set_boolean (value, self->priv->activation_enabled);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_listener_class_init (GSListenerClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_listener_finalize;
+ object_class->get_property = gs_listener_get_property;
+ object_class->set_property = gs_listener_set_property;
+
+ signals [LOCK] =
+ g_signal_new ("lock",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, lock),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [QUIT] =
+ g_signal_new ("quit",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, quit),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [CYCLE] =
+ g_signal_new ("cycle",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, cycle),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [SIMULATE_USER_ACTIVITY] =
+ g_signal_new ("simulate-user-activity",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, simulate_user_activity),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [ACTIVE_CHANGED] =
+ g_signal_new ("active-changed",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, active_changed),
+ NULL,
+ NULL,
+ gs_marshal_BOOLEAN__BOOLEAN,
+ G_TYPE_BOOLEAN,
+ 1,
+ G_TYPE_BOOLEAN);
+ signals [THROTTLE_CHANGED] =
+ g_signal_new ("throttle-changed",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, throttle_changed),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__BOOLEAN,
+ G_TYPE_NONE,
+ 1,
+ G_TYPE_BOOLEAN);
+ signals [SHOW_MESSAGE] =
+ g_signal_new ("show-message",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, show_message),
+ NULL,
+ NULL,
+ gs_marshal_VOID__STRING_STRING_STRING,
+ G_TYPE_NONE,
+ 3,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING);
+
+ g_object_class_install_property (object_class,
+ PROP_ACTIVE,
+ g_param_spec_boolean ("active",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_ACTIVATION_ENABLED,
+ g_param_spec_boolean ("activation-enabled",
+ NULL,
+ NULL,
+ TRUE,
+ G_PARAM_READWRITE));
+
+ g_type_class_add_private (klass, sizeof (GSListenerPrivate));
+}
+
+
+static gboolean
+screensaver_is_running (DBusConnection *connection)
+{
+ DBusError error;
+ gboolean exists;
+
+ g_return_val_if_fail (connection != NULL, FALSE);
+
+ dbus_error_init (&error);
+ exists = dbus_bus_name_has_owner (connection, GS_LISTENER_SERVICE, &error);
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+
+ return exists;
+}
+
+gboolean
+gs_listener_acquire (GSListener *listener,
+ GError **error)
+{
+ gboolean acquired;
+ DBusError buserror;
+ gboolean is_connected;
+
+ g_return_val_if_fail (listener != NULL, FALSE);
+
+ if (! listener->priv->connection)
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("failed to register with the message bus"));
+ return FALSE;
+ }
+
+ is_connected = dbus_connection_get_is_connected (listener->priv->connection);
+ if (! is_connected)
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("not connected to the message bus"));
+ return FALSE;
+ }
+
+ if (screensaver_is_running (listener->priv->connection))
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("screensaver already running in this session"));
+ return FALSE;
+ }
+
+ dbus_error_init (&buserror);
+
+ if (dbus_connection_register_object_path (listener->priv->connection,
+ GS_LISTENER_PATH,
+ &gs_listener_vtable,
+ listener) == FALSE)
+ {
+ g_critical ("out of memory registering object path");
+ return FALSE;
+ }
+
+ acquired = dbus_bus_request_name (listener->priv->connection,
+ GS_LISTENER_SERVICE,
+ 0, &buserror) != -1;
+ if (dbus_error_is_set (&buserror))
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ buserror.message);
+ }
+
+ dbus_error_free (&buserror);
+
+ dbus_connection_add_filter (listener->priv->connection, listener_dbus_filter_function, listener, NULL);
+
+ dbus_bus_add_match (listener->priv->connection,
+ "type='signal'"
+ ",interface='"DBUS_INTERFACE_DBUS"'"
+ ",sender='"DBUS_SERVICE_DBUS"'"
+ ",member='NameOwnerChanged'",
+ NULL);
+
+ if (listener->priv->system_connection != NULL)
+ {
+ dbus_connection_add_filter (listener->priv->system_connection,
+ listener_dbus_system_filter_function,
+ listener,
+ NULL);
+
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"HAL_DEVICE_INTERFACE"'"
+ ",member='Condition'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='Unlock'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='Lock'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='ActiveChanged'",
+ NULL);
+ }
+
+ return acquired;
+}
+
+static char *
+query_session_id (GSListener *listener)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusError error;
+ DBusMessageIter reply_iter;
+ char *ssid;
+
+ if (listener->priv->system_connection == NULL)
+ {
+ gs_debug ("No connection to the system bus");
+ return NULL;
+ }
+
+ ssid = NULL;
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (CK_NAME, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, "GetCurrentSession");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return NULL;
+ }
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection,
+ message,
+ -1, &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return NULL;
+ }
+
+ dbus_message_iter_init (reply, &reply_iter);
+ dbus_message_iter_get_basic (&reply_iter, &ssid);
+
+ dbus_message_unref (reply);
+
+ return g_strdup (ssid);
+}
+
+static void
+init_session_id (GSListener *listener)
+{
+ g_free (listener->priv->session_id);
+ listener->priv->session_id = query_session_id (listener);
+ gs_debug ("Got session-id: %s", listener->priv->session_id);
+}
+
+static void
+gs_listener_init (GSListener *listener)
+{
+ listener->priv = GS_LISTENER_GET_PRIVATE (listener);
+
+ gs_listener_dbus_init (listener);
+
+ init_session_id (listener);
+
+ listener->priv->inhibitors = g_hash_table_new_full (g_int_hash,
+ g_int_equal,
+ NULL,
+ (GDestroyNotify)gs_listener_ref_entry_free);
+ listener->priv->throttlers = g_hash_table_new_full (g_int_hash,
+ g_int_equal,
+ NULL,
+ (GDestroyNotify)gs_listener_ref_entry_free);
+}
+
+static void
+gs_listener_finalize (GObject *object)
+{
+ GSListener *listener;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_LISTENER (object));
+
+ listener = GS_LISTENER (object);
+
+ g_return_if_fail (listener->priv != NULL);
+
+ if (listener->priv->inhibitors)
+ {
+ g_hash_table_destroy (listener->priv->inhibitors);
+ }
+
+ if (listener->priv->throttlers)
+ {
+ g_hash_table_destroy (listener->priv->throttlers);
+ }
+
+ g_free (listener->priv->session_id);
+
+ G_OBJECT_CLASS (gs_listener_parent_class)->finalize (object);
+}
+
+GSListener *
+gs_listener_new (void)
+{
+ GSListener *listener;
+
+ listener = g_object_new (GS_TYPE_LISTENER, NULL);
+
+ return GS_LISTENER (listener);
+}
diff --git a/src/gs-listener-dbus.c.orig b/src/gs-listener-dbus.c.orig
new file mode 100644
index 0000000..b8d4fab
--- /dev/null
+++ b/src/gs-listener-dbus.c.orig
@@ -0,0 +1,2272 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <string.h>
+
+#include <glib/gi18n.h>
+
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+
+#include "gs-listener-dbus.h"
+#include "gs-marshal.h"
+#include "gs-debug.h"
+
+/* this is for dbus < 0.3 */
+#if ((DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 30))
+#define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
+#define dbus_bus_request_name(connection, name, flags, err) dbus_bus_acquire_service(connection, name, flags, err)
+#endif
+
+static void gs_listener_class_init (GSListenerClass *klass);
+static void gs_listener_init (GSListener *listener);
+static void gs_listener_finalize (GObject *object);
+
+static void gs_listener_unregister_handler (DBusConnection *connection,
+ void *data);
+
+static DBusHandlerResult gs_listener_message_handler (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data);
+
+#define GS_LISTENER_SERVICE "org.mate.ScreenSaver"
+#define GS_LISTENER_PATH "/org/mate/ScreenSaver"
+#define GS_LISTENER_INTERFACE "org.mate.ScreenSaver"
+
+#define HAL_DEVICE_INTERFACE "org.freedesktop.Hal.Device"
+
+#define CK_NAME "org.freedesktop.ConsoleKit"
+#define CK_MANAGER_PATH "/org/freedesktop/ConsoleKit/Manager"
+#define CK_MANAGER_INTERFACE "org.freedesktop.ConsoleKit.Manager"
+#define CK_SESSION_INTERFACE "org.freedesktop.ConsoleKit.Session"
+
+#define SESSION_NAME "org.mate.SessionManager"
+#define SESSION_PATH "/org/mate/SessionManager"
+#define SESSION_INTERFACE "org.mate.SessionManager"
+
+#define TYPE_MISMATCH_ERROR GS_LISTENER_INTERFACE ".TypeMismatch"
+
+#define GS_LISTENER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_LISTENER, GSListenerPrivate))
+
+struct GSListenerPrivate
+{
+ DBusConnection *connection;
+ DBusConnection *system_connection;
+
+ guint session_idle : 1;
+ guint active : 1;
+ guint activation_enabled : 1;
+ guint throttled : 1;
+ GHashTable *inhibitors;
+ GHashTable *throttlers;
+ time_t active_start;
+ time_t session_idle_start;
+ char *session_id;
+
+ guint32 ck_throttle_cookie;
+};
+
+typedef struct
+{
+ int entry_type;
+ char *application;
+ char *reason;
+ char *connection;
+ guint32 cookie;
+ guint32 foreign_cookie;
+ GTimeVal since;
+} GSListenerRefEntry;
+
+enum
+{
+ LOCK,
+ CYCLE,
+ QUIT,
+ SIMULATE_USER_ACTIVITY,
+ ACTIVE_CHANGED,
+ THROTTLE_CHANGED,
+ SHOW_MESSAGE,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_ACTIVE,
+ PROP_SESSION_IDLE,
+ PROP_ACTIVATION_ENABLED,
+};
+
+enum
+{
+ REF_ENTRY_TYPE_INHIBIT,
+ REF_ENTRY_TYPE_THROTTLE
+};
+
+static DBusObjectPathVTable
+gs_listener_vtable = { &gs_listener_unregister_handler,
+ &gs_listener_message_handler,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+ };
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSListener, gs_listener, G_TYPE_OBJECT)
+
+GQuark
+gs_listener_error_quark (void)
+{
+ static GQuark quark = 0;
+ if (!quark)
+ {
+ quark = g_quark_from_static_string ("gs_listener_error");
+ }
+
+ return quark;
+}
+
+static void
+gs_listener_ref_entry_free (GSListenerRefEntry *entry)
+{
+ g_free (entry->connection);
+ g_free (entry->application);
+ g_free (entry->reason);
+ g_free (entry);
+ entry = NULL;
+}
+
+static void
+gs_listener_unregister_handler (DBusConnection *connection,
+ void *data)
+{
+}
+
+static gboolean
+send_dbus_message (DBusConnection *connection,
+ DBusMessage *message)
+{
+ gboolean is_connected;
+ gboolean sent;
+
+ g_return_val_if_fail (message != NULL, FALSE);
+
+ if (! connection)
+ {
+ gs_debug ("There is no valid connection to the message bus");
+ return FALSE;
+ }
+
+ is_connected = dbus_connection_get_is_connected (connection);
+ if (! is_connected)
+ {
+ gs_debug ("Not connected to the message bus");
+ return FALSE;
+ }
+
+ sent = dbus_connection_send (connection, message, NULL);
+
+ return sent;
+}
+
+static void
+send_dbus_boolean_signal (GSListener *listener,
+ const char *name,
+ gboolean value)
+{
+ DBusMessage *message;
+ DBusMessageIter iter;
+
+ g_return_if_fail (listener != NULL);
+
+ message = dbus_message_new_signal (GS_LISTENER_PATH,
+ GS_LISTENER_SERVICE,
+ name);
+
+ dbus_message_iter_init_append (message, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value);
+
+ if (! send_dbus_message (listener->priv->connection, message))
+ {
+ gs_debug ("Could not send %s signal", name);
+ }
+
+ dbus_message_unref (message);
+}
+
+static void
+gs_listener_send_signal_active_changed (GSListener *listener)
+{
+ g_return_if_fail (listener != NULL);
+
+ gs_debug ("Sending the ActiveChanged(%s) signal on the session bus",
+ listener->priv->active ? "TRUE" : "FALSE");
+
+ send_dbus_boolean_signal (listener, "ActiveChanged", listener->priv->active);
+}
+
+static const char *
+get_name_for_entry_type (int entry_type)
+{
+ const char *name;
+
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ name = "inhibitor";
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ name = "throttler";
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return name;
+}
+
+static GHashTable *
+get_hash_for_entry_type (GSListener *listener,
+ int entry_type)
+{
+ GHashTable *hash;
+
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ hash = listener->priv->inhibitors;
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ hash = listener->priv->throttlers;
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+
+ return hash;
+}
+
+static void
+list_ref_entry (gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ GSListenerRefEntry *entry;
+
+ entry = (GSListenerRefEntry *)value;
+
+ gs_debug ("%s: %s for reason: %s",
+ get_name_for_entry_type (entry->entry_type),
+ entry->application,
+ entry->reason);
+}
+
+static gboolean
+listener_ref_entry_is_present (GSListener *listener,
+ int entry_type)
+{
+ guint n_entries;
+ gboolean is_set;
+ GHashTable *hash;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ /* if we aren't inhibited then activate */
+ n_entries = 0;
+ if (hash != NULL)
+ {
+ n_entries = g_hash_table_size (hash);
+
+ g_hash_table_foreach (hash, list_ref_entry, NULL);
+ }
+
+ is_set = (n_entries > 0);
+
+ return is_set;
+}
+
+static gboolean
+listener_check_activation (GSListener *listener)
+{
+ gboolean inhibited;
+ gboolean res;
+
+ gs_debug ("Checking for activation");
+
+ if (! listener->priv->activation_enabled)
+ {
+ return TRUE;
+ }
+
+ if (! listener->priv->session_idle)
+ {
+ return TRUE;
+ }
+
+ /* if we aren't inhibited then activate */
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ res = FALSE;
+ if (! inhibited)
+ {
+ gs_debug ("Trying to activate");
+ res = gs_listener_set_active (listener, TRUE);
+ }
+
+ return res;
+}
+
+static void
+gs_listener_set_throttle (GSListener *listener,
+ gboolean throttled)
+{
+ g_return_if_fail (GS_IS_LISTENER (listener));
+
+ if (listener->priv->throttled != throttled)
+ {
+ gs_debug ("Changing throttle status: %d", throttled);
+
+ listener->priv->throttled = throttled;
+
+ g_signal_emit (listener, signals [THROTTLE_CHANGED], 0, throttled);
+ }
+}
+
+static gboolean
+listener_check_throttle (GSListener *listener)
+{
+ gboolean throttled;
+
+ gs_debug ("Checking for throttle");
+
+ throttled = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_THROTTLE);
+
+ if (throttled != listener->priv->throttled)
+ {
+ gs_listener_set_throttle (listener, throttled);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+listener_set_session_idle_internal (GSListener *listener,
+ gboolean idle)
+{
+ listener->priv->session_idle = idle;
+
+ if (idle)
+ {
+ listener->priv->session_idle_start = time (NULL);
+ }
+ else
+ {
+ listener->priv->session_idle_start = 0;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+listener_set_active_internal (GSListener *listener,
+ gboolean active)
+{
+ listener->priv->active = active;
+
+ /* if idle not in sync with active, change it */
+ if (listener->priv->session_idle != active)
+ {
+ listener_set_session_idle_internal (listener, active);
+ }
+
+ if (active)
+ {
+ listener->priv->active_start = time (NULL);
+ }
+ else
+ {
+ listener->priv->active_start = 0;
+ }
+
+ gs_listener_send_signal_active_changed (listener);
+
+ return TRUE;
+}
+
+gboolean
+gs_listener_set_active (GSListener *listener,
+ gboolean active)
+{
+ gboolean res;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ if (listener->priv->active == active)
+ {
+ gs_debug ("Trying to set active state when already: %s",
+ active ? "active" : "inactive");
+ return FALSE;
+ }
+
+ res = FALSE;
+ g_signal_emit (listener, signals [ACTIVE_CHANGED], 0, active, &res);
+ if (! res)
+ {
+ /* if the signal is not handled then we haven't changed state */
+ gs_debug ("Active-changed signal not handled");
+
+ /* clear the idle state */
+ if (active)
+ {
+ listener_set_session_idle_internal (listener, FALSE);
+ }
+
+ return FALSE;
+ }
+
+ listener_set_active_internal (listener, active);
+
+ return TRUE;
+}
+
+gboolean
+gs_listener_set_session_idle (GSListener *listener,
+ gboolean idle)
+{
+ gboolean res;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ gs_debug ("Setting session idle: %d", idle);
+
+ if (listener->priv->session_idle == idle)
+ {
+ gs_debug ("Trying to set idle state when already %s",
+ idle ? "idle" : "not idle");
+ return FALSE;
+ }
+
+ if (idle)
+ {
+ gboolean inhibited;
+
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ /* if we are inhibited then do nothing */
+ if (inhibited)
+ {
+ return FALSE;
+ }
+ }
+
+ listener->priv->session_idle = idle;
+ res = listener_check_activation (listener);
+
+ /* if activation fails then don't set idle */
+ if (res)
+ {
+ listener_set_session_idle_internal (listener, idle);
+ }
+ else
+ {
+ gs_debug ("Idle activation failed");
+ listener->priv->session_idle = !idle;
+ }
+
+ return res;
+}
+
+gboolean
+gs_listener_get_activation_enabled (GSListener *listener)
+{
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ return listener->priv->activation_enabled;
+}
+
+gboolean
+gs_listener_is_inhibited (GSListener *listener)
+{
+ gboolean inhibited;
+
+ g_return_val_if_fail (GS_IS_LISTENER (listener), FALSE);
+
+ inhibited = listener_ref_entry_is_present (listener, REF_ENTRY_TYPE_INHIBIT);
+
+ return inhibited;
+}
+
+void
+gs_listener_set_activation_enabled (GSListener *listener,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_LISTENER (listener));
+
+ if (listener->priv->activation_enabled != enabled)
+ {
+ listener->priv->activation_enabled = enabled;
+ }
+}
+
+static dbus_bool_t
+listener_property_set_bool (GSListener *listener,
+ guint prop_id,
+ dbus_bool_t value)
+{
+ dbus_bool_t ret;
+
+ ret = FALSE;
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ gs_listener_set_active (listener, value);
+ ret = TRUE;
+ break;
+ default:
+ break;
+ }
+
+ return ret;
+}
+
+static void
+raise_error (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *error_name,
+ char *format, ...)
+{
+ char buf[512];
+ DBusMessage *reply;
+
+ va_list args;
+ va_start (args, format);
+ vsnprintf (buf, sizeof (buf), format, args);
+ va_end (args);
+
+ gs_debug (buf);
+ reply = dbus_message_new_error (in_reply_to, error_name, buf);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+}
+
+static void
+raise_syntax (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *method_name)
+{
+ raise_error (connection, in_reply_to,
+ GS_LISTENER_SERVICE ".SyntaxError",
+ "There is a syntax error in the invocation of the method %s",
+ method_name);
+}
+
+static guint32
+generate_cookie (void)
+{
+ guint32 cookie;
+
+ cookie = (guint32)g_random_int_range (1, G_MAXINT32);
+
+ return cookie;
+}
+
+static guint32
+listener_generate_unique_key (GSListener *listener,
+ int entry_type)
+{
+ guint32 cookie;
+ GHashTable *hash;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ do
+ {
+ cookie = generate_cookie ();
+ }
+ while (g_hash_table_lookup (hash, &cookie) != NULL);
+
+ return cookie;
+}
+
+static void
+listener_ref_entry_check (GSListener *listener,
+ int entry_type)
+{
+ switch (entry_type)
+ {
+ case REF_ENTRY_TYPE_INHIBIT:
+ listener_check_activation (listener);
+ break;
+ case REF_ENTRY_TYPE_THROTTLE:
+ listener_check_throttle (listener);
+ break;
+ default:
+ g_assert_not_reached ();
+ break;
+ }
+}
+
+static void
+add_session_inhibit (GSListener *listener,
+ GSListenerRefEntry *entry)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusMessageIter reply_iter;
+ DBusError error;
+ guint xid;
+ guint flags;
+
+ g_return_if_fail (listener != NULL);
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (SESSION_NAME,
+ SESSION_PATH,
+ SESSION_INTERFACE,
+ "Inhibit");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return;
+ }
+
+ dbus_message_iter_init_append (message, &iter);
+ xid = 0;
+ flags = 8;
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &entry->application);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &xid);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &entry->reason);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &flags);
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->connection,
+ message,
+ -1,
+ &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return;
+ }
+
+ dbus_message_iter_init (reply, &reply_iter);
+ dbus_message_iter_get_basic (&reply_iter, &entry->foreign_cookie);
+
+ dbus_message_unref (reply);
+}
+
+static void
+remove_session_inhibit (GSListener *listener,
+ GSListenerRefEntry *entry)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ DBusError error;
+
+ g_return_if_fail (listener != NULL);
+
+ if (entry->foreign_cookie == 0)
+ {
+ gs_debug ("Can't remove inhibitor from session: Session cookie not set");
+ return;
+ }
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (SESSION_NAME,
+ SESSION_PATH,
+ SESSION_INTERFACE,
+ "Uninhibit");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return;
+ }
+
+ dbus_message_iter_init_append (message, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &entry->foreign_cookie);
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->connection,
+ message,
+ -1,
+ &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return;
+ }
+
+ dbus_message_unref (reply);
+}
+
+static void
+listener_add_ref_entry (GSListener *listener,
+ int entry_type,
+ GSListenerRefEntry *entry)
+{
+ GHashTable *hash;
+
+ gs_debug ("adding %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+ g_hash_table_insert (hash, &entry->cookie, entry);
+
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* proxy inhibit over to mate session */
+ add_session_inhibit (listener, entry);
+ }
+
+ listener_ref_entry_check (listener, entry_type);
+}
+
+static gboolean
+listener_remove_ref_entry (GSListener *listener,
+ int entry_type,
+ guint32 cookie)
+{
+ GHashTable *hash;
+ gboolean removed;
+ GSListenerRefEntry *entry;
+
+ removed = FALSE;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ entry = g_hash_table_lookup (hash, &cookie);
+ if (entry == NULL)
+ {
+ goto out;
+ }
+
+ gs_debug ("removing %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* remove inhibit from mate session */
+ remove_session_inhibit (listener, entry);
+ }
+
+ removed = g_hash_table_remove (hash, &cookie);
+out:
+ if (removed)
+ {
+ listener_ref_entry_check (listener, entry_type);
+ }
+ else
+ {
+ gs_debug ("Cookie %u was not in the list!", cookie);
+ }
+
+ return removed;
+}
+
+#if GLIB_CHECK_VERSION(2,12,0)
+#define _g_time_val_to_iso8601(t) g_time_val_to_iso8601(t)
+#else
+/* copied from GLib */
+static gchar *
+_g_time_val_to_iso8601 (GTimeVal *time_)
+{
+ gchar *retval;
+
+ g_return_val_if_fail (time_->tv_usec >= 0 && time_->tv_usec < G_USEC_PER_SEC, NULL);
+
+#define ISO_8601_LEN 21
+#define ISO_8601_FORMAT "%Y-%m-%dT%H:%M:%SZ"
+ retval = g_new0 (gchar, ISO_8601_LEN + 1);
+
+ strftime (retval, ISO_8601_LEN,
+ ISO_8601_FORMAT,
+ gmtime (&(time_->tv_sec)));
+
+ return retval;
+}
+#endif
+
+static void
+accumulate_ref_entry (gpointer key,
+ GSListenerRefEntry *entry,
+ DBusMessageIter *iter)
+{
+ char *description;
+ char *time;
+
+ time = _g_time_val_to_iso8601 (&entry->since);
+
+ description = g_strdup_printf ("Application=\"%s\"; Since=\"%s\"; Reason=\"%s\";",
+ entry->application,
+ time,
+ entry->reason);
+
+ dbus_message_iter_append_basic (iter, DBUS_TYPE_STRING, &description);
+
+ g_free (description);
+ g_free (time);
+}
+
+static DBusHandlerResult
+listener_dbus_get_ref_entries (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ GHashTable *hash;
+ DBusMessageIter iter;
+ DBusMessageIter iter_array;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_iter_init_append (reply, &iter);
+ dbus_message_iter_open_container (&iter,
+ DBUS_TYPE_ARRAY,
+ DBUS_TYPE_STRING_AS_STRING,
+ &iter_array);
+
+ if (hash != NULL)
+ {
+ g_hash_table_foreach (hash,
+ (GHFunc)accumulate_ref_entry,
+ &iter_array);
+ }
+
+ dbus_message_iter_close_container (&iter, &iter_array);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static void
+listener_add_ck_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint32 *cookiep)
+{
+ GSListenerRefEntry *entry;
+
+ entry = g_new0 (GSListenerRefEntry, 1);
+ entry->entry_type = entry_type;
+ entry->connection = g_strdup (dbus_message_get_sender (message));
+ entry->cookie = listener_generate_unique_key (listener, entry_type);
+ entry->application = g_strdup ("ConsoleKit");
+ entry->reason = g_strdup ("Session is not active");
+ g_get_current_time (&entry->since);
+
+ /* takes ownership of entry */
+ listener_add_ref_entry (listener, entry_type, entry);
+
+ if (cookiep != NULL)
+ {
+ *cookiep = entry->cookie;
+ }
+}
+
+static void
+listener_remove_ck_ref_entry (GSListener *listener,
+ int entry_type,
+ guint32 cookie)
+{
+ listener_remove_ref_entry (listener, entry_type, cookie);
+}
+
+static DBusHandlerResult
+listener_dbus_add_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ DBusError error;
+ const char *application;
+ const char *reason;
+ GSListenerRefEntry *entry;
+ DBusMessageIter iter;
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &application,
+ DBUS_TYPE_STRING, &reason,
+ DBUS_TYPE_INVALID))
+ {
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ raise_syntax (connection, message, "Inhibit");
+ }
+ else if (entry_type == REF_ENTRY_TYPE_THROTTLE)
+ {
+ raise_syntax (connection, message, "Throttle");
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ entry = g_new0 (GSListenerRefEntry, 1);
+ entry->entry_type = entry_type;
+ entry->connection = g_strdup (dbus_message_get_sender (message));
+ entry->cookie = listener_generate_unique_key (listener, entry_type);
+ entry->application = g_strdup (application);
+ entry->reason = g_strdup (reason);
+ g_get_current_time (&entry->since);
+
+ listener_add_ref_entry (listener, entry_type, entry);
+
+ dbus_message_iter_init_append (reply, &iter);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &entry->cookie);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_remove_ref_entry (GSListener *listener,
+ int entry_type,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessage *reply;
+ DBusError error;
+ const char *sender;
+ guint32 cookie;
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_UINT32, &cookie,
+ DBUS_TYPE_INVALID))
+ {
+ if (entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ raise_syntax (connection, message, "UnInhibit");
+ }
+ else if (entry_type == REF_ENTRY_TYPE_THROTTLE)
+ {
+ raise_syntax (connection, message, "UnThrottle");
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ g_error ("No memory");
+
+ /* FIXME: check sender is from same connection as entry */
+ sender = dbus_message_get_sender (message);
+
+ listener_remove_ref_entry (listener, entry_type, cookie);
+
+ /* FIXME: Pointless? */
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static gboolean
+listener_ref_entry_remove_for_connection (GSListener *listener,
+ int entry_type,
+ const char *connection)
+{
+ gboolean removed;
+ GHashTable *hash;
+ GHashTableIter iter;
+ GSListenerRefEntry *entry;
+
+ if (connection == NULL)
+ return FALSE;
+
+ hash = get_hash_for_entry_type (listener, entry_type);
+
+ removed = FALSE;
+ g_hash_table_iter_init (&iter, hash);
+ while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&entry))
+ {
+ if (entry->connection != NULL &&
+ strcmp (connection, entry->connection) == 0)
+ {
+ gs_debug ("removing %s from %s for reason '%s' on connection %s",
+ get_name_for_entry_type (entry->entry_type),
+ entry->application,
+ entry->reason,
+ entry->connection);
+
+ if (entry->entry_type == REF_ENTRY_TYPE_INHIBIT)
+ {
+ /* remove inhibit from mate session */
+ remove_session_inhibit (listener, entry);
+ }
+
+ g_hash_table_iter_remove (&iter);
+ removed = TRUE;
+ }
+ }
+
+ return removed;
+}
+
+static void
+listener_service_deleted (GSListener *listener,
+ DBusMessage *message)
+{
+ const char *old_service_name;
+ const char *new_service_name;
+ gboolean removed;
+
+ if (! dbus_message_get_args (message, NULL,
+ DBUS_TYPE_STRING, &old_service_name,
+ DBUS_TYPE_STRING, &new_service_name,
+ DBUS_TYPE_INVALID))
+ {
+ g_error ("Invalid NameOwnerChanged signal from bus!");
+ return;
+ }
+
+ gs_debug ("DBUS service deleted: %s", new_service_name);
+
+ removed = listener_ref_entry_remove_for_connection (listener, REF_ENTRY_TYPE_THROTTLE, new_service_name);
+ if (removed)
+ {
+ listener_ref_entry_check (listener, REF_ENTRY_TYPE_THROTTLE);
+ }
+
+ removed = listener_ref_entry_remove_for_connection (listener, REF_ENTRY_TYPE_INHIBIT, new_service_name);
+ if (removed)
+ {
+ listener_ref_entry_check (listener, REF_ENTRY_TYPE_INHIBIT);
+ }
+
+}
+
+static void
+raise_property_type_error (DBusConnection *connection,
+ DBusMessage *in_reply_to,
+ const char *device_id)
+{
+ char buf [512];
+ DBusMessage *reply;
+
+ snprintf (buf, 511,
+ "Type mismatch setting property with id %s",
+ device_id);
+ gs_debug (buf);
+
+ reply = dbus_message_new_error (in_reply_to,
+ TYPE_MISMATCH_ERROR,
+ buf);
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+}
+
+static DBusHandlerResult
+listener_set_property (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint prop_id)
+{
+ const char *path;
+ int type;
+ gboolean rc;
+ DBusMessageIter iter;
+ DBusMessage *reply;
+
+ path = dbus_message_get_path (message);
+
+ dbus_message_iter_init (message, &iter);
+ type = dbus_message_iter_get_arg_type (&iter);
+ rc = FALSE;
+
+ switch (type)
+ {
+ case DBUS_TYPE_BOOLEAN:
+ {
+ dbus_bool_t v;
+ dbus_message_iter_get_basic (&iter, &v);
+ rc = listener_property_set_bool (listener, prop_id, v);
+ break;
+ }
+ default:
+ gs_debug ("Unsupported property type %d", type);
+ break;
+ }
+
+ if (! rc)
+ {
+ raise_property_type_error (connection, message, path);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ reply = dbus_message_new_method_return (message);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_get_property (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message,
+ guint prop_id)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ g_error ("No memory");
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ {
+ dbus_bool_t b;
+ b = listener->priv->active;
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &b);
+ }
+ break;
+ default:
+ gs_debug ("Unsupported property id %u", prop_id);
+ break;
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_get_active_time (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+ dbus_uint32_t secs;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (listener->priv->active)
+ {
+ time_t now = time (NULL);
+
+ if (now < listener->priv->active_start)
+ {
+ /* shouldn't happen */
+ gs_debug ("Active start time is in the future");
+ secs = 0;
+ }
+ else if (listener->priv->active_start <= 0)
+ {
+ /* shouldn't happen */
+ gs_debug ("Active start time was not set");
+ secs = 0;
+ }
+ else
+ {
+ secs = now - listener->priv->active_start;
+ }
+ }
+ else
+ {
+ secs = 0;
+ }
+
+ gs_debug ("Returning screensaver active for %u seconds", secs);
+ dbus_message_iter_append_basic (&iter, DBUS_TYPE_UINT32, &secs);
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_show_message (GSListener *listener,
+ DBusConnection *connection,
+ DBusMessage *message)
+{
+ DBusMessageIter iter;
+ DBusMessage *reply;
+ DBusError error;
+
+ reply = dbus_message_new_method_return (message);
+
+ dbus_message_iter_init_append (reply, &iter);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (listener->priv->active)
+ {
+ char *summary;
+ char *body;
+ char *icon;
+
+ /* if we're not active we ignore the request */
+
+ dbus_error_init (&error);
+ if (! dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &summary,
+ DBUS_TYPE_STRING, &body,
+ DBUS_TYPE_STRING, &icon,
+ DBUS_TYPE_INVALID))
+ {
+ raise_syntax (connection, message, "ShowMessage");
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ g_signal_emit (listener, signals [SHOW_MESSAGE], 0, summary, body, icon);
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+do_introspect (DBusConnection *connection,
+ DBusMessage *message,
+ dbus_bool_t local_interface)
+{
+ DBusMessage *reply;
+ GString *xml;
+ char *xml_string;
+
+ /* standard header */
+ xml = g_string_new ("<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
+ "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
+ "<node>\n"
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
+ " <method name=\"Introspect\">\n"
+ " <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
+ " </method>\n"
+ " </interface>\n");
+
+ /* ScreenSaver interface */
+ xml = g_string_append (xml,
+ " <interface name=\"org.mate.ScreenSaver\">\n"
+ " <method name=\"Lock\">\n"
+ " </method>\n"
+ " <method name=\"Cycle\">\n"
+ " </method>\n"
+ " <method name=\"SimulateUserActivity\">\n"
+ " </method>\n"
+ " <method name=\"Inhibit\">\n"
+ " <arg name=\"application_name\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"reason\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"cookie\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"UnInhibit\">\n"
+ " <arg name=\"cookie\" direction=\"in\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"GetInhibitors\">\n"
+ " <arg name=\"list\" direction=\"out\" type=\"as\"/>\n"
+ " </method>\n"
+ " <method name=\"Throttle\">\n"
+ " <arg name=\"application_name\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"reason\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"cookie\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"UnThrottle\">\n"
+ " <arg name=\"cookie\" direction=\"in\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"GetActive\">\n"
+ " <arg name=\"value\" direction=\"out\" type=\"b\"/>\n"
+ " </method>\n"
+ " <method name=\"GetActiveTime\">\n"
+ " <arg name=\"seconds\" direction=\"out\" type=\"u\"/>\n"
+ " </method>\n"
+ " <method name=\"SetActive\">\n"
+ " <arg name=\"value\" direction=\"in\" type=\"b\"/>\n"
+ " </method>\n"
+ " <method name=\"ShowMessage\">\n"
+ " <arg name=\"summary\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"body\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"icon\" direction=\"in\" type=\"s\"/>\n"
+ " </method>\n"
+ " <signal name=\"ActiveChanged\">\n"
+ " <arg name=\"new_value\" type=\"b\"/>\n"
+ " </signal>\n"
+ " </interface>\n");
+
+ reply = dbus_message_new_method_return (message);
+
+ xml = g_string_append (xml, "</node>\n");
+ xml_string = g_string_free (xml, FALSE);
+
+ dbus_message_append_args (reply,
+ DBUS_TYPE_STRING, &xml_string,
+ DBUS_TYPE_INVALID);
+
+ g_free (xml_string);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_handle_session_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data,
+ dbus_bool_t local_interface)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+
+#if 0
+ g_message ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Lock"))
+ {
+ g_signal_emit (listener, signals [LOCK], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Quit"))
+ {
+ g_signal_emit (listener, signals [QUIT], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Cycle"))
+ {
+ g_signal_emit (listener, signals [CYCLE], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Inhibit"))
+ {
+ return listener_dbus_add_ref_entry (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "UnInhibit"))
+ {
+ return listener_dbus_remove_ref_entry (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetInhibitors"))
+ {
+ return listener_dbus_get_ref_entries (listener, REF_ENTRY_TYPE_INHIBIT, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "Throttle"))
+ {
+ return listener_dbus_add_ref_entry (listener, REF_ENTRY_TYPE_THROTTLE, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "UnThrottle"))
+ {
+ return listener_dbus_remove_ref_entry (listener, REF_ENTRY_TYPE_THROTTLE, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "SetActive"))
+ {
+ return listener_set_property (listener, connection, message, PROP_ACTIVE);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetActive"))
+ {
+ return listener_get_property (listener, connection, message, PROP_ACTIVE);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "GetActiveTime"))
+ {
+ return listener_get_active_time (listener, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "ShowMessage"))
+ {
+ return listener_show_message (listener, connection, message);
+ }
+ if (dbus_message_is_method_call (message, GS_LISTENER_SERVICE, "SimulateUserActivity"))
+ {
+ g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0);
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ if (dbus_message_is_method_call (message, "org.freedesktop.DBus.Introspectable", "Introspect"))
+ {
+ return do_introspect (connection, message, local_interface);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static gboolean
+_listener_message_path_is_our_session (GSListener *listener,
+ DBusMessage *message)
+{
+ const char *ssid;
+ gboolean ours;
+
+ ours = FALSE;
+
+ ssid = dbus_message_get_path (message);
+ if (ssid != NULL
+ && listener->priv->session_id != NULL
+ && strcmp (ssid, listener->priv->session_id) == 0)
+ {
+ ours = TRUE;
+ }
+
+ return ours;
+}
+
+static DBusHandlerResult
+listener_dbus_handle_system_message (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data,
+ dbus_bool_t local_interface)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+#if 1
+ gs_debug ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ if (dbus_message_is_signal (message, HAL_DEVICE_INTERFACE, "Condition"))
+ {
+ DBusError error;
+ const char *event;
+ const char *keyname;
+
+ dbus_error_init (&error);
+ if (dbus_message_get_args (message, &error,
+ DBUS_TYPE_STRING, &event,
+ DBUS_TYPE_STRING, &keyname,
+ DBUS_TYPE_INVALID))
+ {
+ if ((event && strcmp (event, "ButtonPressed") == 0) &&
+ (keyname && strcmp (keyname, "coffee") == 0))
+ {
+ gs_debug ("Coffee key was pressed - locking");
+ g_signal_emit (listener, signals [LOCK], 0);
+ }
+ }
+
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Unlock"))
+ {
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ gs_debug ("Console kit requested session unlock");
+ gs_listener_set_active (listener, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "Lock"))
+ {
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ gs_debug ("ConsoleKit requested session lock");
+ g_signal_emit (listener, signals [LOCK], 0);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, CK_SESSION_INTERFACE, "ActiveChanged"))
+ {
+ /* NB that `ActiveChanged' refers to the active
+ * session in ConsoleKit terminology - ie which
+ * session is currently displayed on the screen.
+ * mate-screensaver uses `active' to mean `is the
+ * screensaver active' (ie, is the screen locked) but
+ * that's not what we're referring to here.
+ */
+
+ if (_listener_message_path_is_our_session (listener, message))
+ {
+ DBusError error;
+ dbus_bool_t new_active;
+
+ dbus_error_init (&error);
+ if (dbus_message_get_args (message, &error,
+ DBUS_TYPE_BOOLEAN, &new_active,
+ DBUS_TYPE_INVALID))
+ {
+ gs_debug ("ConsoleKit notified ActiveChanged %d", new_active);
+
+ /* when we aren't active add an implicit throttle from CK
+ * when we become active remove the throttle and poke the lock */
+ if (new_active)
+ {
+ if (listener->priv->ck_throttle_cookie != 0)
+ {
+ listener_remove_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ listener->priv->ck_throttle_cookie);
+ listener->priv->ck_throttle_cookie = 0;
+ }
+
+ g_signal_emit (listener, signals [SIMULATE_USER_ACTIVITY], 0);
+ }
+ else
+ {
+ if (listener->priv->ck_throttle_cookie != 0)
+ {
+ g_warning ("ConsoleKit throttle already set");
+ listener_remove_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ listener->priv->ck_throttle_cookie);
+ listener->priv->ck_throttle_cookie = 0;
+ }
+
+ listener_add_ck_ref_entry (listener,
+ REF_ENTRY_TYPE_THROTTLE,
+ connection,
+ message,
+ &listener->priv->ck_throttle_cookie);
+ }
+ }
+
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+}
+
+static DBusHandlerResult
+gs_listener_message_handler (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ g_return_val_if_fail (connection != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+ g_return_val_if_fail (message != NULL, DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
+
+#if 0
+ g_message ("obj_path=%s interface=%s method=%s destination=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message),
+ dbus_message_get_destination (message));
+#endif
+
+ if (dbus_message_is_method_call (message, "org.freedesktop.DBus", "AddMatch"))
+ {
+ DBusMessage *reply;
+
+ reply = dbus_message_new_method_return (message);
+
+ if (reply == NULL)
+ {
+ g_error ("No memory");
+ }
+
+ if (! dbus_connection_send (connection, reply, NULL))
+ {
+ g_error ("No memory");
+ }
+
+ dbus_message_unref (reply);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected") &&
+ strcmp (dbus_message_get_path (message), DBUS_PATH_LOCAL) == 0)
+ {
+ dbus_connection_unref (connection);
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+ }
+ else
+ {
+ return listener_dbus_handle_session_message (connection, message, user_data, TRUE);
+ }
+}
+
+static gboolean
+gs_listener_dbus_init (GSListener *listener)
+{
+ DBusError error;
+
+ dbus_error_init (&error);
+
+ if (listener->priv->connection == NULL)
+ {
+ listener->priv->connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
+ if (listener->priv->connection == NULL)
+ {
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("couldn't connect to session bus: %s",
+ error.message);
+ dbus_error_free (&error);
+ }
+ return FALSE;
+ }
+
+ dbus_connection_setup_with_g_main (listener->priv->connection, NULL);
+ dbus_connection_set_exit_on_disconnect (listener->priv->connection, FALSE);
+ }
+
+ if (listener->priv->system_connection == NULL)
+ {
+ listener->priv->system_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
+ if (listener->priv->system_connection == NULL)
+ {
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("couldn't connect to system bus: %s",
+ error.message);
+ dbus_error_free (&error);
+ }
+ return FALSE;
+ }
+
+ dbus_connection_setup_with_g_main (listener->priv->system_connection, NULL);
+ dbus_connection_set_exit_on_disconnect (listener->priv->system_connection, FALSE);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+reinit_dbus (GSListener *listener)
+{
+ gboolean initialized;
+ gboolean try_again;
+
+ initialized = gs_listener_dbus_init (listener);
+
+ /* if we didn't initialize then try again */
+ /* FIXME: Should we keep trying forever? If we fail more than
+ once or twice then the session bus may have died. The
+ problem is that if it is restarted it will likely have a
+ different bus address and we won't be able to find it */
+ try_again = !initialized;
+
+ return try_again;
+}
+
+static DBusHandlerResult
+listener_dbus_filter_function (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+ const char *path;
+
+ path = dbus_message_get_path (message);
+
+ /*
+ g_message ("obj_path=%s interface=%s method=%s",
+ dbus_message_get_path (message),
+ dbus_message_get_interface (message),
+ dbus_message_get_member (message));
+ */
+
+ if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")
+ && strcmp (path, DBUS_PATH_LOCAL) == 0)
+ {
+
+ g_message ("Got disconnected from the session message bus; "
+ "retrying to reconnect every 10 seconds");
+
+ dbus_connection_unref (connection);
+ listener->priv->connection = NULL;
+
+ g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener);
+ }
+ else if (dbus_message_is_signal (message,
+ DBUS_INTERFACE_DBUS,
+ "NameOwnerChanged"))
+ {
+
+ if (listener->priv->inhibitors != NULL)
+ {
+ listener_service_deleted (listener, message);
+ }
+ }
+ else
+ {
+ return listener_dbus_handle_session_message (connection, message, user_data, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static DBusHandlerResult
+listener_dbus_system_filter_function (DBusConnection *connection,
+ DBusMessage *message,
+ void *user_data)
+{
+ GSListener *listener = GS_LISTENER (user_data);
+ const char *path;
+
+ path = dbus_message_get_path (message);
+
+ if (dbus_message_is_signal (message, DBUS_INTERFACE_LOCAL, "Disconnected")
+ && strcmp (path, DBUS_PATH_LOCAL) == 0)
+ {
+
+ g_message ("Got disconnected from the system message bus; "
+ "retrying to reconnect every 10 seconds");
+
+ dbus_connection_unref (connection);
+ listener->priv->system_connection = NULL;
+
+ g_timeout_add (10000, (GSourceFunc)reinit_dbus, listener);
+ }
+ else
+ {
+ return listener_dbus_handle_system_message (connection, message, user_data, FALSE);
+ }
+
+ return DBUS_HANDLER_RESULT_HANDLED;
+}
+
+static void
+gs_listener_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSListener *self;
+
+ self = GS_LISTENER (object);
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ gs_listener_set_active (self, g_value_get_boolean (value));
+ break;
+ case PROP_SESSION_IDLE:
+ gs_listener_set_session_idle (self, g_value_get_boolean (value));
+ break;
+ case PROP_ACTIVATION_ENABLED:
+ gs_listener_set_activation_enabled (self, g_value_get_boolean (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_listener_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSListener *self;
+
+ self = GS_LISTENER (object);
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ g_value_set_boolean (value, self->priv->active);
+ break;
+ case PROP_SESSION_IDLE:
+ g_value_set_boolean (value, self->priv->session_idle);
+ break;
+ case PROP_ACTIVATION_ENABLED:
+ g_value_set_boolean (value, self->priv->activation_enabled);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_listener_class_init (GSListenerClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_listener_finalize;
+ object_class->get_property = gs_listener_get_property;
+ object_class->set_property = gs_listener_set_property;
+
+ signals [LOCK] =
+ g_signal_new ("lock",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, lock),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [QUIT] =
+ g_signal_new ("quit",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, quit),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [CYCLE] =
+ g_signal_new ("cycle",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, cycle),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [SIMULATE_USER_ACTIVITY] =
+ g_signal_new ("simulate-user-activity",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, simulate_user_activity),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [ACTIVE_CHANGED] =
+ g_signal_new ("active-changed",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, active_changed),
+ NULL,
+ NULL,
+ gs_marshal_BOOLEAN__BOOLEAN,
+ G_TYPE_BOOLEAN,
+ 1,
+ G_TYPE_BOOLEAN);
+ signals [THROTTLE_CHANGED] =
+ g_signal_new ("throttle-changed",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, throttle_changed),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__BOOLEAN,
+ G_TYPE_NONE,
+ 1,
+ G_TYPE_BOOLEAN);
+ signals [SHOW_MESSAGE] =
+ g_signal_new ("show-message",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSListenerClass, show_message),
+ NULL,
+ NULL,
+ gs_marshal_VOID__STRING_STRING_STRING,
+ G_TYPE_NONE,
+ 3,
+ G_TYPE_STRING,
+ G_TYPE_STRING,
+ G_TYPE_STRING);
+
+ g_object_class_install_property (object_class,
+ PROP_ACTIVE,
+ g_param_spec_boolean ("active",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_ACTIVATION_ENABLED,
+ g_param_spec_boolean ("activation-enabled",
+ NULL,
+ NULL,
+ TRUE,
+ G_PARAM_READWRITE));
+
+ g_type_class_add_private (klass, sizeof (GSListenerPrivate));
+}
+
+
+static gboolean
+screensaver_is_running (DBusConnection *connection)
+{
+ DBusError error;
+ gboolean exists;
+
+ g_return_val_if_fail (connection != NULL, FALSE);
+
+ dbus_error_init (&error);
+ exists = dbus_bus_name_has_owner (connection, GS_LISTENER_SERVICE, &error);
+ if (dbus_error_is_set (&error))
+ {
+ dbus_error_free (&error);
+ }
+
+ return exists;
+}
+
+gboolean
+gs_listener_acquire (GSListener *listener,
+ GError **error)
+{
+ gboolean acquired;
+ DBusError buserror;
+ gboolean is_connected;
+
+ g_return_val_if_fail (listener != NULL, FALSE);
+
+ if (! listener->priv->connection)
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("failed to register with the message bus"));
+ return FALSE;
+ }
+
+ is_connected = dbus_connection_get_is_connected (listener->priv->connection);
+ if (! is_connected)
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("not connected to the message bus"));
+ return FALSE;
+ }
+
+ if (screensaver_is_running (listener->priv->connection))
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ _("screensaver already running in this session"));
+ return FALSE;
+ }
+
+ dbus_error_init (&buserror);
+
+ if (dbus_connection_register_object_path (listener->priv->connection,
+ GS_LISTENER_PATH,
+ &gs_listener_vtable,
+ listener) == FALSE)
+ {
+ g_critical ("out of memory registering object path");
+ return FALSE;
+ }
+
+ acquired = dbus_bus_request_name (listener->priv->connection,
+ GS_LISTENER_SERVICE,
+ 0, &buserror) != -1;
+ if (dbus_error_is_set (&buserror))
+ {
+ g_set_error (error,
+ GS_LISTENER_ERROR,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ "%s",
+ buserror.message);
+ }
+
+ dbus_error_free (&buserror);
+
+ dbus_connection_add_filter (listener->priv->connection, listener_dbus_filter_function, listener, NULL);
+
+ dbus_bus_add_match (listener->priv->connection,
+ "type='signal'"
+ ",interface='"DBUS_INTERFACE_DBUS"'"
+ ",sender='"DBUS_SERVICE_DBUS"'"
+ ",member='NameOwnerChanged'",
+ NULL);
+
+ if (listener->priv->system_connection != NULL)
+ {
+ dbus_connection_add_filter (listener->priv->system_connection,
+ listener_dbus_system_filter_function,
+ listener,
+ NULL);
+
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"HAL_DEVICE_INTERFACE"'"
+ ",member='Condition'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='Unlock'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='Lock'",
+ NULL);
+ dbus_bus_add_match (listener->priv->system_connection,
+ "type='signal'"
+ ",interface='"CK_SESSION_INTERFACE"'"
+ ",member='ActiveChanged'",
+ NULL);
+ }
+
+ return acquired;
+}
+
+static char *
+query_session_id (GSListener *listener)
+{
+ DBusMessage *message;
+ DBusMessage *reply;
+ DBusError error;
+ DBusMessageIter reply_iter;
+ char *ssid;
+
+ if (listener->priv->system_connection == NULL)
+ {
+ gs_debug ("No connection to the system bus");
+ return NULL;
+ }
+
+ ssid = NULL;
+
+ dbus_error_init (&error);
+
+ message = dbus_message_new_method_call (CK_NAME, CK_MANAGER_PATH, CK_MANAGER_INTERFACE, "GetCurrentSession");
+ if (message == NULL)
+ {
+ gs_debug ("Couldn't allocate the dbus message");
+ return NULL;
+ }
+
+ /* FIXME: use async? */
+ reply = dbus_connection_send_with_reply_and_block (listener->priv->system_connection,
+ message,
+ -1, &error);
+ dbus_message_unref (message);
+
+ if (dbus_error_is_set (&error))
+ {
+ gs_debug ("%s raised:\n %s\n\n", error.name, error.message);
+ dbus_error_free (&error);
+ return NULL;
+ }
+
+ dbus_message_iter_init (reply, &reply_iter);
+ dbus_message_iter_get_basic (&reply_iter, &ssid);
+
+ dbus_message_unref (reply);
+
+ return g_strdup (ssid);
+}
+
+static void
+init_session_id (GSListener *listener)
+{
+ g_free (listener->priv->session_id);
+ listener->priv->session_id = query_session_id (listener);
+ gs_debug ("Got session-id: %s", listener->priv->session_id);
+}
+
+static void
+gs_listener_init (GSListener *listener)
+{
+ listener->priv = GS_LISTENER_GET_PRIVATE (listener);
+
+ gs_listener_dbus_init (listener);
+
+ init_session_id (listener);
+
+ listener->priv->inhibitors = g_hash_table_new_full (g_int_hash,
+ g_int_equal,
+ NULL,
+ (GDestroyNotify)gs_listener_ref_entry_free);
+ listener->priv->throttlers = g_hash_table_new_full (g_int_hash,
+ g_int_equal,
+ NULL,
+ (GDestroyNotify)gs_listener_ref_entry_free);
+}
+
+static void
+gs_listener_finalize (GObject *object)
+{
+ GSListener *listener;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_LISTENER (object));
+
+ listener = GS_LISTENER (object);
+
+ g_return_if_fail (listener->priv != NULL);
+
+ if (listener->priv->inhibitors)
+ {
+ g_hash_table_destroy (listener->priv->inhibitors);
+ }
+
+ if (listener->priv->throttlers)
+ {
+ g_hash_table_destroy (listener->priv->throttlers);
+ }
+
+ g_free (listener->priv->session_id);
+
+ G_OBJECT_CLASS (gs_listener_parent_class)->finalize (object);
+}
+
+GSListener *
+gs_listener_new (void)
+{
+ GSListener *listener;
+
+ listener = g_object_new (GS_TYPE_LISTENER, NULL);
+
+ return GS_LISTENER (listener);
+}
diff --git a/src/gs-listener-dbus.h b/src/gs-listener-dbus.h
new file mode 100644
index 0000000..a2e298a
--- /dev/null
+++ b/src/gs-listener-dbus.h
@@ -0,0 +1,89 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_LISTENER_H
+#define __GS_LISTENER_H
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_LISTENER (gs_listener_get_type ())
+#define GS_LISTENER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_LISTENER, GSListener))
+#define GS_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_LISTENER, GSListenerClass))
+#define GS_IS_LISTENER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_LISTENER))
+#define GS_IS_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_LISTENER))
+#define GS_LISTENER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_LISTENER, GSListenerClass))
+
+typedef struct GSListenerPrivate GSListenerPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSListenerPrivate *priv;
+} GSListener;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+ void (* lock) (GSListener *listener);
+ void (* cycle) (GSListener *listener);
+ void (* quit) (GSListener *listener);
+ void (* simulate_user_activity) (GSListener *listener);
+ gboolean (* active_changed) (GSListener *listener,
+ gboolean active);
+ void (* throttle_changed) (GSListener *listener,
+ gboolean throttled);
+ void (* show_message) (GSListener *listener,
+ const char *summary,
+ const char *body,
+ const char *icon);
+
+} GSListenerClass;
+
+typedef enum
+{
+ GS_LISTENER_ERROR_SERVICE_UNAVAILABLE,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ GS_LISTENER_ERROR_ACTIVATION_FAILURE
+} GSListenerError;
+
+#define GS_LISTENER_ERROR gs_listener_error_quark ()
+
+GQuark gs_listener_error_quark (void);
+
+GType gs_listener_get_type (void);
+
+GSListener *gs_listener_new (void);
+gboolean gs_listener_acquire (GSListener *listener,
+ GError **error);
+gboolean gs_listener_set_active (GSListener *listener,
+ gboolean active);
+gboolean gs_listener_set_session_idle (GSListener *listener,
+ gboolean idle);
+void gs_listener_set_activation_enabled (GSListener *listener,
+ gboolean enabled);
+gboolean gs_listener_get_activation_enabled (GSListener *listener);
+gboolean gs_listener_is_inhibited (GSListener *listener);
+
+G_END_DECLS
+
+#endif /* __GS_LISTENER_H */
diff --git a/src/gs-listener-dbus.h.orig b/src/gs-listener-dbus.h.orig
new file mode 100644
index 0000000..40a7e99
--- /dev/null
+++ b/src/gs-listener-dbus.h.orig
@@ -0,0 +1,89 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_LISTENER_H
+#define __GS_LISTENER_H
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_LISTENER (gs_listener_get_type ())
+#define GS_LISTENER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_LISTENER, GSListener))
+#define GS_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_LISTENER, GSListenerClass))
+#define GS_IS_LISTENER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_LISTENER))
+#define GS_IS_LISTENER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_LISTENER))
+#define GS_LISTENER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_LISTENER, GSListenerClass))
+
+typedef struct GSListenerPrivate GSListenerPrivate;
+
+typedef struct
+{
+ GObject parent;
+ GSListenerPrivate *priv;
+} GSListener;
+
+typedef struct
+{
+ GObjectClass parent_class;
+
+ void (* lock) (GSListener *listener);
+ void (* cycle) (GSListener *listener);
+ void (* quit) (GSListener *listener);
+ void (* simulate_user_activity) (GSListener *listener);
+ gboolean (* active_changed) (GSListener *listener,
+ gboolean active);
+ void (* throttle_changed) (GSListener *listener,
+ gboolean throttled);
+ void (* show_message) (GSListener *listener,
+ const char *summary,
+ const char *body,
+ const char *icon);
+
+} GSListenerClass;
+
+typedef enum
+{
+ GS_LISTENER_ERROR_SERVICE_UNAVAILABLE,
+ GS_LISTENER_ERROR_ACQUISITION_FAILURE,
+ GS_LISTENER_ERROR_ACTIVATION_FAILURE
+} GSListenerError;
+
+#define GS_LISTENER_ERROR gs_listener_error_quark ()
+
+GQuark gs_listener_error_quark (void);
+
+GType gs_listener_get_type (void);
+
+GSListener *gs_listener_new (void);
+gboolean gs_listener_acquire (GSListener *listener,
+ GError **error);
+gboolean gs_listener_set_active (GSListener *listener,
+ gboolean active);
+gboolean gs_listener_set_session_idle (GSListener *listener,
+ gboolean idle);
+void gs_listener_set_activation_enabled (GSListener *listener,
+ gboolean enabled);
+gboolean gs_listener_get_activation_enabled (GSListener *listener);
+gboolean gs_listener_is_inhibited (GSListener *listener);
+
+G_END_DECLS
+
+#endif /* __GS_LISTENER_H */
diff --git a/src/gs-lock-plug.c b/src/gs-lock-plug.c
new file mode 100644
index 0000000..81bea13
--- /dev/null
+++ b/src/gs-lock-plug.c
@@ -0,0 +1,2050 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/utsname.h>
+
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
+#include <gdk/gdkx.h>
+#include <X11/XKBlib.h>
+#include <gtk/gtk.h>
+#include <mateconf/mateconf-client.h>
+
+#ifdef WITH_KBD_LAYOUT_INDICATOR
+#include <libmatekbd/matekbd-indicator.h>
+#endif
+
+#ifdef WITH_LIBMATENOTIFY
+#include <libmatenotify/notify.h>
+#endif
+
+#include "gs-lock-plug.h"
+
+#include "gs-debug.h"
+
+#define KEY_LOCK_DIALOG_THEME "/apps/mate-screensaver/lock_dialog_theme"
+#define MDM_FLEXISERVER_COMMAND "mdmflexiserver"
+#define MDM_FLEXISERVER_ARGS "--startnew Standard"
+
+/* same as SMS ;) */
+#define NOTE_BUFFER_MAX_CHARS 160
+
+enum
+{
+ AUTH_PAGE = 0,
+};
+
+#define FACE_ICON_SIZE 48
+#define DIALOG_TIMEOUT_MSEC 60000
+
+static void gs_lock_plug_finalize (GObject *object);
+
+#define GS_LOCK_PLUG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_LOCK_PLUG, GSLockPlugPrivate))
+
+struct GSLockPlugPrivate
+{
+ GtkWidget *vbox;
+ GtkWidget *auth_action_area;
+
+ GtkWidget *notebook;
+ GtkWidget *auth_face_image;
+ GtkWidget *auth_realname_label;
+ GtkWidget *auth_username_label;
+ GtkWidget *auth_prompt_label;
+ GtkWidget *auth_prompt_entry;
+ GtkWidget *auth_prompt_box;
+ GtkWidget *auth_capslock_label;
+ GtkWidget *auth_message_label;
+ GtkWidget *status_message_label;
+
+ GtkWidget *auth_unlock_button;
+ GtkWidget *auth_switch_button;
+ GtkWidget *auth_cancel_button;
+ GtkWidget *auth_logout_button;
+ GtkWidget *auth_note_button;
+ GtkWidget *note_tab;
+ GtkWidget *note_tab_label;
+ GtkWidget *note_text_view;
+ GtkWidget *note_ok_button;
+ GtkWidget *note_cancel_button;
+
+ GtkWidget *auth_prompt_kbd_layout_indicator;
+
+ gboolean caps_lock_on;
+ gboolean switch_enabled;
+ gboolean leave_note_enabled;
+ gboolean logout_enabled;
+ char *logout_command;
+ char *status_message;
+
+ guint timeout;
+
+ guint cancel_timeout_id;
+ guint auth_check_idle_id;
+ guint response_idle_id;
+
+ GList *key_events;
+};
+
+typedef struct _ResponseData ResponseData;
+
+struct _ResponseData
+{
+ gint response_id;
+};
+
+
+enum
+{
+ RESPONSE,
+ CLOSE,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_LOGOUT_ENABLED,
+ PROP_LOGOUT_COMMAND,
+ PROP_SWITCH_ENABLED,
+ PROP_STATUS_MESSAGE
+};
+
+static guint lock_plug_signals [LAST_SIGNAL];
+
+G_DEFINE_TYPE (GSLockPlug, gs_lock_plug, GTK_TYPE_PLUG)
+
+static void
+gs_lock_plug_style_set (GtkWidget *widget,
+ GtkStyle *previous_style)
+{
+ GSLockPlug *plug;
+
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->style_set)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->style_set (widget, previous_style);
+ }
+
+ plug = GS_LOCK_PLUG (widget);
+
+ if (! plug->priv->vbox)
+ {
+ return;
+ }
+
+ gtk_container_set_border_width (GTK_CONTAINER (plug->priv->vbox), 12);
+ gtk_box_set_spacing (GTK_BOX (plug->priv->vbox), 12);
+
+ gtk_container_set_border_width (GTK_CONTAINER (plug->priv->auth_action_area), 0);
+ gtk_box_set_spacing (GTK_BOX (plug->priv->auth_action_area), 5);
+}
+
+static void
+do_user_switch (GSLockPlug *plug)
+{
+ GError *error;
+ gboolean res;
+ char *command;
+
+ command = g_strdup_printf ("%s %s",
+ MDM_FLEXISERVER_COMMAND,
+ MDM_FLEXISERVER_ARGS);
+
+ error = NULL;
+ res = gdk_spawn_command_line_on_screen (gdk_screen_get_default (),
+ command,
+ &error);
+
+ g_free (command);
+
+ if (! res)
+ {
+ gs_debug ("Unable to start MDM greeter: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+set_status_text (GSLockPlug *plug,
+ const char *text)
+{
+ if (plug->priv->auth_message_label != NULL)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_message_label), text);
+ }
+}
+
+void
+gs_lock_plug_set_sensitive (GSLockPlug *plug,
+ gboolean sensitive)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, sensitive);
+ gtk_widget_set_sensitive (plug->priv->auth_action_area, sensitive);
+}
+
+static void
+remove_cancel_timeout (GSLockPlug *plug)
+{
+ if (plug->priv->cancel_timeout_id > 0)
+ {
+ g_source_remove (plug->priv->cancel_timeout_id);
+ plug->priv->cancel_timeout_id = 0;
+ }
+}
+
+static void
+remove_response_idle (GSLockPlug *plug)
+{
+ if (plug->priv->response_idle_id > 0)
+ {
+ g_source_remove (plug->priv->response_idle_id);
+ plug->priv->response_idle_id = 0;
+ }
+}
+
+static void
+gs_lock_plug_response (GSLockPlug *plug,
+ gint response_id)
+{
+ int new_response;
+
+ new_response = response_id;
+
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ /* Act only on response IDs we recognize */
+ if (!(response_id == GS_LOCK_PLUG_RESPONSE_OK
+ || response_id == GS_LOCK_PLUG_RESPONSE_CANCEL))
+ {
+ return;
+ }
+
+ remove_cancel_timeout (plug);
+ remove_response_idle (plug);
+
+ if (response_id == GS_LOCK_PLUG_RESPONSE_CANCEL)
+ {
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), "");
+ }
+
+ g_signal_emit (plug,
+ lock_plug_signals [RESPONSE],
+ 0,
+ new_response);
+}
+
+static gboolean
+response_cancel_idle_cb (GSLockPlug *plug)
+{
+ plug->priv->response_idle_id = 0;
+
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+
+ return FALSE;
+}
+
+static gboolean
+dialog_timed_out (GSLockPlug *plug)
+{
+ gs_lock_plug_set_sensitive (plug, FALSE);
+ set_status_text (plug, _("Time has expired."));
+
+ if (plug->priv->response_idle_id != 0)
+ {
+ g_warning ("Response idle ID already set but shouldn't be");
+ }
+
+ remove_response_idle (plug);
+
+ plug->priv->response_idle_id = g_timeout_add (2000,
+ (GSourceFunc)response_cancel_idle_cb,
+ plug);
+ return FALSE;
+}
+
+
+static void
+capslock_update (GSLockPlug *plug,
+ gboolean is_on)
+{
+
+ plug->priv->caps_lock_on = is_on;
+
+ if (plug->priv->auth_capslock_label == NULL)
+ {
+ return;
+ }
+
+ if (is_on)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_capslock_label),
+ _("You have the Caps Lock key on."));
+ }
+ else
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_capslock_label),
+ "");
+ }
+}
+
+static gboolean
+is_capslock_on (void)
+{
+ XkbStateRec states;
+ Display *dsp;
+
+ dsp = GDK_DISPLAY ();
+ if (XkbGetState (dsp, XkbUseCoreKbd, &states) != Success)
+ {
+ return FALSE;
+ }
+
+ return (states.locked_mods & LockMask) != 0;
+}
+
+static void
+restart_cancel_timeout (GSLockPlug *plug)
+{
+ remove_cancel_timeout (plug);
+
+ plug->priv->cancel_timeout_id = g_timeout_add (plug->priv->timeout,
+ (GSourceFunc)dialog_timed_out,
+ plug);
+}
+
+void
+gs_lock_plug_get_text (GSLockPlug *plug,
+ char **text)
+{
+ const char *typed_text;
+ char *null_text;
+ char *local_text;
+
+ typed_text = gtk_entry_get_text (GTK_ENTRY (plug->priv->auth_prompt_entry));
+ local_text = g_locale_from_utf8 (typed_text, strlen (typed_text), NULL, NULL, NULL);
+
+ null_text = g_strnfill (strlen (typed_text) + 1, '\b');
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), null_text);
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), "");
+ g_free (null_text);
+
+ if (text != NULL)
+ {
+ *text = local_text;
+ }
+}
+
+typedef struct
+{
+ GSLockPlug *plug;
+ gint response_id;
+ GMainLoop *loop;
+ gboolean destroyed;
+} RunInfo;
+
+static void
+shutdown_loop (RunInfo *ri)
+{
+ if (g_main_loop_is_running (ri->loop))
+ g_main_loop_quit (ri->loop);
+}
+
+static void
+run_unmap_handler (GSLockPlug *plug,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ shutdown_loop (ri);
+}
+
+static void
+run_response_handler (GSLockPlug *plug,
+ gint response_id,
+ gpointer data)
+{
+ RunInfo *ri;
+
+ ri = data;
+
+ ri->response_id = response_id;
+
+ shutdown_loop (ri);
+}
+
+static gint
+run_delete_handler (GSLockPlug *plug,
+ GdkEventAny *event,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ shutdown_loop (ri);
+
+ return TRUE; /* Do not destroy */
+}
+
+static void
+run_destroy_handler (GSLockPlug *plug,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ /* shutdown_loop will be called by run_unmap_handler */
+ ri->destroyed = TRUE;
+}
+
+/* adapted from GTK+ gtkdialog.c */
+int
+gs_lock_plug_run (GSLockPlug *plug)
+{
+ RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
+ gboolean was_modal;
+ gulong response_handler;
+ gulong unmap_handler;
+ gulong destroy_handler;
+ gulong delete_handler;
+
+ g_return_val_if_fail (GS_IS_LOCK_PLUG (plug), -1);
+
+ g_object_ref (plug);
+
+ was_modal = GTK_WINDOW (plug)->modal;
+ if (!was_modal)
+ {
+ gtk_window_set_modal (GTK_WINDOW (plug), TRUE);
+ }
+
+ if (!GTK_WIDGET_VISIBLE (plug))
+ {
+ gtk_widget_show (GTK_WIDGET (plug));
+ }
+
+ response_handler =
+ g_signal_connect (plug,
+ "response",
+ G_CALLBACK (run_response_handler),
+ &ri);
+
+ unmap_handler =
+ g_signal_connect (plug,
+ "unmap",
+ G_CALLBACK (run_unmap_handler),
+ &ri);
+
+ delete_handler =
+ g_signal_connect (plug,
+ "delete_event",
+ G_CALLBACK (run_delete_handler),
+ &ri);
+
+ destroy_handler =
+ g_signal_connect (plug,
+ "destroy",
+ G_CALLBACK (run_destroy_handler),
+ &ri);
+
+ ri.loop = g_main_loop_new (NULL, FALSE);
+
+ GDK_THREADS_LEAVE ();
+ g_main_loop_run (ri.loop);
+ GDK_THREADS_ENTER ();
+
+ g_main_loop_unref (ri.loop);
+
+ ri.loop = NULL;
+
+ if (!ri.destroyed)
+ {
+ if (! was_modal)
+ {
+ gtk_window_set_modal (GTK_WINDOW (plug), FALSE);
+ }
+
+ g_signal_handler_disconnect (plug, response_handler);
+ g_signal_handler_disconnect (plug, unmap_handler);
+ g_signal_handler_disconnect (plug, delete_handler);
+ g_signal_handler_disconnect (plug, destroy_handler);
+ }
+
+ g_object_unref (plug);
+
+ return ri.response_id;
+}
+
+
+static cairo_surface_t *
+surface_from_pixbuf (GdkPixbuf *pixbuf)
+{
+ cairo_surface_t *surface;
+ cairo_t *cr;
+
+ surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
+ CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
+ gdk_pixbuf_get_width (pixbuf),
+ gdk_pixbuf_get_height (pixbuf));
+ cr = cairo_create (surface);
+ gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
+ cairo_paint (cr);
+ cairo_destroy (cr);
+
+ return surface;
+}
+
+static void
+rounded_rectangle (cairo_t *cr,
+ gdouble aspect,
+ gdouble x,
+ gdouble y,
+ gdouble corner_radius,
+ gdouble width,
+ gdouble height)
+{
+ gdouble radius;
+ gdouble degrees;
+
+ radius = corner_radius / aspect;
+ degrees = G_PI / 180.0;
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr,
+ x + width - radius,
+ y + radius,
+ radius,
+ -90 * degrees,
+ 0 * degrees);
+ cairo_arc (cr,
+ x + width - radius,
+ y + height - radius,
+ radius,
+ 0 * degrees,
+ 90 * degrees);
+ cairo_arc (cr,
+ x + radius,
+ y + height - radius,
+ radius,
+ 90 * degrees,
+ 180 * degrees);
+ cairo_arc (cr,
+ x + radius,
+ y + radius,
+ radius,
+ 180 * degrees,
+ 270 * degrees);
+ cairo_close_path (cr);
+}
+
+static void
+image_set_from_pixbuf (GtkImage *image,
+ GdkPixbuf *source)
+{
+ cairo_t *cr;
+ cairo_t *cr_mask;
+ cairo_surface_t *surface;
+ GdkPixmap *pixmap;
+ GdkPixmap *bitmask;
+ int w;
+ int h;
+ int frame_width;
+ double radius;
+ GdkColor color;
+ double r;
+ double g;
+ double b;
+
+ frame_width = 5;
+
+ w = gdk_pixbuf_get_width (source) + frame_width * 2;
+ h = gdk_pixbuf_get_height (source) + frame_width * 2;
+
+ radius = w / 10;
+
+ pixmap = gdk_pixmap_new (GTK_WIDGET (image)->window, w, h, -1);
+ bitmask = gdk_pixmap_new (GTK_WIDGET (image)->window, w, h, 1);
+
+ cr = gdk_cairo_create (pixmap);
+ cr_mask = gdk_cairo_create (bitmask);
+
+ /* setup mask */
+ cairo_rectangle (cr_mask, 0, 0, w, h);
+ cairo_set_operator (cr_mask, CAIRO_OPERATOR_CLEAR);
+ cairo_fill (cr_mask);
+
+ rounded_rectangle (cr_mask, 1.0, 0.5, 0.5, radius, w - 1, h - 1);
+ cairo_set_operator (cr_mask, CAIRO_OPERATOR_OVER);
+ cairo_set_source_rgb (cr_mask, 1, 1, 1);
+ cairo_fill (cr_mask);
+
+ color = GTK_WIDGET (image)->style->bg [GTK_STATE_NORMAL];
+ r = (float)color.red / 65535.0;
+ g = (float)color.green / 65535.0;
+ b = (float)color.blue / 65535.0;
+
+ /* set up image */
+ cairo_rectangle (cr, 0, 0, w, h);
+ cairo_set_source_rgb (cr, r, g, b);
+ cairo_fill (cr);
+
+ rounded_rectangle (cr,
+ 1.0,
+ frame_width + 0.5,
+ frame_width + 0.5,
+ radius,
+ w - frame_width * 2 - 1,
+ h - frame_width * 2 - 1);
+ cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.3);
+ cairo_fill_preserve (cr);
+
+ surface = surface_from_pixbuf (source);
+ cairo_set_source_surface (cr, surface, frame_width, frame_width);
+ cairo_fill (cr);
+
+ gtk_image_set_from_pixmap (image, pixmap, bitmask);
+
+ cairo_surface_destroy (surface);
+
+ g_object_unref (bitmask);
+ g_object_unref (pixmap);
+
+ cairo_destroy (cr_mask);
+ cairo_destroy (cr);
+}
+
+
+static gboolean
+check_user_file (const gchar *filename,
+ uid_t user,
+ gssize max_file_size,
+ gboolean relax_group,
+ gboolean relax_other)
+{
+ struct stat fileinfo;
+
+ if (max_file_size < 0)
+ {
+ max_file_size = G_MAXSIZE;
+ }
+
+ /* Exists/Readable? */
+ if (g_stat (filename, &fileinfo) < 0)
+ {
+ return FALSE;
+ }
+
+ /* Is a regular file */
+ if (G_UNLIKELY (!S_ISREG (fileinfo.st_mode)))
+ {
+ return FALSE;
+ }
+
+ /* Owned by user? */
+ if (G_UNLIKELY (fileinfo.st_uid != user))
+ {
+ return FALSE;
+ }
+
+ /* Group not writable or relax_group? */
+ if (G_UNLIKELY ((fileinfo.st_mode & S_IWGRP) == S_IWGRP && !relax_group))
+ {
+ return FALSE;
+ }
+
+ /* Other not writable or relax_other? */
+ if (G_UNLIKELY ((fileinfo.st_mode & S_IWOTH) == S_IWOTH && !relax_other))
+ {
+ return FALSE;
+ }
+
+ /* Size is kosher? */
+ if (G_UNLIKELY (fileinfo.st_size > max_file_size))
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+set_face_image (GSLockPlug *plug)
+{
+ GdkPixbuf *pixbuf;
+ const char *homedir;
+ char *path;
+ int icon_size = 96;
+ gsize user_max_file = 65536;
+ uid_t uid;
+
+ homedir = g_get_home_dir ();
+ uid = getuid ();
+
+ path = g_build_filename (homedir, ".face", NULL);
+
+ pixbuf = NULL;
+ if (check_user_file (path, uid, user_max_file, 0, 0))
+ {
+ pixbuf = gdk_pixbuf_new_from_file_at_size (path,
+ icon_size,
+ icon_size,
+ NULL);
+ }
+
+ g_free (path);
+
+ if (pixbuf == NULL)
+ {
+ return FALSE;
+ }
+
+ image_set_from_pixbuf (GTK_IMAGE (plug->priv->auth_face_image), pixbuf);
+
+ g_object_unref (pixbuf);
+
+ return TRUE;
+}
+
+static void
+gs_lock_plug_show (GtkWidget *widget)
+{
+ GSLockPlug *plug = GS_LOCK_PLUG (widget);
+
+ gs_profile_start (NULL);
+
+ gs_profile_start ("parent");
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->show)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->show (widget);
+ }
+
+ gs_profile_end ("parent");
+
+
+ if (plug->priv->auth_face_image)
+ {
+ set_face_image (plug);
+ }
+
+ capslock_update (plug, is_capslock_on ());
+
+ restart_cancel_timeout (plug);
+
+ gs_profile_end (NULL);
+}
+
+static void
+gs_lock_plug_hide (GtkWidget *widget)
+{
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->hide)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->hide (widget);
+ }
+}
+
+static void
+queue_key_event (GSLockPlug *plug,
+ GdkEventKey *event)
+{
+ GdkEvent *saved_event;
+
+ saved_event = gdk_event_copy ((GdkEvent *)event);
+ plug->priv->key_events = g_list_prepend (plug->priv->key_events,
+ saved_event);
+}
+
+static void
+forward_key_events (GSLockPlug *plug)
+{
+ plug->priv->key_events = g_list_reverse (plug->priv->key_events);
+ while (plug->priv->key_events != NULL)
+ {
+ GdkEventKey *event = plug->priv->key_events->data;
+
+ gtk_window_propagate_key_event (GTK_WINDOW (plug), event);
+
+ gdk_event_free ((GdkEvent *)event);
+
+ plug->priv->key_events = g_list_delete_link (plug->priv->key_events,
+ plug->priv->key_events);
+ }
+}
+
+static void
+gs_lock_plug_size_request (GtkWidget *widget,
+ GtkRequisition *requisition)
+{
+ GSLockPlug *plug = GS_LOCK_PLUG (widget);
+ int mod_width;
+ int mod_height;
+
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->size_request)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->size_request (widget, requisition);
+ }
+
+ /* don't constrain size when themed */
+ if (plug->priv->vbox == NULL)
+ {
+ return;
+ }
+
+ mod_width = requisition->height * 1.3;
+ mod_height = requisition->width / 1.6;
+ if (requisition->width < mod_width)
+ {
+ /* if the dialog is tall fill out the width */
+ requisition->width = mod_width;
+ }
+ else if (requisition->height < mod_height)
+ {
+ /* if the dialog is wide fill out the height */
+ requisition->height = mod_height;
+ }
+}
+
+static void
+gs_lock_plug_set_logout_enabled (GSLockPlug *plug,
+ gboolean logout_enabled)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ if (plug->priv->logout_enabled == logout_enabled)
+ {
+ return;
+ }
+
+ plug->priv->logout_enabled = logout_enabled;
+ g_object_notify (G_OBJECT (plug), "logout-enabled");
+
+ if (plug->priv->auth_logout_button == NULL)
+ {
+ return;
+ }
+
+ if (logout_enabled)
+ {
+ gtk_widget_show (plug->priv->auth_logout_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_logout_button);
+ }
+}
+
+static void
+gs_lock_plug_set_logout_command (GSLockPlug *plug,
+ const char *command)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ g_free (plug->priv->logout_command);
+
+ if (command)
+ {
+ plug->priv->logout_command = g_strdup (command);
+ }
+ else
+ {
+ plug->priv->logout_command = NULL;
+ }
+}
+
+static gboolean
+is_program_in_path (const char *program)
+{
+ char *tmp = g_find_program_in_path (program);
+ if (tmp != NULL)
+ {
+ g_free (tmp);
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+static void
+gs_lock_plug_set_status_message (GSLockPlug *plug,
+ const char *status_message)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ g_free (plug->priv->status_message);
+ plug->priv->status_message = g_strdup (status_message);
+
+ if (plug->priv->status_message_label)
+ {
+ if (plug->priv->status_message)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->status_message_label),
+ plug->priv->status_message);
+ gtk_widget_show (plug->priv->status_message_label);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->status_message_label);
+ }
+ }
+}
+
+static void
+gs_lock_plug_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSLockPlug *self;
+
+ self = GS_LOCK_PLUG (object);
+
+ switch (prop_id)
+ {
+ case PROP_LOGOUT_ENABLED:
+ g_value_set_boolean (value, self->priv->logout_enabled);
+ break;
+ case PROP_LOGOUT_COMMAND:
+ g_value_set_string (value, self->priv->logout_command);
+ break;
+ case PROP_SWITCH_ENABLED:
+ g_value_set_boolean (value, self->priv->switch_enabled);
+ break;
+ case PROP_STATUS_MESSAGE:
+ g_value_set_string (value, self->priv->status_message);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_lock_plug_set_switch_enabled (GSLockPlug *plug,
+ gboolean switch_enabled)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ if (plug->priv->switch_enabled == switch_enabled)
+ {
+ return;
+ }
+
+ plug->priv->switch_enabled = switch_enabled;
+ g_object_notify (G_OBJECT (plug), "switch-enabled");
+
+ if (plug->priv->auth_switch_button == NULL)
+ {
+ return;
+ }
+
+ if (switch_enabled)
+ {
+ gboolean found;
+ found = is_program_in_path (MDM_FLEXISERVER_COMMAND);
+ if (found)
+ {
+ gtk_widget_show (plug->priv->auth_switch_button);
+ }
+ else
+ {
+ gs_debug ("Waring: MDM flexiserver command not found: %s", MDM_FLEXISERVER_COMMAND);
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+}
+
+static void
+gs_lock_plug_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSLockPlug *self;
+
+ self = GS_LOCK_PLUG (object);
+
+ switch (prop_id)
+ {
+ case PROP_LOGOUT_ENABLED:
+ gs_lock_plug_set_logout_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOGOUT_COMMAND:
+ gs_lock_plug_set_logout_command (self, g_value_get_string (value));
+ break;
+ case PROP_STATUS_MESSAGE:
+ gs_lock_plug_set_status_message (self, g_value_get_string (value));
+ break;
+ case PROP_SWITCH_ENABLED:
+ gs_lock_plug_set_switch_enabled (self, g_value_get_boolean (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_lock_plug_close (GSLockPlug *plug)
+{
+ /* Synthesize delete_event to close dialog. */
+
+ GtkWidget *widget = GTK_WIDGET (plug);
+ GdkEvent *event;
+
+ event = gdk_event_new (GDK_DELETE);
+ event->any.window = g_object_ref (widget->window);
+ event->any.send_event = TRUE;
+
+ gtk_main_do_event (event);
+ gdk_event_free (event);
+}
+
+static void
+gs_lock_plug_class_init (GSLockPlugClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+ GtkBindingSet *binding_set;
+
+ object_class->finalize = gs_lock_plug_finalize;
+ object_class->get_property = gs_lock_plug_get_property;
+ object_class->set_property = gs_lock_plug_set_property;
+
+ widget_class->style_set = gs_lock_plug_style_set;
+ widget_class->show = gs_lock_plug_show;
+ widget_class->hide = gs_lock_plug_hide;
+ widget_class->size_request = gs_lock_plug_size_request;
+
+ klass->close = gs_lock_plug_close;
+
+ g_type_class_add_private (klass, sizeof (GSLockPlugPrivate));
+
+ lock_plug_signals [RESPONSE] = g_signal_new ("response",
+ G_OBJECT_CLASS_TYPE (klass),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSLockPlugClass, response),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__INT,
+ G_TYPE_NONE, 1,
+ G_TYPE_INT);
+ lock_plug_signals [CLOSE] = g_signal_new ("close",
+ G_OBJECT_CLASS_TYPE (klass),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (GSLockPlugClass, close),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_ENABLED,
+ g_param_spec_boolean ("logout-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_COMMAND,
+ g_param_spec_string ("logout-command",
+ NULL,
+ NULL,
+ NULL,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_STATUS_MESSAGE,
+ g_param_spec_string ("status-message",
+ NULL,
+ NULL,
+ NULL,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_SWITCH_ENABLED,
+ g_param_spec_boolean ("switch-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+
+ binding_set = gtk_binding_set_by_class (klass);
+
+ gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
+ "close", 0);
+}
+
+static void
+clear_clipboards (GSLockPlug *plug)
+{
+ GtkClipboard *clipboard;
+
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (plug), GDK_SELECTION_PRIMARY);
+ gtk_clipboard_clear (clipboard);
+ gtk_clipboard_set_text (clipboard, "", -1);
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (plug), GDK_SELECTION_CLIPBOARD);
+ gtk_clipboard_clear (clipboard);
+ gtk_clipboard_set_text (clipboard, "", -1);
+}
+
+static void
+take_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+ int page;
+
+ page = gtk_notebook_page_num (GTK_NOTEBOOK (plug->priv->notebook), plug->priv->note_tab);
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), page);
+ /* this counts as activity so restart the timer */
+ restart_cancel_timeout (plug);
+}
+
+static void
+submit_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+#ifdef WITH_LIBMATENOTIFY
+ char *text;
+ char summary[128];
+ char *escaped_text;
+ GtkTextBuffer *buffer;
+ GtkTextIter start, end;
+ time_t t;
+ struct tm *tmp;
+ NotifyNotification *note;
+
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), AUTH_PAGE);
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ gtk_text_buffer_get_bounds (buffer, &start, &end);
+ text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+ gtk_text_buffer_set_text (buffer, "", 0);
+ escaped_text = g_markup_escape_text (text, -1);
+
+ t = time (NULL);
+ tmp = localtime (&t);
+ strftime (summary, 128, "%X", tmp);
+
+ notify_init ("mate-screensaver-dialog");
+ note = notify_notification_new (summary, escaped_text, NULL, NULL);
+ notify_notification_set_timeout (note, NOTIFY_EXPIRES_NEVER);
+ notify_notification_show (note, NULL);
+ g_object_unref (note);
+
+ g_free (text);
+ g_free (escaped_text);
+
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+#endif /* WITH_LIBMATENOTIFY */
+}
+
+static void
+cancel_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+ GtkTextBuffer *buffer;
+
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), AUTH_PAGE);
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ gtk_text_buffer_set_text (buffer, "", 0);
+
+ /* this counts as activity so restart the timer */
+ restart_cancel_timeout (plug);
+
+ gtk_window_set_default (GTK_WINDOW (plug), plug->priv->auth_unlock_button);
+
+ clear_clipboards (plug);
+}
+
+static void
+logout_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ char **argv = NULL;
+ GError *error = NULL;
+ gboolean res;
+
+ if (! plug->priv->logout_command)
+ {
+ return;
+ }
+
+ res = g_shell_parse_argv (plug->priv->logout_command, NULL, &argv, &error);
+
+ if (! res)
+ {
+ g_warning ("Could not parse logout command: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_spawn_async (g_get_home_dir (),
+ argv,
+ NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL,
+ NULL,
+ NULL,
+ &error);
+
+ g_strfreev (argv);
+
+ if (error)
+ {
+ g_warning ("Could not run logout command: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+void
+gs_lock_plug_set_busy (GSLockPlug *plug)
+{
+ GdkCursor *cursor;
+ GtkWidget *top_level;
+
+ top_level = gtk_widget_get_toplevel (GTK_WIDGET (plug));
+
+ cursor = gdk_cursor_new (GDK_WATCH);
+ gdk_window_set_cursor (top_level->window, cursor);
+ gdk_cursor_unref (cursor);
+}
+
+void
+gs_lock_plug_set_ready (GSLockPlug *plug)
+{
+ GdkCursor *cursor;
+ GtkWidget *top_level;
+
+ top_level = gtk_widget_get_toplevel (GTK_WIDGET (plug));
+
+ cursor = gdk_cursor_new (GDK_LEFT_PTR);
+ gdk_window_set_cursor (top_level->window, cursor);
+ gdk_cursor_unref (cursor);
+}
+
+void
+gs_lock_plug_enable_prompt (GSLockPlug *plug,
+ const char *message,
+ gboolean visible)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ gs_debug ("Setting prompt to: %s", message);
+
+ gtk_widget_set_sensitive (plug->priv->auth_unlock_button, TRUE);
+ gtk_widget_show (plug->priv->auth_unlock_button);
+ gtk_widget_grab_default (plug->priv->auth_unlock_button);
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_prompt_label), message);
+ gtk_widget_show (plug->priv->auth_prompt_label);
+ gtk_entry_set_visibility (GTK_ENTRY (plug->priv->auth_prompt_entry), visible);
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, TRUE);
+ gtk_widget_show (plug->priv->auth_prompt_entry);
+
+ if (! GTK_WIDGET_HAS_FOCUS (plug->priv->auth_prompt_entry))
+ {
+ gtk_widget_grab_focus (plug->priv->auth_prompt_entry);
+ }
+
+ /* were there any key events sent to the plug while the
+ * entry wasnt ready? If so, forward them along
+ */
+ forward_key_events (plug);
+
+ restart_cancel_timeout (plug);
+}
+
+void
+gs_lock_plug_disable_prompt (GSLockPlug *plug)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ /* gtk_widget_hide (plug->priv->auth_prompt_entry); */
+ /* gtk_widget_hide (plug->priv->auth_prompt_label); */
+ gtk_widget_set_sensitive (plug->priv->auth_unlock_button, FALSE);
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, FALSE);
+ /* gtk_widget_hide (plug->priv->auth_unlock_button); */
+
+ gtk_widget_grab_default (plug->priv->auth_cancel_button);
+}
+
+void
+gs_lock_plug_show_message (GSLockPlug *plug,
+ const char *message)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ set_status_text (plug, message ? message : "");
+}
+
+/* button press handler used to inhibit popup menu */
+static gint
+entry_button_press (GtkWidget *widget,
+ GdkEventButton *event)
+{
+ if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
+ {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gint
+entry_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ GSLockPlug *plug)
+{
+ gboolean capslock_on;
+
+ restart_cancel_timeout (plug);
+
+ capslock_on = is_capslock_on ();
+
+ if (capslock_on != plug->priv->caps_lock_on)
+ {
+ capslock_update (plug, capslock_on);
+ }
+
+ /* if the input widget is visible and ready for input
+ * then just carry on as usual
+ */
+ if (GTK_WIDGET_VISIBLE (plug->priv->auth_prompt_entry) &&
+ GTK_WIDGET_IS_SENSITIVE (plug->priv->auth_prompt_entry))
+ {
+ return FALSE;
+ }
+
+ if (strcmp (event->string, "") == 0)
+ {
+ return FALSE;
+ }
+
+ queue_key_event (plug, event);
+
+ return TRUE;
+}
+
+/* adapted from gtk_dialog_add_button */
+static GtkWidget *
+gs_lock_plug_add_button (GSLockPlug *plug,
+ GtkWidget *action_area,
+ const gchar *button_text)
+{
+ GtkWidget *button;
+
+ g_return_val_if_fail (GS_IS_LOCK_PLUG (plug), NULL);
+ g_return_val_if_fail (button_text != NULL, NULL);
+
+ button = gtk_button_new_from_stock (button_text);
+
+ GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
+
+ gtk_widget_show (button);
+
+ gtk_box_pack_end (GTK_BOX (action_area),
+ button,
+ FALSE, TRUE, 0);
+
+ return button;
+}
+
+static char *
+get_user_display_name (void)
+{
+ const char *name;
+ char *utf8_name;
+
+ name = g_get_real_name ();
+
+ if (name == NULL || strcmp (name, "Unknown") == 0)
+ {
+ name = g_get_user_name ();
+ }
+
+ utf8_name = NULL;
+
+ if (name != NULL)
+ {
+ utf8_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
+ }
+
+ return utf8_name;
+}
+
+static char *
+get_user_name (void)
+{
+ const char *name;
+ char *utf8_name;
+
+ name = g_get_user_name ();
+
+ utf8_name = NULL;
+ if (name != NULL)
+ {
+ utf8_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
+ }
+
+ return utf8_name;
+}
+
+static void
+create_page_one_buttons (GSLockPlug *plug)
+{
+
+ gs_profile_start ("page one buttons");
+
+ plug->priv->auth_switch_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("S_witch User..."));
+ gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (plug->priv->auth_action_area),
+ plug->priv->auth_switch_button,
+ TRUE);
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_switch_button), FALSE);
+ gtk_widget_set_no_show_all (plug->priv->auth_switch_button, TRUE);
+
+ plug->priv->auth_logout_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("Log _Out"));
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_logout_button), FALSE);
+ gtk_widget_set_no_show_all (plug->priv->auth_logout_button, TRUE);
+
+ plug->priv->auth_cancel_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ GTK_STOCK_CANCEL);
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_cancel_button), FALSE);
+
+ plug->priv->auth_unlock_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("_Unlock"));
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_unlock_button), FALSE);
+
+ gtk_window_set_default (GTK_WINDOW (plug), plug->priv->auth_unlock_button);
+
+ gs_profile_end ("page one buttons");
+}
+
+/* adapted from MDM */
+static char *
+expand_string (const char *text)
+{
+ GString *str;
+ const char *p;
+ char *username;
+ int i;
+ int n_chars;
+ struct utsname name;
+
+ str = g_string_sized_new (strlen (text));
+
+ p = text;
+ n_chars = g_utf8_strlen (text, -1);
+ i = 0;
+
+ while (i < n_chars)
+ {
+ gunichar ch;
+
+ ch = g_utf8_get_char (p);
+
+ /* Backslash commands */
+ if (ch == '\\')
+ {
+ p = g_utf8_next_char (p);
+ i++;
+ ch = g_utf8_get_char (p);
+
+ if (i >= n_chars || ch == '\0')
+ {
+ g_warning ("Unescaped \\ at end of text\n");
+ goto bail;
+ }
+ else if (ch == 'n')
+ {
+ g_string_append_unichar (str, '\n');
+ }
+ else
+ {
+ g_string_append_unichar (str, ch);
+ }
+ }
+ else if (ch == '%')
+ {
+ p = g_utf8_next_char (p);
+ i++;
+ ch = g_utf8_get_char (p);
+
+ if (i >= n_chars || ch == '\0')
+ {
+ g_warning ("Unescaped %% at end of text\n");
+ goto bail;
+ }
+
+ switch (ch)
+ {
+ case '%':
+ g_string_append (str, "%");
+ break;
+ case 'c':
+ /* clock */
+ break;
+ case 'd':
+ /* display */
+ g_string_append (str, g_getenv ("DISPLAY"));
+ break;
+ case 'h':
+ /* hostname */
+ g_string_append (str, g_get_host_name ());
+ break;
+ case 'm':
+ /* machine name */
+ uname (&name);
+ g_string_append (str, name.machine);
+ break;
+ case 'n':
+ /* nodename */
+ uname (&name);
+ g_string_append (str, name.nodename);
+ break;
+ case 'r':
+ /* release */
+ uname (&name);
+ g_string_append (str, name.release);
+ break;
+ case 'R':
+ /* Real name */
+ username = get_user_display_name ();
+ g_string_append (str, username);
+ g_free (username);
+ break;
+ case 's':
+ /* system name */
+ uname (&name);
+ g_string_append (str, name.sysname);
+ break;
+ case 'U':
+ /* Username */
+ username = get_user_name ();
+ g_string_append (str, username);
+ g_free (username);
+ break;
+ default:
+ if (ch < 127)
+ {
+ g_warning ("unknown escape code %%%c in text\n", (char)ch);
+ }
+ else
+ {
+ g_warning ("unknown escape code %%(U%x) in text\n", (int)ch);
+ }
+ }
+ }
+ else
+ {
+ g_string_append_unichar (str, ch);
+ }
+ p = g_utf8_next_char (p);
+ i++;
+ }
+
+bail:
+
+ return g_string_free (str, FALSE);
+}
+
+static void
+expand_string_for_label (GtkWidget *label)
+{
+ const char *template;
+ char *str;
+
+ template = gtk_label_get_label (GTK_LABEL (label));
+ str = expand_string (template);
+ gtk_label_set_label (GTK_LABEL (label), str);
+ g_free (str);
+}
+
+static void
+create_page_one (GSLockPlug *plug)
+{
+ GtkWidget *align;
+ GtkWidget *vbox;
+ GtkWidget *vbox2;
+ GtkWidget *hbox;
+ char *str;
+
+ gs_profile_start ("page one");
+
+ align = gtk_alignment_new (0.5, 0.5, 1, 1);
+ gtk_notebook_append_page (GTK_NOTEBOOK (plug->priv->notebook), align, NULL);
+
+ vbox = gtk_vbox_new (FALSE, 12);
+ gtk_container_add (GTK_CONTAINER (align), vbox);
+
+ plug->priv->auth_face_image = gtk_image_new ();
+ gtk_box_pack_start (GTK_BOX (vbox), plug->priv->auth_face_image, TRUE, TRUE, 0);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_face_image), 0.5, 1.0);
+
+ vbox2 = gtk_vbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), vbox2, FALSE, FALSE, 0);
+
+ str = g_strdup ("<span size=\"x-large\">%R</span>");
+ plug->priv->auth_realname_label = gtk_label_new (str);
+ g_free (str);
+ expand_string_for_label (plug->priv->auth_realname_label);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_realname_label), 0.5, 0.5);
+ gtk_label_set_use_markup (GTK_LABEL (plug->priv->auth_realname_label), TRUE);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_realname_label, FALSE, FALSE, 0);
+
+ /* To translators: This expands to USERNAME on HOSTNAME */
+ str = g_strdup_printf ("<span size=\"small\">%s</span>", _("%U on %h"));
+ plug->priv->auth_username_label = gtk_label_new (str);
+ g_free (str);
+ expand_string_for_label (plug->priv->auth_username_label);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_username_label), 0.5, 0.5);
+ gtk_label_set_use_markup (GTK_LABEL (plug->priv->auth_username_label), TRUE);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_username_label, FALSE, FALSE, 0);
+
+ vbox2 = gtk_vbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
+
+ hbox = gtk_hbox_new (FALSE, 6);
+ gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
+
+ plug->priv->auth_prompt_label = gtk_label_new_with_mnemonic (_("_Password:"));
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_prompt_label), 0, 0.5);
+ gtk_box_pack_start (GTK_BOX (hbox), plug->priv->auth_prompt_label, FALSE, FALSE, 0);
+
+ plug->priv->auth_prompt_entry = gtk_entry_new ();
+ gtk_box_pack_start (GTK_BOX (hbox), plug->priv->auth_prompt_entry, TRUE, TRUE, 0);
+
+ gtk_label_set_mnemonic_widget (GTK_LABEL (plug->priv->auth_prompt_label),
+ plug->priv->auth_prompt_entry);
+
+ plug->priv->auth_capslock_label = gtk_label_new ("");
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_capslock_label), 0.5, 0.5);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_capslock_label, FALSE, FALSE, 0);
+
+ /* Status text */
+
+ plug->priv->auth_message_label = gtk_label_new (NULL);
+ gtk_box_pack_start (GTK_BOX (vbox), plug->priv->auth_message_label,
+ FALSE, FALSE, 0);
+ /* Buttons */
+ plug->priv->auth_action_area = gtk_hbutton_box_new ();
+
+ gtk_button_box_set_layout (GTK_BUTTON_BOX (plug->priv->auth_action_area),
+ GTK_BUTTONBOX_END);
+
+ gtk_box_pack_end (GTK_BOX (vbox), plug->priv->auth_action_area,
+ FALSE, TRUE, 0);
+ gtk_widget_show (plug->priv->auth_action_area);
+
+ create_page_one_buttons (plug);
+
+ gs_profile_end ("page one");
+}
+
+static void
+unlock_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_OK);
+}
+
+static void
+cancel_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+}
+
+static void
+switch_user_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+
+ remove_response_idle (plug);
+
+ gs_lock_plug_set_sensitive (plug, FALSE);
+
+ plug->priv->response_idle_id = g_timeout_add (2000,
+ (GSourceFunc)response_cancel_idle_cb,
+ plug);
+
+ gs_lock_plug_set_busy (plug);
+ do_user_switch (plug);
+}
+
+static char *
+get_dialog_theme_name (GSLockPlug *plug)
+{
+ char *name;
+ MateConfClient *client;
+
+ client = mateconf_client_get_default ();
+ name = mateconf_client_get_string (client, KEY_LOCK_DIALOG_THEME, NULL);
+ g_object_unref (client);
+
+ return name;
+}
+
+static gboolean
+load_theme (GSLockPlug *plug)
+{
+ char *theme;
+ char *filename;
+ char *gtkbuilder;
+ char *rc;
+ GtkBuilder *builder;
+ GtkWidget *lock_dialog;
+ GError *error=NULL;
+
+ theme = get_dialog_theme_name (plug);
+ if (theme == NULL)
+ {
+ return FALSE;
+ }
+
+ filename = g_strdup_printf ("lock-dialog-%s.ui", theme);
+ gtkbuilder = g_build_filename (GTKBUILDERDIR, filename, NULL);
+ g_free (filename);
+ if (! g_file_test (gtkbuilder, G_FILE_TEST_IS_REGULAR))
+ {
+ g_free (gtkbuilder);
+ g_free (theme);
+ return FALSE;
+ }
+
+ filename = g_strdup_printf ("lock-dialog-%s.gtkrc", theme);
+ g_free (theme);
+
+ rc = g_build_filename (GTKBUILDERDIR, filename, NULL);
+ g_free (filename);
+ if (g_file_test (rc, G_FILE_TEST_IS_REGULAR))
+ {
+ gtk_rc_parse (rc);
+ }
+ g_free (rc);
+
+ builder = gtk_builder_new();
+
+ if (!gtk_builder_add_from_file (builder,gtkbuilder,&error))
+ {
+ g_warning ("Couldn't load builder file '%s': %s", gtkbuilder, error->message);
+ g_error_free(error);
+ g_free (gtkbuilder);
+ return FALSE;
+ }
+ g_free (gtkbuilder);
+
+ lock_dialog = GTK_WIDGET (gtk_builder_get_object(builder, "lock-dialog"));
+ gtk_container_add (GTK_CONTAINER (plug), lock_dialog);
+
+ plug->priv->vbox = NULL;
+ plug->priv->notebook = GTK_WIDGET (gtk_builder_get_object(builder, "notebook"));
+
+ plug->priv->auth_face_image = GTK_WIDGET (gtk_builder_get_object(builder, "auth-face-image"));
+ plug->priv->auth_action_area = GTK_WIDGET (gtk_builder_get_object(builder, "auth-action-area"));
+ plug->priv->auth_realname_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-realname-label"));
+ plug->priv->auth_username_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-username-label"));
+ plug->priv->auth_prompt_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-label"));
+ plug->priv->auth_prompt_entry = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-entry"));
+ plug->priv->auth_prompt_box = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-box"));
+ plug->priv->auth_capslock_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-capslock-label"));
+ plug->priv->auth_message_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-status-label"));
+ plug->priv->auth_unlock_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-unlock-button"));
+ plug->priv->auth_cancel_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-cancel-button"));
+ plug->priv->auth_logout_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-logout-button"));
+ plug->priv->auth_switch_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-switch-button"));
+ plug->priv->auth_note_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-note-button"));
+ plug->priv->note_tab = GTK_WIDGET (gtk_builder_get_object(builder, "note-tab"));
+ plug->priv->note_tab_label = GTK_WIDGET (gtk_builder_get_object(builder, "note-tab-label"));
+ plug->priv->note_ok_button = GTK_WIDGET (gtk_builder_get_object(builder, "note-ok-button"));
+ plug->priv->note_text_view = GTK_WIDGET (gtk_builder_get_object(builder, "note-text-view"));
+ plug->priv->note_cancel_button = GTK_WIDGET (gtk_builder_get_object(builder, "note-cancel-button"));
+
+ /* Placeholder for the keyboard indicator */
+ plug->priv->auth_prompt_kbd_layout_indicator = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-kbd-layout-indicator"));
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_logout_button, TRUE);
+ }
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_switch_button, TRUE);
+ }
+ if (plug->priv->auth_note_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_note_button, TRUE);
+ }
+
+ gtk_widget_show_all (lock_dialog);
+
+ plug->priv->status_message_label = GTK_WIDGET (gtk_builder_get_object(builder, "status-message-label"));
+
+ return TRUE;
+}
+
+static int
+delete_handler (GSLockPlug *plug,
+ GdkEventAny *event,
+ gpointer data)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+
+ return TRUE; /* Do not destroy */
+}
+
+static void
+on_note_text_buffer_changed (GtkTextBuffer *buffer,
+ GSLockPlug *plug)
+{
+ int len;
+
+ len = gtk_text_buffer_get_char_count (buffer);
+ if (len > NOTE_BUFFER_MAX_CHARS)
+ {
+ gtk_widget_set_sensitive (plug->priv->note_text_view, FALSE);
+ }
+}
+
+static void
+gs_lock_plug_init (GSLockPlug *plug)
+{
+ gs_profile_start (NULL);
+
+ plug->priv = GS_LOCK_PLUG_GET_PRIVATE (plug);
+
+ clear_clipboards (plug);
+
+#ifdef WITH_LIBMATENOTIFY
+ plug->priv->leave_note_enabled = TRUE;
+#else
+ plug->priv->leave_note_enabled = FALSE;
+#endif
+
+ if (! load_theme (plug))
+ {
+ gs_debug ("Unable to load theme!");
+
+ plug->priv->vbox = gtk_vbox_new (FALSE, 0);
+
+ gtk_container_add (GTK_CONTAINER (plug), plug->priv->vbox);
+
+ /* Notebook */
+
+ plug->priv->notebook = gtk_notebook_new ();
+ gtk_notebook_set_show_tabs (GTK_NOTEBOOK (plug->priv->notebook), FALSE);
+ gtk_notebook_set_show_border (GTK_NOTEBOOK (plug->priv->notebook), FALSE);
+ gtk_box_pack_start (GTK_BOX (plug->priv->vbox), plug->priv->notebook, TRUE, TRUE, 0);
+
+ /* Page 1 */
+
+ create_page_one (plug);
+
+ gtk_widget_show_all (plug->priv->vbox);
+ }
+
+ if (plug->priv->note_text_view != NULL)
+ {
+ GtkTextBuffer *buffer;
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ g_signal_connect (buffer, "changed", G_CALLBACK (on_note_text_buffer_changed), plug);
+ }
+
+ /* Layout indicator */
+#ifdef WITH_KBD_LAYOUT_INDICATOR
+ if (plug->priv->auth_prompt_kbd_layout_indicator != NULL)
+ {
+ XklEngine *engine;
+
+ engine = xkl_engine_get_instance (GDK_DISPLAY ());
+ if (xkl_engine_get_num_groups (engine) > 1)
+ {
+ GtkWidget *layout_indicator;
+
+ layout_indicator = matekbd_indicator_new ();
+ matekbd_indicator_set_parent_tooltips (MATEKBD_INDICATOR (layout_indicator), TRUE);
+ gtk_box_pack_start (GTK_BOX (plug->priv->auth_prompt_kbd_layout_indicator),
+ layout_indicator,
+ FALSE,
+ FALSE,
+ 6);
+
+ gtk_widget_show_all (layout_indicator);
+ gtk_widget_show (plug->priv->auth_prompt_kbd_layout_indicator);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_prompt_kbd_layout_indicator);
+ }
+
+ g_object_unref (engine);
+ }
+#endif
+
+ if (plug->priv->auth_note_button != NULL)
+ {
+ if (plug->priv->leave_note_enabled)
+ {
+ gtk_widget_show_all (plug->priv->auth_note_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_note_button);
+ }
+ }
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ if (plug->priv->switch_enabled)
+ {
+ gtk_widget_show_all (plug->priv->auth_switch_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+ }
+
+ gtk_widget_grab_default (plug->priv->auth_unlock_button);
+
+ if (plug->priv->auth_username_label != NULL)
+ {
+ expand_string_for_label (plug->priv->auth_username_label);
+ }
+
+ if (plug->priv->auth_realname_label != NULL)
+ {
+ expand_string_for_label (plug->priv->auth_realname_label);
+ }
+
+ if (! plug->priv->logout_enabled || ! plug->priv->logout_command)
+ {
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ gtk_widget_hide (plug->priv->auth_logout_button);
+ }
+ }
+
+ plug->priv->timeout = DIALOG_TIMEOUT_MSEC;
+
+ g_signal_connect (plug, "key_press_event",
+ G_CALLBACK (entry_key_press), plug);
+
+ /* button press handler used to inhibit popup menu */
+ g_signal_connect (plug->priv->auth_prompt_entry, "button_press_event",
+ G_CALLBACK (entry_button_press), NULL);
+ gtk_entry_set_activates_default (GTK_ENTRY (plug->priv->auth_prompt_entry), TRUE);
+ gtk_entry_set_visibility (GTK_ENTRY (plug->priv->auth_prompt_entry), FALSE);
+
+ g_signal_connect (plug->priv->auth_unlock_button, "clicked",
+ G_CALLBACK (unlock_button_clicked), plug);
+
+ g_signal_connect (plug->priv->auth_cancel_button, "clicked",
+ G_CALLBACK (cancel_button_clicked), plug);
+
+ if (plug->priv->status_message_label)
+ {
+ if (plug->priv->status_message)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->status_message_label),
+ plug->priv->status_message);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->status_message_label);
+ }
+ }
+
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_switch_button, "clicked",
+ G_CALLBACK (switch_user_button_clicked), plug);
+ }
+
+ if (plug->priv->auth_note_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_note_button, "clicked",
+ G_CALLBACK (take_note), plug);
+ g_signal_connect (plug->priv->note_ok_button, "clicked",
+ G_CALLBACK (submit_note), plug);
+ g_signal_connect (plug->priv->note_cancel_button, "clicked",
+ G_CALLBACK (cancel_note), plug);
+ }
+
+ if (plug->priv->note_tab_label != NULL)
+ {
+ expand_string_for_label (plug->priv->note_tab_label);
+ }
+
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_logout_button, "clicked",
+ G_CALLBACK (logout_button_clicked), plug);
+ }
+
+ g_signal_connect (plug, "delete_event", G_CALLBACK (delete_handler), NULL);
+
+ gs_profile_end (NULL);
+}
+
+static void
+gs_lock_plug_finalize (GObject *object)
+{
+ GSLockPlug *plug;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_LOCK_PLUG (object));
+
+ plug = GS_LOCK_PLUG (object);
+
+ g_return_if_fail (plug->priv != NULL);
+
+ g_free (plug->priv->logout_command);
+
+ remove_response_idle (plug);
+ remove_cancel_timeout (plug);
+
+ G_OBJECT_CLASS (gs_lock_plug_parent_class)->finalize (object);
+}
+
+GtkWidget *
+gs_lock_plug_new (void)
+{
+ GtkWidget *result;
+
+ result = g_object_new (GS_TYPE_LOCK_PLUG, NULL);
+
+ gtk_window_set_focus_on_map (GTK_WINDOW (result), TRUE);
+
+ return result;
+}
diff --git a/src/gs-lock-plug.c.orig b/src/gs-lock-plug.c.orig
new file mode 100644
index 0000000..6b5856c
--- /dev/null
+++ b/src/gs-lock-plug.c.orig
@@ -0,0 +1,2050 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2006 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <time.h>
+#include <unistd.h>
+#include <sys/utsname.h>
+
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
+#include <glib/gi18n.h>
+#include <gdk/gdkkeysyms.h>
+#include <gdk/gdkx.h>
+#include <X11/XKBlib.h>
+#include <gtk/gtk.h>
+#include <mateconf/mateconf-client.h>
+
+#ifdef WITH_KBD_LAYOUT_INDICATOR
+#include <libmatekbd/matekbd-indicator.h>
+#endif
+
+#ifdef WITH_LIBMATENOTIFY
+#include <libmatenotify/notify.h>
+#endif
+
+#include "gs-lock-plug.h"
+
+#include "gs-debug.h"
+
+#define KEY_LOCK_DIALOG_THEME "/apps/mate-screensaver/lock_dialog_theme"
+#define MDM_FLEXISERVER_COMMAND "mdmflexiserver"
+#define MDM_FLEXISERVER_ARGS "--startnew Standard"
+
+/* same as SMS ;) */
+#define NOTE_BUFFER_MAX_CHARS 160
+
+enum
+{
+ AUTH_PAGE = 0,
+};
+
+#define FACE_ICON_SIZE 48
+#define DIALOG_TIMEOUT_MSEC 60000
+
+static void gs_lock_plug_finalize (GObject *object);
+
+#define GS_LOCK_PLUG_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_LOCK_PLUG, GSLockPlugPrivate))
+
+struct GSLockPlugPrivate
+{
+ GtkWidget *vbox;
+ GtkWidget *auth_action_area;
+
+ GtkWidget *notebook;
+ GtkWidget *auth_face_image;
+ GtkWidget *auth_realname_label;
+ GtkWidget *auth_username_label;
+ GtkWidget *auth_prompt_label;
+ GtkWidget *auth_prompt_entry;
+ GtkWidget *auth_prompt_box;
+ GtkWidget *auth_capslock_label;
+ GtkWidget *auth_message_label;
+ GtkWidget *status_message_label;
+
+ GtkWidget *auth_unlock_button;
+ GtkWidget *auth_switch_button;
+ GtkWidget *auth_cancel_button;
+ GtkWidget *auth_logout_button;
+ GtkWidget *auth_note_button;
+ GtkWidget *note_tab;
+ GtkWidget *note_tab_label;
+ GtkWidget *note_text_view;
+ GtkWidget *note_ok_button;
+ GtkWidget *note_cancel_button;
+
+ GtkWidget *auth_prompt_kbd_layout_indicator;
+
+ gboolean caps_lock_on;
+ gboolean switch_enabled;
+ gboolean leave_note_enabled;
+ gboolean logout_enabled;
+ char *logout_command;
+ char *status_message;
+
+ guint timeout;
+
+ guint cancel_timeout_id;
+ guint auth_check_idle_id;
+ guint response_idle_id;
+
+ GList *key_events;
+};
+
+typedef struct _ResponseData ResponseData;
+
+struct _ResponseData
+{
+ gint response_id;
+};
+
+
+enum
+{
+ RESPONSE,
+ CLOSE,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_LOGOUT_ENABLED,
+ PROP_LOGOUT_COMMAND,
+ PROP_SWITCH_ENABLED,
+ PROP_STATUS_MESSAGE
+};
+
+static guint lock_plug_signals [LAST_SIGNAL];
+
+G_DEFINE_TYPE (GSLockPlug, gs_lock_plug, GTK_TYPE_PLUG)
+
+static void
+gs_lock_plug_style_set (GtkWidget *widget,
+ GtkStyle *previous_style)
+{
+ GSLockPlug *plug;
+
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->style_set)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->style_set (widget, previous_style);
+ }
+
+ plug = GS_LOCK_PLUG (widget);
+
+ if (! plug->priv->vbox)
+ {
+ return;
+ }
+
+ gtk_container_set_border_width (GTK_CONTAINER (plug->priv->vbox), 12);
+ gtk_box_set_spacing (GTK_BOX (plug->priv->vbox), 12);
+
+ gtk_container_set_border_width (GTK_CONTAINER (plug->priv->auth_action_area), 0);
+ gtk_box_set_spacing (GTK_BOX (plug->priv->auth_action_area), 5);
+}
+
+static void
+do_user_switch (GSLockPlug *plug)
+{
+ GError *error;
+ gboolean res;
+ char *command;
+
+ command = g_strdup_printf ("%s %s",
+ MDM_FLEXISERVER_COMMAND,
+ MDM_FLEXISERVER_ARGS);
+
+ error = NULL;
+ res = gdk_spawn_command_line_on_screen (gdk_screen_get_default (),
+ command,
+ &error);
+
+ g_free (command);
+
+ if (! res)
+ {
+ gs_debug ("Unable to start MDM greeter: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+static void
+set_status_text (GSLockPlug *plug,
+ const char *text)
+{
+ if (plug->priv->auth_message_label != NULL)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_message_label), text);
+ }
+}
+
+void
+gs_lock_plug_set_sensitive (GSLockPlug *plug,
+ gboolean sensitive)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, sensitive);
+ gtk_widget_set_sensitive (plug->priv->auth_action_area, sensitive);
+}
+
+static void
+remove_cancel_timeout (GSLockPlug *plug)
+{
+ if (plug->priv->cancel_timeout_id > 0)
+ {
+ g_source_remove (plug->priv->cancel_timeout_id);
+ plug->priv->cancel_timeout_id = 0;
+ }
+}
+
+static void
+remove_response_idle (GSLockPlug *plug)
+{
+ if (plug->priv->response_idle_id > 0)
+ {
+ g_source_remove (plug->priv->response_idle_id);
+ plug->priv->response_idle_id = 0;
+ }
+}
+
+static void
+gs_lock_plug_response (GSLockPlug *plug,
+ gint response_id)
+{
+ int new_response;
+
+ new_response = response_id;
+
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ /* Act only on response IDs we recognize */
+ if (!(response_id == GS_LOCK_PLUG_RESPONSE_OK
+ || response_id == GS_LOCK_PLUG_RESPONSE_CANCEL))
+ {
+ return;
+ }
+
+ remove_cancel_timeout (plug);
+ remove_response_idle (plug);
+
+ if (response_id == GS_LOCK_PLUG_RESPONSE_CANCEL)
+ {
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), "");
+ }
+
+ g_signal_emit (plug,
+ lock_plug_signals [RESPONSE],
+ 0,
+ new_response);
+}
+
+static gboolean
+response_cancel_idle_cb (GSLockPlug *plug)
+{
+ plug->priv->response_idle_id = 0;
+
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+
+ return FALSE;
+}
+
+static gboolean
+dialog_timed_out (GSLockPlug *plug)
+{
+ gs_lock_plug_set_sensitive (plug, FALSE);
+ set_status_text (plug, _("Time has expired."));
+
+ if (plug->priv->response_idle_id != 0)
+ {
+ g_warning ("Response idle ID already set but shouldn't be");
+ }
+
+ remove_response_idle (plug);
+
+ plug->priv->response_idle_id = g_timeout_add (2000,
+ (GSourceFunc)response_cancel_idle_cb,
+ plug);
+ return FALSE;
+}
+
+
+static void
+capslock_update (GSLockPlug *plug,
+ gboolean is_on)
+{
+
+ plug->priv->caps_lock_on = is_on;
+
+ if (plug->priv->auth_capslock_label == NULL)
+ {
+ return;
+ }
+
+ if (is_on)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_capslock_label),
+ _("You have the Caps Lock key on."));
+ }
+ else
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_capslock_label),
+ "");
+ }
+}
+
+static gboolean
+is_capslock_on (void)
+{
+ XkbStateRec states;
+ Display *dsp;
+
+ dsp = GDK_DISPLAY ();
+ if (XkbGetState (dsp, XkbUseCoreKbd, &states) != Success)
+ {
+ return FALSE;
+ }
+
+ return (states.locked_mods & LockMask) != 0;
+}
+
+static void
+restart_cancel_timeout (GSLockPlug *plug)
+{
+ remove_cancel_timeout (plug);
+
+ plug->priv->cancel_timeout_id = g_timeout_add (plug->priv->timeout,
+ (GSourceFunc)dialog_timed_out,
+ plug);
+}
+
+void
+gs_lock_plug_get_text (GSLockPlug *plug,
+ char **text)
+{
+ const char *typed_text;
+ char *null_text;
+ char *local_text;
+
+ typed_text = gtk_entry_get_text (GTK_ENTRY (plug->priv->auth_prompt_entry));
+ local_text = g_locale_from_utf8 (typed_text, strlen (typed_text), NULL, NULL, NULL);
+
+ null_text = g_strnfill (strlen (typed_text) + 1, '\b');
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), null_text);
+ gtk_entry_set_text (GTK_ENTRY (plug->priv->auth_prompt_entry), "");
+ g_free (null_text);
+
+ if (text != NULL)
+ {
+ *text = local_text;
+ }
+}
+
+typedef struct
+{
+ GSLockPlug *plug;
+ gint response_id;
+ GMainLoop *loop;
+ gboolean destroyed;
+} RunInfo;
+
+static void
+shutdown_loop (RunInfo *ri)
+{
+ if (g_main_loop_is_running (ri->loop))
+ g_main_loop_quit (ri->loop);
+}
+
+static void
+run_unmap_handler (GSLockPlug *plug,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ shutdown_loop (ri);
+}
+
+static void
+run_response_handler (GSLockPlug *plug,
+ gint response_id,
+ gpointer data)
+{
+ RunInfo *ri;
+
+ ri = data;
+
+ ri->response_id = response_id;
+
+ shutdown_loop (ri);
+}
+
+static gint
+run_delete_handler (GSLockPlug *plug,
+ GdkEventAny *event,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ shutdown_loop (ri);
+
+ return TRUE; /* Do not destroy */
+}
+
+static void
+run_destroy_handler (GSLockPlug *plug,
+ gpointer data)
+{
+ RunInfo *ri = data;
+
+ /* shutdown_loop will be called by run_unmap_handler */
+ ri->destroyed = TRUE;
+}
+
+/* adapted from GTK+ gtkdialog.c */
+int
+gs_lock_plug_run (GSLockPlug *plug)
+{
+ RunInfo ri = { NULL, GTK_RESPONSE_NONE, NULL, FALSE };
+ gboolean was_modal;
+ gulong response_handler;
+ gulong unmap_handler;
+ gulong destroy_handler;
+ gulong delete_handler;
+
+ g_return_val_if_fail (GS_IS_LOCK_PLUG (plug), -1);
+
+ g_object_ref (plug);
+
+ was_modal = GTK_WINDOW (plug)->modal;
+ if (!was_modal)
+ {
+ gtk_window_set_modal (GTK_WINDOW (plug), TRUE);
+ }
+
+ if (!GTK_WIDGET_VISIBLE (plug))
+ {
+ gtk_widget_show (GTK_WIDGET (plug));
+ }
+
+ response_handler =
+ g_signal_connect (plug,
+ "response",
+ G_CALLBACK (run_response_handler),
+ &ri);
+
+ unmap_handler =
+ g_signal_connect (plug,
+ "unmap",
+ G_CALLBACK (run_unmap_handler),
+ &ri);
+
+ delete_handler =
+ g_signal_connect (plug,
+ "delete_event",
+ G_CALLBACK (run_delete_handler),
+ &ri);
+
+ destroy_handler =
+ g_signal_connect (plug,
+ "destroy",
+ G_CALLBACK (run_destroy_handler),
+ &ri);
+
+ ri.loop = g_main_loop_new (NULL, FALSE);
+
+ GDK_THREADS_LEAVE ();
+ g_main_loop_run (ri.loop);
+ GDK_THREADS_ENTER ();
+
+ g_main_loop_unref (ri.loop);
+
+ ri.loop = NULL;
+
+ if (!ri.destroyed)
+ {
+ if (! was_modal)
+ {
+ gtk_window_set_modal (GTK_WINDOW (plug), FALSE);
+ }
+
+ g_signal_handler_disconnect (plug, response_handler);
+ g_signal_handler_disconnect (plug, unmap_handler);
+ g_signal_handler_disconnect (plug, delete_handler);
+ g_signal_handler_disconnect (plug, destroy_handler);
+ }
+
+ g_object_unref (plug);
+
+ return ri.response_id;
+}
+
+
+static cairo_surface_t *
+surface_from_pixbuf (GdkPixbuf *pixbuf)
+{
+ cairo_surface_t *surface;
+ cairo_t *cr;
+
+ surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
+ CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
+ gdk_pixbuf_get_width (pixbuf),
+ gdk_pixbuf_get_height (pixbuf));
+ cr = cairo_create (surface);
+ gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
+ cairo_paint (cr);
+ cairo_destroy (cr);
+
+ return surface;
+}
+
+static void
+rounded_rectangle (cairo_t *cr,
+ gdouble aspect,
+ gdouble x,
+ gdouble y,
+ gdouble corner_radius,
+ gdouble width,
+ gdouble height)
+{
+ gdouble radius;
+ gdouble degrees;
+
+ radius = corner_radius / aspect;
+ degrees = G_PI / 180.0;
+
+ cairo_new_sub_path (cr);
+ cairo_arc (cr,
+ x + width - radius,
+ y + radius,
+ radius,
+ -90 * degrees,
+ 0 * degrees);
+ cairo_arc (cr,
+ x + width - radius,
+ y + height - radius,
+ radius,
+ 0 * degrees,
+ 90 * degrees);
+ cairo_arc (cr,
+ x + radius,
+ y + height - radius,
+ radius,
+ 90 * degrees,
+ 180 * degrees);
+ cairo_arc (cr,
+ x + radius,
+ y + radius,
+ radius,
+ 180 * degrees,
+ 270 * degrees);
+ cairo_close_path (cr);
+}
+
+static void
+image_set_from_pixbuf (GtkImage *image,
+ GdkPixbuf *source)
+{
+ cairo_t *cr;
+ cairo_t *cr_mask;
+ cairo_surface_t *surface;
+ GdkPixmap *pixmap;
+ GdkPixmap *bitmask;
+ int w;
+ int h;
+ int frame_width;
+ double radius;
+ GdkColor color;
+ double r;
+ double g;
+ double b;
+
+ frame_width = 5;
+
+ w = gdk_pixbuf_get_width (source) + frame_width * 2;
+ h = gdk_pixbuf_get_height (source) + frame_width * 2;
+
+ radius = w / 10;
+
+ pixmap = gdk_pixmap_new (GTK_WIDGET (image)->window, w, h, -1);
+ bitmask = gdk_pixmap_new (GTK_WIDGET (image)->window, w, h, 1);
+
+ cr = gdk_cairo_create (pixmap);
+ cr_mask = gdk_cairo_create (bitmask);
+
+ /* setup mask */
+ cairo_rectangle (cr_mask, 0, 0, w, h);
+ cairo_set_operator (cr_mask, CAIRO_OPERATOR_CLEAR);
+ cairo_fill (cr_mask);
+
+ rounded_rectangle (cr_mask, 1.0, 0.5, 0.5, radius, w - 1, h - 1);
+ cairo_set_operator (cr_mask, CAIRO_OPERATOR_OVER);
+ cairo_set_source_rgb (cr_mask, 1, 1, 1);
+ cairo_fill (cr_mask);
+
+ color = GTK_WIDGET (image)->style->bg [GTK_STATE_NORMAL];
+ r = (float)color.red / 65535.0;
+ g = (float)color.green / 65535.0;
+ b = (float)color.blue / 65535.0;
+
+ /* set up image */
+ cairo_rectangle (cr, 0, 0, w, h);
+ cairo_set_source_rgb (cr, r, g, b);
+ cairo_fill (cr);
+
+ rounded_rectangle (cr,
+ 1.0,
+ frame_width + 0.5,
+ frame_width + 0.5,
+ radius,
+ w - frame_width * 2 - 1,
+ h - frame_width * 2 - 1);
+ cairo_set_source_rgba (cr, 0.5, 0.5, 0.5, 0.3);
+ cairo_fill_preserve (cr);
+
+ surface = surface_from_pixbuf (source);
+ cairo_set_source_surface (cr, surface, frame_width, frame_width);
+ cairo_fill (cr);
+
+ gtk_image_set_from_pixmap (image, pixmap, bitmask);
+
+ cairo_surface_destroy (surface);
+
+ g_object_unref (bitmask);
+ g_object_unref (pixmap);
+
+ cairo_destroy (cr_mask);
+ cairo_destroy (cr);
+}
+
+
+static gboolean
+check_user_file (const gchar *filename,
+ uid_t user,
+ gssize max_file_size,
+ gboolean relax_group,
+ gboolean relax_other)
+{
+ struct stat fileinfo;
+
+ if (max_file_size < 0)
+ {
+ max_file_size = G_MAXSIZE;
+ }
+
+ /* Exists/Readable? */
+ if (g_stat (filename, &fileinfo) < 0)
+ {
+ return FALSE;
+ }
+
+ /* Is a regular file */
+ if (G_UNLIKELY (!S_ISREG (fileinfo.st_mode)))
+ {
+ return FALSE;
+ }
+
+ /* Owned by user? */
+ if (G_UNLIKELY (fileinfo.st_uid != user))
+ {
+ return FALSE;
+ }
+
+ /* Group not writable or relax_group? */
+ if (G_UNLIKELY ((fileinfo.st_mode & S_IWGRP) == S_IWGRP && !relax_group))
+ {
+ return FALSE;
+ }
+
+ /* Other not writable or relax_other? */
+ if (G_UNLIKELY ((fileinfo.st_mode & S_IWOTH) == S_IWOTH && !relax_other))
+ {
+ return FALSE;
+ }
+
+ /* Size is kosher? */
+ if (G_UNLIKELY (fileinfo.st_size > max_file_size))
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean
+set_face_image (GSLockPlug *plug)
+{
+ GdkPixbuf *pixbuf;
+ const char *homedir;
+ char *path;
+ int icon_size = 96;
+ gsize user_max_file = 65536;
+ uid_t uid;
+
+ homedir = g_get_home_dir ();
+ uid = getuid ();
+
+ path = g_build_filename (homedir, ".face", NULL);
+
+ pixbuf = NULL;
+ if (check_user_file (path, uid, user_max_file, 0, 0))
+ {
+ pixbuf = gdk_pixbuf_new_from_file_at_size (path,
+ icon_size,
+ icon_size,
+ NULL);
+ }
+
+ g_free (path);
+
+ if (pixbuf == NULL)
+ {
+ return FALSE;
+ }
+
+ image_set_from_pixbuf (GTK_IMAGE (plug->priv->auth_face_image), pixbuf);
+
+ g_object_unref (pixbuf);
+
+ return TRUE;
+}
+
+static void
+gs_lock_plug_show (GtkWidget *widget)
+{
+ GSLockPlug *plug = GS_LOCK_PLUG (widget);
+
+ gs_profile_start (NULL);
+
+ gs_profile_start ("parent");
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->show)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->show (widget);
+ }
+
+ gs_profile_end ("parent");
+
+
+ if (plug->priv->auth_face_image)
+ {
+ set_face_image (plug);
+ }
+
+ capslock_update (plug, is_capslock_on ());
+
+ restart_cancel_timeout (plug);
+
+ gs_profile_end (NULL);
+}
+
+static void
+gs_lock_plug_hide (GtkWidget *widget)
+{
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->hide)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->hide (widget);
+ }
+}
+
+static void
+queue_key_event (GSLockPlug *plug,
+ GdkEventKey *event)
+{
+ GdkEvent *saved_event;
+
+ saved_event = gdk_event_copy ((GdkEvent *)event);
+ plug->priv->key_events = g_list_prepend (plug->priv->key_events,
+ saved_event);
+}
+
+static void
+forward_key_events (GSLockPlug *plug)
+{
+ plug->priv->key_events = g_list_reverse (plug->priv->key_events);
+ while (plug->priv->key_events != NULL)
+ {
+ GdkEventKey *event = plug->priv->key_events->data;
+
+ gtk_window_propagate_key_event (GTK_WINDOW (plug), event);
+
+ gdk_event_free ((GdkEvent *)event);
+
+ plug->priv->key_events = g_list_delete_link (plug->priv->key_events,
+ plug->priv->key_events);
+ }
+}
+
+static void
+gs_lock_plug_size_request (GtkWidget *widget,
+ GtkRequisition *requisition)
+{
+ GSLockPlug *plug = GS_LOCK_PLUG (widget);
+ int mod_width;
+ int mod_height;
+
+ if (GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->size_request)
+ {
+ GTK_WIDGET_CLASS (gs_lock_plug_parent_class)->size_request (widget, requisition);
+ }
+
+ /* don't constrain size when themed */
+ if (plug->priv->vbox == NULL)
+ {
+ return;
+ }
+
+ mod_width = requisition->height * 1.3;
+ mod_height = requisition->width / 1.6;
+ if (requisition->width < mod_width)
+ {
+ /* if the dialog is tall fill out the width */
+ requisition->width = mod_width;
+ }
+ else if (requisition->height < mod_height)
+ {
+ /* if the dialog is wide fill out the height */
+ requisition->height = mod_height;
+ }
+}
+
+static void
+gs_lock_plug_set_logout_enabled (GSLockPlug *plug,
+ gboolean logout_enabled)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ if (plug->priv->logout_enabled == logout_enabled)
+ {
+ return;
+ }
+
+ plug->priv->logout_enabled = logout_enabled;
+ g_object_notify (G_OBJECT (plug), "logout-enabled");
+
+ if (plug->priv->auth_logout_button == NULL)
+ {
+ return;
+ }
+
+ if (logout_enabled)
+ {
+ gtk_widget_show (plug->priv->auth_logout_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_logout_button);
+ }
+}
+
+static void
+gs_lock_plug_set_logout_command (GSLockPlug *plug,
+ const char *command)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ g_free (plug->priv->logout_command);
+
+ if (command)
+ {
+ plug->priv->logout_command = g_strdup (command);
+ }
+ else
+ {
+ plug->priv->logout_command = NULL;
+ }
+}
+
+static gboolean
+is_program_in_path (const char *program)
+{
+ char *tmp = g_find_program_in_path (program);
+ if (tmp != NULL)
+ {
+ g_free (tmp);
+ return TRUE;
+ }
+ else
+ {
+ return FALSE;
+ }
+}
+
+static void
+gs_lock_plug_set_status_message (GSLockPlug *plug,
+ const char *status_message)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ g_free (plug->priv->status_message);
+ plug->priv->status_message = g_strdup (status_message);
+
+ if (plug->priv->status_message_label)
+ {
+ if (plug->priv->status_message)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->status_message_label),
+ plug->priv->status_message);
+ gtk_widget_show (plug->priv->status_message_label);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->status_message_label);
+ }
+ }
+}
+
+static void
+gs_lock_plug_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSLockPlug *self;
+
+ self = GS_LOCK_PLUG (object);
+
+ switch (prop_id)
+ {
+ case PROP_LOGOUT_ENABLED:
+ g_value_set_boolean (value, self->priv->logout_enabled);
+ break;
+ case PROP_LOGOUT_COMMAND:
+ g_value_set_string (value, self->priv->logout_command);
+ break;
+ case PROP_SWITCH_ENABLED:
+ g_value_set_boolean (value, self->priv->switch_enabled);
+ break;
+ case PROP_STATUS_MESSAGE:
+ g_value_set_string (value, self->priv->status_message);
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_lock_plug_set_switch_enabled (GSLockPlug *plug,
+ gboolean switch_enabled)
+{
+ g_return_if_fail (GS_LOCK_PLUG (plug));
+
+ if (plug->priv->switch_enabled == switch_enabled)
+ {
+ return;
+ }
+
+ plug->priv->switch_enabled = switch_enabled;
+ g_object_notify (G_OBJECT (plug), "switch-enabled");
+
+ if (plug->priv->auth_switch_button == NULL)
+ {
+ return;
+ }
+
+ if (switch_enabled)
+ {
+ gboolean found;
+ found = is_program_in_path (MDM_FLEXISERVER_COMMAND);
+ if (found)
+ {
+ gtk_widget_show (plug->priv->auth_switch_button);
+ }
+ else
+ {
+ gs_debug ("Waring: MDM flexiserver command not found: %s", MDM_FLEXISERVER_COMMAND);
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+}
+
+static void
+gs_lock_plug_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSLockPlug *self;
+
+ self = GS_LOCK_PLUG (object);
+
+ switch (prop_id)
+ {
+ case PROP_LOGOUT_ENABLED:
+ gs_lock_plug_set_logout_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOGOUT_COMMAND:
+ gs_lock_plug_set_logout_command (self, g_value_get_string (value));
+ break;
+ case PROP_STATUS_MESSAGE:
+ gs_lock_plug_set_status_message (self, g_value_get_string (value));
+ break;
+ case PROP_SWITCH_ENABLED:
+ gs_lock_plug_set_switch_enabled (self, g_value_get_boolean (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_lock_plug_close (GSLockPlug *plug)
+{
+ /* Synthesize delete_event to close dialog. */
+
+ GtkWidget *widget = GTK_WIDGET (plug);
+ GdkEvent *event;
+
+ event = gdk_event_new (GDK_DELETE);
+ event->any.window = g_object_ref (widget->window);
+ event->any.send_event = TRUE;
+
+ gtk_main_do_event (event);
+ gdk_event_free (event);
+}
+
+static void
+gs_lock_plug_class_init (GSLockPlugClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+ GtkBindingSet *binding_set;
+
+ object_class->finalize = gs_lock_plug_finalize;
+ object_class->get_property = gs_lock_plug_get_property;
+ object_class->set_property = gs_lock_plug_set_property;
+
+ widget_class->style_set = gs_lock_plug_style_set;
+ widget_class->show = gs_lock_plug_show;
+ widget_class->hide = gs_lock_plug_hide;
+ widget_class->size_request = gs_lock_plug_size_request;
+
+ klass->close = gs_lock_plug_close;
+
+ g_type_class_add_private (klass, sizeof (GSLockPlugPrivate));
+
+ lock_plug_signals [RESPONSE] = g_signal_new ("response",
+ G_OBJECT_CLASS_TYPE (klass),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSLockPlugClass, response),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__INT,
+ G_TYPE_NONE, 1,
+ G_TYPE_INT);
+ lock_plug_signals [CLOSE] = g_signal_new ("close",
+ G_OBJECT_CLASS_TYPE (klass),
+ G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
+ G_STRUCT_OFFSET (GSLockPlugClass, close),
+ NULL, NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE, 0);
+
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_ENABLED,
+ g_param_spec_boolean ("logout-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_COMMAND,
+ g_param_spec_string ("logout-command",
+ NULL,
+ NULL,
+ NULL,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_STATUS_MESSAGE,
+ g_param_spec_string ("status-message",
+ NULL,
+ NULL,
+ NULL,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_SWITCH_ENABLED,
+ g_param_spec_boolean ("switch-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+
+ binding_set = gtk_binding_set_by_class (klass);
+
+ gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
+ "close", 0);
+}
+
+static void
+clear_clipboards (GSLockPlug *plug)
+{
+ GtkClipboard *clipboard;
+
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (plug), GDK_SELECTION_PRIMARY);
+ gtk_clipboard_clear (clipboard);
+ gtk_clipboard_set_text (clipboard, "", -1);
+ clipboard = gtk_widget_get_clipboard (GTK_WIDGET (plug), GDK_SELECTION_CLIPBOARD);
+ gtk_clipboard_clear (clipboard);
+ gtk_clipboard_set_text (clipboard, "", -1);
+}
+
+static void
+take_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+ int page;
+
+ page = gtk_notebook_page_num (GTK_NOTEBOOK (plug->priv->notebook), plug->priv->note_tab);
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), page);
+ /* this counts as activity so restart the timer */
+ restart_cancel_timeout (plug);
+}
+
+static void
+submit_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+#ifdef WITH_LIBMATENOTIFY
+ char *text;
+ char summary[128];
+ char *escaped_text;
+ GtkTextBuffer *buffer;
+ GtkTextIter start, end;
+ time_t t;
+ struct tm *tmp;
+ NotifyNotification *note;
+
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), AUTH_PAGE);
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ gtk_text_buffer_get_bounds (buffer, &start, &end);
+ text = gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
+ gtk_text_buffer_set_text (buffer, "", 0);
+ escaped_text = g_markup_escape_text (text, -1);
+
+ t = time (NULL);
+ tmp = localtime (&t);
+ strftime (summary, 128, "%X", tmp);
+
+ notify_init ("mate-screensaver-dialog");
+ note = notify_notification_new (summary, escaped_text, NULL, NULL);
+ notify_notification_set_timeout (note, NOTIFY_EXPIRES_NEVER);
+ notify_notification_show (note, NULL);
+ g_object_unref (note);
+
+ g_free (text);
+ g_free (escaped_text);
+
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+#endif /* WITH_LIBMATENOTIFY */
+}
+
+static void
+cancel_note (GtkButton *button,
+ GSLockPlug *plug)
+{
+ GtkTextBuffer *buffer;
+
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (plug->priv->notebook), AUTH_PAGE);
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ gtk_text_buffer_set_text (buffer, "", 0);
+
+ /* this counts as activity so restart the timer */
+ restart_cancel_timeout (plug);
+
+ gtk_window_set_default (GTK_WINDOW (plug), plug->priv->auth_unlock_button);
+
+ clear_clipboards (plug);
+}
+
+static void
+logout_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ char **argv = NULL;
+ GError *error = NULL;
+ gboolean res;
+
+ if (! plug->priv->logout_command)
+ {
+ return;
+ }
+
+ res = g_shell_parse_argv (plug->priv->logout_command, NULL, &argv, &error);
+
+ if (! res)
+ {
+ g_warning ("Could not parse logout command: %s", error->message);
+ g_error_free (error);
+ return;
+ }
+
+ g_spawn_async (g_get_home_dir (),
+ argv,
+ NULL,
+ G_SPAWN_SEARCH_PATH,
+ NULL,
+ NULL,
+ NULL,
+ &error);
+
+ g_strfreev (argv);
+
+ if (error)
+ {
+ g_warning ("Could not run logout command: %s", error->message);
+ g_error_free (error);
+ }
+}
+
+void
+gs_lock_plug_set_busy (GSLockPlug *plug)
+{
+ GdkCursor *cursor;
+ GtkWidget *top_level;
+
+ top_level = gtk_widget_get_toplevel (GTK_WIDGET (plug));
+
+ cursor = gdk_cursor_new (GDK_WATCH);
+ gdk_window_set_cursor (top_level->window, cursor);
+ gdk_cursor_unref (cursor);
+}
+
+void
+gs_lock_plug_set_ready (GSLockPlug *plug)
+{
+ GdkCursor *cursor;
+ GtkWidget *top_level;
+
+ top_level = gtk_widget_get_toplevel (GTK_WIDGET (plug));
+
+ cursor = gdk_cursor_new (GDK_LEFT_PTR);
+ gdk_window_set_cursor (top_level->window, cursor);
+ gdk_cursor_unref (cursor);
+}
+
+void
+gs_lock_plug_enable_prompt (GSLockPlug *plug,
+ const char *message,
+ gboolean visible)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ gs_debug ("Setting prompt to: %s", message);
+
+ gtk_widget_set_sensitive (plug->priv->auth_unlock_button, TRUE);
+ gtk_widget_show (plug->priv->auth_unlock_button);
+ gtk_widget_grab_default (plug->priv->auth_unlock_button);
+ gtk_label_set_text (GTK_LABEL (plug->priv->auth_prompt_label), message);
+ gtk_widget_show (plug->priv->auth_prompt_label);
+ gtk_entry_set_visibility (GTK_ENTRY (plug->priv->auth_prompt_entry), visible);
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, TRUE);
+ gtk_widget_show (plug->priv->auth_prompt_entry);
+
+ if (! GTK_WIDGET_HAS_FOCUS (plug->priv->auth_prompt_entry))
+ {
+ gtk_widget_grab_focus (plug->priv->auth_prompt_entry);
+ }
+
+ /* were there any key events sent to the plug while the
+ * entry wasnt ready? If so, forward them along
+ */
+ forward_key_events (plug);
+
+ restart_cancel_timeout (plug);
+}
+
+void
+gs_lock_plug_disable_prompt (GSLockPlug *plug)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ /* gtk_widget_hide (plug->priv->auth_prompt_entry); */
+ /* gtk_widget_hide (plug->priv->auth_prompt_label); */
+ gtk_widget_set_sensitive (plug->priv->auth_unlock_button, FALSE);
+ gtk_widget_set_sensitive (plug->priv->auth_prompt_entry, FALSE);
+ /* gtk_widget_hide (plug->priv->auth_unlock_button); */
+
+ gtk_widget_grab_default (plug->priv->auth_cancel_button);
+}
+
+void
+gs_lock_plug_show_message (GSLockPlug *plug,
+ const char *message)
+{
+ g_return_if_fail (GS_IS_LOCK_PLUG (plug));
+
+ set_status_text (plug, message ? message : "");
+}
+
+/* button press handler used to inhibit popup menu */
+static gint
+entry_button_press (GtkWidget *widget,
+ GdkEventButton *event)
+{
+ if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
+ {
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static gint
+entry_key_press (GtkWidget *widget,
+ GdkEventKey *event,
+ GSLockPlug *plug)
+{
+ gboolean capslock_on;
+
+ restart_cancel_timeout (plug);
+
+ capslock_on = is_capslock_on ();
+
+ if (capslock_on != plug->priv->caps_lock_on)
+ {
+ capslock_update (plug, capslock_on);
+ }
+
+ /* if the input widget is visible and ready for input
+ * then just carry on as usual
+ */
+ if (GTK_WIDGET_VISIBLE (plug->priv->auth_prompt_entry) &&
+ GTK_WIDGET_IS_SENSITIVE (plug->priv->auth_prompt_entry))
+ {
+ return FALSE;
+ }
+
+ if (strcmp (event->string, "") == 0)
+ {
+ return FALSE;
+ }
+
+ queue_key_event (plug, event);
+
+ return TRUE;
+}
+
+/* adapted from gtk_dialog_add_button */
+static GtkWidget *
+gs_lock_plug_add_button (GSLockPlug *plug,
+ GtkWidget *action_area,
+ const gchar *button_text)
+{
+ GtkWidget *button;
+
+ g_return_val_if_fail (GS_IS_LOCK_PLUG (plug), NULL);
+ g_return_val_if_fail (button_text != NULL, NULL);
+
+ button = gtk_button_new_from_stock (button_text);
+
+ GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
+
+ gtk_widget_show (button);
+
+ gtk_box_pack_end (GTK_BOX (action_area),
+ button,
+ FALSE, TRUE, 0);
+
+ return button;
+}
+
+static char *
+get_user_display_name (void)
+{
+ const char *name;
+ char *utf8_name;
+
+ name = g_get_real_name ();
+
+ if (name == NULL || strcmp (name, "Unknown") == 0)
+ {
+ name = g_get_user_name ();
+ }
+
+ utf8_name = NULL;
+
+ if (name != NULL)
+ {
+ utf8_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
+ }
+
+ return utf8_name;
+}
+
+static char *
+get_user_name (void)
+{
+ const char *name;
+ char *utf8_name;
+
+ name = g_get_user_name ();
+
+ utf8_name = NULL;
+ if (name != NULL)
+ {
+ utf8_name = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
+ }
+
+ return utf8_name;
+}
+
+static void
+create_page_one_buttons (GSLockPlug *plug)
+{
+
+ gs_profile_start ("page one buttons");
+
+ plug->priv->auth_switch_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("S_witch User..."));
+ gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (plug->priv->auth_action_area),
+ plug->priv->auth_switch_button,
+ TRUE);
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_switch_button), FALSE);
+ gtk_widget_set_no_show_all (plug->priv->auth_switch_button, TRUE);
+
+ plug->priv->auth_logout_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("Log _Out"));
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_logout_button), FALSE);
+ gtk_widget_set_no_show_all (plug->priv->auth_logout_button, TRUE);
+
+ plug->priv->auth_cancel_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ GTK_STOCK_CANCEL);
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_cancel_button), FALSE);
+
+ plug->priv->auth_unlock_button = gs_lock_plug_add_button (GS_LOCK_PLUG (plug),
+ plug->priv->auth_action_area,
+ _("_Unlock"));
+ gtk_button_set_focus_on_click (GTK_BUTTON (plug->priv->auth_unlock_button), FALSE);
+
+ gtk_window_set_default (GTK_WINDOW (plug), plug->priv->auth_unlock_button);
+
+ gs_profile_end ("page one buttons");
+}
+
+/* adapted from MDM */
+static char *
+expand_string (const char *text)
+{
+ GString *str;
+ const char *p;
+ char *username;
+ int i;
+ int n_chars;
+ struct utsname name;
+
+ str = g_string_sized_new (strlen (text));
+
+ p = text;
+ n_chars = g_utf8_strlen (text, -1);
+ i = 0;
+
+ while (i < n_chars)
+ {
+ gunichar ch;
+
+ ch = g_utf8_get_char (p);
+
+ /* Backslash commands */
+ if (ch == '\\')
+ {
+ p = g_utf8_next_char (p);
+ i++;
+ ch = g_utf8_get_char (p);
+
+ if (i >= n_chars || ch == '\0')
+ {
+ g_warning ("Unescaped \\ at end of text\n");
+ goto bail;
+ }
+ else if (ch == 'n')
+ {
+ g_string_append_unichar (str, '\n');
+ }
+ else
+ {
+ g_string_append_unichar (str, ch);
+ }
+ }
+ else if (ch == '%')
+ {
+ p = g_utf8_next_char (p);
+ i++;
+ ch = g_utf8_get_char (p);
+
+ if (i >= n_chars || ch == '\0')
+ {
+ g_warning ("Unescaped %% at end of text\n");
+ goto bail;
+ }
+
+ switch (ch)
+ {
+ case '%':
+ g_string_append (str, "%");
+ break;
+ case 'c':
+ /* clock */
+ break;
+ case 'd':
+ /* display */
+ g_string_append (str, g_getenv ("DISPLAY"));
+ break;
+ case 'h':
+ /* hostname */
+ g_string_append (str, g_get_host_name ());
+ break;
+ case 'm':
+ /* machine name */
+ uname (&name);
+ g_string_append (str, name.machine);
+ break;
+ case 'n':
+ /* nodename */
+ uname (&name);
+ g_string_append (str, name.nodename);
+ break;
+ case 'r':
+ /* release */
+ uname (&name);
+ g_string_append (str, name.release);
+ break;
+ case 'R':
+ /* Real name */
+ username = get_user_display_name ();
+ g_string_append (str, username);
+ g_free (username);
+ break;
+ case 's':
+ /* system name */
+ uname (&name);
+ g_string_append (str, name.sysname);
+ break;
+ case 'U':
+ /* Username */
+ username = get_user_name ();
+ g_string_append (str, username);
+ g_free (username);
+ break;
+ default:
+ if (ch < 127)
+ {
+ g_warning ("unknown escape code %%%c in text\n", (char)ch);
+ }
+ else
+ {
+ g_warning ("unknown escape code %%(U%x) in text\n", (int)ch);
+ }
+ }
+ }
+ else
+ {
+ g_string_append_unichar (str, ch);
+ }
+ p = g_utf8_next_char (p);
+ i++;
+ }
+
+bail:
+
+ return g_string_free (str, FALSE);
+}
+
+static void
+expand_string_for_label (GtkWidget *label)
+{
+ const char *template;
+ char *str;
+
+ template = gtk_label_get_label (GTK_LABEL (label));
+ str = expand_string (template);
+ gtk_label_set_label (GTK_LABEL (label), str);
+ g_free (str);
+}
+
+static void
+create_page_one (GSLockPlug *plug)
+{
+ GtkWidget *align;
+ GtkWidget *vbox;
+ GtkWidget *vbox2;
+ GtkWidget *hbox;
+ char *str;
+
+ gs_profile_start ("page one");
+
+ align = gtk_alignment_new (0.5, 0.5, 1, 1);
+ gtk_notebook_append_page (GTK_NOTEBOOK (plug->priv->notebook), align, NULL);
+
+ vbox = gtk_vbox_new (FALSE, 12);
+ gtk_container_add (GTK_CONTAINER (align), vbox);
+
+ plug->priv->auth_face_image = gtk_image_new ();
+ gtk_box_pack_start (GTK_BOX (vbox), plug->priv->auth_face_image, TRUE, TRUE, 0);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_face_image), 0.5, 1.0);
+
+ vbox2 = gtk_vbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), vbox2, FALSE, FALSE, 0);
+
+ str = g_strdup ("<span size=\"x-large\">%R</span>");
+ plug->priv->auth_realname_label = gtk_label_new (str);
+ g_free (str);
+ expand_string_for_label (plug->priv->auth_realname_label);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_realname_label), 0.5, 0.5);
+ gtk_label_set_use_markup (GTK_LABEL (plug->priv->auth_realname_label), TRUE);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_realname_label, FALSE, FALSE, 0);
+
+ /* To translators: This expands to USERNAME on HOSTNAME */
+ str = g_strdup_printf ("<span size=\"small\">%s</span>", _("%U on %h"));
+ plug->priv->auth_username_label = gtk_label_new (str);
+ g_free (str);
+ expand_string_for_label (plug->priv->auth_username_label);
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_username_label), 0.5, 0.5);
+ gtk_label_set_use_markup (GTK_LABEL (plug->priv->auth_username_label), TRUE);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_username_label, FALSE, FALSE, 0);
+
+ vbox2 = gtk_vbox_new (FALSE, 0);
+ gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
+
+ hbox = gtk_hbox_new (FALSE, 6);
+ gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
+
+ plug->priv->auth_prompt_label = gtk_label_new_with_mnemonic (_("_Password:"));
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_prompt_label), 0, 0.5);
+ gtk_box_pack_start (GTK_BOX (hbox), plug->priv->auth_prompt_label, FALSE, FALSE, 0);
+
+ plug->priv->auth_prompt_entry = gtk_entry_new ();
+ gtk_box_pack_start (GTK_BOX (hbox), plug->priv->auth_prompt_entry, TRUE, TRUE, 0);
+
+ gtk_label_set_mnemonic_widget (GTK_LABEL (plug->priv->auth_prompt_label),
+ plug->priv->auth_prompt_entry);
+
+ plug->priv->auth_capslock_label = gtk_label_new ("");
+ gtk_misc_set_alignment (GTK_MISC (plug->priv->auth_capslock_label), 0.5, 0.5);
+ gtk_box_pack_start (GTK_BOX (vbox2), plug->priv->auth_capslock_label, FALSE, FALSE, 0);
+
+ /* Status text */
+
+ plug->priv->auth_message_label = gtk_label_new (NULL);
+ gtk_box_pack_start (GTK_BOX (vbox), plug->priv->auth_message_label,
+ FALSE, FALSE, 0);
+ /* Buttons */
+ plug->priv->auth_action_area = gtk_hbutton_box_new ();
+
+ gtk_button_box_set_layout (GTK_BUTTON_BOX (plug->priv->auth_action_area),
+ GTK_BUTTONBOX_END);
+
+ gtk_box_pack_end (GTK_BOX (vbox), plug->priv->auth_action_area,
+ FALSE, TRUE, 0);
+ gtk_widget_show (plug->priv->auth_action_area);
+
+ create_page_one_buttons (plug);
+
+ gs_profile_end ("page one");
+}
+
+static void
+unlock_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_OK);
+}
+
+static void
+cancel_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+}
+
+static void
+switch_user_button_clicked (GtkButton *button,
+ GSLockPlug *plug)
+{
+
+ remove_response_idle (plug);
+
+ gs_lock_plug_set_sensitive (plug, FALSE);
+
+ plug->priv->response_idle_id = g_timeout_add (2000,
+ (GSourceFunc)response_cancel_idle_cb,
+ plug);
+
+ gs_lock_plug_set_busy (plug);
+ do_user_switch (plug);
+}
+
+static char *
+get_dialog_theme_name (GSLockPlug *plug)
+{
+ char *name;
+ MateConfClient *client;
+
+ client = mateconf_client_get_default ();
+ name = mateconf_client_get_string (client, KEY_LOCK_DIALOG_THEME, NULL);
+ g_object_unref (client);
+
+ return name;
+}
+
+static gboolean
+load_theme (GSLockPlug *plug)
+{
+ char *theme;
+ char *filename;
+ char *gtkbuilder;
+ char *rc;
+ GtkBuilder *builder;
+ GtkWidget *lock_dialog;
+ GError *error=NULL;
+
+ theme = get_dialog_theme_name (plug);
+ if (theme == NULL)
+ {
+ return FALSE;
+ }
+
+ filename = g_strdup_printf ("lock-dialog-%s.ui", theme);
+ gtkbuilder = g_build_filename (GTKBUILDERDIR, filename, NULL);
+ g_free (filename);
+ if (! g_file_test (gtkbuilder, G_FILE_TEST_IS_REGULAR))
+ {
+ g_free (gtkbuilder);
+ g_free (theme);
+ return FALSE;
+ }
+
+ filename = g_strdup_printf ("lock-dialog-%s.gtkrc", theme);
+ g_free (theme);
+
+ rc = g_build_filename (GTKBUILDERDIR, filename, NULL);
+ g_free (filename);
+ if (g_file_test (rc, G_FILE_TEST_IS_REGULAR))
+ {
+ gtk_rc_parse (rc);
+ }
+ g_free (rc);
+
+ builder = gtk_builder_new();
+
+ if (!gtk_builder_add_from_file (builder,gtkbuilder,&error))
+ {
+ g_warning ("Couldn't load builder file '%s': %s", gtkbuilder, error->message);
+ g_error_free(error);
+ g_free (gtkbuilder);
+ return FALSE;
+ }
+ g_free (gtkbuilder);
+
+ lock_dialog = GTK_WIDGET (gtk_builder_get_object(builder, "lock-dialog"));
+ gtk_container_add (GTK_CONTAINER (plug), lock_dialog);
+
+ plug->priv->vbox = NULL;
+ plug->priv->notebook = GTK_WIDGET (gtk_builder_get_object(builder, "notebook"));
+
+ plug->priv->auth_face_image = GTK_WIDGET (gtk_builder_get_object(builder, "auth-face-image"));
+ plug->priv->auth_action_area = GTK_WIDGET (gtk_builder_get_object(builder, "auth-action-area"));
+ plug->priv->auth_realname_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-realname-label"));
+ plug->priv->auth_username_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-username-label"));
+ plug->priv->auth_prompt_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-label"));
+ plug->priv->auth_prompt_entry = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-entry"));
+ plug->priv->auth_prompt_box = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-box"));
+ plug->priv->auth_capslock_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-capslock-label"));
+ plug->priv->auth_message_label = GTK_WIDGET (gtk_builder_get_object(builder, "auth-status-label"));
+ plug->priv->auth_unlock_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-unlock-button"));
+ plug->priv->auth_cancel_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-cancel-button"));
+ plug->priv->auth_logout_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-logout-button"));
+ plug->priv->auth_switch_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-switch-button"));
+ plug->priv->auth_note_button = GTK_WIDGET (gtk_builder_get_object(builder, "auth-note-button"));
+ plug->priv->note_tab = GTK_WIDGET (gtk_builder_get_object(builder, "note-tab"));
+ plug->priv->note_tab_label = GTK_WIDGET (gtk_builder_get_object(builder, "note-tab-label"));
+ plug->priv->note_ok_button = GTK_WIDGET (gtk_builder_get_object(builder, "note-ok-button"));
+ plug->priv->note_text_view = GTK_WIDGET (gtk_builder_get_object(builder, "note-text-view"));
+ plug->priv->note_cancel_button = GTK_WIDGET (gtk_builder_get_object(builder, "note-cancel-button"));
+
+ /* Placeholder for the keyboard indicator */
+ plug->priv->auth_prompt_kbd_layout_indicator = GTK_WIDGET (gtk_builder_get_object(builder, "auth-prompt-kbd-layout-indicator"));
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_logout_button, TRUE);
+ }
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_switch_button, TRUE);
+ }
+ if (plug->priv->auth_note_button != NULL)
+ {
+ gtk_widget_set_no_show_all (plug->priv->auth_note_button, TRUE);
+ }
+
+ gtk_widget_show_all (lock_dialog);
+
+ plug->priv->status_message_label = GTK_WIDGET (gtk_builder_get_object(builder, "status-message-label"));
+
+ return TRUE;
+}
+
+static int
+delete_handler (GSLockPlug *plug,
+ GdkEventAny *event,
+ gpointer data)
+{
+ gs_lock_plug_response (plug, GS_LOCK_PLUG_RESPONSE_CANCEL);
+
+ return TRUE; /* Do not destroy */
+}
+
+static void
+on_note_text_buffer_changed (GtkTextBuffer *buffer,
+ GSLockPlug *plug)
+{
+ int len;
+
+ len = gtk_text_buffer_get_char_count (buffer);
+ if (len > NOTE_BUFFER_MAX_CHARS)
+ {
+ gtk_widget_set_sensitive (plug->priv->note_text_view, FALSE);
+ }
+}
+
+static void
+gs_lock_plug_init (GSLockPlug *plug)
+{
+ gs_profile_start (NULL);
+
+ plug->priv = GS_LOCK_PLUG_GET_PRIVATE (plug);
+
+ clear_clipboards (plug);
+
+#ifdef WITH_LIBMATENOTIFY
+ plug->priv->leave_note_enabled = TRUE;
+#else
+ plug->priv->leave_note_enabled = FALSE;
+#endif
+
+ if (! load_theme (plug))
+ {
+ gs_debug ("Unable to load theme!");
+
+ plug->priv->vbox = gtk_vbox_new (FALSE, 0);
+
+ gtk_container_add (GTK_CONTAINER (plug), plug->priv->vbox);
+
+ /* Notebook */
+
+ plug->priv->notebook = gtk_notebook_new ();
+ gtk_notebook_set_show_tabs (GTK_NOTEBOOK (plug->priv->notebook), FALSE);
+ gtk_notebook_set_show_border (GTK_NOTEBOOK (plug->priv->notebook), FALSE);
+ gtk_box_pack_start (GTK_BOX (plug->priv->vbox), plug->priv->notebook, TRUE, TRUE, 0);
+
+ /* Page 1 */
+
+ create_page_one (plug);
+
+ gtk_widget_show_all (plug->priv->vbox);
+ }
+
+ if (plug->priv->note_text_view != NULL)
+ {
+ GtkTextBuffer *buffer;
+ buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (plug->priv->note_text_view));
+ g_signal_connect (buffer, "changed", G_CALLBACK (on_note_text_buffer_changed), plug);
+ }
+
+ /* Layout indicator */
+#ifdef WITH_KBD_LAYOUT_INDICATOR
+ if (plug->priv->auth_prompt_kbd_layout_indicator != NULL)
+ {
+ XklEngine *engine;
+
+ engine = xkl_engine_get_instance (GDK_DISPLAY ());
+ if (xkl_engine_get_num_groups (engine) > 1)
+ {
+ GtkWidget *layout_indicator;
+
+ layout_indicator = matekbd_indicator_new ();
+ matekbd_indicator_set_parent_tooltips (MATEKBD_INDICATOR (layout_indicator), TRUE);
+ gtk_box_pack_start (GTK_BOX (plug->priv->auth_prompt_kbd_layout_indicator),
+ layout_indicator,
+ FALSE,
+ FALSE,
+ 6);
+
+ gtk_widget_show_all (layout_indicator);
+ gtk_widget_show (plug->priv->auth_prompt_kbd_layout_indicator);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_prompt_kbd_layout_indicator);
+ }
+
+ g_object_unref (engine);
+ }
+#endif
+
+ if (plug->priv->auth_note_button != NULL)
+ {
+ if (plug->priv->leave_note_enabled)
+ {
+ gtk_widget_show_all (plug->priv->auth_note_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_note_button);
+ }
+ }
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ if (plug->priv->switch_enabled)
+ {
+ gtk_widget_show_all (plug->priv->auth_switch_button);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->auth_switch_button);
+ }
+ }
+
+ gtk_widget_grab_default (plug->priv->auth_unlock_button);
+
+ if (plug->priv->auth_username_label != NULL)
+ {
+ expand_string_for_label (plug->priv->auth_username_label);
+ }
+
+ if (plug->priv->auth_realname_label != NULL)
+ {
+ expand_string_for_label (plug->priv->auth_realname_label);
+ }
+
+ if (! plug->priv->logout_enabled || ! plug->priv->logout_command)
+ {
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ gtk_widget_hide (plug->priv->auth_logout_button);
+ }
+ }
+
+ plug->priv->timeout = DIALOG_TIMEOUT_MSEC;
+
+ g_signal_connect (plug, "key_press_event",
+ G_CALLBACK (entry_key_press), plug);
+
+ /* button press handler used to inhibit popup menu */
+ g_signal_connect (plug->priv->auth_prompt_entry, "button_press_event",
+ G_CALLBACK (entry_button_press), NULL);
+ gtk_entry_set_activates_default (GTK_ENTRY (plug->priv->auth_prompt_entry), TRUE);
+ gtk_entry_set_visibility (GTK_ENTRY (plug->priv->auth_prompt_entry), FALSE);
+
+ g_signal_connect (plug->priv->auth_unlock_button, "clicked",
+ G_CALLBACK (unlock_button_clicked), plug);
+
+ g_signal_connect (plug->priv->auth_cancel_button, "clicked",
+ G_CALLBACK (cancel_button_clicked), plug);
+
+ if (plug->priv->status_message_label)
+ {
+ if (plug->priv->status_message)
+ {
+ gtk_label_set_text (GTK_LABEL (plug->priv->status_message_label),
+ plug->priv->status_message);
+ }
+ else
+ {
+ gtk_widget_hide (plug->priv->status_message_label);
+ }
+ }
+
+ if (plug->priv->auth_switch_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_switch_button, "clicked",
+ G_CALLBACK (switch_user_button_clicked), plug);
+ }
+
+ if (plug->priv->auth_note_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_note_button, "clicked",
+ G_CALLBACK (take_note), plug);
+ g_signal_connect (plug->priv->note_ok_button, "clicked",
+ G_CALLBACK (submit_note), plug);
+ g_signal_connect (plug->priv->note_cancel_button, "clicked",
+ G_CALLBACK (cancel_note), plug);
+ }
+
+ if (plug->priv->note_tab_label != NULL)
+ {
+ expand_string_for_label (plug->priv->note_tab_label);
+ }
+
+ if (plug->priv->auth_logout_button != NULL)
+ {
+ g_signal_connect (plug->priv->auth_logout_button, "clicked",
+ G_CALLBACK (logout_button_clicked), plug);
+ }
+
+ g_signal_connect (plug, "delete_event", G_CALLBACK (delete_handler), NULL);
+
+ gs_profile_end (NULL);
+}
+
+static void
+gs_lock_plug_finalize (GObject *object)
+{
+ GSLockPlug *plug;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_LOCK_PLUG (object));
+
+ plug = GS_LOCK_PLUG (object);
+
+ g_return_if_fail (plug->priv != NULL);
+
+ g_free (plug->priv->logout_command);
+
+ remove_response_idle (plug);
+ remove_cancel_timeout (plug);
+
+ G_OBJECT_CLASS (gs_lock_plug_parent_class)->finalize (object);
+}
+
+GtkWidget *
+gs_lock_plug_new (void)
+{
+ GtkWidget *result;
+
+ result = g_object_new (GS_TYPE_LOCK_PLUG, NULL);
+
+ gtk_window_set_focus_on_map (GTK_WINDOW (result), TRUE);
+
+ return result;
+}
diff --git a/src/gs-lock-plug.h b/src/gs-lock-plug.h
new file mode 100644
index 0000000..3217bbf
--- /dev/null
+++ b/src/gs-lock-plug.h
@@ -0,0 +1,82 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_LOCK_PLUG_H
+#define __GS_LOCK_PLUG_H
+
+G_BEGIN_DECLS
+
+typedef enum
+{
+ GS_LOCK_PLUG_RESPONSE_NONE = -1,
+ GS_LOCK_PLUG_RESPONSE_OK = -2,
+ GS_LOCK_PLUG_RESPONSE_CANCEL = -3
+} GSPlugResponseType;
+
+#define GS_TYPE_LOCK_PLUG (gs_lock_plug_get_type ())
+#define GS_LOCK_PLUG(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_LOCK_PLUG, GSLockPlug))
+#define GS_LOCK_PLUG_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_LOCK_PLUG, GSLockPlugClass))
+#define GS_IS_LOCK_PLUG(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_LOCK_PLUG))
+#define GS_IS_LOCK_PLUG_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_LOCK_PLUG))
+#define GS_LOCK_PLUG_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_LOCK_PLUG, GSLockPlugClass))
+
+typedef struct GSLockPlugPrivate GSLockPlugPrivate;
+
+typedef struct
+{
+ GtkPlug parent;
+
+ GSLockPlugPrivate *priv;
+} GSLockPlug;
+
+typedef struct
+{
+ GtkPlugClass parent_class;
+
+ void (* response) (GSLockPlug *plug, gint response_id);
+
+ /* Keybinding signals */
+ void (* close) (GSLockPlug *plug);
+
+} GSLockPlugClass;
+
+GType gs_lock_plug_get_type (void);
+GtkWidget * gs_lock_plug_new (void);
+
+int gs_lock_plug_run (GSLockPlug *plug);
+void gs_lock_plug_set_sensitive (GSLockPlug *plug,
+ gboolean sensitive);
+void gs_lock_plug_enable_prompt (GSLockPlug *plug,
+ const char *message,
+ gboolean visible);
+void gs_lock_plug_disable_prompt (GSLockPlug *plug);
+void gs_lock_plug_set_busy (GSLockPlug *plug);
+void gs_lock_plug_set_ready (GSLockPlug *plug);
+
+void gs_lock_plug_get_text (GSLockPlug *plug,
+ char **text);
+void gs_lock_plug_show_message (GSLockPlug *plug,
+ const char *message);
+
+G_END_DECLS
+
+#endif /* __GS_LOCK_PLUG_H */
diff --git a/src/gs-lock-plug.h.orig b/src/gs-lock-plug.h.orig
new file mode 100644
index 0000000..cc71f21
--- /dev/null
+++ b/src/gs-lock-plug.h.orig
@@ -0,0 +1,82 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2005 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#ifndef __GS_LOCK_PLUG_H
+#define __GS_LOCK_PLUG_H
+
+G_BEGIN_DECLS
+
+typedef enum
+{
+ GS_LOCK_PLUG_RESPONSE_NONE = -1,
+ GS_LOCK_PLUG_RESPONSE_OK = -2,
+ GS_LOCK_PLUG_RESPONSE_CANCEL = -3
+} GSPlugResponseType;
+
+#define GS_TYPE_LOCK_PLUG (gs_lock_plug_get_type ())
+#define GS_LOCK_PLUG(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_LOCK_PLUG, GSLockPlug))
+#define GS_LOCK_PLUG_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), GS_TYPE_LOCK_PLUG, GSLockPlugClass))
+#define GS_IS_LOCK_PLUG(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_LOCK_PLUG))
+#define GS_IS_LOCK_PLUG_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GS_TYPE_LOCK_PLUG))
+#define GS_LOCK_PLUG_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GS_TYPE_LOCK_PLUG, GSLockPlugClass))
+
+typedef struct GSLockPlugPrivate GSLockPlugPrivate;
+
+typedef struct
+{
+ GtkPlug parent;
+
+ GSLockPlugPrivate *priv;
+} GSLockPlug;
+
+typedef struct
+{
+ GtkPlugClass parent_class;
+
+ void (* response) (GSLockPlug *plug, gint response_id);
+
+ /* Keybinding signals */
+ void (* close) (GSLockPlug *plug);
+
+} GSLockPlugClass;
+
+GType gs_lock_plug_get_type (void);
+GtkWidget * gs_lock_plug_new (void);
+
+int gs_lock_plug_run (GSLockPlug *plug);
+void gs_lock_plug_set_sensitive (GSLockPlug *plug,
+ gboolean sensitive);
+void gs_lock_plug_enable_prompt (GSLockPlug *plug,
+ const char *message,
+ gboolean visible);
+void gs_lock_plug_disable_prompt (GSLockPlug *plug);
+void gs_lock_plug_set_busy (GSLockPlug *plug);
+void gs_lock_plug_set_ready (GSLockPlug *plug);
+
+void gs_lock_plug_get_text (GSLockPlug *plug,
+ char **text);
+void gs_lock_plug_show_message (GSLockPlug *plug,
+ const char *message);
+
+G_END_DECLS
+
+#endif /* __GS_LOCK_PLUG_H */
diff --git a/src/gs-manager.c b/src/gs-manager.c
new file mode 100644
index 0000000..3deca22
--- /dev/null
+++ b/src/gs-manager.c
@@ -0,0 +1,2030 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2008 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <time.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+
+#include <mateconf/mateconf-engine.h>
+#include <mateconf/mateconf-client.h>
+
+#define MATE_DESKTOP_USE_UNSTABLE_API
+#include <libmateui/mate-bg.h>
+
+#include "gs-prefs.h" /* for GSSaverMode */
+
+#include "gs-manager.h"
+#include "gs-window.h"
+#include "gs-theme-manager.h"
+#include "gs-job.h"
+#include "gs-grab.h"
+#include "gs-fade.h"
+#include "gs-debug.h"
+
+static void gs_manager_class_init (GSManagerClass *klass);
+static void gs_manager_init (GSManager *manager);
+static void gs_manager_finalize (GObject *object);
+
+#define GS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_MANAGER, GSManagerPrivate))
+
+struct GSManagerPrivate
+{
+ GSList *windows;
+ GHashTable *jobs;
+
+ GSThemeManager *theme_manager;
+ MateConfClient *client;
+ MateBG *bg;
+ guint bg_notify_id;
+
+ /* Policy */
+ glong lock_timeout;
+ glong cycle_timeout;
+ glong logout_timeout;
+
+ guint lock_enabled : 1;
+ guint logout_enabled : 1;
+ guint keyboard_enabled : 1;
+ guint user_switch_enabled : 1;
+ guint throttled : 1;
+
+ char *logout_command;
+ char *keyboard_command;
+
+ char *status_message;
+
+ /* State */
+ guint active : 1;
+ guint lock_active : 1;
+
+ guint fading : 1;
+ guint dialog_up : 1;
+
+ time_t activate_time;
+
+ guint lock_timeout_id;
+ guint cycle_timeout_id;
+
+ GSList *themes;
+ GSSaverMode saver_mode;
+ GSGrab *grab;
+ GSFade *fade;
+ guint unfade_idle_id;
+};
+
+enum
+{
+ ACTIVATED,
+ DEACTIVATED,
+ AUTH_REQUEST_BEGIN,
+ AUTH_REQUEST_END,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_LOCK_ENABLED,
+ PROP_LOGOUT_ENABLED,
+ PROP_USER_SWITCH_ENABLED,
+ PROP_KEYBOARD_ENABLED,
+ PROP_LOCK_TIMEOUT,
+ PROP_CYCLE_TIMEOUT,
+ PROP_LOGOUT_TIMEOUT,
+ PROP_LOGOUT_COMMAND,
+ PROP_KEYBOARD_COMMAND,
+ PROP_STATUS_MESSAGE,
+ PROP_ACTIVE,
+ PROP_THROTTLED,
+};
+
+#define FADE_TIMEOUT 1000
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSManager, gs_manager, G_TYPE_OBJECT)
+
+static void
+manager_add_job_for_window (GSManager *manager,
+ GSWindow *window,
+ GSJob *job)
+{
+ if (manager->priv->jobs == NULL)
+ {
+ return;
+ }
+
+ g_hash_table_insert (manager->priv->jobs, window, job);
+}
+
+static const char *
+select_theme (GSManager *manager)
+{
+ const char *theme = NULL;
+
+ g_return_val_if_fail (manager != NULL, NULL);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), NULL);
+
+ if (manager->priv->saver_mode == GS_MODE_BLANK_ONLY)
+ {
+ return NULL;
+ }
+
+ if (manager->priv->themes)
+ {
+ int number = 0;
+
+ if (manager->priv->saver_mode == GS_MODE_RANDOM)
+ {
+ g_random_set_seed (time (NULL));
+ number = g_random_int_range (0, g_slist_length (manager->priv->themes));
+ }
+ theme = g_slist_nth_data (manager->priv->themes, number);
+ }
+
+ return theme;
+}
+
+static GSJob *
+lookup_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ if (manager->priv->jobs == NULL)
+ {
+ return NULL;
+ }
+
+ job = g_hash_table_lookup (manager->priv->jobs, window);
+
+ return job;
+}
+
+static void
+manager_maybe_stop_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ job = lookup_job_for_window (manager, window);
+
+ if (job == NULL)
+ {
+ gs_debug ("Job not found for window");
+ return;
+ }
+
+ gs_job_stop (job);
+}
+
+static void
+manager_maybe_start_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ job = lookup_job_for_window (manager, window);
+
+ if (job == NULL)
+ {
+ gs_debug ("Job not found for window");
+ return;
+ }
+
+ if (! manager->priv->dialog_up)
+ {
+ if (! manager->priv->throttled)
+ {
+ if (! gs_job_is_running (job))
+ {
+ if (! gs_window_is_obscured (window))
+ {
+ gs_debug ("Starting job for window");
+ gs_job_start (job);
+ }
+ else
+ {
+ gs_debug ("Window is obscured deferring start of job");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because job is running");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because throttled");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because dialog is up");
+ }
+}
+
+static void
+manager_select_theme_for_job (GSManager *manager,
+ GSJob *job)
+{
+ const char *theme;
+
+ theme = select_theme (manager);
+
+ if (theme != NULL)
+ {
+ GSThemeInfo *info;
+ const char *command;
+
+ command = NULL;
+
+ info = gs_theme_manager_lookup_theme_info (manager->priv->theme_manager, theme);
+ if (info != NULL)
+ {
+ command = gs_theme_info_get_exec (info);
+ }
+ else
+ {
+ gs_debug ("Could not find information for theme: %s",
+ theme);
+ }
+
+ gs_job_set_command (job, command);
+
+
+ if (info != NULL)
+ {
+ gs_theme_info_unref (info);
+ }
+ }
+ else
+ {
+ gs_job_set_command (job, NULL);
+ }
+}
+
+static void
+cycle_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ gs_job_stop (job);
+ manager_select_theme_for_job (manager, job);
+ manager_maybe_start_job_for_window (manager, window);
+}
+
+static void
+manager_cycle_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) cycle_job, manager);
+ }
+}
+
+static void
+throttle_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ if (manager->priv->throttled)
+ {
+ gs_job_stop (job);
+ }
+ else
+ {
+ manager_maybe_start_job_for_window (manager, window);
+ }
+}
+
+static void
+manager_throttle_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) throttle_job, manager);
+ }
+}
+
+static void
+resume_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ if (gs_job_is_running (job))
+ {
+ gs_job_suspend (job, FALSE);
+ }
+ else
+ {
+ manager_maybe_start_job_for_window (manager, window);
+ }
+}
+
+static void
+manager_resume_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) resume_job, manager);
+ }
+}
+
+static void
+suspend_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ gs_job_suspend (job, TRUE);
+}
+
+static void
+manager_suspend_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) suspend_job, manager);
+ }
+}
+
+static void
+manager_stop_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_destroy (manager->priv->jobs);
+
+ }
+ manager->priv->jobs = NULL;
+}
+
+void
+gs_manager_set_mode (GSManager *manager,
+ GSSaverMode mode)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ manager->priv->saver_mode = mode;
+}
+
+static void
+free_themes (GSManager *manager)
+{
+ if (manager->priv->themes)
+ {
+ g_slist_foreach (manager->priv->themes, (GFunc)g_free, NULL);
+ g_slist_free (manager->priv->themes);
+ }
+}
+
+void
+gs_manager_set_themes (GSManager *manager,
+ GSList *themes)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ free_themes (manager);
+ manager->priv->themes = NULL;
+
+ for (l = themes; l; l = l->next)
+ {
+ manager->priv->themes = g_slist_append (manager->priv->themes, g_strdup (l->data));
+ }
+}
+
+void
+gs_manager_set_throttled (GSManager *manager,
+ gboolean throttled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->throttled != throttled)
+ {
+ GSList *l;
+
+ manager->priv->throttled = throttled;
+
+ if (! manager->priv->dialog_up)
+ {
+
+ manager_throttle_jobs (manager);
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_clear (l->data);
+ }
+ }
+ }
+}
+
+void
+gs_manager_get_lock_active (GSManager *manager,
+ gboolean *lock_active)
+{
+ if (lock_active != NULL)
+ {
+ *lock_active = FALSE;
+ }
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ *lock_active = manager->priv->lock_active;
+}
+
+void
+gs_manager_set_lock_active (GSManager *manager,
+ gboolean lock_active)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ gs_debug ("Setting lock active: %d", lock_active);
+
+ if (manager->priv->lock_active != lock_active)
+ {
+ GSList *l;
+
+ manager->priv->lock_active = lock_active;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_lock_enabled (l->data, lock_active);
+ }
+ }
+}
+
+void
+gs_manager_get_lock_enabled (GSManager *manager,
+ gboolean *lock_enabled)
+{
+ if (lock_enabled != NULL)
+ {
+ *lock_enabled = FALSE;
+ }
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ *lock_enabled = manager->priv->lock_enabled;
+}
+
+void
+gs_manager_set_lock_enabled (GSManager *manager,
+ gboolean lock_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->lock_enabled != lock_enabled)
+ {
+ manager->priv->lock_enabled = lock_enabled;
+ }
+}
+
+void
+gs_manager_set_logout_enabled (GSManager *manager,
+ gboolean logout_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->logout_enabled != logout_enabled)
+ {
+ GSList *l;
+
+ manager->priv->logout_enabled = logout_enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_enabled (l->data, logout_enabled);
+ }
+ }
+}
+
+void
+gs_manager_set_keyboard_enabled (GSManager *manager,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->keyboard_enabled != enabled)
+ {
+ GSList *l;
+
+ manager->priv->keyboard_enabled = enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_keyboard_enabled (l->data, enabled);
+ }
+ }
+}
+
+void
+gs_manager_set_user_switch_enabled (GSManager *manager,
+ gboolean user_switch_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->user_switch_enabled != user_switch_enabled)
+ {
+ GSList *l;
+
+ manager->priv->user_switch_enabled = user_switch_enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_user_switch_enabled (l->data, user_switch_enabled);
+ }
+ }
+}
+
+static gboolean
+activate_lock_timeout (GSManager *manager)
+{
+ if (manager->priv->lock_enabled)
+ {
+ gs_manager_set_lock_active (manager, TRUE);
+ }
+
+ manager->priv->lock_timeout_id = 0;
+
+ return FALSE;
+}
+
+static void
+remove_lock_timer (GSManager *manager)
+{
+ if (manager->priv->lock_timeout_id != 0)
+ {
+ g_source_remove (manager->priv->lock_timeout_id);
+ manager->priv->lock_timeout_id = 0;
+ }
+}
+
+static void
+add_lock_timer (GSManager *manager,
+ glong timeout)
+{
+ manager->priv->lock_timeout_id = g_timeout_add (timeout,
+ (GSourceFunc)activate_lock_timeout,
+ manager);
+}
+
+void
+gs_manager_set_lock_timeout (GSManager *manager,
+ glong lock_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->lock_timeout != lock_timeout)
+ {
+
+ manager->priv->lock_timeout = lock_timeout;
+
+ if (manager->priv->active
+ && ! manager->priv->lock_active
+ && (lock_timeout >= 0))
+ {
+
+ glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;
+
+ remove_lock_timer (manager);
+
+ if (elapsed >= lock_timeout)
+ {
+ activate_lock_timeout (manager);
+ }
+ else
+ {
+ add_lock_timer (manager, lock_timeout - elapsed);
+ }
+ }
+ }
+}
+
+void
+gs_manager_set_logout_timeout (GSManager *manager,
+ glong logout_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->logout_timeout != logout_timeout)
+ {
+ GSList *l;
+
+ manager->priv->logout_timeout = logout_timeout;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_timeout (l->data, logout_timeout);
+ }
+ }
+}
+
+void
+gs_manager_set_logout_command (GSManager *manager,
+ const char *command)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->logout_command);
+
+ if (command)
+ {
+ manager->priv->logout_command = g_strdup (command);
+ }
+ else
+ {
+ manager->priv->logout_command = NULL;
+ }
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_command (l->data, manager->priv->logout_command);
+ }
+}
+
+void
+gs_manager_set_keyboard_command (GSManager *manager,
+ const char *command)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->keyboard_command);
+
+ if (command)
+ {
+ manager->priv->keyboard_command = g_strdup (command);
+ }
+ else
+ {
+ manager->priv->keyboard_command = NULL;
+ }
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_keyboard_command (l->data, manager->priv->keyboard_command);
+ }
+}
+
+void
+gs_manager_set_status_message (GSManager *manager,
+ const char *status_message)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->status_message);
+
+ manager->priv->status_message = g_strdup (status_message);
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_status_message (l->data, manager->priv->status_message);
+ }
+}
+
+gboolean
+gs_manager_cycle (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ gs_debug ("cycling jobs");
+
+ if (! manager->priv->active)
+ {
+ return FALSE;
+ }
+
+ if (manager->priv->dialog_up)
+ {
+ return FALSE;
+ }
+
+ if (manager->priv->throttled)
+ {
+ return FALSE;
+ }
+
+ manager_cycle_jobs (manager);
+
+ return TRUE;
+}
+
+static gboolean
+cycle_timeout (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ if (! manager->priv->dialog_up)
+ {
+ gs_manager_cycle (manager);
+ }
+
+ return TRUE;
+}
+
+static void
+remove_cycle_timer (GSManager *manager)
+{
+ if (manager->priv->cycle_timeout_id != 0)
+ {
+ g_source_remove (manager->priv->cycle_timeout_id);
+ manager->priv->cycle_timeout_id = 0;
+ }
+}
+
+static void
+add_cycle_timer (GSManager *manager,
+ glong timeout)
+{
+ manager->priv->cycle_timeout_id = g_timeout_add (timeout,
+ (GSourceFunc)cycle_timeout,
+ manager);
+}
+
+void
+gs_manager_set_cycle_timeout (GSManager *manager,
+ glong cycle_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->cycle_timeout != cycle_timeout)
+ {
+
+ manager->priv->cycle_timeout = cycle_timeout;
+
+ if (manager->priv->active && (cycle_timeout >= 0))
+ {
+ glong timeout;
+ glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;
+
+ remove_cycle_timer (manager);
+
+ if (elapsed >= cycle_timeout)
+ {
+ timeout = 0;
+ }
+ else
+ {
+ timeout = cycle_timeout - elapsed;
+ }
+
+ add_cycle_timer (manager, timeout);
+
+ }
+ }
+}
+
+static void
+gs_manager_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSManager *self;
+
+ self = GS_MANAGER (object);
+
+ switch (prop_id)
+ {
+ case PROP_THROTTLED:
+ gs_manager_set_throttled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOCK_ENABLED:
+ gs_manager_set_lock_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOCK_TIMEOUT:
+ gs_manager_set_lock_timeout (self, g_value_get_long (value));
+ break;
+ case PROP_LOGOUT_ENABLED:
+ gs_manager_set_logout_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_KEYBOARD_ENABLED:
+ gs_manager_set_keyboard_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_USER_SWITCH_ENABLED:
+ gs_manager_set_user_switch_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOGOUT_TIMEOUT:
+ gs_manager_set_logout_timeout (self, g_value_get_long (value));
+ break;
+ case PROP_LOGOUT_COMMAND:
+ gs_manager_set_logout_command (self, g_value_get_string (value));
+ break;
+ case PROP_KEYBOARD_COMMAND:
+ gs_manager_set_keyboard_command (self, g_value_get_string (value));
+ break;
+ case PROP_STATUS_MESSAGE:
+ gs_manager_set_status_message (self, g_value_get_string (value));
+ break;
+ case PROP_CYCLE_TIMEOUT:
+ gs_manager_set_cycle_timeout (self, g_value_get_long (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_manager_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSManager *self;
+
+ self = GS_MANAGER (object);
+
+ switch (prop_id)
+ {
+ case PROP_THROTTLED:
+ g_value_set_boolean (value, self->priv->throttled);
+ break;
+ case PROP_LOCK_ENABLED:
+ g_value_set_boolean (value, self->priv->lock_enabled);
+ break;
+ case PROP_LOCK_TIMEOUT:
+ g_value_set_long (value, self->priv->lock_timeout);
+ break;
+ case PROP_LOGOUT_ENABLED:
+ g_value_set_boolean (value, self->priv->logout_enabled);
+ break;
+ case PROP_KEYBOARD_ENABLED:
+ g_value_set_boolean (value, self->priv->keyboard_enabled);
+ break;
+ case PROP_USER_SWITCH_ENABLED:
+ g_value_set_boolean (value, self->priv->user_switch_enabled);
+ break;
+ case PROP_LOGOUT_TIMEOUT:
+ g_value_set_long (value, self->priv->logout_timeout);
+ break;
+ case PROP_LOGOUT_COMMAND:
+ g_value_set_string (value, self->priv->logout_command);
+ break;
+ case PROP_KEYBOARD_COMMAND:
+ g_value_set_string (value, self->priv->keyboard_command);
+ break;
+ case PROP_STATUS_MESSAGE:
+ g_value_set_string (value, self->priv->status_message);
+ break;
+ case PROP_CYCLE_TIMEOUT:
+ g_value_set_long (value, self->priv->cycle_timeout);
+ break;
+ case PROP_ACTIVE:
+ g_value_set_boolean (value, self->priv->active);
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_manager_class_init (GSManagerClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->finalize = gs_manager_finalize;
+ object_class->get_property = gs_manager_get_property;
+ object_class->set_property = gs_manager_set_property;
+
+ signals [ACTIVATED] =
+ g_signal_new ("activated",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSManagerClass, activated),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [DEACTIVATED] =
+ g_signal_new ("deactivated",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSManagerClass, deactivated),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [AUTH_REQUEST_BEGIN] =
+ g_signal_new ("auth-request-begin",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSManagerClass, auth_request_begin),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+ signals [AUTH_REQUEST_END] =
+ g_signal_new ("auth-request-end",
+ G_TYPE_FROM_CLASS (object_class),
+ G_SIGNAL_RUN_LAST,
+ G_STRUCT_OFFSET (GSManagerClass, auth_request_end),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
+ g_object_class_install_property (object_class,
+ PROP_ACTIVE,
+ g_param_spec_boolean ("active",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READABLE));
+ g_object_class_install_property (object_class,
+ PROP_LOCK_ENABLED,
+ g_param_spec_boolean ("lock-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_LOCK_TIMEOUT,
+ g_param_spec_long ("lock-timeout",
+ NULL,
+ NULL,
+ -1,
+ G_MAXLONG,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_ENABLED,
+ g_param_spec_boolean ("logout-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_USER_SWITCH_ENABLED,
+ g_param_spec_boolean ("user-switch-enabled",
+ NULL,
+ NULL,
+ FALSE,
+ G_PARAM_READWRITE));
+
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_TIMEOUT,
+ g_param_spec_long ("logout-timeout",
+ NULL,
+ NULL,
+ -1,
+ G_MAXLONG,
+ 0,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_LOGOUT_COMMAND,
+ g_param_spec_string ("logout-command",
+ NULL,
+ NULL,
+ NULL,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_CYCLE_TIMEOUT,
+ g_param_spec_long ("cycle-timeout",
+ NULL,
+ NULL,
+ 10000,
+ G_MAXLONG,
+ 300000,
+ G_PARAM_READWRITE));
+ g_object_class_install_property (object_class,
+ PROP_THROTTLED,
+ g_param_spec_boolean ("throttled",
+ NULL,
+ NULL,
+ TRUE,
+ G_PARAM_READWRITE));
+
+ g_type_class_add_private (klass, sizeof (GSManagerPrivate));
+}
+
+static void
+on_bg_changed (MateBG *bg,
+ GSManager *manager)
+{
+ gs_debug ("background changed");
+}
+
+static void
+mateconf_changed_callback (MateConfClient *client,
+ guint cnxn_id,
+ MateConfEntry *entry,
+ GSManager *manager)
+{
+ mate_bg_load_from_preferences (manager->priv->bg,
+ manager->priv->client);
+}
+
+static void
+watch_bg_preferences (GSManager *manager)
+{
+ g_assert (manager->priv->bg_notify_id == 0);
+
+ mateconf_client_add_dir (manager->priv->client,
+ MATE_BG_KEY_DIR,
+ MATECONF_CLIENT_PRELOAD_NONE,
+ NULL);
+ manager->priv->bg_notify_id = mateconf_client_notify_add (manager->priv->client,
+ MATE_BG_KEY_DIR,
+ (MateConfClientNotifyFunc)mateconf_changed_callback,
+ manager,
+ NULL,
+ NULL);
+}
+
+static MateConfClient *
+get_mateconf_client (void)
+{
+ MateConfClient *client;
+ GSList *addresses;
+ GError *error;
+ MateConfEngine *engine;
+
+ client = NULL;
+ addresses = NULL;
+
+ addresses = g_slist_prepend (addresses, "xml:merged:" SYSCONFDIR "/mateconf/mateconf.xml.mandatory");
+ addresses = g_slist_prepend (addresses, "xml:merged:" SYSCONFDIR "/mateconf/mateconf.xml.system");
+ addresses = g_slist_prepend (addresses, "xml:merged:" SYSCONFDIR "/mateconf/mateconf.xml.defaults");
+ addresses = g_slist_reverse (addresses);
+
+ error = NULL;
+ engine = mateconf_engine_get_for_addresses (addresses, &error);
+ if (engine == NULL)
+ {
+ gs_debug ("Unable to get mateconf engine for addresses: %s", error->message);
+ g_error_free (error);
+ }
+ else
+ {
+ client = mateconf_client_get_for_engine (engine);
+ }
+
+ g_slist_free (addresses);
+
+ return client;
+}
+
+static void
+gs_manager_init (GSManager *manager)
+{
+ manager->priv = GS_MANAGER_GET_PRIVATE (manager);
+
+ manager->priv->fade = gs_fade_new ();
+ manager->priv->grab = gs_grab_new ();
+ manager->priv->theme_manager = gs_theme_manager_new ();
+
+ manager->priv->client = get_mateconf_client ();
+ if (manager->priv->client != NULL)
+ {
+ manager->priv->bg = mate_bg_new ();
+
+ g_signal_connect (manager->priv->bg,
+ "changed",
+ G_CALLBACK (on_bg_changed),
+ manager);
+ watch_bg_preferences (manager);
+
+ mate_bg_load_from_preferences (manager->priv->bg, manager->priv->client);
+ }
+}
+
+static void
+remove_timers (GSManager *manager)
+{
+ remove_lock_timer (manager);
+ remove_cycle_timer (manager);
+}
+
+static void
+remove_unfade_idle (GSManager *manager)
+{
+ if (manager->priv->unfade_idle_id > 0)
+ {
+ g_source_remove (manager->priv->unfade_idle_id);
+ manager->priv->unfade_idle_id = 0;
+ }
+}
+
+
+static gboolean
+window_deactivated_idle (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ /* don't deactivate directly but only emit a signal
+ so that we let the parent deactivate */
+ g_signal_emit (manager, signals [DEACTIVATED], 0);
+
+ return FALSE;
+}
+
+static void
+window_deactivated_cb (GSWindow *window,
+ GSManager *manager)
+{
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_idle_add ((GSourceFunc)window_deactivated_idle, manager);
+}
+
+static GSWindow *
+find_window_at_pointer (GSManager *manager)
+{
+ GdkDisplay *display;
+ GdkScreen *screen;
+ int monitor;
+ int x, y;
+ GSWindow *window;
+ int screen_num;
+ GSList *l;
+
+ display = gdk_display_get_default ();
+ gdk_display_get_pointer (display, &screen, &x, &y, NULL);
+ monitor = gdk_screen_get_monitor_at_point (screen, x, y);
+ screen_num = gdk_screen_get_number (screen);
+
+ /* Find the gs-window that is on that screen */
+ window = NULL;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ GSWindow *win = GS_WINDOW (l->data);
+ if (gs_window_get_screen (win) == screen
+ && gs_window_get_monitor (win) == monitor)
+ {
+ window = win;
+ }
+ }
+
+ if (window == NULL)
+ {
+ gs_debug ("WARNING: Could not find the GSWindow for screen %d", screen_num);
+ /* take the first one */
+ window = manager->priv->windows->data;
+ }
+ else
+ {
+ gs_debug ("Requesting unlock for screen %d", screen_num);
+ }
+
+ return window;
+}
+
+void
+gs_manager_show_message (GSManager *manager,
+ const char *summary,
+ const char *body,
+ const char *icon)
+{
+ GSWindow *window;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ /* Find the GSWindow that contains the pointer */
+ window = find_window_at_pointer (manager);
+ gs_window_show_message (window, summary, body, icon);
+
+ gs_manager_request_unlock (manager);
+}
+
+static gboolean
+manager_maybe_grab_window (GSManager *manager,
+ GSWindow *window)
+{
+ GdkDisplay *display;
+ GdkScreen *screen;
+ int monitor;
+ int x, y;
+ gboolean grabbed;
+
+ display = gdk_display_get_default ();
+ gdk_display_get_pointer (display, &screen, &x, &y, NULL);
+ monitor = gdk_screen_get_monitor_at_point (screen, x, y);
+
+ gdk_flush ();
+ grabbed = FALSE;
+ if (gs_window_get_screen (window) == screen
+ && gs_window_get_monitor (window) == monitor)
+ {
+ gs_debug ("Moving grab to %p", window);
+ gs_grab_move_to_window (manager->priv->grab,
+ gs_window_get_gdk_window (window),
+ gs_window_get_screen (window),
+ FALSE);
+ grabbed = TRUE;
+ }
+
+ return grabbed;
+}
+
+static void
+window_grab_broken_cb (GSWindow *window,
+ GdkEventGrabBroken *event,
+ GSManager *manager)
+{
+ gs_debug ("GRAB BROKEN!");
+ if (event->keyboard)
+ {
+ gs_grab_keyboard_reset (manager->priv->grab);
+ }
+ else
+ {
+ gs_grab_mouse_reset (manager->priv->grab);
+ }
+}
+
+static gboolean
+unfade_idle (GSManager *manager)
+{
+ gs_debug ("resetting fade");
+ gs_fade_reset (manager->priv->fade);
+ manager->priv->unfade_idle_id = 0;
+ return FALSE;
+}
+
+
+static void
+add_unfade_idle (GSManager *manager)
+{
+ remove_unfade_idle (manager);
+ manager->priv->unfade_idle_id = g_timeout_add (500, (GSourceFunc)unfade_idle, manager);
+}
+
+static gboolean
+window_map_event_cb (GSWindow *window,
+ GdkEvent *event,
+ GSManager *manager)
+{
+ gs_debug ("Handling window map_event event");
+
+ manager_maybe_grab_window (manager, window);
+
+ manager_maybe_start_job_for_window (manager, window);
+
+ return FALSE;
+}
+
+static void
+window_map_cb (GSWindow *window,
+ GSManager *manager)
+{
+ gs_debug ("Handling window map event");
+}
+
+static void
+window_unmap_cb (GSWindow *window,
+ GSManager *manager)
+{
+ gs_debug ("window unmapped!");
+}
+
+static void
+apply_background_to_window (GSManager *manager,
+ GSWindow *window)
+{
+ GdkPixmap *pixmap;
+ GdkScreen *screen;
+ int width;
+ int height;
+
+ if (manager->priv->bg == NULL)
+ {
+ gs_debug ("No background available");
+ gs_window_set_background_pixmap (window, NULL);
+ }
+
+ screen = gs_window_get_screen (window);
+ width = gdk_screen_get_width (screen);
+ height = gdk_screen_get_height (screen);
+ gs_debug ("Creating pixmap background w:%d h:%d", width, height);
+ pixmap = mate_bg_create_pixmap (manager->priv->bg,
+ gs_window_get_gdk_window (window),
+ width,
+ height,
+ FALSE);
+ gs_window_set_background_pixmap (window, pixmap);
+ g_object_unref (pixmap);
+}
+
+static void
+manager_show_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ apply_background_to_window (manager, window);
+
+ job = gs_job_new_for_widget (gs_window_get_drawing_area (window));
+
+ manager_select_theme_for_job (manager, job);
+ manager_add_job_for_window (manager, window, job);
+
+ manager->priv->activate_time = time (NULL);
+
+ if (manager->priv->lock_timeout >= 0)
+ {
+ remove_lock_timer (manager);
+ add_lock_timer (manager, manager->priv->lock_timeout);
+ }
+
+ if (manager->priv->cycle_timeout >= 10000)
+ {
+ remove_cycle_timer (manager);
+ add_cycle_timer (manager, manager->priv->cycle_timeout);
+ }
+
+ add_unfade_idle (manager);
+
+ /* FIXME: only emit signal once */
+ g_signal_emit (manager, signals [ACTIVATED], 0);
+}
+
+static void
+window_show_cb (GSWindow *window,
+ GSManager *manager)
+{
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+ g_return_if_fail (window != NULL);
+ g_return_if_fail (GS_IS_WINDOW (window));
+
+ gs_debug ("Handling window show");
+ manager_show_window (manager, window);
+}
+
+static void
+maybe_set_window_throttle (GSManager *manager,
+ GSWindow *window,
+ gboolean throttled)
+{
+ if (throttled)
+ {
+ manager_maybe_stop_job_for_window (manager, window);
+ }
+ else
+ {
+ manager_maybe_start_job_for_window (manager, window);
+ }
+}
+
+static void
+window_obscured_cb (GSWindow *window,
+ GParamSpec *pspec,
+ GSManager *manager)
+{
+ gboolean obscured;
+
+ obscured = gs_window_is_obscured (window);
+ gs_debug ("Handling window obscured: %s", obscured ? "obscured" : "unobscured");
+
+ maybe_set_window_throttle (manager, window, obscured);
+
+ if (! obscured)
+ {
+ gs_manager_request_unlock (manager);
+ }
+}
+
+static void
+handle_window_dialog_up (GSManager *manager,
+ GSWindow *window)
+{
+ GSList *l;
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ gs_debug ("Handling dialog up");
+
+ g_signal_emit (manager, signals [AUTH_REQUEST_BEGIN], 0);
+
+ manager->priv->dialog_up = TRUE;
+ /* Make all other windows insensitive so we don't get events */
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ if (l->data != window)
+ {
+ gtk_widget_set_sensitive (GTK_WIDGET (l->data), FALSE);
+ }
+ }
+
+ /* Move keyboard and mouse grabs so dialog can be used */
+ gs_grab_move_to_window (manager->priv->grab,
+ gs_window_get_gdk_window (window),
+ gs_window_get_screen (window),
+ FALSE);
+
+ /* Release the pointer grab while dialog is up so that
+ the dialog can be used. We'll regrab it when the dialog goes down. */
+ gs_grab_release_mouse (manager->priv->grab);
+
+ if (! manager->priv->throttled)
+ {
+ gs_debug ("Suspending jobs");
+
+ manager_suspend_jobs (manager);
+ }
+}
+
+static void
+handle_window_dialog_down (GSManager *manager,
+ GSWindow *window)
+{
+ GSList *l;
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ gs_debug ("Handling dialog down");
+
+ /* Regrab the mouse */
+ gs_grab_move_to_window (manager->priv->grab,
+ gs_window_get_gdk_window (window),
+ gs_window_get_screen (window),
+ FALSE);
+
+ /* Make all windows sensitive so we get events */
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gtk_widget_set_sensitive (GTK_WIDGET (l->data), TRUE);
+ }
+
+ manager->priv->dialog_up = FALSE;
+
+ if (! manager->priv->throttled)
+ {
+ manager_resume_jobs (manager);
+ }
+
+ g_signal_emit (manager, signals [AUTH_REQUEST_END], 0);
+}
+
+static void
+window_dialog_up_changed_cb (GSWindow *window,
+ GParamSpec *pspec,
+ GSManager *manager)
+{
+ gboolean up;
+
+ up = gs_window_is_dialog_up (window);
+ gs_debug ("Handling window dialog up changed: %s", up ? "up" : "down");
+ if (up)
+ {
+ handle_window_dialog_up (manager, window);
+ }
+ else
+ {
+ handle_window_dialog_down (manager, window);
+ }
+}
+
+static gboolean
+window_activity_cb (GSWindow *window,
+ GSManager *manager)
+{
+ gboolean handled;
+
+ handled = gs_manager_request_unlock (manager);
+
+ return handled;
+}
+
+static void
+disconnect_window_signals (GSManager *manager,
+ GSWindow *window)
+{
+ g_signal_handlers_disconnect_by_func (window, window_deactivated_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_activity_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_show_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_map_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_map_event_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_obscured_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_dialog_up_changed_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_unmap_cb, manager);
+ g_signal_handlers_disconnect_by_func (window, window_grab_broken_cb, manager);
+}
+
+static void
+window_destroyed_cb (GtkWindow *window,
+ GSManager *manager)
+{
+ disconnect_window_signals (manager, GS_WINDOW (window));
+}
+
+static void
+connect_window_signals (GSManager *manager,
+ GSWindow *window)
+{
+ g_signal_connect_object (window, "destroy",
+ G_CALLBACK (window_destroyed_cb), manager, 0);
+ g_signal_connect_object (window, "activity",
+ G_CALLBACK (window_activity_cb), manager, 0);
+ g_signal_connect_object (window, "deactivated",
+ G_CALLBACK (window_deactivated_cb), manager, 0);
+ g_signal_connect_object (window, "show",
+ G_CALLBACK (window_show_cb), manager, G_CONNECT_AFTER);
+ g_signal_connect_object (window, "map",
+ G_CALLBACK (window_map_cb), manager, G_CONNECT_AFTER);
+ g_signal_connect_object (window, "map_event",
+ G_CALLBACK (window_map_event_cb), manager, G_CONNECT_AFTER);
+ g_signal_connect_object (window, "notify::obscured",
+ G_CALLBACK (window_obscured_cb), manager, G_CONNECT_AFTER);
+ g_signal_connect_object (window, "notify::dialog-up",
+ G_CALLBACK (window_dialog_up_changed_cb), manager, 0);
+ g_signal_connect_object (window, "unmap",
+ G_CALLBACK (window_unmap_cb), manager, G_CONNECT_AFTER);
+ g_signal_connect_object (window, "grab_broken_event",
+ G_CALLBACK (window_grab_broken_cb), manager, G_CONNECT_AFTER);
+}
+
+static void
+gs_manager_create_window_for_monitor (GSManager *manager,
+ GdkScreen *screen,
+ int monitor)
+{
+ GSWindow *window;
+ GdkRectangle rect;
+
+ gdk_screen_get_monitor_geometry (screen, monitor, &rect);
+
+ gs_debug ("Creating window for monitor %d [%d,%d] (%dx%d)",
+ monitor, rect.x, rect.y, rect.width, rect.height);
+
+ window = gs_window_new (screen, monitor, manager->priv->lock_active);
+
+ gs_window_set_user_switch_enabled (window, manager->priv->user_switch_enabled);
+ gs_window_set_logout_enabled (window, manager->priv->logout_enabled);
+ gs_window_set_logout_timeout (window, manager->priv->logout_timeout);
+ gs_window_set_logout_command (window, manager->priv->logout_command);
+ gs_window_set_keyboard_enabled (window, manager->priv->keyboard_enabled);
+ gs_window_set_keyboard_command (window, manager->priv->keyboard_command);
+ gs_window_set_status_message (window, manager->priv->status_message);
+
+ connect_window_signals (manager, window);
+
+ manager->priv->windows = g_slist_append (manager->priv->windows, window);
+
+ if (manager->priv->active && !manager->priv->fading)
+ {
+ gtk_widget_show (GTK_WIDGET (window));
+ }
+}
+
+static void
+on_screen_monitors_changed (GdkScreen *screen,
+ GSManager *manager)
+{
+ GSList *l;
+ int n_monitors;
+ int n_windows;
+ int i;
+
+ n_monitors = gdk_screen_get_n_monitors (screen);
+ n_windows = g_slist_length (manager->priv->windows);
+
+ gs_debug ("Monitors changed for screen %d: num=%d",
+ gdk_screen_get_number (screen),
+ n_monitors);
+
+ if (n_monitors > n_windows)
+ {
+
+ /* Tear down unlock dialog in case we want to move it
+ * to a new monitor
+ */
+ l = manager->priv->windows;
+ while (l != NULL)
+ {
+ gs_window_cancel_unlock_request (GS_WINDOW (l->data));
+ l = l->next;
+ }
+
+ /* add more windows */
+ for (i = n_windows; i < n_monitors; i++)
+ {
+ gs_manager_create_window_for_monitor (manager, screen, i);
+ }
+
+ /* And put unlock dialog up where ever it's supposed to be
+ */
+ gs_manager_request_unlock (manager);
+ }
+ else
+ {
+
+ gdk_x11_grab_server ();
+
+ /* remove the extra windows */
+ l = manager->priv->windows;
+ while (l != NULL)
+ {
+ GdkScreen *this_screen;
+ int this_monitor;
+ GSList *next = l->next;
+
+ this_screen = gs_window_get_screen (GS_WINDOW (l->data));
+ this_monitor = gs_window_get_monitor (GS_WINDOW (l->data));
+ if (this_screen == screen && this_monitor >= n_monitors)
+ {
+ manager_maybe_stop_job_for_window (manager, GS_WINDOW (l->data));
+ g_hash_table_remove (manager->priv->jobs, l->data);
+ gs_window_destroy (GS_WINDOW (l->data));
+ manager->priv->windows = g_slist_delete_link (manager->priv->windows, l);
+ }
+ l = next;
+ }
+
+ /* make sure there is a lock dialog on a connected monitor,
+ * and that the keyboard is still properly grabbed after all
+ * the windows above got destroyed*/
+ if (n_windows > n_monitors)
+ {
+ gs_manager_request_unlock (manager);
+ }
+
+ gdk_flush ();
+ gdk_x11_ungrab_server ();
+ }
+}
+
+static void
+gs_manager_destroy_windows (GSManager *manager)
+{
+ GdkDisplay *display;
+ GSList *l;
+ int n_screens;
+ int i;
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->windows == NULL)
+ {
+ return;
+ }
+
+ display = gdk_display_get_default ();
+
+ n_screens = gdk_display_get_n_screens (display);
+
+ for (i = 0; i < n_screens; i++)
+ {
+ g_signal_handlers_disconnect_by_func (gdk_display_get_screen (display, i),
+ on_screen_monitors_changed,
+ manager);
+ }
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_destroy (l->data);
+ }
+ g_slist_free (manager->priv->windows);
+ manager->priv->windows = NULL;
+}
+
+static void
+gs_manager_finalize (GObject *object)
+{
+ GSManager *manager;
+
+ g_return_if_fail (object != NULL);
+ g_return_if_fail (GS_IS_MANAGER (object));
+
+ manager = GS_MANAGER (object);
+
+ g_return_if_fail (manager->priv != NULL);
+
+ if (manager->priv->bg_notify_id != 0)
+ {
+ mateconf_client_remove_dir (manager->priv->client,
+ MATE_BG_KEY_DIR,
+ NULL);
+ mateconf_client_notify_remove (manager->priv->client,
+ manager->priv->bg_notify_id);
+ manager->priv->bg_notify_id = 0;
+ }
+ if (manager->priv->bg != NULL)
+ {
+ g_object_unref (manager->priv->bg);
+ }
+ if (manager->priv->client != NULL)
+ {
+ g_object_unref (manager->priv->client);
+ }
+
+ free_themes (manager);
+ g_free (manager->priv->logout_command);
+ g_free (manager->priv->keyboard_command);
+ g_free (manager->priv->status_message);
+
+ remove_unfade_idle (manager);
+ remove_timers (manager);
+
+ gs_grab_release (manager->priv->grab);
+
+ manager_stop_jobs (manager);
+
+ gs_manager_destroy_windows (manager);
+
+ manager->priv->active = FALSE;
+ manager->priv->activate_time = 0;
+ manager->priv->lock_enabled = FALSE;
+
+ g_object_unref (manager->priv->fade);
+ g_object_unref (manager->priv->grab);
+ g_object_unref (manager->priv->theme_manager);
+
+ G_OBJECT_CLASS (gs_manager_parent_class)->finalize (object);
+}
+
+static void
+gs_manager_create_windows_for_screen (GSManager *manager,
+ GdkScreen *screen)
+{
+ int n_monitors;
+ int i;
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+ g_return_if_fail (GDK_IS_SCREEN (screen));
+
+ g_object_ref (manager);
+ g_object_ref (screen);
+
+ n_monitors = gdk_screen_get_n_monitors (screen);
+
+ gs_debug ("Creating %d windows for screen %d", n_monitors, gdk_screen_get_number (screen));
+
+ for (i = 0; i < n_monitors; i++)
+ {
+ gs_manager_create_window_for_monitor (manager, screen, i);
+ }
+
+ g_object_unref (screen);
+ g_object_unref (manager);
+}
+
+static void
+gs_manager_create_windows (GSManager *manager)
+{
+ GdkDisplay *display;
+ int n_screens;
+ int i;
+
+ g_return_if_fail (manager != NULL);
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_assert (manager->priv->windows == NULL);
+
+ display = gdk_display_get_default ();
+ n_screens = gdk_display_get_n_screens (display);
+
+ for (i = 0; i < n_screens; i++)
+ {
+ g_signal_connect (gdk_display_get_screen (display, i),
+ "monitors-changed",
+ G_CALLBACK (on_screen_monitors_changed),
+ manager);
+
+ gs_manager_create_windows_for_screen (manager, gdk_display_get_screen (display, i));
+ }
+}
+
+GSManager *
+gs_manager_new (void)
+{
+ GObject *manager;
+
+ manager = g_object_new (GS_TYPE_MANAGER, NULL);
+
+ return GS_MANAGER (manager);
+}
+
+static void
+show_windows (GSList *windows)
+{
+ GSList *l;
+
+ for (l = windows; l; l = l->next)
+ {
+ gtk_widget_show (GTK_WIDGET (l->data));
+ }
+}
+
+static void
+remove_job (GSJob *job)
+{
+ if (job == NULL)
+ {
+ return;
+ }
+
+ gs_job_stop (job);
+ g_object_unref (job);
+}
+
+static void
+fade_done_cb (GSFade *fade,
+ GSManager *manager)
+{
+ gs_debug ("fade completed, showing windows");
+ show_windows (manager->priv->windows);
+ manager->priv->fading = FALSE;
+}
+
+static gboolean
+gs_manager_activate (GSManager *manager)
+{
+ gboolean do_fade;
+ gboolean res;
+
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ if (manager->priv->active)
+ {
+ gs_debug ("Trying to activate manager when already active");
+ return FALSE;
+ }
+
+ res = gs_grab_grab_root (manager->priv->grab, FALSE);
+ if (! res)
+ {
+ return FALSE;
+ }
+
+ if (manager->priv->windows == NULL)
+ {
+ gs_manager_create_windows (GS_MANAGER (manager));
+ }
+
+ manager->priv->jobs = g_hash_table_new_full (g_direct_hash,
+ g_direct_equal,
+ NULL,
+ (GDestroyNotify)remove_job);
+
+ manager->priv->active = TRUE;
+
+ /* fade to black and show windows */
+ do_fade = TRUE;
+ if (do_fade)
+ {
+ manager->priv->fading = TRUE;
+ gs_debug ("fading out");
+ gs_fade_async (manager->priv->fade,
+ FADE_TIMEOUT,
+ (GSFadeDoneFunc)fade_done_cb,
+ manager);
+
+ while (manager->priv->fading)
+ {
+ gtk_main_iteration ();
+ }
+ }
+ else
+ {
+ show_windows (manager->priv->windows);
+ }
+
+ return TRUE;
+}
+
+static gboolean
+gs_manager_deactivate (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ if (! manager->priv->active)
+ {
+ gs_debug ("Trying to deactivate a screensaver that is not active");
+ return FALSE;
+ }
+
+ remove_unfade_idle (manager);
+ gs_fade_reset (manager->priv->fade);
+ remove_timers (manager);
+
+ gs_grab_release (manager->priv->grab);
+
+ manager_stop_jobs (manager);
+
+ gs_manager_destroy_windows (manager);
+
+ /* reset state */
+ manager->priv->active = FALSE;
+ manager->priv->activate_time = 0;
+ manager->priv->lock_active = FALSE;
+ manager->priv->dialog_up = FALSE;
+ manager->priv->fading = FALSE;
+
+ return TRUE;
+}
+
+gboolean
+gs_manager_set_active (GSManager *manager,
+ gboolean active)
+{
+ gboolean res;
+
+ if (active)
+ {
+ res = gs_manager_activate (manager);
+ }
+ else
+ {
+ res = gs_manager_deactivate (manager);
+ }
+
+ return res;
+}
+
+gboolean
+gs_manager_get_active (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ return manager->priv->active;
+}
+
+gboolean
+gs_manager_request_unlock (GSManager *manager)
+{
+ GSWindow *window;
+
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ if (! manager->priv->active)
+ {
+ gs_debug ("Request unlock but manager is not active");
+ return FALSE;
+ }
+
+ if (manager->priv->dialog_up)
+ {
+ gs_debug ("Request unlock but dialog is already up");
+ return FALSE;
+ }
+
+ if (manager->priv->fading)
+ {
+ gs_debug ("Request unlock so finishing fade");
+ gs_fade_finish (manager->priv->fade);
+ }
+
+ if (manager->priv->windows == NULL)
+ {
+ gs_debug ("We don't have any windows!");
+ return FALSE;
+ }
+
+ /* Find the GSWindow that contains the pointer */
+ window = find_window_at_pointer (manager);
+ gs_window_request_unlock (window);
+
+ return TRUE;
+}
+
+void
+gs_manager_cancel_unlock_request (GSManager *manager)
+{
+ GSList *l;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_cancel_unlock_request (l->data);
+ }
+}
diff --git a/src/gs-manager.c.orig b/src/gs-manager.c.orig
new file mode 100644
index 0000000..47c5005
--- /dev/null
+++ b/src/gs-manager.c.orig
@@ -0,0 +1,2030 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2004-2008 William Jon McCann <[email protected]>
+ *
+ * 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.
+ *
+ * Authors: William Jon McCann <[email protected]>
+ *
+ */
+
+#include "config.h"
+
+#include <time.h>
+#include <gdk/gdk.h>
+#include <gdk/gdkx.h>
+
+#include <mateconf/mateconf-engine.h>
+#include <mateconf/mateconf-client.h>
+
+#define MATE_DESKTOP_USE_UNSTABLE_API
+#include <libmateui/mate-bg.h>
+
+#include "gs-prefs.h" /* for GSSaverMode */
+
+#include "gs-manager.h"
+#include "gs-window.h"
+#include "gs-theme-manager.h"
+#include "gs-job.h"
+#include "gs-grab.h"
+#include "gs-fade.h"
+#include "gs-debug.h"
+
+static void gs_manager_class_init (GSManagerClass *klass);
+static void gs_manager_init (GSManager *manager);
+static void gs_manager_finalize (GObject *object);
+
+#define GS_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GS_TYPE_MANAGER, GSManagerPrivate))
+
+struct GSManagerPrivate
+{
+ GSList *windows;
+ GHashTable *jobs;
+
+ GSThemeManager *theme_manager;
+ MateConfClient *client;
+ MateBG *bg;
+ guint bg_notify_id;
+
+ /* Policy */
+ glong lock_timeout;
+ glong cycle_timeout;
+ glong logout_timeout;
+
+ guint lock_enabled : 1;
+ guint logout_enabled : 1;
+ guint keyboard_enabled : 1;
+ guint user_switch_enabled : 1;
+ guint throttled : 1;
+
+ char *logout_command;
+ char *keyboard_command;
+
+ char *status_message;
+
+ /* State */
+ guint active : 1;
+ guint lock_active : 1;
+
+ guint fading : 1;
+ guint dialog_up : 1;
+
+ time_t activate_time;
+
+ guint lock_timeout_id;
+ guint cycle_timeout_id;
+
+ GSList *themes;
+ GSSaverMode saver_mode;
+ GSGrab *grab;
+ GSFade *fade;
+ guint unfade_idle_id;
+};
+
+enum
+{
+ ACTIVATED,
+ DEACTIVATED,
+ AUTH_REQUEST_BEGIN,
+ AUTH_REQUEST_END,
+ LAST_SIGNAL
+};
+
+enum
+{
+ PROP_0,
+ PROP_LOCK_ENABLED,
+ PROP_LOGOUT_ENABLED,
+ PROP_USER_SWITCH_ENABLED,
+ PROP_KEYBOARD_ENABLED,
+ PROP_LOCK_TIMEOUT,
+ PROP_CYCLE_TIMEOUT,
+ PROP_LOGOUT_TIMEOUT,
+ PROP_LOGOUT_COMMAND,
+ PROP_KEYBOARD_COMMAND,
+ PROP_STATUS_MESSAGE,
+ PROP_ACTIVE,
+ PROP_THROTTLED,
+};
+
+#define FADE_TIMEOUT 1000
+
+static guint signals [LAST_SIGNAL] = { 0, };
+
+G_DEFINE_TYPE (GSManager, gs_manager, G_TYPE_OBJECT)
+
+static void
+manager_add_job_for_window (GSManager *manager,
+ GSWindow *window,
+ GSJob *job)
+{
+ if (manager->priv->jobs == NULL)
+ {
+ return;
+ }
+
+ g_hash_table_insert (manager->priv->jobs, window, job);
+}
+
+static const char *
+select_theme (GSManager *manager)
+{
+ const char *theme = NULL;
+
+ g_return_val_if_fail (manager != NULL, NULL);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), NULL);
+
+ if (manager->priv->saver_mode == GS_MODE_BLANK_ONLY)
+ {
+ return NULL;
+ }
+
+ if (manager->priv->themes)
+ {
+ int number = 0;
+
+ if (manager->priv->saver_mode == GS_MODE_RANDOM)
+ {
+ g_random_set_seed (time (NULL));
+ number = g_random_int_range (0, g_slist_length (manager->priv->themes));
+ }
+ theme = g_slist_nth_data (manager->priv->themes, number);
+ }
+
+ return theme;
+}
+
+static GSJob *
+lookup_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ if (manager->priv->jobs == NULL)
+ {
+ return NULL;
+ }
+
+ job = g_hash_table_lookup (manager->priv->jobs, window);
+
+ return job;
+}
+
+static void
+manager_maybe_stop_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ job = lookup_job_for_window (manager, window);
+
+ if (job == NULL)
+ {
+ gs_debug ("Job not found for window");
+ return;
+ }
+
+ gs_job_stop (job);
+}
+
+static void
+manager_maybe_start_job_for_window (GSManager *manager,
+ GSWindow *window)
+{
+ GSJob *job;
+
+ job = lookup_job_for_window (manager, window);
+
+ if (job == NULL)
+ {
+ gs_debug ("Job not found for window");
+ return;
+ }
+
+ if (! manager->priv->dialog_up)
+ {
+ if (! manager->priv->throttled)
+ {
+ if (! gs_job_is_running (job))
+ {
+ if (! gs_window_is_obscured (window))
+ {
+ gs_debug ("Starting job for window");
+ gs_job_start (job);
+ }
+ else
+ {
+ gs_debug ("Window is obscured deferring start of job");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because job is running");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because throttled");
+ }
+ }
+ else
+ {
+ gs_debug ("Not starting job because dialog is up");
+ }
+}
+
+static void
+manager_select_theme_for_job (GSManager *manager,
+ GSJob *job)
+{
+ const char *theme;
+
+ theme = select_theme (manager);
+
+ if (theme != NULL)
+ {
+ GSThemeInfo *info;
+ const char *command;
+
+ command = NULL;
+
+ info = gs_theme_manager_lookup_theme_info (manager->priv->theme_manager, theme);
+ if (info != NULL)
+ {
+ command = gs_theme_info_get_exec (info);
+ }
+ else
+ {
+ gs_debug ("Could not find information for theme: %s",
+ theme);
+ }
+
+ gs_job_set_command (job, command);
+
+
+ if (info != NULL)
+ {
+ gs_theme_info_unref (info);
+ }
+ }
+ else
+ {
+ gs_job_set_command (job, NULL);
+ }
+}
+
+static void
+cycle_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ gs_job_stop (job);
+ manager_select_theme_for_job (manager, job);
+ manager_maybe_start_job_for_window (manager, window);
+}
+
+static void
+manager_cycle_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) cycle_job, manager);
+ }
+}
+
+static void
+throttle_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ if (manager->priv->throttled)
+ {
+ gs_job_stop (job);
+ }
+ else
+ {
+ manager_maybe_start_job_for_window (manager, window);
+ }
+}
+
+static void
+manager_throttle_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) throttle_job, manager);
+ }
+}
+
+static void
+resume_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ if (gs_job_is_running (job))
+ {
+ gs_job_suspend (job, FALSE);
+ }
+ else
+ {
+ manager_maybe_start_job_for_window (manager, window);
+ }
+}
+
+static void
+manager_resume_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) resume_job, manager);
+ }
+}
+
+static void
+suspend_job (GSWindow *window,
+ GSJob *job,
+ GSManager *manager)
+{
+ gs_job_suspend (job, TRUE);
+}
+
+static void
+manager_suspend_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_foreach (manager->priv->jobs, (GHFunc) suspend_job, manager);
+ }
+}
+
+static void
+manager_stop_jobs (GSManager *manager)
+{
+ if (manager->priv->jobs != NULL)
+ {
+ g_hash_table_destroy (manager->priv->jobs);
+
+ }
+ manager->priv->jobs = NULL;
+}
+
+void
+gs_manager_set_mode (GSManager *manager,
+ GSSaverMode mode)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ manager->priv->saver_mode = mode;
+}
+
+static void
+free_themes (GSManager *manager)
+{
+ if (manager->priv->themes)
+ {
+ g_slist_foreach (manager->priv->themes, (GFunc)g_free, NULL);
+ g_slist_free (manager->priv->themes);
+ }
+}
+
+void
+gs_manager_set_themes (GSManager *manager,
+ GSList *themes)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ free_themes (manager);
+ manager->priv->themes = NULL;
+
+ for (l = themes; l; l = l->next)
+ {
+ manager->priv->themes = g_slist_append (manager->priv->themes, g_strdup (l->data));
+ }
+}
+
+void
+gs_manager_set_throttled (GSManager *manager,
+ gboolean throttled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->throttled != throttled)
+ {
+ GSList *l;
+
+ manager->priv->throttled = throttled;
+
+ if (! manager->priv->dialog_up)
+ {
+
+ manager_throttle_jobs (manager);
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_clear (l->data);
+ }
+ }
+ }
+}
+
+void
+gs_manager_get_lock_active (GSManager *manager,
+ gboolean *lock_active)
+{
+ if (lock_active != NULL)
+ {
+ *lock_active = FALSE;
+ }
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ *lock_active = manager->priv->lock_active;
+}
+
+void
+gs_manager_set_lock_active (GSManager *manager,
+ gboolean lock_active)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ gs_debug ("Setting lock active: %d", lock_active);
+
+ if (manager->priv->lock_active != lock_active)
+ {
+ GSList *l;
+
+ manager->priv->lock_active = lock_active;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_lock_enabled (l->data, lock_active);
+ }
+ }
+}
+
+void
+gs_manager_get_lock_enabled (GSManager *manager,
+ gboolean *lock_enabled)
+{
+ if (lock_enabled != NULL)
+ {
+ *lock_enabled = FALSE;
+ }
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ *lock_enabled = manager->priv->lock_enabled;
+}
+
+void
+gs_manager_set_lock_enabled (GSManager *manager,
+ gboolean lock_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->lock_enabled != lock_enabled)
+ {
+ manager->priv->lock_enabled = lock_enabled;
+ }
+}
+
+void
+gs_manager_set_logout_enabled (GSManager *manager,
+ gboolean logout_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->logout_enabled != logout_enabled)
+ {
+ GSList *l;
+
+ manager->priv->logout_enabled = logout_enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_enabled (l->data, logout_enabled);
+ }
+ }
+}
+
+void
+gs_manager_set_keyboard_enabled (GSManager *manager,
+ gboolean enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->keyboard_enabled != enabled)
+ {
+ GSList *l;
+
+ manager->priv->keyboard_enabled = enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_keyboard_enabled (l->data, enabled);
+ }
+ }
+}
+
+void
+gs_manager_set_user_switch_enabled (GSManager *manager,
+ gboolean user_switch_enabled)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->user_switch_enabled != user_switch_enabled)
+ {
+ GSList *l;
+
+ manager->priv->user_switch_enabled = user_switch_enabled;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_user_switch_enabled (l->data, user_switch_enabled);
+ }
+ }
+}
+
+static gboolean
+activate_lock_timeout (GSManager *manager)
+{
+ if (manager->priv->lock_enabled)
+ {
+ gs_manager_set_lock_active (manager, TRUE);
+ }
+
+ manager->priv->lock_timeout_id = 0;
+
+ return FALSE;
+}
+
+static void
+remove_lock_timer (GSManager *manager)
+{
+ if (manager->priv->lock_timeout_id != 0)
+ {
+ g_source_remove (manager->priv->lock_timeout_id);
+ manager->priv->lock_timeout_id = 0;
+ }
+}
+
+static void
+add_lock_timer (GSManager *manager,
+ glong timeout)
+{
+ manager->priv->lock_timeout_id = g_timeout_add (timeout,
+ (GSourceFunc)activate_lock_timeout,
+ manager);
+}
+
+void
+gs_manager_set_lock_timeout (GSManager *manager,
+ glong lock_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->lock_timeout != lock_timeout)
+ {
+
+ manager->priv->lock_timeout = lock_timeout;
+
+ if (manager->priv->active
+ && ! manager->priv->lock_active
+ && (lock_timeout >= 0))
+ {
+
+ glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;
+
+ remove_lock_timer (manager);
+
+ if (elapsed >= lock_timeout)
+ {
+ activate_lock_timeout (manager);
+ }
+ else
+ {
+ add_lock_timer (manager, lock_timeout - elapsed);
+ }
+ }
+ }
+}
+
+void
+gs_manager_set_logout_timeout (GSManager *manager,
+ glong logout_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->logout_timeout != logout_timeout)
+ {
+ GSList *l;
+
+ manager->priv->logout_timeout = logout_timeout;
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_timeout (l->data, logout_timeout);
+ }
+ }
+}
+
+void
+gs_manager_set_logout_command (GSManager *manager,
+ const char *command)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->logout_command);
+
+ if (command)
+ {
+ manager->priv->logout_command = g_strdup (command);
+ }
+ else
+ {
+ manager->priv->logout_command = NULL;
+ }
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_logout_command (l->data, manager->priv->logout_command);
+ }
+}
+
+void
+gs_manager_set_keyboard_command (GSManager *manager,
+ const char *command)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->keyboard_command);
+
+ if (command)
+ {
+ manager->priv->keyboard_command = g_strdup (command);
+ }
+ else
+ {
+ manager->priv->keyboard_command = NULL;
+ }
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_keyboard_command (l->data, manager->priv->keyboard_command);
+ }
+}
+
+void
+gs_manager_set_status_message (GSManager *manager,
+ const char *status_message)
+{
+ GSList *l;
+
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ g_free (manager->priv->status_message);
+
+ manager->priv->status_message = g_strdup (status_message);
+
+ for (l = manager->priv->windows; l; l = l->next)
+ {
+ gs_window_set_status_message (l->data, manager->priv->status_message);
+ }
+}
+
+gboolean
+gs_manager_cycle (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ gs_debug ("cycling jobs");
+
+ if (! manager->priv->active)
+ {
+ return FALSE;
+ }
+
+ if (manager->priv->dialog_up)
+ {
+ return FALSE;
+ }
+
+ if (manager->priv->throttled)
+ {
+ return FALSE;
+ }
+
+ manager_cycle_jobs (manager);
+
+ return TRUE;
+}
+
+static gboolean
+cycle_timeout (GSManager *manager)
+{
+ g_return_val_if_fail (manager != NULL, FALSE);
+ g_return_val_if_fail (GS_IS_MANAGER (manager), FALSE);
+
+ if (! manager->priv->dialog_up)
+ {
+ gs_manager_cycle (manager);
+ }
+
+ return TRUE;
+}
+
+static void
+remove_cycle_timer (GSManager *manager)
+{
+ if (manager->priv->cycle_timeout_id != 0)
+ {
+ g_source_remove (manager->priv->cycle_timeout_id);
+ manager->priv->cycle_timeout_id = 0;
+ }
+}
+
+static void
+add_cycle_timer (GSManager *manager,
+ glong timeout)
+{
+ manager->priv->cycle_timeout_id = g_timeout_add (timeout,
+ (GSourceFunc)cycle_timeout,
+ manager);
+}
+
+void
+gs_manager_set_cycle_timeout (GSManager *manager,
+ glong cycle_timeout)
+{
+ g_return_if_fail (GS_IS_MANAGER (manager));
+
+ if (manager->priv->cycle_timeout != cycle_timeout)
+ {
+
+ manager->priv->cycle_timeout = cycle_timeout;
+
+ if (manager->priv->active && (cycle_timeout >= 0))
+ {
+ glong timeout;
+ glong elapsed = (time (NULL) - manager->priv->activate_time) * 1000;
+
+ remove_cycle_timer (manager);
+
+ if (elapsed >= cycle_timeout)
+ {
+ timeout = 0;
+ }
+ else
+ {
+ timeout = cycle_timeout - elapsed;
+ }
+
+ add_cycle_timer (manager, timeout);
+
+ }
+ }
+}
+
+static void
+gs_manager_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSManager *self;
+
+ self = GS_MANAGER (object);
+
+ switch (prop_id)
+ {
+ case PROP_THROTTLED:
+ gs_manager_set_throttled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOCK_ENABLED:
+ gs_manager_set_lock_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOCK_TIMEOUT:
+ gs_manager_set_lock_timeout (self, g_value_get_long (value));
+ break;
+ case PROP_LOGOUT_ENABLED:
+ gs_manager_set_logout_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_KEYBOARD_ENABLED:
+ gs_manager_set_keyboard_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_USER_SWITCH_ENABLED:
+ gs_manager_set_user_switch_enabled (self, g_value_get_boolean (value));
+ break;
+ case PROP_LOGOUT_TIMEOUT:
+ gs_manager_set_logout_timeout (self, g_value_get_long (value));
+ break;
+ case PROP_LOGOUT_COMMAND:
+ gs_manager_set_logout_command (self, g_value_get_string (value));
+ break;
+ case PROP_KEYBOARD_COMMAND:
+ gs_manager_set_keyboard_command (self, g_value_get_string (value));
+ break;
+ case PROP_STATUS_MESSAGE:
+ gs_manager_set_status_message (self, g_value_get_string (value));
+ break;
+ case PROP_CYCLE_TIMEOUT:
+ gs_manager_set_cycle_timeout (self, g_value_get_long (value));
+ break;
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ break;
+ }
+}
+
+static void
+gs_manager_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSManager *self;
+
+ self = GS_MANAGER (object);
+
+ switch (prop_id)
+ {
+ case PROP_THROTTLED:
+ g_value_set_boolean (value, self->priv->throttled);
+ break;
+ case PROP_LOCK_ENABLED:
+ g_value_set_boolean (value, self->priv->lock_enabled);
+ break;
+ case PROP_LOCK_TIMEOUT:
+ g_value_set_long (value, self->priv->lock_timeout);
+ break;
+ case PROP_LOGOUT_ENABLED:
+ g_value_set_boolean (value, self->priv->logout_enabled);
+ break;
+ cas