1 /* Session management module for systems which understand the X Session
3 Copyright (C) 2002, 2002 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include <X11/SM/SMlib.h>
42 #include <sys/param.h>
46 #include "sysselect.h"
48 #include "termhooks.h"
52 #define MAXPATHLEN 1024
53 #endif /* not MAXPATHLEN */
56 /* The user login name. */
58 extern Lisp_Object Vuser_login_name
;
60 /* This is the event used when save_session occurs */
62 static struct input_event emacs_event
;
64 /* The descriptor that we use to check for data from the session manager. */
66 static int ice_fd
= -1;
68 /* A flag that says if we are in shutdown interactions or not. */
70 static int doing_interact
= False
;
72 /* The session manager object for the session manager connection */
74 static SmcConn smc_conn
;
76 /* The client session id for this session */
77 static char *client_id
;
79 /* The full path name to the Emacs program */
80 static char *emacs_program
;
82 /* The client session id for this session as a lisp object. */
84 Lisp_Object Vx_session_id
;
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. */
89 Lisp_Object Vx_session_previous_id
;
91 /* The option we tell the session manager to start Emacs with when
92 restarting Emacs. The client_id is appended. */
94 #define SMID_OPT "--smid="
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
101 stored, an save_session_event. */
103 x_session_check_input (bufp
, numchars
)
104 struct input_event
*bufp
;
107 SELECT_TYPE read_fds
;
110 if (ice_fd
== -1) return 0;
113 FD_SET (ice_fd
, &read_fds
);
118 /* Reset this so wo can check kind after callbacks have been called by
119 IceProcessMessages. The smc_interact_CB sets the kind to
120 save_session_event, but we don't know beforehand if that callback
122 emacs_event
.kind
= no_event
;
124 if (select (ice_fd
+1, &read_fds
,
125 (SELECT_TYPE
*)0, (SELECT_TYPE
*)0, &tmout
) < 0)
132 if (FD_ISSET (ice_fd
, &read_fds
))
133 IceProcessMessages (SmcGetIceConnection (smc_conn
),
134 (IceReplyWaitInfo
*)0, (Bool
*)0);
137 /* Check if smc_interact_CB was called and we shall generate a
138 save_session event. */
139 if (*numchars
> 0 && emacs_event
.kind
!= no_event
)
141 bcopy (&emacs_event
, bufp
, sizeof (struct input_event
));
151 /* Return non-zero if we have a connection to a session manager.*/
153 x_session_have_connection ()
158 /* This is called when the session manager says it is OK to interact with the
159 user. Here we set the kind to save_session so an event is generated.
160 Then lisp code can interact with the user. */
162 smc_interact_CB (smcConn
, clientData
)
164 SmPointer clientData
;
166 doing_interact
= True
;
167 emacs_event
.kind
= save_session_event
;
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.
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. */
178 smc_save_yourself_CB (smcConn
,
185 SmPointer clientData
;
193 SmProp
*props
[NR_PROPS
];
194 SmProp prop_ptr
[NR_PROPS
];
196 SmPropValue values
[20];
200 char cwd
[MAXPATHLEN
+1];
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
;
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
++];
219 props
[props_idx
]->vals
[0].length
= strlen (XSTRING (Vinvocation_name
)->data
);
220 props
[props_idx
]->vals
[0].value
= XSTRING (Vinvocation_name
)->data
;
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
;
232 smid_opt
= xmalloc (strlen (SMID_OPT
) + strlen (client_id
) + 1);
233 strcpy (smid_opt
, SMID_OPT
);
234 strcat (smid_opt
, client_id
);
236 props
[props_idx
]->vals
[1].length
= strlen (smid_opt
);
237 props
[props_idx
]->vals
[1].value
= smid_opt
;
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
++];
247 props
[props_idx
]->vals
[0].length
= strlen (XSTRING (Vuser_login_name
)->data
);
248 props
[props_idx
]->vals
[0].value
= XSTRING (Vuser_login_name
)->data
;
251 /* The current directory property, not mandatory */
253 if (getcwd (cwd
, MAXPATHLEN
+1) != 0)
255 if (getwd (cwd
) != 0)
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
;
269 SmcSetProperties (smcConn
, props_idx
, props
);
273 /* See if we maybe shall interact with the user. */
274 if (interactStyle
!= SmInteractStyleAny
276 || saveType
== SmSaveLocal
277 || ! SmcInteractRequest (smcConn
, SmDialogNormal
, smc_interact_CB
, 0))
279 /* No interaction, we are done saving ourself. */
280 SmcSaveYourselfDone (smcConn
, True
);
284 /* According to the SM specification, this shall close the connection */
286 smc_die_CB (smcConn
, clientData
)
288 SmPointer clientData
;
290 SmcCloseConnection (smcConn
, 0, 0);
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. */
300 smc_save_complete_CB (smcConn
, clientData
)
302 SmPointer clientData
;
308 smc_shutdown_cancelled_CB (smcConn
, clientData
)
310 SmPointer clientData
;
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. */
318 smc_error_handler (smcConn
,
320 offendingMinorOpcode
,
327 int offendingMinorOpcode
;
328 unsigned long offendingSequence
;
337 ice_error_handler (iceConn
,
339 offendingMinorOpcode
,
346 int offendingMinorOpcode
;
347 unsigned long offendingSequence
;
357 ice_io_error_handler (iceConn
)
360 /* Connection probably gone. */
364 /* This is called when the ICE connection is created or closed. The SM library
365 uses ICE as it transport protocol. */
367 ice_conn_watch_CB (iceConn
, clientData
, opening
, watchData
)
369 IcePointer clientData
;
371 IcePointer
*watchData
;
379 ice_fd
= IceConnectionNumber (iceConn
);
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 */
394 #endif /* ! defined (SIGIO) */
397 /* Try to open a connection to the session manager. */
399 x_session_initialize ()
401 #define SM_ERRORSTRING_LEN 512
402 char errorstring
[SM_ERRORSTRING_LEN
];
403 char* previous_id
= NULL
;
404 SmcCallbacks callbacks
;
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
))
410 previous_id
= XSTRING (Vx_session_previous_id
)->data
;
412 /* Construct the path to the Emacs program. */
413 if (! EQ (Vinvocation_directory
, Qnil
))
414 name_len
+= strlen (XSTRING (Vinvocation_directory
)->data
);
415 name_len
+= strlen (XSTRING (Vinvocation_name
)->data
);
417 /* This malloc will not be freed, but it is only done once, and hopefully
419 emacs_program
= xmalloc (name_len
+ 1);
420 emacs_program
[0] = '\0';
422 if (! EQ (Vinvocation_directory
, Qnil
))
423 strcpy (emacs_program
, XSTRING (Vinvocation_directory
)->data
);
424 strcat (emacs_program
, XSTRING (Vinvocation_name
)->data
);
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;
437 /* Set error handlers. */
438 SmcSetErrorHandler (smc_error_handler
);
439 IceSetErrorHandler (ice_error_handler
);
440 IceSetIOErrorHandler (ice_io_error_handler
);
442 /* Install callback for when connection status changes. */
443 IceAddConnectionWatch (ice_conn_watch_CB
, 0);
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
|
451 SmcSaveCompleteProcMask
|
452 SmcShutdownCancelledProcMask
),
460 Vx_session_id
= make_string (client_id
, strlen (client_id
));
464 DEFUN ("handle-save-session", Fhandle_save_session
,
465 Shandle_save_session
, 1, 1, "e",
466 doc
: /* Handle the save_yourself event from a session manager.
467 A session manager can tell Emacs that the window system is shutting down
468 by sending Emacs a save_yourself message. Emacs executes this function when
469 such an event occurs. This function then executes `emacs-session-save'.
470 After that, this function informs the session manager that it can continue
471 or abort shutting down the window system depending on the return value
472 from `emacs-session-save' If the return value is non-nil the session manager
473 is told to abort the window system shutdown.
475 Do not call this function yourself. */)
479 /* Check doing_interact so that we don't do anything if someone called
480 this at the wrong time. */
483 Bool cancel_shutdown
= False
;
485 cancel_shutdown
= ! EQ (call0 (intern ("emacs-session-save")), Qnil
);
487 SmcInteractDone (smc_conn
, cancel_shutdown
);
488 SmcSaveYourselfDone (smc_conn
, True
);
490 doing_interact
= False
;
497 /***********************************************************************
499 ***********************************************************************/
503 DEFVAR_LISP ("x-session-id", &Vx_session_id
,
504 doc
: /* The session id Emacs got from the session manager for this session.
505 Changing the value does not change the session id used by Emacs.
506 The value is nil if no session manager is running.
507 See also `x-session-previous-id', `emacs-save-session-functions',
508 `emacs-session-save' and `emacs-session-restore'." */);
509 Vx_session_id
= Qnil
;
511 DEFVAR_LISP ("x-session-previous-id", &Vx_session_previous_id
,
512 doc
: /* The previous session id Emacs got from session manager.
513 If Emacs is running on a window system that has a session manager, the
514 session manager gives Emacs a session id. It is feasible for Emacs lisp
515 code to use the session id to save configuration in, for example, a file
516 with a file name based on the session id. If Emacs is running when the
517 window system is shut down, the session manager remembers that Emacs was
518 running and saves the session id Emacs had.
520 When the window system is started again, the session manager restarts
521 Emacs and hands Emacs the session id it had the last time it was
522 running. This is now the previous session id and the value of this
523 variable. If configuration was saved in a file as stated above, the
524 previous session id shall be used to reconstruct the file name.
526 The session id Emacs has while it is running is in the variable
527 `x-session-id'. The value of this variable and `x-session-id' may be the
528 same, depending on how the session manager works.
530 See also `emacs-save-session-functions', `emacs-session-save' and
531 `emacs-session-restore'." */);
532 Vx_session_previous_id
= Qnil
;
534 defsubr (&Shandle_save_session
);
537 #endif /* HAVE_X_SM */