summaryrefslogtreecommitdiff
path: root/src/window.c
blob: f85f4fc777e2bb9406c24e717f2044eb9d078032 (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
#define AL_LOG_SECTION "window"
#include <al/log.h>

#include "window.h"
#include "keys.h"

static bool nop_pointer_pos_callback(void *userdata, f64 x, f64 y)
{
    (void)userdata;
    (void)x;
    (void)y;
    return false;
}

static bool nop_touch_callback(void *userdata, s32 index, u8 phase, f64 x, f64 y)
{
    (void)userdata;
    (void)index;
    (void)phase;
    (void)x;
    (void)y;
    return false;
}

static bool nop_scroll_callback(void *userdata, f64 y)
{
    (void)userdata;
    (void)y;
    return false;
}

static bool nop_mouse_button_callback(void *userdata, u8 state, u8 button)
{
    (void)userdata;
    (void)state;
    (void)button;
    return false;
}

static bool nop_key_callback(void *userdata, u8 state, u16 button)
{
    (void)userdata;
    (void)state;
    (void)button;
    return false;
}

static bool nop_key_immediate_callback(void *userdata, u8 state, u16 button)
{
    (void)userdata;
    (void)state;
    (void)button;
    return false;
}

static void window_default_close(struct stl_window *window)
{
    window->closed = true;
}

void stl_window_init(struct stl_window *window)
{
    window->pointer_pos_callback = nop_pointer_pos_callback;
    window->touch_callback = nop_touch_callback;
    window->scroll_callback = nop_scroll_callback;
    window->mouse_button_callback = nop_mouse_button_callback;
    window->key_callback = nop_key_callback;
    window->key_immediate_callback = nop_key_immediate_callback;
    window->close = window_default_close;
#ifdef STELA_EVENT_BUFFER
    al_ring_buffer_init(&window->events, window->data, STELA_EVENTS_SIZE);
#endif
    window->closed = false;
    window->held_modifiers = 0;
#ifdef STELA_PAUSE
    window->pending_refresh = true;
#endif
}

void stl_window_created(struct stl_window *window, u32 width, u32 height, u32 flags)
{
    window->width = width;
    window->height = height;
    window->flags = flags;
}

void stl_window_send_render_event(struct stl_window *window)
{
    if (window->render_callback) {
        window->render_callback(window->userdata);
    }
}

#ifdef STELA_EVENT_BUFFER
#define GET_EVENT_CHUNK(op) \
    ptrdiff_t size; \
    u8 *ptr = al_ring_buffer_write_chunk(&window->events, &size); \
    if (size < STELA_EVENT_SIZE) { \
        log_warn("Buffer full, discarding event."); \
        return; \
    } \
    ptr[0] = op
#endif

#define APPEND_EVENT_CHUNK() \
    al_ring_buffer_append(&window->events, ptr, STELA_EVENT_SIZE)

void stl_window_send_resize_event(struct stl_window *window, u32 width, u32 height)
{
    al_assert(width == window->width && height == window->height);
#ifdef STELA_EVENT_BUFFER
    window->pending_resize = true;
#else
    window->resize_internal(window, width, height);
    if (window->resize_callback) {
        window->resize_callback(window->userdata, width, height);
    }
#ifdef STELA_PAUSE
    window->pending_refresh = true;
#endif
#endif
}

void stl_window_send_pointer_pos_event(struct stl_window *window, f64 x, f64 y)
{
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_POINTER_POS);
    ((f64 *)(&ptr[1]))[0] = x;
    ((f64 *)(&ptr[9]))[0] = y;
    APPEND_EVENT_CHUNK();
#else
#ifdef STELA_PAUSE
    window->pending_refresh |=
#endif
        window->pointer_pos_callback(window->userdata, x, y);
#endif
}

void stl_window_send_touch_event(struct stl_window *window, s32 index, u8 phase, f64 x, f64 y)
{
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_TOUCH);
    ((s32 *)(&ptr[1]))[0] = index;
    ptr[5] = phase;
    ((f64 *)(&ptr[6]))[0] = x;
    ((f64 *)(&ptr[14]))[0] = y;
    APPEND_EVENT_CHUNK();
#else
#ifdef STELA_PAUSE
    window->pending_refresh |=
#endif
        window->touch_callback(window->userdata, index, phase, x, y);
#endif
}

void stl_window_send_scroll_event(struct stl_window *window, f64 y)
{
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_SCROLL);
    ((f64 *)(&ptr[1]))[0] = y;
    APPEND_EVENT_CHUNK();
#else
#ifdef STELA_PAUSE
    window->pending_refresh |=
#endif
        window->scroll_callback(window->userdata, y);
#endif
}

void stl_window_send_mouse_button_event(struct stl_window *window, u8 state, u8 button)
{
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_MOUSE_BUTTON);
    ptr[1] = state;
    ptr[2] = button;
    APPEND_EVENT_CHUNK();
#else
    window->held_mouse_buttons = (window->held_mouse_buttons & ~button) | (button * (state == STELA_BUTTON_PRESSED));
#ifdef STELA_PAUSE
    window->pending_refresh |=
#endif
        window->mouse_button_callback(window->userdata, state, button);
#endif
}

void stl_window_drop_mouse_buttons(struct stl_window *window)
{
    u8 *buttons = stl_all_mouse_buttons;
    while (window->held_mouse_buttons) {
        u8 button = *buttons++;
        if (window->held_mouse_buttons & button) {
            stl_window_send_mouse_button_event(window, STELA_BUTTON_RELEASED, button);
        }
    }
    al_assert(!window->held_mouse_buttons);
}

#ifdef STELA_WINDOW_WIN32
// It's possible for a window to behave in a way that disallows
// consistancy between BUTTON_PRESSED and BUTTON_RELEASED events.
#define STELA_CORRECT_MODIFIER_INCONSISTENCY
#endif

void stl_window_send_key_event(struct stl_window *window, u8 state, u16 button)
{
    u32 mod = stl_key_to_mod(button);
#ifdef STELA_CORRECT_MODIFIER_INCONSISTENCY
    if (state == STELA_BUTTON_PRESSED) {
        if (window->held_modifiers & mod) {
            window->held_modifiers &= ~mod;
            window->key_callback(window->userdata, STELA_BUTTON_RELEASED, button);
        }
    } else {
        if (!(window->held_modifiers & mod)) return;
    }
#endif
    window->held_modifiers = (window->held_modifiers & ~mod) | (mod * (state == STELA_BUTTON_PRESSED));
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_KEY);
    ptr[1] = state;
    ((u16 *)(&ptr[2]))[0] = button;
    APPEND_EVENT_CHUNK();
#else
#ifdef STELA_PAUSE
    window->pending_refresh |=
#endif
        window->key_callback(window->userdata, state, button);
#endif
}

bool stl_window_send_key_immediate_event(struct stl_window *window, u8 state, u16 button)
{
    // Don't send modifiers on key immediate.
    return window->key_immediate_callback(window->userdata, state, button * (stl_key_to_mod(button) == STELA_MOD_NONE));
}

void stl_window_drop_modifiers(struct stl_window *window)
{
    u32 *mods = stl_all_modifiers;
    while (window->held_modifiers) {
        u32 mod = *mods++;
        if (window->held_modifiers & mod) {
            stl_window_send_key_event(window, STELA_BUTTON_RELEASED, stl_mod_to_key(mod));
        }
    }
    al_assert(!window->held_modifiers);
}

void stl_window_send_should_close_event(struct stl_window *window)
{
#ifdef STELA_EVENT_BUFFER
    GET_EVENT_CHUNK(STELA_EVENT_SHOULD_CLOSE);
    APPEND_EVENT_CHUNK();
#else
    if (window->should_close_callback) {
        window->should_close_callback(window->userdata);
    }
#endif
}

#ifdef STELA_EVENT_BUFFER
bool stl_window_read_events(struct stl_window *window)
{
    u8 op = STELA_EVENT_NONE;

    if (window->pending_resize) {
        op = STELA_EVENT_RESIZE;
        window->resize_internal(window, window->width, window->height);
        if (window->resize_callback) {
            window->resize_callback(window->userdata, window->width, window->height);
        }
#ifdef STELA_PAUSE
        window->pending_refresh = true;
#endif
        window->pending_resize = false;
    }

    ptrdiff_t size;
    u8 *ptr = al_ring_buffer_read_chunk(&window->events, &size);
    u8 *bc = ptr;
    while (bc - ptr < size) {
        op = *bc;
        switch (op) {
        case STELA_EVENT_POINTER_POS: {
            f64 x = ((f64 *)(&bc[1]))[0];
            f64 y = ((f64 *)(&bc[9]))[0];
#ifdef STELA_PAUSE
            window->pending_refresh |=
#endif
                window->pointer_pos_callback(window->userdata, x, y);
            break;
        }
        case STELA_EVENT_TOUCH: {
            s32 index = ((s32 *)(&bc[1]))[0];
            u8 phase = bc[5];
            f64 x = ((f64 *)(&bc[6]))[0];
            f64 y = ((f64 *)(&bc[14]))[0];
#ifdef STELA_PAUSE
            window->pending_refresh |=
#endif
                window->touch_callback(window->userdata, index, phase, x, y);
            break;
        }
        case STELA_EVENT_SCROLL: {
            f64 y = ((f64 *)(&bc[1]))[0];
#ifdef STELA_PAUSE
            window->pending_refresh |=
#endif
                window->scroll_callback(window->userdata, y);
            break;
        }
        case STELA_EVENT_MOUSE_BUTTON: {
            u8 state = bc[1];
            u8 button = bc[2];
#ifdef STELA_PAUSE
            window->pending_refresh |=
#endif
                window->mouse_button_callback(window->userdata, state, button);
            break;
        }
        case STELA_EVENT_KEY: {
            u8 state = bc[1];
            u16 button = ((u16 *)(&bc[2]))[0];
#ifdef STELA_PAUSE
            window->pending_refresh |=
#endif
                window->key_callback(window->userdata, state, button);
            break;
        }
        case STELA_EVENT_SHOULD_CLOSE: {
            if (window->should_close_callback) {
                window->should_close_callback(window->userdata);
            }
            break;
        }
        default:
            break;
        }
        bc += STELA_EVENT_SIZE;
    }
    al_assert(bc - ptr == size);
    al_ring_buffer_consume(&window->events, ptr, size);

    return op != STELA_EVENT_NONE;
}
#endif