summaryrefslogtreecommitdiff
path: root/src/spiceqxl_main_loop.c
blob: e57fb91c73bccba48ba8e4bd8a81e686a62a903b (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
/*
 * Copyright 2011 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/time.h>

#include <spice.h>
#include "spiceqxl_main_loop.h"

static int spiceqxl_main_loop_debug = 0;

#define DPRINTF(x, format, ...) { \
    if (x <= spiceqxl_main_loop_debug) { \
        printf("%s: " format "\n" , __FUNCTION__, ## __VA_ARGS__); \
    } \
}

/* From ring.h */
typedef struct Ring RingItem;
typedef struct Ring {
    RingItem *prev;
    RingItem *next;
} Ring;

static inline void ring_init(Ring *ring)
{
    ring->next = ring->prev = ring;
}

static inline void ring_item_init(RingItem *item)
{
    item->next = item->prev = NULL;
}

static inline int ring_item_is_linked(RingItem *item)
{
    return !!item->next;
}

static inline int ring_is_empty(Ring *ring)
{
    assert(ring->next != NULL && ring->prev != NULL);
    return ring == ring->next;
}

static inline void ring_add(Ring *ring, RingItem *item)
{
    assert(ring->next != NULL && ring->prev != NULL);
    assert(item->next == NULL && item->prev == NULL);

    item->next = ring->next;
    item->prev = ring;
    ring->next = item->next->prev = item;
}

static inline void __ring_remove(RingItem *item)
{
    item->next->prev = item->prev;
    item->prev->next = item->next;
    item->prev = item->next = 0;
}

static inline void ring_remove(RingItem *item)
{
    assert(item->next != NULL && item->prev != NULL);
    assert(item->next != item);

    __ring_remove(item);
}

static inline RingItem *ring_get_head(Ring *ring)
{
    RingItem *ret;

    assert(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->next;
    return ret;
}

static inline RingItem *ring_get_tail(Ring *ring)
{
    RingItem *ret;

    assert(ring->next != NULL && ring->prev != NULL);

    if (ring_is_empty(ring)) {
        return NULL;
    }
    ret = ring->prev;
    return ret;
}

static inline RingItem *ring_next(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    assert(ring->next != NULL && ring->prev != NULL);
    assert(pos);
    assert(pos->next != NULL && pos->prev != NULL);
    ret = pos->next;
    return (ret == ring) ? NULL : ret;
}

static inline RingItem *ring_prev(Ring *ring, RingItem *pos)
{
    RingItem *ret;

    assert(ring->next != NULL && ring->prev != NULL);
    assert(pos);
    assert(pos->next != NULL && pos->prev != NULL);
    ret = pos->prev;
    return (ret == ring) ? NULL : ret;
}

#define RING_FOREACH_SAFE(var, next, ring)                    \
    for ((var) = ring_get_head(ring),                         \
         (next) = (var) ? ring_next(ring, (var)) : NULL;      \
            (var);                                            \
            (var) = (next),                                   \
            (next) = (var) ? ring_next(ring, (var)) : NULL)

/**/

#define NOT_IMPLEMENTED printf("%s not implemented\n", __func__);

static SpiceCoreInterface core;

typedef struct SpiceTimer {
    OsTimerPtr xorg_timer;
    SpiceTimerFunc func;
    void *opaque; // also stored in xorg_timer, but needed for timer_start
} Timer;

static CARD32 xorg_timer_callback(
    OsTimerPtr xorg_timer,
    CARD32 time,
    pointer arg)
{
    SpiceTimer *timer = (SpiceTimer*)arg;

    timer->func(timer->opaque);
    return 0; // if non zero xorg does a TimerSet, we don't want that.
}

static SpiceTimer* timer_add(SpiceTimerFunc func, void *opaque)
{
    SpiceTimer *timer = calloc(sizeof(SpiceTimer), 1);

    timer->xorg_timer = TimerSet(NULL, 0, 1e9 /* TODO: infinity? */, xorg_timer_callback, timer);
    timer->func = func;
    timer->opaque = opaque;
    return timer;
}

static void timer_start(SpiceTimer *timer, uint32_t ms)
{
    TimerSet(timer->xorg_timer, 0 /* flags */, ms, xorg_timer_callback, timer);
}

static void timer_cancel(SpiceTimer *timer)
{
    TimerCancel(timer->xorg_timer);
}

static void timer_remove(SpiceTimer *timer)
{
    TimerFree(timer->xorg_timer);
    free(timer);
}

struct SpiceWatch {
    RingItem link;
    int fd;
    int event_mask;
    SpiceWatchFunc func;
    void *opaque;
    int remove;
};

Ring watches;

int watch_count = 0;

static SpiceWatch *watch_add(int fd, int event_mask, SpiceWatchFunc func, void *opaque)
{
    SpiceWatch *watch = malloc(sizeof(SpiceWatch));

    DPRINTF(0, "adding %p, fd=%d at %d", watch,
        fd, watch_count);
    watch->fd = fd;
    watch->event_mask = event_mask;
    watch->func = func;
    watch->opaque = opaque;
    watch->remove = FALSE;
    ring_item_init(&watch->link);
    ring_add(&watches, &watch->link);
    watch_count++;
    return watch;
}

static void watch_update_mask(SpiceWatch *watch, int event_mask)
{
    DPRINTF(0, "fd %d to %d", watch->fd, event_mask);
    watch->event_mask = event_mask;
}

static void watch_remove(SpiceWatch *watch)
{
    DPRINTF(0, "remove %p (fd %d)", watch, watch->fd);
    watch->remove = TRUE;
    watch_count--;
}

static void channel_event(int event, SpiceChannelEventInfo *info)
{
    NOT_IMPLEMENTED
}

static int set_watch_fds(fd_set *rfds, fd_set *wfds)
{
    SpiceWatch *watch;
    RingItem *link;
    RingItem *next;
    int max_fd = -1;

    RING_FOREACH_SAFE(link, next, &watches) {
        watch = (SpiceWatch*)link;
        if (watch->event_mask & SPICE_WATCH_EVENT_READ) {
            FD_SET(watch->fd, rfds);
            max_fd = watch->fd > max_fd ? watch->fd : max_fd;
        }
        if (watch->event_mask & SPICE_WATCH_EVENT_WRITE) {
            FD_SET(watch->fd, wfds);
            max_fd = watch->fd > max_fd ? watch->fd : max_fd;
        }
    }
    return max_fd;
}

/*
 * called just before the X server goes into select()
 * readmask is just an fdset on linux, but something totally different on windows (etc).
 * DIX has a comment about it using a special type to hide this (so we break that here)
 */
static void xspice_block_handler(pointer data, OSTimePtr timeout, pointer readmask)
{
    /* set all our fd's */
    set_watch_fds((fd_set*)readmask, (fd_set*)readmask);
}

/*
 * xserver only calles wakeup_handler with the read fd_set, so we
 * must either patch it or do a polling select ourselves, this is the
 * later approach. Since we are already doing a polling select, we
 * already select on all (we could avoid selecting on the read since
 * that *is* actually taken care of by the wakeup handler).
 */
static void select_and_check_watches(void)
{
    fd_set rfds, wfds;
    int max_fd = -1;
    SpiceWatch *watch;
    RingItem *link;
    RingItem *next;
    struct timeval timeout;
    int retval;

    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    max_fd = set_watch_fds(&rfds, &wfds);
    watch = (SpiceWatch*)watches.next;
    timeout.tv_sec = timeout.tv_usec = 0;
    retval = select(max_fd + 1, &rfds, &wfds, NULL, &timeout);
    if (retval) {
        RING_FOREACH_SAFE(link, next, &watches) {
            watch = (SpiceWatch*)link;
            if ((watch->event_mask & SPICE_WATCH_EVENT_READ)
                 && FD_ISSET(watch->fd, &rfds)) {
                watch->func(watch->fd, SPICE_WATCH_EVENT_READ, watch->opaque);
            }
            if (!watch->remove && (watch->event_mask & SPICE_WATCH_EVENT_WRITE)
                 && FD_ISSET(watch->fd, &wfds)) {
                watch->func(watch->fd, SPICE_WATCH_EVENT_WRITE, watch->opaque);
            }
            if (watch->remove) {
                ring_remove(&watch->link);
                free(watch);
            }
        }
    }
}

static int no_write_watches(Ring *w)
{
    SpiceWatch *watch;
    RingItem *link;
    RingItem *next;

    RING_FOREACH_SAFE(link, next, w) {
        watch = (SpiceWatch*)link;
        if (!watch->remove && (watch->event_mask & SPICE_WATCH_EVENT_WRITE))
            return 0;
    }

    return 1;
}

static void xspice_wakeup_handler(pointer data, int nfds, pointer readmask)
{
    if (!nfds && no_write_watches(&watches)) {
        return;
    }
    select_and_check_watches();
}

SpiceCoreInterface *basic_event_loop_init(void)
{
    ring_init(&watches);
    bzero(&core, sizeof(core));
    core.base.major_version = SPICE_INTERFACE_CORE_MAJOR;
    core.base.minor_version = SPICE_INTERFACE_CORE_MINOR; // anything less then 3 and channel_event isn't called
    core.timer_add = timer_add;
    core.timer_start = timer_start;
    core.timer_cancel = timer_cancel;
    core.timer_remove = timer_remove;
    core.watch_add = watch_add;
    core.watch_update_mask = watch_update_mask;
    core.watch_remove = watch_remove;
    core.channel_event = channel_event;
    RegisterBlockAndWakeupHandlers(xspice_block_handler, xspice_wakeup_handler, 0);
    return &core;
}