* net/tramp-sh.el (tramp-remote-process-environment): Add "PAGER=\"\"".
[emacs.git] / src / xsmfns.c
blob1f6eb84260e7dc9e648568312ea5f1a2819a26c7
1 /* Session management module for systems which understand the X Session
2 management protocol.
4 Copyright (C) 2002-2012 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 #include <unistd.h>
30 #include <sys/param.h>
31 #include <stdio.h>
32 #include <setjmp.h>
34 #include "lisp.h"
35 #include "systime.h"
36 #include "sysselect.h"
37 #include "frame.h"
38 #include "termhooks.h"
39 #include "termopts.h"
40 #include "xterm.h"
41 #include "process.h"
42 #include "keyboard.h"
44 #if defined USE_GTK && !defined HAVE_GTK3
45 #define gdk_x11_set_sm_client_id(w) gdk_set_sm_client_id (w)
46 #endif
48 /* This is the event used when SAVE_SESSION_EVENT occurs. */
50 static struct input_event emacs_event;
52 /* The descriptor that we use to check for data from the session manager. */
54 static int ice_fd;
56 /* A flag that says if we are in shutdown interactions or not. */
58 static int doing_interact;
60 /* The session manager object for the session manager connection. */
62 static SmcConn smc_conn;
64 /* The client session id for this session. */
66 static char *client_id;
68 /* The full path name to the Emacs program. */
70 static char *emacs_program;
72 /* The option we tell the session manager to start Emacs with when
73 restarting Emacs. The client_id is appended. */
75 #define SMID_OPT "--smid="
78 /* The option to start Emacs without the splash screen when
79 restarting Emacs. */
81 static char NOSPLASH_OPT[] = "--no-splash";
83 /* The option to make Emacs start in the given directory. */
85 #define CHDIR_OPT "--chdir="
87 static void
88 ice_connection_closed (void)
90 if (ice_fd >= 0)
91 delete_read_fd (ice_fd);
92 ice_fd = -1;
96 /* Handle any messages from the session manager. If no connection is
97 open to a session manager, just return. */
99 static void
100 x_session_check_input (int fd, void *data, int for_read)
102 int ret;
104 if (ice_fd == -1) return;
106 /* Reset this so wo can check kind after callbacks have been called by
107 IceProcessMessages. The smc_interact_CB sets the kind to
108 SAVE_SESSION_EVENT, but we don't know beforehand if that callback
109 will be called. */
110 emacs_event.kind = NO_EVENT;
112 ret = IceProcessMessages (SmcGetIceConnection (smc_conn),
113 (IceReplyWaitInfo *)0, (Bool *)0);
114 if (ret != IceProcessMessagesSuccess)
116 /* Either IO error or Connection closed. */
117 if (ret == IceProcessMessagesIOError)
118 IceCloseConnection (SmcGetIceConnection (smc_conn));
120 ice_connection_closed ();
123 /* Check if smc_interact_CB was called and we shall generate a
124 SAVE_SESSION_EVENT. */
125 if (emacs_event.kind != NO_EVENT)
126 kbd_buffer_store_event (&emacs_event);
129 /* Return non-zero if we have a connection to a session manager. */
132 x_session_have_connection (void)
134 return ice_fd != -1;
137 /* This is called when the session manager says it is OK to interact with the
138 user. Here we set the kind to SAVE_SESSION_EVENT so an event is generated.
139 Then lisp code can interact with the user. */
141 static void
142 smc_interact_CB (SmcConn smcConn, SmPointer clientData)
144 doing_interact = True;
145 emacs_event.kind = SAVE_SESSION_EVENT;
146 emacs_event.arg = Qnil;
149 /* This is called when the session manager tells us to save ourselves.
150 We set the required properties so the session manager can restart us,
151 plus the current working directory property (not mandatory) so we
152 are started in the correct directory.
154 If this is a shutdown and we can request to interact with the user,
155 we do so, because we don't know what the lisp code might do. */
157 static void
158 smc_save_yourself_CB (SmcConn smcConn,
159 SmPointer clientData,
160 int saveType,
161 Bool shutdown,
162 int interactStyle,
163 Bool fast)
165 #define NR_PROPS 5
167 SmProp *props[NR_PROPS];
168 SmProp prop_ptr[NR_PROPS];
170 SmPropValue values[20], *vp;
171 int val_idx = 0, vp_idx = 0;
172 int props_idx = 0;
173 int i;
174 char *cwd = get_current_dir_name ();
175 char *smid_opt, *chdir_opt = NULL;
177 /* How to start a new instance of Emacs. */
178 props[props_idx] = &prop_ptr[props_idx];
179 props[props_idx]->name = xstrdup (SmCloneCommand);
180 props[props_idx]->type = xstrdup (SmLISTofARRAY8);
181 props[props_idx]->num_vals = 1;
182 props[props_idx]->vals = &values[val_idx++];
183 props[props_idx]->vals[0].length = strlen (emacs_program);
184 props[props_idx]->vals[0].value = emacs_program;
185 ++props_idx;
187 /* The name of the program. */
188 props[props_idx] = &prop_ptr[props_idx];
189 props[props_idx]->name = xstrdup (SmProgram);
190 props[props_idx]->type = xstrdup (SmARRAY8);
191 props[props_idx]->num_vals = 1;
192 props[props_idx]->vals = &values[val_idx++];
193 props[props_idx]->vals[0].length = SBYTES (Vinvocation_name);
194 props[props_idx]->vals[0].value = SDATA (Vinvocation_name);
195 ++props_idx;
197 /* User id. */
198 props[props_idx] = &prop_ptr[props_idx];
199 props[props_idx]->name = xstrdup (SmUserID);
200 props[props_idx]->type = xstrdup (SmARRAY8);
201 props[props_idx]->num_vals = 1;
202 props[props_idx]->vals = &values[val_idx++];
203 props[props_idx]->vals[0].length = SBYTES (Vuser_login_name);
204 props[props_idx]->vals[0].value = SDATA (Vuser_login_name);
205 ++props_idx;
208 if (cwd)
210 props[props_idx] = &prop_ptr[props_idx];
211 props[props_idx]->name = xstrdup (SmCurrentDirectory);
212 props[props_idx]->type = xstrdup (SmARRAY8);
213 props[props_idx]->num_vals = 1;
214 props[props_idx]->vals = &values[val_idx++];
215 props[props_idx]->vals[0].length = strlen (cwd);
216 props[props_idx]->vals[0].value = cwd;
217 ++props_idx;
221 /* How to restart Emacs. */
222 props[props_idx] = &prop_ptr[props_idx];
223 props[props_idx]->name = xstrdup (SmRestartCommand);
224 props[props_idx]->type = xstrdup (SmLISTofARRAY8);
225 /* /path/to/emacs, --smid=xxx --no-splash --chdir=dir ... */
226 if (INT_MAX - 3 < initial_argc)
227 memory_full (SIZE_MAX);
228 i = 3 + initial_argc;
229 props[props_idx]->num_vals = i;
230 vp = xnmalloc (i, sizeof *vp);
231 props[props_idx]->vals = vp;
232 props[props_idx]->vals[vp_idx].length = strlen (emacs_program);
233 props[props_idx]->vals[vp_idx++].value = emacs_program;
235 smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1);
236 strcpy (smid_opt, SMID_OPT);
237 strcat (smid_opt, client_id);
239 props[props_idx]->vals[vp_idx].length = strlen (smid_opt);
240 props[props_idx]->vals[vp_idx++].value = smid_opt;
242 props[props_idx]->vals[vp_idx].length = strlen (NOSPLASH_OPT);
243 props[props_idx]->vals[vp_idx++].value = NOSPLASH_OPT;
245 if (cwd)
247 chdir_opt = xmalloc (strlen (CHDIR_OPT) + strlen (cwd) + 1);
248 strcpy (chdir_opt, CHDIR_OPT);
249 strcat (chdir_opt, cwd);
251 props[props_idx]->vals[vp_idx].length = strlen (chdir_opt);
252 props[props_idx]->vals[vp_idx++].value = chdir_opt;
255 for (i = 1; i < initial_argc; ++i)
257 props[props_idx]->vals[vp_idx].length = strlen (initial_argv[i]);
258 props[props_idx]->vals[vp_idx++].value = initial_argv[i];
261 ++props_idx;
263 SmcSetProperties (smcConn, props_idx, props);
265 xfree (smid_opt);
266 xfree (chdir_opt);
267 xfree (cwd);
268 xfree (vp);
270 for (i = 0; i < props_idx; ++i)
272 xfree (props[i]->type);
273 xfree (props[i]->name);
276 /* See if we maybe shall interact with the user. */
277 if (interactStyle != SmInteractStyleAny
278 || ! shutdown
279 || saveType == SmSaveLocal
280 || ! SmcInteractRequest (smcConn, SmDialogNormal, smc_interact_CB, 0))
282 /* No interaction, we are done saving ourself. */
283 SmcSaveYourselfDone (smcConn, True);
287 /* According to the SM specification, this shall close the connection. */
289 static void
290 smc_die_CB (SmcConn smcConn, SmPointer clientData)
292 emacs_event.kind = SAVE_SESSION_EVENT;
293 emacs_event.arg = Qt;
296 /* We don't use the next two but they are mandatory, leave them empty.
297 According to the SM specification, we should not interact with the
298 user between smc_save_yourself_CB is called and until smc_save_complete_CB
299 is called. It seems like a lot of job to implement this and it doesn't
300 even seem necessary. */
302 static void
303 smc_save_complete_CB (SmcConn smcConn, SmPointer clientData)
305 /* Empty */
308 static void
309 smc_shutdown_cancelled_CB (SmcConn smcConn, SmPointer clientData)
311 /* Empty */
314 /* Error handlers for SM and ICE. We don't want to exit Emacs just
315 because there is some error in the session management. */
317 static void
318 smc_error_handler (SmcConn smcConn,
319 Bool swap,
320 int offendingMinorOpcode,
321 unsigned long offendingSequence,
322 int errorClass,
323 int severity,
324 SmPointer values)
326 /* Empty */
329 static void
330 ice_error_handler (IceConn iceConn,
331 Bool swap,
332 int offendingMinorOpcode,
333 unsigned long offendingSequence,
334 int errorClass,
335 int severity,
336 IcePointer values)
338 /* Empty */
342 static void
343 ice_io_error_handler (IceConn iceConn)
345 /* Connection probably gone. */
346 ice_connection_closed ();
349 /* This is called when the ICE connection is created or closed. The SM library
350 uses ICE as it transport protocol. */
352 static void
353 ice_conn_watch_CB (IceConn iceConn, IcePointer clientData,
354 int opening, IcePointer *watchData)
356 if (! opening)
358 ice_connection_closed ();
359 return;
362 ice_fd = IceConnectionNumber (iceConn);
363 add_read_fd (ice_fd, x_session_check_input, NULL);
366 /* Create the client leader window. */
368 #ifndef USE_GTK
369 static void
370 create_client_leader_window (struct x_display_info *dpyinfo, char *client_ID)
372 Window w;
373 XClassHint class_hints;
375 w = XCreateSimpleWindow (dpyinfo->display,
376 dpyinfo->root_window,
377 -1, -1, 1, 1,
378 CopyFromParent, CopyFromParent, CopyFromParent);
380 class_hints.res_name = SSDATA (Vx_resource_name);
381 class_hints.res_class = SSDATA (Vx_resource_class);
382 XSetClassHint (dpyinfo->display, w, &class_hints);
383 XStoreName (dpyinfo->display, w, class_hints.res_name);
385 XChangeProperty (dpyinfo->display, w, dpyinfo->Xatom_SM_CLIENT_ID,
386 XA_STRING, 8, PropModeReplace,
387 (unsigned char *) client_ID, strlen (client_ID));
389 dpyinfo->client_leader_window = w;
391 #endif /* ! USE_GTK */
394 /* Try to open a connection to the session manager. */
396 void
397 x_session_initialize (struct x_display_info *dpyinfo)
399 #define SM_ERRORSTRING_LEN 512
400 char errorstring[SM_ERRORSTRING_LEN];
401 char* previous_id = NULL;
402 SmcCallbacks callbacks;
403 ptrdiff_t name_len = 0;
405 ice_fd = -1;
406 doing_interact = False;
408 /* Check if we where started by the session manager. If so, we will
409 have a previous id. */
410 if (! EQ (Vx_session_previous_id, Qnil) && STRINGP (Vx_session_previous_id))
411 previous_id = SSDATA (Vx_session_previous_id);
413 /* Construct the path to the Emacs program. */
414 if (! EQ (Vinvocation_directory, Qnil))
415 name_len += SBYTES (Vinvocation_directory);
416 name_len += SBYTES (Vinvocation_name);
418 /* This malloc will not be freed, but it is only done once, and hopefully
419 not very large */
420 emacs_program = xmalloc (name_len + 1);
421 emacs_program[0] = '\0';
423 if (! EQ (Vinvocation_directory, Qnil))
424 strcpy (emacs_program, SSDATA (Vinvocation_directory));
425 strcat (emacs_program, SSDATA (Vinvocation_name));
427 /* The SM protocol says all callbacks are mandatory, so set up all
428 here and in the mask passed to SmcOpenConnection. */
429 callbacks.save_yourself.callback = smc_save_yourself_CB;
430 callbacks.save_yourself.client_data = 0;
431 callbacks.die.callback = smc_die_CB;
432 callbacks.die.client_data = 0;
433 callbacks.save_complete.callback = smc_save_complete_CB;
434 callbacks.save_complete.client_data = 0;
435 callbacks.shutdown_cancelled.callback = smc_shutdown_cancelled_CB;
436 callbacks.shutdown_cancelled.client_data = 0;
438 /* Set error handlers. */
439 SmcSetErrorHandler (smc_error_handler);
440 IceSetErrorHandler (ice_error_handler);
441 IceSetIOErrorHandler (ice_io_error_handler);
443 /* Install callback for when connection status changes. */
444 IceAddConnectionWatch (ice_conn_watch_CB, 0);
446 /* Open the connection to the session manager. A failure is not
447 critical, it usually means that no session manager is running.
448 The errorstring is here for debugging. */
449 smc_conn = SmcOpenConnection (NULL, NULL, 1, 0,
450 (SmcSaveYourselfProcMask|
451 SmcDieProcMask|
452 SmcSaveCompleteProcMask|
453 SmcShutdownCancelledProcMask),
454 &callbacks,
455 previous_id,
456 &client_id,
457 SM_ERRORSTRING_LEN,
458 errorstring);
460 if (smc_conn != 0)
462 Vx_session_id = build_string (client_id);
464 #ifdef USE_GTK
465 /* GTK creates a leader window by itself, but we need to tell
466 it about our client_id. */
467 gdk_x11_set_sm_client_id (client_id);
468 #else
469 create_client_leader_window (dpyinfo, client_id);
470 #endif
474 /* Ensure that the session manager is not contacted again. */
476 void
477 x_session_close (void)
479 ice_connection_closed ();
483 DEFUN ("handle-save-session", Fhandle_save_session,
484 Shandle_save_session, 1, 1, "e",
485 doc: /* Handle the save_yourself event from a session manager.
486 A session manager can tell Emacs that the window system is shutting down
487 by sending Emacs a save_yourself message. Emacs executes this function when
488 such an event occurs. This function then executes `emacs-session-save'.
489 After that, this function informs the session manager that it can continue
490 or abort shutting down the window system depending on the return value
491 from `emacs-session-save' If the return value is non-nil the session manager
492 is told to abort the window system shutdown.
494 Do not call this function yourself. */)
495 (Lisp_Object event)
497 int kill_emacs = CONSP (event) && CONSP (XCDR (event))
498 && EQ (Qt, XCAR (XCDR (event)));
500 /* Check doing_interact so that we don't do anything if someone called
501 this at the wrong time. */
502 if (doing_interact && ! kill_emacs)
504 Bool cancel_shutdown = False;
506 cancel_shutdown = ! EQ (call0 (intern ("emacs-session-save")), Qnil);
508 SmcInteractDone (smc_conn, cancel_shutdown);
509 SmcSaveYourselfDone (smc_conn, True);
511 doing_interact = False;
513 else if (kill_emacs)
515 /* We should not do user interaction here, but it is not easy to
516 prevent. Fix this in next version. */
517 Fkill_emacs (Qnil);
519 /* This will not be reached, but we want kill-emacs-hook to be run. */
520 SmcCloseConnection (smc_conn, 0, 0);
521 ice_connection_closed ();
524 return Qnil;
529 /***********************************************************************
530 Initialization
531 ***********************************************************************/
532 void
533 syms_of_xsmfns (void)
535 DEFVAR_LISP ("x-session-id", Vx_session_id,
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;
543 DEFVAR_LISP ("x-session-previous-id", Vx_session_previous_id,
544 doc: /* The previous session id Emacs got from session manager.
545 If Emacs is running on a window system that has a session manager, the
546 session manager gives Emacs a session id. It is feasible for Emacs Lisp
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
550 running and saves the session id Emacs had.
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
556 previous session id shall be used to reconstruct the file name.
558 The session id Emacs has while it is running is in the variable
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.
562 See also `emacs-save-session-functions', `emacs-session-save' and
563 `emacs-session-restore'." */);
564 Vx_session_previous_id = Qnil;
566 defsubr (&Shandle_save_session);
569 #endif /* HAVE_X_SM */