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