summaryrefslogtreecommitdiffstats
path: root/ui/qt/glib_mainloop_on_qeventloop.h
blob: 9f66c6816498332d0b492b33858bce79218d7094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/** @file
 *
 * Copyright 2022 Tomasz Mon <desowin@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef GLIB_MAINLOOP_ON_QEVENTLOOP_H
#define GLIB_MAINLOOP_ON_QEVENTLOOP_H

#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <glib.h>

class GLibPoller : public QThread
{
    Q_OBJECT

protected:
    explicit GLibPoller(GMainContext *context);
    ~GLibPoller();

    void run() override;

    QMutex mutex_;
    QWaitCondition dispatched_;
    GMainContext *ctx_;
    gint priority_;
    GPollFD *fds_;
    gint allocated_fds_, nfds_;

signals:
    void polled(void);

    friend class GLibMainloopOnQEventLoop;
};

class GLibMainloopOnQEventLoop : public QObject
{
    Q_OBJECT

protected:
    explicit GLibMainloopOnQEventLoop(QObject *parent);
    ~GLibMainloopOnQEventLoop();

protected slots:
    void checkAndDispatch();

public:
    static void setup(QObject *parent);

protected:
    GLibPoller poller_;
};

#endif /* GLIB_MAINLOOP_ON_QEVENTLOOP_H */