src/video/win32/SDL_win32video.c
author Eli Gottlieb <eligottlieb@gmail.com>
Fri, 09 Jul 2010 20:24:44 -0400
changeset 4788 0bfda420c936
parent 3697 f7b03b6838cb
child 4809 329708ffe2a7
permissions -rw-r--r--
Put in a couple of fixes that I realized hadn't gotten done when I wrote out the SCRUM stuff in TODO. Added SCRUM listings in TODO.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_main.h"
    25 #include "SDL_video.h"
    26 #include "SDL_mouse.h"
    27 #include "../SDL_sysvideo.h"
    28 #include "../SDL_pixels_c.h"
    29 
    30 #include "SDL_win32video.h"
    31 #include "SDL_win32shape.h"
    32 #include "SDL_d3drender.h"
    33 #include "SDL_gdirender.h"
    34 
    35 /* Initialization/Query functions */
    36 static int WIN_VideoInit(_THIS);
    37 static void WIN_VideoQuit(_THIS);
    38 
    39 int total_mice = 0;             /* total mouse count */
    40 HANDLE *mice = NULL;            /* the handles to the detected mice */
    41 HCTX *g_hCtx = NULL;            /* handles to tablet contexts */
    42 int tablet = -1;                /* we're assuming that there is no tablet */
    43 
    44 /* WIN32 driver bootstrap functions */
    45 
    46 static int
    47 WIN_Available(void)
    48 {
    49     return (1);
    50 }
    51 
    52 static void
    53 WIN_DeleteDevice(SDL_VideoDevice * device)
    54 {
    55     SDL_VideoData *data = (SDL_VideoData *) device->driverdata;
    56 
    57     SDL_UnregisterApp();
    58 #if SDL_VIDEO_RENDER_D3D
    59     if (data->d3d) {
    60         IDirect3D9_Release(data->d3d);
    61         FreeLibrary(data->d3dDLL);
    62     }
    63 #endif
    64 #if SDL_VIDEO_RENDER_DDRAW
    65     if (data->ddraw) {
    66         data->ddraw->lpVtbl->Release(data->ddraw);
    67         FreeLibrary(data->ddrawDLL);
    68     }
    69 #endif
    70     if (data->wintabDLL) {
    71         FreeLibrary(data->wintabDLL);
    72     }
    73     SDL_free(device->driverdata);
    74     SDL_free(device);
    75 }
    76 
    77 static SDL_VideoDevice *
    78 WIN_CreateDevice(int devindex)
    79 {
    80     SDL_VideoDevice *device;
    81     SDL_VideoData *data;
    82 
    83     SDL_RegisterApp(NULL, 0, NULL);
    84 
    85     /* Initialize all variables that we clean on shutdown */
    86     device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
    87     if (device) {
    88         data = (struct SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
    89     }
    90     if (!device || !data) {
    91         SDL_OutOfMemory();
    92         if (device) {
    93             SDL_free(device);
    94         }
    95         return NULL;
    96     }
    97     device->driverdata = data;
    98 
    99 #if SDL_VIDEO_RENDER_D3D
   100     data->d3dDLL = LoadLibrary(TEXT("D3D9.DLL"));
   101     if (data->d3dDLL) {
   102         IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
   103 
   104         D3DCreate =
   105             (IDirect3D9 * (WINAPI *) (UINT)) GetProcAddress(data->d3dDLL,
   106                                                             "Direct3DCreate9");
   107         if (D3DCreate) {
   108             data->d3d = D3DCreate(D3D_SDK_VERSION);
   109         }
   110         if (!data->d3d) {
   111             FreeLibrary(data->d3dDLL);
   112             data->d3dDLL = NULL;
   113         }
   114     }
   115 #endif /* SDL_VIDEO_RENDER_D3D */
   116 #if SDL_VIDEO_RENDER_DDRAW
   117     data->ddrawDLL = LoadLibrary(TEXT("ddraw.dll"));
   118     if (data->ddrawDLL) {
   119         IDirectDraw *(WINAPI * DDCreate) (GUID FAR * lpGUID,
   120                                           LPDIRECTDRAW FAR * lplpDD,
   121                                           IUnknown FAR * pUnkOuter);
   122 
   123         DDCreate =
   124             (IDirectDraw *
   125              (WINAPI *) (GUID FAR *, LPDIRECTDRAW FAR *, IUnknown FAR *))
   126             GetProcAddress(data->ddrawDLL, TEXT("DirectDrawCreate"));
   127         if (!DDCreate || DDCreate(NULL, &data->ddraw, NULL) != DD_OK) {
   128             FreeLibrary(data->ddrawDLL);
   129             data->ddrawDLL = NULL;
   130             data->ddraw = NULL;
   131         }
   132     }
   133 #endif /* SDL_VIDEO_RENDER_DDRAW */
   134 
   135     data->wintabDLL = LoadLibrary(TEXT("WINTAB32.DLL"));
   136     if (data->wintabDLL) {
   137 #define PROCNAME(X) #X
   138         data->WTInfoA =
   139             (UINT(*)(UINT, UINT, LPVOID)) GetProcAddress(data->wintabDLL,
   140                                                          PROCNAME(WTInfoA));
   141         data->WTOpenA =
   142             (HCTX(*)(HWND, LPLOGCONTEXTA, BOOL)) GetProcAddress(data->
   143                                                                 wintabDLL,
   144                                                                 PROCNAME
   145                                                                 (WTOpenA));
   146         data->WTPacket =
   147             (int (*)(HCTX, UINT, LPVOID)) GetProcAddress(data->wintabDLL,
   148                                                          PROCNAME(WTPacket));
   149         data->WTClose =
   150             (BOOL(*)(HCTX)) GetProcAddress(data->wintabDLL,
   151                                            PROCNAME(WTClose));
   152 #undef PROCNAME
   153 
   154         if (!data->WTInfoA || !data->WTOpenA || !data->WTPacket
   155             || !data->WTClose) {
   156             FreeLibrary(data->wintabDLL);
   157             data->wintabDLL = NULL;
   158         }
   159     }
   160 
   161     /* Set the function pointers */
   162     device->VideoInit = WIN_VideoInit;
   163     device->VideoQuit = WIN_VideoQuit;
   164     device->GetDisplayBounds = WIN_GetDisplayBounds;
   165     device->GetDisplayModes = WIN_GetDisplayModes;
   166     device->SetDisplayMode = WIN_SetDisplayMode;
   167     device->SetDisplayGammaRamp = WIN_SetDisplayGammaRamp;
   168     device->GetDisplayGammaRamp = WIN_GetDisplayGammaRamp;
   169     device->PumpEvents = WIN_PumpEvents;
   170 
   171 #undef CreateWindow
   172     device->CreateWindow = WIN_CreateWindow;
   173     device->CreateWindowFrom = WIN_CreateWindowFrom;
   174     device->SetWindowTitle = WIN_SetWindowTitle;
   175     device->SetWindowIcon = WIN_SetWindowIcon;
   176     device->SetWindowPosition = WIN_SetWindowPosition;
   177     device->SetWindowSize = WIN_SetWindowSize;
   178     device->ShowWindow = WIN_ShowWindow;
   179     device->HideWindow = WIN_HideWindow;
   180     device->RaiseWindow = WIN_RaiseWindow;
   181     device->MaximizeWindow = WIN_MaximizeWindow;
   182     device->MinimizeWindow = WIN_MinimizeWindow;
   183     device->RestoreWindow = WIN_RestoreWindow;
   184     device->SetWindowGrab = WIN_SetWindowGrab;
   185     device->DestroyWindow = WIN_DestroyWindow;
   186     device->GetWindowWMInfo = WIN_GetWindowWMInfo;
   187     
   188     device->shape_driver.CreateShaper = Win32_CreateShaper;
   189     device->shape_driver.SetWindowShape = Win32_SetWindowShape;
   190     device->shape_driver.ResizeWindowShape = Win32_ResizeWindowShape;
   191     
   192 #ifdef SDL_VIDEO_OPENGL_WGL
   193     device->GL_LoadLibrary = WIN_GL_LoadLibrary;
   194     device->GL_GetProcAddress = WIN_GL_GetProcAddress;
   195     device->GL_UnloadLibrary = WIN_GL_UnloadLibrary;
   196     device->GL_CreateContext = WIN_GL_CreateContext;
   197     device->GL_MakeCurrent = WIN_GL_MakeCurrent;
   198     device->GL_SetSwapInterval = WIN_GL_SetSwapInterval;
   199     device->GL_GetSwapInterval = WIN_GL_GetSwapInterval;
   200     device->GL_SwapWindow = WIN_GL_SwapWindow;
   201     device->GL_DeleteContext = WIN_GL_DeleteContext;
   202 #endif
   203 
   204     device->free = WIN_DeleteDevice;
   205 
   206     return device;
   207 }
   208 
   209 VideoBootStrap WIN32_bootstrap = {
   210     "win32", "SDL Win32/64 video driver", WIN_Available, WIN_CreateDevice
   211 };
   212 
   213 
   214 int
   215 WIN_VideoInit(_THIS)
   216 {
   217     if (WIN_InitModes(_this) < 0) {
   218         return -1;
   219     }
   220 
   221 #if SDL_VIDEO_RENDER_D3D
   222     D3D_AddRenderDriver(_this);
   223 #endif
   224 #if SDL_VIDEO_RENDER_DDRAW
   225     DDRAW_AddRenderDriver(_this);
   226 #endif
   227 #if SDL_VIDEO_RENDER_GDI
   228     GDI_AddRenderDriver(_this);
   229 #endif
   230 #if SDL_VIDEO_RENDER_GAPI
   231     GAPI_AddRenderDriver(_this);
   232 #endif
   233 
   234     g_hCtx = SDL_malloc(sizeof(HCTX));
   235     WIN_InitKeyboard(_this);
   236     WIN_InitMouse(_this);
   237 
   238     return 0;
   239 }
   240 
   241 void
   242 WIN_VideoQuit(_THIS)
   243 {
   244     WIN_QuitModes(_this);
   245     WIN_QuitKeyboard(_this);
   246     WIN_QuitMouse(_this);
   247     SDL_free(g_hCtx);
   248 }
   249 
   250 /* vim: set ts=4 sw=4 expandtab: */