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