File: wayland_runner.cpp

package info (click to toggle)
mir 2.20.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,636 kB
  • sloc: cpp: 174,574; xml: 13,422; ansic: 8,221; python: 1,337; sh: 874; makefile: 216; javascript: 37
file content (244 lines) | stat: -rw-r--r-- 5,628 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 * Copyright © Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 or 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "wayland_runner.h"

#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <sys/eventfd.h>

#include <boost/throw_exception.hpp>

#include <list>
#include <mutex>
#include <system_error>

namespace
{

// We use a namespace object constructor to install our signal handler once
struct State
{
    State();

    void run(wl_display* display);
    void quit();

    static int shutdown_fd;
};

State state;
int State::shutdown_fd = 0;

extern "C" void signal_shutdown(int signum)
{
    printf("Signal %d received. Good night.\n", signum);
    state.quit();
}

class ActionQueue
{
public:
    using FdEvents = uint32_t;

    ActionQueue();
    ~ActionQueue();
    int watch_fd() const;

    void enqueue(std::function<void()> const&& action);

    bool dispatch(FdEvents events);

private:
    bool consume();
    void wake();
    int event_fd;
    std::mutex list_lock;
    std::list<std::function<void()>> actions;
};

ActionQueue action_queue;
}

void State::run(wl_display* display)
{
    enum FdIndices {
        display_fd = 0,
        actions,
        shutdown,
        indices
    };

    struct pollfd fds[indices] = {
        {wl_display_get_fd(display), POLLIN, 0},
        {action_queue.watch_fd(),    POLLIN, 0},
        {shutdown_fd,                POLLIN, 0},
    };

    while (!(fds[shutdown].revents & (POLLIN | POLLERR)))
    {
        while (wl_display_prepare_read(display) != 0)
        {
            switch (wl_display_dispatch_pending(display))
            {
            case -1:
                fprintf(stderr, "Failed to dispatch Wayland events\n");
                abort();
            case 0:
                break;
            default:
                wl_display_flush(display);
            }
        }

        if (poll(fds, indices, -1) == -1)
        {
            fprintf(stderr, "Failed to poll\n");
            wl_display_cancel_read(display);
            break;
        }

        if (fds[display_fd].revents & (POLLIN | POLLERR))
        {
            if (wl_display_read_events(display))
            {
                fprintf(stderr, "Failed to read Wayland events\n");
                abort();
            }
        }
        else
        {
            wl_display_cancel_read(display);
        }

        action_queue.dispatch(fds[actions].revents);
    }
}

State::State()
{
    shutdown_fd = eventfd(0, EFD_CLOEXEC);

    struct sigaction sig_handler_new;
    sigfillset(&sig_handler_new.sa_mask);
    sig_handler_new.sa_flags = 0;
    sig_handler_new.sa_handler = signal_shutdown;

    sigaction(SIGINT, &sig_handler_new, NULL);
    sigaction(SIGTERM, &sig_handler_new, NULL);
    sigaction(SIGHUP, &sig_handler_new, NULL);
}

void State::quit()
{
    if (eventfd_write(shutdown_fd, 1) == -1)
    {
        printf("Failed to execute a shutdown");
        exit(-1);
    }
}

void mir::client::wayland_runner::run(wl_display* display)
{
    state.run(display);
}

void mir::client::wayland_runner::quit()
{
    state.quit();
}

void mir::client::wayland_runner::spawn(std::function<void()>&& function)
{
    action_queue.enqueue(std::move(function));
}

ActionQueue::ActionQueue()
    : event_fd{eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK|EFD_SEMAPHORE)}
{
    if (event_fd < 0)
        BOOST_THROW_EXCEPTION((std::system_error{errno,
                                                 std::system_category(),
                                                 "Failed to create event fd for action queue"}));
}

int ActionQueue::watch_fd() const
{
    return event_fd;
}

void ActionQueue::enqueue(std::function<void()> const&& action)
{
    std::unique_lock lock(list_lock);
    actions.push_back(action);
    wake();
}

bool ActionQueue::dispatch(FdEvents events)
{
    if (events & POLLERR)
        return false;

    if (!consume())
    {
        // Another thread has won the race to process this event
        // That's OK, just bail.
        return true;
    }

    std::function<void()> action_to_process;

    {
        std::unique_lock lock(list_lock);
        action_to_process = actions.front();
        actions.pop_front();
    }

    action_to_process();

    return true;
}

bool ActionQueue::consume()
{
    uint64_t num_actions;
    if (read(event_fd, &num_actions, sizeof num_actions) != sizeof num_actions)
    {
        if (errno == EAGAIN)
        {
            return false;
        }
        BOOST_THROW_EXCEPTION((std::system_error{errno,
                                                 std::system_category(),
                                                 "Failed to consume action queue notification"}));
    }
    return true;
}

void ActionQueue::wake()
{
    uint64_t one_more{1};
    if (write(event_fd, &one_more, sizeof one_more) != sizeof one_more)
        BOOST_THROW_EXCEPTION((std::system_error{errno,
                                                 std::system_category(),
                                                 "Failed to wake action queue"}));
}

ActionQueue::~ActionQueue()
{
    close(event_fd);
}