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