KWin
Loading...
Searching...
No Matches
pipewirecore.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2018-2020 Red Hat Inc
3 SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
4 SPDX-FileContributor: Jan Grulich <jgrulich@redhat.com>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "pipewirecore.h"
10#include "kwinscreencast_logging.h"
11#include <cerrno>
12
13#include <KLocalizedString>
14
15#include <QSocketNotifier>
16
17namespace KWin
18{
19
21{
22 pw_init(nullptr, nullptr);
23 pwCoreEvents.version = PW_VERSION_CORE_EVENTS;
25}
26
28{
29 if (pwMainLoop) {
30 pw_loop_leave(pwMainLoop);
31 }
32
33 if (pwCore) {
34 pw_core_disconnect(pwCore);
35 }
36
37 if (pwContext) {
38 pw_context_destroy(pwContext);
39 }
40
41 if (pwMainLoop) {
42 pw_loop_destroy(pwMainLoop);
43 }
44}
45
46void PipeWireCore::onCoreError(void *data, uint32_t id, int seq, int res, const char *message)
47{
48 qCWarning(KWIN_SCREENCAST) << "PipeWire remote error: " << message;
49 if (id == PW_ID_CORE && res == -EPIPE) {
50 PipeWireCore *pw = static_cast<PipeWireCore *>(data);
51 Q_EMIT pw->pipewireFailed(QString::fromUtf8(message));
52 }
53}
54
56{
57 pwMainLoop = pw_loop_new(nullptr);
58 if (!pwMainLoop) {
59 qCWarning(KWIN_SCREENCAST, "Failed to create PipeWire loop: %s", strerror(errno));
60 m_error = i18n("Failed to start main PipeWire loop");
61 return false;
62 }
63 pw_loop_enter(pwMainLoop);
64
65 QSocketNotifier *notifier = new QSocketNotifier(pw_loop_get_fd(pwMainLoop), QSocketNotifier::Read, this);
66 connect(notifier, &QSocketNotifier::activated, this, [this] {
67 int result = pw_loop_iterate(pwMainLoop, 0);
68 if (result < 0) {
69 qCWarning(KWIN_SCREENCAST) << "pipewire_loop_iterate failed: " << result;
70 }
71 });
72
73 pwContext = pw_context_new(pwMainLoop, nullptr, 0);
74 if (!pwContext) {
75 qCWarning(KWIN_SCREENCAST) << "Failed to create PipeWire context";
76 m_error = i18n("Failed to create PipeWire context");
77 return false;
78 }
79
80 pwCore = pw_context_connect(pwContext, nullptr, 0);
81 if (!pwCore) {
82 qCWarning(KWIN_SCREENCAST) << "Failed to connect PipeWire context";
83 m_error = i18n("Failed to connect PipeWire context");
84 return false;
85 }
86
87 if (pw_loop_iterate(pwMainLoop, 0) < 0) {
88 qCWarning(KWIN_SCREENCAST) << "Failed to start main PipeWire loop";
89 m_error = i18n("Failed to start main PipeWire loop");
90 return false;
91 }
92
93 pw_core_add_listener(pwCore, &coreListener, &pwCoreEvents, this);
94 return true;
95}
96
97} // namespace KWin
98
99#include "moc_pipewirecore.cpp"
struct pw_loop * pwMainLoop
struct pw_core * pwCore
pw_core_events pwCoreEvents
void pipewireFailed(const QString &message)
static void onCoreError(void *data, uint32_t id, int seq, int res, const char *message)
struct pw_context * pwContext