src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 13 Jul 2007 16:11:58 +0000
changeset 2186 6fe43f7efd94
parent 2043 adf732f1f016
child 2189 f54670a477bb
permissions -rw-r--r--
Simplify setting window title
     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         const char *window_name = (SDL_GetAppState() & SDL_APPACTIVE) ?
   160             GEM_title_name : GEM_icon_name;
   161         if (window_name) {
   162             wind_set(GEM_handle, WF_NAME,
   163                      (short) (((unsigned long) window_name) >> 16),
   164                      (short) (((unsigned long) window_name) & 0xffff), 0, 0);
   165         }
   166         GEM_refresh_name = SDL_FALSE;
   167     }
   168 }
   169 
   170 static int
   171 do_messages(_THIS, short *message)
   172 {
   173     int quit, posted;
   174     short x2, y2, w2, h2;
   175 
   176     quit = 0;
   177     switch (message[0]) {
   178     case WM_CLOSED:
   179     case AP_TERM:
   180         posted = SDL_PrivateQuit();
   181         quit = 1;
   182         break;
   183     case WM_MOVED:
   184         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   185                  message[6], message[7]);
   186         break;
   187     case WM_TOPPED:
   188         wind_set(message[3], WF_TOP, message[4], 0, 0, 0);
   189         /* Continue with TOP event processing */
   190     case WM_ONTOP:
   191         SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   192         if (VDI_setpalette) {
   193             VDI_setpalette(this, VDI_curpalette);
   194         }
   195         break;
   196     case WM_REDRAW:
   197         if (!GEM_lock_redraw) {
   198             GEM_wind_redraw(this, message[3], &message[4]);
   199         }
   200         break;
   201     case WM_ICONIFY:
   202     case WM_ALLICONIFY:
   203         wind_set(message[3], WF_ICONIFY, message[4], message[5],
   204                  message[6], message[7]);
   205         /* If we're active, make ourselves inactive */
   206         if (SDL_GetAppState() & SDL_APPACTIVE) {
   207             /* Send an internal deactivate event */
   208             SDL_PrivateAppActive(0, SDL_APPACTIVE);
   209         }
   210         /* Update window title */
   211         if (GEM_refresh_name && GEM_icon_name) {
   212             wind_set(GEM_handle, WF_NAME,
   213                      (short) (((unsigned long) GEM_icon_name) >> 16),
   214                      (short) (((unsigned long) GEM_icon_name) & 0xffff),
   215                      0, 0);
   216             GEM_refresh_name = SDL_FALSE;
   217         }
   218         break;
   219     case WM_UNICONIFY:
   220         wind_set(message[3], WF_UNICONIFY, message[4], message[5],
   221                  message[6], message[7]);
   222         /* If we're not active, make ourselves active */
   223         if (!(SDL_GetAppState() & SDL_APPACTIVE)) {
   224             /* Send an internal activate event */
   225             SDL_PrivateAppActive(1, SDL_APPACTIVE);
   226         }
   227         if (GEM_refresh_name && GEM_title_name) {
   228             wind_set(GEM_handle, WF_NAME,
   229                      (short) (((unsigned long) GEM_title_name) >> 16),
   230                      (short) (((unsigned long) GEM_title_name) & 0xffff),
   231                      0, 0);
   232             GEM_refresh_name = SDL_FALSE;
   233         }
   234         break;
   235     case WM_SIZED:
   236         wind_set(message[3], WF_CURRXYWH, message[4], message[5],
   237                  message[6], message[7]);
   238         wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   239         GEM_win_fulled = SDL_FALSE;     /* Cancel maximized flag */
   240         GEM_lock_redraw = SDL_TRUE;     /* Prevent redraw till buffers resized */
   241         SDL_PrivateResize(w2, h2);
   242         break;
   243     case WM_FULLED:
   244         {
   245             short x, y, w, h;
   246 
   247             if (GEM_win_fulled) {
   248                 wind_get(message[3], WF_PREVXYWH, &x, &y, &w, &h);
   249                 GEM_win_fulled = SDL_FALSE;
   250             } else {
   251                 x = GEM_desk_x;
   252                 y = GEM_desk_y;
   253                 w = GEM_desk_w;
   254                 h = GEM_desk_h;
   255                 GEM_win_fulled = SDL_TRUE;
   256             }
   257             wind_set(message[3], WF_CURRXYWH, x, y, w, h);
   258             wind_get(message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   259             GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
   260             SDL_PrivateResize(w2, h2);
   261         }
   262         break;
   263     case WM_BOTTOMED:
   264         wind_set(message[3], WF_BOTTOM, 0, 0, 0, 0);
   265         /* Continue with BOTTOM event processing */
   266     case WM_UNTOPPED:
   267         SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   268         if (VDI_setpalette) {
   269             VDI_setpalette(this, VDI_oldpalette);
   270         }
   271         break;
   272     }
   273 
   274     return quit;
   275 }
   276 
   277 static void
   278 do_keyboard(short kc, short ks)
   279 {
   280     int scancode, asciicode;
   281 
   282     if (kc) {
   283         scancode = (kc >> 8) & (ATARIBIOS_MAXKEYS - 1);
   284         gem_currentkeyboard[scancode] = 0xFF;
   285     }
   286 
   287     /* Read special keys */
   288     if (ks & K_RSHIFT)
   289         gem_currentkeyboard[SCANCODE_RIGHTSHIFT] = 0xFF;
   290     if (ks & K_LSHIFT)
   291         gem_currentkeyboard[SCANCODE_LEFTSHIFT] = 0xFF;
   292     if (ks & K_CTRL)
   293         gem_currentkeyboard[SCANCODE_LEFTCONTROL] = 0xFF;
   294     if (ks & K_ALT)
   295         gem_currentkeyboard[SCANCODE_LEFTALT] = 0xFF;
   296 }
   297 
   298 static void
   299 do_mouse(_THIS, short mx, short my, short mb, short ks)
   300 {
   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 }
   381 
   382 /* vi: set ts=4 sw=4 expandtab: */