Wed, 02 Jan 2008 12:04:27 +0000
Modified patch from Gabriel (Sylar?) Schulof to deprecate PIDGIN_DIALOG. Closes #4535. (PS: I am aware of --author).
| 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> | |
|
20347
b7de12671217
Save default Gtk+ display in the command line for session restoration.
Ethan Blanton <elb@pidgin.im>
parents:
20147
diff
changeset
|
39 | #include <gdk/gdk.h> |
| 12246 | 40 | |
| 41 | #define ERROR_LENGTH 512 | |
| 42 | ||
| 43 | static IceIOErrorHandler ice_installed_io_error_handler; | |
| 44 | static SmcConn session = NULL; | |
| 45 | static gchar *myself = NULL; | |
| 46 | static gboolean had_first_save = FALSE; | |
| 47 | static gboolean session_managed = FALSE; | |
| 48 | ||
| 49 | /* ICE belt'n'braces stuff */ | |
| 50 | ||
| 51 | struct ice_connection_info { | |
| 52 | IceConn connection; | |
| 53 | guint input_id; | |
| 54 | }; | |
| 55 | ||
| 56 | static void ice_process_messages(gpointer data, gint fd, | |
| 15884 | 57 | PurpleInputCondition condition) { |
| 12246 | 58 | struct ice_connection_info *conninfo = (struct ice_connection_info*) data; |
| 59 | IceProcessMessagesStatus status; | |
| 60 | ||
| 61 | /* please don't block... please! */ | |
| 62 | status = IceProcessMessages(conninfo->connection, NULL, NULL); | |
| 63 | ||
| 64 | if (status == IceProcessMessagesIOError) { | |
| 15884 | 65 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 66 | "ICE IO error, closing connection... "); |
| 67 | ||
| 68 | /* IO error, please disconnect */ | |
| 69 | IceSetShutdownNegotiation(conninfo->connection, False); | |
| 70 | IceCloseConnection(conninfo->connection); | |
| 71 | ||
| 15884 | 72 | purple_debug(PURPLE_DEBUG_INFO, NULL, "done.\n"); |
| 12246 | 73 | |
| 74 | /* cancel the handler */ | |
| 15884 | 75 | purple_input_remove(conninfo->input_id); |
| 12246 | 76 | } |
| 77 | } | |
| 78 | ||
| 79 | static void ice_connection_watch(IceConn connection, IcePointer client_data, | |
| 80 | Bool opening, IcePointer *watch_data) { | |
| 81 | struct ice_connection_info *conninfo = NULL; | |
| 82 | ||
| 83 | if (opening) { | |
| 15884 | 84 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
|
21124
c293f3c40d4a
Add some missing debugging newlines.
Daniel Atallah <datallah@pidgin.im>
parents:
20347
diff
changeset
|
85 | "Handling new ICE connection... \n"); |
| 12246 | 86 | |
| 87 | /* ensure ICE connection is not passed to child processes */ | |
| 88 | fcntl(IceConnectionNumber(connection), F_SETFD, FD_CLOEXEC); | |
| 89 | ||
| 90 | conninfo = g_new(struct ice_connection_info, 1); | |
| 91 | conninfo->connection = connection; | |
| 92 | ||
| 93 | /* watch the connection */ | |
| 15884 | 94 | conninfo->input_id = purple_input_add(IceConnectionNumber(connection), PURPLE_INPUT_READ, |
| 12246 | 95 | ice_process_messages, conninfo); |
| 96 | *watch_data = conninfo; | |
| 97 | } else { | |
| 15884 | 98 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
|
21124
c293f3c40d4a
Add some missing debugging newlines.
Daniel Atallah <datallah@pidgin.im>
parents:
20347
diff
changeset
|
99 | "Handling closed ICE connection... \n"); |
| 12246 | 100 | |
| 101 | /* get the input ID back and stop watching it */ | |
| 102 | conninfo = (struct ice_connection_info*) *watch_data; | |
| 15884 | 103 | purple_input_remove(conninfo->input_id); |
| 12246 | 104 | g_free(conninfo); |
| 105 | } | |
| 106 | ||
| 15884 | 107 | purple_debug(PURPLE_DEBUG_INFO, NULL, "done.\n"); |
| 12246 | 108 | } |
| 109 | ||
| 110 | /* We call any handler installed before (or after) ice_init but | |
| 111 | * avoid calling the default libICE handler which does an exit(). | |
| 112 | * | |
| 113 | * This means we do nothing by default, which is probably correct, | |
| 114 | * the connection will get closed by libICE | |
| 115 | */ | |
| 116 | ||
| 117 | static void ice_io_error_handler(IceConn connection) { | |
| 15884 | 118 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 119 | "Handling ICE IO error... "); |
| 120 | ||
| 121 | if (ice_installed_io_error_handler) | |
| 122 | (*ice_installed_io_error_handler)(connection); | |
| 123 | ||
| 15884 | 124 | purple_debug(PURPLE_DEBUG_INFO, NULL, "done.\n"); |
| 12246 | 125 | } |
| 126 | ||
| 127 | static void ice_init() { | |
| 128 | IceIOErrorHandler default_handler; | |
| 129 | ||
| 130 | ice_installed_io_error_handler = IceSetIOErrorHandler(NULL); | |
| 131 | default_handler = IceSetIOErrorHandler(ice_io_error_handler); | |
| 132 | ||
| 133 | if (ice_installed_io_error_handler == default_handler) | |
| 134 | ice_installed_io_error_handler = NULL; | |
| 135 | ||
| 136 | IceAddConnectionWatch(ice_connection_watch, NULL); | |
| 137 | ||
| 15884 | 138 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 139 | "ICE initialized.\n"); |
| 140 | } | |
| 141 | ||
| 142 | /* my magic utility function */ | |
| 143 | ||
| 144 | static gchar **session_make_command(gchar *client_id, gchar *config_dir) { | |
|
20347
b7de12671217
Save default Gtk+ display in the command line for session restoration.
Ethan Blanton <elb@pidgin.im>
parents:
20147
diff
changeset
|
145 | gint i = 4; |
| 12246 | 146 | gint j = 0; |
| 147 | gchar **ret; | |
| 148 | ||
| 149 | if (client_id) i += 2; | |
| 15884 | 150 | if (config_dir) i += 2; /* we will specify purple's user dir */ |
| 12246 | 151 | |
| 152 | ret = g_new(gchar *, i); | |
| 153 | ret[j++] = g_strdup(myself); | |
| 154 | ||
| 155 | if (client_id) { | |
| 156 | ret[j++] = g_strdup("--session"); | |
| 157 | ret[j++] = g_strdup(client_id); | |
| 158 | } | |
| 159 | ||
| 160 | if (config_dir) { | |
| 161 | ret[j++] = g_strdup("--config"); | |
| 162 | ret[j++] = g_strdup(config_dir); | |
| 163 | } | |
| 164 | ||
|
20347
b7de12671217
Save default Gtk+ display in the command line for session restoration.
Ethan Blanton <elb@pidgin.im>
parents:
20147
diff
changeset
|
165 | ret[j++] = g_strdup("--display"); |
|
b7de12671217
Save default Gtk+ display in the command line for session restoration.
Ethan Blanton <elb@pidgin.im>
parents:
20147
diff
changeset
|
166 | ret[j++] = g_strdup((gchar *)gdk_display_get_name(gdk_display_get_default())); |
|
b7de12671217
Save default Gtk+ display in the command line for session restoration.
Ethan Blanton <elb@pidgin.im>
parents:
20147
diff
changeset
|
167 | |
| 12246 | 168 | ret[j++] = NULL; |
| 169 | ||
| 170 | return ret; | |
| 171 | } | |
| 172 | ||
| 173 | /* SM callback handlers */ | |
| 174 | ||
| 175 | static void session_save_yourself(SmcConn conn, SmPointer data, int save_type, | |
| 176 | Bool shutdown, int interact_style, Bool fast) { | |
| 177 | if (had_first_save == FALSE && save_type == SmSaveLocal && | |
| 178 | interact_style == SmInteractStyleNone && !shutdown && | |
| 179 | !fast) { | |
| 180 | /* this is just a dry run, spit it back */ | |
| 15884 | 181 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 182 | "Received first save_yourself\n"); |
| 183 | SmcSaveYourselfDone(conn, True); | |
| 184 | had_first_save = TRUE; | |
| 185 | return; | |
| 186 | } | |
| 187 | ||
| 188 | /* tum ti tum... don't add anything else here without * | |
| 189 | * reading SMlib.PS from an X.org ftp server near you */ | |
| 190 | ||
| 15884 | 191 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 192 | "Received save_yourself\n"); |
| 193 | ||
| 194 | if (save_type == SmSaveGlobal || save_type == SmSaveBoth) { | |
| 195 | /* may as well do something ... */ | |
| 196 | /* or not -- save_prefs(); */ | |
| 197 | } | |
| 198 | ||
| 199 | SmcSaveYourselfDone(conn, True); | |
| 200 | } | |
| 201 | ||
| 202 | static void session_die(SmcConn conn, SmPointer data) { | |
| 15884 | 203 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 204 | "Received die\n"); |
| 15884 | 205 | purple_core_quit(); |
| 12246 | 206 | } |
| 207 | ||
| 208 | static void session_save_complete(SmcConn conn, SmPointer data) { | |
| 15884 | 209 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 210 | "Received save_complete\n"); |
| 211 | } | |
| 212 | ||
| 213 | static void session_shutdown_cancelled(SmcConn conn, SmPointer data) { | |
| 15884 | 214 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 215 | "Received shutdown_cancelled\n"); |
| 216 | } | |
| 217 | ||
| 218 | /* utility functions stolen from Gnome-client */ | |
| 219 | ||
| 220 | static void session_set_value(SmcConn conn, gchar *name, char *type, | |
| 221 | int num_vals, SmPropValue *vals) { | |
| 222 | SmProp *proplist[1]; | |
| 223 | SmProp prop; | |
| 224 | ||
| 225 | g_return_if_fail(conn); | |
| 226 | ||
| 227 | prop.name = name; | |
| 228 | prop.type = type; | |
| 229 | prop.num_vals = num_vals; | |
| 230 | prop.vals = vals; | |
| 231 | ||
| 232 | proplist[0] = ∝ | |
| 233 | SmcSetProperties(conn, 1, proplist); | |
| 234 | } | |
| 235 | ||
| 236 | static void session_set_string(SmcConn conn, gchar *name, gchar *value) { | |
| 237 | SmPropValue val; | |
| 238 | ||
| 239 | g_return_if_fail(name); | |
| 240 | ||
| 241 | val.length = strlen (value)+1; | |
| 242 | val.value = value; | |
| 243 | ||
| 244 | session_set_value(conn, name, SmARRAY8, 1, &val); | |
| 245 | } | |
| 246 | ||
| 247 | static void session_set_gchar(SmcConn conn, gchar *name, gchar value) { | |
| 248 | SmPropValue val; | |
| 249 | ||
| 250 | g_return_if_fail(name); | |
| 251 | ||
| 252 | val.length = 1; | |
| 253 | val.value = &value; | |
| 254 | ||
| 255 | session_set_value(conn, name, SmCARD8, 1, &val); | |
| 256 | } | |
| 257 | ||
| 258 | static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) { | |
| 259 | gint argc; | |
| 260 | gchar **ptr; | |
| 261 | gint i; | |
| 262 | ||
| 263 | SmPropValue *vals; | |
| 264 | ||
| 265 | g_return_if_fail (name); | |
| 266 | ||
| 267 | /* We count the number of elements in our array. */ | |
| 268 | for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */; | |
| 269 | ||
| 270 | /* Now initialize the 'vals' array. */ | |
| 271 | vals = g_new (SmPropValue, argc); | |
| 272 | for (ptr = array, i = 0 ; i < argc ; ptr++, i++) { | |
| 273 | vals[i].length = strlen (*ptr); | |
| 274 | vals[i].value = *ptr; | |
| 275 | } | |
| 276 | ||
| 277 | session_set_value(conn, name, SmLISTofARRAY8, argc, vals); | |
| 278 | ||
| 279 | g_free (vals); | |
| 280 | } | |
| 281 | ||
| 282 | #endif /* USE_SM */ | |
| 283 | ||
| 284 | /* setup functions */ | |
| 285 | ||
| 286 | void | |
|
15562
8c8249fe5e3c
gaim_gtk to pidgin. I hope
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
287 | pidgin_session_init(gchar *argv0, gchar *previous_id, gchar *config_dir) |
| 12246 | 288 | { |
| 289 | #ifdef USE_SM | |
| 290 | SmcCallbacks callbacks; | |
| 291 | gchar *client_id = NULL; | |
| 292 | gchar error[ERROR_LENGTH] = ""; | |
| 293 | gchar *tmp = NULL; | |
| 294 | gchar **cmd = NULL; | |
| 295 | ||
| 296 | if (session != NULL) { | |
| 297 | /* session is already established, what the hell is going on? */ | |
| 15884 | 298 | purple_debug(PURPLE_DEBUG_WARNING, "Session Management", |
|
15562
8c8249fe5e3c
gaim_gtk to pidgin. I hope
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
299 | "Duplicated call to pidgin_session_init!\n"); |
| 12246 | 300 | return; |
| 301 | } | |
| 302 | ||
| 303 | if (g_getenv("SESSION_MANAGER") == NULL) { | |
| 15884 | 304 | purple_debug(PURPLE_DEBUG_ERROR, "Session Management", |
| 12246 | 305 | "No SESSION_MANAGER found, aborting.\n"); |
| 306 | return; | |
| 307 | } | |
| 308 | ||
| 309 | ice_init(); | |
| 310 | ||
| 311 | callbacks.save_yourself.callback = session_save_yourself; | |
| 312 | callbacks.die.callback = session_die; | |
| 313 | callbacks.save_complete.callback = session_save_complete; | |
| 314 | callbacks.shutdown_cancelled.callback = session_shutdown_cancelled; | |
| 315 | ||
| 316 | callbacks.save_yourself.client_data = NULL; | |
| 317 | callbacks.die.client_data = NULL; | |
| 318 | callbacks.save_complete.client_data = NULL; | |
| 319 | callbacks.shutdown_cancelled.client_data = NULL; | |
| 320 | ||
| 321 | if (previous_id) { | |
| 15884 | 322 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 323 | "Connecting with previous ID %s\n", previous_id); |
| 324 | } else { | |
| 15884 | 325 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 326 | "Connecting with no previous ID\n"); |
| 327 | } | |
| 328 | ||
| 329 | session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask | | |
| 330 | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, | |
| 331 | &callbacks, previous_id, &client_id, ERROR_LENGTH, error); | |
| 332 | ||
| 333 | if (session == NULL) { | |
| 334 | if (error[0] != '\0') { | |
| 15884 | 335 | purple_debug(PURPLE_DEBUG_ERROR, "Session Management", |
| 12246 | 336 | "Connection failed with error: %s\n", error); |
| 337 | } else { | |
| 15884 | 338 | purple_debug(PURPLE_DEBUG_ERROR, "Session Management", |
| 12246 | 339 | "Connetion failed with unknown error.\n"); |
| 340 | } | |
| 341 | return; | |
| 342 | } | |
| 343 | ||
| 344 | tmp = SmcVendor(session); | |
| 15884 | 345 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 346 | "Connected to manager (%s) with client ID %s\n", |
| 347 | tmp, client_id); | |
| 348 | g_free(tmp); | |
| 349 | ||
| 350 | session_managed = TRUE; | |
| 351 | gdk_set_sm_client_id(client_id); | |
| 352 | ||
| 353 | tmp = g_get_current_dir(); | |
| 354 | session_set_string(session, SmCurrentDirectory, tmp); | |
| 355 | g_free(tmp); | |
| 356 | ||
| 357 | tmp = g_strdup_printf("%d", (int) getpid()); | |
| 358 | session_set_string(session, SmProcessID, tmp); | |
| 359 | g_free(tmp); | |
| 360 | ||
| 361 | tmp = g_strdup(g_get_user_name()); | |
| 362 | session_set_string(session, SmUserID, tmp); | |
| 363 | g_free(tmp); | |
| 364 | ||
| 365 | session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning); | |
| 366 | session_set_string(session, SmProgram, g_get_prgname()); | |
| 367 | ||
| 368 | myself = g_strdup(argv0); | |
| 15884 | 369 | purple_debug(PURPLE_DEBUG_MISC, "Session Management", |
| 12246 | 370 | "Using %s as command\n", myself); |
| 371 | ||
| 372 | cmd = session_make_command(NULL, config_dir); | |
| 373 | session_set_array(session, SmCloneCommand, cmd); | |
| 374 | g_strfreev(cmd); | |
| 375 | ||
| 376 | /* this is currently useless, but gnome-session warns 'the following applications will not | |
| 377 | save their current status' bla bla if we don't have it and the user checks 'Save Session' | |
| 378 | when they log out */ | |
| 379 | cmd = g_new(gchar *, 2); | |
| 380 | cmd[0] = g_strdup("/bin/true"); | |
| 381 | cmd[1] = NULL; | |
| 382 | session_set_array(session, SmDiscardCommand, cmd); | |
| 383 | g_strfreev(cmd); | |
| 384 | ||
| 385 | cmd = session_make_command(client_id, config_dir); | |
| 386 | session_set_array(session, SmRestartCommand, cmd); | |
| 387 | g_strfreev(cmd); | |
| 388 | ||
| 389 | g_free(client_id); | |
| 390 | #endif /* USE_SM */ | |
| 391 | } | |
| 392 | ||
| 393 | void | |
|
15562
8c8249fe5e3c
gaim_gtk to pidgin. I hope
Sean Egan <seanegan@pidgin.im>
parents:
15435
diff
changeset
|
394 | pidgin_session_end() |
| 12246 | 395 | { |
| 396 | #ifdef USE_SM | |
| 397 | if (session == NULL) /* no session to close */ | |
| 398 | return; | |
| 399 | ||
| 400 | SmcCloseConnection(session, 0, NULL); | |
| 401 | ||
| 15884 | 402 | purple_debug(PURPLE_DEBUG_INFO, "Session Management", |
| 12246 | 403 | "Connection closed.\n"); |
| 404 | #endif /* USE_SM */ | |
| 405 | } |