src/video/riscos/SDL_riscosevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1402 d910939febfa
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 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,
    74                                  int pressed);
    75 
    76 void RISCOS_PollMouse (_THIS);
    77 void RISCOS_PollKeyboard ();
    78 
    79 void RISCOS_PollMouseHelper (_THIS, int fullscreen);
    80 
    81 #if SDL_THREADS_DISABLED
    82 extern void DRenderer_FillBuffers ();
    83 
    84 /* Timer running function */
    85 extern void RISCOS_CheckTimer ();
    86 
    87 #endif
    88 
    89 void
    90 FULLSCREEN_PumpEvents (_THIS)
    91 {
    92     /* Current implementation requires keyboard and mouse polling */
    93     RISCOS_PollKeyboard ();
    94     RISCOS_PollMouse (this);
    95 #if SDL_THREADS_DISABLED
    96 //      DRenderer_FillBuffers();
    97     if (SDL_timer_running)
    98         RISCOS_CheckTimer ();
    99 #endif
   100 }
   101 
   102 
   103 void
   104 RISCOS_InitOSKeymap (_THIS)
   105 {
   106     int i;
   107 
   108     /* Map the VK keysyms */
   109     for (i = 0; i < SDL_arraysize (RO_keymap); ++i)
   110         RO_keymap[i] = SDLK_UNKNOWN;
   111 
   112     RO_keymap[3] = SDLK_LSHIFT;
   113     RO_keymap[4] = SDLK_LCTRL;
   114     RO_keymap[5] = SDLK_LALT;
   115     RO_keymap[6] = SDLK_RSHIFT;
   116     RO_keymap[7] = SDLK_RCTRL;
   117     RO_keymap[8] = SDLK_RALT;
   118     RO_keymap[16] = SDLK_q;
   119     RO_keymap[17] = SDLK_3;
   120     RO_keymap[18] = SDLK_4;
   121     RO_keymap[19] = SDLK_5;
   122     RO_keymap[20] = SDLK_F4;
   123     RO_keymap[21] = SDLK_8;
   124     RO_keymap[22] = SDLK_F7;
   125     RO_keymap[23] = SDLK_MINUS, RO_keymap[25] = SDLK_LEFT;
   126     RO_keymap[26] = SDLK_KP6;
   127     RO_keymap[27] = SDLK_KP7;
   128     RO_keymap[28] = SDLK_F11;
   129     RO_keymap[29] = SDLK_F12;
   130     RO_keymap[30] = SDLK_F10;
   131     RO_keymap[31] = SDLK_SCROLLOCK;
   132     RO_keymap[32] = SDLK_PRINT;
   133     RO_keymap[33] = SDLK_w;
   134     RO_keymap[34] = SDLK_e;
   135     RO_keymap[35] = SDLK_t;
   136     RO_keymap[36] = SDLK_7;
   137     RO_keymap[37] = SDLK_i;
   138     RO_keymap[38] = SDLK_9;
   139     RO_keymap[39] = SDLK_0;
   140     RO_keymap[41] = SDLK_DOWN;
   141     RO_keymap[42] = SDLK_KP8;
   142     RO_keymap[43] = SDLK_KP9;
   143     RO_keymap[44] = SDLK_BREAK;
   144     RO_keymap[45] = SDLK_BACKQUOTE;
   145 /*  RO_keymap[46] = SDLK_currency; TODO: Figure out if this has a value */
   146     RO_keymap[47] = SDLK_BACKSPACE;
   147     RO_keymap[48] = SDLK_1;
   148     RO_keymap[49] = SDLK_2;
   149     RO_keymap[50] = SDLK_d;
   150     RO_keymap[51] = SDLK_r;
   151     RO_keymap[52] = SDLK_6;
   152     RO_keymap[53] = SDLK_u;
   153     RO_keymap[54] = SDLK_o;
   154     RO_keymap[55] = SDLK_p;
   155     RO_keymap[56] = SDLK_LEFTBRACKET;
   156     RO_keymap[57] = SDLK_UP;
   157     RO_keymap[58] = SDLK_KP_PLUS;
   158     RO_keymap[59] = SDLK_KP_MINUS;
   159     RO_keymap[60] = SDLK_KP_ENTER;
   160     RO_keymap[61] = SDLK_INSERT;
   161     RO_keymap[62] = SDLK_HOME;
   162     RO_keymap[63] = SDLK_PAGEUP;
   163     RO_keymap[64] = SDLK_CAPSLOCK;
   164     RO_keymap[65] = SDLK_a;
   165     RO_keymap[66] = SDLK_x;
   166     RO_keymap[67] = SDLK_f;
   167     RO_keymap[68] = SDLK_y;
   168     RO_keymap[69] = SDLK_j;
   169     RO_keymap[70] = SDLK_k;
   170     RO_keymap[72] = SDLK_SEMICOLON;
   171     RO_keymap[73] = SDLK_RETURN;
   172     RO_keymap[74] = SDLK_KP_DIVIDE;
   173     RO_keymap[76] = SDLK_KP_PERIOD;
   174     RO_keymap[77] = SDLK_NUMLOCK;
   175     RO_keymap[78] = SDLK_PAGEDOWN;
   176     RO_keymap[79] = SDLK_QUOTE;
   177     RO_keymap[81] = SDLK_s;
   178     RO_keymap[82] = SDLK_c;
   179     RO_keymap[83] = SDLK_g;
   180     RO_keymap[84] = SDLK_h;
   181     RO_keymap[85] = SDLK_n;
   182     RO_keymap[86] = SDLK_l;
   183     RO_keymap[87] = SDLK_SEMICOLON;
   184     RO_keymap[88] = SDLK_RIGHTBRACKET;
   185     RO_keymap[89] = SDLK_DELETE;
   186     RO_keymap[90] = SDLK_KP_MINUS;
   187     RO_keymap[91] = SDLK_KP_MULTIPLY;
   188     RO_keymap[93] = SDLK_EQUALS;
   189     RO_keymap[94] = SDLK_BACKSLASH;
   190     RO_keymap[96] = SDLK_TAB;
   191     RO_keymap[97] = SDLK_z;
   192     RO_keymap[98] = SDLK_SPACE;
   193     RO_keymap[99] = SDLK_v;
   194     RO_keymap[100] = SDLK_b;
   195     RO_keymap[101] = SDLK_m;
   196     RO_keymap[102] = SDLK_COMMA;
   197     RO_keymap[103] = SDLK_PERIOD;
   198     RO_keymap[104] = SDLK_SLASH;
   199     RO_keymap[105] = SDLK_END;
   200     RO_keymap[106] = SDLK_KP0;
   201     RO_keymap[107] = SDLK_KP1;
   202     RO_keymap[108] = SDLK_KP3;
   203     RO_keymap[112] = SDLK_ESCAPE;
   204     RO_keymap[113] = SDLK_F1;
   205     RO_keymap[114] = SDLK_F2;
   206     RO_keymap[115] = SDLK_F3;
   207     RO_keymap[116] = SDLK_F5;
   208     RO_keymap[117] = SDLK_F6;
   209     RO_keymap[118] = SDLK_F8;
   210     RO_keymap[119] = SDLK_F9;
   211     RO_keymap[120] = SDLK_HASH;
   212     RO_keymap[121] = SDLK_RIGHT;
   213     RO_keymap[122] = SDLK_KP4;
   214     RO_keymap[123] = SDLK_KP5;
   215     RO_keymap[124] = SDLK_KP2;
   216 
   217     SDL_memset (RO_pressed, 0, ROKEYBD_ARRAYSIZE);
   218 }
   219 
   220 
   221 /* Variable for mouse relative processing */
   222 int mouse_relative = 0;
   223 
   224 /* Check to see if we need to enter or leave mouse relative mode */
   225 
   226 void
   227 RISCOS_CheckMouseMode (_THIS)
   228 {
   229     /* If the mouse is hidden and input is grabbed, we use relative mode */
   230     if (!(SDL_cursorstate & CURSOR_VISIBLE) &&
   231         (this->input_grab != SDL_GRAB_OFF)) {
   232         mouse_relative = 1;
   233     } else {
   234         mouse_relative = 0;
   235     }
   236 }
   237 
   238 
   239 void
   240 RISCOS_PollMouse (_THIS)
   241 {
   242     RISCOS_PollMouseHelper (this, 1);
   243 }
   244 
   245 extern int mouseInWindow;
   246 
   247 void
   248 WIMP_PollMouse (_THIS)
   249 {
   250     /* Only poll when mouse is over the window */
   251     if (!mouseInWindow)
   252         return;
   253 
   254     RISCOS_PollMouseHelper (this, 0);
   255 }
   256 
   257 /* Static variables so only changes are reported */
   258 static Sint16 last_x = -1, last_y = -1;
   259 static int last_buttons = 0;
   260 
   261 /* Share routine between WIMP and FULLSCREEN for polling mouse and
   262    passing on events */
   263 void
   264 RISCOS_PollMouseHelper (_THIS, int fullscreen)
   265 {
   266     _kernel_swi_regs regs;
   267     static int starting = 1;
   268 
   269     if (_kernel_swi (OS_Mouse, &regs, &regs) == NULL) {
   270         Sint16 new_x = regs.r[0];       /* Initialy get as OS units */
   271         Sint16 new_y = regs.r[1];
   272 
   273         /* Discard mouse events until they let go of the mouse after starting */
   274         if (starting && regs.r[2] != 0)
   275             return;
   276         else
   277             starting = 0;
   278 
   279         if (new_x != last_x || new_y != last_y || last_buttons != regs.r[2]) {
   280             /* Something changed so generate appropriate events */
   281             int topLeftX, topLeftY;     /* Top left OS units */
   282             int x, y;           /* Mouse position in SDL pixels */
   283 
   284             if (fullscreen) {
   285                 topLeftX = 0;
   286                 topLeftY = (this->hidden->height << this->hidden->yeig) - 1;
   287             } else {
   288                 int window_state[9];
   289 
   290                 /* Get current window state */
   291                 window_state[0] = this->hidden->window_handle;
   292                 regs.r[1] = (unsigned int) window_state;
   293                 _kernel_swi (Wimp_GetWindowState, &regs, &regs);
   294 
   295                 topLeftX = window_state[1];
   296                 topLeftY = window_state[4];
   297             }
   298 
   299             /* Convert co-ordinates to workspace */
   300             x = new_x - topLeftX;
   301             y = topLeftY - new_y;       /* Y goes from top of window/screen */
   302 
   303             /* Convert OS units to pixels */
   304             x >>= this->hidden->xeig;
   305             y >>= this->hidden->yeig;
   306 
   307             if (last_x != new_x || last_y != new_y) {
   308                 if (mouse_relative) {
   309                     int centre_x = SDL_VideoSurface->w / 2;
   310                     int centre_y = SDL_VideoSurface->h / 2;
   311 
   312                     if (centre_x != x || centre_y != y) {
   313                         if (SDL_VideoSurface)
   314                             SDL_PrivateMouseMotion (0, 1,
   315                                                     x - centre_x,
   316                                                     y - centre_y);
   317                         last_x = topLeftX + (centre_x << this->hidden->xeig);
   318                         last_y = topLeftY - (centre_y << this->hidden->yeig);
   319 
   320                         /* Re-centre the mouse pointer, so we still get relative
   321                            movement when the mouse is at the edge of the window
   322                            or screen.
   323                          */
   324                         {
   325                             unsigned char block[5];
   326 
   327                             block[0] = 3;       /* OSWORD move pointer sub-reason code */
   328                             block[1] = last_x & 0xFF;
   329                             block[2] = (last_x >> 8) & 0xFF;
   330                             block[3] = last_y & 0xFF;
   331                             block[4] = (last_y >> 8) & 0xFF;
   332 
   333                             regs.r[0] = 21;     /* OSWORD pointer stuff code */
   334                             regs.r[1] = (int) block;
   335                             _kernel_swi (OS_Word, &regs, &regs);
   336                         }
   337                     }
   338                 } else {
   339                     last_x = new_x;
   340                     last_y = new_y;
   341                     SDL_PrivateMouseMotion (0, 0, x, y);
   342                 }
   343             }
   344 
   345             if (last_buttons != regs.r[2]) {
   346                 int changed = last_buttons ^ regs.r[2];
   347                 last_buttons = regs.r[2];
   348                 if (changed & 4)
   349                     SDL_PrivateMouseButton ((last_buttons & 4) ?
   350                                             SDL_PRESSED : SDL_RELEASED,
   351                                             SDL_BUTTON_LEFT, 0, 0);
   352                 if (changed & 2)
   353                     SDL_PrivateMouseButton ((last_buttons & 2) ?
   354                                             SDL_PRESSED : SDL_RELEASED,
   355                                             SDL_BUTTON_MIDDLE, 0, 0);
   356                 if (changed & 1)
   357                     SDL_PrivateMouseButton ((last_buttons & 1) ?
   358                                             SDL_PRESSED : SDL_RELEASED,
   359                                             SDL_BUTTON_RIGHT, 0, 0);
   360             }
   361         }
   362     }
   363 }
   364 
   365 void
   366 RISCOS_PollKeyboard ()
   367 {
   368     int which_key = ROKEY_LEFT_SHIFT;
   369     int j;
   370     int min_key, max_key;
   371     SDL_keysym key;
   372 
   373     /* Scan the keyboard to see what is pressed */
   374     while (which_key <= ROKEY_LAST_KEY) {
   375         which_key = (_kernel_osbyte (121, which_key, 0) & 0xFF);
   376         if (which_key != ROKEY_NONE) {
   377             switch (which_key) {
   378                 /* Skip over mouse keys */
   379             case ROKEY_LEFT_MOUSE:
   380             case ROKEY_CENTRE_MOUSE:
   381             case ROKEY_RIGHT_MOUSE:
   382                 which_key = ROKEY_RIGHT_MOUSE;
   383                 break;
   384 
   385                 /* Ignore keys that cause 2 internal number to be generated */
   386             case 71:
   387             case 24:
   388             case 87:
   389             case 40:
   390                 break;
   391 
   392                 /* Ignore break as it can be latched on */
   393             case 44:
   394                 break;
   395 
   396             default:
   397                 RO_pressed[which_key] += 2;
   398                 break;
   399             }
   400             which_key++;
   401         }
   402     }
   403 
   404     /* Generate key released messages */
   405     min_key = ROKEY_LAST_KEY + 1;
   406     max_key = ROKEY_LEFT_SHIFT;
   407 
   408     for (j = ROKEY_LEFT_SHIFT; j <= ROKEY_LAST_KEY; j++) {
   409         if (RO_pressed[j]) {
   410             if (RO_pressed[j] == 1) {
   411                 RO_pressed[j] = 0;
   412                 SDL_PrivateKeyboard (SDL_RELEASED, TranslateKey (j, &key, 0));
   413             } else {
   414                 if (j < min_key)
   415                     min_key = j;
   416                 if (j > max_key)
   417                     max_key = j;
   418             }
   419         }
   420     }
   421 
   422     /* Generate key pressed messages */
   423     for (j = min_key; j <= max_key; j++) {
   424         if (RO_pressed[j]) {
   425             if (RO_pressed[j] == 2) {
   426                 SDL_PrivateKeyboard (SDL_PRESSED, TranslateKey (j, &key, 1));
   427             }
   428             RO_pressed[j] = 1;
   429         }
   430     }
   431 }
   432 
   433 static SDL_keysym *
   434 TranslateKey (int intkey, SDL_keysym * keysym, int pressed)
   435 {
   436     /* Set the keysym information */
   437     keysym->scancode = (unsigned char) intkey;
   438     keysym->sym = RO_keymap[intkey];
   439     keysym->mod = KMOD_NONE;
   440     keysym->unicode = 0;
   441     if (pressed && SDL_TranslateUNICODE) {
   442         int state;
   443         int ch;
   444 
   445         state = (_kernel_osbyte (202, 0, 255) & 0xFF);
   446 
   447         /*TODO: Take into account other keyboard layouts */
   448 
   449         ch = keysym->sym;       /* This should handle most unshifted keys */
   450 
   451         if (intkey < 9 || ch == SDLK_UNKNOWN) {
   452             ch = 0;
   453 
   454         } else if (state & 64) {        /* Control on */
   455             ch = ch & 31;
   456 
   457         } else {
   458             int topOfKey = 0;
   459             if (state & 8) {    /* Shift on */
   460                 topOfKey = 1;
   461             }
   462 
   463             if ((state & 16) == 0) {    /* Caps lock is on */
   464                 if (ch >= SDLK_a && ch <= SDLK_z) {
   465                     if ((state & 128) == 0) {   /* Shift Enable off */
   466                         /* All letter become upper case */
   467                         topOfKey = 1;
   468                     } else {
   469                         /* Shift+Letters gives lower case */
   470                         topOfKey = 1 - topOfKey;
   471                     }
   472                 }
   473             }
   474 
   475             if (topOfKey) {
   476                 /* Key produced with shift held down */
   477 
   478                 /* Letters just give upper case version */
   479                 if (ch >= SDLK_a && ch <= SDLK_z)
   480                     ch = toupper (ch);
   481                 else {
   482                     switch (ch) {
   483                     case SDLK_HASH:
   484                         ch = '~';
   485                         break;
   486                     case SDLK_QUOTE:
   487                         ch = '@';
   488                         break;
   489                     case SDLK_COMMA:
   490                         ch = '<';
   491                         break;
   492                     case SDLK_MINUS:
   493                         ch = '_';
   494                         break;
   495                     case SDLK_PERIOD:
   496                         ch = '>';
   497                         break;
   498                     case SDLK_SLASH:
   499                         ch = '?';
   500                         break;
   501 
   502                     case SDLK_0:
   503                         ch = ')';
   504                         break;
   505                     case SDLK_1:
   506                         ch = '!';
   507                         break;
   508                     case SDLK_2:
   509                         ch = '"';
   510                         break;
   511                     case SDLK_3:
   512                         ch = '';
   513                         break;
   514                     case SDLK_4:
   515                         ch = '$';
   516                         break;
   517                     case SDLK_5:
   518                         ch = '%';
   519                         break;
   520                     case SDLK_6:
   521                         ch = '^';
   522                         break;
   523                     case SDLK_7:
   524                         ch = '&';
   525                         break;
   526                     case SDLK_8:
   527                         ch = '*';
   528                         break;
   529                     case SDLK_9:
   530                         ch = '(';
   531                         break;
   532 
   533                     case SDLK_SEMICOLON:
   534                         ch = ':';
   535                         break;
   536                     case SDLK_EQUALS:
   537                         ch = '+';
   538                         break;
   539                     case SDLK_LEFTBRACKET:
   540                         ch = '{';
   541                         break;
   542                     case SDLK_BACKSLASH:
   543                         ch = '|';
   544                         break;
   545                     case SDLK_RIGHTBRACKET:
   546                         ch = '}';
   547                         break;
   548                     case SDLK_BACKQUOTE:
   549                         ch = '';
   550                         break;
   551 
   552                     default:
   553                         ch = 0; /* Map to zero character if we don't understand it */
   554                         break;
   555                     }
   556                 }
   557 
   558             } else if (ch > 126) {
   559                 /* SDL key code < 126 map directly onto their Unicode equivalents */
   560                 /* Keypad 0 to 9 maps to numeric equivalent */
   561                 if (ch >= SDLK_KP0 && ch <= SDLK_KP9)
   562                     ch = ch - SDLK_KP0 + '0';
   563                 else {
   564                     /* Following switch maps other keys that produce an Ascii value */
   565                     switch (ch) {
   566                     case SDLK_KP_PERIOD:
   567                         ch = '.';
   568                         break;
   569                     case SDLK_KP_DIVIDE:
   570                         ch = '/';
   571                         break;
   572                     case SDLK_KP_MULTIPLY:
   573                         ch = '*';
   574                         break;
   575                     case SDLK_KP_MINUS:
   576                         ch = '-';
   577                         break;
   578                     case SDLK_KP_PLUS:
   579                         ch = '+';
   580                         break;
   581                     case SDLK_KP_EQUALS:
   582                         ch = '=';
   583                         break;
   584 
   585                     default:
   586                         /* If we don't know what it is set the Unicode to 0 */
   587                         ch = 0;
   588                         break;
   589                     }
   590                 }
   591             }
   592         }
   593 
   594         keysym->unicode = ch;
   595     }
   596     return (keysym);
   597 }
   598 
   599 /* end of SDL_riscosevents.c ... */
   600 /* vi: set ts=4 sw=4 expandtab: */