medfall

A super great game engine
Log | Files | Refs

mir_init.cc (10136B)


      1 //========================================================================
      2 // GLFW 3.3 Mir - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2014-2017 Brandon Schaefer <brandon.schaefer@canonical.com>
      5 //
      6 // This software is provided 'as-is', without any express or implied
      7 // warranty. In no event will the authors be held liable for any damages
      8 // arising from the use of this software.
      9 //
     10 // Permission is granted to anyone to use this software for any purpose,
     11 // including commercial applications, and to alter it and redistribute it
     12 // freely, subject to the following restrictions:
     13 //
     14 // 1. The origin of this software must not be misrepresented; you must not
     15 //    claim that you wrote the original software. If you use this software
     16 //    in a product, an acknowledgment in the product documentation would
     17 //    be appreciated but is not required.
     18 //
     19 // 2. Altered source versions must be plainly marked as such, and must not
     20 //    be misrepresented as being the original software.
     21 //
     22 // 3. This notice may not be removed or altered from any source
     23 //    distribution.
     24 //
     25 //========================================================================
     26 
     27 #include "internal.h"
     28 
     29 #include <linux/input.h>
     30 #include <stdlib.h>
     31 #include <string.h>
     32 
     33 
     34 // Create key code translation tables
     35 //
     36 static void createKeyTables(void)
     37 {
     38     int scancode;
     39 
     40     memset(_glfw.mir.keycodes, -1, sizeof(_glfw.mir.keycodes));
     41     memset(_glfw.mir.scancodes, -1, sizeof(_glfw.mir.scancodes));
     42 
     43     _glfw.mir.keycodes[KEY_GRAVE]      = GLFW_KEY_GRAVE_ACCENT;
     44     _glfw.mir.keycodes[KEY_1]          = GLFW_KEY_1;
     45     _glfw.mir.keycodes[KEY_2]          = GLFW_KEY_2;
     46     _glfw.mir.keycodes[KEY_3]          = GLFW_KEY_3;
     47     _glfw.mir.keycodes[KEY_4]          = GLFW_KEY_4;
     48     _glfw.mir.keycodes[KEY_5]          = GLFW_KEY_5;
     49     _glfw.mir.keycodes[KEY_6]          = GLFW_KEY_6;
     50     _glfw.mir.keycodes[KEY_7]          = GLFW_KEY_7;
     51     _glfw.mir.keycodes[KEY_8]          = GLFW_KEY_8;
     52     _glfw.mir.keycodes[KEY_9]          = GLFW_KEY_9;
     53     _glfw.mir.keycodes[KEY_0]          = GLFW_KEY_0;
     54     _glfw.mir.keycodes[KEY_SPACE]      = GLFW_KEY_SPACE;
     55     _glfw.mir.keycodes[KEY_MINUS]      = GLFW_KEY_MINUS;
     56     _glfw.mir.keycodes[KEY_EQUAL]      = GLFW_KEY_EQUAL;
     57     _glfw.mir.keycodes[KEY_Q]          = GLFW_KEY_Q;
     58     _glfw.mir.keycodes[KEY_W]          = GLFW_KEY_W;
     59     _glfw.mir.keycodes[KEY_E]          = GLFW_KEY_E;
     60     _glfw.mir.keycodes[KEY_R]          = GLFW_KEY_R;
     61     _glfw.mir.keycodes[KEY_T]          = GLFW_KEY_T;
     62     _glfw.mir.keycodes[KEY_Y]          = GLFW_KEY_Y;
     63     _glfw.mir.keycodes[KEY_U]          = GLFW_KEY_U;
     64     _glfw.mir.keycodes[KEY_I]          = GLFW_KEY_I;
     65     _glfw.mir.keycodes[KEY_O]          = GLFW_KEY_O;
     66     _glfw.mir.keycodes[KEY_P]          = GLFW_KEY_P;
     67     _glfw.mir.keycodes[KEY_LEFTBRACE]  = GLFW_KEY_LEFT_BRACKET;
     68     _glfw.mir.keycodes[KEY_RIGHTBRACE] = GLFW_KEY_RIGHT_BRACKET;
     69     _glfw.mir.keycodes[KEY_A]          = GLFW_KEY_A;
     70     _glfw.mir.keycodes[KEY_S]          = GLFW_KEY_S;
     71     _glfw.mir.keycodes[KEY_D]          = GLFW_KEY_D;
     72     _glfw.mir.keycodes[KEY_F]          = GLFW_KEY_F;
     73     _glfw.mir.keycodes[KEY_G]          = GLFW_KEY_G;
     74     _glfw.mir.keycodes[KEY_H]          = GLFW_KEY_H;
     75     _glfw.mir.keycodes[KEY_J]          = GLFW_KEY_J;
     76     _glfw.mir.keycodes[KEY_K]          = GLFW_KEY_K;
     77     _glfw.mir.keycodes[KEY_L]          = GLFW_KEY_L;
     78     _glfw.mir.keycodes[KEY_SEMICOLON]  = GLFW_KEY_SEMICOLON;
     79     _glfw.mir.keycodes[KEY_APOSTROPHE] = GLFW_KEY_APOSTROPHE;
     80     _glfw.mir.keycodes[KEY_Z]          = GLFW_KEY_Z;
     81     _glfw.mir.keycodes[KEY_X]          = GLFW_KEY_X;
     82     _glfw.mir.keycodes[KEY_C]          = GLFW_KEY_C;
     83     _glfw.mir.keycodes[KEY_V]          = GLFW_KEY_V;
     84     _glfw.mir.keycodes[KEY_B]          = GLFW_KEY_B;
     85     _glfw.mir.keycodes[KEY_N]          = GLFW_KEY_N;
     86     _glfw.mir.keycodes[KEY_M]          = GLFW_KEY_M;
     87     _glfw.mir.keycodes[KEY_COMMA]      = GLFW_KEY_COMMA;
     88     _glfw.mir.keycodes[KEY_DOT]        = GLFW_KEY_PERIOD;
     89     _glfw.mir.keycodes[KEY_SLASH]      = GLFW_KEY_SLASH;
     90     _glfw.mir.keycodes[KEY_BACKSLASH]  = GLFW_KEY_BACKSLASH;
     91     _glfw.mir.keycodes[KEY_ESC]        = GLFW_KEY_ESCAPE;
     92     _glfw.mir.keycodes[KEY_TAB]        = GLFW_KEY_TAB;
     93     _glfw.mir.keycodes[KEY_LEFTSHIFT]  = GLFW_KEY_LEFT_SHIFT;
     94     _glfw.mir.keycodes[KEY_RIGHTSHIFT] = GLFW_KEY_RIGHT_SHIFT;
     95     _glfw.mir.keycodes[KEY_LEFTCTRL]   = GLFW_KEY_LEFT_CONTROL;
     96     _glfw.mir.keycodes[KEY_RIGHTCTRL]  = GLFW_KEY_RIGHT_CONTROL;
     97     _glfw.mir.keycodes[KEY_LEFTALT]    = GLFW_KEY_LEFT_ALT;
     98     _glfw.mir.keycodes[KEY_RIGHTALT]   = GLFW_KEY_RIGHT_ALT;
     99     _glfw.mir.keycodes[KEY_LEFTMETA]   = GLFW_KEY_LEFT_SUPER;
    100     _glfw.mir.keycodes[KEY_RIGHTMETA]  = GLFW_KEY_RIGHT_SUPER;
    101     _glfw.mir.keycodes[KEY_MENU]       = GLFW_KEY_MENU;
    102     _glfw.mir.keycodes[KEY_NUMLOCK]    = GLFW_KEY_NUM_LOCK;
    103     _glfw.mir.keycodes[KEY_CAPSLOCK]   = GLFW_KEY_CAPS_LOCK;
    104     _glfw.mir.keycodes[KEY_PRINT]      = GLFW_KEY_PRINT_SCREEN;
    105     _glfw.mir.keycodes[KEY_SCROLLLOCK] = GLFW_KEY_SCROLL_LOCK;
    106     _glfw.mir.keycodes[KEY_PAUSE]      = GLFW_KEY_PAUSE;
    107     _glfw.mir.keycodes[KEY_DELETE]     = GLFW_KEY_DELETE;
    108     _glfw.mir.keycodes[KEY_BACKSPACE]  = GLFW_KEY_BACKSPACE;
    109     _glfw.mir.keycodes[KEY_ENTER]      = GLFW_KEY_ENTER;
    110     _glfw.mir.keycodes[KEY_HOME]       = GLFW_KEY_HOME;
    111     _glfw.mir.keycodes[KEY_END]        = GLFW_KEY_END;
    112     _glfw.mir.keycodes[KEY_PAGEUP]     = GLFW_KEY_PAGE_UP;
    113     _glfw.mir.keycodes[KEY_PAGEDOWN]   = GLFW_KEY_PAGE_DOWN;
    114     _glfw.mir.keycodes[KEY_INSERT]     = GLFW_KEY_INSERT;
    115     _glfw.mir.keycodes[KEY_LEFT]       = GLFW_KEY_LEFT;
    116     _glfw.mir.keycodes[KEY_RIGHT]      = GLFW_KEY_RIGHT;
    117     _glfw.mir.keycodes[KEY_DOWN]       = GLFW_KEY_DOWN;
    118     _glfw.mir.keycodes[KEY_UP]         = GLFW_KEY_UP;
    119     _glfw.mir.keycodes[KEY_F1]         = GLFW_KEY_F1;
    120     _glfw.mir.keycodes[KEY_F2]         = GLFW_KEY_F2;
    121     _glfw.mir.keycodes[KEY_F3]         = GLFW_KEY_F3;
    122     _glfw.mir.keycodes[KEY_F4]         = GLFW_KEY_F4;
    123     _glfw.mir.keycodes[KEY_F5]         = GLFW_KEY_F5;
    124     _glfw.mir.keycodes[KEY_F6]         = GLFW_KEY_F6;
    125     _glfw.mir.keycodes[KEY_F7]         = GLFW_KEY_F7;
    126     _glfw.mir.keycodes[KEY_F8]         = GLFW_KEY_F8;
    127     _glfw.mir.keycodes[KEY_F9]         = GLFW_KEY_F9;
    128     _glfw.mir.keycodes[KEY_F10]        = GLFW_KEY_F10;
    129     _glfw.mir.keycodes[KEY_F11]        = GLFW_KEY_F11;
    130     _glfw.mir.keycodes[KEY_F12]        = GLFW_KEY_F12;
    131     _glfw.mir.keycodes[KEY_F13]        = GLFW_KEY_F13;
    132     _glfw.mir.keycodes[KEY_F14]        = GLFW_KEY_F14;
    133     _glfw.mir.keycodes[KEY_F15]        = GLFW_KEY_F15;
    134     _glfw.mir.keycodes[KEY_F16]        = GLFW_KEY_F16;
    135     _glfw.mir.keycodes[KEY_F17]        = GLFW_KEY_F17;
    136     _glfw.mir.keycodes[KEY_F18]        = GLFW_KEY_F18;
    137     _glfw.mir.keycodes[KEY_F19]        = GLFW_KEY_F19;
    138     _glfw.mir.keycodes[KEY_F20]        = GLFW_KEY_F20;
    139     _glfw.mir.keycodes[KEY_F21]        = GLFW_KEY_F21;
    140     _glfw.mir.keycodes[KEY_F22]        = GLFW_KEY_F22;
    141     _glfw.mir.keycodes[KEY_F23]        = GLFW_KEY_F23;
    142     _glfw.mir.keycodes[KEY_F24]        = GLFW_KEY_F24;
    143     _glfw.mir.keycodes[KEY_KPSLASH]    = GLFW_KEY_KP_DIVIDE;
    144     _glfw.mir.keycodes[KEY_KPDOT]      = GLFW_KEY_KP_MULTIPLY;
    145     _glfw.mir.keycodes[KEY_KPMINUS]    = GLFW_KEY_KP_SUBTRACT;
    146     _glfw.mir.keycodes[KEY_KPPLUS]     = GLFW_KEY_KP_ADD;
    147     _glfw.mir.keycodes[KEY_KP0]        = GLFW_KEY_KP_0;
    148     _glfw.mir.keycodes[KEY_KP1]        = GLFW_KEY_KP_1;
    149     _glfw.mir.keycodes[KEY_KP2]        = GLFW_KEY_KP_2;
    150     _glfw.mir.keycodes[KEY_KP3]        = GLFW_KEY_KP_3;
    151     _glfw.mir.keycodes[KEY_KP4]        = GLFW_KEY_KP_4;
    152     _glfw.mir.keycodes[KEY_KP5]        = GLFW_KEY_KP_5;
    153     _glfw.mir.keycodes[KEY_KP6]        = GLFW_KEY_KP_6;
    154     _glfw.mir.keycodes[KEY_KP7]        = GLFW_KEY_KP_7;
    155     _glfw.mir.keycodes[KEY_KP8]        = GLFW_KEY_KP_8;
    156     _glfw.mir.keycodes[KEY_KP9]        = GLFW_KEY_KP_9;
    157     _glfw.mir.keycodes[KEY_KPCOMMA]    = GLFW_KEY_KP_DECIMAL;
    158     _glfw.mir.keycodes[KEY_KPEQUAL]    = GLFW_KEY_KP_EQUAL;
    159     _glfw.mir.keycodes[KEY_KPENTER]    = GLFW_KEY_KP_ENTER;
    160 
    161     for (scancode = 0;  scancode < 256;  scancode++)
    162     {
    163         if (_glfw.mir.keycodes[scancode] > 0)
    164             _glfw.mir.scancodes[_glfw.mir.keycodes[scancode]] = scancode;
    165     }
    166 }
    167 
    168 
    169 //////////////////////////////////////////////////////////////////////////
    170 //////                       GLFW internal API                      //////
    171 //////////////////////////////////////////////////////////////////////////
    172 
    173 int _glfwPlatformInit(void)
    174 {
    175     int error;
    176 
    177     _glfw.mir.connection = mir_connect_sync(NULL, __PRETTY_FUNCTION__);
    178 
    179     if (!mir_connection_is_valid(_glfw.mir.connection))
    180     {
    181         _glfwInputError(GLFW_PLATFORM_ERROR,
    182                         "Mir: Unable to connect to server: %s",
    183                         mir_connection_get_error_message(_glfw.mir.connection));
    184 
    185         return GLFW_FALSE;
    186     }
    187 
    188     _glfw.mir.display =
    189         mir_connection_get_egl_native_display(_glfw.mir.connection);
    190 
    191     createKeyTables();
    192 
    193     if (!_glfwInitJoysticksLinux())
    194         return GLFW_FALSE;
    195 
    196     _glfwInitTimerPOSIX();
    197 
    198     _glfw.mir.eventQueue = calloc(1, sizeof(EventQueue));
    199     _glfwInitEventQueueMir(_glfw.mir.eventQueue);
    200 
    201     error = pthread_mutex_init(&_glfw.mir.eventMutex, NULL);
    202     if (error)
    203     {
    204         _glfwInputError(GLFW_PLATFORM_ERROR,
    205                         "Mir: Failed to create event mutex: %s",
    206                         strerror(error));
    207         return GLFW_FALSE;
    208     }
    209 
    210     _glfwPollMonitorsMir();
    211     return GLFW_TRUE;
    212 }
    213 
    214 void _glfwPlatformTerminate(void)
    215 {
    216     _glfwTerminateEGL();
    217     _glfwTerminateJoysticksLinux();
    218 
    219     _glfwDeleteEventQueueMir(_glfw.mir.eventQueue);
    220 
    221     pthread_mutex_destroy(&_glfw.mir.eventMutex);
    222 
    223     mir_connection_release(_glfw.mir.connection);
    224 }
    225 
    226 const char* _glfwPlatformGetVersionString(void)
    227 {
    228     return _GLFW_VERSION_NUMBER " Mir EGL"
    229 #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
    230         " clock_gettime"
    231 #else
    232         " gettimeofday"
    233 #endif
    234         " evdev"
    235 #if defined(_GLFW_BUILD_DLL)
    236         " shared"
    237 #endif
    238         ;
    239 }
    240