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