Commit | Line | Data |
---|---|---|
afb4ecad JD |
1 | /* Session management module for systems which understand the X Session |
2 | management protocol. | |
95df8112 | 3 | |
ab422c4d | 4 | Copyright (C) 2002-2013 Free Software Foundation, Inc. |
afb4ecad JD |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
afb4ecad | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
afb4ecad JD |
12 | |
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. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
afb4ecad JD |
20 | |
21 | #include <config.h> | |
22 | ||
23 | #ifdef HAVE_X_SM | |
24 | ||
25 | #include <X11/SM/SMlib.h> | |
231d6cfb JD |
26 | #include <X11/Xlib.h> |
27 | #include <X11/Xutil.h> | |
28 | ||
afb4ecad | 29 | #include <unistd.h> |
afb4ecad | 30 | #include <sys/param.h> |
656132eb | 31 | #include <stdio.h> |
afb4ecad | 32 | |
fa8459a3 | 33 | #include "lisp.h" |
afb4ecad JD |
34 | #include "systime.h" |
35 | #include "sysselect.h" | |
428a555e | 36 | #include "frame.h" |
afb4ecad | 37 | #include "termhooks.h" |
231d6cfb | 38 | #include "xterm.h" |
4df0af9b JD |
39 | #include "process.h" |
40 | #include "keyboard.h" | |
afb4ecad | 41 | |
dde42981 | 42 | #if defined USE_GTK && !defined HAVE_GTK3 |
2baccd04 | 43 | #define gdk_x11_set_sm_client_id(w) gdk_set_sm_client_id (w) |
0afb4571 J |
44 | #endif |
45 | ||
3b8f9651 | 46 | /* This is the event used when SAVE_SESSION_EVENT occurs. */ |
afb4ecad JD |
47 | |
48 | static struct input_event emacs_event; | |
49 | ||
3b8f9651 | 50 | /* The descriptor that we use to check for data from the session manager. */ |
afb4ecad | 51 | |
182659ae | 52 | static int ice_fd; |
afb4ecad | 53 | |
3b8f9651 | 54 | /* A flag that says if we are in shutdown interactions or not. */ |
afb4ecad | 55 | |
182659ae | 56 | static int doing_interact; |
afb4ecad | 57 | |
4f2f546e | 58 | /* The session manager object for the session manager connection. */ |
afb4ecad JD |
59 | |
60 | static SmcConn smc_conn; | |
61 | ||
4f2f546e JD |
62 | /* The client session id for this session. */ |
63 | ||
afb4ecad JD |
64 | static char *client_id; |
65 | ||
4f2f546e JD |
66 | /* The full path name to the Emacs program. */ |
67 | ||
afb4ecad JD |
68 | static char *emacs_program; |
69 | ||
afb4ecad | 70 | /* The option we tell the session manager to start Emacs with when |
4f2f546e | 71 | restarting Emacs. The client_id is appended. */ |
afb4ecad JD |
72 | |
73 | #define SMID_OPT "--smid=" | |
74 | ||
75 | ||
ca2417b9 | 76 | /* The option to start Emacs without the splash screen when |
4f2f546e | 77 | restarting Emacs. */ |
ca2417b9 | 78 | |
42ca4633 | 79 | static char NOSPLASH_OPT[] = "--no-splash"; |
ca2417b9 | 80 | |
f63d0028 JD |
81 | /* The option to make Emacs start in the given directory. */ |
82 | ||
83 | #define CHDIR_OPT "--chdir=" | |
84 | ||
182659ae | 85 | static void |
971de7fb | 86 | ice_connection_closed (void) |
182659ae JD |
87 | { |
88 | if (ice_fd >= 0) | |
4df0af9b | 89 | delete_read_fd (ice_fd); |
182659ae JD |
90 | ice_fd = -1; |
91 | } | |
92 | ||
ca2417b9 | 93 | |
afb4ecad | 94 | /* Handle any messages from the session manager. If no connection is |
4df0af9b | 95 | open to a session manager, just return. */ |
4f2f546e | 96 | |
4df0af9b | 97 | static void |
de1339b0 | 98 | x_session_check_input (int fd, void *data) |
afb4ecad | 99 | { |
182659ae | 100 | int ret; |
7d0393cf | 101 | |
4df0af9b | 102 | if (ice_fd == -1) return; |
7d0393cf | 103 | |
afb4ecad JD |
104 | /* Reset this so wo can check kind after callbacks have been called by |
105 | IceProcessMessages. The smc_interact_CB sets the kind to | |
3b8f9651 | 106 | SAVE_SESSION_EVENT, but we don't know beforehand if that callback |
4f2f546e | 107 | will be called. */ |
3b8f9651 | 108 | emacs_event.kind = NO_EVENT; |
afb4ecad | 109 | |
4df0af9b JD |
110 | ret = IceProcessMessages (SmcGetIceConnection (smc_conn), |
111 | (IceReplyWaitInfo *)0, (Bool *)0); | |
112 | if (ret != IceProcessMessagesSuccess) | |
afb4ecad | 113 | { |
4df0af9b JD |
114 | /* Either IO error or Connection closed. */ |
115 | if (ret == IceProcessMessagesIOError) | |
116 | IceCloseConnection (SmcGetIceConnection (smc_conn)); | |
117 | ||
182659ae JD |
118 | ice_connection_closed (); |
119 | } | |
7d0393cf | 120 | |
afb4ecad | 121 | /* Check if smc_interact_CB was called and we shall generate a |
4f2f546e | 122 | SAVE_SESSION_EVENT. */ |
182659ae | 123 | if (emacs_event.kind != NO_EVENT) |
4df0af9b | 124 | kbd_buffer_store_event (&emacs_event); |
afb4ecad JD |
125 | } |
126 | ||
4f2f546e JD |
127 | /* Return non-zero if we have a connection to a session manager. */ |
128 | ||
afb4ecad | 129 | int |
971de7fb | 130 | x_session_have_connection (void) |
afb4ecad JD |
131 | { |
132 | return ice_fd != -1; | |
133 | } | |
134 | ||
135 | /* This is called when the session manager says it is OK to interact with the | |
3b8f9651 | 136 | user. Here we set the kind to SAVE_SESSION_EVENT so an event is generated. |
4f2f546e JD |
137 | Then lisp code can interact with the user. */ |
138 | ||
afb4ecad | 139 | static void |
971de7fb | 140 | smc_interact_CB (SmcConn smcConn, SmPointer clientData) |
afb4ecad JD |
141 | { |
142 | doing_interact = True; | |
3b8f9651 | 143 | emacs_event.kind = SAVE_SESSION_EVENT; |
0b9fc69a | 144 | emacs_event.arg = Qnil; |
afb4ecad JD |
145 | } |
146 | ||
f25dcaa0 | 147 | /* This is called when the session manager tells us to save ourselves. |
afb4ecad JD |
148 | We set the required properties so the session manager can restart us, |
149 | plus the current working directory property (not mandatory) so we | |
150 | are started in the correct directory. | |
151 | ||
152 | If this is a shutdown and we can request to interact with the user, | |
4f2f546e JD |
153 | we do so, because we don't know what the lisp code might do. */ |
154 | ||
afb4ecad | 155 | static void |
dd4c5104 DN |
156 | smc_save_yourself_CB (SmcConn smcConn, |
157 | SmPointer clientData, | |
158 | int saveType, | |
159 | Bool shutdown, | |
160 | int interactStyle, | |
161 | Bool fast) | |
afb4ecad JD |
162 | { |
163 | #define NR_PROPS 5 | |
7d0393cf | 164 | |
afb4ecad JD |
165 | SmProp *props[NR_PROPS]; |
166 | SmProp prop_ptr[NR_PROPS]; | |
7d0393cf | 167 | |
4df0af9b JD |
168 | SmPropValue values[20], *vp; |
169 | int val_idx = 0, vp_idx = 0; | |
afb4ecad | 170 | int props_idx = 0; |
42ca4633 | 171 | int i; |
4df0af9b | 172 | char *cwd = get_current_dir_name (); |
f63d0028 | 173 | char *smid_opt, *chdir_opt = NULL; |
afb4ecad | 174 | |
4f2f546e | 175 | /* How to start a new instance of Emacs. */ |
afb4ecad | 176 | props[props_idx] = &prop_ptr[props_idx]; |
42ca4633 J |
177 | props[props_idx]->name = xstrdup (SmCloneCommand); |
178 | props[props_idx]->type = xstrdup (SmLISTofARRAY8); | |
afb4ecad JD |
179 | props[props_idx]->num_vals = 1; |
180 | props[props_idx]->vals = &values[val_idx++]; | |
181 | props[props_idx]->vals[0].length = strlen (emacs_program); | |
182 | props[props_idx]->vals[0].value = emacs_program; | |
183 | ++props_idx; | |
184 | ||
4f2f546e | 185 | /* The name of the program. */ |
afb4ecad | 186 | props[props_idx] = &prop_ptr[props_idx]; |
42ca4633 J |
187 | props[props_idx]->name = xstrdup (SmProgram); |
188 | props[props_idx]->type = xstrdup (SmARRAY8); | |
afb4ecad JD |
189 | props[props_idx]->num_vals = 1; |
190 | props[props_idx]->vals = &values[val_idx++]; | |
2606c57b | 191 | props[props_idx]->vals[0].length = SBYTES (Vinvocation_name); |
d5db4077 | 192 | props[props_idx]->vals[0].value = SDATA (Vinvocation_name); |
afb4ecad | 193 | ++props_idx; |
7d0393cf | 194 | |
4f2f546e | 195 | /* User id. */ |
afb4ecad | 196 | props[props_idx] = &prop_ptr[props_idx]; |
42ca4633 J |
197 | props[props_idx]->name = xstrdup (SmUserID); |
198 | props[props_idx]->type = xstrdup (SmARRAY8); | |
afb4ecad JD |
199 | props[props_idx]->num_vals = 1; |
200 | props[props_idx]->vals = &values[val_idx++]; | |
2606c57b | 201 | props[props_idx]->vals[0].length = SBYTES (Vuser_login_name); |
d5db4077 | 202 | props[props_idx]->vals[0].value = SDATA (Vuser_login_name); |
afb4ecad JD |
203 | ++props_idx; |
204 | ||
c187839d EZ |
205 | |
206 | if (cwd) | |
afb4ecad JD |
207 | { |
208 | props[props_idx] = &prop_ptr[props_idx]; | |
42ca4633 J |
209 | props[props_idx]->name = xstrdup (SmCurrentDirectory); |
210 | props[props_idx]->type = xstrdup (SmARRAY8); | |
afb4ecad JD |
211 | props[props_idx]->num_vals = 1; |
212 | props[props_idx]->vals = &values[val_idx++]; | |
213 | props[props_idx]->vals[0].length = strlen (cwd); | |
214 | props[props_idx]->vals[0].value = cwd; | |
215 | ++props_idx; | |
216 | } | |
7d0393cf JB |
217 | |
218 | ||
4df0af9b JD |
219 | /* How to restart Emacs. */ |
220 | props[props_idx] = &prop_ptr[props_idx]; | |
221 | props[props_idx]->name = xstrdup (SmRestartCommand); | |
222 | props[props_idx]->type = xstrdup (SmLISTofARRAY8); | |
223 | /* /path/to/emacs, --smid=xxx --no-splash --chdir=dir ... */ | |
0065d054 | 224 | if (INT_MAX - 3 < initial_argc) |
1d526e2f | 225 | memory_full (SIZE_MAX); |
4df0af9b JD |
226 | i = 3 + initial_argc; |
227 | props[props_idx]->num_vals = i; | |
0065d054 | 228 | vp = xnmalloc (i, sizeof *vp); |
4df0af9b JD |
229 | props[props_idx]->vals = vp; |
230 | props[props_idx]->vals[vp_idx].length = strlen (emacs_program); | |
231 | props[props_idx]->vals[vp_idx++].value = emacs_program; | |
232 | ||
233 | smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1); | |
234 | strcpy (smid_opt, SMID_OPT); | |
235 | strcat (smid_opt, client_id); | |
236 | ||
237 | props[props_idx]->vals[vp_idx].length = strlen (smid_opt); | |
238 | props[props_idx]->vals[vp_idx++].value = smid_opt; | |
239 | ||
240 | props[props_idx]->vals[vp_idx].length = strlen (NOSPLASH_OPT); | |
241 | props[props_idx]->vals[vp_idx++].value = NOSPLASH_OPT; | |
242 | ||
243 | if (cwd) | |
244 | { | |
245 | chdir_opt = xmalloc (strlen (CHDIR_OPT) + strlen (cwd) + 1); | |
246 | strcpy (chdir_opt, CHDIR_OPT); | |
247 | strcat (chdir_opt, cwd); | |
248 | ||
249 | props[props_idx]->vals[vp_idx].length = strlen (chdir_opt); | |
250 | props[props_idx]->vals[vp_idx++].value = chdir_opt; | |
251 | } | |
252 | ||
2baccd04 | 253 | for (i = 1; i < initial_argc; ++i) |
4df0af9b JD |
254 | { |
255 | props[props_idx]->vals[vp_idx].length = strlen (initial_argv[i]); | |
256 | props[props_idx]->vals[vp_idx++].value = initial_argv[i]; | |
257 | } | |
258 | ||
259 | ++props_idx; | |
260 | ||
afb4ecad JD |
261 | SmcSetProperties (smcConn, props_idx, props); |
262 | ||
263 | xfree (smid_opt); | |
f63d0028 | 264 | xfree (chdir_opt); |
4df0af9b JD |
265 | xfree (cwd); |
266 | xfree (vp); | |
afb4ecad | 267 | |
42ca4633 J |
268 | for (i = 0; i < props_idx; ++i) |
269 | { | |
270 | xfree (props[i]->type); | |
271 | xfree (props[i]->name); | |
272 | } | |
c187839d | 273 | |
4f2f546e | 274 | /* See if we maybe shall interact with the user. */ |
afb4ecad JD |
275 | if (interactStyle != SmInteractStyleAny |
276 | || ! shutdown | |
277 | || saveType == SmSaveLocal | |
278 | || ! SmcInteractRequest (smcConn, SmDialogNormal, smc_interact_CB, 0)) | |
279 | { | |
4f2f546e | 280 | /* No interaction, we are done saving ourself. */ |
afb4ecad JD |
281 | SmcSaveYourselfDone (smcConn, True); |
282 | } | |
283 | } | |
284 | ||
4f2f546e JD |
285 | /* According to the SM specification, this shall close the connection. */ |
286 | ||
afb4ecad | 287 | static void |
971de7fb | 288 | smc_die_CB (SmcConn smcConn, SmPointer clientData) |
afb4ecad | 289 | { |
0b9fc69a JD |
290 | emacs_event.kind = SAVE_SESSION_EVENT; |
291 | emacs_event.arg = Qt; | |
afb4ecad JD |
292 | } |
293 | ||
294 | /* We don't use the next two but they are mandatory, leave them empty. | |
295 | According to the SM specification, we should not interact with the | |
296 | user between smc_save_yourself_CB is called and until smc_save_complete_CB | |
297 | is called. It seems like a lot of job to implement this and it doesn't | |
4f2f546e JD |
298 | even seem necessary. */ |
299 | ||
afb4ecad | 300 | static void |
971de7fb | 301 | smc_save_complete_CB (SmcConn smcConn, SmPointer clientData) |
afb4ecad JD |
302 | { |
303 | /* Empty */ | |
304 | } | |
305 | ||
306 | static void | |
971de7fb | 307 | smc_shutdown_cancelled_CB (SmcConn smcConn, SmPointer clientData) |
afb4ecad JD |
308 | { |
309 | /* Empty */ | |
310 | } | |
311 | ||
f25dcaa0 | 312 | /* Error handlers for SM and ICE. We don't want to exit Emacs just |
4f2f546e JD |
313 | because there is some error in the session management. */ |
314 | ||
afb4ecad | 315 | static void |
dd4c5104 DN |
316 | smc_error_handler (SmcConn smcConn, |
317 | Bool swap, | |
318 | int offendingMinorOpcode, | |
319 | unsigned long offendingSequence, | |
320 | int errorClass, | |
321 | int severity, | |
322 | SmPointer values) | |
afb4ecad | 323 | { |
4f2f546e | 324 | /* Empty */ |
afb4ecad JD |
325 | } |
326 | ||
327 | static void | |
dd4c5104 DN |
328 | ice_error_handler (IceConn iceConn, |
329 | Bool swap, | |
330 | int offendingMinorOpcode, | |
331 | unsigned long offendingSequence, | |
332 | int errorClass, | |
333 | int severity, | |
334 | IcePointer values) | |
afb4ecad | 335 | { |
4f2f546e | 336 | /* Empty */ |
afb4ecad JD |
337 | } |
338 | ||
339 | ||
340 | static void | |
971de7fb | 341 | ice_io_error_handler (IceConn iceConn) |
afb4ecad | 342 | { |
4f2f546e | 343 | /* Connection probably gone. */ |
182659ae | 344 | ice_connection_closed (); |
afb4ecad JD |
345 | } |
346 | ||
347 | /* This is called when the ICE connection is created or closed. The SM library | |
4f2f546e JD |
348 | uses ICE as it transport protocol. */ |
349 | ||
afb4ecad | 350 | static void |
4df0af9b JD |
351 | ice_conn_watch_CB (IceConn iceConn, IcePointer clientData, |
352 | int opening, IcePointer *watchData) | |
afb4ecad JD |
353 | { |
354 | if (! opening) | |
355 | { | |
182659ae | 356 | ice_connection_closed (); |
afb4ecad JD |
357 | return; |
358 | } | |
7d0393cf | 359 | |
afb4ecad | 360 | ice_fd = IceConnectionNumber (iceConn); |
4df0af9b | 361 | add_read_fd (ice_fd, x_session_check_input, NULL); |
afb4ecad JD |
362 | } |
363 | ||
231d6cfb | 364 | /* Create the client leader window. */ |
4f2f546e | 365 | |
e4c8d29a | 366 | #ifndef USE_GTK |
231d6cfb | 367 | static void |
2baccd04 | 368 | create_client_leader_window (struct x_display_info *dpyinfo, char *client_ID) |
231d6cfb JD |
369 | { |
370 | Window w; | |
371 | XClassHint class_hints; | |
231d6cfb JD |
372 | |
373 | w = XCreateSimpleWindow (dpyinfo->display, | |
374 | dpyinfo->root_window, | |
375 | -1, -1, 1, 1, | |
376 | CopyFromParent, CopyFromParent, CopyFromParent); | |
377 | ||
51b59d79 PE |
378 | class_hints.res_name = SSDATA (Vx_resource_name); |
379 | class_hints.res_class = SSDATA (Vx_resource_class); | |
231d6cfb JD |
380 | XSetClassHint (dpyinfo->display, w, &class_hints); |
381 | XStoreName (dpyinfo->display, w, class_hints.res_name); | |
382 | ||
2d9074ba JD |
383 | XChangeProperty (dpyinfo->display, w, dpyinfo->Xatom_SM_CLIENT_ID, |
384 | XA_STRING, 8, PropModeReplace, | |
2baccd04 | 385 | (unsigned char *) client_ID, strlen (client_ID)); |
231d6cfb JD |
386 | |
387 | dpyinfo->client_leader_window = w; | |
388 | } | |
e4c8d29a J |
389 | #endif /* ! USE_GTK */ |
390 | ||
231d6cfb | 391 | |
4f2f546e JD |
392 | /* Try to open a connection to the session manager. */ |
393 | ||
afb4ecad | 394 | void |
971de7fb | 395 | x_session_initialize (struct x_display_info *dpyinfo) |
afb4ecad JD |
396 | { |
397 | #define SM_ERRORSTRING_LEN 512 | |
398 | char errorstring[SM_ERRORSTRING_LEN]; | |
399 | char* previous_id = NULL; | |
400 | SmcCallbacks callbacks; | |
2606c57b | 401 | ptrdiff_t name_len = 0; |
7d0393cf | 402 | |
182659ae JD |
403 | ice_fd = -1; |
404 | doing_interact = False; | |
405 | ||
afb4ecad | 406 | /* Check if we where started by the session manager. If so, we will |
4f2f546e | 407 | have a previous id. */ |
afb4ecad | 408 | if (! EQ (Vx_session_previous_id, Qnil) && STRINGP (Vx_session_previous_id)) |
e4c8d29a | 409 | previous_id = SSDATA (Vx_session_previous_id); |
afb4ecad | 410 | |
4f2f546e | 411 | /* Construct the path to the Emacs program. */ |
afb4ecad | 412 | if (! EQ (Vinvocation_directory, Qnil)) |
2606c57b PE |
413 | name_len += SBYTES (Vinvocation_directory); |
414 | name_len += SBYTES (Vinvocation_name); | |
afb4ecad JD |
415 | |
416 | /* This malloc will not be freed, but it is only done once, and hopefully | |
4f2f546e | 417 | not very large */ |
afb4ecad JD |
418 | emacs_program = xmalloc (name_len + 1); |
419 | emacs_program[0] = '\0'; | |
420 | ||
421 | if (! EQ (Vinvocation_directory, Qnil)) | |
e4c8d29a J |
422 | strcpy (emacs_program, SSDATA (Vinvocation_directory)); |
423 | strcat (emacs_program, SSDATA (Vinvocation_name)); | |
7d0393cf | 424 | |
afb4ecad | 425 | /* The SM protocol says all callbacks are mandatory, so set up all |
4f2f546e | 426 | here and in the mask passed to SmcOpenConnection. */ |
afb4ecad JD |
427 | callbacks.save_yourself.callback = smc_save_yourself_CB; |
428 | callbacks.save_yourself.client_data = 0; | |
429 | callbacks.die.callback = smc_die_CB; | |
430 | callbacks.die.client_data = 0; | |
431 | callbacks.save_complete.callback = smc_save_complete_CB; | |
432 | callbacks.save_complete.client_data = 0; | |
433 | callbacks.shutdown_cancelled.callback = smc_shutdown_cancelled_CB; | |
434 | callbacks.shutdown_cancelled.client_data = 0; | |
435 | ||
4f2f546e | 436 | /* Set error handlers. */ |
afb4ecad JD |
437 | SmcSetErrorHandler (smc_error_handler); |
438 | IceSetErrorHandler (ice_error_handler); | |
439 | IceSetIOErrorHandler (ice_io_error_handler); | |
440 | ||
4f2f546e | 441 | /* Install callback for when connection status changes. */ |
afb4ecad JD |
442 | IceAddConnectionWatch (ice_conn_watch_CB, 0); |
443 | ||
444 | /* Open the connection to the session manager. A failure is not | |
f25dcaa0 | 445 | critical, it usually means that no session manager is running. |
4f2f546e | 446 | The errorstring is here for debugging. */ |
afb4ecad JD |
447 | smc_conn = SmcOpenConnection (NULL, NULL, 1, 0, |
448 | (SmcSaveYourselfProcMask| | |
449 | SmcDieProcMask| | |
450 | SmcSaveCompleteProcMask| | |
451 | SmcShutdownCancelledProcMask), | |
452 | &callbacks, | |
453 | previous_id, | |
454 | &client_id, | |
455 | SM_ERRORSTRING_LEN, | |
456 | errorstring); | |
457 | ||
458 | if (smc_conn != 0) | |
231d6cfb | 459 | { |
2606c57b | 460 | Vx_session_id = build_string (client_id); |
231d6cfb JD |
461 | |
462 | #ifdef USE_GTK | |
da6062e6 | 463 | /* GTK creates a leader window by itself, but we need to tell |
231d6cfb | 464 | it about our client_id. */ |
0afb4571 | 465 | gdk_x11_set_sm_client_id (client_id); |
231d6cfb JD |
466 | #else |
467 | create_client_leader_window (dpyinfo, client_id); | |
468 | #endif | |
469 | } | |
afb4ecad JD |
470 | } |
471 | ||
d51abf22 KL |
472 | /* Ensure that the session manager is not contacted again. */ |
473 | ||
474 | void | |
971de7fb | 475 | x_session_close (void) |
d51abf22 | 476 | { |
182659ae | 477 | ice_connection_closed (); |
d51abf22 KL |
478 | } |
479 | ||
afb4ecad JD |
480 | |
481 | DEFUN ("handle-save-session", Fhandle_save_session, | |
482 | Shandle_save_session, 1, 1, "e", | |
483 | doc: /* Handle the save_yourself event from a session manager. | |
7d0393cf | 484 | A session manager can tell Emacs that the window system is shutting down |
afb4ecad JD |
485 | by sending Emacs a save_yourself message. Emacs executes this function when |
486 | such an event occurs. This function then executes `emacs-session-save'. | |
487 | After that, this function informs the session manager that it can continue | |
488 | or abort shutting down the window system depending on the return value | |
489 | from `emacs-session-save' If the return value is non-nil the session manager | |
490 | is told to abort the window system shutdown. | |
491 | ||
492 | Do not call this function yourself. */) | |
5842a27b | 493 | (Lisp_Object event) |
afb4ecad | 494 | { |
0b9fc69a JD |
495 | int kill_emacs = CONSP (event) && CONSP (XCDR (event)) |
496 | && EQ (Qt, XCAR (XCDR (event))); | |
497 | ||
afb4ecad JD |
498 | /* Check doing_interact so that we don't do anything if someone called |
499 | this at the wrong time. */ | |
0b9fc69a | 500 | if (doing_interact && ! kill_emacs) |
afb4ecad JD |
501 | { |
502 | Bool cancel_shutdown = False; | |
503 | ||
504 | cancel_shutdown = ! EQ (call0 (intern ("emacs-session-save")), Qnil); | |
505 | ||
506 | SmcInteractDone (smc_conn, cancel_shutdown); | |
507 | SmcSaveYourselfDone (smc_conn, True); | |
508 | ||
509 | doing_interact = False; | |
510 | } | |
0b9fc69a JD |
511 | else if (kill_emacs) |
512 | { | |
513 | /* We should not do user interaction here, but it is not easy to | |
514 | prevent. Fix this in next version. */ | |
515 | Fkill_emacs (Qnil); | |
ad4ace7a | 516 | |
d9df6f40 | 517 | #if 0 |
0b9fc69a JD |
518 | /* This will not be reached, but we want kill-emacs-hook to be run. */ |
519 | SmcCloseConnection (smc_conn, 0, 0); | |
520 | ice_connection_closed (); | |
d9df6f40 | 521 | #endif |
0b9fc69a | 522 | } |
51b59d79 | 523 | |
ad4ace7a | 524 | return Qnil; |
afb4ecad | 525 | } |
51b59d79 | 526 | |
afb4ecad JD |
527 | |
528 | \f | |
529 | /*********************************************************************** | |
530 | Initialization | |
531 | ***********************************************************************/ | |
532 | void | |
971de7fb | 533 | syms_of_xsmfns (void) |
afb4ecad | 534 | { |
29208e82 | 535 | DEFVAR_LISP ("x-session-id", Vx_session_id, |
afb4ecad JD |
536 | doc: /* The session id Emacs got from the session manager for this session. |
537 | Changing the value does not change the session id used by Emacs. | |
538 | The value is nil if no session manager is running. | |
539 | See also `x-session-previous-id', `emacs-save-session-functions', | |
540 | `emacs-session-save' and `emacs-session-restore'." */); | |
541 | Vx_session_id = Qnil; | |
542 | ||
29208e82 | 543 | DEFVAR_LISP ("x-session-previous-id", Vx_session_previous_id, |
afb4ecad | 544 | doc: /* The previous session id Emacs got from session manager. |
7d0393cf | 545 | If Emacs is running on a window system that has a session manager, the |
fbcdaefb | 546 | session manager gives Emacs a session id. It is feasible for Emacs Lisp |
7d0393cf JB |
547 | code to use the session id to save configuration in, for example, a file |
548 | with a file name based on the session id. If Emacs is running when the | |
549 | window system is shut down, the session manager remembers that Emacs was | |
afb4ecad JD |
550 | running and saves the session id Emacs had. |
551 | ||
7d0393cf JB |
552 | When the window system is started again, the session manager restarts |
553 | Emacs and hands Emacs the session id it had the last time it was | |
554 | running. This is now the previous session id and the value of this | |
555 | variable. If configuration was saved in a file as stated above, the | |
afb4ecad JD |
556 | previous session id shall be used to reconstruct the file name. |
557 | ||
7d0393cf | 558 | The session id Emacs has while it is running is in the variable |
afb4ecad JD |
559 | `x-session-id'. The value of this variable and `x-session-id' may be the |
560 | same, depending on how the session manager works. | |
561 | ||
562 | See also `emacs-save-session-functions', `emacs-session-save' and | |
563 | `emacs-session-restore'." */); | |
564 | Vx_session_previous_id = Qnil; | |
7d0393cf | 565 | |
afb4ecad JD |
566 | defsubr (&Shandle_save_session); |
567 | } | |
568 | ||
569 | #endif /* HAVE_X_SM */ |