slitaz-tools view tinyutils/tazx @ rev 615

tazx: only root can config Xorg (Thanks Stephane)
author Christophe Lincoln <pankso@slitaz.org>
date Fri Jun 03 12:34:16 2011 +0200 (2011-06-03)
parents 221221001c4b
children cc4df6554e18
line source
1 #!/bin/sh
2 #
3 # Tazx - Ncurses X configuration for SliTaz GNU/Linux using Dialog boxes.
4 # This tinyutils is part of slitaz-tools. Tazx can configure Xorg with
5 # several Window Managers.
6 #
7 # (c) 2011 SliTaz GNU/Linux - GNU gpl v3.
8 # Authors: Christophe Lincoln <pankso@slitaz.org>
9 # Pascal Bellard <pascal.bellard@slitaz.org>
10 #
11 : ${DIALOG=tazdialog}
13 # Default value.
14 WM=openbox
16 # Default user for config files in Live mode, id is 1000 since it is
17 # created by /etc/init.d/bootopts.sh.
18 USER=`cat /etc/passwd | grep 1000 | cut -d ":" -f 1`
21 # Functions
23 # Be sure we're root.
24 check_root() {
25 [ $(id -u) != 0 ] && gettext -e "\nYou must be root to cook.\n\n" && exit 0
26 }
28 # Populate xorg.conf.d.
29 xorg_conf_d()
30 {
31 # Define the xorg.conf.d (can be /etc/X11/xorg.conf.d or /usr/share/X11/xorg.conf.d)
32 xorg_config=/etc/X11/xorg.conf.d
34 # Define the xorg.conf.new place.
35 xorg_template=/root/xorg.conf.new
37 # Obtain a default configuration file from Xorg.
38 Xorg -configure :1
40 # Backup existing config.
41 tar -cf $xorg_config/../Previous_xorg.conf.d.tar $xorg_config/ &> /dev/null
43 # Put the differents sections in separate files in the configuration directory.
44 sed -e '/Section "ServerLayout"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template | grep -v Core > $xorg_config/10-ServerLayout.conf
45 sed -e '/Section "Files"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template > $xorg_config/20-Files.conf
46 sed -e '/Section "Module"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template > $xorg_config/30-Module.conf
47 sed -e '/Section "Monitor"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template > $xorg_config/50-Monitor.conf
48 sed -e '/Section "Device"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template > $xorg_config/60-Device.conf
49 sed -e '/Section "Screen"/,/EndSection/!d' -e "s/EndSection/EndSection\n/" $xorg_template > $xorg_config/70-Screen.conf
51 # Remove the template.
52 rm $xorg_template
54 # Configure the keyboard with the right keymap.
55 keymap=`cat /etc/keymap.conf`
56 keyboard_config=$xorg_config/40-Keyboard.conf
57 echo 'Section "InputClass"
58 Identifier "Keyboard Defaults"
59 MatchIsKeyboard "yes"' > $keyboard_config
60 case $keymap in
61 fr_CH-latin1)
62 # Swiss FrenCH
63 echo ' Option "XkbLayout" "ch"
64 Option "XkbVariant" "fr"' >> $keyboard_config ;;
65 uk)
66 # Englisk UK
67 echo ' Option "XkbLayout" "gb"' >> $keyboard_config ;;
68 ru)
69 # Russian
70 echo ' Option "XkbLayout" "us,ru(winkeys)"
71 Option "XkbVariant" "grp:alt_shift_toggle"' >> $keyboard_config ;;
72 slovene)
73 # Slovenian
74 echo ' Option "XkbLayout" "si"
75 Option "XkbOptions" "grp:alt_shift_toggle"' >> $keyboard_config ;;
76 us-acentos)
77 echo ' Option "XkbLayout" "us"
78 Option "XkbVariant" "intl"' >> $keyboard_config ;;
79 *)
80 # Use clean /etc/keymap.conf value.
81 keymap=${keymap%-latin1}
82 keymap=${keymap%-lat2}
83 keymap=${keymap%-lat6}
84 keymap=${keymap%-abnt2}
85 echo ' Option "XkbLayout" "'$keymap\" >> $keyboard_config
86 ;;
87 esac
89 echo 'Endsection' >> $keyboard_config
91 # Create a xorg.conf if needed.
92 [ ! -f /etc/X11/xorg.conf ] && echo "# You can put here your own Xorg configurations.
93 # This configuration file is read before all files in /etc/X11/xorg.conf.d
94 # This file will not be erased by any updates." > /etc/X11/xorg.conf
95 }
97 # Install xorg server.
98 install_xorg()
99 {
100 tazpkg recharge
101 exec 3>&1
102 value=`$DIALOG --clear --colors --title " Install Xorg " \
103 --menu \
104 "The 'tazx' application helps you to select your X driver." 16 70 5 \
105 $(grep xorg-xf86-video /var/lib/tazpkg/packages.list | cut -d- -f4 | while read x; do echo $x; echo driver; done) \
106 "quit" "Quitter" \
107 2>&1 1>&3`
108 retval=$?
109 exec 3>&-
110 # Continue, exit...
111 case $retval in
112 1)
113 echo "Cancel pressed..."
114 exit 0 ;;
115 255)
116 if test -z "$value"; then
117 echo "ESC pressed..."
118 exit 0
119 fi ;;
120 esac
121 # Set selected value.
122 case $value in
123 quit)
124 echo "Quit..."
125 exit 0 ;;
126 *)
127 tazpkg get-install xorg-server
128 tazpkg get-install xorg-xf86-video-$value
129 xorg_conf_d
130 XSERVER=Xorg ;;
131 esac
132 }
134 # Screen configuration dialog.
135 screen_config_dialog()
136 {
137 exec 3>&1
138 value=`$DIALOG \
139 --clear --colors \
140 --title " Configure X " \
141 --menu \
142 "The 'tazx' application helps you to configure your Xorg.\n\
143 Window Manager : \Z2$WM\Zn \n\
144 X server : \Z2Xorg\Zn" 16 70 5 \
145 "xorg" "Install or reconfigure Xorg" \
146 "quit" "Quit Tazx utility" \
147 2>&1 1>&3`
148 retval=$?
149 exec 3>&-
150 # Continue, exit or help...
151 case $retval in
152 0)
153 continue ;;
154 1)
155 echo "Cancel pressed..."
156 exit 0 ;;
157 255)
158 if test -n "$value"; then
159 continue
160 else
161 echo "ESC pressed..."
162 exit 0
163 fi ;;
164 esac
165 # Set selected value.
166 case $value in
167 xorg)
168 install_xorg ;;
169 *)
170 continue ;;
171 esac
172 }
174 # Window manager specific configuration.
175 wm_config()
176 {
177 case $WM in
178 ob|openbox)
179 WM=openbox-session
180 # Check if a personal autostart script exists if OB is installed.
181 if [ -d "/var/lib/tazpkg/installed/openbox" ]; then
182 if [ ! -f "$HOME/.config/openbox/autostart.sh" ]; then
183 mkdir -p $HOME/.config/openbox
184 cp /etc/xdg/openbox/autostart.sh $HOME/.config/openbox
185 fi
186 # Script for default user (uid=1000).
187 if [ ! -f "/home/$USER/.config/openbox/autostart.sh" ]; then
188 mkdir -p /home/$USER/.config/openbox
189 cp /etc/xdg/openbox/autostart.sh /home/$USER/.config/openbox
190 fi
191 if [ ! -f "/home/$USER/.config/openbox/menu.xml" ]; then
192 mkdir -p /home/$USER/.config/openbox
193 cp /etc/xdg/openbox/menu.xml /home/$USER/.config/openbox
194 fi
195 chown -R $USER.users /home/$USER/.config
196 fi ;;
197 jwm)
198 WM=jwm
199 JWM_CONFIG=$HOME/.jwmrc
200 if [ -d "/var/lib/tazpkg/installed/jwm" ]; then
201 if [ ! -f "$JWM_CONFIG" ]; then
202 cp /etc/jwm/system.jwmrc $JWM_CONFIG
203 fi
204 # In Live mode default user/root JWM config does not exist and
205 # $HOME is not set, this is because tazx is executed by boot
206 # scripts.
207 if [ ! -f "/home/$USER/.jwmrc" ]; then
208 cp /etc/jwm/system.jwmrc /home/$USER/.jwmrc
209 chown $USER.users /home/$USER/.jwmrc
210 fi
211 if [ ! -f "/root/.jwmrc" -a `id -u` = 0 ]; then
212 cp /etc/jwm/system.jwmrc /root/.jwmrc
213 fi
214 fi ;;
215 pekwm)
216 WM=pekwm
217 if [ -d "/var/lib/tazpkg/installed/pekwm" ]; then
218 if [ -d "$HOME/.pekwm" ]; then
219 cp -R /etc/pekwm $HOME/.pekwm
220 fi
221 # In Live mode we want config before starting pekwm the first time.
222 if [ ! -d "/home/$USER/.pekwm" ]; then
223 cp -R /etc/pekwm /home/$USER/.pekwm
224 chown -R $USER.users /home/$USER/.pekwm
225 chmod +x /home/$USER/.pekwm/start
226 fi
227 if [ ! -d "/root/.pekwm" -a `id -u` = 0 ]; then
228 cp -R /etc/pekwm /root/.pekwm
229 chmod +x /root/.pekwm/start
230 fi
231 fi ;;
232 e17|enlightenment)
233 WM=enlightenment_start ;;
234 fluxbox)
235 WM=startfluxbox ;;
236 dwm|karmen)
237 WM=$WM-session ;;
238 awesome)
239 WM=awesome ;;
240 xfce|xfce4)
241 WM=xfce4-session ;;
242 esac
243 }
245 # Sample xinitrc for user (WM can be specified with F1 at slim login).
246 xinitrc_sample()
247 {
248 cat > $FILE << "EOF"
249 # ~/.xinitrc: Executed by slim login manager to startx X session.
250 # You can use F1 with Slim to change your window manager or configure
251 # it permanently with your personal applications.conf file.
252 #
253 . $HOME/.config/slitaz/applications.conf
255 case $1 in
256 e17|enlightenment*)
257 exec enlightenment_start ;;
258 openbox|openbox-session|ob)
259 exec openbox-session ;;
260 dwm|dwm-session)
261 exec dwm-session ;;
262 fluxbox|startfluxbox)
263 exec startfluxbox ;;
264 awesome)
265 exec awesome ;;
266 pekwm)
267 exec pekwm ;;
268 karmen|karmen-session)
269 exec karmen-session ;;
270 jwm)
271 lxpanel &
272 exec jwm ;;
273 xfce|xfce4|xfce4-session)
274 xfce4-session ;;
275 *)
276 exec $WINDOW_MANAGER ;;
277 esac
278 EOF
279 # Set default WM in applications.conf user file. Default WM can be
280 # configured graphically with 'desktopbox tazapps'
281 . $CONFIG
282 sed -i s/"WINDOW_MANAGER=.*"/"WINDOW_MANAGER=\"$WM\""/ \
283 $CONFIG
284 }
286 # ~/.xinitrc for X login from a DM.
287 creat_xinitrc()
288 {
289 FILE=$HOME/.xinitrc
290 CONFIG=$HOME/.config/slitaz/applications.conf
291 if [ ! -f $CONFIG ]; then
292 mkdir -p $HOME/.config/slitaz
293 cp /etc/slitaz/applications.conf $CONFIG
294 fi
295 xinitrc_sample
296 # .xinitrc and config for /etc/skel so new added user will get
297 # a working X session.
298 if test $(id -u) = 0; then
299 FILE=/etc/skel/.xinitrc
300 CONFIG=/etc/skel/.config/slitaz/applications.conf
301 mkdir -p /etc/skel/.config/slitaz
302 cp -f /etc/slitaz/applications.conf $CONFIG
303 xinitrc_sample
304 fi
305 # In Live mode default user needs a xinitrc, since tazx
306 # is executed only by root.
307 CONFIG=/home/$USER/.config/slitaz/applications.conf
308 if [ ! -f $CONFIG ]; then
309 mkdir -p /home/$USER/.config/slitaz
310 cp /etc/slitaz/applications.conf $CONFIG
311 fi
312 chown -R $USER.users /home/$USER/.config/slitaz
313 if [ ! -f /home/$USER/.xinitrc ]; then
314 FILE=/home/$USER/.xinitrc
315 xinitrc_sample
316 chown $USER.users $FILE
317 fi
318 }
320 # Create ~/.xsession to keep the configuration selected (used
321 # only by startx, Slim login manager uses .xinitrc).
322 creat_xsession()
323 {
324 [ -f $HOME/.xsession ] && cp -f $HOME/.xsession $HOME/.previous_xsession
325 cat > $HOME/.xsession << _EOF_
326 # ~/.xsession: Start X window session manually on your system (startx).
327 #
328 Xorg &
329 #xterm &
330 _EOF_
331 # LXpanel by default with JWM.
332 if [ "$WM" = "jwm" ]; then
333 echo 'lxpanel &' >> $HOME/.xsession
334 fi
335 echo "exec $WM" >> $HOME/.xsession
336 chmod 700 $HOME/.xsession
337 }
340 # Commands - WM can be specified on cmdline.
342 case "$1" in
343 install-xorg)
344 check_root
345 if [ -n "$2" ]; then
346 WM=$2
347 fi
348 echo "xorg" > /etc/X11/screen.conf
349 install_xorg
350 wm_config
351 creat_xinitrc
352 creat_xsession ;;
353 config-xorg)
354 check_root
355 if [ -n "$2" ]; then
356 WM=$2
357 fi
358 echo "xorg" > /etc/X11/screen.conf
359 wm_config
360 creat_xinitrc
361 creat_xsession
362 xorg_conf_d
363 if grep -qs screen= /proc/cmdline ; then
364 MODE="$(sed 's/.*screen=\([0-9]*x[0-9]*\).*/\1/' < /proc/cmdline)"
365 sed -i "s/.*EndSubSection.*/\\t\\tModes\\t\"$MODE\"\\n&/" \
366 /etc/X11/xorg.conf.d/70-Screen.conf
367 fi ;;
368 *)
369 # User can get a new .xinitrc with tazx from cmdline.
370 if [ -n "$1" ]; then
371 WM=$1
372 fi
373 if test $(id -u) = 0; then
374 echo "xorg" > /etc/X11/screen.conf
375 screen_config_dialog
376 fi
377 wm_config
378 creat_xinitrc
379 creat_xsession ;;
380 esac
382 exit 0