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