File indexing completed on 2024-12-08 04:57:54

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