Thu, 09 Jan 2003 19:19:06 +0000
[gaim-migrate @ 4516]
This is apparently the "tiniest patch ever". I disagree. What does Robot101
know anyway?
| 4158 | 1 | /* |
| 2 | * session management for Gaim | |
| 3 | * | |
| 4 | * Copyright (C) 2002, Robert McQueen <robot101@debian.org> but | |
| 5 | * much code shamelessly cribbed from GsmClient (C) 2001 Havoc | |
| 6 | * Pennington, which is in turn inspired by various other pieces | |
| 7 | * of code including GnomeClient (C) 1998 Carsten Schaar, Tom | |
| 8 | * Tromey, and twm session code (C) 1998 The Open Group. | |
| 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 | |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 23 | * | |
| 24 | */ | |
| 25 | ||
| 26 | #ifdef HAVE_CONFIG_H | |
| 27 | #include <config.h> | |
| 28 | #endif | |
| 29 | ||
| 4210 | 30 | #include <string.h> |
| 31 | ||
| 4158 | 32 | #include "gaim.h" |
| 33 | ||
| 34 | #ifdef USE_SM | |
| 35 | ||
| 36 | #include <X11/ICE/ICElib.h> | |
| 37 | #include <X11/SM/SMlib.h> | |
| 38 | #include <unistd.h> | |
| 39 | #include <fcntl.h> | |
| 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 | gboolean session_managed = FALSE; | |
| 47 | ||
| 48 | /* ICE belt'n'braces stuff */ | |
| 49 | ||
| 50 | static gboolean ice_process_messages(GIOChannel *channel, GIOCondition condition, | |
| 51 | gpointer data) { | |
| 52 | IceConn connection = (IceConn)data; | |
| 53 | IceProcessMessagesStatus status; | |
| 54 | ||
| 55 | /* please don't block... please! */ | |
| 56 | status = IceProcessMessages(connection, NULL, NULL); | |
| 57 | ||
| 58 | if (status == IceProcessMessagesIOError) { | |
| 59 | debug_printf("Session Management: ICE IO error, closing connection... "); | |
| 60 | ||
| 61 | /* IO error, please disconnect */ | |
| 62 | IceSetShutdownNegotiation(connection, False); | |
| 63 | IceCloseConnection(connection); | |
| 64 | ||
| 65 | debug_printf("done\n"); | |
| 66 | ||
| 67 | /* cancel the handler */ | |
| 68 | return FALSE; | |
| 69 | } | |
| 70 | ||
| 71 | /* live to see another day */ | |
| 72 | return TRUE; | |
| 73 | } | |
| 74 | ||
| 75 | static void ice_connection_watch(IceConn connection, IcePointer client_data, | |
| 76 | Bool opening, IcePointer *watch_data) { | |
| 77 | guint input_id; | |
| 78 | ||
| 79 | if (opening) { | |
| 80 | GIOChannel *channel; | |
| 81 | ||
| 82 | debug_printf("Session Management: handling new ICE connection... "); | |
| 83 | ||
| 84 | /* ensure ICE connection is not passed to child processes */ | |
| 85 | fcntl(IceConnectionNumber(connection), F_SETFD, FD_CLOEXEC); | |
| 86 | ||
| 87 | /* get glib to watch the connection for us */ | |
| 88 | channel = g_io_channel_unix_new(IceConnectionNumber(connection)); | |
| 89 | input_id = g_io_add_watch(channel, G_IO_IN | G_IO_ERR, | |
| 90 | ice_process_messages, connection); | |
| 91 | g_io_channel_unref(channel); | |
| 92 | ||
| 93 | /* store the input ID as a pointer for when it closes */ | |
| 94 | *watch_data = (IcePointer)GUINT_TO_POINTER(input_id); | |
| 95 | } else { | |
| 96 | debug_printf("Session Management: handling closed ICE connection... "); | |
| 97 | ||
| 98 | /* get the input ID back and stop watching it */ | |
| 99 | input_id = GPOINTER_TO_UINT((gpointer) *watch_data); | |
| 100 | g_source_remove(input_id); | |
| 101 | } | |
| 102 | ||
| 103 | debug_printf("done\n"); | |
| 104 | } | |
| 105 | ||
| 106 | /* We call any handler installed before (or after) ice_init but | |
| 107 | * avoid calling the default libICE handler which does an exit(). | |
| 108 | * | |
| 109 | * This means we do nothing by default, which is probably correct, | |
| 110 | * the connection will get closed by libICE | |
| 111 | */ | |
| 112 | ||
| 113 | static void ice_io_error_handler(IceConn connection) { | |
| 114 | debug_printf("Session Management: handling ICE IO error... "); | |
| 115 | ||
| 116 | if (ice_installed_io_error_handler) | |
| 117 | (*ice_installed_io_error_handler)(connection); | |
| 118 | ||
| 119 | debug_printf("done\n"); | |
| 120 | } | |
| 121 | ||
| 122 | static void ice_init() { | |
| 123 | IceIOErrorHandler default_handler; | |
| 124 | ||
| 125 | ice_installed_io_error_handler = IceSetIOErrorHandler(NULL); | |
| 126 | default_handler = IceSetIOErrorHandler(ice_io_error_handler); | |
| 127 | ||
| 128 | if (ice_installed_io_error_handler == default_handler) | |
| 129 | ice_installed_io_error_handler = NULL; | |
| 130 | ||
| 131 | IceAddConnectionWatch(ice_connection_watch, NULL); | |
| 132 | ||
| 133 | debug_printf("Session Management: ICE initialised\n"); | |
| 134 | } | |
| 135 | ||
| 136 | /* SM callback handlers */ | |
| 137 | ||
| 138 | void session_save_yourself(SmcConn conn, SmPointer data, int save_type, | |
| 139 | Bool shutdown, int interact_style, Bool fast) { | |
| 140 | if (had_first_save == FALSE && save_type == SmSaveLocal && | |
| 141 | interact_style == SmInteractStyleNone && !shutdown && | |
| 142 | !fast) { | |
| 143 | /* this is just a dry run, spit it back */ | |
| 144 | debug_printf("Session Management: received first save_yourself\n"); | |
| 145 | SmcSaveYourselfDone(conn, True); | |
| 146 | had_first_save = TRUE; | |
| 147 | return; | |
| 148 | } | |
| 149 | ||
| 150 | /* tum ti tum... don't add anything else here without * | |
| 151 | * reading SMlib.PS from an X.org ftp server near you */ | |
| 152 | ||
| 153 | debug_printf("Session Management: received save_yourself\n"); | |
| 154 | ||
| 155 | if (save_type == SmSaveGlobal || save_type == SmSaveBoth) { | |
| 156 | /* may as well do something ... */ | |
| 157 | save_prefs(); | |
| 158 | } | |
| 159 | ||
| 160 | SmcSaveYourselfDone(conn, True); | |
| 161 | } | |
| 162 | ||
| 163 | void session_die(SmcConn conn, SmPointer data) { | |
| 164 | debug_printf("Session Management: received die\n"); | |
| 165 | do_quit(); | |
| 166 | } | |
| 167 | ||
| 168 | void session_save_complete(SmcConn conn, SmPointer data) { | |
| 169 | debug_printf("Session Management: received save_complete\n"); | |
| 170 | } | |
| 171 | ||
| 172 | void session_shutdown_cancelled(SmcConn conn, SmPointer data) { | |
| 173 | debug_printf("Session Management: received shutdown_cancelled\n"); | |
| 174 | } | |
| 175 | ||
| 176 | /* utility functions stolen from Gnome-client */ | |
| 177 | ||
| 178 | static void session_set_value(SmcConn conn, gchar *name, char *type, | |
| 179 | int num_vals, SmPropValue *vals) { | |
| 180 | SmProp *proplist[1]; | |
| 181 | SmProp prop; | |
| 182 | ||
| 183 | g_return_if_fail(conn); | |
| 184 | ||
| 185 | prop.name = name; | |
| 186 | prop.type = type; | |
| 187 | prop.num_vals = num_vals; | |
| 188 | prop.vals = vals; | |
| 189 | ||
| 190 | proplist[0] = ∝ | |
| 191 | SmcSetProperties(conn, 1, proplist); | |
| 192 | } | |
| 193 | ||
| 194 | static void session_set_string(SmcConn conn, gchar *name, gchar *value) { | |
| 195 | SmPropValue val; | |
| 196 | ||
| 197 | g_return_if_fail(name); | |
| 198 | ||
| 199 | val.length = strlen (value)+1; | |
| 200 | val.value = value; | |
| 201 | ||
| 202 | session_set_value(conn, name, SmARRAY8, 1, &val); | |
| 203 | } | |
| 204 | ||
| 205 | static void session_set_gchar(SmcConn conn, gchar *name, gchar value) { | |
| 206 | SmPropValue val; | |
| 207 | ||
| 208 | g_return_if_fail(name); | |
| 209 | ||
| 210 | val.length = 1; | |
| 211 | val.value = &value; | |
| 212 | ||
| 213 | session_set_value(conn, name, SmCARD8, 1, &val); | |
| 214 | } | |
| 215 | ||
| 216 | static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) { | |
| 217 | gint argc; | |
| 218 | gchar **ptr; | |
| 219 | gint i; | |
| 220 | ||
| 221 | SmPropValue *vals; | |
| 222 | ||
| 223 | g_return_if_fail (name); | |
| 224 | ||
| 225 | /* We count the number of elements in our array. */ | |
| 226 | for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */; | |
| 227 | ||
| 228 | /* Now initialize the 'vals' array. */ | |
| 229 | vals = g_new (SmPropValue, argc); | |
| 230 | for (ptr = array, i = 0 ; i < argc ; ptr++, i++) { | |
| 231 | vals[i].length = strlen (*ptr); | |
| 232 | vals[i].value = *ptr; | |
| 233 | } | |
| 234 | ||
| 235 | session_set_value(conn, name, SmLISTofARRAY8, argc, vals); | |
| 236 | ||
| 237 | g_free (vals); | |
| 238 | } | |
| 239 | ||
| 240 | #endif /* USE_SM */ | |
| 241 | ||
| 242 | /* setup functions */ | |
| 243 | ||
| 244 | void session_init(gchar *argv0, gchar *previous_id) { | |
| 245 | #ifdef USE_SM | |
| 246 | SmcCallbacks callbacks; | |
| 247 | gchar *client_id = NULL; | |
| 248 | gchar error[ERROR_LENGTH] = ""; | |
| 249 | gchar *tmp = NULL; | |
| 250 | gchar *cmd[4] = { NULL, NULL, NULL, NULL }; | |
| 251 | ||
| 252 | if (session != NULL) { | |
| 253 | /* session is already established, what the hell is going on? */ | |
| 254 | debug_printf("Session Management: duplicated call to session_init!\n"); | |
| 255 | return; | |
| 256 | } | |
| 257 | ||
| 258 | if (g_getenv("SESSION_MANAGER") == NULL) { | |
| 259 | debug_printf("Session Management: no SESSION_MANAGER found, aborting\n"); | |
| 260 | return; | |
| 261 | } | |
| 262 | ||
| 263 | ice_init(); | |
| 264 | ||
| 265 | callbacks.save_yourself.callback = session_save_yourself; | |
| 266 | callbacks.die.callback = session_die; | |
| 267 | callbacks.save_complete.callback = session_save_complete; | |
| 268 | callbacks.shutdown_cancelled.callback = session_shutdown_cancelled; | |
| 269 | ||
| 270 | callbacks.save_yourself.client_data = NULL; | |
| 271 | callbacks.die.client_data = NULL; | |
| 272 | callbacks.save_complete.client_data = NULL; | |
| 273 | callbacks.shutdown_cancelled.client_data = NULL; | |
| 274 | ||
| 275 | debug_printf("Session Management: connecting with previous ID %s\n", previous_id); | |
| 276 | ||
| 277 | session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask | | |
| 278 | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, | |
| 279 | &callbacks, previous_id, &client_id, ERROR_LENGTH, error); | |
| 280 | ||
| 281 | if (session == NULL) { | |
| 282 | if (error[0] != '\0') { | |
| 283 | debug_printf("Session Management: connection failed with error: %s\n", error); | |
| 284 | } else { | |
| 285 | debug_printf("Session Management: connection failed with unknown error\n"); | |
| 286 | } | |
| 287 | return; | |
| 288 | } | |
| 289 | ||
| 290 | tmp = SmcVendor(session); | |
| 291 | debug_printf("Session Management: connected to manager (%s) with client ID %s\n", tmp, client_id); | |
| 292 | g_free(tmp); | |
| 293 | ||
| 294 | session_managed = TRUE; | |
| 295 | gdk_set_sm_client_id(client_id); | |
| 296 | ||
| 4265 | 297 | tmp = g_get_current_dir(); |
| 298 | session_set_string(session, SmCurrentDirectory, tmp); | |
| 299 | g_free(tmp); | |
| 300 | ||
| 4210 | 301 | tmp = g_strdup_printf("%d", (int) getpid()); |
| 4158 | 302 | session_set_string(session, SmProcessID, tmp); |
| 303 | g_free(tmp); | |
| 304 | ||
| 4210 | 305 | tmp = g_strdup(g_get_user_name()); |
| 4158 | 306 | session_set_string(session, SmUserID, tmp); |
| 307 | g_free(tmp); | |
| 308 | ||
| 309 | session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning); | |
| 310 | session_set_string(session, SmProgram, g_get_prgname()); | |
| 311 | ||
| 312 | myself = g_strdup(argv0); | |
| 313 | debug_printf("Session Management: using %s as command\n", myself); | |
| 314 | ||
| 315 | cmd[0] = myself; | |
| 316 | cmd[1] = NULL; | |
| 317 | session_set_array(session, SmCloneCommand, cmd); | |
| 318 | ||
| 4265 | 319 | /* this is currently useless, but gnome-session warns 'the following applications will not |
| 320 | save their current status' bla bla if we don't have it and the user checks 'Save Session' | |
| 321 | when they log out */ | |
| 4158 | 322 | cmd[1] = "-v"; |
| 323 | cmd[2] = NULL; | |
| 324 | session_set_array(session, SmDiscardCommand, cmd); | |
| 325 | ||
| 326 | cmd[1] = "--session"; | |
| 327 | cmd[2] = client_id; | |
| 328 | cmd[3] = NULL; | |
| 329 | session_set_array(session, SmRestartCommand, cmd); | |
| 330 | g_free(client_id); | |
| 331 | #endif /* USE_SM */ | |
| 332 | } | |
| 333 | ||
| 334 | void session_end() { | |
| 335 | #ifdef USE_SM | |
| 336 | if (session == NULL) /* no session to close */ | |
| 337 | return; | |
| 338 | ||
| 339 | SmcCloseConnection(session, 0, NULL); | |
| 340 | ||
| 341 | debug_printf("Session Management: connection closed\n"); | |
| 342 | #endif /* USE_SM */ | |
| 343 | } |