forked from evertiro/cdm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdm
executable file
·159 lines (141 loc) · 4.26 KB
/
cdm
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
#
# CDM: The Console Display Manager
#
# Copyright (C) 2009-2011, Daniel J Griffiths <ghost1227@archlinux.us>
# Thanks to:
#
# Andrwe beta-testing and submitting the fix for the all
# important X incrementation function
# brisbin33 code cleanup
# tigrmesh finding a critical issue with the gnome-session handler
# Profjim several incredibly useful patches
# lambchops468 consolekit and hibernation patches
# CasperVector Massive rearchitecturing and code sanisation
#
# This program 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 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
name=$(basename "$0")
longname='Console Display Manager'
ver='0.6'
trap '' SIGINT SIGTSTP
source /etc/init.d/functions.sh
# Exit functions.
exitnormal() { exit 0; }
exiterror() { sleep 1; exit 1; }
# Source cdm configurations.
if [[ -n "$1" ]]; then
if [[ -f "$HOME/.cdmrc" ]]
then
source "$1"
else
eerror "$self: config file \`$1' does not exist."
exiterror
fi
elif [[ -f "$HOME/.cdmrc" ]]; then
source "$HOME/.cdmrc"
elif [[ -f /etc/cdmrc ]]; then
source /etc/cdmrc
fi
# Default options.
binlist=${binlist:-()}
namelist=${namelist:-()}
flaglist=${flaglist:-()}
dialogrc=${dialogrc:-}
countfrom=${countfrom:-0}
display=${display:-0}
locktty=${locktty:-no}
xtty=${xtty:-7}
serverargs=${serverargs:-'-nolisten tcp'}
consolekit=${consolekit:-yes}
cktimeout=${cktimeout:-30}
# Offer all available sessions in /etc/X11/Sessions,
# if binlist if not explicitly set in cdmrc.
if [[ "${#binlist[@]}" == 0 ]]; then
binlist=($(ls /etc/X11/Sessions))
flaglist=($(sed 's/[[:digit:]]\+/X/g' <<< ${!flaglist[*]}))
namelist=(${binlist[@]^})
fi
# Generate the main menu.
menu=()
for ((count = 0; count < ${#namelist[@]}; count++)); do
menu=("${menu[@]}" "$((count+countfrom))" "${namelist[${count}]}")
done
# Override dialog display if only one option is available.
if [[ "$count" == 1 ]]; then
binindex=0
else
# Display selection dialog.
binindex=$(
DIALOGRC="$dialogrc" dialog --colors --stdout \
--backtitle "${longname} v${ver}" \
--ok-label ' Select ' --cancel-label ' Exit ' \
--menu 'Select session' 0 0 0 "${menu[@]}"
)
if [[ $? != 0 ]]; then
clear
exitnormal
fi
fi
# Run $bin according to its flag.
let binindex-=countfrom
bin="${binlist[${binindex}]}"
case ${flaglist[$binindex]} in
# *C*onsole programs.
[Cc])
clear
# If $bin is a login shell, it might `exec' cdm again, causing an endless
# loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and
# only let the shell automatically `exec' cdm when $CDM_SPAWN is not set.
# See also the example shell profile file shipped with this cdm package.
CDM_SPAWN=$$ exec $bin
;;
# *X* programs.
[Xx])
clear
# If X is already running and locktty=yes, activate it,
# otherwise increment.
if $(yesno locktty); then
# Activate existing X session.
if xdpyinfo -display ":$display.0" &> /dev/null; then
chvt "$((display+xtty))"
exitnormal
fi
else
# Get the first empty display.
display=0
while ((display < 7)); do
if dpyinfo=$(xdpyinfo -display ":$display.0" 2>&1 1>/dev/null) ||
# Display is in use by another user.
[[ "$dpyinfo" == 'No protocol specified'* ]]
then
let display+=1
else
break
fi
done
fi
serverargs=":${display} $serverargs vt$((xtty+display))"
$(yesno consolekit) && launchflags="-c -t $cktimeout"
if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then
ewarn "$self: \`cdm-xlaunch' exited unsuccessfully."
exiterror
fi
;;
*)
eerror "$self: unknown flag: \`${flaglist[$binindex]}'."
exiterror
;;
esac