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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 Author: Bastien Nocera <hadess@hadess.net>
26 #include <glib/gi18n.h>
30 #include <X11/extensions/XTest.h>
31 #endif /* HAVE_XTEST */
32 #include <X11/keysym.h>
35 #include <dbus/dbus-glib.h>
37 #define GS_SERVICE "org.gnome.ScreenSaver"
38 #define GS_PATH "/org/gnome/ScreenSaver"
39 #define GS_INTERFACE "org.gnome.ScreenSaver"
40 #endif /* ENABLE_DBUS */
42 #include "totem-scrsaver.h"
44 #define XSCREENSAVER_MIN_TIMEOUT 60
46 static GObjectClass *parent_class = NULL;
47 static void totem_scrsaver_class_init (TotemScrsaverClass *class);
48 static void totem_scrsaver_init (TotemScrsaver *parser);
49 static void totem_scrsaver_finalize (GObject *object);
52 struct TotemScrsaverPrivate {
53 /* Whether the screensaver is disabled */
57 DBusGConnection *connection;
60 #endif /* ENABLE_DBUS */
62 /* To save the screensaver info */
68 /* For use with XTest */
69 int keycode1, keycode2;
74 G_DEFINE_TYPE(TotemScrsaver, totem_scrsaver, G_TYPE_OBJECT)
77 screensaver_is_running_dbus (TotemScrsaver *scr)
80 if (! scr->priv->connection)
83 if (! scr->priv->gs_proxy)
89 #endif /* ENABLE_DBUS */
93 screensaver_inhibit_dbus (TotemScrsaver *scr,
100 g_return_if_fail (scr != NULL);
101 g_return_if_fail (scr->priv->connection != NULL);
102 g_return_if_fail (scr->priv->gs_proxy != NULL);
110 application = g_strdup ("Evince");
111 reason = g_strdup (_("Running in presentation mode"));
113 res = dbus_g_proxy_call (scr->priv->gs_proxy,
116 G_TYPE_STRING, application,
117 G_TYPE_STRING, reason,
119 G_TYPE_UINT, &cookie,
123 /* save the cookie */
124 scr->priv->cookie = cookie;
126 /* try the old API */
127 res = dbus_g_proxy_call (scr->priv->gs_proxy,
130 G_TYPE_STRING, reason,
136 g_free (application);
139 res = dbus_g_proxy_call (scr->priv->gs_proxy,
142 G_TYPE_UINT, scr->priv->cookie,
146 /* clear the cookie */
147 scr->priv->cookie = 0;
149 /* try the old API */
150 res = dbus_g_proxy_call (scr->priv->gs_proxy,
160 g_warning ("Problem inhibiting the screensaver: %s", error->message);
161 g_error_free (error);
164 #endif /* ENABLE_DBUS */
168 screensaver_enable_dbus (TotemScrsaver *scr)
170 screensaver_inhibit_dbus (scr, FALSE);
174 screensaver_disable_dbus (TotemScrsaver *scr)
176 screensaver_inhibit_dbus (scr, TRUE);
181 gs_proxy_destroy_cb (GObject *proxy,
184 g_warning ("Detected that GNOME screensaver has left the bus");
186 /* just invalidate for now */
187 scr->priv->gs_proxy = NULL;
192 screensaver_init_dbus (TotemScrsaver *scr)
195 GError *error = NULL;
197 scr->priv->connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
199 if (! scr->priv->connection) {
201 g_warning ("Failed to connect to the session bus: %s", error->message);
202 g_error_free (error);
207 scr->priv->gs_proxy = dbus_g_proxy_new_for_name_owner (scr->priv->connection,
212 if (scr->priv->gs_proxy != NULL) {
213 g_signal_connect_object (scr->priv->gs_proxy,
215 G_CALLBACK (gs_proxy_destroy_cb),
221 #endif /* ENABLE_DBUS */
225 screensaver_finalize_dbus (TotemScrsaver *scr)
228 if (scr->priv->gs_proxy) {
229 g_object_unref (scr->priv->gs_proxy);
231 #endif /* ENABLE_DBUS */
235 screensaver_enable_x11 (TotemScrsaver *scr)
239 if (scr->priv->have_xtest == True)
241 g_source_remove_by_user_data (scr);
244 #endif /* HAVE_XTEST */
246 XLockDisplay (GDK_DISPLAY());
247 XSetScreenSaver (GDK_DISPLAY(),
250 scr->priv->prefer_blanking,
251 scr->priv->allow_exposures);
252 XUnlockDisplay (GDK_DISPLAY());
257 fake_event (TotemScrsaver *scr)
259 if (scr->priv->disabled)
261 XLockDisplay (GDK_DISPLAY());
262 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
264 XTestFakeKeyEvent (GDK_DISPLAY(), *scr->priv->keycode,
266 XUnlockDisplay (GDK_DISPLAY());
267 /* Swap the keycode */
268 if (scr->priv->keycode == &scr->priv->keycode1)
269 scr->priv->keycode = &scr->priv->keycode2;
271 scr->priv->keycode = &scr->priv->keycode1;
276 #endif /* HAVE_XTEST */
279 screensaver_disable_x11 (TotemScrsaver *scr)
283 if (scr->priv->have_xtest == True)
285 XLockDisplay (GDK_DISPLAY());
286 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
287 &scr->priv->interval,
288 &scr->priv->prefer_blanking,
289 &scr->priv->allow_exposures);
290 XUnlockDisplay (GDK_DISPLAY());
292 if (scr->priv->timeout != 0)
294 g_timeout_add (scr->priv->timeout / 2 * 1000,
295 (GSourceFunc) fake_event, scr);
297 g_timeout_add (XSCREENSAVER_MIN_TIMEOUT / 2 * 1000,
298 (GSourceFunc) fake_event, scr);
303 #endif /* HAVE_XTEST */
305 XLockDisplay (GDK_DISPLAY());
306 XGetScreenSaver(GDK_DISPLAY(), &scr->priv->timeout,
307 &scr->priv->interval,
308 &scr->priv->prefer_blanking,
309 &scr->priv->allow_exposures);
310 XSetScreenSaver(GDK_DISPLAY(), 0, 0,
311 DontPreferBlanking, DontAllowExposures);
312 XUnlockDisplay (GDK_DISPLAY());
316 screensaver_init_x11 (TotemScrsaver *scr)
321 XLockDisplay (GDK_DISPLAY());
322 scr->priv->have_xtest = XTestQueryExtension (GDK_DISPLAY(), &a, &b, &c, &d);
323 if(scr->priv->have_xtest == True)
325 scr->priv->keycode1 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
326 if (scr->priv->keycode1 == 0) {
327 g_warning ("scr->priv->keycode1 not existant");
329 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_R);
330 if (scr->priv->keycode2 == 0) {
331 scr->priv->keycode2 = XKeysymToKeycode (GDK_DISPLAY(), XK_Alt_L);
332 if (scr->priv->keycode2 == 0) {
333 g_warning ("scr->priv->keycode2 not existant");
336 scr->priv->keycode = &scr->priv->keycode1;
338 XUnlockDisplay (GDK_DISPLAY());
339 #endif /* HAVE_XTEST */
343 screensaver_finalize_x11 (TotemScrsaver *scr)
345 g_source_remove_by_user_data (scr);
349 totem_scrsaver_class_init (TotemScrsaverClass *klass)
351 GObjectClass *object_class = G_OBJECT_CLASS (klass);
353 parent_class = g_type_class_peek_parent (klass);
355 object_class->finalize = totem_scrsaver_finalize;
359 totem_scrsaver_new (void)
361 return TOTEM_SCRSAVER (g_object_new (TOTEM_TYPE_SCRSAVER, NULL));
365 totem_scrsaver_init (TotemScrsaver *scr)
367 scr->priv = g_new0 (TotemScrsaverPrivate, 1);
369 screensaver_init_dbus (scr);
370 screensaver_init_x11 (scr);
374 totem_scrsaver_disable (TotemScrsaver *scr)
376 if (scr->priv->disabled != FALSE)
379 scr->priv->disabled = TRUE;
381 if (screensaver_is_running_dbus (scr) != FALSE)
382 screensaver_disable_dbus (scr);
384 screensaver_disable_x11 (scr);
388 totem_scrsaver_enable (TotemScrsaver *scr)
390 if (scr->priv->disabled == FALSE)
393 scr->priv->disabled = FALSE;
395 if (screensaver_is_running_dbus (scr) != FALSE)
396 screensaver_enable_dbus (scr);
398 screensaver_enable_x11 (scr);
402 totem_scrsaver_finalize (GObject *object)
404 TotemScrsaver *scr = TOTEM_SCRSAVER (object);
406 screensaver_finalize_dbus (scr);
407 screensaver_finalize_x11 (scr);
411 if (G_OBJECT_CLASS (parent_class)->finalize != NULL) {
412 (* G_OBJECT_CLASS (parent_class)->finalize) (object);