startx.cpp revision 36ffeb23
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#if defined(__SCO__) || defined(__UNIXWARE__) || defined(__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
36#ifdef __APPLE__
37oldbindir=/usr/X11R6/bin
38#else
39oldbindir=/usr/bin/X11
40#endif
41
42if [ -d "$oldbindir" ] ; then
43    case $PATH in
44        *:$oldbindir | *:$oldbindir:* | $oldbindir:*) ;;
45        *) PATH=$PATH:$oldbindir ;;
46    esac
47fi
48
49XCOMM Bourne shell does not automatically export modified environment variables
50XCOMM so export the new PATH just in case the user changes the shell
51export PATH
52#endif
53
54#if defined(__SCO__) || defined(__UNIXWARE__)
55XCOMM Set up the XMERGE env var so that dos merge is happy under X
56
57if [ -f /usr/lib/merge/xmergeset.sh ]; then
58	. /usr/lib/merge/xmergeset.sh
59elif [ -f /usr/lib/merge/console.disp ]; then
60	XMERGE=`cat /usr/lib/merge/console.disp`
61	export XMERGE
62fi
63
64userclientrc=$HOME/.startxrc
65sysclientrc=LIBDIR/sys.startxrc
66scouserclientrc=$HOME/.xinitrc
67scosysclientrc=XINITDIR/xinitrc
68#else
69userclientrc=$HOME/.xinitrc
70sysclientrc=XINITDIR/xinitrc
71#endif
72
73userserverrc=$HOME/.xserverrc
74sysserverrc=XINITDIR/xserverrc
75defaultclient=XTERM
76defaultserver=XSERVER
77defaultclientargs=""
78defaultserverargs="-noretro"
79defaultdisplay=":0"
80clientargs=""
81serverargs=""
82
83#ifdef __APPLE__
84
85if [ "x$X11_PREFS_DOMAIN" = x ] ; then
86    export X11_PREFS_DOMAIN=BUNDLE_ID_PREFIX".X11"
87fi
88
89XCOMM Initialize defaults (this will cut down on "safe" error messages)
90if ! defaults read $X11_PREFS_DOMAIN cache_fonts > /dev/null 2>&1 ; then
91    defaults write $X11_PREFS_DOMAIN cache_fonts -bool true
92fi
93
94if ! defaults read $X11_PREFS_DOMAIN no_auth > /dev/null 2>&1 ; then
95    defaults write $X11_PREFS_DOMAIN no_auth -bool false
96fi
97
98if ! defaults read $X11_PREFS_DOMAIN nolisten_tcp > /dev/null 2>&1 ; then
99    defaults write $X11_PREFS_DOMAIN nolisten_tcp -bool true
100fi
101
102XCOMM First, start caching fonts
103if [ x`defaults read $X11_PREFS_DOMAIN cache_fonts` = x1 ] ; then
104    if [ -x $bindir/font_cache ] ; then
105        $bindir/font_cache &
106    elif [ -x $bindir/font_cache.sh ] ; then
107        $bindir/font_cache.sh &
108    elif [ -x $bindir/fc-cache ] ; then
109        $bindir/fc-cache &
110    fi
111fi
112
113if [ -x XINITDIR/privileged_startx ] ; then
114	# Don't push this into the background becasue it can cause
115	# a race to create /tmp/.X11-unix
116	XINITDIR/privileged_startx
117fi
118
119if [ x`defaults read $X11_PREFS_DOMAIN no_auth` = x0 ] ; then
120    enable_xauth=1
121else
122    enable_xauth=0
123fi
124
125if [ x`defaults read $X11_PREFS_DOMAIN nolisten_tcp` = x1 ] ; then
126    defaultserverargs="$defaultserverargs -nolisten tcp"
127fi
128
129XCOMM The second check is the real one.  The first is to hopefully avoid
130XCOMM needless syslog spamming.
131if defaults read $X11_PREFS_DOMAIN 2> /dev/null | grep -q 'dpi' && defaults read $X11_PREFS_DOMAIN dpi > /dev/null 2>&1 ; then
132    defaultserverargs="$defaultserverargs -dpi `defaults read $X11_PREFS_DOMAIN dpi`"
133fi
134
135#else
136enable_xauth=1
137#endif
138
139XCOMM Automatically determine an unused $DISPLAY
140d=0
141while true ; do
142    [ -e /tmp/.X$d-lock ] || break
143    d=$(($d + 1))
144done
145defaultdisplay=":$d"
146unset d
147
148#if defined(__SCO__) || defined(__UNIXWARE__)
149
150XCOMM SCO -t option: do not start an X server
151case $1 in
152  -t)   if [ -n "$DISPLAY" ]; then
153                REMOTE_SERVER=TRUE
154                shift
155        else
156                echo "DISPLAY environment variable not set"
157                exit 1
158        fi
159        ;;
160esac
161#endif
162
163whoseargs="client"
164while [ x"$1" != x ]; do
165    case "$1" in
166    XCOMM '' required to prevent cpp from treating "/*" as a C comment.
167    /''*|\./''*)
168	if [ "$whoseargs" = "client" ]; then
169	    if [ x"$client" = x ] && [ x"$clientargs" = x ]; then
170		client="$1"
171	    else
172		clientargs="$clientargs $1"
173	    fi
174	else
175	    if [ x"$server" = x ] && [ x"$serverargs" = x ]; then
176		server="$1"
177	    else
178		serverargs="$serverargs $1"
179	    fi
180	fi
181	;;
182    --)
183	whoseargs="server"
184	;;
185    *)
186	if [ "$whoseargs" = "client" ]; then
187	    clientargs="$clientargs $1"
188	else
189	    XCOMM display must be the FIRST server argument
190	    if [ x"$serverargs" = x ] && @@
191		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
192		display="$1"
193	    else
194		serverargs="$serverargs $1"
195	    fi
196	fi
197	;;
198    esac
199    shift
200done
201
202XCOMM process client arguments
203if [ x"$client" = x ]; then
204    client=$defaultclient
205
206    XCOMM For compatibility reasons, only use startxrc if there were no client command line arguments
207    if [ x"$clientargs" = x ]; then
208        if [ -f "$userclientrc" ]; then
209            client=$userclientrc
210        elif [ -f "$sysclientrc" ]; then
211            client=$sysclientrc
212#if defined(__SCO__) || defined(__UNIXWARE__)
213        elif [ -f "$scouserclientrc" ]; then
214            client=$scouserclientrc
215        elif [ -f "$scosysclientrc" ]; then
216            client=$scosysclientrc
217#endif
218        fi
219    fi
220fi
221
222XCOMM if no client arguments, use defaults
223if [ x"$clientargs" = x ]; then
224    clientargs=$defaultclientargs
225fi
226
227XCOMM process server arguments
228if [ x"$server" = x ]; then
229    server=$defaultserver
230
231    XCOMM For compatibility reasons, only use xserverrc if there were no server command line arguments
232    if [ x"$serverargs" = x -a x"$display" = x ]; then
233	if [ -f "$userserverrc" ]; then
234	    server=$userserverrc
235	elif [ -f "$sysserverrc" ]; then
236	    server=$sysserverrc
237	fi
238    fi
239fi
240
241XCOMM if no server arguments, use defaults
242if [ x"$serverargs" = x ]; then
243    serverargs=$defaultserverargs
244fi
245
246XCOMM if no display, use default
247if [ x"$display" = x ]; then
248    display=$defaultdisplay
249fi
250
251if [ x"$enable_xauth" = x1 ] ; then
252    if [ x"$XAUTHORITY" = x ]; then
253        XAUTHORITY=$HOME/.Xauthority
254        export XAUTHORITY
255    fi
256
257    removelist=
258
259    XCOMM set up default Xauth info for this machine
260    case `uname` in
261    Linux*)
262        if [ -z "`hostname --version 2>&1 | grep GNU`" ]; then
263            hostname=`hostname -f`
264        else
265            hostname=`hostname`
266        fi
267        ;;
268    *)
269        hostname=`hostname`
270        ;;
271    esac
272
273    authdisplay=${display:-:0}
274#if defined(HAS_COOKIE_MAKER) && defined(MK_COOKIE)
275    mcookie=`MK_COOKIE`
276#else
277    if [ -r /dev/urandom ]; then
278        mcookie=`dd if=/dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
279    else
280        mcookie=`dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
281    fi
282#endif
283    if test x"$mcookie" = x; then
284        echo "Couldn't create cookie"
285        exit 1
286    fi
287    dummy=0
288
289    XCOMM create a file with auth information for the server. ':0' is a dummy.
290    xserverauthfile=$HOME/.serverauth.$$
291    trap "rm -f '$xserverauthfile'" HUP INT QUIT ILL TRAP KILL BUS TERM
292    xauth -q -f "$xserverauthfile" << EOF
293add :$dummy . $mcookie
294EOF
295#if defined(__APPLE__) || defined(__CYGWIN__)
296    xserverauthfilequoted=$(echo ${xserverauthfile} | sed "s/'/'\\\\''/g")
297    serverargs=${serverargs}" -auth '"${xserverauthfilequoted}"'"
298#else
299    serverargs=${serverargs}" -auth "${xserverauthfile}
300#endif
301
302    XCOMM now add the same credentials to the client authority file
303    XCOMM if '$displayname' already exists do not overwrite it as another
304    XCOMM server man need it. Add them to the '$xserverauthfile' instead.
305    for displayname in $authdisplay $hostname$authdisplay; do
306        authcookie=`XAUTH list "$displayname" @@
307        | sed -n "s/.*$displayname[[:space:]*].*[[:space:]*]//p"` 2>/dev/null;
308        if [ "z${authcookie}" = "z" ] ; then
309            XAUTH -q << EOF
310add $displayname . $mcookie
311EOF
312        removelist="$displayname $removelist"
313        else
314            dummy=$(($dummy+1));
315            XAUTH -q -f "$xserverauthfile" << EOF
316add :$dummy . $authcookie
317EOF
318        fi
319    done
320fi
321
322#if defined(__SCO__) || defined(__UNIXWARE__)
323if [ "$REMOTE_SERVER" = "TRUE" ]; then
324        exec SHELL_CMD ${client}
325else
326        XINIT "$client" $clientargs -- "$server" $display $serverargs
327fi
328#else
329
330#if defined(__APPLE__) || defined(__CYGWIN__)
331eval XINIT \"$client\" $clientargs -- \"$server\" $display $serverargs
332#else
333XINIT "$client" $clientargs -- "$server" $display $serverargs
334#endif
335
336#endif
337retval=$?
338
339if [ x"$enable_xauth" = x1 ] ; then
340    if [ x"$removelist" != x ]; then
341        XAUTH remove $removelist
342    fi
343    if [ x"$xserverauthfile" != x ]; then
344        rm -f "$xserverauthfile"
345    fi
346fi
347
348/*
349 * various machines need special cleaning up
350 */
351#ifdef __linux__
352if command -v deallocvt > /dev/null 2>&1; then
353    deallocvt
354fi
355#endif
356
357#ifdef macII
358Xrepair
359screenrestore
360#endif
361
362#if defined(sun)
363kbd_mode -a
364#endif
365
366exit $retval
367
368