Changeset View
Changeset View
Standalone View
Standalone View
tests/screenrecorder.cpp
- This file was added.
1 | #include <KWayland/Server/display.h> | ||||
---|---|---|---|---|---|
2 | #include <KWayland/Server/output_interface.h> | ||||
3 | #include <KWayland/Server/compositor_interface.h> | ||||
4 | #include <KWayland/Server/buffer_interface.h> | ||||
5 | | ||||
6 | #include <QGuiApplication> | ||||
7 | #include <QWindow> | ||||
8 | #include <QDebug> | ||||
9 | #include <QDBusConnection> | ||||
10 | #include <QDBusMessage> | ||||
11 | #include <QDBusPendingCallWatcher> | ||||
12 | | ||||
13 | using namespace KWayland::Server; | ||||
14 | | ||||
15 | /* | ||||
16 | * This is a simple screen recorder app | ||||
17 | * It connects to a wayland server then dumps all frames in the CWD | ||||
18 | */ | ||||
19 | | ||||
20 | class Recorder : public QObject | ||||
21 | { | ||||
22 | Q_OBJECT | ||||
23 | public: | ||||
24 | Recorder(); | ||||
25 | private slots: | ||||
26 | void newImage(const QImage &image); | ||||
27 | private: | ||||
28 | quint64 m_frame = 0; | ||||
29 | }; | ||||
30 | | ||||
31 | int main(int argc, char ** argv) | ||||
32 | { | ||||
33 | QGuiApplication app(argc, argv); | ||||
34 | Recorder server; | ||||
35 | app.exec(); | ||||
36 | } | ||||
37 | | ||||
38 | | ||||
39 | Recorder::Recorder() | ||||
40 | { | ||||
41 | auto display = new Display(this); | ||||
42 | | ||||
43 | const QString socketName("recorder"); | ||||
44 | | ||||
45 | //TODO export wl_display_connect_to_fd (int fd) in Server::Display | ||||
46 | //then pass a proper FD over DBUS | ||||
47 | display->setSocketName(socketName); | ||||
48 | display->start(); | ||||
49 | display->createShm(); | ||||
50 | | ||||
51 | auto compositor = display->createCompositor(this); | ||||
52 | compositor->create(); | ||||
53 | | ||||
54 | connect(compositor, &CompositorInterface::surfaceCreated, this, [this](SurfaceInterface* si) { | ||||
55 | qDebug() << "kwin surface found"; | ||||
56 | connect(si, &SurfaceInterface::damaged, this, [this, si]() { | ||||
57 | if (!si->buffer()) return; | ||||
58 | | ||||
59 | auto image = si->buffer()->data(); | ||||
60 | newImage(image); | ||||
61 | }); | ||||
62 | }); | ||||
63 | | ||||
64 | auto msg = QDBusMessage::createMethodCall("org.kde.KWin", "/Screencast", "org.kde.kwin.Screencast", "screencast"); | ||||
65 | msg << socketName; | ||||
66 | | ||||
67 | auto reply = QDBusConnection::sessionBus().asyncCall(msg); | ||||
68 | auto watcher = new QDBusPendingCallWatcher(reply, this); | ||||
69 | connect(watcher, &QDBusPendingCallWatcher::finished, this, [reply, watcher]() { | ||||
70 | watcher->deleteLater(); | ||||
71 | if (reply.isError()) { | ||||
72 | qWarning() << reply.error().message(); | ||||
73 | } | ||||
74 | }); | ||||
75 | } | ||||
76 | | ||||
77 | void Recorder::newImage(const QImage &image) | ||||
78 | { | ||||
79 | const QString path = "out_" + QString::number(m_frame); | ||||
80 | image.save(path + ".png"); | ||||
81 | m_frame++; | ||||
82 | } | ||||
83 | | ||||
84 | | ||||
85 | #include "screenrecorder.moc" |