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