Source

xwem / lisp / xwem-main.el

Full commit
;;; xwem-main.el --- Main part of xwem.

;; Copyright (C) 2003 by Free Software Foundation, Inc.

;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; Created: 21 Mar 2003
;; Keywords: xlib, xwem
;; X-CVS: $Id$

;; This file is part of XWEM.

;; XWEM 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.

;; XWEM 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 XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not in FSF

;; Commentary:
;;
;; This main part of XWEM.
;;
;; I strongly recommend you to raise max-lisp-eval-depth value to say
;; 5000.
;; 	(setq max-lisp-eval-depth 5000)
;;
;; Try to avoid to use such evil thing as `mouse-avoidance-mode', but
;; if you really want it, than set it to either 'banish or 'jump.
;;
;; If you want develop some xwem addons or take in touch with xwem, it
;; will be usefull to change `find-function-regexp', because xwem uses
;; its own syntax to define interactive commands.

;;     (setq find-function-regexp
;;    	     (concat "^\\s-*(\\(def[^cgvW]\\w+\\*?"
;;    		  "\\|define-function"
;;    		  "\\|define-obsolete-function-alias"
;;    		  "\\|define-compatible-function-alias"
;;    		  "\\|define-derived-mode"
;;    		  "\\|define-xwem-command"
;;    		  "\\)\\s-+%s\\(\\s-\\|$\\)"))

;;; Code



(eval-when-compile
  (require 'cl))			;last, intersection etc

;;;###autoload
(defconst xwem-version "xwem(xemacs-package):  $Revision$")

(defgroup xwem nil
  "XWEM window manager."
  :prefix "xwem-"
  :group 'applications)

(defgroup xwem-hooks nil
  "Group to customize xwem hooks."
  :prefix "xwem-"
  :group 'xwem)

;;;###autoload
(defcustom xwem-dir "~/.xwem"
  "Directory to store XWEM's files."
  :type 'string
  :group 'xwem)

;;;###autoload
(defcustom xwem-debug nil
  "*Non-nil mean run xlib and xwem in debugging mode."
  :type 'boolean
  :group 'xwem)

;;;###autoload
(defcustom xwem-commands-inhibit-gc t
  "*Non-nil mean that xwem interactive commands runs without GCing."
  :type 'boolean
  :group 'xwem)

(defcustom xwem-custom-display nil ;"127.0.0.1:2"
  "*Custom display, mostly for debugging purposes."
  :type 'string
  :group 'xwem)

;;;###autoload
(defcustom xwem-load-hook nil
  "*Hooks to call after xwem was load."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-after-init-hook nil
  "Hooks to be runned after xwem initialisation."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-exit-hook nil
  "Hooks called after xwem exit."
  :type 'hook
  :group 'xwem-hooks)


;;; Variables
;;;###autoload
(defvar xwem-started nil
  "Non-nil when xwem started.
Do not modify!")


;;; Functions
(defun xwem-init-wins ()
  "Manage all mapped X windows."
  (xwem-message 'msg "Initializing X windows ... wait")

  (let ((wins (XQueryTree (xwem-dpy) (xwem-rootwin)))
	(attrs nil)
	(shcfgl nil))
    (setq wins (cdr (cdr (cdr (cdr wins)))))

    (X-Dpy-log (xwem-dpy) "IN xwem-init-wins: wins length = %d\n" '(length wins))
    (while wins
      (X-Dpy-log (xwem-dpy) "XGetWindowAttr BEGIN in xwem-init-wins\n")
      (setq attrs (XGetWindowAttributes (xwem-dpy) (car wins)))
      (X-Dpy-log (xwem-dpy) "XGetWindowAttr END in xwem-init-wins, as=%s\n" 'attrs)

      (when (and (not (X-Attr-override-redirect attrs))
		 (= (X-Attr-mapstate attrs) X-Viewable)
		 (not (xwem-find-frame (car wins))))
	;; X window visible and not XWEM frame
	(setq shcfgl (cons (cons (car wins) attrs) shcfgl))
	)

      (setq wins (cdr wins)))
    
    ;; Manage all visible clients
    (mapc (lambda (cl)
	    (xwem-make-client (car cl) (cdr cl)))
	  shcfgl)
    ))

(defun xwem-after-window-setup ()
  "Function which will be added to `window-setup-hook' and called
  after ~/.emacs file loaded and emacs X window subsystems
  initialized."

  ;; revert back `menubar-visible-p' specifier
  (set-specifier menubar-visible-p xwem-saved-menubar-visible-p)

  (let ((dfen (or xwem-custom-display (getenv "DISPLAY"))))
    (xwem-init-root
     (if (eq (aref dfen 0) ?\:)
	 (concat "127.0.0.1" dfen)
       dfen)))

  ;; Select input on root window
  (XSelectInput (xwem-dpy) (xwem-rootwin) xwem-root-ev-mask)
  (X-Win-EventHandler-add-new (xwem-rootwin) 'xwem-root-events-handler 100)

  ;; Create XWEM minibuffer
  (xwem-minib-create)

  (xwem-init-cursors)
  (xwem-init-faces)
  (xwem-init-events)
  (xwem-kbd-init)
  (xwem-init-frames)
  (xwem-init-win)
  (xwem-init-misc)

  ;; Handle all X clients
  (xwem-init-wins)

  (when xwem-strokes-enabled
    (xwem-strokes-init))

  ;; Initialize xwem system tray
  (when xwem-tray-enabled
    (xwem-tray-startit (xwem-dpy)))

  ;; Refit frames, so they will adjust sizes according to
  ;; xwem-minibuffer size, after it was managed.
  (mapc 'xwem-frame-fit-screen xwem-frames-list)

  ;; Make sure all events are processes
  (XSync (xwem-dpy))
  (XSync (xwem-dpy) t)

  (setq xwem-started t)

  ;; Now xwem is fully intialized and it is time to run hooks
  (run-hooks 'xwem-after-init-hook)

  (xwem-message 'asis (concat (xwem-logo-string)
			      " succesfully started. Start with `M-x xwem-help RET'."))
  )

;;;###autoload
(defun xwem-init ()
  "Initialization of xwem subsystems."

  (setq inhibit-startup-message t)

  ;; read configuration
  (let ((cfg (expand-file-name (concat xwem-dir "/xwemrc.el"))))
    (if (file-exists-p cfg)
	(load cfg)
      (xwem-message 'warn "Configuration file `%s' does not exists" cfg)))

  ;; Initialize various stuff that does not need display
  (xwem-manda-init)

  (add-hook 'window-setup-hook 'xwem-after-window-setup)
  (add-hook 'kill-emacs-hook 'xwem-fini t)
  )

;;;###autoload
(defun xwem-fini ()
  "Fini all subsystems."
  (xwem-kbd-quit)
  (xwem-fini-events)
  (xwem-fini-frames)
  (xwem-fini-clients)
  (when xwem-tray-enabled
    (xwem-tray-fini))
  (xwem-fini-root)

  ;; Finally run exit hooks
  (run-hooks 'xwem-exit-hook))


(provide 'xwem-main)

;;; xwem-main.el ends here