x11_window.c (101826B)
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 #define _GNU_SOURCE 31 32 #include "internal.h" 33 34 #include <X11/cursorfont.h> 35 #include <X11/Xmd.h> 36 37 #include <poll.h> 38 #include <signal.h> 39 #include <time.h> 40 41 #include <string.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <limits.h> 45 #include <errno.h> 46 #include <assert.h> 47 48 // Action for EWMH client messages 49 #define _NET_WM_STATE_REMOVE 0 50 #define _NET_WM_STATE_ADD 1 51 #define _NET_WM_STATE_TOGGLE 2 52 53 // Additional mouse button names for XButtonEvent 54 #define Button6 6 55 #define Button7 7 56 57 // Motif WM hints flags 58 #define MWM_HINTS_DECORATIONS 2 59 #define MWM_DECOR_ALL 1 60 61 #define _GLFW_XDND_VERSION 5 62 63 // Wait for data to arrive on any of the specified file descriptors 64 // 65 static GLFWbool waitForData(struct pollfd* fds, nfds_t count, double* timeout) 66 { 67 for (;;) 68 { 69 if (timeout) 70 { 71 const uint64_t base = _glfwPlatformGetTimerValue(); 72 73 #if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__CYGWIN__) 74 const time_t seconds = (time_t) *timeout; 75 const long nanoseconds = (long) ((*timeout - seconds) * 1e9); 76 const struct timespec ts = { seconds, nanoseconds }; 77 const int result = ppoll(fds, count, &ts, NULL); 78 #elif defined(__NetBSD__) 79 const time_t seconds = (time_t) *timeout; 80 const long nanoseconds = (long) ((*timeout - seconds) * 1e9); 81 const struct timespec ts = { seconds, nanoseconds }; 82 const int result = pollts(fds, count, &ts, NULL); 83 #else 84 const int milliseconds = (int) (*timeout * 1e3); 85 const int result = poll(fds, count, milliseconds); 86 #endif 87 const int error = errno; // clock_gettime may overwrite our error 88 89 *timeout -= (_glfwPlatformGetTimerValue() - base) / 90 (double) _glfwPlatformGetTimerFrequency(); 91 92 if (result > 0) 93 return GLFW_TRUE; 94 else if (result == -1 && error != EINTR && error != EAGAIN) 95 return GLFW_FALSE; 96 else if (*timeout <= 0.0) 97 return GLFW_FALSE; 98 } 99 else 100 { 101 const int result = poll(fds, count, -1); 102 if (result > 0) 103 return GLFW_TRUE; 104 else if (result == -1 && errno != EINTR && errno != EAGAIN) 105 return GLFW_FALSE; 106 } 107 } 108 } 109 110 // Wait for event data to arrive on the X11 display socket 111 // This avoids blocking other threads via the per-display Xlib lock that also 112 // covers GLX functions 113 // 114 static GLFWbool waitForX11Event(double* timeout) 115 { 116 struct pollfd fd = { ConnectionNumber(_glfw.x11.display), POLLIN }; 117 118 while (!XPending(_glfw.x11.display)) 119 { 120 if (!waitForData(&fd, 1, timeout)) 121 return GLFW_FALSE; 122 } 123 124 return GLFW_TRUE; 125 } 126 127 // Wait for event data to arrive on any event file descriptor 128 // This avoids blocking other threads via the per-display Xlib lock that also 129 // covers GLX functions 130 // 131 static GLFWbool waitForAnyEvent(double* timeout) 132 { 133 nfds_t count = 2; 134 struct pollfd fds[3] = 135 { 136 { ConnectionNumber(_glfw.x11.display), POLLIN }, 137 { _glfw.x11.emptyEventPipe[0], POLLIN } 138 }; 139 140 #if defined(__linux__) 141 if (_glfw.linjs.inotify > 0) 142 fds[count++] = (struct pollfd) { _glfw.linjs.inotify, POLLIN }; 143 #endif 144 145 while (!XPending(_glfw.x11.display)) 146 { 147 if (!waitForData(fds, count, timeout)) 148 return GLFW_FALSE; 149 150 for (int i = 1; i < count; i++) 151 { 152 if (fds[i].revents & POLLIN) 153 return GLFW_TRUE; 154 } 155 } 156 157 return GLFW_TRUE; 158 } 159 160 // Writes a byte to the empty event pipe 161 // 162 static void writeEmptyEvent(void) 163 { 164 for (;;) 165 { 166 const char byte = 0; 167 const ssize_t result = write(_glfw.x11.emptyEventPipe[1], &byte, 1); 168 if (result == 1 || (result == -1 && errno != EINTR)) 169 break; 170 } 171 } 172 173 // Drains available data from the empty event pipe 174 // 175 static void drainEmptyEvents(void) 176 { 177 for (;;) 178 { 179 char dummy[64]; 180 const ssize_t result = read(_glfw.x11.emptyEventPipe[0], dummy, sizeof(dummy)); 181 if (result == -1 && errno != EINTR) 182 break; 183 } 184 } 185 186 // Waits until a VisibilityNotify event arrives for the specified window or the 187 // timeout period elapses (ICCCM section 4.2.2) 188 // 189 static GLFWbool waitForVisibilityNotify(_GLFWwindow* window) 190 { 191 XEvent dummy; 192 double timeout = 0.1; 193 194 while (!XCheckTypedWindowEvent(_glfw.x11.display, 195 window->x11.handle, 196 VisibilityNotify, 197 &dummy)) 198 { 199 if (!waitForX11Event(&timeout)) 200 return GLFW_FALSE; 201 } 202 203 return GLFW_TRUE; 204 } 205 206 // Returns whether the window is iconified 207 // 208 static int getWindowState(_GLFWwindow* window) 209 { 210 int result = WithdrawnState; 211 struct { 212 CARD32 state; 213 Window icon; 214 } *state = NULL; 215 216 if (_glfwGetWindowPropertyX11(window->x11.handle, 217 _glfw.x11.WM_STATE, 218 _glfw.x11.WM_STATE, 219 (unsigned char**) &state) >= 2) 220 { 221 result = state->state; 222 } 223 224 if (state) 225 XFree(state); 226 227 return result; 228 } 229 230 // Returns whether the event is a selection event 231 // 232 static Bool isSelectionEvent(Display* display, XEvent* event, XPointer pointer) 233 { 234 if (event->xany.window != _glfw.x11.helperWindowHandle) 235 return False; 236 237 return event->type == SelectionRequest || 238 event->type == SelectionNotify || 239 event->type == SelectionClear; 240 } 241 242 // Returns whether it is a _NET_FRAME_EXTENTS event for the specified window 243 // 244 static Bool isFrameExtentsEvent(Display* display, XEvent* event, XPointer pointer) 245 { 246 _GLFWwindow* window = (_GLFWwindow*) pointer; 247 return event->type == PropertyNotify && 248 event->xproperty.state == PropertyNewValue && 249 event->xproperty.window == window->x11.handle && 250 event->xproperty.atom == _glfw.x11.NET_FRAME_EXTENTS; 251 } 252 253 // Returns whether it is a property event for the specified selection transfer 254 // 255 static Bool isSelPropNewValueNotify(Display* display, XEvent* event, XPointer pointer) 256 { 257 XEvent* notification = (XEvent*) pointer; 258 return event->type == PropertyNotify && 259 event->xproperty.state == PropertyNewValue && 260 event->xproperty.window == notification->xselection.requestor && 261 event->xproperty.atom == notification->xselection.property; 262 } 263 264 // Translates an X event modifier state mask 265 // 266 static int translateState(int state) 267 { 268 int mods = 0; 269 270 if (state & ShiftMask) 271 mods |= GLFW_MOD_SHIFT; 272 if (state & ControlMask) 273 mods |= GLFW_MOD_CONTROL; 274 if (state & Mod1Mask) 275 mods |= GLFW_MOD_ALT; 276 if (state & Mod4Mask) 277 mods |= GLFW_MOD_SUPER; 278 if (state & LockMask) 279 mods |= GLFW_MOD_CAPS_LOCK; 280 if (state & Mod2Mask) 281 mods |= GLFW_MOD_NUM_LOCK; 282 283 return mods; 284 } 285 286 // Translates an X11 key code to a GLFW key token 287 // 288 static int translateKey(int scancode) 289 { 290 // Use the pre-filled LUT (see createKeyTables() in x11_init.c) 291 if (scancode < 0 || scancode > 255) 292 return GLFW_KEY_UNKNOWN; 293 294 return _glfw.x11.keycodes[scancode]; 295 } 296 297 // Sends an EWMH or ICCCM event to the window manager 298 // 299 static void sendEventToWM(_GLFWwindow* window, Atom type, 300 long a, long b, long c, long d, long e) 301 { 302 XEvent event = { ClientMessage }; 303 event.xclient.window = window->x11.handle; 304 event.xclient.format = 32; // Data is 32-bit longs 305 event.xclient.message_type = type; 306 event.xclient.data.l[0] = a; 307 event.xclient.data.l[1] = b; 308 event.xclient.data.l[2] = c; 309 event.xclient.data.l[3] = d; 310 event.xclient.data.l[4] = e; 311 312 XSendEvent(_glfw.x11.display, _glfw.x11.root, 313 False, 314 SubstructureNotifyMask | SubstructureRedirectMask, 315 &event); 316 } 317 318 // Updates the normal hints according to the window settings 319 // 320 static void updateNormalHints(_GLFWwindow* window, int width, int height) 321 { 322 XSizeHints* hints = XAllocSizeHints(); 323 324 if (!window->monitor) 325 { 326 if (window->resizable) 327 { 328 if (window->minwidth != GLFW_DONT_CARE && 329 window->minheight != GLFW_DONT_CARE) 330 { 331 hints->flags |= PMinSize; 332 hints->min_width = window->minwidth; 333 hints->min_height = window->minheight; 334 } 335 336 if (window->maxwidth != GLFW_DONT_CARE && 337 window->maxheight != GLFW_DONT_CARE) 338 { 339 hints->flags |= PMaxSize; 340 hints->max_width = window->maxwidth; 341 hints->max_height = window->maxheight; 342 } 343 344 if (window->numer != GLFW_DONT_CARE && 345 window->denom != GLFW_DONT_CARE) 346 { 347 hints->flags |= PAspect; 348 hints->min_aspect.x = hints->max_aspect.x = window->numer; 349 hints->min_aspect.y = hints->max_aspect.y = window->denom; 350 } 351 } 352 else 353 { 354 hints->flags |= (PMinSize | PMaxSize); 355 hints->min_width = hints->max_width = width; 356 hints->min_height = hints->max_height = height; 357 } 358 } 359 360 hints->flags |= PWinGravity; 361 hints->win_gravity = StaticGravity; 362 363 XSetWMNormalHints(_glfw.x11.display, window->x11.handle, hints); 364 XFree(hints); 365 } 366 367 // Updates the full screen status of the window 368 // 369 static void updateWindowMode(_GLFWwindow* window) 370 { 371 if (window->monitor) 372 { 373 if (_glfw.x11.xinerama.available && 374 _glfw.x11.NET_WM_FULLSCREEN_MONITORS) 375 { 376 sendEventToWM(window, 377 _glfw.x11.NET_WM_FULLSCREEN_MONITORS, 378 window->monitor->x11.index, 379 window->monitor->x11.index, 380 window->monitor->x11.index, 381 window->monitor->x11.index, 382 0); 383 } 384 385 if (_glfw.x11.NET_WM_STATE && _glfw.x11.NET_WM_STATE_FULLSCREEN) 386 { 387 sendEventToWM(window, 388 _glfw.x11.NET_WM_STATE, 389 _NET_WM_STATE_ADD, 390 _glfw.x11.NET_WM_STATE_FULLSCREEN, 391 0, 1, 0); 392 } 393 else 394 { 395 // This is the butcher's way of removing window decorations 396 // Setting the override-redirect attribute on a window makes the 397 // window manager ignore the window completely (ICCCM, section 4) 398 // The good thing is that this makes undecorated full screen windows 399 // easy to do; the bad thing is that we have to do everything 400 // manually and some things (like iconify/restore) won't work at 401 // all, as those are tasks usually performed by the window manager 402 403 XSetWindowAttributes attributes; 404 attributes.override_redirect = True; 405 XChangeWindowAttributes(_glfw.x11.display, 406 window->x11.handle, 407 CWOverrideRedirect, 408 &attributes); 409 410 window->x11.overrideRedirect = GLFW_TRUE; 411 } 412 413 // Enable compositor bypass 414 if (!window->x11.transparent) 415 { 416 const unsigned long value = 1; 417 418 XChangeProperty(_glfw.x11.display, window->x11.handle, 419 _glfw.x11.NET_WM_BYPASS_COMPOSITOR, XA_CARDINAL, 32, 420 PropModeReplace, (unsigned char*) &value, 1); 421 } 422 } 423 else 424 { 425 if (_glfw.x11.xinerama.available && 426 _glfw.x11.NET_WM_FULLSCREEN_MONITORS) 427 { 428 XDeleteProperty(_glfw.x11.display, window->x11.handle, 429 _glfw.x11.NET_WM_FULLSCREEN_MONITORS); 430 } 431 432 if (_glfw.x11.NET_WM_STATE && _glfw.x11.NET_WM_STATE_FULLSCREEN) 433 { 434 sendEventToWM(window, 435 _glfw.x11.NET_WM_STATE, 436 _NET_WM_STATE_REMOVE, 437 _glfw.x11.NET_WM_STATE_FULLSCREEN, 438 0, 1, 0); 439 } 440 else 441 { 442 XSetWindowAttributes attributes; 443 attributes.override_redirect = False; 444 XChangeWindowAttributes(_glfw.x11.display, 445 window->x11.handle, 446 CWOverrideRedirect, 447 &attributes); 448 449 window->x11.overrideRedirect = GLFW_FALSE; 450 } 451 452 // Disable compositor bypass 453 if (!window->x11.transparent) 454 { 455 XDeleteProperty(_glfw.x11.display, window->x11.handle, 456 _glfw.x11.NET_WM_BYPASS_COMPOSITOR); 457 } 458 } 459 } 460 461 // Decode a Unicode code point from a UTF-8 stream 462 // Based on cutef8 by Jeff Bezanson (Public Domain) 463 // 464 #if defined(X_HAVE_UTF8_STRING) 465 static uint32_t decodeUTF8(const char** s) 466 { 467 uint32_t codepoint = 0, count = 0; 468 static const uint32_t offsets[] = 469 { 470 0x00000000u, 0x00003080u, 0x000e2080u, 471 0x03c82080u, 0xfa082080u, 0x82082080u 472 }; 473 474 do 475 { 476 codepoint = (codepoint << 6) + (unsigned char) **s; 477 (*s)++; 478 count++; 479 } while ((**s & 0xc0) == 0x80); 480 481 assert(count <= 6); 482 return codepoint - offsets[count - 1]; 483 } 484 #endif /*X_HAVE_UTF8_STRING*/ 485 486 // Convert the specified Latin-1 string to UTF-8 487 // 488 static char* convertLatin1toUTF8(const char* source) 489 { 490 size_t size = 1; 491 const char* sp; 492 493 for (sp = source; *sp; sp++) 494 size += (*sp & 0x80) ? 2 : 1; 495 496 char* target = calloc(size, 1); 497 char* tp = target; 498 499 for (sp = source; *sp; sp++) 500 tp += _glfwEncodeUTF8(tp, *sp); 501 502 return target; 503 } 504 505 // Updates the cursor image according to its cursor mode 506 // 507 static void updateCursorImage(_GLFWwindow* window) 508 { 509 if (window->cursorMode == GLFW_CURSOR_NORMAL) 510 { 511 if (window->cursor) 512 { 513 XDefineCursor(_glfw.x11.display, window->x11.handle, 514 window->cursor->x11.handle); 515 } 516 else 517 XUndefineCursor(_glfw.x11.display, window->x11.handle); 518 } 519 else 520 { 521 XDefineCursor(_glfw.x11.display, window->x11.handle, 522 _glfw.x11.hiddenCursorHandle); 523 } 524 } 525 526 // Enable XI2 raw mouse motion events 527 // 528 static void enableRawMouseMotion(_GLFWwindow* window) 529 { 530 XIEventMask em; 531 unsigned char mask[XIMaskLen(XI_RawMotion)] = { 0 }; 532 533 em.deviceid = XIAllMasterDevices; 534 em.mask_len = sizeof(mask); 535 em.mask = mask; 536 XISetMask(mask, XI_RawMotion); 537 538 XISelectEvents(_glfw.x11.display, _glfw.x11.root, &em, 1); 539 } 540 541 // Disable XI2 raw mouse motion events 542 // 543 static void disableRawMouseMotion(_GLFWwindow* window) 544 { 545 XIEventMask em; 546 unsigned char mask[] = { 0 }; 547 548 em.deviceid = XIAllMasterDevices; 549 em.mask_len = sizeof(mask); 550 em.mask = mask; 551 552 XISelectEvents(_glfw.x11.display, _glfw.x11.root, &em, 1); 553 } 554 555 // Apply disabled cursor mode to a focused window 556 // 557 static void disableCursor(_GLFWwindow* window) 558 { 559 if (window->rawMouseMotion) 560 enableRawMouseMotion(window); 561 562 _glfw.x11.disabledCursorWindow = window; 563 _glfwPlatformGetCursorPos(window, 564 &_glfw.x11.restoreCursorPosX, 565 &_glfw.x11.restoreCursorPosY); 566 updateCursorImage(window); 567 _glfwCenterCursorInContentArea(window); 568 XGrabPointer(_glfw.x11.display, window->x11.handle, True, 569 ButtonPressMask | ButtonReleaseMask | PointerMotionMask, 570 GrabModeAsync, GrabModeAsync, 571 window->x11.handle, 572 _glfw.x11.hiddenCursorHandle, 573 CurrentTime); 574 } 575 576 // Exit disabled cursor mode for the specified window 577 // 578 static void enableCursor(_GLFWwindow* window) 579 { 580 if (window->rawMouseMotion) 581 disableRawMouseMotion(window); 582 583 _glfw.x11.disabledCursorWindow = NULL; 584 XUngrabPointer(_glfw.x11.display, CurrentTime); 585 _glfwPlatformSetCursorPos(window, 586 _glfw.x11.restoreCursorPosX, 587 _glfw.x11.restoreCursorPosY); 588 updateCursorImage(window); 589 } 590 591 // Create the X11 window (and its colormap) 592 // 593 static GLFWbool createNativeWindow(_GLFWwindow* window, 594 const _GLFWwndconfig* wndconfig, 595 Visual* visual, int depth) 596 { 597 int width = wndconfig->width; 598 int height = wndconfig->height; 599 600 if (wndconfig->scaleToMonitor) 601 { 602 width *= _glfw.x11.contentScaleX; 603 height *= _glfw.x11.contentScaleY; 604 } 605 606 // Create a colormap based on the visual used by the current context 607 window->x11.colormap = XCreateColormap(_glfw.x11.display, 608 _glfw.x11.root, 609 visual, 610 AllocNone); 611 612 window->x11.transparent = _glfwIsVisualTransparentX11(visual); 613 614 XSetWindowAttributes wa = { 0 }; 615 wa.colormap = window->x11.colormap; 616 wa.event_mask = StructureNotifyMask | KeyPressMask | KeyReleaseMask | 617 PointerMotionMask | ButtonPressMask | ButtonReleaseMask | 618 ExposureMask | FocusChangeMask | VisibilityChangeMask | 619 EnterWindowMask | LeaveWindowMask | PropertyChangeMask; 620 621 _glfwGrabErrorHandlerX11(); 622 623 window->x11.parent = _glfw.x11.root; 624 window->x11.handle = XCreateWindow(_glfw.x11.display, 625 _glfw.x11.root, 626 0, 0, // Position 627 width, height, 628 0, // Border width 629 depth, // Color depth 630 InputOutput, 631 visual, 632 CWBorderPixel | CWColormap | CWEventMask, 633 &wa); 634 635 _glfwReleaseErrorHandlerX11(); 636 637 if (!window->x11.handle) 638 { 639 _glfwInputErrorX11(GLFW_PLATFORM_ERROR, 640 "X11: Failed to create window"); 641 return GLFW_FALSE; 642 } 643 644 XSaveContext(_glfw.x11.display, 645 window->x11.handle, 646 _glfw.x11.context, 647 (XPointer) window); 648 649 if (!wndconfig->decorated) 650 _glfwPlatformSetWindowDecorated(window, GLFW_FALSE); 651 652 if (_glfw.x11.NET_WM_STATE && !window->monitor) 653 { 654 Atom states[3]; 655 int count = 0; 656 657 if (wndconfig->floating) 658 { 659 if (_glfw.x11.NET_WM_STATE_ABOVE) 660 states[count++] = _glfw.x11.NET_WM_STATE_ABOVE; 661 } 662 663 if (wndconfig->maximized) 664 { 665 if (_glfw.x11.NET_WM_STATE_MAXIMIZED_VERT && 666 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ) 667 { 668 states[count++] = _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT; 669 states[count++] = _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ; 670 window->x11.maximized = GLFW_TRUE; 671 } 672 } 673 674 if (count) 675 { 676 XChangeProperty(_glfw.x11.display, window->x11.handle, 677 _glfw.x11.NET_WM_STATE, XA_ATOM, 32, 678 PropModeReplace, (unsigned char*) states, count); 679 } 680 } 681 682 // Declare the WM protocols supported by GLFW 683 { 684 Atom protocols[] = 685 { 686 _glfw.x11.WM_DELETE_WINDOW, 687 _glfw.x11.NET_WM_PING 688 }; 689 690 XSetWMProtocols(_glfw.x11.display, window->x11.handle, 691 protocols, sizeof(protocols) / sizeof(Atom)); 692 } 693 694 // Declare our PID 695 { 696 const long pid = getpid(); 697 698 XChangeProperty(_glfw.x11.display, window->x11.handle, 699 _glfw.x11.NET_WM_PID, XA_CARDINAL, 32, 700 PropModeReplace, 701 (unsigned char*) &pid, 1); 702 } 703 704 if (_glfw.x11.NET_WM_WINDOW_TYPE && _glfw.x11.NET_WM_WINDOW_TYPE_NORMAL) 705 { 706 Atom type = _glfw.x11.NET_WM_WINDOW_TYPE_NORMAL; 707 XChangeProperty(_glfw.x11.display, window->x11.handle, 708 _glfw.x11.NET_WM_WINDOW_TYPE, XA_ATOM, 32, 709 PropModeReplace, (unsigned char*) &type, 1); 710 } 711 712 // Set ICCCM WM_HINTS property 713 { 714 XWMHints* hints = XAllocWMHints(); 715 if (!hints) 716 { 717 _glfwInputError(GLFW_OUT_OF_MEMORY, 718 "X11: Failed to allocate WM hints"); 719 return GLFW_FALSE; 720 } 721 722 hints->flags = StateHint; 723 hints->initial_state = NormalState; 724 725 XSetWMHints(_glfw.x11.display, window->x11.handle, hints); 726 XFree(hints); 727 } 728 729 updateNormalHints(window, width, height); 730 731 // Set ICCCM WM_CLASS property 732 { 733 XClassHint* hint = XAllocClassHint(); 734 735 if (strlen(wndconfig->x11.instanceName) && 736 strlen(wndconfig->x11.className)) 737 { 738 hint->res_name = (char*) wndconfig->x11.instanceName; 739 hint->res_class = (char*) wndconfig->x11.className; 740 } 741 else 742 { 743 const char* resourceName = getenv("RESOURCE_NAME"); 744 if (resourceName && strlen(resourceName)) 745 hint->res_name = (char*) resourceName; 746 else if (strlen(wndconfig->title)) 747 hint->res_name = (char*) wndconfig->title; 748 else 749 hint->res_name = (char*) "glfw-application"; 750 751 if (strlen(wndconfig->title)) 752 hint->res_class = (char*) wndconfig->title; 753 else 754 hint->res_class = (char*) "GLFW-Application"; 755 } 756 757 XSetClassHint(_glfw.x11.display, window->x11.handle, hint); 758 XFree(hint); 759 } 760 761 // Announce support for Xdnd (drag and drop) 762 { 763 const Atom version = _GLFW_XDND_VERSION; 764 XChangeProperty(_glfw.x11.display, window->x11.handle, 765 _glfw.x11.XdndAware, XA_ATOM, 32, 766 PropModeReplace, (unsigned char*) &version, 1); 767 } 768 769 _glfwPlatformSetWindowTitle(window, wndconfig->title); 770 771 if (_glfw.x11.im) 772 { 773 window->x11.ic = XCreateIC(_glfw.x11.im, 774 XNInputStyle, 775 XIMPreeditNothing | XIMStatusNothing, 776 XNClientWindow, 777 window->x11.handle, 778 XNFocusWindow, 779 window->x11.handle, 780 NULL); 781 } 782 783 if (window->x11.ic) 784 { 785 unsigned long filter = 0; 786 if (XGetICValues(window->x11.ic, XNFilterEvents, &filter, NULL) == NULL) 787 XSelectInput(_glfw.x11.display, window->x11.handle, wa.event_mask | filter); 788 } 789 790 _glfwPlatformGetWindowPos(window, &window->x11.xpos, &window->x11.ypos); 791 _glfwPlatformGetWindowSize(window, &window->x11.width, &window->x11.height); 792 793 return GLFW_TRUE; 794 } 795 796 // Set the specified property to the selection converted to the requested target 797 // 798 static Atom writeTargetToProperty(const XSelectionRequestEvent* request) 799 { 800 int i; 801 char* selectionString = NULL; 802 const Atom formats[] = { _glfw.x11.UTF8_STRING, XA_STRING }; 803 const int formatCount = sizeof(formats) / sizeof(formats[0]); 804 805 if (request->selection == _glfw.x11.PRIMARY) 806 selectionString = _glfw.x11.primarySelectionString; 807 else 808 selectionString = _glfw.x11.clipboardString; 809 810 if (request->property == None) 811 { 812 // The requester is a legacy client (ICCCM section 2.2) 813 // We don't support legacy clients, so fail here 814 return None; 815 } 816 817 if (request->target == _glfw.x11.TARGETS) 818 { 819 // The list of supported targets was requested 820 821 const Atom targets[] = { _glfw.x11.TARGETS, 822 _glfw.x11.MULTIPLE, 823 _glfw.x11.UTF8_STRING, 824 XA_STRING }; 825 826 XChangeProperty(_glfw.x11.display, 827 request->requestor, 828 request->property, 829 XA_ATOM, 830 32, 831 PropModeReplace, 832 (unsigned char*) targets, 833 sizeof(targets) / sizeof(targets[0])); 834 835 return request->property; 836 } 837 838 if (request->target == _glfw.x11.MULTIPLE) 839 { 840 // Multiple conversions were requested 841 842 Atom* targets; 843 unsigned long i, count; 844 845 count = _glfwGetWindowPropertyX11(request->requestor, 846 request->property, 847 _glfw.x11.ATOM_PAIR, 848 (unsigned char**) &targets); 849 850 for (i = 0; i < count; i += 2) 851 { 852 int j; 853 854 for (j = 0; j < formatCount; j++) 855 { 856 if (targets[i] == formats[j]) 857 break; 858 } 859 860 if (j < formatCount) 861 { 862 XChangeProperty(_glfw.x11.display, 863 request->requestor, 864 targets[i + 1], 865 targets[i], 866 8, 867 PropModeReplace, 868 (unsigned char *) selectionString, 869 strlen(selectionString)); 870 } 871 else 872 targets[i + 1] = None; 873 } 874 875 XChangeProperty(_glfw.x11.display, 876 request->requestor, 877 request->property, 878 _glfw.x11.ATOM_PAIR, 879 32, 880 PropModeReplace, 881 (unsigned char*) targets, 882 count); 883 884 XFree(targets); 885 886 return request->property; 887 } 888 889 if (request->target == _glfw.x11.SAVE_TARGETS) 890 { 891 // The request is a check whether we support SAVE_TARGETS 892 // It should be handled as a no-op side effect target 893 894 XChangeProperty(_glfw.x11.display, 895 request->requestor, 896 request->property, 897 _glfw.x11.NULL_, 898 32, 899 PropModeReplace, 900 NULL, 901 0); 902 903 return request->property; 904 } 905 906 // Conversion to a data target was requested 907 908 for (i = 0; i < formatCount; i++) 909 { 910 if (request->target == formats[i]) 911 { 912 // The requested target is one we support 913 914 XChangeProperty(_glfw.x11.display, 915 request->requestor, 916 request->property, 917 request->target, 918 8, 919 PropModeReplace, 920 (unsigned char *) selectionString, 921 strlen(selectionString)); 922 923 return request->property; 924 } 925 } 926 927 // The requested target is not supported 928 929 return None; 930 } 931 932 static void handleSelectionRequest(XEvent* event) 933 { 934 const XSelectionRequestEvent* request = &event->xselectionrequest; 935 936 XEvent reply = { SelectionNotify }; 937 reply.xselection.property = writeTargetToProperty(request); 938 reply.xselection.display = request->display; 939 reply.xselection.requestor = request->requestor; 940 reply.xselection.selection = request->selection; 941 reply.xselection.target = request->target; 942 reply.xselection.time = request->time; 943 944 XSendEvent(_glfw.x11.display, request->requestor, False, 0, &reply); 945 } 946 947 static const char* getSelectionString(Atom selection) 948 { 949 char** selectionString = NULL; 950 const Atom targets[] = { _glfw.x11.UTF8_STRING, XA_STRING }; 951 const size_t targetCount = sizeof(targets) / sizeof(targets[0]); 952 953 if (selection == _glfw.x11.PRIMARY) 954 selectionString = &_glfw.x11.primarySelectionString; 955 else 956 selectionString = &_glfw.x11.clipboardString; 957 958 if (XGetSelectionOwner(_glfw.x11.display, selection) == 959 _glfw.x11.helperWindowHandle) 960 { 961 // Instead of doing a large number of X round-trips just to put this 962 // string into a window property and then read it back, just return it 963 return *selectionString; 964 } 965 966 free(*selectionString); 967 *selectionString = NULL; 968 969 for (size_t i = 0; i < targetCount; i++) 970 { 971 char* data; 972 Atom actualType; 973 int actualFormat; 974 unsigned long itemCount, bytesAfter; 975 XEvent notification, dummy; 976 977 XConvertSelection(_glfw.x11.display, 978 selection, 979 targets[i], 980 _glfw.x11.GLFW_SELECTION, 981 _glfw.x11.helperWindowHandle, 982 CurrentTime); 983 984 while (!XCheckTypedWindowEvent(_glfw.x11.display, 985 _glfw.x11.helperWindowHandle, 986 SelectionNotify, 987 ¬ification)) 988 { 989 waitForX11Event(NULL); 990 } 991 992 if (notification.xselection.property == None) 993 continue; 994 995 XCheckIfEvent(_glfw.x11.display, 996 &dummy, 997 isSelPropNewValueNotify, 998 (XPointer) ¬ification); 999 1000 XGetWindowProperty(_glfw.x11.display, 1001 notification.xselection.requestor, 1002 notification.xselection.property, 1003 0, 1004 LONG_MAX, 1005 True, 1006 AnyPropertyType, 1007 &actualType, 1008 &actualFormat, 1009 &itemCount, 1010 &bytesAfter, 1011 (unsigned char**) &data); 1012 1013 if (actualType == _glfw.x11.INCR) 1014 { 1015 size_t size = 1; 1016 char* string = NULL; 1017 1018 for (;;) 1019 { 1020 while (!XCheckIfEvent(_glfw.x11.display, 1021 &dummy, 1022 isSelPropNewValueNotify, 1023 (XPointer) ¬ification)) 1024 { 1025 waitForX11Event(NULL); 1026 } 1027 1028 XFree(data); 1029 XGetWindowProperty(_glfw.x11.display, 1030 notification.xselection.requestor, 1031 notification.xselection.property, 1032 0, 1033 LONG_MAX, 1034 True, 1035 AnyPropertyType, 1036 &actualType, 1037 &actualFormat, 1038 &itemCount, 1039 &bytesAfter, 1040 (unsigned char**) &data); 1041 1042 if (itemCount) 1043 { 1044 size += itemCount; 1045 string = realloc(string, size); 1046 string[size - itemCount - 1] = '\0'; 1047 strcat(string, data); 1048 } 1049 1050 if (!itemCount) 1051 { 1052 if (string) 1053 { 1054 if (targets[i] == XA_STRING) 1055 { 1056 *selectionString = convertLatin1toUTF8(string); 1057 free(string); 1058 } 1059 else 1060 *selectionString = string; 1061 } 1062 1063 break; 1064 } 1065 } 1066 } 1067 else if (actualType == targets[i]) 1068 { 1069 if (targets[i] == XA_STRING) 1070 *selectionString = convertLatin1toUTF8(data); 1071 else 1072 *selectionString = _glfw_strdup(data); 1073 } 1074 1075 XFree(data); 1076 1077 if (*selectionString) 1078 break; 1079 } 1080 1081 if (!*selectionString) 1082 { 1083 _glfwInputError(GLFW_FORMAT_UNAVAILABLE, 1084 "X11: Failed to convert selection to string"); 1085 } 1086 1087 return *selectionString; 1088 } 1089 1090 // Make the specified window and its video mode active on its monitor 1091 // 1092 static void acquireMonitor(_GLFWwindow* window) 1093 { 1094 if (_glfw.x11.saver.count == 0) 1095 { 1096 // Remember old screen saver settings 1097 XGetScreenSaver(_glfw.x11.display, 1098 &_glfw.x11.saver.timeout, 1099 &_glfw.x11.saver.interval, 1100 &_glfw.x11.saver.blanking, 1101 &_glfw.x11.saver.exposure); 1102 1103 // Disable screen saver 1104 XSetScreenSaver(_glfw.x11.display, 0, 0, DontPreferBlanking, 1105 DefaultExposures); 1106 } 1107 1108 if (!window->monitor->window) 1109 _glfw.x11.saver.count++; 1110 1111 _glfwSetVideoModeX11(window->monitor, &window->videoMode); 1112 1113 if (window->x11.overrideRedirect) 1114 { 1115 int xpos, ypos; 1116 GLFWvidmode mode; 1117 1118 // Manually position the window over its monitor 1119 _glfwPlatformGetMonitorPos(window->monitor, &xpos, &ypos); 1120 _glfwPlatformGetVideoMode(window->monitor, &mode); 1121 1122 XMoveResizeWindow(_glfw.x11.display, window->x11.handle, 1123 xpos, ypos, mode.width, mode.height); 1124 } 1125 1126 _glfwInputMonitorWindow(window->monitor, window); 1127 } 1128 1129 // Remove the window and restore the original video mode 1130 // 1131 static void releaseMonitor(_GLFWwindow* window) 1132 { 1133 if (window->monitor->window != window) 1134 return; 1135 1136 _glfwInputMonitorWindow(window->monitor, NULL); 1137 _glfwRestoreVideoModeX11(window->monitor); 1138 1139 _glfw.x11.saver.count--; 1140 1141 if (_glfw.x11.saver.count == 0) 1142 { 1143 // Restore old screen saver settings 1144 XSetScreenSaver(_glfw.x11.display, 1145 _glfw.x11.saver.timeout, 1146 _glfw.x11.saver.interval, 1147 _glfw.x11.saver.blanking, 1148 _glfw.x11.saver.exposure); 1149 } 1150 } 1151 1152 // Process the specified X event 1153 // 1154 static void processEvent(XEvent *event) 1155 { 1156 int keycode = 0; 1157 Bool filtered = False; 1158 1159 // HACK: Save scancode as some IMs clear the field in XFilterEvent 1160 if (event->type == KeyPress || event->type == KeyRelease) 1161 keycode = event->xkey.keycode; 1162 1163 if (_glfw.x11.im) 1164 filtered = XFilterEvent(event, None); 1165 1166 if (_glfw.x11.randr.available) 1167 { 1168 if (event->type == _glfw.x11.randr.eventBase + RRNotify) 1169 { 1170 XRRUpdateConfiguration(event); 1171 _glfwPollMonitorsX11(); 1172 return; 1173 } 1174 } 1175 1176 if (_glfw.x11.xkb.available) 1177 { 1178 if (event->type == _glfw.x11.xkb.eventBase + XkbEventCode) 1179 { 1180 if (((XkbEvent*) event)->any.xkb_type == XkbStateNotify && 1181 (((XkbEvent*) event)->state.changed & XkbGroupStateMask)) 1182 { 1183 _glfw.x11.xkb.group = ((XkbEvent*) event)->state.group; 1184 } 1185 1186 return; 1187 } 1188 } 1189 1190 if (event->type == GenericEvent) 1191 { 1192 if (_glfw.x11.xi.available) 1193 { 1194 _GLFWwindow* window = _glfw.x11.disabledCursorWindow; 1195 1196 if (window && 1197 window->rawMouseMotion && 1198 event->xcookie.extension == _glfw.x11.xi.majorOpcode && 1199 XGetEventData(_glfw.x11.display, &event->xcookie) && 1200 event->xcookie.evtype == XI_RawMotion) 1201 { 1202 XIRawEvent* re = event->xcookie.data; 1203 if (re->valuators.mask_len) 1204 { 1205 const double* values = re->raw_values; 1206 double xpos = window->virtualCursorPosX; 1207 double ypos = window->virtualCursorPosY; 1208 1209 if (XIMaskIsSet(re->valuators.mask, 0)) 1210 { 1211 xpos += *values; 1212 values++; 1213 } 1214 1215 if (XIMaskIsSet(re->valuators.mask, 1)) 1216 ypos += *values; 1217 1218 _glfwInputCursorPos(window, xpos, ypos); 1219 } 1220 } 1221 1222 XFreeEventData(_glfw.x11.display, &event->xcookie); 1223 } 1224 1225 return; 1226 } 1227 1228 if (event->type == SelectionRequest) 1229 { 1230 handleSelectionRequest(event); 1231 return; 1232 } 1233 1234 _GLFWwindow* window = NULL; 1235 if (XFindContext(_glfw.x11.display, 1236 event->xany.window, 1237 _glfw.x11.context, 1238 (XPointer*) &window) != 0) 1239 { 1240 // This is an event for a window that has already been destroyed 1241 return; 1242 } 1243 1244 switch (event->type) 1245 { 1246 case ReparentNotify: 1247 { 1248 window->x11.parent = event->xreparent.parent; 1249 return; 1250 } 1251 1252 case KeyPress: 1253 { 1254 const int key = translateKey(keycode); 1255 const int mods = translateState(event->xkey.state); 1256 const int plain = !(mods & (GLFW_MOD_CONTROL | GLFW_MOD_ALT)); 1257 1258 if (window->x11.ic) 1259 { 1260 // HACK: Do not report the key press events duplicated by XIM 1261 // Duplicate key releases are filtered out implicitly by 1262 // the GLFW key repeat logic in _glfwInputKey 1263 // A timestamp per key is used to handle simultaneous keys 1264 // NOTE: Always allow the first event for each key through 1265 // (the server never sends a timestamp of zero) 1266 // NOTE: Timestamp difference is compared to handle wrap-around 1267 Time diff = event->xkey.time - window->x11.keyPressTimes[keycode]; 1268 if (diff == event->xkey.time || (diff > 0 && diff < ((Time)1 << 31))) 1269 { 1270 if (keycode) 1271 _glfwInputKey(window, key, keycode, GLFW_PRESS, mods); 1272 1273 window->x11.keyPressTimes[keycode] = event->xkey.time; 1274 } 1275 1276 if (!filtered) 1277 { 1278 int count; 1279 Status status; 1280 #if defined(X_HAVE_UTF8_STRING) 1281 char buffer[100]; 1282 char* chars = buffer; 1283 1284 count = Xutf8LookupString(window->x11.ic, 1285 &event->xkey, 1286 buffer, sizeof(buffer) - 1, 1287 NULL, &status); 1288 1289 if (status == XBufferOverflow) 1290 { 1291 chars = calloc(count + 1, 1); 1292 count = Xutf8LookupString(window->x11.ic, 1293 &event->xkey, 1294 chars, count, 1295 NULL, &status); 1296 } 1297 1298 if (status == XLookupChars || status == XLookupBoth) 1299 { 1300 const char* c = chars; 1301 chars[count] = '\0'; 1302 while (c - chars < count) 1303 _glfwInputChar(window, decodeUTF8(&c), mods, plain); 1304 } 1305 #else /*X_HAVE_UTF8_STRING*/ 1306 wchar_t buffer[16]; 1307 wchar_t* chars = buffer; 1308 1309 count = XwcLookupString(window->x11.ic, 1310 &event->xkey, 1311 buffer, 1312 sizeof(buffer) / sizeof(wchar_t), 1313 NULL, 1314 &status); 1315 1316 if (status == XBufferOverflow) 1317 { 1318 chars = calloc(count, sizeof(wchar_t)); 1319 count = XwcLookupString(window->x11.ic, 1320 &event->xkey, 1321 chars, count, 1322 NULL, &status); 1323 } 1324 1325 if (status == XLookupChars || status == XLookupBoth) 1326 { 1327 int i; 1328 for (i = 0; i < count; i++) 1329 _glfwInputChar(window, chars[i], mods, plain); 1330 } 1331 #endif /*X_HAVE_UTF8_STRING*/ 1332 1333 if (chars != buffer) 1334 free(chars); 1335 } 1336 } 1337 else 1338 { 1339 KeySym keysym; 1340 XLookupString(&event->xkey, NULL, 0, &keysym, NULL); 1341 1342 _glfwInputKey(window, key, keycode, GLFW_PRESS, mods); 1343 1344 const uint32_t codepoint = _glfwKeySym2Unicode(keysym); 1345 if (codepoint != GLFW_INVALID_CODEPOINT) 1346 _glfwInputChar(window, codepoint, mods, plain); 1347 } 1348 1349 return; 1350 } 1351 1352 case KeyRelease: 1353 { 1354 const int key = translateKey(keycode); 1355 const int mods = translateState(event->xkey.state); 1356 1357 if (!_glfw.x11.xkb.detectable) 1358 { 1359 // HACK: Key repeat events will arrive as KeyRelease/KeyPress 1360 // pairs with similar or identical time stamps 1361 // The key repeat logic in _glfwInputKey expects only key 1362 // presses to repeat, so detect and discard release events 1363 if (XEventsQueued(_glfw.x11.display, QueuedAfterReading)) 1364 { 1365 XEvent next; 1366 XPeekEvent(_glfw.x11.display, &next); 1367 1368 if (next.type == KeyPress && 1369 next.xkey.window == event->xkey.window && 1370 next.xkey.keycode == keycode) 1371 { 1372 // HACK: The time of repeat events sometimes doesn't 1373 // match that of the press event, so add an 1374 // epsilon 1375 // Toshiyuki Takahashi can press a button 1376 // 16 times per second so it's fairly safe to 1377 // assume that no human is pressing the key 50 1378 // times per second (value is ms) 1379 if ((next.xkey.time - event->xkey.time) < 20) 1380 { 1381 // This is very likely a server-generated key repeat 1382 // event, so ignore it 1383 return; 1384 } 1385 } 1386 } 1387 } 1388 1389 _glfwInputKey(window, key, keycode, GLFW_RELEASE, mods); 1390 return; 1391 } 1392 1393 case ButtonPress: 1394 { 1395 const int mods = translateState(event->xbutton.state); 1396 1397 if (event->xbutton.button == Button1) 1398 _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_LEFT, GLFW_PRESS, mods); 1399 else if (event->xbutton.button == Button2) 1400 _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_MIDDLE, GLFW_PRESS, mods); 1401 else if (event->xbutton.button == Button3) 1402 _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, mods); 1403 1404 // Modern X provides scroll events as mouse button presses 1405 else if (event->xbutton.button == Button4) 1406 _glfwInputScroll(window, 0.0, 1.0); 1407 else if (event->xbutton.button == Button5) 1408 _glfwInputScroll(window, 0.0, -1.0); 1409 else if (event->xbutton.button == Button6) 1410 _glfwInputScroll(window, 1.0, 0.0); 1411 else if (event->xbutton.button == Button7) 1412 _glfwInputScroll(window, -1.0, 0.0); 1413 1414 else 1415 { 1416 // Additional buttons after 7 are treated as regular buttons 1417 // We subtract 4 to fill the gap left by scroll input above 1418 _glfwInputMouseClick(window, 1419 event->xbutton.button - Button1 - 4, 1420 GLFW_PRESS, 1421 mods); 1422 } 1423 1424 return; 1425 } 1426 1427 case ButtonRelease: 1428 { 1429 const int mods = translateState(event->xbutton.state); 1430 1431 if (event->xbutton.button == Button1) 1432 { 1433 _glfwInputMouseClick(window, 1434 GLFW_MOUSE_BUTTON_LEFT, 1435 GLFW_RELEASE, 1436 mods); 1437 } 1438 else if (event->xbutton.button == Button2) 1439 { 1440 _glfwInputMouseClick(window, 1441 GLFW_MOUSE_BUTTON_MIDDLE, 1442 GLFW_RELEASE, 1443 mods); 1444 } 1445 else if (event->xbutton.button == Button3) 1446 { 1447 _glfwInputMouseClick(window, 1448 GLFW_MOUSE_BUTTON_RIGHT, 1449 GLFW_RELEASE, 1450 mods); 1451 } 1452 else if (event->xbutton.button > Button7) 1453 { 1454 // Additional buttons after 7 are treated as regular buttons 1455 // We subtract 4 to fill the gap left by scroll input above 1456 _glfwInputMouseClick(window, 1457 event->xbutton.button - Button1 - 4, 1458 GLFW_RELEASE, 1459 mods); 1460 } 1461 1462 return; 1463 } 1464 1465 case EnterNotify: 1466 { 1467 // XEnterWindowEvent is XCrossingEvent 1468 const int x = event->xcrossing.x; 1469 const int y = event->xcrossing.y; 1470 1471 // HACK: This is a workaround for WMs (KWM, Fluxbox) that otherwise 1472 // ignore the defined cursor for hidden cursor mode 1473 if (window->cursorMode == GLFW_CURSOR_HIDDEN) 1474 updateCursorImage(window); 1475 1476 _glfwInputCursorEnter(window, GLFW_TRUE); 1477 _glfwInputCursorPos(window, x, y); 1478 1479 window->x11.lastCursorPosX = x; 1480 window->x11.lastCursorPosY = y; 1481 return; 1482 } 1483 1484 case LeaveNotify: 1485 { 1486 _glfwInputCursorEnter(window, GLFW_FALSE); 1487 return; 1488 } 1489 1490 case MotionNotify: 1491 { 1492 const int x = event->xmotion.x; 1493 const int y = event->xmotion.y; 1494 1495 if (x != window->x11.warpCursorPosX || 1496 y != window->x11.warpCursorPosY) 1497 { 1498 // The cursor was moved by something other than GLFW 1499 1500 if (window->cursorMode == GLFW_CURSOR_DISABLED) 1501 { 1502 if (_glfw.x11.disabledCursorWindow != window) 1503 return; 1504 if (window->rawMouseMotion) 1505 return; 1506 1507 const int dx = x - window->x11.lastCursorPosX; 1508 const int dy = y - window->x11.lastCursorPosY; 1509 1510 _glfwInputCursorPos(window, 1511 window->virtualCursorPosX + dx, 1512 window->virtualCursorPosY + dy); 1513 } 1514 else 1515 _glfwInputCursorPos(window, x, y); 1516 } 1517 1518 window->x11.lastCursorPosX = x; 1519 window->x11.lastCursorPosY = y; 1520 return; 1521 } 1522 1523 case ConfigureNotify: 1524 { 1525 if (event->xconfigure.width != window->x11.width || 1526 event->xconfigure.height != window->x11.height) 1527 { 1528 _glfwInputFramebufferSize(window, 1529 event->xconfigure.width, 1530 event->xconfigure.height); 1531 1532 _glfwInputWindowSize(window, 1533 event->xconfigure.width, 1534 event->xconfigure.height); 1535 1536 window->x11.width = event->xconfigure.width; 1537 window->x11.height = event->xconfigure.height; 1538 } 1539 1540 int xpos = event->xconfigure.x; 1541 int ypos = event->xconfigure.y; 1542 1543 // NOTE: ConfigureNotify events from the server are in local 1544 // coordinates, so if we are reparented we need to translate 1545 // the position into root (screen) coordinates 1546 if (!event->xany.send_event && window->x11.parent != _glfw.x11.root) 1547 { 1548 _glfwGrabErrorHandlerX11(); 1549 1550 Window dummy; 1551 XTranslateCoordinates(_glfw.x11.display, 1552 window->x11.parent, 1553 _glfw.x11.root, 1554 xpos, ypos, 1555 &xpos, &ypos, 1556 &dummy); 1557 1558 _glfwReleaseErrorHandlerX11(); 1559 if (_glfw.x11.errorCode == BadWindow) 1560 return; 1561 } 1562 1563 if (xpos != window->x11.xpos || ypos != window->x11.ypos) 1564 { 1565 _glfwInputWindowPos(window, xpos, ypos); 1566 window->x11.xpos = xpos; 1567 window->x11.ypos = ypos; 1568 } 1569 1570 return; 1571 } 1572 1573 case ClientMessage: 1574 { 1575 // Custom client message, probably from the window manager 1576 1577 if (filtered) 1578 return; 1579 1580 if (event->xclient.message_type == None) 1581 return; 1582 1583 if (event->xclient.message_type == _glfw.x11.WM_PROTOCOLS) 1584 { 1585 const Atom protocol = event->xclient.data.l[0]; 1586 if (protocol == None) 1587 return; 1588 1589 if (protocol == _glfw.x11.WM_DELETE_WINDOW) 1590 { 1591 // The window manager was asked to close the window, for 1592 // example by the user pressing a 'close' window decoration 1593 // button 1594 _glfwInputWindowCloseRequest(window); 1595 } 1596 else if (protocol == _glfw.x11.NET_WM_PING) 1597 { 1598 // The window manager is pinging the application to ensure 1599 // it's still responding to events 1600 1601 XEvent reply = *event; 1602 reply.xclient.window = _glfw.x11.root; 1603 1604 XSendEvent(_glfw.x11.display, _glfw.x11.root, 1605 False, 1606 SubstructureNotifyMask | SubstructureRedirectMask, 1607 &reply); 1608 } 1609 } 1610 else if (event->xclient.message_type == _glfw.x11.XdndEnter) 1611 { 1612 // A drag operation has entered the window 1613 unsigned long i, count; 1614 Atom* formats = NULL; 1615 const GLFWbool list = event->xclient.data.l[1] & 1; 1616 1617 _glfw.x11.xdnd.source = event->xclient.data.l[0]; 1618 _glfw.x11.xdnd.version = event->xclient.data.l[1] >> 24; 1619 _glfw.x11.xdnd.format = None; 1620 1621 if (_glfw.x11.xdnd.version > _GLFW_XDND_VERSION) 1622 return; 1623 1624 if (list) 1625 { 1626 count = _glfwGetWindowPropertyX11(_glfw.x11.xdnd.source, 1627 _glfw.x11.XdndTypeList, 1628 XA_ATOM, 1629 (unsigned char**) &formats); 1630 } 1631 else 1632 { 1633 count = 3; 1634 formats = (Atom*) event->xclient.data.l + 2; 1635 } 1636 1637 for (i = 0; i < count; i++) 1638 { 1639 if (formats[i] == _glfw.x11.text_uri_list) 1640 { 1641 _glfw.x11.xdnd.format = _glfw.x11.text_uri_list; 1642 break; 1643 } 1644 } 1645 1646 if (list && formats) 1647 XFree(formats); 1648 } 1649 else if (event->xclient.message_type == _glfw.x11.XdndDrop) 1650 { 1651 // The drag operation has finished by dropping on the window 1652 Time time = CurrentTime; 1653 1654 if (_glfw.x11.xdnd.version > _GLFW_XDND_VERSION) 1655 return; 1656 1657 if (_glfw.x11.xdnd.format) 1658 { 1659 if (_glfw.x11.xdnd.version >= 1) 1660 time = event->xclient.data.l[2]; 1661 1662 // Request the chosen format from the source window 1663 XConvertSelection(_glfw.x11.display, 1664 _glfw.x11.XdndSelection, 1665 _glfw.x11.xdnd.format, 1666 _glfw.x11.XdndSelection, 1667 window->x11.handle, 1668 time); 1669 } 1670 else if (_glfw.x11.xdnd.version >= 2) 1671 { 1672 XEvent reply = { ClientMessage }; 1673 reply.xclient.window = _glfw.x11.xdnd.source; 1674 reply.xclient.message_type = _glfw.x11.XdndFinished; 1675 reply.xclient.format = 32; 1676 reply.xclient.data.l[0] = window->x11.handle; 1677 reply.xclient.data.l[1] = 0; // The drag was rejected 1678 reply.xclient.data.l[2] = None; 1679 1680 XSendEvent(_glfw.x11.display, _glfw.x11.xdnd.source, 1681 False, NoEventMask, &reply); 1682 XFlush(_glfw.x11.display); 1683 } 1684 } 1685 else if (event->xclient.message_type == _glfw.x11.XdndPosition) 1686 { 1687 // The drag operation has moved over the window 1688 const int xabs = (event->xclient.data.l[2] >> 16) & 0xffff; 1689 const int yabs = (event->xclient.data.l[2]) & 0xffff; 1690 Window dummy; 1691 int xpos, ypos; 1692 1693 if (_glfw.x11.xdnd.version > _GLFW_XDND_VERSION) 1694 return; 1695 1696 XTranslateCoordinates(_glfw.x11.display, 1697 _glfw.x11.root, 1698 window->x11.handle, 1699 xabs, yabs, 1700 &xpos, &ypos, 1701 &dummy); 1702 1703 _glfwInputCursorPos(window, xpos, ypos); 1704 1705 XEvent reply = { ClientMessage }; 1706 reply.xclient.window = _glfw.x11.xdnd.source; 1707 reply.xclient.message_type = _glfw.x11.XdndStatus; 1708 reply.xclient.format = 32; 1709 reply.xclient.data.l[0] = window->x11.handle; 1710 reply.xclient.data.l[2] = 0; // Specify an empty rectangle 1711 reply.xclient.data.l[3] = 0; 1712 1713 if (_glfw.x11.xdnd.format) 1714 { 1715 // Reply that we are ready to copy the dragged data 1716 reply.xclient.data.l[1] = 1; // Accept with no rectangle 1717 if (_glfw.x11.xdnd.version >= 2) 1718 reply.xclient.data.l[4] = _glfw.x11.XdndActionCopy; 1719 } 1720 1721 XSendEvent(_glfw.x11.display, _glfw.x11.xdnd.source, 1722 False, NoEventMask, &reply); 1723 XFlush(_glfw.x11.display); 1724 } 1725 1726 return; 1727 } 1728 1729 case SelectionNotify: 1730 { 1731 if (event->xselection.property == _glfw.x11.XdndSelection) 1732 { 1733 // The converted data from the drag operation has arrived 1734 char* data; 1735 const unsigned long result = 1736 _glfwGetWindowPropertyX11(event->xselection.requestor, 1737 event->xselection.property, 1738 event->xselection.target, 1739 (unsigned char**) &data); 1740 1741 if (result) 1742 { 1743 int i, count; 1744 char** paths = _glfwParseUriList(data, &count); 1745 1746 _glfwInputDrop(window, count, (const char**) paths); 1747 1748 for (i = 0; i < count; i++) 1749 free(paths[i]); 1750 free(paths); 1751 } 1752 1753 if (data) 1754 XFree(data); 1755 1756 if (_glfw.x11.xdnd.version >= 2) 1757 { 1758 XEvent reply = { ClientMessage }; 1759 reply.xclient.window = _glfw.x11.xdnd.source; 1760 reply.xclient.message_type = _glfw.x11.XdndFinished; 1761 reply.xclient.format = 32; 1762 reply.xclient.data.l[0] = window->x11.handle; 1763 reply.xclient.data.l[1] = result; 1764 reply.xclient.data.l[2] = _glfw.x11.XdndActionCopy; 1765 1766 XSendEvent(_glfw.x11.display, _glfw.x11.xdnd.source, 1767 False, NoEventMask, &reply); 1768 XFlush(_glfw.x11.display); 1769 } 1770 } 1771 1772 return; 1773 } 1774 1775 case FocusIn: 1776 { 1777 if (event->xfocus.mode == NotifyGrab || 1778 event->xfocus.mode == NotifyUngrab) 1779 { 1780 // Ignore focus events from popup indicator windows, window menu 1781 // key chords and window dragging 1782 return; 1783 } 1784 1785 if (window->cursorMode == GLFW_CURSOR_DISABLED) 1786 disableCursor(window); 1787 1788 if (window->x11.ic) 1789 XSetICFocus(window->x11.ic); 1790 1791 _glfwInputWindowFocus(window, GLFW_TRUE); 1792 return; 1793 } 1794 1795 case FocusOut: 1796 { 1797 if (event->xfocus.mode == NotifyGrab || 1798 event->xfocus.mode == NotifyUngrab) 1799 { 1800 // Ignore focus events from popup indicator windows, window menu 1801 // key chords and window dragging 1802 return; 1803 } 1804 1805 if (window->cursorMode == GLFW_CURSOR_DISABLED) 1806 enableCursor(window); 1807 1808 if (window->x11.ic) 1809 XUnsetICFocus(window->x11.ic); 1810 1811 if (window->monitor && window->autoIconify) 1812 _glfwPlatformIconifyWindow(window); 1813 1814 _glfwInputWindowFocus(window, GLFW_FALSE); 1815 return; 1816 } 1817 1818 case Expose: 1819 { 1820 _glfwInputWindowDamage(window); 1821 return; 1822 } 1823 1824 case PropertyNotify: 1825 { 1826 if (event->xproperty.state != PropertyNewValue) 1827 return; 1828 1829 if (event->xproperty.atom == _glfw.x11.WM_STATE) 1830 { 1831 const int state = getWindowState(window); 1832 if (state != IconicState && state != NormalState) 1833 return; 1834 1835 const GLFWbool iconified = (state == IconicState); 1836 if (window->x11.iconified != iconified) 1837 { 1838 if (window->monitor) 1839 { 1840 if (iconified) 1841 releaseMonitor(window); 1842 else 1843 acquireMonitor(window); 1844 } 1845 1846 window->x11.iconified = iconified; 1847 _glfwInputWindowIconify(window, iconified); 1848 } 1849 } 1850 else if (event->xproperty.atom == _glfw.x11.NET_WM_STATE) 1851 { 1852 const GLFWbool maximized = _glfwPlatformWindowMaximized(window); 1853 if (window->x11.maximized != maximized) 1854 { 1855 window->x11.maximized = maximized; 1856 _glfwInputWindowMaximize(window, maximized); 1857 } 1858 } 1859 1860 return; 1861 } 1862 1863 case DestroyNotify: 1864 return; 1865 } 1866 } 1867 1868 1869 ////////////////////////////////////////////////////////////////////////// 1870 ////// GLFW internal API ////// 1871 ////////////////////////////////////////////////////////////////////////// 1872 1873 // Retrieve a single window property of the specified type 1874 // Inspired by fghGetWindowProperty from freeglut 1875 // 1876 unsigned long _glfwGetWindowPropertyX11(Window window, 1877 Atom property, 1878 Atom type, 1879 unsigned char** value) 1880 { 1881 Atom actualType; 1882 int actualFormat; 1883 unsigned long itemCount, bytesAfter; 1884 1885 XGetWindowProperty(_glfw.x11.display, 1886 window, 1887 property, 1888 0, 1889 LONG_MAX, 1890 False, 1891 type, 1892 &actualType, 1893 &actualFormat, 1894 &itemCount, 1895 &bytesAfter, 1896 value); 1897 1898 return itemCount; 1899 } 1900 1901 GLFWbool _glfwIsVisualTransparentX11(Visual* visual) 1902 { 1903 if (!_glfw.x11.xrender.available) 1904 return GLFW_FALSE; 1905 1906 XRenderPictFormat* pf = XRenderFindVisualFormat(_glfw.x11.display, visual); 1907 return pf && pf->direct.alphaMask; 1908 } 1909 1910 // Push contents of our selection to clipboard manager 1911 // 1912 void _glfwPushSelectionToManagerX11(void) 1913 { 1914 XConvertSelection(_glfw.x11.display, 1915 _glfw.x11.CLIPBOARD_MANAGER, 1916 _glfw.x11.SAVE_TARGETS, 1917 None, 1918 _glfw.x11.helperWindowHandle, 1919 CurrentTime); 1920 1921 for (;;) 1922 { 1923 XEvent event; 1924 1925 while (XCheckIfEvent(_glfw.x11.display, &event, isSelectionEvent, NULL)) 1926 { 1927 switch (event.type) 1928 { 1929 case SelectionRequest: 1930 handleSelectionRequest(&event); 1931 break; 1932 1933 case SelectionNotify: 1934 { 1935 if (event.xselection.target == _glfw.x11.SAVE_TARGETS) 1936 { 1937 // This means one of two things; either the selection 1938 // was not owned, which means there is no clipboard 1939 // manager, or the transfer to the clipboard manager has 1940 // completed 1941 // In either case, it means we are done here 1942 return; 1943 } 1944 1945 break; 1946 } 1947 } 1948 } 1949 1950 waitForX11Event(NULL); 1951 } 1952 } 1953 1954 1955 ////////////////////////////////////////////////////////////////////////// 1956 ////// GLFW platform API ////// 1957 ////////////////////////////////////////////////////////////////////////// 1958 1959 int _glfwPlatformCreateWindow(_GLFWwindow* window, 1960 const _GLFWwndconfig* wndconfig, 1961 const _GLFWctxconfig* ctxconfig, 1962 const _GLFWfbconfig* fbconfig) 1963 { 1964 Visual* visual = NULL; 1965 int depth; 1966 1967 if (ctxconfig->client != GLFW_NO_API) 1968 { 1969 if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API) 1970 { 1971 if (!_glfwInitGLX()) 1972 return GLFW_FALSE; 1973 if (!_glfwChooseVisualGLX(wndconfig, ctxconfig, fbconfig, &visual, &depth)) 1974 return GLFW_FALSE; 1975 } 1976 else if (ctxconfig->source == GLFW_EGL_CONTEXT_API) 1977 { 1978 if (!_glfwInitEGL()) 1979 return GLFW_FALSE; 1980 if (!_glfwChooseVisualEGL(wndconfig, ctxconfig, fbconfig, &visual, &depth)) 1981 return GLFW_FALSE; 1982 } 1983 else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) 1984 { 1985 if (!_glfwInitOSMesa()) 1986 return GLFW_FALSE; 1987 } 1988 } 1989 1990 if (!visual) 1991 { 1992 visual = DefaultVisual(_glfw.x11.display, _glfw.x11.screen); 1993 depth = DefaultDepth(_glfw.x11.display, _glfw.x11.screen); 1994 } 1995 1996 if (!createNativeWindow(window, wndconfig, visual, depth)) 1997 return GLFW_FALSE; 1998 1999 if (ctxconfig->client != GLFW_NO_API) 2000 { 2001 if (ctxconfig->source == GLFW_NATIVE_CONTEXT_API) 2002 { 2003 if (!_glfwCreateContextGLX(window, ctxconfig, fbconfig)) 2004 return GLFW_FALSE; 2005 } 2006 else if (ctxconfig->source == GLFW_EGL_CONTEXT_API) 2007 { 2008 if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig)) 2009 return GLFW_FALSE; 2010 } 2011 else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) 2012 { 2013 if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) 2014 return GLFW_FALSE; 2015 } 2016 2017 if (!_glfwRefreshContextAttribs(window, ctxconfig)) 2018 return GLFW_FALSE; 2019 } 2020 2021 if (window->monitor) 2022 { 2023 _glfwPlatformShowWindow(window); 2024 updateWindowMode(window); 2025 acquireMonitor(window); 2026 2027 if (wndconfig->centerCursor) 2028 _glfwCenterCursorInContentArea(window); 2029 } 2030 else 2031 { 2032 if (wndconfig->visible) 2033 { 2034 _glfwPlatformShowWindow(window); 2035 if (wndconfig->focused) 2036 _glfwPlatformFocusWindow(window); 2037 } 2038 } 2039 2040 XFlush(_glfw.x11.display); 2041 return GLFW_TRUE; 2042 } 2043 2044 void _glfwPlatformDestroyWindow(_GLFWwindow* window) 2045 { 2046 if (_glfw.x11.disabledCursorWindow == window) 2047 _glfw.x11.disabledCursorWindow = NULL; 2048 2049 if (window->monitor) 2050 releaseMonitor(window); 2051 2052 if (window->x11.ic) 2053 { 2054 XDestroyIC(window->x11.ic); 2055 window->x11.ic = NULL; 2056 } 2057 2058 if (window->context.destroy) 2059 window->context.destroy(window); 2060 2061 if (window->x11.handle) 2062 { 2063 XDeleteContext(_glfw.x11.display, window->x11.handle, _glfw.x11.context); 2064 XUnmapWindow(_glfw.x11.display, window->x11.handle); 2065 XDestroyWindow(_glfw.x11.display, window->x11.handle); 2066 window->x11.handle = (Window) 0; 2067 } 2068 2069 if (window->x11.colormap) 2070 { 2071 XFreeColormap(_glfw.x11.display, window->x11.colormap); 2072 window->x11.colormap = (Colormap) 0; 2073 } 2074 2075 XFlush(_glfw.x11.display); 2076 } 2077 2078 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) 2079 { 2080 #if defined(X_HAVE_UTF8_STRING) 2081 Xutf8SetWMProperties(_glfw.x11.display, 2082 window->x11.handle, 2083 title, title, 2084 NULL, 0, 2085 NULL, NULL, NULL); 2086 #else 2087 // This may be a slightly better fallback than using XStoreName and 2088 // XSetIconName, which always store their arguments using STRING 2089 XmbSetWMProperties(_glfw.x11.display, 2090 window->x11.handle, 2091 title, title, 2092 NULL, 0, 2093 NULL, NULL, NULL); 2094 #endif 2095 2096 XChangeProperty(_glfw.x11.display, window->x11.handle, 2097 _glfw.x11.NET_WM_NAME, _glfw.x11.UTF8_STRING, 8, 2098 PropModeReplace, 2099 (unsigned char*) title, strlen(title)); 2100 2101 XChangeProperty(_glfw.x11.display, window->x11.handle, 2102 _glfw.x11.NET_WM_ICON_NAME, _glfw.x11.UTF8_STRING, 8, 2103 PropModeReplace, 2104 (unsigned char*) title, strlen(title)); 2105 2106 XFlush(_glfw.x11.display); 2107 } 2108 2109 void _glfwPlatformSetWindowIcon(_GLFWwindow* window, 2110 int count, const GLFWimage* images) 2111 { 2112 if (count) 2113 { 2114 int i, j, longCount = 0; 2115 2116 for (i = 0; i < count; i++) 2117 longCount += 2 + images[i].width * images[i].height; 2118 2119 unsigned long* icon = calloc(longCount, sizeof(unsigned long)); 2120 unsigned long* target = icon; 2121 2122 for (i = 0; i < count; i++) 2123 { 2124 *target++ = images[i].width; 2125 *target++ = images[i].height; 2126 2127 for (j = 0; j < images[i].width * images[i].height; j++) 2128 { 2129 *target++ = (((unsigned long) images[i].pixels[j * 4 + 0]) << 16) | 2130 (((unsigned long) images[i].pixels[j * 4 + 1]) << 8) | 2131 (((unsigned long) images[i].pixels[j * 4 + 2]) << 0) | 2132 (((unsigned long) images[i].pixels[j * 4 + 3]) << 24); 2133 } 2134 } 2135 2136 // NOTE: XChangeProperty expects 32-bit values like the image data above to be 2137 // placed in the 32 least significant bits of individual longs. This is 2138 // true even if long is 64-bit and a WM protocol calls for "packed" data. 2139 // This is because of a historical mistake that then became part of the Xlib 2140 // ABI. Xlib will pack these values into a regular array of 32-bit values 2141 // before sending it over the wire. 2142 XChangeProperty(_glfw.x11.display, window->x11.handle, 2143 _glfw.x11.NET_WM_ICON, 2144 XA_CARDINAL, 32, 2145 PropModeReplace, 2146 (unsigned char*) icon, 2147 longCount); 2148 2149 free(icon); 2150 } 2151 else 2152 { 2153 XDeleteProperty(_glfw.x11.display, window->x11.handle, 2154 _glfw.x11.NET_WM_ICON); 2155 } 2156 2157 XFlush(_glfw.x11.display); 2158 } 2159 2160 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos) 2161 { 2162 Window dummy; 2163 int x, y; 2164 2165 XTranslateCoordinates(_glfw.x11.display, window->x11.handle, _glfw.x11.root, 2166 0, 0, &x, &y, &dummy); 2167 2168 if (xpos) 2169 *xpos = x; 2170 if (ypos) 2171 *ypos = y; 2172 } 2173 2174 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) 2175 { 2176 // HACK: Explicitly setting PPosition to any value causes some WMs, notably 2177 // Compiz and Metacity, to honor the position of unmapped windows 2178 if (!_glfwPlatformWindowVisible(window)) 2179 { 2180 long supplied; 2181 XSizeHints* hints = XAllocSizeHints(); 2182 2183 if (XGetWMNormalHints(_glfw.x11.display, window->x11.handle, hints, &supplied)) 2184 { 2185 hints->flags |= PPosition; 2186 hints->x = hints->y = 0; 2187 2188 XSetWMNormalHints(_glfw.x11.display, window->x11.handle, hints); 2189 } 2190 2191 XFree(hints); 2192 } 2193 2194 XMoveWindow(_glfw.x11.display, window->x11.handle, xpos, ypos); 2195 XFlush(_glfw.x11.display); 2196 } 2197 2198 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) 2199 { 2200 XWindowAttributes attribs; 2201 XGetWindowAttributes(_glfw.x11.display, window->x11.handle, &attribs); 2202 2203 if (width) 2204 *width = attribs.width; 2205 if (height) 2206 *height = attribs.height; 2207 } 2208 2209 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) 2210 { 2211 if (window->monitor) 2212 { 2213 if (window->monitor->window == window) 2214 acquireMonitor(window); 2215 } 2216 else 2217 { 2218 if (!window->resizable) 2219 updateNormalHints(window, width, height); 2220 2221 XResizeWindow(_glfw.x11.display, window->x11.handle, width, height); 2222 } 2223 2224 XFlush(_glfw.x11.display); 2225 } 2226 2227 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, 2228 int minwidth, int minheight, 2229 int maxwidth, int maxheight) 2230 { 2231 int width, height; 2232 _glfwPlatformGetWindowSize(window, &width, &height); 2233 updateNormalHints(window, width, height); 2234 XFlush(_glfw.x11.display); 2235 } 2236 2237 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, int numer, int denom) 2238 { 2239 int width, height; 2240 _glfwPlatformGetWindowSize(window, &width, &height); 2241 updateNormalHints(window, width, height); 2242 XFlush(_glfw.x11.display); 2243 } 2244 2245 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, int* width, int* height) 2246 { 2247 _glfwPlatformGetWindowSize(window, width, height); 2248 } 2249 2250 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, 2251 int* left, int* top, 2252 int* right, int* bottom) 2253 { 2254 long* extents = NULL; 2255 2256 if (window->monitor || !window->decorated) 2257 return; 2258 2259 if (_glfw.x11.NET_FRAME_EXTENTS == None) 2260 return; 2261 2262 if (!_glfwPlatformWindowVisible(window) && 2263 _glfw.x11.NET_REQUEST_FRAME_EXTENTS) 2264 { 2265 XEvent event; 2266 double timeout = 0.5; 2267 2268 // Ensure _NET_FRAME_EXTENTS is set, allowing glfwGetWindowFrameSize to 2269 // function before the window is mapped 2270 sendEventToWM(window, _glfw.x11.NET_REQUEST_FRAME_EXTENTS, 2271 0, 0, 0, 0, 0); 2272 2273 // HACK: Use a timeout because earlier versions of some window managers 2274 // (at least Unity, Fluxbox and Xfwm) failed to send the reply 2275 // They have been fixed but broken versions are still in the wild 2276 // If you are affected by this and your window manager is NOT 2277 // listed above, PLEASE report it to their and our issue trackers 2278 while (!XCheckIfEvent(_glfw.x11.display, 2279 &event, 2280 isFrameExtentsEvent, 2281 (XPointer) window)) 2282 { 2283 if (!waitForX11Event(&timeout)) 2284 { 2285 _glfwInputError(GLFW_PLATFORM_ERROR, 2286 "X11: The window manager has a broken _NET_REQUEST_FRAME_EXTENTS implementation; please report this issue"); 2287 return; 2288 } 2289 } 2290 } 2291 2292 if (_glfwGetWindowPropertyX11(window->x11.handle, 2293 _glfw.x11.NET_FRAME_EXTENTS, 2294 XA_CARDINAL, 2295 (unsigned char**) &extents) == 4) 2296 { 2297 if (left) 2298 *left = extents[0]; 2299 if (top) 2300 *top = extents[2]; 2301 if (right) 2302 *right = extents[1]; 2303 if (bottom) 2304 *bottom = extents[3]; 2305 } 2306 2307 if (extents) 2308 XFree(extents); 2309 } 2310 2311 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window, 2312 float* xscale, float* yscale) 2313 { 2314 if (xscale) 2315 *xscale = _glfw.x11.contentScaleX; 2316 if (yscale) 2317 *yscale = _glfw.x11.contentScaleY; 2318 } 2319 2320 void _glfwPlatformIconifyWindow(_GLFWwindow* window) 2321 { 2322 if (window->x11.overrideRedirect) 2323 { 2324 // Override-redirect windows cannot be iconified or restored, as those 2325 // tasks are performed by the window manager 2326 _glfwInputError(GLFW_PLATFORM_ERROR, 2327 "X11: Iconification of full screen windows requires a WM that supports EWMH full screen"); 2328 return; 2329 } 2330 2331 XIconifyWindow(_glfw.x11.display, window->x11.handle, _glfw.x11.screen); 2332 XFlush(_glfw.x11.display); 2333 } 2334 2335 void _glfwPlatformRestoreWindow(_GLFWwindow* window) 2336 { 2337 if (window->x11.overrideRedirect) 2338 { 2339 // Override-redirect windows cannot be iconified or restored, as those 2340 // tasks are performed by the window manager 2341 _glfwInputError(GLFW_PLATFORM_ERROR, 2342 "X11: Iconification of full screen windows requires a WM that supports EWMH full screen"); 2343 return; 2344 } 2345 2346 if (_glfwPlatformWindowIconified(window)) 2347 { 2348 XMapWindow(_glfw.x11.display, window->x11.handle); 2349 waitForVisibilityNotify(window); 2350 } 2351 else if (_glfwPlatformWindowVisible(window)) 2352 { 2353 if (_glfw.x11.NET_WM_STATE && 2354 _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT && 2355 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ) 2356 { 2357 sendEventToWM(window, 2358 _glfw.x11.NET_WM_STATE, 2359 _NET_WM_STATE_REMOVE, 2360 _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT, 2361 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ, 2362 1, 0); 2363 } 2364 } 2365 2366 XFlush(_glfw.x11.display); 2367 } 2368 2369 void _glfwPlatformMaximizeWindow(_GLFWwindow* window) 2370 { 2371 if (!_glfw.x11.NET_WM_STATE || 2372 !_glfw.x11.NET_WM_STATE_MAXIMIZED_VERT || 2373 !_glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ) 2374 { 2375 return; 2376 } 2377 2378 if (_glfwPlatformWindowVisible(window)) 2379 { 2380 sendEventToWM(window, 2381 _glfw.x11.NET_WM_STATE, 2382 _NET_WM_STATE_ADD, 2383 _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT, 2384 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ, 2385 1, 0); 2386 } 2387 else 2388 { 2389 Atom* states = NULL; 2390 unsigned long count = 2391 _glfwGetWindowPropertyX11(window->x11.handle, 2392 _glfw.x11.NET_WM_STATE, 2393 XA_ATOM, 2394 (unsigned char**) &states); 2395 2396 // NOTE: We don't check for failure as this property may not exist yet 2397 // and that's fine (and we'll create it implicitly with append) 2398 2399 Atom missing[2] = 2400 { 2401 _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT, 2402 _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ 2403 }; 2404 unsigned long missingCount = 2; 2405 2406 for (unsigned long i = 0; i < count; i++) 2407 { 2408 for (unsigned long j = 0; j < missingCount; j++) 2409 { 2410 if (states[i] == missing[j]) 2411 { 2412 missing[j] = missing[missingCount - 1]; 2413 missingCount--; 2414 } 2415 } 2416 } 2417 2418 if (states) 2419 XFree(states); 2420 2421 if (!missingCount) 2422 return; 2423 2424 XChangeProperty(_glfw.x11.display, window->x11.handle, 2425 _glfw.x11.NET_WM_STATE, XA_ATOM, 32, 2426 PropModeAppend, 2427 (unsigned char*) missing, 2428 missingCount); 2429 } 2430 2431 XFlush(_glfw.x11.display); 2432 } 2433 2434 void _glfwPlatformShowWindow(_GLFWwindow* window) 2435 { 2436 if (_glfwPlatformWindowVisible(window)) 2437 return; 2438 2439 XMapWindow(_glfw.x11.display, window->x11.handle); 2440 waitForVisibilityNotify(window); 2441 } 2442 2443 void _glfwPlatformHideWindow(_GLFWwindow* window) 2444 { 2445 XUnmapWindow(_glfw.x11.display, window->x11.handle); 2446 XFlush(_glfw.x11.display); 2447 } 2448 2449 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window) 2450 { 2451 if (!_glfw.x11.NET_WM_STATE || !_glfw.x11.NET_WM_STATE_DEMANDS_ATTENTION) 2452 return; 2453 2454 sendEventToWM(window, 2455 _glfw.x11.NET_WM_STATE, 2456 _NET_WM_STATE_ADD, 2457 _glfw.x11.NET_WM_STATE_DEMANDS_ATTENTION, 2458 0, 1, 0); 2459 } 2460 2461 void _glfwPlatformFocusWindow(_GLFWwindow* window) 2462 { 2463 if (_glfw.x11.NET_ACTIVE_WINDOW) 2464 sendEventToWM(window, _glfw.x11.NET_ACTIVE_WINDOW, 1, 0, 0, 0, 0); 2465 else if (_glfwPlatformWindowVisible(window)) 2466 { 2467 XRaiseWindow(_glfw.x11.display, window->x11.handle); 2468 XSetInputFocus(_glfw.x11.display, window->x11.handle, 2469 RevertToParent, CurrentTime); 2470 } 2471 2472 XFlush(_glfw.x11.display); 2473 } 2474 2475 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window, 2476 _GLFWmonitor* monitor, 2477 int xpos, int ypos, 2478 int width, int height, 2479 int refreshRate) 2480 { 2481 if (window->monitor == monitor) 2482 { 2483 if (monitor) 2484 { 2485 if (monitor->window == window) 2486 acquireMonitor(window); 2487 } 2488 else 2489 { 2490 if (!window->resizable) 2491 updateNormalHints(window, width, height); 2492 2493 XMoveResizeWindow(_glfw.x11.display, window->x11.handle, 2494 xpos, ypos, width, height); 2495 } 2496 2497 XFlush(_glfw.x11.display); 2498 return; 2499 } 2500 2501 if (window->monitor) 2502 { 2503 _glfwPlatformSetWindowDecorated(window, window->decorated); 2504 _glfwPlatformSetWindowFloating(window, window->floating); 2505 releaseMonitor(window); 2506 } 2507 2508 _glfwInputWindowMonitor(window, monitor); 2509 updateNormalHints(window, width, height); 2510 2511 if (window->monitor) 2512 { 2513 if (!_glfwPlatformWindowVisible(window)) 2514 { 2515 XMapRaised(_glfw.x11.display, window->x11.handle); 2516 waitForVisibilityNotify(window); 2517 } 2518 2519 updateWindowMode(window); 2520 acquireMonitor(window); 2521 } 2522 else 2523 { 2524 updateWindowMode(window); 2525 XMoveResizeWindow(_glfw.x11.display, window->x11.handle, 2526 xpos, ypos, width, height); 2527 } 2528 2529 XFlush(_glfw.x11.display); 2530 } 2531 2532 int _glfwPlatformWindowFocused(_GLFWwindow* window) 2533 { 2534 Window focused; 2535 int state; 2536 2537 XGetInputFocus(_glfw.x11.display, &focused, &state); 2538 return window->x11.handle == focused; 2539 } 2540 2541 int _glfwPlatformWindowIconified(_GLFWwindow* window) 2542 { 2543 return getWindowState(window) == IconicState; 2544 } 2545 2546 int _glfwPlatformWindowVisible(_GLFWwindow* window) 2547 { 2548 XWindowAttributes wa; 2549 XGetWindowAttributes(_glfw.x11.display, window->x11.handle, &wa); 2550 return wa.map_state == IsViewable; 2551 } 2552 2553 int _glfwPlatformWindowMaximized(_GLFWwindow* window) 2554 { 2555 Atom* states; 2556 unsigned long i; 2557 GLFWbool maximized = GLFW_FALSE; 2558 2559 if (!_glfw.x11.NET_WM_STATE || 2560 !_glfw.x11.NET_WM_STATE_MAXIMIZED_VERT || 2561 !_glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ) 2562 { 2563 return maximized; 2564 } 2565 2566 const unsigned long count = 2567 _glfwGetWindowPropertyX11(window->x11.handle, 2568 _glfw.x11.NET_WM_STATE, 2569 XA_ATOM, 2570 (unsigned char**) &states); 2571 2572 for (i = 0; i < count; i++) 2573 { 2574 if (states[i] == _glfw.x11.NET_WM_STATE_MAXIMIZED_VERT || 2575 states[i] == _glfw.x11.NET_WM_STATE_MAXIMIZED_HORZ) 2576 { 2577 maximized = GLFW_TRUE; 2578 break; 2579 } 2580 } 2581 2582 if (states) 2583 XFree(states); 2584 2585 return maximized; 2586 } 2587 2588 int _glfwPlatformWindowHovered(_GLFWwindow* window) 2589 { 2590 Window w = _glfw.x11.root; 2591 while (w) 2592 { 2593 Window root; 2594 int rootX, rootY, childX, childY; 2595 unsigned int mask; 2596 2597 _glfwGrabErrorHandlerX11(); 2598 2599 const Bool result = XQueryPointer(_glfw.x11.display, w, 2600 &root, &w, &rootX, &rootY, 2601 &childX, &childY, &mask); 2602 2603 _glfwReleaseErrorHandlerX11(); 2604 2605 if (_glfw.x11.errorCode == BadWindow) 2606 w = _glfw.x11.root; 2607 else if (!result) 2608 return GLFW_FALSE; 2609 else if (w == window->x11.handle) 2610 return GLFW_TRUE; 2611 } 2612 2613 return GLFW_FALSE; 2614 } 2615 2616 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window) 2617 { 2618 if (!window->x11.transparent) 2619 return GLFW_FALSE; 2620 2621 return XGetSelectionOwner(_glfw.x11.display, _glfw.x11.NET_WM_CM_Sx) != None; 2622 } 2623 2624 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled) 2625 { 2626 int width, height; 2627 _glfwPlatformGetWindowSize(window, &width, &height); 2628 updateNormalHints(window, width, height); 2629 } 2630 2631 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled) 2632 { 2633 struct 2634 { 2635 unsigned long flags; 2636 unsigned long functions; 2637 unsigned long decorations; 2638 long input_mode; 2639 unsigned long status; 2640 } hints = {0}; 2641 2642 hints.flags = MWM_HINTS_DECORATIONS; 2643 hints.decorations = enabled ? MWM_DECOR_ALL : 0; 2644 2645 XChangeProperty(_glfw.x11.display, window->x11.handle, 2646 _glfw.x11.MOTIF_WM_HINTS, 2647 _glfw.x11.MOTIF_WM_HINTS, 32, 2648 PropModeReplace, 2649 (unsigned char*) &hints, 2650 sizeof(hints) / sizeof(long)); 2651 } 2652 2653 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled) 2654 { 2655 if (!_glfw.x11.NET_WM_STATE || !_glfw.x11.NET_WM_STATE_ABOVE) 2656 return; 2657 2658 if (_glfwPlatformWindowVisible(window)) 2659 { 2660 const long action = enabled ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE; 2661 sendEventToWM(window, 2662 _glfw.x11.NET_WM_STATE, 2663 action, 2664 _glfw.x11.NET_WM_STATE_ABOVE, 2665 0, 1, 0); 2666 } 2667 else 2668 { 2669 Atom* states = NULL; 2670 unsigned long i, count; 2671 2672 count = _glfwGetWindowPropertyX11(window->x11.handle, 2673 _glfw.x11.NET_WM_STATE, 2674 XA_ATOM, 2675 (unsigned char**) &states); 2676 2677 // NOTE: We don't check for failure as this property may not exist yet 2678 // and that's fine (and we'll create it implicitly with append) 2679 2680 if (enabled) 2681 { 2682 for (i = 0; i < count; i++) 2683 { 2684 if (states[i] == _glfw.x11.NET_WM_STATE_ABOVE) 2685 break; 2686 } 2687 2688 if (i == count) 2689 { 2690 XChangeProperty(_glfw.x11.display, window->x11.handle, 2691 _glfw.x11.NET_WM_STATE, XA_ATOM, 32, 2692 PropModeAppend, 2693 (unsigned char*) &_glfw.x11.NET_WM_STATE_ABOVE, 2694 1); 2695 } 2696 } 2697 else if (states) 2698 { 2699 for (i = 0; i < count; i++) 2700 { 2701 if (states[i] == _glfw.x11.NET_WM_STATE_ABOVE) 2702 break; 2703 } 2704 2705 if (i < count) 2706 { 2707 states[i] = states[count - 1]; 2708 count--; 2709 2710 XChangeProperty(_glfw.x11.display, window->x11.handle, 2711 _glfw.x11.NET_WM_STATE, XA_ATOM, 32, 2712 PropModeReplace, (unsigned char*) states, count); 2713 } 2714 } 2715 2716 if (states) 2717 XFree(states); 2718 } 2719 2720 XFlush(_glfw.x11.display); 2721 } 2722 2723 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window) 2724 { 2725 float opacity = 1.f; 2726 2727 if (XGetSelectionOwner(_glfw.x11.display, _glfw.x11.NET_WM_CM_Sx)) 2728 { 2729 CARD32* value = NULL; 2730 2731 if (_glfwGetWindowPropertyX11(window->x11.handle, 2732 _glfw.x11.NET_WM_WINDOW_OPACITY, 2733 XA_CARDINAL, 2734 (unsigned char**) &value)) 2735 { 2736 opacity = (float) (*value / (double) 0xffffffffu); 2737 } 2738 2739 if (value) 2740 XFree(value); 2741 } 2742 2743 return opacity; 2744 } 2745 2746 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity) 2747 { 2748 const CARD32 value = (CARD32) (0xffffffffu * (double) opacity); 2749 XChangeProperty(_glfw.x11.display, window->x11.handle, 2750 _glfw.x11.NET_WM_WINDOW_OPACITY, XA_CARDINAL, 32, 2751 PropModeReplace, (unsigned char*) &value, 1); 2752 } 2753 2754 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled) 2755 { 2756 if (!_glfw.x11.xi.available) 2757 return; 2758 2759 if (_glfw.x11.disabledCursorWindow != window) 2760 return; 2761 2762 if (enabled) 2763 enableRawMouseMotion(window); 2764 else 2765 disableRawMouseMotion(window); 2766 } 2767 2768 GLFWbool _glfwPlatformRawMouseMotionSupported(void) 2769 { 2770 return _glfw.x11.xi.available; 2771 } 2772 2773 void _glfwPlatformPollEvents(void) 2774 { 2775 drainEmptyEvents(); 2776 2777 #if defined(__linux__) 2778 _glfwDetectJoystickConnectionLinux(); 2779 #endif 2780 XPending(_glfw.x11.display); 2781 2782 while (XQLength(_glfw.x11.display)) 2783 { 2784 XEvent event; 2785 XNextEvent(_glfw.x11.display, &event); 2786 processEvent(&event); 2787 } 2788 2789 _GLFWwindow* window = _glfw.x11.disabledCursorWindow; 2790 if (window) 2791 { 2792 int width, height; 2793 _glfwPlatformGetWindowSize(window, &width, &height); 2794 2795 // NOTE: Re-center the cursor only if it has moved since the last call, 2796 // to avoid breaking glfwWaitEvents with MotionNotify 2797 if (window->x11.lastCursorPosX != width / 2 || 2798 window->x11.lastCursorPosY != height / 2) 2799 { 2800 _glfwPlatformSetCursorPos(window, width / 2, height / 2); 2801 } 2802 } 2803 2804 XFlush(_glfw.x11.display); 2805 } 2806 2807 void _glfwPlatformWaitEvents(void) 2808 { 2809 waitForAnyEvent(NULL); 2810 _glfwPlatformPollEvents(); 2811 } 2812 2813 void _glfwPlatformWaitEventsTimeout(double timeout) 2814 { 2815 waitForAnyEvent(&timeout); 2816 _glfwPlatformPollEvents(); 2817 } 2818 2819 void _glfwPlatformPostEmptyEvent(void) 2820 { 2821 writeEmptyEvent(); 2822 } 2823 2824 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos) 2825 { 2826 Window root, child; 2827 int rootX, rootY, childX, childY; 2828 unsigned int mask; 2829 2830 XQueryPointer(_glfw.x11.display, window->x11.handle, 2831 &root, &child, 2832 &rootX, &rootY, &childX, &childY, 2833 &mask); 2834 2835 if (xpos) 2836 *xpos = childX; 2837 if (ypos) 2838 *ypos = childY; 2839 } 2840 2841 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) 2842 { 2843 // Store the new position so it can be recognized later 2844 window->x11.warpCursorPosX = (int) x; 2845 window->x11.warpCursorPosY = (int) y; 2846 2847 XWarpPointer(_glfw.x11.display, None, window->x11.handle, 2848 0,0,0,0, (int) x, (int) y); 2849 XFlush(_glfw.x11.display); 2850 } 2851 2852 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) 2853 { 2854 if (mode == GLFW_CURSOR_DISABLED) 2855 { 2856 if (_glfwPlatformWindowFocused(window)) 2857 disableCursor(window); 2858 } 2859 else if (_glfw.x11.disabledCursorWindow == window) 2860 enableCursor(window); 2861 else 2862 updateCursorImage(window); 2863 2864 XFlush(_glfw.x11.display); 2865 } 2866 2867 const char* _glfwPlatformGetScancodeName(int scancode) 2868 { 2869 if (!_glfw.x11.xkb.available) 2870 return NULL; 2871 2872 if (scancode < 0 || scancode > 0xff || 2873 _glfw.x11.keycodes[scancode] == GLFW_KEY_UNKNOWN) 2874 { 2875 _glfwInputError(GLFW_INVALID_VALUE, "Invalid scancode %i", scancode); 2876 return NULL; 2877 } 2878 2879 const int key = _glfw.x11.keycodes[scancode]; 2880 const KeySym keysym = XkbKeycodeToKeysym(_glfw.x11.display, 2881 scancode, _glfw.x11.xkb.group, 0); 2882 if (keysym == NoSymbol) 2883 return NULL; 2884 2885 const uint32_t codepoint = _glfwKeySym2Unicode(keysym); 2886 if (codepoint == GLFW_INVALID_CODEPOINT) 2887 return NULL; 2888 2889 const size_t count = _glfwEncodeUTF8(_glfw.x11.keynames[key], codepoint); 2890 if (count == 0) 2891 return NULL; 2892 2893 _glfw.x11.keynames[key][count] = '\0'; 2894 return _glfw.x11.keynames[key]; 2895 } 2896 2897 int _glfwPlatformGetKeyScancode(int key) 2898 { 2899 return _glfw.x11.scancodes[key]; 2900 } 2901 2902 int _glfwPlatformCreateCursor(_GLFWcursor* cursor, 2903 const GLFWimage* image, 2904 int xhot, int yhot) 2905 { 2906 cursor->x11.handle = _glfwCreateCursorX11(image, xhot, yhot); 2907 if (!cursor->x11.handle) 2908 return GLFW_FALSE; 2909 2910 return GLFW_TRUE; 2911 } 2912 2913 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape) 2914 { 2915 int native = 0; 2916 2917 if (shape == GLFW_ARROW_CURSOR) 2918 native = XC_left_ptr; 2919 else if (shape == GLFW_IBEAM_CURSOR) 2920 native = XC_xterm; 2921 else if (shape == GLFW_CROSSHAIR_CURSOR) 2922 native = XC_crosshair; 2923 else if (shape == GLFW_HAND_CURSOR) 2924 native = XC_hand2; 2925 else if (shape == GLFW_HRESIZE_CURSOR) 2926 native = XC_sb_h_double_arrow; 2927 else if (shape == GLFW_VRESIZE_CURSOR) 2928 native = XC_sb_v_double_arrow; 2929 else 2930 return GLFW_FALSE; 2931 2932 cursor->x11.handle = XCreateFontCursor(_glfw.x11.display, native); 2933 if (!cursor->x11.handle) 2934 { 2935 _glfwInputError(GLFW_PLATFORM_ERROR, 2936 "X11: Failed to create standard cursor"); 2937 return GLFW_FALSE; 2938 } 2939 2940 return GLFW_TRUE; 2941 } 2942 2943 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) 2944 { 2945 if (cursor->x11.handle) 2946 XFreeCursor(_glfw.x11.display, cursor->x11.handle); 2947 } 2948 2949 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) 2950 { 2951 if (window->cursorMode == GLFW_CURSOR_NORMAL) 2952 { 2953 updateCursorImage(window); 2954 XFlush(_glfw.x11.display); 2955 } 2956 } 2957 2958 void _glfwPlatformSetClipboardString(const char* string) 2959 { 2960 char* copy = _glfw_strdup(string); 2961 free(_glfw.x11.clipboardString); 2962 _glfw.x11.clipboardString = copy; 2963 2964 XSetSelectionOwner(_glfw.x11.display, 2965 _glfw.x11.CLIPBOARD, 2966 _glfw.x11.helperWindowHandle, 2967 CurrentTime); 2968 2969 if (XGetSelectionOwner(_glfw.x11.display, _glfw.x11.CLIPBOARD) != 2970 _glfw.x11.helperWindowHandle) 2971 { 2972 _glfwInputError(GLFW_PLATFORM_ERROR, 2973 "X11: Failed to become owner of clipboard selection"); 2974 } 2975 } 2976 2977 const char* _glfwPlatformGetClipboardString(void) 2978 { 2979 return getSelectionString(_glfw.x11.CLIPBOARD); 2980 } 2981 2982 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) 2983 { 2984 if (!_glfw.vk.KHR_surface) 2985 return; 2986 2987 if (!_glfw.vk.KHR_xcb_surface || !_glfw.x11.x11xcb.handle) 2988 { 2989 if (!_glfw.vk.KHR_xlib_surface) 2990 return; 2991 } 2992 2993 extensions[0] = "VK_KHR_surface"; 2994 2995 // NOTE: VK_KHR_xcb_surface is preferred due to some early ICDs exposing but 2996 // not correctly implementing VK_KHR_xlib_surface 2997 if (_glfw.vk.KHR_xcb_surface && _glfw.x11.x11xcb.handle) 2998 extensions[1] = "VK_KHR_xcb_surface"; 2999 else 3000 extensions[1] = "VK_KHR_xlib_surface"; 3001 } 3002 3003 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, 3004 VkPhysicalDevice device, 3005 uint32_t queuefamily) 3006 { 3007 VisualID visualID = XVisualIDFromVisual(DefaultVisual(_glfw.x11.display, 3008 _glfw.x11.screen)); 3009 3010 if (_glfw.vk.KHR_xcb_surface && _glfw.x11.x11xcb.handle) 3011 { 3012 PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR 3013 vkGetPhysicalDeviceXcbPresentationSupportKHR = 3014 (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR) 3015 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceXcbPresentationSupportKHR"); 3016 if (!vkGetPhysicalDeviceXcbPresentationSupportKHR) 3017 { 3018 _glfwInputError(GLFW_API_UNAVAILABLE, 3019 "X11: Vulkan instance missing VK_KHR_xcb_surface extension"); 3020 return GLFW_FALSE; 3021 } 3022 3023 xcb_connection_t* connection = XGetXCBConnection(_glfw.x11.display); 3024 if (!connection) 3025 { 3026 _glfwInputError(GLFW_PLATFORM_ERROR, 3027 "X11: Failed to retrieve XCB connection"); 3028 return GLFW_FALSE; 3029 } 3030 3031 return vkGetPhysicalDeviceXcbPresentationSupportKHR(device, 3032 queuefamily, 3033 connection, 3034 visualID); 3035 } 3036 else 3037 { 3038 PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR 3039 vkGetPhysicalDeviceXlibPresentationSupportKHR = 3040 (PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR) 3041 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceXlibPresentationSupportKHR"); 3042 if (!vkGetPhysicalDeviceXlibPresentationSupportKHR) 3043 { 3044 _glfwInputError(GLFW_API_UNAVAILABLE, 3045 "X11: Vulkan instance missing VK_KHR_xlib_surface extension"); 3046 return GLFW_FALSE; 3047 } 3048 3049 return vkGetPhysicalDeviceXlibPresentationSupportKHR(device, 3050 queuefamily, 3051 _glfw.x11.display, 3052 visualID); 3053 } 3054 } 3055 3056 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, 3057 _GLFWwindow* window, 3058 const VkAllocationCallbacks* allocator, 3059 VkSurfaceKHR* surface) 3060 { 3061 if (_glfw.vk.KHR_xcb_surface && _glfw.x11.x11xcb.handle) 3062 { 3063 VkResult err; 3064 VkXcbSurfaceCreateInfoKHR sci; 3065 PFN_vkCreateXcbSurfaceKHR vkCreateXcbSurfaceKHR; 3066 3067 xcb_connection_t* connection = XGetXCBConnection(_glfw.x11.display); 3068 if (!connection) 3069 { 3070 _glfwInputError(GLFW_PLATFORM_ERROR, 3071 "X11: Failed to retrieve XCB connection"); 3072 return VK_ERROR_EXTENSION_NOT_PRESENT; 3073 } 3074 3075 vkCreateXcbSurfaceKHR = (PFN_vkCreateXcbSurfaceKHR) 3076 vkGetInstanceProcAddr(instance, "vkCreateXcbSurfaceKHR"); 3077 if (!vkCreateXcbSurfaceKHR) 3078 { 3079 _glfwInputError(GLFW_API_UNAVAILABLE, 3080 "X11: Vulkan instance missing VK_KHR_xcb_surface extension"); 3081 return VK_ERROR_EXTENSION_NOT_PRESENT; 3082 } 3083 3084 memset(&sci, 0, sizeof(sci)); 3085 sci.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; 3086 sci.connection = connection; 3087 sci.window = window->x11.handle; 3088 3089 err = vkCreateXcbSurfaceKHR(instance, &sci, allocator, surface); 3090 if (err) 3091 { 3092 _glfwInputError(GLFW_PLATFORM_ERROR, 3093 "X11: Failed to create Vulkan XCB surface: %s", 3094 _glfwGetVulkanResultString(err)); 3095 } 3096 3097 return err; 3098 } 3099 else 3100 { 3101 VkResult err; 3102 VkXlibSurfaceCreateInfoKHR sci; 3103 PFN_vkCreateXlibSurfaceKHR vkCreateXlibSurfaceKHR; 3104 3105 vkCreateXlibSurfaceKHR = (PFN_vkCreateXlibSurfaceKHR) 3106 vkGetInstanceProcAddr(instance, "vkCreateXlibSurfaceKHR"); 3107 if (!vkCreateXlibSurfaceKHR) 3108 { 3109 _glfwInputError(GLFW_API_UNAVAILABLE, 3110 "X11: Vulkan instance missing VK_KHR_xlib_surface extension"); 3111 return VK_ERROR_EXTENSION_NOT_PRESENT; 3112 } 3113 3114 memset(&sci, 0, sizeof(sci)); 3115 sci.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; 3116 sci.dpy = _glfw.x11.display; 3117 sci.window = window->x11.handle; 3118 3119 err = vkCreateXlibSurfaceKHR(instance, &sci, allocator, surface); 3120 if (err) 3121 { 3122 _glfwInputError(GLFW_PLATFORM_ERROR, 3123 "X11: Failed to create Vulkan X11 surface: %s", 3124 _glfwGetVulkanResultString(err)); 3125 } 3126 3127 return err; 3128 } 3129 } 3130 3131 3132 ////////////////////////////////////////////////////////////////////////// 3133 ////// GLFW native API ////// 3134 ////////////////////////////////////////////////////////////////////////// 3135 3136 GLFWAPI Display* glfwGetX11Display(void) 3137 { 3138 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 3139 return _glfw.x11.display; 3140 } 3141 3142 GLFWAPI Window glfwGetX11Window(GLFWwindow* handle) 3143 { 3144 _GLFWwindow* window = (_GLFWwindow*) handle; 3145 _GLFW_REQUIRE_INIT_OR_RETURN(None); 3146 return window->x11.handle; 3147 } 3148 3149 GLFWAPI void glfwSetX11SelectionString(const char* string) 3150 { 3151 _GLFW_REQUIRE_INIT(); 3152 3153 free(_glfw.x11.primarySelectionString); 3154 _glfw.x11.primarySelectionString = _glfw_strdup(string); 3155 3156 XSetSelectionOwner(_glfw.x11.display, 3157 _glfw.x11.PRIMARY, 3158 _glfw.x11.helperWindowHandle, 3159 CurrentTime); 3160 3161 if (XGetSelectionOwner(_glfw.x11.display, _glfw.x11.PRIMARY) != 3162 _glfw.x11.helperWindowHandle) 3163 { 3164 _glfwInputError(GLFW_PLATFORM_ERROR, 3165 "X11: Failed to become owner of primary selection"); 3166 } 3167 } 3168 3169 GLFWAPI const char* glfwGetX11SelectionString(void) 3170 { 3171 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 3172 return getSelectionString(_glfw.x11.PRIMARY); 3173 } 3174