Warning, /plasma/plasma-workspace/ksmserver/README is written in an unsupported language. File is not indexed.

0001 KDE session manager  (ksmserver)
0002 --------------------------------
0003 
0004 Matthias Ettrich <ettrich@kde.org>
0005 Lubos Lunak <l.lunak@kde.org>
0006 
0007 ksmserver is KDE's session management server. It talks the
0008 standard X11R6 session management protocol (XSMP).  Thus, in theory,
0009 it should be compatible with all session management compliant X11R6
0010 applications. Unfortunately, there aren't that many of them. To be
0011 precise, I have never seen a single commercial application that
0012 supports it and even within the official X11R6 distribution, 'xclock'
0013 is the only exception. Nevertheless we've chosen to support XSMP
0014 despite the complexity of the protocol in order to provide KDE users
0015 more interoperability with applications that were not explicitly
0016 written with KDE in mind. XSMP, as an official X standard, promised to
0017 be more likely to be supported by third parties than even a superior
0018 KDE-specific protocol. Let's see whether we were right and more
0019 applications will actually talk XSMP. At least all KDE applications do
0020 now.
0021 
0022 Here's a short overview on how session management works.
0023 
0024 Contents
0025 --------
0026 
0027 Starting the server
0028 KDE startup sequence
0029 Establishing the connection
0030 Authorization
0031 Requesting a shutdown
0032 
0033 
0034 Starting the server
0035 -------------------
0036 
0037 The server is usually started from the 'startkde' script. It supports the following options:
0038 
0039   -r, --restore             Restores the previous session if available
0040   -w, --windowmanager <wm>  Starts 'wm' in case no other window manager is
0041                             participating in the session. Default is 'kwin'
0042 
0043 The default 'startkde' launches 'ksmserver --restore'.  The
0044 'windowmanager' option is useful for users that prefer a window
0045 manager other than kwin. Since the window manager has to participate
0046 in the session (it has to remember window positions and states), it is
0047 usually restarted when the session is restored. To be *really* sure
0048 that this happens, even if the wm might have crashed during the
0049 previous session, ksmserver ensures that. The option specifies, which
0050 windowmanager shall be launched for sure. But again: if the stored
0051 session contains a window manager, the restored one will be used, not
0052 the specified one. As a special feature, ksmserver always starts the
0053 specified window manager first, which results in a much nicer startup
0054 sequence (less flashy).
0055 
0056 KDE startup sequence
0057 --------------------
0058 
0059 Ksmserver controls the second part of the KDE startup sequence,
0060 after it gets control from the startkde script, which controls
0061 the first part of the startup sequence. All code related to startup
0062 should be in startup.cpp and going down in that source file should
0063 follow the startup order (but note that this is just a documentation
0064 which may get outdated, so in case of doubts the source wins ;) ).
0065 
0066 The startkde scripts already launches kdeinit, which in turns launches
0067 KDE daemons like dbus daemon, klauncher and kded. Kded loads autoloaded
0068 kded modules, i.e. those that have X-KDE-Kded-autoload=true in .desktop
0069 files. The exact way autoloading works is controlled by X-KDE-Kded-phase=,
0070 which may be 0, 1 or 2 (the default). Kded phase 0 means the module is
0071 always loaded by kded, even outside of KDE session. It should used only
0072 by kded modules which must be always running. Kded phase 1 modules are
0073 loaded right after kded startup, but only during KDE startup, i.e. it is
0074 for modules that are always needed by the KDE session. Phase 2 modules
0075 will be loaded later. [More information about kded modules could be
0076 found in kdelibs/kded/HOWTO]
0077 
0078 Startkde also launches kcminit, which performs initialization done by kcontrol
0079 modules. There are two kcminit phases, 0, 1, controlled
0080 by X-KDE-Init-Phase= in the .desktop file, which defaults to 1. Phase 0 kcminit
0081 modules should be only those that really need to be run early in the startup
0082 process (and those should probably actually use kstartupconfig in startkde
0083 to be done even before kdeinit and daemons). After executing phase 0
0084 modules kcminit returns and waits.
0085 
0086 When ksmserver is launched, the first thing it does is launching
0087 the window manager, as the WM is necessary before any windows are possibly
0088 shown. When the WM is ready, ksmserver tells klauncher to perform autostart
0089 phase 0 ($KDEHOME/share/autostart). There are 3 autostart phases, 0, 1 and 2,
0090 defined by X-KDE-autostart-phase, defaulting to 2. Phase 0 is reserved only
0091 for the actual visible base components of KDE, i.e. Plasma, in order to make
0092 the startup appear visually faster. Plasma uses D-Bus calls suspendStartup()
0093 and resumeStartup() to make ksmserver stay waiting for autostart phase 0
0094 until Plasma is ready (note: suspendStartup() needs to be called before
0095 the application registers with ksmserver, i.e. before KApplication ctor is called).
0096 
0097 Next step is telling the waiting kcminit to perform phase 1 - all kcminit
0098 modules that should be executed before KDE startup is considered done.
0099 After that ksmserver tells klauncher to perform autostart phase 1,
0100 i.e. launching normal components of KDE that should be available right
0101 after KDE startup, and after this session restore is performed,
0102 i.e. launching all applications that were running during last session
0103 saving (usually logout).
0104 
0105 By this time KDE session is considered to be more or less ready and
0106 ksmserver does the knotify startkde event (i.e. plays the login sound).
0107 It also tells klauncher to perform autostart phase 2, kded to load all
0108 remaining autoload (i.e. kded phase 2) modules, and it itself executes
0109 the user Autostart folder.
0110 
0111 Technical note: There's a reason why kded modules and items in autostart
0112 default to the latest phase. Before you explicitly use a different phase,
0113 read and understand what's above. You should also consider whether something
0114 really needs to be launched during KDE startup and can't be loaded on-demand
0115 when really needed. Abusing the phases will result in public spanking
0116 for making KDE startup slower.
0117 
0118 
0119 Establishing the connection
0120 ---------------------------
0121 
0122 As required by the XSMP specification, the session management server
0123 propagates its network address in the SESSION_MANAGER environment
0124 variable. Probably not the best way to do it, but it's the way it
0125 is. All KDE (and plain Qt) applications simply read this variable and
0126 try to establish a connection to an XSMP server at this address. If
0127 the variable is undefined, nothing happens.
0128 
0129 This means, if you want to start a program that should not participate
0130 in the session, simply undefine SESSION_MANAGER in your terminal
0131 window and launch the application. If you want to see an application
0132 desparately trying to connect to something, try setting it to some
0133 bogus value.
0134 
0135 In addition, ksmserver propagates both its network address and its
0136 process id in ~/.kde/socket-$HOSTNAME/KSMserver-$DISPLAY. A
0137 utility function KWorkSpace::propagateSessionManager() reads this
0138 setting and sets SESSION_MANAGER accordingly, so that child processes
0139 can pick it up.  The function is called by clients that are started
0140 outside the session (i.e. before ksmserver is started), but want to
0141 launch other processes that should participate in the session.
0142 
0143 Authorization
0144 -------------
0145 
0146 XSMP is built on top of the Inter Client Exchange (ICE) protocol,
0147 which comes standard as a part of X11R6 and later.
0148 Authorization is done using 'iceauth', with MIT-MAGIC-COOKIE as used
0149 by X. In order to be able to access the session management server, you
0150 need to be able to read ~/.ICEauthority. For security reasons, we do
0151 not provide any host-based authorization.
0152 
0153 
0154 Requesting a shutdown
0155 ---------------------
0156 
0157 If an application wants to request a shutdown (i.e. a logout), it does
0158 this via an SmcRequestSaveYourself message to the server. In KDE, a
0159 utility function KWorkSpace::requestShutDown() does exactly
0160 this. It's for example called by KDE's panel or by the context menu of
0161 the desktop.