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