src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Wed, 28 Aug 2013 16:51:07 -0400
changeset 8512 5b345a756965
parent 8510 b828c4cc5cd8
child 8515 bc6cf9201dab
permissions -rw-r--r--
WinRT: corrected SDL_MOUSE* coordinates in non-Portrait modes

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