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