admin/notes/bugtracker (bugtracker_debbugs_url): Fix typo.
[emacs.git] / src / xsmfns.c
blob5c09c9dfb739952559d27396fb312d19cdcbb265
1 /* Session management module for systems which understand the X Session
2 management protocol.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 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/>. */
21 #include <config.h>
23 #ifdef HAVE_X_SM
25 #include <X11/SM/SMlib.h>
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
29 #ifdef HAVE_UNISTD_H
30 #include <unistd.h>
31 #endif
33 #include <sys/param.h>
34 #include <stdio.h>
35 #include <setjmp.h>
37 #include "lisp.h"
38 #include "systime.h"
39 #include "sysselect.h"
40 #include "frame.h"
41 #include "termhooks.h"
42 #include "termopts.h"
43 #include "xterm.h"
45 /* The user login name. */
47 extern Lisp_Object Vuser_login_name;
49 /* This is the event used when SAVE_SESSION_EVENT occurs. */
51 static struct input_event emacs_event;
53 /* The descriptor that we use to check for data from the session manager. */
55 static int ice_fd;
57 /* A flag that says if we are in shutdown interactions or not. */
59 static int doing_interact;
61 /* The session manager object for the session manager connection. */
63 static SmcConn smc_conn;
65 /* The client session id for this session. */
67 static char *client_id;
69 /* The full path name to the Emacs program. */
71 static char *emacs_program;
73 /* The client session id for this session as a lisp object. */
75 Lisp_Object Vx_session_id;
77 /* The id we had the previous session. This is only available if we
78 have been started by the session manager with SMID_OPT. */
80 Lisp_Object Vx_session_previous_id;
82 /* The option we tell the session manager to start Emacs with when
83 restarting Emacs. The client_id is appended. */
85 #define SMID_OPT "--smid="
88 /* The option to start Emacs without the splash screen when
89 restarting Emacs. */
91 #define NOSPLASH_OPT "--no-splash"
93 static void
94 ice_connection_closed ()
96 if (ice_fd >= 0)
97 delete_keyboard_wait_descriptor (ice_fd);
98 ice_fd = -1;
102 /* Handle any messages from the session manager. If no connection is
103 open to a session manager, just return 0.
104 Otherwise returns 1 if SAVE_SESSION_EVENT is stored in buffer BUFP. */
107 x_session_check_input (bufp)
108 struct input_event *bufp;
110 SELECT_TYPE read_fds;
111 EMACS_TIME tmout;
112 int ret;
114 if (ice_fd == -1) return 0;
115 FD_ZERO (&read_fds);
116 FD_SET (ice_fd, &read_fds);
118 tmout.tv_sec = 0;
119 tmout.tv_usec = 0;
121 /* Reset this so wo can check kind after callbacks have been called by
122 IceProcessMessages. The smc_interact_CB sets the kind to
123 SAVE_SESSION_EVENT, but we don't know beforehand if that callback
124 will be called. */
125 emacs_event.kind = NO_EVENT;
127 ret = select (ice_fd+1, &read_fds,
128 (SELECT_TYPE *)0, (SELECT_TYPE *)0, &tmout);
130 if (ret < 0)
132 ice_connection_closed ();
134 else if (ret > 0 && FD_ISSET (ice_fd, &read_fds))
136 ret = IceProcessMessages (SmcGetIceConnection (smc_conn),
137 (IceReplyWaitInfo *)0, (Bool *)0);
138 if (ret != IceProcessMessagesSuccess)
140 /* Either IO error or Connection closed. */
141 if (ret == IceProcessMessagesIOError)
142 IceCloseConnection (SmcGetIceConnection (smc_conn));
144 ice_connection_closed ();
148 /* Check if smc_interact_CB was called and we shall generate a
149 SAVE_SESSION_EVENT. */
150 if (emacs_event.kind != NO_EVENT)
151 bcopy (&emacs_event, bufp, sizeof (struct input_event));
153 return emacs_event.kind != NO_EVENT ? 1 : 0;
156 /* Return non-zero if we have a connection to a session manager. */
159 x_session_have_connection ()
161 return ice_fd != -1;
164 /* This is called when the session manager says it is OK to interact with the
165 user. Here we set the kind to SAVE_SESSION_EVENT so an event is generated.
166 Then lisp code can interact with the user. */
168 static void
169 smc_interact_CB (smcConn, clientData)
170 SmcConn smcConn;
171 SmPointer clientData;
173 doing_interact = True;
174 emacs_event.kind = SAVE_SESSION_EVENT;
175 emacs_event.arg = Qnil;
178 /* This is called when the session manager tells us to save ourselves.
179 We set the required properties so the session manager can restart us,
180 plus the current working directory property (not mandatory) so we
181 are started in the correct directory.
183 If this is a shutdown and we can request to interact with the user,
184 we do so, because we don't know what the lisp code might do. */
186 static void
187 smc_save_yourself_CB (smcConn,
188 clientData,
189 saveType,
190 shutdown,
191 interactStyle,
192 fast)
193 SmcConn smcConn;
194 SmPointer clientData;
195 int saveType;
196 Bool shutdown;
197 int interactStyle;
198 Bool fast;
200 #define NR_PROPS 5
202 SmProp *props[NR_PROPS];
203 SmProp prop_ptr[NR_PROPS];
205 SmPropValue values[20];
206 int val_idx = 0;
207 int props_idx = 0;
209 char *cwd = NULL;
210 char *smid_opt;
212 /* How to start a new instance of Emacs. */
213 props[props_idx] = &prop_ptr[props_idx];
214 props[props_idx]->name = SmCloneCommand;
215 props[props_idx]->type = SmLISTofARRAY8;
216 props[props_idx]->num_vals = 1;
217 props[props_idx]->vals = &values[val_idx++];
218 props[props_idx]->vals[0].length = strlen (emacs_program);
219 props[props_idx]->vals[0].value = emacs_program;
220 ++props_idx;
222 /* The name of the program. */
223 props[props_idx] = &prop_ptr[props_idx];
224 props[props_idx]->name = SmProgram;
225 props[props_idx]->type = SmARRAY8;
226 props[props_idx]->num_vals = 1;
227 props[props_idx]->vals = &values[val_idx++];
228 props[props_idx]->vals[0].length = strlen (SDATA (Vinvocation_name));
229 props[props_idx]->vals[0].value = SDATA (Vinvocation_name);
230 ++props_idx;
232 /* How to restart Emacs (i.e.: /path/to/emacs --smid=xxxx --no-splash). */
233 props[props_idx] = &prop_ptr[props_idx];
234 props[props_idx]->name = SmRestartCommand;
235 props[props_idx]->type = SmLISTofARRAY8;
236 props[props_idx]->num_vals = 3; /* /path/to/emacs, --smid=xxx --no-splash */
237 props[props_idx]->vals = &values[val_idx];
238 props[props_idx]->vals[0].length = strlen (emacs_program);
239 props[props_idx]->vals[0].value = emacs_program;
241 smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1);
242 strcpy (smid_opt, SMID_OPT);
243 strcat (smid_opt, client_id);
245 props[props_idx]->vals[1].length = strlen (smid_opt);
246 props[props_idx]->vals[1].value = smid_opt;
248 props[props_idx]->vals[2].length = strlen (NOSPLASH_OPT);
249 props[props_idx]->vals[2].value = NOSPLASH_OPT;
250 val_idx += 3;
251 ++props_idx;
253 /* User id. */
254 props[props_idx] = &prop_ptr[props_idx];
255 props[props_idx]->name = SmUserID;
256 props[props_idx]->type = SmARRAY8;
257 props[props_idx]->num_vals = 1;
258 props[props_idx]->vals = &values[val_idx++];
259 props[props_idx]->vals[0].length = strlen (SDATA (Vuser_login_name));
260 props[props_idx]->vals[0].value = SDATA (Vuser_login_name);
261 ++props_idx;
263 cwd = get_current_dir_name ();
265 if (cwd)
267 props[props_idx] = &prop_ptr[props_idx];
268 props[props_idx]->name = SmCurrentDirectory;
269 props[props_idx]->type = SmARRAY8;
270 props[props_idx]->num_vals = 1;
271 props[props_idx]->vals = &values[val_idx++];
272 props[props_idx]->vals[0].length = strlen (cwd);
273 props[props_idx]->vals[0].value = cwd;
274 ++props_idx;
278 SmcSetProperties (smcConn, props_idx, props);
280 xfree (smid_opt);
282 free (cwd);
284 /* See if we maybe shall interact with the user. */
285 if (interactStyle != SmInteractStyleAny
286 || ! shutdown
287 || saveType == SmSaveLocal
288 || ! SmcInteractRequest (smcConn, SmDialogNormal, smc_interact_CB, 0))
290 /* No interaction, we are done saving ourself. */
291 SmcSaveYourselfDone (smcConn, True);
295 /* According to the SM specification, this shall close the connection. */
297 static void
298 smc_die_CB (smcConn, clientData)
299 SmcConn smcConn;
300 SmPointer clientData;
302 emacs_event.kind = SAVE_SESSION_EVENT;
303 emacs_event.arg = Qt;
306 /* We don't use the next two but they are mandatory, leave them empty.
307 According to the SM specification, we should not interact with the
308 user between smc_save_yourself_CB is called and until smc_save_complete_CB
309 is called. It seems like a lot of job to implement this and it doesn't
310 even seem necessary. */
312 static void
313 smc_save_complete_CB (smcConn, clientData)
314 SmcConn smcConn;
315 SmPointer clientData;
317 /* Empty */
320 static void
321 smc_shutdown_cancelled_CB (smcConn, clientData)
322 SmcConn smcConn;
323 SmPointer clientData;
325 /* Empty */
328 /* Error handlers for SM and ICE. We don't want to exit Emacs just
329 because there is some error in the session management. */
331 static void
332 smc_error_handler (smcConn,
333 swap,
334 offendingMinorOpcode,
335 offendingSequence,
336 errorClass,
337 severity,
338 values)
339 SmcConn smcConn;
340 Bool swap;
341 int offendingMinorOpcode;
342 unsigned long offendingSequence;
343 int errorClass;
344 int severity;
345 SmPointer values;
347 /* Empty */
350 static void
351 ice_error_handler (iceConn,
352 swap,
353 offendingMinorOpcode,
354 offendingSequence,
355 errorClass,
356 severity,
357 values)
358 IceConn iceConn;
359 Bool swap;
360 int offendingMinorOpcode;
361 unsigned long offendingSequence;
362 int errorClass;
363 int severity;
364 IcePointer values;
366 /* Empty */
370 static void
371 ice_io_error_handler (iceConn)
372 IceConn iceConn;
374 /* Connection probably gone. */
375 ice_connection_closed ();
378 /* This is called when the ICE connection is created or closed. The SM library
379 uses ICE as it transport protocol. */
381 static void
382 ice_conn_watch_CB (iceConn, clientData, opening, watchData)
383 IceConn iceConn;
384 IcePointer clientData;
385 Bool opening;
386 IcePointer *watchData;
388 if (! opening)
390 ice_connection_closed ();
391 return;
394 ice_fd = IceConnectionNumber (iceConn);
395 #ifdef F_SETOWN
396 fcntl (ice_fd, F_SETOWN, getpid ());
397 #endif /* ! defined (F_SETOWN) */
399 #ifdef SIGIO
400 if (interrupt_input)
401 init_sigio (ice_fd);
402 #endif /* ! defined (SIGIO) */
404 add_keyboard_wait_descriptor (ice_fd);
407 /* Create the client leader window. */
409 static void
410 create_client_leader_window (dpyinfo, client_id)
411 struct x_display_info *dpyinfo;
412 char *client_id;
414 Window w;
415 XClassHint class_hints;
416 Atom sm_id;
418 w = XCreateSimpleWindow (dpyinfo->display,
419 dpyinfo->root_window,
420 -1, -1, 1, 1,
421 CopyFromParent, CopyFromParent, CopyFromParent);
423 class_hints.res_name = (char *) SDATA (Vx_resource_name);
424 class_hints.res_class = (char *) SDATA (Vx_resource_class);
425 XSetClassHint (dpyinfo->display, w, &class_hints);
426 XStoreName (dpyinfo->display, w, class_hints.res_name);
428 sm_id = XInternAtom (dpyinfo->display, "SM_CLIENT_ID", False);
429 XChangeProperty (dpyinfo->display, w, sm_id, XA_STRING, 8, PropModeReplace,
430 client_id, strlen (client_id));
432 dpyinfo->client_leader_window = w;
435 /* Try to open a connection to the session manager. */
437 void
438 x_session_initialize (dpyinfo)
439 struct x_display_info *dpyinfo;
441 #define SM_ERRORSTRING_LEN 512
442 char errorstring[SM_ERRORSTRING_LEN];
443 char* previous_id = NULL;
444 SmcCallbacks callbacks;
445 int name_len = 0;
447 ice_fd = -1;
448 doing_interact = False;
450 /* Check if we where started by the session manager. If so, we will
451 have a previous id. */
452 if (! EQ (Vx_session_previous_id, Qnil) && STRINGP (Vx_session_previous_id))
453 previous_id = SDATA (Vx_session_previous_id);
455 /* Construct the path to the Emacs program. */
456 if (! EQ (Vinvocation_directory, Qnil))
457 name_len += strlen (SDATA (Vinvocation_directory));
458 name_len += strlen (SDATA (Vinvocation_name));
460 /* This malloc will not be freed, but it is only done once, and hopefully
461 not very large */
462 emacs_program = xmalloc (name_len + 1);
463 emacs_program[0] = '\0';
465 if (! EQ (Vinvocation_directory, Qnil))
466 strcpy (emacs_program, SDATA (Vinvocation_directory));
467 strcat (emacs_program, SDATA (Vinvocation_name));
469 /* The SM protocol says all callbacks are mandatory, so set up all
470 here and in the mask passed to SmcOpenConnection. */
471 callbacks.save_yourself.callback = smc_save_yourself_CB;
472 callbacks.save_yourself.client_data = 0;
473 callbacks.die.callback = smc_die_CB;
474 callbacks.die.client_data = 0;
475 callbacks.save_complete.callback = smc_save_complete_CB;
476 callbacks.save_complete.client_data = 0;
477 callbacks.shutdown_cancelled.callback = smc_shutdown_cancelled_CB;
478 callbacks.shutdown_cancelled.client_data = 0;
480 /* Set error handlers. */
481 SmcSetErrorHandler (smc_error_handler);
482 IceSetErrorHandler (ice_error_handler);
483 IceSetIOErrorHandler (ice_io_error_handler);
485 /* Install callback for when connection status changes. */
486 IceAddConnectionWatch (ice_conn_watch_CB, 0);
488 /* Open the connection to the session manager. A failure is not
489 critical, it usually means that no session manager is running.
490 The errorstring is here for debugging. */
491 smc_conn = SmcOpenConnection (NULL, NULL, 1, 0,
492 (SmcSaveYourselfProcMask|
493 SmcDieProcMask|
494 SmcSaveCompleteProcMask|
495 SmcShutdownCancelledProcMask),
496 &callbacks,
497 previous_id,
498 &client_id,
499 SM_ERRORSTRING_LEN,
500 errorstring);
502 if (smc_conn != 0)
504 Vx_session_id = make_string (client_id, strlen (client_id));
506 #ifdef USE_GTK
507 /* GTK creats a leader window by itself, but we need to tell
508 it about our client_id. */
509 gdk_set_sm_client_id (client_id);
510 #else
511 create_client_leader_window (dpyinfo, client_id);
512 #endif
516 /* Ensure that the session manager is not contacted again. */
518 void
519 x_session_close ()
521 ice_connection_closed ();
525 DEFUN ("handle-save-session", Fhandle_save_session,
526 Shandle_save_session, 1, 1, "e",
527 doc: /* Handle the save_yourself event from a session manager.
528 A session manager can tell Emacs that the window system is shutting down
529 by sending Emacs a save_yourself message. Emacs executes this function when
530 such an event occurs. This function then executes `emacs-session-save'.
531 After that, this function informs the session manager that it can continue
532 or abort shutting down the window system depending on the return value
533 from `emacs-session-save' If the return value is non-nil the session manager
534 is told to abort the window system shutdown.
536 Do not call this function yourself. */)
537 (event)
538 Lisp_Object event;
540 int kill_emacs = CONSP (event) && CONSP (XCDR (event))
541 && EQ (Qt, XCAR (XCDR (event)));
543 /* Check doing_interact so that we don't do anything if someone called
544 this at the wrong time. */
545 if (doing_interact && ! kill_emacs)
547 Bool cancel_shutdown = False;
549 cancel_shutdown = ! EQ (call0 (intern ("emacs-session-save")), Qnil);
551 SmcInteractDone (smc_conn, cancel_shutdown);
552 SmcSaveYourselfDone (smc_conn, True);
554 doing_interact = False;
556 else if (kill_emacs)
558 /* We should not do user interaction here, but it is not easy to
559 prevent. Fix this in next version. */
560 Fkill_emacs (Qnil);
562 /* This will not be reached, but we want kill-emacs-hook to be run. */
563 SmcCloseConnection (smc_conn, 0, 0);
564 ice_connection_closed ();
567 return Qnil;
572 /***********************************************************************
573 Initialization
574 ***********************************************************************/
575 void
576 syms_of_xsmfns ()
578 DEFVAR_LISP ("x-session-id", &Vx_session_id,
579 doc: /* The session id Emacs got from the session manager for this session.
580 Changing the value does not change the session id used by Emacs.
581 The value is nil if no session manager is running.
582 See also `x-session-previous-id', `emacs-save-session-functions',
583 `emacs-session-save' and `emacs-session-restore'." */);
584 Vx_session_id = Qnil;
586 DEFVAR_LISP ("x-session-previous-id", &Vx_session_previous_id,
587 doc: /* The previous session id Emacs got from session manager.
588 If Emacs is running on a window system that has a session manager, the
589 session manager gives Emacs a session id. It is feasible for Emacs Lisp
590 code to use the session id to save configuration in, for example, a file
591 with a file name based on the session id. If Emacs is running when the
592 window system is shut down, the session manager remembers that Emacs was
593 running and saves the session id Emacs had.
595 When the window system is started again, the session manager restarts
596 Emacs and hands Emacs the session id it had the last time it was
597 running. This is now the previous session id and the value of this
598 variable. If configuration was saved in a file as stated above, the
599 previous session id shall be used to reconstruct the file name.
601 The session id Emacs has while it is running is in the variable
602 `x-session-id'. The value of this variable and `x-session-id' may be the
603 same, depending on how the session manager works.
605 See also `emacs-save-session-functions', `emacs-session-save' and
606 `emacs-session-restore'." */);
607 Vx_session_previous_id = Qnil;
609 defsubr (&Shandle_save_session);
612 #endif /* HAVE_X_SM */
614 /* arch-tag: 56a2c58c-adfa-430a-b772-130abd29fd2e
615 (do not change this comment) */