From mboxrd@z Thu Jan 1 00:00:00 1970 Path: quimby.gnus.org!not-for-mail From: "Jan D." Newsgroups: gmane.emacs.devel Subject: New session management patch. Date: Tue, 26 Feb 2002 23:49:13 +0100 (CET) Message-ID: <200202262249.XAA20211@gaffa.gaia.swipnet.se> NNTP-Posting-Host: quimby2.netfonds.no Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Trace: quimby2.netfonds.no 1014764109 30988 195.204.10.66 (26 Feb 2002 22:55:09 GMT) X-Complaints-To: usenet@quimby2.netfonds.no NNTP-Posting-Date: 26 Feb 2002 22:55:09 GMT Cc: emacs-devel@gnu.org Original-Received: from fencepost.gnu.org ([199.232.76.164]) by quimby2.netfonds.no with esmtp (Exim 3.12 #1 (Debian)) id 16fqV1-00083h-00 for ; Tue, 26 Feb 2002 23:55:08 +0100 Original-Received: from localhost ([127.0.0.1] helo=fencepost.gnu.org) by fencepost.gnu.org with esmtp (Exim 3.33 #1 (Debian)) id 16fqRM-0003v4-00; Tue, 26 Feb 2002 17:51:20 -0500 Original-Received: from aurora.natverket.com ([195.196.30.60]) by fencepost.gnu.org with esmtp (Exim 3.33 #1 (Debian)) id 16fqPl-0003nH-00 for ; Tue, 26 Feb 2002 17:49:41 -0500 Original-Received: from gaffa.gaia.swipnet.se (pc35.bodenonline.com [195.196.29.227] (may be forged)) by aurora.natverket.com (8.9.3/8.9.3) with ESMTP id AAA20258; Wed, 27 Feb 2002 00:32:48 +0100 (CET) In-Reply-To: <5x1yfe5yaz.fsf@kfs2.cua.dk> "from Kim F. Storm at Feb 21, 2002 10:18:12 pm" Original-To: "Kim F. Storm" Errors-To: emacs-devel-admin@gnu.org X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.0.5 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: Xref: quimby.gnus.org gmane.emacs.devel:1566 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:1566 Kim F. Storm wrote: > Another problem is how to restore things after restarting emacs. I > don't think you can rely on a hook to do this. Some things may not > have been added to the restore-hook when you run it (e.g. if gnus > stores its state in a file, there need to be a gnus-restore-session > function in the hook list for this to work.) > > One possible fix would be to let the save-hook functions insert lisp > code in the session save file which - when emacs restarts - is eval'ed > to restore the previous state. > > You should make a default choice of how the session file is named > based on the session-id (define a function emacs-session-save-filename > in x-win.el which the user can rewrite), and then -- at the place > where you now do ... I have incorporated your ideas almost verbatim, Your remark about the problem of restore is well put. Also a default filename makes it easier for functions to save state. But I haven't made variables for functions emacs-session-save and emacs-session-restore. I figured that anyone can redefine the functions if they so wish. Another thing is that I had to make a PATH search for emacs to find the absolute path for the emacs exeutable. In the path search and also in some lisp code that makes the filename, the directory separator "/" is hard coded. Is this OK? Is there a variable to look at instead? I found directory-sep-char but it is deprecated. I know VMS for instance has X, but does it have SM and ICE also? This patch still doesn't save any state, but it is getting closer. Jan D. Index: lisp/term/x-win.el --- lisp/term/x-win.el.orig Thu Jan 24 20:20:12 2002 +++ lisp/term/x-win.el Tue Feb 26 22:48:49 2002 @@ -234,6 +234,72 @@ (funcall handler this-switch)) (setq args (cons orig-this-switch args))))) (nconc (nreverse args) x-invocation-args)) + +;; Handle the --smid switch. This is used by the session manager +;; to give us back our session id we had on the previous run. +(defun x-handle-smid (switch) + (or (consp x-invocation-args) + (error "%s: missing argument to `%s' option" (invocation-name) switch)) + (setq x-session-previous-id (car x-invocation-args) + x-invocation-args (cdr x-invocation-args))) + +(defvar emacs-save-session-functions nil + "Functions to run when a save-session event occurs. +The functions does not get any argument. +Functions can return non-nil to inform the session manager that the +window system shutdown should be aborted. + +See also `emacs-session-save'.") + +(defun emacs-session-filename (session-id) + "Construct a filename to save the session in based on SESSION-ID. +If the directory ~/.emacs.d exists, we make a filename in there, otherwise +a file in the home directory." + (let ((basename (concat "emacs." session-id)) + (emacs-dir "~/.emacs.d/")) + (expand-file-name (if (file-directory-p emacs-dir) + (concat emacs-dir basename) + (concat "~/." basename))))) + + (defun emacs-session-save () + "Run the `emacs-save-session-functions' when window system is closing. +When a session manager tells Emacs that the window system is shutting +down, this function is called. It calls the functions in the hook +`emacs-save-session-functions'. Functions are called with the current +buffer set to a temporary buffer. Functions should use `insert' to insert +lisp code to save the session state. The buffer is saved +in a file in the home directory of the user running Emacs. The file +is evaluated when Emacs is restarted by the session manager. + +If any of the functions returns non-nil, no more functions are called +and this function returns non-nil. This will inform the session manager +that it should abort the window system shutdown." + (let ((filename (emacs-session-filename x-session-id)) + (buf (get-buffer-create (concat " *SES " x-session-id)))) + (when (file-exists-p filename) + (delete-file filename)) + (with-current-buffer buf + (let ((cancel-shutdown (condition-case nil + (run-hook-with-args-until-success + 'emacs-save-session-functions x-session-id) + (error t)))) + (unless cancel-shutdown + (write-file filename)) + (kill-buffer buf) + cancel-shutdown)))) + +(defun emacs-session-restore () + "Restore the Emacs session if started by a session manager. +The file saved by `emacs-session-save' is evaluated and deleted if it +exists." + (let ((filename (emacs-session-filename x-session-previous-id))) + (when (file-exists-p filename) + (load-file filename) + (delete-file filename) + (message "Restored session data")))) + + + ;; ;; Standard X cursor shapes, courtesy of Mr. Fox, who wanted ALL of them. Index: lisp/startup.el --- lisp/startup.el.orig Wed Feb 6 15:59:10 2002 +++ lisp/startup.el Tue Feb 26 22:48:49 2002 @@ -236,7 +236,8 @@ ("--cursor-color" 1 x-handle-switch cursor-color) ("--vertical-scroll-bars" 0 x-handle-switch vertical-scroll-bars t) ("--line-spacing" 1 x-handle-numeric-switch line-spacing) - ("--border-color" 1 x-handle-switch border-width)) + ("--border-color" 1 x-handle-switch border-width) + ("--smid" 1 x-handle-smid)) "Alist of X Windows options. Each element has the form (NAME NUMARGS HANDLER FRAME-PARAM VALUE) @@ -1028,7 +1029,12 @@ (command-line-1 (cdr command-line-args)) ;; If -batch, terminate after processing the command options. - (if noninteractive (kill-emacs t))) + (if noninteractive (kill-emacs t)) + + ;; Run emacs-session-restore (session management) if started by + ;; the session manager and we have a session manager connection. + (if (and (stringp x-session-previous-id) (stringp x-session-id)) + (emacs-session-restore))) (defcustom initial-scratch-message (purecopy "\ ;; This buffer is for notes you don't want to save, and for Lisp evaluation. Index: src/Makefile.in --- src/Makefile.in.orig Tue Feb 26 22:44:10 2002 +++ src/Makefile.in Tue Feb 26 22:48:49 2002 @@ -294,7 +294,7 @@ #ifdef HAVE_MENUS /* Include xmenu.o in the list of X object files. */ -XOBJ= xterm.o xfns.o xselect.o xrdb.o fontset.o +XOBJ= xterm.o xfns.o xselect.o xrdb.o fontset.o xsmfns.o /* The X Menu stuff is present in the X10 distribution, but missing from X11. If we have X10, just use the installed library; @@ -315,7 +315,7 @@ /* Otherwise, omit xmenu.o from the list of X object files, and don't worry about the menu library at all. */ -XOBJ= xterm.o xfns.o xselect.o xrdb.o fontset.o +XOBJ= xterm.o xfns.o xselect.o xrdb.o fontset.o xsmfns.o LIBXMENU= #endif /* not HAVE_MENUS */ @@ -366,7 +366,11 @@ #endif /* not LIBXT_STATIC */ #else /* not USE_X_TOOLKIT */ +#ifdef HAVE_X_SM +LIBXT=-lSM -lICE +#else LIBXT= +#endif #endif /* not USE_X_TOOLKIT */ #if HAVE_XPM @@ -549,7 +553,7 @@ These go in the DOC file on all machines in case they are needed there. */ SOME_MACHINE_OBJECTS = sunfns.o dosfns.o msdos.o \ - xterm.o xfns.o xmenu.o xselect.o xrdb.o + xterm.o xfns.o xmenu.o xselect.o xrdb.o xsmfns.o #ifdef TERMINFO @@ -1128,6 +1132,7 @@ xselect.o: xselect.c dispextern.h frame.h xterm.h blockinput.h charset.h \ coding.h ccl.h buffer.h atimer.h systime.h $(config_h) xrdb.o: xrdb.c $(config_h) epaths.h +xsmfns.o: xsmfns.c $(config_h) systime.h sysselect.h lisp.h termhooks.h hftctl.o: hftctl.c $(config_h) sound.o: sound.c dispextern.h $(config_h) atimer.o: atimer.c atimer.h systime.h $(config_h) Index: src/xterm.c --- src/xterm.c.orig Tue Feb 26 22:44:13 2002 +++ src/xterm.c Tue Feb 26 22:48:49 2002 @@ -10032,6 +10032,12 @@ x_io_error_quitter (dpyinfo->display); } +#ifdef HAVE_X_SM + BLOCK_INPUT; + count += x_session_check_input (bufp, &numchars); + UNBLOCK_INPUT; +#endif + while (XPending (dpyinfo->display)) { XNextEvent (dpyinfo->display, &event); @@ -10114,11 +10120,17 @@ the session manager, who's looking for such a PropertyNotify. Can restart processing when a keyboard or mouse event arrives. */ - if (numchars > 0) + /* If we have a session manager, don't set this. + KDE will then start two Emacsen, one for the + session manager and one for this. */ + if (numchars > 0 +#ifdef HAVE_X_SM + && ! x_session_have_connection () +#endif + ) { f = x_top_window_to_frame (dpyinfo, event.xclient.window); - /* This is just so we only give real data once for a single Emacs process. */ if (f == SELECTED_FRAME ()) @@ -15046,6 +15058,10 @@ #endif /* ! defined (SIGWINCH) */ signal (SIGPIPE, x_connection_signal); + +#ifdef HAVE_X_SM + x_session_initialize (); +#endif } Index: src/xterm.h --- src/xterm.h.orig Tue Feb 26 22:44:13 2002 +++ src/xterm.h Tue Feb 26 22:48:49 2002 @@ -1098,3 +1098,11 @@ #ifdef USE_X_TOOLKIT extern void widget_store_internal_border P_ ((Widget)); #endif + +/* Defined in xsmfns.c */ +#ifdef HAVE_X_SM +extern void x_session_initialize P_ ((void)); +extern int x_session_check_input P_ ((struct input_event *bufp, + int *numchars)); +extern int x_session_have_connection P_ ((void)); +#endif Index: src/termhooks.h --- src/termhooks.h.orig Sat Oct 20 07:51:38 2001 +++ src/termhooks.h Tue Feb 26 22:48:49 2002 @@ -323,7 +323,11 @@ /* Queued from XTread_socket on FocusIn events. Translated into `switch-frame' events in kbd_buffer_get_event, if necessary. */ - FOCUS_IN_EVENT + FOCUS_IN_EVENT, + + /* Queued from XTread_socket when session manager sends + save yourself before shutdown. */ + save_session_event }; /* If a struct input_event has a kind which is selection_request_event Index: src/keyboard.c --- src/keyboard.c.orig Tue Feb 26 22:44:10 2002 +++ src/keyboard.c Tue Feb 26 22:48:49 2002 @@ -554,6 +554,8 @@ Lisp_Object Qlanguage_change; #endif Lisp_Object Qdrag_n_drop; +Lisp_Object Qsave_session; + /* Lisp_Object Qmouse_movement; - also an event header */ /* Properties of event headers. */ @@ -3725,6 +3727,11 @@ kbd_fetch_ptr = event + 1; } #endif + else if (event->kind == save_session_event) + { + obj = Fcons (Qsave_session, Qnil); + kbd_fetch_ptr = event + 1; + } /* Just discard these, by returning nil. With MULTI_KBOARD, these events are used as placeholders when we need to randomly delete events from the queue. @@ -5389,6 +5396,9 @@ /* A user signal. */ return *lispy_user_signals[event->code]; + case save_session_event: + return Qsave_session; + /* The 'kind' field of the event is something we don't recognize. */ default: abort (); @@ -10351,6 +10361,9 @@ Qdrag_n_drop = intern ("drag-n-drop"); staticpro (&Qdrag_n_drop); + Qsave_session = intern ("save-session"); + staticpro(&Qsave_session); + Qusr1_signal = intern ("usr1-signal"); staticpro (&Qusr1_signal); Qusr2_signal = intern ("usr2-signal"); @@ -10935,4 +10948,6 @@ "ignore-event"); initial_define_lispy_key (Vspecial_event_map, "make-frame-visible", "ignore-event"); + initial_define_lispy_key (Vspecial_event_map, "save-session", + "handle-save-session"); } Index: src/lisp.h --- src/lisp.h.orig Thu Feb 7 21:22:19 2002 +++ src/lisp.h Tue Feb 26 22:48:49 2002 @@ -3040,6 +3040,9 @@ EXFUN (Fx_file_dialog, 4); #endif /* HAVE_X_WINDOWS */ +/* Defined in xsmfns.c */ +extern void syms_of_xsmfns P_ ((void)); + /* Defined in xselect.c */ extern void syms_of_xselect P_ ((void)); Index: src/emacs.c --- src/emacs.c.orig Mon Jan 14 14:47:56 2002 +++ src/emacs.c Tue Feb 26 22:48:49 2002 @@ -1464,6 +1464,7 @@ syms_of_xterm (); syms_of_xfns (); syms_of_fontset (); + syms_of_xsmfns (); #ifdef HAVE_X11 syms_of_xselect (); #endif Index: src/config.in --- src/config.in.orig Fri Dec 28 20:06:08 2001 +++ src/config.in Tue Feb 26 22:48:49 2002 @@ -92,6 +92,9 @@ /* Define if we should use XIM, if it is available. */ #undef USE_XIM +/* Define if we have the session management (SM) library. */ +#undef HAVE_X_SM + /* Define if netdb.h declares h_errno. */ #undef HAVE_H_ERRNO Index: src/xsmfns.c --- src/xsmfns.c.orig Sun Feb 17 01:58:43 2002 +++ src/xsmfns.c Tue Feb 26 22:48:49 2002 @@ -0,0 +1,626 @@ +/* Session management module for systems which understand the X Session + management protocol. + Copyright (C) 2002 Free Software Foundation, Inc. + +This file is part of GNU Emacs. + +GNU Emacs is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Emacs is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Emacs; see the file COPYING. If not, write to +the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +Boston, MA 02111-1307, USA. */ + +#include + +#ifdef HAVE_X_SM + +#include +#ifdef HAVE_STRING_H +#include +#else +#ifdef HAVE_STRINGS_H +#include +#endif +#endif + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_STDLIB_H +#include +#endif + +#include +#include + +#include "systime.h" +#include "sysselect.h" +#include "lisp.h" +#include "termhooks.h" + +#if !defined(S_ISDIR) && defined(S_IFDIR) +#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) +#endif + +#define PATH_SEP_CHAR '/' +#define PATH_SEP_STR "/" + +#ifndef MAXPATHLEN +#define MAXPATHLEN 1024 +#endif /* not MAXPATHLEN */ + + +extern char *getenv (); + + +/* This is the event used when save_session occurs */ + +static struct input_event emacs_event; + +/* The descriptor that we use to check for data from the session manager. */ + +static int ice_fd = -1; + +/* A flag that says if we are in shutdown interactions or not. */ + +static int doing_interact = False; + +/* Initial values of argv. */ + +extern char **initial_argv; + +/* The session manager object for the session manager connection */ + +static SmcConn smc_conn; + +/* The client session id for this session */ +static char *client_id; + +/* The client session id for this session as a lisp object. */ + +Lisp_Object Vx_session_id; + +/* The id we had the previous session. This is only available if we + have been started by the session manager with SMID_OPT. */ + +Lisp_Object Vx_session_previous_id; + +/* The option we tell the session manager to start Emacs with when + restarting Emacs. The client_id is appended. */ + +#define SMID_OPT "--smid=" + + +/* Return non-zero if PATH is an executable file. */ +static int +executable_file_p (path) + char *path; +{ + struct stat status; + + return (access (path, X_OK) == 0 /* exists and is executable */ + && stat (path, &status) == 0 /* get the status */ + && (S_ISDIR (status.st_mode)) == 0); /* not a directory */ +} + +/* If FILENAME is not an absolute path, make it so by prepending + CURRENT_DIRECTORY. The return value is allocated with malloc and + the caller must free it. */ +static char* +make_absolute_path (filename, current_directory) + char *filename; + char *current_directory; +{ + if (filename[0] == PATH_SEP_CHAR || ! current_directory) + return xstrdup (filename); + + if (strchr (filename, PATH_SEP_CHAR)) + { + char *ret; + + ret = xmalloc (strlen (filename)+strlen (current_directory)+2); + strcpy (ret, current_directory); + strcat (ret, PATH_SEP_STR); + strcat (ret, filename); + + return ret; + } + + return xstrdup (filename); +} + +/* Try to find program by searching path from environment. If not found, + the CURRENT_DIRECTORY concatenated with PROGRAM is returned. + The return value is allocated with malloc and the caller must free it. */ +static char* +path_search (program, current_directory) + char *program; + char *current_directory; +{ + char* bp; + char* ep; + char *ret; + size_t proglen = strlen (program); + + if (strchr(program, PATH_SEP_CHAR) || ! (bp = getenv ("PATH"))) + return make_absolute_path (program, current_directory); + + while (bp) + { + size_t len; + + ep = strchr (bp, ':'); + if (ep == bp || bp[0] == '\0') + { + /* Empty PATH component, treat as current directory */ + bp = "."; + len = 1; + } + else if (ep) len = ep-bp; + else len = strlen (bp); + + if (proglen+len+1 <= MAXPATHLEN) + { + char maybe[MAXPATHLEN+1]; + + strncpy (maybe, bp, len); + maybe[len] = '\0'; + strcat (maybe, PATH_SEP_STR); + strcat (maybe, program); + if (executable_file_p (maybe)) + return make_absolute_path (maybe, current_directory); + } + + if (ep) bp = ep+1; + else bp = ep; + } + + return make_absolute_path (program, current_directory); +} + +/* Handle any messages from the session manager. If no connection is + open to a session manager, just return 0. + Otherwise returns the number of events stored in buffer BUFP, + which can hold up to *NUMCHARS characters. At most one event is + stored, an save_session_event. */ +int +x_session_check_input (bufp, numchars) + struct input_event *bufp; + int *numchars; +{ + SELECT_TYPE read_fds; + EMACS_TIME tmout; + + if (ice_fd == -1) return 0; + + FD_ZERO (&read_fds); + FD_SET (ice_fd, &read_fds); + + tmout.tv_sec = 0; + tmout.tv_usec = 0; + + /* Reset this so wo can check kind after callbacks have been called by + IceProcessMessages. The smc_interact_CB sets the kind to + save_session_event, but we don't know beforehand if that callback + will be called. */ + emacs_event.kind = no_event; + + if (select (ice_fd+1, &read_fds, + (SELECT_TYPE *)0, (SELECT_TYPE *)0, &tmout) < 0) + { + ice_fd = -1; + return 0; + } + + + if (FD_ISSET (ice_fd, &read_fds)) + IceProcessMessages (SmcGetIceConnection (smc_conn), + (IceReplyWaitInfo *)0, (Bool *)0); + + + /* Check if smc_interact_CB was called and we shall generate a + save_session event. */ + if (*numchars > 0 && emacs_event.kind != no_event) + { + bcopy (&emacs_event, bufp, sizeof (struct input_event)); + bufp++; + (*numchars)--; + + return 1; + } + + return 0; +} + +/* Return non-zero if we have a connection to a session manager.*/ +int +x_session_have_connection () +{ + return ice_fd != -1; +} + +/* This is called when the session manager says it is OK to interact with the + user. Here we set the kind to save_session so an event is generated. + Then lisp code can interact with the user. */ +static void +smc_interact_CB (smcConn, clientData) + SmcConn smcConn; + SmPointer clientData; +{ + doing_interact = True; + emacs_event.kind = save_session_event; +} + +/* This is called when the session manager tells us to save ourself. + We set the required properties so the session manager can restart us, + plus the current working directory property (not mandatory) so we + are started in the correct directory. + + If this is a shutdown and we can request to interact with the user, + we do so, because we don't know what the lisp code might do. */ +static void +smc_save_yourself_CB (smcConn, + clientData, + saveType, + shutdown, + interactStyle, + fast) + SmcConn smcConn; + SmPointer clientData; + int saveType; + Bool shutdown; + int interactStyle; + Bool fast; +{ +#define NR_PROPS 5 + + SmProp *props[NR_PROPS]; + SmProp prop_ptr[NR_PROPS]; + + SmPropValue values[20]; + int val_idx = 0; + int props_idx = 0; + int has_cwd; + + char cwd[MAXPATHLEN+1]; + char user_id[16]; + + char *program; + char *smid_opt; + +#ifdef HAVE_GETCWD + has_cwd = getcwd (cwd, MAXPATHLEN+1) != 0; +#else + has_cwd = getwd (cwd) != 0; +#endif + + if (has_cwd) + program = path_search (initial_argv[0], cwd); + else + program = path_search (initial_argv[0], 0); + + /* How to start a new instance of Emacs */ + props[props_idx] = &prop_ptr[props_idx]; + props[props_idx]->name = SmCloneCommand; + props[props_idx]->type = SmLISTofARRAY8; + props[props_idx]->num_vals = 1; + props[props_idx]->vals = &values[val_idx++]; + props[props_idx]->vals[0].length = strlen (program); + props[props_idx]->vals[0].value = program; + ++props_idx; + + /* The name of the program */ + props[props_idx] = &prop_ptr[props_idx]; + props[props_idx]->name = SmProgram; + props[props_idx]->type = SmARRAY8; + props[props_idx]->num_vals = 1; + props[props_idx]->vals = &values[val_idx++]; + props[props_idx]->vals[0].length = strlen (initial_argv[0]); + props[props_idx]->vals[0].value = initial_argv[0]; + ++props_idx; + + /* How to restart Emacs (i.e.: /path/to/emacs --smid=xxxx). */ + props[props_idx] = &prop_ptr[props_idx]; + props[props_idx]->name = SmRestartCommand; + props[props_idx]->type = SmLISTofARRAY8; + props[props_idx]->num_vals = 2; /* 2 values: /path/to/emacs, --smid=xxx */ + props[props_idx]->vals = &values[val_idx]; + props[props_idx]->vals[0].length = strlen (program); + props[props_idx]->vals[0].value = program; + + smid_opt = xmalloc (strlen (SMID_OPT) + strlen (client_id) + 1); + strcpy(smid_opt, SMID_OPT); + strcat(smid_opt, client_id); + + props[props_idx]->vals[1].length = strlen (smid_opt); + props[props_idx]->vals[1].value = smid_opt; + val_idx += 2; + ++props_idx; + + /* User id */ + sprintf(user_id, "%d", getpid()); + props[props_idx] = &prop_ptr[props_idx]; + props[props_idx]->name = SmUserID; + props[props_idx]->type = SmARRAY8; + props[props_idx]->num_vals = 1; + props[props_idx]->vals = &values[val_idx++]; + props[props_idx]->vals[0].length = strlen (user_id); + props[props_idx]->vals[0].value = user_id; + ++props_idx; + + /* The current directory property, not mandatory */ + if (has_cwd) + { + props[props_idx] = &prop_ptr[props_idx]; + props[props_idx]->name = SmCurrentDirectory; + props[props_idx]->type = SmARRAY8; + props[props_idx]->num_vals = 1; + props[props_idx]->vals = &values[val_idx++]; + props[props_idx]->vals[0].length = strlen (cwd); + props[props_idx]->vals[0].value = cwd; + ++props_idx; + } + + + SmcSetProperties (smcConn, props_idx, props); + + xfree (program); + xfree (smid_opt); + + /* See if we maybe shall interact with the user. */ + if (interactStyle != SmInteractStyleAny + || ! shutdown + || saveType == SmSaveLocal + || ! SmcInteractRequest(smcConn, SmDialogNormal, smc_interact_CB, 0)) + { + /* No interaction, we are done saving ourself. */ + SmcSaveYourselfDone (smcConn, True); + } +} + +/* According to the SM specification, this shall close the connection */ +static void +smc_die_CB (smcConn, clientData) + SmcConn smcConn; + SmPointer clientData; +{ + SmcCloseConnection (smcConn, 0, 0); + ice_fd = -1; +} + +/* We don't use the next two but they are mandatory, leave them empty. + According to the SM specification, we should not interact with the + user between smc_save_yourself_CB is called and until smc_save_complete_CB + is called. It seems like a lot of job to implement this and it doesn't + even seem necessary. */ +static void +smc_save_complete_CB (smcConn, clientData) + SmcConn smcConn; + SmPointer clientData; +{ + /* Empty */ +} + +static void +smc_shutdown_cancelled_CB (smcConn, clientData) + SmcConn smcConn; + SmPointer clientData; +{ + /* Empty */ +} + +/* Error handlers for SM and ICE. We don't wan't to exit Emacs just + because there is some error in the session management. */ +static void +smc_error_handler (smcConn, + swap, + offendingMinorOpcode, + offendingSequence, + errorClass, + severity, + values) + SmcConn smcConn; + Bool swap; + int offendingMinorOpcode; + unsigned long offendingSequence; + int errorClass; + int severity; + SmPointer values; +{ + /* Empty */ +} + +static void +ice_error_handler (iceConn, + swap, + offendingMinorOpcode, + offendingSequence, + errorClass, + severity, + values) + IceConn iceConn; + Bool swap; + int offendingMinorOpcode; + unsigned long offendingSequence; + int errorClass; + int severity; + IcePointer values; +{ + /* Empty */ +} + + +static void +ice_io_error_handler (iceConn) + IceConn iceConn; +{ + /* Connection probably gone. */ + ice_fd = -1; +} + +/* This is called when the ICE connection is created or closed. The SM library + uses ICE as it transport protocol. */ +static void +ice_conn_watch_CB (iceConn, clientData, opening, watchData) + IceConn iceConn; + IcePointer clientData; + Bool opening; + IcePointer *watchData; +{ + if (! opening) + { + ice_fd = -1; + return; + } + + ice_fd = IceConnectionNumber (iceConn); +#ifndef F_SETOWN_BUG +#ifdef F_SETOWN +#ifdef F_SETOWN_SOCK_NEG + /* stdin is a socket here */ + fcntl (ice_fd, F_SETOWN, -getpid ()); +#else /* ! defined (F_SETOWN_SOCK_NEG) */ + fcntl (ice_fd, F_SETOWN, getpid ()); +#endif /* ! defined (F_SETOWN_SOCK_NEG) */ +#endif /* ! defined (F_SETOWN) */ +#endif /* F_SETOWN_BUG */ + +#ifdef SIGIO + if (interrupt_input) + init_sigio (ice_fd); +#endif /* ! defined (SIGIO) */ +} + +/* Try to open a connection to the session manager. */ +void +x_session_initialize () +{ +#define SM_ERRORSTRING_LEN 512 + char errorstring[SM_ERRORSTRING_LEN]; + char* previous_id = NULL; + SmcCallbacks callbacks; + + /* Check if we where started by the session manager. If so, we will + have a previous id. */ + if (! EQ (Vx_session_previous_id, Qnil) && STRINGP (Vx_session_previous_id)) + previous_id = XSTRING (Vx_session_previous_id)->data; + + /* The SM protocol says all callbacks are mandatory, so set up all + here and in the mask passed to SmcOpenConnection */ + callbacks.save_yourself.callback = smc_save_yourself_CB; + callbacks.save_yourself.client_data = 0; + callbacks.die.callback = smc_die_CB; + callbacks.die.client_data = 0; + callbacks.save_complete.callback = smc_save_complete_CB; + callbacks.save_complete.client_data = 0; + callbacks.shutdown_cancelled.callback = smc_shutdown_cancelled_CB; + callbacks.shutdown_cancelled.client_data = 0; + + /* Set error handlers. */ + SmcSetErrorHandler (smc_error_handler); + IceSetErrorHandler (ice_error_handler); + IceSetIOErrorHandler (ice_io_error_handler); + + /* Install callback for when connection status changes. */ + IceAddConnectionWatch (ice_conn_watch_CB, 0); + + /* Open the connection to the session manager. A failure is not + critical, it usualy means that no session manager is running. + The errorstring is here for debugging. */ + smc_conn = SmcOpenConnection (NULL, NULL, 1, 0, + (SmcSaveYourselfProcMask| + SmcDieProcMask| + SmcSaveCompleteProcMask| + SmcShutdownCancelledProcMask), + &callbacks, + previous_id, + &client_id, + SM_ERRORSTRING_LEN, + errorstring); + + if (smc_conn != 0) + Vx_session_id = make_string (client_id, strlen (client_id)); +} + + +DEFUN ("handle-save-session", Fhandle_save_session, + Shandle_save_session, 1, 1, "e", + doc: /* Handle the save_yourself event from a session manager. +A session manager can tell Emacs that the window system is shutting down +by sending Emacs a save_yourself message. Emacs executes this function when +such an event occurs. This function then executes `emacs-session-save'. +After that, this function informs the session manager that it can continue +or abort shutting down the window system depending on the return value +from `emacs-session-save' If the return value is non-nil the session manager +is told to abort the window system shutdown. + +Do not call this function yourself. */) + (event) + Lisp_Object event; +{ + /* Check doing_interact so that we don't do anything if someone called + this at the wrong time. */ + if (doing_interact) + { + Bool cancel_shutdown = False; + + cancel_shutdown = ! EQ (call0 (intern ("emacs-session-save")), Qnil); + + SmcInteractDone (smc_conn, cancel_shutdown); + SmcSaveYourselfDone (smc_conn, True); + + doing_interact = False; + } +} + + +/*********************************************************************** + Initialization + ***********************************************************************/ +void +syms_of_xsmfns () +{ + DEFVAR_LISP ("x-session-id", &Vx_session_id, + doc: /* The session id Emacs got from the session manager for this session. +Changing the value does not change the session id used by Emacs. +The value is nil if no session manager is running. +See also `x-session-previous-id', `emacs-save-session-functions', +`emacs-session-save' and `emacs-session-restore'." */); + Vx_session_id = Qnil; + + DEFVAR_LISP ("x-session-previous-id", &Vx_session_previous_id, + doc: /* The previous session id Emacs got from session manager. +If Emacs is running on a window system that has a session manager, the +session manager gives Emacs a session id. It is feasible for Emacs lisp +code to use the session id to save configuration in, for example, a file +with a file name based on the session id. If Emacs is running when the +window system is shut down, the session manager remembers that Emacs was +running and saves the session id Emacs had. + +When the window system is started again, the session manager restarts +Emacs and hands Emacs the session id it had the last time it was +running. This is now the previous session id and the value of this +variable. If configuration was saved in a file as stated above, the +previous session id shall be used to reconstruct the file name. + +The session id Emacs has while it is running is in the variable +`x-session-id'. The value of this variable and `x-session-id' may be the +same, depending on how the session manager works. + +See also `emacs-save-session-functions', `emacs-session-save' and +`emacs-session-restore'." */); + Vx_session_previous_id = Qnil; + + defsubr (&Shandle_save_session); +} + +#endif /* HAVE_X_SM */ Index: configure.in --- configure.in.orig Sun Jan 27 11:03:14 2002 +++ configure.in Tue Feb 26 22:48:49 2002 @@ -1915,6 +1915,21 @@ fi fi +### Use session management (-lSM -lICE) if available +HAVE_X_SM=no +if test "${HAVE_X11}" = "yes"; then + AC_CHECK_HEADER(X11/SM/SMlib.h, + AC_CHECK_LIB(SM, SmcOpenConnection, HAVE_X_SM=yes, -lICE)) + + if test "${HAVE_X_SM}" = "yes"; then + AC_DEFINE(HAVE_X_SM) + case "$LIBS" in + *-lSM*) ;; + *) LIBS="-lSM -lICE $LIBS" ;; + esac + fi +fi + # If netdb.h doesn't declare h_errno, we must declare it by hand. AC_CACHE_CHECK(whether netdb declares h_errno, emacs_cv_netdb_declares_h_errno, _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://mail.gnu.org/mailman/listinfo/emacs-devel