aboutsummaryrefslogtreecommitdiff
path: root/src/lock-helper.c
blob: b2ae7f0cf6b7d16bd7ce6719d8cf5609c7b1b287 (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

#include <dbus/dbus-glib.h>
#include "lock-helper.h"

static DBusGProxy * gss_proxy = NULL;
static GMainLoop * gss_mainloop = NULL;

static DBusGProxy * gdm_settings_proxy = NULL;
static gboolean gdm_auto_login = FALSE;
static const gchar * gdm_auto_login_string = "daemon/AutomaticLoginEnable";

static gboolean is_guest = FALSE;

static gdm_autologin_cb_t gdm_autologin_cb = NULL;

/* Setting up a call back */
void
lock_screen_gdm_cb_set (gdm_autologin_cb_t cb)
{
	if (gdm_autologin_cb) {
		g_warning("Already had a callback, setting up a new one...");
	}

	gdm_autologin_cb = cb;
	return;
}

/* This is our logic on whether the screen should be locked
   or not.  It effects everything else. */
gboolean
will_lock_screen (void)
{
	if (gdm_auto_login) {
		return FALSE;
	}
	if (is_guest) {
		return FALSE;
	}

	return TRUE;
}

/* Respond to the signal of autologin changing to see if the
   setting for timed login changes. */
static void
gdm_settings_change (DBusGProxy * proxy, const gchar * value, const gchar * old, const gchar * new, gpointer data)
{
	if (g_strcmp0(value, gdm_auto_login_string)) {
		/* This is not a setting that we care about,
		   there is only one. */
		return;
	}
	g_debug("GDM Settings change: %s", new);

	if (g_strcmp0(new, "true") == 0) {
		gdm_auto_login = TRUE;
	} else {
		gdm_auto_login = FALSE;
	}

	if (gdm_autologin_cb != NULL) {
		gdm_autologin_cb();
	}

	return;
}

/* Get back the data from querying to see if there is auto
   login enabled in GDM */
static void
gdm_get_autologin (DBusGProxy * proxy, DBusGProxyCall * call, gpointer data)
{
	GError * error = NULL;
	gchar * value = NULL;

	if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_STRING, &value, G_TYPE_INVALID)) {
		g_warning("Unable to get autologin setting: %s", error != NULL ? error->message : "null");
		g_error_free(error);
		return;
	}

	g_return_if_fail(value != NULL);
	gdm_settings_change(proxy, gdm_auto_login_string, NULL, value, NULL);

	return;
}

/* Sets up the proxy and queries for the setting to know
   whether we're doing an autologin. */
static void
build_gdm_proxy (void)
{
	g_return_if_fail(gdm_settings_proxy == NULL);

	/* Grab the system bus */
	DBusGConnection * bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
	g_return_if_fail(bus != NULL);

	/* Get the settings proxy */
	gdm_settings_proxy = dbus_g_proxy_new_for_name_owner(bus,
	                                                     "org.gnome.DisplayManager",
	                                                     "/org/gnome/DisplayManager/Settings",
	                                                     "org.gnome.DisplayManager.Settings", NULL);
	g_return_if_fail(gdm_settings_proxy != NULL);

	/* Signal for value changed */
	dbus_g_proxy_add_signal(gdm_settings_proxy,
	                        "ValueChanged",
	                        G_TYPE_STRING,
	                        G_TYPE_STRING,
	                        G_TYPE_STRING,
	                        G_TYPE_INVALID);
	dbus_g_proxy_connect_signal(gdm_settings_proxy,
	                            "ValueChanged",
	                            G_CALLBACK(gdm_settings_change),
	                            NULL,
	                            NULL);

	/* Start to get the initial value */
	dbus_g_proxy_begin_call(gdm_settings_proxy,
	                        "GetValue",
	                        gdm_get_autologin,
	                        NULL,
	                        NULL,
	                        G_TYPE_STRING,
	                        gdm_auto_login_string,
	                        G_TYPE_INVALID);

	return;
}

/* When the screensave go active, if we've got a mainloop
   running we should quit it. */
static void
gss_active_changed (DBusGProxy * proxy, gboolean active, gpointer data)
{
	if (active && gss_mainloop != NULL) {
		g_main_loop_quit(gss_mainloop);
	}

	return;
}

/* Build the gss proxy and set up it's signals */
void
build_gss_proxy (void)
{
	DBusGConnection * session_bus = dbus_g_bus_get(DBUS_BUS_SESSION, NULL);
	g_return_if_fail(session_bus != NULL);

	gss_proxy = dbus_g_proxy_new_for_name_owner(session_bus,
	                                            "org.gnome.ScreenSaver",
	                                            "/",
	                                            "org.gnome.ScreenSaver",
	                                            NULL);
	g_return_if_fail(gss_proxy != NULL);

	dbus_g_proxy_add_signal(gss_proxy, "ActiveChanged", G_TYPE_BOOLEAN, G_TYPE_INVALID);
	dbus_g_proxy_connect_signal(gss_proxy, "ActiveChanged", G_CALLBACK(gss_active_changed), NULL, NULL);

	return;
}

/* This is a timeout, we only want to wait for the screen to
   lock for a little bit, but not forever. */
static gboolean
activate_timeout (gpointer data)
{
	guint * address = (guint *)data;
	*address = 0;

	if (gss_mainloop != NULL) {
		g_main_loop_quit(gss_mainloop);
	}
	
	return FALSE;
}

/* A fun little function to actually lock the screen.  If,
   that's what you want, let's do it! */
void
lock_screen (DbusmenuMenuitem * mi, gpointer data)
{
	g_debug("Lock Screen");
	if (!will_lock_screen()) {
		g_debug("\tGDM set to autologin, blocking lock");
		return;
	}

	g_return_if_fail(gss_proxy != NULL);

	dbus_g_proxy_call_no_reply(gss_proxy,
	                           "Lock",
	                           G_TYPE_INVALID,
	                           G_TYPE_INVALID);

	if (gss_mainloop == NULL) {
		gss_mainloop = g_main_loop_new(NULL, FALSE);
	}

	guint timer = g_timeout_add_seconds(1, activate_timeout, &timer);

	g_main_loop_run(gss_mainloop);

	if (timer != 0) {
		g_source_remove(timer);
	}

	return;
}

/* Do what it takes to make the lock screen function work
   and be happy. */
gboolean
lock_screen_setup (gpointer data)
{
	if (!g_strcmp0(g_get_user_name(), "guest")) {
		is_guest = TRUE;
	}

	build_gdm_proxy();
	build_gss_proxy();

	return FALSE;
}