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