| 1 | /* |
| 2 | SDL - Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2009 Sam Lantinga |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Library General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Library General Public |
| 16 | License along with this library; if not, write to the Free |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | Sam Lantinga |
| 20 | slouken@libsdl.org |
| 21 | */ |
| 22 | #include "SDL_config.h" |
| 23 | |
| 24 | /* |
| 25 | File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability |
| 26 | 27 March 2003 |
| 27 | |
| 28 | Implements keyboard setup, event pump and keyboard and mouse polling |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "SDL.h" |
| 33 | #include "../../timer/SDL_timer_c.h" |
| 34 | #include "../../events/SDL_sysevents.h" |
| 35 | #include "../../events/SDL_events_c.h" |
| 36 | #include "../SDL_cursor_c.h" |
| 37 | #include "SDL_riscosvideo.h" |
| 38 | #include "SDL_riscosevents_c.h" |
| 39 | |
| 40 | #include "memory.h" |
| 41 | #include "stdlib.h" |
| 42 | #include "ctype.h" |
| 43 | |
| 44 | #include "kernel.h" |
| 45 | #include "swis.h" |
| 46 | |
| 47 | /* The translation table from a RISC OS internal key numbers to a SDL keysym */ |
| 48 | static SDLKey RO_keymap[SDLK_LAST]; |
| 49 | |
| 50 | /* RISC OS Key codes */ |
| 51 | #define ROKEY_SHIFT 0 |
| 52 | #define ROKEY_CTRL 1 |
| 53 | #define ROKEY_ALT 2 |
| 54 | /* Left shift is first key we will check for */ |
| 55 | #define ROKEY_LEFT_SHIFT 3 |
| 56 | |
| 57 | /* Need to ignore mouse buttons as they are processed separately */ |
| 58 | #define ROKEY_LEFT_MOUSE 9 |
| 59 | #define ROKEY_CENTRE_MOUSE 10 |
| 60 | #define ROKEY_RIGHT_MOUSE 11 |
| 61 | |
| 62 | /* No key has been pressed return value*/ |
| 63 | #define ROKEY_NONE 255 |
| 64 | |
| 65 | /* Id of last key in keyboard */ |
| 66 | #define ROKEY_LAST_KEY 124 |
| 67 | |
| 68 | /* Size of array for all keys */ |
| 69 | #define ROKEYBD_ARRAYSIZE 125 |
| 70 | |
| 71 | static char RO_pressed[ROKEYBD_ARRAYSIZE]; |
| 72 | |
| 73 | static SDL_keysym *TranslateKey(int intkey, SDL_keysym *keysym, int pressed); |
| 74 | |
| 75 | void RISCOS_PollMouse(_THIS); |
| 76 | void RISCOS_PollKeyboard(); |
| 77 | |
| 78 | void RISCOS_PollMouseHelper(_THIS, int fullscreen); |
| 79 | |
| 80 | #if SDL_THREADS_DISABLED |
| 81 | extern void DRenderer_FillBuffers(); |
| 82 | |
| 83 | /* Timer running function */ |
| 84 | extern void RISCOS_CheckTimer(); |
| 85 | |
| 86 | #endif |
| 87 | |
| 88 | void FULLSCREEN_PumpEvents(_THIS) |
| 89 | { |
| 90 | /* Current implementation requires keyboard and mouse polling */ |
| 91 | RISCOS_PollKeyboard(); |
| 92 | RISCOS_PollMouse(this); |
| 93 | #if SDL_THREADS_DISABLED |
| 94 | // DRenderer_FillBuffers(); |
| 95 | if (SDL_timer_running) RISCOS_CheckTimer(); |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | |
| 100 | void RISCOS_InitOSKeymap(_THIS) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | /* Map the VK keysyms */ |
| 105 | for ( i=0; i<SDL_arraysize(RO_keymap); ++i ) |
| 106 | RO_keymap[i] = SDLK_UNKNOWN; |
| 107 | |
| 108 | RO_keymap[3] = SDLK_LSHIFT; |
| 109 | RO_keymap[4] = SDLK_LCTRL; |
| 110 | RO_keymap[5] = SDLK_LALT; |
| 111 | RO_keymap[6] = SDLK_RSHIFT; |
| 112 | RO_keymap[7] = SDLK_RCTRL; |
| 113 | RO_keymap[8] = SDLK_RALT; |
| 114 | RO_keymap[16] = SDLK_q; |
| 115 | RO_keymap[17] = SDLK_3; |
| 116 | RO_keymap[18] = SDLK_4; |
| 117 | RO_keymap[19] = SDLK_5; |
| 118 | RO_keymap[20] = SDLK_F4; |
| 119 | RO_keymap[21] = SDLK_8; |
| 120 | RO_keymap[22] = SDLK_F7; |
| 121 | RO_keymap[23] = SDLK_MINUS, |
| 122 | RO_keymap[25] = SDLK_LEFT; |
| 123 | RO_keymap[26] = SDLK_KP6; |
| 124 | RO_keymap[27] = SDLK_KP7; |
| 125 | RO_keymap[28] = SDLK_F11; |
| 126 | RO_keymap[29] = SDLK_F12; |
| 127 | RO_keymap[30] = SDLK_F10; |
| 128 | RO_keymap[31] = SDLK_SCROLLOCK; |
| 129 | RO_keymap[32] = SDLK_PRINT; |
| 130 | RO_keymap[33] = SDLK_w; |
| 131 | RO_keymap[34] = SDLK_e; |
| 132 | RO_keymap[35] = SDLK_t; |
| 133 | RO_keymap[36] = SDLK_7; |
| 134 | RO_keymap[37] = SDLK_i; |
| 135 | RO_keymap[38] = SDLK_9; |
| 136 | RO_keymap[39] = SDLK_0; |
| 137 | RO_keymap[41] = SDLK_DOWN; |
| 138 | RO_keymap[42] = SDLK_KP8; |
| 139 | RO_keymap[43] = SDLK_KP9; |
| 140 | RO_keymap[44] = SDLK_BREAK; |
| 141 | RO_keymap[45] = SDLK_BACKQUOTE; |
| 142 | /* RO_keymap[46] = SDLK_currency; TODO: Figure out if this has a value */ |
| 143 | RO_keymap[47] = SDLK_BACKSPACE; |
| 144 | RO_keymap[48] = SDLK_1; |
| 145 | RO_keymap[49] = SDLK_2; |
| 146 | RO_keymap[50] = SDLK_d; |
| 147 | RO_keymap[51] = SDLK_r; |
| 148 | RO_keymap[52] = SDLK_6; |
| 149 | RO_keymap[53] = SDLK_u; |
| 150 | RO_keymap[54] = SDLK_o; |
| 151 | RO_keymap[55] = SDLK_p; |
| 152 | RO_keymap[56] = SDLK_LEFTBRACKET; |
| 153 | RO_keymap[57] = SDLK_UP; |
| 154 | RO_keymap[58] = SDLK_KP_PLUS; |
| 155 | RO_keymap[59] = SDLK_KP_MINUS; |
| 156 | RO_keymap[60] = SDLK_KP_ENTER; |
| 157 | RO_keymap[61] = SDLK_INSERT; |
| 158 | RO_keymap[62] = SDLK_HOME; |
| 159 | RO_keymap[63] = SDLK_PAGEUP; |
| 160 | RO_keymap[64] = SDLK_CAPSLOCK; |
| 161 | RO_keymap[65] = SDLK_a; |
| 162 | RO_keymap[66] = SDLK_x; |
| 163 | RO_keymap[67] = SDLK_f; |
| 164 | RO_keymap[68] = SDLK_y; |
| 165 | RO_keymap[69] = SDLK_j; |
| 166 | RO_keymap[70] = SDLK_k; |
| 167 | RO_keymap[72] = SDLK_SEMICOLON; |
| 168 | RO_keymap[73] = SDLK_RETURN; |
| 169 | RO_keymap[74] = SDLK_KP_DIVIDE; |
| 170 | RO_keymap[76] = SDLK_KP_PERIOD; |
| 171 | RO_keymap[77] = SDLK_NUMLOCK; |
| 172 | RO_keymap[78] = SDLK_PAGEDOWN; |
| 173 | RO_keymap[79] = SDLK_QUOTE; |
| 174 | RO_keymap[81] = SDLK_s; |
| 175 | RO_keymap[82] = SDLK_c; |
| 176 | RO_keymap[83] = SDLK_g; |
| 177 | RO_keymap[84] = SDLK_h; |
| 178 | RO_keymap[85] = SDLK_n; |
| 179 | RO_keymap[86] = SDLK_l; |
| 180 | RO_keymap[87] = SDLK_SEMICOLON; |
| 181 | RO_keymap[88] = SDLK_RIGHTBRACKET; |
| 182 | RO_keymap[89] = SDLK_DELETE; |
| 183 | RO_keymap[90] = SDLK_KP_MINUS; |
| 184 | RO_keymap[91] = SDLK_KP_MULTIPLY; |
| 185 | RO_keymap[93] = SDLK_EQUALS; |
| 186 | RO_keymap[94] = SDLK_BACKSLASH; |
| 187 | RO_keymap[96] = SDLK_TAB; |
| 188 | RO_keymap[97] = SDLK_z; |
| 189 | RO_keymap[98] = SDLK_SPACE; |
| 190 | RO_keymap[99] = SDLK_v; |
| 191 | RO_keymap[100] = SDLK_b; |
| 192 | RO_keymap[101] = SDLK_m; |
| 193 | RO_keymap[102] = SDLK_COMMA; |
| 194 | RO_keymap[103] = SDLK_PERIOD; |
| 195 | RO_keymap[104] = SDLK_SLASH; |
| 196 | RO_keymap[105] = SDLK_END; |
| 197 | RO_keymap[106] = SDLK_KP0; |
| 198 | RO_keymap[107] = SDLK_KP1; |
| 199 | RO_keymap[108] = SDLK_KP3; |
| 200 | RO_keymap[112] = SDLK_ESCAPE; |
| 201 | RO_keymap[113] = SDLK_F1; |
| 202 | RO_keymap[114] = SDLK_F2; |
| 203 | RO_keymap[115] = SDLK_F3; |
| 204 | RO_keymap[116] = SDLK_F5; |
| 205 | RO_keymap[117] = SDLK_F6; |
| 206 | RO_keymap[118] = SDLK_F8; |
| 207 | RO_keymap[119] = SDLK_F9; |
| 208 | RO_keymap[120] = SDLK_HASH; |
| 209 | RO_keymap[121] = SDLK_RIGHT; |
| 210 | RO_keymap[122] = SDLK_KP4; |
| 211 | RO_keymap[123] = SDLK_KP5; |
| 212 | RO_keymap[124] = SDLK_KP2; |
| 213 | |
| 214 | SDL_memset(RO_pressed, 0, ROKEYBD_ARRAYSIZE); |
| 215 | } |
| 216 | |
| 217 | |
| 218 | /* Variable for mouse relative processing */ |
| 219 | int mouse_relative = 0; |
| 220 | |
| 221 | /* Check to see if we need to enter or leave mouse relative mode */ |
| 222 | |
| 223 | void RISCOS_CheckMouseMode(_THIS) |
| 224 | { |
| 225 | /* If the mouse is hidden and input is grabbed, we use relative mode */ |
| 226 | if ( !(SDL_cursorstate & CURSOR_VISIBLE) && |
| 227 | (this->input_grab != SDL_GRAB_OFF) ) { |
| 228 | mouse_relative = 1; |
| 229 | } else { |
| 230 | mouse_relative = 0; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | void RISCOS_PollMouse(_THIS) |
| 236 | { |
| 237 | RISCOS_PollMouseHelper(this, 1); |
| 238 | } |
| 239 | |
| 240 | extern int mouseInWindow; |
| 241 | |
| 242 | void WIMP_PollMouse(_THIS) |
| 243 | { |
| 244 | /* Only poll when mouse is over the window */ |
| 245 | if (!mouseInWindow) return; |
| 246 | |
| 247 | RISCOS_PollMouseHelper(this, 0); |
| 248 | } |
| 249 | |
| 250 | /* Static variables so only changes are reported */ |
| 251 | static Sint16 last_x = -1, last_y = -1; |
| 252 | static int last_buttons = 0; |
| 253 | |
| 254 | /* Share routine between WIMP and FULLSCREEN for polling mouse and |
| 255 | passing on events */ |
| 256 | void RISCOS_PollMouseHelper(_THIS, int fullscreen) |
| 257 | { |
| 258 | _kernel_swi_regs regs; |
| 259 | static int starting = 1; |
| 260 | |
| 261 | if (_kernel_swi(OS_Mouse, ®s, ®s) == NULL) |
| 262 | { |
| 263 | Sint16 new_x = regs.r[0]; /* Initialy get as OS units */ |
| 264 | Sint16 new_y = regs.r[1]; |
| 265 | |
| 266 | /* Discard mouse events until they let go of the mouse after starting */ |
| 267 | if (starting && regs.r[2] != 0) |
| 268 | return; |
| 269 | else |
| 270 | starting = 0; |
| 271 | |
| 272 | if (new_x != last_x || new_y != last_y || last_buttons != regs.r[2]) |
| 273 | { |
| 274 | /* Something changed so generate appropriate events */ |
| 275 | int topLeftX, topLeftY; /* Top left OS units */ |
| 276 | int x, y; /* Mouse position in SDL pixels */ |
| 277 | |
| 278 | if (fullscreen) |
| 279 | { |
| 280 | topLeftX = 0; |
| 281 | topLeftY = (this->hidden->height << this->hidden->yeig) - 1; |
| 282 | } else |
| 283 | { |
| 284 | int window_state[9]; |
| 285 | |
| 286 | /* Get current window state */ |
| 287 | window_state[0] = this->hidden->window_handle; |
| 288 | regs.r[1] = (unsigned int)window_state; |
| 289 | _kernel_swi(Wimp_GetWindowState, ®s, ®s); |
| 290 | |
| 291 | topLeftX = window_state[1]; |
| 292 | topLeftY = window_state[4]; |
| 293 | } |
| 294 | |
| 295 | /* Convert co-ordinates to workspace */ |
| 296 | x = new_x - topLeftX; |
| 297 | y = topLeftY - new_y; /* Y goes from top of window/screen */ |
| 298 | |
| 299 | /* Convert OS units to pixels */ |
| 300 | x >>= this->hidden->xeig; |
| 301 | y >>= this->hidden->yeig; |
| 302 | |
| 303 | if (last_x != new_x || last_y != new_y) |
| 304 | { |
| 305 | if (mouse_relative) |
| 306 | { |
| 307 | int centre_x = SDL_VideoSurface->w/2; |
| 308 | int centre_y = SDL_VideoSurface->h/2; |
| 309 | |
| 310 | if (centre_x != x || centre_y != y) |
| 311 | { |
| 312 | if (SDL_VideoSurface) SDL_PrivateMouseMotion(0,1,x - centre_x, y - centre_y); |
| 313 | last_x = topLeftX + (centre_x << this->hidden->xeig); |
| 314 | last_y = topLeftY - (centre_y << this->hidden->yeig); |
| 315 | |
| 316 | /* Re-centre the mouse pointer, so we still get relative |
| 317 | movement when the mouse is at the edge of the window |
| 318 | or screen. |
| 319 | */ |
| 320 | { |
| 321 | unsigned char block[5]; |
| 322 | |
| 323 | block[0] = 3; /* OSWORD move pointer sub-reason code */ |
| 324 | block[1] = last_x & 0xFF; |
| 325 | block[2] = (last_x >> 8) & 0xFF; |
| 326 | block[3] = last_y & 0xFF; |
| 327 | block[4] = (last_y >> 8) & 0xFF; |
| 328 | |
| 329 | regs.r[0] = 21; /* OSWORD pointer stuff code */ |
| 330 | regs.r[1] = (int)block; |
| 331 | _kernel_swi(OS_Word, ®s, ®s); |
| 332 | } |
| 333 | } |
| 334 | } else |
| 335 | { |
| 336 | last_x = new_x; |
| 337 | last_y = new_y; |
| 338 | SDL_PrivateMouseMotion(0,0,x,y); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (last_buttons != regs.r[2]) |
| 343 | { |
| 344 | int changed = last_buttons ^ regs.r[2]; |
| 345 | last_buttons = regs.r[2]; |
| 346 | if (changed & 4) SDL_PrivateMouseButton((last_buttons & 4) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_LEFT, 0, 0); |
| 347 | if (changed & 2) SDL_PrivateMouseButton((last_buttons & 2) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_MIDDLE, 0, 0); |
| 348 | if (changed & 1) SDL_PrivateMouseButton((last_buttons & 1) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_RIGHT, 0, 0); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void RISCOS_PollKeyboard() |
| 355 | { |
| 356 | int which_key = ROKEY_LEFT_SHIFT; |
| 357 | int j; |
| 358 | int min_key, max_key; |
| 359 | SDL_keysym key; |
| 360 | |
| 361 | /* Scan the keyboard to see what is pressed */ |
| 362 | while (which_key <= ROKEY_LAST_KEY) |
| 363 | { |
| 364 | which_key = (_kernel_osbyte(121, which_key, 0) & 0xFF); |
| 365 | if (which_key != ROKEY_NONE) |
| 366 | { |
| 367 | switch(which_key) |
| 368 | { |
| 369 | /* Skip over mouse keys */ |
| 370 | case ROKEY_LEFT_MOUSE: |
| 371 | case ROKEY_CENTRE_MOUSE: |
| 372 | case ROKEY_RIGHT_MOUSE: |
| 373 | which_key = ROKEY_RIGHT_MOUSE; |
| 374 | break; |
| 375 | |
| 376 | /* Ignore keys that cause 2 internal number to be generated */ |
| 377 | case 71: case 24: case 87: case 40: |
| 378 | break; |
| 379 | |
| 380 | /* Ignore break as it can be latched on */ |
| 381 | case 44: |
| 382 | break; |
| 383 | |
| 384 | default: |
| 385 | RO_pressed[which_key] += 2; |
| 386 | break; |
| 387 | } |
| 388 | which_key++; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | /* Generate key released messages */ |
| 393 | min_key = ROKEY_LAST_KEY+1; |
| 394 | max_key = ROKEY_LEFT_SHIFT; |
| 395 | |
| 396 | for (j = ROKEY_LEFT_SHIFT; j <= ROKEY_LAST_KEY; j++) |
| 397 | { |
| 398 | if (RO_pressed[j]) |
| 399 | { |
| 400 | if (RO_pressed[j] == 1) |
| 401 | { |
| 402 | RO_pressed[j] = 0; |
| 403 | SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(j,&key,0)); |
| 404 | } else |
| 405 | { |
| 406 | if (j < min_key) min_key = j; |
| 407 | if (j > max_key) max_key = j; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* Generate key pressed messages */ |
| 413 | for (j = min_key; j <= max_key; j++) |
| 414 | { |
| 415 | if (RO_pressed[j]) |
| 416 | { |
| 417 | if (RO_pressed[j] == 2) |
| 418 | { |
| 419 | SDL_PrivateKeyboard(SDL_PRESSED,TranslateKey(j,&key,1)); |
| 420 | } |
| 421 | RO_pressed[j] = 1; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | static SDL_keysym *TranslateKey(int intkey, SDL_keysym *keysym, int pressed) |
| 427 | { |
| 428 | /* Set the keysym information */ |
| 429 | keysym->scancode = (unsigned char) intkey; |
| 430 | keysym->sym = RO_keymap[intkey]; |
| 431 | keysym->mod = KMOD_NONE; |
| 432 | keysym->unicode = 0; |
| 433 | if ( pressed && SDL_TranslateUNICODE ) |
| 434 | { |
| 435 | int state; |
| 436 | int ch; |
| 437 | |
| 438 | state = (_kernel_osbyte(202, 0, 255) & 0xFF); |
| 439 | |
| 440 | /*TODO: Take into account other keyboard layouts */ |
| 441 | |
| 442 | ch = keysym->sym; /* This should handle most unshifted keys */ |
| 443 | |
| 444 | if (intkey < 9 || ch == SDLK_UNKNOWN) |
| 445 | { |
| 446 | ch = 0; |
| 447 | |
| 448 | } else if (state & 64) /* Control on */ |
| 449 | { |
| 450 | ch = ch & 31; |
| 451 | |
| 452 | } else |
| 453 | { |
| 454 | int topOfKey = 0; |
| 455 | if (state & 8) /* Shift on */ |
| 456 | { |
| 457 | topOfKey = 1; |
| 458 | } |
| 459 | |
| 460 | if ((state & 16) == 0) /* Caps lock is on */ |
| 461 | { |
| 462 | if (ch >= SDLK_a && ch <= SDLK_z) |
| 463 | { |
| 464 | if ((state & 128) == 0) /* Shift Enable off */ |
| 465 | { |
| 466 | /* All letter become upper case */ |
| 467 | topOfKey = 1; |
| 468 | } else |
| 469 | { |
| 470 | /* Shift+Letters gives lower case */ |
| 471 | topOfKey = 1 - topOfKey; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (topOfKey) |
| 477 | { |
| 478 | /* Key produced with shift held down */ |
| 479 | |
| 480 | /* Letters just give upper case version */ |
| 481 | if (ch >= SDLK_a && ch <= SDLK_z) ch = toupper(ch); |
| 482 | else |
| 483 | { |
| 484 | switch(ch) |
| 485 | { |
| 486 | case SDLK_HASH: ch = '~'; break; |
| 487 | case SDLK_QUOTE: ch = '@'; break; |
| 488 | case SDLK_COMMA: ch = '<'; break; |
| 489 | case SDLK_MINUS: ch = '_'; break; |
| 490 | case SDLK_PERIOD: ch = '>'; break; |
| 491 | case SDLK_SLASH: ch = '?'; break; |
| 492 | |
| 493 | case SDLK_0: ch = ')'; break; |
| 494 | case SDLK_1: ch = '!'; break; |
| 495 | case SDLK_2: ch = '"'; break; |
| 496 |