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 ******************************************************************/
25 #include <config-workspace.h>
26 #include <config-ksmserver.h>
33 #include <KMessageBox>
34 #include <QtDBus/QtDBus>
36 #include <kapplication.h>
37 #include <kcmdlineargs.h>
38 #include <kconfiggroup.h>
39 #include <kaboutdata.h>
44 #include <kmanagerselection.h>
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
)
62 int fd
= mkstemp(path
.data());
65 if (write(fd
, "Hello World\n", 12) == -1)
67 int save_errno
= errno
;
78 void sanity_check( int argc
, char* argv
[] )
81 QByteArray path
= getenv("HOME");
82 QByteArray readOnly
= getenv("KDE_HOME_READONLY");
85 msg
= "$HOME not set!";
87 if (msg
.isEmpty() && access(path
.data(), W_OK
))
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
))
97 msg
= "$HOME directory (%s) does not exist.";
99 msg
= "No read access to $HOME directory (%s).";
101 if (msg
.isEmpty() && readOnly
.isEmpty() && !writeTest(path
))
104 msg
= "$HOME directory (%s) is out of disk space.";
106 msg
= QByteArray("Writing to the $HOME directory (%s) failed with\n "
107 "the error '")+QByteArray(strerror(errno
))+QByteArray("'");
111 path
= getenv("ICEAUTHORITY");
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'.";
126 #warning Is something like this needed for D-BUS?
129 path
= DCOPClient::dcopServerFile();
130 if (access(path
.data(), R_OK
) && (errno
== ENOENT
))
133 if (DCOPClient::iceauthPath().isEmpty())
134 msg
= "Could not find 'iceauth' in path.";
140 path
= getenv("KDETMP");
143 if (!writeTest(path
))
146 msg
= "Temp directory (%s) is out of disk space.";
148 msg
= "Writing to the temp directory (%s) failed with\n "
149 "the error '"+QByteArray(strerror(errno
))+QByteArray("'");
152 if (msg
.isEmpty() && (path
!= "/tmp"))
155 if (!writeTest(path
))
158 msg
= "Temp directory (%s) is out of disk space.";
160 msg
= "Writing to the temp directory (%s) failed with\n "
161 "the error '"+QByteArray(strerror(errno
))+QByteArray("'");
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'.";
174 const char *msg_pre
=
175 "The following installation problem was detected\n"
176 "while trying to start KDE:"
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
);
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
;
206 options
.add("restore", ki18n("Restores the saved user session if available"));
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.");
224 QString wm
= args
->getOption("windowmanager");
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)
235 qWarning("--[no]local is not supported on your platform. Sorry.");
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
);
265 server
->startDefaultSession();