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