| 1 | //======================================================================== |
| 2 | // GLFW 3.2 X11 - www.glfw.org |
| 3 | //------------------------------------------------------------------------ |
| 4 | // Copyright (c) 2002-2006 Marcus Geelnard |
| 5 | // Copyright (c) 2006-2016 Camilla Berglund <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 | |
| 28 | #include "internal.h" |
| 29 | |
| 30 | #include <X11/Xresource.h> |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <limits.h> |
| 35 | #include <stdio.h> |
| 36 | #include <locale.h> |
| 37 | |
| 38 | |
| 39 | // Translate an X11 key code to a GLFW key code. |
| 40 | // |
| 41 | static int translateKeyCode(int scancode) |
| 42 | { |
| 43 | int keySym; |
| 44 | |
| 45 | // Valid key code range is [8,255], according to the Xlib manual |
| 46 | if (scancode < 8 || scancode > 255) |
| 47 | return GLFW_KEY_UNKNOWN; |
| 48 | |
| 49 | if (_glfw.x11.xkb.available) |
| 50 | { |
| 51 | // Try secondary keysym, for numeric keypad keys |
| 52 | // Note: This way we always force "NumLock = ON", which is intentional |
| 53 | // since the returned key code should correspond to a physical |
| 54 | // location. |
| 55 | keySym = XkbKeycodeToKeysym(_glfw.x11.display, scancode, 0, 1); |
| 56 | switch (keySym) |
| 57 | { |
| 58 | case XK_KP_0: return GLFW_KEY_KP_0; |
| 59 | case XK_KP_1: return GLFW_KEY_KP_1; |
| 60 | case XK_KP_2: return GLFW_KEY_KP_2; |
| 61 | case XK_KP_3: return GLFW_KEY_KP_3; |
| 62 | case XK_KP_4: return GLFW_KEY_KP_4; |
| 63 | case XK_KP_5: return GLFW_KEY_KP_5; |
| 64 | case XK_KP_6: return GLFW_KEY_KP_6; |
| 65 | case XK_KP_7: return GLFW_KEY_KP_7; |
| 66 | case XK_KP_8: return GLFW_KEY_KP_8; |
| 67 | case XK_KP_9: return GLFW_KEY_KP_9; |
| 68 | case XK_KP_Separator: |
| 69 | case XK_KP_Decimal: return GLFW_KEY_KP_DECIMAL; |
| 70 | case XK_KP_Equal: return GLFW_KEY_KP_EQUAL; |
| 71 | case XK_KP_Enter: return GLFW_KEY_KP_ENTER; |
| 72 | default: break; |
| 73 | } |
| 74 | |
| 75 | // Now try primary keysym for function keys (non-printable keys). These |
| 76 | // should not be layout dependent (i.e. US layout and international |
| 77 | // layouts should give the same result). |
| 78 | keySym = XkbKeycodeToKeysym(_glfw.x11.display, scancode, 0, 0); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | int dummy; |
| 83 | KeySym* keySyms; |
| 84 | |
| 85 | keySyms = XGetKeyboardMapping(_glfw.x11.display, scancode, 1, &dummy); |
| 86 | keySym = keySyms[0]; |
| 87 | XFree(keySyms); |
| 88 | } |
| 89 | |
| 90 | switch (keySym) |
| 91 | { |
| 92 | case XK_Escape: return GLFW_KEY_ESCAPE; |
| 93 | case XK_Tab: return GLFW_KEY_TAB; |
| 94 | case XK_Shift_L: return GLFW_KEY_LEFT_SHIFT; |
| 95 | case XK_Shift_R: return GLFW_KEY_RIGHT_SHIFT; |
| 96 | case XK_Control_L: return GLFW_KEY_LEFT_CONTROL; |
| 97 | case XK_Control_R: return GLFW_KEY_RIGHT_CONTROL; |
| 98 | case XK_Meta_L: |
| 99 | case XK_Alt_L: return GLFW_KEY_LEFT_ALT; |
| 100 | case XK_Mode_switch: // Mapped to Alt_R on many keyboards |
| 101 | case XK_ISO_Level3_Shift: // AltGr on at least some machines |
| 102 | case XK_Meta_R: |
| 103 | case XK_Alt_R: return GLFW_KEY_RIGHT_ALT; |
| 104 | case XK_Super_L: return GLFW_KEY_LEFT_SUPER; |
| 105 | case XK_Super_R: return GLFW_KEY_RIGHT_SUPER; |
| 106 | case XK_Menu: return GLFW_KEY_MENU; |
| 107 | case XK_Num_Lock: return GLFW_KEY_NUM_LOCK; |
| 108 | case XK_Caps_Lock: return GLFW_KEY_CAPS_LOCK; |
| 109 | case XK_Print: return GLFW_KEY_PRINT_SCREEN; |
| 110 | case XK_Scroll_Lock: return GLFW_KEY_SCROLL_LOCK; |
| 111 | case XK_Pause: return GLFW_KEY_PAUSE; |
| 112 | case XK_Delete: return GLFW_KEY_DELETE; |
| 113 | case XK_BackSpace: return GLFW_KEY_BACKSPACE; |
| 114 | case XK_Return: return GLFW_KEY_ENTER; |
| 115 | case XK_Home: return GLFW_KEY_HOME; |
| 116 | case XK_End: return GLFW_KEY_END; |
| 117 | case XK_Page_Up: return GLFW_KEY_PAGE_UP; |
| 118 | case XK_Page_Down: return GLFW_KEY_PAGE_DOWN; |
| 119 | case XK_Insert: return GLFW_KEY_INSERT; |
| 120 | case XK_Left: return GLFW_KEY_LEFT; |
| 121 | case XK_Right: return GLFW_KEY_RIGHT; |
| 122 | case XK_Down: return GLFW_KEY_DOWN; |
| 123 | case XK_Up: return GLFW_KEY_UP; |
| 124 | case XK_F1: return GLFW_KEY_F1; |
| 125 | case XK_F2: return GLFW_KEY_F2; |
| 126 | case XK_F3: return GLFW_KEY_F3; |
| 127 | case XK_F4: return GLFW_KEY_F4; |
| 128 | case XK_F5: return GLFW_KEY_F5; |
| 129 | case XK_F6: return GLFW_KEY_F6; |
| 130 | case XK_F7: return GLFW_KEY_F7; |
| 131 | case XK_F8: return GLFW_KEY_F8; |
| 132 | case XK_F9: return GLFW_KEY_F9; |
| 133 | case XK_F10: return GLFW_KEY_F10; |
| 134 | case XK_F11: return GLFW_KEY_F11; |
| 135 | case XK_F12: return GLFW_KEY_F12; |
| 136 | case XK_F13: return GLFW_KEY_F13; |
| 137 | case XK_F14: return GLFW_KEY_F14; |
| 138 | case XK_F15: return GLFW_KEY_F15; |
| 139 | case XK_F16: return GLFW_KEY_F16; |
| 140 | case XK_F17: return GLFW_KEY_F17; |
| 141 | case XK_F18: return GLFW_KEY_F18; |
| 142 | case XK_F19: return GLFW_KEY_F19; |
| 143 | case XK_F20: return GLFW_KEY_F20; |
| 144 | case XK_F21: return GLFW_KEY_F21; |
| 145 | case XK_F22: return GLFW_KEY_F22; |
| 146 | case XK_F23: return GLFW_KEY_F23; |
| 147 | case XK_F24: return GLFW_KEY_F24; |
| 148 | case XK_F25: return GLFW_KEY_F25; |
| 149 | |
| 150 | // Numeric keypad |
| 151 | case XK_KP_Divide: return GLFW_KEY_KP_DIVIDE; |
| 152 | case XK_KP_Multiply: return GLFW_KEY_KP_MULTIPLY; |
| 153 | case XK_KP_Subtract: return GLFW_KEY_KP_SUBTRACT; |
| 154 | case XK_KP_Add: return GLFW_KEY_KP_ADD; |
| 155 | |
| 156 | // These should have been detected in secondary keysym test above! |
| 157 | case XK_KP_Insert: return GLFW_KEY_KP_0; |
| 158 | case XK_KP_End: return GLFW_KEY_KP_1; |
| 159 | case XK_KP_Down: return GLFW_KEY_KP_2; |
| 160 | case XK_KP_Page_Down: return GLFW_KEY_KP_3; |
| 161 | case XK_KP_Left: return GLFW_KEY_KP_4; |
| 162 | case XK_KP_Right: return GLFW_KEY_KP_6; |
| 163 | case XK_KP_Home: return GLFW_KEY_KP_7; |
| 164 | case XK_KP_Up: return GLFW_KEY_KP_8; |
| 165 | case XK_KP_Page_Up: return GLFW_KEY_KP_9; |
| 166 | case XK_KP_Delete: return GLFW_KEY_KP_DECIMAL; |
| 167 | case XK_KP_Equal: return GLFW_KEY_KP_EQUAL; |
| 168 | case XK_KP_Enter: return GLFW_KEY_KP_ENTER; |
| 169 | |
| 170 | // Last resort: Check for printable keys (should not happen if the XKB |
| 171 | // extension is available). This will give a layout dependent mapping |
| 172 | // (which is wrong, and we may miss some keys, especially on non-US |
| 173 | // keyboards), but it's better than nothing... |
| 174 | case XK_a: return GLFW_KEY_A; |
| 175 | case XK_b: return GLFW_KEY_B; |
| 176 | case XK_c: return GLFW_KEY_C; |
| 177 | case XK_d: return GLFW_KEY_D; |
| 178 | case XK_e: return GLFW_KEY_E; |
| 179 | case XK_f: return GLFW_KEY_F; |
| 180 | case XK_g: return GLFW_KEY_G; |
| 181 | case XK_h: return GLFW_KEY_H; |
| 182 | case XK_i: return GLFW_KEY_I; |
| 183 | case XK_j: return GLFW_KEY_J; |
| 184 | case XK_k: return GLFW_KEY_K; |
| 185 | case XK_l: return GLFW_KEY_L; |
| 186 | case XK_m: return GLFW_KEY_M; |
| 187 | case XK_n: return GLFW_KEY_N; |
| 188 | case XK_o: return GLFW_KEY_O; |
| 189 | case XK_p: return GLFW_KEY_P; |
| 190 | case XK_q: return GLFW_KEY_Q; |
| 191 | case XK_r: return GLFW_KEY_R; |
| 192 | case XK_s: return GLFW_KEY_S; |
| 193 | case XK_t: return GLFW_KEY_T; |
| 194 | case XK_u: return GLFW_KEY_U; |
| 195 | case XK_v: return GLFW_KEY_V; |
| 196 | case XK_w: return GLFW_KEY_W; |
| 197 | case XK_x: return GLFW_KEY_X; |
| 198 | case XK_y: return GLFW_KEY_Y; |
| 199 | case XK_z: return GLFW_KEY_Z; |
| 200 | case XK_1: return GLFW_KEY_1; |
| 201 | case XK_2: return GLFW_KEY_2; |
| 202 | case XK_3: return GLFW_KEY_3; |
| 203 | case XK_4: return GLFW_KEY_4; |
| 204 | case XK_5: return GLFW_KEY_5; |
| 205 | case XK_6: return GLFW_KEY_6; |
| 206 | case XK_7: return GLFW_KEY_7; |
| 207 | case XK_8: return GLFW_KEY_8; |
| 208 | case XK_9: return GLFW_KEY_9; |
| 209 | case XK_0: return GLFW_KEY_0; |
| 210 | case XK_space: return GLFW_KEY_SPACE; |
| 211 | case XK_minus: return GLFW_KEY_MINUS; |
| 212 | case XK_equal: return GLFW_KEY_EQUAL; |
| 213 | case XK_bracketleft: return GLFW_KEY_LEFT_BRACKET; |
| 214 | case XK_bracketright: return GLFW_KEY_RIGHT_BRACKET; |
| 215 | case XK_backslash: return GLFW_KEY_BACKSLASH; |
| 216 | case XK_semicolon: return GLFW_KEY_SEMICOLON; |
| 217 | case XK_apostrophe: return GLFW_KEY_APOSTROPHE; |
| 218 | case XK_grave: return GLFW_KEY_GRAVE_ACCENT; |
| 219 | case XK_comma: return GLFW_KEY_COMMA; |
| 220 | case XK_period: return GLFW_KEY_PERIOD; |
| 221 | case XK_slash: return GLFW_KEY_SLASH; |
| 222 | case XK_less: return GLFW_KEY_WORLD_1; // At least in some layouts... |
| 223 | default: break; |
| 224 | } |
| 225 | |
| 226 | // No matching translation was found |
| 227 | return GLFW_KEY_UNKNOWN; |
| 228 | } |
| 229 | |
| 230 | // Create key code translation tables |
| 231 | // |
| 232 | static void createKeyTables(void) |
| 233 | { |
| 234 | int scancode, key; |
| 235 | |
| 236 | memset(_glfw.x11.publicKeys, -1, sizeof(_glfw.x11.publicKeys)); |
| 237 | memset(_glfw.x11.nativeKeys, -1, sizeof(_glfw.x11.nativeKeys)); |
| 238 | |
| 239 | if (_glfw.x11.xkb.available) |
| 240 | { |
| 241 | // Use XKB to determine physical key locations independently of the current |
| 242 | // keyboard layout |
| 243 | |
| 244 | char name[XkbKeyNameLength + 1]; |
| 245 | XkbDescPtr desc = XkbGetMap(_glfw.x11.display, 0, XkbUseCoreKbd); |
| 246 | XkbGetNames(_glfw.x11.display, XkbKeyNamesMask, desc); |
| 247 | |
| 248 | // Find the X11 key code -> GLFW key code mapping |
| 249 | for (scancode = desc->min_key_code; scancode <= desc->max_key_code; scancode++) |
| 250 | { |
| 251 | memcpy(name, desc->names->keys[scancode].name, XkbKeyNameLength); |
| 252 | name[XkbKeyNameLength] = '\0'; |
| 253 | |
| 254 | // Map the key name to a GLFW key code. Note: We only map printable |
| 255 | // keys here, and we use the US keyboard layout. The rest of the |
| 256 | // keys (function keys) are mapped using traditional KeySym |
| 257 | // translations. |
| 258 | if (strcmp(name, "TLDE" ) == 0) key = GLFW_KEY_GRAVE_ACCENT; |
| 259 | else if (strcmp(name, "AE01" ) == 0) key = GLFW_KEY_1; |
| 260 | else if (strcmp(name, "AE02" ) == 0) key = GLFW_KEY_2; |
| 261 | else if (strcmp(name, "AE03" ) == 0) key = GLFW_KEY_3; |
| 262 | else if (strcmp(name, "AE04" ) == 0) key = GLFW_KEY_4; |
| 263 | else if (strcmp(name, "AE05" ) == 0) key = GLFW_KEY_5; |
| 264 | else if (strcmp(name, "AE06" ) == 0) key = GLFW_KEY_6; |
| 265 | else if (strcmp(name, "AE07" ) == 0) key = GLFW_KEY_7; |
| 266 | else if (strcmp(name, "AE08" ) == 0) key = GLFW_KEY_8; |
| 267 | else if (strcmp(name, "AE09" ) == 0) key = GLFW_KEY_9; |
| 268 | else if (strcmp(name, "AE10" ) == 0) key = GLFW_KEY_0; |
| 269 | else if (strcmp(name, "AE11" ) == 0) key = GLFW_KEY_MINUS; |
| 270 | else if (strcmp(name, "AE12" ) == 0) key = GLFW_KEY_EQUAL; |
| 271 | else if (strcmp(name, "AD01" ) == 0) key = GLFW_KEY_Q; |
| 272 | else if (strcmp(name, "AD02" ) == 0) key = GLFW_KEY_W; |
| 273 | else if (strcmp(name, "AD03" ) == 0) key = GLFW_KEY_E; |
| 274 | else if (strcmp(name, "AD04" ) == 0) key = GLFW_KEY_R; |
| 275 | else if (strcmp(name, "AD05" ) == 0) key = GLFW_KEY_T; |
| 276 | else if (strcmp(name, "AD06" ) == 0) key = GLFW_KEY_Y; |
| 277 | else if (strcmp(name, "AD07" ) == 0) key = GLFW_KEY_U; |
| 278 | else if (strcmp(name, "AD08" ) == 0) key = GLFW_KEY_I; |
| 279 | else if (strcmp(name, "AD09" ) == 0) key = GLFW_KEY_O; |
| 280 | else if (strcmp(name, "AD10" ) == 0) key = GLFW_KEY_P; |
| 281 | else if (strcmp(name, "AD11" ) == 0) key = GLFW_KEY_LEFT_BRACKET; |
| 282 | else if (strcmp(name, "AD12" ) == 0) key = GLFW_KEY_RIGHT_BRACKET; |
| 283 | else if (strcmp(name, "AC01" ) == 0) key = GLFW_KEY_A; |
| 284 | else if (strcmp(name, "AC02" ) == 0) key = GLFW_KEY_S; |
| 285 | else if (strcmp(name, "AC03" ) == 0) key = GLFW_KEY_D; |
| 286 | else if (strcmp(name, "AC04" ) == 0) key = GLFW_KEY_F; |
| 287 | else if (strcmp(name, "AC05" ) == 0) key = GLFW_KEY_G; |
| 288 | else if (strcmp(name, "AC06" ) == 0) key = GLFW_KEY_H; |
| 289 | else if (strcmp(name, "AC07" ) == 0) key = GLFW_KEY_J; |
| 290 | else if (strcmp(name, "AC08" ) == 0) key = GLFW_KEY_K; |
| 291 | else if (strcmp(name, "AC09" ) == 0) key = GLFW_KEY_L; |
| 292 | else if (strcmp(name, "AC10" ) == 0) key = GLFW_KEY_SEMICOLON; |
| 293 | else if (strcmp(name, "AC11" ) == 0) key = GLFW_KEY_APOSTROPHE; |
| 294 | else if (strcmp(name, "AB01" ) == 0) key = GLFW_KEY_Z; |
| 295 | else if (strcmp(name, "AB02" ) == 0) key = GLFW_KEY_X; |
| 296 | else if (strcmp(name, "AB03" ) == 0) key = GLFW_KEY_C; |
| 297 | else if (strcmp(name, "AB04" ) == 0) key = GLFW_KEY_V; |
| 298 | else if (strcmp(name, "AB05" ) == 0) key = GLFW_KEY_B; |
| 299 | else if (strcmp(name, "AB06" ) == 0) key = GLFW_KEY_N; |
| 300 | else if (strcmp(name, "AB07" ) == 0) key = GLFW_KEY_M; |
| 301 | else if (strcmp(name, "AB08" ) == 0) key = GLFW_KEY_COMMA; |
| 302 | else if (strcmp(name, "AB09" ) == 0) key = GLFW_KEY_PERIOD; |
| 303 | else if (strcmp(name, "AB10" ) == 0) key = GLFW_KEY_SLASH; |
| 304 | else if (strcmp(name, "BKSL" ) == 0) key = GLFW_KEY_BACKSLASH; |
| 305 | else if (strcmp(name, "LSGT" ) == 0) key = GLFW_KEY_WORLD_1; |
| 306 | else key = GLFW_KEY_UNKNOWN; |
| 307 | |
| 308 | if ((scancode >= 0) && (scancode < 256)) |
| 309 | _glfw.x11.publicKeys[scancode] = key; |
| 310 | } |
| 311 | |
| 312 | XkbFreeNames(desc, XkbKeyNamesMask, True); |
| 313 | XkbFreeKeyboard(desc, 0, True); |
| 314 | } |
| 315 | |
| 316 | for (scancode = 0; scancode < 256; scancode++) |
| 317 | { |
| 318 | // Translate the un-translated key codes using traditional X11 KeySym |
| 319 | // lookups |
| 320 | if (_glfw.x11.publicKeys[scancode] < 0) |
| 321 | _glfw.x11.publicKeys[scancode] = translateKeyCode(scancode); |
| 322 | |
| 323 | // Store the reverse translation for faster key name lookup |
| 324 | if (_glfw.x11.publicKeys[scancode] > 0) |
| 325 | _glfw.x11.nativeKeys[_glfw.x11.publicKeys[scancode]] = scancode; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // Check whether the IM has a usable style |
| 330 | // |
| 331 | static GLFWbool hasUsableInputMethodStyle(void) |
| 332 | { |
| 333 | unsigned int i; |
| 334 | GLFWbool found = GLFW_FALSE; |
| 335 | XIMStyles* styles = NULL; |
| 336 | |
| 337 | if (XGetIMValues(_glfw.x11.im, XNQueryInputStyle, &styles, NULL) != NULL) |
| 338 | return GLFW_FALSE; |
| 339 | |
| 340 | for (i = 0; i < styles->count_styles; i++) |
| 341 | { |
| 342 | if (styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) |
| 343 | { |
| 344 | found = GLFW_TRUE; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | XFree(styles); |
| 350 | return found; |
| 351 | } |
| 352 | |
| 353 | // Check whether the specified atom is supported |
| 354 | // |
| 355 | static Atom getSupportedAtom(Atom* supportedAtoms, |
| 356 | unsigned long atomCount, |
| 357 | const char* atomName) |
| 358 | { |
| 359 | unsigned long i; |
| 360 | const Atom atom = XInternAtom(_glfw.x11.display, atomName, False); |
| 361 | |
| 362 | for (i = 0; i < atomCount; i++) |
| 363 | { |
| 364 | if (supportedAtoms[i] == atom) |
| 365 | return atom; |
| 366 | } |
| 367 | |
| 368 | return None; |
| 369 | } |
| 370 | |
| 371 | // Check whether the running window manager is EWMH-compliant |
| 372 | // |
| 373 | static void detectEWMH(void) |
| 374 | { |
| 375 | Window* windowFromRoot = NULL; |
| 376 | Window* windowFromChild = NULL; |
| 377 | |
| 378 | // First we need a couple of atoms |
| 379 | const Atom supportingWmCheck = |
| 380 | XInternAtom(_glfw.x11.display, "_NET_SUPPORTING_WM_CHECK" , False); |
| 381 | const Atom wmSupported = |
| 382 | XInternAtom(_glfw.x11.display, "_NET_SUPPORTED" , False); |
| 383 | |
| 384 | // Then we look for the _NET_SUPPORTING_WM_CHECK property of the root window |
| 385 | if (_glfwGetWindowPropertyX11(_glfw.x11.root, |
| 386 | supportingWmCheck, |
| 387 | XA_WINDOW, |
| 388 | (unsigned char**) &windowFromRoot) != 1) |
| 389 | { |
| 390 | if (windowFromRoot) |
| 391 | XFree(windowFromRoot); |
| 392 | return; |
| 393 | } |
| 394 | |
| 395 | _glfwGrabErrorHandlerX11(); |
| 396 | |
| 397 | // It should be the ID of a child window (of the root) |
| 398 | // Then we look for the same property on the child window |
| 399 | if (_glfwGetWindowPropertyX11(*windowFromRoot, |
| 400 | supportingWmCheck, |
| 401 | XA_WINDOW, |
| 402 | (unsigned char**) &windowFromChild) != 1) |
| 403 | { |
| 404 | XFree(windowFromRoot); |
| 405 | if (windowFromChild) |
| 406 | XFree(windowFromChild); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | _glfwReleaseErrorHandlerX11(); |
| 411 | |
| 412 | // It should be the ID of that same child window |
| 413 | if (*windowFromRoot != *windowFromChild) |
| 414 | { |
| 415 | XFree(windowFromRoot); |
| 416 | XFree(windowFromChild); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | XFree(windowFromRoot); |
| 421 | XFree(windowFromChild); |
| 422 | |
| 423 | // We are now fairly sure that an EWMH-compliant window manager is running |
| 424 | |
| 425 | Atom* supportedAtoms; |
| 426 | unsigned long atomCount; |
| 427 | |
| 428 | // Now we need to check the _NET_SUPPORTED property of the root window |
| 429 | // It should be a list of supported WM protocol and state atoms |
| 430 | atomCount = _glfwGetWindowPropertyX11(_glfw.x11.root, |
| 431 | wmSupported, |
| 432 | XA_ATOM, |
| 433 | (unsigned char**) &supportedAtoms); |
| 434 | |
| 435 | // See which of the atoms we support that are supported by the WM |
| 436 | _glfw.x11.NET_WM_STATE = |
| 437 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_STATE" ); |
| 438 | _glfw.x11.NET_WM_STATE_ABOVE = |
| 439 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_STATE_ABOVE" ); |
| 440 | _glfw.x11.NET_WM_STATE_FULLSCREEN = |
| 441 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_STATE_FULLSCREEN" ); |
| 442 | _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT = |
| 443 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_STATE_MAXIMIZED_VERT" ); |
| 444 | _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ = |
| 445 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_STATE_MAXIMIZED_HORZ" ); |
| 446 | _glfw.x11.NET_WM_FULLSCREEN_MONITORS = |
| 447 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_FULLSCREEN_MONITORS" ); |
| 448 | _glfw.x11.NET_WM_WINDOW_TYPE = |
| 449 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_WINDOW_TYPE" ); |
| 450 | _glfw.x11.NET_WM_WINDOW_TYPE_NORMAL = |
| 451 | getSupportedAtom(supportedAtoms, atomCount, "_NET_WM_WINDOW_TYPE_NORMAL" ); |
| 452 | _glfw.x11.NET_ACTIVE_WINDOW = |
| 453 | getSupportedAtom(supportedAtoms, atomCount, "_NET_ACTIVE_WINDOW" ); |
| 454 | _glfw.x11.NET_FRAME_EXTENTS = |
| 455 | getSupportedAtom(supportedAtoms, atomCount, "_NET_FRAME_EXTENTS" ); |
| 456 | _glfw.x11.NET_REQUEST_FRAME_EXTENTS = |
| 457 | getSupportedAtom(supportedAtoms, atomCount, "_NET_REQUEST_FRAME_EXTENTS" ); |
| 458 | |
| 459 | XFree(supportedAtoms); |
| 460 | } |
| 461 | |
| 462 | // Initialize X11 display and look for supported X11 extensions |
| 463 | // |
| 464 | static GLFWbool initExtensions(void) |
| 465 | { |
| 466 | #if defined(_GLFW_HAS_XF86VM) |
| 467 | // Check for XF86VidMode extension |
| 468 | _glfw.x11.vidmode.available = |
| 469 | XF86VidModeQueryExtension(_glfw.x11.display, |
| 470 | &_glfw.x11.vidmode.eventBase, |
| 471 | &_glfw.x11.vidmode.errorBase); |
| 472 | #endif /*_GLFW_HAS_XF86VM*/ |
| 473 | |
| 474 | // Check for RandR extension |
| 475 | if (XRRQueryExtension(_glfw.x11.display, |
| 476 | &_glfw.x11.randr.eventBase, |
| 477 | &_glfw.x11.randr.errorBase)) |
| 478 | { |
| 479 | if (XRRQueryVersion(_glfw.x11.display, |
| 480 | &_glfw.x11.randr.major, |
| 481 | &_glfw.x11.randr.minor)) |
| 482 | { |
| 483 | // The GLFW RandR path requires at least version 1.3 |
| 484 | if (_glfw.x11.randr.major > 1 || _glfw.x11.randr.minor >= 3) |
| 485 | _glfw.x11.randr.available = GLFW_TRUE; |
| 486 | } |
| 487 | else |
| 488 | { |
| 489 | _glfwInputError(GLFW_PLATFORM_ERROR, |
| 490 | "X11: Failed to query RandR version" ); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (_glfw.x11.randr.available) |
| 495 | { |
| 496 | XRRScreenResources* sr = XRRGetScreenResources(_glfw.x11.display, |
| 497 | _glfw.x11.root); |
| 498 | |
| 499 | if (!sr->ncrtc || !XRRGetCrtcGammaSize(_glfw.x11.display, sr->crtcs[0])) |
| 500 | { |
| 501 | // This is either a headless system or an older Nvidia binary driver |
| 502 | // with broken gamma support |
| 503 | // Flag it as useless and fall back to Xf86VidMode gamma, if |
| 504 | // available |
| 505 | _glfwInputError(GLFW_PLATFORM_ERROR, |
| 506 | "X11: RandR gamma ramp support seems broken" ); |
| 507 | _glfw.x11.randr.gammaBroken = GLFW_TRUE; |
| 508 | } |
| 509 | |
| 510 | XRRFreeScreenResources(sr); |
| 511 | |
| 512 | XRRSelectInput(_glfw.x11.display, _glfw.x11.root, |
| 513 | RROutputChangeNotifyMask); |
| 514 | } |
| 515 | |
| 516 | if (XineramaQueryExtension(_glfw.x11.display, |
| 517 | &_glfw.x11.xinerama.major, |
| 518 | &_glfw.x11.xinerama.minor)) |
| 519 | { |
| 520 | if (XineramaIsActive(_glfw.x11.display)) |
| 521 | _glfw.x11.xinerama.available = GLFW_TRUE; |
| 522 | } |
| 523 | |
| 524 | // Check if Xkb is supported on this display |
| 525 | _glfw.x11.xkb.major = 1; |
| 526 | _glfw.x11.xkb.minor = 0; |
| 527 | _glfw.x11.xkb.available = |
| 528 | XkbQueryExtension(_glfw.x11.display, |
| 529 | &_glfw.x11.xkb.majorOpcode, |
| 530 | &_glfw.x11.xkb.eventBase, |
| 531 | &_glfw.x11.xkb.errorBase, |
| 532 | &_glfw.x11.xkb.major, |
| 533 | &_glfw.x11.xkb.minor); |
| 534 | |
| 535 | if (_glfw.x11.xkb.available) |
| 536 | { |
| 537 | Bool supported; |
| 538 | |
| 539 | if (XkbSetDetectableAutoRepeat(_glfw.x11.display, True, &supported)) |
| 540 | { |
| 541 | if (supported) |
| 542 | _glfw.x11.xkb.detectable = GLFW_TRUE; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | _glfw.x11.x11xcb.handle = dlopen("libX11-xcb.so" , RTLD_LAZY | RTLD_GLOBAL); |
| 547 | if (_glfw.x11.x11xcb.handle) |
| 548 | { |
| 549 | _glfw.x11.x11xcb.XGetXCBConnection = (XGETXCBCONNECTION_T) |
| 550 | dlsym(_glfw.x11.x11xcb.handle, "XGetXCBConnection" ); |
| 551 | } |
| 552 | |
| 553 | // Update the key code LUT |
| 554 | // FIXME: We should listen to XkbMapNotify events to track changes to |
| 555 | // the keyboard mapping. |
| 556 | createKeyTables(); |
| 557 | |
| 558 | // Detect whether an EWMH-conformant window manager is running |
| 559 | detectEWMH(); |
| 560 | |
| 561 | // String format atoms |
| 562 | _glfw.x11.NULL_ = XInternAtom(_glfw.x11.display, "NULL" , False); |
| 563 | _glfw.x11.UTF8_STRING = |
| 564 | XInternAtom(_glfw.x11.display, "UTF8_STRING" , False); |
| 565 | _glfw.x11.COMPOUND_STRING = |
| 566 | XInternAtom(_glfw.x11.display, "COMPOUND_STRING" , False); |
| 567 | _glfw.x11.ATOM_PAIR = XInternAtom(_glfw.x11.display, "ATOM_PAIR" , False); |
| 568 | |
| 569 | // Custom selection property atom |
| 570 | _glfw.x11.GLFW_SELECTION = |
| 571 | XInternAtom(_glfw.x11.display, "GLFW_SELECTION" , False); |
| 572 | |
| 573 | // ICCCM standard clipboard atoms |
| 574 | _glfw.x11.TARGETS = XInternAtom(_glfw.x11.display, "TARGETS" , False); |
| 575 | _glfw.x11.MULTIPLE = XInternAtom(_glfw.x11.display, "MULTIPLE" , False); |
| 576 | _glfw.x11.CLIPBOARD = XInternAtom(_glfw.x11.display, "CLIPBOARD" , False); |
| 577 | |
| 578 | // Clipboard manager atoms |
| 579 | _glfw.x11.CLIPBOARD_MANAGER = |
| 580 | XInternAtom(_glfw.x11.display, "CLIPBOARD_MANAGER" , False); |
| 581 | _glfw.x11.SAVE_TARGETS = |
| 582 | XInternAtom(_glfw.x11.display, "SAVE_TARGETS" , False); |
| 583 | |
| 584 | // Xdnd (drag and drop) atoms |
| 585 | _glfw.x11.XdndAware = XInternAtom(_glfw.x11.display, "XdndAware" , False); |
| 586 | _glfw.x11.XdndEnter = XInternAtom(_glfw.x11.display, "XdndEnter" , False); |
| 587 | _glfw.x11.XdndPosition = XInternAtom(_glfw.x11.display, "XdndPosition" , False); |
| 588 | _glfw.x11.XdndStatus = XInternAtom(_glfw.x11.display, "XdndStatus" , False); |
| 589 | _glfw.x11.XdndActionCopy = XInternAtom(_glfw.x11.display, "XdndActionCopy" , False); |
| 590 | _glfw.x11.XdndDrop = XInternAtom(_glfw.x11.display, "XdndDrop" , False); |
| 591 | _glfw.x11.XdndLeave = XInternAtom(_glfw.x11.display, "XdndLeave" , False); |
| 592 | _glfw.x11.XdndFinished = XInternAtom(_glfw.x11.display, "XdndFinished" , False); |
| 593 | _glfw.x11.XdndSelection = XInternAtom(_glfw.x11.display, "XdndSelection" , False); |
| 594 | |
| 595 | // ICCCM, EWMH and Motif window property atoms |
| 596 | // These can be set safely even without WM support |
| 597 | // The EWMH atoms that require WM support are handled in detectEWMH |
| 598 | _glfw.x11.WM_PROTOCOLS = |
| 599 | XInternAtom(_glfw.x11.display, "WM_PROTOCOLS" , False); |
| 600 | _glfw.x11.WM_STATE = |
| 601 | XInternAtom(_glfw.x11.display, "WM_STATE" , False); |
| 602 | _glfw.x11.WM_DELETE_WINDOW = |
| 603 | XInternAtom(_glfw.x11.display, "WM_DELETE_WINDOW" , False); |
| 604 | _glfw.x11.NET_WM_ICON = |
| 605 | XInternAtom(_glfw.x11.display, "_NET_WM_ICON" , False); |
| 606 | _glfw.x11.NET_WM_PING = |
| 607 | XInternAtom(_glfw.x11.display, "_NET_WM_PING" , False); |
| 608 | _glfw.x11.NET_WM_PID = |
| 609 | XInternAtom(_glfw.x11.display, "_NET_WM_PID" , False); |
| 610 | _glfw.x11.NET_WM_NAME = |
| 611 | XInternAtom(_glfw.x11.display, "_NET_WM_NAME" , False); |
| 612 | _glfw.x11.NET_WM_ICON_NAME = |
| 613 | XInternAtom(_glfw.x11.display, "_NET_WM_ICON_NAME" , False); |
| 614 | _glfw.x11.NET_WM_BYPASS_COMPOSITOR = |
| 615 | XInternAtom(_glfw.x11.display, "_NET_WM_BYPASS_COMPOSITOR" , False); |
| 616 | _glfw.x11.MOTIF_WM_HINTS = |
| 617 | XInternAtom(_glfw.x11.display, "_MOTIF_WM_HINTS" , False); |
| 618 | |
| 619 | return GLFW_TRUE; |
| 620 | } |
| 621 | |
| 622 | // Create a blank cursor for hidden and disabled cursor modes |
| 623 | // |
| 624 | static Cursor createHiddenCursor(void) |
| 625 | { |
| 626 | unsigned char pixels[16 * 16 * 4]; |
| 627 | GLFWimage image = { 16, 16, pixels }; |
| 628 | |
| 629 | memset(pixels, 0, sizeof(pixels)); |
| 630 | |
| 631 | return _glfwCreateCursorX11(&image, 0, 0); |
| 632 | } |
| 633 | |
| 634 | // X error handler |
| 635 | // |
| 636 | static int errorHandler(Display *display, XErrorEvent* event) |
| 637 | { |
| 638 | _glfw.x11.errorCode = event->error_code; |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | ////////////////////////////////////////////////////////////////////////// |
| 644 | ////// GLFW internal API ////// |
| 645 | ////////////////////////////////////////////////////////////////////////// |
| 646 | |
| 647 | // Sets the X error handler callback |
| 648 | // |
| 649 | void _glfwGrabErrorHandlerX11(void) |
| 650 | { |
| 651 | _glfw.x11.errorCode = Success; |
| 652 | XSetErrorHandler(errorHandler); |
| 653 | } |
| 654 | |
| 655 | // Clears the X error handler callback |
| 656 | // |
| 657 | void _glfwReleaseErrorHandlerX11(void) |
| 658 | { |
| 659 | // Synchronize to make sure all commands are processed |
| 660 | XSync(_glfw.x11.display, False); |
| 661 | XSetErrorHandler(NULL); |
| 662 | } |
| 663 | |
| 664 | // Reports the specified error, appending information about the last X error |
| 665 | // |
| 666 | void _glfwInputErrorX11(int error, const char* message) |
| 667 | { |
| 668 | char buffer[8192]; |
| 669 | XGetErrorText(_glfw.x11.display, _glfw.x11.errorCode, |
| 670 | buffer, sizeof(buffer)); |
| 671 | |
| 672 | _glfwInputError(error, "%s: %s" , message, buffer); |
| 673 | } |
| 674 | |
| 675 | // Creates a native cursor object from the specified image and hotspot |
| 676 | // |
| 677 | Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot) |
| 678 | { |
| 679 | int i; |
| 680 | Cursor cursor; |
| 681 | |
| 682 | XcursorImage* native = XcursorImageCreate(image->width, image->height); |
| 683 | if (native == NULL) |
| 684 | return None; |
| 685 | |
| 686 | native->xhot = xhot; |
| 687 | native->yhot = yhot; |
| 688 | |
| 689 | unsigned char* source = (unsigned char*) image->pixels; |
| 690 | XcursorPixel* target = native->pixels; |
| 691 | |
| 692 | for (i = 0; i < image->width * image->height; i++, target++, source += 4) |
| 693 | { |
| 694 | unsigned int alpha = source[3]; |
| 695 | |
| 696 | *target = (alpha << 24) | |
| 697 | ((unsigned char) ((source[0] * alpha) / 255) << 16) | |
| 698 | ((unsigned char) ((source[1] * alpha) / 255) << 8) | |
| 699 | ((unsigned char) ((source[2] * alpha) / 255) << 0); |
| 700 | } |
| 701 | |
| 702 | cursor = XcursorImageLoadCursor(_glfw.x11.display, native); |
| 703 | XcursorImageDestroy(native); |
| 704 | |
| 705 | return cursor; |
| 706 | } |
| 707 | |
| 708 | |
| 709 | ////////////////////////////////////////////////////////////////////////// |
| 710 | ////// GLFW platform API ////// |
| 711 | ////////////////////////////////////////////////////////////////////////// |
| 712 | |
| 713 | int _glfwPlatformInit(void) |
| 714 | { |
| 715 | #if !defined(X_HAVE_UTF8_STRING) |
| 716 | // HACK: If the current locale is C, apply the environment's locale |
| 717 | // This is done because the C locale breaks wide character input |
| 718 | if (strcmp(setlocale(LC_CTYPE, NULL), "C" ) == 0) |
| 719 | setlocale(LC_CTYPE, "" ); |
| 720 | #endif |
| 721 | |
| 722 | XInitThreads(); |
| 723 | |
| 724 | _glfw.x11.display = XOpenDisplay(NULL); |
| 725 | if (!_glfw.x11.display) |
| 726 | { |
| 727 | const char* display = getenv("DISPLAY" ); |
| 728 | if (display) |
| 729 | { |
| 730 | _glfwInputError(GLFW_PLATFORM_ERROR, |
| 731 | "X11: Failed to open display %s" , display); |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | _glfwInputError(GLFW_PLATFORM_ERROR, |
| 736 | "X11: The DISPLAY environment variable is missing" ); |
| 737 | } |
| 738 | |
| 739 | return GLFW_FALSE; |
| 740 | } |
| 741 | |
| 742 | _glfw.x11.screen = DefaultScreen(_glfw.x11.display); |
| 743 | _glfw.x11.root = RootWindow(_glfw.x11.display, _glfw.x11.screen); |
| 744 | _glfw.x11.context = XUniqueContext(); |
| 745 | |
| 746 | if (!initExtensions()) |
| 747 | return GLFW_FALSE; |
| 748 | |
| 749 | _glfw.x11.cursor = createHiddenCursor(); |
| 750 | |
| 751 | if (XSupportsLocale()) |
| 752 | { |
| 753 | XSetLocaleModifiers("" ); |
| 754 | |
| 755 | _glfw.x11.im = XOpenIM(_glfw.x11.display, 0, NULL, NULL); |
| 756 | if (_glfw.x11.im) |
| 757 | { |
| 758 | if (!hasUsableInputMethodStyle()) |
| 759 | { |
| 760 | XCloseIM(_glfw.x11.im); |
| 761 | _glfw.x11.im = NULL; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | if (!_glfwInitThreadLocalStoragePOSIX()) |
| 767 | return GLFW_FALSE; |
| 768 | |
| 769 | if (!_glfwInitGLX()) |
| 770 | return GLFW_FALSE; |
| 771 | |
| 772 | if (!_glfwInitJoysticksLinux()) |
| 773 | return GLFW_FALSE; |
| 774 | |
| 775 | _glfwInitEGL(); |
| 776 | _glfwInitTimerPOSIX(); |
| 777 | |
| 778 | return GLFW_TRUE; |
| 779 | } |
| 780 | |
| 781 | void _glfwPlatformTerminate(void) |
| 782 | { |
| 783 | if (_glfw.x11.x11xcb.handle) |
| 784 | { |
| 785 | dlclose(_glfw.x11.x11xcb.handle); |
| 786 | _glfw.x11.x11xcb.handle = NULL; |
| 787 | } |
| 788 | |
| 789 | if (_glfw.x11.cursor) |
| 790 | { |
| 791 | XFreeCursor(_glfw.x11.display, _glfw.x11.cursor); |
| 792 | _glfw.x11.cursor = (Cursor) 0; |
| 793 | } |
| 794 | |
| 795 | free(_glfw.x11.clipboardString); |
| 796 | |
| 797 | if (_glfw.x11.im) |
| 798 | { |
| 799 | XCloseIM(_glfw.x11.im); |
| 800 | _glfw.x11.im = NULL; |
| 801 | } |
| 802 | |
| 803 | if (_glfw.x11.display) |
| 804 | { |
| 805 | XCloseDisplay(_glfw.x11.display); |
| 806 | _glfw.x11.display = NULL; |
| 807 | } |
| 808 | |
| 809 | // NOTE: This needs to be done after XCloseDisplay, as libGL registers |
| 810 | // cleanup callbacks that get called by it |
| 811 | _glfwTerminateEGL(); |
| 812 | _glfwTerminateGLX(); |
| 813 | |
| 814 | _glfwTerminateJoysticksLinux(); |
| 815 | _glfwTerminateThreadLocalStoragePOSIX(); |
| 816 | } |
| 817 | |
| 818 | const char* _glfwPlatformGetVersionString(void) |
| 819 | { |
| 820 | return _GLFW_VERSION_NUMBER " X11 GLX EGL" |
| 821 | #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) |
| 822 | " clock_gettime" |
| 823 | #else |
| 824 | " gettimeofday" |
| 825 | #endif |
| 826 | #if defined(__linux__) |
| 827 | " /dev/js" |
| 828 | #endif |
| 829 | #if defined(_GLFW_HAS_XF86VM) |
| 830 | " Xf86vm" |
| 831 | #endif |
| 832 | #if defined(_GLFW_BUILD_DLL) |
| 833 | " shared" |
| 834 | #endif |
| 835 | ; |
| 836 | } |
| 837 | |
| 838 | |