1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright (C) 2004-2005 William Jon McCann <mccann@jhu.edu>
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU 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
* This program is distributed in the hope that it will be useful, but
11
* WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20
* Authors: William Jon McCann <mccann@jhu.edu>
28
#include <glib/gi18n.h>
30
#define DBUS_API_SUBJECT_TO_CHANGE
31
#include <dbus/dbus.h>
32
#include <dbus/dbus-glib.h>
33
#include <dbus/dbus-glib-lowlevel.h>
35
#define GS_SERVICE "org.gnome.screensaver"
36
#define GS_PATH "/org/gnome/screensaver"
37
#define GS_INTERFACE "org.gnome.screensaver"
39
/* this is for dbus < 0.3 */
40
#if ((DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 30))
41
#define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
44
static gboolean do_quit = FALSE;
45
static gboolean do_lock = FALSE;
46
static gboolean do_cycle = FALSE;
47
static gboolean do_activate = FALSE;
48
static gboolean do_deactivate = FALSE;
49
static gboolean do_throttle = FALSE;
50
static gboolean do_unthrottle = FALSE;
51
static gboolean do_version = FALSE;
52
static gboolean do_poke = FALSE;
54
static gboolean do_query = FALSE;
56
static GOptionEntry entries [] = {
57
{ "exit", 0, 0, G_OPTION_ARG_NONE, &do_quit,
58
N_("Causes the screensaver to exit gracefully"), NULL },
59
{ "query", 0, 0, G_OPTION_ARG_NONE, &do_query,
60
N_("Query the state of the screensaver"), NULL },
61
{ "lock", 0, 0, G_OPTION_ARG_NONE, &do_lock,
62
N_("Tells the running screensaver process to lock the screen immediately"), NULL },
63
{ "cycle", 0, 0, G_OPTION_ARG_NONE, &do_cycle,
64
N_("If the screensaver is active then switch to another graphics demo"), NULL },
65
{ "activate", 0, 0, G_OPTION_ARG_NONE, &do_activate,
66
N_("Turn the screensaver on (blank the screen)"), NULL },
67
{ "deactivate", 0, 0, G_OPTION_ARG_NONE, &do_deactivate,
68
N_("If the screensaver is active then deactivate it (un-blank the screen)"), NULL },
69
{ "throttle", 0, 0, G_OPTION_ARG_NONE, &do_throttle,
70
N_("Disable running graphical themes while blanked"), NULL },
71
{ "unthrottle", 0, 0, G_OPTION_ARG_NONE, &do_unthrottle,
72
N_("Enable running graphical themes while blanked (if applicable)"), NULL },
73
{ "poke", 0, 0, G_OPTION_ARG_NONE, &do_poke,
74
N_("Poke the running screensaver to simulate user activity"), NULL },
75
{ "version", 0, 0, G_OPTION_ARG_NONE, &do_version,
76
N_("Version of this application"), NULL },
80
static GMainLoop *loop = NULL;
83
screensaver_is_running (DBusConnection *connection)
88
g_return_val_if_fail (connection != NULL, FALSE);
90
dbus_error_init (&error);
91
exists = dbus_bus_name_has_owner (connection, GS_SERVICE, &error);
92
if (dbus_error_is_set (&error))
93
dbus_error_free (&error);
99
screensaver_send_message_bool (DBusConnection *connection,
103
DBusMessage *message;
106
DBusMessageIter iter;
108
g_return_val_if_fail (connection != NULL, NULL);
109
g_return_val_if_fail (name != NULL, NULL);
111
dbus_error_init (&error);
113
message = dbus_message_new_method_call (GS_SERVICE, GS_PATH, GS_INTERFACE, name);
114
if (message == NULL) {
115
g_warning ("Couldn't allocate the dbus message");
119
dbus_message_iter_init_append (message, &iter);
120
dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value);
122
reply = dbus_connection_send_with_reply_and_block (connection,
125
if (dbus_error_is_set (&error)) {
126
g_warning ("%s raised:\n %s\n\n", error.name, error.message);
130
dbus_connection_flush (connection);
132
dbus_message_unref (message);
133
dbus_error_free (&error);
139
screensaver_send_message_void (DBusConnection *connection,
141
gboolean expect_reply)
143
DBusMessage *message;
147
g_return_val_if_fail (connection != NULL, NULL);
148
g_return_val_if_fail (name != NULL, NULL);
150
dbus_error_init (&error);
152
message = dbus_message_new_method_call (GS_SERVICE, GS_PATH, GS_INTERFACE, name);
153
if (message == NULL) {
154
g_warning ("Couldn't allocate the dbus message");
158
if (! expect_reply) {
159
if (!dbus_connection_send (connection, message, NULL))
160
g_warning ("could not send message");
163
reply = dbus_connection_send_with_reply_and_block (connection,
166
if (dbus_error_is_set (&error)) {
167
g_warning ("%s raised:\n %s\n\n", error.name, error.message);
171
dbus_connection_flush (connection);
173
dbus_message_unref (message);
174
dbus_error_free (&error);
180
do_command (DBusConnection *connection)
185
reply = screensaver_send_message_void (connection, "quit", FALSE);
190
DBusMessageIter iter;
193
reply = screensaver_send_message_void (connection, "getActive", TRUE);
195
g_message ("Did not receive a reply from the screensaver.");
199
dbus_message_iter_init (reply, &iter);
200
dbus_message_iter_get_basic (&iter, &v);
201
g_print (_("The screensaver is %s\n"), v ? _("active") : _("inactive"));
203
dbus_message_unref (reply);
207
reply = screensaver_send_message_void (connection, "lock", FALSE);
211
reply = screensaver_send_message_void (connection, "cycle", FALSE);
215
reply = screensaver_send_message_void (connection, "poke", FALSE);
219
reply = screensaver_send_message_bool (connection, "setActive", TRUE);
221
g_message ("Did not receive a reply from the screensaver.");
224
dbus_message_unref (reply);
228
reply = screensaver_send_message_bool (connection, "setActive", FALSE);
230
g_message ("Did not receive a reply from the screensaver.");
233
dbus_message_unref (reply);
237
reply = screensaver_send_message_bool (connection, "setThrottleEnabled", TRUE);
239
g_message ("Did not receive a reply from the screensaver.");
242
dbus_message_unref (reply);
246
reply = screensaver_send_message_bool (connection, "setThrottleEnabled", FALSE);
248
g_message ("Did not receive a reply from the screensaver.");
251
dbus_message_unref (reply);
255
g_main_loop_quit (loop);
264
DBusConnection *connection;
265
DBusError dbus_error;
266
GOptionContext *context;
268
GError *error = NULL;
271
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
272
# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
273
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
275
textdomain (GETTEXT_PACKAGE);
280
g_set_prgname (argv[0]);
282
if (setlocale (LC_ALL, "") == NULL)
283
g_warning ("Locale not understood by C library, internationalization will not work\n");
285
context = g_option_context_new (NULL);
286
g_option_context_set_ignore_unknown_options (context, TRUE);
287
g_option_context_add_main_entries (context, entries, NULL);
288
retval = g_option_context_parse (context, &argc, &argv, &error);
290
g_option_context_free (context);
293
g_warning ("%s", error->message);
294
g_error_free (error);
299
g_print ("%s %s\n", argv [0], VERSION);
303
dbus_error_init (&dbus_error);
304
connection = dbus_bus_get (DBUS_BUS_SESSION, &dbus_error);
306
g_message ("Failed to connect to the D-BUS daemon: %s", dbus_error.message);
307
dbus_error_free (&dbus_error);
311
dbus_connection_setup_with_g_main (connection, NULL);
313
if (! screensaver_is_running (connection)) {
314
g_message ("Screensaver is not running!");
318
g_idle_add ((GSourceFunc)do_command, connection);
320
loop = g_main_loop_new (NULL, FALSE);
321
g_main_loop_run (loop);
323
dbus_connection_disconnect (connection);