Advertisement
unixwz0r

cdm (console display manager program)

Jan 19th, 2015
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # I have made the changes of the title to my distro and version number
  4. # This CDM program is used on the Tux Hat Linux Distro v3.3
  5. # For more information keep on reading this file
  6. #
  7. # CDM: The Console Display Manager
  8. #
  9. # Copyright (C) 2009-2012, Daniel J Griffiths <[email protected]>
  10. # Thanks to:
  11. #
  12. # Andrwe beta-testing and submitting the fix for the all
  13. # important X incrementation function
  14. # brisbin33 code cleanup
  15. # tigrmesh finding a critical issue with the gnome-session handler
  16. # Profjim several incredibly useful patches
  17. # lambchops468 consolekit and hibernation patches
  18. # CasperVector Massive rearchitecturing and code sanitation
  19. #
  20. # This program is free software; you can redistribute it and/or modify
  21. # it under the terms of the GNU General Public License as published by
  22. # the Free Software Foundation; either version 2 of the License, or
  23. # (at your option) any later version.
  24. #
  25. # This program is distributed in the hope that it will be useful,
  26. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. # GNU General Public License for more details.
  29. #
  30. # You should have received a copy of the GNU General Public License
  31. # along with this program; if not, write to the Free Software
  32. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  33. # MA 02110-1301, USA.
  34.  
  35. name=$(basename "$0")
  36. longname='Tux Hat Linux'
  37. ver='3.3'
  38.  
  39. trap '' SIGINT SIGTSTP
  40.  
  41. # Helper functions.
  42.  
  43. warn() { (printf ' \033[01;33m*\033[00m '; echo "$name: $*") > /dev/stderr; }
  44. error() { (printf ' \033[01;31m*\033[00m '; echo "$name: $*") > /dev/stderr; }
  45. exitnormal() { exit 0; }
  46. exiterror() { sleep 1; exit 1; }
  47. exitcancel() { exit 2; }
  48. yesno()
  49. {
  50. [ -z "$1" ] && return 1
  51. eval value=\$${1}
  52.  
  53. case "$value" in
  54. [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0;;
  55. [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1;;
  56. *) warn "invalid value for \`$1'; falling back to \`no' for now.";;
  57. esac
  58. }
  59.  
  60. # Source cdm configurations.
  61.  
  62. if [[ -n "$1" ]]; then
  63. if [[ -f "$1" ]]
  64. then
  65. source "$1"
  66. else
  67. error "config file \`$1' does not exist."
  68. exiterror
  69. fi
  70. elif [[ -f "$HOME/.cdmrc" ]]; then
  71. source "$HOME/.cdmrc"
  72. elif [[ -f /etc/cdmrc ]]; then
  73. source /etc/cdmrc
  74. fi
  75.  
  76. # Default options.
  77.  
  78. dialogrc=${dialogrc:-}
  79. countfrom=${countfrom:-0}
  80. display=${display:-0}
  81. xtty=${xtty:-7}
  82. locktty=${locktty:-no}
  83. consolekit=${consolekit:-yes}
  84. cktimeout=${cktimeout:-30}
  85. altstartx=${altstartx:-no}
  86. startxlog="${startxlog:-/dev/null}"
  87. [[ -z "${binlist[*]}" ]] && binlist=()
  88. [[ -z "${namelist[*]}" ]] && namelist=()
  89. [[ -z "${flaglist[*]}" ]] && flaglist=()
  90. [[ -z "${serverargs[*]}" ]] && serverargs=(-nolisten tcp)
  91.  
  92. # Offer all available sessions in /etc/X11/Sessions,
  93. # if $binlist if not explicitly set in cdmrc.
  94.  
  95. if [[ "${#binlist[@]}" == 0 && -d /etc/X11/Sessions ]]; then
  96. binlist=($(find /etc/X11/Sessions -maxdepth 1 -type f))
  97. flaglist=($(sed 's/[[:digit:]]\+/X/g' <<< ${!binlist[@]}))
  98. namelist=(${binlist[@]##*/})
  99. fi
  100.  
  101. # If $binlist is not set in cdmrc or by files in /etc/X11/Sessions,
  102. # try .desktop files in /usr/share/xsessions/ .
  103.  
  104. if [[ "${#binlist[@]}" == 0 && -d /usr/share/xsessions ]]; then
  105. desktopsessions=($(find /usr/share/xsessions/ -regex .\*.desktop))
  106. #TODO: allow full quoting and expansion according to desktop entry spec:
  107. # http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
  108.  
  109. for ((count=0; count < ${#desktopsessions[@]}; count++)); do
  110. # TryExec key is there to determine if executable is present,
  111. # but as we are going to test the Exec key anyway, we ignore it.
  112. execkey=$(sed -n -e 's/^Exec=//p' <${desktopsessions[${count}]})
  113. namekey=$(sed -n -e 's/^Name=//p' <${desktopsessions[${count}]})
  114. if [[ -n ${execkey} && -n ${namekey} ]]; then
  115. # The .desktop files allow there Exec keys to use $PATH lookup.
  116. binitem="$(which $(cut -f 1 -d ' ' <<< ${execkey}))"
  117. # If which fails to return valid path, skip to next .desktop file.
  118. if [[ $? != 0 ]]; then continue; fi
  119. binlist+=("${binitem} $(cut -s -f 2- -d ' ' <<< ${execkey})")
  120. flaglist+=('X')
  121. namelist+=("${namekey}")
  122. fi
  123. done
  124. fi
  125.  
  126. case "${#binlist[@]}" in
  127. 0)
  128. error "No programs found in cdm config file, /etc/X11/Sessions or /usr/share/xsessions."
  129. exiterror
  130. ;;
  131. 1)
  132. # No need to call dialog AND clear, only one possible program
  133. binindex=0
  134. ;;
  135. *)
  136. menu=()
  137. for ((count = 0; count < ${#namelist[@]}; count++)); do
  138. menu=("${menu[@]}" "$((count+countfrom))" "${namelist[${count}]}")
  139. done
  140. binindex=$(
  141. DIALOGRC="$dialogrc" dialog --colors --stdout \
  142. --backtitle "$longname v$ver" --ok-label ' Select ' \
  143. --cancel-label ' Exit ' --menu 'Select session' 0 0 0 "${menu[@]}"
  144. )
  145. if [[ $? != 0 ]]; then
  146. clear; exitcancel
  147. fi
  148. clear
  149. let binindex-=countfrom
  150. ;;
  151. esac
  152.  
  153. # Run $bin according to its flag.
  154. bin=($(eval echo "${binlist[${binindex}]}"))
  155. case ${flaglist[$binindex]} in
  156. # *C*onsole programs.
  157. [Cc])
  158. # If $bin is a login shell, it might `exec' cdm again, causing an endless
  159. # loop. To solve this problem, export $CDM_SPAWN when `exec'ing $bin and
  160. # only let the shell automatically `exec' cdm when $CDM_SPAWN is not set.
  161. # See also the example shell profile file shipped with the cdm package.
  162.  
  163. # Also untrap SIGINT and SIGTSTP before spawning process: If this is not
  164. # done, any child process of any child (bash) shell will completely
  165. # ignore SIGINT, which is rather confusing, and cannot be undone.
  166.  
  167. trap - SIGINT SIGTSTP
  168. CDM_SPAWN=$$ exec "${bin[@]}"
  169. ;;
  170.  
  171. # *X* programs.
  172. [Xx])
  173. # If X is already running and locktty=yes, activate it.
  174. if $(yesno locktty) && xdpyinfo -display ":$display.0" &> /dev/null; then
  175. chvt "$((display+xtty))"
  176. exitnormal
  177. fi
  178.  
  179. # Get the first empty display.
  180. display=0
  181. while ((display < 7)); do
  182. if dpyinfo=$(xdpyinfo -display ":$display.0" 2>&1 1>/dev/null) ||
  183. # Display is in use by another user.
  184. [[ "$dpyinfo" == 'No protocol specified'* ]] ||
  185. # Invalid MIT cookie.
  186. [[ "$dpyinfo" == 'Invalid MIT'* ]]
  187. then
  188. let display+=1
  189. else
  190. break
  191. fi
  192. done
  193.  
  194. # Support for running X in current tty.
  195. if [[ $xtty == 'keep' ]]; then
  196. vt="$(tty)"
  197. if [[ "$vt" != '/dev/tty'* ]]; then
  198. error 'invalid TTY.'
  199. exiterror
  200. fi
  201. vt="${vt#/dev/tty}"
  202. else
  203. vt="$((xtty+display))"
  204. fi
  205.  
  206. serverargs=(":${display}" "${serverargs[@]}" "vt$vt")
  207.  
  208. $(yesno consolekit) && launchflags=(-c -t "$cktimeout")
  209. $(yesno altstartx) && launchflags=("${launchflags[@]}" --altstartx)
  210. launchflags=("${launchflags[@]}" --startxlog "$startxlog")
  211. if cdm-xlaunch "${launchflags[@]}" -- "${bin[@]}" -- "${serverargs[@]}"
  212. then
  213. exitnormal
  214. else
  215. warn "\`cdm-xlaunch' exited unsuccessfully."
  216. exiterror
  217. fi
  218. ;;
  219.  
  220. *)
  221. error "unknown flag: \`${flaglist[$binindex]}'."
  222. exiterror
  223. ;;
  224. esac
  225.  
  226. # vim:ts=4:sw=4:et
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement