unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Yet another session management patch.
@ 2002-03-04 19:01 Jan D.
  2002-03-05 19:07 ` Richard Stallman
  0 siblings, 1 reply; 2+ messages in thread
From: Jan D. @ 2002-03-04 19:01 UTC (permalink / raw)


Hello.

I've made some small changes, and if no one objects I was thinking of
checking this in.  It still doesn't save anything, but it restarts Emacs
at least.

Also, I am writing documentation, but I have trouble selecting a place for
it.  The best suggestion I have is Elisp manual, "Operating system interface".
Any better ideas?

	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	Sat Mar  2 19:28:21 2002
***************
*** 234,239 ****
--- 234,307 ----
  	    (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 "session." session-id))
+ 	(emacs-dir "~/.emacs.d/"))
+     (expand-file-name (if (file-directory-p emacs-dir)
+ 			  (concat emacs-dir basename)
+ 			(concat "~/.emacs-" basename)))))
+ 	
+     (defun emacs-session-save ()
+   "This function is called when the window system is shutting down.
+ If this function returns non-nil, the window system shutdown is cancelled.
+ 
+ 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"))))
+ 
+  
+   
  \f
  ;;
  ;; 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	Sat Mar  2 12:34:25 2002
***************
*** 236,242 ****
      ("--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))
    "Alist of X Windows options.
  Each element has the form
    (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
--- 236,243 ----
      ("--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)
!     ("--smid" 1 x-handle-smid))
    "Alist of X Windows options.
  Each element has the form
    (NAME NUMARGS HANDLER FRAME-PARAM VALUE)
***************
*** 1028,1034 ****
    (command-line-1 (cdr command-line-args))
  
    ;; If -batch, terminate after processing the command options.
!   (if noninteractive (kill-emacs t)))
  
  (defcustom initial-scratch-message (purecopy "\
  ;; This buffer is for notes you don't want to save, and for Lisp evaluation.
--- 1029,1040 ----
    (command-line-1 (cdr command-line-args))
  
    ;; If -batch, terminate after processing the command options.
!   (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	Sat Mar  2 12:34:25 2002
***************
*** 294,300 ****
  #ifdef HAVE_MENUS
  
  /* Include xmenu.o in the list of X object files.  */
! XOBJ= xterm.o xfns.o xselect.o xrdb.o fontset.o
  
  /* The X Menu stuff is present in the X10 distribution, but missing
     from X11.  If we have X10, just use the installed library;
--- 294,300 ----
  #ifdef HAVE_MENUS
  
  /* Include xmenu.o in the list of X object files.  */
! 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,321 ****
  
  /* 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
  LIBXMENU=
  #endif /* not HAVE_MENUS */
  
--- 315,321 ----
  
  /* 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 xsmfns.o
  LIBXMENU=
  #endif /* not HAVE_MENUS */
  
***************
*** 366,372 ****
--- 366,376 ----
  #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,555 ****
     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
  
  
  #ifdef TERMINFO
--- 553,559 ----
     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 xsmfns.o
  
  
  #ifdef TERMINFO
***************
*** 1128,1133 ****
--- 1132,1138 ----
  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	Sat Mar  2 12:33:56 2002
--- src/xterm.c	Sat Mar  2 12:34:25 2002
***************
*** 10032,10037 ****
--- 10032,10043 ----
  	  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,10124 ****
  			   the session manager, who's looking for such a
  			   PropertyNotify.  Can restart processing when
  			   a keyboard or mouse event arrives.  */
! 			if (numchars > 0)
  			  {
  			    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 ())
--- 10120,10136 ----
  			   the session manager, who's looking for such a
  			   PropertyNotify.  Can restart processing when
  			   a keyboard or mouse event arrives.  */
!                         /* 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 ())
***************
*** 15056,15061 ****
--- 15068,15077 ----
  #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	Sat Mar  2 12:34:25 2002
***************
*** 1098,1100 ****
--- 1098,1108 ----
  #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	Sat Mar  2 12:34:25 2002
***************
*** 323,329 ****
  
    /* Queued from XTread_socket on FocusIn events.  Translated into
       `switch-frame' events in kbd_buffer_get_event, if necessary.  */
!   FOCUS_IN_EVENT
  };
  
  /* If a struct input_event has a kind which is selection_request_event
--- 323,333 ----
  
    /* Queued from XTread_socket on FocusIn events.  Translated into
       `switch-frame' events in kbd_buffer_get_event, if necessary.  */
!   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	Sat Mar  2 12:34:25 2002
***************
*** 554,559 ****
--- 554,561 ----
  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,3730 ****
--- 3727,3737 ----
  	  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,5394 ****
--- 5396,5404 ----
        /* 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,10356 ****
--- 10361,10369 ----
    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,10938 ****
--- 10948,10953 ----
  			    "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	Sat Mar  2 12:33:51 2002
--- src/lisp.h	Sat Mar  2 12:34:25 2002
***************
*** 3062,3067 ****
--- 3062,3070 ----
  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	Sat Mar  2 12:34:25 2002
***************
*** 1464,1469 ****
--- 1464,1470 ----
        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	Sat Mar  2 12:34:25 2002
***************
*** 92,97 ****
--- 92,100 ----
  /* 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	Sat Mar  2 19:53:10 2002
***************
*** 0 ****
--- 1,533 ----
+ /* 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 <config.h>
+ 
+ #ifdef HAVE_X_SM
+ 
+ #include <X11/SM/SMlib.h>
+ #ifdef HAVE_STRING_H
+ #include <string.h>
+ #else
+ #ifdef HAVE_STRINGS_H
+ #include <strings.h>
+ #endif
+ #endif
+ 
+ #ifdef HAVE_UNISTD_H
+ #include <unistd.h>
+ #endif
+ #ifdef HAVE_STDLIB_H
+ #include <stdlib.h>
+ #endif
+ 
+ #include <sys/param.h>
+ 
+ #include "systime.h"
+ #include "sysselect.h"
+ #include "lisp.h"
+ #include "termhooks.h"
+ 
+ #ifndef MAXPATHLEN
+ #define MAXPATHLEN 1024
+ #endif /* not MAXPATHLEN */
+ 
+ 
+ /* The user login name.  */
+ 
+ extern Lisp_Object Vuser_login_name;
+ 
+ /* 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;
+ 
+ /* 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 full path name to the Emacs program */
+ static char *emacs_program;
+ 
+ /* 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="
+ 
+ 
+ /* 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;
+   
+   char cwd[MAXPATHLEN+1];
+   char *smid_opt;
+ 
+   /* 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 (emacs_program);
+   props[props_idx]->vals[0].value = emacs_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 (XSTRING (Vinvocation_name)->data);
+   props[props_idx]->vals[0].value = XSTRING (Vinvocation_name)->data;
+   ++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 (emacs_program);
+   props[props_idx]->vals[0].value = emacs_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 */
+   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 (XSTRING (Vuser_login_name)->data);
+   props[props_idx]->vals[0].value = XSTRING (Vuser_login_name)->data;
+   ++props_idx;
+ 
+   /* The current directory property, not mandatory */
+ #ifdef HAVE_GETCWD
+   if (getcwd (cwd, MAXPATHLEN+1) != 0)
+ #else
+   if (getwd (cwd) != 0)
+ #endif
+     {
+       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 (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;
+   int  name_len = 0;
+   
+   /* 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;
+ 
+   /* Construct the path to the Emacs program. */
+   if (! EQ (Vinvocation_directory, Qnil))
+     name_len += strlen (XSTRING (Vinvocation_directory)->data);
+   name_len += strlen (XSTRING (Vinvocation_name)->data);
+ 
+   /* This malloc will not be freed, but it is only done once, and hopefully
+      not very large  */
+   emacs_program = xmalloc (name_len + 1);
+   emacs_program[0] = '\0';
+ 
+   if (! EQ (Vinvocation_directory, Qnil))
+     strcpy (emacs_program, XSTRING (Vinvocation_directory)->data);
+   strcat (emacs_program, XSTRING (Vinvocation_name)->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;
+     }
+ }
+ 
+ \f
+ /***********************************************************************
+ 			    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	Sat Mar  2 12:32:34 2002
--- configure.in	Sat Mar  2 12:34:25 2002
***************
*** 1935,1940 ****
--- 1935,1955 ----
    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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-03-05 19:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-03-04 19:01 Yet another session management patch Jan D.
2002-03-05 19:07 ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).