Check for writeable /tmp before executing hardware detection
[grml-x.git] / grml-x
1 #!/bin/zsh
2 # Filename:      grml-x
3 # Purpose:       wrapper for startx on grml [providing new xconfiguration tool]
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 # Latest change: Die Sep 04 01:44:19 CEST 2007 [mika]
8 ################################################################################
9
10 # make sure /tmp is writeable {{{
11   if ! [ -w /tmp ] ; then
12      echo 'Fatal: /tmp is not writeable. Can not resume therefore, sorry.' >&2
13      exit 1
14   fi
15 # }}}
16
17 # debugging {{{
18 # usage: DEBUG=1 grml-x ..... 2>/tmp/grml-x-debug.log
19   if [[ $DEBUG -gt 0 ]]; then
20       setopt xtrace
21   fi
22 # }}}
23
24 # functions and color {{{
25   # use colors only if not booted with nocolor bootoption
26   if ! grep -q nocolor /proc/cmdline ; then
27      autoload colors ; colors
28      [ -r /etc/grml_colors ] && . /etc/grml_colors
29   fi
30
31   # some functions like getBootParam
32   if [ -r /etc/grml/script-functions -a -r /etc/grml/sh-lib ] ; then
33      source /etc/grml/script-functions
34      source /etc/grml/sh-lib
35   else
36     echo 'Error: sourcing function files failed. Exiting.'
37     exit 1
38   fi
39
40   check4root &>/dev/null && ROOT='1' || ROOT=''
41 # }}}
42
43 # set variables  {{{
44   PROGRAMNAME=${0##*/}
45   HWINFO='/usr/sbin/hwinfo'
46   DATE=$(date)
47   [ -n "$XINITRC" ] || XINITRC="$HOME/.xinitrc"
48
49   # temporary files
50   HWINFO_TMP="/tmp/hwinfo.$$"
51   MONITORINFO="/tmp/monitorinfo.$$"
52   MOUSEINFO="/tmp/mouse.$$"
53
54   if [ -r /etc/sysconfig/keyboard ] ; then
55     source /etc/sysconfig/keyboard
56   else
57     XKEYBOARD='us'
58   fi
59
60   XSERVER="Xorg"
61   XCONFIG='/etc/X11/xorg.conf'
62   KEYBOARD="# Driver      \"kbd\"
63 #         Option      \"XkbRules\"   \"xfree86\"
64 #         Option      \"XkbRules\"   \"xorg\"
65 #         Option      \"XkbModel\"   \"pc105\"
66 #         Option      \"XkbLayout\"  \"${XKEYBOARD},us\"
67 #         Option      \"XkbVariant\" \"nodeadkeys\""
68
69   # check for font path
70   if [ -d /usr/share/fonts/X11 ] ; then
71      XFONTS="        FontPath     \"/usr/share/fonts/X11/misc\"
72         FontPath     \"/usr/share/fonts/X11/100dpi/:unscaled\"
73         FontPath     \"/usr/share/fonts/X11/75dpi/:unscaled\"
74         FontPath     \"/usr/share/fonts/X11/Type1\"
75         FontPath     \"/usr/share/fonts/X11/100dpi\"
76         FontPath     \"/usr/share/fonts/X11/75dpi\""
77   fi
78   # /usr/X11R6/lib/X11/fonts exists nearly everywhere, assume
79   # /usr/X11R6/lib/X11/fonts as valid font path only if fonts.dir
80   # exists for "misc"
81   if [ -r /usr/X11R6/lib/X11/fonts/misc/fonts.dir ] ; then
82       XFONTS="$XFONTS
83         FontPath     \"/usr/X11R6/lib/X11/fonts/Type1\"
84         FontPath     \"/usr/X11R6/lib/X11/fonts/misc:unscaled\"
85         FontPath     \"/usr/X11R6/lib/X11/fonts/misc\"
86         FontPath     \"/usr/X11R6/lib/X11/fonts/75dpi:unscaled\"
87         FontPath     \"/usr/X11R6/lib/X11/fonts/75dpi\"
88         FontPath     \"/usr/X11R6/lib/X11/fonts/100dpi:unscaled\"
89         FontPath     \"/usr/X11R6/lib/X11/fonts/100dpi\""
90   fi
91 # }}}
92
93 # make sure we don't leave any temp files {{{
94 bailout() {
95   rm -f "$HWINFO_TMP" "$MONITORINFO" "$MOUSEINFO"
96   [ -n "$1" ] && EXIT="$1" || EXIT="1"
97   print "$bg[black]$fg[red]${bold_color}Exiting...${reset_color}">&2
98   exit "$EXIT"
99 }
100
101 trap bailout 1 2 3 15
102 # }}}
103
104 # warn if running as user root {{{
105   if [ -n "$ROOT" ] ; then
106      if [ -r /etc/grml_cd ] ; then
107         print "$bg[black]$fg[red]${bold_color}Warning: Please do not run grml-x as user root.${reset_color}"
108         print "$bg[black]$fg[red]${bold_color}Running grml-x as user root is *not* supported!${reset_color}"
109         print "$bg[black]$fg[red]${bold_color}Switch to user grml or run su - grml -c 'grml-x ...' instead.${reset_color}"
110         print ''
111      else
112         print "$bg[black]$fg[red]${bold_color}Warning: Please do not run X.org as user root!${reset_color}"
113         print "$bg[black]$fg[red]${bold_color}As soon as you have a working $XCONFIG please use startx instead of grml-x.${reset_color}"
114         print ''
115      fi
116   fi
117   fstabuser=$(grep ':x:1000:' /etc/passwd)
118   fstabuser=${fstabuser%%[:]*}
119 # }}}
120
121 # usage information {{{
122 usage()
123 {
124   if [[ $1 != '' ]] ; then echo 1>&2 "\n$1" ; fi
125   print "$bg[black]$fg[red]$bold_color"
126   print 1>&2 "
127 Usage: $PROGRAMNAME
128        $PROGRAMNAME [-options] windowmanager
129
130 Usage examples:
131   $PROGRAMNAME wmii
132   $PROGRAMNAME fluxbox
133   $PROGRAMNAME openbox
134   $PROGRAMNAME -force -nostart fluxbox
135   $PROGRAMNAME -nosynaptics fluxbox
136   $PROGRAMNAME -nosync fluxbox
137   $PROGRAMNAME -noddc wmii
138   $PROGRAMNAME -xinerama -composite wmii
139   $PROGRAMNAME -module radeon -mode 1024x768 -vsync 60 openbox
140   XINITRC=~/.xinitrc $PROGRAMNAME openbox
141   $PROGRAMNAME -display 8 openbox
142
143 More information on grml-x can be found in the manual page: man grml-x
144
145 Report bugs, send wishes and feedback to the grml team:
146 http://grml.org/bugs/ - contact (at) grml.org
147 "
148   print "${reset_color}"
149   exit 2
150 }
151 # }}}
152
153 # writehwinfo {{{
154 writehwinfo()
155 {
156    if [ -n "$ROOT" ] ; then
157      su - $fstabuser -c "$HWINFO > $HWINFO_TMP"
158    else
159      $HWINFO > $HWINFO_TMP
160    fi
161 }
162 # }}}
163
164 # monitor {{{
165 monitor()
166 {
167   sudo $HWINFO --monitor > $MONITORINFO
168 }
169 # }}}
170
171 # mode {{{
172 mode()
173 {
174    [ -r "$MONITORINFO" ] || monitor # get monitor settings
175    modes=$(perl -e 'while (<STDIN>) {if (/  Resolution:/) { s/.*\s+(\d+x\d+).*/$1/; print} }' < $MONITORINFO |
176 sort -nur | perl -ne 's/\s+/ /; s/(\d+x\d+)/"$1"/; print')
177
178    if [ -n "$XINERAMA" ] ; then
179
180       print "$fg[green]
181
182 Option for $fg[yellow]Xinerama$fg[green] found, please use xrandr inside X.org for your further configuration!
183 Trying to identify monitors now..."
184
185       # make sure we have information from 2 monitors:
186       # - first one from 'hwinfo --monitor'      => $MONITORINFO
187       # - second one from 'hwinfo --framebuffer' => $FRAMEBUFFER
188       FRAMEBUFFER=$(hwinfo --framebuffer | grep 'Mode 0x.* bits' | head -1)
189       if [ -z "$FRAMEBUFFER" ] ; then
190          print "$fg[red]
191 Fatal: could not identify two monitors - no chance to
192        set up multihead using Xinerama - sorry. :-(
193
194 Please run grml-x without the -xinerama option to start
195 X anyway (dropping support for Xinerama of course) and
196 contact grml developers if you have any further useful
197 information.                  => http://grml.org/bugs/
198 "
199          bailout 10
200       fi
201
202       # check whether we can read the $MONITORINFO file
203       if ! [ -r "$MONITORINFO" ] ; then
204          print "$fg[red]
205 Fatal: could not identify monitor - no chance to
206        set up multihead using Xinerama - sorry. :-(
207 "
208          bailout 11
209       else
210          # Currently we only know that it works with current intel driver,
211          # so inform user about that:
212          if [[ "$MODULE" != "intel" ]] ; then
213             print "$fg[red]
214
215 Warning: using a non-intel driver - trying Xinerama setup anyway.
216 If it worked please contact grml developers providing information
217 about your setup so we can adjust grml-x according!
218
219       -> http://grml.org/contact/
220
221 Sleeping for 10 seconds now... Will continue then...
222 Just press Ctrl-C to cancel operation.
223 "
224             sleep 10
225          fi
226
227          # now calculate Virtual size for use with Xinerama
228          HORIZ_RES1="$(grep 'Max. Resolution:' $MONITORINFO | sed 's/    Max\. Resolution: \(.*\)x\(.*\)/\1/')"
229          VERIZ_RES1="$(grep 'Max. Resolution:' $MONITORINFO | sed 's/    Max\. Resolution: \(.*\)x\(.*\)/\2/')"
230          # it might happen that we don't have a Max Resolution in hwinfo output :-/
231          # fall back to a generic value then...
232          # TODO: check out how to get resolution of external monitor from console
233          if [ -z "$HORIZ_RES1" ] ; then
234             HORIZ_RES1="1024"
235             VERIZ_RES1="768"
236          fi
237
238          # get maximum of output
239          HORIZ_RES2="$(echo $FRAMEBUFFER | grep 'Mode 0x' | sed 's/  Mode 0x.*: \(.*\)x.*/\1/'| sort -u | head -1)"
240          VERIZ_RES2="$(echo $FRAMEBUFFER | grep 'Mode 0x' | sed 's/  Mode 0x.*x\(.*\) (.*/\1/' | sort -u | head -1)"
241
242          if [ -n "$HORIZ_RES1" -a -n "$VERIZ_RES1" -a -n "$HORIZ_RES2" -a -n "$VERIZ_RES2" ] ; then
243             if [ "$(echo $VERIZ_RES1" - "$VERIZ_RES2 | bc -l)" -eq 0 ] ; then
244             VERIZ_RESULT="$VERIZ_RES1"
245             elif [ "$VERIZ_RES1" -gt "$VERIZ_RES2" ] ; then
246                VERIZ_RESULT="$VERIZ_RES1"
247             else
248                VERIZ_RESULT="$VERIZ_RES2"
249             fi
250
251             HORIZ_RESULT=$(echo $HORIZ_RES1 + $HORIZ_RES2 | bc -l)
252
253             # important: keep newline for appropriate placing below $MODES!
254             if [ -n "$HORIZ_RESULT" ] ; then
255                VIRTUAL="
256                 Virtual $HORIZ_RESULT $VERIZ_RES1"
257             fi
258          fi
259        fi
260    fi
261
262    if [[ -n $NODDC ]] ; then
263      MODES="Modes \"1024x768\" \"800x600\" \"640x480\"  \"1600x1200\" \"1280x1024\" \"1280x960\"$VIRTUAL"
264    elif [[ "$modes" == "\"1024x768\" " || -z $modes ]] ; then
265      MODES="# Modes \"1024x768\" \"800x600\" \"640x480\"  \"1600x1200\" \"1280x1024\" \"1280x960\"$VIRTUAL"
266    else
267      MODES="# Modes $modes$VIRTUAL"
268    fi
269 }
270 # }}}
271
272 # sync - get hsync/vsync settings {{{
273 sync()
274 {
275    [ -r "$MONITORINFO" ] || monitor # get monitor settings
276    vsyncval=$(awk '/Vert. Sync Range:/{print $4}' $MONITORINFO | sed 's/-/.0 - / ; s/$/.0/' | head -1)
277    hsyncval=$(awk '/Hor. Sync Range:/{print $4}'  $MONITORINFO | sed 's/-/.0 - / ; s/$/.0/' | head -1)
278    if [ -z $vsyncval ] ; then
279      vsyncval='50.0 - 60.0'
280    fi
281    if [ -z $hsyncval ] ; then
282      hsyncval='28.0 - 96.0'
283    fi
284 }
285 # }}}
286
287 # mouse {{{
288 mouse()
289 {
290    sudo $HWINFO --mouse > $MOUSEINFO
291
292    # SynPS/2 Synaptics TouchPad
293    if grep -q 'Device:.*Synaptics' "$MOUSEINFO" ; then
294     if [[ "$SYNAPTICS" == "yes" ]] ; then # check for '-nosynaptics'-option
295      MOUSEDRIVER='synaptics'
296      SYNMOUSE='InputDevice    "Synaptics"  "AlwaysCore"'
297      SYNMOUSEDETAIL="
298 Section \"InputDevice\"
299   Driver        \"synaptics\"
300   Identifier    \"Synaptics\"
301   Option        \"Device\"        \"/dev/psaux\"
302   Option        \"Protocol\"      \"auto-dev\"
303   Option        \"LeftEdge\"      \"1700\"
304   Option        \"RightEdge\"     \"5300\"
305   Option        \"TopEdge\"       \"1700\"
306   Option        \"BottomEdge\"    \"4200\"
307   Option        \"FingerLow\"     \"25\"
308   Option        \"FingerHigh\"    \"30\"
309   Option        \"ZAxisMapping\"   \"4 5\"
310   Option        \"MaxTapTime\"    \"180\"
311   Option        \"MaxTapMove\"    \"220\"
312   Option        \"VertScrollDelta\" \"100\"
313   Option        \"MinSpeed\"      \"0.06\"
314   Option        \"MaxSpeed\"      \"0.12\"
315   Option        \"AccelFactor\" \"0.0010\"
316 #  Option       \"SHMConfig\"     \"on\"
317 #  Option       \"Repeater\"      \"/dev/ps2mouse\"
318 EndSection
319 "
320     else
321      MOUSEDRIVER='mouse'
322      SYNMOUSEDETAIL=""
323      SYNMOUSE='# No synaptics touchpad detected.'
324     fi
325    else
326
327     # AlpsPS/2 ALPS TouchPad (with Synapticsdriver)
328     if grep -q 'Device:.*ALPS' "$MOUSEINFO" ; then
329      if [[ "$SYNAPTICS" == "yes" ]] ; then # check for '-nosynaptics'-option
330       MOUSEDRIVER='synaptics'
331       SYNMOUSE='InputDevice    "Synaptics"  "AlwaysCore"'
332       SYNMOUSEDETAIL="
333 Section \"InputDevice\"
334   Driver        \"synaptics\"
335   Identifier    \"Synaptics\"
336   Option        \"Device\"        \"/dev/psaux\"
337   Option        \"Protocol\"      \"auto-dev\"
338   Option        \"LeftEdge\"      \"120\"
339   Option        \"RightEdge\"     \"850\"
340   Option        \"TopEdge\"       \"120\"
341   Option        \"BottomEdge\"    \"650\"
342   Option        \"FingerLow\"     \"14\"
343   Option        \"FingerHigh\"    \"15\"
344   Option        \"ZAxisMapping\"   \"4 5\"
345   Option        \"MaxTapTime\"    \"180\"
346   Option        \"MaxTapMove\"    \"50\"
347   Option        \"MaxDoubleTapTime\"    \"100\"
348   Option        \"VertScrollDelta\" \"20\"
349   Option        \"HorizScrollDelta\" \"20\"
350   Option        \"MinSpeed\"      \"0.3\"
351   Option        \"MaxSpeed\"      \"2.00\"
352   Option        \"AccelFactor\" \"0.030\"
353   Option        \"UpDownScrolling\" \"1\"
354   Option        \"EmulateMiddleButtonTime\" \"75\"
355   Option        \"CircularScrolling\" \"1\"
356   Option        \"CircScrollDelta\" \"0.1\"
357   Option        \"CircScrollTrigger\" \"8\"
358 #  Option       \"SHMConfig\"     \"on\"
359 #  Option       \"Repeater\"      \"/dev/ps2mouse\"
360 EndSection
361 "
362      else
363       MOUSEDRIVER='mouse'
364       SYNMOUSEDETAIL=""
365       SYNMOUSE='# No alps touchpad detected.'
366      fi
367     else
368       SYNMOUSE='# No synaptics/alps touchpad present.'
369     fi
370    fi
371
372    # USB-PS/2 Optical Mouse
373    if [ -n "$USE_USB" ] ; then
374      USBMOUSE='InputDevice    "USB Mouse"     "CorePointer"'
375      USBMOUSEDETAIL="
376 Section \"InputDevice\"
377         Identifier      \"USB Mouse\"
378         Driver          \"mouse\"
379         Option          \"Device\"                \"/dev/input/mice\"
380         Option          \"Protocol\"              \"auto\"
381         Option          \"ZAxisMapping\"          \"4 5\"
382         Option          \"Buttons\"               \"5\"
383         Option          \"SendCoreEvents\"        \"true\"
384 EndSection
385 "
386    else
387      USBMOUSE='# InputDevice    "USB Mouse"     "CorePointer"'
388      USBMOUSEDETAIL=''
389    fi
390
391    if grep -q 'Device:.*Serial' "$MOUSEINFO" ; then
392      SERIAL='yes'
393      SERMOUSE='InputDevice    "Serial Mouse"     "CorePointer"'
394      SERMOUSEDETAIL="
395 Section \"InputDevice\"
396         Identifier  \"Serial Mouse\"
397         Driver      \"mouse\"
398         Option      \"Device\" \"/dev/ttyS0\"
399         Option      \"Protocol\" \"Microsoft\"
400         Option      \"Emulate3Buttons\" \"true\"
401         Option      \"Emulate3Timeout\" \"70\"
402         Option      \"SendCoreEvents\"  \"true\"
403 EndSection
404 "
405    else
406      SERMOUSE='# No serial mouse detected.'
407      SERMOUSEDETAIL=''
408    fi
409
410    # ImExPS/2 Logitech Explorer Mouse
411    # "PS2++ Logitech MX Mouse"
412    if [ -n "$USE_PS2" ] ; then
413       PS2='yes'
414       PS2MOUSE='InputDevice    "PS/2 Mouse"    "CorePointer"'
415       PS2MOUSEDETAIL="
416 Section \"InputDevice\"
417         Identifier  \"PS/2 Mouse\"
418         Driver      \"mouse\"
419         Option      \"Device\" \"/dev/input/mice\"
420         # Option      \"Device\" \"/dev/psaux\"
421         Option      \"Protocol\" \"PS/2\"
422         Option      \"Emulate3Buttons\" \"true\"
423         Option      \"Emulate3Timeout\" \"70\"
424         Option      \"SendCoreEvents\"  \"true\"
425 EndSection
426 "
427      else
428       PS2MOUSE='# InputDevice    "PS/2 Mouse"    "CorePointer"'
429       PS2MOUSEDETAIL=''
430      fi
431
432      EVDEV_MOUSE="
433 # Section \"InputDevice\"
434 #         Identifier      \"Generic Mouse\"
435 #         Driver          \"evdev\"
436 #         Option          \"Device\"                \"/dev/input/mice\"
437 #         Option          \"Protocol\"              \"auto\"
438 #         Option          \"ZAxisMapping\"          \"4 5\"
439 #         Option          \"Buttons\"               \"5\"
440 #         Option          \"SendCoreEvents\"        \"true\"
441 # EndSection
442 "
443
444    MOUSE="        $USBMOUSE
445         $PS2MOUSE
446         $SYNMOUSE
447         $SERMOUSE"
448 }
449 # }}}
450
451 # commandline parsing {{{
452 parse_options()
453 {
454    zparseopts -K -- module:=o_module help=o_help noddc=o_noddc nosync=o_nosync \
455                     vsync:=o_vsync hsync:=o_hsync mode:=o_mode force=o_force display:=o_display   \
456                     nostart=o_nostart nodpms=o_nodpms nosynaptics=o_nosynaptics nousb=o_nousb \
457                     nops2=o_nops2 genmouse=o_genmouse novref=o_novref nohsync=o_nohsync \
458                     fallback=o_fallback usb=o_usb ps2=o_ps2 composite=o_composite \
459                     xinerama=o_xinerama
460
461    if [[ $# == 0 || "$o_help" != "" || "$1" == '-h' || "$1" == '--help' ]]; then
462       usage
463    fi
464
465    if [[ "$o_force" != "" ]]; then
466       FORCE='yes'
467    fi
468
469    if [[ "$o_fallback" != "" ]]; then
470       FALLBACK="yes"
471       if [ -r /etc/X11/xorg.conf.example ] ; then
472          sudo cp /etc/X11/xorg.conf.example $XCONFIG
473          print "$bold_color$fg[blue]Copying /etc/X11/xorg.conf.example to $XCONFIG as requested via fallback option."
474       else
475          echo 'Error: /etc/X11/xorg.conf.example not readable, exiting.'
476          exit 1
477       fi
478    fi
479
480    if [[ "$o_nodpms" != "" ]]; then
481       DPMS='Option       "DPMS"      "false"'
482    else
483       DPMS='Option       "DPMS"      "true"'
484    fi
485
486    if [[ "$o_noddc" != "" ]]; then
487       NODDC="yes"
488    fi
489
490    if [[ "$o_vsync" != "" ]]; then
491       FORCE="yes"
492    fi
493
494    if [[ "$o_hsync" != "" ]]; then
495       FORCE="yes"
496    fi
497
498    if [[ "$o_nousb" != "" ]]; then
499       echo 'Warning: option -nousb is deprecated.'>&2
500    fi
501
502    if [[ "$o_usb" != "" ]]; then
503       USE_USB='yes'
504    fi
505
506    if [[ "$o_nops2" != "" ]]; then
507       echo 'Warning: optino -nops2 is deprecatedË™'>&2
508    fi
509
510    if [[ "$o_ps2" != "" ]]; then
511       USE_PS2='yes'
512    fi
513
514    if [[ "$o_genmouse" != "" ]]; then
515       GENERICMOUSE='yes'
516    fi
517
518    if [[ "$o_nosynaptics" != "" ]]; then
519       SYNAPTICS='no'
520    else
521       SYNAPTICS='yes'
522    fi
523
524    if [[ "$o_nostart" != "" ]]; then
525       NOSTART="yes"
526    fi
527
528    DISPLAY=$o_display[2]
529
530    eval WINDOWMANAGER=\${$#}
531
532    if [[ "$XKEYBOARD" == de ]] ; then
533       KEYBOARD="$KEYBOARD
534 #         Option      \"XkbVariant\" \"nodeadkeys\""
535    fi
536
537    if [ -n "$FORCE" -o ! -r "$XCONFIG" -a -z "$FALLBACK" ] ; then
538      print -n "$bold_color$fg[blue]Gathering hardware information...$fg[red]"
539
540      sync # get hsync/vsync
541
542      if [ -z "$o_hsync" ] ; then
543        o_hsync=(-hsync "$hsyncval")
544        HSYNC=$o_hsync[2]
545        HORIZSYNC="        HorizSync    $HSYNC"
546      else
547        o_hsync=(-hsync "$o_hsync[2]")
548        HSYNC=$o_hsync[2]
549        HORIZSYNC="        HorizSync    $HSYNC"
550      fi
551
552      if [ -z "$o_vsync" ] ; then
553        o_vsync=(-vsync "$vsyncval")
554        VSYNC=$o_vsync[2]
555        VERTISYNC="        VertRefresh  $VSYNC"
556      else
557        o_vsync=(-vsync "$o_vsync[2]")
558        VSYNC=$o_vsync[2]
559        VERTISYNC="        VertRefresh  $VSYNC"
560      fi
561
562      if [[ "$o_nosync" != "" ]]; then
563         HORIZSYNC="#       HorizSync   28.0 - 96.0  # deactivated via -nosync option of grml-x"
564         VERTISYNC="#       VertRefresh 50.0 - 60.0  # deactivated via -nosync option of grml-x"
565      fi
566
567      if [[ "$o_nohsync" != "" ]]; then
568         HORIZSYNC="#       HorizSync   28.0 - 96.0  # deactivated via -nohsync option of grml-x"
569      fi
570
571      if [[ "$o_novref" != "" ]]; then
572         VERTISYNC="#       VertRefresh 50.0 - 60.0  # deactivated via -novref option of grml-x"
573      fi
574
575      if [[ "$o_xinerama" != "" ]]; then
576         XINERAMA=1
577      fi
578
579      if [[ "$o_composite" != "" ]]; then
580         COMPOSITE="Section \"Extensions\"
581     Option \"Composite\" \"Enable\"
582 EndSection"
583      else
584         COMPOSITE="#Section \"Extensions\"
585 #    Option \"Composite\" \"Enable\"
586 #EndSection"
587      fi
588
589      # write hwinfo stuff
590      writehwinfo
591
592      # monitor stuff
593      MONITOR=$(awk '/monitor.1:/{print $3}' $HWINFO_TMP)
594      [[ $MONITOR != 'ddc' ]] && NODDC=yes
595
596      # module handling
597      MODULE=$o_module[2]
598      if [ -z "$MODULE" ] ; then
599        MODULE="$(getBootParam xmodule 2>/dev/null)"
600        if [ -z "$MODULE" ] ; then
601          MODULE=$(grep 'XFree86 v4 Server Module:' "${HWINFO_TMP}" | head -1 | awk '{print $5}')
602          if [ -z "$MODULE" ] ; then
603            MODULE='vesa'
604          fi
605          # hwinfo >=13.28 reports driver 'intel' instead of i810
606          if [[ "$MODULE" == 'intel' ]] ; then
607             [ -r /usr/lib/xorg/modules/drivers/intel_drv.so ] || MODULE='i810'
608          fi
609        fi
610      else
611        FORCE="yes"
612      fi
613
614      mouse    # get mouse settings
615      VGA=$(lspci | grep VGA | sed 's/.*compatible controller: //' | head -1)
616
617      MODE=$o_mode[2]
618      if [ -z $MODE ] ; then
619        B_MODE="$(getBootParam xmode 2>/dev/null)"
620        if [ -n "$B_MODE" ] ; then
621          MODES="Modes \"$B_MODE\""
622          FORCE="yes"
623        fi
624        if [ -z "$MODES" ] ; then
625           mode # get available modes
626        fi
627      else
628        MODES="Modes \"$MODE\""
629        FORCE="yes"
630      fi
631
632      print "$fg[green]done$reset_color"
633      print "$bg[black]$fg[white]$bold_color"
634      print "$fg[green]Specified windowmanager is $fg[yellow]$WINDOWMANAGER"
635      print "$fg[green]Video is $fg[yellow]$VGA$fg[green] using $bg[black]$fg[yellow]${XSERVER}$fg[cyan](${MODULE})$fg[green] Server"
636      [[ -z $HSYNC ]] && [[ -z $VSYNC ]] && print "$fg[green]Monitor is $fg[yellow]$MONITOR"
637      [[ -z $HSYNC ]] && [[ -n $VSYNC ]] && print "$fg[green]Monitor is $fg[yellow]$MONITOR$fg[green], VSYNC: $fg[yellow]$VSYNC"
638      [[ -z $VSYNC ]] && [[ -n $HSYNC ]] && print "$fg[green]Monitor is $fg[yellow]$MONITOR$fg[green], HSYNC: $fg[yellow]$HSYNC"
639      [[ -n $VSYNC ]] && [[ -n $HSYNC ]] && print "$fg[green]Monitor is $fg[yellow]$MONITOR$fg[green], HSYNC: $fg[yellow]$HSYNC $fg[green]VSYNC: $fg[yellow]$VSYNC"
640      [[ -n $modes ]] && print "$fg[green]Using default X.org modes."
641      [[ -z $modes ]] && print "$fg[green]Using Mode: $fg[yellow]$MODE"
642      print "${reset_color}"
643    fi
644 }
645 parse_options $*
646 # }}}
647
648 # check for requirements {{{
649 requirements()
650 {
651 if ! [ -x $(which hwinfo) ] ; then
652   print "$bg[black]$fg[red]${bold_color}Error: hwinfo not found in path.${reset_color}
653 Note:  run 'aptitude install hwinfo' on systems running debian.
654 Exiting.${reset_color}"
655   exit -1
656 fi
657
658 if ! [[ -d /sys ]] ; then
659   print "$bg[black]$fg[red]${bold_color}Error: mounted /sys required (for hwinfo).${reset_color}
660 You may want to add the following line to your /etc/fstab:
661
662   sysfs /sys sysfs defaults 0 0
663
664 or just run 'mount /sys'. Exiting.${reset_color}"
665   exit -1
666 fi
667 }
668 requirements
669 # }}}
670
671 # xconfig {{{
672 xconfig() {
673 cat << EOX
674 ################################################################################
675 # Filename:      $XCONFIG
676 # Purpose:       config file for xserver - generated by grml-x
677 # Bug-Reports:   see http://grml.org/bugs/
678 # Latest change: ${DATE}
679 # See also:
680 #   /usr/share/doc/xserver-xorg/   and
681 #   http://wiki.x.org/wiki/Home    and
682 #   http://ftp.x.org/pub/X11R7.0/doc/html/index.html for information on Xorg
683
684 # Refer to the xorg.conf man page and to
685 # http://ftp.x.org/pub/X11R7.0/doc/html/xorg.conf.5.html
686 # for details about the format of this file.
687 #
688 # See http://wiki.debian.org/XStrikeForce/FAQ for information
689 # regarding Xorg packages within Debian.
690 #
691 # If you would like this file to be automatically reconfigured by debian,
692 # run the following command:
693 #   sudo dpkg-reconfigure -phigh xserver-xorg
694 ################################################################################
695
696 Section "ServerLayout"
697         Identifier     "XServer Configured"
698         Screen      0  "Screen0" 0 0
699         # InputDevice    "Keyboard0"     "CoreKeyboard"
700         # InputDevice    "Generic Mouse" "CorePointer"
701 $MOUSE
702 EndSection
703
704 Section "ServerFlags"
705         Option "AllowMouseOpenFail"  "true"  # allows the server to start up even if the mouse does not work
706         Option "DontVTSwitch"        "false" # allow switching between virtual terminal
707         # Option "DontZap"             "true"  # disable <Crtl><Alt><BS> (server abort)
708         # Option "DontZoom"            "true"  # disable <Crtl><Alt><KP_+>/<KP_-> (resolution switching)
709 EndSection
710
711 Section "Files"
712         # More information:  http://ftp.x.org/pub/X11R7.0/doc/html/fonts.html
713 $XFONTS
714         # FontPath     "/usr/share/fonts/ttf/western"
715         # FontPath     "/usr/share/fonts/ttf/decoratives"
716         FontPath     "/usr/share/fonts/truetype/ttf-bitstream-vera"
717         FontPath     "/usr/share/fonts/latex-ttf-fonts"
718         FontPath     "/var/lib/defoma/x-ttcidfont-conf.d/dirs/TrueType"
719 EndSection
720
721 # Modules - see /usr/X11R6/lib/modules/fonts and /usr/X11R6/lib/modules/extensions
722 Section "Module"
723         Load  "dbe"       # double buffer extension
724         Load  "dri"       # direct rendering
725         Load  "glx"       # 3D layer / GLX extension
726         Load  "type1"     # type1 font module
727         Load  "freetype"  # freetype fonts rendering
728         Load  "extmod"    # some commonly used server extensions (e.g. shape extension)
729         Load  "record"    # recording extension
730         Load  "evdev"     # generic input handling driver on Linux
731         # Load  "vbe"       # Vesa BIOS Extension
732         # Load  "i2c"       # I2C bus
733         # Load  "int10"     # initialize graphics cards via int10 call to the BIOS
734         # Load  "v4l"       # Video for Linux
735         ## Deprecated/unneeded modules with Xorg >=7.0:
736         # Load  "speedo"    # font module (does not exist anymore)
737         # Load  "ddc"       # ddc probing of monitor (automatically loaded)
738         # Load  "GLcore"    # render OpenGL in software (automatically loaded)
739         # Load  "bitmap"    # bitmap fonts (automatically loaded)
740 # Valid entries - see /usr/lib/xorg/modules/[extensions/]
741 # afb bitmap cfb cfb16 cfb24 cfb32 cw damage dbe ddc dri drm extmod fb
742 # fbdevhw freetype GLcore glx i2c int10 int10 layer mfb pcidata rac ramdac
743 # record scanpci shadow shadowfb type1 vbe vgahw xaa xf1bpp xf24_32bpp xf4bpp
744 # xf8_16bpp xf8_32bpp xtrap
745 EndSection
746
747 # If you'd like to switch the positions of your capslock and control keys, use:
748 # Option "XkbOptions" "ctrl:swapcaps"
749 # Or if you just want both to be control, use:
750 # Option "XkbOptions" "ctrl:nocaps"
751 # More information: http://ftp.x.org/pub/X11R7.0/doc/html/XKB-Config.html
752 # Section "InputDevice"
753 #         Identifier  "Keyboard0"
754 #         Option      "CoreKeyboard"
755 #         $KEYBOARD
756 #         # Option      "XkbOptions" "ctrl:swapcaps,grp:alt_shift_toggle,grp_led:scroll,compose:menu"
757 # EndSection
758
759 # More information: http://ftp.x.org/pub/X11R7.0/doc/html/mouse.html
760 ${USBMOUSEDETAIL}${PS2MOUSEDETAIL}${SERMOUSEDETAIL}${SYNMOUSEDETAIL}${EVDEV_MOUSE}
761 Section "Monitor"
762         Identifier   "Monitor0"
763 #       ModelName    "Old Monitor (no DDC)"
764         $DPMS
765 #       HorizSync    28.0 - 78.0 # Warning: This may fry very old Monitors
766 #       HorizSync    28.0 - 96.0 # Warning: This may fry old Monitors
767 $HORIZSYNC
768 #       VertRefresh  50.0 - 76.0 # Very conservative. May flicker.
769 #       VertRefresh  50.0 - 60.0 # Extreme conservative. Will flicker. TFT default.
770 $VERTISYNC
771 # Need more information?
772 #  http://xtiming.sourceforge.net/cgi-bin/xtiming.pl
773 #  http://en.tldp.org/HOWTO/XFree86-Video-Timings-HOWTO/
774 EndSection
775
776 Section "Device"
777         ### Available Driver options are:
778         ## sw_cursor is needed for some ati and radeon cards
779         # Option     "sw_cursor"
780         # Option     "hw_cursor"
781         # Option     "NoAccel"
782         # Option     "ShowCache"
783         # Option     "ShadowFB"
784         # Option     "UseFBDev"
785         # Option     "Rotate"
786         ## xorg + nvidia:
787         # Option "RenderAccel" "true"
788         # Option "AllowGLXWithComposite" "true"
789         Identifier  "Card0"
790         # The following line is auto-generated by grml-x
791         Driver      "$MODULE"
792         VendorName  "All"
793         BoardName   "All"
794         ## Workaround for drivers (for example radeon) which might send output to wrong device:
795         # Option "MonitorLayout" "LVDS, AUTO"
796         # Option "MonitorLayout" "LVDS, CRT"
797         # Option "MonitorLayout" "NONE, STV"
798         # Option "MonitorLayout" "LVDS"
799         ## Specify BusID:
800         # BusID       "PCI:1:0:0"
801 EndSection
802
803 Section "Screen"
804         Identifier "Screen0"
805         Device     "Card0"
806         Monitor    "Monitor0"
807         DefaultColorDepth 16
808         SubSection "Display"
809                 Depth     1
810                 $MODES
811         EndSubSection
812         SubSection "Display"
813                 Depth     4
814                 $MODES
815         EndSubSection
816         SubSection "Display"
817                 Depth     8
818                 $MODES
819         EndSubSection
820         SubSection "Display"
821                 Depth     15
822                 $MODES
823         EndSubSection
824         SubSection "Display"
825                 Depth     16
826                 $MODES
827         EndSubSection
828         SubSection "Display"
829                 Depth     24
830                 $MODES
831         EndSubSection
832         SubSection "Display"
833                 Depth     32
834                 $MODES
835         EndSubSection
836 EndSection
837
838 # Make sure you have the relevant Debian packages on your system
839 # to be able to use DRI (libgl1-mesa-dri for example)
840 Section "DRI"
841         Mode 0666
842 EndSection
843
844 $COMPOSITE
845
846 ## END OF FILE #################################################################
847 EOX
848 }
849 # }}}
850
851 # writeit {{{
852 writeit() {
853     XCONFTMP="/tmp/xconfig.$$"
854     xconfig > $XCONFTMP
855     # we do not want to have two CorePointers, deactivate one therefore
856     if grep -Eq '^[[:space:]]+InputDevice[ ]+"USB Mouse"[ ]+"CorePointer"' $XCONFTMP ; then
857        if grep -Eq '^[[:space:]]+InputDevice[ ]+"PS/2 Mouse"[ ]+"CorePointer"' $XCONFTMP ; then
858           sed -i 's|InputDevice.*PS/2.*CorePointer|# & # deactivated to avoid two CorePointers|' $XCONFTMP
859        fi
860     fi
861     [ -f $XCONFIG ] && sudo mv -f $XCONFIG $XCONFIG.old
862     sudo mv $XCONFTMP $XCONFIG
863     sudo chown root.root $XCONFIG
864     sudo chmod 644 $XCONFIG
865 }
866 # }}}
867
868 # writeconfig {{{
869 function writeconfig
870 {
871   if [[ ! -f $XCONFIG ]] ; then
872     print -n "$bold_color$fg[blue]Creating $XCONFIG: $fg[red]"
873     writeit && print "$fg[green]done$reset_color"
874   else
875     if [ -z "$FORCE" -a -z "$FALLBACK" ] ; then
876        print "$bold_color$fg[blue]Notice: $XCONFIG exists already.
877 Use the force-option (-force) to force creation.
878 $fg[red]"
879     fi
880   fi
881   if [[ -n "$FORCE" ]] ; then
882     print "$bold_color$fg[blue]Force-switch or manual option(s) detected:"
883     print -n "\-> Moving eventual existing $XCONFIG to ${XCONFIG}.old: $fg[red]"
884     writeit && print "$fg[green]done$reset_color"
885   fi
886 }
887 # }}}
888
889 # runit {{{
890 function runit
891 {
892   writeconfig
893   if [ -z "$NOSTART" ] ; then
894     print "$reset_color"
895     if [ -x /etc/init.d/xorg-common ] ; then
896       sudo /etc/init.d/xorg-common start
897     else
898       if [ -x /etc/init.d/xfree86-common ] ; then
899         sudo /etc/init.d/xfree86-common start
900       fi
901     fi
902     print ""
903     if [ -z "$DISPLAY" ] ; then
904       print "$bold_color$fg[green]Now trying to run startx.$reset_color"
905       startx $XINITRC -- $XOPTS
906     else
907       print "$bold_color$fg[green]Now trying to run startx on display $DISPLAY.$reset_color"
908       startx $XINITRC -- :$DISPLAY $XOPTS
909     fi
910   else
911     print "$bold_color$fg[blue]Not running startx as requested via option.$reset_color"
912   fi
913   return 1
914 }
915 # }}}
916
917 # failed {{{
918 function failed
919 {
920   print "$fg[red]"
921   if [ -z "$ROOT" ] ; then
922     if [[ $(tty) == /dev/pts/* ]] ; then
923       print "It seems you are running $PROGRAMNAME from inside GNU screen.
924 Notice that this might fail if running as user grml!
925 Please exit screen and try to run $PROGRAMNAME again."
926     fi
927   fi
928   print "Run the following commands for getting information on your hardware:
929   hwinfo --gfxcard
930   discover -v --data-path=xfree86/server/device/driver display
931   xdebconfigurator -c -d -i -x
932   get-edid | parse-edid
933
934 Do you want to go the debian way of life? Run:
935   aptitude install xorg read-edid mdetect hwinfo xdebconfigurator
936   dpkg-reconfigure -phigh xserver-xorg
937
938 Problems with the module used for X? Try to load another one or
939 fall back to module vesa:
940   $PROGRAMNAME -module radeon ...
941   $PROGRAMNAME -module vesa  ...
942
943 Do you want to deactivate a present synaptics touchpad? Run:
944   $PROGRAMNAME -nosynaptics ...
945
946 Your monitor is very old and/or does not support DDC-probing?
947   $PROGRAMNAME -noddc ...
948
949 Do you want to create a x configuration file but do not start X?
950   $PROGRAMNAME -nostart ...
951
952 Monitor frequency too high or too low? Just specify hsync/vsync manually:
953   $PROGRAMNAME -hsync 30-65 ...
954   $PROGRAMNAME -hsync 30-65 -vsync 50-60 ...
955
956 Want to adjust the resolution? Use the mode-switch:
957   $PROGRAMNAME -mode 1024x768 ...
958   $PROGRAMNAME -mode '1280x1024 1024x768' ...
959
960 Problems? Use vesa with resolution 1024x768:
961   $PROGRAMNAME -module vesa -mode 1024x768 ...
962
963 Still problems with X? Use the fallback option:
964   $PROGRAMNAME -fallback ...
965
966 To adjust resolution while running X execute:
967   xrandr -s '1024x768'
968
969 More information on grml-x can be found in the manual page: man grml-x
970
971 Report bugs, send wishes and feedback to the grml team:
972 http://grml.org/bugs/ - contact (at) grml.org
973 "
974 print -n "$reset_color"
975 }
976 # }}}
977
978 # cleanup {{{
979 cleanup()
980 {
981   rm -f $HWINFO_TMP
982   rm -f $MONITORINFO
983   rm -f $MOUSEINFO
984   rm -f $XCONFTMP
985 }
986 cleanup
987 # }}}
988
989 # xinitrc {{{
990   if ! [ -x "$(which $WINDOWMANAGER)" ] ; then
991      print "$bg[black]$fg[red]${bold_color}Fatal: windowmanager $fg[blue]$WINDOWMANAGER$fg[red] not executable, startx won' work.${reset_color}">&2
992      bailout
993   fi
994   if [ -w "$XINITRC" ] ; then
995     sed -i "s|^[^#]*exec.*|  exec $WINDOWMANAGER|g" $XINITRC
996     runit || failed
997   else
998     echo -e "#!/bin/sh\n  exec $WINDOWMANAGER" >> $XINITRC
999     runit || failed
1000   fi
1001 # }}}
1002
1003 ## END OF FILE #################################################################
1004 # vim:foldmethod=marker expandtab ai ft=zsh