src/video/windowsrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Sat, 09 Feb 2013 14:35:06 -0500
changeset 8411 aca0591bc58f
parent 8401 29ecd2eca4af
child 8412 59f1191a5cca
permissions -rw-r--r--
WinRT: provided access, via SDL_GetWindowWMInfo, to SDL's WinRT CoreWindow
dludwig@8327
     1
/*
dludwig@8327
     2
  Simple DirectMedia Layer
dludwig@8327
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
dludwig@8327
     4
dludwig@8327
     5
  This software is provided 'as-is', without any express or implied
dludwig@8327
     6
  warranty.  In no event will the authors be held liable for any damages
dludwig@8327
     7
  arising from the use of this software.
dludwig@8327
     8
dludwig@8327
     9
  Permission is granted to anyone to use this software for any purpose,
dludwig@8327
    10
  including commercial applications, and to alter it and redistribute it
dludwig@8327
    11
  freely, subject to the following restrictions:
dludwig@8327
    12
dludwig@8327
    13
  1. The origin of this software must not be misrepresented; you must not
dludwig@8327
    14
     claim that you wrote the original software. If you use this software
dludwig@8327
    15
     in a product, an acknowledgment in the product documentation would be
dludwig@8327
    16
     appreciated but is not required.
dludwig@8327
    17
  2. Altered source versions must be plainly marked as such, and must not be
dludwig@8327
    18
     misrepresented as being the original software.
dludwig@8327
    19
  3. This notice may not be removed or altered from any source distribution.
dludwig@8327
    20
*/
dludwig@8327
    21
#include "SDL_config.h"
dludwig@8327
    22
dludwig@8327
    23
#if SDL_VIDEO_DRIVER_WINRT
dludwig@8327
    24
dludwig@8327
    25
/* WinRT SDL video driver implementation
dludwig@8327
    26
dludwig@8327
    27
   Initial work on this was done by David Ludwig (dludwig@pobox.com), and
dludwig@8327
    28
   was based off of SDL's "dummy" video driver.
dludwig@8327
    29
 */
dludwig@8327
    30
dludwig@8329
    31
extern "C" {
dludwig@8327
    32
#include "SDL_video.h"
dludwig@8327
    33
#include "SDL_mouse.h"
dludwig@8327
    34
#include "../SDL_sysvideo.h"
dludwig@8327
    35
#include "../SDL_pixels_c.h"
dludwig@8327
    36
#include "../../events/SDL_events_c.h"
dludwig@8400
    37
#include "../../render/SDL_sysrender.h"
dludwig@8411
    38
#include "SDL_syswm.h"
dludwig@8329
    39
}
dludwig@8327
    40
dludwig@8329
    41
#include "SDL_WinRTApp.h"
dludwig@8327
    42
#include "SDL_winrtvideo.h"
dludwig@8327
    43
#include "SDL_winrtevents_c.h"
dludwig@8327
    44
#include "SDL_winrtframebuffer_c.h"
dludwig@8374
    45
#include "SDL_winrtmouse.h"
dludwig@8327
    46
dludwig@8333
    47
/* On Windows, windows.h defines CreateWindow */
dludwig@8333
    48
#ifdef CreateWindow
dludwig@8333
    49
#undef CreateWindow
dludwig@8333
    50
#endif
dludwig@8333
    51
dludwig@8332
    52
extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
dludwig@8332
    53
dludwig@8368
    54
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
    55
dludwig@8327
    56
/* Initialization/Query functions */
dludwig@8327
    57
static int WINRT_VideoInit(_THIS);
dludwig@8374
    58
static int WINRT_InitModes(_THIS);
dludwig@8327
    59
static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
dludwig@8327
    60
static void WINRT_VideoQuit(_THIS);
dludwig@8327
    61
dludwig@8333
    62
/* Window functions */
dludwig@8333
    63
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@8333
    64
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    65
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    66
dludwig@8327
    67
/* WinRT driver bootstrap functions */
dludwig@8327
    68
dludwig@8327
    69
static int
dludwig@8327
    70
WINRT_Available(void)
dludwig@8327
    71
{
dludwig@8328
    72
    return (1);
dludwig@8327
    73
}
dludwig@8327
    74
dludwig@8327
    75
static void
dludwig@8327
    76
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
    77
{
dludwig@8327
    78
    SDL_free(device);
dludwig@8327
    79
}
dludwig@8327
    80
dludwig@8327
    81
static SDL_VideoDevice *
dludwig@8327
    82
WINRT_CreateDevice(int devindex)
dludwig@8327
    83
{
dludwig@8327
    84
    SDL_VideoDevice *device;
dludwig@8327
    85
dludwig@8327
    86
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
    87
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
    88
    if (!device) {
dludwig@8327
    89
        SDL_OutOfMemory();
dludwig@8327
    90
        if (device) {
dludwig@8327
    91
            SDL_free(device);
dludwig@8327
    92
        }
dludwig@8327
    93
        return (0);
dludwig@8327
    94
    }
dludwig@8327
    95
dludwig@8327
    96
    /* Set the function pointers */
dludwig@8327
    97
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
    98
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
    99
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   100
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   101
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   102
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8327
   103
    device->CreateWindowFramebuffer = SDL_WINRT_CreateWindowFramebuffer;
dludwig@8327
   104
    device->UpdateWindowFramebuffer = SDL_WINRT_UpdateWindowFramebuffer;
dludwig@8327
   105
    device->DestroyWindowFramebuffer = SDL_WINRT_DestroyWindowFramebuffer;
dludwig@8411
   106
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8327
   107
dludwig@8327
   108
    device->free = WINRT_DeleteDevice;
dludwig@8327
   109
dludwig@8327
   110
    return device;
dludwig@8327
   111
}
dludwig@8327
   112
dludwig@8327
   113
VideoBootStrap WINRT_bootstrap = {
dludwig@8327
   114
    WINRTVID_DRIVER_NAME, "SDL Windows RT video driver",
dludwig@8327
   115
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   116
};
dludwig@8327
   117
dludwig@8327
   118
int
dludwig@8327
   119
WINRT_VideoInit(_THIS)
dludwig@8327
   120
{
dludwig@8374
   121
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   122
        return -1;
dludwig@8374
   123
    }
dludwig@8374
   124
dludwig@8374
   125
    WINRT_InitMouse(_this);
dludwig@8374
   126
dludwig@8374
   127
    return 0;
dludwig@8374
   128
}
dludwig@8374
   129
dludwig@8374
   130
static int
dludwig@8374
   131
WINRT_InitModes(_THIS)
dludwig@8374
   132
{
dludwig@8329
   133
    SDL_DisplayMode mode = SDL_WinRTGlobalApp->GetMainDisplayMode();
dludwig@8329
   134
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8329
   135
        return -1;
dludwig@8374
   136
    }
dludwig@8374
   137
dludwig@8374
   138
    SDL_AddDisplayMode(&_this->displays[0], &mode);
dludwig@8327
   139
    return 0;
dludwig@8327
   140
}
dludwig@8327
   141
dludwig@8327
   142
static int
dludwig@8327
   143
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   144
{
dludwig@8327
   145
    return 0;
dludwig@8327
   146
}
dludwig@8327
   147
dludwig@8327
   148
void
dludwig@8327
   149
WINRT_VideoQuit(_THIS)
dludwig@8327
   150
{
dludwig@8374
   151
    WINRT_QuitMouse(_this);
dludwig@8327
   152
}
dludwig@8327
   153
dludwig@8333
   154
int
dludwig@8333
   155
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8333
   156
{
dludwig@8334
   157
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8334
   158
    // support is added.
dludwig@8334
   159
    if (SDL_WinRTGlobalApp->HasSDLWindowData())
dludwig@8334
   160
    {
dludwig@8334
   161
        SDL_SetError("WinRT only supports one window");
dludwig@8334
   162
        return -1;
dludwig@8334
   163
    }
dludwig@8333
   164
dludwig@8411
   165
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8333
   166
    if (!data) {
dludwig@8333
   167
        SDL_OutOfMemory();
dludwig@8333
   168
        return -1;
dludwig@8333
   169
    }
dludwig@8333
   170
    data->sdlWindow = window;
dludwig@8411
   171
    data->coreWindow = new CoreWindow^(CoreWindow::GetForCurrentThread());
dludwig@8333
   172
dludwig@8367
   173
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8367
   174
       and is considered fullscreen, shown, and the like.
dludwig@8367
   175
    */
dludwig@8333
   176
    window->x = 0;
dludwig@8333
   177
    window->y = 0;
dludwig@8367
   178
    window->flags =
dludwig@8367
   179
        SDL_WINDOW_FULLSCREEN |
dludwig@8367
   180
        SDL_WINDOW_SHOWN |
dludwig@8367
   181
        SDL_WINDOW_BORDERLESS |
dludwig@8367
   182
        SDL_WINDOW_MAXIMIZED |
dludwig@8367
   183
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8333
   184
dludwig@8367
   185
    /* HACK from DLudwig: The following line of code prevents
dludwig@8367
   186
       SDL_CreateWindow and SDL_UpdateFullscreenMode from trying to resize
dludwig@8367
   187
       the window after the call to WINRT_CreateWindow returns.
dludwig@8367
   188
       
dludwig@8367
   189
       This hack should allow a window to be created in virtually any size,
dludwig@8367
   190
       and more importantly, it allows a window's framebuffer, as created and
dludwig@8367
   191
       retrieved via SDL_GetWindowSurface, to be in any size.  This can be
dludwig@8367
   192
       utilized by apps centered around software rendering, such as ports
dludwig@8367
   193
       of older apps.  The app can have SDL create a framebuffer in any size
dludwig@8367
   194
       it chooses.  SDL will scale the framebuffer to the native
dludwig@8367
   195
       screen size on the GPU (via SDL_UpdateWindowSurface).
dludwig@8367
   196
    */
dludwig@8367
   197
    _this->displays[0].fullscreen_window = window;
dludwig@8367
   198
dludwig@8367
   199
    /* Further prevent any display resizing, and make sure SDL_GetWindowDisplayMode
dludwig@8367
   200
       can report the correct size of windows, by creating a new display
dludwig@8367
   201
       mode in the requested size.  To note, if the window is being created in
dludwig@8367
   202
       the device's native screen size, SDL_AddDisplayMode will do nothing.
dludwig@8367
   203
    */
dludwig@8367
   204
    window->fullscreen_mode = SDL_WinRTGlobalApp->GetMainDisplayMode();
dludwig@8367
   205
    window->fullscreen_mode.w = window->w;
dludwig@8367
   206
    window->fullscreen_mode.h = window->h;
dludwig@8367
   207
    SDL_AddDisplayMode(&_this->displays[0], &window->fullscreen_mode);
dludwig@8367
   208
dludwig@8367
   209
    /* TODO: Consider removing custom display modes in WINRT_DestroyWindow. */
dludwig@8367
   210
 
dludwig@8367
   211
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8367
   212
       behalf of SDL:
dludwig@8367
   213
    */
dludwig@8333
   214
    SDL_WinRTGlobalApp->SetSDLWindowData(data);
dludwig@8333
   215
dludwig@8400
   216
    /* For now, create a Direct3D 11 renderer up-front.  Eventually, this
dludwig@8400
   217
       won't be done in WINRT_CreateWindow, although it may get done in
dludwig@8400
   218
       SDL_WINRT_CreateWindowFramebuffer.
dludwig@8400
   219
    */
dludwig@8400
   220
dludwig@8400
   221
    // Link SDL_winrtrenderer to the SDL_Renderer temporarily,
dludwig@8400
   222
    // for refactoring purposes.  Initialize the SDL_Renderer
dludwig@8400
   223
    // first in order to give it the opportunity to create key
dludwig@8400
   224
    // resources first.
dludwig@8401
   225
    //
dludwig@8401
   226
    // TODO, WinRT: either make WINRT_CreateWindow not call SDL_CreateRenderer, or have it do error checking if it does call it
dludwig@8411
   227
dludwig@8411
   228
    // HACK: make sure the SDL window references SDL_WindowData data now, in
dludwig@8411
   229
    // order to allow the SDL_Renderer to be created in WINRT_CreateWindow
dludwig@8411
   230
    window->driverdata = data;
dludwig@8411
   231
dludwig@8400
   232
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);
dludwig@8401
   233
    SDL_WinRTGlobalApp->m_renderer->m_sdlRenderer = renderer;
dludwig@8400
   234
    SDL_WinRTGlobalApp->m_renderer->m_sdlRendererData = (D3D11_RenderData *) renderer->driverdata;
dludwig@8400
   235
    SDL_WinRTGlobalApp->m_renderer->Initialize(CoreWindow::GetForCurrentThread());
dludwig@8400
   236
dludwig@8367
   237
    /* All done! */
dludwig@8333
   238
    return 0;
dludwig@8333
   239
}
dludwig@8333
   240
dludwig@8333
   241
void
dludwig@8333
   242
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   243
{
dludwig@8411
   244
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   245
dludwig@8411
   246
    if (data) {
dludwig@8411
   247
        // Delete the reference to the WinRT CoreWindow:
dludwig@8411
   248
        CoreWindow ^* windowPointer = ((SDL_WindowData *) window->driverdata)->coreWindow;
dludwig@8411
   249
        if (windowPointer) {
dludwig@8411
   250
            *windowPointer = nullptr;   // Clear the C++/CX reference to the CoreWindow
dludwig@8411
   251
            delete windowPointer;       // Delete the C++/CX reference itself
dludwig@8411
   252
        }
dludwig@8411
   253
dludwig@8411
   254
        // Delete the internal window data:
dludwig@8411
   255
        delete data;
dludwig@8411
   256
        data = NULL;
dludwig@8411
   257
    }
dludwig@8411
   258
dludwig@8335
   259
    if (SDL_WinRTGlobalApp->HasSDLWindowData() &&
dludwig@8335
   260
        SDL_WinRTGlobalApp->GetSDLWindowData()->sdlWindow == window)
dludwig@8334
   261
    {
dludwig@8334
   262
        SDL_WinRTGlobalApp->SetSDLWindowData(NULL);
dludwig@8334
   263
    }
dludwig@8333
   264
}
dludwig@8333
   265
dludwig@8411
   266
SDL_bool
dludwig@8411
   267
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8411
   268
{
dludwig@8411
   269
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   270
    CoreWindow ^* windowPointer = data->coreWindow;
dludwig@8411
   271
dludwig@8411
   272
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8411
   273
        info->subsystem = SDL_SYSWM_WINDOWSRT;
dludwig@8411
   274
        info->info.winrt.window = windowPointer;
dludwig@8411
   275
        return SDL_TRUE;
dludwig@8411
   276
    } else {
dludwig@8411
   277
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8411
   278
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8411
   279
        return SDL_FALSE;
dludwig@8411
   280
    }
dludwig@8411
   281
    return SDL_FALSE;
dludwig@8411
   282
}
dludwig@8333
   283
dludwig@8327
   284
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   285
dludwig@8327
   286
/* vi: set ts=4 sw=4 expandtab: */