Add end-session module

+ see the README.org for details
This commit is contained in:
sdilts 2018-02-15 13:53:11 -07:00
parent a22fe1b29b
commit 99b25d625e
5 changed files with 118 additions and 0 deletions

View file

@ -67,6 +67,7 @@ to the world!
- [[./modeline/net/README.org][net]]
- [[./modeline/wifi/README.org][wifi]]
** Utilities
- [[./util/end-session/README.org][end-session]]
- [[./util/app-menu/README.org][app-menu]]
- [[./util/numpad-layouts/README.org][numpad-layouts]]
- [[./util/app-menu/README.org][menu]]

View file

@ -0,0 +1,21 @@
** end-session
This module provides commands to gracefully close all open programs
and shutdown or reboot the computer. A logout command is also
provided.
This module requires the use of =systemD=, and requires the =polkit=
package to be installed, as well as the =wmctl= command.
** Usage
Add these lines to your =.stumprc= file:
#+BEGIN_SRC lisp
;; if you haven't added to load path yet:
(add-to-load-path #p"path-to-contrib/util/end-session")
;; actually load the module
(load-module "end-session")
#+END_SRC
*** Commands Provided:
- =end-session= Prompts for shutdown, restart, or logoff. You can
customize what this shows with =*end-session-menu*=. See [[file:session-ending.lisp::77]]
- shutdown-computer
- restart-computer
- logout

View file

@ -0,0 +1,10 @@
;;;; end-session.asd
(asdf:defsystem #:end-session
:description "Provides commands to stumpwm that allow the user to shutdown, restart, and logoff through the stumpwm UI"
:author "Stuart Dilts"
:license "GPLv3"
:depends-on (#:stumpwm)
:serial t
:components ((:file "package")
(:file "end-session")))

View file

@ -0,0 +1,82 @@
;;;; session-ending.lisp
(in-package #:end-session)
;;; Commands to exit out of stumpwm whie closing open programs nicely
;;;
;;; Copyright (c) 2018 Stuart Dilts
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.
;;;
(defun yes-no-diag (query-string)
"Presents a yes-no dialog to the user asking query-string.
Returns true when yes is selected"
(equal :yes (cadr (select-from-menu (current-screen)
'(("No" :no) ("Yes" :yes))
query-string))))
(defun close-all-apps ()
"Closes all windows managed by stumpwm gracefully"
;; yes, this uses an external tool instead of stumpwm internals
(let ((win-index-text (run-shell-command "wmctrl -l | awk '{print $1}'" t)))
(dolist (window (cl-ppcre:split "\\\n" win-index-text))
(run-shell-command (format nil "wmctrl -i -c ~A" window)))))
(defcommand shutdown-computer () ()
(let ((choice (yes-no-diag "Really Shutdown? (All programs will be closed)")))
(when choice
(echo-string (current-screen) "Shutting down...")
(close-all-apps)
(run-hook *quit-hook*)
(run-shell-command "systemctl poweroff"))))
;; can't name the function "restart"
(defcommand restart-computer () ()
(let ((choice (yes-no-diag "Really Restart? (All programs will be closed)")))
(when choice
(echo-string (current-screen) "Restarting...")
(close-all-apps)
(run-hook *quit-hook*)
(run-shell-command "systemctl restart"))))
(defcommand logout () ()
(let ((choice (yes-no-diag "Close all programs and quit stumpwm?")))
(when choice
(echo-string (current-screen) "Ending Session...")
(close-all-apps)
(run-hook *quit-hook*)
(quit))))
(defcommand end-session () ()
(let ((choice (select-from-menu (current-screen) *end-session-menu*
"Quit Session?")))
(when choice
(apply (second choice) nil))))
(defvar *end-session-menu*
(list (list "Logout" #'logout)
(list "Shutdown" #'shutdown-computer)
(list "Restart" #'restart-computer))
"The options that are available to quit a stumpwm session.
Entries in the list has the format of (\"item in menu\" #'function-to-call)")

View file

@ -0,0 +1,4 @@
;;;; package.lisp
(defpackage #:end-session
(:use #:cl :stumpwm))