src/video/riscos/SDL_riscosevents.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 17 Sep 2004 13:20:10 +0000
changeset 955 d74fbf56f2f6
parent 769 b8d311d90021
child 1035 974ba6ae0fa3
permissions -rw-r--r--
Date: Fri, 25 Jun 2004 13:29:15 +0100
From: "alan buckley"
Subject: Modification for RISC OS version of SDL

Ive attached a zip file with the changes to this email, it contains the
following:

The file sdldiff.txt is the output from cvs diff u. .
The directory thread/riscos contains all the new files to support threading.

Readme.riscos is a new readme file to add.
     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@devolution.com
    21 */
    22 
    23 /*
    24      File added by Alan Buckley (alan_baa@hotmail.com) for RISCOS 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 RISCOS internal key numbers to a SDL keysym */
    47 static SDLKey RO_keymap[SDLK_LAST];
    48 
    49 /* RISCOS 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 the let go of the mouse after starting */
   266        if (starting && regs.r[2] != 0) return;
   267        else starting = 0;
   268 
   269        if (new_x != last_x || new_y != last_y || last_buttons != regs.r[2])
   270        {
   271           /* Something changed so generate appropriate events */
   272           int topLeftX, topLeftY;  /* Top left OS units */
   273           int x, y;                /* Mouse position in SDL pixels */
   274 
   275           if (fullscreen)
   276           {
   277              topLeftX = 0;
   278              topLeftY = (this->hidden->height << this->hidden->yeig) - 1;
   279           } else
   280           {
   281              int window_state[9];
   282 
   283 	         /* Get current window state */
   284 		     window_state[0] = this->hidden->window_handle;
   285 		     regs.r[1] = (unsigned int)window_state;
   286 		     _kernel_swi(Wimp_GetWindowState, &regs, &regs);
   287 
   288              topLeftX = window_state[1];
   289              topLeftY = window_state[4];
   290           }
   291 
   292 		  /* Convert co-ordinates to workspace */
   293 		  x = new_x - topLeftX;
   294           y = topLeftY - new_y; /* Y goes from top of window/screen */
   295 
   296 	 	  /* Convert OS units to pixels */
   297 	      x >>= this->hidden->xeig;
   298 		  y >>= this->hidden->yeig;
   299 
   300           if (last_x != new_x || last_y != new_y)
   301           {
   302              if (mouse_relative)
   303              {
   304                 int centre_x = SDL_VideoSurface->w/2;
   305                 int centre_y = SDL_VideoSurface->h/2;
   306 
   307                 if (centre_x != x || centre_y != y)
   308                 {
   309                    if (SDL_VideoSurface) SDL_PrivateMouseMotion(0,1,x - centre_x, y - centre_y);
   310                    last_x = topLeftX + (centre_x << this->hidden->xeig);
   311                    last_y = topLeftY - (centre_y << this->hidden->yeig);
   312 
   313                    /* Re-centre the mouse pointer, so we still get relative
   314                       movement when the mouse is at the edge of the window
   315                       or screen.
   316                    */
   317                    {
   318                       unsigned char block[5];
   319 
   320                       block[0] = 3; /* OSWORD move pointer sub-reason code */
   321                       block[1] = last_x & 0xFF;
   322                       block[2] = (last_x >> 8) & 0xFF;
   323                       block[3] = last_y & 0xFF;
   324                       block[4] = (last_y >> 8) & 0xFF;
   325                        
   326                       regs.r[0] = 21; /* OSWORD pointer stuff code */
   327                       regs.r[1] = (int)block;
   328                       _kernel_swi(OS_Word, &regs, &regs);
   329                	   }
   330                 }
   331              } else
   332              {
   333                 last_x = new_x;
   334                 last_y = new_y;
   335                 SDL_PrivateMouseMotion(0,0,x,y);
   336              }
   337           }
   338 
   339           if (last_buttons != regs.r[2])
   340           {
   341              int changed = last_buttons ^ regs.r[2];
   342              last_buttons = regs.r[2];
   343              if (changed & 4) SDL_PrivateMouseButton((last_buttons & 4) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_LEFT, 0, 0);
   344              if (changed & 2) SDL_PrivateMouseButton((last_buttons & 2) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_MIDDLE, 0, 0);
   345              if (changed & 1) SDL_PrivateMouseButton((last_buttons & 1) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_RIGHT, 0, 0);
   346           }
   347        }
   348     }
   349 }
   350 
   351 void RISCOS_PollKeyboard()
   352 {
   353 	int which_key = ROKEY_LEFT_SHIFT;
   354 	int j;
   355 	int min_key, max_key;
   356 	SDL_keysym key;
   357 
   358 	/* Scan the keyboard to see what is pressed */
   359 	while (which_key <= ROKEY_LAST_KEY)
   360 	{
   361 		which_key = (_kernel_osbyte(121, which_key, 0) & 0xFF);
   362 	    if (which_key != ROKEY_NONE)
   363 	    {
   364 		    switch(which_key)
   365 		    {
   366 		    /* Skip over mouse keys */
   367 		    case ROKEY_LEFT_MOUSE:
   368 		    case ROKEY_CENTRE_MOUSE:
   369 		    case ROKEY_RIGHT_MOUSE:
   370 				which_key = ROKEY_RIGHT_MOUSE;
   371 				break;
   372 
   373             /* Ignore keys that cause 2 internal number to be generated */
   374 			case 71: case 24: case 87: case 40:
   375 			    break;
   376 
   377             /* Ignore break as it can be latched on */
   378             case 44:
   379                 break;
   380 
   381 			default:
   382 				RO_pressed[which_key] += 2;
   383 				break;
   384 		    }
   385 			which_key++;
   386 		}
   387 	}
   388 
   389 	/* Generate key released messages */
   390 	min_key = ROKEY_LAST_KEY+1;
   391 	max_key = ROKEY_LEFT_SHIFT;
   392 
   393 	for (j = ROKEY_LEFT_SHIFT; j <= ROKEY_LAST_KEY; j++)
   394 	{
   395 		if (RO_pressed[j])
   396 		{
   397 			if (RO_pressed[j] == 1)
   398 			{
   399 				RO_pressed[j] = 0;
   400 				SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(j,&key,0));
   401 			} else 
   402 			{
   403 				if (j < min_key) min_key = j;
   404 				if (j > max_key) max_key = j;
   405 			}
   406 		}
   407 	}
   408 
   409 	/* Generate key pressed messages */
   410 	for (j = min_key; j <= max_key; j++)
   411 	{
   412 		if (RO_pressed[j])
   413 		{
   414 			if (RO_pressed[j] == 2)
   415 			{
   416 				SDL_PrivateKeyboard(SDL_PRESSED,TranslateKey(j,&key,1));
   417 			}
   418 			RO_pressed[j] = 1;
   419 		}
   420 	}
   421 }
   422 
   423 static SDL_keysym *TranslateKey(int intkey, SDL_keysym *keysym, int pressed)
   424 {
   425 	/* Set the keysym information */
   426 	keysym->scancode = (unsigned char) intkey;
   427 	keysym->sym = RO_keymap[intkey];
   428 	keysym->mod = KMOD_NONE;
   429 	keysym->unicode = 0;
   430 	if ( pressed && SDL_TranslateUNICODE )
   431 	{
   432 		int state;
   433 		int ch;
   434 
   435 		state = (_kernel_osbyte(202, 0, 255) & 0xFF);
   436 
   437 		/*TODO: Take into account other keyboard layouts */
   438 
   439 		ch = keysym->sym; /* This should handle most unshifted keys */
   440 
   441         if (intkey < 9 || ch == SDLK_UNKNOWN)
   442         {
   443            ch = 0;
   444 
   445         } else if (state & 64) /* Control on */
   446 		{
   447 			ch = ch & 31;
   448 
   449 		} else 
   450 		{
   451 			int topOfKey = 0;
   452             if (state & 8) /* Shift on */
   453 			{
   454 				topOfKey = 1;
   455 			}
   456 
   457 			if ((state & 16) == 0) /* Caps lock is on */
   458 			{
   459 			   if (ch >= SDLK_a && ch <= SDLK_z)
   460 			   {
   461  				  if ((state & 128) == 0) /* Shift Enable off */
   462 				  {
   463 				     /* All letter become upper case */
   464 				 	 topOfKey = 1;
   465 				  } else
   466 				  {
   467 				     /* Shift+Letters gives lower case */
   468 				     topOfKey = 1 - topOfKey;
   469 				  }
   470 		       }
   471 			}
   472 
   473 			if (topOfKey)
   474 			{
   475 				/* Key produced with shift held down */
   476 
   477 				/* Letters just give upper case version */
   478 				if (ch >= SDLK_a && ch <= SDLK_z) ch = toupper(ch);
   479 				else
   480 				{
   481 					switch(ch)
   482 					{
   483 					case SDLK_HASH:   ch = '~'; break;
   484 					case SDLK_QUOTE:  ch = '@'; break;
   485 					case SDLK_COMMA:  ch = '<'; break;
   486 					case SDLK_MINUS:  ch = '_'; break;
   487 					case SDLK_PERIOD: ch = '>'; break;
   488 					case SDLK_SLASH:  ch = '?'; break;
   489 
   490 					case SDLK_0: ch = ')'; break;
   491 					case SDLK_1: ch = '!'; break;
   492 					case SDLK_2: ch = '"'; break;
   493 					case SDLK_3: ch = ''; break;
   494 					case SDLK_4: ch = '$'; break;
   495 					case SDLK_5: ch = '%'; break;
   496 					case SDLK_6: ch = '^'; break;
   497 					case SDLK_7: ch = '&'; break;
   498 					case SDLK_8: ch = '*'; break;
   499 					case SDLK_9: ch = '('; break;
   500 
   501 					case SDLK_SEMICOLON:    ch = ':'; break;
   502 					case SDLK_EQUALS:       ch = '+'; break;
   503 					case SDLK_LEFTBRACKET:  ch = '{'; break;
   504 					case SDLK_BACKSLASH:    ch = '|'; break;
   505 					case SDLK_RIGHTBRACKET: ch = '}'; break;
   506 					case SDLK_BACKQUOTE:    ch = ''; break;
   507 
   508 					default:
   509 						ch = 0; /* Map to zero character if we don't understand it */
   510 						break;
   511 					}
   512 				}
   513 
   514 			} else if (ch > 126)
   515 			{
   516 				/* SDL key code < 126 map directly onto their Unicode equivalents */
   517 				/* Keypad 0 to 9 maps to numeric equivalent */
   518 				if (ch >= SDLK_KP0 && ch <= SDLK_KP9) ch = ch - SDLK_KP0 + '0';
   519 				else
   520 				{
   521 					/* Following switch maps other keys that produce an Ascii value */
   522 					switch(ch)
   523 					{
   524 					case SDLK_KP_PERIOD:   ch = '.'; break;
   525 					case SDLK_KP_DIVIDE:   ch = '/'; break;
   526 					case SDLK_KP_MULTIPLY: ch = '*'; break;
   527 					case SDLK_KP_MINUS:    ch = '-'; break;
   528 					case SDLK_KP_PLUS:     ch = '+'; break;
   529 					case SDLK_KP_EQUALS:   ch = '='; break;
   530 
   531 					default:
   532 						/* If we don't know what it is set the Unicode to 0 */
   533 						ch = 0;
   534 						break;
   535 					}
   536 				}
   537 			}			
   538 		}
   539 				
   540 		keysym->unicode = ch;
   541 	}
   542 	return(keysym);
   543 }
   544 
   545 /* end of SDL_riscosevents.c ... */
   546