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