1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3 Copyright (C) 2004-2006 Bastien Nocera <hadess@hadess.net>
4 Copyright © 2010 Christian Persch
6 The Gnome Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The Gnome Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301 USA.
21 Author: Bastien Nocera <hadess@hadess.net>
27 #include <glib/gi18n.h>
31 #ifdef GDK_WINDOWING_X11
33 #include <X11/keysym.h>
36 #include <X11/extensions/XTest.h>
37 #endif /* HAVE_XTEST */
38 #endif /* GDK_WINDOWING_X11 */
40 #include "totem-scrsaver.h"
42 #define GS_SERVICE "org.gnome.ScreenSaver"
43 #define GS_PATH "/org/gnome/ScreenSaver"
44 #define GS_INTERFACE "org.gnome.ScreenSaver"
46 #define XSCREENSAVER_MIN_TIMEOUT 60
48 static void totem_scrsaver_finalize (GObject *object);
50 struct TotemScrsaverPrivate {
51 /* Whether the screensaver is disabled */
55 gboolean have_screensaver_dbus;
59 /* To save the screensaver info */
65 /* For use with XTest */
66 int keycode1, keycode2;
71 G_DEFINE_TYPE(TotemScrsaver, totem_scrsaver, G_TYPE_OBJECT)
74 screensaver_is_running_dbus (TotemScrsaver *scr)
76 return scr->priv->have_screensaver_dbus;
80 screensaver_inhibit_dbus (TotemScrsaver *scr,
83 TotemScrsaverPrivate *priv = scr->priv;
87 if (!priv->have_screensaver_dbus)
91 value = g_dbus_proxy_call_sync (priv->gs_proxy,
93 g_variant_new ("(ss)",
95 _("Running in presentation mode")),
96 G_DBUS_CALL_FLAGS_NO_AUTO_START,
100 if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
101 /* try the old API */
102 g_clear_error (&error);
103 value = g_dbus_proxy_call_sync (priv->gs_proxy,
105 g_variant_new ("(s)",
106 _("Running in presentation mode")),
107 G_DBUS_CALL_FLAGS_NO_AUTO_START,
113 /* save the cookie */
114 if (g_variant_is_of_type (value, G_VARIANT_TYPE ("(u)")))
115 g_variant_get (value, "(u)", &priv->cookie);
118 g_variant_unref (value);
120 g_warning ("Problem inhibiting the screensaver: %s", error->message);
121 g_error_free (error);
125 value = g_dbus_proxy_call_sync (priv->gs_proxy,
127 g_variant_new ("(u)", priv->cookie),
128 G_DBUS_CALL_FLAGS_NO_AUTO_START,
132 if (error && g_error_matches (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD)) {
133 /* try the old API */
134 g_clear_error (&error);
135 value = g_dbus_proxy_call_sync (priv->gs_proxy,
137 g_variant_new ("()"),
138 G_DBUS_CALL_FLAGS_NO_AUTO_START,
144 /* clear the cookie */
146 g_variant_unref (value);
148 g_warning ("Problem uninhibiting the screensaver: %s", error->message);
149 g_error_free (error);
155 screensaver_enable_dbus (TotemScrsaver *scr)
157 screensaver_inhibit_dbus (scr, FALSE);
161 screensaver_disable_dbus (TotemScrsaver *scr)
163 screensaver_inhibit_dbus (scr, TRUE);
167 screensaver_dbus_appeared_cb (GDBusConnection *connection,
169 const gchar *name_owner,
173 TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
174 TotemScrsaverPrivate *priv = scr->priv;
176 priv->gs_proxy = g_object_ref (proxy);
178 priv->have_screensaver_dbus = TRUE;
182 screensaver_dbus_disappeared_cb (GDBusConnection *connection,
186 TotemScrsaver *scr = TOTEM_SCRSAVER (user_data);
187 TotemScrsaverPrivate *priv = scr->priv;
189 if (priv->gs_proxy) {
190 g_object_unref (priv->gs_proxy);
191 priv->gs_proxy = NULL;
194 priv->have_screensaver_dbus = FALSE;
198 screensaver_init_dbus (TotemScrsaver *scr)
200 TotemScrsaverPrivate *priv = scr->priv;
202 priv->watch_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
204 G_BUS_NAME_WATCHER_FLAGS_NONE,
208 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
209 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
210 screensaver_dbus_appeared_cb,
211 screensaver_dbus_disappeared_cb,
216 screensaver_finalize_dbus (TotemScrsaver *scr)
218 TotemScrsaverPrivate *priv = scr->priv;
220 if (scr->priv->gs_proxy) {
221 g_object_unref (scr->priv->gs_proxy);
224 if (priv->watch_id > 0)
225 g_bus_unwatch_proxy (priv->watch_id);
228 #ifdef GDK_WINDOWING_X11
230 screensaver_enable_x11 (TotemScrsaver *scr)
234 if (scr->priv->have_xtest != FALSE)
236 g_source_remove_by_user_data (scr);
239 #endif /* HAVE_XTEST */
241 XLockDisplay (GDK_DISPLAY());
242 XSetScreenSaver (GDK_DISPLAY(),
245 scr->priv->prefer_blanking,
246 scr->priv->allow_exposures);
247 XUnlockDisplay (GDK_DISPLAY());
252 fake_event (TotemScrsaver *scr)
254 if (scr->priv->disabled)
256 XLockDisplay (GDK_DISPLAY());
257 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
259 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
261 XUnlockDisplay (GDK_DISPLAY());
262 /* Swap the keycode */
263 if (scr->priv->keycode == &scr->priv->keycode1)
264 scr->priv->keycode = &scr->priv->keycode2;
266 scr->priv->keycode = &scr->priv->keycode1;
271 #endif /* HAVE_XTEST */
274 screensaver_disable_x11 (TotemScrsaver *scr)
278 if (scr->priv->have_xtest != FALSE)
280 XLockDisplay (GDK_DISPLAY());
281 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
282 &scr->priv->interval,
283 &scr->priv->prefer_blanking,
284 &scr->priv->allow_exposures);
285 XUnlockDisplay (GDK_DISPLAY());
287 if (scr->priv->timeout != 0) {
288 g_timeout_add_seconds (scr->priv->timeout / 2,
289 (GSourceFunc) fake_event, scr);
291 g_timeout_add_seconds (XSCREENSAVER_MIN_TIMEOUT / 2,
292 (GSourceFunc) fake_event, scr);
297 #endif /* HAVE_XTEST */
299 XLockDisplay (GDK_DISPLAY());
300 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
301 &scr->priv->interval,
302 &scr->priv->prefer_blanking,
303 &scr->priv->allow_exposures);
304 XSetScreenSaver(GDK_DISPLAY(), 0, 0,
305 DontPreferBlanking, DontAllowExposures);
306 XUnlockDisplay (GDK_DISPLAY());
310 screensaver_init_x11 (TotemScrsaver *scr)
315 XLockDisplay (GDK_DISPLAY());
316 scr->priv->have_xtest = (XTestQueryExtension (GDK_DISPLAY(), &a, &b, &c, &d) == True);
317 if (scr->priv->have_xtest != FALSE)
319 scr->priv->keycode1 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
320 if (scr->priv->keycode1 == 0) {
321 g_warning ("scr->priv->keycode1 not existant");
323 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_R);
324 if (scr->priv->keycode2 == 0) {
325 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
326 if (scr->priv->keycode2 == 0) {
327 g_warning ("scr->priv->keycode2 not existant");
330 scr->priv->keycode = &scr->priv->keycode1;
332 XUnlockDisplay (GDK_DISPLAY());
333 #endif /* HAVE_XTEST */
337 screensaver_finalize_x11 (TotemScrsaver *scr)
339 g_source_remove_by_user_data (scr);
344 totem_scrsaver_class_init (TotemScrsaverClass *klass)
346 GObjectClass *object_class = G_OBJECT_CLASS (klass);
348 object_class->finalize = totem_scrsaver_finalize;
352 * totem_scrsaver_new:
354 * Creates a #TotemScrsaver object.
355 * If the GNOME screen saver is running, it uses its DBUS interface to
356 * inhibit the screensaver; otherwise it falls back to using the X
357 * screensaver functionality for this.
359 * Returns: a newly created #TotemScrsaver
362 totem_scrsaver_new (void)
364 return TOTEM_SCRSAVER (g_object_new (TOTEM_TYPE_SCRSAVER, NULL));
368 totem_scrsaver_init (TotemScrsaver *scr)
370 scr->priv = g_new0 (TotemScrsaverPrivate, 1);
372 screensaver_init_dbus (scr);
373 #ifdef GDK_WINDOWING_X11
374 screensaver_init_x11 (scr);
376 #warning Unimplemented
381 totem_scrsaver_disable (TotemScrsaver *scr)
383 g_return_if_fail (TOTEM_SCRSAVER (scr));
385 if (scr->priv->disabled != FALSE)
388 scr->priv->disabled = TRUE;
390 if (screensaver_is_running_dbus (scr) != FALSE)
391 screensaver_disable_dbus (scr);
393 #ifdef GDK_WINDOWING_X11
394 screensaver_disable_x11 (scr);
396 #warning Unimplemented
402 totem_scrsaver_enable (TotemScrsaver *scr)
404 g_return_if_fail (TOTEM_SCRSAVER (scr));
406 if (scr->priv->disabled == FALSE)
409 scr->priv->disabled = FALSE;
411 if (screensaver_is_running_dbus (scr) != FALSE)
412 screensaver_enable_dbus (scr);
414 #ifdef GDK_WINDOWING_X11
415 screensaver_enable_x11 (scr);
417 #warning Unimplemented
423 totem_scrsaver_set_state (TotemScrsaver *scr, gboolean enable)
425 g_return_if_fail (TOTEM_SCRSAVER (scr));
427 if (scr->priv->disabled == !enable)
431 totem_scrsaver_disable (scr);
433 totem_scrsaver_enable (scr);
437 totem_scrsaver_finalize (GObject *object)
439 TotemScrsaver *scr = TOTEM_SCRSAVER (object);
441 screensaver_finalize_dbus (scr);
442 #ifdef GDK_WINDOWING_X11
443 screensaver_finalize_x11 (scr);
445 #warning Unimplemented
449 G_OBJECT_CLASS (totem_scrsaver_parent_class)->finalize (object);