startx.cpp revision 040e9fe7
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
129if defaults read $X11_PREFS_DOMAIN dpi > /dev/null 2>&1 ; then
130    defaultserverargs="$defaultserverargs -dpi `defaults read $X11_PREFS_DOMAIN dpi`"
131fi
132
133d=0
134while true ; do
135    [ -e /tmp/.X$d-lock ] || break
136    d=$(($d + 1))
137done
138defaultdisplay=":$d"
139unset d
140
141#else
142enable_xauth=1
143#endif
144
145#if defined(__SCO__) || defined(__UNIXWARE__)
146
147XCOMM SCO -t option: do not start an X server
148case $1 in
149  -t)   if [ -n "$DISPLAY" ]; then
150                REMOTE_SERVER=TRUE
151                shift
152        else
153                echo "DISPLAY environment variable not set"
154                exit 1
155        fi
156        ;;
157esac
158#endif
159
160whoseargs="client"
161while [ x"$1" != x ]; do
162    case "$1" in
163    XCOMM '' required to prevent cpp from treating "/*" as a C comment.
164    /''*|\./''*)
165	if [ "$whoseargs" = "client" ]; then
166	    if [ x"$client" = x ] && [ x"$clientargs" = x ]; then
167		client="$1"
168	    else
169		clientargs="$clientargs $1"
170	    fi
171	else
172	    if [ x"$server" = x ] && [ x"$serverargs" = x ]; then
173		server="$1"
174	    else
175		serverargs="$serverargs $1"
176	    fi
177	fi
178	;;
179    --)
180	whoseargs="server"
181	;;
182    *)
183	if [ "$whoseargs" = "client" ]; then
184	    clientargs="$clientargs $1"
185	else
186	    XCOMM display must be the FIRST server argument
187	    if [ x"$serverargs" = x ] && @@
188		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
189		display="$1"
190	    else
191		serverargs="$serverargs $1"
192	    fi
193	fi
194	;;
195    esac
196    shift
197done
198
199XCOMM process client arguments
200if [ x"$client" = x ]; then
201    client=$defaultclient
202
203    XCOMM if no client arguments either, use rc file instead
204    if [ x"$clientargs" = x ]; then
205        if [ -f "$userclientrc" ]; then
206            client=$userclientrc
207        elif [ -f "$sysclientrc" ]; then
208            client=$sysclientrc
209#if defined(__SCO__) || defined(__UNIXWARE__)
210        elif [ -f "$scouserclientrc" ]; then
211            client=$scouserclientrc
212        elif [ -f "$scosysclientrc" ]; then
213            client=$scosysclientrc
214#endif
215        fi
216
217        clientargs=$defaultclientargs
218    fi
219fi
220
221XCOMM process server arguments
222if [ x"$server" = x ]; then
223    server=$defaultserver
224
225    XCOMM if no server arguments or display either, use defaults
226    if [ x"$serverargs" = x -a x"$display" = x ]; then
227	XCOMM For compatibility reasons, only use xserverrc if there were no server command line arguments
228	if [ -f "$userserverrc" ]; then
229	    server=$userserverrc
230	elif [ -f "$sysserverrc" ]; then
231	    server=$sysserverrc
232	fi
233
234	serverargs=$defaultserverargs
235	display=$defaultdisplay
236    fi
237fi
238
239if [ x"$enable_xauth" = x1 ] ; then
240    if [ x"$XAUTHORITY" = x ]; then
241        XAUTHORITY=$HOME/.Xauthority
242        export XAUTHORITY
243    fi
244
245    removelist=
246
247    XCOMM set up default Xauth info for this machine
248    case `uname` in
249    Linux*)
250        if [ -z "`hostname --version 2>&1 | grep GNU`" ]; then
251            hostname=`hostname -f`
252        else
253            hostname=`hostname`
254        fi
255        ;;
256    *)
257        hostname=`hostname`
258        ;;
259    esac
260
261    authdisplay=${display:-:0}
262#if defined(HAS_COOKIE_MAKER) && defined(MK_COOKIE)
263    mcookie=`MK_COOKIE`
264#else
265    if [ -r /dev/urandom ]; then
266        mcookie=`dd if=/dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
267    else
268        mcookie=`dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
269    fi
270#endif
271    if test x"$mcookie" = x; then
272        echo "Couldn't create cookie"
273        exit 1
274    fi
275    dummy=0
276
277    XCOMM create a file with auth information for the server. ':0' is a dummy.
278    xserverauthfile=$HOME/.serverauth.$$
279    trap "rm -f '$xserverauthfile'" HUP INT QUIT ILL TRAP KILL BUS TERM
280    xauth -q -f "$xserverauthfile" << EOF
281add :$dummy . $mcookie
282EOF
283#if defined(__APPLE__) || defined(__CYGWIN__)
284    xserverauthfilequoted=$(echo ${xserverauthfile} | sed "s/'/'\\\\''/g")
285    serverargs=${serverargs}" -auth '"${xserverauthfilequoted}"'"
286#else
287    serverargs=${serverargs}" -auth "${xserverauthfile}
288#endif
289
290    XCOMM now add the same credentials to the client authority file
291    XCOMM if '$displayname' already exists do not overwrite it as another
292    XCOMM server man need it. Add them to the '$xserverauthfile' instead.
293    for displayname in $authdisplay $hostname$authdisplay; do
294        authcookie=`XAUTH list "$displayname" @@
295        | sed -n "s/.*$displayname[[:space:]*].*[[:space:]*]//p"` 2>/dev/null;
296        if [ "z${authcookie}" = "z" ] ; then
297            XAUTH -q << EOF
298add $displayname . $mcookie
299EOF
300        removelist="$displayname $removelist"
301        else
302            dummy=$(($dummy+1));
303            XAUTH -q -f "$xserverauthfile" << EOF
304add :$dummy . $authcookie
305EOF
306        fi
307    done
308fi
309
310#if defined(__SCO__) || defined(__UNIXWARE__)
311if [ "$REMOTE_SERVER" = "TRUE" ]; then
312        exec SHELL_CMD ${client}
313else
314        XINIT "$client" $clientargs -- "$server" $display $serverargs
315fi
316#else
317
318#if defined(__APPLE__) || defined(__CYGWIN__)
319eval XINIT \"$client\" $clientargs -- \"$server\" $display $serverargs
320#else
321XINIT "$client" $clientargs -- "$server" $display $serverargs
322#endif
323
324#endif
325retval=$?
326
327if [ x"$enable_xauth" = x1 ] ; then
328    if [ x"$removelist" != x ]; then
329        XAUTH remove $removelist
330    fi
331    if [ x"$xserverauthfile" != x ]; then
332        rm -f "$xserverauthfile"
333    fi
334fi
335
336/*
337 * various machines need special cleaning up
338 */
339#ifdef __linux__
340if command -v deallocvt > /dev/null 2>&1; then
341    deallocvt
342fi
343#endif
344
345#ifdef macII
346Xrepair
347screenrestore
348#endif
349
350#if defined(sun)
351kbd_mode -a
352#endif
353
354exit $retval
355
356