include/SDL_syswm.h
author Ryan C. Gordon <icculus@icculus.org>
Sun, 11 Jun 2017 00:50:26 -0400
changeset 11088 69452f9839d5
parent 11065 9e8d300d50da
child 11287 4d2a6202caa4
permissions -rw-r--r--
syswm: prevent buffer overflow if SDL and app have different config headers.

This only affects Wayland and DirectFB, as a Unix system generally has X11
support. Other platforms also have different sizes for the C union in
question, but are likely the only target for that platform, etc.

Apps that might run on Wayland or DirectFB will need to be compiled against
new headers from an official 2.0.6 release, or be prepared to force the x11
target, or not use SDL_GetWindowWMInfo().

Fixes Bugzilla #3428.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 /**
    23  *  \file SDL_syswm.h
    24  *
    25  *  Include file for SDL custom system window manager hooks.
    26  */
    27 
    28 #ifndef SDL_syswm_h_
    29 #define SDL_syswm_h_
    30 
    31 #include "SDL_stdinc.h"
    32 #include "SDL_error.h"
    33 #include "SDL_video.h"
    34 #include "SDL_version.h"
    35 
    36 #include "begin_code.h"
    37 /* Set up for C function definitions, even when using C++ */
    38 #ifdef __cplusplus
    39 extern "C" {
    40 #endif
    41 
    42 /**
    43  *  \file SDL_syswm.h
    44  *
    45  *  Your application has access to a special type of event ::SDL_SYSWMEVENT,
    46  *  which contains window-manager specific information and arrives whenever
    47  *  an unhandled window event occurs.  This event is ignored by default, but
    48  *  you can enable it with SDL_EventState().
    49  */
    50 #ifdef SDL_PROTOTYPES_ONLY
    51 struct SDL_SysWMinfo;
    52 #else
    53 
    54 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
    55 #ifndef WIN32_LEAN_AND_MEAN
    56 #define WIN32_LEAN_AND_MEAN
    57 #endif
    58 #include <windows.h>
    59 #endif
    60 
    61 #if defined(SDL_VIDEO_DRIVER_WINRT)
    62 #include <Inspectable.h>
    63 #endif
    64 
    65 /* This is the structure for custom window manager events */
    66 #if defined(SDL_VIDEO_DRIVER_X11)
    67 #if defined(__APPLE__) && defined(__MACH__)
    68 /* conflicts with Quickdraw.h */
    69 #define Cursor X11Cursor
    70 #endif
    71 
    72 #include <X11/Xlib.h>
    73 #include <X11/Xatom.h>
    74 
    75 #if defined(__APPLE__) && defined(__MACH__)
    76 /* matches the re-define above */
    77 #undef Cursor
    78 #endif
    79 
    80 #endif /* defined(SDL_VIDEO_DRIVER_X11) */
    81 
    82 #if defined(SDL_VIDEO_DRIVER_DIRECTFB)
    83 #include <directfb.h>
    84 #endif
    85 
    86 #if defined(SDL_VIDEO_DRIVER_COCOA)
    87 #ifdef __OBJC__
    88 @class NSWindow;
    89 #else
    90 typedef struct _NSWindow NSWindow;
    91 #endif
    92 #endif
    93 
    94 #if defined(SDL_VIDEO_DRIVER_UIKIT)
    95 #ifdef __OBJC__
    96 #include <UIKit/UIKit.h>
    97 #else
    98 typedef struct _UIWindow UIWindow;
    99 typedef struct _UIViewController UIViewController;
   100 #endif
   101 typedef Uint32 GLuint;
   102 #endif
   103 
   104 #if defined(SDL_VIDEO_DRIVER_ANDROID)
   105 typedef struct ANativeWindow ANativeWindow;
   106 typedef void *EGLSurface;
   107 #endif
   108 
   109 #if defined(SDL_VIDEO_DRIVER_VIVANTE)
   110 #include "SDL_egl.h"
   111 #endif
   112 
   113 /**
   114  *  These are the various supported windowing subsystems
   115  */
   116 typedef enum
   117 {
   118     SDL_SYSWM_UNKNOWN,
   119     SDL_SYSWM_WINDOWS,
   120     SDL_SYSWM_X11,
   121     SDL_SYSWM_DIRECTFB,
   122     SDL_SYSWM_COCOA,
   123     SDL_SYSWM_UIKIT,
   124     SDL_SYSWM_WAYLAND,
   125     SDL_SYSWM_MIR,
   126     SDL_SYSWM_WINRT,
   127     SDL_SYSWM_ANDROID,
   128     SDL_SYSWM_VIVANTE
   129 } SDL_SYSWM_TYPE;
   130 
   131 /**
   132  *  The custom event structure.
   133  */
   134 struct SDL_SysWMmsg
   135 {
   136     SDL_version version;
   137     SDL_SYSWM_TYPE subsystem;
   138     union
   139     {
   140 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
   141         struct {
   142             HWND hwnd;                  /**< The window for the message */
   143             UINT msg;                   /**< The type of message */
   144             WPARAM wParam;              /**< WORD message parameter */
   145             LPARAM lParam;              /**< LONG message parameter */
   146         } win;
   147 #endif
   148 #if defined(SDL_VIDEO_DRIVER_X11)
   149         struct {
   150             XEvent event;
   151         } x11;
   152 #endif
   153 #if defined(SDL_VIDEO_DRIVER_DIRECTFB)
   154         struct {
   155             DFBEvent event;
   156         } dfb;
   157 #endif
   158 #if defined(SDL_VIDEO_DRIVER_COCOA)
   159         struct
   160         {
   161             /* Latest version of Xcode clang complains about empty structs in C v. C++:
   162                  error: empty struct has size 0 in C, size 1 in C++
   163              */
   164             int dummy;
   165             /* No Cocoa window events yet */
   166         } cocoa;
   167 #endif
   168 #if defined(SDL_VIDEO_DRIVER_UIKIT)
   169         struct
   170         {
   171             int dummy;
   172             /* No UIKit window events yet */
   173         } uikit;
   174 #endif
   175 #if defined(SDL_VIDEO_DRIVER_VIVANTE)
   176         struct
   177         {
   178             int dummy;
   179             /* No Vivante window events yet */
   180         } vivante;
   181 #endif
   182         /* Can't have an empty union */
   183         int dummy;
   184     } msg;
   185 };
   186 
   187 /**
   188  *  The custom window manager information structure.
   189  *
   190  *  When this structure is returned, it holds information about which
   191  *  low level system it is using, and will be one of SDL_SYSWM_TYPE.
   192  */
   193 struct SDL_SysWMinfo
   194 {
   195     SDL_version version;
   196     SDL_SYSWM_TYPE subsystem;
   197     union
   198     {
   199 #if defined(SDL_VIDEO_DRIVER_WINDOWS)
   200         struct
   201         {
   202             HWND window;                /**< The window handle */
   203             HDC hdc;                    /**< The window device context */
   204             HINSTANCE hinstance;        /**< The instance handle */
   205         } win;
   206 #endif
   207 #if defined(SDL_VIDEO_DRIVER_WINRT)
   208         struct
   209         {
   210             IInspectable * window;      /**< The WinRT CoreWindow */
   211         } winrt;
   212 #endif
   213 #if defined(SDL_VIDEO_DRIVER_X11)
   214         struct
   215         {
   216             Display *display;           /**< The X11 display */
   217             Window window;              /**< The X11 window */
   218         } x11;
   219 #endif
   220 #if defined(SDL_VIDEO_DRIVER_DIRECTFB)
   221         struct
   222         {
   223             IDirectFB *dfb;             /**< The directfb main interface */
   224             IDirectFBWindow *window;    /**< The directfb window handle */
   225             IDirectFBSurface *surface;  /**< The directfb client surface */
   226         } dfb;
   227 #endif
   228 #if defined(SDL_VIDEO_DRIVER_COCOA)
   229         struct
   230         {
   231 #if defined(__OBJC__) && defined(__has_feature) && __has_feature(objc_arc)
   232             NSWindow __unsafe_unretained *window; /**< The Cocoa window */
   233 #else
   234             NSWindow *window;                     /**< The Cocoa window */
   235 #endif
   236         } cocoa;
   237 #endif
   238 #if defined(SDL_VIDEO_DRIVER_UIKIT)
   239         struct
   240         {
   241 #if defined(__OBJC__) && defined(__has_feature) && __has_feature(objc_arc)
   242             UIWindow __unsafe_unretained *window; /**< The UIKit window */
   243 #else
   244             UIWindow *window;                     /**< The UIKit window */
   245 #endif
   246             GLuint framebuffer; /**< The GL view's Framebuffer Object. It must be bound when rendering to the screen using GL. */
   247             GLuint colorbuffer; /**< The GL view's color Renderbuffer Object. It must be bound when SDL_GL_SwapWindow is called. */
   248             GLuint resolveFramebuffer; /**< The Framebuffer Object which holds the resolve color Renderbuffer, when MSAA is used. */
   249         } uikit;
   250 #endif
   251 #if defined(SDL_VIDEO_DRIVER_WAYLAND)
   252         struct
   253         {
   254             struct wl_display *display;            /**< Wayland display */
   255             struct wl_surface *surface;            /**< Wayland surface */
   256             struct wl_shell_surface *shell_surface; /**< Wayland shell_surface (window manager handle) */
   257         } wl;
   258 #endif
   259 #if defined(SDL_VIDEO_DRIVER_MIR)
   260         struct
   261         {
   262             struct MirConnection *connection;  /**< Mir display server connection */
   263             struct MirSurface *surface;  /**< Mir surface */
   264         } mir;
   265 #endif
   266 
   267 #if defined(SDL_VIDEO_DRIVER_ANDROID)
   268         struct
   269         {
   270             ANativeWindow *window;
   271             EGLSurface surface;
   272         } android;
   273 #endif
   274 
   275 #if defined(SDL_VIDEO_DRIVER_VIVANTE)
   276         struct
   277         {
   278             EGLNativeDisplayType display;
   279             EGLNativeWindowType window;
   280         } vivante;
   281 #endif
   282 
   283         /* Make sure this union is always 64 bytes (8 64-bit pointers). */
   284         /* Be careful not to overflow this if you add a new target! */
   285         Uint8 dummy[64];
   286     } info;
   287 };
   288 
   289 #endif /* SDL_PROTOTYPES_ONLY */
   290 
   291 typedef struct SDL_SysWMinfo SDL_SysWMinfo;
   292 
   293 /* Function prototypes */
   294 /**
   295  *  \brief This function allows access to driver-dependent window information.
   296  *
   297  *  \param window The window about which information is being requested
   298  *  \param info This structure must be initialized with the SDL version, and is
   299  *              then filled in with information about the given window.
   300  *
   301  *  \return SDL_TRUE if the function is implemented and the version member of
   302  *          the \c info struct is valid, SDL_FALSE otherwise.
   303  *
   304  *  You typically use this function like this:
   305  *  \code
   306  *  SDL_SysWMinfo info;
   307  *  SDL_VERSION(&info.version);
   308  *  if ( SDL_GetWindowWMInfo(window, &info) ) { ... }
   309  *  \endcode
   310  */
   311 extern DECLSPEC SDL_bool SDLCALL SDL_GetWindowWMInfo(SDL_Window * window,
   312                                                      SDL_SysWMinfo * info);
   313 
   314 
   315 /* Ends C function definitions when using C++ */
   316 #ifdef __cplusplus
   317 }
   318 #endif
   319 #include "close_code.h"
   320 
   321 #endif /* SDL_syswm_h_ */
   322 
   323 /* vi: set ts=4 sw=4 expandtab: */