view src/events/scancodes_darwin.h @ 3069:caefe2344f65

Date: Thu, 27 Dec 2007 07:38:25 +0000 From: John Bartholomew Subject: [SDL] SDL Semaphore implementation broken on Windows? Hi, Over the past couple of days, I've been battling with SDL, SDL_Mixer and SMPEG to try to find an audio hang bug. I believe I've found the problem, which I think is a race condition inside SDL's semaphore implementation (at least the Windows implementation). The semaphore code uses Windows' built in semaphore functions, but it also maintains a separate count value. This count value is updated with bare increment and decrement operations in SemPost and SemWaitTimeout - no locking primitives to protect them. In tracking down the apparent audio bug, I found that at some point a semaphore's count value was being decremented to -1, which is clearly not a valid value for it to take. I'm still not certain exactly what sequence of operations is occuring for this to happen, but I believe that overall it's a race condition between a thread calling SemPost (which increments the count) and the thread on the other end calling SemWait (which decrements it). I will try to make a test case to verify this, but I'm not sure if I'll be able to (threading errors being difficult to reproduce even in the best circumstances). However, assuming this is the cause of my problems, there is a very simple fix: Windows provides InterlockedIncrement() and InterlockedDecrement() functions to perform increments and decrements which are guaranteed to be atomic. So the fix is in thread/win32/SDL_syssem.c: replace occurrences of --sem->count with InterlockedDecrement(&sem->count); and replace occurrences of ++sem->count with InterlockedIncrement(&sem->count); This is using SDL v1.2.12, built with VC++ 2008 Express, running on a Core 2 duo processor.
author Sam Lantinga <slouken@libsdl.org>
date Tue, 17 Feb 2009 05:39:18 +0000
parents 99210400e8b9
children dc1eb82ffdaa
line wrap: on
line source

/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2009 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/

/* Mac virtual key code to SDL scancode mapping table
   Sources:
   - Inside Macintosh: Text <http://developer.apple.com/documentation/mac/Text/Text-571.html>
   - Apple USB keyboard driver source <http://darwinsource.opendarwin.org/10.4.6.ppc/IOHIDFamily-172.8/IOHIDFamily/Cosmo_USB2ADB.c>
   - experimentation on various ADB and USB ISO keyboards and one ADB ANSI keyboard
*/
/* *INDENT-OFF* */
static SDL_scancode darwin_scancode_table[] = {
    /*   0 */   SDL_SCANCODE_A,
    /*   1 */   SDL_SCANCODE_S,
    /*   2 */   SDL_SCANCODE_D,
    /*   3 */   SDL_SCANCODE_F,
    /*   4 */   SDL_SCANCODE_H,
    /*   5 */   SDL_SCANCODE_G,
    /*   6 */   SDL_SCANCODE_Z,
    /*   7 */   SDL_SCANCODE_X,
    /*   8 */   SDL_SCANCODE_C,
    /*   9 */   SDL_SCANCODE_V,
    /*  10 */   SDL_SCANCODE_NONUSBACKSLASH, /* SDL_SCANCODE_NONUSBACKSLASH on ANSI and JIS keyboards (if this key would exist there), SDL_SCANCODE_GRAVE on ISO. (The USB keyboard driver actually translates these usage codes to different virtual key codes depending on whether the keyboard is ISO/ANSI/JIS. That's why you have to help it identify the keyboard type when you plug in a PC USB keyboard. It's a historical thing - ADB keyboards are wired this way.) */
    /*  11 */   SDL_SCANCODE_B,
    /*  12 */   SDL_SCANCODE_Q,
    /*  13 */   SDL_SCANCODE_W,
    /*  14 */   SDL_SCANCODE_E,
    /*  15 */   SDL_SCANCODE_R,
    /*  16 */   SDL_SCANCODE_Y,
    /*  17 */   SDL_SCANCODE_T,
    /*  18 */   SDL_SCANCODE_1,
    /*  19 */   SDL_SCANCODE_2,
    /*  20 */   SDL_SCANCODE_3,
    /*  21 */   SDL_SCANCODE_4,
    /*  22 */   SDL_SCANCODE_6,
    /*  23 */   SDL_SCANCODE_5,
    /*  24 */   SDL_SCANCODE_EQUALS,
    /*  25 */   SDL_SCANCODE_9,
    /*  26 */   SDL_SCANCODE_7,
    /*  27 */   SDL_SCANCODE_MINUS,
    /*  28 */   SDL_SCANCODE_8,
    /*  29 */   SDL_SCANCODE_0,
    /*  30 */   SDL_SCANCODE_RIGHTBRACKET,
    /*  31 */   SDL_SCANCODE_O,
    /*  32 */   SDL_SCANCODE_U,
    /*  33 */   SDL_SCANCODE_LEFTBRACKET,
    /*  34 */   SDL_SCANCODE_I,
    /*  35 */   SDL_SCANCODE_P,
    /*  36 */   SDL_SCANCODE_RETURN,
    /*  37 */   SDL_SCANCODE_L,
    /*  38 */   SDL_SCANCODE_J,
    /*  39 */   SDL_SCANCODE_APOSTROPHE,
    /*  40 */   SDL_SCANCODE_K,
    /*  41 */   SDL_SCANCODE_SEMICOLON,
    /*  42 */   SDL_SCANCODE_BACKSLASH,
    /*  43 */   SDL_SCANCODE_COMMA,
    /*  44 */   SDL_SCANCODE_SLASH,
    /*  45 */   SDL_SCANCODE_N,
    /*  46 */   SDL_SCANCODE_M,
    /*  47 */   SDL_SCANCODE_PERIOD,
    /*  48 */   SDL_SCANCODE_TAB,
    /*  49 */   SDL_SCANCODE_SPACE,
    /*  50 */   SDL_SCANCODE_GRAVE, /* SDL_SCANCODE_GRAVE on ANSI and JIS keyboards, SDL_SCANCODE_NONUSBACKSLASH on ISO (see comment about virtual key code 10 above) */
    /*  51 */   SDL_SCANCODE_BACKSPACE,
    /*  52 */   SDL_SCANCODE_KP_ENTER, /* keyboard enter on portables */
    /*  53 */   SDL_SCANCODE_ESCAPE,
    /*  54 */   SDL_SCANCODE_RGUI,
    /*  55 */   SDL_SCANCODE_LGUI,
    /*  56 */   SDL_SCANCODE_LSHIFT,
    /*  57 */   SDL_SCANCODE_CAPSLOCK,
    /*  58 */   SDL_SCANCODE_LALT,
    /*  59 */   SDL_SCANCODE_LCTRL,
    /*  60 */   SDL_SCANCODE_RSHIFT,
    /*  61 */   SDL_SCANCODE_RALT,
    /*  62 */   SDL_SCANCODE_RCTRL,
    /*  63 */   SDL_SCANCODE_RGUI, /* fn on portables, acts as a hardware-level modifier already, so we don't generate events for it, also XK_Meta_R */
    /*  64 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  65 */   SDL_SCANCODE_KP_PERIOD,
    /*  66 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  67 */   SDL_SCANCODE_KP_MULTIPLY,
    /*  68 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  69 */   SDL_SCANCODE_KP_PLUS,
    /*  70 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  71 */   SDL_SCANCODE_NUMLOCKCLEAR,
    /*  72 */   SDL_SCANCODE_VOLUMEUP,
    /*  73 */   SDL_SCANCODE_VOLUMEDOWN,
    /*  74 */   SDL_SCANCODE_MUTE,
    /*  75 */   SDL_SCANCODE_KP_DIVIDE,
    /*  76 */   SDL_SCANCODE_KP_ENTER, /* keypad enter on external keyboards, fn-return on portables */
    /*  77 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  78 */   SDL_SCANCODE_KP_MINUS,
    /*  79 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  80 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  81 */   SDL_SCANCODE_KP_EQUALS,
    /*  82 */   SDL_SCANCODE_KP_0,
    /*  83 */   SDL_SCANCODE_KP_1,
    /*  84 */   SDL_SCANCODE_KP_2,
    /*  85 */   SDL_SCANCODE_KP_3,
    /*  86 */   SDL_SCANCODE_KP_4,
    /*  87 */   SDL_SCANCODE_KP_5,
    /*  88 */   SDL_SCANCODE_KP_6,
    /*  89 */   SDL_SCANCODE_KP_7,
    /*  90 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /*  91 */   SDL_SCANCODE_KP_8,
    /*  92 */   SDL_SCANCODE_KP_9,
    /*  93 */   SDL_SCANCODE_INTERNATIONAL3, /* Cosmo_USB2ADB.c says "Yen (JIS)" */
    /*  94 */   SDL_SCANCODE_INTERNATIONAL1, /* Cosmo_USB2ADB.c says "Ro (JIS)" */
    /*  95 */   SDL_SCANCODE_KP_COMMA, /* Cosmo_USB2ADB.c says ", JIS only" */
    /*  96 */   SDL_SCANCODE_F5,
    /*  97 */   SDL_SCANCODE_F6,
    /*  98 */   SDL_SCANCODE_F7,
    /*  99 */   SDL_SCANCODE_F3,
    /* 100 */   SDL_SCANCODE_F8,
    /* 101 */   SDL_SCANCODE_F9,
    /* 102 */   SDL_SCANCODE_LANG2, /* Cosmo_USB2ADB.c says "Eisu" */
    /* 103 */   SDL_SCANCODE_F11,
    /* 104 */   SDL_SCANCODE_LANG1, /* Cosmo_USB2ADB.c says "Kana" */
    /* 105 */   SDL_SCANCODE_PRINTSCREEN, /* On ADB keyboards, this key is labeled "F13/print screen". Problem: USB has different usage codes for these two functions. On Apple USB keyboards, the key is labeled "F13" and sends the F13 usage code (SDL_SCANCODE_F13). I decided to use SDL_SCANCODE_PRINTSCREEN here nevertheless since SDL applications are more likely to assume the presence of a print screen key than an F13 key. */
    /* 106 */   SDL_SCANCODE_F16,
    /* 107 */   SDL_SCANCODE_SCROLLLOCK, /* F14/scroll lock, see comment about F13/print screen above */
    /* 108 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /* 109 */   SDL_SCANCODE_F10,
    /* 110 */   SDL_SCANCODE_APPLICATION, /* windows contextual menu key, fn-enter on portables */
    /* 111 */   SDL_SCANCODE_F12,
    /* 112 */   SDL_SCANCODE_UNKNOWN, /* unknown (unused?) */
    /* 113 */   SDL_SCANCODE_PAUSE, /* F15/pause, see comment about F13/print screen above */
    /* 114 */   SDL_SCANCODE_INSERT, /* the key is actually labeled "help" on Apple keyboards, and works as such in Mac OS, but it sends the "insert" usage code even on Apple USB keyboards */
    /* 115 */   SDL_SCANCODE_HOME,
    /* 116 */   SDL_SCANCODE_PAGEUP,
    /* 117 */   SDL_SCANCODE_DELETE,
    /* 118 */   SDL_SCANCODE_F4,
    /* 119 */   SDL_SCANCODE_END,
    /* 120 */   SDL_SCANCODE_F2,
    /* 121 */   SDL_SCANCODE_PAGEDOWN,
    /* 122 */   SDL_SCANCODE_F1,
    /* 123 */   SDL_SCANCODE_LEFT,
    /* 124 */   SDL_SCANCODE_RIGHT,
    /* 125 */   SDL_SCANCODE_DOWN,
    /* 126 */   SDL_SCANCODE_UP,
    /* 127 */   SDL_SCANCODE_POWER
};
/* *INDENT-ON* */