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