1 /*
2 Simple DirectMedia Layer
3 Copyright (C) 2017 BlackBerry Limited
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #include "../../SDL_internal.h"
23 #include "../../events/SDL_keyboard_c.h"
24 #include "SDL_scancode.h"
25 #include "SDL_events.h"
26 #include "sdl_qnx.h"
27 #include <sys/keycodes.h>
28
29 /**
30 * A map thta translates Screen key names to SDL scan codes.
31 * This map is incomplete, but should include most major keys.
32 */
33 static int key_to_sdl[] = {
34 [KEYCODE_SPACE] = SDL_SCANCODE_SPACE,
35 [KEYCODE_APOSTROPHE] = SDL_SCANCODE_APOSTROPHE,
36 [KEYCODE_COMMA] = SDL_SCANCODE_COMMA,
37 [KEYCODE_MINUS] = SDL_SCANCODE_MINUS,
38 [KEYCODE_PERIOD] = SDL_SCANCODE_PERIOD,
39 [KEYCODE_SLASH] = SDL_SCANCODE_SLASH,
40 [KEYCODE_ZERO] = SDL_SCANCODE_0,
41 [KEYCODE_ONE] = SDL_SCANCODE_1,
42 [KEYCODE_TWO] = SDL_SCANCODE_2,
43 [KEYCODE_THREE] = SDL_SCANCODE_3,
44 [KEYCODE_FOUR] = SDL_SCANCODE_4,
45 [KEYCODE_FIVE] = SDL_SCANCODE_5,
46 [KEYCODE_SIX] = SDL_SCANCODE_6,
47 [KEYCODE_SEVEN] = SDL_SCANCODE_7,
48 [KEYCODE_EIGHT] = SDL_SCANCODE_8,
49 [KEYCODE_NINE] = SDL_SCANCODE_9,
50 [KEYCODE_SEMICOLON] = SDL_SCANCODE_SEMICOLON,
51 [KEYCODE_EQUAL] = SDL_SCANCODE_EQUALS,
52 [KEYCODE_LEFT_BRACKET] = SDL_SCANCODE_LEFTBRACKET,
53 [KEYCODE_BACK_SLASH] = SDL_SCANCODE_BACKSLASH,
54 [KEYCODE_RIGHT_BRACKET] = SDL_SCANCODE_RIGHTBRACKET,
55 [KEYCODE_GRAVE] = SDL_SCANCODE_GRAVE,
56 [KEYCODE_A] = SDL_SCANCODE_A,
57 [KEYCODE_B] = SDL_SCANCODE_B,
58 [KEYCODE_C] = SDL_SCANCODE_C,
59 [KEYCODE_D] = SDL_SCANCODE_D,
60 [KEYCODE_E] = SDL_SCANCODE_E,
61 [KEYCODE_F] = SDL_SCANCODE_F,
62 [KEYCODE_G] = SDL_SCANCODE_G,
63 [KEYCODE_H] = SDL_SCANCODE_H,
64 [KEYCODE_I] = SDL_SCANCODE_I,
65 [KEYCODE_J] = SDL_SCANCODE_J,
66 [KEYCODE_K] = SDL_SCANCODE_K,
67 [KEYCODE_L] = SDL_SCANCODE_L,
68 [KEYCODE_M] = SDL_SCANCODE_M,
69 [KEYCODE_N] = SDL_SCANCODE_N,
70 [KEYCODE_O] = SDL_SCANCODE_O,
71 [KEYCODE_P] = SDL_SCANCODE_P,
72 [KEYCODE_Q] = SDL_SCANCODE_Q,
73 [KEYCODE_R] = SDL_SCANCODE_R,
74 [KEYCODE_S] = SDL_SCANCODE_S,
75 [KEYCODE_T] = SDL_SCANCODE_T,
76 [KEYCODE_U] = SDL_SCANCODE_U,
77 [KEYCODE_V] = SDL_SCANCODE_V,
78 [KEYCODE_W] = SDL_SCANCODE_W,
79 [KEYCODE_X] = SDL_SCANCODE_X,
80 [KEYCODE_Y] = SDL_SCANCODE_Y,
81 [KEYCODE_Z] = SDL_SCANCODE_Z,
82 [KEYCODE_UP] = SDL_SCANCODE_UP,
83 [KEYCODE_DOWN] = SDL_SCANCODE_DOWN,
84 [KEYCODE_LEFT] = SDL_SCANCODE_LEFT,
85 [KEYCODE_PG_UP] = SDL_SCANCODE_PAGEUP,
86 [KEYCODE_PG_DOWN] = SDL_SCANCODE_PAGEDOWN,
87 [KEYCODE_RIGHT] = SDL_SCANCODE_RIGHT,
88 [KEYCODE_RETURN] = SDL_SCANCODE_RETURN,
89 [KEYCODE_TAB] = SDL_SCANCODE_TAB,
90 [KEYCODE_ESCAPE] = SDL_SCANCODE_ESCAPE,
91 };
92
93 /**
94 * Called from the event dispatcher when a keyboard event is encountered.
95 * Translates the event such that it can be handled by SDL.
96 * @param event Screen keyboard event
97 */
98 void
handleKeyboardEvent(screen_event_t event)99 handleKeyboardEvent(screen_event_t event)
100 {
101 int val;
102 SDL_Scancode scancode;
103
104 // Get the key value.
105 if (screen_get_event_property_iv(event, SCREEN_PROPERTY_SYM, &val) < 0) {
106 return;
107 }
108
109 // Skip unrecognized keys.
110 if ((val < 0) || (val >= SDL_TABLESIZE(key_to_sdl))) {
111 return;
112 }
113
114 // Translate to an SDL scan code.
115 scancode = key_to_sdl[val];
116 if (scancode == 0) {
117 return;
118 }
119
120 // Get event flags (key state).
121 if (screen_get_event_property_iv(event, SCREEN_PROPERTY_FLAGS, &val) < 0) {
122 return;
123 }
124
125 // Propagate the event to SDL.
126 // FIXME:
127 // Need to handle more key states (such as key combinations).
128 if (val & KEY_DOWN) {
129 SDL_SendKeyboardKey(SDL_PRESSED, scancode);
130 } else {
131 SDL_SendKeyboardKey(SDL_RELEASED, scancode);
132 }
133 }
134