Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / workspace / ksmserver / main.cpp
bloba467455455dff7b5f46109c830c9c67fc303b339
1 /*****************************************************************
2 ksmserver - the KDE session management server
4 Copyright 2000 Matthias Ettrich <ettrich@kde.org>
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 ******************************************************************/
24 #include <fixx11h.h>
25 #include <config-workspace.h>
26 #include <config-ksmserver.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <string.h>
33 #include <KMessageBox>
34 #include <QtDBus/QtDBus>
36 #include <kapplication.h>
37 #include <kcmdlineargs.h>
38 #include <kconfiggroup.h>
39 #include <kaboutdata.h>
40 #include <kdebug.h>
41 #include <klocale.h>
42 #include <kglobal.h>
43 #include <kconfig.h>
44 #include <kmanagerselection.h>
45 #include "server.h"
46 #include <QX11Info>
49 static const char version[] = "0.4";
50 static const char description[] = I18N_NOOP( "The reliable KDE session manager that talks the standard X11R6 \nsession management protocol (XSMP)." );
52 extern KSMServer* the_server;
54 void IoErrorHandler ( IceConn iceConn)
56 the_server->ioError( iceConn );
59 bool writeTest(QByteArray path)
61 path += "/XXXXXX";
62 int fd = mkstemp(path.data());
63 if (fd == -1)
64 return false;
65 if (write(fd, "Hello World\n", 12) == -1)
67 int save_errno = errno;
68 close(fd);
69 unlink(path.data());
70 errno = save_errno;
71 return false;
73 close(fd);
74 unlink(path.data());
75 return true;
78 void sanity_check( int argc, char* argv[] )
80 QByteArray msg;
81 QByteArray path = getenv("HOME");
82 QByteArray readOnly = getenv("KDE_HOME_READONLY");
83 if (path.isEmpty())
85 msg = "$HOME not set!";
87 if (msg.isEmpty() && access(path.data(), W_OK))
89 if (errno == ENOENT)
90 msg = "$HOME directory (%s) does not exist.";
91 else if (readOnly.isEmpty())
92 msg = "No write access to $HOME directory (%s).";
94 if (msg.isEmpty() && access(path.data(), R_OK))
96 if (errno == ENOENT)
97 msg = "$HOME directory (%s) does not exist.";
98 else
99 msg = "No read access to $HOME directory (%s).";
101 if (msg.isEmpty() && readOnly.isEmpty() && !writeTest(path))
103 if (errno == ENOSPC)
104 msg = "$HOME directory (%s) is out of disk space.";
105 else
106 msg = QByteArray("Writing to the $HOME directory (%s) failed with\n "
107 "the error '")+QByteArray(strerror(errno))+QByteArray("'");
109 if (msg.isEmpty())
111 path = getenv("ICEAUTHORITY");
112 if (path.isEmpty())
114 path = getenv("HOME");
115 path += "/.ICEauthority";
118 if (access(path.data(), W_OK) && (errno != ENOENT))
119 msg = "No write access to '%s'.";
120 else if (access(path.data(), R_OK) && (errno != ENOENT))
121 msg = "No read access to '%s'.";
123 if (msg.isEmpty())
125 #ifdef __GNUC__
126 #warning Is something like this needed for D-BUS?
127 #endif
128 #if 0
129 path = DCOPClient::dcopServerFile();
130 if (access(path.data(), R_OK) && (errno == ENOENT))
132 // Check iceauth
133 if (DCOPClient::iceauthPath().isEmpty())
134 msg = "Could not find 'iceauth' in path.";
136 #endif
138 if (msg.isEmpty())
140 path = getenv("KDETMP");
141 if (path.isEmpty())
142 path = "/tmp";
143 if (!writeTest(path))
145 if (errno == ENOSPC)
146 msg = "Temp directory (%s) is out of disk space.";
147 else
148 msg = "Writing to the temp directory (%s) failed with\n "
149 "the error '"+QByteArray(strerror(errno))+QByteArray("'");
152 if (msg.isEmpty() && (path != "/tmp"))
154 path = "/tmp";
155 if (!writeTest(path))
157 if (errno == ENOSPC)
158 msg = "Temp directory (%s) is out of disk space.";
159 else
160 msg = "Writing to the temp directory (%s) failed with\n "
161 "the error '"+QByteArray(strerror(errno))+QByteArray("'");
164 if (msg.isEmpty())
166 path += ".ICE-unix";
167 if (access(path.data(), W_OK) && (errno != ENOENT))
168 msg = "No write access to '%s'.";
169 else if (access(path.data(), R_OK) && (errno != ENOENT))
170 msg = "No read access to '%s'.";
172 if (!msg.isEmpty())
174 const char *msg_pre =
175 "The following installation problem was detected\n"
176 "while trying to start KDE:"
177 "\n\n ";
178 const char *msg_post = "\n\nKDE is unable to start.\n";
179 fputs(msg_pre, stderr);
180 fprintf(stderr, msg.data(), path.data());
181 fputs(msg_post, stderr);
183 QApplication a(argc, argv);
184 QString qmsg = QString::fromLatin1(msg); // no translations possible yet...
185 qmsg.replace("%s", path);
186 qmsg = msg_pre+qmsg+msg_post;
187 KMessageBox::error(0, "KDE Installation Problem!", qmsg);
188 exit(255);
192 extern "C" KDE_EXPORT int kdemain( int argc, char* argv[] )
194 sanity_check(argc, argv);
196 KAboutData aboutData( "ksmserver", 0, ki18n("The KDE Session Manager"),
197 version, ki18n(description), KAboutData::License_BSD,
198 ki18n("(C) 2000, The KDE Developers"));
199 aboutData.addAuthor(ki18n("Matthias Ettrich"),KLocalizedString(), "ettrich@kde.org");
200 aboutData.addAuthor(ki18n("Luboš Luňák"), ki18n( "Maintainer" ), "l.lunak@kde.org" );
202 KCmdLineArgs::init(argc, argv, &aboutData);
204 KCmdLineOptions options;
205 options.add("r");
206 options.add("restore", ki18n("Restores the saved user session if available"));
207 options.add("w");
208 options.add("windowmanager <wm>", ki18n("Starts 'wm' in case no other window manager is \nparticipating in the session. Default is 'kwin'"));
209 options.add("nolocal", ki18n("Also allow remote connections"));
210 KCmdLineArgs::addCmdLineOptions( options );
212 putenv((char*)"SESSION_MANAGER=");
213 KApplication a(true); // Disable styles until we need them.
214 fcntl(ConnectionNumber(QX11Info::display()), F_SETFD, 1);
216 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
218 if( !QDBusConnection::sessionBus().interface()->registerService( "org.kde.ksmserver", QDBusConnectionInterface::DontQueueService ) )
220 qWarning("Could not register with D-BUS. Aborting.");
221 return 1;
224 QString wm = args->getOption("windowmanager");
225 if ( wm.isEmpty() )
226 wm = "kwin";
228 bool only_local = args->isSet("local");
229 #ifndef HAVE__ICETRANSNOLISTEN
230 /* this seems strange, but the default is only_local, so if !only_local
231 * the option --nolocal was given, and we warn (the option --nolocal
232 * does nothing on this platform, as here the default is reversed)
234 if (!only_local) {
235 qWarning("--[no]local is not supported on your platform. Sorry.");
237 only_local = false;
238 #endif
240 KSMServer *server = new KSMServer( wm, only_local);
242 // for the KDE-already-running check in startkde
243 KSelectionOwner kde_running( "_KDE_RUNNING", 0 );
244 kde_running.claim( false );
246 IceSetIOErrorHandler( IoErrorHandler );
248 KConfigGroup config(KGlobal::config(), "General");
250 int realScreenCount = ScreenCount( QX11Info::display() );
251 bool screenCountChanged =
252 ( config.readEntry( "screenCount", realScreenCount ) != realScreenCount );
254 QString loginMode = config.readEntry( "loginMode", "restorePreviousLogout" );
256 if ( args->isSet("restore") && ! screenCountChanged )
257 server->restoreSession( SESSION_BY_USER );
258 else if ( loginMode == "default" || screenCountChanged )
259 server->startDefaultSession();
260 else if ( loginMode == "restorePreviousLogout" )
261 server->restoreSession( SESSION_PREVIOUS_LOGOUT );
262 else if ( loginMode == "restoreSavedSession" )
263 server->restoreSession( SESSION_BY_USER );
264 else
265 server->startDefaultSession();
266 return a.exec();