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