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_call_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_call_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_call_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_call_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 if (scr->priv->connection == NULL)
216 g_assert (scr->priv->connection == connection);
217 g_object_unref (scr->priv->connection);
218 scr->priv->connection = NULL;
223 screensaver_init_dbus (TotemScrsaver *scr)
226 scr->priv->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION,
228 G_BUS_NAME_WATCHER_FLAGS_NONE,
229 screensaver_dbus_appeared_cb,
230 screensaver_dbus_disappeared_cb,
232 #endif /* WITH_DBUS */
236 screensaver_finalize_dbus (TotemScrsaver *scr)
239 g_bus_unwatch_name (scr->priv->watch_id);
241 if (scr->priv->connection != NULL)
242 g_object_unref (scr->priv->connection);
243 #endif /* WITH_DBUS */
246 #ifdef GDK_WINDOWING_X11
248 screensaver_enable_x11 (TotemScrsaver *scr)
252 if (scr->priv->have_xtest != FALSE)
254 g_source_remove_by_user_data (scr);
257 #endif /* HAVE_XTEST */
259 XLockDisplay (GDK_DISPLAY());
260 XSetScreenSaver (GDK_DISPLAY(),
263 scr->priv->prefer_blanking,
264 scr->priv->allow_exposures);
265 XUnlockDisplay (GDK_DISPLAY());
270 fake_event (TotemScrsaver *scr)
272 if (scr->priv->disabled)
274 XLockDisplay (GDK_DISPLAY());
275 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
277 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
279 XUnlockDisplay (GDK_DISPLAY());
280 /* Swap the keycode */
281 if (scr->priv->keycode == &scr->priv->keycode1)
282 scr->priv->keycode = &scr->priv->keycode2;
284 scr->priv->keycode = &scr->priv->keycode1;
289 #endif /* HAVE_XTEST */
292 screensaver_disable_x11 (TotemScrsaver *scr)
296 if (scr->priv->have_xtest != FALSE)
298 XLockDisplay (GDK_DISPLAY());
299 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
300 &scr->priv->interval,
301 &scr->priv->prefer_blanking,
302 &scr->priv->allow_exposures);
303 XUnlockDisplay (GDK_DISPLAY());
305 if (scr->priv->timeout != 0) {
306 g_timeout_add_seconds (scr->priv->timeout / 2,
307 (GSourceFunc) fake_event, scr);
309 g_timeout_add_seconds (XSCREENSAVER_MIN_TIMEOUT / 2,
310 (GSourceFunc) fake_event, scr);
315 #endif /* HAVE_XTEST */
317 XLockDisplay (GDK_DISPLAY());
318 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
319 &scr->priv->interval,
320 &scr->priv->prefer_blanking,
321 &scr->priv->allow_exposures);
322 XSetScreenSaver(GDK_DISPLAY(), 0, 0,
323 DontPreferBlanking, DontAllowExposures);
324 XUnlockDisplay (GDK_DISPLAY());
328 screensaver_init_x11 (TotemScrsaver *scr)
333 XLockDisplay (GDK_DISPLAY());
334 scr->priv->have_xtest = (XTestQueryExtension (GDK_DISPLAY(), &a, &b, &c, &d) == True);
335 if (scr->priv->have_xtest != FALSE)
337 scr->priv->keycode1 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
338 if (scr->priv->keycode1 == 0) {
339 g_warning ("scr->priv->keycode1 not existant");
341 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_R);
342 if (scr->priv->keycode2 == 0) {
343 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
344 if (scr->priv->keycode2 == 0) {
345 g_warning ("scr->priv->keycode2 not existant");
348 scr->priv->keycode = &scr->priv->keycode1;
350 XUnlockDisplay (GDK_DISPLAY());
351 #endif /* HAVE_XTEST */
355 screensaver_finalize_x11 (TotemScrsaver *scr)
357 g_source_remove_by_user_data (scr);
362 totem_scrsaver_class_init (TotemScrsaverClass *klass)
364 GObjectClass *object_class = G_OBJECT_CLASS (klass);
366 parent_class = g_type_class_peek_parent (klass);
368 object_class->finalize = totem_scrsaver_finalize;
372 totem_scrsaver_new (void)
374 return TOTEM_SCRSAVER (g_object_new (TOTEM_TYPE_SCRSAVER, NULL));
378 totem_scrsaver_init (TotemScrsaver *scr)
380 scr->priv = g_new0 (TotemScrsaverPrivate, 1);
382 screensaver_init_dbus (scr);
383 #ifdef GDK_WINDOWING_X11
384 screensaver_init_x11 (scr);
386 #warning Unimplemented
391 totem_scrsaver_disable (TotemScrsaver *scr)
393 g_return_if_fail (TOTEM_SCRSAVER (scr));
395 if (scr->priv->disabled != FALSE)
398 scr->priv->disabled = TRUE;
400 if (screensaver_is_running_dbus (scr) != FALSE)
401 screensaver_disable_dbus (scr);
403 #ifdef GDK_WINDOWING_X11
404 screensaver_disable_x11 (scr);
406 #warning Unimplemented
412 totem_scrsaver_enable (TotemScrsaver *scr)
414 g_return_if_fail (TOTEM_SCRSAVER (scr));
416 if (scr->priv->disabled == FALSE)
419 scr->priv->disabled = FALSE;
421 if (screensaver_is_running_dbus (scr) != FALSE)
422 screensaver_enable_dbus (scr);
424 #ifdef GDK_WINDOWING_X11
425 screensaver_enable_x11 (scr);
427 #warning Unimplemented
433 totem_scrsaver_set_state (TotemScrsaver *scr, gboolean enable)
435 g_return_if_fail (TOTEM_SCRSAVER (scr));
437 if (scr->priv->disabled == !enable)
441 totem_scrsaver_disable (scr);
443 totem_scrsaver_enable (scr);
447 totem_scrsaver_finalize (GObject *object)
449 TotemScrsaver *scr = TOTEM_SCRSAVER (object);
451 screensaver_finalize_dbus (scr);
452 #ifdef GDK_WINDOWING_X11
453 screensaver_finalize_x11 (scr);
455 #warning Unimplemented
461 if (G_OBJECT_CLASS (parent_class)->finalize != NULL) {
462 (* G_OBJECT_CLASS (parent_class)->finalize) (object);