README: mention SDL2 backend
[rofl0r-concol.git] / sdlconsole_keyboard.c
blobfe0eca3c0e6906e735f70d636bcff008084f4fbb
1 #include "sdlconsole.h"
2 #include "console_keys.h"
3 #if USE_SDL2 +0 == 0
4 #include <SDL/SDL.h>
5 #else
6 #include <SDL2/SDL.h>
7 #endif
9 #if SDL_MAJOR_VERSION == 2
10 #define SDL_keysym SDL_Keysym
11 #define SDLK_KP0 SDLK_KP_0
12 #define SDLK_KP1 SDLK_KP_1
13 #define SDLK_KP2 SDLK_KP_2
14 #define SDLK_KP3 SDLK_KP_3
15 #define SDLK_KP4 SDLK_KP_4
16 #define SDLK_KP5 SDLK_KP_5
17 #define SDLK_KP6 SDLK_KP_6
18 #define SDLK_KP7 SDLK_KP_7
19 #define SDLK_KP8 SDLK_KP_8
20 #define SDLK_KP9 SDLK_KP_9
21 #define KMOD_LMETA KMOD_LGUI
22 #define KMOD_RMETA KMOD_RGUI
23 #endif
26 #ifdef IN_KDEVELOP_PARSER
27 #define INCLUDED_FROM_SDLCONSOLE
28 #endif
30 #ifdef INCLUDED_FROM_SDLCONSOLE
32 static int sdlkey_to_ascii(Console* self, struct SDL_keysym* key) {
33 SDLConsole *c = &self->backend.sdl;
34 int shift;
36 if( (key->mod & KMOD_LSHIFT) || (key->mod & KMOD_RSHIFT))
37 shift = 1;
38 else if((key->mod & KMOD_LALT) || (key->mod & KMOD_RALT))
39 shift = -1; // ALT
40 else if((key->mod & KMOD_LCTRL) || (key->mod & KMOD_RCTRL))
41 shift = -2; // CTRL
42 else if(key->mod & KMOD_MODE)
43 shift = -3; // ALT GR
44 else
45 shift = 0;
47 // printf("shift: %d, mod %d, sym %d, name %s\n", shift, key->mod, key->sym, SDL_GetKeyName(key->sym));
48 switch(shift) {
49 // alt
50 case -1:
51 #if 0
52 if(key->sym == SDLK_RETURN)
53 console_toggle_fullscreen(self);
54 #endif
55 break;
56 //ctrl
57 case -2:
58 if(c->paintmode)
59 switch(key->sym) {
60 case SDLK_q:
61 return 201;
62 case SDLK_a:
63 return 204;
64 case SDLK_y:
65 return 200;
66 case SDLK_x:
67 return 205;
68 case SDLK_s:
69 return 206;
70 case SDLK_w:
71 return 203;
72 case SDLK_c:
73 return 188;
74 case SDLK_d:
75 return 185;
76 case SDLK_e:
77 return 187;
79 case SDLK_b:
80 return 176;
81 case SDLK_n:
82 return 177;
83 case SDLK_m:
84 return 178;
86 case SDLK_SPACE:
87 return 219;
89 default:
90 return key->sym;
91 } else
92 return key->sym;
94 break;
95 //alt gr
96 case -3:
97 printf("alt gr pressed!\n");
98 switch(key->sym) {
99 #if SDL_MAJOR_VERSION == 1
100 case SDLK_q:
101 return '@';
102 case SDLK_7:
103 return '{';
104 case SDLK_8:
105 return '[';
106 case SDLK_9:
107 return ']';
108 case SDLK_0:
109 return '}';
111 case SDLK_WORLD_63:
112 return '\\';
113 case SDLK_PLUS:
114 return '~';
115 case SDLK_LESS:
116 return '|';
117 #endif
118 default:
119 return key->sym;
122 case 1:
123 switch(key->sym) {
124 case SDLK_a:
125 return 'A';
126 case SDLK_b:
127 return 'B';
128 case SDLK_c:
129 return 'C';
130 case SDLK_d:
131 return 'D';
132 case SDLK_e:
133 return 'E';
134 case SDLK_f:
135 return 'F';
136 case SDLK_g:
137 return 'G';
138 case SDLK_h:
139 return 'H';
140 case SDLK_i:
141 return 'I';
142 case SDLK_j:
143 return 'J';
144 case SDLK_k:
145 return 'K';
146 case SDLK_l:
147 return 'L';
148 case SDLK_m:
149 return 'M';
150 case SDLK_n:
151 return 'N';
152 case SDLK_o:
153 return 'O';
154 case SDLK_p:
155 return 'P';
156 case SDLK_q:
157 return 'Q';
158 case SDLK_r:
159 return 'R';
160 case SDLK_s:
161 return 'S';
162 case SDLK_t:
163 return 'T';
164 case SDLK_u:
165 return 'U';
166 case SDLK_v:
167 return 'V';
168 case SDLK_w:
169 return 'W';
170 case SDLK_x:
171 return 'X';
172 case SDLK_y:
173 return 'Y';
174 case SDLK_z:
175 return 'Z';
176 #if SDL_MAJOR_VERSION == 1
177 case SDLK_1:
178 return '!';
179 case SDLK_2:
180 return '"';
181 case SDLK_3:
182 return '-';
183 case SDLK_4:
184 return '$';
185 case SDLK_5:
186 return '%';
187 case SDLK_6:
188 return '&';
189 case SDLK_7:
190 return '/';
191 case SDLK_8:
192 return '(';
193 case SDLK_9:
194 return ')';
195 case SDLK_0:
196 return '=';
198 case SDLK_HASH:
199 return '\'';
201 case SDLK_PLUS:
202 return '*';
204 case SDLK_LESS:
205 return '>';
207 case SDLK_WORLD_63:
208 return '?';
209 case SDLK_PERIOD:
210 return ':';
211 case SDLK_COMMA:
212 return ';';
213 case SDLK_MINUS:
214 return '_';
215 #endif
216 default:
217 return key->sym;
219 default:
220 switch(key->sym) {
221 case SDLK_DELETE:
222 return CK_DEL;
223 case SDLK_BACKSPACE:
224 return CK_BACKSPACE;
225 case SDLK_RETURN:
226 return CK_RETURN;
227 case SDLK_a:
228 return 'a';
229 case SDLK_b:
230 return 'b';
231 case SDLK_c:
232 return 'c';
233 case SDLK_d:
234 return 'd';
235 case SDLK_e:
236 return 'e';
237 case SDLK_f:
238 return 'f';
239 case SDLK_g:
240 return 'g';
241 case SDLK_h:
242 return 'h';
243 case SDLK_i:
244 return 'i';
245 case SDLK_j:
246 return 'j';
247 case SDLK_k:
248 return 'k';
249 case SDLK_l:
250 return 'l';
251 case SDLK_m:
252 return 'm';
253 case SDLK_n:
254 return 'n';
255 case SDLK_o:
256 return 'o';
257 case SDLK_p:
258 return 'p';
259 case SDLK_q:
260 return 'q';
261 case SDLK_r:
262 return 'r';
263 case SDLK_s:
264 return 's';
265 case SDLK_t:
266 return 't';
267 case SDLK_u:
268 return 'u';
269 case SDLK_v:
270 return 'v';
271 case SDLK_w:
272 return 'w';
273 case SDLK_x:
274 return 'x';
275 case SDLK_y:
276 return 'y';
277 case SDLK_z:
278 return 'z';
279 default:
280 return key->sym;
283 return 0;
286 #include "colors.h"
287 static void print_all_fonts(Console* c) {
288 size_t i;
289 int x, y, w, h;
290 console_getbounds(c, &w, &h);
291 console_goto(c, 0, 0);
292 console_setcolors(c, RGB3(WHITE), RGB3(BLACK));
293 console_printf(c, "%.3d %c ", 0, ' ');
294 for(i = 1; i < 256; i++) {
295 console_getcursor(c, &x, &y);
296 if(x + 6 > w)
297 console_linebreak(c);
298 console_printf(c, "%.3d %c ", (int) i, (int) i);
300 console_draw(c);
303 enum event_actions {
304 ea_none = 0,
305 ea_toggle_fullscreen,
306 ea_resized,
307 ea_redraw,
311 #if 0
312 static
313 #endif
314 int sdlconsole_translate_event(Console* self, SDL_Event* ev, int *action) {
315 SDLConsole* c = &self->backend.sdl;
316 int keymods;
317 SDL_Event event = *ev;
318 *action = 0;
319 #ifdef CONSOLE_DEBUG
320 if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
321 printf("%s: mod %d, sym %d, name %s\n", event.type == SDL_KEYDOWN ? "DOWN" : "UP",
322 event.key.keysym.mod, event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
323 #endif
324 switch (event.type) {
325 case SDL_QUIT:
326 return CK_QUIT;
327 case SDL_KEYDOWN:
328 keymods = 0;
329 if(event.key.keysym.mod & KMOD_LSHIFT || event.key.keysym.mod & KMOD_RSHIFT) keymods |= CK_MOD_SHIFT;
330 if(event.key.keysym.mod & KMOD_LALT || event.key.keysym.mod & KMOD_RALT ) keymods |= CK_MOD_ALT;
331 if(event.key.keysym.mod & KMOD_LCTRL || event.key.keysym.mod & KMOD_RCTRL ) keymods |= CK_MOD_CTRL;
332 if(event.key.keysym.mod & KMOD_LMETA || event.key.keysym.mod & KMOD_RMETA ) keymods |= CK_MOD_FLAG;
333 if(event.key.keysym.mod & KMOD_MODE) keymods |= CK_MOD_ALTGR;
334 /* there's apparently a bug in SDL 1.2.0: when i press 'c', KMOD_NUM is set
335 if(event.key.keysym.mod & KMOD_NUM) keymods |= CK_MOD_NUMERICPAD; */
336 switch (event.key.keysym.sym) {
337 case SDLK_PAGEUP:
338 return keymods | CK_PAGE_UP;
339 case SDLK_PAGEDOWN:
340 return keymods | CK_PAGE_DOWN;
341 case SDLK_UP:
342 return keymods | CK_CURSOR_UP;
343 case SDLK_DOWN:
344 return keymods | CK_CURSOR_DOWN;
345 case SDLK_LEFT:
346 return keymods | CK_CURSOR_LEFT;
347 case SDLK_RIGHT:
348 return keymods | CK_CURSOR_RIGHT;
349 case SDLK_F1: case SDLK_F2: case SDLK_F3:
350 case SDLK_F4: case SDLK_F5: case SDLK_F6:
351 case SDLK_F7: case SDLK_F8: case SDLK_F9:
352 case SDLK_F10: case SDLK_F11: case SDLK_F12:
353 if(event.key.keysym.sym == SDLK_F12) {
354 if(keymods & CK_MOD_ALT) {
355 print_all_fonts(self);
356 return CK_UNDEF;
359 return keymods | (CK_F1 + (event.key.keysym.sym - SDLK_F1));
360 case SDLK_RSHIFT:
361 return keymods | CK_RSHIFT;
362 case SDLK_LSHIFT:
363 return keymods | CK_LSHIFT;
364 //case SDLK_NUMLOCK:
365 case SDLK_LCTRL:
366 return keymods | CK_LCTRL;
367 case SDLK_RCTRL:
368 return keymods | CK_RCTRL;
369 case SDLK_MODE: //alt gr
370 return keymods | CK_ALTGR;
371 case SDLK_ESCAPE:
372 return keymods | CK_ESCAPE;
373 case SDLK_RETURN:
374 if(keymods & CK_MOD_ALT) *action = ea_toggle_fullscreen;
375 return keymods | CK_RETURN;
376 case SDLK_BACKSPACE:
377 return keymods | CK_BACKSPACE;
378 case SDLK_DELETE:
379 return keymods | CK_DEL;
380 case SDLK_HOME:
381 return keymods | CK_HOME;
382 case SDLK_END:
383 return keymods | CK_END;
384 case SDLK_KP_PLUS:
385 return keymods | CK_PLUS;
386 case SDLK_KP_MINUS:
387 return keymods | CK_MINUS;
388 case SDLK_KP_DIVIDE:
389 return keymods | '%';
390 case SDLK_KP_MULTIPLY:
391 return keymods | '*';
392 case SDLK_KP_ENTER:
393 return keymods | CK_RETURN;
394 case SDLK_KP0: case SDLK_KP1: case SDLK_KP2: case SDLK_KP3:
395 case SDLK_KP4: case SDLK_KP5: case SDLK_KP6: case SDLK_KP7:
396 case SDLK_KP8: case SDLK_KP9:
397 return keymods | ('0' + (event.key.keysym.sym - SDLK_KP0));
398 default:
399 return keymods | sdlkey_to_ascii(self, &event.key.keysym);
401 break;
402 #if SDL_MAJOR_VERSION == 2
403 case SDL_MOUSEWHEEL:
404 self->mouse.mouse_ev = event.wheel.y > 0 ? ME_WHEEL_UP : ME_WHEEL_DOWN;
405 return CK_MOUSE_EVENT;
406 #endif
407 case SDL_MOUSEBUTTONUP:
408 case SDL_MOUSEBUTTONDOWN:
409 self->mouse.mouse_ev = (event.type == SDL_MOUSEBUTTONDOWN) ? ME_BUTTON_DOWN : ME_BUTTON_UP;
411 switch(event.button.button) {
412 case SDL_BUTTON_MIDDLE:
413 self->mouse.button = MB_LEFT;
414 break;
415 case SDL_BUTTON_RIGHT:
416 self->mouse.button = MB_LEFT;
417 break;
418 case SDL_BUTTON_LEFT:
419 self->mouse.button = MB_LEFT;
420 break;
421 #if SDL_MAJOR_VERSION == 1
422 case SDL_BUTTON_WHEELUP:
423 case SDL_BUTTON_WHEELDOWN:
424 self->mouse.mouse_ev = (event.button.button == SDL_BUTTON_WHEELUP) ? ME_WHEEL_UP : ME_WHEEL_DOWN;
425 break;
426 #endif
427 default:
428 self->mouse.button = MB_NONE;
429 break;
431 self->mouse.coords.x = event.button.x / c->fnt->dim.x;
432 self->mouse.coords.y = event.button.y / c->fnt->dim.y;
434 return CK_MOUSE_EVENT;
435 case SDL_MOUSEMOTION:
436 if(event.motion.state & SDL_BUTTON(SDL_BUTTON_LEFT)) self->mouse.button = MB_LEFT;
437 else if(event.motion.state & SDL_BUTTON(SDL_BUTTON_RIGHT)) self->mouse.button = MB_RIGHT;
438 else if(event.motion.state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) self->mouse.button = MB_MIDDLE;
439 else self->mouse.button = MB_NONE;
440 self->mouse.coords.x = event.motion.x / c->fnt->dim.x;
441 self->mouse.coords.y = event.motion.y / c->fnt->dim.y;
442 self->mouse.mouse_ev = ME_MOVE;
443 return CK_MOUSE_EVENT;
444 #if SDL_MAJOR_VERSION == 1
445 case SDL_VIDEORESIZE:
446 *action = ea_resized;
447 return CK_RESIZE_EVENT;
448 #elif SDL_MAJOR_VERSION == 2
449 case SDL_WINDOWEVENT:
450 if(event.window.event == SDL_WINDOWEVENT_RESIZED) {
451 *action = ea_resized;
452 return CK_RESIZE_EVENT;
453 } else if (event.window.event == SDL_WINDOWEVENT_EXPOSED) {
454 *action = ea_redraw;
455 return CK_UNDEF;
457 break;
458 #endif
459 default:
460 break;
462 return CK_UNDEF;
465 #endif