2 * Copyright (C) 2007 Novell, Inc.
4 * Inspired by various other pieces of code including GsmClient (C)
5 * 2001 Havoc Pennington, GnomeClient (C) 1998 Carsten Schaar, and twm
6 * session code (C) 1998 The Open Group.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
26 #include "eggsmclient.h"
27 #include "eggsmclient-private.h"
29 #include "eggdesktopfile.h"
36 #include <X11/SM/SMlib.h>
40 #define EGG_TYPE_SM_CLIENT_XSMP (egg_sm_client_xsmp_get_type ())
41 #define EGG_SM_CLIENT_XSMP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EGG_TYPE_SM_CLIENT_XSMP, EggSMClientXSMP))
42 #define EGG_SM_CLIENT_XSMP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), EGG_TYPE_SM_CLIENT_XSMP, EggSMClientXSMPClass))
43 #define EGG_IS_SM_CLIENT_XSMP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), EGG_TYPE_SM_CLIENT_XSMP))
44 #define EGG_IS_SM_CLIENT_XSMP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), EGG_TYPE_SM_CLIENT_XSMP))
45 #define EGG_SM_CLIENT_XSMP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), EGG_TYPE_SM_CLIENT_XSMP, EggSMClientXSMPClass))
47 typedef struct _EggSMClientXSMP EggSMClientXSMP;
48 typedef struct _EggSMClientXSMPClass EggSMClientXSMPClass;
50 /* These mostly correspond to the similarly-named states in section
51 * 9.1 of the XSMP spec. Some of the states there aren't represented
52 * here, because we don't need them. SHUTDOWN_CANCELLED is slightly
53 * different from the spec; we use it when the client is IDLE after a
54 * ShutdownCancelled message, but the application is still interacting
55 * and doesn't know the shutdown has been cancelled yet.
60 XSMP_STATE_SAVE_YOURSELF,
61 XSMP_STATE_INTERACT_REQUEST,
63 XSMP_STATE_SAVE_YOURSELF_DONE,
64 XSMP_STATE_SHUTDOWN_CANCELLED,
65 XSMP_STATE_CONNECTION_CLOSED,
66 } EggSMClientXSMPState;
68 static const char *state_names[] = {
79 #define EGG_SM_CLIENT_XSMP_STATE(xsmp) (state_names[(xsmp)->state])
81 struct _EggSMClientXSMP
88 EggSMClientXSMPState state;
89 char **restart_command;
90 gboolean set_restart_command;
95 /* Current SaveYourself state */
96 guint expecting_initial_save_yourself : 1;
97 guint need_save_state : 1;
98 guint need_quit_requested : 1;
99 guint interact_errors : 1;
100 guint shutting_down : 1;
103 guint waiting_to_set_initial_properties : 1;
104 guint waiting_to_emit_quit : 1;
105 guint waiting_to_emit_quit_cancelled : 1;
106 guint waiting_to_save_myself : 1;
110 struct _EggSMClientXSMPClass
112 EggSMClientClass parent_class;
116 static void sm_client_xsmp_startup (EggSMClient *client,
117 const char *client_id);
118 static void sm_client_xsmp_set_restart_command (EggSMClient *client,
121 static void sm_client_xsmp_will_quit (EggSMClient *client,
123 static gboolean sm_client_xsmp_end_session (EggSMClient *client,
124 EggSMClientEndStyle style,
125 gboolean request_confirmation);
127 static void xsmp_save_yourself (SmcConn smc_conn,
128 SmPointer client_data,
133 static void xsmp_die (SmcConn smc_conn,
134 SmPointer client_data);
135 static void xsmp_save_complete (SmcConn smc_conn,
136 SmPointer client_data);
137 static void xsmp_shutdown_cancelled (SmcConn smc_conn,
138 SmPointer client_data);
139 static void xsmp_interact (SmcConn smc_conn,
140 SmPointer client_data);
142 static SmProp *array_prop (const char *name,
144 static SmProp *ptrarray_prop (const char *name,
146 static SmProp *string_prop (const char *name,
148 static SmProp *card8_prop (const char *name,
149 unsigned char value);
151 static void set_properties (EggSMClientXSMP *xsmp, ...);
152 static void delete_properties (EggSMClientXSMP *xsmp, ...);
154 static GPtrArray *generate_command (char **restart_command,
155 const char *client_id,
156 const char *state_file);
158 static void save_state (EggSMClientXSMP *xsmp);
159 static void do_save_yourself (EggSMClientXSMP *xsmp);
160 static void update_pending_events (EggSMClientXSMP *xsmp);
162 static void ice_init (void);
163 static gboolean process_ice_messages (IceConn ice_conn);
164 static void smc_error_handler (SmcConn smc_conn,
166 int offending_minor_opcode,
167 unsigned long offending_sequence,
172 G_DEFINE_TYPE (EggSMClientXSMP, egg_sm_client_xsmp, EGG_TYPE_SM_CLIENT)
175 egg_sm_client_xsmp_init (EggSMClientXSMP *xsmp)
177 xsmp->state = XSMP_STATE_CONNECTION_CLOSED;
178 xsmp->connection = NULL;
179 xsmp->restart_style = SmRestartIfRunning;
183 egg_sm_client_xsmp_class_init (EggSMClientXSMPClass *klass)
185 EggSMClientClass *sm_client_class = EGG_SM_CLIENT_CLASS (klass);
187 sm_client_class->startup = sm_client_xsmp_startup;
188 sm_client_class->set_restart_command = sm_client_xsmp_set_restart_command;
189 sm_client_class->will_quit = sm_client_xsmp_will_quit;
190 sm_client_class->end_session = sm_client_xsmp_end_session;
194 egg_sm_client_xsmp_new (void)
196 if (!g_getenv ("SESSION_MANAGER"))
199 return g_object_new (EGG_TYPE_SM_CLIENT_XSMP, NULL);
203 sm_client_xsmp_set_initial_properties (gpointer user_data)
205 EggSMClientXSMP *xsmp = user_data;
206 EggDesktopFile *desktop_file;
207 GPtrArray *clone, *restart;
212 g_source_remove (xsmp->idle);
215 xsmp->waiting_to_set_initial_properties = FALSE;
217 if (egg_sm_client_get_mode () == EGG_SM_CLIENT_MODE_NO_RESTART)
218 xsmp->restart_style = SmRestartNever;
220 /* Parse info out of desktop file */
221 desktop_file = egg_get_desktop_file ();
226 char *cmdline, **argv;
229 key_file = egg_desktop_file_get_key_file (desktop_file);
231 if (xsmp->restart_style == SmRestartIfRunning)
233 if (g_key_file_has_key (key_file, EGG_DESKTOP_FILE_GROUP,
234 "X-GNOME-AutoRestart", NULL) &&
235 g_key_file_get_boolean (key_file, EGG_DESKTOP_FILE_GROUP,
236 "X-GNOME-AutoRestart", NULL))
237 xsmp->restart_style = SmRestartImmediately;
240 if (!xsmp->set_restart_command)
242 cmdline = egg_desktop_file_parse_exec (desktop_file, NULL, &err);
243 if (cmdline && g_shell_parse_argv (cmdline, &argc, &argv, &err))
245 egg_sm_client_set_restart_command (EGG_SM_CLIENT (xsmp),
246 argc, (const char **)argv);
251 g_warning ("Could not parse Exec line in desktop file: %s",
259 if (!xsmp->set_restart_command)
260 xsmp->restart_command = g_strsplit (g_get_prgname (), " ", -1);
262 clone = generate_command (xsmp->restart_command, NULL, NULL);
263 restart = generate_command (xsmp->restart_command, xsmp->client_id, NULL);
265 g_debug ("Setting initial properties");
267 /* Program, CloneCommand, RestartCommand, and UserID are required.
268 * ProcessID isn't required, but the SM may be able to do something
271 g_snprintf (pid_str, sizeof (pid_str), "%lu", (gulong) getpid ());
272 set_properties (xsmp,
273 string_prop (SmProgram, g_get_prgname ()),
274 ptrarray_prop (SmCloneCommand, clone),
275 ptrarray_prop (SmRestartCommand, restart),
276 string_prop (SmUserID, g_get_user_name ()),
277 string_prop (SmProcessID, pid_str),
278 card8_prop (SmRestartStyleHint, xsmp->restart_style),
280 g_ptr_array_free (clone, TRUE);
281 g_ptr_array_free (restart, TRUE);
285 set_properties (xsmp,
286 string_prop ("_GSM_DesktopFile", egg_desktop_file_get_source (desktop_file)),
290 update_pending_events (xsmp);
294 /* This gets called from two different places: xsmp_die() (when the
295 * server asks us to disconnect) and process_ice_messages() (when the
296 * server disconnects unexpectedly).
299 sm_client_xsmp_disconnect (EggSMClientXSMP *xsmp)
303 if (!xsmp->connection)
306 g_debug ("Disconnecting");
308 connection = xsmp->connection;
309 xsmp->connection = NULL;
310 SmcCloseConnection (connection, 0, NULL);
311 xsmp->state = XSMP_STATE_CONNECTION_CLOSED;
313 xsmp->waiting_to_save_myself = FALSE;
314 update_pending_events (xsmp);
318 sm_client_xsmp_startup (EggSMClient *client,
319 const char *client_id)
321 EggSMClientXSMP *xsmp = (EggSMClientXSMP *)client;
322 SmcCallbacks callbacks;
324 char error_string_ret[256];
326 xsmp->client_id = g_strdup (client_id);
329 SmcSetErrorHandler (smc_error_handler);
331 callbacks.save_yourself.callback = xsmp_save_yourself;
332 callbacks.die.callback = xsmp_die;
333 callbacks.save_complete.callback = xsmp_save_complete;
334 callbacks.shutdown_cancelled.callback = xsmp_shutdown_cancelled;
336 callbacks.save_yourself.client_data = xsmp;
337 callbacks.die.client_data = xsmp;
338 callbacks.save_complete.client_data = xsmp;
339 callbacks.shutdown_cancelled.client_data = xsmp;
342 error_string_ret[0] = '\0';
344 SmcOpenConnection (NULL, xsmp, SmProtoMajor, SmProtoMinor,
345 SmcSaveYourselfProcMask | SmcDieProcMask |
346 SmcSaveCompleteProcMask |
347 SmcShutdownCancelledProcMask,
349 xsmp->client_id, &ret_client_id,
350 sizeof (error_string_ret), error_string_ret);
352 if (!xsmp->connection)
354 g_warning ("Failed to connect to the session manager: %s\n",
355 error_string_ret[0] ?
356 error_string_ret : "no error message given");
357 xsmp->state = XSMP_STATE_CONNECTION_CLOSED;
361 /* We expect a pointless initial SaveYourself if either (a) we
362 * didn't have an initial client ID, or (b) we DID have an initial
363 * client ID, but the server rejected it and gave us a new one.
365 if (!xsmp->client_id ||
366 (ret_client_id && strcmp (xsmp->client_id, ret_client_id) != 0))
367 xsmp->expecting_initial_save_yourself = TRUE;
371 g_free (xsmp->client_id);
372 xsmp->client_id = g_strdup (ret_client_id);
373 free (ret_client_id);
375 gdk_threads_enter ();
376 gdk_set_sm_client_id (xsmp->client_id);
377 gdk_threads_leave ();
379 g_debug ("Got client ID \"%s\"", xsmp->client_id);
382 xsmp->state = XSMP_STATE_IDLE;
384 /* Do not set the initial properties until we reach the main loop,
385 * so that the application has a chance to call
386 * egg_set_desktop_file(). (This may also help the session manager
387 * have a better idea of when the application is fully up and
390 xsmp->waiting_to_set_initial_properties = TRUE;
391 xsmp->idle = g_idle_add (sm_client_xsmp_set_initial_properties, client);
395 sm_client_xsmp_set_restart_command (EggSMClient *client,
399 EggSMClientXSMP *xsmp = (EggSMClientXSMP *)client;
402 g_strfreev (xsmp->restart_command);
404 xsmp->restart_command = g_new (char *, argc + 1);
405 for (i = 0; i < argc; i++)
406 xsmp->restart_command[i] = g_strdup (argv[i]);
407 xsmp->restart_command[i] = NULL;
409 xsmp->set_restart_command = TRUE;
413 sm_client_xsmp_will_quit (EggSMClient *client,
416 EggSMClientXSMP *xsmp = (EggSMClientXSMP *)client;
418 if (xsmp->state == XSMP_STATE_CONNECTION_CLOSED)
420 /* The session manager has already exited! Schedule a quit
423 xsmp->waiting_to_emit_quit = TRUE;
424 update_pending_events (xsmp);
427 else if (xsmp->state == XSMP_STATE_SHUTDOWN_CANCELLED)
429 /* We received a ShutdownCancelled message while the application
430 * was interacting; Schedule a quit_cancelled signal.
432 xsmp->waiting_to_emit_quit_cancelled = TRUE;
433 update_pending_events (xsmp);
437 g_return_if_fail (xsmp->state == XSMP_STATE_INTERACT);
439 g_debug ("Sending InteractDone(%s)", will_quit ? "False" : "True");
440 SmcInteractDone (xsmp->connection, !will_quit);
442 if (will_quit && xsmp->need_save_state)
445 g_debug ("Sending SaveYourselfDone(%s)", will_quit ? "True" : "False");
446 SmcSaveYourselfDone (xsmp->connection, will_quit);
447 xsmp->state = XSMP_STATE_SAVE_YOURSELF_DONE;
451 sm_client_xsmp_end_session (EggSMClient *client,
452 EggSMClientEndStyle style,
453 gboolean request_confirmation)
455 EggSMClientXSMP *xsmp = (EggSMClientXSMP *)client;
458 /* To end the session via XSMP, we have to send a
459 * SaveYourselfRequest. We aren't allowed to do that if anything
460 * else is going on, but we don't want to expose this fact to the
461 * application. So we do our best to patch things up here...
463 * In the worst case, this method might block for some length of
464 * time in process_ice_messages, but the only time that code path is
465 * honestly likely to get hit is if the application tries to end the
466 * session as the very first thing it does, in which case it
467 * probably won't actually block anyway. It's not worth gunking up
468 * the API to try to deal nicely with the other 0.01% of cases where
472 while (xsmp->state != XSMP_STATE_IDLE ||
473 xsmp->expecting_initial_save_yourself)
475 /* If we're already shutting down, we don't need to do anything. */
476 if (xsmp->shutting_down)
481 case XSMP_STATE_CONNECTION_CLOSED:
484 case XSMP_STATE_SAVE_YOURSELF:
485 /* Trying to log out from the save_state callback? Whatever.
486 * Abort the save_state.
488 SmcSaveYourselfDone (xsmp->connection, FALSE);
489 xsmp->state = XSMP_STATE_SAVE_YOURSELF_DONE;
492 case XSMP_STATE_INTERACT_REQUEST:
493 case XSMP_STATE_INTERACT:
494 case XSMP_STATE_SHUTDOWN_CANCELLED:
495 /* Already in a shutdown-related state, just ignore
496 * the new shutdown request...
500 case XSMP_STATE_IDLE:
501 if (xsmp->waiting_to_set_initial_properties)
502 sm_client_xsmp_set_initial_properties (xsmp);
504 if (!xsmp->expecting_initial_save_yourself)
506 /* else fall through */
508 case XSMP_STATE_SAVE_YOURSELF_DONE:
509 /* We need to wait for some response from the server.*/
510 process_ice_messages (SmcGetIceConnection (xsmp->connection));
514 /* Hm... shouldn't happen */
519 /* xfce4-session will do the wrong thing if we pass SmSaveGlobal and
520 * the user chooses to save the session. But gnome-session will do
521 * the wrong thing if we pass SmSaveBoth and the user chooses NOT to
522 * save the session... Sigh.
524 if (!strcmp (SmcVendor (xsmp->connection), "xfce4-session"))
525 save_type = SmSaveBoth;
527 save_type = SmSaveGlobal;
529 g_debug ("Sending SaveYourselfRequest(SmSaveGlobal, Shutdown, SmInteractStyleAny, %sFast)", request_confirmation ? "!" : "");
530 SmcRequestSaveYourself (xsmp->connection,
534 !request_confirmation, /* fast */
540 idle_do_pending_events (gpointer data)
542 EggSMClientXSMP *xsmp = data;
543 EggSMClient *client = data;
545 gdk_threads_enter ();
549 if (xsmp->waiting_to_emit_quit)
551 xsmp->waiting_to_emit_quit = FALSE;
552 egg_sm_client_quit (client);
556 if (xsmp->waiting_to_emit_quit_cancelled)
558 xsmp->waiting_to_emit_quit_cancelled = FALSE;
559 egg_sm_client_quit_cancelled (client);
560 xsmp->state = XSMP_STATE_IDLE;
563 if (xsmp->waiting_to_save_myself)
565 xsmp->waiting_to_save_myself = FALSE;
566 do_save_yourself (xsmp);
570 gdk_threads_leave ();
575 update_pending_events (EggSMClientXSMP *xsmp)
578 xsmp->waiting_to_emit_quit ||
579 xsmp->waiting_to_emit_quit_cancelled ||
580 xsmp->waiting_to_save_myself;
585 xsmp->idle = g_idle_add (idle_do_pending_events, xsmp);
590 g_source_remove (xsmp->idle);
596 fix_broken_state (EggSMClientXSMP *xsmp, const char *message,
597 gboolean send_interact_done,
598 gboolean send_save_yourself_done)
600 g_warning ("Received XSMP %s message in state %s: client or server error",
601 message, EGG_SM_CLIENT_XSMP_STATE (xsmp));
603 /* Forget any pending SaveYourself plans we had */
604 xsmp->waiting_to_save_myself = FALSE;
605 update_pending_events (xsmp);
607 if (send_interact_done)
608 SmcInteractDone (xsmp->connection, False);
609 if (send_save_yourself_done)
610 SmcSaveYourselfDone (xsmp->connection, True);
612 xsmp->state = send_save_yourself_done ? XSMP_STATE_SAVE_YOURSELF_DONE : XSMP_STATE_IDLE;
618 xsmp_save_yourself (SmcConn smc_conn,
619 SmPointer client_data,
625 EggSMClientXSMP *xsmp = client_data;
626 gboolean wants_quit_requested;
628 g_debug ("Received SaveYourself(%s, %s, %s, %s) in state %s",
629 save_type == SmSaveLocal ? "SmSaveLocal" :
630 save_type == SmSaveGlobal ? "SmSaveGlobal" : "SmSaveBoth",
631 shutdown ? "Shutdown" : "!Shutdown",
632 interact_style == SmInteractStyleAny ? "SmInteractStyleAny" :
633 interact_style == SmInteractStyleErrors ? "SmInteractStyleErrors" :
634 "SmInteractStyleNone", fast ? "Fast" : "!Fast",
635 EGG_SM_CLIENT_XSMP_STATE (xsmp));
637 if (xsmp->state != XSMP_STATE_IDLE &&
638 xsmp->state != XSMP_STATE_SHUTDOWN_CANCELLED)
640 fix_broken_state (xsmp, "SaveYourself", FALSE, TRUE);
644 if (xsmp->waiting_to_set_initial_properties)
645 sm_client_xsmp_set_initial_properties (xsmp);
647 /* If this is the initial SaveYourself, ignore it; we've already set
648 * properties and there's no reason to actually save state too.
650 if (xsmp->expecting_initial_save_yourself)
652 xsmp->expecting_initial_save_yourself = FALSE;
654 if (save_type == SmSaveLocal &&
655 interact_style == SmInteractStyleNone &&
658 g_debug ("Sending SaveYourselfDone(True) for initial SaveYourself");
659 SmcSaveYourselfDone (xsmp->connection, True);
660 /* As explained in the comment at the end of
661 * do_save_yourself(), SAVE_YOURSELF_DONE is the correct
662 * state here, not IDLE.
664 xsmp->state = XSMP_STATE_SAVE_YOURSELF_DONE;
668 g_warning ("First SaveYourself was not the expected one!");
671 /* Even ignoring the "fast" flag completely, there are still 18
672 * different combinations of save_type, shutdown and interact_style.
673 * We interpret them as follows:
675 * Type Shutdown Interact Interpretation
676 * G F A/E/N do nothing (1)
677 * G T N do nothing (1)*
678 * G T A/E quit_requested (2)
679 * L/B F A/E/N save_state (3)
680 * L/B T N save_state (3)*
681 * L/B T A/E quit_requested, then save_state (4)
683 * 1. Do nothing, because the SM asked us to do something
684 * uninteresting (save open files, but then don't quit
685 * afterward) or rude (save open files without asking the user
688 * 2. Request interaction and then emit ::quit_requested. This
689 * perhaps isn't quite correct for the SmInteractStyleErrors
690 * case, but we don't care.
692 * 3. Emit ::save_state. The SmSaveBoth SaveYourselfs in these
693 * rows essentially get demoted to SmSaveLocal, because their
694 * Global halves correspond to "do nothing".
696 * 4. Request interaction, emit ::quit_requested, and then emit
697 * ::save_state after interacting. This is the SmSaveBoth
698 * equivalent of #2, but we also promote SmSaveLocal shutdown
699 * SaveYourselfs to SmSaveBoth here, because we want to give
700 * the user a chance to save open files before quitting.
702 * (* It would be nice if we could do something useful when the
703 * session manager sends a SaveYourself with shutdown True and
704 * SmInteractStyleNone. But we can't, so we just pretend it didn't
705 * even tell us it was shutting down. The docs for ::quit mention
706 * that it might not always be preceded by ::quit_requested.)
709 /* As an optimization, we don't actually request interaction and
710 * emit ::quit_requested if the application isn't listening to the
713 wants_quit_requested = g_signal_has_handler_pending (xsmp, g_signal_lookup ("quit_requested", EGG_TYPE_SM_CLIENT), 0, FALSE);
715 xsmp->need_save_state = (save_type != SmSaveGlobal);
716 xsmp->need_quit_requested = (shutdown && wants_quit_requested &&
717 interact_style != SmInteractStyleNone);
718 xsmp->interact_errors = (interact_style == SmInteractStyleErrors);
720 xsmp->shutting_down = shutdown;
722 do_save_yourself (xsmp);
726 do_save_yourself (EggSMClientXSMP *xsmp)
728 if (xsmp->state == XSMP_STATE_SHUTDOWN_CANCELLED)
730 /* The SM cancelled a previous SaveYourself, but we haven't yet
731 * had a chance to tell the application, so we can't start
732 * processing this SaveYourself yet.
734 xsmp->waiting_to_save_myself = TRUE;
735 update_pending_events (xsmp);
739 if (xsmp->need_quit_requested)
741 xsmp->state = XSMP_STATE_INTERACT_REQUEST;
743 g_debug ("Sending InteractRequest(%s)",
744 xsmp->interact_errors ? "Error" : "Normal");
745 SmcInteractRequest (xsmp->connection,
746 xsmp->interact_errors ? SmDialogError : SmDialogNormal,
752 if (xsmp->need_save_state)
756 /* Though unlikely, the client could have been disconnected
757 * while the application was saving its state.
759 if (!xsmp->connection)
763 g_debug ("Sending SaveYourselfDone(True)");
764 SmcSaveYourselfDone (xsmp->connection, True);
766 /* The client state diagram in the XSMP spec says that after a
767 * non-shutdown SaveYourself, we go directly back to "idle". But
768 * everything else in both the XSMP spec and the libSM docs
771 xsmp->state = XSMP_STATE_SAVE_YOURSELF_DONE;
775 merge_keyfiles (GKeyFile *dest, GKeyFile *source)
778 char **groups, **keys, *value;
780 groups = g_key_file_get_groups (source, NULL);
781 for (g = 0; groups[g]; g++)
783 keys = g_key_file_get_keys (source, groups[g], NULL, NULL);
784 for (k = 0; keys[k]; k++)
786 value = g_key_file_get_value (source, groups[g], keys[k], NULL);
789 g_key_file_set_value (dest, groups[g], keys[k], value);
799 save_state (EggSMClientXSMP *xsmp)
801 GKeyFile *state_file;
802 char *state_file_path, *data;
803 EggDesktopFile *desktop_file;
807 /* We set xsmp->state before emitting save_state, but our caller is
808 * responsible for setting it back afterward.
810 xsmp->state = XSMP_STATE_SAVE_YOURSELF;
812 state_file = egg_sm_client_save_state ((EggSMClient *)xsmp);
815 restart = generate_command (xsmp->restart_command, xsmp->client_id, NULL);
816 set_properties (xsmp,
817 ptrarray_prop (SmRestartCommand, restart),
819 g_ptr_array_free (restart, TRUE);
820 delete_properties (xsmp, SmDiscardCommand, NULL);
824 desktop_file = egg_get_desktop_file ();
827 GKeyFile *merged_file;
831 merged_file = g_key_file_new ();
832 merge_keyfiles (merged_file, egg_desktop_file_get_key_file (desktop_file));
833 merge_keyfiles (merged_file, state_file);
835 g_key_file_free (state_file);
836 state_file = merged_file;
838 /* Update Exec key using "--sm-client-state-file %k" */
839 restart = generate_command (xsmp->restart_command,
841 for (i = 0; i < restart->len; i++)
842 restart->pdata[i] = g_shell_quote (restart->pdata[i]);
843 g_ptr_array_add (restart, NULL);
844 exec = g_strjoinv (" ", (char **)restart->pdata);
845 g_strfreev ((char **)restart->pdata);
846 g_ptr_array_free (restart, FALSE);
848 g_key_file_set_string (state_file, EGG_DESKTOP_FILE_GROUP,
849 EGG_DESKTOP_FILE_KEY_EXEC,
854 /* Now write state_file to disk. (We can't use mktemp(), because
855 * that requires the filename to end with "XXXXXX", and we want
856 * it to end with ".desktop".)
859 data = g_key_file_to_data (state_file, NULL, NULL);
860 g_key_file_free (state_file);
865 state_file_path = g_strdup_printf ("%s%csession-state%c%s-%ld.%s",
866 g_get_user_config_dir (),
867 G_DIR_SEPARATOR, G_DIR_SEPARATOR,
869 (long)time (NULL) + offset,
870 desktop_file ? "desktop" : "state");
872 fd = open (state_file_path, O_WRONLY | O_CREAT | O_EXCL, 0644);
878 g_free (state_file_path);
881 else if (errno == ENOTDIR || errno == ENOENT)
883 char *sep = strrchr (state_file_path, G_DIR_SEPARATOR);
886 if (g_mkdir_with_parents (state_file_path, 0755) != 0)
888 g_warning ("Could not create directory '%s'",
890 g_free (state_file_path);
891 state_file_path = NULL;
898 g_warning ("Could not create file '%s': %s",
899 state_file_path, g_strerror (errno));
900 g_free (state_file_path);
901 state_file_path = NULL;
906 g_file_set_contents (state_file_path, data, -1, NULL);
911 restart = generate_command (xsmp->restart_command, xsmp->client_id,
913 set_properties (xsmp,
914 ptrarray_prop (SmRestartCommand, restart),
916 g_ptr_array_free (restart, TRUE);
920 set_properties (xsmp,
921 array_prop (SmDiscardCommand,
922 "/bin/rm", "-rf", state_file_path,
925 g_free (state_file_path);
930 xsmp_interact (SmcConn smc_conn,
931 SmPointer client_data)
933 EggSMClientXSMP *xsmp = client_data;
934 EggSMClient *client = client_data;
936 g_debug ("Received Interact message in state %s",
937 EGG_SM_CLIENT_XSMP_STATE (xsmp));
939 if (xsmp->state != XSMP_STATE_INTERACT_REQUEST)
941 fix_broken_state (xsmp, "Interact", TRUE, TRUE);
945 xsmp->state = XSMP_STATE_INTERACT;
946 egg_sm_client_quit_requested (client);
950 xsmp_die (SmcConn smc_conn,
951 SmPointer client_data)
953 EggSMClientXSMP *xsmp = client_data;
954 EggSMClient *client = client_data;
956 g_debug ("Received Die message in state %s",
957 EGG_SM_CLIENT_XSMP_STATE (xsmp));
959 sm_client_xsmp_disconnect (xsmp);
960 egg_sm_client_quit (client);
964 xsmp_save_complete (SmcConn smc_conn,
965 SmPointer client_data)
967 EggSMClientXSMP *xsmp = client_data;
969 g_debug ("Received SaveComplete message in state %s",
970 EGG_SM_CLIENT_XSMP_STATE (xsmp));
972 if (xsmp->state == XSMP_STATE_SAVE_YOURSELF_DONE)
973 xsmp->state = XSMP_STATE_IDLE;
975 fix_broken_state (xsmp, "SaveComplete", FALSE, FALSE);
979 xsmp_shutdown_cancelled (SmcConn smc_conn,
980 SmPointer client_data)
982 EggSMClientXSMP *xsmp = client_data;
983 EggSMClient *client = client_data;
985 g_debug ("Received ShutdownCancelled message in state %s",
986 EGG_SM_CLIENT_XSMP_STATE (xsmp));
988 xsmp->shutting_down = FALSE;
990 if (xsmp->state == XSMP_STATE_SAVE_YOURSELF_DONE)
992 /* We've finished interacting and now the SM has agreed to
993 * cancel the shutdown.
995 xsmp->state = XSMP_STATE_IDLE;
996 egg_sm_client_quit_cancelled (client);
998 else if (xsmp->state == XSMP_STATE_SHUTDOWN_CANCELLED)
1000 /* Hm... ok, so we got a shutdown SaveYourself, which got
1001 * cancelled, but the application was still interacting, so we
1002 * didn't tell it yet, and then *another* SaveYourself arrived,
1003 * which we must still be waiting to tell the app about, except
1004 * that now that SaveYourself has been cancelled too! Dizzy yet?
1006 xsmp->waiting_to_save_myself = FALSE;
1007 update_pending_events (xsmp);
1011 g_debug ("Sending SaveYourselfDone(False)");
1012 SmcSaveYourselfDone (xsmp->connection, False);
1014 if (xsmp->state == XSMP_STATE_INTERACT)
1016 /* The application is currently interacting, so we can't
1017 * tell it about the cancellation yet; we will wait until
1018 * after it calls egg_sm_client_will_quit().
1020 xsmp->state = XSMP_STATE_SHUTDOWN_CANCELLED;
1024 /* The shutdown was cancelled before the application got a
1025 * chance to interact.
1027 xsmp->state = XSMP_STATE_IDLE;
1034 /* Create a restart/clone/Exec command based on @restart_command.
1035 * If @client_id is non-%NULL, add "--sm-client-id @client_id".
1036 * If @state_file is non-%NULL, add "--sm-client-state-file @state_file".
1038 * None of the input strings are g_strdup()ed; the caller must keep
1039 * them around until it is done with the returned GPtrArray, and must
1040 * then free the array, but not its contents.
1043 generate_command (char **restart_command, const char *client_id,
1044 const char *state_file)
1049 cmd = g_ptr_array_new ();
1050 g_ptr_array_add (cmd, restart_command[0]);
1054 g_ptr_array_add (cmd, "--sm-client-id");
1055 g_ptr_array_add (cmd, (char *)client_id);
1060 g_ptr_array_add (cmd, "--sm-client-state-file");
1061 g_ptr_array_add (cmd, (char *)state_file);
1064 for (i = 1; restart_command[i]; i++)
1065 g_ptr_array_add (cmd, restart_command[i]);
1070 /* Takes a NULL-terminated list of SmProp * values, created by
1071 * array_prop, ptrarray_prop, string_prop, card8_prop, sets them, and
1075 set_properties (EggSMClientXSMP *xsmp, ...)
1082 props = g_ptr_array_new ();
1084 va_start (ap, xsmp);
1085 while ((prop = va_arg (ap, SmProp *)))
1086 g_ptr_array_add (props, prop);
1089 if (xsmp->connection)
1091 SmcSetProperties (xsmp->connection, props->len,
1092 (SmProp **)props->pdata);
1095 for (i = 0; i < props->len; i++)
1097 prop = props->pdata[i];
1098 g_free (prop->vals);
1101 g_ptr_array_free (props, TRUE);
1104 /* Takes a NULL-terminated list of property names and deletes them. */
1106 delete_properties (EggSMClientXSMP *xsmp, ...)
1112 if (!xsmp->connection)
1115 props = g_ptr_array_new ();
1117 va_start (ap, xsmp);
1118 while ((prop = va_arg (ap, char *)))
1119 g_ptr_array_add (props, prop);
1122 SmcDeleteProperties (xsmp->connection, props->len,
1123 (char **)props->pdata);
1125 g_ptr_array_free (props, TRUE);
1128 /* Takes an array of strings and creates a LISTofARRAY8 property. The
1129 * strings are neither dupped nor freed; they need to remain valid
1130 * until you're done with the SmProp.
1133 array_prop (const char *name, ...)
1141 prop = g_new (SmProp, 1);
1142 prop->name = (char *)name;
1143 prop->type = SmLISTofARRAY8;
1145 vals = g_array_new (FALSE, FALSE, sizeof (SmPropValue));
1147 va_start (ap, name);
1148 while ((value = va_arg (ap, char *)))
1150 pv.length = strlen (value);
1152 g_array_append_val (vals, pv);
1155 prop->num_vals = vals->len;
1156 prop->vals = (SmPropValue *)vals->data;
1158 g_array_free (vals, FALSE);
1163 /* Takes a GPtrArray of strings and creates a LISTofARRAY8 property.
1164 * The array contents are neither dupped nor freed; they need to
1165 * remain valid until you're done with the SmProp.
1168 ptrarray_prop (const char *name, GPtrArray *values)
1175 prop = g_new (SmProp, 1);
1176 prop->name = (char *)name;
1177 prop->type = SmLISTofARRAY8;
1179 vals = g_array_new (FALSE, FALSE, sizeof (SmPropValue));
1181 for (i = 0; i < values->len; i++)
1183 pv.length = strlen (values->pdata[i]);
1184 pv.value = values->pdata[i];
1185 g_array_append_val (vals, pv);
1188 prop->num_vals = vals->len;
1189 prop->vals = (SmPropValue *)vals->data;
1191 g_array_free (vals, FALSE);
1196 /* Takes a string and creates an ARRAY8 property. The string is
1197 * neither dupped nor freed; it needs to remain valid until you're
1198 * done with the SmProp.
1201 string_prop (const char *name, const char *value)
1205 prop = g_new (SmProp, 1);
1206 prop->name = (char *)name;
1207 prop->type = SmARRAY8;
1210 prop->vals = g_new (SmPropValue, 1);
1212 prop->vals[0].length = strlen (value);
1213 prop->vals[0].value = (char *)value;
1218 /* Takes a char and creates a CARD8 property. */
1220 card8_prop (const char *name, unsigned char value)
1225 /* To avoid having to allocate and free prop->vals[0], we cheat and
1226 * make vals a 2-element-long array and then use the second element
1230 prop = g_new (SmProp, 1);
1231 prop->name = (char *)name;
1232 prop->type = SmCARD8;
1235 prop->vals = g_new (SmPropValue, 2);
1236 card8val = (char *)(&prop->vals[1]);
1237 card8val[0] = value;
1239 prop->vals[0].length = 1;
1240 prop->vals[0].value = card8val;
1245 /* ICE code. This makes no effort to play nice with anyone else trying
1246 * to use libICE. Fortunately, no one uses libICE for anything other
1247 * than SM. (DCOP uses ICE, but it has its own private copy of
1250 * When this moves to gtk, it will need to be cleverer, to avoid
1251 * tripping over old apps that use GnomeClient or that use libSM
1255 #include <X11/ICE/ICElib.h>
1258 static void ice_error_handler (IceConn ice_conn,
1260 int offending_minor_opcode,
1261 unsigned long offending_sequence,
1265 static void ice_io_error_handler (IceConn ice_conn);
1266 static void ice_connection_watch (IceConn ice_conn,
1267 IcePointer client_data,
1269 IcePointer *watch_data);
1274 IceSetIOErrorHandler (ice_io_error_handler);
1275 IceSetErrorHandler (ice_error_handler);
1276 IceAddConnectionWatch (ice_connection_watch, NULL);
1280 process_ice_messages (IceConn ice_conn)
1282 IceProcessMessagesStatus status;
1284 gdk_threads_enter ();
1285 status = IceProcessMessages (ice_conn, NULL, NULL);
1286 gdk_threads_leave ();
1290 case IceProcessMessagesSuccess:
1293 case IceProcessMessagesIOError:
1294 sm_client_xsmp_disconnect (IceGetConnectionContext (ice_conn));
1297 case IceProcessMessagesConnectionClosed:
1301 g_assert_not_reached ();
1306 ice_iochannel_watch (GIOChannel *channel,
1307 GIOCondition condition,
1308 gpointer client_data)
1310 return process_ice_messages (client_data);
1314 ice_connection_watch (IceConn ice_conn,
1315 IcePointer client_data,
1317 IcePointer *watch_data)
1323 GIOChannel *channel;
1324 int fd = IceConnectionNumber (ice_conn);
1326 fcntl (fd, F_SETFD, fcntl (fd, F_GETFD, 0) | FD_CLOEXEC);
1327 channel = g_io_channel_unix_new (fd);
1328 watch_id = g_io_add_watch (channel, G_IO_IN | G_IO_ERR,
1329 ice_iochannel_watch, ice_conn);
1330 g_io_channel_unref (channel);
1332 *watch_data = GUINT_TO_POINTER (watch_id);
1336 watch_id = GPOINTER_TO_UINT (*watch_data);
1337 g_source_remove (watch_id);
1342 ice_error_handler (IceConn ice_conn,
1344 int offending_minor_opcode,
1345 unsigned long offending_sequence,
1354 ice_io_error_handler (IceConn ice_conn)
1360 smc_error_handler (SmcConn smc_conn,
1362 int offending_minor_opcode,
1363 unsigned long offending_sequence,