Fix typo in comment.
[bpt/emacs.git] / src / xsmfns.c
CommitLineData
afb4ecad
JD
1/* Session management module for systems which understand the X Session
2 management protocol.
656132eb 3 Copyright (C) 2002, 2002 Free Software Foundation, Inc.
afb4ecad
JD
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
21
22#include <config.h>
23
24#ifdef HAVE_X_SM
25
26#include <X11/SM/SMlib.h>
27#ifdef HAVE_STRING_H
28#include <string.h>
29#else
30#ifdef HAVE_STRINGS_H
31#include <strings.h>
32#endif
33#endif
34
35#ifdef HAVE_UNISTD_H
36#include <unistd.h>
37#endif
38#ifdef HAVE_STDLIB_H
39#include <stdlib.h>
40#endif
41
42#include <sys/param.h>
656132eb 43#include <stdio.h>
afb4ecad
JD
44
45#include "systime.h"
46#include "sysselect.h"
47#include "lisp.h"
48#include "termhooks.h"
656132eb 49#include "termopts.h"
afb4ecad
JD
50
51#ifndef MAXPATHLEN
52#define MAXPATHLEN 1024
53#endif /* not MAXPATHLEN */
54
55
56/* The user login name. */
57
58extern Lisp_Object Vuser_login_name;
59
3b8f9651 60/* This is the event used when SAVE_SESSION_EVENT occurs. */
afb4ecad
JD
61
62static struct input_event emacs_event;
63
3b8f9651 64/* The descriptor that we use to check for data from the session manager. */
afb4ecad
JD
65
66static int ice_fd = -1;
67
3b8f9651 68/* A flag that says if we are in shutdown interactions or not. */
afb4ecad
JD
69
70static int doing_interact = False;
71
72/* The session manager object for the session manager connection */
73
74static SmcConn smc_conn;
75
76/* The client session id for this session */
77static char *client_id;
78
79/* The full path name to the Emacs program */
80static char *emacs_program;
81
82/* The client session id for this session as a lisp object. */
83
84Lisp_Object Vx_session_id;
85
86/* The id we had the previous session. This is only available if we
87 have been started by the session manager with SMID_OPT. */
88
89Lisp_Object Vx_session_previous_id;
90
91/* The option we tell the session manager to start Emacs with when
92 restarting Emacs. The client_id is appended. */
93
94#define SMID_OPT "--smid="
95
96
97/* Handle any messages from the session manager. If no connection is
98 open to a session manager, just return 0.
99 Otherwise returns the number of events stored in buffer BUFP,
100 which can hold up to *NUMCHARS characters. At most one event is
7d0393cf 101 stored, a SAVE_SESSION_EVENT. */
afb4ecad
JD
102int
103x_session_check_input (bufp, numchars)
104 struct input_event *bufp;
105 int *numchars;
106{
107 SELECT_TYPE read_fds;
108 EMACS_TIME tmout;
7d0393cf 109
afb4ecad 110 if (ice_fd == -1) return 0;
7d0393cf 111
afb4ecad
JD
112 FD_ZERO (&read_fds);
113 FD_SET (ice_fd, &read_fds);
7d0393cf 114
afb4ecad
JD
115 tmout.tv_sec = 0;
116 tmout.tv_usec = 0;
7d0393cf 117
afb4ecad
JD
118 /* Reset this so wo can check kind after callbacks have been called by
119 IceProcessMessages. The smc_interact_CB sets the kind to
3b8f9651 120 SAVE_SESSION_EVENT, but we don't know beforehand if that callback
afb4ecad 121 will be called. */
3b8f9651 122 emacs_event.kind = NO_EVENT;
afb4ecad
JD
123
124 if (select (ice_fd+1, &read_fds,
125 (SELECT_TYPE *)0, (SELECT_TYPE *)0, &tmout) < 0)
126 {
127 ice_fd = -1;
128 return 0;
129 }
7d0393cf 130
afb4ecad
JD
131
132 if (FD_ISSET (ice_fd, &read_fds))
133 IceProcessMessages (SmcGetIceConnection (smc_conn),
134 (IceReplyWaitInfo *)0, (Bool *)0);
135
7d0393cf 136
afb4ecad 137 /* Check if smc_interact_CB was called and we shall generate a
3b8f9651
PJ
138 SAVE_SESSION_EVENT. */
139 if (*numchars > 0 && emacs_event.kind != NO_EVENT)
afb4ecad
JD
140 {
141 bcopy (&emacs_event, bufp, sizeof (struct input_event));
142 bufp++;
143 (*numchars)--;
144
145 return 1;
146 }
147
148 return 0;
149}
150
151/* Return non-zero if we have a connection to a session manager.*/
152int
153x_session_have_connection ()
154{
155 return ice_fd != -1;
156}
157
158/* This is called when the session manager says it is OK to interact with the
3b8f9651 159 user. Here we set the kind to SAVE_SESSION_EVENT so an event is generated.
afb4ecad
JD
160 Then lisp code can interact with the user. */
161static void
162smc_interact_CB (smcConn, clientData)
163 SmcConn smcConn;
164 SmPointer clientData;
165{
166 doing_interact = True;
3b8f9651 167 emacs_event.kind = SAVE_SESSION_EVENT;
afb4ecad
JD
168}
169
170/* This is called when the session manager tells us to save ourself.
171 We set the required properties so the session manager can restart us,
172 plus the current working directory property (not mandatory) so we
173 are started in the correct directory.
174
175 If this is a shutdown and we can request to interact with the user,
176 we do so, because we don't know what the lisp code might do. */
177static void
178smc_save_yourself_CB (smcConn,
179 clientData,
180 saveType,
181 shutdown,
182 interactStyle,
183 fast)
184 SmcConn smcConn;
185 SmPointer clientData;
186 int saveType;
187 Bool shutdown;
188 int interactStyle;
189 Bool fast;
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;
7d0393cf 199
afb4ecad
JD
200 char cwd[MAXPATHLEN+1];
201 char *smid_opt;
202
203 /* How to start a new instance of Emacs */
204 props[props_idx] = &prop_ptr[props_idx];
205 props[props_idx]->name = SmCloneCommand;
206 props[props_idx]->type = SmLISTofARRAY8;
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
213 /* The name of the program */
214 props[props_idx] = &prop_ptr[props_idx];
215 props[props_idx]->name = SmProgram;
216 props[props_idx]->type = SmARRAY8;
217 props[props_idx]->num_vals = 1;
218 props[props_idx]->vals = &values[val_idx++];
d5db4077
KR
219 props[props_idx]->vals[0].length = strlen (SDATA (Vinvocation_name));
220 props[props_idx]->vals[0].value = SDATA (Vinvocation_name);
afb4ecad 221 ++props_idx;
7d0393cf 222
afb4ecad
JD
223 /* How to restart Emacs (i.e.: /path/to/emacs --smid=xxxx). */
224 props[props_idx] = &prop_ptr[props_idx];
225 props[props_idx]->name = SmRestartCommand;
226 props[props_idx]->type = SmLISTofARRAY8;
227 props[props_idx]->num_vals = 2; /* 2 values: /path/to/emacs, --smid=xxx */
228 props[props_idx]->vals = &values[val_idx];
229 props[props_idx]->vals[0].length = strlen (emacs_program);
230 props[props_idx]->vals[0].value = emacs_program;
231
232 smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1);
233 strcpy (smid_opt, SMID_OPT);
234 strcat (smid_opt, client_id);
7d0393cf 235
afb4ecad
JD
236 props[props_idx]->vals[1].length = strlen (smid_opt);
237 props[props_idx]->vals[1].value = smid_opt;
238 val_idx += 2;
239 ++props_idx;
240
241 /* User id */
242 props[props_idx] = &prop_ptr[props_idx];
243 props[props_idx]->name = SmUserID;
244 props[props_idx]->type = SmARRAY8;
245 props[props_idx]->num_vals = 1;
246 props[props_idx]->vals = &values[val_idx++];
d5db4077
KR
247 props[props_idx]->vals[0].length = strlen (SDATA (Vuser_login_name));
248 props[props_idx]->vals[0].value = SDATA (Vuser_login_name);
afb4ecad
JD
249 ++props_idx;
250
251 /* The current directory property, not mandatory */
252#ifdef HAVE_GETCWD
253 if (getcwd (cwd, MAXPATHLEN+1) != 0)
254#else
255 if (getwd (cwd) != 0)
256#endif
257 {
258 props[props_idx] = &prop_ptr[props_idx];
259 props[props_idx]->name = SmCurrentDirectory;
260 props[props_idx]->type = SmARRAY8;
261 props[props_idx]->num_vals = 1;
262 props[props_idx]->vals = &values[val_idx++];
263 props[props_idx]->vals[0].length = strlen (cwd);
264 props[props_idx]->vals[0].value = cwd;
265 ++props_idx;
266 }
7d0393cf
JB
267
268
afb4ecad
JD
269 SmcSetProperties (smcConn, props_idx, props);
270
271 xfree (smid_opt);
272
273 /* See if we maybe shall interact with the user. */
274 if (interactStyle != SmInteractStyleAny
275 || ! shutdown
276 || saveType == SmSaveLocal
277 || ! SmcInteractRequest (smcConn, SmDialogNormal, smc_interact_CB, 0))
278 {
279 /* No interaction, we are done saving ourself. */
280 SmcSaveYourselfDone (smcConn, True);
281 }
282}
283
284/* According to the SM specification, this shall close the connection */
285static void
286smc_die_CB (smcConn, clientData)
287 SmcConn smcConn;
288 SmPointer clientData;
289{
290 SmcCloseConnection (smcConn, 0, 0);
291 ice_fd = -1;
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
298 even seem necessary. */
299static void
300smc_save_complete_CB (smcConn, clientData)
301 SmcConn smcConn;
302 SmPointer clientData;
303{
304 /* Empty */
305}
306
307static void
308smc_shutdown_cancelled_CB (smcConn, clientData)
309 SmcConn smcConn;
310 SmPointer clientData;
311{
312 /* Empty */
313}
314
315/* Error handlers for SM and ICE. We don't wan't to exit Emacs just
316 because there is some error in the session management. */
317static void
318smc_error_handler (smcConn,
319 swap,
320 offendingMinorOpcode,
321 offendingSequence,
322 errorClass,
323 severity,
324 values)
325 SmcConn smcConn;
326 Bool swap;
327 int offendingMinorOpcode;
328 unsigned long offendingSequence;
329 int errorClass;
330 int severity;
331 SmPointer values;
332{
333 /* Empty */
334}
335
336static void
337ice_error_handler (iceConn,
338 swap,
339 offendingMinorOpcode,
340 offendingSequence,
341 errorClass,
342 severity,
343 values)
344 IceConn iceConn;
345 Bool swap;
346 int offendingMinorOpcode;
347 unsigned long offendingSequence;
348 int errorClass;
349 int severity;
350 IcePointer values;
351{
352 /* Empty */
353}
354
355
356static void
357ice_io_error_handler (iceConn)
358 IceConn iceConn;
359{
360 /* Connection probably gone. */
361 ice_fd = -1;
362}
363
364/* This is called when the ICE connection is created or closed. The SM library
365 uses ICE as it transport protocol. */
366static void
367ice_conn_watch_CB (iceConn, clientData, opening, watchData)
368 IceConn iceConn;
369 IcePointer clientData;
370 Bool opening;
371 IcePointer *watchData;
372{
373 if (! opening)
374 {
375 ice_fd = -1;
376 return;
377 }
7d0393cf 378
afb4ecad
JD
379 ice_fd = IceConnectionNumber (iceConn);
380#ifndef F_SETOWN_BUG
381#ifdef F_SETOWN
382#ifdef F_SETOWN_SOCK_NEG
383 /* stdin is a socket here */
384 fcntl (ice_fd, F_SETOWN, -getpid ());
385#else /* ! defined (F_SETOWN_SOCK_NEG) */
386 fcntl (ice_fd, F_SETOWN, getpid ());
387#endif /* ! defined (F_SETOWN_SOCK_NEG) */
388#endif /* ! defined (F_SETOWN) */
389#endif /* F_SETOWN_BUG */
390
391#ifdef SIGIO
392 if (interrupt_input)
393 init_sigio (ice_fd);
394#endif /* ! defined (SIGIO) */
395}
396
397/* Try to open a connection to the session manager. */
398void
399x_session_initialize ()
400{
401#define SM_ERRORSTRING_LEN 512
402 char errorstring[SM_ERRORSTRING_LEN];
403 char* previous_id = NULL;
404 SmcCallbacks callbacks;
405 int name_len = 0;
7d0393cf 406
afb4ecad
JD
407 /* Check if we where started by the session manager. If so, we will
408 have a previous id. */
409 if (! EQ (Vx_session_previous_id, Qnil) && STRINGP (Vx_session_previous_id))
d5db4077 410 previous_id = SDATA (Vx_session_previous_id);
afb4ecad
JD
411
412 /* Construct the path to the Emacs program. */
413 if (! EQ (Vinvocation_directory, Qnil))
d5db4077
KR
414 name_len += strlen (SDATA (Vinvocation_directory));
415 name_len += strlen (SDATA (Vinvocation_name));
afb4ecad
JD
416
417 /* This malloc will not be freed, but it is only done once, and hopefully
418 not very large */
419 emacs_program = xmalloc (name_len + 1);
420 emacs_program[0] = '\0';
421
422 if (! EQ (Vinvocation_directory, Qnil))
d5db4077
KR
423 strcpy (emacs_program, SDATA (Vinvocation_directory));
424 strcat (emacs_program, SDATA (Vinvocation_name));
7d0393cf 425
afb4ecad
JD
426 /* The SM protocol says all callbacks are mandatory, so set up all
427 here and in the mask passed to SmcOpenConnection */
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
437 /* Set error handlers. */
438 SmcSetErrorHandler (smc_error_handler);
439 IceSetErrorHandler (ice_error_handler);
440 IceSetIOErrorHandler (ice_io_error_handler);
441
442 /* Install callback for when connection status changes. */
443 IceAddConnectionWatch (ice_conn_watch_CB, 0);
444
445 /* Open the connection to the session manager. A failure is not
446 critical, it usualy means that no session manager is running.
447 The errorstring is here for debugging. */
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)
460 Vx_session_id = make_string (client_id, strlen (client_id));
461}
462
463
464DEFUN ("handle-save-session", Fhandle_save_session,
465 Shandle_save_session, 1, 1, "e",
466 doc: /* Handle the save_yourself event from a session manager.
7d0393cf 467A session manager can tell Emacs that the window system is shutting down
afb4ecad
JD
468by sending Emacs a save_yourself message. Emacs executes this function when
469such an event occurs. This function then executes `emacs-session-save'.
470After that, this function informs the session manager that it can continue
471or abort shutting down the window system depending on the return value
472from `emacs-session-save' If the return value is non-nil the session manager
473is told to abort the window system shutdown.
474
475Do not call this function yourself. */)
476 (event)
477 Lisp_Object event;
478{
479 /* Check doing_interact so that we don't do anything if someone called
480 this at the wrong time. */
481 if (doing_interact)
482 {
483 Bool cancel_shutdown = False;
484
485 cancel_shutdown = ! EQ (call0 (intern ("emacs-session-save")), Qnil);
486
487 SmcInteractDone (smc_conn, cancel_shutdown);
488 SmcSaveYourselfDone (smc_conn, True);
489
490 doing_interact = False;
491 }
ad4ace7a
JD
492
493 return Qnil;
afb4ecad
JD
494}
495
496\f
497/***********************************************************************
498 Initialization
499 ***********************************************************************/
500void
501syms_of_xsmfns ()
502{
503 DEFVAR_LISP ("x-session-id", &Vx_session_id,
504 doc: /* The session id Emacs got from the session manager for this session.
505Changing the value does not change the session id used by Emacs.
506The value is nil if no session manager is running.
507See also `x-session-previous-id', `emacs-save-session-functions',
508`emacs-session-save' and `emacs-session-restore'." */);
509 Vx_session_id = Qnil;
510
511 DEFVAR_LISP ("x-session-previous-id", &Vx_session_previous_id,
512 doc: /* The previous session id Emacs got from session manager.
7d0393cf
JB
513If Emacs is running on a window system that has a session manager, the
514session manager gives Emacs a session id. It is feasible for Emacs lisp
515code to use the session id to save configuration in, for example, a file
516with a file name based on the session id. If Emacs is running when the
517window system is shut down, the session manager remembers that Emacs was
afb4ecad
JD
518running and saves the session id Emacs had.
519
7d0393cf
JB
520When the window system is started again, the session manager restarts
521Emacs and hands Emacs the session id it had the last time it was
522running. This is now the previous session id and the value of this
523variable. If configuration was saved in a file as stated above, the
afb4ecad
JD
524previous session id shall be used to reconstruct the file name.
525
7d0393cf 526The session id Emacs has while it is running is in the variable
afb4ecad
JD
527`x-session-id'. The value of this variable and `x-session-id' may be the
528same, depending on how the session manager works.
529
530See also `emacs-save-session-functions', `emacs-session-save' and
531`emacs-session-restore'." */);
532 Vx_session_previous_id = Qnil;
7d0393cf 533
afb4ecad
JD
534 defsubr (&Shandle_save_session);
535}
536
537#endif /* HAVE_X_SM */