Implement keyboard input on RISC OS Partially based on a patch from http://www.riscos.info/websvn/listing.php?repname=gccsdk&path=%2Ftrunk%2Fautobuilder%2Flibraries%2Fsdl%2Flibsdl2%2F&rev=7174#a6401c766f408f1ea356e6977894cc6a5 Currently lacks support for mapping scancodes to keycodes.
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
diff --git a/src/video/riscos/SDL_riscosevents.c b/src/video/riscos/SDL_riscosevents.c
index 3c428f5..b571d75 100644
--- a/src/video/riscos/SDL_riscosevents.c
+++ b/src/video/riscos/SDL_riscosevents.c
@@ -22,18 +22,118 @@
#if SDL_VIDEO_DRIVER_RISCOS
-/* Being a null driver, there's no event stream. We just define stubs for
- most of the API. */
-
#include "../../events/SDL_events_c.h"
+#include "SDL_log.h"
#include "SDL_riscosvideo.h"
#include "SDL_riscosevents_c.h"
+#include "scancodes_riscos.h"
+
+#include <kernel.h>
+
+static SDL_Scancode
+SDL_RISCOS_translate_keycode(int keycode)
+{
+ SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
+
+ if (keycode < SDL_arraysize(riscos_scancode_table)) {
+ scancode = riscos_scancode_table[keycode];
+
+ if (scancode == SDL_SCANCODE_UNKNOWN) {
+ SDL_Log("The key you just pressed is not recognized by SDL: %d", keycode);
+ }
+ }
+
+ return scancode;
+}
+
+void
+RISCOS_PollKeyboard(_THIS)
+{
+ SDL_VideoData *driverdata = (SDL_VideoData *)_this->driverdata;
+ Uint8 key = 2;
+ int i;
+
+ /* Check for key releases */
+ for (i = 0; i < RISCOS_MAX_KEYS_PRESSED; i++) {
+ if (driverdata->key_pressed[i] != 255) {
+ if ((_kernel_osbyte(129, driverdata->key_pressed[i] ^ 0xff, 0xff) & 0xff) != 255) {
+ SDL_SendKeyboardKey(SDL_RELEASED, SDL_RISCOS_translate_keycode(driverdata->key_pressed[i]));
+ driverdata->key_pressed[i] = 255;
+ }
+ }
+ }
+
+ /* Check for key presses */
+ while (key < 0xff) {
+ SDL_bool already_pressed = SDL_FALSE;
+ key = _kernel_osbyte(121, key + 1, 0) & 0xff;
+ switch (key) {
+ case 255:
+ /* Ignore mouse keys */
+ case 9:
+ case 10:
+ case 11:
+ /* Ignore keys with multiple INKEY codes */
+ case 24:
+ case 40:
+ case 71:
+ case 87:
+ break;
+
+ default:
+ /* Do we already know of this key? */
+ for (i = 0; i < RISCOS_MAX_KEYS_PRESSED; i++) {
+ if (driverdata->key_pressed[i] == key) {
+ already_pressed = SDL_TRUE;
+ break;
+ }
+ }
+
+ if (!already_pressed) {
+ SDL_SendKeyboardKey(SDL_PRESSED, SDL_RISCOS_translate_keycode(key));
+ /* Record the press so we can detect release later. */
+ for (i = 0; i < RISCOS_MAX_KEYS_PRESSED; i++) {
+ if (driverdata->key_pressed[i] == 255) {
+ driverdata->key_pressed[i] = key;
+ break;
+ }
+ }
+ }
+ }
+ }
+}
+
+int
+RISCOS_InitEvents(_THIS)
+{
+ SDL_VideoData *driverdata = (SDL_VideoData *) _this->driverdata;
+ int i, status;
+
+ for (i = 0; i < RISCOS_MAX_KEYS_PRESSED; i++)
+ driverdata->key_pressed[i] = 255;
+
+ status = (_kernel_osbyte(202, 0, 255) & 0xFF);
+ SDL_ToggleModState(KMOD_NUM, (status & (1 << 2)) == 0);
+ SDL_ToggleModState(KMOD_CAPS, (status & (1 << 4)) == 0);
+
+ /* Disable escape. */
+ _kernel_osbyte(229, 1, 0);
+
+ return 0;
+}
void
RISCOS_PumpEvents(_THIS)
{
- /* do nothing. */
+ RISCOS_PollKeyboard(_this);
+}
+
+void
+RISCOS_QuitEvents(_THIS)
+{
+ /* Re-enable escape. */
+ _kernel_osbyte(229, 0, 0);
}
#endif /* SDL_VIDEO_DRIVER_RISCOS */
diff --git a/src/video/riscos/SDL_riscosevents_c.h b/src/video/riscos/SDL_riscosevents_c.h
index 373bb72..204ee34 100644
--- a/src/video/riscos/SDL_riscosevents_c.h
+++ b/src/video/riscos/SDL_riscosevents_c.h
@@ -26,7 +26,9 @@
#include "SDL_riscosvideo.h"
+extern int RISCOS_InitEvents(_THIS);
extern void RISCOS_PumpEvents(_THIS);
+extern void RISCOS_QuitEvents(_THIS);
#endif /* SDL_riscosevents_c_h_ */
diff --git a/src/video/riscos/SDL_riscosvideo.c b/src/video/riscos/SDL_riscosvideo.c
index 5907944..5b481e6 100644
--- a/src/video/riscos/SDL_riscosvideo.c
+++ b/src/video/riscos/SDL_riscosvideo.c
@@ -45,6 +45,7 @@ static void RISCOS_VideoQuit(_THIS);
static void
RISCOS_DeleteDevice(SDL_VideoDevice * device)
{
+ SDL_free(device->driverdata);
SDL_free(device);
}
@@ -52,6 +53,7 @@ static SDL_VideoDevice *
RISCOS_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
+ SDL_VideoData *phdata;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
@@ -60,6 +62,16 @@ RISCOS_CreateDevice(int devindex)
return (0);
}
+ /* Initialize internal data */
+ phdata = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
+ if (phdata == NULL) {
+ SDL_OutOfMemory();
+ SDL_free(device);
+ return NULL;
+ }
+
+ device->driverdata = phdata;
+
/* Set the function pointers */
device->VideoInit = RISCOS_VideoInit;
device->VideoQuit = RISCOS_VideoQuit;
@@ -89,6 +101,10 @@ VideoBootStrap RISCOS_bootstrap = {
static int
RISCOS_VideoInit(_THIS)
{
+ if (RISCOS_InitEvents(_this) < 0) {
+ return -1;
+ }
+
if (RISCOS_InitModes(_this) < 0) {
return -1;
}
@@ -100,6 +116,7 @@ RISCOS_VideoInit(_THIS)
static void
RISCOS_VideoQuit(_THIS)
{
+ RISCOS_QuitEvents(_this);
}
#endif /* SDL_VIDEO_DRIVER_RISCOS */
diff --git a/src/video/riscos/SDL_riscosvideo.h b/src/video/riscos/SDL_riscosvideo.h
index 5bf4b14..7be93af 100644
--- a/src/video/riscos/SDL_riscosvideo.h
+++ b/src/video/riscos/SDL_riscosvideo.h
@@ -25,6 +25,13 @@
#include "../SDL_sysvideo.h"
+#define RISCOS_MAX_KEYS_PRESSED 6
+
+typedef struct SDL_VideoData
+{
+ Uint8 key_pressed[RISCOS_MAX_KEYS_PRESSED];
+} SDL_VideoData;
+
#endif /* SDL_riscosvideo_h_ */
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/riscos/scancodes_riscos.h b/src/video/riscos/scancodes_riscos.h
new file mode 100644
index 0000000..23d77e2
--- /dev/null
+++ b/src/video/riscos/scancodes_riscos.h
@@ -0,0 +1,158 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_scancode.h"
+
+/* RISC OS key code to SDL_Keycode mapping table
+ Sources:
+ - https://www.riscosopen.org/wiki/documentation/show/Keyboard Scan Codes
+*/
+/* *INDENT-OFF* */
+static SDL_Scancode const riscos_scancode_table[] = {
+ /* 0 */ SDL_SCANCODE_UNKNOWN, /* Shift */
+ /* 1 */ SDL_SCANCODE_UNKNOWN, /* Ctrl */
+ /* 2 */ SDL_SCANCODE_UNKNOWN, /* Alt */
+ /* 3 */ SDL_SCANCODE_LSHIFT,
+ /* 4 */ SDL_SCANCODE_LCTRL,
+ /* 5 */ SDL_SCANCODE_LALT,
+ /* 6 */ SDL_SCANCODE_RSHIFT,
+ /* 7 */ SDL_SCANCODE_RCTRL,
+ /* 8 */ SDL_SCANCODE_RALT,
+ /* 9 */ SDL_SCANCODE_UNKNOWN, /* Left mouse */
+ /* 10 */ SDL_SCANCODE_UNKNOWN, /* Center mouse */
+ /* 11 */ SDL_SCANCODE_UNKNOWN, /* Right mouse */
+ /* 12 */ SDL_SCANCODE_UNKNOWN,
+ /* 13 */ SDL_SCANCODE_UNKNOWN,
+ /* 14 */ SDL_SCANCODE_UNKNOWN,
+ /* 15 */ SDL_SCANCODE_UNKNOWN,
+ /* 16 */ SDL_SCANCODE_Q,
+ /* 17 */ SDL_SCANCODE_3,
+ /* 18 */ SDL_SCANCODE_4,
+ /* 19 */ SDL_SCANCODE_5,
+ /* 20 */ SDL_SCANCODE_F4,
+ /* 21 */ SDL_SCANCODE_8,
+ /* 22 */ SDL_SCANCODE_F7,
+ /* 23 */ SDL_SCANCODE_MINUS,
+ /* 24 */ SDL_SCANCODE_6, /* Duplicate of 52 */
+ /* 25 */ SDL_SCANCODE_LEFT,
+ /* 26 */ SDL_SCANCODE_KP_6,
+ /* 27 */ SDL_SCANCODE_KP_7,
+ /* 28 */ SDL_SCANCODE_F11,
+ /* 29 */ SDL_SCANCODE_F12,
+ /* 30 */ SDL_SCANCODE_F10,
+ /* 31 */ SDL_SCANCODE_SCROLLLOCK,
+ /* 32 */ SDL_SCANCODE_PRINTSCREEN,
+ /* 33 */ SDL_SCANCODE_W,
+ /* 34 */ SDL_SCANCODE_E,
+ /* 35 */ SDL_SCANCODE_T,
+ /* 36 */ SDL_SCANCODE_7,
+ /* 37 */ SDL_SCANCODE_I,
+ /* 38 */ SDL_SCANCODE_9,
+ /* 39 */ SDL_SCANCODE_0,
+ /* 40 */ SDL_SCANCODE_MINUS, /* Duplicate of 23 */
+ /* 41 */ SDL_SCANCODE_DOWN,
+ /* 42 */ SDL_SCANCODE_KP_8,
+ /* 43 */ SDL_SCANCODE_KP_9,
+ /* 44 */ SDL_SCANCODE_PAUSE,
+ /* 45 */ SDL_SCANCODE_GRAVE,
+ /* 46 */ SDL_SCANCODE_CURRENCYUNIT,
+ /* 47 */ SDL_SCANCODE_BACKSPACE,
+ /* 48 */ SDL_SCANCODE_1,
+ /* 49 */ SDL_SCANCODE_2,
+ /* 50 */ SDL_SCANCODE_D,
+ /* 51 */ SDL_SCANCODE_R,
+ /* 52 */ SDL_SCANCODE_6,
+ /* 53 */ SDL_SCANCODE_U,
+ /* 54 */ SDL_SCANCODE_O,
+ /* 55 */ SDL_SCANCODE_P,
+ /* 56 */ SDL_SCANCODE_LEFTBRACKET,
+ /* 57 */ SDL_SCANCODE_UP,
+ /* 58 */ SDL_SCANCODE_KP_PLUS,
+ /* 59 */ SDL_SCANCODE_KP_MINUS,
+ /* 60 */ SDL_SCANCODE_KP_ENTER,
+ /* 61 */ SDL_SCANCODE_INSERT,
+ /* 62 */ SDL_SCANCODE_HOME,
+ /* 63 */ SDL_SCANCODE_PAGEUP,
+ /* 64 */ SDL_SCANCODE_CAPSLOCK,
+ /* 65 */ SDL_SCANCODE_A,
+ /* 66 */ SDL_SCANCODE_X,
+ /* 67 */ SDL_SCANCODE_F,
+ /* 68 */ SDL_SCANCODE_Y,
+ /* 69 */ SDL_SCANCODE_J,
+ /* 70 */ SDL_SCANCODE_K,
+ /* 71 */ SDL_SCANCODE_2, /* Duplicate of 49 */
+ /* 72 */ SDL_SCANCODE_SEMICOLON, /* Duplicate of 87 */
+ /* 73 */ SDL_SCANCODE_RETURN,
+ /* 74 */ SDL_SCANCODE_KP_DIVIDE,
+ /* 75 */ SDL_SCANCODE_UNKNOWN,
+ /* 76 */ SDL_SCANCODE_KP_PERIOD,
+ /* 77 */ SDL_SCANCODE_NUMLOCKCLEAR,
+ /* 78 */ SDL_SCANCODE_PAGEDOWN,
+ /* 79 */ SDL_SCANCODE_APOSTROPHE,
+ /* 80 */ SDL_SCANCODE_UNKNOWN,
+ /* 81 */ SDL_SCANCODE_S,
+ /* 82 */ SDL_SCANCODE_C,
+ /* 83 */ SDL_SCANCODE_G,
+ /* 84 */ SDL_SCANCODE_H,
+ /* 85 */ SDL_SCANCODE_N,
+ /* 86 */ SDL_SCANCODE_L,
+ /* 87 */ SDL_SCANCODE_SEMICOLON,
+ /* 88 */ SDL_SCANCODE_RIGHTBRACKET,
+ /* 89 */ SDL_SCANCODE_DELETE,
+ /* 90 */ SDL_SCANCODE_KP_HASH,
+ /* 91 */ SDL_SCANCODE_KP_MULTIPLY,
+ /* 92 */ SDL_SCANCODE_UNKNOWN,
+ /* 93 */ SDL_SCANCODE_EQUALS,
+ /* 94 */ SDL_SCANCODE_NONUSBACKSLASH,
+ /* 95 */ SDL_SCANCODE_UNKNOWN,
+ /* 96 */ SDL_SCANCODE_TAB,
+ /* 97 */ SDL_SCANCODE_Z,
+ /* 98 */ SDL_SCANCODE_SPACE,
+ /* 99 */ SDL_SCANCODE_V,
+ /* 100 */ SDL_SCANCODE_B,
+ /* 101 */ SDL_SCANCODE_M,
+ /* 102 */ SDL_SCANCODE_COMMA,
+ /* 103 */ SDL_SCANCODE_PERIOD,
+ /* 104 */ SDL_SCANCODE_SLASH,
+ /* 105 */ SDL_SCANCODE_END,
+ /* 106 */ SDL_SCANCODE_KP_0,
+ /* 107 */ SDL_SCANCODE_KP_1,
+ /* 108 */ SDL_SCANCODE_KP_3,
+ /* 109 */ SDL_SCANCODE_UNKNOWN,
+ /* 110 */ SDL_SCANCODE_UNKNOWN,
+ /* 111 */ SDL_SCANCODE_UNKNOWN,
+ /* 112 */ SDL_SCANCODE_ESCAPE,
+ /* 113 */ SDL_SCANCODE_F1,
+ /* 114 */ SDL_SCANCODE_F2,
+ /* 115 */ SDL_SCANCODE_F3,
+ /* 116 */ SDL_SCANCODE_F5,
+ /* 117 */ SDL_SCANCODE_F6,
+ /* 118 */ SDL_SCANCODE_F8,
+ /* 119 */ SDL_SCANCODE_F9,
+ /* 120 */ SDL_SCANCODE_BACKSLASH,
+ /* 121 */ SDL_SCANCODE_RIGHT,
+ /* 122 */ SDL_SCANCODE_KP_4,
+ /* 123 */ SDL_SCANCODE_KP_5,
+ /* 124 */ SDL_SCANCODE_KP_2,
+ /* 125 */ SDL_SCANCODE_LGUI,
+ /* 126 */ SDL_SCANCODE_RGUI,
+ /* 127 */ SDL_SCANCODE_MENU
+};
+/* *INDENT-ON* */