medfall

A super great game engine
Log | Files | Refs

x11_platform.h (15167B)


      1 //========================================================================
      2 // GLFW 3.3 X11 - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2002-2006 Marcus Geelnard
      5 // Copyright (c) 2006-2016 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 VkFlags VkXlibSurfaceCreateFlagsKHR;
    120 typedef VkFlags VkXcbSurfaceCreateFlagsKHR;
    121 
    122 typedef struct VkXlibSurfaceCreateInfoKHR
    123 {
    124     VkStructureType             sType;
    125     const void*                 pNext;
    126     VkXlibSurfaceCreateFlagsKHR flags;
    127     Display*                    dpy;
    128     Window                      window;
    129 } VkXlibSurfaceCreateInfoKHR;
    130 
    131 typedef struct VkXcbSurfaceCreateInfoKHR
    132 {
    133     VkStructureType             sType;
    134     const void*                 pNext;
    135     VkXcbSurfaceCreateFlagsKHR  flags;
    136     xcb_connection_t*           connection;
    137     xcb_window_t                window;
    138 } VkXcbSurfaceCreateInfoKHR;
    139 
    140 typedef VkResult (APIENTRY *PFN_vkCreateXlibSurfaceKHR)(VkInstance,const VkXlibSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
    141 typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR)(VkPhysicalDevice,uint32_t,Display*,VisualID);
    142 typedef VkResult (APIENTRY *PFN_vkCreateXcbSurfaceKHR)(VkInstance,const VkXcbSurfaceCreateInfoKHR*,const VkAllocationCallbacks*,VkSurfaceKHR*);
    143 typedef VkBool32 (APIENTRY *PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)(VkPhysicalDevice,uint32_t,xcb_connection_t*,xcb_visualid_t);
    144 
    145 #include "posix_thread.h"
    146 #include "posix_time.h"
    147 #include "xkb_unicode.h"
    148 #include "glx_context.h"
    149 #include "egl_context.h"
    150 #include "osmesa_context.h"
    151 #if defined(__linux__)
    152 #include "linux_joystick.h"
    153 #else
    154 #include "null_joystick.h"
    155 #endif
    156 
    157 #define _glfw_dlopen(name) dlopen(name, RTLD_LAZY | RTLD_LOCAL)
    158 #define _glfw_dlclose(handle) dlclose(handle)
    159 #define _glfw_dlsym(handle, name) dlsym(handle, name)
    160 
    161 #define _GLFW_EGL_NATIVE_WINDOW  ((EGLNativeWindowType) window->x11.handle)
    162 #define _GLFW_EGL_NATIVE_DISPLAY ((EGLNativeDisplayType) _glfw.x11.display)
    163 
    164 #define _GLFW_PLATFORM_WINDOW_STATE         _GLFWwindowX11  x11
    165 #define _GLFW_PLATFORM_LIBRARY_WINDOW_STATE _GLFWlibraryX11 x11
    166 #define _GLFW_PLATFORM_MONITOR_STATE        _GLFWmonitorX11 x11
    167 #define _GLFW_PLATFORM_CURSOR_STATE         _GLFWcursorX11  x11
    168 
    169 
    170 // X11-specific per-window data
    171 //
    172 typedef struct _GLFWwindowX11
    173 {
    174     Colormap        colormap;
    175     Window          handle;
    176     XIC             ic;
    177 
    178     GLFWbool        overrideRedirect;
    179     GLFWbool        iconified;
    180     GLFWbool        maximized;
    181 
    182     // Cached position and size used to filter out duplicate events
    183     int             width, height;
    184     int             xpos, ypos;
    185 
    186     // The last received cursor position, regardless of source
    187     int             lastCursorPosX, lastCursorPosY;
    188     // The last position the cursor was warped to by GLFW
    189     int             warpCursorPosX, warpCursorPosY;
    190 
    191     // The time of the last KeyPress event
    192     Time            lastKeyTime;
    193 
    194 } _GLFWwindowX11;
    195 
    196 // X11-specific global data
    197 //
    198 typedef struct _GLFWlibraryX11
    199 {
    200     Display*        display;
    201     int             screen;
    202     Window          root;
    203 
    204     // Helper window for IPC
    205     Window          helperWindowHandle;
    206     // Invisible cursor for hidden cursor mode
    207     Cursor          hiddenCursorHandle;
    208     // Context for mapping window XIDs to _GLFWwindow pointers
    209     XContext        context;
    210     // XIM input method
    211     XIM             im;
    212     // Most recent error code received by X error handler
    213     int             errorCode;
    214     // Primary selection string (while the primary selection is owned)
    215     char*           primarySelectionString;
    216     // Clipboard string (while the selection is owned)
    217     char*           clipboardString;
    218     // Key name string
    219     char            keyName[5];
    220     // X11 keycode to GLFW key LUT
    221     short int       keycodes[256];
    222     // GLFW key to X11 keycode LUT
    223     short int       scancodes[GLFW_KEY_LAST + 1];
    224     // Where to place the cursor when re-enabled
    225     double          restoreCursorPosX, restoreCursorPosY;
    226     // The window whose disabled cursor mode is active
    227     _GLFWwindow*    disabledCursorWindow;
    228 
    229     // Window manager atoms
    230     Atom            WM_PROTOCOLS;
    231     Atom            WM_STATE;
    232     Atom            WM_DELETE_WINDOW;
    233     Atom            NET_WM_NAME;
    234     Atom            NET_WM_ICON_NAME;
    235     Atom            NET_WM_ICON;
    236     Atom            NET_WM_PID;
    237     Atom            NET_WM_PING;
    238     Atom            NET_WM_WINDOW_TYPE;
    239     Atom            NET_WM_WINDOW_TYPE_NORMAL;
    240     Atom            NET_WM_STATE;
    241     Atom            NET_WM_STATE_ABOVE;
    242     Atom            NET_WM_STATE_FULLSCREEN;
    243     Atom            NET_WM_STATE_MAXIMIZED_VERT;
    244     Atom            NET_WM_STATE_MAXIMIZED_HORZ;
    245     Atom            NET_WM_STATE_DEMANDS_ATTENTION;
    246     Atom            NET_WM_BYPASS_COMPOSITOR;
    247     Atom            NET_WM_FULLSCREEN_MONITORS;
    248     Atom            NET_ACTIVE_WINDOW;
    249     Atom            NET_FRAME_EXTENTS;
    250     Atom            NET_REQUEST_FRAME_EXTENTS;
    251     Atom            MOTIF_WM_HINTS;
    252 
    253     // Xdnd (drag and drop) atoms
    254     Atom            XdndAware;
    255     Atom            XdndEnter;
    256     Atom            XdndPosition;
    257     Atom            XdndStatus;
    258     Atom            XdndActionCopy;
    259     Atom            XdndDrop;
    260     Atom            XdndFinished;
    261     Atom            XdndSelection;
    262     Atom            XdndTypeList;
    263     Atom            text_uri_list;
    264 
    265     // Selection (clipboard) atoms
    266     Atom            TARGETS;
    267     Atom            MULTIPLE;
    268     Atom            CLIPBOARD;
    269     Atom            PRIMARY;
    270     Atom            CLIPBOARD_MANAGER;
    271     Atom            SAVE_TARGETS;
    272     Atom            NULL_;
    273     Atom            UTF8_STRING;
    274     Atom            COMPOUND_STRING;
    275     Atom            ATOM_PAIR;
    276     Atom            GLFW_SELECTION;
    277 
    278     struct {
    279         GLFWbool    available;
    280         void*       handle;
    281         int         eventBase;
    282         int         errorBase;
    283         int         major;
    284         int         minor;
    285         GLFWbool    gammaBroken;
    286         GLFWbool    monitorBroken;
    287         PFN_XRRAllocGamma AllocGamma;
    288         PFN_XRRFreeCrtcInfo FreeCrtcInfo;
    289         PFN_XRRFreeGamma FreeGamma;
    290         PFN_XRRFreeOutputInfo FreeOutputInfo;
    291         PFN_XRRFreeScreenResources FreeScreenResources;
    292         PFN_XRRGetCrtcGamma GetCrtcGamma;
    293         PFN_XRRGetCrtcGammaSize GetCrtcGammaSize;
    294         PFN_XRRGetCrtcInfo GetCrtcInfo;
    295         PFN_XRRGetOutputInfo GetOutputInfo;
    296         PFN_XRRGetOutputPrimary GetOutputPrimary;
    297         PFN_XRRGetScreenResourcesCurrent GetScreenResourcesCurrent;
    298         PFN_XRRQueryExtension QueryExtension;
    299         PFN_XRRQueryVersion QueryVersion;
    300         PFN_XRRSelectInput SelectInput;
    301         PFN_XRRSetCrtcConfig SetCrtcConfig;
    302         PFN_XRRSetCrtcGamma SetCrtcGamma;
    303         PFN_XRRUpdateConfiguration UpdateConfiguration;
    304     } randr;
    305 
    306     struct {
    307         GLFWbool    available;
    308         GLFWbool    detectable;
    309         int         majorOpcode;
    310         int         eventBase;
    311         int         errorBase;
    312         int         major;
    313         int         minor;
    314     } xkb;
    315 
    316     struct {
    317         int         count;
    318         int         timeout;
    319         int         interval;
    320         int         blanking;
    321         int         exposure;
    322     } saver;
    323 
    324     struct {
    325         int         version;
    326         Window      source;
    327         Atom        format;
    328     } xdnd;
    329 
    330     struct {
    331         void*       handle;
    332         PFN_XcursorImageCreate ImageCreate;
    333         PFN_XcursorImageDestroy ImageDestroy;
    334         PFN_XcursorImageLoadCursor ImageLoadCursor;
    335     } xcursor;
    336 
    337     struct {
    338         GLFWbool    available;
    339         void*       handle;
    340         int         major;
    341         int         minor;
    342         PFN_XineramaIsActive IsActive;
    343         PFN_XineramaQueryExtension QueryExtension;
    344         PFN_XineramaQueryScreens QueryScreens;
    345     } xinerama;
    346 
    347     struct {
    348         void*       handle;
    349         PFN_XGetXCBConnection GetXCBConnection;
    350     } x11xcb;
    351 
    352     struct {
    353         GLFWbool    available;
    354         void*       handle;
    355         int         eventBase;
    356         int         errorBase;
    357         PFN_XF86VidModeQueryExtension QueryExtension;
    358         PFN_XF86VidModeGetGammaRamp GetGammaRamp;
    359         PFN_XF86VidModeSetGammaRamp SetGammaRamp;
    360         PFN_XF86VidModeGetGammaRampSize GetGammaRampSize;
    361     } vidmode;
    362 
    363     struct {
    364         GLFWbool    available;
    365         void*       handle;
    366         int         majorOpcode;
    367         int         eventBase;
    368         int         errorBase;
    369         int         major;
    370         int         minor;
    371         PFN_XIQueryVersion QueryVersion;
    372         PFN_XISelectEvents SelectEvents;
    373     } xi;
    374 
    375 } _GLFWlibraryX11;
    376 
    377 // X11-specific per-monitor data
    378 //
    379 typedef struct _GLFWmonitorX11
    380 {
    381     RROutput        output;
    382     RRCrtc          crtc;
    383     RRMode          oldMode;
    384 
    385     // Index of corresponding Xinerama screen,
    386     // for EWMH full screen window placement
    387     int             index;
    388 
    389 } _GLFWmonitorX11;
    390 
    391 // X11-specific per-cursor data
    392 //
    393 typedef struct _GLFWcursorX11
    394 {
    395     Cursor handle;
    396 
    397 } _GLFWcursorX11;
    398 
    399 
    400 void _glfwPollMonitorsX11(void);
    401 GLFWbool _glfwSetVideoModeX11(_GLFWmonitor* monitor, const GLFWvidmode* desired);
    402 void _glfwRestoreVideoModeX11(_GLFWmonitor* monitor);
    403 
    404 Cursor _glfwCreateCursorX11(const GLFWimage* image, int xhot, int yhot);
    405 
    406 unsigned long _glfwGetWindowPropertyX11(Window window,
    407                                         Atom property,
    408                                         Atom type,
    409                                         unsigned char** value);
    410 
    411 void _glfwGrabErrorHandlerX11(void);
    412 void _glfwReleaseErrorHandlerX11(void);
    413 void _glfwInputErrorX11(int error, const char* message);
    414 
    415 void _glfwPushSelectionToManagerX11(void);
    416