x11_platform.h 16 KB

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