initial commit
[profile/ivi/xorg-x11-xinit.git] / startx.cpp
1 XCOMM!SHELL_CMD
2
3 XCOMM $Xorg: startx.cpp,v 1.3 2000/08/17 19:54:29 cpqbld Exp $
4 XCOMM
5 XCOMM This is just a sample implementation of a slightly less primitive
6 XCOMM interface than xinit.  It looks for user .xinitrc and .xserverrc
7 XCOMM files, then system xinitrc and xserverrc files, else lets xinit choose
8 XCOMM its default.  The system xinitrc should probably do things like check
9 XCOMM for .Xresources files and merge them in, startup up a window manager,
10 XCOMM and pop a clock and serveral xterms.
11 XCOMM
12 XCOMM Site administrators are STRONGLY urged to write nicer versions.
13 XCOMM
14 XCOMM $XFree86: xc/programs/xinit/startx.cpp,v 3.16tsi Exp $
15
16 unset DBUS_SESSION_BUS_ADDRESS
17 unset SESSION_MANAGER
18
19 #if defined(__SCO__) || defined(__UNIXWARE__) || defined(__APPLE__)
20
21 XCOMM Check for /usr/bin/X11 and BINDIR in the path, if not add them.
22 XCOMM This allows startx to be placed in a place like /usr/bin or /usr/local/bin
23 XCOMM and people may use X without changing their PATH.
24 XCOMM Note that we put our own bin directory at the front of the path, and
25 XCOMM the standard system path at the back, since if you are using the Xorg
26 XCOMM server theres a pretty good chance you want to bias the Xorg clients
27 XCOMM over the old system's clients.
28
29 XCOMM First our compiled path
30 bindir=__bindir__
31
32 case $PATH in
33     *:$bindir | *:$bindir:* | $bindir:*) ;;
34     *) PATH=$bindir:$PATH ;;
35 esac
36
37 XCOMM Now the "old" compiled path
38 #ifdef __APPLE__
39 oldbindir=/usr/X11R6/bin
40 #else
41 oldbindir=/usr/bin/X11
42 #endif
43
44 if [ -d "$oldbindir" ] ; then
45     case $PATH in
46         *:$oldbindir | *:$oldbindir:* | $oldbindir:*) ;;
47         *) PATH=$PATH:$oldbindir ;;
48     esac
49 fi
50
51 XCOMM Bourne shell does not automatically export modified environment variables
52 XCOMM so export the new PATH just in case the user changes the shell
53 export PATH
54 #endif
55
56 #if defined(__SCO__) || defined(__UNIXWARE__)
57 XCOMM Set up the XMERGE env var so that dos merge is happy under X
58
59 if [ -f /usr/lib/merge/xmergeset.sh ]; then
60         . /usr/lib/merge/xmergeset.sh
61 elif [ -f /usr/lib/merge/console.disp ]; then
62         XMERGE=`cat /usr/lib/merge/console.disp`
63         export XMERGE
64 fi
65
66 userclientrc=$HOME/.startxrc
67 sysclientrc=LIBDIR/sys.startxrc
68 scouserclientrc=$HOME/.xinitrc
69 scosysclientrc=XINITDIR/xinitrc
70 #else
71 userclientrc=$HOME/.xinitrc
72 sysclientrc=XINITDIR/xinitrc
73 #endif
74
75 userserverrc=$HOME/.xserverrc
76 sysserverrc=XINITDIR/xserverrc
77 defaultclient=XTERM
78 defaultserver=XSERVER
79 defaultclientargs=""
80 defaultserverargs=""
81 defaultdisplay=":0"
82 clientargs=""
83 serverargs=""
84
85 #ifdef __APPLE__
86
87 XCOMM Initialize defaults (this will cut down on "safe" error messages)
88 if ! defaults read org.x.X11 cache_fonts >& /dev/null ; then
89     defaults write org.x.X11 cache_fonts -bool true
90 fi
91
92 if ! defaults read org.x.X11 no_auth >& /dev/null ; then
93     defaults write org.x.X11 no_auth -bool false
94 fi
95
96 if ! defaults read org.x.X11 nolisten_tcp >& /dev/null ; then
97     defaults write org.x.X11 nolisten_tcp -bool true
98 fi
99
100 XCOMM First, start caching fonts
101 if [ 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
109 fi
110
111 if [ -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
115 fi
116
117 if [ x`defaults read org.x.X11 no_auth` = x0 ] ; then
118     enable_xauth=1
119 else
120     enable_xauth=0
121 fi
122
123 if [ x`defaults read org.x.X11 nolisten_tcp` = x1 ] ; then
124     defaultserverargs="$defaultserverargs -nolisten tcp"
125 fi
126
127 for ((d=0; ; d++)) ; do
128     [[ -e /tmp/.X$d-lock ]] || break
129 done
130 defaultdisplay=":$d"
131
132 #else
133 enable_xauth=1
134 #endif
135
136
137 if [ -f $userclientrc ]; then
138     defaultclientargs=$userclientrc
139 elif [ -f $sysclientrc ]; then
140     defaultclientargs=$sysclientrc
141 #if defined(__SCO__) || defined(__UNIXWARE__)
142 elif [ -f $scouserclientrc ]; then
143     defaultclientargs=$scouserclientrc
144 elif [ -f $scosysclientrc ]; then
145     defaultclientargs=$scosysclientrc
146 #endif
147 fi
148
149 #if defined(__SCO__) || defined(__UNIXWARE__)
150
151 XCOMM SCO -t option: do not start an X server
152 case $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         ;;
161 esac
162 #endif
163
164 whoseargs="client"
165 while [ 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
201 done
202
203 XCOMM process client arguments
204 if [ 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
211 fi
212
213 XCOMM process server arguments
214 if [ 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
229 fi
230
231 if [ 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
273 add :$dummy . $mcookie
274 EOF
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 
285 add $displayname . $mcookie
286 EOF
287         removelist="$displayname $removelist"
288         else
289             dummy=$(($dummy+1));
290             XAUTH -q -f $xserverauthfile << EOF
291 add :$dummy . $authcookie
292 EOF
293         fi
294     done
295 fi
296
297 #if defined(__SCO__) || defined(__UNIXWARE__)
298 if [ "$REMOTE_SERVER" = "TRUE" ]; then
299         exec SHELL_CMD ${client}
300 else
301         XINIT $client $clientargs -- $server $display $serverargs
302 fi
303 #else
304 XINIT $client $clientargs -- $server $display $serverargs
305 #endif
306
307 if [ 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
314 fi
315
316 /*
317  * various machines need special cleaning up
318  */
319 #ifdef __linux__
320 if command -v deallocvt > /dev/null 2>&1; then
321     deallocvt
322 fi
323 #endif
324
325 #ifdef macII
326 Xrepair
327 screenrestore
328 #endif
329
330 #if defined(sun)
331 kbd_mode -a
332 #endif