File indexing completed on 2024-12-01 11:10:10

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2015 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 
0011 #include <kwinglobals.h>
0012 
0013 #include <QObject>
0014 #include <QPointer>
0015 #include <QSet>
0016 
0017 class QThread;
0018 class QProcess;
0019 class QWindow;
0020 
0021 namespace KWaylandServer
0022 {
0023 class AppMenuManagerInterface;
0024 class ClientConnection;
0025 class CompositorInterface;
0026 class Display;
0027 class DataDeviceInterface;
0028 class IdleInterface;
0029 class InputMethodV1Interface;
0030 class SeatInterface;
0031 class DataDeviceManagerInterface;
0032 class ServerSideDecorationManagerInterface;
0033 class ServerSideDecorationPaletteManagerInterface;
0034 class SurfaceInterface;
0035 class OutputInterface;
0036 class PlasmaShellInterface;
0037 class PlasmaWindowActivationFeedbackInterface;
0038 class PlasmaVirtualDesktopManagementInterface;
0039 class PlasmaWindowManagementInterface;
0040 class OutputDeviceV2Interface;
0041 class OutputManagementV2Interface;
0042 class XdgForeignV2Interface;
0043 class XdgOutputManagerV1Interface;
0044 class LinuxDmaBufV1ClientBufferIntegration;
0045 class LinuxDmaBufV1ClientBuffer;
0046 class TabletManagerV2Interface;
0047 class KeyboardShortcutsInhibitManagerV1Interface;
0048 class XdgDecorationManagerV1Interface;
0049 class XWaylandKeyboardGrabManagerV1Interface;
0050 class ContentTypeManagerV1Interface;
0051 class DrmLeaseManagerV1;
0052 class TearingControlManagerV1Interface;
0053 class XwaylandShellV1Interface;
0054 class OutputOrderV1Interface;
0055 }
0056 
0057 namespace KWin
0058 {
0059 
0060 class Window;
0061 class Output;
0062 class XdgActivationV1Integration;
0063 class XdgPopupWindow;
0064 class XdgSurfaceWindow;
0065 class XdgToplevelWindow;
0066 
0067 class KWIN_EXPORT WaylandServer : public QObject
0068 {
0069     Q_OBJECT
0070 
0071 public:
0072     enum class InitializationFlag {
0073         NoOptions = 0x0,
0074         LockScreen = 0x1,
0075         NoLockScreenIntegration = 0x2,
0076         NoGlobalShortcuts = 0x4
0077     };
0078 
0079     Q_DECLARE_FLAGS(InitializationFlags, InitializationFlag)
0080 
0081     ~WaylandServer() override;
0082     bool init(const QString &socketName, InitializationFlags flags = InitializationFlag::NoOptions);
0083     bool init(InitializationFlags flags = InitializationFlag::NoOptions);
0084 
0085     bool start();
0086 
0087     KWaylandServer::Display *display() const
0088     {
0089         return m_display;
0090     }
0091     KWaylandServer::CompositorInterface *compositor() const
0092     {
0093         return m_compositor;
0094     }
0095     KWaylandServer::SeatInterface *seat() const
0096     {
0097         return m_seat;
0098     }
0099     KWaylandServer::TabletManagerV2Interface *tabletManagerV2() const
0100     {
0101         return m_tabletManagerV2;
0102     }
0103     KWaylandServer::DataDeviceManagerInterface *dataDeviceManager() const
0104     {
0105         return m_dataDeviceManager;
0106     }
0107     KWaylandServer::PlasmaWindowActivationFeedbackInterface *plasmaActivationFeedback() const
0108     {
0109         return m_plasmaActivationFeedback;
0110     }
0111     KWaylandServer::PlasmaVirtualDesktopManagementInterface *virtualDesktopManagement() const
0112     {
0113         return m_virtualDesktopManagement;
0114     }
0115     KWaylandServer::PlasmaWindowManagementInterface *windowManagement() const
0116     {
0117         return m_windowManagement;
0118     }
0119     KWaylandServer::ServerSideDecorationManagerInterface *decorationManager() const
0120     {
0121         return m_decorationManager;
0122     }
0123     KWaylandServer::XdgOutputManagerV1Interface *xdgOutputManagerV1() const
0124     {
0125         return m_xdgOutputManagerV1;
0126     }
0127     KWaylandServer::KeyboardShortcutsInhibitManagerV1Interface *keyboardShortcutsInhibitManager() const
0128     {
0129         return m_keyboardShortcutsInhibitManager;
0130     }
0131     KWaylandServer::XwaylandShellV1Interface *xwaylandShell() const
0132     {
0133         return m_xwaylandShell;
0134     }
0135 
0136     bool isKeyboardShortcutsInhibited() const;
0137 
0138     KWaylandServer::LinuxDmaBufV1ClientBufferIntegration *linuxDmabuf();
0139 
0140     KWaylandServer::InputMethodV1Interface *inputMethod() const
0141     {
0142         return m_inputMethod;
0143     }
0144     KWaylandServer::IdleInterface *idle() const
0145     {
0146         return m_idle;
0147     }
0148     QList<Window *> windows() const
0149     {
0150         return m_windows;
0151     }
0152     void removeWindow(Window *c);
0153     Window *findWindow(const KWaylandServer::SurfaceInterface *surface) const;
0154     XdgToplevelWindow *findXdgToplevelWindow(KWaylandServer::SurfaceInterface *surface) const;
0155     XdgSurfaceWindow *findXdgSurfaceWindow(KWaylandServer::SurfaceInterface *surface) const;
0156 
0157     /**
0158      * @returns a transient parent of a surface imported with the foreign protocol, if any
0159      */
0160     KWaylandServer::SurfaceInterface *findForeignTransientForSurface(KWaylandServer::SurfaceInterface *surface);
0161 
0162     /**
0163      * @returns file descriptor for Xwayland to connect to.
0164      */
0165     int createXWaylandConnection();
0166     void destroyXWaylandConnection();
0167 
0168     /**
0169      * @returns file descriptor to the input method server's socket.
0170      */
0171     int createInputMethodConnection();
0172     void destroyInputMethodConnection();
0173 
0174     /**
0175      * @returns true if screen is locked.
0176      */
0177     bool isScreenLocked() const;
0178     /**
0179      * @returns whether integration with KScreenLocker is available.
0180      */
0181     bool hasScreenLockerIntegration() const;
0182 
0183     /**
0184      * @returns whether any kind of global shortcuts are supported.
0185      */
0186     bool hasGlobalShortcutSupport() const;
0187 
0188     void initWorkspace();
0189 
0190     KWaylandServer::ClientConnection *xWaylandConnection() const;
0191     KWaylandServer::ClientConnection *inputMethodConnection() const;
0192     KWaylandServer::ClientConnection *screenLockerClientConnection() const
0193     {
0194         return m_screenLockerClientConnection;
0195     }
0196 
0197     /**
0198      * Struct containing information for a created Wayland connection through a
0199      * socketpair.
0200      */
0201     struct SocketPairConnection
0202     {
0203         /**
0204          * ServerSide Connection
0205          */
0206         KWaylandServer::ClientConnection *connection = nullptr;
0207         /**
0208          * client-side file descriptor for the socket
0209          */
0210         int fd = -1;
0211     };
0212     /**
0213      * Creates a Wayland connection using a socket pair.
0214      */
0215     SocketPairConnection createConnection();
0216 
0217     QSet<KWaylandServer::LinuxDmaBufV1ClientBuffer *> linuxDmabufBuffers() const
0218     {
0219         return m_linuxDmabufBuffers;
0220     }
0221     void addLinuxDmabufBuffer(KWaylandServer::LinuxDmaBufV1ClientBuffer *buffer)
0222     {
0223         m_linuxDmabufBuffers << buffer;
0224     }
0225     void removeLinuxDmabufBuffer(KWaylandServer::LinuxDmaBufV1ClientBuffer *buffer)
0226     {
0227         m_linuxDmabufBuffers.remove(buffer);
0228     }
0229 
0230     /**
0231      * Returns the first socket name that can be used to connect to this server.
0232      * For a full list, use display()->socketNames()
0233      */
0234     QString socketName() const;
0235 
0236     XdgActivationV1Integration *xdgActivationIntegration() const
0237     {
0238         return m_xdgActivationIntegration;
0239     }
0240 
0241 Q_SIGNALS:
0242     void windowAdded(KWin::Window *);
0243     void windowRemoved(KWin::Window *);
0244     void initialized();
0245     void foreignTransientChanged(KWaylandServer::SurfaceInterface *child);
0246     void lockStateChanged();
0247 
0248 private:
0249     int createScreenLockerConnection();
0250     void windowShown(Window *t);
0251     void initScreenLocker();
0252     void registerXdgGenericWindow(Window *window);
0253     void registerXdgToplevelWindow(XdgToplevelWindow *window);
0254     void registerXdgPopupWindow(XdgPopupWindow *window);
0255     void registerWindow(Window *window);
0256     void handleOutputAdded(Output *output);
0257     void handleOutputRemoved(Output *output);
0258     void handleOutputEnabled(Output *output);
0259     void handleOutputDisabled(Output *output);
0260 
0261     class LockScreenPresentationWatcher : public QObject
0262     {
0263     public:
0264         LockScreenPresentationWatcher(WaylandServer *server);
0265 
0266     private:
0267         QSet<Output *> m_signaledOutputs;
0268     };
0269 
0270     KWaylandServer::Display *m_display = nullptr;
0271     KWaylandServer::CompositorInterface *m_compositor = nullptr;
0272     KWaylandServer::SeatInterface *m_seat = nullptr;
0273     KWaylandServer::TabletManagerV2Interface *m_tabletManagerV2 = nullptr;
0274     KWaylandServer::DataDeviceManagerInterface *m_dataDeviceManager = nullptr;
0275     KWaylandServer::PlasmaShellInterface *m_plasmaShell = nullptr;
0276     KWaylandServer::PlasmaWindowActivationFeedbackInterface *m_plasmaActivationFeedback = nullptr;
0277     KWaylandServer::PlasmaWindowManagementInterface *m_windowManagement = nullptr;
0278     KWaylandServer::PlasmaVirtualDesktopManagementInterface *m_virtualDesktopManagement = nullptr;
0279     KWaylandServer::ServerSideDecorationManagerInterface *m_decorationManager = nullptr;
0280     KWaylandServer::OutputManagementV2Interface *m_outputManagement = nullptr;
0281     KWaylandServer::AppMenuManagerInterface *m_appMenuManager = nullptr;
0282     KWaylandServer::ServerSideDecorationPaletteManagerInterface *m_paletteManager = nullptr;
0283     KWaylandServer::IdleInterface *m_idle = nullptr;
0284     KWaylandServer::XdgOutputManagerV1Interface *m_xdgOutputManagerV1 = nullptr;
0285     KWaylandServer::XdgDecorationManagerV1Interface *m_xdgDecorationManagerV1 = nullptr;
0286     KWaylandServer::LinuxDmaBufV1ClientBufferIntegration *m_linuxDmabuf = nullptr;
0287     KWaylandServer::KeyboardShortcutsInhibitManagerV1Interface *m_keyboardShortcutsInhibitManager = nullptr;
0288     QSet<KWaylandServer::LinuxDmaBufV1ClientBuffer *> m_linuxDmabufBuffers;
0289     QPointer<KWaylandServer::ClientConnection> m_xwaylandConnection;
0290     KWaylandServer::InputMethodV1Interface *m_inputMethod = nullptr;
0291     QPointer<KWaylandServer::ClientConnection> m_inputMethodServerConnection;
0292     KWaylandServer::ClientConnection *m_screenLockerClientConnection = nullptr;
0293     KWaylandServer::XdgForeignV2Interface *m_XdgForeign = nullptr;
0294     XdgActivationV1Integration *m_xdgActivationIntegration = nullptr;
0295     KWaylandServer::XWaylandKeyboardGrabManagerV1Interface *m_xWaylandKeyboardGrabManager = nullptr;
0296     KWaylandServer::ContentTypeManagerV1Interface *m_contentTypeManager = nullptr;
0297     KWaylandServer::TearingControlManagerV1Interface *m_tearingControlInterface = nullptr;
0298     KWaylandServer::XwaylandShellV1Interface *m_xwaylandShell = nullptr;
0299     QList<Window *> m_windows;
0300     InitializationFlags m_initFlags;
0301     QHash<Output *, KWaylandServer::OutputInterface *> m_waylandOutputs;
0302     QHash<Output *, KWaylandServer::OutputDeviceV2Interface *> m_waylandOutputDevices;
0303     KWaylandServer::DrmLeaseManagerV1 *m_leaseManager = nullptr;
0304     KWaylandServer::OutputOrderV1Interface *m_outputOrder = nullptr;
0305     KWIN_SINGLETON(WaylandServer)
0306 };
0307 
0308 inline WaylandServer *waylandServer()
0309 {
0310     return WaylandServer::self();
0311 }
0312 
0313 } // namespace KWin