1 /* Session management module for systems which understand the X Session
4 Copyright (C) 2002-2011 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs 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
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include <X11/SM/SMlib.h>
27 #include <X11/Xutil.h>
30 #include <sys/param.h>
36 #include "sysselect.h"
38 #include "termhooks.h"
44 /* This is the event used when SAVE_SESSION_EVENT occurs. */
46 static struct input_event emacs_event
;
48 /* The descriptor that we use to check for data from the session manager. */
52 /* A flag that says if we are in shutdown interactions or not. */
54 static int doing_interact
;
56 /* The session manager object for the session manager connection. */
58 static SmcConn smc_conn
;
60 /* The client session id for this session. */
62 static char *client_id
;
64 /* The full path name to the Emacs program. */
66 static char *emacs_program
;
68 /* The option we tell the session manager to start Emacs with when
69 restarting Emacs. The client_id is appended. */
71 #define SMID_OPT "--smid="
74 /* The option to start Emacs without the splash screen when
77 static char NOSPLASH_OPT
[] = "--no-splash";
79 /* The option to make Emacs start in the given directory. */
81 #define CHDIR_OPT "--chdir="
84 ice_connection_closed (void)
87 delete_read_fd (ice_fd
);
92 /* Handle any messages from the session manager. If no connection is
93 open to a session manager, just return. */
96 x_session_check_input (int fd
, void *data
, int for_read
)
100 if (ice_fd
== -1) return;
102 /* Reset this so wo can check kind after callbacks have been called by
103 IceProcessMessages. The smc_interact_CB sets the kind to
104 SAVE_SESSION_EVENT, but we don't know beforehand if that callback
106 emacs_event
.kind
= NO_EVENT
;
108 ret
= IceProcessMessages (SmcGetIceConnection (smc_conn
),
109 (IceReplyWaitInfo
*)0, (Bool
*)0);
110 if (ret
!= IceProcessMessagesSuccess
)
112 /* Either IO error or Connection closed. */
113 if (ret
== IceProcessMessagesIOError
)
114 IceCloseConnection (SmcGetIceConnection (smc_conn
));
116 ice_connection_closed ();
119 /* Check if smc_interact_CB was called and we shall generate a
120 SAVE_SESSION_EVENT. */
121 if (emacs_event
.kind
!= NO_EVENT
)
122 kbd_buffer_store_event (&emacs_event
);
125 /* Return non-zero if we have a connection to a session manager. */
128 x_session_have_connection (void)
133 /* This is called when the session manager says it is OK to interact with the
134 user. Here we set the kind to SAVE_SESSION_EVENT so an event is generated.
135 Then lisp code can interact with the user. */
138 smc_interact_CB (SmcConn smcConn
, SmPointer clientData
)
140 doing_interact
= True
;
141 emacs_event
.kind
= SAVE_SESSION_EVENT
;
142 emacs_event
.arg
= Qnil
;
145 /* This is called when the session manager tells us to save ourselves.
146 We set the required properties so the session manager can restart us,
147 plus the current working directory property (not mandatory) so we
148 are started in the correct directory.
150 If this is a shutdown and we can request to interact with the user,
151 we do so, because we don't know what the lisp code might do. */
154 smc_save_yourself_CB (SmcConn smcConn
,
155 SmPointer clientData
,
163 SmProp
*props
[NR_PROPS
];
164 SmProp prop_ptr
[NR_PROPS
];
166 SmPropValue values
[20], *vp
;
167 int val_idx
= 0, vp_idx
= 0;
170 char *cwd
= get_current_dir_name ();
171 char *smid_opt
, *chdir_opt
= NULL
;
173 /* How to start a new instance of Emacs. */
174 props
[props_idx
] = &prop_ptr
[props_idx
];
175 props
[props_idx
]->name
= xstrdup (SmCloneCommand
);
176 props
[props_idx
]->type
= xstrdup (SmLISTofARRAY8
);
177 props
[props_idx
]->num_vals
= 1;
178 props
[props_idx
]->vals
= &values
[val_idx
++];
179 props
[props_idx
]->vals
[0].length
= strlen (emacs_program
);
180 props
[props_idx
]->vals
[0].value
= emacs_program
;
183 /* The name of the program. */
184 props
[props_idx
] = &prop_ptr
[props_idx
];
185 props
[props_idx
]->name
= xstrdup (SmProgram
);
186 props
[props_idx
]->type
= xstrdup (SmARRAY8
);
187 props
[props_idx
]->num_vals
= 1;
188 props
[props_idx
]->vals
= &values
[val_idx
++];
189 props
[props_idx
]->vals
[0].length
= strlen (SSDATA (Vinvocation_name
));
190 props
[props_idx
]->vals
[0].value
= SDATA (Vinvocation_name
);
194 props
[props_idx
] = &prop_ptr
[props_idx
];
195 props
[props_idx
]->name
= xstrdup (SmUserID
);
196 props
[props_idx
]->type
= xstrdup (SmARRAY8
);
197 props
[props_idx
]->num_vals
= 1;
198 props
[props_idx
]->vals
= &values
[val_idx
++];
199 props
[props_idx
]->vals
[0].length
= strlen (SSDATA (Vuser_login_name
));
200 props
[props_idx
]->vals
[0].value
= SDATA (Vuser_login_name
);
206 props
[props_idx
] = &prop_ptr
[props_idx
];
207 props
[props_idx
]->name
= xstrdup (SmCurrentDirectory
);
208 props
[props_idx
]->type
= xstrdup (SmARRAY8
);
209 props
[props_idx
]->num_vals
= 1;
210 props
[props_idx
]->vals
= &values
[val_idx
++];
211 props
[props_idx
]->vals
[0].length
= strlen (cwd
);
212 props
[props_idx
]->vals
[0].value
= cwd
;
217 /* How to restart Emacs. */
218 props
[props_idx
] = &prop_ptr
[props_idx
];
219 props
[props_idx
]->name
= xstrdup (SmRestartCommand
);
220 props
[props_idx
]->type
= xstrdup (SmLISTofARRAY8
);
221 /* /path/to/emacs, --smid=xxx --no-splash --chdir=dir ... */
222 i
= 3 + initial_argc
;
223 props
[props_idx
]->num_vals
= i
;
224 vp
= (SmPropValue
*) xmalloc (i
* sizeof(*vp
));
225 props
[props_idx
]->vals
= vp
;
226 props
[props_idx
]->vals
[vp_idx
].length
= strlen (emacs_program
);
227 props
[props_idx
]->vals
[vp_idx
++].value
= emacs_program
;
229 smid_opt
= xmalloc (strlen (SMID_OPT
) + strlen (client_id
) + 1);
230 strcpy (smid_opt
, SMID_OPT
);
231 strcat (smid_opt
, client_id
);
233 props
[props_idx
]->vals
[vp_idx
].length
= strlen (smid_opt
);
234 props
[props_idx
]->vals
[vp_idx
++].value
= smid_opt
;
236 props
[props_idx
]->vals
[vp_idx
].length
= strlen (NOSPLASH_OPT
);
237 props
[props_idx
]->vals
[vp_idx
++].value
= NOSPLASH_OPT
;
241 chdir_opt
= xmalloc (strlen (CHDIR_OPT
) + strlen (cwd
) + 1);
242 strcpy (chdir_opt
, CHDIR_OPT
);
243 strcat (chdir_opt
, cwd
);
245 props
[props_idx
]->vals
[vp_idx
].length
= strlen (chdir_opt
);
246 props
[props_idx
]->vals
[vp_idx
++].value
= chdir_opt
;
249 for (i
= 1; i
< initial_argc
; ++i
)
251 props
[props_idx
]->vals
[vp_idx
].length
= strlen (initial_argv
[i
]);
252 props
[props_idx
]->vals
[vp_idx
++].value
= initial_argv
[i
];
257 SmcSetProperties (smcConn
, props_idx
, props
);
264 for (i
= 0; i
< props_idx
; ++i
)
266 xfree (props
[i
]->type
);
267 xfree (props
[i
]->name
);
270 /* See if we maybe shall interact with the user. */
271 if (interactStyle
!= SmInteractStyleAny
273 || saveType
== SmSaveLocal
274 || ! SmcInteractRequest (smcConn
, SmDialogNormal
, smc_interact_CB
, 0))
276 /* No interaction, we are done saving ourself. */
277 SmcSaveYourselfDone (smcConn
, True
);
281 /* According to the SM specification, this shall close the connection. */
284 smc_die_CB (SmcConn smcConn
, SmPointer clientData
)
286 emacs_event
.kind
= SAVE_SESSION_EVENT
;
287 emacs_event
.arg
= Qt
;
290 /* We don't use the next two but they are mandatory, leave them empty.
291 According to the SM specification, we should not interact with the
292 user between smc_save_yourself_CB is called and until smc_save_complete_CB
293 is called. It seems like a lot of job to implement this and it doesn't
294 even seem necessary. */
297 smc_save_complete_CB (SmcConn smcConn
, SmPointer clientData
)
303 smc_shutdown_cancelled_CB (SmcConn smcConn
, SmPointer clientData
)
308 /* Error handlers for SM and ICE. We don't want to exit Emacs just
309 because there is some error in the session management. */
312 smc_error_handler (SmcConn smcConn
,
314 int offendingMinorOpcode
,
315 unsigned long offendingSequence
,
324 ice_error_handler (IceConn iceConn
,
326 int offendingMinorOpcode
,
327 unsigned long offendingSequence
,
337 ice_io_error_handler (IceConn iceConn
)
339 /* Connection probably gone. */
340 ice_connection_closed ();
343 /* This is called when the ICE connection is created or closed. The SM library
344 uses ICE as it transport protocol. */
347 ice_conn_watch_CB (IceConn iceConn
, IcePointer clientData
,
348 int opening
, IcePointer
*watchData
)
352 ice_connection_closed ();
356 ice_fd
= IceConnectionNumber (iceConn
);
357 add_read_fd (ice_fd
, x_session_check_input
, NULL
);
360 /* Create the client leader window. */
364 create_client_leader_window (struct x_display_info
*dpyinfo
, char *client_id
)
367 XClassHint class_hints
;
370 w
= XCreateSimpleWindow (dpyinfo
->display
,
371 dpyinfo
->root_window
,
373 CopyFromParent
, CopyFromParent
, CopyFromParent
);
375 class_hints
.res_name
= SSDATA (Vx_resource_name
);
376 class_hints
.res_class
= SSDATA (Vx_resource_class
);
377 XSetClassHint (dpyinfo
->display
, w
, &class_hints
);
378 XStoreName (dpyinfo
->display
, w
, class_hints
.res_name
);
380 XChangeProperty (dpyinfo
->display
, w
, dpyinfo
->Xatom_SM_CLIENT_ID
,
381 XA_STRING
, 8, PropModeReplace
,
382 (unsigned char *)client_id
, strlen (client_id
));
384 dpyinfo
->client_leader_window
= w
;
386 #endif /* ! USE_GTK */
389 /* Try to open a connection to the session manager. */
392 x_session_initialize (struct x_display_info
*dpyinfo
)
394 #define SM_ERRORSTRING_LEN 512
395 char errorstring
[SM_ERRORSTRING_LEN
];
396 char* previous_id
= NULL
;
397 SmcCallbacks callbacks
;
401 doing_interact
= False
;
403 /* Check if we where started by the session manager. If so, we will
404 have a previous id. */
405 if (! EQ (Vx_session_previous_id
, Qnil
) && STRINGP (Vx_session_previous_id
))
406 previous_id
= SSDATA (Vx_session_previous_id
);
408 /* Construct the path to the Emacs program. */
409 if (! EQ (Vinvocation_directory
, Qnil
))
410 name_len
+= strlen (SSDATA (Vinvocation_directory
));
411 name_len
+= strlen (SSDATA (Vinvocation_name
));
413 /* This malloc will not be freed, but it is only done once, and hopefully
415 emacs_program
= xmalloc (name_len
+ 1);
416 emacs_program
[0] = '\0';
418 if (! EQ (Vinvocation_directory
, Qnil
))
419 strcpy (emacs_program
, SSDATA (Vinvocation_directory
));
420 strcat (emacs_program
, SSDATA (Vinvocation_name
));
422 /* The SM protocol says all callbacks are mandatory, so set up all
423 here and in the mask passed to SmcOpenConnection. */
424 callbacks
.save_yourself
.callback
= smc_save_yourself_CB
;
425 callbacks
.save_yourself
.client_data
= 0;
426 callbacks
.die
.callback
= smc_die_CB
;
427 callbacks
.die
.client_data
= 0;
428 callbacks
.save_complete
.callback
= smc_save_complete_CB
;
429 callbacks
.save_complete
.client_data
= 0;
430 callbacks
.shutdown_cancelled
.callback
= smc_shutdown_cancelled_CB
;
431 callbacks
.shutdown_cancelled
.client_data
= 0;
433 /* Set error handlers. */
434 SmcSetErrorHandler (smc_error_handler
);
435 IceSetErrorHandler (ice_error_handler
);
436 IceSetIOErrorHandler (ice_io_error_handler
);
438 /* Install callback for when connection status changes. */
439 IceAddConnectionWatch (ice_conn_watch_CB
, 0);
441 /* Open the connection to the session manager. A failure is not
442 critical, it usually means that no session manager is running.
443 The errorstring is here for debugging. */
444 smc_conn
= SmcOpenConnection (NULL
, NULL
, 1, 0,
445 (SmcSaveYourselfProcMask
|
447 SmcSaveCompleteProcMask
|
448 SmcShutdownCancelledProcMask
),
457 Vx_session_id
= make_string (client_id
, strlen (client_id
));
460 /* GTK creats a leader window by itself, but we need to tell
461 it about our client_id. */
462 gdk_set_sm_client_id (client_id
);
464 create_client_leader_window (dpyinfo
, client_id
);
469 /* Ensure that the session manager is not contacted again. */
472 x_session_close (void)
474 ice_connection_closed ();
478 DEFUN ("handle-save-session", Fhandle_save_session
,
479 Shandle_save_session
, 1, 1, "e",
480 doc
: /* Handle the save_yourself event from a session manager.
481 A session manager can tell Emacs that the window system is shutting down
482 by sending Emacs a save_yourself message. Emacs executes this function when
483 such an event occurs. This function then executes `emacs-session-save'.
484 After that, this function informs the session manager that it can continue
485 or abort shutting down the window system depending on the return value
486 from `emacs-session-save' If the return value is non-nil the session manager
487 is told to abort the window system shutdown.
489 Do not call this function yourself. */)
492 int kill_emacs
= CONSP (event
) && CONSP (XCDR (event
))
493 && EQ (Qt
, XCAR (XCDR (event
)));
495 /* Check doing_interact so that we don't do anything if someone called
496 this at the wrong time. */
497 if (doing_interact
&& ! kill_emacs
)
499 Bool cancel_shutdown
= False
;
501 cancel_shutdown
= ! EQ (call0 (intern ("emacs-session-save")), Qnil
);
503 SmcInteractDone (smc_conn
, cancel_shutdown
);
504 SmcSaveYourselfDone (smc_conn
, True
);
506 doing_interact
= False
;
510 /* We should not do user interaction here, but it is not easy to
511 prevent. Fix this in next version. */
514 /* This will not be reached, but we want kill-emacs-hook to be run. */
515 SmcCloseConnection (smc_conn
, 0, 0);
516 ice_connection_closed ();
524 /***********************************************************************
526 ***********************************************************************/
528 syms_of_xsmfns (void)
530 DEFVAR_LISP ("x-session-id", Vx_session_id
,
531 doc
: /* The session id Emacs got from the session manager for this session.
532 Changing the value does not change the session id used by Emacs.
533 The value is nil if no session manager is running.
534 See also `x-session-previous-id', `emacs-save-session-functions',
535 `emacs-session-save' and `emacs-session-restore'." */);
536 Vx_session_id
= Qnil
;
538 DEFVAR_LISP ("x-session-previous-id", Vx_session_previous_id
,
539 doc
: /* The previous session id Emacs got from session manager.
540 If Emacs is running on a window system that has a session manager, the
541 session manager gives Emacs a session id. It is feasible for Emacs Lisp
542 code to use the session id to save configuration in, for example, a file
543 with a file name based on the session id. If Emacs is running when the
544 window system is shut down, the session manager remembers that Emacs was
545 running and saves the session id Emacs had.
547 When the window system is started again, the session manager restarts
548 Emacs and hands Emacs the session id it had the last time it was
549 running. This is now the previous session id and the value of this
550 variable. If configuration was saved in a file as stated above, the
551 previous session id shall be used to reconstruct the file name.
553 The session id Emacs has while it is running is in the variable
554 `x-session-id'. The value of this variable and `x-session-id' may be the
555 same, depending on how the session manager works.
557 See also `emacs-save-session-functions', `emacs-session-save' and
558 `emacs-session-restore'." */);
559 Vx_session_previous_id
= Qnil
;
561 defsubr (&Shandle_save_session
);
564 #endif /* HAVE_X_SM */