1 #include "sdlconsole.h"
2 #include "console_keys.h"
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
26 #ifdef IN_KDEVELOP_PARSER
27 #define INCLUDED_FROM_SDLCONSOLE
30 #ifdef INCLUDED_FROM_SDLCONSOLE
32 static int sdlkey_to_ascii(Console
* self
, struct SDL_keysym
* key
) {
33 SDLConsole
*c
= &self
->backend
.sdl
;
36 if( (key
->mod
& KMOD_LSHIFT
) || (key
->mod
& KMOD_RSHIFT
))
38 else if((key
->mod
& KMOD_LALT
) || (key
->mod
& KMOD_RALT
))
40 else if((key
->mod
& KMOD_LCTRL
) || (key
->mod
& KMOD_RCTRL
))
42 else if(key
->mod
& KMOD_MODE
)
47 // printf("shift: %d, mod %d, sym %d, name %s\n", shift, key->mod, key->sym, SDL_GetKeyName(key->sym));
52 if(key
->sym
== SDLK_RETURN
)
53 console_toggle_fullscreen(self
);
97 printf("alt gr pressed!\n");
99 #if SDL_MAJOR_VERSION == 1
176 #if SDL_MAJOR_VERSION == 1
287 static void print_all_fonts(Console
* c
) {
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
);
297 console_linebreak(c
);
298 console_printf(c
, "%.3d %c ", (int) i
, (int) i
);
305 ea_toggle_fullscreen
,
314 int sdlconsole_translate_event(Console
* self
, SDL_Event
* ev
, int *action
) {
315 SDLConsole
* c
= &self
->backend
.sdl
;
317 SDL_Event event
= *ev
;
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
));
324 switch (event
.type
) {
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
) {
338 return keymods
| CK_PAGE_UP
;
340 return keymods
| CK_PAGE_DOWN
;
342 return keymods
| CK_CURSOR_UP
;
344 return keymods
| CK_CURSOR_DOWN
;
346 return keymods
| CK_CURSOR_LEFT
;
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
);
359 return keymods
| (CK_F1
+ (event
.key
.keysym
.sym
- SDLK_F1
));
361 return keymods
| CK_RSHIFT
;
363 return keymods
| CK_LSHIFT
;
366 return keymods
| CK_LCTRL
;
368 return keymods
| CK_RCTRL
;
369 case SDLK_MODE
: //alt gr
370 return keymods
| CK_ALTGR
;
372 return keymods
| CK_ESCAPE
;
374 if(keymods
& CK_MOD_ALT
) *action
= ea_toggle_fullscreen
;
375 return keymods
| CK_RETURN
;
377 return keymods
| CK_BACKSPACE
;
379 return keymods
| CK_DEL
;
381 return keymods
| CK_HOME
;
383 return keymods
| CK_END
;
385 return keymods
| CK_PLUS
;
387 return keymods
| CK_MINUS
;
389 return keymods
| '%';
390 case SDLK_KP_MULTIPLY
:
391 return keymods
| '*';
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
));
399 return keymods
| sdlkey_to_ascii(self
, &event
.key
.keysym
);
402 #if SDL_MAJOR_VERSION == 2
404 self
->mouse
.mouse_ev
= event
.wheel
.y
> 0 ? ME_WHEEL_UP
: ME_WHEEL_DOWN
;
405 return CK_MOUSE_EVENT
;
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
;
415 case SDL_BUTTON_RIGHT
:
416 self
->mouse
.button
= MB_LEFT
;
418 case SDL_BUTTON_LEFT
:
419 self
->mouse
.button
= MB_LEFT
;
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
;
428 self
->mouse
.button
= MB_NONE
;
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
) {