summaryrefslogtreecommitdiff
path: root/server/event-loop.c
blob: d295e42e58e48bb9592f1860f74e27f53008c72b (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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2009-2015 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

/*
 * This file exports a global variable:
 *
 * const SpiceCoreInterfaceInternal event_loop_core;
 */
#include <config.h>

#include "red-common.h"

typedef struct SpiceCoreFuncs {
    void (*timer_start)(SpiceTimer *timer, uint32_t ms);
    void (*timer_cancel)(SpiceTimer *timer);
    void (*timer_remove)(SpiceTimer *timer);
    void (*watch_update_mask)(SpiceWatch *watch, int event_mask);
    void (*watch_remove)(SpiceWatch *watch);
} SpiceCoreFuncs;

struct SpiceTimer {
    const SpiceCoreFuncs *funcs;
};

struct SpiceWatch {
    const SpiceCoreFuncs *funcs;
};

void red_timer_start(SpiceTimer *timer, uint32_t ms)
{
    if (timer) {
        timer->funcs->timer_start(timer, ms);
    }
}

void red_timer_cancel(SpiceTimer *timer)
{
    if (timer) {
        timer->funcs->timer_cancel(timer);
    }
}

void red_timer_remove(SpiceTimer *timer)
{
    if (timer) {
        timer->funcs->timer_remove(timer);
    }
}

void red_watch_update_mask(SpiceWatch *watch, int event_mask)
{
    if (watch) {
        watch->funcs->watch_update_mask(watch, event_mask);
    }
}

void red_watch_remove(SpiceWatch *watch)
{
    if (watch) {
        watch->funcs->watch_remove(watch);
    }
}

static const SpiceCoreFuncs glib_core_funcs;

typedef struct SpiceTimerGlib {
    SpiceTimer base;
    GMainContext *context;
    SpiceTimerFunc func;
    void *opaque;
    GSource *source;
} SpiceTimerGlib;

bool got_event_watch;
bool got_event_watch_read;
bool got_event_watch_write;
bool got_event_timeout;

static SpiceTimer* timer_add(const SpiceCoreInterfaceInternal *iface,
                             SpiceTimerFunc func, void *opaque)
{
    SpiceTimerGlib *timer = g_new0(SpiceTimerGlib, 1);

    timer->base.funcs = &glib_core_funcs;
    timer->context = iface->main_context;
    timer->func = func;
    timer->opaque = opaque;

    return &timer->base;
}

static gboolean timer_func(gpointer user_data)
{
    SpiceTimerGlib *timer = (SpiceTimerGlib*) user_data;

    got_event_timeout = true;
    timer->func(timer->opaque);
    /* timer might be free after func(), don't touch */

    return FALSE;
}

static void timer_cancel(SpiceTimer *timer_base)
{
    SpiceTimerGlib *timer = SPICE_UPCAST(SpiceTimerGlib, timer_base);
    if (timer->source) {
        g_source_destroy(timer->source);
        g_source_unref(timer->source);
        timer->source = NULL;
    }
}

static void timer_start(SpiceTimer *timer_base, uint32_t ms)
{
    timer_cancel(timer_base);

    SpiceTimerGlib *timer = SPICE_UPCAST(SpiceTimerGlib, timer_base);

    timer->source = g_timeout_source_new(ms);
    spice_assert(timer->source != NULL);

    g_source_set_callback(timer->source, timer_func, timer, NULL);

    g_source_attach(timer->source, timer->context);
}

static void timer_remove(SpiceTimer *timer_base)
{
    timer_cancel(timer_base);

    SpiceTimerGlib *timer = SPICE_UPCAST(SpiceTimerGlib, timer_base);
    spice_assert(timer->source == NULL);
    g_free(timer);
}

static GIOCondition spice_event_to_giocondition(int event_mask)
{
    int condition = 0;

    if (event_mask & SPICE_WATCH_EVENT_READ)
        condition |= G_IO_IN;
    if (event_mask & SPICE_WATCH_EVENT_WRITE)
        condition |= G_IO_OUT;

    return (GIOCondition) condition;
}

static int giocondition_to_spice_event(GIOCondition condition)
{
    int event = 0;

    if (condition & G_IO_IN)
        event |= SPICE_WATCH_EVENT_READ;
    if (condition & G_IO_OUT)
        event |= SPICE_WATCH_EVENT_WRITE;

    return event;
}

#ifdef _WIN32
typedef struct SpiceWatchGlib {
    SpiceWatch base;
    GMainContext *context;
    void *opaque;
    GSource *source;
    GIOChannel *channel;
    SpiceWatchFunc func;
} SpiceWatchGlib;

static gboolean watch_func(GIOChannel *source, GIOCondition condition,
                           gpointer data)
{
    SpiceWatchGlib *watch = (SpiceWatchGlib*) data;
    // this works also under Windows despite the name
    int fd = g_io_channel_unix_get_fd(source);

    got_event_watch = true;
    if (condition & G_IO_IN)
        got_event_watch_read = true;
    if (condition & G_IO_OUT)
        got_event_watch_write = true;
    watch->func(fd, giocondition_to_spice_event(condition), watch->opaque);

    return TRUE;
}

static void watch_update_mask(SpiceWatch *watch_base, int event_mask)
{
    SpiceWatchGlib *watch = SPICE_UPCAST(SpiceWatchGlib, watch_base);
    if (watch->source) {
        g_source_destroy(watch->source);
        g_source_unref(watch->source);
        watch->source = NULL;
    }

    if (!event_mask)
        return;

    watch->source = g_io_create_watch(watch->channel, spice_event_to_giocondition(event_mask));
    /* g_source_set_callback() documentation says:
     * "The exact type of func depends on the type of source; ie. you should
     *  not count on func being called with data as its first parameter."
     * In this case it is a GIOFunc. First cast to GIOFunc to make sure it is the right type.
     * The other casts silence the warning from gcc */
    g_source_set_callback(watch->source, (GSourceFunc)(void*)(GIOFunc)watch_func, watch, NULL);
    g_source_attach(watch->source, watch->context);
}

static SpiceWatch *watch_add(const SpiceCoreInterfaceInternal *iface,
                             int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    SpiceWatchGlib *watch;

    spice_return_val_if_fail(fd != -1, NULL);
    spice_return_val_if_fail(func != NULL, NULL);

    watch = g_new0(SpiceWatchGlib, 1);
    watch->base.funcs = &glib_core_funcs;
    watch->context = iface->main_context;
    watch->channel = g_io_channel_win32_new_socket(fd);
    watch->func = func;
    watch->opaque = opaque;

    watch_update_mask(&watch->base, event_mask);

    return &watch->base;
}

static void watch_remove(SpiceWatch *watch_base)
{
    SpiceWatchGlib *watch = SPICE_UPCAST(SpiceWatchGlib, watch_base);
    watch_update_mask(watch_base, 0);
    spice_assert(watch->source == NULL);

    g_io_channel_unref(watch->channel);
    g_free(watch);
}

#else

typedef struct SpiceWatchGlib {
    GSource source;
    SpiceWatch spice_base;
    gpointer unix_fd;
    int fd;
} SpiceWatchGlib;

static gboolean
spice_watch_check(GSource *source)
{
    SpiceWatchGlib *watch = SPICE_CONTAINEROF(source, SpiceWatchGlib, source);

    return g_source_query_unix_fd(&watch->source, watch->unix_fd) != 0;
}

static gboolean
spice_watch_dispatch(GSource     *source,
                     GSourceFunc  callback,
                     gpointer     user_data)
{
    SpiceWatchGlib *watch = SPICE_CONTAINEROF(source, SpiceWatchGlib, source);
    SpiceWatchFunc func = (SpiceWatchFunc)(void*) callback;
    GIOCondition condition = g_source_query_unix_fd(&watch->source, watch->unix_fd);

    func(watch->fd, giocondition_to_spice_event(condition), user_data);
    /* timer might be free after func(), don't touch */

    return G_SOURCE_CONTINUE;
}

static GSourceFuncs spice_watch_funcs = {
    .check = spice_watch_check,
    .dispatch = spice_watch_dispatch,
};

static void watch_update_mask(SpiceWatch *watch_base, int event_mask)
{
    SpiceWatchGlib *watch = SPICE_CONTAINEROF(watch_base, SpiceWatchGlib, spice_base);
    GIOCondition condition = spice_event_to_giocondition(event_mask);

    g_source_modify_unix_fd(&watch->source, watch->unix_fd, condition);
}

static SpiceWatch *watch_add(const SpiceCoreInterfaceInternal *iface,
                             int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    SPICE_VERIFY(SPICE_OFFSETOF(SpiceWatchGlib, source) == 0);
    SpiceWatchGlib *watch =
        (SpiceWatchGlib *) g_source_new(&spice_watch_funcs, sizeof(SpiceWatchGlib));

    spice_return_val_if_fail(fd != -1, NULL);
    spice_return_val_if_fail(func != NULL, NULL);

    watch->spice_base.funcs = &glib_core_funcs;
    watch->fd = fd;

    g_source_set_callback(&watch->source, (GSourceFunc)(void*) func, opaque, NULL);

    g_source_attach(&watch->source, iface->main_context);

    GIOCondition condition = spice_event_to_giocondition(event_mask);
    watch->unix_fd = g_source_add_unix_fd(&watch->source, watch->fd, condition);

    return &watch->spice_base;
}

static void watch_remove(SpiceWatch *watch_base)
{
    SpiceWatchGlib *watch = SPICE_CONTAINEROF(watch_base, SpiceWatchGlib, spice_base);

    g_source_remove_unix_fd(&watch->source, watch->unix_fd);
    g_source_destroy(&watch->source);
    g_source_unref(&watch->source);
}
#endif

static const SpiceCoreFuncs glib_core_funcs = {
    .timer_start = timer_start,
    .timer_cancel = timer_cancel,
    .timer_remove = timer_remove,

    .watch_update_mask = watch_update_mask,
    .watch_remove = watch_remove,
};

const SpiceCoreInterfaceInternal event_loop_core = {
    .timer_add = timer_add,
    .watch_add = watch_add,
};

/*
 * Adapter for SpiceCodeInterface
 */

static const SpiceCoreFuncs qemu_core_funcs;

typedef struct SpiceTimerQemu {
    SpiceTimer base;
    SpiceCoreInterface *core;
    SpiceTimer *qemu_timer;
} SpiceTimerQemu;

static SpiceTimer *adapter_timer_add(const SpiceCoreInterfaceInternal *iface, SpiceTimerFunc func, void *opaque)
{
    SpiceTimerQemu *timer = g_new0(SpiceTimerQemu, 1);

    timer->base.funcs = &qemu_core_funcs;
    timer->core = iface->public_interface;
    timer->qemu_timer = timer->core->timer_add(func, opaque);
    return &timer->base;
}

static void adapter_timer_start(SpiceTimer *timer_, uint32_t ms)
{
    SpiceTimerQemu *timer = SPICE_UPCAST(SpiceTimerQemu, timer_);
    timer->core->timer_start(timer->qemu_timer, ms);
}

static void adapter_timer_cancel(SpiceTimer *timer_)
{
    SpiceTimerQemu *timer = SPICE_UPCAST(SpiceTimerQemu, timer_);
    timer->core->timer_cancel(timer->qemu_timer);
}

static void adapter_timer_remove(SpiceTimer *timer_)
{
    SpiceTimerQemu *timer = SPICE_UPCAST(SpiceTimerQemu, timer_);
    timer->core->timer_remove(timer->qemu_timer);
    g_free(timer);
}

typedef struct SpiceWatchQemu {
    SpiceWatch base;
    SpiceCoreInterface *core;
    SpiceWatch *qemu_watch;
} SpiceWatchQemu;

static SpiceWatch *adapter_watch_add(const SpiceCoreInterfaceInternal *iface,
                                     int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    // note: Qemu API is fine having a SOCKET on Windows
    SpiceWatchQemu *watch = g_new0(SpiceWatchQemu, 1);

    watch->base.funcs = &qemu_core_funcs;
    watch->core = iface->public_interface;
    watch->qemu_watch = watch->core->watch_add(fd, event_mask, func, opaque);
    return &watch->base;
}

static void adapter_watch_update_mask(SpiceWatch *watch_, int event_mask)
{
    SpiceWatchQemu *watch = SPICE_UPCAST(SpiceWatchQemu, watch_);
    watch->core->watch_update_mask(watch->qemu_watch, event_mask);
}

static void adapter_watch_remove(SpiceWatch *watch_)
{
    SpiceWatchQemu *watch = SPICE_UPCAST(SpiceWatchQemu, watch_);
    watch->core->watch_remove(watch->qemu_watch);
    g_free(watch);
}

static void adapter_channel_event(const SpiceCoreInterfaceInternal *iface, int event, SpiceChannelEventInfo *info)
{
    if (iface->public_interface->base.minor_version >= 3 && iface->public_interface->channel_event != NULL)
        iface->public_interface->channel_event(event, info);
}

static const SpiceCoreFuncs qemu_core_funcs = {
    .timer_start = adapter_timer_start,
    .timer_cancel = adapter_timer_cancel,
    .timer_remove = adapter_timer_remove,

    .watch_update_mask = adapter_watch_update_mask,
    .watch_remove = adapter_watch_remove,
};

const SpiceCoreInterfaceInternal core_interface_adapter = {
    .timer_add = adapter_timer_add,
    .watch_add = adapter_watch_add,
    .channel_event = adapter_channel_event,
};