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