startx.cpp revision a8eb8bc0
1XCOMM!SHELL_CMD
2
3XCOMM
4XCOMM This is just a sample implementation of a slightly less primitive
5XCOMM interface than xinit.  It looks for user .xinitrc and .xserverrc
6XCOMM files, then system xinitrc and xserverrc files, else lets xinit choose
7XCOMM its default.  The system xinitrc should probably do things like check
8XCOMM for .Xresources files and merge them in, start up a window manager,
9XCOMM and pop a clock and several xterms.
10XCOMM
11XCOMM Site administrators are STRONGLY urged to write nicer versions.
12XCOMM
13
14unset DBUS_SESSION_BUS_ADDRESS
15unset SESSION_MANAGER
16
17#ifdef __APPLE__
18
19XCOMM Check for /usr/bin/X11 and BINDIR in the path, if not add them.
20XCOMM This allows startx to be placed in a place like /usr/bin or /usr/local/bin
21XCOMM and people may use X without changing their PATH.
22XCOMM Note that we put our own bin directory at the front of the path, and
23XCOMM the standard system path at the back, since if you are using the Xorg
24XCOMM server there's a pretty good chance you want to bias the Xorg clients
25XCOMM over the old system's clients.
26
27XCOMM First our compiled path
28bindir=__bindir__
29
30case $PATH in
31    *:$bindir | *:$bindir:* | $bindir:*) ;;
32    *) PATH=$bindir:$PATH ;;
33esac
34
35XCOMM Now the "old" compiled path
36oldbindir=/usr/X11R6/bin
37
38if [ -d "$oldbindir" ] ; then
39    case $PATH in
40        *:$oldbindir | *:$oldbindir:* | $oldbindir:*) ;;
41        *) PATH=$PATH:$oldbindir ;;
42    esac
43fi
44
45XCOMM Bourne shell does not automatically export modified environment variables
46XCOMM so export the new PATH just in case the user changes the shell
47export PATH
48#endif
49
50userclientrc=$HOME/.xinitrc
51sysclientrc=XINITDIR/xinitrc
52
53userserverrc=$HOME/.xserverrc
54sysserverrc=XINITDIR/xserverrc
55defaultclient=XTERM
56defaultserver=XSERVER
57defaultclientargs=""
58defaultserverargs="-noretro"
59defaultdisplay=""
60clientargs=""
61serverargs=""
62vtarg=""
63
64#ifdef __APPLE__
65
66if [ "x$X11_PREFS_DOMAIN" = x ] ; then
67    export X11_PREFS_DOMAIN=BUNDLE_ID_PREFIX".X11"
68fi
69
70XCOMM Initialize defaults (this will cut down on "safe" error messages)
71if ! defaults read $X11_PREFS_DOMAIN cache_fonts > /dev/null 2>&1 ; then
72    defaults write $X11_PREFS_DOMAIN cache_fonts -bool true
73fi
74
75if ! defaults read $X11_PREFS_DOMAIN no_auth > /dev/null 2>&1 ; then
76    defaults write $X11_PREFS_DOMAIN no_auth -bool false
77fi
78
79if ! defaults read $X11_PREFS_DOMAIN nolisten_tcp > /dev/null 2>&1 ; then
80    defaults write $X11_PREFS_DOMAIN nolisten_tcp -bool true
81fi
82
83if ! defaults read $X11_PREFS_DOMAIN enable_iglx > /dev/null 2>&1 ; then
84    defaults write $X11_PREFS_DOMAIN enable_iglx -bool false
85fi
86
87XCOMM First, start caching fonts
88if [ x`defaults read $X11_PREFS_DOMAIN cache_fonts` = x1 ] ; then
89    if [ -x $bindir/font_cache ] ; then
90        $bindir/font_cache &
91    elif [ -x $bindir/font_cache.sh ] ; then
92        $bindir/font_cache.sh &
93    elif [ -x $bindir/fc-cache ] ; then
94        $bindir/fc-cache &
95    fi
96fi
97
98if [ -x __libexecdir__/privileged_startx ] ; then
99	# Don't push this into the background becasue it can cause
100	# a race to create /tmp/.X11-unix
101	__libexecdir__/privileged_startx
102fi
103
104if [ x`defaults read $X11_PREFS_DOMAIN no_auth` = x0 ] ; then
105    enable_xauth=1
106else
107    enable_xauth=0
108fi
109
110if [ x`defaults read $X11_PREFS_DOMAIN nolisten_tcp` = x1 ] ; then
111    defaultserverargs="$defaultserverargs -nolisten tcp"
112else
113    defaultserverargs="$defaultserverargs -listen tcp"
114fi
115
116if [ x`defaults read $X11_PREFS_DOMAIN enable_iglx` = x1 ] ; then
117    defaultserverargs="$defaultserverargs +iglx"
118else
119    defaultserverargs="$defaultserverargs -iglx"
120fi
121
122XCOMM The second check is the real one.  The first is to hopefully avoid
123XCOMM needless syslog spamming.
124if defaults read $X11_PREFS_DOMAIN 2> /dev/null | grep -q 'dpi' && defaults read $X11_PREFS_DOMAIN dpi > /dev/null 2>&1 ; then
125    defaultserverargs="$defaultserverargs -dpi `defaults read $X11_PREFS_DOMAIN dpi`"
126fi
127
128#else
129enable_xauth=1
130#endif
131
132XCOMM Automatically determine an unused $DISPLAY
133d=0
134while true ; do
135    [ -e "/tmp/.X$d-lock" -o -S "/tmp/.X11-unix/X$d" ] || break
136    d=$(($d + 1))
137done
138defaultdisplay=":$d"
139unset d
140
141whoseargs="client"
142while [ x"$1" != x ]; do
143    case "$1" in
144    XCOMM '' required to prevent cpp from treating "/*" as a C comment.
145    /''*|\./''*)
146	if [ "$whoseargs" = "client" ]; then
147	    if [ x"$client" = x ] && [ x"$clientargs" = x ]; then
148		client="$1"
149	    else
150		clientargs="$clientargs $1"
151	    fi
152	else
153	    if [ x"$server" = x ] && [ x"$serverargs" = x ]; then
154		server="$1"
155	    else
156		serverargs="$serverargs $1"
157	    fi
158	fi
159	;;
160    --)
161	whoseargs="server"
162	;;
163    *)
164	if [ "$whoseargs" = "client" ]; then
165	    clientargs="$clientargs $1"
166	else
167	    XCOMM display must be the FIRST server argument
168	    if [ x"$serverargs" = x ] && @@
169		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
170		display="$1"
171	    else
172		serverargs="$serverargs $1"
173	    fi
174	fi
175	;;
176    esac
177    shift
178done
179
180XCOMM process client arguments
181if [ x"$client" = x ]; then
182    client=$defaultclient
183
184    XCOMM For compatibility reasons, only use startxrc if there were no client command line arguments
185    if [ x"$clientargs" = x ]; then
186        if [ -f "$userclientrc" ]; then
187            client=$userclientrc
188        elif [ -f "$sysclientrc" ]; then
189            client=$sysclientrc
190        fi
191    fi
192fi
193
194XCOMM if no client arguments, use defaults
195if [ x"$clientargs" = x ]; then
196    clientargs=$defaultclientargs
197fi
198
199XCOMM process server arguments
200if [ x"$server" = x ]; then
201    server=$defaultserver
202
203#ifdef __linux__
204    XCOMM When starting the defaultserver start X on the current tty to avoid
205    XCOMM the startx session being seen as inactive:
206    XCOMM "https://bugzilla.redhat.com/show_bug.cgi?id=806491"
207    tty=$(tty)
208    if expr "$tty" : '/dev/tty[0-9][0-9]*$' > /dev/null; then
209        tty_num=$(echo "$tty" | grep -oE '[0-9]+$')
210        vtarg="vt$tty_num -keeptty"
211    fi
212#endif
213
214    XCOMM For compatibility reasons, only use xserverrc if there were no server command line arguments
215    if [ x"$serverargs" = x -a x"$display" = x ]; then
216	if [ -f "$userserverrc" ]; then
217	    server=$userserverrc
218	elif [ -f "$sysserverrc" ]; then
219	    server=$sysserverrc
220	fi
221    fi
222fi
223
224XCOMM if no server arguments, use defaults
225if [ x"$serverargs" = x ]; then
226    serverargs=$defaultserverargs
227fi
228
229XCOMM if no vt is specified add vtarg (which may be empty)
230have_vtarg="no"
231for i in $serverargs; do
232    if expr "$i" : 'vt[0-9][0-9]*$' > /dev/null; then
233        have_vtarg="yes"
234    fi
235done
236if [ "$have_vtarg" = "no" ]; then
237    serverargs="$serverargs $vtarg"
238fi
239
240XCOMM if no display, use default
241if [ x"$display" = x ]; then
242    display=$defaultdisplay
243fi
244
245if [ x"$enable_xauth" = x1 ] ; then
246    if [ x"$XAUTHORITY" = x ]; then
247        XAUTHORITY=$HOME/.Xauthority
248        export XAUTHORITY
249    fi
250
251    removelist=
252
253    XCOMM set up default Xauth info for this machine
254    case `uname` in
255    Linux*)
256        if [ -z "`hostname --version 2>&1 | grep GNU`" ]; then
257            hostname=`hostname -f`
258        else
259            hostname=`hostname`
260        fi
261        ;;
262    *)
263        hostname=`hostname`
264        ;;
265    esac
266
267    authdisplay=${display:-:0}
268#if defined(HAS_COOKIE_MAKER) && defined(MK_COOKIE)
269    mcookie=`MK_COOKIE`
270#else
271    if [ -r /dev/urandom ]; then
272        mcookie=`dd if=/dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
273    else
274        mcookie=`dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
275    fi
276#endif
277    if test x"$mcookie" = x; then
278        echo "Couldn't create cookie"
279        exit 1
280    fi
281    dummy=0
282
283    XCOMM create a file with auth information for the server. ':0' is a dummy.
284    xserverauthfile=$HOME/.serverauth.$$
285    trap "rm -f '$xserverauthfile'" HUP INT QUIT ILL TRAP BUS TERM
286    xauth -q -f "$xserverauthfile" << EOF
287add :$dummy . $mcookie
288EOF
289#if defined(__APPLE__) || defined(__CYGWIN__)
290    xserverauthfilequoted=$(echo ${xserverauthfile} | sed "s/'/'\\\\''/g")
291    serverargs=${serverargs}" -auth '"${xserverauthfilequoted}"'"
292#else
293    serverargs=${serverargs}" -auth "${xserverauthfile}
294#endif
295
296    XCOMM now add the same credentials to the client authority file
297    XCOMM if '$displayname' already exists do not overwrite it as another
298    XCOMM server may need it. Add them to the '$xserverauthfile' instead.
299    for displayname in $authdisplay $hostname$authdisplay; do
300        authcookie=`XAUTH list "$displayname" @@
301        | sed -n "s/.*$displayname[[:space:]*].*[[:space:]*]//p"` 2>/dev/null;
302        if [ "z${authcookie}" = "z" ] ; then
303            XAUTH -q << EOF
304add $displayname . $mcookie
305EOF
306        removelist="$displayname $removelist"
307        else
308            dummy=$(($dummy+1));
309            XAUTH -q -f "$xserverauthfile" << EOF
310add :$dummy . $authcookie
311EOF
312        fi
313    done
314fi
315
316#if defined(__APPLE__) || defined(__CYGWIN__)
317eval XINIT \"$client\" $clientargs -- \"$server\" $display $serverargs
318#else
319XINIT "$client" $clientargs -- "$server" $display $serverargs
320#endif
321retval=$?
322
323if [ x"$enable_xauth" = x1 ] ; then
324    if [ x"$removelist" != x ]; then
325        XAUTH remove $removelist
326    fi
327    if [ x"$xserverauthfile" != x ]; then
328        rm -f "$xserverauthfile"
329    fi
330fi
331
332/*
333 * various machines need special cleaning up
334 */
335#ifdef __linux__
336if command -v deallocvt > /dev/null 2>&1; then
337    deallocvt
338fi
339#endif
340
341#if defined(sun)
342kbd_mode -a
343#endif
344
345exit $retval
346
347