src/video/gem/SDL_gemevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 24 Sep 2006 15:56:55 +0000
changeset 2043 adf732f1f016
parent 2030 d774e9d7c2a8
child 2186 6fe43f7efd94
permissions -rw-r--r--
Formatting update
     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,
   151                                                        SDL_FALSE));
   152     }
   153 
   154     SDL_memcpy(gem_previouskeyboard, gem_currentkeyboard,
   155                sizeof(gem_previouskeyboard));
   156 
   157     /* Refresh window name ? */
   158     if (GEM_refresh_name) {
   159         if (SDL_GetAppState() & SDL_APPACTIVE) {
   160             /* Fullscreen/windowed */
   161             if (GEM_title_name) {
   162                 wind_set(GEM_handle, WF_NAME,
   163                          (short) (((unsigned long) GEM_title_name) >>
   164                                   16),
   165                          (short) (((unsigned long) GEM_title_name) &
   166                                   0xffff), 0, 0);
   167             }
   168         } else {
   169             /* Iconified */
   170             if (GEM_icon_name) {
   171                 wind_set(GEM_handle, WF_NAME,
   172                          (short) (((unsigned long) GEM_icon_name) >>
   173                                   16),
   174                          (short) (((unsigned long) GEM_icon_name) &
   175                                   0xffff), 0, 0);
   176             }
   177         }
   178         GEM_refresh_name = SDL_FALSE;
   179     }
   180 }
   181 
   182 static int
   183 do_messages(_THIS, short *message)
   184 {
   185     int quit, posted;
   186     short x2, y2, w2, h2;
   187 
   188     quit = 0;
   189     switch (message[0]) {
   190     case WM_CLOSED:
   191     case AP_TERM:
   192         posted = SDL_PrivateQuit();
   193         quit = 1;
   194         break;
   195     case WM_MOVED:
   196         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   197                  message[6], message[7]);
   198         break;
   199     case WM_TOPPED:
   200         wind_set(message[3], WF_TOP, message[4], 0, 0, 0);
   201         /* Continue with TOP event processing */
   202     case WM_ONTOP:
   203         SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   204         if (VDI_setpalette) {
   205             VDI_setpalette(this, VDI_curpalette);
   206         }
   207         break;
   208     case WM_REDRAW:
   209         if (!GEM_lock_redraw) {
   210             GEM_wind_redraw(this, message[3], &message[4]);
   211         }
   212         break;
   213     case WM_ICONIFY:
   214     case WM_ALLICONIFY:
   215         wind_set(message[3], WF_ICONIFY, message[4], message[5],
   216                  message[6], message[7]);
   217         /* If we're active, make ourselves inactive */
   218         if (SDL_GetAppState() & SDL_APPACTIVE) {
   219             /* Send an internal deactivate event */
   220             SDL_PrivateAppActive(0, SDL_APPACTIVE);
   221         }
   222         /* Update window title */
   223         if (GEM_refresh_name && GEM_icon_name) {
   224             wind_set(GEM_handle, WF_NAME,
   225                      (short) (((unsigned long) GEM_icon_name) >> 16),
   226                      (short) (((unsigned long) GEM_icon_name) & 0xffff),
   227                      0, 0);
   228             GEM_refresh_name = SDL_FALSE;
   229         }
   230         break;
   231     case WM_UNICONIFY:
   232         wind_set(message[3], WF_UNICONIFY, message[4], message[5],
   233                  message[6], message[7]);
   234         /* If we're not active, make ourselves active */
   235         if (!(SDL_GetAppState() & SDL_APPACTIVE)) {
   236             /* Send an internal activate event */
   237             SDL_PrivateAppActive(1, SDL_APPACTIVE);
   238         }
   239         if (GEM_refresh_name && GEM_title_name) {
   240             wind_set(GEM_handle, WF_NAME,
   241                      (short) (((unsigned long) GEM_title_name) >> 16),
   242                      (short) (((unsigned long) GEM_title_name) & 0xffff),
   243                      0, 0);
   244             GEM_refresh_name = SDL_FALSE;
   245         }
   246         break;
   247     case WM_SIZED:
   248         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   249                  message[6], message[7]);
   250         wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   251         GEM_win_fulled = SDL_FALSE;     /* Cancel maximized flag */
   252         GEM_lock_redraw = SDL_TRUE;     /* Prevent redraw till buffers resized */
   253         SDL_PrivateResize(w2, h2);
   254         break;
   255     case WM_FULLED:
   256         {
   257             short x, y, w, h;
   258 
   259             if (GEM_win_fulled) {
   260                 wind_get(message[3], WF_PREVXYWH, &x, &y, &w, &h);
   261                 GEM_win_fulled = SDL_FALSE;
   262             } else {
   263                 x = GEM_desk_x;
   264                 y = GEM_desk_y;
   265                 w = GEM_desk_w;
   266                 h = GEM_desk_h;
   267                 GEM_win_fulled = SDL_TRUE;
   268             }
   269             wind_set(message[3], WF_CURRXYWH, x, y, w, h);
   270             wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   271             GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
   272             SDL_PrivateResize(w2, h2);
   273         }
   274         break;
   275     case WM_BOTTOMED:
   276         wind_set(message[3], WF_BOTTOM, 0, 0, 0, 0);
   277         /* Continue with BOTTOM event processing */
   278     case WM_UNTOPPED:
   279         SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   280         if (VDI_setpalette) {
   281             VDI_setpalette(this, VDI_oldpalette);
   282         }
   283         break;
   284     }
   285 
   286     return quit;
   287 }
   288 
   289 static void
   290 do_keyboard(short kc, short ks)
   291 {
   292     int scancode, asciicode;
   293 
   294     if (kc) {
   295         scancode = (kc >> 8) & (ATARIBIOS_MAXKEYS - 1);
   296         gem_currentkeyboard[scancode] = 0xFF;
   297     }
   298 
   299     /* Read special keys */
   300     if (ks & K_RSHIFT)
   301         gem_currentkeyboard[SCANCODE_RIGHTSHIFT] = 0xFF;
   302     if (ks & K_LSHIFT)
   303         gem_currentkeyboard[SCANCODE_LEFTSHIFT] = 0xFF;
   304     if (ks & K_CTRL)
   305         gem_currentkeyboard[SCANCODE_LEFTCONTROL] = 0xFF;
   306     if (ks & K_ALT)
   307         gem_currentkeyboard[SCANCODE_LEFTALT] = 0xFF;
   308 }
   309 
   310 static void
   311 do_mouse(_THIS, short mx, short my, short mb, short ks)
   312 {
   313     static short prevmousex = 0, prevmousey = 0, prevmouseb = 0;
   314     short x2, y2, w2, h2;
   315 
   316     /* Don't return mouse events if out of window */
   317     if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) == 0) {
   318         return;
   319     }
   320 
   321     /* Retrieve window coords, and generate mouse events accordingly */
   322     x2 = y2 = 0;
   323     w2 = VDI_w;
   324     h2 = VDI_h;
   325     if ((!GEM_fullscreen) && (GEM_handle >= 0)) {
   326         wind_get(GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   327 
   328         /* Do not generate mouse button event if out of window working area */
   329         if ((mx < x2) || (mx >= x2 + w2) || (my < y2) || (my >= y2 + h2)) {
   330             mb = prevmouseb;
   331         }
   332     }
   333 
   334     /* Mouse motion ? */
   335     if (GEM_mouse_relative) {
   336         if (GEM_usedevmouse) {
   337             SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   338         } else {
   339             SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   340         }
   341     } else {
   342         if ((prevmousex != mx) || (prevmousey != my)) {
   343             int posx, posy;
   344 
   345             /* Give mouse position relative to window position */
   346             posx = mx - x2;
   347             if (posx < 0)
   348                 posx = 0;
   349             if (posx > w2)
   350                 posx = w2 - 1;
   351             posy = my - y2;
   352             if (posy < 0)
   353                 posy = 0;
   354             if (posy > h2)
   355                 posy = h2 - 1;
   356 
   357             SDL_PrivateMouseMotion(0, 0, posx, posy);
   358         }
   359         prevmousex = mx;
   360         prevmousey = my;
   361     }
   362 
   363     /* Mouse button ? */
   364     if (prevmouseb != mb) {
   365         int i;
   366 
   367         for (i = 0; i < 2; i++) {
   368             int curbutton, prevbutton;
   369 
   370             curbutton = mb & (1 << i);
   371             prevbutton = prevmouseb & (1 << i);
   372 
   373             if (curbutton && !prevbutton) {
   374                 SDL_PrivateMouseButton(SDL_PRESSED, i + 1, 0, 0);
   375             }
   376             if (!curbutton && prevbutton) {
   377                 SDL_PrivateMouseButton(SDL_RELEASED, i + 1, 0, 0);
   378             }
   379         }
   380         prevmouseb = mb;
   381     }
   382 
   383     /* Read special keys */
   384     if (ks & K_RSHIFT)
   385         gem_currentkeyboard[SCANCODE_RIGHTSHIFT] = 0xFF;
   386     if (ks & K_LSHIFT)
   387         gem_currentkeyboard[SCANCODE_LEFTSHIFT] = 0xFF;
   388     if (ks & K_CTRL)
   389         gem_currentkeyboard[SCANCODE_LEFTCONTROL] = 0xFF;
   390     if (ks & K_ALT)
   391         gem_currentkeyboard[SCANCODE_LEFTALT] = 0xFF;
   392 }
   393 
   394 /* vi: set ts=4 sw=4 expandtab: */