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