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