Skip to content
This repository
Browse code

Major restructuring.

classify menu items into console programs and X programs.
Console programs are directly `exec'ed.
X programs are run with cdm-xlaunch(1).
$allowconsole, $usexinit, shutdownmenu(), etc., are removed.
  • Loading branch information...
commit 4b0e9825ebc50525550c9d499244e680d06b0a94 1 parent f54e4a2
Casper Ti. Vector authored April 07, 2012
1  install
@@ -12,3 +12,4 @@ else
12 12
 	install -Dm755 zzz-cdm-profile.sh /etc/profile.d/zzz-cdm-profile.sh
13 13
 	cp -Rf themes /usr/share/cdm/
14 14
 fi
  15
+
242  src/cdm
@@ -27,18 +27,13 @@
27 27
 #   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 28
 #   MA 02110-1301, USA.
29 29
 
30  
-name="Console Display Manager"
31  
-ver="0.5.3"
  30
+name=$(basename "$0")
  31
+longname='Console Display Manager'
  32
+ver='0.5.3'
32 33
 
33 34
 trap '' SIGINT SIGTSTP
34 35
 source /etc/init.d/functions.sh
35 36
 
36  
-dialogrc=${dialogrc:-}
37  
-userclientrc=$HOME/.xinitrc
38  
-sysclientrc=/etc/X11/xinit/xinitrc
39  
-# userserverrc=$HOME/.xserverrc
40  
-# sysserverrc=/etc/X11/xinit/xserverrc
41  
-
42 37
 # Source cdm configurations.
43 38
 
44 39
 if [[ -n "$1" ]]; then
@@ -49,159 +44,108 @@ elif [[ -f /etc/cdmrc ]]; then
49 44
 	source /etc/cdmrc
50 45
 fi
51 46
 
52  
-# Offer all available sessions in /etc/X11/Sessions
53  
-# if wmbinlist if not explicitly set in cdmrc
  47
+# Default options.
54 48
 
55  
-if [[ "${#wmbinlist[@]}" -eq 0 ]]; then
56  
-    wmbinlist=($(ls /etc/X11/Sessions))
57  
-    wmdisplist=($(echo ${wmbinlist[@]^}))
  49
+dialogrc=${dialogrc:-}
  50
+countfrom=${countfrom:-0}
  51
+binlist=${binlist:-()}
  52
+namelist=${namelist:-()}
  53
+xtty=${xtty:-7}
  54
+locktty=${locktty:-no}
  55
+display=${display:-0}
  56
+consolekit=${consolekit:-yes}
  57
+cktimeout=${cktimeout:-30}
  58
+serverargs=${serverargs:-'-nolisten tcp'}
  59
+
  60
+# Exit functions.
  61
+
  62
+exitnormal() { exit 0; }
  63
+exiterror() { sleep 1; exit 1; }
  64
+
  65
+# Offer all available sessions in /etc/X11/Sessions,
  66
+# if binlist if not explicitly set in cdmrc.
  67
+
  68
+if [[ "${#binlist[@]}" == 0 ]]; then
  69
+	binlist=($(ls /etc/X11/Sessions))
  70
+	flaglist=($(sed 's/[[:digit:]]\+/X/g' <<< ${!flaglist[*]}))
  71
+	namelist=(${binlist[@]^})
58 72
 fi
59 73
 
60  
-mainmenu() {
61  
-	# Generate main menu
62  
-	count=0
63  
-	menu=$(
64  
-		while [[ ${count} -lt ${#wmdisplist[@]} ]]; do
65  
-			echo -ne "$((count+countfrom)) ${wmdisplist[${count}]} "
66  
-			let count=count+1
67  
-		done
  74
+# Generate the main menu.
  75
+menu=()
  76
+for ((count=0; count<${#namelist[@]}; count++)); do
  77
+	menu=("${menu[@]}" "$((count+countfrom))" "${namelist[${count}]}")
  78
+done
  79
+
  80
+# Override dialog display if only one option is available.
  81
+if [[ "$count" == 1 ]]; then
  82
+	binindex=0
  83
+else
  84
+	# Display selection dialog.
  85
+	binindex=$(
  86
+		DIALOGRC="$dialogrc" dialog --colors --stdout \
  87
+		--backtitle "${longname} v${ver}" \
  88
+		--ok-label ' Select ' --cancel-label ' Exit ' \
  89
+		--menu 'Select session' 0 0 0 "${menu[@]}"
68 90
 	)
69  
-
70  
-	# Check if console access is allowed
71  
-	if $(yesno allowconsole); then
72  
-		if ! $(yesno allowshutdown); then
73  
-			let halt=99
74  
-		fi
75  
-		let console=${#wmdisplist[@]}+countfrom
76  
-		menu="${menu} ${console} Console "
77  
-	fi
78  
-
79  
-	# Check if shutdown access is allowed
80  
-	if $(yesno allowshutdown); then
81  
-		if ! $(yesno allowconsole); then
82  
-			let halt=${#wmdisplist[@]}
83  
-		else
84  
-			let halt=${#wmdisplist[@]}+1
85  
-		fi
86  
-		let halt=halt+countfrom
87  
-		menu="${menu} ${halt} Shutdown "
  91
+	if [[ $? != 0 ]]; then
  92
+		clear
  93
+		exitnormal
88 94
 	fi
  95
+fi
89 96
 
90  
-	# Override dialog display if only one option is available
91  
-	if ! $(yesno allowconsole) && ! $(yesno allowshutdown) && [[ ${#wmdisplist[@]} == 1 ]]; then
92  
-		wm=$countfrom
93  
-	else
94  
-		# Display selection dialog
95  
-		wm=$(
96  
-			DIALOGRC="$dialogrc" dialog --colors \
97  
-			--backtitle "${name} v${ver}" --stdout \
98  
-			--ok-label " Select " --cancel-label " Logout " \
99  
-			--menu "Select Window Manager" 0 0 0 ${menu}
100  
-		)
101  
-		if [[ $? != 0 ]]; then
102  
-			clear
103  
-			exit 0
104  
-		fi
105  
-	fi
  97
+# Run $bin according to its flag.
  98
+let binindex-=countfrom
  99
+bin="${binlist[${binindex}]}"
  100
+case ${flaglist[$binindex]} in
  101
+	# *C*onsole programs.
  102
+	[Cc])
  103
+		clear
  104
+		# If $bin is a login shell, it might `exec' cdm again, causing an endless
  105
+		# loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and
  106
+		# only let the shell automatically `exec' cdm when $CDM_SPAWN is not set.
  107
+		# See also the example shell profile file shipped with this cdm package.
  108
+		CDM_SPAWN=$$ exec $bin
  109
+		;;
  110
+
  111
+	# *X* programs.
  112
+	[Xx])
  113
+		clear
106 114
 
107  
-	# Set wm_bin
108  
-	clear
109  
-	if [[ ${wm} -eq ${console} ]]; then
110  
-		exit 2 #2 signals to the profile file to not exit.
111  
-	elif [[ ${wm} -eq ${halt} ]]; then
112  
-		shutdownmenu
113  
-	else
114  
-		let wm=wm-countfrom
115  
-		wm_bin="${wmbinlist[${wm}]}"
116  
-		xstart
117  
-	fi
118  
-}
119  
-
120  
-shutdownmenu() {
121  
-	count=$countfrom
122  
-	haltmenu=$(
123  
-		for opt in Shutdown Reboot Suspend Hibernate; do
124  
-			# Check if suspend/hibernate is enabled
125  
-			if ([[ $opt != Suspend ]] && [[ $opt != Hibernate ]]) ||
126  
-				([[ $opt == Suspend ]] && $(yesno allowsuspend)) ||
127  
-				([[ $opt == Hibernate ]] && $(yesno allowhibernate)); then
128  
-				echo -ne "$count $opt "
129  
-				let count=count+1
  115
+		# If X is already running and locktty=yes, activate it,
  116
+		# otherwise increment.
  117
+		if $(yesno locktty); then
  118
+			# Activate existing X session.
  119
+			if xdpyinfo -display ":$display.0" &> /dev/null; then
  120
+				chvt "$((display+xtty))"
  121
+				exitnormal
130 122
 			fi
131  
-		done
132  
-	)
133  
-
134  
-	# Display shutdown dialog
135  
-	haltopt=$(
136  
-		DIALOGRC="$dialogrc" dialog --colors \
137  
-		--backtitle "${name} v${ver}" --stdout \
138  
-		--ok-label " Select " --cancel-label " Cancel " \
139  
-		--menu "Shutdown" 0 0 0 ${haltmenu}
140  
-	)
141  
-
142  
-	if [[ $? == 0 ]]; then
143  
-		clear
144  
-		if [[ ${haltopt} -eq $countfrom ]]; then
145  
-			${shutdowncommand}
146  
-		elif [[ ${haltopt} -eq $((countfrom+1)) ]]; then
147  
-			${rebootcommand}
148  
-		elif [[ ${haltopt} -eq $((countfrom+2)) ]] && $(yesno allowsuspend); then
149  
-			${suspendcommand}
150 123
 		else
151  
-			${hibernatecommand}
152  
-		fi
153  
-	else
154  
-		mainmenu
155  
-	fi
156  
-}
157  
-
158  
-xstart() {
159  
-	# If X is already running and locktty=yes, activate it,
160  
-	# otherwise, increment.
161  
-	if $(yesno locktty); then
162  
-		# Verify display exists
163  
-		if [[ -n "${display//[0-9]/}" ]]; then
  124
+			# Get the first empty display.
164 125
 			display=0
  126
+			while [[ "$display" < 7 ]]; do
  127
+				if dpyinfo=$(xdpyinfo -display ":$display.0" 2>&1 1>/dev/null) ||
  128
+					# Display is in use by another user.
  129
+					[[ "$dpyinfo" == 'No protocol specified'* ]]
  130
+				then
  131
+					let display+=1
  132
+				else
  133
+					break
  134
+				fi
  135
+			done
165 136
 		fi
166  
-		# Activate existing X session
167  
-		if xdpyinfo -display :${display}.0 &> /dev/null; then
168  
-			let tty=display+xtty
169  
-			chvt ${tty}
170  
-			exit 0
171  
-		fi
172  
-	else
173  
-		# Get the first empty display
174  
-		display=0
175  
-		while [[ ${display} -lt 7 ]]; do
176  
-			if dpyinfo=$(xdpyinfo -display :${display}.0 2>&1 1>/dev/null); then
177  
-				let display=display+1
178  
-			elif [[ $dpyinfo = No\ protocol\ specified* ]]; then
179  
-				# Display is in use by another user
180  
-				let display=display+1
181  
-			else
182  
-				break
183  
-			fi
184  
-		done
185  
-	fi
  137
+		serverargs=":${display} $serverargs vt$((xtty+display))"
186 138
 
187  
-	# Start X
188  
-	if $(yesno usexinit); then
189  
-		if [[ -f $userclientrc ]]; then
190  
-			wm_bin="$userclientrc $wm_bin"
191  
-		elif [[ -f $sysclientrc ]]; then
192  
-			wm_bin="$sysclientrc $wm_bin"
  139
+		$(yesno consolekit) && launchflags="-c -t $cktimeout"
  140
+		if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then
  141
+			ewarn "$self: \`cdm-xlaunch' exited unsuccessfully."
  142
+			exiterror
193 143
 		fi
194  
-	else
195  
-		wm_bin="/etc/X11/cdm/xinitrc $wm_bin"
196  
-	fi
197  
-
198  
-	serverargs=":${display} ${serverargs} vt$((xtty+display))"
  144
+		;;
199 145
 
200  
-	$(yesno consolekit) && launchflags="-c -t $cktimeout"
201  
-	if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then
202  
-		ewarn "\`cdm-xlaunch' exited unsuccessfully."
203  
-		exit 1
204  
-	fi
205  
-}
206  
-mainmenu
  146
+	*)
  147
+		eerror "$self: unknown flag: \`${flaglist[$binindex]}'."
  148
+		exiterror
  149
+		;;
  150
+esac
207 151
 
99  src/cdmrc
... ...
@@ -1,70 +1,51 @@
1  
-############################
2  
-### GLOBAL CONFIGURATION ###
3  
-############################
4  
-
5  
-# Set CDM theme and dialog options
6  
-dialogrc=/usr/share/cdm/themes/cdm
7  
-countfrom=1
8  
-
9  
-# List all WM binary names
10  
-# If this is not set all sessions in
11  
-# /etc/X11/Sessions are offered as choices.
12  
-wmbinlist=()
13  
-
14  
-# List all WM display names
15  
-wmdisplist=()
16  
-
17  
-# Allow console login?
18  
-allowconsole=yes
19  
-
20  
-# Allow shutdown?
21  
-# Note that this option requires sudo
22  
-# to be installed and properly configured.
23  
-allowshutdown=no
24  
-shutdowncommand='sudo shutdown -h now'
25  
-rebootcommand='sudo shutdown -r now'
26  
-
27  
-# Allow suspend?
28  
-# Note that this option requires pm-utils
29  
-# to be installed and properly configured.
30  
-allowsuspend=no
31  
-suspendcommand='sudo pm-suspend'
32  
-allowhibernate=no
33  
-hibernatecommand='sudo pm-hibernate'
34  
-
35  
-############################
36  
-### SYSTEM CONFIGURATION ###
37  
-############################
38  
-
39  
-# Set default display
  1
+#!/bin/bash
  2
+#
  3
+# Example config file for cdm(1).
  4
+# Values set here is the default as in cdm(1).
  5
+
  6
+# Style for the cdm dialog, which is printed with dialog(1).
  7
+# Default to unset, causing dialog(1) to use the system wide default.
  8
+# See /usr/share/cdm/themes for some nice choices.
  9
+dialogrc=
  10
+
  11
+# Index of the first item in the menu.
  12
+countfrom=0
  13
+
  14
+# List of programs, commands with whitespaces should be quoted or escaped.
  15
+# If unset, all sessions in /etc/X11/Sessions are offered as choices.
  16
+#
  17
+# (An example:)
  18
+# binlist=("~/.xsession" "/bin/bash --login" "/usr/bin/fbterm")
  19
+
  20
+# List all program display names, one-by-one matched with $binlist.
  21
+# Names with whitespaces should be quoted or escaped.
  22
+#
  23
+# (Continued example:)
  24
+# namelist=(XSession Console FBTerm)
  25
+
  26
+# Type of the programs, one-by-one matched with $binlist.
  27
+# `C' for *C*onsole programs, which would be `exec'ed.
  28
+# `X' for *X* programs, which would be run with cdm-xlaunch(1).
  29
+#
  30
+# (Continued example:)
  31
+# flaglist=(X C C)
  32
+
  33
+# Set default display.
40 34
 display=0
41 35
 
42 36
 # Where should first X tty be spawned?
43 37
 xtty=7
44 38
 
45  
-# Restrict tty? (By default, cdm increments X tty, this setting
46  
-# allows administrators to lock users into one specific tty by
47  
-# setting the display=N option on a per-user basis.)
48  
-locktty=no
  39
+# Should cdm(1) stick to the specified display?
  40
+locktty=yes
49 41
 
50  
-# The use of consolekit has recently become manditory with hal. However,
51  
-# some users might prefer not using either one. If you don't want to
52  
-# use consolekit, set the following variable to "no".
  42
+# Use consolekit for X session?
53 43
 consolekit=yes
54 44
 
55  
-# Timeout for waiting for X session to register with consoleKit
56  
-cktimeout=30
57  
-
58  
-# Additional arguments to pass to X server
59  
-# When usexinit=yes, we use ~/.xinitrc when it exists or /etc/X11/xinit/xinitrc
60  
-# when it doesn't. These are passed the chosen window manager as $1; note that
61  
-# the stock /etc/X11/xinit/xinitrc ignores this and always starts twm and three
62  
-# xterms. Your custom xinitrc should start the window manager supplied as $1
63  
-# instead. There is a sample xinitrc included at /usr/share/cdm/xinitrc.skel.
64  
-# If usexinit=no, then we instead call the chosen window manager directly.
65  
-usexinit=no
  45
+# Timeout for waiting for X session to register with consolekit.
  46
+cktimeout=5
66 47
 
67 48
 # Additional arguments to pass to X server; it will be called as:
68  
-#	exec /usr/bin/X :$display $serverargs vt$((xtty+display))
69  
-serverargs="-nolisten tcp"
  49
+#   exec /usr/bin/X :$display $serverargs vt$((xtty+display))
  50
+serverargs='-nolisten tcp'
70 51
 
7  src/profile
... ...
@@ -0,0 +1,7 @@
  1
+#!/bin/sh -
  2
+
  3
+if [[ "$(tty)" == /dev/tty1 ]]; then
  4
+	[[ -n "$CDM_SPAWN" ]] && return
  5
+	[[ -z "$DISPLAY`pgrep xinit`$SSH_TTY" ]] && exec cdm
  6
+fi
  7
+
10  src/xinitrc
... ...
@@ -1,10 +0,0 @@
1  
-#!/bin/sh
2  
-#
3  
-# If option usexinit is set then this file is not used.  Instead ~/.xinitrc
4  
-# if it exists or else /etc/X11/xinit/xinitrc is used in it's place.
5  
-
6  
-
7  
-# Put your configuration above this line.
8  
-
9  
-wm_bin="$1"
10  
-exec ${wm_bin}
9  src/zzz-cdm-profile.sh
... ...
@@ -1,9 +0,0 @@
1  
-if [[ -z "$DISPLAY" && -z "$SSH_TTY" && $(tty) = /dev/tty* ]]; then
2  
-	# Drop root to console
3  
-	if ! [[ $EUID -eq 0 ]]; then
4  
-		cdm
5  
-		if (( $? == 0 )); then
6  
-			exit 0
7  
-		fi
8  
-	fi
9  
-fi

0 notes on commit 4b0e982

Please sign in to comment.
Something went wrong with that request. Please try again.