1 //========================================================================
2 // GLFW 3.2 Mir - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2014-2015 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 //
createKeyTables(void)36 static void createKeyTables(void)
37 {
38 memset(_glfw.mir.publicKeys, -1, sizeof(_glfw.mir.publicKeys));
39
40 _glfw.mir.publicKeys[KEY_GRAVE] = GLFW_KEY_GRAVE_ACCENT;
41 _glfw.mir.publicKeys[KEY_1] = GLFW_KEY_1;
42 _glfw.mir.publicKeys[KEY_2] = GLFW_KEY_2;
43 _glfw.mir.publicKeys[KEY_3] = GLFW_KEY_3;
44 _glfw.mir.publicKeys[KEY_4] = GLFW_KEY_4;
45 _glfw.mir.publicKeys[KEY_5] = GLFW_KEY_5;
46 _glfw.mir.publicKeys[KEY_6] = GLFW_KEY_6;
47 _glfw.mir.publicKeys[KEY_7] = GLFW_KEY_7;
48 _glfw.mir.publicKeys[KEY_8] = GLFW_KEY_8;
49 _glfw.mir.publicKeys[KEY_9] = GLFW_KEY_9;
50 _glfw.mir.publicKeys[KEY_0] = GLFW_KEY_0;
51 _glfw.mir.publicKeys[KEY_MINUS] = GLFW_KEY_MINUS;
52 _glfw.mir.publicKeys[KEY_EQUAL] = GLFW_KEY_EQUAL;
53 _glfw.mir.publicKeys[KEY_Q] = GLFW_KEY_Q;
54 _glfw.mir.publicKeys[KEY_W] = GLFW_KEY_W;
55 _glfw.mir.publicKeys[KEY_E] = GLFW_KEY_E;
56 _glfw.mir.publicKeys[KEY_R] = GLFW_KEY_R;
57 _glfw.mir.publicKeys[KEY_T] = GLFW_KEY_T;
58 _glfw.mir.publicKeys[KEY_Y] = GLFW_KEY_Y;
59 _glfw.mir.publicKeys[KEY_U] = GLFW_KEY_U;
60 _glfw.mir.publicKeys[KEY_I] = GLFW_KEY_I;
61 _glfw.mir.publicKeys[KEY_O] = GLFW_KEY_O;
62 _glfw.mir.publicKeys[KEY_P] = GLFW_KEY_P;
63 _glfw.mir.publicKeys[KEY_LEFTBRACE] = GLFW_KEY_LEFT_BRACKET;
64 _glfw.mir.publicKeys[KEY_RIGHTBRACE] = GLFW_KEY_RIGHT_BRACKET;
65 _glfw.mir.publicKeys[KEY_A] = GLFW_KEY_A;
66 _glfw.mir.publicKeys[KEY_S] = GLFW_KEY_S;
67 _glfw.mir.publicKeys[KEY_D] = GLFW_KEY_D;
68 _glfw.mir.publicKeys[KEY_F] = GLFW_KEY_F;
69 _glfw.mir.publicKeys[KEY_G] = GLFW_KEY_G;
70 _glfw.mir.publicKeys[KEY_H] = GLFW_KEY_H;
71 _glfw.mir.publicKeys[KEY_J] = GLFW_KEY_J;
72 _glfw.mir.publicKeys[KEY_K] = GLFW_KEY_K;
73 _glfw.mir.publicKeys[KEY_L] = GLFW_KEY_L;
74 _glfw.mir.publicKeys[KEY_SEMICOLON] = GLFW_KEY_SEMICOLON;
75 _glfw.mir.publicKeys[KEY_APOSTROPHE] = GLFW_KEY_APOSTROPHE;
76 _glfw.mir.publicKeys[KEY_Z] = GLFW_KEY_Z;
77 _glfw.mir.publicKeys[KEY_X] = GLFW_KEY_X;
78 _glfw.mir.publicKeys[KEY_C] = GLFW_KEY_C;
79 _glfw.mir.publicKeys[KEY_V] = GLFW_KEY_V;
80 _glfw.mir.publicKeys[KEY_B] = GLFW_KEY_B;
81 _glfw.mir.publicKeys[KEY_N] = GLFW_KEY_N;
82 _glfw.mir.publicKeys[KEY_M] = GLFW_KEY_M;
83 _glfw.mir.publicKeys[KEY_COMMA] = GLFW_KEY_COMMA;
84 _glfw.mir.publicKeys[KEY_DOT] = GLFW_KEY_PERIOD;
85 _glfw.mir.publicKeys[KEY_SLASH] = GLFW_KEY_SLASH;
86 _glfw.mir.publicKeys[KEY_BACKSLASH] = GLFW_KEY_BACKSLASH;
87 _glfw.mir.publicKeys[KEY_ESC] = GLFW_KEY_ESCAPE;
88 _glfw.mir.publicKeys[KEY_TAB] = GLFW_KEY_TAB;
89 _glfw.mir.publicKeys[KEY_LEFTSHIFT] = GLFW_KEY_LEFT_SHIFT;
90 _glfw.mir.publicKeys[KEY_RIGHTSHIFT] = GLFW_KEY_RIGHT_SHIFT;
91 _glfw.mir.publicKeys[KEY_LEFTCTRL] = GLFW_KEY_LEFT_CONTROL;
92 _glfw.mir.publicKeys[KEY_RIGHTCTRL] = GLFW_KEY_RIGHT_CONTROL;
93 _glfw.mir.publicKeys[KEY_LEFTALT] = GLFW_KEY_LEFT_ALT;
94 _glfw.mir.publicKeys[KEY_RIGHTALT] = GLFW_KEY_RIGHT_ALT;
95 _glfw.mir.publicKeys[KEY_LEFTMETA] = GLFW_KEY_LEFT_SUPER;
96 _glfw.mir.publicKeys[KEY_RIGHTMETA] = GLFW_KEY_RIGHT_SUPER;
97 _glfw.mir.publicKeys[KEY_MENU] = GLFW_KEY_MENU;
98 _glfw.mir.publicKeys[KEY_NUMLOCK] = GLFW_KEY_NUM_LOCK;
99 _glfw.mir.publicKeys[KEY_CAPSLOCK] = GLFW_KEY_CAPS_LOCK;
100 _glfw.mir.publicKeys[KEY_PRINT] = GLFW_KEY_PRINT_SCREEN;
101 _glfw.mir.publicKeys[KEY_SCROLLLOCK] = GLFW_KEY_SCROLL_LOCK;
102 _glfw.mir.publicKeys[KEY_PAUSE] = GLFW_KEY_PAUSE;
103 _glfw.mir.publicKeys[KEY_DELETE] = GLFW_KEY_DELETE;
104 _glfw.mir.publicKeys[KEY_BACKSPACE] = GLFW_KEY_BACKSPACE;
105 _glfw.mir.publicKeys[KEY_ENTER] = GLFW_KEY_ENTER;
106 _glfw.mir.publicKeys[KEY_HOME] = GLFW_KEY_HOME;
107 _glfw.mir.publicKeys[KEY_END] = GLFW_KEY_END;
108 _glfw.mir.publicKeys[KEY_PAGEUP] = GLFW_KEY_PAGE_UP;
109 _glfw.mir.publicKeys[KEY_PAGEDOWN] = GLFW_KEY_PAGE_DOWN;
110 _glfw.mir.publicKeys[KEY_INSERT] = GLFW_KEY_INSERT;
111 _glfw.mir.publicKeys[KEY_LEFT] = GLFW_KEY_LEFT;
112 _glfw.mir.publicKeys[KEY_RIGHT] = GLFW_KEY_RIGHT;
113 _glfw.mir.publicKeys[KEY_DOWN] = GLFW_KEY_DOWN;
114 _glfw.mir.publicKeys[KEY_UP] = GLFW_KEY_UP;
115 _glfw.mir.publicKeys[KEY_F1] = GLFW_KEY_F1;
116 _glfw.mir.publicKeys[KEY_F2] = GLFW_KEY_F2;
117 _glfw.mir.publicKeys[KEY_F3] = GLFW_KEY_F3;
118 _glfw.mir.publicKeys[KEY_F4] = GLFW_KEY_F4;
119 _glfw.mir.publicKeys[KEY_F5] = GLFW_KEY_F5;
120 _glfw.mir.publicKeys[KEY_F6] = GLFW_KEY_F6;
121 _glfw.mir.publicKeys[KEY_F7] = GLFW_KEY_F7;
122 _glfw.mir.publicKeys[KEY_F8] = GLFW_KEY_F8;
123 _glfw.mir.publicKeys[KEY_F9] = GLFW_KEY_F9;
124 _glfw.mir.publicKeys[KEY_F10] = GLFW_KEY_F10;
125 _glfw.mir.publicKeys[KEY_F11] = GLFW_KEY_F11;
126 _glfw.mir.publicKeys[KEY_F12] = GLFW_KEY_F12;
127 _glfw.mir.publicKeys[KEY_F13] = GLFW_KEY_F13;
128 _glfw.mir.publicKeys[KEY_F14] = GLFW_KEY_F14;
129 _glfw.mir.publicKeys[KEY_F15] = GLFW_KEY_F15;
130 _glfw.mir.publicKeys[KEY_F16] = GLFW_KEY_F16;
131 _glfw.mir.publicKeys[KEY_F17] = GLFW_KEY_F17;
132 _glfw.mir.publicKeys[KEY_F18] = GLFW_KEY_F18;
133 _glfw.mir.publicKeys[KEY_F19] = GLFW_KEY_F19;
134 _glfw.mir.publicKeys[KEY_F20] = GLFW_KEY_F20;
135 _glfw.mir.publicKeys[KEY_F21] = GLFW_KEY_F21;
136 _glfw.mir.publicKeys[KEY_F22] = GLFW_KEY_F22;
137 _glfw.mir.publicKeys[KEY_F23] = GLFW_KEY_F23;
138 _glfw.mir.publicKeys[KEY_F24] = GLFW_KEY_F24;
139 _glfw.mir.publicKeys[KEY_KPSLASH] = GLFW_KEY_KP_DIVIDE;
140 _glfw.mir.publicKeys[KEY_KPDOT] = GLFW_KEY_KP_MULTIPLY;
141 _glfw.mir.publicKeys[KEY_KPMINUS] = GLFW_KEY_KP_SUBTRACT;
142 _glfw.mir.publicKeys[KEY_KPPLUS] = GLFW_KEY_KP_ADD;
143 _glfw.mir.publicKeys[KEY_KP0] = GLFW_KEY_KP_0;
144 _glfw.mir.publicKeys[KEY_KP1] = GLFW_KEY_KP_1;
145 _glfw.mir.publicKeys[KEY_KP2] = GLFW_KEY_KP_2;
146 _glfw.mir.publicKeys[KEY_KP3] = GLFW_KEY_KP_3;
147 _glfw.mir.publicKeys[KEY_KP4] = GLFW_KEY_KP_4;
148 _glfw.mir.publicKeys[KEY_KP5] = GLFW_KEY_KP_5;
149 _glfw.mir.publicKeys[KEY_KP6] = GLFW_KEY_KP_6;
150 _glfw.mir.publicKeys[KEY_KP7] = GLFW_KEY_KP_7;
151 _glfw.mir.publicKeys[KEY_KP8] = GLFW_KEY_KP_8;
152 _glfw.mir.publicKeys[KEY_KP9] = GLFW_KEY_KP_9;
153 _glfw.mir.publicKeys[KEY_KPCOMMA] = GLFW_KEY_KP_DECIMAL;
154 _glfw.mir.publicKeys[KEY_KPEQUAL] = GLFW_KEY_KP_EQUAL;
155 _glfw.mir.publicKeys[KEY_KPENTER] = GLFW_KEY_KP_ENTER;
156 }
157
158
159 //////////////////////////////////////////////////////////////////////////
160 ////// GLFW internal API //////
161 //////////////////////////////////////////////////////////////////////////
162
_glfwPlatformInit(void)163 int _glfwPlatformInit(void)
164 {
165 int error;
166
167 _glfw.mir.connection = mir_connect_sync(NULL, __PRETTY_FUNCTION__);
168
169 if (!mir_connection_is_valid(_glfw.mir.connection))
170 {
171 _glfwInputError(GLFW_PLATFORM_ERROR,
172 "Mir: Unable to connect to server: %s",
173 mir_connection_get_error_message(_glfw.mir.connection));
174
175 return GLFW_FALSE;
176 }
177
178 _glfw.mir.display =
179 mir_connection_get_egl_native_display(_glfw.mir.connection);
180
181 createKeyTables();
182
183 if (!_glfwInitThreadLocalStoragePOSIX())
184 return GLFW_FALSE;
185
186 if (!_glfwInitJoysticksLinux())
187 return GLFW_FALSE;
188
189 _glfwInitTimerPOSIX();
190
191 // Need the default conf for when we set a NULL cursor
192 _glfw.mir.default_conf = mir_cursor_configuration_from_name(mir_arrow_cursor_name);
193
194 _glfw.mir.event_queue = calloc(1, sizeof(EventQueue));
195 _glfwInitEventQueueMir(_glfw.mir.event_queue);
196
197 error = pthread_mutex_init(&_glfw.mir.event_mutex, NULL);
198 if (error)
199 {
200 _glfwInputError(GLFW_PLATFORM_ERROR,
201 "Mir: Failed to create event mutex: %s",
202 strerror(error));
203 return GLFW_FALSE;
204 }
205
206 return GLFW_TRUE;
207 }
208
_glfwPlatformTerminate(void)209 void _glfwPlatformTerminate(void)
210 {
211 _glfwTerminateEGL();
212 _glfwTerminateJoysticksLinux();
213 _glfwTerminateThreadLocalStoragePOSIX();
214
215 _glfwDeleteEventQueueMir(_glfw.mir.event_queue);
216
217 pthread_mutex_destroy(&_glfw.mir.event_mutex);
218
219 mir_connection_release(_glfw.mir.connection);
220 }
221
_glfwPlatformGetVersionString(void)222 const char* _glfwPlatformGetVersionString(void)
223 {
224 return _GLFW_VERSION_NUMBER " Mir EGL"
225 #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
226 " clock_gettime"
227 #else
228 " gettimeofday"
229 #endif
230 #if defined(__linux__)
231 " /dev/js"
232 #endif
233 #if defined(_GLFW_BUILD_DLL)
234 " shared"
235 #endif
236 ;
237 }
238
239