File indexing completed on 2024-05-12 05:32:20

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003     SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 #pragma once
0008 
0009 #include "kwin_export.h"
0010 
0011 #include <QList>
0012 #include <QObject>
0013 
0014 struct wl_client;
0015 struct wl_display;
0016 struct wl_resource;
0017 
0018 namespace KWin
0019 {
0020 
0021 class ClientConnection;
0022 class DisplayPrivate;
0023 class OutputInterface;
0024 class OutputDeviceV2Interface;
0025 class SeatInterface;
0026 class GraphicsBuffer;
0027 
0028 /**
0029  * @brief Class holding the Wayland server display loop.
0030  *
0031  * @todo Improve documentation
0032  */
0033 class KWIN_EXPORT Display : public QObject
0034 {
0035     Q_OBJECT
0036     Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
0037 public:
0038     explicit Display(QObject *parent = nullptr);
0039     virtual ~Display();
0040 
0041     /**
0042      * Adds a socket with the given @p fileDescriptor to the Wayland display. This function
0043      * returns @c true if the socket has been added successfully; otherwise returns @c false.
0044      *
0045      * The compositor can call this function even after the display has been started.
0046      * @arg socketName can optionally be parsed to store the socket name represented by the given file-descriptor
0047      *
0048      * @see start()
0049      */
0050     bool addSocketFileDescriptor(int fileDescriptor, const QString &socketName = QString());
0051     /**
0052      * Adds a UNIX socket with the specified @p name to the Wayland display. This function
0053      * returns @c true if the socket has been added successfully; otherwise returns @c false.
0054      *
0055      * If the specified socket name @p name is empty, the display will pick a free socket with
0056      * a filename "wayland-%d".
0057      *
0058      * The compositor can call this function even after the display has been started.
0059      *
0060      * @see start()
0061      */
0062     bool addSocketName(const QString &name = QString());
0063 
0064     /**
0065      * Returns the list of socket names that the display listens for client connections.
0066      */
0067     QStringList socketNames() const;
0068 
0069     quint32 serial();
0070     quint32 nextSerial();
0071 
0072     /**
0073      * Start accepting client connections. If the display has started successfully, this
0074      * function returns @c true; otherwise @c false is returned.
0075      */
0076     bool start();
0077     void dispatchEvents();
0078 
0079     /**
0080      * Create a client for the given file descriptor.
0081      *
0082      * The client is created as if it connected through the normal server
0083      * socket. This method can be used to create a connection bypassing the
0084      * normal socket connection. It's recommended to use together with
0085      * socketpair and pass the other side of the socket to the client.
0086      *
0087      * @param fd The file descriptor for the socket to the client
0088      * @returns The new ClientConnection or @c null on failure.
0089      */
0090     ClientConnection *createClient(int fd);
0091 
0092     operator wl_display *();
0093     operator wl_display *() const;
0094     bool isRunning() const;
0095 
0096     void createShm();
0097     /**
0098      * @returns All SeatInterface currently managed on the Display.
0099      */
0100     QList<SeatInterface *> seats() const;
0101     QList<OutputDeviceV2Interface *> outputDevices() const;
0102     QList<OutputInterface *> outputs() const;
0103     QList<OutputInterface *> outputsIntersecting(const QRect &rect) const;
0104     OutputInterface *largestIntersectingOutput(const QRect &rect) const;
0105 
0106     /**
0107      * Gets the ClientConnection for the given @p client.
0108      * If there is no ClientConnection yet for the given @p client, it will be created.
0109      * @param client The native client for which the ClientConnection is retrieved
0110      * @return The ClientConnection for the given native client
0111      */
0112     ClientConnection *getConnection(wl_client *client);
0113     QList<ClientConnection *> connections() const;
0114 
0115     /**
0116      * Returns the graphics buffer for the given @a resource, or @c null if there's no buffer.
0117      */
0118     static GraphicsBuffer *bufferForResource(wl_resource *resource);
0119 
0120 private Q_SLOTS:
0121     void flush();
0122 
0123 Q_SIGNALS:
0124     void socketNamesChanged();
0125     void runningChanged(bool);
0126     void clientConnected(KWin::ClientConnection *);
0127     void clientDisconnected(KWin::ClientConnection *);
0128 
0129 private:
0130     friend class DisplayPrivate;
0131     std::unique_ptr<DisplayPrivate> d;
0132 };
0133 
0134 }