File indexing completed on 2024-04-28 16:54:31

0001 /*
0002     SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer <kalle@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kworkspace.h"
0008 #include "config-libkworkspace.h"
0009 
0010 #include <QDBusConnection>
0011 #include <QDataStream>
0012 #include <QDateTime>
0013 #include <QFile>
0014 #include <QFileInfo>
0015 #include <QSocketNotifier>
0016 #include <QTextStream>
0017 #include <ksmserver_interface.h>
0018 #include <stdlib.h> // getenv()
0019 
0020 #if HAVE_X11
0021 #include <X11/SM/SMlib.h>
0022 #include <X11/Xatom.h>
0023 #include <X11/Xlib.h>
0024 #include <X11/Xutil.h>
0025 #include <fixx11h.h>
0026 #endif
0027 
0028 #include "config-workspace.h"
0029 
0030 #ifdef HAVE_UNISTD_H
0031 #include <unistd.h>
0032 #endif // HAVE_UNISTD_H
0033 
0034 #include <pwd.h>
0035 #include <sys/types.h>
0036 
0037 namespace KWorkSpace
0038 {
0039 void requestShutDown(ShutdownConfirm confirm, ShutdownType sdtype, ShutdownMode sdmode)
0040 {
0041     org::kde::KSMServerInterface ksmserver(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
0042     ksmserver.logout((int)confirm, (int)sdtype, (int)sdmode);
0043 }
0044 
0045 bool canShutDown(ShutdownConfirm confirm, ShutdownType sdtype, ShutdownMode sdmode)
0046 {
0047 #if HAVE_X11
0048     if (confirm == ShutdownConfirmYes || sdtype != ShutdownTypeDefault || sdmode != ShutdownModeDefault) {
0049         org::kde::KSMServerInterface ksmserver(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
0050         QDBusReply<bool> reply = ksmserver.canShutdown();
0051         if (!reply.isValid()) {
0052             return false;
0053         }
0054         return reply;
0055     }
0056 
0057     return true;
0058 #else
0059     return false;
0060 #endif
0061 }
0062 
0063 bool isShuttingDown()
0064 {
0065     org::kde::KSMServerInterface ksmserver(QStringLiteral("org.kde.ksmserver"), QStringLiteral("/KSMServer"), QDBusConnection::sessionBus());
0066     QDBusReply<bool> reply = ksmserver.isShuttingDown();
0067     if (!reply.isValid()) {
0068         return false;
0069     }
0070     return reply;
0071 }
0072 
0073 static QTime smModificationTime;
0074 void propagateSessionManager()
0075 {
0076 #if HAVE_X11
0077     QByteArray fName = QFile::encodeName(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) + "/KSMserver");
0078     QString display = QString::fromLocal8Bit(::getenv("DISPLAY"));
0079     // strip the screen number from the display
0080     display.remove(QRegularExpression(QStringLiteral("\\.\\d+$")));
0081     int i;
0082     while ((i = display.indexOf(QLatin1Char(':'))) >= 0)
0083         display[i] = '_';
0084     while ((i = display.indexOf(QLatin1Char('/'))) >= 0)
0085         display[i] = '_';
0086 
0087     fName += '_';
0088     fName += display.toLocal8Bit();
0089     QByteArray smEnv = ::getenv("SESSION_MANAGER");
0090     bool check = smEnv.isEmpty();
0091     if (!check && smModificationTime.isValid()) {
0092         QFileInfo info(fName);
0093         QTime current = info.lastModified().time();
0094         check = current > smModificationTime;
0095     }
0096     if (check) {
0097         QFile f(fName);
0098         if (!f.open(QIODevice::ReadOnly))
0099             return;
0100         QFileInfo info(f);
0101         smModificationTime = QTime(info.lastModified().time());
0102         ::setenv("SESSION_MANAGER", f.readLine().trimmed(), true);
0103     }
0104 #endif
0105 }
0106 
0107 void detectPlatform(int argc, char **argv)
0108 {
0109     if (qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) {
0110         return;
0111     }
0112     for (int i = 0; i < argc; i++) {
0113         if (qstrcmp(argv[i], "-platform") == 0 || qstrcmp(argv[i], "--platform") == 0 || QByteArray(argv[i]).startsWith("-platform=")
0114             || QByteArray(argv[i]).startsWith("--platform=")) {
0115             return;
0116         }
0117     }
0118     const QByteArray sessionType = qgetenv("XDG_SESSION_TYPE");
0119     if (sessionType.isEmpty()) {
0120         return;
0121     }
0122     if (qstrcmp(sessionType, "wayland") == 0) {
0123         qputenv("QT_QPA_PLATFORM", "wayland");
0124     } else if (qstrcmp(sessionType, "x11") == 0) {
0125         qputenv("QT_QPA_PLATFORM", "xcb");
0126     }
0127 }
0128 
0129 } // end namespace