DSCF

.co.uk

PC32

A ColorForth compiler and editor for x86-64
git clone https://dscf.co.uk/clone/PC32.git
Log | Files | Refs | README

input.c (37983B)


      1 //========================================================================
      2 // GLFW 3.3 - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2002-2006 Marcus Geelnard
      5 // Copyright (c) 2006-2019 Camilla Löwy <elmindreda@glfw.org>
      6 //
      7 // This software is provided 'as-is', without any express or implied
      8 // warranty. In no event will the authors be held liable for any damages
      9 // arising from the use of this software.
     10 //
     11 // Permission is granted to anyone to use this software for any purpose,
     12 // including commercial applications, and to alter it and redistribute it
     13 // freely, subject to the following restrictions:
     14 //
     15 // 1. The origin of this software must not be misrepresented; you must not
     16 //    claim that you wrote the original software. If you use this software
     17 //    in a product, an acknowledgment in the product documentation would
     18 //    be appreciated but is not required.
     19 //
     20 // 2. Altered source versions must be plainly marked as such, and must not
     21 //    be misrepresented as being the original software.
     22 //
     23 // 3. This notice may not be removed or altered from any source
     24 //    distribution.
     25 //
     26 //========================================================================
     27 // Please use C89 style variable declarations in this file because VS 2010
     28 //========================================================================
     29 
     30 #include "internal.h"
     31 #include "mappings.h"
     32 
     33 #include <assert.h>
     34 #include <float.h>
     35 #include <math.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 
     39 // Internal key state used for sticky keys
     40 #define _GLFW_STICK 3
     41 
     42 // Internal constants for gamepad mapping source types
     43 #define _GLFW_JOYSTICK_AXIS     1
     44 #define _GLFW_JOYSTICK_BUTTON   2
     45 #define _GLFW_JOYSTICK_HATBIT   3
     46 
     47 // Finds a mapping based on joystick GUID
     48 //
     49 static _GLFWmapping* findMapping(const char* guid)
     50 {
     51     int i;
     52 
     53     for (i = 0;  i < _glfw.mappingCount;  i++)
     54     {
     55         if (strcmp(_glfw.mappings[i].guid, guid) == 0)
     56             return _glfw.mappings + i;
     57     }
     58 
     59     return NULL;
     60 }
     61 
     62 // Checks whether a gamepad mapping element is present in the hardware
     63 //
     64 static GLFWbool isValidElementForJoystick(const _GLFWmapelement* e,
     65                                           const _GLFWjoystick* js)
     66 {
     67     if (e->type == _GLFW_JOYSTICK_HATBIT && (e->index >> 4) >= js->hatCount)
     68         return GLFW_FALSE;
     69     else if (e->type == _GLFW_JOYSTICK_BUTTON && e->index >= js->buttonCount)
     70         return GLFW_FALSE;
     71     else if (e->type == _GLFW_JOYSTICK_AXIS && e->index >= js->axisCount)
     72         return GLFW_FALSE;
     73 
     74     return GLFW_TRUE;
     75 }
     76 
     77 // Finds a mapping based on joystick GUID and verifies element indices
     78 //
     79 static _GLFWmapping* findValidMapping(const _GLFWjoystick* js)
     80 {
     81     _GLFWmapping* mapping = findMapping(js->guid);
     82     if (mapping)
     83     {
     84         int i;
     85 
     86         for (i = 0;  i <= GLFW_GAMEPAD_BUTTON_LAST;  i++)
     87         {
     88             if (!isValidElementForJoystick(mapping->buttons + i, js))
     89                 return NULL;
     90         }
     91 
     92         for (i = 0;  i <= GLFW_GAMEPAD_AXIS_LAST;  i++)
     93         {
     94             if (!isValidElementForJoystick(mapping->axes + i, js))
     95                 return NULL;
     96         }
     97     }
     98 
     99     return mapping;
    100 }
    101 
    102 // Parses an SDL_GameControllerDB line and adds it to the mapping list
    103 //
    104 static GLFWbool parseMapping(_GLFWmapping* mapping, const char* string)
    105 {
    106     const char* c = string;
    107     size_t i, length;
    108     struct
    109     {
    110         const char* name;
    111         _GLFWmapelement* element;
    112     } fields[] =
    113     {
    114         { "platform",      NULL },
    115         { "a",             mapping->buttons + GLFW_GAMEPAD_BUTTON_A },
    116         { "b",             mapping->buttons + GLFW_GAMEPAD_BUTTON_B },
    117         { "x",             mapping->buttons + GLFW_GAMEPAD_BUTTON_X },
    118         { "y",             mapping->buttons + GLFW_GAMEPAD_BUTTON_Y },
    119         { "back",          mapping->buttons + GLFW_GAMEPAD_BUTTON_BACK },
    120         { "start",         mapping->buttons + GLFW_GAMEPAD_BUTTON_START },
    121         { "guide",         mapping->buttons + GLFW_GAMEPAD_BUTTON_GUIDE },
    122         { "leftshoulder",  mapping->buttons + GLFW_GAMEPAD_BUTTON_LEFT_BUMPER },
    123         { "rightshoulder", mapping->buttons + GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER },
    124         { "leftstick",     mapping->buttons + GLFW_GAMEPAD_BUTTON_LEFT_THUMB },
    125         { "rightstick",    mapping->buttons + GLFW_GAMEPAD_BUTTON_RIGHT_THUMB },
    126         { "dpup",          mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_UP },
    127         { "dpright",       mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_RIGHT },
    128         { "dpdown",        mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_DOWN },
    129         { "dpleft",        mapping->buttons + GLFW_GAMEPAD_BUTTON_DPAD_LEFT },
    130         { "lefttrigger",   mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_TRIGGER },
    131         { "righttrigger",  mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER },
    132         { "leftx",         mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_X },
    133         { "lefty",         mapping->axes + GLFW_GAMEPAD_AXIS_LEFT_Y },
    134         { "rightx",        mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_X },
    135         { "righty",        mapping->axes + GLFW_GAMEPAD_AXIS_RIGHT_Y }
    136     };
    137 
    138     length = strcspn(c, ",");
    139     if (length != 32 || c[length] != ',')
    140     {
    141         _glfwInputError(GLFW_INVALID_VALUE, NULL);
    142         return GLFW_FALSE;
    143     }
    144 
    145     memcpy(mapping->guid, c, length);
    146     c += length + 1;
    147 
    148     length = strcspn(c, ",");
    149     if (length >= sizeof(mapping->name) || c[length] != ',')
    150     {
    151         _glfwInputError(GLFW_INVALID_VALUE, NULL);
    152         return GLFW_FALSE;
    153     }
    154 
    155     memcpy(mapping->name, c, length);
    156     c += length + 1;
    157 
    158     while (*c)
    159     {
    160         // TODO: Implement output modifiers
    161         if (*c == '+' || *c == '-')
    162             return GLFW_FALSE;
    163 
    164         for (i = 0;  i < sizeof(fields) / sizeof(fields[0]);  i++)
    165         {
    166             length = strlen(fields[i].name);
    167             if (strncmp(c, fields[i].name, length) != 0 || c[length] != ':')
    168                 continue;
    169 
    170             c += length + 1;
    171 
    172             if (fields[i].element)
    173             {
    174                 _GLFWmapelement* e = fields[i].element;
    175                 int8_t minimum = -1;
    176                 int8_t maximum = 1;
    177 
    178                 if (*c == '+')
    179                 {
    180                     minimum = 0;
    181                     c += 1;
    182                 }
    183                 else if (*c == '-')
    184                 {
    185                     maximum = 0;
    186                     c += 1;
    187                 }
    188 
    189                 if (*c == 'a')
    190                     e->type = _GLFW_JOYSTICK_AXIS;
    191                 else if (*c == 'b')
    192                     e->type = _GLFW_JOYSTICK_BUTTON;
    193                 else if (*c == 'h')
    194                     e->type = _GLFW_JOYSTICK_HATBIT;
    195                 else
    196                     break;
    197 
    198                 if (e->type == _GLFW_JOYSTICK_HATBIT)
    199                 {
    200                     const unsigned long hat = strtoul(c + 1, (char**) &c, 10);
    201                     const unsigned long bit = strtoul(c + 1, (char**) &c, 10);
    202                     e->index = (uint8_t) ((hat << 4) | bit);
    203                 }
    204                 else
    205                     e->index = (uint8_t) strtoul(c + 1, (char**) &c, 10);
    206 
    207                 if (e->type == _GLFW_JOYSTICK_AXIS)
    208                 {
    209                     e->axisScale = 2 / (maximum - minimum);
    210                     e->axisOffset = -(maximum + minimum);
    211 
    212                     if (*c == '~')
    213                     {
    214                         e->axisScale = -e->axisScale;
    215                         e->axisOffset = -e->axisOffset;
    216                     }
    217                 }
    218             }
    219             else
    220             {
    221                 length = strlen(_GLFW_PLATFORM_MAPPING_NAME);
    222                 if (strncmp(c, _GLFW_PLATFORM_MAPPING_NAME, length) != 0)
    223                     return GLFW_FALSE;
    224             }
    225 
    226             break;
    227         }
    228 
    229         c += strcspn(c, ",");
    230         c += strspn(c, ",");
    231     }
    232 
    233     for (i = 0;  i < 32;  i++)
    234     {
    235         if (mapping->guid[i] >= 'A' && mapping->guid[i] <= 'F')
    236             mapping->guid[i] += 'a' - 'A';
    237     }
    238 
    239     _glfwPlatformUpdateGamepadGUID(mapping->guid);
    240     return GLFW_TRUE;
    241 }
    242 
    243 
    244 //////////////////////////////////////////////////////////////////////////
    245 //////                         GLFW event API                       //////
    246 //////////////////////////////////////////////////////////////////////////
    247 
    248 // Notifies shared code of a physical key event
    249 //
    250 void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int mods)
    251 {
    252     if (key >= 0 && key <= GLFW_KEY_LAST)
    253     {
    254         GLFWbool repeated = GLFW_FALSE;
    255 
    256         if (action == GLFW_RELEASE && window->keys[key] == GLFW_RELEASE)
    257             return;
    258 
    259         if (action == GLFW_PRESS && window->keys[key] == GLFW_PRESS)
    260             repeated = GLFW_TRUE;
    261 
    262         if (action == GLFW_RELEASE && window->stickyKeys)
    263             window->keys[key] = _GLFW_STICK;
    264         else
    265             window->keys[key] = (char) action;
    266 
    267         if (repeated)
    268             action = GLFW_REPEAT;
    269     }
    270 
    271     if (!window->lockKeyMods)
    272         mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
    273 
    274     if (window->callbacks.key)
    275         window->callbacks.key((GLFWwindow*) window, key, scancode, action, mods);
    276 }
    277 
    278 // Notifies shared code of a Unicode codepoint input event
    279 // The 'plain' parameter determines whether to emit a regular character event
    280 //
    281 void _glfwInputChar(_GLFWwindow* window, uint32_t codepoint, int mods, GLFWbool plain)
    282 {
    283     if (codepoint < 32 || (codepoint > 126 && codepoint < 160))
    284         return;
    285 
    286     if (!window->lockKeyMods)
    287         mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
    288 
    289     if (window->callbacks.charmods)
    290         window->callbacks.charmods((GLFWwindow*) window, codepoint, mods);
    291 
    292     if (plain)
    293     {
    294         if (window->callbacks.character)
    295             window->callbacks.character((GLFWwindow*) window, codepoint);
    296     }
    297 }
    298 
    299 // Notifies shared code of a scroll event
    300 //
    301 void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset)
    302 {
    303     if (window->callbacks.scroll)
    304         window->callbacks.scroll((GLFWwindow*) window, xoffset, yoffset);
    305 }
    306 
    307 // Notifies shared code of a mouse button click event
    308 //
    309 void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods)
    310 {
    311     if (button < 0 || button > GLFW_MOUSE_BUTTON_LAST)
    312         return;
    313 
    314     if (!window->lockKeyMods)
    315         mods &= ~(GLFW_MOD_CAPS_LOCK | GLFW_MOD_NUM_LOCK);
    316 
    317     if (action == GLFW_RELEASE && window->stickyMouseButtons)
    318         window->mouseButtons[button] = _GLFW_STICK;
    319     else
    320         window->mouseButtons[button] = (char) action;
    321 
    322     if (window->callbacks.mouseButton)
    323         window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods);
    324 }
    325 
    326 // Notifies shared code of a cursor motion event
    327 // The position is specified in content area relative screen coordinates
    328 //
    329 void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos)
    330 {
    331     if (window->virtualCursorPosX == xpos && window->virtualCursorPosY == ypos)
    332         return;
    333 
    334     window->virtualCursorPosX = xpos;
    335     window->virtualCursorPosY = ypos;
    336 
    337     if (window->callbacks.cursorPos)
    338         window->callbacks.cursorPos((GLFWwindow*) window, xpos, ypos);
    339 }
    340 
    341 // Notifies shared code of a cursor enter/leave event
    342 //
    343 void _glfwInputCursorEnter(_GLFWwindow* window, GLFWbool entered)
    344 {
    345     if (window->callbacks.cursorEnter)
    346         window->callbacks.cursorEnter((GLFWwindow*) window, entered);
    347 }
    348 
    349 // Notifies shared code of files or directories dropped on a window
    350 //
    351 void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths)
    352 {
    353     if (window->callbacks.drop)
    354         window->callbacks.drop((GLFWwindow*) window, count, paths);
    355 }
    356 
    357 // Notifies shared code of a joystick connection or disconnection
    358 //
    359 void _glfwInputJoystick(_GLFWjoystick* js, int event)
    360 {
    361     const int jid = (int) (js - _glfw.joysticks);
    362 
    363     if (event == GLFW_CONNECTED)
    364         js->connected = GLFW_TRUE;
    365     else if (event == GLFW_DISCONNECTED)
    366         js->connected = GLFW_FALSE;
    367 
    368     if (_glfw.callbacks.joystick)
    369         _glfw.callbacks.joystick(jid, event);
    370 }
    371 
    372 // Notifies shared code of the new value of a joystick axis
    373 //
    374 void _glfwInputJoystickAxis(_GLFWjoystick* js, int axis, float value)
    375 {
    376     js->axes[axis] = value;
    377 }
    378 
    379 // Notifies shared code of the new value of a joystick button
    380 //
    381 void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value)
    382 {
    383     js->buttons[button] = value;
    384 }
    385 
    386 // Notifies shared code of the new value of a joystick hat
    387 //
    388 void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value)
    389 {
    390     const int base = js->buttonCount + hat * 4;
    391 
    392     js->buttons[base + 0] = (value & 0x01) ? GLFW_PRESS : GLFW_RELEASE;
    393     js->buttons[base + 1] = (value & 0x02) ? GLFW_PRESS : GLFW_RELEASE;
    394     js->buttons[base + 2] = (value & 0x04) ? GLFW_PRESS : GLFW_RELEASE;
    395     js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE;
    396 
    397     js->hats[hat] = value;
    398 }
    399 
    400 
    401 //////////////////////////////////////////////////////////////////////////
    402 //////                       GLFW internal API                      //////
    403 //////////////////////////////////////////////////////////////////////////
    404 
    405 // Adds the built-in set of gamepad mappings
    406 //
    407 void _glfwInitGamepadMappings(void)
    408 {
    409     int jid;
    410     size_t i;
    411     const size_t count = sizeof(_glfwDefaultMappings) / sizeof(char*);
    412     _glfw.mappings = calloc(count, sizeof(_GLFWmapping));
    413 
    414     for (i = 0;  i < count;  i++)
    415     {
    416         if (parseMapping(&_glfw.mappings[_glfw.mappingCount], _glfwDefaultMappings[i]))
    417             _glfw.mappingCount++;
    418     }
    419 
    420     for (jid = 0;  jid <= GLFW_JOYSTICK_LAST;  jid++)
    421     {
    422         _GLFWjoystick* js = _glfw.joysticks + jid;
    423         if (js->connected)
    424             js->mapping = findValidMapping(js);
    425     }
    426 }
    427 
    428 // Returns an available joystick object with arrays and name allocated
    429 //
    430 _GLFWjoystick* _glfwAllocJoystick(const char* name,
    431                                   const char* guid,
    432                                   int axisCount,
    433                                   int buttonCount,
    434                                   int hatCount)
    435 {
    436     int jid;
    437     _GLFWjoystick* js;
    438 
    439     for (jid = 0;  jid <= GLFW_JOYSTICK_LAST;  jid++)
    440     {
    441         if (!_glfw.joysticks[jid].allocated)
    442             break;
    443     }
    444 
    445     if (jid > GLFW_JOYSTICK_LAST)
    446         return NULL;
    447 
    448     js = _glfw.joysticks + jid;
    449     js->allocated   = GLFW_TRUE;
    450     js->axes        = calloc(axisCount, sizeof(float));
    451     js->buttons     = calloc(buttonCount + (size_t) hatCount * 4, 1);
    452     js->hats        = calloc(hatCount, 1);
    453     js->axisCount   = axisCount;
    454     js->buttonCount = buttonCount;
    455     js->hatCount    = hatCount;
    456 
    457     strncpy(js->name, name, sizeof(js->name) - 1);
    458     strncpy(js->guid, guid, sizeof(js->guid) - 1);
    459     js->mapping = findValidMapping(js);
    460 
    461     return js;
    462 }
    463 
    464 // Frees arrays and name and flags the joystick object as unused
    465 //
    466 void _glfwFreeJoystick(_GLFWjoystick* js)
    467 {
    468     free(js->axes);
    469     free(js->buttons);
    470     free(js->hats);
    471     memset(js, 0, sizeof(_GLFWjoystick));
    472 }
    473 
    474 // Center the cursor in the content area of the specified window
    475 //
    476 void _glfwCenterCursorInContentArea(_GLFWwindow* window)
    477 {
    478     int width, height;
    479 
    480     _glfwPlatformGetWindowSize(window, &width, &height);
    481     _glfwPlatformSetCursorPos(window, width / 2.0, height / 2.0);
    482 }
    483 
    484 
    485 //////////////////////////////////////////////////////////////////////////
    486 //////                        GLFW public API                       //////
    487 //////////////////////////////////////////////////////////////////////////
    488 
    489 GLFWAPI int glfwGetInputMode(GLFWwindow* handle, int mode)
    490 {
    491     _GLFWwindow* window = (_GLFWwindow*) handle;
    492     assert(window != NULL);
    493 
    494     _GLFW_REQUIRE_INIT_OR_RETURN(0);
    495 
    496     switch (mode)
    497     {
    498         case GLFW_CURSOR:
    499             return window->cursorMode;
    500         case GLFW_STICKY_KEYS:
    501             return window->stickyKeys;
    502         case GLFW_STICKY_MOUSE_BUTTONS:
    503             return window->stickyMouseButtons;
    504         case GLFW_LOCK_KEY_MODS:
    505             return window->lockKeyMods;
    506         case GLFW_RAW_MOUSE_MOTION:
    507             return window->rawMouseMotion;
    508     }
    509 
    510     _glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode);
    511     return 0;
    512 }
    513 
    514 GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value)
    515 {
    516     _GLFWwindow* window = (_GLFWwindow*) handle;
    517     assert(window != NULL);
    518 
    519     _GLFW_REQUIRE_INIT();
    520 
    521     if (mode == GLFW_CURSOR)
    522     {
    523         if (value != GLFW_CURSOR_NORMAL &&
    524             value != GLFW_CURSOR_HIDDEN &&
    525             value != GLFW_CURSOR_DISABLED)
    526         {
    527             _glfwInputError(GLFW_INVALID_ENUM,
    528                             "Invalid cursor mode 0x%08X",
    529                             value);
    530             return;
    531         }
    532 
    533         if (window->cursorMode == value)
    534             return;
    535 
    536         window->cursorMode = value;
    537 
    538         _glfwPlatformGetCursorPos(window,
    539                                   &window->virtualCursorPosX,
    540                                   &window->virtualCursorPosY);
    541         _glfwPlatformSetCursorMode(window, value);
    542     }
    543     else if (mode == GLFW_STICKY_KEYS)
    544     {
    545         value = value ? GLFW_TRUE : GLFW_FALSE;
    546         if (window->stickyKeys == value)
    547             return;
    548 
    549         if (!value)
    550         {
    551             int i;
    552 
    553             // Release all sticky keys
    554             for (i = 0;  i <= GLFW_KEY_LAST;  i++)
    555             {
    556                 if (window->keys[i] == _GLFW_STICK)
    557                     window->keys[i] = GLFW_RELEASE;
    558             }
    559         }
    560 
    561         window->stickyKeys = value;
    562     }
    563     else if (mode == GLFW_STICKY_MOUSE_BUTTONS)
    564     {
    565         value = value ? GLFW_TRUE : GLFW_FALSE;
    566         if (window->stickyMouseButtons == value)
    567             return;
    568 
    569         if (!value)
    570         {
    571             int i;
    572 
    573             // Release all sticky mouse buttons
    574             for (i = 0;  i <= GLFW_MOUSE_BUTTON_LAST;  i++)
    575             {
    576                 if (window->mouseButtons[i] == _GLFW_STICK)
    577                     window->mouseButtons[i] = GLFW_RELEASE;
    578             }
    579         }
    580 
    581         window->stickyMouseButtons = value;
    582     }
    583     else if (mode == GLFW_LOCK_KEY_MODS)
    584     {
    585         window->lockKeyMods = value ? GLFW_TRUE : GLFW_FALSE;
    586     }
    587     else if (mode == GLFW_RAW_MOUSE_MOTION)
    588     {
    589         if (!_glfwPlatformRawMouseMotionSupported())
    590         {
    591             _glfwInputError(GLFW_PLATFORM_ERROR,
    592                             "Raw mouse motion is not supported on this system");
    593             return;
    594         }
    595 
    596         value = value ? GLFW_TRUE : GLFW_FALSE;
    597         if (window->rawMouseMotion == value)
    598             return;
    599 
    600         window->rawMouseMotion = value;
    601         _glfwPlatformSetRawMouseMotion(window, value);
    602     }
    603     else
    604         _glfwInputError(GLFW_INVALID_ENUM, "Invalid input mode 0x%08X", mode);
    605 }
    606 
    607 GLFWAPI int glfwRawMouseMotionSupported(void)
    608 {
    609     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
    610     return _glfwPlatformRawMouseMotionSupported();
    611 }
    612 
    613 GLFWAPI const char* glfwGetKeyName(int key, int scancode)
    614 {
    615     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    616 
    617     if (key != GLFW_KEY_UNKNOWN)
    618     {
    619         if (key != GLFW_KEY_KP_EQUAL &&
    620             (key < GLFW_KEY_KP_0 || key > GLFW_KEY_KP_ADD) &&
    621             (key < GLFW_KEY_APOSTROPHE || key > GLFW_KEY_WORLD_2))
    622         {
    623             return NULL;
    624         }
    625 
    626         scancode = _glfwPlatformGetKeyScancode(key);
    627     }
    628 
    629     return _glfwPlatformGetScancodeName(scancode);
    630 }
    631 
    632 GLFWAPI int glfwGetKeyScancode(int key)
    633 {
    634     _GLFW_REQUIRE_INIT_OR_RETURN(-1);
    635 
    636     if (key < GLFW_KEY_SPACE || key > GLFW_KEY_LAST)
    637     {
    638         _glfwInputError(GLFW_INVALID_ENUM, "Invalid key %i", key);
    639         return GLFW_RELEASE;
    640     }
    641 
    642     return _glfwPlatformGetKeyScancode(key);
    643 }
    644 
    645 GLFWAPI int glfwGetKey(GLFWwindow* handle, int key)
    646 {
    647     _GLFWwindow* window = (_GLFWwindow*) handle;
    648     assert(window != NULL);
    649 
    650     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);
    651 
    652     if (key < GLFW_KEY_SPACE || key > GLFW_KEY_LAST)
    653     {
    654         _glfwInputError(GLFW_INVALID_ENUM, "Invalid key %i", key);
    655         return GLFW_RELEASE;
    656     }
    657 
    658     if (window->keys[key] == _GLFW_STICK)
    659     {
    660         // Sticky mode: release key now
    661         window->keys[key] = GLFW_RELEASE;
    662         return GLFW_PRESS;
    663     }
    664 
    665     return (int) window->keys[key];
    666 }
    667 
    668 GLFWAPI int glfwGetMouseButton(GLFWwindow* handle, int button)
    669 {
    670     _GLFWwindow* window = (_GLFWwindow*) handle;
    671     assert(window != NULL);
    672 
    673     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_RELEASE);
    674 
    675     if (button < GLFW_MOUSE_BUTTON_1 || button > GLFW_MOUSE_BUTTON_LAST)
    676     {
    677         _glfwInputError(GLFW_INVALID_ENUM, "Invalid mouse button %i", button);
    678         return GLFW_RELEASE;
    679     }
    680 
    681     if (window->mouseButtons[button] == _GLFW_STICK)
    682     {
    683         // Sticky mode: release mouse button now
    684         window->mouseButtons[button] = GLFW_RELEASE;
    685         return GLFW_PRESS;
    686     }
    687 
    688     return (int) window->mouseButtons[button];
    689 }
    690 
    691 GLFWAPI void glfwGetCursorPos(GLFWwindow* handle, double* xpos, double* ypos)
    692 {
    693     _GLFWwindow* window = (_GLFWwindow*) handle;
    694     assert(window != NULL);
    695 
    696     if (xpos)
    697         *xpos = 0;
    698     if (ypos)
    699         *ypos = 0;
    700 
    701     _GLFW_REQUIRE_INIT();
    702 
    703     if (window->cursorMode == GLFW_CURSOR_DISABLED)
    704     {
    705         if (xpos)
    706             *xpos = window->virtualCursorPosX;
    707         if (ypos)
    708             *ypos = window->virtualCursorPosY;
    709     }
    710     else
    711         _glfwPlatformGetCursorPos(window, xpos, ypos);
    712 }
    713 
    714 GLFWAPI void glfwSetCursorPos(GLFWwindow* handle, double xpos, double ypos)
    715 {
    716     _GLFWwindow* window = (_GLFWwindow*) handle;
    717     assert(window != NULL);
    718 
    719     _GLFW_REQUIRE_INIT();
    720 
    721     if (xpos != xpos || xpos < -DBL_MAX || xpos > DBL_MAX ||
    722         ypos != ypos || ypos < -DBL_MAX || ypos > DBL_MAX)
    723     {
    724         _glfwInputError(GLFW_INVALID_VALUE,
    725                         "Invalid cursor position %f %f",
    726                         xpos, ypos);
    727         return;
    728     }
    729 
    730     if (!_glfwPlatformWindowFocused(window))
    731         return;
    732 
    733     if (window->cursorMode == GLFW_CURSOR_DISABLED)
    734     {
    735         // Only update the accumulated position if the cursor is disabled
    736         window->virtualCursorPosX = xpos;
    737         window->virtualCursorPosY = ypos;
    738     }
    739     else
    740     {
    741         // Update system cursor position
    742         _glfwPlatformSetCursorPos(window, xpos, ypos);
    743     }
    744 }
    745 
    746 GLFWAPI GLFWcursor* glfwCreateCursor(const GLFWimage* image, int xhot, int yhot)
    747 {
    748     _GLFWcursor* cursor;
    749 
    750     assert(image != NULL);
    751     assert(image->pixels != NULL);
    752 
    753     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    754 
    755     if (image->width <= 0 || image->height <= 0)
    756     {
    757         _glfwInputError(GLFW_INVALID_VALUE, "Invalid image dimensions for cursor");
    758         return NULL;
    759     }
    760 
    761     cursor = calloc(1, sizeof(_GLFWcursor));
    762     cursor->next = _glfw.cursorListHead;
    763     _glfw.cursorListHead = cursor;
    764 
    765     if (!_glfwPlatformCreateCursor(cursor, image, xhot, yhot))
    766     {
    767         glfwDestroyCursor((GLFWcursor*) cursor);
    768         return NULL;
    769     }
    770 
    771     return (GLFWcursor*) cursor;
    772 }
    773 
    774 GLFWAPI GLFWcursor* glfwCreateStandardCursor(int shape)
    775 {
    776     _GLFWcursor* cursor;
    777 
    778     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    779 
    780     if (shape != GLFW_ARROW_CURSOR &&
    781         shape != GLFW_IBEAM_CURSOR &&
    782         shape != GLFW_CROSSHAIR_CURSOR &&
    783         shape != GLFW_HAND_CURSOR &&
    784         shape != GLFW_HRESIZE_CURSOR &&
    785         shape != GLFW_VRESIZE_CURSOR)
    786     {
    787         _glfwInputError(GLFW_INVALID_ENUM, "Invalid standard cursor 0x%08X", shape);
    788         return NULL;
    789     }
    790 
    791     cursor = calloc(1, sizeof(_GLFWcursor));
    792     cursor->next = _glfw.cursorListHead;
    793     _glfw.cursorListHead = cursor;
    794 
    795     if (!_glfwPlatformCreateStandardCursor(cursor, shape))
    796     {
    797         glfwDestroyCursor((GLFWcursor*) cursor);
    798         return NULL;
    799     }
    800 
    801     return (GLFWcursor*) cursor;
    802 }
    803 
    804 GLFWAPI void glfwDestroyCursor(GLFWcursor* handle)
    805 {
    806     _GLFWcursor* cursor = (_GLFWcursor*) handle;
    807 
    808     _GLFW_REQUIRE_INIT();
    809 
    810     if (cursor == NULL)
    811         return;
    812 
    813     // Make sure the cursor is not being used by any window
    814     {
    815         _GLFWwindow* window;
    816 
    817         for (window = _glfw.windowListHead;  window;  window = window->next)
    818         {
    819             if (window->cursor == cursor)
    820                 glfwSetCursor((GLFWwindow*) window, NULL);
    821         }
    822     }
    823 
    824     _glfwPlatformDestroyCursor(cursor);
    825 
    826     // Unlink cursor from global linked list
    827     {
    828         _GLFWcursor** prev = &_glfw.cursorListHead;
    829 
    830         while (*prev != cursor)
    831             prev = &((*prev)->next);
    832 
    833         *prev = cursor->next;
    834     }
    835 
    836     free(cursor);
    837 }
    838 
    839 GLFWAPI void glfwSetCursor(GLFWwindow* windowHandle, GLFWcursor* cursorHandle)
    840 {
    841     _GLFWwindow* window = (_GLFWwindow*) windowHandle;
    842     _GLFWcursor* cursor = (_GLFWcursor*) cursorHandle;
    843     assert(window != NULL);
    844 
    845     _GLFW_REQUIRE_INIT();
    846 
    847     window->cursor = cursor;
    848 
    849     _glfwPlatformSetCursor(window, cursor);
    850 }
    851 
    852 GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow* handle, GLFWkeyfun cbfun)
    853 {
    854     _GLFWwindow* window = (_GLFWwindow*) handle;
    855     assert(window != NULL);
    856 
    857     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    858     _GLFW_SWAP_POINTERS(window->callbacks.key, cbfun);
    859     return cbfun;
    860 }
    861 
    862 GLFWAPI GLFWcharfun glfwSetCharCallback(GLFWwindow* handle, GLFWcharfun cbfun)
    863 {
    864     _GLFWwindow* window = (_GLFWwindow*) handle;
    865     assert(window != NULL);
    866 
    867     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    868     _GLFW_SWAP_POINTERS(window->callbacks.character, cbfun);
    869     return cbfun;
    870 }
    871 
    872 GLFWAPI GLFWcharmodsfun glfwSetCharModsCallback(GLFWwindow* handle, GLFWcharmodsfun cbfun)
    873 {
    874     _GLFWwindow* window = (_GLFWwindow*) handle;
    875     assert(window != NULL);
    876 
    877     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    878     _GLFW_SWAP_POINTERS(window->callbacks.charmods, cbfun);
    879     return cbfun;
    880 }
    881 
    882 GLFWAPI GLFWmousebuttonfun glfwSetMouseButtonCallback(GLFWwindow* handle,
    883                                                       GLFWmousebuttonfun cbfun)
    884 {
    885     _GLFWwindow* window = (_GLFWwindow*) handle;
    886     assert(window != NULL);
    887 
    888     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    889     _GLFW_SWAP_POINTERS(window->callbacks.mouseButton, cbfun);
    890     return cbfun;
    891 }
    892 
    893 GLFWAPI GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow* handle,
    894                                                   GLFWcursorposfun cbfun)
    895 {
    896     _GLFWwindow* window = (_GLFWwindow*) handle;
    897     assert(window != NULL);
    898 
    899     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    900     _GLFW_SWAP_POINTERS(window->callbacks.cursorPos, cbfun);
    901     return cbfun;
    902 }
    903 
    904 GLFWAPI GLFWcursorenterfun glfwSetCursorEnterCallback(GLFWwindow* handle,
    905                                                       GLFWcursorenterfun cbfun)
    906 {
    907     _GLFWwindow* window = (_GLFWwindow*) handle;
    908     assert(window != NULL);
    909 
    910     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    911     _GLFW_SWAP_POINTERS(window->callbacks.cursorEnter, cbfun);
    912     return cbfun;
    913 }
    914 
    915 GLFWAPI GLFWscrollfun glfwSetScrollCallback(GLFWwindow* handle,
    916                                             GLFWscrollfun cbfun)
    917 {
    918     _GLFWwindow* window = (_GLFWwindow*) handle;
    919     assert(window != NULL);
    920 
    921     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    922     _GLFW_SWAP_POINTERS(window->callbacks.scroll, cbfun);
    923     return cbfun;
    924 }
    925 
    926 GLFWAPI GLFWdropfun glfwSetDropCallback(GLFWwindow* handle, GLFWdropfun cbfun)
    927 {
    928     _GLFWwindow* window = (_GLFWwindow*) handle;
    929     assert(window != NULL);
    930 
    931     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    932     _GLFW_SWAP_POINTERS(window->callbacks.drop, cbfun);
    933     return cbfun;
    934 }
    935 
    936 GLFWAPI int glfwJoystickPresent(int jid)
    937 {
    938     _GLFWjoystick* js;
    939 
    940     assert(jid >= GLFW_JOYSTICK_1);
    941     assert(jid <= GLFW_JOYSTICK_LAST);
    942 
    943     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
    944 
    945     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
    946     {
    947         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
    948         return GLFW_FALSE;
    949     }
    950 
    951     js = _glfw.joysticks + jid;
    952     if (!js->connected)
    953         return GLFW_FALSE;
    954 
    955     return _glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE);
    956 }
    957 
    958 GLFWAPI const float* glfwGetJoystickAxes(int jid, int* count)
    959 {
    960     _GLFWjoystick* js;
    961 
    962     assert(jid >= GLFW_JOYSTICK_1);
    963     assert(jid <= GLFW_JOYSTICK_LAST);
    964     assert(count != NULL);
    965 
    966     *count = 0;
    967 
    968     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    969 
    970     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
    971     {
    972         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
    973         return NULL;
    974     }
    975 
    976     js = _glfw.joysticks + jid;
    977     if (!js->connected)
    978         return NULL;
    979 
    980     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_AXES))
    981         return NULL;
    982 
    983     *count = js->axisCount;
    984     return js->axes;
    985 }
    986 
    987 GLFWAPI const unsigned char* glfwGetJoystickButtons(int jid, int* count)
    988 {
    989     _GLFWjoystick* js;
    990 
    991     assert(jid >= GLFW_JOYSTICK_1);
    992     assert(jid <= GLFW_JOYSTICK_LAST);
    993     assert(count != NULL);
    994 
    995     *count = 0;
    996 
    997     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
    998 
    999     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1000     {
   1001         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1002         return NULL;
   1003     }
   1004 
   1005     js = _glfw.joysticks + jid;
   1006     if (!js->connected)
   1007         return NULL;
   1008 
   1009     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
   1010         return NULL;
   1011 
   1012     if (_glfw.hints.init.hatButtons)
   1013         *count = js->buttonCount + js->hatCount * 4;
   1014     else
   1015         *count = js->buttonCount;
   1016 
   1017     return js->buttons;
   1018 }
   1019 
   1020 GLFWAPI const unsigned char* glfwGetJoystickHats(int jid, int* count)
   1021 {
   1022     _GLFWjoystick* js;
   1023 
   1024     assert(jid >= GLFW_JOYSTICK_1);
   1025     assert(jid <= GLFW_JOYSTICK_LAST);
   1026     assert(count != NULL);
   1027 
   1028     *count = 0;
   1029 
   1030     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1031 
   1032     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1033     {
   1034         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1035         return NULL;
   1036     }
   1037 
   1038     js = _glfw.joysticks + jid;
   1039     if (!js->connected)
   1040         return NULL;
   1041 
   1042     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_BUTTONS))
   1043         return NULL;
   1044 
   1045     *count = js->hatCount;
   1046     return js->hats;
   1047 }
   1048 
   1049 GLFWAPI const char* glfwGetJoystickName(int jid)
   1050 {
   1051     _GLFWjoystick* js;
   1052 
   1053     assert(jid >= GLFW_JOYSTICK_1);
   1054     assert(jid <= GLFW_JOYSTICK_LAST);
   1055 
   1056     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1057 
   1058     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1059     {
   1060         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1061         return NULL;
   1062     }
   1063 
   1064     js = _glfw.joysticks + jid;
   1065     if (!js->connected)
   1066         return NULL;
   1067 
   1068     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
   1069         return NULL;
   1070 
   1071     return js->name;
   1072 }
   1073 
   1074 GLFWAPI const char* glfwGetJoystickGUID(int jid)
   1075 {
   1076     _GLFWjoystick* js;
   1077 
   1078     assert(jid >= GLFW_JOYSTICK_1);
   1079     assert(jid <= GLFW_JOYSTICK_LAST);
   1080 
   1081     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1082 
   1083     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1084     {
   1085         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1086         return NULL;
   1087     }
   1088 
   1089     js = _glfw.joysticks + jid;
   1090     if (!js->connected)
   1091         return NULL;
   1092 
   1093     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
   1094         return NULL;
   1095 
   1096     return js->guid;
   1097 }
   1098 
   1099 GLFWAPI void glfwSetJoystickUserPointer(int jid, void* pointer)
   1100 {
   1101     _GLFWjoystick* js;
   1102 
   1103     assert(jid >= GLFW_JOYSTICK_1);
   1104     assert(jid <= GLFW_JOYSTICK_LAST);
   1105 
   1106     _GLFW_REQUIRE_INIT();
   1107 
   1108     js = _glfw.joysticks + jid;
   1109     if (!js->allocated)
   1110         return;
   1111 
   1112     js->userPointer = pointer;
   1113 }
   1114 
   1115 GLFWAPI void* glfwGetJoystickUserPointer(int jid)
   1116 {
   1117     _GLFWjoystick* js;
   1118 
   1119     assert(jid >= GLFW_JOYSTICK_1);
   1120     assert(jid <= GLFW_JOYSTICK_LAST);
   1121 
   1122     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1123 
   1124     js = _glfw.joysticks + jid;
   1125     if (!js->allocated)
   1126         return NULL;
   1127 
   1128     return js->userPointer;
   1129 }
   1130 
   1131 GLFWAPI GLFWjoystickfun glfwSetJoystickCallback(GLFWjoystickfun cbfun)
   1132 {
   1133     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1134     _GLFW_SWAP_POINTERS(_glfw.callbacks.joystick, cbfun);
   1135     return cbfun;
   1136 }
   1137 
   1138 GLFWAPI int glfwUpdateGamepadMappings(const char* string)
   1139 {
   1140     int jid;
   1141     const char* c = string;
   1142 
   1143     assert(string != NULL);
   1144 
   1145     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
   1146 
   1147     while (*c)
   1148     {
   1149         if ((*c >= '0' && *c <= '9') ||
   1150             (*c >= 'a' && *c <= 'f') ||
   1151             (*c >= 'A' && *c <= 'F'))
   1152         {
   1153             char line[1024];
   1154 
   1155             const size_t length = strcspn(c, "\r\n");
   1156             if (length < sizeof(line))
   1157             {
   1158                 _GLFWmapping mapping = {{0}};
   1159 
   1160                 memcpy(line, c, length);
   1161                 line[length] = '\0';
   1162 
   1163                 if (parseMapping(&mapping, line))
   1164                 {
   1165                     _GLFWmapping* previous = findMapping(mapping.guid);
   1166                     if (previous)
   1167                         *previous = mapping;
   1168                     else
   1169                     {
   1170                         _glfw.mappingCount++;
   1171                         _glfw.mappings =
   1172                             realloc(_glfw.mappings,
   1173                                     sizeof(_GLFWmapping) * _glfw.mappingCount);
   1174                         _glfw.mappings[_glfw.mappingCount - 1] = mapping;
   1175                     }
   1176                 }
   1177             }
   1178 
   1179             c += length;
   1180         }
   1181         else
   1182         {
   1183             c += strcspn(c, "\r\n");
   1184             c += strspn(c, "\r\n");
   1185         }
   1186     }
   1187 
   1188     for (jid = 0;  jid <= GLFW_JOYSTICK_LAST;  jid++)
   1189     {
   1190         _GLFWjoystick* js = _glfw.joysticks + jid;
   1191         if (js->connected)
   1192             js->mapping = findValidMapping(js);
   1193     }
   1194 
   1195     return GLFW_TRUE;
   1196 }
   1197 
   1198 GLFWAPI int glfwJoystickIsGamepad(int jid)
   1199 {
   1200     _GLFWjoystick* js;
   1201 
   1202     assert(jid >= GLFW_JOYSTICK_1);
   1203     assert(jid <= GLFW_JOYSTICK_LAST);
   1204 
   1205     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
   1206 
   1207     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1208     {
   1209         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1210         return GLFW_FALSE;
   1211     }
   1212 
   1213     js = _glfw.joysticks + jid;
   1214     if (!js->connected)
   1215         return GLFW_FALSE;
   1216 
   1217     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
   1218         return GLFW_FALSE;
   1219 
   1220     return js->mapping != NULL;
   1221 }
   1222 
   1223 GLFWAPI const char* glfwGetGamepadName(int jid)
   1224 {
   1225     _GLFWjoystick* js;
   1226 
   1227     assert(jid >= GLFW_JOYSTICK_1);
   1228     assert(jid <= GLFW_JOYSTICK_LAST);
   1229 
   1230     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1231 
   1232     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1233     {
   1234         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1235         return NULL;
   1236     }
   1237 
   1238     js = _glfw.joysticks + jid;
   1239     if (!js->connected)
   1240         return NULL;
   1241 
   1242     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_PRESENCE))
   1243         return NULL;
   1244 
   1245     if (!js->mapping)
   1246         return NULL;
   1247 
   1248     return js->mapping->name;
   1249 }
   1250 
   1251 GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state)
   1252 {
   1253     int i;
   1254     _GLFWjoystick* js;
   1255 
   1256     assert(jid >= GLFW_JOYSTICK_1);
   1257     assert(jid <= GLFW_JOYSTICK_LAST);
   1258     assert(state != NULL);
   1259 
   1260     memset(state, 0, sizeof(GLFWgamepadstate));
   1261 
   1262     _GLFW_REQUIRE_INIT_OR_RETURN(GLFW_FALSE);
   1263 
   1264     if (jid < 0 || jid > GLFW_JOYSTICK_LAST)
   1265     {
   1266         _glfwInputError(GLFW_INVALID_ENUM, "Invalid joystick ID %i", jid);
   1267         return GLFW_FALSE;
   1268     }
   1269 
   1270     js = _glfw.joysticks + jid;
   1271     if (!js->connected)
   1272         return GLFW_FALSE;
   1273 
   1274     if (!_glfwPlatformPollJoystick(js, _GLFW_POLL_ALL))
   1275         return GLFW_FALSE;
   1276 
   1277     if (!js->mapping)
   1278         return GLFW_FALSE;
   1279 
   1280     for (i = 0;  i <= GLFW_GAMEPAD_BUTTON_LAST;  i++)
   1281     {
   1282         const _GLFWmapelement* e = js->mapping->buttons + i;
   1283         if (e->type == _GLFW_JOYSTICK_AXIS)
   1284         {
   1285             const float value = js->axes[e->index] * e->axisScale + e->axisOffset;
   1286             // HACK: This should be baked into the value transform
   1287             // TODO: Bake into transform when implementing output modifiers
   1288             if (e->axisOffset < 0 || (e->axisOffset == 0 && e->axisScale > 0))
   1289             {
   1290                 if (value >= 0.f)
   1291                     state->buttons[i] = GLFW_PRESS;
   1292             }
   1293             else
   1294             {
   1295                 if (value <= 0.f)
   1296                     state->buttons[i] = GLFW_PRESS;
   1297             }
   1298         }
   1299         else if (e->type == _GLFW_JOYSTICK_HATBIT)
   1300         {
   1301             const unsigned int hat = e->index >> 4;
   1302             const unsigned int bit = e->index & 0xf;
   1303             if (js->hats[hat] & bit)
   1304                 state->buttons[i] = GLFW_PRESS;
   1305         }
   1306         else if (e->type == _GLFW_JOYSTICK_BUTTON)
   1307             state->buttons[i] = js->buttons[e->index];
   1308     }
   1309 
   1310     for (i = 0;  i <= GLFW_GAMEPAD_AXIS_LAST;  i++)
   1311     {
   1312         const _GLFWmapelement* e = js->mapping->axes + i;
   1313         if (e->type == _GLFW_JOYSTICK_AXIS)
   1314         {
   1315             const float value = js->axes[e->index] * e->axisScale + e->axisOffset;
   1316             state->axes[i] = _glfw_fminf(_glfw_fmaxf(value, -1.f), 1.f);
   1317         }
   1318         else if (e->type == _GLFW_JOYSTICK_HATBIT)
   1319         {
   1320             const unsigned int hat = e->index >> 4;
   1321             const unsigned int bit = e->index & 0xf;
   1322             if (js->hats[hat] & bit)
   1323                 state->axes[i] = 1.f;
   1324             else
   1325                 state->axes[i] = -1.f;
   1326         }
   1327         else if (e->type == _GLFW_JOYSTICK_BUTTON)
   1328             state->axes[i] = js->buttons[e->index] * 2.f - 1.f;
   1329     }
   1330 
   1331     return GLFW_TRUE;
   1332 }
   1333 
   1334 GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string)
   1335 {
   1336     assert(string != NULL);
   1337 
   1338     _GLFW_REQUIRE_INIT();
   1339     _glfwPlatformSetClipboardString(string);
   1340 }
   1341 
   1342 GLFWAPI const char* glfwGetClipboardString(GLFWwindow* handle)
   1343 {
   1344     _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
   1345     return _glfwPlatformGetClipboardString();
   1346 }
   1347 
   1348 GLFWAPI double glfwGetTime(void)
   1349 {
   1350     _GLFW_REQUIRE_INIT_OR_RETURN(0.0);
   1351     return (double) (_glfwPlatformGetTimerValue() - _glfw.timer.offset) /
   1352         _glfwPlatformGetTimerFrequency();
   1353 }
   1354 
   1355 GLFWAPI void glfwSetTime(double time)
   1356 {
   1357     _GLFW_REQUIRE_INIT();
   1358 
   1359     if (time != time || time < 0.0 || time > 18446744073.0)
   1360     {
   1361         _glfwInputError(GLFW_INVALID_VALUE, "Invalid time %f", time);
   1362         return;
   1363     }
   1364 
   1365     _glfw.timer.offset = _glfwPlatformGetTimerValue() -
   1366         (uint64_t) (time * _glfwPlatformGetTimerFrequency());
   1367 }
   1368 
   1369 GLFWAPI uint64_t glfwGetTimerValue(void)
   1370 {
   1371     _GLFW_REQUIRE_INIT_OR_RETURN(0);
   1372     return _glfwPlatformGetTimerValue();
   1373 }
   1374 
   1375 GLFWAPI uint64_t glfwGetTimerFrequency(void)
   1376 {
   1377     _GLFW_REQUIRE_INIT_OR_RETURN(0);
   1378     return _glfwPlatformGetTimerFrequency();
   1379 }
   1380