src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Mon, 16 Sep 2013 11:02:18 -0400
changeset 8527 e3807733fad5
parent 8522 3ae9dc9c439f
child 8541 0041edb891f3
permissions -rw-r--r--
WinRT: made SDL_GetWindowWMInfo return window data in a slightly easier-to-use format

Having the window pointer available as a WinRT IInspectable should make it a bit easier to use in conjunction with WRL-based weak references.
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@8494
    31
/* Windows includes */
dludwig@8494
    32
#include <agile.h>
dludwig@8494
    33
using namespace Windows::UI::Core;
dludwig@8494
    34
dludwig@8494
    35
dludwig@8494
    36
/* SDL includes */
dludwig@8329
    37
extern "C" {
dludwig@8327
    38
#include "SDL_video.h"
dludwig@8327
    39
#include "SDL_mouse.h"
dludwig@8327
    40
#include "../SDL_sysvideo.h"
dludwig@8327
    41
#include "../SDL_pixels_c.h"
dludwig@8327
    42
#include "../../events/SDL_events_c.h"
dludwig@8400
    43
#include "../../render/SDL_sysrender.h"
dludwig@8411
    44
#include "SDL_syswm.h"
dludwig@8329
    45
}
dludwig@8327
    46
dludwig@8522
    47
#include "../../core/winrt/SDL_winrtapp_direct3d.h"
dludwig@8522
    48
#include "../../core/winrt/SDL_winrtapp_xaml.h"
dludwig@8512
    49
#include "SDL_winrtvideo_cpp.h"
dludwig@8327
    50
#include "SDL_winrtevents_c.h"
dludwig@8516
    51
#include "SDL_winrtmouse_c.h"
dludwig@8505
    52
#include "SDL_main.h"
dludwig@8505
    53
#include "SDL_system.h"
dludwig@8327
    54
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@8494
    62
dludwig@8333
    63
/* Window functions */
dludwig@8522
    64
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@8333
    65
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    66
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    67
dludwig@8494
    68
dludwig@8515
    69
/* SDL-internal globals: */
dludwig@8497
    70
SDL_Window * WINRT_GlobalSDLWindow = NULL;
dludwig@8498
    71
SDL_VideoDevice * WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
    72
dludwig@8498
    73
dludwig@8327
    74
/* WinRT driver bootstrap functions */
dludwig@8327
    75
dludwig@8327
    76
static int
dludwig@8327
    77
WINRT_Available(void)
dludwig@8327
    78
{
dludwig@8328
    79
    return (1);
dludwig@8327
    80
}
dludwig@8327
    81
dludwig@8327
    82
static void
dludwig@8327
    83
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
    84
{
dludwig@8498
    85
    if (device == WINRT_GlobalSDLVideoDevice) {
dludwig@8498
    86
        WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
    87
    }
dludwig@8327
    88
    SDL_free(device);
dludwig@8327
    89
}
dludwig@8327
    90
dludwig@8327
    91
static SDL_VideoDevice *
dludwig@8327
    92
WINRT_CreateDevice(int devindex)
dludwig@8327
    93
{
dludwig@8327
    94
    SDL_VideoDevice *device;
dludwig@8327
    95
dludwig@8327
    96
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
    97
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
    98
    if (!device) {
dludwig@8327
    99
        SDL_OutOfMemory();
dludwig@8327
   100
        if (device) {
dludwig@8327
   101
            SDL_free(device);
dludwig@8327
   102
        }
dludwig@8327
   103
        return (0);
dludwig@8327
   104
    }
dludwig@8327
   105
dludwig@8327
   106
    /* Set the function pointers */
dludwig@8327
   107
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   108
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
   109
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   110
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   111
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   112
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8411
   113
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8433
   114
    device->free = WINRT_DeleteDevice;
dludwig@8501
   115
    WINRT_GlobalSDLVideoDevice = device;
dludwig@8327
   116
dludwig@8327
   117
    return device;
dludwig@8327
   118
}
dludwig@8327
   119
dludwig@8494
   120
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
   121
VideoBootStrap WINRT_bootstrap = {
dludwig@8500
   122
    WINRTVID_DRIVER_NAME, "SDL WinRT video driver",
dludwig@8327
   123
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   124
};
dludwig@8327
   125
dludwig@8327
   126
int
dludwig@8327
   127
WINRT_VideoInit(_THIS)
dludwig@8327
   128
{
dludwig@8374
   129
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   130
        return -1;
dludwig@8374
   131
    }
dludwig@8374
   132
    WINRT_InitMouse(_this);
dludwig@8515
   133
    WINRT_InitTouch(_this);
dludwig@8522
   134
dludwig@8374
   135
    return 0;
dludwig@8374
   136
}
dludwig@8374
   137
dludwig@8522
   138
SDL_DisplayMode
dludwig@8522
   139
WINRT_CalcDisplayModeUsingNativeWindow()
dludwig@8522
   140
{
dludwig@8522
   141
    using namespace Windows::Graphics::Display;
dludwig@8522
   142
dludwig@8522
   143
    // Create an empty, zeroed-out display mode:
dludwig@8522
   144
    SDL_DisplayMode mode;
dludwig@8522
   145
    SDL_zero(mode);
dludwig@8522
   146
dludwig@8522
   147
    // Go no further if a native window cannot be accessed.  This can happen,
dludwig@8522
   148
    // for example, if this function is called from certain threads, such as
dludwig@8522
   149
    // the SDL/XAML thread.
dludwig@8522
   150
    if (!CoreWindow::GetForCurrentThread()) {
dludwig@8522
   151
        return mode;
dludwig@8522
   152
    }
dludwig@8522
   153
dludwig@8522
   154
    // Fill in most fields:
dludwig@8522
   155
    mode.format = SDL_PIXELFORMAT_RGB888;
dludwig@8522
   156
    mode.refresh_rate = 0;  // TODO, WinRT: see if refresh rate data is available, or relevant (for WinRT apps)
dludwig@8522
   157
    mode.driverdata = (void *) DisplayProperties::CurrentOrientation;
dludwig@8522
   158
dludwig@8522
   159
    // Calculate the display size given the window size, taking into account
dludwig@8522
   160
    // the current display's DPI:
dludwig@8522
   161
    const float currentDPI = Windows::Graphics::Display::DisplayProperties::LogicalDpi; 
dludwig@8522
   162
    const float dipsPerInch = 96.0f;
dludwig@8522
   163
    mode.w = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Width * currentDPI) / dipsPerInch);
dludwig@8522
   164
    mode.h = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Height * currentDPI) / dipsPerInch);
dludwig@8522
   165
dludwig@8522
   166
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8522
   167
    // On Windows Phone, the native window's size is always in portrait,
dludwig@8522
   168
    // regardless of the device's orientation.  This is in contrast to
dludwig@8522
   169
    // Windows 8/RT, which will resize the native window as the device's
dludwig@8522
   170
    // orientation changes.  In order to compensate for this behavior,
dludwig@8522
   171
    // on Windows Phone, the mode's width and height will be swapped when
dludwig@8522
   172
    // the device is in a landscape (non-portrait) mode.
dludwig@8522
   173
    switch (DisplayProperties::CurrentOrientation) {
dludwig@8522
   174
        case DisplayOrientations::Landscape:
dludwig@8522
   175
        case DisplayOrientations::LandscapeFlipped:
dludwig@8522
   176
        {
dludwig@8522
   177
            const int tmp = mode.h;
dludwig@8522
   178
            mode.h = mode.w;
dludwig@8522
   179
            mode.w = tmp;
dludwig@8522
   180
            break;
dludwig@8522
   181
        }
dludwig@8522
   182
dludwig@8522
   183
        default:
dludwig@8522
   184
            break;
dludwig@8522
   185
    }
dludwig@8522
   186
dludwig@8522
   187
    // Attach the mode to te
dludwig@8522
   188
#endif
dludwig@8522
   189
dludwig@8522
   190
    return mode;
dludwig@8522
   191
}
dludwig@8503
   192
dludwig@8505
   193
int
dludwig@8374
   194
WINRT_InitModes(_THIS)
dludwig@8374
   195
{
dludwig@8505
   196
    // Retrieve the display mode:
dludwig@8522
   197
    SDL_DisplayMode mode = WINRT_CalcDisplayModeUsingNativeWindow();
dludwig@8522
   198
    if (mode.w == 0 || mode.h == 0) {
dludwig@8522
   199
        return SDL_SetError("Unable to calculate the WinRT window/display's size");
dludwig@8522
   200
    }
dludwig@8522
   201
dludwig@8522
   202
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8522
   203
        return -1;
dludwig@8374
   204
    }
dludwig@8374
   205
dludwig@8374
   206
    SDL_AddDisplayMode(&_this->displays[0], &mode);
dludwig@8327
   207
    return 0;
dludwig@8327
   208
}
dludwig@8327
   209
dludwig@8327
   210
static int
dludwig@8327
   211
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   212
{
dludwig@8327
   213
    return 0;
dludwig@8327
   214
}
dludwig@8327
   215
dludwig@8327
   216
void
dludwig@8327
   217
WINRT_VideoQuit(_THIS)
dludwig@8327
   218
{
dludwig@8374
   219
    WINRT_QuitMouse(_this);
dludwig@8327
   220
}
dludwig@8327
   221
dludwig@8522
   222
int
dludwig@8522
   223
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8522
   224
{
dludwig@8522
   225
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8522
   226
    // support is added.
dludwig@8522
   227
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8522
   228
        SDL_SetError("WinRT only supports one window");
dludwig@8522
   229
        return -1;
dludwig@8522
   230
    }
dludwig@8522
   231
dludwig@8522
   232
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8522
   233
    if (!data) {
dludwig@8522
   234
        SDL_OutOfMemory();
dludwig@8522
   235
        return -1;
dludwig@8522
   236
    }
dludwig@8522
   237
    window->driverdata = data;
dludwig@8522
   238
    data->sdlWindow = window;
dludwig@8522
   239
dludwig@8522
   240
    /* To note, when XAML support is enabled, access to the CoreWindow will not
dludwig@8522
   241
       be possible, at least not via the SDL/XAML thread.  Attempts to access it
dludwig@8522
   242
       from there will throw exceptions.  As such, the SDL_WindowData's
dludwig@8522
   243
       'coreWindow' field will only be set (to a non-null value) if XAML isn't
dludwig@8522
   244
       enabled.
dludwig@8522
   245
    */
dludwig@8522
   246
    if (!WINRT_XAMLWasEnabled) {
dludwig@8522
   247
        data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@8522
   248
    }
dludwig@8522
   249
dludwig@8522
   250
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8522
   251
       and is considered fullscreen, shown, and the like.
dludwig@8522
   252
    */
dludwig@8522
   253
    window->x = 0;
dludwig@8522
   254
    window->y = 0;
dludwig@8522
   255
    window->flags =
dludwig@8522
   256
        SDL_WINDOW_FULLSCREEN |
dludwig@8522
   257
        SDL_WINDOW_SHOWN |
dludwig@8522
   258
        SDL_WINDOW_BORDERLESS |
dludwig@8522
   259
        SDL_WINDOW_MAXIMIZED |
dludwig@8522
   260
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8522
   261
dludwig@8522
   262
    /* WinRT does not, as of this writing, appear to support app-adjustable
dludwig@8522
   263
       window sizes.  Set the window size to whatever the native WinRT
dludwig@8522
   264
       CoreWindow is set at.
dludwig@8522
   265
dludwig@8522
   266
       TODO, WinRT: if and when non-fullscreen XAML control support is added to SDL, consider making those resizable via SDL_Window's interfaces.
dludwig@8522
   267
    */
dludwig@8522
   268
    window->w = _this->displays[0].current_mode.w;
dludwig@8522
   269
    window->h = _this->displays[0].current_mode.h;
dludwig@8522
   270
 
dludwig@8522
   271
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8522
   272
       behalf of SDL:
dludwig@8522
   273
    */
dludwig@8522
   274
    WINRT_GlobalSDLWindow = window;
dludwig@8522
   275
dludwig@8522
   276
    /* All done! */
dludwig@8522
   277
    return 0;
dludwig@8522
   278
}
dludwig@8522
   279
dludwig@8333
   280
void
dludwig@8333
   281
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   282
{
dludwig@8411
   283
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   284
dludwig@8497
   285
    if (WINRT_GlobalSDLWindow == window) {
dludwig@8497
   286
        WINRT_GlobalSDLWindow = NULL;
dludwig@8424
   287
    }
dludwig@8424
   288
dludwig@8411
   289
    if (data) {
dludwig@8411
   290
        // Delete the internal window data:
dludwig@8411
   291
        delete data;
dludwig@8411
   292
        data = NULL;
dludwig@8411
   293
    }
dludwig@8333
   294
}
dludwig@8333
   295
dludwig@8522
   296
SDL_bool
dludwig@8522
   297
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8522
   298
{
dludwig@8522
   299
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8522
   300
dludwig@8522
   301
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8522
   302
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8527
   303
        info->info.winrt.window = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@8522
   304
        return SDL_TRUE;
dludwig@8522
   305
    } else {
dludwig@8522
   306
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8522
   307
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8522
   308
        return SDL_FALSE;
dludwig@8522
   309
    }
dludwig@8522
   310
    return SDL_FALSE;
dludwig@8411
   311
}
dludwig@8333
   312
dludwig@8327
   313
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   314
dludwig@8327
   315
/* vi: set ts=4 sw=4 expandtab: */