reorganize this code without actually changing any of it (I think it's a bit clearer...
[kdebase.git] / workspace / startkde.cmake
blobe1817dfc55d3d894123038527d78803c0814976d
1 #!/bin/sh
3 # DEFAULT KDE STARTUP SCRIPT ( @KDE_VERSION_STRING@ )
6 # When the X server dies we get a HUP signal from xinit. We must ignore it
7 # because we still need to do some cleanup.
8 trap 'echo GOT SIGHUP' HUP
10 # Check if a KDE session already is running and whether it's possible to connect to X
11 kcheckrunning
12 kcheckrunning_result=$?
13 if test $kcheckrunning_result -eq 0 ; then
14 echo "KDE seems to be already running on this display."
15 xmessage -geometry 500x100 "KDE seems to be already running on this display." > /dev/null 2>/dev/null
16 exit 1
17 elif test $kcheckrunning_result -eq 2 ; then
18 echo "\$DISPLAY is not set or cannot connect to the X server."
19 exit 1
22 # Set the background to plain grey.
23 # The standard X background is nasty, causing moire effects and exploding
24 # people's heads. We use colours from the standard KDE palette for those with
25 # palettised displays.
26 if test -z "$XDM_MANAGED" || echo "$XDM_MANAGED" | grep ",auto" > /dev/null; then
27 xsetroot -solid "#000000"
30 # we have to unset this for Darwin since it will screw up KDE's dynamic-loading
31 unset DYLD_FORCE_FLAT_NAMESPACE
33 # Enable lightweight memory corruption checker
34 MALLOC_CHECK_=2
35 export MALLOC_CHECK_
37 # in case we have been started with full pathname spec without being in PATH
38 bindir=`echo "$0" | sed -n 's,^\(/.*\)/[^/][^/]*$,\1,p'`
39 if [ -n "$bindir" ]; then
40 qbindir=`$bindir/kde4-config --qt-binaries`
41 if [ -n "$qbindir" ]; then
42 case $PATH in
43 $qbindir|$qbindir:*|*:$qbindir|*:$qbindir:*) ;;
44 *) PATH=$qbindir:$PATH; export PATH;;
45 esac
47 case $PATH in
48 $bindir|$bindir:*|*:$bindir|*:$bindir:*) ;;
49 *) PATH=$bindir:$PATH; export PATH;;
50 esac
53 # Boot sequence:
55 # kdeinit is used to fork off processes which improves memory usage
56 # and startup time.
58 # * kdeinit starts klauncher first.
59 # * Then kded is started. kded is responsible for keeping the sycoca
60 # database up to date. When an up to date database is present it goes
61 # into the background and the startup continues.
62 # * Then kdeinit starts kcminit. kcminit performs initialisation of
63 # certain devices according to the user's settings
65 # * Then ksmserver is started which takes control of the rest of the startup sequence
67 # The user's personal KDE directory is usually ~/.kde, but this setting
68 # may be overridden by setting KDEHOME.
70 kdehome=$HOME/@KDE_DEFAULT_HOME@
71 test -n "$KDEHOME" && kdehome=`echo "$KDEHOME"|sed "s,^~/,$HOME/,"`
73 # see kstartupconfig source for usage
74 mkdir -m 700 -p $kdehome
75 mkdir -m 700 -p $kdehome/share
76 mkdir -m 700 -p $kdehome/share/config
77 cat >$kdehome/share/config/startupconfigkeys <<EOF
78 kcminputrc Mouse cursorTheme 'Oxygen_Black'
79 kcminputrc Mouse cursorSize ''
80 ksplashrc KSplash Theme Default
81 ksplashrc KSplash Engine KSplashX
82 kcmrandrrc Display ApplyOnStartup false
83 kcmrandrrc [Screen0]
84 kcmrandrrc [Screen1]
85 kcmrandrrc [Screen2]
86 kcmrandrrc [Screen3]
87 kcmfonts General forceFontDPI 0
88 kdeglobals Locale Language '' # trigger requesting languages from KLocale
89 EOF
90 kstartupconfig4
91 if test $? -ne 0; then
92 xmessage -geometry 500x100 "Could not start kstartupconfig4. Check your installation."
93 exit 1
95 [ -r $kdehome/share/config/startupconfig ] && . $kdehome/share/config/startupconfig
97 # XCursor mouse theme needs to be applied here to work even for kded or ksmserver
98 if test -n "$kcminputrc_mouse_cursortheme" -o -n "$kcminputrc_mouse_cursorsize" ; then
99 @EXPORT_XCURSOR_PATH@
101 kapplymousetheme "$kcminputrc_mouse_cursortheme" "$kcminputrc_mouse_cursorsize"
102 if test $? -eq 10; then
103 XCURSOR_THEME=default
104 export XCURSOR_THEME
105 elif test -n "$kcminputrc_mouse_cursortheme"; then
106 XCURSOR_THEME="$kcminputrc_mouse_cursortheme"
107 export XCURSOR_THEME
109 if test -n "$kcminputrc_mouse_cursorsize"; then
110 XCURSOR_SIZE="$kcminputrc_mouse_cursorsize"
111 export XCURSOR_SIZE
115 if test "$kcmrandrrc_display_applyonstartup" = "true"; then
116 # 4 screens is hopefully enough
117 for scrn in 0 1 2 3; do
118 args=
119 width="\$kcmrandrrc_screen${scrn}_width" ; eval "width=$width"
120 height="\$kcmrandrrc_screen${scrn}_height" ; eval "height=$height"
121 if test -n "${width}" -a -n "${height}"; then
122 args="$args -s ${width}x${height}"
124 refresh="\$kcmrandrrc_screen${scrn}_refresh" ; eval "refresh=$refresh"
125 if test -n "${refresh}"; then
126 args="$args -r ${refresh}"
128 rotation="\$kcmrandrrc_screen${scrn}_rotation" ; eval "rotation=$rotation"
129 if test -n "${rotation}"; then
130 case "${rotation}" in
132 args="$args -o 0"
135 args="$args -o 1"
137 180)
138 args="$args -o 2"
140 270)
141 args="$args -o 3"
143 esac
145 reflectx="\$kcmrandrrc_screen${scrn}_reflectx" ; eval "reflectx=$reflectx"
146 if test "${refrectx}" = "true"; then
147 args="$args -x"
149 reflecty="\$kcmrandrrc_screen${scrn}_reflecty" ; eval "reflecty=$reflecty"
150 if test "${refrecty}" = "true"; then
151 args="$args -y"
153 if test -n "$args"; then
154 xrandr $args
156 done
159 if test "$kcmfonts_general_forcefontdpi" -eq 120; then
160 xrdb -quiet -merge -nocpp <<EOF
161 Xft.dpi: 120
163 elif test "$kcmfonts_general_forcefontdpi" -eq 96; then
164 xrdb -quiet -merge -nocpp <<EOF
165 Xft.dpi: 96
170 dl=$DESKTOP_LOCKED
171 unset DESKTOP_LOCKED # Don't want it in the environment
173 ksplash_pid=
174 if test -z "$dl"; then
175 # languages as resolved by KLocale, for the splash screens use
176 # klocale_languages is assembled by kdostartupconfig4 calling KLocale
177 KLOCALE_LANGUAGES="$klocale_languages"
178 export KLOCALE_LANGUAGES
179 # the splashscreen and progress indicator
180 case "$ksplashrc_ksplash_engine" in
181 KSplashX)
182 ksplash_pid=`ksplashx ${ksplashrc_ksplash_theme} --pid`
184 None)
186 Simple)
187 ksplash_pid=`ksplashsimple ${ksplashrc_ksplash_theme} --pid`
191 esac
192 # no longer needed in the environment
193 unset KLOCALE_LANGUAGES
196 # Source scripts found in <localprefix>/env/*.sh and <prefixes>/env/*.sh
197 # (where <localprefix> is $KDEHOME or ~/.kde, and <prefixes> is where KDE is installed)
199 # This is where you can define environment variables that will be available to
200 # all KDE programs, so this is where you can run agents using e.g. eval `ssh-agent`
201 # or eval `gpg-agent --daemon`.
202 # Note: if you do that, you should also put "ssh-agent -k" as a shutdown script
204 # (see end of this file).
205 # For anything else (that doesn't set env vars, or that needs a window manager),
206 # better use the Autostart folder.
208 libpath=`kde4-config --path lib | tr : '\n'`
210 for prefix in `echo "$libpath" | sed -n -e 's,/lib[^/]*/,/env/,p'`; do
211 for file in "$prefix"*.sh; do
212 test -r "$file" && . "$file"
213 done
214 done
216 # Set the path for Qt plugins provided by KDE
217 if test -n "$QT_PLUGIN_PATH"; then
218 QT_PLUGIN_PATH="$QT_PLUGIN_PATH:`kde4-config --path qtplugins`"
219 else
220 QT_PLUGIN_PATH="`kde4-config --path qtplugins`"
222 export QT_PLUGIN_PATH
224 # Activate the kde font directories.
226 # There are 4 directories that may be used for supplying fonts for KDE.
228 # There are two system directories. These belong to the administrator.
229 # There are two user directories, where the user may add her own fonts.
231 # The 'override' versions are for fonts that should come first in the list,
232 # i.e. if you have a font in your 'override' directory, it will be used in
233 # preference to any other.
235 # The preference order looks like this:
236 # user override, system override, X, user, system
238 # Where X is the original font database that was set up before this script
239 # runs.
241 usr_odir=$HOME/.fonts/kde-override
242 usr_fdir=$HOME/.fonts
244 if test -n "$KDEDIRS"; then
245 kdedirs_first=`echo "$KDEDIRS"|sed -e 's/:.*//'`
246 sys_odir=$kdedirs_first/share/fonts/override
247 sys_fdir=$kdedirs_first/share/fonts
248 else
249 sys_odir=$KDEDIR/share/fonts/override
250 sys_fdir=$KDEDIR/share/fonts
253 # We run mkfontdir on the user's font dirs (if we have permission) to pick
254 # up any new fonts they may have installed. If mkfontdir fails, we still
255 # add the user's dirs to the font path, as they might simply have been made
256 # read-only by the administrator, for whatever reason.
258 test -d "$sys_odir" && xset +fp "$sys_odir"
259 test -d "$usr_odir" && (mkfontdir "$usr_odir" ; xset +fp "$usr_odir")
260 test -d "$usr_fdir" && (mkfontdir "$usr_fdir" ; xset fp+ "$usr_fdir")
261 test -d "$sys_fdir" && xset fp+ "$sys_fdir"
263 # Ask X11 to rebuild its font list.
264 xset fp rehash
266 # Set a left cursor instead of the standard X11 "X" cursor, since I've heard
267 # from some users that they're confused and don't know what to do. This is
268 # especially necessary on slow machines, where starting KDE takes one or two
269 # minutes until anything appears on the screen.
271 # If the user has overwritten fonts, the cursor font may be different now
272 # so don't move this up.
274 xsetroot -cursor_name left_ptr
276 # Get Ghostscript to look into user's KDE fonts dir for additional Fontmap
277 if test -n "$GS_LIB" ; then
278 GS_LIB=$usr_fdir:$GS_LIB
279 export GS_LIB
280 else
281 GS_LIB=$usr_fdir
282 export GS_LIB
285 lnusertemp=`kde4-config --path exe --locate lnusertemp`
286 if test -z "$lnusertemp"; then
287 # Startup error
288 echo 'startkde: ERROR: Could not locate lnusertemp in '`kde4-config --path exe` 1>&2
291 # Link "tmp" "socket" and "cache" resources to directory in /tmp
292 # Creates:
293 # - a directory /tmp/kde-$USER and links $KDEHOME/tmp-$HOSTNAME to it.
294 # - a directory /tmp/ksocket-$USER and links $KDEHOME/socket-$HOSTNAME to it.
295 # - a directory /var/tmp/kdecache-$USER and links $KDEHOME/cache-$HOSTNAME to it.
296 # Note: temporary locations can be overriden through the KDETMP and KDEVARTMP
297 # environment variables
298 for resource in tmp cache socket; do
299 if "$lnusertemp" $resource >/dev/null; then
300 : # ok
301 else
302 echo 'startkde: Call to lnusertemp failed (temporary directories full?). Check your installation.' 1>&2
303 test -n "$ksplash_pid" && kill "$ksplash_pid" 2>/dev/null
304 xmessage -geometry 600x100 "Call to lnusertemp failed (temporary directories full?). Check your installation."
305 exit 1
307 done
309 # In case of dcop sockets left by a previous session, cleanup
310 #dcopserver_shutdown
312 echo 'startkde: Starting up...' 1>&2
314 # Make sure that D-Bus is running
315 if test -z "$XDG_DATA_DIRS"; then
316 XDG_DATA_DIRS="`kde4-config --prefix`/share:/usr/share:/usr/local/share"
317 export XDG_DATA_DIRS
319 # D-Bus autolaunch is broken
320 if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
321 eval `dbus-launch --sh-syntax --exit-with-session`
323 if qdbus >/dev/null 2>/dev/null; then
324 : # ok
325 else
326 echo 'startkde: Could not start D-Bus. Check your installation.' 1>&2
327 test -n "$ksplash_pid" && kill "$ksplash_pid" 2>/dev/null
328 xmessage -geometry 500x100 "Could not start D-Bus. Check your installation."
329 exit 1
333 # Mark that full KDE session is running (e.g. Konqueror preloading works only
334 # with full KDE running). The KDE_FULL_SESSION property can be detected by
335 # any X client connected to the same X session, even if not launched
336 # directly from the KDE session but e.g. using "ssh -X", kdesu. $KDE_FULL_SESSION
337 # however guarantees that the application is launched in the same environment
338 # like the KDE session and that e.g. KDE utilities/libraries are available.
339 # KDE_FULL_SESSION property is also only available since KDE 3.5.5.
340 # The matching tests are:
341 # For $KDE_FULL_SESSION:
342 # if test -n "$KDE_FULL_SESSION"; then ... whatever
343 # For KDE_FULL_SESSION property:
344 # xprop -root | grep "^KDE_FULL_SESSION" >/dev/null 2>/dev/null
345 # if test $? -eq 0; then ... whatever
347 # Additionally there is (since KDE 3.5.7) $KDE_SESSION_UID with the uid
348 # of the user running the KDE session. It should be rarely needed (e.g.
349 # after sudo to prevent desktop-wide functionality in the new user's kded).
351 # Since KDE4 there is also KDE_SESSION_VERSION, containing the major version number.
352 # Note that this didn't exist in KDE3, which can be detected by its absense and
353 # the presence of KDE_FULL_SESSION.
355 KDE_FULL_SESSION=true
356 export KDE_FULL_SESSION
357 xprop -root -f KDE_FULL_SESSION 8t -set KDE_FULL_SESSION true
359 KDE_SESSION_VERSION=4
360 export KDE_SESSION_VERSION
361 xprop -root -f KDE_SESSION_VERSION 32c -set KDE_SESSION_VERSION 4
363 KDE_SESSION_UID=$UID
364 export KDE_SESSION_UID
366 # We set LD_BIND_NOW to increase the efficiency of kdeinit.
367 # kdeinit unsets this variable before loading applications.
368 LD_BIND_NOW=true kdeinit4 +kcminit_startup
369 if test $? -ne 0; then
370 # Startup error
371 echo 'startkde: Could not start kdeinit4. Check your installation.' 1>&2
372 test -n "$ksplash_pid" && kill "$ksplash_pid" 2>/dev/null
373 xmessage -geometry 500x100 "Could not start kdeinit4. Check your installation."
374 exit 1
377 # If the session should be locked from the start (locked autologin),
378 # lock now and do the rest of the KDE startup underneath the locker.
379 if test -n "$dl"; then
380 kwrapper4 krunner_lock --forcelock &
381 # Give it some time for starting up. This is somewhat unclean; some
382 # notification would be better.
383 sleep 1
386 # finally, give the session control to the session manager
387 # see kdebase/ksmserver for the description of the rest of the startup sequence
388 # if the KDEWM environment variable has been set, then it will be used as KDE's
389 # window manager instead of kwin.
390 # if KDEWM is not set, ksmserver will ensure kwin is started.
391 # kwrapper4 is used to reduce startup time and memory usage
392 # kwrapper4 does not return useful error codes such as the exit code of ksmserver.
393 # We only check for 255 which means that the ksmserver process could not be
394 # started, any problems thereafter, e.g. ksmserver failing to initialize,
395 # will remain undetected.
396 test -n "$KDEWM" && KDEWM="--windowmanager $KDEWM"
397 kwrapper4 ksmserver $KDEWM
398 if test $? -eq 255; then
399 # Startup error
400 echo 'startkde: Could not start ksmserver. Check your installation.' 1>&2
401 test -n "$ksplash_pid" && kill "$ksplash_pid" 2>/dev/null
402 xmessage -geometry 500x100 "Could not start ksmserver. Check your installation."
405 # wait if there's any crashhandler shown
406 while qdbus | grep "^[^w]*org.kde.drkonqi" > /dev/null ; do
407 sleep 5
408 done
410 echo 'startkde: Shutting down...' 1>&2
411 # just in case
412 test -n "$ksplash_pid" && kill "$ksplash_pid" 2>/dev/null
414 # Clean up
415 kdeinit4_shutdown
416 # KDE3 support
417 kde3 kdeinit_shutdown 2>/dev/null
418 kde3 dcopserver_shutdown --wait 2>/dev/null
420 echo 'startkde: Running shutdown scripts...' 1>&2
422 # Run scripts found in $KDEDIRS/shutdown
423 for prefix in `echo "$libpath" | sed -n -e 's,/lib[^/]*/,/shutdown/,p'`; do
424 for file in `ls "$prefix" 2> /dev/null | egrep -v '(~|\.bak)$'`; do
425 test -x "$prefix$file" && "$prefix$file"
426 done
427 done
429 unset KDE_FULL_SESSION
430 xprop -root -remove KDE_FULL_SESSION
431 unset KDE_SESSION_VERSION
432 xprop -root -remove KDE_SESSION_VERSION
433 unset KDE_SESSION_UID
435 echo 'startkde: Done.' 1>&2