Mercurial > pidgin.yaz
comparison src/gtksession.c @ 12246:b7a51e68d0b8
[gaim-migrate @ 14548]
Make some things static and namespace session_init and session_end
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Mon, 28 Nov 2005 06:20:06 +0000 |
parents | |
children | bc249de5ea02 |
comparison
equal
deleted
inserted
replaced
12245:465ddcb3e9e8 | 12246:b7a51e68d0b8 |
---|---|
1 /* | |
2 * @file gtksession.c X Windows session management API | |
3 * @ingroup gtkui | |
4 * | |
5 * Gaim is the legal property of its developers, whose names are too numerous | |
6 * to list here. Please refer to the COPYRIGHT file distributed with this | |
7 * source distribution. | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License | |
20 * along with this program; if not, write to the Free Software | |
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
22 * | |
23 */ | |
24 #include "internal.h" | |
25 | |
26 #include "core.h" | |
27 #include "debug.h" | |
28 #include "eventloop.h" | |
29 | |
30 #ifdef USE_SM | |
31 | |
32 #include <X11/ICE/ICElib.h> | |
33 #include <X11/SM/SMlib.h> | |
34 #include <gdk/gdkx.h> | |
35 #include <unistd.h> | |
36 #include <fcntl.h> | |
37 | |
38 #define ERROR_LENGTH 512 | |
39 | |
40 static IceIOErrorHandler ice_installed_io_error_handler; | |
41 static SmcConn session = NULL; | |
42 static gchar *myself = NULL; | |
43 static gboolean had_first_save = FALSE; | |
44 static gboolean session_managed = FALSE; | |
45 | |
46 /* ICE belt'n'braces stuff */ | |
47 | |
48 struct ice_connection_info { | |
49 IceConn connection; | |
50 guint input_id; | |
51 }; | |
52 | |
53 static void ice_process_messages(gpointer data, gint fd, | |
54 GaimInputCondition condition) { | |
55 struct ice_connection_info *conninfo = (struct ice_connection_info*) data; | |
56 IceProcessMessagesStatus status; | |
57 | |
58 /* please don't block... please! */ | |
59 status = IceProcessMessages(conninfo->connection, NULL, NULL); | |
60 | |
61 if (status == IceProcessMessagesIOError) { | |
62 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
63 "ICE IO error, closing connection... "); | |
64 | |
65 /* IO error, please disconnect */ | |
66 IceSetShutdownNegotiation(conninfo->connection, False); | |
67 IceCloseConnection(conninfo->connection); | |
68 | |
69 gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); | |
70 | |
71 /* cancel the handler */ | |
72 gaim_input_remove(conninfo->input_id); | |
73 } | |
74 } | |
75 | |
76 static void ice_connection_watch(IceConn connection, IcePointer client_data, | |
77 Bool opening, IcePointer *watch_data) { | |
78 struct ice_connection_info *conninfo = NULL; | |
79 | |
80 if (opening) { | |
81 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
82 "Handling new ICE connection... "); | |
83 | |
84 /* ensure ICE connection is not passed to child processes */ | |
85 fcntl(IceConnectionNumber(connection), F_SETFD, FD_CLOEXEC); | |
86 | |
87 conninfo = g_new(struct ice_connection_info, 1); | |
88 conninfo->connection = connection; | |
89 | |
90 /* watch the connection */ | |
91 conninfo->input_id = gaim_input_add(IceConnectionNumber(connection), GAIM_INPUT_READ, | |
92 ice_process_messages, conninfo); | |
93 *watch_data = conninfo; | |
94 } else { | |
95 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
96 "Handling closed ICE connection... "); | |
97 | |
98 /* get the input ID back and stop watching it */ | |
99 conninfo = (struct ice_connection_info*) *watch_data; | |
100 gaim_input_remove(conninfo->input_id); | |
101 g_free(conninfo); | |
102 } | |
103 | |
104 gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); | |
105 } | |
106 | |
107 /* We call any handler installed before (or after) ice_init but | |
108 * avoid calling the default libICE handler which does an exit(). | |
109 * | |
110 * This means we do nothing by default, which is probably correct, | |
111 * the connection will get closed by libICE | |
112 */ | |
113 | |
114 static void ice_io_error_handler(IceConn connection) { | |
115 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
116 "Handling ICE IO error... "); | |
117 | |
118 if (ice_installed_io_error_handler) | |
119 (*ice_installed_io_error_handler)(connection); | |
120 | |
121 gaim_debug(GAIM_DEBUG_INFO, NULL, "done.\n"); | |
122 } | |
123 | |
124 static void ice_init() { | |
125 IceIOErrorHandler default_handler; | |
126 | |
127 ice_installed_io_error_handler = IceSetIOErrorHandler(NULL); | |
128 default_handler = IceSetIOErrorHandler(ice_io_error_handler); | |
129 | |
130 if (ice_installed_io_error_handler == default_handler) | |
131 ice_installed_io_error_handler = NULL; | |
132 | |
133 IceAddConnectionWatch(ice_connection_watch, NULL); | |
134 | |
135 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
136 "ICE initialized.\n"); | |
137 } | |
138 | |
139 /* my magic utility function */ | |
140 | |
141 static gchar **session_make_command(gchar *client_id, gchar *config_dir) { | |
142 gint i = 2; | |
143 gint j = 0; | |
144 gchar **ret; | |
145 | |
146 if (client_id) i += 2; | |
147 if (config_dir) i += 2; /* we will specify gaim's user dir */ | |
148 | |
149 ret = g_new(gchar *, i); | |
150 ret[j++] = g_strdup(myself); | |
151 | |
152 if (client_id) { | |
153 ret[j++] = g_strdup("--session"); | |
154 ret[j++] = g_strdup(client_id); | |
155 } | |
156 | |
157 if (config_dir) { | |
158 ret[j++] = g_strdup("--config"); | |
159 ret[j++] = g_strdup(config_dir); | |
160 } | |
161 | |
162 ret[j++] = NULL; | |
163 | |
164 return ret; | |
165 } | |
166 | |
167 /* SM callback handlers */ | |
168 | |
169 static void session_save_yourself(SmcConn conn, SmPointer data, int save_type, | |
170 Bool shutdown, int interact_style, Bool fast) { | |
171 if (had_first_save == FALSE && save_type == SmSaveLocal && | |
172 interact_style == SmInteractStyleNone && !shutdown && | |
173 !fast) { | |
174 /* this is just a dry run, spit it back */ | |
175 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
176 "Received first save_yourself\n"); | |
177 SmcSaveYourselfDone(conn, True); | |
178 had_first_save = TRUE; | |
179 return; | |
180 } | |
181 | |
182 /* tum ti tum... don't add anything else here without * | |
183 * reading SMlib.PS from an X.org ftp server near you */ | |
184 | |
185 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
186 "Received save_yourself\n"); | |
187 | |
188 if (save_type == SmSaveGlobal || save_type == SmSaveBoth) { | |
189 /* may as well do something ... */ | |
190 /* or not -- save_prefs(); */ | |
191 } | |
192 | |
193 SmcSaveYourselfDone(conn, True); | |
194 } | |
195 | |
196 static void session_die(SmcConn conn, SmPointer data) { | |
197 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
198 "Received die\n"); | |
199 gaim_core_quit(); | |
200 } | |
201 | |
202 static void session_save_complete(SmcConn conn, SmPointer data) { | |
203 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
204 "Received save_complete\n"); | |
205 } | |
206 | |
207 static void session_shutdown_cancelled(SmcConn conn, SmPointer data) { | |
208 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
209 "Received shutdown_cancelled\n"); | |
210 } | |
211 | |
212 /* utility functions stolen from Gnome-client */ | |
213 | |
214 static void session_set_value(SmcConn conn, gchar *name, char *type, | |
215 int num_vals, SmPropValue *vals) { | |
216 SmProp *proplist[1]; | |
217 SmProp prop; | |
218 | |
219 g_return_if_fail(conn); | |
220 | |
221 prop.name = name; | |
222 prop.type = type; | |
223 prop.num_vals = num_vals; | |
224 prop.vals = vals; | |
225 | |
226 proplist[0] = ∝ | |
227 SmcSetProperties(conn, 1, proplist); | |
228 } | |
229 | |
230 static void session_set_string(SmcConn conn, gchar *name, gchar *value) { | |
231 SmPropValue val; | |
232 | |
233 g_return_if_fail(name); | |
234 | |
235 val.length = strlen (value)+1; | |
236 val.value = value; | |
237 | |
238 session_set_value(conn, name, SmARRAY8, 1, &val); | |
239 } | |
240 | |
241 static void session_set_gchar(SmcConn conn, gchar *name, gchar value) { | |
242 SmPropValue val; | |
243 | |
244 g_return_if_fail(name); | |
245 | |
246 val.length = 1; | |
247 val.value = &value; | |
248 | |
249 session_set_value(conn, name, SmCARD8, 1, &val); | |
250 } | |
251 | |
252 static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) { | |
253 gint argc; | |
254 gchar **ptr; | |
255 gint i; | |
256 | |
257 SmPropValue *vals; | |
258 | |
259 g_return_if_fail (name); | |
260 | |
261 /* We count the number of elements in our array. */ | |
262 for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */; | |
263 | |
264 /* Now initialize the 'vals' array. */ | |
265 vals = g_new (SmPropValue, argc); | |
266 for (ptr = array, i = 0 ; i < argc ; ptr++, i++) { | |
267 vals[i].length = strlen (*ptr); | |
268 vals[i].value = *ptr; | |
269 } | |
270 | |
271 session_set_value(conn, name, SmLISTofARRAY8, argc, vals); | |
272 | |
273 g_free (vals); | |
274 } | |
275 | |
276 #endif /* USE_SM */ | |
277 | |
278 /* setup functions */ | |
279 | |
280 void | |
281 gaim_gtk_session_init(gchar *argv0, gchar *previous_id, gchar *config_dir) | |
282 { | |
283 #ifdef USE_SM | |
284 SmcCallbacks callbacks; | |
285 gchar *client_id = NULL; | |
286 gchar error[ERROR_LENGTH] = ""; | |
287 gchar *tmp = NULL; | |
288 gchar **cmd = NULL; | |
289 | |
290 if (session != NULL) { | |
291 /* session is already established, what the hell is going on? */ | |
292 gaim_debug(GAIM_DEBUG_WARNING, "Session Management", | |
293 "Duplicated call to gaim_gtk_session_init!\n"); | |
294 return; | |
295 } | |
296 | |
297 if (g_getenv("SESSION_MANAGER") == NULL) { | |
298 gaim_debug(GAIM_DEBUG_ERROR, "Session Management", | |
299 "No SESSION_MANAGER found, aborting.\n"); | |
300 return; | |
301 } | |
302 | |
303 ice_init(); | |
304 | |
305 callbacks.save_yourself.callback = session_save_yourself; | |
306 callbacks.die.callback = session_die; | |
307 callbacks.save_complete.callback = session_save_complete; | |
308 callbacks.shutdown_cancelled.callback = session_shutdown_cancelled; | |
309 | |
310 callbacks.save_yourself.client_data = NULL; | |
311 callbacks.die.client_data = NULL; | |
312 callbacks.save_complete.client_data = NULL; | |
313 callbacks.shutdown_cancelled.client_data = NULL; | |
314 | |
315 if (previous_id) { | |
316 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
317 "Connecting with previous ID %s\n", previous_id); | |
318 } else { | |
319 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
320 "Connecting with no previous ID\n"); | |
321 } | |
322 | |
323 session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask | | |
324 SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, | |
325 &callbacks, previous_id, &client_id, ERROR_LENGTH, error); | |
326 | |
327 if (session == NULL) { | |
328 if (error[0] != '\0') { | |
329 gaim_debug(GAIM_DEBUG_ERROR, "Session Management", | |
330 "Connection failed with error: %s\n", error); | |
331 } else { | |
332 gaim_debug(GAIM_DEBUG_ERROR, "Session Management", | |
333 "Connetion failed with unknown error.\n"); | |
334 } | |
335 return; | |
336 } | |
337 | |
338 tmp = SmcVendor(session); | |
339 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
340 "Connected to manager (%s) with client ID %s\n", | |
341 tmp, client_id); | |
342 g_free(tmp); | |
343 | |
344 session_managed = TRUE; | |
345 gdk_set_sm_client_id(client_id); | |
346 | |
347 tmp = g_get_current_dir(); | |
348 session_set_string(session, SmCurrentDirectory, tmp); | |
349 g_free(tmp); | |
350 | |
351 tmp = g_strdup_printf("%d", (int) getpid()); | |
352 session_set_string(session, SmProcessID, tmp); | |
353 g_free(tmp); | |
354 | |
355 tmp = g_strdup(g_get_user_name()); | |
356 session_set_string(session, SmUserID, tmp); | |
357 g_free(tmp); | |
358 | |
359 session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning); | |
360 session_set_string(session, SmProgram, g_get_prgname()); | |
361 | |
362 myself = g_strdup(argv0); | |
363 gaim_debug(GAIM_DEBUG_MISC, "Session Management", | |
364 "Using %s as command\n", myself); | |
365 | |
366 cmd = session_make_command(NULL, config_dir); | |
367 session_set_array(session, SmCloneCommand, cmd); | |
368 g_strfreev(cmd); | |
369 | |
370 /* this is currently useless, but gnome-session warns 'the following applications will not | |
371 save their current status' bla bla if we don't have it and the user checks 'Save Session' | |
372 when they log out */ | |
373 cmd = g_new(gchar *, 2); | |
374 cmd[0] = g_strdup("/bin/true"); | |
375 cmd[1] = NULL; | |
376 session_set_array(session, SmDiscardCommand, cmd); | |
377 g_strfreev(cmd); | |
378 | |
379 cmd = session_make_command(client_id, config_dir); | |
380 session_set_array(session, SmRestartCommand, cmd); | |
381 g_strfreev(cmd); | |
382 | |
383 g_free(client_id); | |
384 #endif /* USE_SM */ | |
385 } | |
386 | |
387 void | |
388 gaim_gtk_session_end() | |
389 { | |
390 #ifdef USE_SM | |
391 if (session == NULL) /* no session to close */ | |
392 return; | |
393 | |
394 SmcCloseConnection(session, 0, NULL); | |
395 | |
396 gaim_debug(GAIM_DEBUG_INFO, "Session Management", | |
397 "Connection closed.\n"); | |
398 #endif /* USE_SM */ | |
399 } |