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