Skip to content

Commit d060bef

Browse files
committed
fix: try to fix crash in processWaylandEvents
1 parent 0522f1c commit d060bef

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/server/kernel/private/wserver_p.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ class Q_DECL_HIDDEN WServerPrivate : public WObjectPrivate
2222
{
2323
public:
2424
WServerPrivate(WServer *qq);
25-
~WServerPrivate();
25+
~WServerPrivate() override;
2626

2727
void init();
2828
void stop();
2929

30+
void dispatchEvents();
31+
void flush();
32+
3033
void initSocket(WSocket *socketServer);
3134

3235
W_DECLARE_PUBLIC(WServer)

src/server/kernel/wserver.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <QAbstractEventDispatcher>
2626
#include <QSocketNotifier>
2727
#include <QMutex>
28-
#include <QDebug>
28+
#include <QLoggingCategory>
2929
#include <QProcess>
3030
#include <QLocalServer>
3131
#include <QLocalSocket>
@@ -38,6 +38,8 @@
3838
QW_USE_NAMESPACE
3939
WAYLIB_SERVER_BEGIN_NAMESPACE
4040

41+
Q_LOGGING_CATEGORY(qLcWlrServer, "waylib.server.core")
42+
4143
static bool globalFilter(const wl_client *client,
4244
const wl_global *global,
4345
void *data) {
@@ -102,20 +104,23 @@ void WServerPrivate::init()
102104
}
103105

104106
loop = wl_display_get_event_loop(display->handle());
105-
int fd = wl_event_loop_get_fd(loop);
106-
107-
auto processWaylandEvents = [this] {
108-
int ret = wl_event_loop_dispatch(loop, 0);
109-
if (ret)
110-
fprintf(stderr, "wl_event_loop_dispatch error: %d\n", ret);
111-
wl_display_flush_clients(display->handle());
112-
};
107+
const int fd = wl_event_loop_get_fd(loop);
108+
if (fd == -1) {
109+
qCFatal(qLcWlrServer) << "Did not get the file descriptor for the event loop";
110+
}
113111

114112
sockNot.reset(new QSocketNotifier(fd, QSocketNotifier::Read));
115-
QObject::connect(sockNot.get(), &QSocketNotifier::activated, q, processWaylandEvents);
113+
QObject::connect(sockNot.get(), &QSocketNotifier::activated, q, [this] {
114+
dispatchEvents();
115+
});
116116

117-
QAbstractEventDispatcher *dispatcher = QThread::currentThread()->eventDispatcher();
118-
QObject::connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, q, processWaylandEvents);
117+
QAbstractEventDispatcher *dispatcher = QAbstractEventDispatcher::instance();
118+
QObject::connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, q, [this] {
119+
flush();
120+
});
121+
QObject::connect(dispatcher, &QAbstractEventDispatcher::awake, q, [this] {
122+
flush();
123+
});
119124

120125
for (auto socket : std::as_const(sockets))
121126
initSocket(socket);
@@ -142,6 +147,18 @@ void WServerPrivate::stop()
142147
QThread::currentThread()->eventDispatcher()->disconnect(q);
143148
}
144149

150+
void WServerPrivate::dispatchEvents()
151+
{
152+
int ret = wl_event_loop_dispatch(loop, 0);
153+
if (ret)
154+
qCCritical(qLcWlrServer, "wl_event_loop_dispatch error: %d\n", ret);
155+
}
156+
157+
void WServerPrivate::flush()
158+
{
159+
wl_display_flush_clients(display->handle());
160+
}
161+
145162
void WServerPrivate::initSocket(WSocket *socketServer)
146163
{
147164
bool ok = socketServer->listen(display->handle());

0 commit comments

Comments
 (0)