-
Notifications
You must be signed in to change notification settings - Fork 460
/
events_keyboard.cpp
215 lines (199 loc) · 5.82 KB
/
events_keyboard.cpp
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
#include "main.h"
#include "platform.h"
#include <assert.h>
#include <ion/events.h>
#include <SDL.h>
#include <string.h>
#if EPSILON_SDL_SCREEN_ONLY
template<typename T, int N>
class Queue {
public:
Queue() : m_first(&m_elements[0]), m_last(&m_elements[0]) {}
int size() {
if (m_last >= m_first) {
return m_last - m_first;
} else {
return m_last - (m_first - N);
}
}
void enqueue(T element) {
if (size() > N) {
// Queue is full
return;
}
*m_last = element;
m_last = next(m_last);
}
T dequeue() {
if (size() <= 0) {
// Dequeueing an empty queue
return T();
}
T e = *m_first;
m_first = next(m_first);
return e;
}
private:
T * next(T * p) {
if (p >= m_elements + N) {
return m_elements;
} else {
return p + 1;
}
}
T * m_first;
T * m_last;
T m_elements[N];
};
static Queue<Ion::Events::Event, 1024> sEventQueue;
void IonSimulatorEventsPushEvent(int eventNumber) {
sEventQueue.enqueue(Ion::Events::Event(eventNumber));
}
#endif
namespace Ion {
namespace Events {
static Event eventFromSDLKeyboardEvent(SDL_KeyboardEvent event) {
/* If an event is detected, we want to remove the Shift modifier to mimic the
* device behaviour. If no event was detected, we restore the previous
* ShiftAlphaStatus. */
Ion::Events::ShiftAlphaStatus previousShiftAlphaStatus = Ion::Events::shiftAlphaStatus();
Ion::Events::removeShift();
if (event.keysym.mod & KMOD_CTRL) {
switch (event.keysym.sym) {
case SDLK_x:
return Cut;
case SDLK_c:
return Copy;
case SDLK_v:
return Paste;
}
}
if (event.keysym.mod & KMOD_ALT) {
if (event.keysym.mod & KMOD_SHIFT) {
switch (event.keysym.sym) {
case SDLK_s:
return Arcsine;
case SDLK_c:
return Arccosine;
case SDLK_t:
return Arctangent;
}
}
switch (event.keysym.sym) {
case SDLK_v:
return Var;
case SDLK_x:
return Exp;
case SDLK_n:
return Ln;
case SDLK_l:
return Log;
case SDLK_i:
return Imaginary;
case SDLK_EQUALS:
return Sto;
case SDLK_s:
return Sine;
case SDLK_c:
return Cosine;
case SDLK_t:
return Tangent;
case SDLK_p:
return Pi;
case SDLK_r:
return Sqrt;
case SDLK_2:
return Square;
case SDLK_e:
return EE;
case SDLK_a:
return Ans;
}
}
switch(event.keysym.sym) {
case SDLK_AC_BACK:
return Termination;
}
// No event was detected, restore the previous ShiftAlphaStatus.
Ion::Events::setShiftAlphaStatus(previousShiftAlphaStatus);
return None;
}
static constexpr Event sEventForASCIICharAbove32[95] = {
Space, Exclamation, DoubleQuotes, None, None, None, None, None,
LeftParenthesis, RightParenthesis, Multiplication, Plus, Comma, Minus, Dot, Division,
Zero, One, Two, Three, Four, Five, Six, Seven,
Eight, Nine, Colon, SemiColon, Lower, Equal, Greater, Question,
None, UpperA, UpperB, UpperC, UpperD, UpperE, UpperF, UpperG,
UpperH, UpperI, UpperJ, UpperK, UpperL, UpperM, UpperN, UpperO,
UpperP, UpperQ, UpperR, UpperS, UpperT, UpperU, UpperV, UpperW,
UpperX, UpperY, UpperZ, LeftBracket, None, RightBracket, Power, Underscore,
None, LowerA, LowerB, LowerC, LowerD, LowerE, LowerF, LowerG,
LowerH, LowerI, LowerJ, LowerK, LowerL, LowerM, LowerN, LowerO,
LowerP, LowerQ, LowerR, LowerS, LowerT, LowerU, LowerV, LowerW,
LowerX, LowerY, LowerZ, LeftBrace, None, RightBrace, None
};
static Event eventFromSDLTextInputEvent(SDL_TextInputEvent event) {
if (strlen(event.text) != 1) {
return None;
}
char character = event.text[0];
if (character >= 32 && character < 127) {
/* We remove the shift, otherwise it might stay activated when it shouldn't.
* For instance on a French keyboard, to input "1", we first press "Shift"
* (which activates the Shift modifier on the calculator), then we press
* "&", transformed by eventFromSDLTextInputEvent into the text "1". If we
* do not remove the Shift here, it would still be pressed afterwards. */
Ion::Events::removeShift();
return sEventForASCIICharAbove32[character-32];
}
return None;
}
Event getPlatformEvent() {
#if EPSILON_SDL_SCREEN_ONLY
if (sEventQueue.size() > 0) {
Event event = sEventQueue.dequeue();
return event;
}
#endif
SDL_Event event;
Event result = None;
while (SDL_PollEvent(&event)) {
// The while is important: it'll do a fast-pass over all useless SDL events
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
Ion::Simulator::Main::relayout();
}
}
if (event.type == SDL_QUIT) {
result = Termination;
break;
}
if (event.type == SDL_KEYDOWN) {
if (IonSimulatorSDLKeyDetectedByScan(event.key.keysym.scancode)) {
continue;
}
result = eventFromSDLKeyboardEvent(event.key);
break;
}
if (event.type == SDL_TEXTINPUT) {
result = eventFromSDLTextInputEvent(event.text);
break;
}
}
if (result != None) {
/* When events are not being processed - for instance when a Python script
* is being executed - SDL puts all ingoing events in a queue.
* When events processing goes back to normal, all the queued events are
* processed, which can result in weird behaviours (for instance, really
* fast navigation in the calculator, "instantanate" text input, ...).
* These behaviours are even more visible if the script contains an infinite
* loop.
* To prevent that, we flush all queued events after encountering a non-null
* event -> the first event from the queue will still be processed, but not
* the subsequent ones. */
SDL_FlushEvents(0, UINT32_MAX);
}
return result;
}
}
}