-
Notifications
You must be signed in to change notification settings - Fork 29
/
dbus-session.c
146 lines (115 loc) · 3.78 KB
/
dbus-session.c
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
/*
* MacBook automatic light sensor daemon
* Copyright 2011 Pau Oliva Fora <pof@eslack.org>
*
* Some portions of this file taken from consolekit code:
* Copyright (C) 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.
*
*/
#include <stdlib.h>
#include <gio/gio.h>
#include <systemd/sd-login.h>
#define SD_NAME "org.freedesktop.login1"
#define SD_MANAGER_PATH "/org/freedesktop/login1"
#define SD_MANAGER_INTERFACE "org.freedesktop.login1.Manager"
#define SD_SESSION_INTERFACE "org.freedesktop.login1.Session"
int get_session_active (GDBusProxy *proxy)
{
GVariant *value;
char *state;
value = g_dbus_proxy_get_cached_property (proxy, "State");
if (!value)
return FALSE;
g_variant_get (value, "&s", &state);
g_variant_unref (value);
if (g_strcmp0 (state, "active") == 0) {
return TRUE;
} else {
return FALSE;
}
}
GDBusProxy* get_dbus_proxy_session(GDBusConnection *connection, GDBusProxy *proxy_manager)
{
GDBusProxy *proxy;
GError *error = NULL;
char *session;
GVariant *res;
char *ssid;
sd_pid_get_session (getpid (), &session);
res = g_dbus_proxy_call_sync (proxy_manager,
"GetSession",
g_variant_new("(s)", session),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
&error);
if (res == NULL) {
g_warning ("%s failed: %s", "GetSession", error->message);
g_error_free (error);
}
// Get the object path from the introspection data
g_variant_get (res, "(&o)", &ssid);
proxy = g_dbus_proxy_new_sync (connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
SD_NAME,
ssid,
SD_SESSION_INTERFACE,
NULL,
&error);
if (proxy == NULL) {
g_warning ("%s: Could not get dbus session proxy: %s", __func__, error->message);
g_error_free (error);
exit (1);
}
g_free (session);
g_free (ssid);
return proxy;
}
GDBusProxy* get_dbus_proxy_manager(GDBusConnection *connection)
{
GDBusProxy *proxy;
GError *error;
proxy = g_dbus_proxy_new_sync (connection,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
SD_NAME,
SD_MANAGER_PATH,
SD_MANAGER_INTERFACE,
NULL,
&error);
if (proxy == NULL) {
g_warning ("%s: Could not get dbus manager proxy: %s", __func__, error->message);
g_error_free (error);
exit (1);
}
return proxy;
}
GDBusConnection* get_dbus_connection()
{
GDBusConnection *connection;
GOptionContext *context;
gboolean retval;
GError *error = NULL;
context = g_option_context_new (NULL);
retval = g_option_context_parse (context, NULL, NULL, &error);
g_option_context_free (context);
if (! retval) {
g_warning ("%s", error->message);
g_error_free (error);
exit(1);
}
error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
if (connection == NULL) {
g_message ("%s: Failed to connect to the D-Bus daemon: %s", __func__, error->message);
g_error_free (error);
exit (1);
}
return connection;
}