src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Mon, 02 Sep 2013 15:29:46 -0400
changeset 8516 f3e0e381bdcd
parent 8515 bc6cf9201dab
child 8522 3ae9dc9c439f
permissions -rw-r--r--
WinRT: renamed a mouse-related header file for naming-consistency's sake
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@8515
    48
#include "../../core/winrt/SDL_winrtxaml_cpp.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@8333
    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@8374
   134
dludwig@8374
   135
    return 0;
dludwig@8374
   136
}
dludwig@8374
   137
dludwig@8503
   138
SDL_DisplayMode
dludwig@8503
   139
WINRT_CalcDisplayModeUsingNativeWindow()
dludwig@8503
   140
{
dludwig@8510
   141
    using namespace Windows::Graphics::Display;
dludwig@8510
   142
dludwig@8503
   143
    // Create an empty, zeroed-out display mode:
dludwig@8503
   144
    SDL_DisplayMode mode;
dludwig@8503
   145
    SDL_zero(mode);
dludwig@8503
   146
dludwig@8505
   147
    // Go no further if a native window cannot be accessed.  This can happen,
dludwig@8505
   148
    // for example, if this function is called from certain threads, such as
dludwig@8505
   149
    // the SDL/XAML thread.
dludwig@8505
   150
    if (!CoreWindow::GetForCurrentThread()) {
dludwig@8505
   151
        return mode;
dludwig@8505
   152
    }
dludwig@8505
   153
dludwig@8503
   154
    // Fill in most fields:
dludwig@8503
   155
    mode.format = SDL_PIXELFORMAT_RGB888;
dludwig@8503
   156
    mode.refresh_rate = 0;  // TODO, WinRT: see if refresh rate data is available, or relevant (for WinRT apps)
dludwig@8510
   157
    mode.driverdata = (void *) DisplayProperties::CurrentOrientation;
dludwig@8503
   158
dludwig@8503
   159
    // Calculate the display size given the window size, taking into account
dludwig@8503
   160
    // the current display's DPI:
dludwig@8503
   161
    const float currentDPI = Windows::Graphics::Display::DisplayProperties::LogicalDpi; 
dludwig@8503
   162
    const float dipsPerInch = 96.0f;
dludwig@8503
   163
    mode.w = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Width * currentDPI) / dipsPerInch);
dludwig@8503
   164
    mode.h = (int) ((CoreWindow::GetForCurrentThread()->Bounds.Height * currentDPI) / dipsPerInch);
dludwig@8503
   165
dludwig@8510
   166
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8510
   167
    // On Windows Phone, the native window's size is always in portrait,
dludwig@8510
   168
    // regardless of the device's orientation.  This is in contrast to
dludwig@8510
   169
    // Windows 8/RT, which will resize the native window as the device's
dludwig@8510
   170
    // orientation changes.  In order to compensate for this behavior,
dludwig@8510
   171
    // on Windows Phone, the mode's width and height will be swapped when
dludwig@8510
   172
    // the device is in a landscape (non-portrait) mode.
dludwig@8510
   173
    switch (DisplayProperties::CurrentOrientation) {
dludwig@8510
   174
        case DisplayOrientations::Landscape:
dludwig@8510
   175
        case DisplayOrientations::LandscapeFlipped:
dludwig@8510
   176
        {
dludwig@8510
   177
            const int tmp = mode.h;
dludwig@8510
   178
            mode.h = mode.w;
dludwig@8510
   179
            mode.w = tmp;
dludwig@8510
   180
            break;
dludwig@8510
   181
        }
dludwig@8510
   182
dludwig@8510
   183
        default:
dludwig@8510
   184
            break;
dludwig@8510
   185
    }
dludwig@8510
   186
dludwig@8510
   187
    // Attach the mode to te
dludwig@8510
   188
#endif
dludwig@8510
   189
dludwig@8503
   190
    return mode;
dludwig@8503
   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@8503
   197
    SDL_DisplayMode mode = WINRT_CalcDisplayModeUsingNativeWindow();
dludwig@8505
   198
    if (mode.w == 0 || mode.h == 0) {
dludwig@8505
   199
        return SDL_SetError("Unable to calculate the WinRT window/display's size");
dludwig@8505
   200
    }
dludwig@8505
   201
dludwig@8329
   202
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8329
   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@8333
   222
int
dludwig@8333
   223
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8333
   224
{
dludwig@8334
   225
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8334
   226
    // support is added.
dludwig@8497
   227
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8334
   228
        SDL_SetError("WinRT only supports one window");
dludwig@8334
   229
        return -1;
dludwig@8334
   230
    }
dludwig@8333
   231
dludwig@8411
   232
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8333
   233
    if (!data) {
dludwig@8333
   234
        SDL_OutOfMemory();
dludwig@8333
   235
        return -1;
dludwig@8333
   236
    }
dludwig@8417
   237
    window->driverdata = data;
dludwig@8333
   238
    data->sdlWindow = window;
dludwig@8505
   239
dludwig@8505
   240
    /* To note, when XAML support is enabled, access to the CoreWindow will not
dludwig@8505
   241
       be possible, at least not via the SDL/XAML thread.  Attempts to access it
dludwig@8505
   242
       from there will throw exceptions.  As such, the SDL_WindowData's
dludwig@8505
   243
       'coreWindow' field will only be set (to a non-null value) if XAML isn't
dludwig@8505
   244
       enabled.
dludwig@8505
   245
    */
dludwig@8505
   246
    if (!WINRT_XAMLWasEnabled) {
dludwig@8505
   247
        data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@8505
   248
    }
dludwig@8333
   249
dludwig@8367
   250
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8367
   251
       and is considered fullscreen, shown, and the like.
dludwig@8367
   252
    */
dludwig@8333
   253
    window->x = 0;
dludwig@8333
   254
    window->y = 0;
dludwig@8367
   255
    window->flags =
dludwig@8367
   256
        SDL_WINDOW_FULLSCREEN |
dludwig@8367
   257
        SDL_WINDOW_SHOWN |
dludwig@8367
   258
        SDL_WINDOW_BORDERLESS |
dludwig@8367
   259
        SDL_WINDOW_MAXIMIZED |
dludwig@8367
   260
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8333
   261
dludwig@8491
   262
    /* WinRT does not, as of this writing, appear to support app-adjustable
dludwig@8491
   263
       window sizes.  Set the window size to whatever the native WinRT
dludwig@8491
   264
       CoreWindow is set at.
dludwig@8491
   265
dludwig@8491
   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@8367
   267
    */
dludwig@8491
   268
    window->w = _this->displays[0].current_mode.w;
dludwig@8491
   269
    window->h = _this->displays[0].current_mode.h;
dludwig@8367
   270
 
dludwig@8367
   271
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8367
   272
       behalf of SDL:
dludwig@8367
   273
    */
dludwig@8497
   274
    WINRT_GlobalSDLWindow = window;
dludwig@8333
   275
dludwig@8367
   276
    /* All done! */
dludwig@8333
   277
    return 0;
dludwig@8333
   278
}
dludwig@8333
   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@8411
   296
SDL_bool
dludwig@8411
   297
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8411
   298
{
dludwig@8411
   299
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   300
dludwig@8411
   301
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8496
   302
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8463
   303
        info->info.winrt.window = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
dludwig@8411
   304
        return SDL_TRUE;
dludwig@8411
   305
    } else {
dludwig@8411
   306
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8411
   307
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8411
   308
        return SDL_FALSE;
dludwig@8411
   309
    }
dludwig@8411
   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: */