x11_platform.h (16711B)
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 28 #include <unistd.h> 29 #include <signal.h> 30 #include <stdint.h> 31 #include <dlfcn.h> 32 33 #include <X11/Xlib.h> 34 #include <X11/keysym.h> 35 #include <X11/Xatom.h> 36 #include <X11/Xcursor/Xcursor.h> 37 38 // The XRandR extension provides mode setting and gamma control 39 #include <X11/extensions/Xrandr.h> 40 41 // The Xkb extension provides improved keyboard support 42 #include <X11/XKBlib.h> 43 44 // The Xinerama extension provides legacy monitor indices 45 #include <X11/extensions/Xinerama.h> 46 47 // The XInput extension provides raw mouse motion input 48 #include <X11/extensions/XInput2.h> 49 50 typedef XRRCrtcGamma* (* PFN_XRRAllocGamma)(int); 51 typedef void (* PFN_XRRFreeCrtcInfo)(XRRCrtcInfo*); 52 typedef void (* PFN_XRRFreeGamma)(XRRCrtcGamma*); 53 typedef void (* PFN_XRRFreeOutputInfo)(XRROutputInfo*); 54 typedef void (* PFN_XRRFreeScreenResources)(XRRScreenResources*); 55 typedef XRRCrtcGamma* (* PFN_XRRGetCrtcGamma)(Display*,RRCrtc); 56 typedef int (* PFN_XRRGetCrtcGammaSize)(Display*,RRCrtc); 57 typedef XRRCrtcInfo* (* PFN_XRRGetCrtcInfo) (Display*,XRRScreenResources*,RRCrtc); 58 typedef XRROutputInfo* (* PFN_XRRGetOutputInfo)(Display*,XRRScreenResources*,RROutput); 59 typedef RROutput (* PFN_XRRGetOutputPrimary)(Display*,Window); 60 typedef XRRScreenResources* (* PFN_XRRGetScreenResourcesCurrent)(Display*,Window); 61 typedef Bool (* PFN_XRRQueryExtension)(Display*,int*,int*); 62 typedef Status (* PFN_XRRQueryVersion)(Display*,int*,int*); 63 typedef void (* PFN_XRRSelectInput)(Display*,Window,int); 64 typedef Status (* PFN_XRRSetCrtcConfig)(Display*,XRRScreenResources*,RRCrtc,Time,int,int,RRMode,Rotation,RROutput*,int); 65 typedef void (* PFN_XRRSetCrtcGamma)(Display*,RRCrtc,XRRCrtcGamma*); 66 typedef int (* PFN_XRRUpdateConfiguration)(XEvent*); 67 #define XRRAllocGamma _glfw.x11.randr.AllocGamma 68 #define XRRFreeCrtcInfo _glfw.x11.randr.FreeCrtcInfo 69 #define XRRFreeGamma _glfw.x11.randr.FreeGamma 70 #define XRRFreeOutputInfo _glfw.x11.randr.FreeOutputInfo 71 #define XRRFreeScreenResources _glfw.x11.randr.FreeScreenResources 72 #define XRRGetCrtcGamma _glfw.x11.randr.GetCrtcGamma 73 #define XRRGetCrtcGammaSize _glfw.x11.randr.GetCrtcGammaSize 74 #define XRRGetCrtcInfo _glfw.x11.randr.GetCrtcInfo 75 #define XRRGetOutputInfo _glfw.x11.randr.GetOutputInfo 76 #define XRRGetOutputPrimary _glfw.x11.randr.GetOutputPrimary 77 #define XRRGetScreenResourcesCurrent _glfw.x11.randr.GetScreenResourcesCurrent 78 #define XRRQueryExtension _glfw.x11.randr.QueryExtension 79 #define XRRQueryVersion _glfw.x11.randr.QueryVersion 80 #define XRRSelectInput _glfw.x11.randr.SelectInput 81 #define XRRSetCrtcConfig _glfw.x11.randr.SetCrtcConfig 82 #define XRRSetCrtcGamma _glfw.x11.randr.SetCrtcGamma 83 #define XRRUpdateConfiguration _glfw.x11.randr.UpdateConfiguration 84 85 typedef XcursorImage* (* PFN_XcursorImageCreate)(int,int); 86 typedef void (* PFN_XcursorImageDestroy)(XcursorImage*); 87 typedef Cursor (* PFN_XcursorImageLoadCursor)(Display*,const XcursorImage*); 88 #define XcursorImageCreate _glfw.x11.xcursor.ImageCreate 89 #define XcursorImageDestroy _glfw.x11.xcursor.ImageDestroy 90 #define XcursorImageLoadCursor _glfw.x11.xcursor.ImageLoadCursor 91 92 typedef Bool (* PFN_XineramaIsActive)(Display*); 93 typedef Bool (* PFN_XineramaQueryExtension)(Display*,int*,int*); 94 typedef XineramaScreenInfo* (* PFN_XineramaQueryScreens)(Display*,int*); 95 #define XineramaIsActive _glfw.x11.xinerama.IsActive 96 #define XineramaQueryExtension _glfw.x11.xinerama.QueryExtension 97 #define XineramaQueryScreens _glfw.x11.xinerama.QueryScreens 98 99 typedef XID xcb_window_t; 100 typedef XID xcb_visualid_t; 101 typedef struct xcb_connection_t xcb_connection_t; 102 typedef xcb_connection_t* (* PFN_XGetXCBConnection)(Display*); 103 #define XGetXCBConnection _glfw.x11.x11xcb.GetXCBConnection 104 105 typedef Bool (* PFN_XF86VidModeQueryExtension)(Display*,int*,int*); 106 typedef Bool (* PFN_XF86VidModeGetGammaRamp)(Display*,int,int,unsigned short*,unsigned short*,unsigned short*); 107 typedef Bool (* PFN_XF86VidModeSetGammaRamp)(Display*,int,int,unsigned short*,unsigned short*,unsigned short*); 108 typedef Bool (* PFN_XF86VidModeGetGammaRampSize)(Display*,int,int*); 109 #define XF86VidModeQueryExtension _glfw.x11.vidmode.QueryExtension 110 #define XF86VidModeGetGammaRamp _glfw.x11.vidmode.GetGammaRamp 111 #define XF86VidModeSetGammaRamp _glfw.x11.vidmode.SetGammaRamp 112 #define XF86VidModeGetGammaRampSize _glfw.x11.vidmode.GetGammaRampSize 113 114 typedef Status (* PFN_XIQueryVersion)(Display*,int*,int*); 115 typedef int (* PFN_XISelectEvents)(Display*,Window,XIEventMask*,int); 116 #define XIQueryVersion _glfw.x11.xi.QueryVersion 117 #define XISelectEvents _glfw.x11.xi.SelectEvents 118 119 typedef Bool (* PFN_XRenderQueryExtension)(Display*,int*,int*); 120 typedef Status (* PFN_XRenderQueryVersion)(Display*dpy,int*,int*); 121 typedef XRenderPictFormat* (* PFN_XRenderFindVisualFormat)(Display*,Visual const*); 122 #define XRenderQueryExtension _glfw.x11.xrender.QueryExtension 123 #define XRenderQueryVersion _glfw.x11.xrender.QueryVersion 124 #define XRenderFindVisualFormat _glfw.x11.xrender.FindVisualFormat 125 126 typedef VkFlags VkXlibSurfaceCreateFlagsKHR; 127 typedef VkFlags VkXcbSurfaceCreateFlagsKHR; 128 129 typedef struct VkXlibSurfaceCreateInfoKHR 130 { 131 VkStructureType sType; 132 const void* pNext; 133 VkXlibSurfaceCreateFlagsKHR flags; 134 Display* dpy; 135 Window window; 136 } VkXlibSurfaceCreateInfoKHR; 137 138 typedef struct VkXcbSurfaceCreateInfoKHR 139 { 140 VkStructureType sType; 141 const void* pNext; 142 VkXcbSurfaceCreateFlagsKHR flags; 143 xcb_connection_t* connection; 144 xcb_window_t window; 145 } VkXcbSurfaceCreateInfoKHR; 146 147 typedef VkResult (APIENTRY *PFN_vkCreateXlibSurfaceKHR)(VkInstance,const VkXlibSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*); 148 typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR)(VkPhysicalDevice,uint32_t,Display*,VisualID); 149 typedef VkResult (APIENTRY *PFN_vkCreateXcbSurfaceKHR)(VkInstance,const VkXcbSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*); 150 typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)(VkPhysicalDevice,uint32_t,xcb_connection_t*,xcb_visualid_t); 151 152 #include "posix_thread.h" 153 #include "posix_time.h" 154 #include "xkb_unicode.h" 155 #include "glx_context.h" 156 #include "egl_context.h" 157 #include "osmesa_context.h" 158 #if defined(__linux__) 159 #include "linux_joystick.h" 160 #else 161 #include "null_joystick.h" 162 #endif 163 164 #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL) 165 #define _glfw_dlclose(handle) dlclose(handle) 166 #define _glfw_dlsym(handle, name) dlsym(handle, name) 167 168 #define _GLFW_EGL_NATIVE_WINDOW ((EGLNativeWindowType) window->x11.handle) 169 #define _GLFW_EGL_NATIVE_DISPLAY ((EGLNativeDisplayType) _glfw.x11.display) 170 171 #define _GLFW_PLATFORM_WINDOW_STATE _GLFWwindowX11 x11 172 #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryX11 x11 173 #define _GLFW_PLATFORM_MONITOR_STATE _GLFWmonitorX11 x11 174 #define _GLFW_PLATFORM_CURSOR_STATE _GLFWcursorX11 x11 175 176 177 // X11-specific per-window data 178 // 179 typedef struct _GLFWwindowX11 180 { 181 Colormap colormap; 182 Window handle; 183 Window parent; 184 XIC ic; 185 186 GLFWbool overrideRedirect; 187 GLFWbool iconified; 188 GLFWbool maximized; 189 190 // Whether the visual supports framebuffer transparency 191 GLFWbool transparent; 192 193 // Cached position and size used to filter out duplicate events 194 int width, height; 195 int xpos, ypos; 196 197 // The last received cursor position, regardless of source 198 int lastCursorPosX, lastCursorPosY; 199 // The last position the cursor was warped to by GLFW 200 int warpCursorPosX, warpCursorPosY; 201 202 // The time of the last KeyPress event per keycode, for discarding 203 // duplicate key events generated for some keys by ibus 204 Time keyPressTimes[256]; 205 } _GLFWwindowX11; 206 207 // X11-specific global data 208 // 209 typedef struct _GLFWlibraryX11 210 { 211 Display* display; 212 int screen; 213 Window root; 214 215 // System content scale 216 float contentScaleX, contentScaleY; 217 // Helper window for IPC 218 Window helperWindowHandle; 219 // Invisible cursor for hidden cursor mode 220 Cursor hiddenCursorHandle; 221 // Context for mapping window XIDs to _GLFWwindow pointers 222 XContext context; 223 // XIM input method 224 XIM im; 225 // The previous X error handler, to be restored later 226 XErrorHandler errorHandler; 227 // Most recent error code received by X error handler 228 int errorCode; 229 // Primary selection string (while the primary selection is owned) 230 char* primarySelectionString; 231 // Clipboard string (while the selection is owned) 232 char* clipboardString; 233 // Key name string 234 char keynames[GLFW_KEY_LAST + 1][5]; 235 // X11 keycode to GLFW key LUT 236 short int keycodes[256]; 237 // GLFW key to X11 keycode LUT 238 short int scancodes[GLFW_KEY_LAST + 1]; 239 // Where to place the cursor when re-enabled 240 double restoreCursorPosX, restoreCursorPosY; 241 // The window whose disabled cursor mode is active 242 _GLFWwindow* disabledCursorWindow; 243 int emptyEventPipe[2]; 244 245 // Window manager atoms 246 Atom NET_SUPPORTED; 247 Atom NET_SUPPORTING_WM_CHECK; 248 Atom WM_PROTOCOLS; 249 Atom WM_STATE; 250 Atom WM_DELETE_WINDOW; 251 Atom NET_WM_NAME; 252 Atom NET_WM_ICON_NAME; 253 Atom NET_WM_ICON; 254 Atom NET_WM_PID; 255 Atom NET_WM_PING; 256 Atom NET_WM_WINDOW_TYPE; 257 Atom NET_WM_WINDOW_TYPE_NORMAL; 258 Atom NET_WM_STATE; 259 Atom NET_WM_STATE_ABOVE; 260 Atom NET_WM_STATE_FULLSCREEN; 261 Atom NET_WM_STATE_MAXIMIZED_VERT; 262 Atom NET_WM_STATE_MAXIMIZED_HORZ; 263 Atom NET_WM_STATE_DEMANDS_ATTENTION; 264 Atom NET_WM_BYPASS_COMPOSITOR; 265 Atom NET_WM_FULLSCREEN_MONITORS; 266 Atom NET_WM_WINDOW_OPACITY; 267 Atom NET_WM_CM_Sx; 268 Atom NET_WORKAREA; 269 Atom NET_CURRENT_DESKTOP; 270 Atom NET_ACTIVE_WINDOW; 271 Atom NET_FRAME_EXTENTS; 272 Atom NET_REQUEST_FRAME_EXTENTS; 273 Atom MOTIF_WM_HINTS; 274 275 // Xdnd (drag and drop) atoms 276 Atom XdndAware; 277 Atom XdndEnter; 278 Atom XdndPosition; 279 Atom XdndStatus; 280 Atom XdndActionCopy; 281 Atom XdndDrop; 282 Atom XdndFinished; 283 Atom XdndSelection; 284 Atom XdndTypeList; 285 Atom text_uri_list; 286 287 // Selection (clipboard) atoms 288 Atom TARGETS; 289 Atom MULTIPLE; 290 Atom INCR; 291 Atom CLIPBOARD; 292 Atom PRIMARY; 293 Atom CLIPBOARD_MANAGER; 294 Atom SAVE_TARGETS; 295 Atom NULL_; 296 Atom UTF8_STRING; 297 Atom COMPOUND_STRING; 298 Atom ATOM_PAIR; 299 Atom GLFW_SELECTION; 300 301 struct { 302 GLFWbool available; 303 void* handle; 304 int eventBase; 305 int errorBase; 306 int major; 307 int minor; 308 GLFWbool gammaBroken; 309 GLFWbool monitorBroken; 310 PFN_XRRAllocGamma AllocGamma; 311 PFN_XRRFreeCrtcInfo FreeCrtcInfo; 312 PFN_XRRFreeGamma FreeGamma; 313 PFN_XRRFreeOutputInfo FreeOutputInfo; 314 PFN_XRRFreeScreenResources FreeScreenResources; 315 PFN_XRRGetCrtcGamma GetCrtcGamma; 316 PFN_XRRGetCrtcGammaSize GetCrtcGammaSize; 317 PFN_XRRGetCrtcInfo GetCrtcInfo; 318 PFN_XRRGetOutputInfo GetOutputInfo; 319 PFN_XRRGetOutputPrimary GetOutputPrimary; 320 PFN_XRRGetScreenResourcesCurrent GetScreenResourcesCurrent; 321 PFN_XRRQueryExtension QueryExtension; 322 PFN_XRRQueryVersion QueryVersion; 323 PFN_XRRSelectInput SelectInput; 324 PFN_XRRSetCrtcConfig SetCrtcConfig; 325 PFN_XRRSetCrtcGamma SetCrtcGamma; 326 PFN_XRRUpdateConfiguration UpdateConfiguration; 327 } randr; 328 329 struct { 330 GLFWbool available; 331 GLFWbool detectable; 332 int majorOpcode; 333 int eventBase; 334 int errorBase; 335 int major; 336 int minor; 337 unsigned int group; 338 } xkb; 339 340 struct { 341 int count; 342 int timeout; 343 int interval; 344 int blanking; 345 int exposure; 346 } saver; 347 348 struct { 349 int version; 350 Window source; 351 Atom format; 352 } xdnd; 353 354 struct { 355 void* handle; 356 PFN_XcursorImageCreate ImageCreate; 357 PFN_XcursorImageDestroy ImageDestroy; 358 PFN_XcursorImageLoadCursor ImageLoadCursor; 359 } xcursor; 360 361 struct { 362 GLFWbool available; 363 void* handle; 364 int major; 365 int minor; 366 PFN_XineramaIsActive IsActive; 367 PFN_XineramaQueryExtension QueryExtension; 368 PFN_XineramaQueryScreens QueryScreens; 369 } xinerama; 370 371 struct { 372 void* handle; 373 PFN_XGetXCBConnection GetXCBConnection; 374 } x11xcb; 375 376 struct { 377 GLFWbool available; 378 void* handle; 379 int eventBase; 380 int errorBase; 381 PFN_XF86VidModeQueryExtension QueryExtension; 382 PFN_XF86VidModeGetGammaRamp GetGammaRamp; 383 PFN_XF86VidModeSetGammaRamp SetGammaRamp; 384 PFN_XF86VidModeGetGammaRampSize GetGammaRampSize; 385 } vidmode; 386 387 struct { 388 GLFWbool available; 389 void* handle; 390 int majorOpcode; 391 int eventBase; 392 int errorBase; 393 int major; 394 int minor; 395 PFN_XIQueryVersion QueryVersion; 396 PFN_XISelectEvents SelectEvents; 397 } xi; 398 399 struct { 400 GLFWbool available; 401 void* handle; 402 int major; 403 int minor; 404 int eventBase; 405 int errorBase; 406 PFN_XRenderQueryExtension QueryExtension; 407 PFN_XRenderQueryVersion QueryVersion; 408 PFN_XRenderFindVisualFormat FindVisualFormat; 409 } xrender; 410 } _GLFWlibraryX11; 411 412 // X11-specific per-monitor data 413 // 414 typedef struct _GLFWmonitorX11 415 { 416 RROutput output; 417 RRCrtc crtc; 418 RRMode oldMode; 419 420 // Index of corresponding Xinerama screen, 421 // for EWMH full screen window placement 422 int index; 423 } _GLFWmonitorX11; 424 425 // X11-specific per-cursor data 426 // 427 typedef struct _GLFWcursorX11 428 { 429 Cursor handle; 430 } _GLFWcursorX11; 431 432 433 void _glfwPollMonitorsX11(void); 434 void _glfwSetVideoModeX11(_GLFWmonitor* monitor, const GLFWvidmode* desired); 435 void _glfwRestoreVideoModeX11(_GLFWmonitor* monitor); 436 437 Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot); 438 439 unsigned long _glfwGetWindowPropertyX11(Window window, 440 Atom property, 441 Atom type, 442 unsigned char** value); 443 GLFWbool _glfwIsVisualTransparentX11(Visual* visual); 444 445 void _glfwGrabErrorHandlerX11(void); 446 void _glfwReleaseErrorHandlerX11(void); 447 void _glfwInputErrorX11(int error, const char* message); 448 449 void _glfwPushSelectionToManagerX11(void); 450