-
Notifications
You must be signed in to change notification settings - Fork 137
/
end-session.lisp
92 lines (79 loc) · 3.56 KB
/
end-session.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
;;;; 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.
;;;
(defvar *end-session-command* "systemctl")
(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))))
(defcommand suspend-computer () ()
"Suspends the computer"
(let ((choice (yes-no-diag "Really suspend?")))
(when choice
(echo-string (current-screen) "Suspending...")
(run-shell-command (concat *end-session-command* " suspend")))))
(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 (concat *end-session-command* " 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 (concat *end-session-command* " reboot")))))
(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)
(list "Suspend" #'suspend-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)")