src/video/photon/SDL_ph_events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Dec 2003 12:35:56 +0000
changeset 753 b14fdadd8311
parent 663 8bedd6d61642
child 769 b8d311d90021
permissions -rw-r--r--
Date: Thu, 4 Dec 2003 07:48:40 +0200
From: "Mike Gorchak"
Subject: SDL/QNX6 new patch

Here in attachment my patch for the SDL/QNX6 again :) It contain non-crtitical/cosmetic fixes:

1. Fixed window centering at other than the first consoles.
2. Fixed window centering algorithm in case when window height or width are greater than the desktop resolution.
3. Fixed window positioning on other than the first consoles.
4. Fixed occasional input focus lost when switching to fullscreen.
5. Removed the Photon's default chroma color for the overlays, added RGB(12, 6, 12) color instead (very dark pink).
6. Added more checks to the YUV overlay code (fixed crashes during resolution mode switches).
7. Added support for Enter/Backspace keys in unicode mode (used by Maelstrom and by other games).
8. Fixed window restore/maximize function. It works, finally.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
/* Handle the event stream, converting photon events into SDL events */
slouken@0
    29
slouken@19
    30
#define DISABLE_X11
slouken@19
    31
slouken@0
    32
#include <stdio.h>
slouken@0
    33
#include <setjmp.h>
slouken@663
    34
#include <sys/time.h>
slouken@663
    35
slouken@663
    36
#include <Ph.h>
slouken@0
    37
#include <photon/PkKeyDef.h>
slouken@0
    38
slouken@0
    39
#include "SDL.h"
slouken@0
    40
#include "SDL_syswm.h"
slouken@0
    41
#include "SDL_sysevents.h"
slouken@0
    42
#include "SDL_sysvideo.h"
slouken@0
    43
#include "SDL_events_c.h"
slouken@0
    44
#include "SDL_ph_video.h"
slouken@0
    45
#include "SDL_ph_modes_c.h"
slouken@0
    46
#include "SDL_ph_image_c.h"
slouken@0
    47
#include "SDL_ph_events_c.h"
slouken@663
    48
#include "SDL_phyuv_c.h"
slouken@663
    49
slouken@0
    50
slouken@0
    51
slouken@0
    52
/* The translation tables from a photon keysym to a SDL keysym */
slouken@0
    53
static SDLKey ODD_keymap[256];
slouken@0
    54
static SDLKey MISC_keymap[0xFF + 1];
slouken@0
    55
SDL_keysym *ph_TranslateKey(PhKeyEvent_t *key, SDL_keysym *keysym);
slouken@0
    56
slouken@0
    57
/* Check to see if this is a repeated key.
slouken@0
    58
   (idea shamelessly lifted from GII -- thanks guys! :)
slouken@0
    59
 */
slouken@0
    60
slouken@283
    61
static int ph_WarpedMotion(_THIS, PhEvent_t *winEvent)
slouken@283
    62
{
slouken@571
    63
    PhRect_t *rect = PhGetRects( winEvent );
slouken@266
    64
slouken@571
    65
    int centre_x, centre_y;
slouken@571
    66
    int dx, dy;
slouken@571
    67
    short abs_x, abs_y;
slouken@571
    68
    int posted;
slouken@0
    69
slouken@571
    70
    centre_x = SDL_VideoSurface->w / 2;
slouken@571
    71
    centre_y = SDL_VideoSurface->h / 2;
slouken@0
    72
slouken@571
    73
    dx = rect->ul.x - centre_x;
slouken@571
    74
    dy = rect->ul.y - centre_y;
slouken@283
    75
slouken@571
    76
    posted = SDL_PrivateMouseMotion( 0, 1, dx, dy );
slouken@283
    77
slouken@571
    78
    /* Move mouse cursor to middle of the window */
slouken@571
    79
    PtGetAbsPosition( window, &abs_x, &abs_y );
slouken@571
    80
    PhMoveCursorAbs(PhInputGroup(NULL), abs_x + centre_x, abs_y + centre_y);
slouken@283
    81
slouken@571
    82
    return (posted);
slouken@0
    83
}
slouken@0
    84
slouken@220
    85
/* Control which motion flags the window has set, a flags value of -1 sets
slouken@220
    86
 * MOTION_BUTTON and MOTION_NOBUTTON */
slouken@266
    87
slouken@220
    88
static void set_motion_sensitivity(_THIS, unsigned int flags)
slouken@220
    89
{
slouken@571
    90
    int rid;
slouken@571
    91
    int fields = Ph_EV_PTR_MOTION_BUTTON | Ph_EV_PTR_MOTION_NOBUTTON;
slouken@571
    92
    PhRegion_t region;
slouken@220
    93
slouken@571
    94
    if( window )
slouken@571
    95
    {
slouken@663
    96
        rid = PtWidgetRid(window);
slouken@663
    97
        if( rid != 0 && PhRegionQuery(rid, &region, NULL, NULL, 0) == 0 )
slouken@571
    98
        {
slouken@571
    99
            region.events_sense=(region.events_sense & ~fields)|(flags & fields);
slouken@571
   100
            PhRegionChange(Ph_REGION_EV_SENSE, 0, &region, NULL, NULL);
slouken@571
   101
        }
slouken@571
   102
    }
slouken@220
   103
}
slouken@220
   104
slouken@220
   105
/* Convert the photon button state value to an SDL value */
slouken@571
   106
static Uint8 ph2sdl_mousebutton(unsigned short button_state)
slouken@220
   107
{
slouken@571
   108
    Uint8 mouse_button = 0;
slouken@220
   109
slouken@571
   110
    if (button_state & Ph_BUTTON_SELECT)
slouken@571
   111
        mouse_button |= SDL_BUTTON_LEFT;
slouken@571
   112
    if (button_state & Ph_BUTTON_MENU)
slouken@571
   113
        mouse_button |= SDL_BUTTON_RIGHT;
slouken@571
   114
    if (button_state & Ph_BUTTON_ADJUST)
slouken@571
   115
        mouse_button |= SDL_BUTTON_MIDDLE;
slouken@220
   116
slouken@571
   117
    return (mouse_button);
slouken@220
   118
}
slouken@220
   119
slouken@663
   120
//                   void* PtAppCreateContext();
slouken@663
   121
slouken@0
   122
static int ph_DispatchEvent(_THIS)
slouken@0
   123
{
slouken@320
   124
    int posted;
slouken@320
   125
    PhRect_t* rect;
slouken@320
   126
    PhPointerEvent_t* pointerEvent;
slouken@320
   127
    PhKeyEvent_t* keyEvent;
slouken@320
   128
    PhWindowEvent_t* winEvent;
slouken@320
   129
    int i, buttons;
slouken@320
   130
    SDL_Rect sdlrects[50]; 
slouken@0
   131
	
slouken@320
   132
    posted = 0;
slouken@0
   133
	
slouken@320
   134
    switch (event->type)
slouken@320
   135
    {
slouken@320
   136
        case Ph_EV_BOUNDARY:
slouken@320
   137
        {
slouken@320
   138
            if (event->subtype == Ph_EV_PTR_ENTER)
slouken@320
   139
            {
slouken@320
   140
                posted = SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
slouken@320
   141
            }
slouken@320
   142
            else if (event->subtype ==Ph_EV_PTR_LEAVE)
slouken@320
   143
            {
slouken@320
   144
                posted = SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);	
slouken@320
   145
            }
slouken@320
   146
        }
slouken@320
   147
        break;
slouken@320
   148
slouken@320
   149
        case Ph_EV_PTR_MOTION_BUTTON:
slouken@320
   150
        case Ph_EV_PTR_MOTION_NOBUTTON:
slouken@320
   151
        {
slouken@320
   152
            if (SDL_VideoSurface)
slouken@320
   153
            {
slouken@320
   154
                pointerEvent = PhGetData(event);
slouken@320
   155
                rect = PhGetRects(event);
slouken@320
   156
slouken@320
   157
                if (mouse_relative)
slouken@320
   158
                {
slouken@320
   159
                    posted = ph_WarpedMotion(this, event);
slouken@320
   160
                }
slouken@320
   161
                else
slouken@320
   162
                {
slouken@320
   163
                    posted = SDL_PrivateMouseMotion(0, 0, rect->ul.x, rect->ul.y);
slouken@320
   164
                }
slouken@320
   165
            }
slouken@320
   166
        }
slouken@320
   167
        break;
slouken@320
   168
slouken@320
   169
        case Ph_EV_BUT_PRESS:
slouken@320
   170
        {
slouken@320
   171
            pointerEvent = PhGetData( event );
slouken@320
   172
            buttons = ph2sdl_mousebutton( pointerEvent->buttons );
slouken@320
   173
            if (buttons != 0)
slouken@320
   174
            {
slouken@320
   175
                posted = SDL_PrivateMouseButton(SDL_PRESSED, buttons, 0, 0);
slouken@320
   176
            }
slouken@320
   177
        }
slouken@320
   178
        break;
slouken@320
   179
slouken@320
   180
        case Ph_EV_BUT_RELEASE:
slouken@320
   181
        {
slouken@320
   182
            pointerEvent = PhGetData(event);
slouken@320
   183
            buttons = ph2sdl_mousebutton(pointerEvent->buttons);
slouken@320
   184
            if (event->subtype == Ph_EV_RELEASE_REAL && buttons != 0)
slouken@320
   185
            {
slouken@320
   186
                posted = SDL_PrivateMouseButton(SDL_RELEASED, buttons, 0, 0);
slouken@320
   187
            }
slouken@320
   188
            else if(event->subtype == Ph_EV_RELEASE_PHANTOM)
slouken@320
   189
            {
slouken@320
   190
                /* If the mouse is outside the window,
slouken@320
   191
                 * only a phantom release event is sent, so
slouken@320
   192
                 * check if the window doesn't have mouse focus.
slouken@320
   193
                 * Not perfect, maybe checking the mouse button
slouken@320
   194
                 * state for Ph_EV_BOUNDARY events would be
slouken@320
   195
                 * better. */
slouken@320
   196
                if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS) == 0)
slouken@0
   197
		{
slouken@320
   198
                    posted = SDL_PrivateMouseButton(SDL_RELEASED, buttons, 0, 0);
slouken@320
   199
                }
slouken@320
   200
            }
slouken@320
   201
        }
slouken@320
   202
        break;
slouken@0
   203
slouken@320
   204
        case Ph_EV_WM:
slouken@320
   205
        {
slouken@320
   206
            winEvent = PhGetData(event);
slouken@0
   207
slouken@320
   208
            /* losing focus */
slouken@320
   209
            if ((winEvent->event_f==Ph_WM_FOCUS) && (winEvent->event_state==Ph_WM_EVSTATE_FOCUSLOST))
slouken@320
   210
            {
slouken@320
   211
                set_motion_sensitivity(this, Ph_EV_PTR_MOTION_BUTTON);
slouken@320
   212
                posted = SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);	
slouken@320
   213
            }
slouken@320
   214
            /* gaining focus */
slouken@320
   215
            else if ((winEvent->event_f==Ph_WM_FOCUS) && (winEvent->event_state==Ph_WM_EVSTATE_FOCUS))
slouken@320
   216
            {
slouken@320
   217
                set_motion_sensitivity(this, -1);
slouken@320
   218
                posted = SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
slouken@320
   219
            }
slouken@753
   220
            /* quit request */
slouken@320
   221
            else if (winEvent->event_f==Ph_WM_CLOSE)
slouken@320
   222
            {
slouken@320
   223
                posted = SDL_PrivateQuit();
slouken@320
   224
            }
slouken@753
   225
            /* hide/unhide request */
slouken@663
   226
            else if (winEvent->event_f==Ph_WM_HIDE)
slouken@663
   227
            {
slouken@663
   228
                if (currently_hided)
slouken@663
   229
                {
slouken@663
   230
                   /* got unhide window event                                */
slouken@663
   231
                   /* TODO: restore application's palette if in palette mode */
slouken@663
   232
                   currently_hided=0;
slouken@663
   233
                }
slouken@663
   234
                else
slouken@663
   235
                {
slouken@663
   236
                   /* got hide window event                                  */
slouken@663
   237
                   /* TODO: restore original palette if in palette mode      */
slouken@663
   238
                   currently_hided=1;
slouken@663
   239
                }
slouken@663
   240
            }
slouken@320
   241
            /* request to resize */
slouken@320
   242
            else if (winEvent->event_f==Ph_WM_RESIZE)
slouken@320
   243
            {
slouken@753
   244
                SDL_PrivateResize(winEvent->size.w+1, winEvent->size.h+1);
slouken@320
   245
            }
slouken@663
   246
            /* request to move */
slouken@663
   247
            else if (winEvent->event_f==Ph_WM_MOVE)
slouken@663
   248
            {
slouken@663
   249
                if (current_overlay!=NULL)
slouken@663
   250
                {
slouken@663
   251
                   int lockedstate=current_overlay->hwdata->locked;
slouken@663
   252
                   int chromastate=current_overlay->hwdata->ischromakey;
slouken@753
   253
                   int error;
slouken@663
   254
                   SDL_Rect target;
slouken@663
   255
slouken@663
   256
                   current_overlay->hwdata->locked=1;
slouken@663
   257
                   target.x=current_overlay->hwdata->CurrentViewPort.pos.x;
slouken@663
   258
                   target.y=current_overlay->hwdata->CurrentViewPort.pos.y;
slouken@663
   259
                   target.w=current_overlay->hwdata->CurrentViewPort.size.w;
slouken@663
   260
                   target.h=current_overlay->hwdata->CurrentViewPort.size.h;
slouken@663
   261
                   current_overlay->hwdata->ischromakey=0;
slouken@753
   262
                   error=ph_DisplayYUVOverlay(this, current_overlay, &target);
slouken@753
   263
                   if (!error)
slouken@753
   264
                   {
slouken@753
   265
                       current_overlay->hwdata->ischromakey=chromastate;
slouken@753
   266
                       current_overlay->hwdata->locked=lockedstate;
slouken@753
   267
                   }
slouken@663
   268
                }
slouken@663
   269
            }
slouken@753
   270
            /* maximize request */
slouken@320
   271
            else if (winEvent->event_f==Ph_WM_MAX)
slouken@320
   272
            {
slouken@663
   273
                /* window already moved and resized here */
slouken@753
   274
                currently_maximized=1;
slouken@663
   275
            }
slouken@753
   276
            /* restore request */
slouken@663
   277
            else if (winEvent->event_f==Ph_WM_RESTORE)
slouken@663
   278
            {
slouken@753
   279
                /* window already moved and resized here */
slouken@753
   280
                currently_maximized=0;
slouken@320
   281
            }
slouken@320
   282
        }
slouken@320
   283
        break;
slouken@0
   284
slouken@320
   285
        /* window has been resized, moved or removed */
slouken@320
   286
        case Ph_EV_EXPOSE:
slouken@320
   287
        {
slouken@663
   288
            if (event->num_rects!=0)
slouken@320
   289
            {
slouken@663
   290
                if (SDL_VideoSurface)
slouken@663
   291
                {
slouken@663
   292
                    rect = PhGetRects(event);
slouken@0
   293
slouken@753
   294
                    for(i=0; i<event->num_rects; i++)
slouken@663
   295
                    {
slouken@663
   296
                        sdlrects[i].x = rect[i].ul.x;
slouken@663
   297
                        sdlrects[i].y = rect[i].ul.y;
slouken@663
   298
                        sdlrects[i].w = rect[i].lr.x - rect[i].ul.x + 1;
slouken@663
   299
                        sdlrects[i].h = rect[i].lr.y - rect[i].ul.y + 1;
slouken@663
   300
                    }
slouken@663
   301
slouken@663
   302
                    this->UpdateRects(this, event->num_rects, sdlrects);
slouken@663
   303
slouken@663
   304
                    if (current_overlay!=NULL)
slouken@663
   305
                    {
slouken@663
   306
                        int lockedstate=current_overlay->hwdata->locked;
slouken@753
   307
                        int error;
slouken@663
   308
                        SDL_Rect target;
slouken@663
   309
slouken@663
   310
                        current_overlay->hwdata->locked=1;
slouken@663
   311
                        target.x=current_overlay->hwdata->CurrentViewPort.pos.x;
slouken@663
   312
                        target.y=current_overlay->hwdata->CurrentViewPort.pos.y;
slouken@663
   313
                        target.w=current_overlay->hwdata->CurrentViewPort.size.w;
slouken@663
   314
                        target.h=current_overlay->hwdata->CurrentViewPort.size.h;
slouken@663
   315
                        current_overlay->hwdata->forcedredraw=1;
slouken@753
   316
                        error=ph_DisplayYUVOverlay(this, current_overlay, &target);
slouken@753
   317
                        if (!error)
slouken@753
   318
                        {
slouken@753
   319
                            current_overlay->hwdata->forcedredraw=0;
slouken@753
   320
                            current_overlay->hwdata->locked=lockedstate;
slouken@753
   321
                        }
slouken@663
   322
                    }
slouken@320
   323
                }
slouken@320
   324
            }
slouken@320
   325
        }
slouken@320
   326
	break;
slouken@0
   327
slouken@320
   328
        case Ph_EV_KEY:
slouken@320
   329
        {
slouken@320
   330
            SDL_keysym keysym;
slouken@283
   331
slouken@320
   332
            posted = 0;
slouken@0
   333
slouken@320
   334
            keyEvent = PhGetData( event );
slouken@220
   335
slouken@320
   336
            if (Pk_KF_Key_Down & keyEvent->key_flags)
slouken@320
   337
            {
slouken@663
   338
                /* split the wheel events from real key events */
slouken@663
   339
                if ((keyEvent->key_cap==Pk_Up) && (keyEvent->key_scan==0) && ((keyEvent->key_flags & Pk_KF_Scan_Valid)==Pk_KF_Scan_Valid))
slouken@663
   340
                {
slouken@663
   341
                   posted = SDL_PrivateMouseButton(SDL_PRESSED, SDL_BUTTON_WHEELUP, 0, 0);
slouken@663
   342
                   break;
slouken@663
   343
                }
slouken@663
   344
                if ((keyEvent->key_cap==Pk_Down) && (keyEvent->key_scan==0) && ((keyEvent->key_flags & Pk_KF_Scan_Valid)==Pk_KF_Scan_Valid))
slouken@663
   345
                {
slouken@663
   346
                   posted = SDL_PrivateMouseButton(SDL_PRESSED, SDL_BUTTON_WHEELDOWN, 0, 0);
slouken@663
   347
                   break;
slouken@663
   348
                }
slouken@320
   349
                posted = SDL_PrivateKeyboard(SDL_PRESSED, ph_TranslateKey(keyEvent, &keysym));
slouken@320
   350
            }
slouken@320
   351
            else /* must be key release */
slouken@320
   352
            {
slouken@663
   353
                /* split the wheel events from real key events */
slouken@663
   354
                if ((keyEvent->key_cap==Pk_Up) && (keyEvent->key_scan==0) && ((keyEvent->key_flags & Pk_KF_Scan_Valid)==Pk_KF_Scan_Valid))
slouken@663
   355
                {
slouken@663
   356
                   posted = SDL_PrivateMouseButton(SDL_RELEASED, SDL_BUTTON_WHEELUP, 0, 0);
slouken@663
   357
                   break;
slouken@663
   358
                }
slouken@663
   359
                if ((keyEvent->key_cap==Pk_Down) && (keyEvent->key_scan==0) && ((keyEvent->key_flags & Pk_KF_Scan_Valid)==Pk_KF_Scan_Valid))
slouken@663
   360
                {
slouken@663
   361
                   posted = SDL_PrivateMouseButton(SDL_RELEASED, SDL_BUTTON_WHEELDOWN, 0, 0);
slouken@663
   362
                   break;
slouken@663
   363
                }
slouken@320
   364
                posted = SDL_PrivateKeyboard(SDL_RELEASED, ph_TranslateKey( keyEvent, &keysym));
slouken@320
   365
            }
slouken@320
   366
        }
slouken@320
   367
        break;
slouken@753
   368
        
slouken@753
   369
        case Ph_EV_INFO:
slouken@753
   370
        {
slouken@753
   371
        }
slouken@753
   372
        break;
slouken@320
   373
    }
slouken@0
   374
slouken@320
   375
    return(posted);
slouken@0
   376
}
slouken@0
   377
slouken@0
   378
/* perform a blocking read if no events available */
slouken@0
   379
int ph_Pending(_THIS)
slouken@0
   380
{
slouken@571
   381
    /* Flush the display connection and look to see if events are queued */
slouken@571
   382
    PgFlush();
slouken@0
   383
slouken@663
   384
    while (1)
slouken@663
   385
    {
slouken@663
   386
        switch(PhEventPeek(event, EVENT_SIZE))
slouken@571
   387
        {
slouken@571
   388
            case Ph_EVENT_MSG:
slouken@0
   389
                 return 1;
slouken@0
   390
                 break;
slouken@571
   391
            case -1:
slouken@571
   392
                 perror("ph_Pending(): PhEventNext failed");
slouken@0
   393
                 break;
slouken@571
   394
            default:
slouken@571
   395
                 return 0;
slouken@0
   396
        }
slouken@0
   397
    }
slouken@571
   398
slouken@571
   399
    /* Oh well, nothing is ready .. */
slouken@571
   400
    return(0);
slouken@0
   401
}
slouken@0
   402
slouken@0
   403
void ph_PumpEvents(_THIS)
slouken@0
   404
{
slouken@571
   405
    /* Flush the display connection and look to see if events are queued */
slouken@571
   406
    PgFlush();
slouken@0
   407
slouken@571
   408
    while (ph_Pending(this))
slouken@571
   409
    {
slouken@663
   410
        PtEventHandler(event);
slouken@571
   411
        ph_DispatchEvent(this);
slouken@571
   412
    }
slouken@0
   413
}
slouken@0
   414
slouken@0
   415
void ph_InitKeymap(void)
slouken@0
   416
{
slouken@571
   417
    int i;
slouken@0
   418
slouken@571
   419
    /* Odd keys used in international keyboards */
slouken@571
   420
    for (i=0; i<SDL_TABLESIZE(ODD_keymap); ++i)
slouken@663
   421
    {
slouken@571
   422
        ODD_keymap[i] = SDLK_UNKNOWN;
slouken@663
   423
    }
slouken@0
   424
slouken@571
   425
    /* Map the miscellaneous keys */
slouken@571
   426
    for (i=0; i<SDL_TABLESIZE(MISC_keymap); ++i)
slouken@663
   427
    {
slouken@571
   428
        MISC_keymap[i] = SDLK_UNKNOWN;
slouken@663
   429
    }
slouken@0
   430
slouken@571
   431
    MISC_keymap[Pk_BackSpace&0xFF] = SDLK_BACKSPACE;
slouken@571
   432
    MISC_keymap[Pk_Tab&0xFF] = SDLK_TAB;
slouken@571
   433
    MISC_keymap[Pk_Clear&0xFF] = SDLK_CLEAR;
slouken@571
   434
    MISC_keymap[Pk_Return&0xFF] = SDLK_RETURN;
slouken@571
   435
    MISC_keymap[Pk_Pause&0xFF] = SDLK_PAUSE;
slouken@571
   436
    MISC_keymap[Pk_Escape&0xFF] = SDLK_ESCAPE;
slouken@571
   437
    MISC_keymap[Pk_Delete&0xFF] = SDLK_DELETE;
slouken@0
   438
slouken@571
   439
    MISC_keymap[Pk_KP_0&0xFF] = SDLK_KP0;
slouken@571
   440
    MISC_keymap[Pk_KP_1&0xFF] = SDLK_KP1;
slouken@571
   441
    MISC_keymap[Pk_KP_2&0xFF] = SDLK_KP2;
slouken@571
   442
    MISC_keymap[Pk_KP_3&0xFF] = SDLK_KP3;
slouken@571
   443
    MISC_keymap[Pk_KP_4&0xFF] = SDLK_KP4;
slouken@571
   444
    MISC_keymap[Pk_KP_5&0xFF] = SDLK_KP5;
slouken@571
   445
    MISC_keymap[Pk_KP_6&0xFF] = SDLK_KP6;
slouken@571
   446
    MISC_keymap[Pk_KP_7&0xFF] = SDLK_KP7;
slouken@571
   447
    MISC_keymap[Pk_KP_8&0xFF] = SDLK_KP8;
slouken@571
   448
    MISC_keymap[Pk_KP_9&0xFF] = SDLK_KP9;
slouken@0
   449
slouken@571
   450
    MISC_keymap[Pk_KP_Decimal&0xFF] = SDLK_KP_PERIOD;
slouken@571
   451
    MISC_keymap[Pk_KP_Divide&0xFF] = SDLK_KP_DIVIDE;
slouken@571
   452
    MISC_keymap[Pk_KP_Multiply&0xFF] = SDLK_KP_MULTIPLY;
slouken@571
   453
    MISC_keymap[Pk_KP_Subtract&0xFF] = SDLK_KP_MINUS;
slouken@571
   454
    MISC_keymap[Pk_KP_Add&0xFF] = SDLK_KP_PLUS;
slouken@571
   455
    MISC_keymap[Pk_KP_Enter&0xFF] = SDLK_KP_ENTER;
slouken@571
   456
    MISC_keymap[Pk_KP_Equal&0xFF] = SDLK_KP_EQUALS;
slouken@0
   457
slouken@571
   458
    MISC_keymap[Pk_Up&0xFF] = SDLK_UP;
slouken@571
   459
    MISC_keymap[Pk_Down&0xFF] = SDLK_DOWN;
slouken@571
   460
    MISC_keymap[Pk_Right&0xFF] = SDLK_RIGHT;
slouken@571
   461
    MISC_keymap[Pk_Left&0xFF] = SDLK_LEFT;
slouken@571
   462
    MISC_keymap[Pk_Insert&0xFF] = SDLK_INSERT;
slouken@571
   463
    MISC_keymap[Pk_Home&0xFF] = SDLK_HOME;
slouken@571
   464
    MISC_keymap[Pk_End&0xFF] = SDLK_END;
slouken@571
   465
    MISC_keymap[Pk_Pg_Up&0xFF] = SDLK_PAGEUP;
slouken@571
   466
    MISC_keymap[Pk_Pg_Down&0xFF] = SDLK_PAGEDOWN;
slouken@0
   467
slouken@571
   468
    MISC_keymap[Pk_F1&0xFF] = SDLK_F1;
slouken@571
   469
    MISC_keymap[Pk_F2&0xFF] = SDLK_F2;
slouken@571
   470
    MISC_keymap[Pk_F3&0xFF] = SDLK_F3;
slouken@571
   471
    MISC_keymap[Pk_F4&0xFF] = SDLK_F4;
slouken@571
   472
    MISC_keymap[Pk_F5&0xFF] = SDLK_F5;
slouken@571
   473
    MISC_keymap[Pk_F6&0xFF] = SDLK_F6;
slouken@571
   474
    MISC_keymap[Pk_F7&0xFF] = SDLK_F7;
slouken@571
   475
    MISC_keymap[Pk_F8&0xFF] = SDLK_F8;
slouken@571
   476
    MISC_keymap[Pk_F9&0xFF] = SDLK_F9;
slouken@571
   477
    MISC_keymap[Pk_F10&0xFF] = SDLK_F10;
slouken@571
   478
    MISC_keymap[Pk_F11&0xFF] = SDLK_F11;
slouken@571
   479
    MISC_keymap[Pk_F12&0xFF] = SDLK_F12;
slouken@571
   480
    MISC_keymap[Pk_F13&0xFF] = SDLK_F13;
slouken@571
   481
    MISC_keymap[Pk_F14&0xFF] = SDLK_F14;
slouken@571
   482
    MISC_keymap[Pk_F15&0xFF] = SDLK_F15;
slouken@0
   483
slouken@571
   484
    MISC_keymap[Pk_Num_Lock&0xFF] = SDLK_NUMLOCK;
slouken@571
   485
    MISC_keymap[Pk_Caps_Lock&0xFF] = SDLK_CAPSLOCK;
slouken@571
   486
    MISC_keymap[Pk_Scroll_Lock&0xFF] = SDLK_SCROLLOCK;
slouken@571
   487
    MISC_keymap[Pk_Shift_R&0xFF] = SDLK_RSHIFT;
slouken@571
   488
    MISC_keymap[Pk_Shift_L&0xFF] = SDLK_LSHIFT;
slouken@571
   489
    MISC_keymap[Pk_Control_R&0xFF] = SDLK_RCTRL;
slouken@571
   490
    MISC_keymap[Pk_Control_L&0xFF] = SDLK_LCTRL;
slouken@571
   491
    MISC_keymap[Pk_Alt_R&0xFF] = SDLK_RALT;
slouken@571
   492
    MISC_keymap[Pk_Alt_L&0xFF] = SDLK_LALT;
slouken@571
   493
    MISC_keymap[Pk_Meta_R&0xFF] = SDLK_RMETA;
slouken@571
   494
    MISC_keymap[Pk_Meta_L&0xFF] = SDLK_LMETA;
slouken@663
   495
    MISC_keymap[Pk_Super_L&0xFF] = SDLK_LSUPER;
slouken@663
   496
    MISC_keymap[Pk_Super_R&0xFF] = SDLK_RSUPER;
slouken@571
   497
    MISC_keymap[Pk_Mode_switch&0xFF] = SDLK_MODE; /* "Alt Gr" key    */
slouken@0
   498
slouken@571
   499
    MISC_keymap[Pk_Help&0xFF] = SDLK_HELP;
slouken@571
   500
    MISC_keymap[Pk_Print&0xFF] = SDLK_PRINT;
slouken@571
   501
    MISC_keymap[Pk_Break&0xFF] = SDLK_BREAK;
slouken@663
   502
    MISC_keymap[Pk_Menu&0xFF] = SDLK_MENU;        /* Windows "Menu" key */
slouken@663
   503
slouken@663
   504
    MISC_keymap[Pk_Hyper_R&0xFF] = SDLK_RSUPER;   /* Right "Windows" */
slouken@663
   505
slouken@663
   506
    /* Left "Windows" key, but it can't be catched by application */
slouken@663
   507
    MISC_keymap[Pk_Hyper_L&0xFF] = SDLK_LSUPER;
slouken@0
   508
}
slouken@0
   509
slouken@0
   510
static unsigned long cap;
slouken@0
   511
slouken@0
   512
SDL_keysym *ph_TranslateKey(PhKeyEvent_t *key, SDL_keysym *keysym)
slouken@0
   513
{
slouken@571
   514
    /* 'sym' is set to the value of the key with modifiers applied to it.
slouken@571
   515
       This member is valid only if Pk_KF_Sym_Valid is set in the key_flags.
slouken@571
   516
       We will assume it is valid. */
slouken@283
   517
slouken@571
   518
    /* FIXME: This needs to check whether the cap & scancode is valid */
slouken@283
   519
slouken@571
   520
    cap = key->key_cap;
slouken@571
   521
slouken@571
   522
    switch (cap>>8)
slouken@571
   523
    {
slouken@571
   524
        case 0x00:  /* Latin 1 */
slouken@571
   525
        case 0x01:  /* Latin 2 */
slouken@571
   526
        case 0x02:  /* Latin 3 */
slouken@571
   527
        case 0x03:  /* Latin 4 */
slouken@571
   528
        case 0x04:  /* Katakana */
slouken@571
   529
        case 0x05:  /* Arabic */
slouken@571
   530
        case 0x06:  /* Cyrillic */
slouken@571
   531
        case 0x07:  /* Greek */
slouken@571
   532
        case 0x08:  /* Technical */
slouken@571
   533
        case 0x0A:  /* Publishing */
slouken@571
   534
        case 0x0C:  /* Hebrew */
slouken@571
   535
        case 0x0D:  /* Thai */
slouken@571
   536
                   keysym->sym = (SDLKey)(cap&0xFF);
slouken@571
   537
                   /* Map capital letter syms to lowercase */
slouken@571
   538
                   if ((keysym->sym >= 'A')&&(keysym->sym <= 'Z'))
slouken@571
   539
                       keysym->sym += ('a'-'A');
slouken@571
   540
                   break;
slouken@571
   541
        case 0xF0:
slouken@571
   542
                   keysym->sym = MISC_keymap[cap&0xFF];
slouken@571
   543
                   break;
slouken@571
   544
        default:
slouken@571
   545
                   keysym->sym = SDLK_UNKNOWN;                
slouken@571
   546
                   break;
slouken@571
   547
    }
slouken@571
   548
slouken@571
   549
    keysym->scancode = key->key_scan;
slouken@571
   550
    keysym->unicode = 0;
slouken@571
   551
slouken@571
   552
    if (SDL_TranslateUNICODE)
slouken@571
   553
    {
slouken@571
   554
        char utf8[MB_CUR_MAX];
slouken@571
   555
        int utf8len;
slouken@571
   556
        wchar_t unicode;
slouken@571
   557
slouken@663
   558
        switch (keysym->scancode)
slouken@571
   559
        {
slouken@753
   560
           /* Esc key */
slouken@663
   561
           case 0x01: keysym->unicode = 27;
slouken@663
   562
                      break;
slouken@753
   563
           /* BackSpace key */
slouken@753
   564
           case 0x0E: keysym->unicode = 127;
slouken@753
   565
                      break;
slouken@753
   566
           /* Enter key */
slouken@753
   567
           case 0x1C: keysym->unicode = 10;
slouken@753
   568
                      break;
slouken@663
   569
           default:
slouken@663
   570
                      utf8len = PhKeyToMb(utf8, key);
slouken@663
   571
                      if (utf8len > 0)
slouken@663
   572
                      {
slouken@753
   573
                         utf8len = mbtowc(&unicode, utf8, utf8len);
slouken@663
   574
                         if (utf8len > 0)
slouken@663
   575
                         {
slouken@663
   576
                             keysym->unicode = unicode;
slouken@663
   577
                         }
slouken@663
   578
                      }
slouken@663
   579
                      break;
slouken@571
   580
        }
slouken@663
   581
slouken@571
   582
    }
slouken@571
   583
slouken@571
   584
    return (keysym);
slouken@0
   585
}
slouken@0
   586
slouken@0
   587
void ph_InitOSKeymap(_THIS)
slouken@0
   588
{
slouken@571
   589
    ph_InitKeymap();
slouken@0
   590
}