-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_event.h
More file actions
561 lines (514 loc) · 13.6 KB
/
pal_event.h
File metadata and controls
561 lines (514 loc) · 13.6 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/**
Copyright (C) 2025 Nicholas Agbo
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/**
* @defgroup pal_event Event
* Event PAL functionality such as queues, event drivers, and event callbacks.
*
* @{
*/
#ifndef _PAL_EVENT_H
#define _PAL_EVENT_H
#include "pal_core.h"
/**
* @struct PalEventDriver
* @brief Opaque handle to an event driver.
*
* @since 1.0
* @ingroup pal_event
*/
typedef struct PalEventDriver PalEventDriver;
/**
* @struct PalEvent
* @brief A single event.
*
* @since 1.0
* @ingroup pal_event
*/
typedef struct PalEvent PalEvent;
/**
* @typedef PalEventCallback
* @brief Function pointer type used for event callbacks.
*
* @param[in] userData Optional pointer to user data. Can be nullptr.
* @param[in] event The event.
*
* @since 1.0
* @ingroup pal_event
* @sa PalPushFn
*/
typedef void(PAL_CALL* PalEventCallback)(
void* userData,
const PalEvent* event);
/**
* @typedef PalPushFn
* @brief Function pointer type used for pushing events into event queues.
*
* @param[in] userData Optional pointer to user data. Can be nullptr.
* @param[in] event The event to push.
*
* @since 1.0
* @ingroup pal_event
* @sa PalEventCallback
*/
typedef void(PAL_CALL* PalPushFn)(
void* userData,
PalEvent* event);
/**
* @typedef PalPollFn
* @brief Function pointer type used for polling events from event queues.
*
* This function should return false if the event queue is empty.
* If the queue is not empty and the event was retrieved, this should return
* true.
*
* @param[in] userData Optional pointer to user data. Can be nullptr.
* @param[out] event Pointer to a PalEvent to recieve the event.
*
* @since 1.0
* @ingroup pal_event
* @sa PalPushFn
*/
typedef bool(PAL_CALL* PalPollFn)(
void* userData,
PalEvent* outEvent);
/**
* @enum PalEventType
* @brief Event types. This is not a bitmask enum.
*
* All event types follow the format `PAL_EVENT_**` for consistency and
* API use.
*
* @since 1.0
* @ingroup pal_event
*/
typedef enum {
/**
* PAL_EVENT_WINDOW_CLOSE
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_CLOSE,
/**
* PAL_EVENT_WINDOW_SIZE
*
* event.data : lower 32 bits = width, upper 32 bits = height
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackUint32()
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_SIZE,
/**
* PAL_EVENT_WINDOW_MOVE
*
* event.data : lower 32 bits = x, upper 32 bits = y
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackInt32()
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_MOVE,
/**
* PAL_EVENT_WINDOW_STATE
*
* event.data : state(minimized, maximized, restored).
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_STATE,
/**
* PAL_EVENT_WINDOW_FOCUS
*
* event.data : `true` for focus gained or `false` for focus lost.
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_FOCUS,
/**
* PAL_EVENT_WINDOW_VISIBILITY
*
* event.data : `true` for visible or `false` for hidden.
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_WINDOW_VISIBILITY,
/**
* @brief WM_ENTERSIZEMOVE (Windows Only).
*
* PAL_EVENT_WINDOW_MODAL_BEGIN
*
* event.data2 : window
*/
PAL_EVENT_WINDOW_MODAL_BEGIN,
/**
* @brief WM_EXITSIZEMOVE (Windows Only).
*
* PAL_EVENT_WINDOW_MODAL_END
*
* event.data2 : window
*/
PAL_EVENT_WINDOW_MODAL_END,
/**
* PAL_EVENT_MONITOR_DPI_CHANGED
*
* event.data2 : window
*/
PAL_EVENT_MONITOR_DPI_CHANGED,
/**
* @brief Monitor list changed
*
* PAL_EVENT_MONITOR_LIST_CHANGED
*
* event.data2 : window
*/
PAL_EVENT_MONITOR_LIST_CHANGED,
/**
* PAL_EVENT_KEYDOWN
*
* event.data : lower 32 bits = keycode, upper 32 bits = scancode
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackUint32()
* - palUnpackPointer()
*/
PAL_EVENT_KEYDOWN,
/**
* PAL_EVENT_KEYREPEAT
*
* event.data : lower 32 bits = keycode, upper 32 bits = scancode
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackUint32()
* - palUnpackPointer()
*/
PAL_EVENT_KEYREPEAT,
/**
* PAL_EVENT_KEYUP
*
* event.data : lower 32 bits = keycode, upper 32 bits = scancode
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackUint32()
* - palUnpackPointer()
*/
PAL_EVENT_KEYUP,
/**
* PAL_EVENT_MOUSE_BUTTONDOWN
*
* event.data : mouse button
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_MOUSE_BUTTONDOWN,
/**
* PAL_EVENT_MOUSE_BUTTONUP
*
* event.data : mouse button
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_MOUSE_BUTTONUP,
/**
* PAL_EVENT_MOUSE_MOVE
*
* event.data : lower 32 bits = x, upper 32 bits = y
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackInt32()
* - palUnpackPointer()
*/
PAL_EVENT_MOUSE_MOVE,
/**
* @brief Mouse movement delta.
*
* PAL_EVENT_MOUSE_DELTA
*
* event.data : lower 32 bits = dx, upper 32 bits = dy
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackInt32()
* - palUnpackPointer()
*/
PAL_EVENT_MOUSE_DELTA,
/**
* PAL_EVENT_MOUSE_WHEEL
*
* event.data : lower 32 bits = dx, upper 32 bits = dy
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackInt32()
* - palUnpackPointer()
*/
PAL_EVENT_MOUSE_WHEEL,
/**
* PAL_EVENT_USER
*
* event.userId : User event ID or type.
*
* Use inline helpers:
* - palPackInt32()
* - palPackUint32()
* - palPackPointer()
* - palUnpackInt32()
* - palUnpackUint32()
* - palUnpackPointer()
*/
PAL_EVENT_USER,
/**
* PAL_EVENT_USER
*
* event.data : codepoint
*
* event.data2 : window
*
* Use inline helpers:
* - palUnpackPointer()
*/
PAL_EVENT_KEYCHAR,
PAL_EVENT_MAX
} PalEventType;
/**
* @enum PalDispatchMode
* @brief Dispatch types for an event. This is not a bitmask enum.
*
* All dispatch modes follow the format `PAL_DISPATCH_**` for consistency
* and API use.
*
* @since 1.0
* @ingroup pal_event
*/
typedef enum {
PAL_DISPATCH_NONE, /**< No dispatch.*/
PAL_DISPATCH_CALLBACK, /**< Dispatch to event callback.*/
PAL_DISPATCH_POLL, /**< Dispatch to the event queue.*/
PAL_DISPATCH_MAX
} PalDispatchMode;
struct PalEvent {
PalEventType type;
Int64 data; /**< First data payload.*/
Int64 data2; /**< Second data payload.*/
Int64 userId; /**< You can have user events upto Int64 max.*/
};
/**
* @struct PalEventQueue
* @brief Custom event queue.
*
* Provides user-defined event queue push and poll functions.
*
* @since 1.0
* @ingroup pal_event
*/
typedef struct {
PalPushFn push;
PalPollFn poll;
void* userData; /**< Optional user-provided data. Can be nullptr.*/
} PalEventQueue;
/**
* @struct PalEventDriverCreateInfo
* @brief Creation parameters for an event driver.
*
* Uninitialized fields may result in undefined behavior.
*
* @since 1.0
* @ingroup pal_event
*/
typedef struct {
const PalAllocator* allocator; /**< Set to nullptr to use default.*/
PalEventQueue* queue; /**< Set to nullptr to use default.*/
PalEventCallback callback; /**< Can be nullptr.*/
void* userData; /**< Optional user-provided data. Can be nullptr.*/
} PalEventDriverCreateInfo;
/**
* @brief Create an event driver.
*
* The allocator field in the provided PalEventDriverCreateInfo struct will not
* be copied, therefore the pointer must remain valid until the event driver is
* destroyed. Destroy the event driver with palDestroyEventDriver() when no
* longer needed.
*
* @param[in] info Pointer to a PalEventDriverCreateInfo struct that specifies
* paramters. Must not be nullptr.
* @param[out] outEventDriver Pointer to a PalEventDriver to recieve the created
* event driver. Must not be nullptr.
*
* @return `PAL_RESULT_SUCCESS` on success or a result code on
* failure. Call palFormatResult() for more information.
*
* Thread safety: This function is thread safe if the provided allocator is
* thread safe and `outEventDriver` is thread local. The default allocator is
* thread safe.
*
* @since 1.0
* @ingroup pal_event
* @sa palDestroyEventDriver
*/
PAL_API PalResult PAL_CALL palCreateEventDriver(
const PalEventDriverCreateInfo* info,
PalEventDriver** outEventDriver);
/**
* @brief Destroy the provided event driver.
*
* If the provided event driver is invalid or nullptr, this function returns
* silently.
*
* @param[in] eventDriver Pointer to the event driver to destroy.
*
* Thread safety: This function is thread safe if the allocator used to create
* the event driver is thread safe and `eventDriver` is thread local.
*
* @since 1.0
* @ingroup pal_event
* @sa palCreateEventDriver
*/
PAL_API void PAL_CALL palDestroyEventDriver(PalEventDriver* eventDriver);
/**
* @brief Set the dispatch mode for an event type with the provided event
* driver.
*
* If the provided event driver is invalid or nullptr, this function returns
* silently.
*
* If the dispatch mode is `PAL_DISPATCH_POLL`, the event will be dispatched
* into the event drivers event queue. If the dispatch mode is
* `PAL_DISPATCH_CALLBACK` and the event driver has a valid callback function,
* the event will be dispatched to the callback function of the event driver
* otherwise the event will be discarded.
*
* @param[in] eventDriver Pointer to the event driver.
* @param[in] type Event type to set dispatch mode for.
* @param[in] mode Dispatch mode to use.
*
* Thread safety: This function is thread if multiple threads are not
* simultaneously setting dispatch mode on the same `eventDriver`.
*
* @since 1.0
* @ingroup pal_event
* @sa palGetEventDispatchMode
*/
PAL_API void PAL_CALL palSetEventDispatchMode(
PalEventDriver* eventDriver,
PalEventType type,
PalDispatchMode mode);
/**
* @brief Get the dispatch mode for an event type with the provided event
* driver.
*
* @param[in] eventDriver Pointer to the event driver.
* @param[in] type The event type.
*
* @return The dispatch mode on success or `PAL_DISPATCH_NONE` on failure.
*
* Thread safety: This function is thread if multiple threads are not
* simultaneously setting dispatch mode on the same `eventDriver`.
*
* @since 1.0
* @ingroup pal_event
* @sa palSetEventDispatchMode
*/
PAL_API PalDispatchMode PAL_CALL palGetEventDispatchMode(
PalEventDriver* eventDriver,
PalEventType type);
/**
* @brief Push an event into the queue or callback function of the provided
* event driver.
*
* If the provided event driver is invalid or nullptr, this function returns
* silently.
*
* If the dispatch mode for the event is `PAL_DISPATCH_POLL`, the event will be
* pushed to the event queue.
*
* If dispatch mode is `PAL_DISPATCH_CALLBACK` and the event driver has a valid
* event callback, the callback will be called otherwise the event will be
* discarded.
*
* @param[in] eventDriver Pointer to the event driver.
* @param[in] event Pointer to the event to push.
*
* Thread safety: This function is thread if the provided event queue is thread
* safe or every thread has its own `eventDriver`. The default event queue is
* not thread safe.
*
* @since 1.0
* @ingroup pal_event
* @sa palPollEvent
*/
PAL_API void PAL_CALL palPushEvent(
PalEventDriver* eventDriver,
PalEvent* event);
/**
* @brief Retrieve the next available event from the queue of the provided event
* driver.
*
* If the provided event driver is invalid or nullptr, this function returns
* silently.
*
* This function retrieves the next pending event from the queue of the
* provided event driver without blocking. If no events are available, it
* returns false.
*
* @param[in] eventDriver Pointer to the event driver.
* @param[out] outEvent Pointer to a PalEvent to recieve the event. Must be
* valid.
*
* Thread safety: This function is thread if the provided event queue is thread
* safe or every thread has its own `eventDriver`. The default event queue is
* not thread safe.
*
* @since 1.0
* @ingroup pal_event
* @sa palPushEvent
*/
PAL_API bool PAL_CALL palPollEvent(
PalEventDriver* eventDriver,
PalEvent* outEvent);
/** @} */ // end of pal_event group
#endif // _PAL_EVENT_H