-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtdm
executable file
·276 lines (243 loc) · 6.73 KB
/
tdm
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash
# tdm: session selector after login.
# This file is part of tdm, a tiny display manager.
#
# tdm 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 3 of the License, or
# (at your option) any later version.
#
# tdm 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 tdm. If not, see <http://www.gnu.org/licenses/>.
VERSION="1.4.1"
xstart=false
xrunning_check=true
silent=false
disable_long_names=false
skip_single_session=false
nulltype(){
type "$1" > /dev/null 2>/dev/null
}
function load_confdir_path() {
# usage: load_confdir_path
for cfd in "${XDG_CONFIG_HOME:-$HOME/.config}/tdm" "$HOME/.tdm" ; do
if [ -d "$cfd" ] ; then
echo "$cfd"
return
fi
done
echo "${XDG_CONFIG_HOME:-$HOME/.config}/tdm"
}
fallback(){
if [ -n "$1" ]; then
$silent || echo -e '\033[31;1m'"$1"'\033[0m'
fi
exit 1
}
get_needs(){
local _type
_type="$(file -p --mime-type "$1" | awk '{print $2}' | cut -d'/' -f1)"
if [ "${_type}" == "text" ]; then
grep '#[ \t]*needs[ \t]*:' "$1"|sed -e 's/#.*://g'
fi
}
valid_item(){
local needs
read -r -a needs<<<"$(get_needs "$1")"
for _i in "${needs[@]}"
do
nulltype "${_i}" || return 1
done
return 0
}
# directory settings
CONFDIR="$(load_confdir_path)"
SESSIONS=${CONFDIR}/sessions
EXTRA=${CONFDIR}/extra
SAVELAST=1
STARTXARGS=
eval set -- "$(
getopt -o h \
--long help \
--long xstart \
--long disable-xrunning-check \
--long silent \
--long disable-long-names \
--long skip-single-session \
-n "$(basename "$0")" -- "$@" || \
echo usage
)"
if [ "$CONFDIR" == "$HOME/.tdm" ] ; then
echo "Following tdm v1.3.0, the configuration files should be moved to '${XDG_CONFIG_HOME:-$HOME/.config}/tdm' in order to become XDG compliant"
echo "Use 'tdmctl migrate' to automatically migrate your configuration to the new location"
echo "Support for the old configuration directory ($CONFDIR) will be dropped in tdm 2.x.y"
fi
if [ -z "$XDG_VTNR" ]; then
XDG_VTNR="$(tty | sed -n -e 's|^/dev/tty\([1-9][0-9]*\)|\1|p')"
export XDG_VTNR
fi
while [ $# -gt 0 ]; do
case "$1" in
'--xstart')
xstart=true
;;
'--disable-xrunning-check')
xrunning_check=false
;;
'--silent')
silent=true
;;
'--help'|'-h')
usage 0
;;
'--disable-long-names')
disable_long_names=true
;;
'--skip-single-session')
skip_single_session=true
;;
'--')
shift
break
;;
*)
>&2 printf 'Whoops, option "%s" is not yet implemented!\n' "$1"
exit 42
;;
esac
shift
done
if [ $# -gt 0 ]; then
>&2 echo 'Too many arguments.'
exit 2
fi
# started from startx, so start session
if "$xstart" ; then
if [[ -f "${CONFDIR}/tdmexit" ]]; then
# shellcheck source=tdmexit
. "${CONFDIR}/tdmexit"
fi
if [[ -x "/tmp/tdmdefault" ]]; then
exec /tmp/tdmdefault
else
exec "${CONFDIR}/default"
fi
fi
clear
# check for a 'good' tty
(basename "$(tty)" |grep -q tty) || fallback "Invalid tty"
# X started, do not show X session options in the menu
hide_xsessions=false
$xrunning_check && [[ "$(find /tmp/.X11-unix -type s | wc -l)" -gt 0 ]] && hide_xsessions=true
# build confdir
if [ ! -d "${CONFDIR}" ]; then
tdmctl init
fi
# otherwise, run as the session chosen script
if [[ -f "${CONFDIR}/tdminit" ]]; then
# shellcheck source=tdminit
source "${CONFDIR}/tdminit"
fi
# XID: X session Number
# TOTAL: Number of items
# sid : id that user selects
((XID=0))
((TOTAL=0))
xsessions=()
prglist=()
DEFOPT=()
if [[ -x "${CONFDIR}/default" ]]; then
SDEFAULT=$(readlink "${CONFDIR}/default")
DEFAULTWM=$(basename "${SDEFAULT}")
else
SDEFAULT=
DEFAULTWM=
fi
if [ -d "${SESSIONS}" ] && ! $hide_xsessions; then
for script in "${SESSIONS}"/*; do
if [ -x "${script}" ] && valid_item "${script}" ; then
xsessions[$XID]="${script}"
NAME=$(basename "${script}")
prglist=("${prglist[@]}" "${XID}" "${NAME}")
if [[ "${NAME}" == "${DEFAULTWM}" ]]; then
DEFOPT=("--default-item" "${XID}")
fi
(( XID=XID+1 ))
(( TOTAL=TOTAL+1 ))
fi
done
elif ! [ -d "${SESSIONS}" ]; then
echo "${SESSIONS} doesn't exist."
echo "Making this directory."
mkdir -p "${SESSIONS}"
fi
if [ -d "${EXTRA}" ]; then
extra_marker="extra/"
$disable_long_names && extra_marker=""
for script in "${EXTRA}"/*; do
if [ -x "${script}" ] && valid_item "${script}" ; then
xsessions[$TOTAL]="${script}"
NAME="$extra_marker$(basename "${script}")"
prglist=("${prglist[@]}" "${TOTAL}" "${NAME}")
(( TOTAL=TOTAL+1 ))
fi
done
fi
if [ $TOTAL -eq 0 ]; then
$hide_xsessions && fallback "X started."
fallback "No sessions found."
fi
tdm_curses(){
tempfile="/tmp/tdm_$$"
trap 'rm -f ${tempfile}' 0 1 2 3 6 14 15
dialog "${DEFOPT[@]}" --menu "TDM ${VERSION}" 0 0 0 "${prglist[@]}" 2>${tempfile}
sid=$(cat ${tempfile})
[ -n "$sid" ]||fallback "Falling back to shell."
}
tdm_text(){
echo "This is TDM ${VERSION}, a tiny display manager."
echo "Please select from the following: (default ${DEFAULTWM})"
local _i=0
while [ ${_i} -lt ${TOTAL} ]
do
echo "${_i} ${prglist[${_i}*2+1]}"
(( _i=_i+1 ))
done
echo -n "Program ID: "
read -r sid
}
if ! nulltype dialog; then
#no dialog program, force to use tdm_text
TDMUI=tdm_text
elif [ ! "${TDMUI}" == "tdm_text" ]; then
TDMUI=tdm_curses
fi
echo "TOTOAL $TOTAL"
if [ "$skip_single_session" == "true" ] && [ "$TOTAL" -eq 1 ] ; then
sid=0
else
${TDMUI}
fi
rm -f /tmp/tdmdefault
if [[ (-n $sid) && ($sid -lt $TOTAL) && ($sid -ge $XID) ]]; then
exec "${xsessions[$sid]}"
elif [[ (-n $sid) && ($sid -lt $XID) && ($sid -ge 0) ]]; then
if [[ ${SAVELAST} -ne 0 ]]; then
ln -sf "${xsessions[${sid}]}" "${CONFDIR}/default"
else
ln -sf "${xsessions[${sid}]}" "/tmp/tdmdefault"
fi
startx ${STARTXARGS}
else
echo "Unknown value, load default."
if [ -x "${CONFDIR}/default" ]; then
startx ${STARTXARGS}
else
fallback "Session not defined, fallback."
fi
fi