summaryrefslogtreecommitdiff
path: root/src/mate-screensaver-command.c
blob: d4f765757b8084563b120cb1982240cff54c10ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2004-2006 William Jon McCann <mccann@jhu.edu>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * Authors: William Jon McCann <mccann@jhu.edu>
 *
 */

#include "config.h"

#include <stdlib.h>
#include <locale.h>
#include <glib.h>
#include <glib/gi18n.h>

#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#define GS_SERVICE   "org.mate.ScreenSaver"
#define GS_PATH      "/org/mate/ScreenSaver"
#define GS_INTERFACE "org.mate.ScreenSaver"

/* this is for dbus < 0.3 */
#if ((DBUS_VERSION_MAJOR == 0) && (DBUS_VERSION_MINOR < 30))
#define dbus_bus_name_has_owner(connection, name, err) dbus_bus_service_exists(connection, name, err)
#endif

static gboolean do_quit       = FALSE;
static gboolean do_lock       = FALSE;
static gboolean do_cycle      = FALSE;
static gboolean do_activate   = FALSE;
static gboolean do_deactivate = FALSE;
static gboolean do_version    = FALSE;
static gboolean do_poke       = FALSE;
static gboolean do_inhibit    = FALSE;

static gboolean do_query      = FALSE;
static gboolean do_time       = FALSE;

static char    *inhibit_reason      = NULL;
static char    *inhibit_application = NULL;

static GOptionEntry entries [] =
{
	{
		"exit", 0, 0, G_OPTION_ARG_NONE, &do_quit,
		N_("Causes the screensaver to exit gracefully"), NULL
	},
	{
		"query", 'q', 0, G_OPTION_ARG_NONE, &do_query,
		N_("Query the state of the screensaver"), NULL
	},
	{
		"time", 't', 0, G_OPTION_ARG_NONE, &do_time,
		N_("Query the length of time the screensaver has been active"), NULL
	},
	{
		"lock", 'l', 0, G_OPTION_ARG_NONE, &do_lock,
		N_("Tells the running screensaver process to lock the screen immediately"), NULL
	},
	{
		"cycle", 'c', 0, G_OPTION_ARG_NONE, &do_cycle,
		N_("If the screensaver is active then switch to another graphics demo"), NULL
	},
	{
		"activate", 'a', 0, G_OPTION_ARG_NONE, &do_activate,
		N_("Turn the screensaver on (blank the screen)"), NULL
	},
	{
		"deactivate", 'd', 0, G_OPTION_ARG_NONE, &do_deactivate,
		N_("If the screensaver is active then deactivate it (un-blank the screen)"), NULL
	},
	{
		"poke", 'p', 0, G_OPTION_ARG_NONE, &do_poke,
		N_("Poke the running screensaver to simulate user activity"), NULL
	},
	{
		"inhibit", 'i', 0, G_OPTION_ARG_NONE, &do_inhibit,
		N_("Inhibit the screensaver from activating.  Command blocks while inhibit is active."), NULL
	},
	{
		"application-name", 'n', 0, G_OPTION_ARG_STRING, &inhibit_application,
		N_("The calling application that is inhibiting the screensaver"), NULL
	},
	{
		"reason", 'r', 0, G_OPTION_ARG_STRING, &inhibit_reason,
		N_("The reason for inhibiting the screensaver"), NULL
	},
	{
		"version", 'V', 0, G_OPTION_ARG_NONE, &do_version,
		N_("Version of this application"), NULL
	},
	{ NULL }
};

static GMainLoop *loop = NULL;

static gboolean
screensaver_is_running (DBusConnection *connection)
{
	DBusError               error;
	gboolean                exists;

	g_return_val_if_fail (connection != NULL, FALSE);

	dbus_error_init (&error);
	exists = dbus_bus_name_has_owner (connection, GS_SERVICE, &error);
	if (dbus_error_is_set (&error))
		dbus_error_free (&error);

	return exists;
}

static DBusMessage *
screensaver_send_message_inhibit (DBusConnection *connection,
                                  const char     *application,
                                  const char     *reason)
{
	DBusMessage    *message;
	DBusMessage    *reply;
	DBusError       error;
	DBusMessageIter iter;

	g_return_val_if_fail (connection != NULL, NULL);

	dbus_error_init (&error);

	message = dbus_message_new_method_call (GS_SERVICE, GS_PATH, GS_INTERFACE, "Inhibit");
	if (message == NULL)
	{
		g_warning ("Couldn't allocate the dbus message");
		return NULL;
	}

	dbus_message_iter_init_append (message, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &application);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &reason);

	reply = dbus_connection_send_with_reply_and_block (connection,
	        message,
	        -1, &error);
	if (dbus_error_is_set (&error))
	{
		g_warning ("%s raised:\n %s\n\n", error.name, error.message);
		reply = NULL;
	}

	dbus_connection_flush (connection);

	dbus_message_unref (message);
	dbus_error_free (&error);

	return reply;
}

static DBusMessage *
screensaver_send_message_bool (DBusConnection *connection,
                               const char     *name,
                               gboolean        value)
{
	DBusMessage    *message;
	DBusMessage    *reply;
	DBusError       error;
	DBusMessageIter iter;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	dbus_error_init (&error);

	message = dbus_message_new_method_call (GS_SERVICE, GS_PATH, GS_INTERFACE, name);
	if (message == NULL)
	{
		g_warning ("Couldn't allocate the dbus message");
		return NULL;
	}

	dbus_message_iter_init_append (message, &iter);
	dbus_message_iter_append_basic (&iter, DBUS_TYPE_BOOLEAN, &value);

	reply = dbus_connection_send_with_reply_and_block (connection,
	        message,
	        -1, &error);
	if (dbus_error_is_set (&error))
	{
		g_warning ("%s raised:\n %s\n\n", error.name, error.message);
		reply = NULL;
	}

	dbus_connection_flush (connection);

	dbus_message_unref (message);
	dbus_error_free (&error);

	return reply;
}

static DBusMessage *
screensaver_send_message_void (DBusConnection *connection,
                               const char     *name,
                               gboolean        expect_reply)
{
	DBusMessage *message;
	DBusMessage *reply;
	DBusError    error;

	g_return_val_if_fail (connection != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);

	dbus_error_init (&error);

	message = dbus_message_new_method_call (GS_SERVICE, GS_PATH, GS_INTERFACE, name);
	if (message == NULL)
	{
		g_warning ("Couldn't allocate the dbus message");
		return NULL;
	}

	if (! expect_reply)
	{
		if (!dbus_connection_send (connection, message, NULL))
			g_warning ("could not send message");
		reply = NULL;
	}
	else
	{
		reply = dbus_connection_send_with_reply_and_block (connection,
		        message,
		        -1, &error);
		if (dbus_error_is_set (&error))
		{
			g_warning ("%s raised:\n %s\n\n", error.name, error.message);
			reply = NULL;
		}
	}
	dbus_connection_flush (connection);

	dbus_message_unref (message);
	dbus_error_free (&error);

	return reply;
}

static char **
get_string_from_iter (DBusMessageIter *iter,
                      int             *num_elements)
{
	int    count;
	char **buffer;

	if (num_elements != NULL)
	{
		*num_elements = 0;
	}

	count = 0;
	buffer = (char **)malloc (sizeof (char *) * 8);

	if (buffer == NULL)
	{
		goto oom;
	}

	buffer[0] = NULL;
	while (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_STRING)
	{
		const char *value;
		char       *str;

		if ((count % 8) == 0 && count != 0)
		{
			buffer = realloc (buffer, sizeof (char *) * (count + 8));
			if (buffer == NULL)
			{
				goto oom;
			}
		}

		dbus_message_iter_get_basic (iter, &value);
		str = strdup (value);
		if (str == NULL)
		{
			goto oom;
		}

		buffer[count] = str;

		dbus_message_iter_next (iter);
		count++;
	}

	if ((count % 8) == 0)
	{
		buffer = realloc (buffer, sizeof (char *) * (count + 1));
		if (buffer == NULL)
		{
			goto oom;
		}
	}

	buffer[count] = NULL;
	if (num_elements != NULL)
	{
		*num_elements = count;
	}
	return buffer;

oom:
	g_debug ("%s %d : error allocating memory\n", __FILE__, __LINE__);
	return NULL;

}

static gboolean
do_command (DBusConnection *connection)
{
	DBusMessage *reply;

	if (do_quit)
	{
		reply = screensaver_send_message_void (connection, "Quit", FALSE);
		goto done;
	}

	if (do_query)
	{
		DBusMessageIter iter;
		DBusMessageIter array;
		dbus_bool_t     v;

		reply = screensaver_send_message_void (connection, "GetActive", TRUE);
		if (! reply)
		{
			g_message ("Did not receive a reply from the screensaver.");
			goto done;
		}

		dbus_message_iter_init (reply, &iter);
		dbus_message_iter_get_basic (&iter, &v);
		g_print (_("The screensaver is %s\n"), v ? _("active") : _("inactive"));

		dbus_message_unref (reply);

		reply = screensaver_send_message_void (connection, "GetInhibitors", TRUE);
		if (! reply)
		{
			g_message ("Did not receive a reply from screensaver.");
			goto done;
		}

		dbus_message_iter_init (reply, &iter);
		dbus_message_iter_recurse (&iter, &array);

		if (dbus_message_iter_get_arg_type (&array) == DBUS_TYPE_INVALID)
		{
			g_print (_("The screensaver is not inhibited\n"));
		}
		else
		{
			char **inhibitors;
			int    i;
			int    num;

			g_print (_("The screensaver is being inhibited by:\n"));
			inhibitors = get_string_from_iter (&array, &num);
			for (i = 0; i < num; i++)
			{
				g_print ("\t%s\n", inhibitors[i]);
			}
			g_strfreev (inhibitors);
		}

		dbus_message_unref (reply);
	}

	if (do_time)
	{
		DBusMessageIter iter;
		dbus_bool_t     v;
		dbus_int32_t    t;

		reply = screensaver_send_message_void (connection, "GetActive", TRUE);
		if (! reply)
		{
			g_message ("Did not receive a reply from the screensaver.");
			goto done;
		}

		dbus_message_iter_init (reply, &iter);
		dbus_message_iter_get_basic (&iter, &v);
		dbus_message_unref (reply);

		if (v)
		{

			reply = screensaver_send_message_void (connection, "GetActiveTime", TRUE);
			dbus_message_iter_init (reply, &iter);
			dbus_message_iter_get_basic (&iter, &t);
			g_print (_("The screensaver has been active for %d seconds.\n"), t);

			dbus_message_unref (reply);
		}
		else
		{
			g_print (_("The screensaver is not currently active.\n"));
		}
	}

	if (do_lock)
	{
		reply = screensaver_send_message_void (connection, "Lock", FALSE);
	}

	if (do_cycle)
	{
		reply = screensaver_send_message_void (connection, "Cycle", FALSE);
	}

	if (do_poke)
	{
		reply = screensaver_send_message_void (connection, "SimulateUserActivity", FALSE);
	}

	if (do_activate)
	{
		reply = screensaver_send_message_bool (connection, "SetActive", TRUE);
		if (! reply)
		{
			g_message ("Did not receive a reply from the screensaver.");
			goto done;
		}
		dbus_message_unref (reply);
	}

	if (do_deactivate)
	{
		reply = screensaver_send_message_bool (connection, "SetActive", FALSE);
		if (! reply)
		{
			g_message ("Did not receive a reply from the screensaver.");
			goto done;
		}
		dbus_message_unref (reply);
	}

	if (do_inhibit)
	{
		reply = screensaver_send_message_inhibit (connection,
		        inhibit_application ? inhibit_application : "Unknown",
		        inhibit_reason ? inhibit_reason : "Unknown");
		if (! reply)
		{
			g_message ("Did not receive a reply from the screensaver.");
			goto done;
		}
		dbus_message_unref (reply);

		return FALSE;
	}

done:
	g_main_loop_quit (loop);

	return FALSE;
}

int
main (int    argc,
      char **argv)
{
	DBusConnection *connection;
	DBusError       dbus_error;
	GOptionContext *context;
	gboolean        retval;
	GError         *error = NULL;

#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR);
# ifdef HAVE_BIND_TEXTDOMAIN_CODESET
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
# endif
	textdomain (GETTEXT_PACKAGE);
#endif

	g_set_prgname (argv[0]);

	if (setlocale (LC_ALL, "") == NULL)
		g_warning ("Locale not understood by C library, internationalization will not work\n");

	context = g_option_context_new (NULL);
	g_option_context_add_main_entries (context, entries, NULL);
	retval = g_option_context_parse (context, &argc, &argv, &error);

	g_option_context_free (context);

	if (! retval)
	{
		g_warning ("%s", error->message);
		g_error_free (error);
		exit (1);
	}

	if (do_version)
	{
		g_print ("%s %s\n", argv [0], VERSION);
		exit (1);
	}

	dbus_error_init (&dbus_error);
	connection = dbus_bus_get (DBUS_BUS_SESSION, &dbus_error);
	if (! connection)
	{
		g_message ("Failed to connect to the D-BUS daemon: %s", dbus_error.message);
		dbus_error_free (&dbus_error);
		exit (1);
	}

	dbus_connection_setup_with_g_main (connection, NULL);

	if (! screensaver_is_running (connection))
	{
		g_message ("Screensaver is not running!");
		exit (1);
	}

	g_idle_add ((GSourceFunc)do_command, connection);

	loop = g_main_loop_new (NULL, FALSE);
	g_main_loop_run (loop);

	return 0;
}