2020-03-15 15:19:28 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
|
2014-11-10 13:56:17 +00:00
|
|
|
|
2020-03-15 15:19:28 +00:00
|
|
|
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
|
|
|
|
*/
|
2023-05-25 11:15:57 +00:00
|
|
|
|
2023-09-13 05:52:59 +00:00
|
|
|
#include "core/graphicsbufferview.h"
|
2023-10-06 09:11:35 +00:00
|
|
|
#include "wayland/compositor.h"
|
|
|
|
#include "wayland/datadevicemanager.h"
|
|
|
|
#include "wayland/display.h"
|
|
|
|
#include "wayland/keyboard.h"
|
|
|
|
#include "wayland/output.h"
|
|
|
|
#include "wayland/pointer.h"
|
|
|
|
#include "wayland/seat.h"
|
|
|
|
#include "wayland/xdgshell.h"
|
2014-11-10 13:56:17 +00:00
|
|
|
|
2022-08-25 08:02:48 +00:00
|
|
|
#include "fakeoutput.h"
|
|
|
|
|
2014-11-10 13:56:17 +00:00
|
|
|
#include <QApplication>
|
2015-01-09 11:08:23 +00:00
|
|
|
#include <QCommandLineParser>
|
2014-11-10 13:56:17 +00:00
|
|
|
#include <QDateTime>
|
2023-03-24 21:46:46 +00:00
|
|
|
#include <QFile>
|
2014-11-10 13:56:17 +00:00
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QPainter>
|
2023-03-24 21:46:46 +00:00
|
|
|
#include <QThreadPool>
|
2014-11-10 13:56:17 +00:00
|
|
|
#include <QWidget>
|
2015-01-09 11:08:23 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2021-08-29 05:11:06 +00:00
|
|
|
#include <unistd.h>
|
2015-01-09 11:08:23 +00:00
|
|
|
|
|
|
|
static int startXServer()
|
|
|
|
{
|
|
|
|
const QByteArray process = QByteArrayLiteral("Xwayland");
|
|
|
|
int pipeFds[2];
|
|
|
|
if (pipe(pipeFds) != 0) {
|
2021-08-29 05:11:06 +00:00
|
|
|
std::cerr << "FATAL ERROR failed to create pipe to start X Server " << process.constData() << std::endl;
|
2015-01-09 11:08:23 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
// child process - should be turned into Xwayland
|
|
|
|
// writes to pipe, closes read side
|
|
|
|
close(pipeFds[0]);
|
|
|
|
char fdbuf[16];
|
|
|
|
sprintf(fdbuf, "%d", pipeFds[1]);
|
2017-01-16 08:44:16 +00:00
|
|
|
execlp(process.constData(), process.constData(), "-displayfd", fdbuf, "-rootless", (char *)nullptr);
|
2015-01-09 11:08:23 +00:00
|
|
|
close(pipeFds[1]);
|
|
|
|
exit(20);
|
|
|
|
}
|
|
|
|
// parent process - this is the wayland server
|
|
|
|
// reads from pipe, closes write side
|
|
|
|
close(pipeFds[1]);
|
|
|
|
return pipeFds[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void readDisplayFromPipe(int pipe)
|
|
|
|
{
|
|
|
|
QFile readPipe;
|
|
|
|
if (!readPipe.open(pipe, QIODevice::ReadOnly)) {
|
|
|
|
std::cerr << "FATAL ERROR failed to open pipe to start X Server XWayland" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
QByteArray displayNumber = readPipe.readLine();
|
|
|
|
|
|
|
|
displayNumber.prepend(QByteArray(":"));
|
2021-08-29 05:11:06 +00:00
|
|
|
displayNumber.remove(displayNumber.size() - 1, 1);
|
2015-01-09 11:08:23 +00:00
|
|
|
std::cout << "X-Server started on display " << displayNumber.constData() << std::endl;
|
|
|
|
|
|
|
|
setenv("DISPLAY", displayNumber.constData(), true);
|
|
|
|
|
|
|
|
// close our pipe
|
|
|
|
close(pipe);
|
|
|
|
}
|
2014-11-10 13:56:17 +00:00
|
|
|
|
|
|
|
class CompositorWindow : public QWidget
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit CompositorWindow(QWidget *parent = nullptr);
|
|
|
|
virtual ~CompositorWindow();
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
void surfaceCreated(KWin::XdgToplevelInterface *surface);
|
2014-11-10 13:56:17 +00:00
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
void setSeat(const QPointer<KWin::SeatInterface> &seat);
|
2014-11-10 13:56:17 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void paintEvent(QPaintEvent *event) override;
|
|
|
|
void keyPressEvent(QKeyEvent *event) override;
|
|
|
|
void keyReleaseEvent(QKeyEvent *event) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent *event) override;
|
|
|
|
void mousePressEvent(QMouseEvent *event) override;
|
|
|
|
void mouseReleaseEvent(QMouseEvent *event) override;
|
|
|
|
void wheelEvent(QWheelEvent *event) override;
|
|
|
|
|
|
|
|
private:
|
2014-11-27 18:24:16 +00:00
|
|
|
void updateFocus();
|
2023-09-13 17:59:29 +00:00
|
|
|
QList<KWin::XdgToplevelInterface *> m_stackingOrder;
|
|
|
|
QPointer<KWin::SeatInterface> m_seat;
|
2014-11-10 13:56:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CompositorWindow::CompositorWindow(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
setMouseTracking(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
CompositorWindow::~CompositorWindow() = default;
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
void CompositorWindow::surfaceCreated(KWin::XdgToplevelInterface *surface)
|
2014-11-10 13:56:17 +00:00
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2020-05-04 13:32:23 +00:00
|
|
|
surface->sendConfigure(QSize(), XdgToplevelInterface::States());
|
2014-11-10 13:56:17 +00:00
|
|
|
m_stackingOrder << surface;
|
|
|
|
connect(surface->surface(), &SurfaceInterface::damaged, this, static_cast<void (CompositorWindow::*)()>(&CompositorWindow::update));
|
2021-08-29 05:11:06 +00:00
|
|
|
connect(surface, &XdgToplevelInterface::destroyed, this, [surface, this] {
|
|
|
|
m_stackingOrder.removeAll(surface);
|
|
|
|
updateFocus();
|
|
|
|
update();
|
|
|
|
});
|
2014-11-27 18:24:16 +00:00
|
|
|
updateFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::updateFocus()
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-27 18:24:16 +00:00
|
|
|
if (!m_seat || m_stackingOrder.isEmpty()) {
|
|
|
|
return;
|
2014-11-27 10:25:28 +00:00
|
|
|
}
|
2021-08-29 05:11:06 +00:00
|
|
|
auto it = std::find_if(m_stackingOrder.constBegin(), m_stackingOrder.constEnd(), [](XdgToplevelInterface *toplevel) {
|
|
|
|
return toplevel->surface()->isMapped();
|
|
|
|
});
|
2014-11-27 18:24:16 +00:00
|
|
|
if (it == m_stackingOrder.constEnd()) {
|
|
|
|
return;
|
|
|
|
}
|
2022-02-23 19:03:51 +00:00
|
|
|
m_seat->notifyPointerEnter((*it)->surface(), m_seat->pointerPos());
|
2014-11-27 18:24:16 +00:00
|
|
|
m_seat->setFocusedKeyboardSurface((*it)->surface());
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
void CompositorWindow::setSeat(const QPointer<KWin::SeatInterface> &seat)
|
2014-11-10 13:56:17 +00:00
|
|
|
{
|
|
|
|
m_seat = seat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::paintEvent(event);
|
|
|
|
QPainter p(this);
|
2020-05-20 09:37:33 +00:00
|
|
|
for (auto window : m_stackingOrder) {
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::SurfaceInterface *surface = window->surface();
|
2020-05-20 09:37:33 +00:00
|
|
|
if (!surface || !surface->isMapped()) {
|
|
|
|
continue;
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
2023-05-25 11:15:57 +00:00
|
|
|
KWin::GraphicsBufferView view(surface->buffer());
|
|
|
|
if (view.image()) {
|
|
|
|
p.drawImage(QPoint(0, 0), *view.image());
|
2021-07-20 17:02:13 +00:00
|
|
|
}
|
2020-05-20 09:37:33 +00:00
|
|
|
surface->frameRendered(QDateTime::currentMSecsSinceEpoch());
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::keyPressEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::keyPressEvent(event);
|
|
|
|
if (!m_seat) {
|
|
|
|
return;
|
|
|
|
}
|
2014-11-26 14:00:44 +00:00
|
|
|
if (!m_seat->focusedKeyboardSurface()) {
|
2014-11-27 18:24:16 +00:00
|
|
|
updateFocus();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyKeyboardKey(event->nativeScanCode() - 8, KWin::KeyboardKeyState::Pressed);
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::keyReleaseEvent(event);
|
|
|
|
if (!m_seat) {
|
|
|
|
return;
|
|
|
|
}
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyKeyboardKey(event->nativeScanCode() - 8, KWin::KeyboardKeyState::Released);
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::mouseMoveEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::mouseMoveEvent(event);
|
2014-11-25 14:54:28 +00:00
|
|
|
if (!m_seat->focusedPointerSurface()) {
|
2014-11-27 18:24:16 +00:00
|
|
|
updateFocus();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2021-03-11 08:24:47 +00:00
|
|
|
m_seat->notifyPointerMotion(event->localPos().toPoint());
|
|
|
|
m_seat->notifyPointerFrame();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::mousePressEvent(event);
|
2014-11-25 14:54:28 +00:00
|
|
|
if (!m_seat->focusedPointerSurface()) {
|
2014-11-10 13:56:17 +00:00
|
|
|
if (!m_stackingOrder.isEmpty()) {
|
2022-02-23 19:03:51 +00:00
|
|
|
m_seat->notifyPointerEnter(m_stackingOrder.last()->surface(), event->globalPos());
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyPointerButton(event->button(), KWin::PointerButtonState::Pressed);
|
2021-03-11 08:24:47 +00:00
|
|
|
m_seat->notifyPointerFrame();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::mouseReleaseEvent(event);
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyPointerButton(event->button(), KWin::PointerButtonState::Released);
|
2021-03-11 08:24:47 +00:00
|
|
|
m_seat->notifyPointerFrame();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompositorWindow::wheelEvent(QWheelEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::wheelEvent(event);
|
2022-12-21 00:55:20 +00:00
|
|
|
m_seat->setTimestamp(std::chrono::milliseconds(event->timestamp()));
|
2014-11-10 13:56:17 +00:00
|
|
|
const QPoint &angle = event->angleDelta() / (8 * 15);
|
|
|
|
if (angle.x() != 0) {
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyPointerAxis(Qt::Horizontal, angle.x(), 1, KWin::PointerAxisSource::Wheel);
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
if (angle.y() != 0) {
|
2023-09-13 17:59:29 +00:00
|
|
|
m_seat->notifyPointerAxis(Qt::Vertical, angle.y(), 1, KWin::PointerAxisSource::Wheel);
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
2021-03-11 08:24:47 +00:00
|
|
|
m_seat->notifyPointerFrame();
|
2014-11-10 13:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2023-09-13 17:59:29 +00:00
|
|
|
using namespace KWin;
|
2014-11-10 13:56:17 +00:00
|
|
|
QApplication app(argc, argv);
|
|
|
|
|
2015-01-09 11:08:23 +00:00
|
|
|
QCommandLineParser parser;
|
|
|
|
parser.addHelpOption();
|
2021-08-29 05:11:06 +00:00
|
|
|
QCommandLineOption xwaylandOption(QStringList{QStringLiteral("x"), QStringLiteral("xwayland")}, QStringLiteral("Start a rootless Xwayland server"));
|
2015-01-09 11:08:23 +00:00
|
|
|
parser.addOption(xwaylandOption);
|
|
|
|
parser.process(app);
|
|
|
|
|
2023-09-13 17:59:29 +00:00
|
|
|
KWin::Display display;
|
2014-11-10 13:56:17 +00:00
|
|
|
display.start();
|
2020-12-09 20:13:19 +00:00
|
|
|
new DataDeviceManagerInterface(&display);
|
|
|
|
new CompositorInterface(&display, &display);
|
|
|
|
XdgShellInterface *shell = new XdgShellInterface(&display);
|
2014-11-10 13:56:17 +00:00
|
|
|
display.createShm();
|
2022-08-25 08:02:48 +00:00
|
|
|
|
2014-11-10 13:56:17 +00:00
|
|
|
const QSize windowSize(1024, 768);
|
2022-08-25 08:02:48 +00:00
|
|
|
|
|
|
|
auto outputHandle = std::make_unique<FakeOutput>();
|
|
|
|
outputHandle->setPhysicalSize(QSize(269, 202));
|
|
|
|
outputHandle->setMode(windowSize, 60000);
|
|
|
|
|
2022-10-15 11:52:22 +00:00
|
|
|
auto outputInterface = std::make_unique<OutputInterface>(&display, outputHandle.get());
|
2022-08-25 08:02:48 +00:00
|
|
|
|
2020-12-09 20:13:19 +00:00
|
|
|
SeatInterface *seat = new SeatInterface(&display);
|
2014-11-10 13:56:17 +00:00
|
|
|
seat->setHasKeyboard(true);
|
|
|
|
seat->setHasPointer(true);
|
|
|
|
seat->setName(QStringLiteral("testSeat0"));
|
|
|
|
|
|
|
|
CompositorWindow compositorWindow;
|
|
|
|
compositorWindow.setSeat(seat);
|
|
|
|
compositorWindow.setMinimumSize(windowSize);
|
|
|
|
compositorWindow.setMaximumSize(windowSize);
|
|
|
|
compositorWindow.setGeometry(QRect(QPoint(0, 0), windowSize));
|
|
|
|
compositorWindow.show();
|
2020-05-04 13:32:23 +00:00
|
|
|
QObject::connect(shell, &XdgShellInterface::toplevelCreated, &compositorWindow, &CompositorWindow::surfaceCreated);
|
2014-11-10 13:56:17 +00:00
|
|
|
|
2015-01-09 11:08:23 +00:00
|
|
|
// start XWayland
|
|
|
|
if (parser.isSet(xwaylandOption)) {
|
|
|
|
// starts XWayland by forking and opening a pipe
|
|
|
|
const int pipe = startXServer();
|
|
|
|
if (pipe == -1) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-03-24 21:46:46 +00:00
|
|
|
QThreadPool::globalInstance()->start([pipe] {
|
2021-08-29 05:11:06 +00:00
|
|
|
readDisplayFromPipe(pipe);
|
|
|
|
});
|
2015-01-09 11:08:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 13:56:17 +00:00
|
|
|
return app.exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "renderingservertest.moc"
|