1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 Copyright (C) 2004-2006 Bastien Nocera <hadess@hadess.net>
5 The Gnome Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The Gnome Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the Gnome Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA.
20 Author: Bastien Nocera <hadess@hadess.net>
26 /* Evince/Totem differences */
33 #include <glib/gi18n.h>
37 #ifdef GDK_WINDOWING_X11
39 #include <X11/keysym.h>
42 #include <X11/extensions/XTest.h>
43 #endif /* HAVE_XTEST */
44 #endif /* GDK_WINDOWING_X11 */
47 #define GS_SERVICE "org.gnome.ScreenSaver"
48 #define GS_PATH "/org/gnome/ScreenSaver"
49 #define GS_INTERFACE "org.gnome.ScreenSaver"
50 #endif /* WITH_DBUS */
52 #include "totem-scrsaver.h"
54 #define XSCREENSAVER_MIN_TIMEOUT 60
56 static GObjectClass *parent_class = NULL;
57 static void totem_scrsaver_finalize (GObject *object);
60 struct TotemScrsaverPrivate {
61 /* Whether the screensaver is disabled */
65 GDBusConnection *connection;
68 #endif /* WITH_DBUS */
70 /* To save the screensaver info */
76 /* For use with XTest */
77 int keycode1, keycode2;
82 G_DEFINE_TYPE(TotemScrsaver, totem_scrsaver, G_TYPE_OBJECT)
85 screensaver_is_running_dbus (TotemScrsaver *scr)
88 return scr->priv->connection != NULL;
91 #endif /* WITH_DBUS */
95 screensaver_inhibit_dbus (TotemScrsaver *scr,
102 if (scr->priv->connection == NULL)
106 value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
111 g_variant_new ("(ss)",
113 _("Running in presentation mode")),
114 G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
118 if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
119 /* try the old API */
120 g_clear_error (&error);
121 value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
126 g_variant_new ("(s)",
127 _("Running in presentation mode")),
128 G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
134 /* save the cookie */
135 if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(u)")))
136 g_variant_get (value, "(u)", &scr->priv->cookie);
138 scr->priv->cookie = 0;
139 g_variant_unref (value);
141 g_warning ("Problem inhibiting the screensaver: %s", error->message);
142 g_error_free (error);
146 value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
151 g_variant_new ("(u)", scr->priv->cookie),
152 G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
156 if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
157 /* try the old API */
158 g_clear_error (&error);
159 value = g_dbus_connection_invoke_method_sync (scr->priv->connection,
164 g_variant_new ("()"),
165 G_DBUS_INVOKE_METHOD_FLAGS_NO_AUTO_START,
171 /* clear the cookie */
172 scr->priv->cookie = 0;
173 g_variant_unref (value);
175 g_warning ("Problem uninhibiting the screensaver: %s", error->message);
176 g_error_free (error);
179 #endif /* WITH_DBUS */
183 screensaver_enable_dbus (TotemScrsaver *scr)
185 screensaver_inhibit_dbus (scr, FALSE);
189 screensaver_disable_dbus (TotemScrsaver *scr)
191 screensaver_inhibit_dbus (scr, TRUE);
196 screensaver_dbus_appeared_cb (GDBusConnection *connection,
198 const char *name_owner,
201 TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
203 scr->priv->connection = g_object_ref (connection);
207 screensaver_dbus_disappeared_cb (GDBusConnection *connection,
211 TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
213 g_assert (scr->priv->connection == connection);
214 g_object_unref (scr->priv->connection);
215 scr->priv->connection = NULL;
220 screensaver_init_dbus (TotemScrsaver *scr)
223 scr->priv->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
225 G_BUS_NAME_WATCHER_FLAGS_NONE,
226 screensaver_dbus_appeared_cb,
227 screensaver_dbus_disappeared_cb,
229 #endif /* WITH_DBUS */
233 screensaver_finalize_dbus (TotemScrsaver *scr)
236 g_bus_unwatch_name (scr->priv->watch_id);
238 if (scr->priv->connection != NULL)
239 g_object_unref (scr->priv->connection);
240 #endif /* WITH_DBUS */
243 #ifdef GDK_WINDOWING_X11
245 screensaver_enable_x11 (TotemScrsaver *scr)
249 if (scr->priv->have_xtest != FALSE)
251 g_source_remove_by_user_data (scr);
254 #endif /* HAVE_XTEST */
256 XLockDisplay (GDK_DISPLAY());
257 XSetScreenSaver (GDK_DISPLAY(),
260 scr->priv->prefer_blanking,
261 scr->priv->allow_exposures);
262 XUnlockDisplay (GDK_DISPLAY());
267 fake_event (TotemScrsaver *scr)
269 if (scr->priv->disabled)
271 XLockDisplay (GDK_DISPLAY());
272 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
274 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
276 XUnlockDisplay (GDK_DISPLAY());
277 /* Swap the keycode */
278 if (scr->priv->keycode == &scr->priv->keycode1)
279 scr->priv->keycode = &scr->priv->keycode2;
281 scr->priv->keycode = &scr->priv->keycode1;
286 #endif /* HAVE_XTEST */
289 screensaver_disable_x11 (TotemScrsaver *scr)
293 if (scr->priv->have_xtest != FALSE)
295 XLockDisplay (GDK_DISPLAY());
296 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
297 &scr->priv->interval,
298 &scr->priv->prefer_blanking,
299 &scr->priv->allow_exposures);
300 XUnlockDisplay (GDK_DISPLAY());
302 if (scr->priv->timeout != 0) {
303 g_timeout_add_seconds (scr->priv->timeout / 2,
304 (GSourceFunc) fake_event, scr);
306 g_timeout_add_seconds (XSCREENSAVER_MIN_TIMEOUT / 2,
307 (GSourceFunc) fake_event, scr);
312 #endif /* HAVE_XTEST */
314 XLockDisplay (GDK_DISPLAY());
315 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
316 &scr->priv->interval,
317 &scr->priv->prefer_blanking,
318 &scr->priv->allow_exposures);
319 XSetScreenSaver(GDK_DISPLAY(), 0, 0,
320 DontPreferBlanking, DontAllowExposures);
321 XUnlockDisplay (GDK_DISPLAY());
325 screensaver_init_x11 (TotemScrsaver *scr)
330 XLockDisplay (GDK_DISPLAY());
331 scr->priv->have_xtest = (XTestQueryExtension (GDK_DISPLAY(), &a, &b, &c, &d) == True);
332 if (scr->priv->have_xtest != FALSE)
334 scr->priv->keycode1 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
335 if (scr->priv->keycode1 == 0) {
336 g_warning ("scr->priv->keycode1 not existant");
338 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_R);
339 if (scr->priv->keycode2 == 0) {
340 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
341 if (scr->priv->keycode2 == 0) {
342 g_warning ("scr->priv->keycode2 not existant");
345 scr->priv->keycode = &scr->priv->keycode1;
347 XUnlockDisplay (GDK_DISPLAY());
348 #endif /* HAVE_XTEST */
352 screensaver_finalize_x11 (TotemScrsaver *scr)
354 g_source_remove_by_user_data (scr);
359 totem_scrsaver_class_init (TotemScrsaverClass *klass)
361 GObjectClass *object_class = G_OBJECT_CLASS (klass);
363 parent_class = g_type_class_peek_parent (klass);
365 object_class->finalize = totem_scrsaver_finalize;
369 totem_scrsaver_new (void)
371 return TOTEM_SCRSAVER (g_object_new (TOTEM_TYPE_SCRSAVER, NULL));
375 totem_scrsaver_init (TotemScrsaver *scr)
377 scr->priv = g_new0 (TotemScrsaverPrivate, 1);
379 screensaver_init_dbus (scr);
380 #ifdef GDK_WINDOWING_X11
381 screensaver_init_x11 (scr);
383 #warning Unimplemented
388 totem_scrsaver_disable (TotemScrsaver *scr)
390 g_return_if_fail (TOTEM_SCRSAVER (scr));
392 if (scr->priv->disabled != FALSE)
395 scr->priv->disabled = TRUE;
397 if (screensaver_is_running_dbus (scr) != FALSE)
398 screensaver_disable_dbus (scr);
400 #ifdef GDK_WINDOWING_X11
401 screensaver_disable_x11 (scr);
403 #warning Unimplemented
409 totem_scrsaver_enable (TotemScrsaver *scr)
411 g_return_if_fail (TOTEM_SCRSAVER (scr));
413 if (scr->priv->disabled == FALSE)
416 scr->priv->disabled = FALSE;
418 if (screensaver_is_running_dbus (scr) != FALSE)
419 screensaver_enable_dbus (scr);
421 #ifdef GDK_WINDOWING_X11
422 screensaver_enable_x11 (scr);
424 #warning Unimplemented
430 totem_scrsaver_set_state (TotemScrsaver *scr, gboolean enable)
432 g_return_if_fail (TOTEM_SCRSAVER (scr));
434 if (scr->priv->disabled == !enable)
438 totem_scrsaver_disable (scr);
440 totem_scrsaver_enable (scr);
444 totem_scrsaver_finalize (GObject *object)
446 TotemScrsaver *scr = TOTEM_SCRSAVER (object);
448 screensaver_finalize_dbus (scr);
449 #ifdef GDK_WINDOWING_X11
450 screensaver_finalize_x11 (scr);
452 #warning Unimplemented
458 if (G_OBJECT_CLASS (parent_class)->finalize != NULL) {
459 (* G_OBJECT_CLASS (parent_class)->finalize) (object);