x11_init.c (47852B)
1 //======================================================================== 2 // GLFW 3.3 X11 - 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 // It is fine to use C99 in this file because it will not be built with VS 28 //======================================================================== 29 30 #include "internal.h" 31 32 #include <X11/Xresource.h> 33 34 #include <stdlib.h> 35 #include <string.h> 36 #include <limits.h> 37 #include <stdio.h> 38 #include <locale.h> 39 #include <unistd.h> 40 #include <fcntl.h> 41 #include <errno.h> 42 #include <assert.h> 43 44 45 // Translate the X11 KeySyms for a key to a GLFW key code 46 // NOTE: This is only used as a fallback, in case the XKB method fails 47 // It is layout-dependent and will fail partially on most non-US layouts 48 // 49 static int translateKeySyms(const KeySym* keysyms, int width) 50 { 51 if (width > 1) 52 { 53 switch (keysyms[1]) 54 { 55 case XK_KP_0: return GLFW_KEY_KP_0; 56 case XK_KP_1: return GLFW_KEY_KP_1; 57 case XK_KP_2: return GLFW_KEY_KP_2; 58 case XK_KP_3: return GLFW_KEY_KP_3; 59 case XK_KP_4: return GLFW_KEY_KP_4; 60 case XK_KP_5: return GLFW_KEY_KP_5; 61 case XK_KP_6: return GLFW_KEY_KP_6; 62 case XK_KP_7: return GLFW_KEY_KP_7; 63 case XK_KP_8: return GLFW_KEY_KP_8; 64 case XK_KP_9: return GLFW_KEY_KP_9; 65 case XK_KP_Separator: 66 case XK_KP_Decimal: return GLFW_KEY_KP_DECIMAL; 67 case XK_KP_Equal: return GLFW_KEY_KP_EQUAL; 68 case XK_KP_Enter: return GLFW_KEY_KP_ENTER; 69 default: break; 70 } 71 } 72 73 switch (keysyms[0]) 74 { 75 case XK_Escape: return GLFW_KEY_ESCAPE; 76 case XK_Tab: return GLFW_KEY_TAB; 77 case XK_Shift_L: return GLFW_KEY_LEFT_SHIFT; 78 case XK_Shift_R: return GLFW_KEY_RIGHT_SHIFT; 79 case XK_Control_L: return GLFW_KEY_LEFT_CONTROL; 80 case XK_Control_R: return GLFW_KEY_RIGHT_CONTROL; 81 case XK_Meta_L: 82 case XK_Alt_L: return GLFW_KEY_LEFT_ALT; 83 case XK_Mode_switch: // Mapped to Alt_R on many keyboards 84 case XK_ISO_Level3_Shift: // AltGr on at least some machines 85 case XK_Meta_R: 86 case XK_Alt_R: return GLFW_KEY_RIGHT_ALT; 87 case XK_Super_L: return GLFW_KEY_LEFT_SUPER; 88 case XK_Super_R: return GLFW_KEY_RIGHT_SUPER; 89 case XK_Menu: return GLFW_KEY_MENU; 90 case XK_Num_Lock: return GLFW_KEY_NUM_LOCK; 91 case XK_Caps_Lock: return GLFW_KEY_CAPS_LOCK; 92 case XK_Print: return GLFW_KEY_PRINT_SCREEN; 93 case XK_Scroll_Lock: return GLFW_KEY_SCROLL_LOCK; 94 case XK_Pause: return GLFW_KEY_PAUSE; 95 case XK_Delete: return GLFW_KEY_DELETE; 96 case XK_BackSpace: return GLFW_KEY_BACKSPACE; 97 case XK_Return: return GLFW_KEY_ENTER; 98 case XK_Home: return GLFW_KEY_HOME; 99 case XK_End: return GLFW_KEY_END; 100 case XK_Page_Up: return GLFW_KEY_PAGE_UP; 101 case XK_Page_Down: return GLFW_KEY_PAGE_DOWN; 102 case XK_Insert: return GLFW_KEY_INSERT; 103 case XK_Left: return GLFW_KEY_LEFT; 104 case XK_Right: return GLFW_KEY_RIGHT; 105 case XK_Down: return GLFW_KEY_DOWN; 106 case XK_Up: return GLFW_KEY_UP; 107 case XK_F1: return GLFW_KEY_F1; 108 case XK_F2: return GLFW_KEY_F2; 109 case XK_F3: return GLFW_KEY_F3; 110 case XK_F4: return GLFW_KEY_F4; 111 case XK_F5: return GLFW_KEY_F5; 112 case XK_F6: return GLFW_KEY_F6; 113 case XK_F7: return GLFW_KEY_F7; 114 case XK_F8: return GLFW_KEY_F8; 115 case XK_F9: return GLFW_KEY_F9; 116 case XK_F10: return GLFW_KEY_F10; 117 case XK_F11: return GLFW_KEY_F11; 118 case XK_F12: return GLFW_KEY_F12; 119 case XK_F13: return GLFW_KEY_F13; 120 case XK_F14: return GLFW_KEY_F14; 121 case XK_F15: return GLFW_KEY_F15; 122 case XK_F16: return GLFW_KEY_F16; 123 case XK_F17: return GLFW_KEY_F17; 124 case XK_F18: return GLFW_KEY_F18; 125 case XK_F19: return GLFW_KEY_F19; 126 case XK_F20: return GLFW_KEY_F20; 127 case XK_F21: return GLFW_KEY_F21; 128 case XK_F22: return GLFW_KEY_F22; 129 case XK_F23: return GLFW_KEY_F23; 130 case XK_F24: return GLFW_KEY_F24; 131 case XK_F25: return GLFW_KEY_F25; 132 133 // Numeric keypad 134 case XK_KP_Divide: return GLFW_KEY_KP_DIVIDE; 135 case XK_KP_Multiply: return GLFW_KEY_KP_MULTIPLY; 136 case XK_KP_Subtract: return GLFW_KEY_KP_SUBTRACT; 137 case XK_KP_Add: return GLFW_KEY_KP_ADD; 138 139 // These should have been detected in secondary keysym test above! 140 case XK_KP_Insert: return GLFW_KEY_KP_0; 141 case XK_KP_End: return GLFW_KEY_KP_1; 142 case XK_KP_Down: return GLFW_KEY_KP_2; 143 case XK_KP_Page_Down: return GLFW_KEY_KP_3; 144 case XK_KP_Left: return GLFW_KEY_KP_4; 145 case XK_KP_Right: return GLFW_KEY_KP_6; 146 case XK_KP_Home: return GLFW_KEY_KP_7; 147 case XK_KP_Up: return GLFW_KEY_KP_8; 148 case XK_KP_Page_Up: return GLFW_KEY_KP_9; 149 case XK_KP_Delete: return GLFW_KEY_KP_DECIMAL; 150 case XK_KP_Equal: return GLFW_KEY_KP_EQUAL; 151 case XK_KP_Enter: return GLFW_KEY_KP_ENTER; 152 153 // Last resort: Check for printable keys (should not happen if the XKB 154 // extension is available). This will give a layout dependent mapping 155 // (which is wrong, and we may miss some keys, especially on non-US 156 // keyboards), but it's better than nothing... 157 case XK_a: return GLFW_KEY_A; 158 case XK_b: return GLFW_KEY_B; 159 case XK_c: return GLFW_KEY_C; 160 case XK_d: return GLFW_KEY_D; 161 case XK_e: return GLFW_KEY_E; 162 case XK_f: return GLFW_KEY_F; 163 case XK_g: return GLFW_KEY_G; 164 case XK_h: return GLFW_KEY_H; 165 case XK_i: return GLFW_KEY_I; 166 case XK_j: return GLFW_KEY_J; 167 case XK_k: return GLFW_KEY_K; 168 case XK_l: return GLFW_KEY_L; 169 case XK_m: return GLFW_KEY_M; 170 case XK_n: return GLFW_KEY_N; 171 case XK_o: return GLFW_KEY_O; 172 case XK_p: return GLFW_KEY_P; 173 case XK_q: return GLFW_KEY_Q; 174 case XK_r: return GLFW_KEY_R; 175 case XK_s: return GLFW_KEY_S; 176 case XK_t: return GLFW_KEY_T; 177 case XK_u: return GLFW_KEY_U; 178 case XK_v: return GLFW_KEY_V; 179 case XK_w: return GLFW_KEY_W; 180 case XK_x: return GLFW_KEY_X; 181 case XK_y: return GLFW_KEY_Y; 182 case XK_z: return GLFW_KEY_Z; 183 case XK_1: return GLFW_KEY_1; 184 case XK_2: return GLFW_KEY_2; 185 case XK_3: return GLFW_KEY_3; 186 case XK_4: return GLFW_KEY_4; 187 case XK_5: return GLFW_KEY_5; 188 case XK_6: return GLFW_KEY_6; 189 case XK_7: return GLFW_KEY_7; 190 case XK_8: return GLFW_KEY_8; 191 case XK_9: return GLFW_KEY_9; 192 case XK_0: return GLFW_KEY_0; 193 case XK_space: return GLFW_KEY_SPACE; 194 case XK_minus: return GLFW_KEY_MINUS; 195 case XK_equal: return GLFW_KEY_EQUAL; 196 case XK_bracketleft: return GLFW_KEY_LEFT_BRACKET; 197 case XK_bracketright: return GLFW_KEY_RIGHT_BRACKET; 198 case XK_backslash: return GLFW_KEY_BACKSLASH; 199 case XK_semicolon: return GLFW_KEY_SEMICOLON; 200 case XK_apostrophe: return GLFW_KEY_APOSTROPHE; 201 case XK_grave: return GLFW_KEY_GRAVE_ACCENT; 202 case XK_comma: return GLFW_KEY_COMMA; 203 case XK_period: return GLFW_KEY_PERIOD; 204 case XK_slash: return GLFW_KEY_SLASH; 205 case XK_less: return GLFW_KEY_WORLD_1; // At least in some layouts... 206 default: break; 207 } 208 209 // No matching translation was found 210 return GLFW_KEY_UNKNOWN; 211 } 212 213 // Create key code translation tables 214 // 215 static void createKeyTables(void) 216 { 217 int scancode, scancodeMin, scancodeMax; 218 219 memset(_glfw.x11.keycodes, -1, sizeof(_glfw.x11.keycodes)); 220 memset(_glfw.x11.scancodes, -1, sizeof(_glfw.x11.scancodes)); 221 222 if (_glfw.x11.xkb.available) 223 { 224 // Use XKB to determine physical key locations independently of the 225 // current keyboard layout 226 227 XkbDescPtr desc = XkbGetMap(_glfw.x11.display, 0, XkbUseCoreKbd); 228 XkbGetNames(_glfw.x11.display, XkbKeyNamesMask | XkbKeyAliasesMask, desc); 229 230 scancodeMin = desc->min_key_code; 231 scancodeMax = desc->max_key_code; 232 233 const struct 234 { 235 int key; 236 char* name; 237 } keymap[] = 238 { 239 { GLFW_KEY_GRAVE_ACCENT, "TLDE" }, 240 { GLFW_KEY_1, "AE01" }, 241 { GLFW_KEY_2, "AE02" }, 242 { GLFW_KEY_3, "AE03" }, 243 { GLFW_KEY_4, "AE04" }, 244 { GLFW_KEY_5, "AE05" }, 245 { GLFW_KEY_6, "AE06" }, 246 { GLFW_KEY_7, "AE07" }, 247 { GLFW_KEY_8, "AE08" }, 248 { GLFW_KEY_9, "AE09" }, 249 { GLFW_KEY_0, "AE10" }, 250 { GLFW_KEY_MINUS, "AE11" }, 251 { GLFW_KEY_EQUAL, "AE12" }, 252 { GLFW_KEY_Q, "AD01" }, 253 { GLFW_KEY_W, "AD02" }, 254 { GLFW_KEY_E, "AD03" }, 255 { GLFW_KEY_R, "AD04" }, 256 { GLFW_KEY_T, "AD05" }, 257 { GLFW_KEY_Y, "AD06" }, 258 { GLFW_KEY_U, "AD07" }, 259 { GLFW_KEY_I, "AD08" }, 260 { GLFW_KEY_O, "AD09" }, 261 { GLFW_KEY_P, "AD10" }, 262 { GLFW_KEY_LEFT_BRACKET, "AD11" }, 263 { GLFW_KEY_RIGHT_BRACKET, "AD12" }, 264 { GLFW_KEY_A, "AC01" }, 265 { GLFW_KEY_S, "AC02" }, 266 { GLFW_KEY_D, "AC03" }, 267 { GLFW_KEY_F, "AC04" }, 268 { GLFW_KEY_G, "AC05" }, 269 { GLFW_KEY_H, "AC06" }, 270 { GLFW_KEY_J, "AC07" }, 271 { GLFW_KEY_K, "AC08" }, 272 { GLFW_KEY_L, "AC09" }, 273 { GLFW_KEY_SEMICOLON, "AC10" }, 274 { GLFW_KEY_APOSTROPHE, "AC11" }, 275 { GLFW_KEY_Z, "AB01" }, 276 { GLFW_KEY_X, "AB02" }, 277 { GLFW_KEY_C, "AB03" }, 278 { GLFW_KEY_V, "AB04" }, 279 { GLFW_KEY_B, "AB05" }, 280 { GLFW_KEY_N, "AB06" }, 281 { GLFW_KEY_M, "AB07" }, 282 { GLFW_KEY_COMMA, "AB08" }, 283 { GLFW_KEY_PERIOD, "AB09" }, 284 { GLFW_KEY_SLASH, "AB10" }, 285 { GLFW_KEY_BACKSLASH, "BKSL" }, 286 { GLFW_KEY_WORLD_1, "LSGT" }, 287 { GLFW_KEY_SPACE, "SPCE" }, 288 { GLFW_KEY_ESCAPE, "ESC" }, 289 { GLFW_KEY_ENTER, "RTRN" }, 290 { GLFW_KEY_TAB, "TAB" }, 291 { GLFW_KEY_BACKSPACE, "BKSP" }, 292 { GLFW_KEY_INSERT, "INS" }, 293 { GLFW_KEY_DELETE, "DELE" }, 294 { GLFW_KEY_RIGHT, "RGHT" }, 295 { GLFW_KEY_LEFT, "LEFT" }, 296 { GLFW_KEY_DOWN, "DOWN" }, 297 { GLFW_KEY_UP, "UP" }, 298 { GLFW_KEY_PAGE_UP, "PGUP" }, 299 { GLFW_KEY_PAGE_DOWN, "PGDN" }, 300 { GLFW_KEY_HOME, "HOME" }, 301 { GLFW_KEY_END, "END" }, 302 { GLFW_KEY_CAPS_LOCK, "CAPS" }, 303 { GLFW_KEY_SCROLL_LOCK, "SCLK" }, 304 { GLFW_KEY_NUM_LOCK, "NMLK" }, 305 { GLFW_KEY_PRINT_SCREEN, "PRSC" }, 306 { GLFW_KEY_PAUSE, "PAUS" }, 307 { GLFW_KEY_F1, "FK01" }, 308 { GLFW_KEY_F2, "FK02" }, 309 { GLFW_KEY_F3, "FK03" }, 310 { GLFW_KEY_F4, "FK04" }, 311 { GLFW_KEY_F5, "FK05" }, 312 { GLFW_KEY_F6, "FK06" }, 313 { GLFW_KEY_F7, "FK07" }, 314 { GLFW_KEY_F8, "FK08" }, 315 { GLFW_KEY_F9, "FK09" }, 316 { GLFW_KEY_F10, "FK10" }, 317 { GLFW_KEY_F11, "FK11" }, 318 { GLFW_KEY_F12, "FK12" }, 319 { GLFW_KEY_F13, "FK13" }, 320 { GLFW_KEY_F14, "FK14" }, 321 { GLFW_KEY_F15, "FK15" }, 322 { GLFW_KEY_F16, "FK16" }, 323 { GLFW_KEY_F17, "FK17" }, 324 { GLFW_KEY_F18, "FK18" }, 325 { GLFW_KEY_F19, "FK19" }, 326 { GLFW_KEY_F20, "FK20" }, 327 { GLFW_KEY_F21, "FK21" }, 328 { GLFW_KEY_F22, "FK22" }, 329 { GLFW_KEY_F23, "FK23" }, 330 { GLFW_KEY_F24, "FK24" }, 331 { GLFW_KEY_F25, "FK25" }, 332 { GLFW_KEY_KP_0, "KP0" }, 333 { GLFW_KEY_KP_1, "KP1" }, 334 { GLFW_KEY_KP_2, "KP2" }, 335 { GLFW_KEY_KP_3, "KP3" }, 336 { GLFW_KEY_KP_4, "KP4" }, 337 { GLFW_KEY_KP_5, "KP5" }, 338 { GLFW_KEY_KP_6, "KP6" }, 339 { GLFW_KEY_KP_7, "KP7" }, 340 { GLFW_KEY_KP_8, "KP8" }, 341 { GLFW_KEY_KP_9, "KP9" }, 342 { GLFW_KEY_KP_DECIMAL, "KPDL" }, 343 { GLFW_KEY_KP_DIVIDE, "KPDV" }, 344 { GLFW_KEY_KP_MULTIPLY, "KPMU" }, 345 { GLFW_KEY_KP_SUBTRACT, "KPSU" }, 346 { GLFW_KEY_KP_ADD, "KPAD" }, 347 { GLFW_KEY_KP_ENTER, "KPEN" }, 348 { GLFW_KEY_KP_EQUAL, "KPEQ" }, 349 { GLFW_KEY_LEFT_SHIFT, "LFSH" }, 350 { GLFW_KEY_LEFT_CONTROL, "LCTL" }, 351 { GLFW_KEY_LEFT_ALT, "LALT" }, 352 { GLFW_KEY_LEFT_SUPER, "LWIN" }, 353 { GLFW_KEY_RIGHT_SHIFT, "RTSH" }, 354 { GLFW_KEY_RIGHT_CONTROL, "RCTL" }, 355 { GLFW_KEY_RIGHT_ALT, "RALT" }, 356 { GLFW_KEY_RIGHT_ALT, "LVL3" }, 357 { GLFW_KEY_RIGHT_ALT, "MDSW" }, 358 { GLFW_KEY_RIGHT_SUPER, "RWIN" }, 359 { GLFW_KEY_MENU, "MENU" } 360 }; 361 362 // Find the X11 key code -> GLFW key code mapping 363 for (scancode = scancodeMin; scancode <= scancodeMax; scancode++) 364 { 365 int key = GLFW_KEY_UNKNOWN; 366 367 // Map the key name to a GLFW key code. Note: We use the US 368 // keyboard layout. Because function keys aren't mapped correctly 369 // when using traditional KeySym translations, they are mapped 370 // here instead. 371 for (int i = 0; i < sizeof(keymap) / sizeof(keymap[0]); i++) 372 { 373 if (strncmp(desc->names->keys[scancode].name, 374 keymap[i].name, 375 XkbKeyNameLength) == 0) 376 { 377 key = keymap[i].key; 378 break; 379 } 380 } 381 382 // Fall back to key aliases in case the key name did not match 383 for (int i = 0; i < desc->names->num_key_aliases; i++) 384 { 385 if (key != GLFW_KEY_UNKNOWN) 386 break; 387 388 if (strncmp(desc->names->key_aliases[i].real, 389 desc->names->keys[scancode].name, 390 XkbKeyNameLength) != 0) 391 { 392 continue; 393 } 394 395 for (int j = 0; j < sizeof(keymap) / sizeof(keymap[0]); j++) 396 { 397 if (strncmp(desc->names->key_aliases[i].alias, 398 keymap[j].name, 399 XkbKeyNameLength) == 0) 400 { 401 key = keymap[j].key; 402 break; 403 } 404 } 405 } 406 407 _glfw.x11.keycodes[scancode] = key; 408 } 409 410 XkbFreeNames(desc, XkbKeyNamesMask, True); 411 XkbFreeKeyboard(desc, 0, True); 412 } 413 else 414 XDisplayKeycodes(_glfw.x11.display, &scancodeMin, &scancodeMax); 415 416 int width; 417 KeySym* keysyms = XGetKeyboardMapping(_glfw.x11.display, 418 scancodeMin, 419 scancodeMax - scancodeMin + 1, 420 &width); 421 422 for (scancode = scancodeMin; scancode <= scancodeMax; scancode++) 423 { 424 // Translate the un-translated key codes using traditional X11 KeySym 425 // lookups 426 if (_glfw.x11.keycodes[scancode] < 0) 427 { 428 const size_t base = (scancode - scancodeMin) * width; 429 _glfw.x11.keycodes[scancode] = translateKeySyms(&keysyms[base], width); 430 } 431 432 // Store the reverse translation for faster key name lookup 433 if (_glfw.x11.keycodes[scancode] > 0) 434 _glfw.x11.scancodes[_glfw.x11.keycodes[scancode]] = scancode; 435 } 436 437 XFree(keysyms); 438 } 439 440 // Check whether the IM has a usable style 441 // 442 static GLFWbool hasUsableInputMethodStyle(void) 443 { 444 GLFWbool found = GLFW_FALSE; 445 XIMStyles* styles = NULL; 446 447 if (XGetIMValues(_glfw.x11.im, XNQueryInputStyle, &styles, NULL) != NULL) 448 return GLFW_FALSE; 449 450 for (unsigned int i = 0; i < styles->count_styles; i++) 451 { 452 if (styles->supported_styles[i] == (XIMPreeditNothing | XIMStatusNothing)) 453 { 454 found = GLFW_TRUE; 455 break; 456 } 457 } 458 459 XFree(styles); 460 return found; 461 } 462 463 // Check whether the specified atom is supported 464 // 465 static Atom getAtomIfSupported(Atom* supportedAtoms, 466 unsigned long atomCount, 467 const char* atomName) 468 { 469 const Atom atom = XInternAtom(_glfw.x11.display, atomName, False); 470 471 for (unsigned long i = 0; i < atomCount; i++) 472 { 473 if (supportedAtoms[i] == atom) 474 return atom; 475 } 476 477 return None; 478 } 479 480 // Check whether the running window manager is EWMH-compliant 481 // 482 static void detectEWMH(void) 483 { 484 // First we read the _NET_SUPPORTING_WM_CHECK property on the root window 485 486 Window* windowFromRoot = NULL; 487 if (!_glfwGetWindowPropertyX11(_glfw.x11.root, 488 _glfw.x11.NET_SUPPORTING_WM_CHECK, 489 XA_WINDOW, 490 (unsigned char**) &windowFromRoot)) 491 { 492 return; 493 } 494 495 _glfwGrabErrorHandlerX11(); 496 497 // If it exists, it should be the XID of a top-level window 498 // Then we look for the same property on that window 499 500 Window* windowFromChild = NULL; 501 if (!_glfwGetWindowPropertyX11(*windowFromRoot, 502 _glfw.x11.NET_SUPPORTING_WM_CHECK, 503 XA_WINDOW, 504 (unsigned char**) &windowFromChild)) 505 { 506 XFree(windowFromRoot); 507 return; 508 } 509 510 _glfwReleaseErrorHandlerX11(); 511 512 // If the property exists, it should contain the XID of the window 513 514 if (*windowFromRoot != *windowFromChild) 515 { 516 XFree(windowFromRoot); 517 XFree(windowFromChild); 518 return; 519 } 520 521 XFree(windowFromRoot); 522 XFree(windowFromChild); 523 524 // We are now fairly sure that an EWMH-compliant WM is currently running 525 // We can now start querying the WM about what features it supports by 526 // looking in the _NET_SUPPORTED property on the root window 527 // It should contain a list of supported EWMH protocol and state atoms 528 529 Atom* supportedAtoms = NULL; 530 const unsigned long atomCount = 531 _glfwGetWindowPropertyX11(_glfw.x11.root, 532 _glfw.x11.NET_SUPPORTED, 533 XA_ATOM, 534 (unsigned char**) &supportedAtoms); 535 536 // See which of the atoms we support that are supported by the WM 537 538 _glfw.x11.NET_WM_STATE = 539 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE"); 540 _glfw.x11.NET_WM_STATE_ABOVE = 541 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE_ABOVE"); 542 _glfw.x11.NET_WM_STATE_FULLSCREEN = 543 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE_FULLSCREEN"); 544 _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT = 545 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE_MAXIMIZED_VERT"); 546 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ = 547 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE_MAXIMIZED_HORZ"); 548 _glfw.x11.NET_WM_STATE_DEMANDS_ATTENTION = 549 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_STATE_DEMANDS_ATTENTION"); 550 _glfw.x11.NET_WM_FULLSCREEN_MONITORS = 551 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_FULLSCREEN_MONITORS"); 552 _glfw.x11.NET_WM_WINDOW_TYPE = 553 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_WINDOW_TYPE"); 554 _glfw.x11.NET_WM_WINDOW_TYPE_NORMAL = 555 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WM_WINDOW_TYPE_NORMAL"); 556 _glfw.x11.NET_WORKAREA = 557 getAtomIfSupported(supportedAtoms, atomCount, "_NET_WORKAREA"); 558 _glfw.x11.NET_CURRENT_DESKTOP = 559 getAtomIfSupported(supportedAtoms, atomCount, "_NET_CURRENT_DESKTOP"); 560 _glfw.x11.NET_ACTIVE_WINDOW = 561 getAtomIfSupported(supportedAtoms, atomCount, "_NET_ACTIVE_WINDOW"); 562 _glfw.x11.NET_FRAME_EXTENTS = 563 getAtomIfSupported(supportedAtoms, atomCount, "_NET_FRAME_EXTENTS"); 564 _glfw.x11.NET_REQUEST_FRAME_EXTENTS = 565 getAtomIfSupported(supportedAtoms, atomCount, "_NET_REQUEST_FRAME_EXTENTS"); 566 567 if (supportedAtoms) 568 XFree(supportedAtoms); 569 } 570 571 // Look for and initialize supported X11 extensions 572 // 573 static GLFWbool initExtensions(void) 574 { 575 #if defined(__OpenBSD__) || defined(__NetBSD__) 576 _glfw.x11.vidmode.handle = _glfw_dlopen("libXxf86vm.so"); 577 #else 578 _glfw.x11.vidmode.handle = _glfw_dlopen("libXxf86vm.so.1"); 579 #endif 580 if (_glfw.x11.vidmode.handle) 581 { 582 _glfw.x11.vidmode.QueryExtension = (PFN_XF86VidModeQueryExtension) 583 _glfw_dlsym(_glfw.x11.vidmode.handle, "XF86VidModeQueryExtension"); 584 _glfw.x11.vidmode.GetGammaRamp = (PFN_XF86VidModeGetGammaRamp) 585 _glfw_dlsym(_glfw.x11.vidmode.handle, "XF86VidModeGetGammaRamp"); 586 _glfw.x11.vidmode.SetGammaRamp = (PFN_XF86VidModeSetGammaRamp) 587 _glfw_dlsym(_glfw.x11.vidmode.handle, "XF86VidModeSetGammaRamp"); 588 _glfw.x11.vidmode.GetGammaRampSize = (PFN_XF86VidModeGetGammaRampSize) 589 _glfw_dlsym(_glfw.x11.vidmode.handle, "XF86VidModeGetGammaRampSize"); 590 591 _glfw.x11.vidmode.available = 592 XF86VidModeQueryExtension(_glfw.x11.display, 593 &_glfw.x11.vidmode.eventBase, 594 &_glfw.x11.vidmode.errorBase); 595 } 596 597 #if defined(__CYGWIN__) 598 _glfw.x11.xi.handle = _glfw_dlopen("libXi-6.so"); 599 #elif defined(__OpenBSD__) || defined(__NetBSD__) 600 _glfw.x11.xi.handle = _glfw_dlopen("libXi.so"); 601 #else 602 _glfw.x11.xi.handle = _glfw_dlopen("libXi.so.6"); 603 #endif 604 if (_glfw.x11.xi.handle) 605 { 606 _glfw.x11.xi.QueryVersion = (PFN_XIQueryVersion) 607 _glfw_dlsym(_glfw.x11.xi.handle, "XIQueryVersion"); 608 _glfw.x11.xi.SelectEvents = (PFN_XISelectEvents) 609 _glfw_dlsym(_glfw.x11.xi.handle, "XISelectEvents"); 610 611 if (XQueryExtension(_glfw.x11.display, 612 "XInputExtension", 613 &_glfw.x11.xi.majorOpcode, 614 &_glfw.x11.xi.eventBase, 615 &_glfw.x11.xi.errorBase)) 616 { 617 _glfw.x11.xi.major = 2; 618 _glfw.x11.xi.minor = 0; 619 620 if (XIQueryVersion(_glfw.x11.display, 621 &_glfw.x11.xi.major, 622 &_glfw.x11.xi.minor) == Success) 623 { 624 _glfw.x11.xi.available = GLFW_TRUE; 625 } 626 } 627 } 628 629 #if defined(__CYGWIN__) 630 _glfw.x11.randr.handle = _glfw_dlopen("libXrandr-2.so"); 631 #elif defined(__OpenBSD__) || defined(__NetBSD__) 632 _glfw.x11.randr.handle = _glfw_dlopen("libXrandr.so"); 633 #else 634 _glfw.x11.randr.handle = _glfw_dlopen("libXrandr.so.2"); 635 #endif 636 if (_glfw.x11.randr.handle) 637 { 638 _glfw.x11.randr.AllocGamma = (PFN_XRRAllocGamma) 639 _glfw_dlsym(_glfw.x11.randr.handle, "XRRAllocGamma"); 640 _glfw.x11.randr.FreeGamma = (PFN_XRRFreeGamma) 641 _glfw_dlsym(_glfw.x11.randr.handle, "XRRFreeGamma"); 642 _glfw.x11.randr.FreeCrtcInfo = (PFN_XRRFreeCrtcInfo) 643 _glfw_dlsym(_glfw.x11.randr.handle, "XRRFreeCrtcInfo"); 644 _glfw.x11.randr.FreeGamma = (PFN_XRRFreeGamma) 645 _glfw_dlsym(_glfw.x11.randr.handle, "XRRFreeGamma"); 646 _glfw.x11.randr.FreeOutputInfo = (PFN_XRRFreeOutputInfo) 647 _glfw_dlsym(_glfw.x11.randr.handle, "XRRFreeOutputInfo"); 648 _glfw.x11.randr.FreeScreenResources = (PFN_XRRFreeScreenResources) 649 _glfw_dlsym(_glfw.x11.randr.handle, "XRRFreeScreenResources"); 650 _glfw.x11.randr.GetCrtcGamma = (PFN_XRRGetCrtcGamma) 651 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetCrtcGamma"); 652 _glfw.x11.randr.GetCrtcGammaSize = (PFN_XRRGetCrtcGammaSize) 653 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetCrtcGammaSize"); 654 _glfw.x11.randr.GetCrtcInfo = (PFN_XRRGetCrtcInfo) 655 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetCrtcInfo"); 656 _glfw.x11.randr.GetOutputInfo = (PFN_XRRGetOutputInfo) 657 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetOutputInfo"); 658 _glfw.x11.randr.GetOutputPrimary = (PFN_XRRGetOutputPrimary) 659 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetOutputPrimary"); 660 _glfw.x11.randr.GetScreenResourcesCurrent = (PFN_XRRGetScreenResourcesCurrent) 661 _glfw_dlsym(_glfw.x11.randr.handle, "XRRGetScreenResourcesCurrent"); 662 _glfw.x11.randr.QueryExtension = (PFN_XRRQueryExtension) 663 _glfw_dlsym(_glfw.x11.randr.handle, "XRRQueryExtension"); 664 _glfw.x11.randr.QueryVersion = (PFN_XRRQueryVersion) 665 _glfw_dlsym(_glfw.x11.randr.handle, "XRRQueryVersion"); 666 _glfw.x11.randr.SelectInput = (PFN_XRRSelectInput) 667 _glfw_dlsym(_glfw.x11.randr.handle, "XRRSelectInput"); 668 _glfw.x11.randr.SetCrtcConfig = (PFN_XRRSetCrtcConfig) 669 _glfw_dlsym(_glfw.x11.randr.handle, "XRRSetCrtcConfig"); 670 _glfw.x11.randr.SetCrtcGamma = (PFN_XRRSetCrtcGamma) 671 _glfw_dlsym(_glfw.x11.randr.handle, "XRRSetCrtcGamma"); 672 _glfw.x11.randr.UpdateConfiguration = (PFN_XRRUpdateConfiguration) 673 _glfw_dlsym(_glfw.x11.randr.handle, "XRRUpdateConfiguration"); 674 675 if (XRRQueryExtension(_glfw.x11.display, 676 &_glfw.x11.randr.eventBase, 677 &_glfw.x11.randr.errorBase)) 678 { 679 if (XRRQueryVersion(_glfw.x11.display, 680 &_glfw.x11.randr.major, 681 &_glfw.x11.randr.minor)) 682 { 683 // The GLFW RandR path requires at least version 1.3 684 if (_glfw.x11.randr.major > 1 || _glfw.x11.randr.minor >= 3) 685 _glfw.x11.randr.available = GLFW_TRUE; 686 } 687 else 688 { 689 _glfwInputError(GLFW_PLATFORM_ERROR, 690 "X11: Failed to query RandR version"); 691 } 692 } 693 } 694 695 if (_glfw.x11.randr.available) 696 { 697 XRRScreenResources* sr = XRRGetScreenResourcesCurrent(_glfw.x11.display, 698 _glfw.x11.root); 699 700 if (!sr->ncrtc || !XRRGetCrtcGammaSize(_glfw.x11.display, sr->crtcs[0])) 701 { 702 // This is likely an older Nvidia driver with broken gamma support 703 // Flag it as useless and fall back to xf86vm gamma, if available 704 _glfw.x11.randr.gammaBroken = GLFW_TRUE; 705 } 706 707 if (!sr->ncrtc) 708 { 709 // A system without CRTCs is likely a system with broken RandR 710 // Disable the RandR monitor path and fall back to core functions 711 _glfw.x11.randr.monitorBroken = GLFW_TRUE; 712 } 713 714 XRRFreeScreenResources(sr); 715 } 716 717 if (_glfw.x11.randr.available && !_glfw.x11.randr.monitorBroken) 718 { 719 XRRSelectInput(_glfw.x11.display, _glfw.x11.root, 720 RROutputChangeNotifyMask); 721 } 722 723 #if defined(__CYGWIN__) 724 _glfw.x11.xcursor.handle = _glfw_dlopen("libXcursor-1.so"); 725 #elif defined(__OpenBSD__) || defined(__NetBSD__) 726 _glfw.x11.xcursor.handle = _glfw_dlopen("libXcursor.so"); 727 #else 728 _glfw.x11.xcursor.handle = _glfw_dlopen("libXcursor.so.1"); 729 #endif 730 if (_glfw.x11.xcursor.handle) 731 { 732 _glfw.x11.xcursor.ImageCreate = (PFN_XcursorImageCreate) 733 _glfw_dlsym(_glfw.x11.xcursor.handle, "XcursorImageCreate"); 734 _glfw.x11.xcursor.ImageDestroy = (PFN_XcursorImageDestroy) 735 _glfw_dlsym(_glfw.x11.xcursor.handle, "XcursorImageDestroy"); 736 _glfw.x11.xcursor.ImageLoadCursor = (PFN_XcursorImageLoadCursor) 737 _glfw_dlsym(_glfw.x11.xcursor.handle, "XcursorImageLoadCursor"); 738 } 739 740 #if defined(__CYGWIN__) 741 _glfw.x11.xinerama.handle = _glfw_dlopen("libXinerama-1.so"); 742 #elif defined(__OpenBSD__) || defined(__NetBSD__) 743 _glfw.x11.xinerama.handle = _glfw_dlopen("libXinerama.so"); 744 #else 745 _glfw.x11.xinerama.handle = _glfw_dlopen("libXinerama.so.1"); 746 #endif 747 if (_glfw.x11.xinerama.handle) 748 { 749 _glfw.x11.xinerama.IsActive = (PFN_XineramaIsActive) 750 _glfw_dlsym(_glfw.x11.xinerama.handle, "XineramaIsActive"); 751 _glfw.x11.xinerama.QueryExtension = (PFN_XineramaQueryExtension) 752 _glfw_dlsym(_glfw.x11.xinerama.handle, "XineramaQueryExtension"); 753 _glfw.x11.xinerama.QueryScreens = (PFN_XineramaQueryScreens) 754 _glfw_dlsym(_glfw.x11.xinerama.handle, "XineramaQueryScreens"); 755 756 if (XineramaQueryExtension(_glfw.x11.display, 757 &_glfw.x11.xinerama.major, 758 &_glfw.x11.xinerama.minor)) 759 { 760 if (XineramaIsActive(_glfw.x11.display)) 761 _glfw.x11.xinerama.available = GLFW_TRUE; 762 } 763 } 764 765 _glfw.x11.xkb.major = 1; 766 _glfw.x11.xkb.minor = 0; 767 _glfw.x11.xkb.available = 768 XkbQueryExtension(_glfw.x11.display, 769 &_glfw.x11.xkb.majorOpcode, 770 &_glfw.x11.xkb.eventBase, 771 &_glfw.x11.xkb.errorBase, 772 &_glfw.x11.xkb.major, 773 &_glfw.x11.xkb.minor); 774 775 if (_glfw.x11.xkb.available) 776 { 777 Bool supported; 778 779 if (XkbSetDetectableAutoRepeat(_glfw.x11.display, True, &supported)) 780 { 781 if (supported) 782 _glfw.x11.xkb.detectable = GLFW_TRUE; 783 } 784 785 XkbStateRec state; 786 if (XkbGetState(_glfw.x11.display, XkbUseCoreKbd, &state) == Success) 787 _glfw.x11.xkb.group = (unsigned int)state.group; 788 789 XkbSelectEventDetails(_glfw.x11.display, XkbUseCoreKbd, XkbStateNotify, 790 XkbGroupStateMask, XkbGroupStateMask); 791 } 792 793 #if defined(__CYGWIN__) 794 _glfw.x11.x11xcb.handle = _glfw_dlopen("libX11-xcb-1.so"); 795 #elif defined(__OpenBSD__) || defined(__NetBSD__) 796 _glfw.x11.x11xcb.handle = _glfw_dlopen("libX11-xcb.so"); 797 #else 798 _glfw.x11.x11xcb.handle = _glfw_dlopen("libX11-xcb.so.1"); 799 #endif 800 if (_glfw.x11.x11xcb.handle) 801 { 802 _glfw.x11.x11xcb.GetXCBConnection = (PFN_XGetXCBConnection) 803 _glfw_dlsym(_glfw.x11.x11xcb.handle, "XGetXCBConnection"); 804 } 805 806 #if defined(__CYGWIN__) 807 _glfw.x11.xrender.handle = _glfw_dlopen("libXrender-1.so"); 808 #elif defined(__OpenBSD__) || defined(__NetBSD__) 809 _glfw.x11.xrender.handle = _glfw_dlopen("libXrender.so"); 810 #else 811 _glfw.x11.xrender.handle = _glfw_dlopen("libXrender.so.1"); 812 #endif 813 if (_glfw.x11.xrender.handle) 814 { 815 _glfw.x11.xrender.QueryExtension = (PFN_XRenderQueryExtension) 816 _glfw_dlsym(_glfw.x11.xrender.handle, "XRenderQueryExtension"); 817 _glfw.x11.xrender.QueryVersion = (PFN_XRenderQueryVersion) 818 _glfw_dlsym(_glfw.x11.xrender.handle, "XRenderQueryVersion"); 819 _glfw.x11.xrender.FindVisualFormat = (PFN_XRenderFindVisualFormat) 820 _glfw_dlsym(_glfw.x11.xrender.handle, "XRenderFindVisualFormat"); 821 822 if (XRenderQueryExtension(_glfw.x11.display, 823 &_glfw.x11.xrender.errorBase, 824 &_glfw.x11.xrender.eventBase)) 825 { 826 if (XRenderQueryVersion(_glfw.x11.display, 827 &_glfw.x11.xrender.major, 828 &_glfw.x11.xrender.minor)) 829 { 830 _glfw.x11.xrender.available = GLFW_TRUE; 831 } 832 } 833 } 834 835 // Update the key code LUT 836 // FIXME: We should listen to XkbMapNotify events to track changes to 837 // the keyboard mapping. 838 createKeyTables(); 839 840 // String format atoms 841 _glfw.x11.NULL_ = XInternAtom(_glfw.x11.display, "NULL", False); 842 _glfw.x11.UTF8_STRING = XInternAtom(_glfw.x11.display, "UTF8_STRING", False); 843 _glfw.x11.ATOM_PAIR = XInternAtom(_glfw.x11.display, "ATOM_PAIR", False); 844 845 // Custom selection property atom 846 _glfw.x11.GLFW_SELECTION = 847 XInternAtom(_glfw.x11.display, "GLFW_SELECTION", False); 848 849 // ICCCM standard clipboard atoms 850 _glfw.x11.TARGETS = XInternAtom(_glfw.x11.display, "TARGETS", False); 851 _glfw.x11.MULTIPLE = XInternAtom(_glfw.x11.display, "MULTIPLE", False); 852 _glfw.x11.PRIMARY = XInternAtom(_glfw.x11.display, "PRIMARY", False); 853 _glfw.x11.INCR = XInternAtom(_glfw.x11.display, "INCR", False); 854 _glfw.x11.CLIPBOARD = XInternAtom(_glfw.x11.display, "CLIPBOARD", False); 855 856 // Clipboard manager atoms 857 _glfw.x11.CLIPBOARD_MANAGER = 858 XInternAtom(_glfw.x11.display, "CLIPBOARD_MANAGER", False); 859 _glfw.x11.SAVE_TARGETS = 860 XInternAtom(_glfw.x11.display, "SAVE_TARGETS", False); 861 862 // Xdnd (drag and drop) atoms 863 _glfw.x11.XdndAware = XInternAtom(_glfw.x11.display, "XdndAware", False); 864 _glfw.x11.XdndEnter = XInternAtom(_glfw.x11.display, "XdndEnter", False); 865 _glfw.x11.XdndPosition = XInternAtom(_glfw.x11.display, "XdndPosition", False); 866 _glfw.x11.XdndStatus = XInternAtom(_glfw.x11.display, "XdndStatus", False); 867 _glfw.x11.XdndActionCopy = XInternAtom(_glfw.x11.display, "XdndActionCopy", False); 868 _glfw.x11.XdndDrop = XInternAtom(_glfw.x11.display, "XdndDrop", False); 869 _glfw.x11.XdndFinished = XInternAtom(_glfw.x11.display, "XdndFinished", False); 870 _glfw.x11.XdndSelection = XInternAtom(_glfw.x11.display, "XdndSelection", False); 871 _glfw.x11.XdndTypeList = XInternAtom(_glfw.x11.display, "XdndTypeList", False); 872 _glfw.x11.text_uri_list = XInternAtom(_glfw.x11.display, "text/uri-list", False); 873 874 // ICCCM, EWMH and Motif window property atoms 875 // These can be set safely even without WM support 876 // The EWMH atoms that require WM support are handled in detectEWMH 877 _glfw.x11.WM_PROTOCOLS = 878 XInternAtom(_glfw.x11.display, "WM_PROTOCOLS", False); 879 _glfw.x11.WM_STATE = 880 XInternAtom(_glfw.x11.display, "WM_STATE", False); 881 _glfw.x11.WM_DELETE_WINDOW = 882 XInternAtom(_glfw.x11.display, "WM_DELETE_WINDOW", False); 883 _glfw.x11.NET_SUPPORTED = 884 XInternAtom(_glfw.x11.display, "_NET_SUPPORTED", False); 885 _glfw.x11.NET_SUPPORTING_WM_CHECK = 886 XInternAtom(_glfw.x11.display, "_NET_SUPPORTING_WM_CHECK", False); 887 _glfw.x11.NET_WM_ICON = 888 XInternAtom(_glfw.x11.display, "_NET_WM_ICON", False); 889 _glfw.x11.NET_WM_PING = 890 XInternAtom(_glfw.x11.display, "_NET_WM_PING", False); 891 _glfw.x11.NET_WM_PID = 892 XInternAtom(_glfw.x11.display, "_NET_WM_PID", False); 893 _glfw.x11.NET_WM_NAME = 894 XInternAtom(_glfw.x11.display, "_NET_WM_NAME", False); 895 _glfw.x11.NET_WM_ICON_NAME = 896 XInternAtom(_glfw.x11.display, "_NET_WM_ICON_NAME", False); 897 _glfw.x11.NET_WM_BYPASS_COMPOSITOR = 898 XInternAtom(_glfw.x11.display, "_NET_WM_BYPASS_COMPOSITOR", False); 899 _glfw.x11.NET_WM_WINDOW_OPACITY = 900 XInternAtom(_glfw.x11.display, "_NET_WM_WINDOW_OPACITY", False); 901 _glfw.x11.MOTIF_WM_HINTS = 902 XInternAtom(_glfw.x11.display, "_MOTIF_WM_HINTS", False); 903 904 // The compositing manager selection name contains the screen number 905 { 906 char name[32]; 907 snprintf(name, sizeof(name), "_NET_WM_CM_S%u", _glfw.x11.screen); 908 _glfw.x11.NET_WM_CM_Sx = XInternAtom(_glfw.x11.display, name, False); 909 } 910 911 // Detect whether an EWMH-conformant window manager is running 912 detectEWMH(); 913 914 return GLFW_TRUE; 915 } 916 917 // Retrieve system content scale via folklore heuristics 918 // 919 static void getSystemContentScale(float* xscale, float* yscale) 920 { 921 // Start by assuming the default X11 DPI 922 // NOTE: Some desktop environments (KDE) may remove the Xft.dpi field when it 923 // would be set to 96, so assume that is the case if we cannot find it 924 float xdpi = 96.f, ydpi = 96.f; 925 926 // NOTE: Basing the scale on Xft.dpi where available should provide the most 927 // consistent user experience (matches Qt, Gtk, etc), although not 928 // always the most accurate one 929 char* rms = XResourceManagerString(_glfw.x11.display); 930 if (rms) 931 { 932 XrmDatabase db = XrmGetStringDatabase(rms); 933 if (db) 934 { 935 XrmValue value; 936 char* type = NULL; 937 938 if (XrmGetResource(db, "Xft.dpi", "Xft.Dpi", &type, &value)) 939 { 940 if (type && strcmp(type, "String") == 0) 941 xdpi = ydpi = atof(value.addr); 942 } 943 944 XrmDestroyDatabase(db); 945 } 946 } 947 948 *xscale = xdpi / 96.f; 949 *yscale = ydpi / 96.f; 950 } 951 952 // Create a blank cursor for hidden and disabled cursor modes 953 // 954 static Cursor createHiddenCursor(void) 955 { 956 unsigned char pixels[16 * 16 * 4] = { 0 }; 957 GLFWimage image = { 16, 16, pixels }; 958 return _glfwCreateCursorX11(&image, 0, 0); 959 } 960 961 // Create a helper window for IPC 962 // 963 static Window createHelperWindow(void) 964 { 965 XSetWindowAttributes wa; 966 wa.event_mask = PropertyChangeMask; 967 968 return XCreateWindow(_glfw.x11.display, _glfw.x11.root, 969 0, 0, 1, 1, 0, 0, 970 InputOnly, 971 DefaultVisual(_glfw.x11.display, _glfw.x11.screen), 972 CWEventMask, &wa); 973 } 974 975 // Create the pipe for empty events without assumuing the OS has pipe2(2) 976 // 977 static GLFWbool createEmptyEventPipe(void) 978 { 979 if (pipe(_glfw.x11.emptyEventPipe) != 0) 980 { 981 _glfwInputError(GLFW_PLATFORM_ERROR, 982 "X11: Failed to create empty event pipe: %s", 983 strerror(errno)); 984 return GLFW_FALSE; 985 } 986 987 for (int i = 0; i < 2; i++) 988 { 989 const int sf = fcntl(_glfw.x11.emptyEventPipe[i], F_GETFL, 0); 990 const int df = fcntl(_glfw.x11.emptyEventPipe[i], F_GETFD, 0); 991 992 if (sf == -1 || df == -1 || 993 fcntl(_glfw.x11.emptyEventPipe[i], F_SETFL, sf | O_NONBLOCK) == -1 || 994 fcntl(_glfw.x11.emptyEventPipe[i], F_SETFD, df | FD_CLOEXEC) == -1) 995 { 996 _glfwInputError(GLFW_PLATFORM_ERROR, 997 "X11: Failed to set flags for empty event pipe: %s", 998 strerror(errno)); 999 return GLFW_FALSE; 1000 } 1001 } 1002 1003 return GLFW_TRUE; 1004 } 1005 1006 // X error handler 1007 // 1008 static int errorHandler(Display *display, XErrorEvent* event) 1009 { 1010 if (_glfw.x11.display != display) 1011 return 0; 1012 1013 _glfw.x11.errorCode = event->error_code; 1014 return 0; 1015 } 1016 1017 1018 ////////////////////////////////////////////////////////////////////////// 1019 ////// GLFW internal API ////// 1020 ////////////////////////////////////////////////////////////////////////// 1021 1022 // Sets the X error handler callback 1023 // 1024 void _glfwGrabErrorHandlerX11(void) 1025 { 1026 assert(_glfw.x11.errorHandler == NULL); 1027 _glfw.x11.errorCode = Success; 1028 _glfw.x11.errorHandler = XSetErrorHandler(errorHandler); 1029 } 1030 1031 // Clears the X error handler callback 1032 // 1033 void _glfwReleaseErrorHandlerX11(void) 1034 { 1035 // Synchronize to make sure all commands are processed 1036 XSync(_glfw.x11.display, False); 1037 XSetErrorHandler(_glfw.x11.errorHandler); 1038 _glfw.x11.errorHandler = NULL; 1039 } 1040 1041 // Reports the specified error, appending information about the last X error 1042 // 1043 void _glfwInputErrorX11(int error, const char* message) 1044 { 1045 char buffer[_GLFW_MESSAGE_SIZE]; 1046 XGetErrorText(_glfw.x11.display, _glfw.x11.errorCode, 1047 buffer, sizeof(buffer)); 1048 1049 _glfwInputError(error, "%s: %s", message, buffer); 1050 } 1051 1052 // Creates a native cursor object from the specified image and hotspot 1053 // 1054 Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot) 1055 { 1056 int i; 1057 Cursor cursor; 1058 1059 if (!_glfw.x11.xcursor.handle) 1060 return None; 1061 1062 XcursorImage* native = XcursorImageCreate(image->width, image->height); 1063 if (native == NULL) 1064 return None; 1065 1066 native->xhot = xhot; 1067 native->yhot = yhot; 1068 1069 unsigned char* source = (unsigned char*) image->pixels; 1070 XcursorPixel* target = native->pixels; 1071 1072 for (i = 0; i < image->width * image->height; i++, target++, source += 4) 1073 { 1074 unsigned int alpha = source[3]; 1075 1076 *target = (alpha << 24) | 1077 ((unsigned char) ((source[0] * alpha) / 255) << 16) | 1078 ((unsigned char) ((source[1] * alpha) / 255) << 8) | 1079 ((unsigned char) ((source[2] * alpha) / 255) << 0); 1080 } 1081 1082 cursor = XcursorImageLoadCursor(_glfw.x11.display, native); 1083 XcursorImageDestroy(native); 1084 1085 return cursor; 1086 } 1087 1088 1089 ////////////////////////////////////////////////////////////////////////// 1090 ////// GLFW platform API ////// 1091 ////////////////////////////////////////////////////////////////////////// 1092 1093 int _glfwPlatformInit(void) 1094 { 1095 // HACK: If the application has left the locale as "C" then both wide 1096 // character text input and explicit UTF-8 input via XIM will break 1097 // This sets the CTYPE part of the current locale from the environment 1098 // in the hope that it is set to something more sane than "C" 1099 if (strcmp(setlocale(LC_CTYPE, NULL), "C") == 0) 1100 setlocale(LC_CTYPE, ""); 1101 1102 XInitThreads(); 1103 XrmInitialize(); 1104 1105 _glfw.x11.display = XOpenDisplay(NULL); 1106 if (!_glfw.x11.display) 1107 { 1108 const char* display = getenv("DISPLAY"); 1109 if (display) 1110 { 1111 _glfwInputError(GLFW_PLATFORM_ERROR, 1112 "X11: Failed to open display %s", display); 1113 } 1114 else 1115 { 1116 _glfwInputError(GLFW_PLATFORM_ERROR, 1117 "X11: The DISPLAY environment variable is missing"); 1118 } 1119 1120 return GLFW_FALSE; 1121 } 1122 1123 _glfw.x11.screen = DefaultScreen(_glfw.x11.display); 1124 _glfw.x11.root = RootWindow(_glfw.x11.display, _glfw.x11.screen); 1125 _glfw.x11.context = XUniqueContext(); 1126 1127 getSystemContentScale(&_glfw.x11.contentScaleX, &_glfw.x11.contentScaleY); 1128 1129 if (!createEmptyEventPipe()) 1130 return GLFW_FALSE; 1131 1132 if (!initExtensions()) 1133 return GLFW_FALSE; 1134 1135 _glfw.x11.helperWindowHandle = createHelperWindow(); 1136 _glfw.x11.hiddenCursorHandle = createHiddenCursor(); 1137 1138 if (XSupportsLocale()) 1139 { 1140 XSetLocaleModifiers(""); 1141 1142 _glfw.x11.im = XOpenIM(_glfw.x11.display, 0, NULL, NULL); 1143 if (_glfw.x11.im) 1144 { 1145 if (!hasUsableInputMethodStyle()) 1146 { 1147 XCloseIM(_glfw.x11.im); 1148 _glfw.x11.im = NULL; 1149 } 1150 } 1151 } 1152 1153 #if defined(__linux__) 1154 if (!_glfwInitJoysticksLinux()) 1155 return GLFW_FALSE; 1156 #endif 1157 1158 _glfwInitTimerPOSIX(); 1159 1160 _glfwPollMonitorsX11(); 1161 return GLFW_TRUE; 1162 } 1163 1164 void _glfwPlatformTerminate(void) 1165 { 1166 if (_glfw.x11.helperWindowHandle) 1167 { 1168 if (XGetSelectionOwner(_glfw.x11.display, _glfw.x11.CLIPBOARD) == 1169 _glfw.x11.helperWindowHandle) 1170 { 1171 _glfwPushSelectionToManagerX11(); 1172 } 1173 1174 XDestroyWindow(_glfw.x11.display, _glfw.x11.helperWindowHandle); 1175 _glfw.x11.helperWindowHandle = None; 1176 } 1177 1178 if (_glfw.x11.hiddenCursorHandle) 1179 { 1180 XFreeCursor(_glfw.x11.display, _glfw.x11.hiddenCursorHandle); 1181 _glfw.x11.hiddenCursorHandle = (Cursor) 0; 1182 } 1183 1184 free(_glfw.x11.primarySelectionString); 1185 free(_glfw.x11.clipboardString); 1186 1187 if (_glfw.x11.im) 1188 { 1189 XCloseIM(_glfw.x11.im); 1190 _glfw.x11.im = NULL; 1191 } 1192 1193 if (_glfw.x11.display) 1194 { 1195 XCloseDisplay(_glfw.x11.display); 1196 _glfw.x11.display = NULL; 1197 } 1198 1199 if (_glfw.x11.x11xcb.handle) 1200 { 1201 _glfw_dlclose(_glfw.x11.x11xcb.handle); 1202 _glfw.x11.x11xcb.handle = NULL; 1203 } 1204 1205 if (_glfw.x11.xcursor.handle) 1206 { 1207 _glfw_dlclose(_glfw.x11.xcursor.handle); 1208 _glfw.x11.xcursor.handle = NULL; 1209 } 1210 1211 if (_glfw.x11.randr.handle) 1212 { 1213 _glfw_dlclose(_glfw.x11.randr.handle); 1214 _glfw.x11.randr.handle = NULL; 1215 } 1216 1217 if (_glfw.x11.xinerama.handle) 1218 { 1219 _glfw_dlclose(_glfw.x11.xinerama.handle); 1220 _glfw.x11.xinerama.handle = NULL; 1221 } 1222 1223 if (_glfw.x11.xrender.handle) 1224 { 1225 _glfw_dlclose(_glfw.x11.xrender.handle); 1226 _glfw.x11.xrender.handle = NULL; 1227 } 1228 1229 if (_glfw.x11.vidmode.handle) 1230 { 1231 _glfw_dlclose(_glfw.x11.vidmode.handle); 1232 _glfw.x11.vidmode.handle = NULL; 1233 } 1234 1235 if (_glfw.x11.xi.handle) 1236 { 1237 _glfw_dlclose(_glfw.x11.xi.handle); 1238 _glfw.x11.xi.handle = NULL; 1239 } 1240 1241 _glfwTerminateOSMesa(); 1242 // NOTE: These need to be unloaded after XCloseDisplay, as they register 1243 // cleanup callbacks that get called by that function 1244 _glfwTerminateEGL(); 1245 _glfwTerminateGLX(); 1246 1247 #if defined(__linux__) 1248 _glfwTerminateJoysticksLinux(); 1249 #endif 1250 1251 if (_glfw.x11.emptyEventPipe[0] || _glfw.x11.emptyEventPipe[1]) 1252 { 1253 close(_glfw.x11.emptyEventPipe[0]); 1254 close(_glfw.x11.emptyEventPipe[1]); 1255 } 1256 } 1257 1258 const char* _glfwPlatformGetVersionString(void) 1259 { 1260 return _GLFW_VERSION_NUMBER " X11 GLX EGL OSMesa" 1261 #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) 1262 " clock_gettime" 1263 #else 1264 " gettimeofday" 1265 #endif 1266 #if defined(__linux__) 1267 " evdev" 1268 #endif 1269 #if defined(_GLFW_BUILD_DLL) 1270 " shared" 1271 #endif 1272 ; 1273 } 1274