src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 27 Aug 2013 12:13:45 -0400
changeset 8498 333ae9a0f411
parent 8497 103bb054aa7d
child 8499 00e397f1b7a8
permissions -rw-r--r--
WinRT: moved the WinRT SDL_VideoDevice out of SDL_WinRTApp
This was done to help pave the way for XAML support.
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@8327
    50
dludwig@8332
    51
extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
dludwig@8332
    52
dludwig@8327
    53
dludwig@8327
    54
/* Initialization/Query functions */
dludwig@8327
    55
static int WINRT_VideoInit(_THIS);
dludwig@8374
    56
static int WINRT_InitModes(_THIS);
dludwig@8327
    57
static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
dludwig@8327
    58
static void WINRT_VideoQuit(_THIS);
dludwig@8327
    59
dludwig@8494
    60
dludwig@8333
    61
/* Window functions */
dludwig@8333
    62
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@8333
    63
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    64
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    65
dludwig@8494
    66
dludwig@8494
    67
/* Internal window data */
dludwig@8494
    68
struct SDL_WindowData
dludwig@8494
    69
{
dludwig@8494
    70
    SDL_Window *sdlWindow;
dludwig@8494
    71
    Platform::Agile<Windows::UI::Core::CoreWindow> coreWindow;
dludwig@8494
    72
};
dludwig@8494
    73
dludwig@8494
    74
dludwig@8497
    75
/* The global, WinRT, SDL Window.
dludwig@8497
    76
   For now, SDL/WinRT only supports one window (due to platform limitations of
dludwig@8497
    77
   WinRT.
dludwig@8497
    78
*/
dludwig@8497
    79
SDL_Window * WINRT_GlobalSDLWindow = NULL;
dludwig@8497
    80
dludwig@8497
    81
dludwig@8498
    82
/* The global, WinRT, video device.
dludwig@8498
    83
*/
dludwig@8498
    84
SDL_VideoDevice * WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
    85
dludwig@8498
    86
dludwig@8327
    87
/* WinRT driver bootstrap functions */
dludwig@8327
    88
dludwig@8327
    89
static int
dludwig@8327
    90
WINRT_Available(void)
dludwig@8327
    91
{
dludwig@8328
    92
    return (1);
dludwig@8327
    93
}
dludwig@8327
    94
dludwig@8327
    95
static void
dludwig@8327
    96
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
    97
{
dludwig@8498
    98
    if (device == WINRT_GlobalSDLVideoDevice) {
dludwig@8498
    99
        WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8498
   100
    }
dludwig@8327
   101
    SDL_free(device);
dludwig@8327
   102
}
dludwig@8327
   103
dludwig@8327
   104
static SDL_VideoDevice *
dludwig@8327
   105
WINRT_CreateDevice(int devindex)
dludwig@8327
   106
{
dludwig@8327
   107
    SDL_VideoDevice *device;
dludwig@8327
   108
dludwig@8327
   109
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
   110
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
   111
    if (!device) {
dludwig@8327
   112
        SDL_OutOfMemory();
dludwig@8327
   113
        if (device) {
dludwig@8327
   114
            SDL_free(device);
dludwig@8327
   115
        }
dludwig@8327
   116
        return (0);
dludwig@8327
   117
    }
dludwig@8327
   118
dludwig@8327
   119
    /* Set the function pointers */
dludwig@8327
   120
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   121
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
   122
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   123
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   124
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   125
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8411
   126
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8433
   127
    device->free = WINRT_DeleteDevice;
dludwig@8498
   128
    WINRT_GlobalSDLVideoDevice = NULL;
dludwig@8327
   129
dludwig@8327
   130
    return device;
dludwig@8327
   131
}
dludwig@8327
   132
dludwig@8494
   133
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
   134
VideoBootStrap WINRT_bootstrap = {
dludwig@8327
   135
    WINRTVID_DRIVER_NAME, "SDL Windows RT video driver",
dludwig@8327
   136
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   137
};
dludwig@8327
   138
dludwig@8327
   139
int
dludwig@8327
   140
WINRT_VideoInit(_THIS)
dludwig@8327
   141
{
dludwig@8476
   142
    // TODO, WinRT: consider adding a hack to wait (here) for the app's orientation to finish getting set (before the initial display mode is set up)
dludwig@8475
   143
dludwig@8374
   144
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   145
        return -1;
dludwig@8374
   146
    }
dludwig@8374
   147
    WINRT_InitMouse(_this);
dludwig@8374
   148
dludwig@8374
   149
    return 0;
dludwig@8374
   150
}
dludwig@8374
   151
dludwig@8374
   152
static int
dludwig@8374
   153
WINRT_InitModes(_THIS)
dludwig@8374
   154
{
dludwig@8492
   155
    SDL_DisplayMode mode = SDL_WinRTGlobalApp->CalcCurrentDisplayMode();
dludwig@8329
   156
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8329
   157
        return -1;
dludwig@8374
   158
    }
dludwig@8374
   159
dludwig@8374
   160
    SDL_AddDisplayMode(&_this->displays[0], &mode);
dludwig@8327
   161
    return 0;
dludwig@8327
   162
}
dludwig@8327
   163
dludwig@8327
   164
static int
dludwig@8327
   165
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   166
{
dludwig@8327
   167
    return 0;
dludwig@8327
   168
}
dludwig@8327
   169
dludwig@8327
   170
void
dludwig@8327
   171
WINRT_VideoQuit(_THIS)
dludwig@8327
   172
{
dludwig@8374
   173
    WINRT_QuitMouse(_this);
dludwig@8327
   174
}
dludwig@8327
   175
dludwig@8333
   176
int
dludwig@8333
   177
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8333
   178
{
dludwig@8334
   179
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8334
   180
    // support is added.
dludwig@8497
   181
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8334
   182
        SDL_SetError("WinRT only supports one window");
dludwig@8334
   183
        return -1;
dludwig@8334
   184
    }
dludwig@8333
   185
dludwig@8411
   186
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8333
   187
    if (!data) {
dludwig@8333
   188
        SDL_OutOfMemory();
dludwig@8333
   189
        return -1;
dludwig@8333
   190
    }
dludwig@8417
   191
    window->driverdata = data;
dludwig@8333
   192
    data->sdlWindow = window;
dludwig@8463
   193
    data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@8333
   194
dludwig@8367
   195
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8367
   196
       and is considered fullscreen, shown, and the like.
dludwig@8367
   197
    */
dludwig@8333
   198
    window->x = 0;
dludwig@8333
   199
    window->y = 0;
dludwig@8367
   200
    window->flags =
dludwig@8367
   201
        SDL_WINDOW_FULLSCREEN |
dludwig@8367
   202
        SDL_WINDOW_SHOWN |
dludwig@8367
   203
        SDL_WINDOW_BORDERLESS |
dludwig@8367
   204
        SDL_WINDOW_MAXIMIZED |
dludwig@8367
   205
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8333
   206
dludwig@8491
   207
    /* WinRT does not, as of this writing, appear to support app-adjustable
dludwig@8491
   208
       window sizes.  Set the window size to whatever the native WinRT
dludwig@8491
   209
       CoreWindow is set at.
dludwig@8491
   210
dludwig@8491
   211
       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
   212
    */
dludwig@8491
   213
    window->w = _this->displays[0].current_mode.w;
dludwig@8491
   214
    window->h = _this->displays[0].current_mode.h;
dludwig@8367
   215
 
dludwig@8367
   216
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8367
   217
       behalf of SDL:
dludwig@8367
   218
    */
dludwig@8497
   219
    WINRT_GlobalSDLWindow = window;
dludwig@8333
   220
dludwig@8367
   221
    /* All done! */
dludwig@8333
   222
    return 0;
dludwig@8333
   223
}
dludwig@8333
   224
dludwig@8333
   225
void
dludwig@8333
   226
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   227
{
dludwig@8411
   228
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   229
dludwig@8497
   230
    if (WINRT_GlobalSDLWindow == window) {
dludwig@8497
   231
        WINRT_GlobalSDLWindow = NULL;
dludwig@8424
   232
    }
dludwig@8424
   233
dludwig@8411
   234
    if (data) {
dludwig@8411
   235
        // Delete the internal window data:
dludwig@8411
   236
        delete data;
dludwig@8411
   237
        data = NULL;
dludwig@8411
   238
    }
dludwig@8333
   239
}
dludwig@8333
   240
dludwig@8411
   241
SDL_bool
dludwig@8411
   242
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8411
   243
{
dludwig@8411
   244
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   245
dludwig@8411
   246
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8496
   247
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8463
   248
        info->info.winrt.window = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
dludwig@8411
   249
        return SDL_TRUE;
dludwig@8411
   250
    } else {
dludwig@8411
   251
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8411
   252
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8411
   253
        return SDL_FALSE;
dludwig@8411
   254
    }
dludwig@8411
   255
    return SDL_FALSE;
dludwig@8411
   256
}
dludwig@8333
   257
dludwig@8327
   258
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   259
dludwig@8327
   260
/* vi: set ts=4 sw=4 expandtab: */