comparison src/events/SDL_keyboard.c @ 1895:c121d94672cb

SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 10 Jul 2006 21:04:37 +0000
parents a9be6a3a51d1
children 25d6537feea4
comparison
equal deleted inserted replaced
1894:c69cee13dd76 1895:c121d94672cb
27 #include "SDL_events.h" 27 #include "SDL_events.h"
28 #include "SDL_events_c.h" 28 #include "SDL_events_c.h"
29 #include "SDL_sysevents.h" 29 #include "SDL_sysevents.h"
30 30
31 31
32 /* Global keystate information */ 32 /* Global keyboard information */
33 static Uint8 SDL_KeyState[SDLK_LAST];
34 static SDLMod SDL_ModState;
35 int SDL_TranslateUNICODE = 0; 33 int SDL_TranslateUNICODE = 0;
36 34 static int SDL_num_keyboards;
37 static const char *keynames[SDLK_LAST]; /* Array of keycode names */ 35 static int SDL_current_keyboard;
38 36 static SDL_Keyboard **SDL_keyboards;
39 /* 37
40 * jk 991215 - added 38 static const char *SDL_keynames[SDLK_LAST]; /* Array of keycode names */
41 */
42 struct {
43 int firsttime; /* if we check against the delay or repeat value */
44 int delay; /* the delay before we start repeating */
45 int interval; /* the delay between key repeat events */
46 Uint32 timestamp; /* the time the first keydown event occurred */
47
48 SDL_Event evt; /* the event we are supposed to repeat */
49 } SDL_KeyRepeat;
50 39
51 /* Public functions */ 40 /* Public functions */
52 int SDL_KeyboardInit(void) 41 int
53 { 42 SDL_KeyboardInit(void)
54 SDL_VideoDevice *video = current_video; 43 {
55 SDL_VideoDevice *this = current_video; 44 int i;
56 45
57 /* Set default mode of UNICODE translation */ 46 /* Set default mode of UNICODE translation */
58 SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION); 47 SDL_EnableUNICODE(DEFAULT_UNICODE_TRANSLATION);
59 48
60 /* Initialize the tables */ 49 /* Initialize the tables */
61 SDL_ModState = KMOD_NONE; 50 for (i = 0; i < SDL_arraysize(SDL_keynames); ++i) {
62 SDL_memset((void*)keynames, 0, sizeof(keynames)); 51 switch (i) {
63 SDL_memset(SDL_KeyState, 0, sizeof(SDL_KeyState)); 52 case SDLK_BACKSPACE:
64 video->InitOSKeymap(this); 53 SDL_keynames[i] = "backspace";
65 54 break;
66 SDL_EnableKeyRepeat(0, 0); 55 case SDLK_TAB:
67 56 SDL_keynames[i] = "tab";
68 /* Fill in the blanks in keynames */ 57 break;
69 keynames[SDLK_BACKSPACE] = "backspace"; 58 case SDLK_CLEAR:
70 keynames[SDLK_TAB] = "tab"; 59 SDL_keynames[i] = "clear";
71 keynames[SDLK_CLEAR] = "clear"; 60 break;
72 keynames[SDLK_RETURN] = "return"; 61 case SDLK_RETURN:
73 keynames[SDLK_PAUSE] = "pause"; 62 SDL_keynames[i] = "return";
74 keynames[SDLK_ESCAPE] = "escape"; 63 break;
75 keynames[SDLK_SPACE] = "space"; 64 case SDLK_PAUSE:
76 keynames[SDLK_EXCLAIM] = "!"; 65 SDL_keynames[i] = "pause";
77 keynames[SDLK_QUOTEDBL] = "\""; 66 break;
78 keynames[SDLK_HASH] = "#"; 67 case SDLK_ESCAPE:
79 keynames[SDLK_DOLLAR] = "$"; 68 SDL_keynames[i] = "escape";
80 keynames[SDLK_AMPERSAND] = "&"; 69 break;
81 keynames[SDLK_QUOTE] = "'"; 70 case SDLK_SPACE:
82 keynames[SDLK_LEFTPAREN] = "("; 71 SDL_keynames[i] = "space";
83 keynames[SDLK_RIGHTPAREN] = ")"; 72 break;
84 keynames[SDLK_ASTERISK] = "*"; 73
85 keynames[SDLK_PLUS] = "+"; 74 case SDLK_KP0:
86 keynames[SDLK_COMMA] = ","; 75 SDL_keynames[i] = "[0]";
87 keynames[SDLK_MINUS] = "-"; 76 break;
88 keynames[SDLK_PERIOD] = "."; 77 case SDLK_KP1:
89 keynames[SDLK_SLASH] = "/"; 78 SDL_keynames[i] = "[1]";
90 keynames[SDLK_0] = "0"; 79 break;
91 keynames[SDLK_1] = "1"; 80 case SDLK_KP2:
92 keynames[SDLK_2] = "2"; 81 SDL_keynames[i] = "[2]";
93 keynames[SDLK_3] = "3"; 82 break;
94 keynames[SDLK_4] = "4"; 83 case SDLK_KP3:
95 keynames[SDLK_5] = "5"; 84 SDL_keynames[i] = "[3]";
96 keynames[SDLK_6] = "6"; 85 break;
97 keynames[SDLK_7] = "7"; 86 case SDLK_KP4:
98 keynames[SDLK_8] = "8"; 87 SDL_keynames[i] = "[4]";
99 keynames[SDLK_9] = "9"; 88 break;
100 keynames[SDLK_COLON] = ":"; 89 case SDLK_KP5:
101 keynames[SDLK_SEMICOLON] = ";"; 90 SDL_keynames[i] = "[5]";
102 keynames[SDLK_LESS] = "<"; 91 break;
103 keynames[SDLK_EQUALS] = "="; 92 case SDLK_KP6:
104 keynames[SDLK_GREATER] = ">"; 93 SDL_keynames[i] = "[6]";
105 keynames[SDLK_QUESTION] = "?"; 94 break;
106 keynames[SDLK_AT] = "@"; 95 case SDLK_KP7:
107 keynames[SDLK_LEFTBRACKET] = "["; 96 SDL_keynames[i] = "[7]";
108 keynames[SDLK_BACKSLASH] = "\\"; 97 break;
109 keynames[SDLK_RIGHTBRACKET] = "]"; 98 case SDLK_KP8:
110 keynames[SDLK_CARET] = "^"; 99 SDL_keynames[i] = "[8]";
111 keynames[SDLK_UNDERSCORE] = "_"; 100 break;
112 keynames[SDLK_BACKQUOTE] = "`"; 101 case SDLK_KP9:
113 keynames[SDLK_a] = "a"; 102 SDL_keynames[i] = "[9]";
114 keynames[SDLK_b] = "b"; 103 break;
115 keynames[SDLK_c] = "c"; 104 case SDLK_KP_PERIOD:
116 keynames[SDLK_d] = "d"; 105 SDL_keynames[i] = "[.]";
117 keynames[SDLK_e] = "e"; 106 break;
118 keynames[SDLK_f] = "f"; 107 case SDLK_KP_DIVIDE:
119 keynames[SDLK_g] = "g"; 108 SDL_keynames[i] = "[/]";
120 keynames[SDLK_h] = "h"; 109 break;
121 keynames[SDLK_i] = "i"; 110 case SDLK_KP_MULTIPLY:
122 keynames[SDLK_j] = "j"; 111 SDL_keynames[i] = "[*]";
123 keynames[SDLK_k] = "k"; 112 break;
124 keynames[SDLK_l] = "l"; 113 case SDLK_KP_MINUS:
125 keynames[SDLK_m] = "m"; 114 SDL_keynames[i] = "[-]";
126 keynames[SDLK_n] = "n"; 115 break;
127 keynames[SDLK_o] = "o"; 116 case SDLK_KP_PLUS:
128 keynames[SDLK_p] = "p"; 117 SDL_keynames[i] = "[+]";
129 keynames[SDLK_q] = "q"; 118 break;
130 keynames[SDLK_r] = "r"; 119 case SDLK_KP_ENTER:
131 keynames[SDLK_s] = "s"; 120 SDL_keynames[i] = "enter";
132 keynames[SDLK_t] = "t"; 121 break;
133 keynames[SDLK_u] = "u"; 122 case SDLK_KP_EQUALS:
134 keynames[SDLK_v] = "v"; 123 SDL_keynames[i] = "equals";
135 keynames[SDLK_w] = "w"; 124 break;
136 keynames[SDLK_x] = "x"; 125
137 keynames[SDLK_y] = "y"; 126 case SDLK_UP:
138 keynames[SDLK_z] = "z"; 127 SDL_keynames[i] = "up";
139 keynames[SDLK_DELETE] = "delete"; 128 break;
140 129 case SDLK_DOWN:
141 keynames[SDLK_WORLD_0] = "world 0"; 130 SDL_keynames[i] = "down";
142 keynames[SDLK_WORLD_1] = "world 1"; 131 break;
143 keynames[SDLK_WORLD_2] = "world 2"; 132 case SDLK_RIGHT:
144 keynames[SDLK_WORLD_3] = "world 3"; 133 SDL_keynames[i] = "right";
145 keynames[SDLK_WORLD_4] = "world 4"; 134 break;
146 keynames[SDLK_WORLD_5] = "world 5"; 135 case SDLK_LEFT:
147 keynames[SDLK_WORLD_6] = "world 6"; 136 SDL_keynames[i] = "left";
148 keynames[SDLK_WORLD_7] = "world 7"; 137 break;
149 keynames[SDLK_WORLD_8] = "world 8"; 138 case SDLK_INSERT:
150 keynames[SDLK_WORLD_9] = "world 9"; 139 SDL_keynames[i] = "insert";
151 keynames[SDLK_WORLD_10] = "world 10"; 140 break;
152 keynames[SDLK_WORLD_11] = "world 11"; 141 case SDLK_HOME:
153 keynames[SDLK_WORLD_12] = "world 12"; 142 SDL_keynames[i] = "home";
154 keynames[SDLK_WORLD_13] = "world 13"; 143 break;
155 keynames[SDLK_WORLD_14] = "world 14"; 144 case SDLK_END:
156 keynames[SDLK_WORLD_15] = "world 15"; 145 SDL_keynames[i] = "end";
157 keynames[SDLK_WORLD_16] = "world 16"; 146 break;
158 keynames[SDLK_WORLD_17] = "world 17"; 147 case SDLK_PAGEUP:
159 keynames[SDLK_WORLD_18] = "world 18"; 148 SDL_keynames[i] = "page up";
160 keynames[SDLK_WORLD_19] = "world 19"; 149 break;
161 keynames[SDLK_WORLD_20] = "world 20"; 150 case SDLK_PAGEDOWN:
162 keynames[SDLK_WORLD_21] = "world 21"; 151 SDL_keynames[i] = "page down";
163 keynames[SDLK_WORLD_22] = "world 22"; 152 break;
164 keynames[SDLK_WORLD_23] = "world 23"; 153
165 keynames[SDLK_WORLD_24] = "world 24"; 154 case SDLK_F1:
166 keynames[SDLK_WORLD_25] = "world 25"; 155 SDL_keynames[i] = "f1";
167 keynames[SDLK_WORLD_26] = "world 26"; 156 break;
168 keynames[SDLK_WORLD_27] = "world 27"; 157 case SDLK_F2:
169 keynames[SDLK_WORLD_28] = "world 28"; 158 SDL_keynames[i] = "f2";
170 keynames[SDLK_WORLD_29] = "world 29"; 159 break;
171 keynames[SDLK_WORLD_30] = "world 30"; 160 case SDLK_F3:
172 keynames[SDLK_WORLD_31] = "world 31"; 161 SDL_keynames[i] = "f3";
173 keynames[SDLK_WORLD_32] = "world 32"; 162 break;
174 keynames[SDLK_WORLD_33] = "world 33"; 163 case SDLK_F4:
175 keynames[SDLK_WORLD_34] = "world 34"; 164 SDL_keynames[i] = "f4";
176 keynames[SDLK_WORLD_35] = "world 35"; 165 break;
177 keynames[SDLK_WORLD_36] = "world 36"; 166 case SDLK_F5:
178 keynames[SDLK_WORLD_37] = "world 37"; 167 SDL_keynames[i] = "f5";
179 keynames[SDLK_WORLD_38] = "world 38"; 168 break;
180 keynames[SDLK_WORLD_39] = "world 39"; 169 case SDLK_F6:
181 keynames[SDLK_WORLD_40] = "world 40"; 170 SDL_keynames[i] = "f6";
182 keynames[SDLK_WORLD_41] = "world 41"; 171 break;
183 keynames[SDLK_WORLD_42] = "world 42"; 172 case SDLK_F7:
184 keynames[SDLK_WORLD_43] = "world 43"; 173 SDL_keynames[i] = "f7";
185 keynames[SDLK_WORLD_44] = "world 44"; 174 break;
186 keynames[SDLK_WORLD_45] = "world 45"; 175 case SDLK_F8:
187 keynames[SDLK_WORLD_46] = "world 46"; 176 SDL_keynames[i] = "f8";
188 keynames[SDLK_WORLD_47] = "world 47"; 177 break;
189 keynames[SDLK_WORLD_48] = "world 48"; 178 case SDLK_F9:
190 keynames[SDLK_WORLD_49] = "world 49"; 179 SDL_keynames[i] = "f9";
191 keynames[SDLK_WORLD_50] = "world 50"; 180 break;
192 keynames[SDLK_WORLD_51] = "world 51"; 181 case SDLK_F10:
193 keynames[SDLK_WORLD_52] = "world 52"; 182 SDL_keynames[i] = "f10";
194 keynames[SDLK_WORLD_53] = "world 53"; 183 break;
195 keynames[SDLK_WORLD_54] = "world 54"; 184 case SDLK_F11:
196 keynames[SDLK_WORLD_55] = "world 55"; 185 SDL_keynames[i] = "f11";
197 keynames[SDLK_WORLD_56] = "world 56"; 186 break;
198 keynames[SDLK_WORLD_57] = "world 57"; 187 case SDLK_F12:
199 keynames[SDLK_WORLD_58] = "world 58"; 188 SDL_keynames[i] = "f12";
200 keynames[SDLK_WORLD_59] = "world 59"; 189 break;
201 keynames[SDLK_WORLD_60] = "world 60"; 190 case SDLK_F13:
202 keynames[SDLK_WORLD_61] = "world 61"; 191 SDL_keynames[i] = "f13";
203 keynames[SDLK_WORLD_62] = "world 62"; 192 break;
204 keynames[SDLK_WORLD_63] = "world 63"; 193 case SDLK_F14:
205 keynames[SDLK_WORLD_64] = "world 64"; 194 SDL_keynames[i] = "f14";
206 keynames[SDLK_WORLD_65] = "world 65"; 195 break;
207 keynames[SDLK_WORLD_66] = "world 66"; 196 case SDLK_F15:
208 keynames[SDLK_WORLD_67] = "world 67"; 197 SDL_keynames[i] = "f15";
209 keynames[SDLK_WORLD_68] = "world 68"; 198 break;
210 keynames[SDLK_WORLD_69] = "world 69"; 199
211 keynames[SDLK_WORLD_70] = "world 70"; 200 case SDLK_NUMLOCK:
212 keynames[SDLK_WORLD_71] = "world 71"; 201 SDL_keynames[i] = "numlock";
213 keynames[SDLK_WORLD_72] = "world 72"; 202 break;
214 keynames[SDLK_WORLD_73] = "world 73"; 203 case SDLK_CAPSLOCK:
215 keynames[SDLK_WORLD_74] = "world 74"; 204 SDL_keynames[i] = "caps lock";
216 keynames[SDLK_WORLD_75] = "world 75"; 205 break;
217 keynames[SDLK_WORLD_76] = "world 76"; 206 case SDLK_SCROLLOCK:
218 keynames[SDLK_WORLD_77] = "world 77"; 207 SDL_keynames[i] = "scroll lock";
219 keynames[SDLK_WORLD_78] = "world 78"; 208 break;
220 keynames[SDLK_WORLD_79] = "world 79"; 209 case SDLK_RSHIFT:
221 keynames[SDLK_WORLD_80] = "world 80"; 210 SDL_keynames[i] = "right shift";
222 keynames[SDLK_WORLD_81] = "world 81"; 211 break;
223 keynames[SDLK_WORLD_82] = "world 82"; 212 case SDLK_LSHIFT:
224 keynames[SDLK_WORLD_83] = "world 83"; 213 SDL_keynames[i] = "left shift";
225 keynames[SDLK_WORLD_84] = "world 84"; 214 break;
226 keynames[SDLK_WORLD_85] = "world 85"; 215 case SDLK_RCTRL:
227 keynames[SDLK_WORLD_86] = "world 86"; 216 SDL_keynames[i] = "right ctrl";
228 keynames[SDLK_WORLD_87] = "world 87"; 217 break;
229 keynames[SDLK_WORLD_88] = "world 88"; 218 case SDLK_LCTRL:
230 keynames[SDLK_WORLD_89] = "world 89"; 219 SDL_keynames[i] = "left ctrl";
231 keynames[SDLK_WORLD_90] = "world 90"; 220 break;
232 keynames[SDLK_WORLD_91] = "world 91"; 221 case SDLK_RALT:
233 keynames[SDLK_WORLD_92] = "world 92"; 222 SDL_keynames[i] = "right alt";
234 keynames[SDLK_WORLD_93] = "world 93"; 223 break;
235 keynames[SDLK_WORLD_94] = "world 94"; 224 case SDLK_LALT:
236 keynames[SDLK_WORLD_95] = "world 95"; 225 SDL_keynames[i] = "left alt";
237 226 break;
238 keynames[SDLK_KP0] = "[0]"; 227 case SDLK_RMETA:
239 keynames[SDLK_KP1] = "[1]"; 228 SDL_keynames[i] = "right meta";
240 keynames[SDLK_KP2] = "[2]"; 229 break;
241 keynames[SDLK_KP3] = "[3]"; 230 case SDLK_LMETA:
242 keynames[SDLK_KP4] = "[4]"; 231 SDL_keynames[i] = "left meta";
243 keynames[SDLK_KP5] = "[5]"; 232 break;
244 keynames[SDLK_KP6] = "[6]"; 233 case SDLK_LSUPER:
245 keynames[SDLK_KP7] = "[7]"; 234 SDL_keynames[i] = "left super"; /* "Windows" keys */
246 keynames[SDLK_KP8] = "[8]"; 235 break;
247 keynames[SDLK_KP9] = "[9]"; 236 case SDLK_RSUPER:
248 keynames[SDLK_KP_PERIOD] = "[.]"; 237 SDL_keynames[i] = "right super";
249 keynames[SDLK_KP_DIVIDE] = "[/]"; 238 break;
250 keynames[SDLK_KP_MULTIPLY] = "[*]"; 239 case SDLK_MODE:
251 keynames[SDLK_KP_MINUS] = "[-]"; 240 SDL_keynames[i] = "alt gr";
252 keynames[SDLK_KP_PLUS] = "[+]"; 241 break;
253 keynames[SDLK_KP_ENTER] = "enter"; 242 case SDLK_COMPOSE:
254 keynames[SDLK_KP_EQUALS] = "equals"; 243 SDL_keynames[i] = "compose";
255 244 break;
256 keynames[SDLK_UP] = "up"; 245
257 keynames[SDLK_DOWN] = "down"; 246 case SDLK_HELP:
258 keynames[SDLK_RIGHT] = "right"; 247 SDL_keynames[i] = "help";
259 keynames[SDLK_LEFT] = "left"; 248 break;
260 keynames[SDLK_DOWN] = "down"; 249 case SDLK_PRINT:
261 keynames[SDLK_INSERT] = "insert"; 250 SDL_keynames[i] = "print screen";
262 keynames[SDLK_HOME] = "home"; 251 break;
263 keynames[SDLK_END] = "end"; 252 case SDLK_SYSREQ:
264 keynames[SDLK_PAGEUP] = "page up"; 253 SDL_keynames[i] = "sys req";
265 keynames[SDLK_PAGEDOWN] = "page down"; 254 break;
266 255 case SDLK_BREAK:
267 keynames[SDLK_F1] = "f1"; 256 SDL_keynames[i] = "break";
268 keynames[SDLK_F2] = "f2"; 257 break;
269 keynames[SDLK_F3] = "f3"; 258 case SDLK_MENU:
270 keynames[SDLK_F4] = "f4"; 259 SDL_keynames[i] = "menu";
271 keynames[SDLK_F5] = "f5"; 260 break;
272 keynames[SDLK_F6] = "f6"; 261 case SDLK_POWER:
273 keynames[SDLK_F7] = "f7"; 262 SDL_keynames[i] = "power";
274 keynames[SDLK_F8] = "f8"; 263 break;
275 keynames[SDLK_F9] = "f9"; 264 case SDLK_EURO:
276 keynames[SDLK_F10] = "f10"; 265 SDL_keynames[i] = "euro";
277 keynames[SDLK_F11] = "f11"; 266 break;
278 keynames[SDLK_F12] = "f12"; 267 case SDLK_UNDO:
279 keynames[SDLK_F13] = "f13"; 268 SDL_keynames[i] = "undo";
280 keynames[SDLK_F14] = "f14"; 269 break;
281 keynames[SDLK_F15] = "f15"; 270
282 271 default:
283 keynames[SDLK_NUMLOCK] = "numlock"; 272 SDL_keynames[i] = NULL;
284 keynames[SDLK_CAPSLOCK] = "caps lock"; 273 break;
285 keynames[SDLK_SCROLLOCK] = "scroll lock"; 274 }
286 keynames[SDLK_RSHIFT] = "right shift"; 275 }
287 keynames[SDLK_LSHIFT] = "left shift"; 276
288 keynames[SDLK_RCTRL] = "right ctrl"; 277 /* Done. Whew. */
289 keynames[SDLK_LCTRL] = "left ctrl"; 278 return (0);
290 keynames[SDLK_RALT] = "right alt"; 279 }
291 keynames[SDLK_LALT] = "left alt"; 280
292 keynames[SDLK_RMETA] = "right meta"; 281 SDL_Keyboard *
293 keynames[SDLK_LMETA] = "left meta"; 282 SDL_GetKeyboard(int index)
294 keynames[SDLK_LSUPER] = "left super"; /* "Windows" keys */ 283 {
295 keynames[SDLK_RSUPER] = "right super"; 284 if (index < 0 || index >= SDL_num_keyboards) {
296 keynames[SDLK_MODE] = "alt gr"; 285 return NULL;
297 keynames[SDLK_COMPOSE] = "compose"; 286 }
298 287 return SDL_keyboards[index];
299 keynames[SDLK_HELP] = "help"; 288 }
300 keynames[SDLK_PRINT] = "print screen"; 289
301 keynames[SDLK_SYSREQ] = "sys req"; 290 int
302 keynames[SDLK_BREAK] = "break"; 291 SDL_AddKeyboard(const SDL_Keyboard * keyboard, int index)
303 keynames[SDLK_MENU] = "menu"; 292 {
304 keynames[SDLK_POWER] = "power"; 293 SDL_Keyboard **keyboards;
305 keynames[SDLK_EURO] = "euro"; 294
306 keynames[SDLK_UNDO] = "undo"; 295 /* Add the keyboard to the list of keyboards */
307 296 if (index < 0 || index >= SDL_num_keyboards || SDL_keyboards[index]) {
308 /* Done. Whew. */ 297 keyboards =
309 return(0); 298 (SDL_Keyboard **) SDL_realloc(SDL_keyboards,
310 } 299 (SDL_num_keyboards +
311 void SDL_KeyboardQuit(void) 300 1) * sizeof(*keyboards));
312 { 301 if (!keyboards) {
313 } 302 SDL_OutOfMemory();
314 303 return -1;
315 /* We lost the keyboard, so post key up messages for all pressed keys */ 304 }
316 void SDL_ResetKeyboard(void) 305
317 { 306 SDL_keyboards = keyboards;
318 SDL_keysym keysym; 307 index = SDL_num_keyboards++;
319 SDLKey key; 308 }
320 309 SDL_keyboards[index] =
321 SDL_memset(&keysym, 0, (sizeof keysym)); 310 (SDL_Keyboard *) SDL_malloc(sizeof(*SDL_keyboards[index]));
322 for ( key=SDLK_FIRST; key<SDLK_LAST; ++key ) { 311 if (!SDL_keyboards[index]) {
323 if ( SDL_KeyState[key] == SDL_PRESSED ) { 312 SDL_OutOfMemory();
324 keysym.sym = key; 313 return -1;
325 SDL_PrivateKeyboard(SDL_RELEASED, &keysym); 314 }
326 } 315 *SDL_keyboards[index] = *keyboard;
327 } 316
328 SDL_KeyRepeat.timestamp = 0; 317 return index;
329 } 318 }
330 319
331 int SDL_EnableUNICODE(int enable) 320 void
332 { 321 SDL_DelKeyboard(int index)
333 int old_mode; 322 {
334 323 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
335 old_mode = SDL_TranslateUNICODE; 324
336 if ( enable >= 0 ) { 325 if (!keyboard) {
337 SDL_TranslateUNICODE = enable; 326 return;
338 } 327 }
339 return(old_mode); 328
340 } 329 if (keyboard->FreeKeyboard) {
341 330 keyboard->FreeKeyboard(keyboard);
342 Uint8 * SDL_GetKeyState (int *numkeys) 331 }
343 { 332 SDL_free(keyboard);
344 if ( numkeys != (int *)0 ) 333
345 *numkeys = SDLK_LAST; 334 SDL_keyboards[index] = NULL;
346 return(SDL_KeyState); 335 }
347 } 336
348 SDLMod SDL_GetModState (void) 337 void
349 { 338 SDL_ResetKeyboard(int index)
350 return(SDL_ModState); 339 {
351 } 340 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
352 void SDL_SetModState (SDLMod modstate) 341 SDLKey key;
353 { 342
354 SDL_ModState = modstate; 343 if (!keyboard) {
355 } 344 return;
356 345 }
357 char *SDL_GetKeyName(SDLKey key) 346
358 { 347 for (key = SDLK_FIRST; key < SDLK_LAST; ++key) {
359 const char *keyname; 348 if (keyboard->keystate[key] == SDL_PRESSED) {
360 349 SDL_SendKeyboardKey(index, SDL_RELEASED, 0, key);
361 keyname = NULL; 350 }
362 if ( key < SDLK_LAST ) { 351 }
363 keyname = keynames[key]; 352 keyboard->repeat.timestamp = 0;
364 } 353 }
365 if ( keyname == NULL ) { 354
366 keyname = "unknown key"; 355 void
367 } 356 SDL_KeyboardQuit(void)
368 /* FIXME: make this function const in 1.3 */ 357 {
369 return (char *)(keyname); 358 int i;
370 } 359
371 360 for (i = 0; i < SDL_num_keyboards; ++i) {
372 /* These are global for SDL_eventloop.c */ 361 SDL_DelKeyboard(i);
373 int SDL_PrivateKeyboard(Uint8 state, SDL_keysym *keysym) 362 }
374 { 363 SDL_num_keyboards = 0;
375 SDL_Event event; 364 SDL_current_keyboard = 0;
376 int posted, repeatable; 365
377 Uint16 modstate; 366 if (SDL_keyboards) {
378 367 SDL_free(SDL_keyboards);
379 SDL_memset(&event, 0, sizeof(event)); 368 SDL_keyboards = NULL;
380 369 }
370 }
371
372 int
373 SDL_GetNumKeyboards(void)
374 {
375 return SDL_num_keyboards;
376 }
377
378 int
379 SDL_SelectKeyboard(int index)
380 {
381 if (index >= 0 && index < SDL_num_keyboards) {
382 SDL_current_keyboard = index;
383 }
384 return SDL_current_keyboard;
385 }
386
387 int
388 SDL_EnableUNICODE(int enable)
389 {
390 int old_mode;
391
392 old_mode = SDL_TranslateUNICODE;
393 if (enable >= 0) {
394 SDL_TranslateUNICODE = enable;
395 }
396 return (old_mode);
397 }
398
399 Uint8 *
400 SDL_GetKeyState(int *numkeys)
401 {
402 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
403
404 if (numkeys != (int *) 0) {
405 *numkeys = SDLK_LAST;
406 }
407
408 if (!keyboard) {
409 return NULL;
410 }
411 return keyboard->keystate;
412 }
413
414 SDLMod
415 SDL_GetModState(void)
416 {
417 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
418
419 if (!keyboard) {
420 return KMOD_NONE;
421 }
422 return keyboard->modstate;
423 }
424
425 void
426 SDL_SetModState(SDLMod modstate)
427 {
428 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
429
430 if (!keyboard) {
431 return;
432 }
433 keyboard->modstate = modstate;
434 }
435
436 const char *
437 SDL_GetKeyName(SDLKey key)
438 {
439 const char *keyname;
440
441 if (key < SDL_arraysize(SDL_keynames)) {
442 keyname = SDL_keynames[key];
443 } else {
444 keyname = NULL;
445 }
446 if (keyname == NULL) {
447 if (key < 256) {
448 static char temp[4];
449 char *cvt;
450 temp[0] = (char) key;
451 temp[1] = '\0';
452 cvt = SDL_iconv_string("UTF-8", "LATIN1", temp, 1);
453 SDL_strlcpy(temp, cvt, SDL_arraysize(temp));
454 SDL_free(cvt);
455 keyname = temp;
456 } else {
457 keyname = "unknown key";
458 }
459 }
460 return keyname;
461 }
462
463 void
464 SDL_SetKeyboardFocus(int index, SDL_WindowID windowID)
465 {
466 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
467 int i;
468 SDL_bool focus;
469
470 if (!keyboard || (keyboard->focus == windowID)) {
471 return;
472 }
473
474 /* See if the current window has lost focus */
475 if (keyboard->focus) {
476 focus = SDL_FALSE;
477 for (i = 0; i < SDL_num_keyboards; ++i) {
478 SDL_Keyboard *check;
479 if (i != index) {
480 check = SDL_GetKeyboard(i);
481 if (check && check->focus == keyboard->focus) {
482 focus = SDL_TRUE;
483 break;
484 }
485 }
486 }
487 if (!focus) {
488 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_LOST,
489 0, 0);
490 }
491 }
492
493 keyboard->focus = windowID;
494
495 if (keyboard->focus) {
496 focus = SDL_FALSE;
497 for (i = 0; i < SDL_num_keyboards; ++i) {
498 SDL_Keyboard *check;
499 if (i != index) {
500 check = SDL_GetKeyboard(i);
501 if (check && check->focus == keyboard->focus) {
502 focus = SDL_TRUE;
503 break;
504 }
505 }
506 }
507 if (!focus) {
508 SDL_SendWindowEvent(keyboard->focus, SDL_WINDOWEVENT_FOCUS_GAINED,
509 0, 0);
510 }
511 }
512 }
513
514 int
515 SDL_SendKeyboardKey(int index, Uint8 state, Uint8 scancode, SDLKey key)
516 {
517 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
518 int posted, repeatable;
519 Uint16 modstate;
520 Uint8 type;
521
522 if (!keyboard) {
523 return 0;
524 }
381 #if 0 525 #if 0
382 printf("The '%s' key has been %s\n", SDL_GetKeyName(keysym->sym), 526 printf("The '%s' key has been %s\n", SDL_GetKeyName(key),
383 state == SDL_PRESSED ? "pressed" : "released"); 527 state == SDL_PRESSED ? "pressed" : "released");
384 #endif 528 #endif
385 /* Set up the keysym */ 529 repeatable = 0;
386 modstate = (Uint16)SDL_ModState; 530 if (state == SDL_PRESSED) {
387 531 modstate = keyboard->modstate;
388 repeatable = 0; 532 switch (key) {
389 533 case SDLK_UNKNOWN:
390 if ( state == SDL_PRESSED ) { 534 break;
391 keysym->mod = (SDLMod)modstate; 535 case SDLK_NUMLOCK:
392 switch (keysym->sym) { 536 keyboard->modstate ^= KMOD_NUM;
393 case SDLK_UNKNOWN: 537 break;
394 break; 538 case SDLK_CAPSLOCK:
395 case SDLK_NUMLOCK: 539 keyboard->modstate ^= KMOD_CAPS;
396 modstate ^= KMOD_NUM; 540 break;
397 if ( ! (modstate&KMOD_NUM) ) 541 case SDLK_LCTRL:
398 state = SDL_RELEASED; 542 keyboard->modstate |= KMOD_LCTRL;
399 keysym->mod = (SDLMod)modstate; 543 break;
400 break; 544 case SDLK_RCTRL:
401 case SDLK_CAPSLOCK: 545 keyboard->modstate |= KMOD_RCTRL;
402 modstate ^= KMOD_CAPS; 546 break;
403 if ( ! (modstate&KMOD_CAPS) ) 547 case SDLK_LSHIFT:
404 state = SDL_RELEASED; 548 keyboard->modstate |= KMOD_LSHIFT;
405 keysym->mod = (SDLMod)modstate; 549 break;
406 break; 550 case SDLK_RSHIFT:
407 case SDLK_LCTRL: 551 keyboard->modstate |= KMOD_RSHIFT;
408 modstate |= KMOD_LCTRL; 552 break;
409 break; 553 case SDLK_LALT:
410 case SDLK_RCTRL: 554 keyboard->modstate |= KMOD_LALT;
411 modstate |= KMOD_RCTRL; 555 break;
412 break; 556 case SDLK_RALT:
413 case SDLK_LSHIFT: 557 keyboard->modstate |= KMOD_RALT;
414 modstate |= KMOD_LSHIFT; 558 break;
415 break; 559 case SDLK_LMETA:
416 case SDLK_RSHIFT: 560 keyboard->modstate |= KMOD_LMETA;
417 modstate |= KMOD_RSHIFT; 561 break;
418 break; 562 case SDLK_RMETA:
419 case SDLK_LALT: 563 keyboard->modstate |= KMOD_RMETA;
420 modstate |= KMOD_LALT; 564 break;
421 break; 565 case SDLK_MODE:
422 case SDLK_RALT: 566 keyboard->modstate |= KMOD_MODE;
423 modstate |= KMOD_RALT; 567 break;
424 break; 568 default:
425 case SDLK_LMETA: 569 repeatable = 1;
426 modstate |= KMOD_LMETA; 570 break;
427 break; 571 }
428 case SDLK_RMETA: 572 } else {
429 modstate |= KMOD_RMETA; 573 switch (key) {
430 break; 574 case SDLK_UNKNOWN:
431 case SDLK_MODE: 575 break;
432 modstate |= KMOD_MODE; 576 case SDLK_NUMLOCK:
433 break; 577 case SDLK_CAPSLOCK:
434 default: 578 break;
435 repeatable = 1; 579 case SDLK_LCTRL:
436 break; 580 keyboard->modstate &= ~KMOD_LCTRL;
437 } 581 break;
438 } else { 582 case SDLK_RCTRL:
439 switch (keysym->sym) { 583 keyboard->modstate &= ~KMOD_RCTRL;
440 case SDLK_UNKNOWN: 584 break;
441 break; 585 case SDLK_LSHIFT:
442 case SDLK_NUMLOCK: 586 keyboard->modstate &= ~KMOD_LSHIFT;
443 case SDLK_CAPSLOCK: 587 break;
444 /* Only send keydown events */ 588 case SDLK_RSHIFT:
445 return(0); 589 keyboard->modstate &= ~KMOD_RSHIFT;
446 case SDLK_LCTRL: 590 break;
447 modstate &= ~KMOD_LCTRL; 591 case SDLK_LALT:
448 break; 592 keyboard->modstate &= ~KMOD_LALT;
449 case SDLK_RCTRL: 593 break;
450 modstate &= ~KMOD_RCTRL; 594 case SDLK_RALT:
451 break; 595 keyboard->modstate &= ~KMOD_RALT;
452 case SDLK_LSHIFT: 596 break;
453 modstate &= ~KMOD_LSHIFT; 597 case SDLK_LMETA:
454 break; 598 keyboard->modstate &= ~KMOD_LMETA;
455 case SDLK_RSHIFT: 599 break;
456 modstate &= ~KMOD_RSHIFT; 600 case SDLK_RMETA:
457 break; 601 keyboard->modstate &= ~KMOD_RMETA;
458 case SDLK_LALT: 602 break;
459 modstate &= ~KMOD_LALT; 603 case SDLK_MODE:
460 break; 604 keyboard->modstate &= ~KMOD_MODE;
461 case SDLK_RALT: 605 break;
462 modstate &= ~KMOD_RALT; 606 default:
463 break; 607 break;
464 case SDLK_LMETA: 608 }
465 modstate &= ~KMOD_LMETA; 609 modstate = keyboard->modstate;
466 break; 610 }
467 case SDLK_RMETA: 611
468 modstate &= ~KMOD_RMETA; 612 /* Figure out what type of event this is */
469 break; 613 switch (state) {
470 case SDLK_MODE: 614 case SDL_PRESSED:
471 modstate &= ~KMOD_MODE; 615 type = SDL_KEYDOWN;
472 break; 616 break;
473 default: 617 case SDL_RELEASED:
474 break; 618 type = SDL_KEYUP;
475 } 619 /*
476 keysym->mod = (SDLMod)modstate; 620 * jk 991215 - Added
477 } 621 */
478 622 if (keyboard->repeat.timestamp &&
479 /* Figure out what type of event this is */ 623 keyboard->repeat.evt.key.keysym.sym == key) {
480 switch (state) { 624 keyboard->repeat.timestamp = 0;
481 case SDL_PRESSED: 625 }
482 event.type = SDL_KEYDOWN; 626 break;
483 break; 627 default:
484 case SDL_RELEASED: 628 /* Invalid state -- bail */
485 event.type = SDL_KEYUP; 629 return 0;
486 /* 630 }
487 * jk 991215 - Added 631
488 */ 632 if (key != SDLK_UNKNOWN) {
489 if ( SDL_KeyRepeat.timestamp && 633 /* Drop events that don't change state */
490 SDL_KeyRepeat.evt.key.keysym.sym == keysym->sym ) { 634 if (keyboard->keystate[key] == state) {
491 SDL_KeyRepeat.timestamp = 0;
492 }
493 break;
494 default:
495 /* Invalid state -- bail */
496 return(0);
497 }
498
499 if ( keysym->sym != SDLK_UNKNOWN ) {
500 /* Drop events that don't change state */
501 if ( SDL_KeyState[keysym->sym] == state ) {
502 #if 0 635 #if 0
503 printf("Keyboard event didn't change state - dropped!\n"); 636 printf("Keyboard event didn't change state - dropped!\n");
504 #endif 637 #endif
505 return(0); 638 return 0;
506 } 639 }
507 640
508 /* Update internal keyboard state */ 641 /* Update internal keyboard state */
509 SDL_ModState = (SDLMod)modstate; 642 keyboard->keystate[key] = state;
510 SDL_KeyState[keysym->sym] = state; 643 }
511 } 644
512 645 /* Post the event, if desired */
513 /* Post the event, if desired */ 646 posted = 0;
514 posted = 0; 647 if (SDL_ProcessEvents[type] == SDL_ENABLE) {
515 if ( SDL_ProcessEvents[event.type] == SDL_ENABLE ) { 648 SDL_Event event;
516 event.key.state = state; 649 event.key.type = type;
517 event.key.keysym = *keysym; 650 event.key.which = (Uint8) index;
518 /* 651 event.key.state = state;
519 * jk 991215 - Added 652 event.key.keysym.scancode = scancode;
520 */ 653 event.key.keysym.sym = (Uint16) key;
521 if (repeatable && (SDL_KeyRepeat.delay != 0)) { 654 event.key.keysym.mod = modstate;
522 SDL_KeyRepeat.evt = event; 655 event.key.keysym.unicode = 0;
523 SDL_KeyRepeat.firsttime = 1; 656 event.key.windowID = keyboard->focus;
524 SDL_KeyRepeat.timestamp=SDL_GetTicks(); 657 /*
525 } 658 * jk 991215 - Added
526 if ( (SDL_EventOK == NULL) || SDL_EventOK(&event) ) { 659 */
527 posted = 1; 660 if (repeatable && (keyboard->repeat.delay != 0)) {
528 SDL_PushEvent(&event); 661 Uint32 timestamp = SDL_GetTicks();
529 } 662 if (!timestamp) {
530 } 663 timestamp = 1;
531 return(posted); 664 }
665 keyboard->repeat.evt = event;
666 keyboard->repeat.firsttime = 1;
667 keyboard->repeat.timestamp = 1;
668 }
669 posted = (SDL_PushEvent(&event) > 0);
670 }
671 return (posted);
672 }
673
674 int
675 SDL_SendKeyboardText(int index, const char *text)
676 {
677 SDL_Keyboard *keyboard = SDL_GetKeyboard(index);
678 int posted;
679
680 if (!keyboard) {
681 return 0;
682 }
683
684 /* Post the event, if desired */
685 posted = 0;
686 if (SDL_ProcessEvents[SDL_TEXTINPUT] == SDL_ENABLE) {
687 SDL_Event event;
688 event.text.type = SDL_TEXTINPUT;
689 event.text.which = (Uint8) index;
690 SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text));
691 event.key.windowID = keyboard->focus;
692 posted = (SDL_PushEvent(&event) > 0);
693 }
694 return (posted);
532 } 695 }
533 696
534 /* 697 /*
535 * jk 991215 - Added 698 * jk 991215 - Added
536 */ 699 */
537 void SDL_CheckKeyRepeat(void) 700 void
538 { 701 SDL_CheckKeyRepeat(void)
539 if ( SDL_KeyRepeat.timestamp ) { 702 {
540 Uint32 now, interval; 703 int i;
541 704
542 now = SDL_GetTicks(); 705 for (i = 0; i < SDL_num_keyboards; ++i) {
543 interval = (now - SDL_KeyRepeat.timestamp); 706 SDL_Keyboard *keyboard = SDL_keyboards[i];
544 if ( SDL_KeyRepeat.firsttime ) { 707
545 if ( interval > (Uint32)SDL_KeyRepeat.delay ) { 708 if (!keyboard) {
546 SDL_KeyRepeat.timestamp = now; 709 continue;
547 SDL_KeyRepeat.firsttime = 0; 710 }
548 } 711
549 } else { 712 if (keyboard->repeat.timestamp) {
550 if ( interval > (Uint32)SDL_KeyRepeat.interval ) { 713 Uint32 now, interval;
551 SDL_KeyRepeat.timestamp = now; 714
552 if ( (SDL_EventOK == NULL) || SDL_EventOK(&SDL_KeyRepeat.evt) ) { 715 now = SDL_GetTicks();
553 SDL_PushEvent(&SDL_KeyRepeat.evt); 716 interval = (now - keyboard->repeat.timestamp);
554 } 717 if (keyboard->repeat.firsttime) {
555 } 718 if (interval > (Uint32) keyboard->repeat.delay) {
556 } 719 keyboard->repeat.timestamp = now;
557 } 720 keyboard->repeat.firsttime = 0;
558 } 721 }
559 722 } else {
560 int SDL_EnableKeyRepeat(int delay, int interval) 723 if (interval > (Uint32) keyboard->repeat.interval) {
561 { 724 keyboard->repeat.timestamp = now;
562 if ( (delay < 0) || (interval < 0) ) { 725 SDL_PushEvent(&keyboard->repeat.evt);
563 SDL_SetError("keyboard repeat value less than zero"); 726 }
564 return(-1); 727 }
565 } 728 }
566 SDL_KeyRepeat.firsttime = 0; 729 }
567 SDL_KeyRepeat.delay = delay; 730 }
568 SDL_KeyRepeat.interval = interval; 731
569 SDL_KeyRepeat.timestamp = 0; 732 int
570 return(0); 733 SDL_EnableKeyRepeat(int delay, int interval)
571 } 734 {
572 735 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
573 void SDL_GetKeyRepeat(int *delay, int *interval) 736
574 { 737 if (!keyboard) {
575 *delay = SDL_KeyRepeat.delay; 738 SDL_SetError("No keyboard is currently selected");
576 *interval = SDL_KeyRepeat.interval; 739 return -1;
577 } 740 }
578 741
742 if ((delay < 0) || (interval < 0)) {
743 SDL_SetError("keyboard repeat value less than zero");
744 return -1;
745 }
746
747 keyboard->repeat.firsttime = 0;
748 keyboard->repeat.delay = delay;
749 keyboard->repeat.interval = interval;
750 keyboard->repeat.timestamp = 0;
751
752 return 0;
753 }
754
755 void
756 SDL_GetKeyRepeat(int *delay, int *interval)
757 {
758 SDL_Keyboard *keyboard = SDL_GetKeyboard(SDL_current_keyboard);
759
760 if (!keyboard) {
761 if (delay) {
762 *delay = 0;
763 }
764 if (interval) {
765 *interval = 0;
766 }
767 return;
768 }
769 if (delay) {
770 *delay = keyboard->repeat.delay;
771 }
772 if (interval) {
773 *interval = keyboard->repeat.interval;
774 }
775 }
776
777 /* vi: set ts=4 sw=4 expandtab: */