src/video/riscos/SDL_riscosevents.c
changeset 630 550bccdf04bd
child 658 e71b7108d2d7
equal deleted inserted replaced
629:3fa401bb4bb5 630:550bccdf04bd
       
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  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 
       
    75 void RISCOS_PollMouse(_THIS);
       
    76 void RISCOS_PollKeyboard();
       
    77 
       
    78 void RISCOS_PollMouseHelper(_THIS, int fullscreen);
       
    79 
       
    80 extern void DRenderer_FillBuffers();
       
    81 
       
    82 /* Timer running function */
       
    83 extern void RISCOS_CheckTimer();
       
    84 
       
    85 void FULLSCREEN_PumpEvents(_THIS)
       
    86 {
       
    87     /* Current implementation requires keyboard and mouse polling */
       
    88 	RISCOS_PollKeyboard();
       
    89 	RISCOS_PollMouse(this);
       
    90 	DRenderer_FillBuffers();
       
    91 	if (SDL_timer_running) RISCOS_CheckTimer();
       
    92 }
       
    93 
       
    94 
       
    95 void RISCOS_InitOSKeymap(_THIS)
       
    96 {
       
    97 	int i;
       
    98 
       
    99 	/* Map the VK keysyms */
       
   100 	for ( i=0; i<SDL_TABLESIZE(RO_keymap); ++i )
       
   101 		RO_keymap[i] = SDLK_UNKNOWN;
       
   102 
       
   103   RO_keymap[3] = SDLK_LSHIFT;
       
   104   RO_keymap[4] = SDLK_LCTRL;
       
   105   RO_keymap[5] = SDLK_LALT;
       
   106   RO_keymap[6] = SDLK_RSHIFT;
       
   107   RO_keymap[7] = SDLK_RCTRL;
       
   108   RO_keymap[8] = SDLK_RALT;
       
   109   RO_keymap[16] = SDLK_q;
       
   110   RO_keymap[17] = SDLK_3;
       
   111   RO_keymap[18] = SDLK_4;
       
   112   RO_keymap[19] = SDLK_5;
       
   113   RO_keymap[20] = SDLK_F4;
       
   114   RO_keymap[21] = SDLK_8;
       
   115   RO_keymap[22] = SDLK_F7;
       
   116   RO_keymap[23] = SDLK_MINUS,
       
   117   RO_keymap[25] = SDLK_LEFT;
       
   118   RO_keymap[26] = SDLK_KP6;
       
   119   RO_keymap[27] = SDLK_KP7;
       
   120   RO_keymap[28] = SDLK_F11;
       
   121   RO_keymap[29] = SDLK_F12;
       
   122   RO_keymap[30] = SDLK_F10;
       
   123   RO_keymap[31] = SDLK_SCROLLOCK;
       
   124   RO_keymap[32] = SDLK_PRINT;
       
   125   RO_keymap[33] = SDLK_w;
       
   126   RO_keymap[34] = SDLK_e;
       
   127   RO_keymap[35] = SDLK_t;
       
   128   RO_keymap[36] = SDLK_7;
       
   129   RO_keymap[37] = SDLK_i;
       
   130   RO_keymap[38] = SDLK_9;
       
   131   RO_keymap[39] = SDLK_0;
       
   132   RO_keymap[41] = SDLK_DOWN;
       
   133   RO_keymap[42] = SDLK_KP8;
       
   134   RO_keymap[43] = SDLK_KP9;
       
   135   RO_keymap[44] = SDLK_BREAK;
       
   136   RO_keymap[45] = SDLK_BACKQUOTE;
       
   137 /*  RO_keymap[46] = SDLK_currency; TODO: Figure out if this has a value */
       
   138   RO_keymap[47] = SDLK_BACKSPACE;
       
   139   RO_keymap[48] = SDLK_1;
       
   140   RO_keymap[49] = SDLK_2;
       
   141   RO_keymap[50] = SDLK_d;
       
   142   RO_keymap[51] = SDLK_r;
       
   143   RO_keymap[52] = SDLK_6;
       
   144   RO_keymap[53] = SDLK_u;
       
   145   RO_keymap[54] = SDLK_o;
       
   146   RO_keymap[55] = SDLK_p;
       
   147   RO_keymap[56] = SDLK_LEFTPAREN;
       
   148   RO_keymap[57] = SDLK_UP;
       
   149   RO_keymap[58] = SDLK_KP_PLUS;
       
   150   RO_keymap[59] = SDLK_KP_MINUS;
       
   151   RO_keymap[60] = SDLK_KP_ENTER;
       
   152   RO_keymap[61] = SDLK_INSERT;
       
   153   RO_keymap[62] = SDLK_HOME;
       
   154   RO_keymap[63] = SDLK_PAGEUP;
       
   155   RO_keymap[64] = SDLK_CAPSLOCK;
       
   156   RO_keymap[65] = SDLK_a;
       
   157   RO_keymap[66] = SDLK_x;
       
   158   RO_keymap[67] = SDLK_f;
       
   159   RO_keymap[68] = SDLK_y;
       
   160   RO_keymap[69] = SDLK_j;
       
   161   RO_keymap[70] = SDLK_k;
       
   162   RO_keymap[72] = SDLK_SEMICOLON;
       
   163   RO_keymap[73] = SDLK_RETURN;
       
   164   RO_keymap[74] = SDLK_KP_DIVIDE;
       
   165   RO_keymap[76] = SDLK_KP_PERIOD;
       
   166   RO_keymap[77] = SDLK_NUMLOCK;
       
   167   RO_keymap[78] = SDLK_PAGEDOWN;
       
   168   RO_keymap[79] = SDLK_QUOTE;
       
   169   RO_keymap[81] = SDLK_s;
       
   170   RO_keymap[82] = SDLK_c;
       
   171   RO_keymap[83] = SDLK_g;
       
   172   RO_keymap[84] = SDLK_h;
       
   173   RO_keymap[85] = SDLK_n;
       
   174   RO_keymap[86] = SDLK_l;
       
   175   RO_keymap[87] = SDLK_SEMICOLON;
       
   176   RO_keymap[88] = SDLK_RIGHTPAREN;
       
   177   RO_keymap[89] = SDLK_DELETE;
       
   178   RO_keymap[90] = SDLK_KP_MINUS;
       
   179   RO_keymap[91] = SDLK_KP_MULTIPLY;
       
   180   RO_keymap[93] = SDLK_EQUALS;
       
   181   RO_keymap[94] = SDLK_BACKSLASH;
       
   182   RO_keymap[96] = SDLK_TAB;
       
   183   RO_keymap[97] = SDLK_z;
       
   184   RO_keymap[98] = SDLK_SPACE;
       
   185   RO_keymap[99] = SDLK_v;
       
   186   RO_keymap[100] = SDLK_b;
       
   187   RO_keymap[101] = SDLK_m;
       
   188   RO_keymap[102] = SDLK_COMMA;
       
   189   RO_keymap[103] = SDLK_PERIOD;
       
   190   RO_keymap[104] = SDLK_SLASH;
       
   191   RO_keymap[105] = SDLK_END;
       
   192   RO_keymap[106] = SDLK_KP0;
       
   193   RO_keymap[107] = SDLK_KP1;
       
   194   RO_keymap[108] = SDLK_KP3;
       
   195   RO_keymap[112] = SDLK_ESCAPE;
       
   196   RO_keymap[113] = SDLK_F1;
       
   197   RO_keymap[114] = SDLK_F2;
       
   198   RO_keymap[115] = SDLK_F3;
       
   199   RO_keymap[116] = SDLK_F5;
       
   200   RO_keymap[117] = SDLK_F6;
       
   201   RO_keymap[118] = SDLK_F8;
       
   202   RO_keymap[119] = SDLK_F9;
       
   203   RO_keymap[120] = SDLK_HASH;
       
   204   RO_keymap[121] = SDLK_RIGHT;
       
   205   RO_keymap[122] = SDLK_KP4;
       
   206   RO_keymap[123] = SDLK_KP5;
       
   207   RO_keymap[124] = SDLK_KP2;
       
   208 
       
   209   memset(RO_pressed, 0, ROKEYBD_ARRAYSIZE);
       
   210 }
       
   211 
       
   212 
       
   213 /* Variable for mouse relative processing */
       
   214 int mouse_relative = 0;
       
   215 
       
   216 /* Check to see if we need to enter or leave mouse relative mode */
       
   217 
       
   218 void RISCOS_CheckMouseMode(_THIS)
       
   219 {
       
   220     /* If the mouse is hidden and input is grabbed, we use relative mode */
       
   221     if ( !(SDL_cursorstate & CURSOR_VISIBLE) &&
       
   222         (this->input_grab != SDL_GRAB_OFF) ) {
       
   223             mouse_relative = 1;
       
   224      } else {
       
   225             mouse_relative = 0;
       
   226      }
       
   227 }
       
   228 
       
   229 
       
   230 void RISCOS_PollMouse(_THIS)
       
   231 {
       
   232    RISCOS_PollMouseHelper(this, 1);
       
   233 }
       
   234 
       
   235 extern int mouseInWindow;
       
   236 
       
   237 void WIMP_PollMouse(_THIS)
       
   238 {
       
   239    /* Only poll when mouse is over the window */
       
   240    if (!mouseInWindow) return;
       
   241 
       
   242    RISCOS_PollMouseHelper(this, 0);
       
   243 }
       
   244 
       
   245 /* Static variables so only changes are reported */
       
   246 static Sint16 last_x = -1, last_y = -1;
       
   247 static int last_buttons = 0;
       
   248 
       
   249 /* Share routine between WIMP and FULLSCREEN for polling mouse and
       
   250    passing on events */
       
   251 void RISCOS_PollMouseHelper(_THIS, int fullscreen)
       
   252 {
       
   253     _kernel_swi_regs regs;
       
   254     static int starting = 1;
       
   255 
       
   256     if (_kernel_swi(OS_Mouse, &regs, &regs) == NULL)
       
   257     {
       
   258        Sint16 new_x = regs.r[0]; /* Initialy get as OS units */
       
   259        Sint16 new_y = regs.r[1];
       
   260 
       
   261 /* Discard mouse events until the let go of the mouse after starting */
       
   262        if (starting && regs.r[2] != 0) return;
       
   263        else starting = 0;
       
   264 
       
   265        if (new_x != last_x || new_y != last_y || last_buttons != regs.r[2])
       
   266        {
       
   267           /* Something changed so generate appropriate events */
       
   268           int topLeftX, topLeftY;  /* Top left OS units */
       
   269           int x, y;                /* Mouse position in SDL pixels */
       
   270 
       
   271           if (fullscreen)
       
   272           {
       
   273              topLeftX = 0;
       
   274              topLeftY = (this->hidden->height << this->hidden->yeig) - 1;
       
   275           } else
       
   276           {
       
   277              int window_state[9];
       
   278 
       
   279 	         /* Get current window state */
       
   280 		     window_state[0] = this->hidden->window_handle;
       
   281 		     regs.r[1] = (unsigned int)window_state;
       
   282 		     _kernel_swi(Wimp_GetWindowState, &regs, &regs);
       
   283 
       
   284              topLeftX = window_state[1];
       
   285              topLeftY = window_state[4];
       
   286           }
       
   287 
       
   288 		  /* Convert co-ordinates to workspace */
       
   289 		  x = new_x - topLeftX;
       
   290           y = topLeftY - new_y; /* Y goes from top of window/screen */
       
   291 
       
   292 	 	  /* Convert OS units to pixels */
       
   293 	      x >>= this->hidden->xeig;
       
   294 		  y >>= this->hidden->yeig;
       
   295 
       
   296           if (last_x != new_x || last_y != new_y)
       
   297           {
       
   298              if (mouse_relative)
       
   299              {
       
   300                 int centre_x = SDL_VideoSurface->w/2;
       
   301                 int centre_y = SDL_VideoSurface->h/2;
       
   302 
       
   303                 if (centre_x != x || centre_y != y)
       
   304                 {
       
   305                    if (SDL_VideoSurface) SDL_PrivateMouseMotion(0,1,x - centre_x, y - centre_y);
       
   306                    last_x = topLeftX + (centre_x << this->hidden->xeig);
       
   307                    last_y = topLeftY - (centre_y << this->hidden->yeig);
       
   308 
       
   309                    /* Re-centre the mouse pointer, so we still get relative
       
   310                       movement when the mouse is at the edge of the window
       
   311                       or screen.
       
   312                    */
       
   313                    {
       
   314                       unsigned char block[5];
       
   315 
       
   316                       block[0] = 3; /* OSWORD move pointer sub-reason code */
       
   317                       block[1] = last_x & 0xFF;
       
   318                       block[2] = (last_x >> 8) & 0xFF;
       
   319                       block[3] = last_y & 0xFF;
       
   320                       block[4] = (last_y >> 8) & 0xFF;
       
   321                        
       
   322                       regs.r[0] = 21; /* OSWORD pointer stuff code */
       
   323                       regs.r[1] = (int)block;
       
   324                       _kernel_swi(OS_Word, &regs, &regs);
       
   325                	   }
       
   326                 }
       
   327              } else
       
   328              {
       
   329                 last_x = new_x;
       
   330                 last_y = new_y;
       
   331                 SDL_PrivateMouseMotion(0,0,x,y);
       
   332              }
       
   333           }
       
   334 
       
   335           if (last_buttons != regs.r[2])
       
   336           {
       
   337              int changed = last_buttons ^ regs.r[2];
       
   338              last_buttons = regs.r[2];
       
   339              if (changed & 4) SDL_PrivateMouseButton((last_buttons & 4) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_LEFT, x, y);
       
   340              if (changed & 2) SDL_PrivateMouseButton((last_buttons & 2) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_MIDDLE, x, y);
       
   341              if (changed & 1) SDL_PrivateMouseButton((last_buttons & 1) ? SDL_PRESSED : SDL_RELEASED, SDL_BUTTON_RIGHT, x, y);
       
   342           }
       
   343        }
       
   344     }
       
   345 }
       
   346 
       
   347 void RISCOS_PollKeyboard()
       
   348 {
       
   349 	int which_key = ROKEY_LEFT_SHIFT;
       
   350 	int j;
       
   351 	int min_key, max_key;
       
   352 	SDL_keysym key;
       
   353 
       
   354 	/* Scan the keyboard to see what is pressed */
       
   355 	while (which_key <= ROKEY_LAST_KEY)
       
   356 	{
       
   357 		which_key = (_kernel_osbyte(121, which_key, 0) & 0xFF);
       
   358 	    if (which_key != ROKEY_NONE)
       
   359 		{
       
   360 		    switch(which_key)
       
   361 		    {
       
   362 		    /* Skip over mouse keys */
       
   363 		    case ROKEY_LEFT_MOUSE:
       
   364 		    case ROKEY_CENTRE_MOUSE:
       
   365 		    case ROKEY_RIGHT_MOUSE:
       
   366 				which_key = ROKEY_RIGHT_MOUSE;
       
   367 				break;
       
   368 
       
   369             /* Ignore keys that cause 2 internal number to be generated */
       
   370 			case 71: case 24: case 87: case 40:
       
   371 			    break;
       
   372 
       
   373 			default:
       
   374 				RO_pressed[which_key] += 2;
       
   375 				break;
       
   376 		    }
       
   377 			which_key++;
       
   378 		}
       
   379 	}
       
   380 
       
   381 	/* Generate key released messages */
       
   382 	min_key = ROKEY_LAST_KEY+1;
       
   383 	max_key = ROKEY_LEFT_SHIFT;
       
   384 
       
   385 	for (j = ROKEY_LEFT_SHIFT; j <= ROKEY_LAST_KEY; j++)
       
   386 	{
       
   387 		if (RO_pressed[j])
       
   388 		{
       
   389 			if (RO_pressed[j] == 1)
       
   390 			{
       
   391 				RO_pressed[j] = 0;
       
   392 				SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(j,&key,0));
       
   393 			} else 
       
   394 			{
       
   395 				if (j < min_key) min_key = j;
       
   396 				if (j > max_key) max_key = j;
       
   397 			}
       
   398 		}
       
   399 	}
       
   400 
       
   401 	/* Generate key pressed messages */
       
   402 	for (j = min_key; j <= max_key; j++)
       
   403 	{
       
   404 		if (RO_pressed[j])
       
   405 		{
       
   406 			if (RO_pressed[j] == 2)
       
   407 			{
       
   408 				SDL_PrivateKeyboard(SDL_PRESSED,TranslateKey(j,&key,1));
       
   409 			}
       
   410 			RO_pressed[j] = 1;
       
   411 		}
       
   412 	}
       
   413 }
       
   414 
       
   415 static SDL_keysym *TranslateKey(int intkey, SDL_keysym *keysym, int pressed)
       
   416 {
       
   417 	/* Set the keysym information */
       
   418 	keysym->scancode = (unsigned char) intkey;
       
   419 	keysym->sym = RO_keymap[intkey];
       
   420 	keysym->mod = KMOD_NONE;
       
   421 	keysym->unicode = 0;
       
   422 	if ( pressed && SDL_TranslateUNICODE )
       
   423 	{
       
   424 		int state;
       
   425 		int ch;
       
   426 
       
   427 		state = (_kernel_osbyte(202, 0, 255) & 0xFF);
       
   428 
       
   429 		/*TODO: better key to char mapping */
       
   430 
       
   431 		ch = keysym->sym; /* This should handle most keys */
       
   432 
       
   433         if (intkey < 9)
       
   434         {
       
   435            ch = 0;
       
   436 
       
   437         } else if (state & 64) /* Control on */
       
   438 		{
       
   439 			ch = ch & 31;
       
   440 
       
   441 		} else if ((state & 16) == 0) /* Caps lock is on */
       
   442 		{
       
   443 			if ((state & 128) == 0) /* Shift Enable off */
       
   444 			{
       
   445 				ch = toupper(ch);
       
   446 			}
       
   447 		} else if (state & 8) /* Shift on */
       
   448 		{
       
   449 			ch = toupper(ch);
       
   450 		}
       
   451 				
       
   452 		keysym->unicode = ch;
       
   453 	}
       
   454 	return(keysym);
       
   455 }
       
   456 
       
   457 /* end of SDL_riscosevents.c ... */
       
   458