src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 27 Aug 2013 21:21:09 -0400
changeset 8505 a76a3842c9eb
parent 8503 90b33b6c6ab9
child 8510 b828c4cc5cd8
permissions -rw-r--r--
WinRT: experimental and preliminary support for XAML-based overlays on Windows 8/RT

The XAML support here is still rudimentary. Bugs do exist. You've been warned.

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