~ubuntu-branches/ubuntu/dapper/gnome-screensaver/dapper

« back to all changes in this revision

Viewing changes to src/gnome-screensaver-command.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2005-10-10 00:18:18 UTC
  • Revision ID: james.westby@ubuntu.com-20051010001818-3mujs05r8rht7xi1
Tags: upstream-0.0.15
ImportĀ upstreamĀ versionĀ 0.0.15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2004-2005 William Jon McCann <mccann@jhu.edu>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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
 
18
 * 02111-1307, USA.
 
19
 *
 
20
 * Authors: William Jon McCann <mccann@jhu.edu>
 
21
 *
 
22
 */
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include <stdlib.h>
 
27
#include <glib.h>
 
28
#include <glib/gi18n.h>
 
29
 
 
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>
 
34
 
 
35
#define GS_SERVICE   "org.gnome.screensaver"
 
36
#define GS_PATH      "/org/gnome/screensaver"
 
37
#define GS_INTERFACE "org.gnome.screensaver"
 
38
 
 
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)
 
42
#endif
 
43
 
 
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;
 
53
 
 
54
static gboolean do_query      = FALSE;
 
55
 
 
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 },
 
77
        { NULL }
 
78
};
 
79
 
 
80
static GMainLoop *loop = NULL;
 
81
 
 
82
static gboolean
 
83
screensaver_is_running (DBusConnection *connection)
 
84
{
 
85
        DBusError               error;
 
86
        gboolean                exists;
 
87
 
 
88
        g_return_val_if_fail (connection != NULL, FALSE);
 
89
 
 
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);
 
94
 
 
95
        return exists;
 
96
}
 
97
 
 
98
static DBusMessage *
 
99
screensaver_send_message_bool (DBusConnection *connection,
 
100
                               const char     *name,
 
101
                               gboolean        value)
 
102
{
 
103
        DBusMessage    *message;
 
104
        DBusMessage    *reply;
 
105
        DBusError       error;
 
106
        DBusMessageIter iter;
 
107
 
 
108
        g_return_val_if_fail (connection != NULL, NULL);
 
109
        g_return_val_if_fail (name != NULL, NULL);
 
110
 
 
111
        dbus_error_init (&error);
 
112
 
 
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");
 
116
                return NULL;
 
117
        }
 
118
 
 
119
        dbus_message_iter_init_append (message, &iter);
 
120
        dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value);
 
121
 
 
122
        reply = dbus_connection_send_with_reply_and_block (connection,
 
123
                                                           message,
 
124
                                                           -1, &error);
 
125
        if (dbus_error_is_set (&error)) {
 
126
                g_warning ("%s raised:\n %s\n\n", error.name, error.message);
 
127
                reply = NULL;
 
128
        }
 
129
 
 
130
        dbus_connection_flush (connection);
 
131
 
 
132
        dbus_message_unref (message);
 
133
        dbus_error_free (&error);
 
134
 
 
135
        return reply;
 
136
}
 
137
 
 
138
static DBusMessage *
 
139
screensaver_send_message_void (DBusConnection *connection,
 
140
                               const char     *name,
 
141
                               gboolean        expect_reply)
 
142
{
 
143
        DBusMessage *message;
 
144
        DBusMessage *reply;
 
145
        DBusError    error;
 
146
 
 
147
        g_return_val_if_fail (connection != NULL, NULL);
 
148
        g_return_val_if_fail (name != NULL, NULL);
 
149
 
 
150
        dbus_error_init (&error);
 
151
 
 
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");
 
155
                return NULL;
 
156
        }
 
157
 
 
158
        if (! expect_reply) {
 
159
                if (!dbus_connection_send (connection, message, NULL))
 
160
                        g_warning ("could not send message");
 
161
                reply = NULL;
 
162
        } else {
 
163
                reply = dbus_connection_send_with_reply_and_block (connection,
 
164
                                                                   message,
 
165
                                                                   -1, &error);
 
166
                if (dbus_error_is_set (&error)) {
 
167
                        g_warning ("%s raised:\n %s\n\n", error.name, error.message);
 
168
                        reply = NULL;
 
169
                }
 
170
        }
 
171
        dbus_connection_flush (connection);
 
172
 
 
173
        dbus_message_unref (message);
 
174
        dbus_error_free (&error);
 
175
 
 
176
        return reply;
 
177
}
 
178
 
 
179
static gboolean
 
180
do_command (DBusConnection *connection)
 
181
{
 
182
        DBusMessage *reply;
 
183
 
 
184
        if (do_quit) {
 
185
                reply = screensaver_send_message_void (connection, "quit", FALSE);
 
186
                goto done;
 
187
        }
 
188
 
 
189
        if (do_query) {
 
190
                DBusMessageIter iter;
 
191
                dbus_bool_t     v;
 
192
 
 
193
                reply = screensaver_send_message_void (connection, "getActive", TRUE);
 
194
                if (! reply) {
 
195
                        g_message ("Did not receive a reply from the screensaver.");
 
196
                        goto done;
 
197
                }
 
198
 
 
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"));
 
202
 
 
203
                dbus_message_unref (reply);
 
204
        }
 
205
 
 
206
        if (do_lock) {
 
207
                reply = screensaver_send_message_void (connection, "lock", FALSE);
 
208
        }
 
209
 
 
210
        if (do_cycle) {
 
211
                reply = screensaver_send_message_void (connection, "cycle", FALSE);
 
212
        }
 
213
 
 
214
        if (do_poke) {
 
215
                reply = screensaver_send_message_void (connection, "poke", FALSE);
 
216
        }
 
217
 
 
218
        if (do_activate) {
 
219
                reply = screensaver_send_message_bool (connection, "setActive", TRUE);
 
220
                if (! reply) {
 
221
                        g_message ("Did not receive a reply from the screensaver.");
 
222
                        goto done;
 
223
                }
 
224
                dbus_message_unref (reply);
 
225
        }
 
226
 
 
227
        if (do_deactivate) {
 
228
                reply = screensaver_send_message_bool (connection, "setActive", FALSE);
 
229
                if (! reply) {
 
230
                        g_message ("Did not receive a reply from the screensaver.");
 
231
                        goto done;
 
232
                }
 
233
                dbus_message_unref (reply);
 
234
        }
 
235
 
 
236
        if (do_throttle) {
 
237
                reply = screensaver_send_message_bool (connection, "setThrottleEnabled", TRUE);
 
238
                if (! reply) {
 
239
                        g_message ("Did not receive a reply from the screensaver.");
 
240
                        goto done;
 
241
                }
 
242
                dbus_message_unref (reply);
 
243
        }
 
244
 
 
245
        if (do_unthrottle) {
 
246
                reply = screensaver_send_message_bool (connection, "setThrottleEnabled", FALSE);
 
247
                if (! reply) {
 
248
                        g_message ("Did not receive a reply from the screensaver.");
 
249
                        goto done;
 
250
                }
 
251
                dbus_message_unref (reply);
 
252
        }
 
253
 
 
254
 done:
 
255
        g_main_loop_quit (loop);
 
256
 
 
257
        return FALSE;
 
258
}
 
259
 
 
260
int
 
261
main (int    argc,
 
262
      char **argv)
 
263
{
 
264
        DBusConnection *connection;
 
265
        DBusError       dbus_error;        
 
266
        GOptionContext *context;
 
267
        gboolean        retval;
 
268
        GError         *error = NULL;
 
269
 
 
270
#ifdef ENABLE_NLS
 
271
        bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
 
272
# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
 
273
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
274
# endif 
 
275
        textdomain (GETTEXT_PACKAGE);
 
276
#endif 
 
277
 
 
278
        g_type_init ();
 
279
 
 
280
        g_set_prgname (argv[0]);
 
281
 
 
282
        if (setlocale (LC_ALL, "") == NULL)
 
283
                g_warning ("Locale not understood by C library, internationalization will not work\n");
 
284
 
 
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);
 
289
 
 
290
        g_option_context_free (context);
 
291
 
 
292
        if (! retval) {
 
293
                g_warning ("%s", error->message);
 
294
                g_error_free (error);
 
295
                exit (1);
 
296
        }
 
297
 
 
298
        if (do_version) {
 
299
                g_print ("%s %s\n", argv [0], VERSION);
 
300
                exit (1);
 
301
        }
 
302
 
 
303
        dbus_error_init (&dbus_error);
 
304
        connection = dbus_bus_get (DBUS_BUS_SESSION, &dbus_error);
 
305
        if (! connection) {
 
306
                g_message ("Failed to connect to the D-BUS daemon: %s", dbus_error.message);
 
307
                dbus_error_free (&dbus_error);
 
308
                exit (1);
 
309
        }
 
310
 
 
311
        dbus_connection_setup_with_g_main (connection, NULL);
 
312
 
 
313
        if (! screensaver_is_running (connection)) {
 
314
                g_message ("Screensaver is not running!");
 
315
                exit (1);
 
316
        }
 
317
 
 
318
        g_idle_add ((GSourceFunc)do_command, connection);
 
319
 
 
320
        loop = g_main_loop_new (NULL, FALSE);
 
321
        g_main_loop_run (loop);
 
322
 
 
323
        dbus_connection_disconnect (connection);
 
324
 
 
325
        return 0;
 
326
}