src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 20 Sep 2006 19:47:09 +0000
changeset 2030 d774e9d7c2a8
parent 2019 d20f4510c718
child 2043 adf732f1f016
permissions -rw-r--r--
Forward keyboard reworking from 1.2
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /*
    25  * GEM SDL video driver implementation
    26  * inspired from the Dummy SDL driver
    27  * 
    28  * Patrice Mandin
    29  * and work from
    30  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    31  */
    32 
    33 #include <gem.h>
    34 
    35 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "SDL_gemvideo.h"
    38 #include "SDL_gemevents_c.h"
    39 #include "../ataricommon/SDL_atarikeys.h"       /* for keyboard scancodes */
    40 #include "../ataricommon/SDL_atarievents_c.h"
    41 #include "../ataricommon/SDL_xbiosevents_c.h"
    42 #include "../ataricommon/SDL_ataridevmouse_c.h"
    43 
    44 /* Variables */
    45 
    46 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    47 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    48 
    49 /* Functions prototypes */
    50 
    51 static int do_messages(_THIS, short *message);
    52 static void do_keyboard(short kc, short ks);
    53 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    54 
    55 /* Functions */
    56 
    57 void
    58 GEM_InitOSKeymap(_THIS)
    59 {
    60     SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    61     SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    62 
    63     /* Mouse init */
    64     GEM_mouse_relative = SDL_FALSE;
    65 
    66     SDL_Atari_InitInternalKeymap(this);
    67 }
    68 
    69 void
    70 GEM_PumpEvents(_THIS)
    71 {
    72     short mousex, mousey, mouseb, dummy;
    73     short kstate, prevkc, prevks;
    74     int i;
    75     SDL_keysym keysym;
    76 
    77     SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    78     prevkc = prevks = 0;
    79 
    80     for (;;) {
    81         int quit, resultat, event_mask, mouse_event;
    82         short buffer[8], kc;
    83         short x2, y2, w2, h2;
    84 
    85         quit = mouse_event = x2 = y2 = w2 = h2 = 0;
    86 
    87         event_mask = MU_MESAG | MU_TIMER | MU_KEYBD;
    88         if (!GEM_fullscreen && (GEM_handle >= 0)) {
    89             wind_get(GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
    90             event_mask |= MU_M1;
    91             if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)) {
    92                 mouse_event = MO_LEAVE;
    93             } else {
    94                 mouse_event = MO_ENTER;
    95             }
    96         }
    97 
    98         resultat = evnt_multi(event_mask,
    99                               0, 0, 0,
   100                               mouse_event, x2, y2, w2, h2,
   101                               0, 0, 0, 0, 0,
   102                               buffer,
   103                               10,
   104                               &dummy, &dummy, &dummy, &kstate, &kc, &dummy);
   105 
   106         /* Message event ? */
   107         if (resultat & MU_MESAG)
   108             quit = do_messages(this, buffer);
   109 
   110         /* Keyboard event ? */
   111         if (resultat & MU_KEYBD) {
   112             if ((prevkc != kc) || (prevks != kstate)) {
   113                 do_keyboard(kc, kstate);
   114             } else {
   115                 /* Avoid looping, if repeating same key */
   116                 break;
   117             }
   118         }
   119 
   120         /* Mouse entering/leaving window */
   121         if (resultat & MU_M1) {
   122             if (this->input_grab == SDL_GRAB_OFF) {
   123                 if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
   124                     SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   125                 } else {
   126                     SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   127                 }
   128             }
   129         }
   130 
   131         /* Timer event ? */
   132         if ((resultat & MU_TIMER) || quit)
   133             break;
   134     }
   135 
   136     /* Update mouse */
   137     graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   138     do_mouse(this, mousex, mousey, mouseb, kstate);
   139 
   140     /* Now generate keyboard events */
   141     for (i = 0; i < ATARIBIOS_MAXKEYS; i++) {
   142         /* Key pressed ? */
   143         if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   144             SDL_PrivateKeyboard(SDL_PRESSED,
   145                                 SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   146 
   147         /* Key unpressed ? */
   148         if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   149             SDL_PrivateKeyboard(SDL_RELEASED,
   150                                 SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   151     }
   152 
   153     SDL_memcpy(gem_previouskeyboard, gem_currentkeyboard,
   154                sizeof(gem_previouskeyboard));
   155 
   156     /* Refresh window name ? */
   157     if (GEM_refresh_name) {
   158         if (SDL_GetAppState() & SDL_APPACTIVE) {
   159             /* Fullscreen/windowed */
   160             if (GEM_title_name) {
   161                 wind_set(GEM_handle, WF_NAME,
   162                          (short) (((unsigned long) GEM_title_name) >>
   163                                   16),
   164                          (short) (((unsigned long) GEM_title_name) &
   165                                   0xffff), 0, 0);
   166             }
   167         } else {
   168             /* Iconified */
   169             if (GEM_icon_name) {
   170                 wind_set(GEM_handle, WF_NAME,
   171                          (short) (((unsigned long) GEM_icon_name) >>
   172                                   16),
   173                          (short) (((unsigned long) GEM_icon_name) &
   174                                   0xffff), 0, 0);
   175             }
   176         }
   177         GEM_refresh_name = SDL_FALSE;
   178     }
   179 }
   180 
   181 static int
   182 do_messages(_THIS, short *message)
   183 {
   184     int quit, posted;
   185     short x2, y2, w2, h2;
   186 
   187     quit = 0;
   188     switch (message[0]) {
   189     case WM_CLOSED:
   190     case AP_TERM:
   191         posted = SDL_PrivateQuit();
   192         quit = 1;
   193         break;
   194     case WM_MOVED:
   195         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   196                  message[6], message[7]);
   197         break;
   198     case WM_TOPPED:
   199         wind_set(message[3], WF_TOP, message[4], 0, 0, 0);
   200         /* Continue with TOP event processing */
   201     case WM_ONTOP:
   202         SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   203         if (VDI_setpalette) {
   204             VDI_setpalette(this, VDI_curpalette);
   205         }
   206         break;
   207     case WM_REDRAW:
   208         if (!GEM_lock_redraw) {
   209             GEM_wind_redraw(this, message[3], &message[4]);
   210         }
   211         break;
   212     case WM_ICONIFY:
   213     case WM_ALLICONIFY:
   214         wind_set(message[3], WF_ICONIFY, message[4], message[5],
   215                  message[6], message[7]);
   216         /* If we're active, make ourselves inactive */
   217         if (SDL_GetAppState() & SDL_APPACTIVE) {
   218             /* Send an internal deactivate event */
   219             SDL_PrivateAppActive(0, SDL_APPACTIVE);
   220         }
   221         /* Update window title */
   222         if (GEM_refresh_name && GEM_icon_name) {
   223             wind_set(GEM_handle, WF_NAME,
   224                      (short) (((unsigned long) GEM_icon_name) >> 16),
   225                      (short) (((unsigned long) GEM_icon_name) & 0xffff),
   226                      0, 0);
   227             GEM_refresh_name = SDL_FALSE;
   228         }
   229         break;
   230     case WM_UNICONIFY:
   231         wind_set(message[3], WF_UNICONIFY, message[4], message[5],
   232                  message[6], message[7]);
   233         /* If we're not active, make ourselves active */
   234         if (!(SDL_GetAppState() & SDL_APPACTIVE)) {
   235             /* Send an internal activate event */
   236             SDL_PrivateAppActive(1, SDL_APPACTIVE);
   237         }
   238         if (GEM_refresh_name && GEM_title_name) {
   239             wind_set(GEM_handle, WF_NAME,
   240                      (short) (((unsigned long) GEM_title_name) >> 16),
   241                      (short) (((unsigned long) GEM_title_name) & 0xffff),
   242                      0, 0);
   243             GEM_refresh_name = SDL_FALSE;
   244         }
   245         break;
   246     case WM_SIZED:
   247         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   248                  message[6], message[7]);
   249         wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   250         GEM_win_fulled = SDL_FALSE;     /* Cancel maximized flag */
   251         GEM_lock_redraw = SDL_TRUE;     /* Prevent redraw till buffers resized */
   252         SDL_PrivateResize(w2, h2);
   253         break;
   254     case WM_FULLED:
   255         {
   256             short x, y, w, h;
   257 
   258             if (GEM_win_fulled) {
   259                 wind_get(message[3], WF_PREVXYWH, &x, &y, &w, &h);
   260                 GEM_win_fulled = SDL_FALSE;
   261             } else {
   262                 x = GEM_desk_x;
   263                 y = GEM_desk_y;
   264                 w = GEM_desk_w;
   265                 h = GEM_desk_h;
   266                 GEM_win_fulled = SDL_TRUE;
   267             }
   268             wind_set(message[3], WF_CURRXYWH, x, y, w, h);
   269             wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   270             GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
   271             SDL_PrivateResize(w2, h2);
   272         }
   273         break;
   274     case WM_BOTTOMED:
   275         wind_set(message[3], WF_BOTTOM, 0, 0, 0, 0);
   276         /* Continue with BOTTOM event processing */
   277     case WM_UNTOPPED:
   278         SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   279         if (VDI_setpalette) {
   280             VDI_setpalette(this, VDI_oldpalette);
   281         }
   282         break;
   283     }
   284 
   285     return quit;
   286 }
   287 
   288 static void
   289 do_keyboard(short kc, short ks)
   290 {
   291     int scancode, asciicode;
   292 
   293     if (kc) {
   294         scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   295         gem_currentkeyboard[scancode]=0xFF;
   296     }
   297 
   298     /* Read special keys */
   299     if (ks & K_RSHIFT)
   300         gem_currentkeyboard[SCANCODE_RIGHTSHIFT] = 0xFF;
   301     if (ks & K_LSHIFT)
   302         gem_currentkeyboard[SCANCODE_LEFTSHIFT] = 0xFF;
   303     if (ks & K_CTRL)
   304         gem_currentkeyboard[SCANCODE_LEFTCONTROL] = 0xFF;
   305     if (ks & K_ALT)
   306         gem_currentkeyboard[SCANCODE_LEFTALT] = 0xFF;
   307 }
   308 
   309 static void
   310 do_mouse(_THIS, short mx, short my, short mb, short ks)
   311 {
   312     static short prevmousex = 0, prevmousey = 0, prevmouseb = 0;
   313     short x2, y2, w2, h2;
   314 
   315     /* Don't return mouse events if out of window */
   316     if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) == 0) {
   317         return;
   318     }
   319 
   320     /* Retrieve window coords, and generate mouse events accordingly */
   321     x2 = y2 = 0;
   322     w2 = VDI_w;
   323     h2 = VDI_h;
   324     if ((!GEM_fullscreen) && (GEM_handle >= 0)) {
   325         wind_get(GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   326 
   327         /* Do not generate mouse button event if out of window working area */
   328         if ((mx < x2) || (mx >= x2 + w2) || (my < y2) || (my >= y2 + h2)) {
   329             mb = prevmouseb;
   330         }
   331     }
   332 
   333     /* Mouse motion ? */
   334     if (GEM_mouse_relative) {
   335         if (GEM_usedevmouse) {
   336             SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   337         } else {
   338             SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   339         }
   340     } else {
   341         if ((prevmousex != mx) || (prevmousey != my)) {
   342             int posx, posy;
   343 
   344             /* Give mouse position relative to window position */
   345             posx = mx - x2;
   346             if (posx < 0)
   347                 posx = 0;
   348             if (posx > w2)
   349                 posx = w2 - 1;
   350             posy = my - y2;
   351             if (posy < 0)
   352                 posy = 0;
   353             if (posy > h2)
   354                 posy = h2 - 1;
   355 
   356             SDL_PrivateMouseMotion(0, 0, posx, posy);
   357         }
   358         prevmousex = mx;
   359         prevmousey = my;
   360     }
   361 
   362     /* Mouse button ? */
   363     if (prevmouseb != mb) {
   364         int i;
   365 
   366         for (i = 0; i < 2; i++) {
   367             int curbutton, prevbutton;
   368 
   369             curbutton = mb & (1 << i);
   370             prevbutton = prevmouseb & (1 << i);
   371 
   372             if (curbutton && !prevbutton) {
   373                 SDL_PrivateMouseButton(SDL_PRESSED, i + 1, 0, 0);
   374             }
   375             if (!curbutton && prevbutton) {
   376                 SDL_PrivateMouseButton(SDL_RELEASED, i + 1, 0, 0);
   377             }
   378         }
   379         prevmouseb = mb;
   380     }
   381 
   382     /* Read special keys */
   383     if (ks & K_RSHIFT)
   384         gem_currentkeyboard[SCANCODE_RIGHTSHIFT] = 0xFF;
   385     if (ks & K_LSHIFT)
   386         gem_currentkeyboard[SCANCODE_LEFTSHIFT] = 0xFF;
   387     if (ks & K_CTRL)
   388         gem_currentkeyboard[SCANCODE_LEFTCONTROL] = 0xFF;
   389     if (ks & K_ALT)
   390         gem_currentkeyboard[SCANCODE_LEFTALT] = 0xFF;
   391 }
   392 
   393 /* vi: set ts=4 sw=4 expandtab: */