startx.cpp revision a73027ba
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
87XCOMM Initialize defaults (this will cut down on "safe" error messages)
88if ! defaults read org.x.X11 cache_fonts >& /dev/null ; then
89    defaults write org.x.X11 cache_fonts -bool true
90fi
91
92if ! defaults read org.x.X11 no_auth >& /dev/null ; then
93    defaults write org.x.X11 no_auth -bool false
94fi
95
96if ! defaults read org.x.X11 nolisten_tcp >& /dev/null ; then
97    defaults write org.x.X11 nolisten_tcp -bool true
98fi
99
100XCOMM First, start caching fonts
101if [ x`defaults read org.x.X11 cache_fonts` = x1 ] ; then
102    if [ -x /usr/X11/bin/font_cache ] ; then
103        /usr/X11/bin/font_cache &
104    elif [ -x /usr/X11/bin/font_cache.sh ] ; then
105        /usr/X11/bin/font_cache.sh &
106    elif [ -x /usr/X11/bin/fc-cache ] ; then
107        /usr/X11/bin/fc-cache &
108    fi
109fi
110
111if [ -x XINITDIR/privileged_startx ] ; then
112	# Don't push this into the background becasue it can cause
113	# a race to create /tmp/.X11-unix
114	XINITDIR/privileged_startx
115fi
116
117if [ x`defaults read org.x.X11 no_auth` = x0 ] ; then
118    enable_xauth=1
119else
120    enable_xauth=0
121fi
122
123if [ x`defaults read org.x.X11 nolisten_tcp` = x1 ] ; then
124    defaultserverargs="$defaultserverargs -nolisten tcp"
125fi
126
127for ((d=0; ; d++)) ; do
128    [[ -e /tmp/.X$d-lock ]] || break
129done
130defaultdisplay=":$d"
131
132#else
133enable_xauth=1
134#endif
135
136
137if [ -f $userclientrc ]; then
138    defaultclientargs=$userclientrc
139elif [ -f $sysclientrc ]; then
140    defaultclientargs=$sysclientrc
141#if defined(__SCO__) || defined(__UNIXWARE__)
142elif [ -f $scouserclientrc ]; then
143    defaultclientargs=$scouserclientrc
144elif [ -f $scosysclientrc ]; then
145    defaultclientargs=$scosysclientrc
146#endif
147fi
148
149#if defined(__SCO__) || defined(__UNIXWARE__)
150
151XCOMM SCO -t option: do not start an X server
152case $1 in
153  -t)   if [ -n "$DISPLAY" ]; then
154                REMOTE_SERVER=TRUE
155                shift
156        else
157                echo "DISPLAY environment variable not set"
158                exit 1
159        fi
160        ;;
161esac
162#endif
163
164whoseargs="client"
165while [ x"$1" != x ]; do
166    case "$1" in
167    XCOMM '' required to prevent cpp from treating "/*" as a C comment.
168    /''*|\./''*)
169	if [ "$whoseargs" = "client" ]; then
170	    if [ x"$clientargs" = x ]; then
171		client="$1"
172	    else
173		clientargs="$clientargs $1"
174	    fi
175	else
176	    if [ x"$serverargs" = x ]; then
177		server="$1"
178	    else
179		serverargs="$serverargs $1"
180	    fi
181	fi
182	;;
183    --)
184	whoseargs="server"
185	;;
186    *)
187	if [ "$whoseargs" = "client" ]; then
188	    clientargs="$clientargs $1"
189	else
190	    XCOMM display must be the FIRST server argument
191	    if [ x"$serverargs" = x ] && @@
192		 expr "$1" : ':[0-9][0-9]*$' > /dev/null 2>&1; then
193		display="$1"
194	    else
195		serverargs="$serverargs $1"
196	    fi
197	fi
198	;;
199    esac
200    shift
201done
202
203XCOMM process client arguments
204if [ x"$client" = x ]; then
205    XCOMM if no client arguments either, use rc file instead
206    if [ x"$clientargs" = x ]; then
207	client="$defaultclientargs"
208    else
209	client=$defaultclient
210    fi
211fi
212
213XCOMM process server arguments
214if [ x"$server" = x ]; then
215    server=$defaultserver
216
217    XCOMM if no server arguments or display either, use defaults
218    if [ x"$serverargs" = x -a x"$display" = x ]; then
219	XCOMM For compatibility reasons, only use xserverrc if there were no server command line arguments
220	if [ -f $userserverrc ]; then
221	    server=$userserverrc
222	elif [ -f $sysserverrc ]; then
223	    server=$sysserverrc
224	fi
225
226	serverargs=$defaultserverargs
227	display=$defaultdisplay
228    fi
229fi
230
231if [ x"$enable_xauth" = x1 ] ; then
232    if [ x"$XAUTHORITY" = x ]; then
233        XAUTHORITY=$HOME/.Xauthority
234        export XAUTHORITY
235    fi
236
237    removelist=
238
239    XCOMM set up default Xauth info for this machine
240    case `uname` in
241    Linux*)
242        if [ -z "`hostname --version 2>&1 | grep GNU`" ]; then
243            hostname=`hostname -f`
244        else
245            hostname=`hostname`
246        fi
247        ;;
248    *)
249        hostname=`hostname`
250        ;;
251    esac
252
253    authdisplay=${display:-:0}
254#if defined(HAS_COOKIE_MAKER) && defined(MK_COOKIE)
255    mcookie=`MK_COOKIE`
256#else
257    if [ -r /dev/urandom ]; then
258        mcookie=`dd if=/dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
259    else
260        mcookie=`dd if=/dev/random bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
261    fi
262#endif
263    if test x"$mcookie" = x; then
264        echo "Couldn't create cookie"
265        exit 1
266    fi
267    dummy=0
268
269    XCOMM create a file with auth information for the server. ':0' is a dummy.
270    xserverauthfile=$HOME/.serverauth.$$
271    trap "rm -f $xserverauthfile" HUP INT QUIT ILL TRAP KILL BUS TERM
272    xauth -q -f $xserverauthfile << EOF
273add :$dummy . $mcookie
274EOF
275    serverargs=${serverargs}" -auth "${xserverauthfile}
276
277    XCOMM now add the same credentials to the client authority file
278    XCOMM if '$displayname' already exists do not overwrite it as another
279    XCOMM server man need it. Add them to the '$xserverauthfile' instead.
280    for displayname in $authdisplay $hostname$authdisplay; do
281        authcookie=`XAUTH list "$displayname" @@
282        | sed -n "s/.*$displayname[[:space:]*].*[[:space:]*]//p"` 2>/dev/null;
283        if [ "z${authcookie}" = "z" ] ; then
284            XAUTH -q << EOF
285add $displayname . $mcookie
286EOF
287        removelist="$displayname $removelist"
288        else
289            dummy=$(($dummy+1));
290            XAUTH -q -f $xserverauthfile << EOF
291add :$dummy . $authcookie
292EOF
293        fi
294    done
295fi
296
297#if defined(__SCO__) || defined(__UNIXWARE__)
298if [ "$REMOTE_SERVER" = "TRUE" ]; then
299        exec SHELL_CMD ${client}
300else
301        XINIT $client $clientargs -- $server $display $serverargs
302fi
303#else
304XINIT $client $clientargs -- $server $display $serverargs
305#endif
306
307if [ x"$enable_xauth" = x1 ] ; then
308    if [ x"$removelist" != x ]; then
309        XAUTH remove $removelist
310    fi
311    if [ x"$xserverauthfile" != x ]; then
312        rm -f $xserverauthfile
313    fi
314fi
315
316/*
317 * various machines need special cleaning up
318 */
319#ifdef __linux__
320if command -v deallocvt > /dev/null 2>&1; then
321    deallocvt
322fi
323#endif
324
325#ifdef macII
326Xrepair
327screenrestore
328#endif
329
330#if defined(sun)
331kbd_mode -a
332#endif
333