src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig <dludwig@pobox.com>
Tue, 27 Aug 2013 10:56:10 -0400
changeset 8491 50b0bdce11cd
parent 8483 d403ded25942
child 8492 1bcacb98366e
permissions -rw-r--r--
WinRT: made all SDL_Windows get sized to the WinRT-defined window size
This change removes some code that attempted to allow non-standard window sizes, the idea of which was to do display scaling, and a hackish one at that. If display scaling is needed, use SDL_Renderer as appropriate.
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@8329
    31
extern "C" {
dludwig@8327
    32
#include "SDL_video.h"
dludwig@8327
    33
#include "SDL_mouse.h"
dludwig@8327
    34
#include "../SDL_sysvideo.h"
dludwig@8327
    35
#include "../SDL_pixels_c.h"
dludwig@8327
    36
#include "../../events/SDL_events_c.h"
dludwig@8400
    37
#include "../../render/SDL_sysrender.h"
dludwig@8411
    38
#include "SDL_syswm.h"
dludwig@8329
    39
}
dludwig@8327
    40
dludwig@8483
    41
#include "../../core/winrt/SDL_winrtapp.h"
dludwig@8327
    42
#include "SDL_winrtvideo.h"
dludwig@8327
    43
#include "SDL_winrtevents_c.h"
dludwig@8374
    44
#include "SDL_winrtmouse.h"
dludwig@8327
    45
dludwig@8437
    46
using namespace Windows::UI::Core;
dludwig@8437
    47
dludwig@8333
    48
/* On Windows, windows.h defines CreateWindow */
dludwig@8333
    49
#ifdef CreateWindow
dludwig@8333
    50
#undef CreateWindow
dludwig@8333
    51
#endif
dludwig@8333
    52
dludwig@8332
    53
extern SDL_WinRTApp ^ SDL_WinRTGlobalApp;
dludwig@8332
    54
dludwig@8368
    55
#define WINRTVID_DRIVER_NAME "winrt"
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@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@8327
    68
/* WinRT driver bootstrap functions */
dludwig@8327
    69
dludwig@8327
    70
static int
dludwig@8327
    71
WINRT_Available(void)
dludwig@8327
    72
{
dludwig@8328
    73
    return (1);
dludwig@8327
    74
}
dludwig@8327
    75
dludwig@8327
    76
static void
dludwig@8327
    77
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
    78
{
dludwig@8433
    79
    SDL_WinRTGlobalApp->SetSDLVideoDevice(NULL);
dludwig@8327
    80
    SDL_free(device);
dludwig@8327
    81
}
dludwig@8327
    82
dludwig@8327
    83
static SDL_VideoDevice *
dludwig@8327
    84
WINRT_CreateDevice(int devindex)
dludwig@8327
    85
{
dludwig@8327
    86
    SDL_VideoDevice *device;
dludwig@8327
    87
dludwig@8327
    88
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
    89
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
    90
    if (!device) {
dludwig@8327
    91
        SDL_OutOfMemory();
dludwig@8327
    92
        if (device) {
dludwig@8327
    93
            SDL_free(device);
dludwig@8327
    94
        }
dludwig@8327
    95
        return (0);
dludwig@8327
    96
    }
dludwig@8327
    97
dludwig@8327
    98
    /* Set the function pointers */
dludwig@8327
    99
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   100
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
   101
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   102
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   103
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   104
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8417
   105
    //device->CreateWindowFramebuffer = SDL_WINRT_CreateWindowFramebuffer;
dludwig@8417
   106
    //device->UpdateWindowFramebuffer = SDL_WINRT_UpdateWindowFramebuffer;
dludwig@8417
   107
    //device->DestroyWindowFramebuffer = SDL_WINRT_DestroyWindowFramebuffer;
dludwig@8411
   108
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8433
   109
    device->free = WINRT_DeleteDevice;
dludwig@8327
   110
dludwig@8433
   111
    SDL_WinRTGlobalApp->SetSDLVideoDevice(device);
dludwig@8327
   112
dludwig@8327
   113
    return device;
dludwig@8327
   114
}
dludwig@8327
   115
dludwig@8327
   116
VideoBootStrap WINRT_bootstrap = {
dludwig@8327
   117
    WINRTVID_DRIVER_NAME, "SDL Windows RT video driver",
dludwig@8327
   118
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   119
};
dludwig@8327
   120
dludwig@8327
   121
int
dludwig@8327
   122
WINRT_VideoInit(_THIS)
dludwig@8327
   123
{
dludwig@8476
   124
    // 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
   125
dludwig@8374
   126
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   127
        return -1;
dludwig@8374
   128
    }
dludwig@8374
   129
    WINRT_InitMouse(_this);
dludwig@8374
   130
dludwig@8374
   131
    return 0;
dludwig@8374
   132
}
dludwig@8374
   133
dludwig@8374
   134
static int
dludwig@8374
   135
WINRT_InitModes(_THIS)
dludwig@8374
   136
{
dludwig@8329
   137
    SDL_DisplayMode mode = SDL_WinRTGlobalApp->GetMainDisplayMode();
dludwig@8329
   138
    if (SDL_AddBasicVideoDisplay(&mode) < 0) {
dludwig@8329
   139
        return -1;
dludwig@8374
   140
    }
dludwig@8374
   141
dludwig@8374
   142
    SDL_AddDisplayMode(&_this->displays[0], &mode);
dludwig@8327
   143
    return 0;
dludwig@8327
   144
}
dludwig@8327
   145
dludwig@8327
   146
static int
dludwig@8327
   147
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   148
{
dludwig@8327
   149
    return 0;
dludwig@8327
   150
}
dludwig@8327
   151
dludwig@8327
   152
void
dludwig@8327
   153
WINRT_VideoQuit(_THIS)
dludwig@8327
   154
{
dludwig@8374
   155
    WINRT_QuitMouse(_this);
dludwig@8327
   156
}
dludwig@8327
   157
dludwig@8333
   158
int
dludwig@8333
   159
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8333
   160
{
dludwig@8334
   161
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8334
   162
    // support is added.
dludwig@8334
   163
    if (SDL_WinRTGlobalApp->HasSDLWindowData())
dludwig@8334
   164
    {
dludwig@8334
   165
        SDL_SetError("WinRT only supports one window");
dludwig@8334
   166
        return -1;
dludwig@8334
   167
    }
dludwig@8333
   168
dludwig@8411
   169
    SDL_WindowData *data = new SDL_WindowData;
dludwig@8333
   170
    if (!data) {
dludwig@8333
   171
        SDL_OutOfMemory();
dludwig@8333
   172
        return -1;
dludwig@8333
   173
    }
dludwig@8417
   174
    window->driverdata = data;
dludwig@8333
   175
    data->sdlWindow = window;
dludwig@8463
   176
    data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@8333
   177
dludwig@8367
   178
    /* Make sure the window is considered to be positioned at {0,0},
dludwig@8367
   179
       and is considered fullscreen, shown, and the like.
dludwig@8367
   180
    */
dludwig@8333
   181
    window->x = 0;
dludwig@8333
   182
    window->y = 0;
dludwig@8367
   183
    window->flags =
dludwig@8367
   184
        SDL_WINDOW_FULLSCREEN |
dludwig@8367
   185
        SDL_WINDOW_SHOWN |
dludwig@8367
   186
        SDL_WINDOW_BORDERLESS |
dludwig@8367
   187
        SDL_WINDOW_MAXIMIZED |
dludwig@8367
   188
        SDL_WINDOW_INPUT_GRABBED;
dludwig@8333
   189
dludwig@8491
   190
    /* WinRT does not, as of this writing, appear to support app-adjustable
dludwig@8491
   191
       window sizes.  Set the window size to whatever the native WinRT
dludwig@8491
   192
       CoreWindow is set at.
dludwig@8491
   193
dludwig@8491
   194
       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
   195
    */
dludwig@8491
   196
    window->w = _this->displays[0].current_mode.w;
dludwig@8491
   197
    window->h = _this->displays[0].current_mode.h;
dludwig@8367
   198
 
dludwig@8367
   199
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8367
   200
       behalf of SDL:
dludwig@8367
   201
    */
dludwig@8333
   202
    SDL_WinRTGlobalApp->SetSDLWindowData(data);
dludwig@8333
   203
dludwig@8367
   204
    /* All done! */
dludwig@8333
   205
    return 0;
dludwig@8333
   206
}
dludwig@8333
   207
dludwig@8333
   208
void
dludwig@8333
   209
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   210
{
dludwig@8411
   211
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   212
dludwig@8424
   213
    if (SDL_WinRTGlobalApp->HasSDLWindowData() &&
dludwig@8424
   214
        SDL_WinRTGlobalApp->GetSDLWindowData()->sdlWindow == window)
dludwig@8424
   215
    {
dludwig@8424
   216
        SDL_WinRTGlobalApp->SetSDLWindowData(NULL);
dludwig@8424
   217
    }
dludwig@8424
   218
dludwig@8411
   219
    if (data) {
dludwig@8411
   220
        // Delete the internal window data:
dludwig@8411
   221
        delete data;
dludwig@8411
   222
        data = NULL;
dludwig@8411
   223
    }
dludwig@8333
   224
}
dludwig@8333
   225
dludwig@8411
   226
SDL_bool
dludwig@8411
   227
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8411
   228
{
dludwig@8411
   229
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   230
dludwig@8411
   231
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8411
   232
        info->subsystem = SDL_SYSWM_WINDOWSRT;
dludwig@8463
   233
        info->info.winrt.window = reinterpret_cast<IUnknown *>(data->coreWindow.Get());
dludwig@8411
   234
        return SDL_TRUE;
dludwig@8411
   235
    } else {
dludwig@8411
   236
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8411
   237
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8411
   238
        return SDL_FALSE;
dludwig@8411
   239
    }
dludwig@8411
   240
    return SDL_FALSE;
dludwig@8411
   241
}
dludwig@8333
   242
dludwig@8327
   243
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   244
dludwig@8327
   245
/* vi: set ts=4 sw=4 expandtab: */