Skip to content

Commit

Permalink
Major restructuring.
Browse files Browse the repository at this point in the history
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
CasperVector committed Apr 7, 2012
1 parent f54e4a2 commit 4b0e982
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 227 deletions.
1 change: 1 addition & 0 deletions install
Expand Up @@ -12,3 +12,4 @@ else
install -Dm755 zzz-cdm-profile.sh /etc/profile.d/zzz-cdm-profile.sh install -Dm755 zzz-cdm-profile.sh /etc/profile.d/zzz-cdm-profile.sh
cp -Rf themes /usr/share/cdm/ cp -Rf themes /usr/share/cdm/
fi fi

242 changes: 93 additions & 149 deletions src/cdm
Expand Up @@ -27,18 +27,13 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA. # MA 02110-1301, USA.


name="Console Display Manager" name=$(basename "$0")
ver="0.5.3" longname='Console Display Manager'
ver='0.5.3'


trap '' SIGINT SIGTSTP trap '' SIGINT SIGTSTP
source /etc/init.d/functions.sh source /etc/init.d/functions.sh


dialogrc=${dialogrc:-}
userclientrc=$HOME/.xinitrc
sysclientrc=/etc/X11/xinit/xinitrc
# userserverrc=$HOME/.xserverrc
# sysserverrc=/etc/X11/xinit/xserverrc

# Source cdm configurations. # Source cdm configurations.


if [[ -n "$1" ]]; then if [[ -n "$1" ]]; then
Expand All @@ -49,159 +44,108 @@ elif [[ -f /etc/cdmrc ]]; then
source /etc/cdmrc source /etc/cdmrc
fi fi


# Offer all available sessions in /etc/X11/Sessions # Default options.
# if wmbinlist if not explicitly set in cdmrc


if [[ "${#wmbinlist[@]}" -eq 0 ]]; then dialogrc=${dialogrc:-}
wmbinlist=($(ls /etc/X11/Sessions)) countfrom=${countfrom:-0}
wmdisplist=($(echo ${wmbinlist[@]^})) binlist=${binlist:-()}
namelist=${namelist:-()}
xtty=${xtty:-7}
locktty=${locktty:-no}
display=${display:-0}
consolekit=${consolekit:-yes}
cktimeout=${cktimeout:-30}
serverargs=${serverargs:-'-nolisten tcp'}

# Exit functions.

exitnormal() { exit 0; }
exiterror() { sleep 1; exit 1; }

# 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 fi


mainmenu() { # Generate the main menu.
# Generate main menu menu=()
count=0 for ((count=0; count<${#namelist[@]}; count++)); do
menu=$( menu=("${menu[@]}" "$((count+countfrom))" "${namelist[${count}]}")
while [[ ${count} -lt ${#wmdisplist[@]} ]]; do done
echo -ne "$((count+countfrom)) ${wmdisplist[${count}]} "
let count=count+1 # Override dialog display if only one option is available.
done 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
# Check if console access is allowed clear
if $(yesno allowconsole); then exitnormal
if ! $(yesno allowshutdown); then
let halt=99
fi
let console=${#wmdisplist[@]}+countfrom
menu="${menu} ${console} Console "
fi

# Check if shutdown access is allowed
if $(yesno allowshutdown); then
if ! $(yesno allowconsole); then
let halt=${#wmdisplist[@]}
else
let halt=${#wmdisplist[@]}+1
fi
let halt=halt+countfrom
menu="${menu} ${halt} Shutdown "
fi fi
fi


# Override dialog display if only one option is available # Run $bin according to its flag.
if ! $(yesno allowconsole) && ! $(yesno allowshutdown) && [[ ${#wmdisplist[@]} == 1 ]]; then let binindex-=countfrom
wm=$countfrom bin="${binlist[${binindex}]}"
else case ${flaglist[$binindex]} in
# Display selection dialog # *C*onsole programs.
wm=$( [Cc])
DIALOGRC="$dialogrc" dialog --colors \ clear
--backtitle "${name} v${ver}" --stdout \ # If $bin is a login shell, it might `exec' cdm again, causing an endless
--ok-label " Select " --cancel-label " Logout " \ # loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and
--menu "Select Window Manager" 0 0 0 ${menu} # 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.
if [[ $? != 0 ]]; then CDM_SPAWN=$$ exec $bin
clear ;;
exit 0
fi # *X* programs.
fi [Xx])
clear


# Set wm_bin # If X is already running and locktty=yes, activate it,
clear # otherwise increment.
if [[ ${wm} -eq ${console} ]]; then if $(yesno locktty); then
exit 2 #2 signals to the profile file to not exit. # Activate existing X session.
elif [[ ${wm} -eq ${halt} ]]; then if xdpyinfo -display ":$display.0" &> /dev/null; then
shutdownmenu chvt "$((display+xtty))"
else exitnormal
let wm=wm-countfrom
wm_bin="${wmbinlist[${wm}]}"
xstart
fi
}

shutdownmenu() {
count=$countfrom
haltmenu=$(
for opt in Shutdown Reboot Suspend Hibernate; do
# Check if suspend/hibernate is enabled
if ([[ $opt != Suspend ]] && [[ $opt != Hibernate ]]) ||
([[ $opt == Suspend ]] && $(yesno allowsuspend)) ||
([[ $opt == Hibernate ]] && $(yesno allowhibernate)); then
echo -ne "$count $opt "
let count=count+1
fi fi
done
)

# Display shutdown dialog
haltopt=$(
DIALOGRC="$dialogrc" dialog --colors \
--backtitle "${name} v${ver}" --stdout \
--ok-label " Select " --cancel-label " Cancel " \
--menu "Shutdown" 0 0 0 ${haltmenu}
)

if [[ $? == 0 ]]; then
clear
if [[ ${haltopt} -eq $countfrom ]]; then
${shutdowncommand}
elif [[ ${haltopt} -eq $((countfrom+1)) ]]; then
${rebootcommand}
elif [[ ${haltopt} -eq $((countfrom+2)) ]] && $(yesno allowsuspend); then
${suspendcommand}
else else
${hibernatecommand} # Get the first empty display.
fi
else
mainmenu
fi
}

xstart() {
# If X is already running and locktty=yes, activate it,
# otherwise, increment.
if $(yesno locktty); then
# Verify display exists
if [[ -n "${display//[0-9]/}" ]]; then
display=0 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 fi
# Activate existing X session serverargs=":${display} $serverargs vt$((xtty+display))"
if xdpyinfo -display :${display}.0 &> /dev/null; then
let tty=display+xtty
chvt ${tty}
exit 0
fi
else
# Get the first empty display
display=0
while [[ ${display} -lt 7 ]]; do
if dpyinfo=$(xdpyinfo -display :${display}.0 2>&1 1>/dev/null); then
let display=display+1
elif [[ $dpyinfo = No\ protocol\ specified* ]]; then
# Display is in use by another user
let display=display+1
else
break
fi
done
fi


# Start X $(yesno consolekit) && launchflags="-c -t $cktimeout"
if $(yesno usexinit); then if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then
if [[ -f $userclientrc ]]; then ewarn "$self: \`cdm-xlaunch' exited unsuccessfully."
wm_bin="$userclientrc $wm_bin" exiterror
elif [[ -f $sysclientrc ]]; then
wm_bin="$sysclientrc $wm_bin"
fi fi
else ;;
wm_bin="/etc/X11/cdm/xinitrc $wm_bin"
fi

serverargs=":${display} ${serverargs} vt$((xtty+display))"


$(yesno consolekit) && launchflags="-c -t $cktimeout" *)
if ! eval cdm-xlaunch $launchflags -- $bin -- $serverargs; then eerror "$self: unknown flag: \`${flaglist[$binindex]}'."
ewarn "\`cdm-xlaunch' exited unsuccessfully." exiterror
exit 1 ;;
fi esac
}
mainmenu


99 changes: 40 additions & 59 deletions src/cdmrc
@@ -1,70 +1,51 @@
############################ #!/bin/bash
### GLOBAL CONFIGURATION ### #
############################ # Example config file for cdm(1).

# Values set here is the default as in cdm(1).
# Set CDM theme and dialog options
dialogrc=/usr/share/cdm/themes/cdm # Style for the cdm dialog, which is printed with dialog(1).
countfrom=1 # Default to unset, causing dialog(1) to use the system wide default.

# See /usr/share/cdm/themes for some nice choices.
# List all WM binary names dialogrc=
# If this is not set all sessions in
# /etc/X11/Sessions are offered as choices. # Index of the first item in the menu.
wmbinlist=() countfrom=0


# List all WM display names # List of programs, commands with whitespaces should be quoted or escaped.
wmdisplist=() # If unset, all sessions in /etc/X11/Sessions are offered as choices.

#
# Allow console login? # (An example:)
allowconsole=yes # binlist=("~/.xsession" "/bin/bash --login" "/usr/bin/fbterm")


# Allow shutdown? # List all program display names, one-by-one matched with $binlist.
# Note that this option requires sudo # Names with whitespaces should be quoted or escaped.
# to be installed and properly configured. #
allowshutdown=no # (Continued example:)
shutdowncommand='sudo shutdown -h now' # namelist=(XSession Console FBTerm)
rebootcommand='sudo shutdown -r now'

# Type of the programs, one-by-one matched with $binlist.
# Allow suspend? # `C' for *C*onsole programs, which would be `exec'ed.
# Note that this option requires pm-utils # `X' for *X* programs, which would be run with cdm-xlaunch(1).
# to be installed and properly configured. #
allowsuspend=no # (Continued example:)
suspendcommand='sudo pm-suspend' # flaglist=(X C C)
allowhibernate=no
hibernatecommand='sudo pm-hibernate' # Set default display.

############################
### SYSTEM CONFIGURATION ###
############################

# Set default display
display=0 display=0


# Where should first X tty be spawned? # Where should first X tty be spawned?
xtty=7 xtty=7


# Restrict tty? (By default, cdm increments X tty, this setting # Should cdm(1) stick to the specified display?
# allows administrators to lock users into one specific tty by locktty=yes
# setting the display=N option on a per-user basis.)
locktty=no


# The use of consolekit has recently become manditory with hal. However, # Use consolekit for X session?
# some users might prefer not using either one. If you don't want to
# use consolekit, set the following variable to "no".
consolekit=yes consolekit=yes


# Timeout for waiting for X session to register with consoleKit # Timeout for waiting for X session to register with consolekit.
cktimeout=30 cktimeout=5

# Additional arguments to pass to X server
# When usexinit=yes, we use ~/.xinitrc when it exists or /etc/X11/xinit/xinitrc
# when it doesn't. These are passed the chosen window manager as $1; note that
# the stock /etc/X11/xinit/xinitrc ignores this and always starts twm and three
# xterms. Your custom xinitrc should start the window manager supplied as $1
# instead. There is a sample xinitrc included at /usr/share/cdm/xinitrc.skel.
# If usexinit=no, then we instead call the chosen window manager directly.
usexinit=no


# Additional arguments to pass to X server; it will be called as: # Additional arguments to pass to X server; it will be called as:
# exec /usr/bin/X :$display $serverargs vt$((xtty+display)) # exec /usr/bin/X :$display $serverargs vt$((xtty+display))
serverargs="-nolisten tcp" serverargs='-nolisten tcp'


7 changes: 7 additions & 0 deletions src/profile
@@ -0,0 +1,7 @@
#!/bin/sh -

if [[ "$(tty)" == /dev/tty1 ]]; then
[[ -n "$CDM_SPAWN" ]] && return
[[ -z "$DISPLAY`pgrep xinit`$SSH_TTY" ]] && exec cdm
fi

0 comments on commit 4b0e982

Please sign in to comment.