-
Notifications
You must be signed in to change notification settings - Fork 0
/
DGLEVENT.H
executable file
·154 lines (119 loc) · 4.4 KB
/
DGLEVENT.H
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
#ifndef LIBDGL_DGLEVENT_H
#define LIBDGL_DGLEVENT_H
#include "dglcmn.h"
#include "dglkbrd.h"
#include "dglmouse.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef uint8 EVENT_TYPE;
#define EVENT_TYPE_KEYBOARD 1
#define EVENT_TYPE_MOUSE_MOTION 2
#define EVENT_TYPE_MOUSE_BUTTON 3
typedef uint8 EVENT_ACTION;
#define EVENT_ACTION_PRESSED 1
#define EVENT_ACTION_RELEASED 2
#define EVENT_ACTION_HELD 3
typedef struct {
KEY key;
EVENT_ACTION action;
uint16 mod;
} INPUTEVENT_KEYBOARD;
typedef struct {
int x;
int y;
int x_delta;
int y_delta;
MOUSE_BUTTON buttons;
} INPUTEVENT_MOUSE_MOTION;
typedef struct {
int x;
int y;
MOUSE_BUTTON button;
EVENT_ACTION action;
} INPUTEVENT_MOUSE_BUTTON;
typedef struct {
EVENT_TYPE type;
union {
INPUTEVENT_KEYBOARD keyboard;
INPUTEVENT_MOUSE_MOTION mouse_motion;
INPUTEVENT_MOUSE_BUTTON mouse_button;
};
} INPUTEVENT;
extern volatile bool _events_enabled;
#define EVENTS_BUFFER_SIZE 32
extern volatile INPUTEVENT _events_buffer[EVENTS_BUFFER_SIZE];
extern volatile int _events_buffer_start;
extern volatile int _events_buffer_end;
bool events_init(void);
bool events_shutdown(void);
static bool events_is_initialized(void);
static bool events_is_empty(void);
bool events_poll(volatile INPUTEVENT **event);
bool events_peek(volatile INPUTEVENT **event);
void events_clear(void);
static bool events_key_pressed(INPUTEVENT *event, KEY key);
static bool events_key_released(INPUTEVENT *event, KEY key);
static bool events_key_held(INPUTEVENT *event, KEY key);
static bool events_mouse_pressed(INPUTEVENT *event, MOUSE_BUTTON button);
static bool events_mouse_released(INPUTEVENT *event, MOUSE_BUTTON button);
static bool events_mouse_held(INPUTEVENT *event, MOUSE_BUTTON button);
static void _events_push(INPUTEVENT **out_event);
// ---------------------------------------------------------------------------
static bool events_is_initialized(void) {
return _events_enabled;
}
static bool events_is_empty(void) {
return (_events_buffer_start == _events_buffer_end);
}
static bool events_key_pressed(INPUTEVENT *event, KEY key) {
return event->type == EVENT_TYPE_KEYBOARD &&
event->keyboard.action == EVENT_ACTION_PRESSED &&
event->keyboard.key == key;
}
static bool events_key_released(INPUTEVENT *event, KEY key) {
return event->type == EVENT_TYPE_KEYBOARD &&
event->keyboard.action == EVENT_ACTION_RELEASED &&
event->keyboard.key == key;
}
static bool events_key_held(INPUTEVENT *event, KEY key) {
return event->type == EVENT_TYPE_KEYBOARD &&
event->keyboard.action == EVENT_ACTION_HELD &&
event->keyboard.key == key;
}
static bool events_mouse_pressed(INPUTEVENT *event, MOUSE_BUTTON button) {
return event->type == EVENT_TYPE_MOUSE_BUTTON &&
event->mouse_button.action == EVENT_ACTION_PRESSED &&
event->mouse_button.button == button;
}
static bool events_mouse_released(INPUTEVENT *event, MOUSE_BUTTON button) {
return event->type == EVENT_TYPE_MOUSE_BUTTON &&
event->mouse_button.action == EVENT_ACTION_RELEASED &&
event->mouse_button.button == button;
}
static bool events_mouse_held(INPUTEVENT *event, MOUSE_BUTTON button) {
return event->type == EVENT_TYPE_MOUSE_BUTTON &&
event->mouse_button.action == EVENT_ACTION_HELD &&
event->mouse_button.button == button;
}
// only intended to be called from input device interrupt handler (the
// usage is a little weird as a result)
static void _events_push(INPUTEVENT **out_event) {
*out_event = (INPUTEVENT*)&_events_buffer[_events_buffer_end];
++_events_buffer_end;
// wrap around
if (_events_buffer_end >= EVENTS_BUFFER_SIZE)
_events_buffer_end = 0;
// is the events buffer full? (if the end meets up to the start, yes)
if (_events_buffer_end == _events_buffer_start) {
// move the start up. this ensures start always points to the oldest
// event in the buffer
++_events_buffer_start;
if (_events_buffer_start >= EVENTS_BUFFER_SIZE)
_events_buffer_start = 0;
}
}
#ifdef __cplusplus
}
#endif
#endif