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