upload tizen2.0 source
[framework/uifw/xorg/app/xinit.git] / startx.cpp
1 XCOMM!SHELL_CMD
2
3 XCOMM
4 XCOMM This is just a sample implementation of a slightly less primitive
5 XCOMM interface than xinit.  It looks for user .xinitrc and .xserverrc
6 XCOMM files, then system xinitrc and xserverrc files, else lets xinit choose
7 XCOMM its default.  The system xinitrc should probably do things like check
8 XCOMM for .Xresources files and merge them in, start up a window manager,
9 XCOMM and pop a clock and several xterms.
10 XCOMM
11 XCOMM Site administrators are STRONGLY urged to write nicer versions.
12 XCOMM
13
14 unset DBUS_SESSION_BUS_ADDRESS
15 unset SESSION_MANAGER
16
17 #if defined(__SCO__) || defined(__UNIXWARE__) || defined(__APPLE__)
18
19 XCOMM Check for /usr/bin/X11 and BINDIR in the path, if not add them.
20 XCOMM This allows startx to be placed in a place like /usr/bin or /usr/local/bin
21 XCOMM and people may use X without changing their PATH.
22 XCOMM Note that we put our own bin directory at the front of the path, and
23 XCOMM the standard system path at the back, since if you are using the Xorg
24 XCOMM server there's a pretty good chance you want to bias the Xorg clients
25 XCOMM over the old system's clients.
26
27 XCOMM First our compiled path
28 bindir=__bindir__
29
30 case $PATH in
31     *:$bindir | *:$bindir:* | $bindir:*) ;;
32     *) PATH=$bindir:$PATH ;;
33 esac
34
35 XCOMM Now the "old" compiled path
36 #ifdef __APPLE__
37 oldbindir=/usr/X11R6/bin
38 #else
39 oldbindir=/usr/bin/X11
40 #endif
41
42 if [ -d "$oldbindir" ] ; then
43     case $PATH in
44         *:$oldbindir | *:$oldbindir:* | $oldbindir:*) ;;
45         *) PATH=$PATH:$oldbindir ;;
46     esac
47 fi
48
49 XCOMM Bourne shell does not automatically export modified environment variables
50 XCOMM so export the new PATH just in case the user changes the shell
51 export PATH
52 #endif
53
54 #if defined(__SCO__) || defined(__UNIXWARE__)
55 XCOMM Set up the XMERGE env var so that dos merge is happy under X
56
57 if [ -f /usr/lib/merge/xmergeset.sh ]; then
58         . /usr/lib/merge/xmergeset.sh
59 elif [ -f /usr/lib/merge/console.disp ]; then
60         XMERGE=`cat /usr/lib/merge/console.disp`
61         export XMERGE
62 fi
63
64 userclientrc=$HOME/.startxrc
65 sysclientrc=LIBDIR/sys.startxrc
66 scouserclientrc=$HOME/.xinitrc
67 scosysclientrc=XINITDIR/xinitrc
68 #else
69 userclientrc=$HOME/.xinitrc
70 sysclientrc=XINITDIR/xinitrc
71 #endif
72
73 userserverrc=$HOME/.xserverrc
74 sysserverrc=XINITDIR/xserverrc
75 defaultclient=XTERM
76 defaultserver=XSERVER
77 defaultclientargs=""
78 defaultserverargs=""
79 defaultdisplay=":0"
80 clientargs=""
81 serverargs=""
82
83 #ifdef __APPLE__
84
85 if [ "x$X11_PREFS_DOMAIN" = x ] ; then
86     export X11_PREFS_DOMAIN=BUNDLE_ID_PREFIX".X11"
87 fi
88
89 XCOMM Initialize defaults (this will cut down on "safe" error messages)
90 if ! defaults read $X11_PREFS_DOMAIN cache_fonts > /dev/null 2>&1 ; then
91     defaults write $X11_PREFS_DOMAIN cache_fonts -bool true
92 fi
93
94 if ! defaults read $X11_PREFS_DOMAIN no_auth > /dev/null 2>&1 ; then
95     defaults write $X11_PREFS_DOMAIN no_auth -bool false
96 fi
97
98 if ! defaults read $X11_PREFS_DOMAIN nolisten_tcp > /dev/null 2>&1 ; then
99     defaults write $X11_PREFS_DOMAIN nolisten_tcp -bool true
100 fi
101
102 XCOMM First, start caching fonts
103 if [ 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
111 fi
112
113 if [ -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
117 fi
118
119 if [ x`defaults read $X11_PREFS_DOMAIN no_auth` = x0 ] ; then
120     enable_xauth=1
121 else
122     enable_xauth=0
123 fi
124
125 if [ x`defaults read $X11_PREFS_DOMAIN nolisten_tcp` = x1 ] ; then
126     defaultserverargs="$defaultserverargs -nolisten tcp"
127 fi
128
129 if defaults read $X11_PREFS_DOMAIN dpi > /dev/null 2>&1 ; then
130     defaultserverargs="$defaultserverargs -dpi `defaults read $X11_PREFS_DOMAIN dpi`"
131 fi
132
133 #else
134 enable_xauth=1
135 #endif
136
137 XCOMM Automatically determine an unused $DISPLAY
138 d=0
139 while true ; do
140     [ -e /tmp/.X$d-lock ] || break
141     d=$(($d + 1))
142 done
143 defaultdisplay=":$d"
144 unset d
145
146 #if defined(__SCO__) || defined(__UNIXWARE__)
147
148 XCOMM SCO -t option: do not start an X server
149 case $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         ;;
158 esac
159 #endif
160
161 whoseargs="client"
162 while [ 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
198 done
199
200 XCOMM process client arguments
201 if [ 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
218 fi
219
220 XCOMM if no client arguments, use defaults
221 if [ x"$clientargs" = x ]; then
222     clientargs=$defaultclientargs
223 fi
224
225 XCOMM process server arguments
226 if [ 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
237 fi
238
239 XCOMM if no server arguments, use defaults
240 if [ x"$serverargs" = x ]; then
241     serverargs=$defaultserverargs
242 fi
243
244 XCOMM if no display, use default
245 if [ x"$display" = x ]; then
246     display=$defaultdisplay
247 fi
248
249 if [ 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
291 add :$dummy . $mcookie
292 EOF
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 
308 add $displayname . $mcookie
309 EOF
310         removelist="$displayname $removelist"
311         else
312             dummy=$(($dummy+1));
313             XAUTH -q -f "$xserverauthfile" << EOF
314 add :$dummy . $authcookie
315 EOF
316         fi
317     done
318 fi
319
320 #if defined(__SCO__) || defined(__UNIXWARE__)
321 if [ "$REMOTE_SERVER" = "TRUE" ]; then
322         exec SHELL_CMD ${client}
323 else
324         XINIT "$client" $clientargs -- "$server" $display $serverargs
325 fi
326 #else
327
328 #if defined(__APPLE__) || defined(__CYGWIN__)
329 eval XINIT \"$client\" $clientargs -- \"$server\" $display $serverargs
330 #else
331 XINIT "$client" $clientargs -- "$server" $display $serverargs
332 #endif
333
334 #endif
335 retval=$?
336
337 if [ 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
344 fi
345
346 /*
347  * various machines need special cleaning up
348  */
349 #ifdef __linux__
350 if command -v deallocvt > /dev/null 2>&1; then
351     deallocvt
352 fi
353 #endif
354
355 #ifdef macII
356 Xrepair
357 screenrestore
358 #endif
359
360 #if defined(sun)
361 kbd_mode -a
362 #endif
363
364 exit $retval
365