File: egg-console-kit.c

package info (click to toggle)
ukui-power-manager 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,612 kB
  • sloc: ansic: 16,522; makefile: 1,036; xml: 107; sh: 19
file content (470 lines) | stat: -rw-r--r-- 13,349 bytes parent folder | download | duplicates (4)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006-2008 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <dbus/dbus.h>

#include "egg-debug.h"
#include "egg-console-kit.h"

static void     egg_console_kit_finalize	(GObject		*object);

#define EGG_CONSOLE_KIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_CONSOLE_KIT, EggConsoleKitPrivate))

#define CONSOLEKIT_NAME			"org.freedesktop.ConsoleKit"
#define CONSOLEKIT_PATH			"/org/freedesktop/ConsoleKit"
#define CONSOLEKIT_INTERFACE		"org.freedesktop.ConsoleKit"

#define CONSOLEKIT_MANAGER_PATH	 	"/org/freedesktop/ConsoleKit/Manager"
#define CONSOLEKIT_MANAGER_INTERFACE    "org.freedesktop.ConsoleKit.Manager"
#define CONSOLEKIT_SEAT_INTERFACE       "org.freedesktop.ConsoleKit.Seat"
#define CONSOLEKIT_SESSION_INTERFACE    "org.freedesktop.ConsoleKit.Session"

struct EggConsoleKitPrivate
{
	DBusGConnection		*connection;
	DBusGProxy		*proxy_manager;
	DBusGProxy		*proxy_session;
	gchar			*session_id;
};

enum {
	EGG_CONSOLE_KIT_ACTIVE_CHANGED,
	EGG_CONSOLE_KIT_LAST_SIGNAL
};

static gpointer egg_console_kit_object = NULL;
static guint signals [EGG_CONSOLE_KIT_LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (EggConsoleKit, egg_console_kit, G_TYPE_OBJECT)

/**
 * egg_console_kit_restart:
 **/
gboolean
egg_console_kit_restart (EggConsoleKit *console, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "Restart", &error_local,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't restart: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}
	return ret;
}

/**
 * egg_console_kit_stop:
 **/
gboolean
egg_console_kit_stop (EggConsoleKit *console, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "Stop", &error_local,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't stop: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}
	return ret;
}

/**
 * egg_console_kit_suspend:
 **/
gboolean
egg_console_kit_suspend (EggConsoleKit *console, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "Suspend", &error_local,
				 G_TYPE_BOOLEAN, TRUE,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't suspend: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}
	return ret;
}

/**
 * egg_console_kit_hibernate:
 **/
gboolean
egg_console_kit_hibernate (EggConsoleKit *console, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "Hibernate", &error_local,
				 G_TYPE_BOOLEAN, TRUE,
				 G_TYPE_INVALID, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't hibernate: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}
	return ret;
}

/**
 * egg_console_kit_can_stop:
 **/
gboolean
egg_console_kit_can_stop (EggConsoleKit *console, gboolean *can_stop, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "CanStop", &error_local,
				 G_TYPE_INVALID,
				 G_TYPE_BOOLEAN, can_stop, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't do CanStop: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
		/* CanStop was only added in new versions of ConsoleKit,
		 * so assume true if this failed */
		*can_stop = TRUE;
	}
	return ret;
}

/**
 * egg_console_kit_can_restart:
 **/
gboolean
egg_console_kit_can_restart (EggConsoleKit *console, gboolean *can_restart, GError **error)
{
	gboolean ret;
	GError *error_local = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "CanRestart", &error_local,
				 G_TYPE_INVALID,
				 G_TYPE_BOOLEAN, can_restart, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't do CanRestart: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
		/* CanRestart was only added in new versions of ConsoleKit,
		 * so assume true if this failed */
		*can_restart = TRUE;
	}
	return ret;
}

/**
 * egg_console_kit_can_suspend:
 **/
gboolean
egg_console_kit_can_suspend (EggConsoleKit *console, gboolean *can_suspend, GError **error)
{
	GError *error_local = NULL;
	gboolean ret;
	gchar  *retval;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "CanSuspend", &error_local,
				 G_TYPE_INVALID,
				 G_TYPE_STRING, &retval, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't do CanSuspend: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}

	*can_suspend = g_strcmp0 (retval, "yes") == 0 ||
		       g_strcmp0 (retval, "challenge") == 0;

	g_free (retval);
	return ret;
}

/**
 * egg_console_kit_can_hibernate:
 **/

gboolean
egg_console_kit_can_hibernate (EggConsoleKit *console, gboolean *can_hibernate, GError **error)
{
	GError *error_local = NULL;
	gboolean ret;
	gchar  *retval;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);
	g_return_val_if_fail (console->priv->proxy_manager != NULL, FALSE);

	ret = dbus_g_proxy_call (console->priv->proxy_manager, "CanHibernate", &error_local,
				 G_TYPE_INVALID,
				 G_TYPE_STRING, &retval, G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Couldn't do CanHibernate: %s", error_local->message);
		if (error != NULL)
			*error = g_error_new (1, 0, "%s", error_local->message);
		g_error_free (error_local);
	}

	*can_hibernate = g_strcmp0 (retval, "yes") == 0 ||
	                 g_strcmp0 (retval, "challenge") == 0;
	return ret;
}

/**
 * egg_console_kit_is_local:
 *
 * Return value: Returns whether the session is local
 **/
gboolean
egg_console_kit_is_local (EggConsoleKit *console)
{
	gboolean ret = FALSE;
	gboolean value = FALSE;
	GError *error = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);

	/* maybe console kit does not know about our session */
	if (console->priv->proxy_session == NULL) {
		egg_warning ("no ConsoleKit session");
		goto out;
	}

	/* is our session local */
	ret = dbus_g_proxy_call (console->priv->proxy_session, "IsLocal", &error, G_TYPE_INVALID,
				 G_TYPE_BOOLEAN, &value, G_TYPE_INVALID);
	if (!ret) {
		g_warning ("IsLocal failed: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* return value only if we successed */
	ret = value;
out:
	return ret;
}

/**
 * egg_console_kit_is_active:
 *
 * Return value: Returns whether the session is active on the Seat that it is attached to.
 **/
gboolean
egg_console_kit_is_active (EggConsoleKit *console)
{
	gboolean ret = FALSE;
	gboolean value = FALSE;
	GError *error = NULL;

	g_return_val_if_fail (EGG_IS_CONSOLE_KIT (console), FALSE);

	/* maybe console kit does not know about our session */
	if (console->priv->proxy_session == NULL) {
		egg_warning ("no ConsoleKit session");
		goto out;
	}

	/* is our session active */
	ret = dbus_g_proxy_call (console->priv->proxy_session, "IsActive", &error, G_TYPE_INVALID,
				 G_TYPE_BOOLEAN, &value, G_TYPE_INVALID);
	if (!ret) {
		g_warning ("IsActive failed: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* return value only if we successed */
	ret = value;
out:
	return ret;
}

/**
 * egg_console_kit_active_changed_cb:
 **/
static void
egg_console_kit_active_changed_cb (DBusGProxy *proxy, gboolean active, EggConsoleKit *console)
{
	egg_debug ("emitting active: %i", active);
	g_signal_emit (console, signals [EGG_CONSOLE_KIT_ACTIVE_CHANGED], 0, active);
}

/**
 * egg_console_kit_class_init:
 * @klass: The EggConsoleKitClass
 **/
static void
egg_console_kit_class_init (EggConsoleKitClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = egg_console_kit_finalize;
	g_type_class_add_private (klass, sizeof (EggConsoleKitPrivate));
	signals [EGG_CONSOLE_KIT_ACTIVE_CHANGED] =
		g_signal_new ("active-changed",
			      G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
			      G_STRUCT_OFFSET (EggConsoleKitClass, active_changed),
			      NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
			      G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}

/**
 * egg_console_kit_init:
 **/
static void
egg_console_kit_init (EggConsoleKit *console)
{
	gboolean ret;
	GError *error = NULL;
	guint32 pid;

	console->priv = EGG_CONSOLE_KIT_GET_PRIVATE (console);
	console->priv->proxy_manager = NULL;
	console->priv->session_id = NULL;

	/* connect to D-Bus */
	console->priv->connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
	if (console->priv->connection == NULL) {
		egg_warning ("Failed to connect to the D-Bus daemon: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* connect to ConsoleKit */
	console->priv->proxy_manager =
		dbus_g_proxy_new_for_name (console->priv->connection, CONSOLEKIT_NAME,
					   CONSOLEKIT_MANAGER_PATH, CONSOLEKIT_MANAGER_INTERFACE);
	if (console->priv->proxy_manager == NULL) {
		egg_warning ("cannot connect to ConsoleKit");
		goto out;
	}

	/* get the session we are running in */
	pid = getpid ();
	ret = dbus_g_proxy_call (console->priv->proxy_manager, "GetSessionForUnixProcess", &error,
				 G_TYPE_UINT, pid,
				 G_TYPE_INVALID,
				 DBUS_TYPE_G_OBJECT_PATH, &console->priv->session_id,
				 G_TYPE_INVALID);
	if (!ret) {
		egg_warning ("Failed to get session for pid %i: %s", pid, error->message);
		g_error_free (error);
		goto out;
	}
	egg_debug ("ConsoleKit session ID: %s", console->priv->session_id);

	/* connect to session */
	console->priv->proxy_session =
		dbus_g_proxy_new_for_name (console->priv->connection, CONSOLEKIT_NAME,
					   console->priv->session_id, CONSOLEKIT_SESSION_INTERFACE);
	if (console->priv->proxy_session == NULL) {
		egg_warning ("cannot connect to: %s", console->priv->session_id);
		goto out;
	}
	dbus_g_proxy_add_signal (console->priv->proxy_session, "ActiveChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal (console->priv->proxy_session, "ActiveChanged",
				     G_CALLBACK (egg_console_kit_active_changed_cb), console, NULL);

out:
	return;
}

/**
 * egg_console_kit_finalize:
 * @object: The object to finalize
 **/
static void
egg_console_kit_finalize (GObject *object)
{
	EggConsoleKit *console;

	g_return_if_fail (EGG_IS_CONSOLE_KIT (object));

	console = EGG_CONSOLE_KIT (object);

	g_return_if_fail (console->priv != NULL);
	if (console->priv->proxy_manager != NULL)
		g_object_unref (console->priv->proxy_manager);
	if (console->priv->proxy_session != NULL)
		g_object_unref (console->priv->proxy_session);
	g_free (console->priv->session_id);

	G_OBJECT_CLASS (egg_console_kit_parent_class)->finalize (object);
}

/**
 * egg_console_kit_new:
 *
 * Return value: a new EggConsoleKit object.
 **/
EggConsoleKit *
egg_console_kit_new (void)
{
	if (egg_console_kit_object != NULL) {
		g_object_ref (egg_console_kit_object);
	} else {
		egg_console_kit_object = g_object_new (EGG_TYPE_CONSOLE_KIT, NULL);
		g_object_add_weak_pointer (egg_console_kit_object, &egg_console_kit_object);
	}

	return EGG_CONSOLE_KIT (egg_console_kit_object);
}