File indexing completed on 2024-05-19 16:35:37

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2019 Roman Gilg <subdiff@gmail.com>
0006     SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include <QObject>
0014 #include <QProcess>
0015 #include <QSocketNotifier>
0016 #include <QTemporaryFile>
0017 #include <QVector>
0018 #include <memory>
0019 
0020 #include <kwin_export.h>
0021 
0022 class QTimer;
0023 
0024 namespace KWin
0025 {
0026 class XwaylandSocket;
0027 
0028 namespace Xwl
0029 {
0030 
0031 class KWIN_EXPORT XwaylandLauncher : public QObject
0032 {
0033     Q_OBJECT
0034 public:
0035     explicit XwaylandLauncher(QObject *parent);
0036     ~XwaylandLauncher();
0037 
0038     /**
0039      * Set file descriptors that xwayland should use for listening
0040      * This is to be used in conjuction with kwin_wayland_wrapper which creates a socket externally
0041      * That external process is responsible for setting up the DISPLAY env with a valid value.
0042      * Ownership of the file descriptor is not transferrred.
0043      */
0044     void setListenFDs(const QVector<int> &listenFds);
0045 
0046     /**
0047      * Sets the display name used by XWayland (i.e ':0')
0048      * This is to be used in conjuction with kwin_wayland_wrapper to provide the name of the socket
0049      * created externally
0050      */
0051     void setDisplayName(const QString &displayName);
0052 
0053     /**
0054      * Sets the xauthority file to be used by XWayland
0055      * This is to be used in conjuction with kwin_wayland_wrapper
0056      */
0057     void setXauthority(const QString &xauthority);
0058 
0059     void start();
0060     void stop();
0061 
0062     QString displayName() const;
0063     QString xauthority() const;
0064     int xcbConnectionFd() const;
0065 
0066     /**
0067      * @internal
0068      */
0069     QProcess *process() const;
0070 Q_SIGNALS:
0071     /**
0072      * This signal is emitted when the Xwayland server has been started successfully and it is
0073      * ready to accept and manage X11 clients.
0074      * For restarts it may be emitted multiple times
0075      */
0076     void started();
0077     /**
0078      * This signal is emitted when the Xwayland server quits or crashes
0079      */
0080     void finished();
0081     /**
0082      * This signal is emitted when an error occurs with the Xwayland server.
0083      */
0084     void errorOccurred();
0085 
0086 private Q_SLOTS:
0087     void resetCrashCount();
0088     void handleXwaylandFinished(int exitCode, QProcess::ExitStatus exitStatus);
0089     void handleXwaylandError(QProcess::ProcessError error);
0090 
0091 private:
0092     void maybeDestroyReadyNotifier();
0093 
0094     bool startInternal();
0095     void stopInternal();
0096     void restartInternal();
0097 
0098     QProcess *m_xwaylandProcess = nullptr;
0099     QSocketNotifier *m_readyNotifier = nullptr;
0100     QTimer *m_resetCrashCountTimer = nullptr;
0101     // this is only used when kwin is run without kwin_wayland_wrapper
0102     std::unique_ptr<XwaylandSocket> m_socket;
0103     QVector<int> m_listenFds;
0104     QString m_displayName;
0105     QString m_xAuthority;
0106 
0107     int m_crashCount = 0;
0108     int m_xcbConnectionFd = -1;
0109 };
0110 
0111 }
0112 }