Sun, 13 Apr 2003 01:42:18 +0000
[gaim-migrate @ 5480]
Fix for non aim/icq font size. Previoulsy the POINT_SIZE
conversion was being done for all protocols.
| 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 | ||
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
136 | /* my magic utility function */ |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
137 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
138 | static gchar **session_make_command(gchar *client_id) { |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
139 | gint i = 2; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
140 | gint j = 0; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
141 | gchar **ret; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
142 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
143 | if (client_id) i += 2; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
144 | if (opt_rcfile_arg) i += 2; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
145 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
146 | ret = g_new(gchar *, i); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
147 | ret[j++] = g_strdup(myself); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
148 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
149 | if (client_id) { |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
150 | ret[j++] = g_strdup("--session"); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
151 | ret[j++] = g_strdup(client_id); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
152 | } |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
153 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
154 | if (opt_rcfile_arg) { |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
155 | ret[j++] = g_strdup("--file"); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
156 | ret[j++] = g_strdup(opt_rcfile_arg); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
157 | } |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
158 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
159 | ret[j++] = NULL; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
160 | |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
161 | return ret; |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
162 | } |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
163 | |
| 4158 | 164 | /* SM callback handlers */ |
| 165 | ||
| 166 | void session_save_yourself(SmcConn conn, SmPointer data, int save_type, | |
| 167 | Bool shutdown, int interact_style, Bool fast) { | |
| 168 | if (had_first_save == FALSE && save_type == SmSaveLocal && | |
| 169 | interact_style == SmInteractStyleNone && !shutdown && | |
| 170 | !fast) { | |
| 171 | /* this is just a dry run, spit it back */ | |
| 172 | debug_printf("Session Management: received first save_yourself\n"); | |
| 173 | SmcSaveYourselfDone(conn, True); | |
| 174 | had_first_save = TRUE; | |
| 175 | return; | |
| 176 | } | |
| 177 | ||
| 178 | /* tum ti tum... don't add anything else here without * | |
| 179 | * reading SMlib.PS from an X.org ftp server near you */ | |
| 180 | ||
| 181 | debug_printf("Session Management: received save_yourself\n"); | |
| 182 | ||
| 183 | if (save_type == SmSaveGlobal || save_type == SmSaveBoth) { | |
| 184 | /* may as well do something ... */ | |
| 185 | save_prefs(); | |
| 186 | } | |
| 187 | ||
| 188 | SmcSaveYourselfDone(conn, True); | |
| 189 | } | |
| 190 | ||
| 191 | void session_die(SmcConn conn, SmPointer data) { | |
| 192 | debug_printf("Session Management: received die\n"); | |
| 193 | do_quit(); | |
| 194 | } | |
| 195 | ||
| 196 | void session_save_complete(SmcConn conn, SmPointer data) { | |
| 197 | debug_printf("Session Management: received save_complete\n"); | |
| 198 | } | |
| 199 | ||
| 200 | void session_shutdown_cancelled(SmcConn conn, SmPointer data) { | |
| 201 | debug_printf("Session Management: received shutdown_cancelled\n"); | |
| 202 | } | |
| 203 | ||
| 204 | /* utility functions stolen from Gnome-client */ | |
| 205 | ||
| 206 | static void session_set_value(SmcConn conn, gchar *name, char *type, | |
| 207 | int num_vals, SmPropValue *vals) { | |
| 208 | SmProp *proplist[1]; | |
| 209 | SmProp prop; | |
| 210 | ||
| 211 | g_return_if_fail(conn); | |
| 212 | ||
| 213 | prop.name = name; | |
| 214 | prop.type = type; | |
| 215 | prop.num_vals = num_vals; | |
| 216 | prop.vals = vals; | |
| 217 | ||
| 218 | proplist[0] = ∝ | |
| 219 | SmcSetProperties(conn, 1, proplist); | |
| 220 | } | |
| 221 | ||
| 222 | static void session_set_string(SmcConn conn, gchar *name, gchar *value) { | |
| 223 | SmPropValue val; | |
| 224 | ||
| 225 | g_return_if_fail(name); | |
| 226 | ||
| 227 | val.length = strlen (value)+1; | |
| 228 | val.value = value; | |
| 229 | ||
| 230 | session_set_value(conn, name, SmARRAY8, 1, &val); | |
| 231 | } | |
| 232 | ||
| 233 | static void session_set_gchar(SmcConn conn, gchar *name, gchar value) { | |
| 234 | SmPropValue val; | |
| 235 | ||
| 236 | g_return_if_fail(name); | |
| 237 | ||
| 238 | val.length = 1; | |
| 239 | val.value = &value; | |
| 240 | ||
| 241 | session_set_value(conn, name, SmCARD8, 1, &val); | |
| 242 | } | |
| 243 | ||
| 244 | static void session_set_array(SmcConn conn, gchar *name, gchar *array[]) { | |
| 245 | gint argc; | |
| 246 | gchar **ptr; | |
| 247 | gint i; | |
| 248 | ||
| 249 | SmPropValue *vals; | |
| 250 | ||
| 251 | g_return_if_fail (name); | |
| 252 | ||
| 253 | /* We count the number of elements in our array. */ | |
| 254 | for (ptr = array, argc = 0; *ptr ; ptr++, argc++) /* LOOP */; | |
| 255 | ||
| 256 | /* Now initialize the 'vals' array. */ | |
| 257 | vals = g_new (SmPropValue, argc); | |
| 258 | for (ptr = array, i = 0 ; i < argc ; ptr++, i++) { | |
| 259 | vals[i].length = strlen (*ptr); | |
| 260 | vals[i].value = *ptr; | |
| 261 | } | |
| 262 | ||
| 263 | session_set_value(conn, name, SmLISTofARRAY8, argc, vals); | |
| 264 | ||
| 265 | g_free (vals); | |
| 266 | } | |
| 267 | ||
| 268 | #endif /* USE_SM */ | |
| 269 | ||
| 270 | /* setup functions */ | |
| 271 | ||
| 272 | void session_init(gchar *argv0, gchar *previous_id) { | |
| 273 | #ifdef USE_SM | |
| 274 | SmcCallbacks callbacks; | |
| 275 | gchar *client_id = NULL; | |
| 276 | gchar error[ERROR_LENGTH] = ""; | |
| 277 | gchar *tmp = NULL; | |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
278 | gchar **cmd = NULL; |
| 4158 | 279 | |
| 280 | if (session != NULL) { | |
| 281 | /* session is already established, what the hell is going on? */ | |
| 282 | debug_printf("Session Management: duplicated call to session_init!\n"); | |
| 283 | return; | |
| 284 | } | |
| 285 | ||
| 286 | if (g_getenv("SESSION_MANAGER") == NULL) { | |
| 287 | debug_printf("Session Management: no SESSION_MANAGER found, aborting\n"); | |
| 288 | return; | |
| 289 | } | |
| 290 | ||
| 291 | ice_init(); | |
| 292 | ||
| 293 | callbacks.save_yourself.callback = session_save_yourself; | |
| 294 | callbacks.die.callback = session_die; | |
| 295 | callbacks.save_complete.callback = session_save_complete; | |
| 296 | callbacks.shutdown_cancelled.callback = session_shutdown_cancelled; | |
| 297 | ||
| 298 | callbacks.save_yourself.client_data = NULL; | |
| 299 | callbacks.die.client_data = NULL; | |
| 300 | callbacks.save_complete.client_data = NULL; | |
| 301 | callbacks.shutdown_cancelled.client_data = NULL; | |
| 302 | ||
|
4544
fc7df66dc394
[gaim-migrate @ 4823]
Robert McQueen <robot101@debian.org>
parents:
4281
diff
changeset
|
303 | if (previous_id) { |
|
fc7df66dc394
[gaim-migrate @ 4823]
Robert McQueen <robot101@debian.org>
parents:
4281
diff
changeset
|
304 | debug_printf("Session Management: connecting with previous ID %s\n", previous_id); |
|
fc7df66dc394
[gaim-migrate @ 4823]
Robert McQueen <robot101@debian.org>
parents:
4281
diff
changeset
|
305 | } else { |
|
fc7df66dc394
[gaim-migrate @ 4823]
Robert McQueen <robot101@debian.org>
parents:
4281
diff
changeset
|
306 | debug_printf("Session Management: connecting with no previous ID\n"); |
|
fc7df66dc394
[gaim-migrate @ 4823]
Robert McQueen <robot101@debian.org>
parents:
4281
diff
changeset
|
307 | } |
| 4158 | 308 | |
| 309 | session = SmcOpenConnection(NULL, "session", SmProtoMajor, SmProtoMinor, SmcSaveYourselfProcMask | | |
| 310 | SmcDieProcMask | SmcSaveCompleteProcMask | SmcShutdownCancelledProcMask, | |
| 311 | &callbacks, previous_id, &client_id, ERROR_LENGTH, error); | |
| 312 | ||
| 313 | if (session == NULL) { | |
| 314 | if (error[0] != '\0') { | |
| 315 | debug_printf("Session Management: connection failed with error: %s\n", error); | |
| 316 | } else { | |
| 317 | debug_printf("Session Management: connection failed with unknown error\n"); | |
| 318 | } | |
| 319 | return; | |
| 320 | } | |
| 321 | ||
| 322 | tmp = SmcVendor(session); | |
| 323 | debug_printf("Session Management: connected to manager (%s) with client ID %s\n", tmp, client_id); | |
| 324 | g_free(tmp); | |
| 325 | ||
| 326 | session_managed = TRUE; | |
| 327 | gdk_set_sm_client_id(client_id); | |
| 328 | ||
| 4265 | 329 | tmp = g_get_current_dir(); |
| 330 | session_set_string(session, SmCurrentDirectory, tmp); | |
| 331 | g_free(tmp); | |
| 332 | ||
| 4210 | 333 | tmp = g_strdup_printf("%d", (int) getpid()); |
| 4158 | 334 | session_set_string(session, SmProcessID, tmp); |
| 335 | g_free(tmp); | |
| 336 | ||
| 4210 | 337 | tmp = g_strdup(g_get_user_name()); |
| 4158 | 338 | session_set_string(session, SmUserID, tmp); |
| 339 | g_free(tmp); | |
| 340 | ||
| 341 | session_set_gchar(session, SmRestartStyleHint, (gchar) SmRestartIfRunning); | |
| 342 | session_set_string(session, SmProgram, g_get_prgname()); | |
| 343 | ||
| 344 | myself = g_strdup(argv0); | |
| 345 | debug_printf("Session Management: using %s as command\n", myself); | |
| 346 | ||
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
347 | cmd = session_make_command(NULL); |
| 4158 | 348 | session_set_array(session, SmCloneCommand, cmd); |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
349 | g_strfreev(cmd); |
| 4158 | 350 | |
| 4265 | 351 | /* this is currently useless, but gnome-session warns 'the following applications will not |
| 352 | save their current status' bla bla if we don't have it and the user checks 'Save Session' | |
| 353 | when they log out */ | |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
354 | cmd = g_new(gchar *, 2); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
355 | cmd[0] = g_strdup("/bin/true"); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
356 | cmd[1] = NULL; |
| 4158 | 357 | session_set_array(session, SmDiscardCommand, cmd); |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
358 | g_strfreev(cmd); |
| 4158 | 359 | |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
360 | cmd = session_make_command(client_id); |
| 4158 | 361 | session_set_array(session, SmRestartCommand, cmd); |
|
4281
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
362 | g_strfreev(cmd); |
|
bc2ff98fa384
[gaim-migrate @ 4532]
Robert McQueen <robot101@debian.org>
parents:
4265
diff
changeset
|
363 | |
| 4158 | 364 | g_free(client_id); |
| 365 | #endif /* USE_SM */ | |
| 366 | } | |
| 367 | ||
| 368 | void session_end() { | |
| 369 | #ifdef USE_SM | |
| 370 | if (session == NULL) /* no session to close */ | |
| 371 | return; | |
| 372 | ||
| 373 | SmcCloseConnection(session, 0, NULL); | |
| 374 | ||
| 375 | debug_printf("Session Management: connection closed\n"); | |
| 376 | #endif /* USE_SM */ | |
| 377 | } |