src/video/winrt/SDL_winrtvideo.cpp
author David Ludwig
Thu, 26 Nov 2015 01:36:36 -0500
changeset 9925 2be74843315c
parent 9924 355cccccc7dc
child 9926 277db103ed10
permissions -rw-r--r--
WinRT: bug-fix, fullscreen window flags weren't set if device was rotated 90 degrees
dludwig@8327
     1
/*
dludwig@8327
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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@8600
    21
#include "../../SDL_internal.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@9924
    33
#include <windows.graphics.display.h>
dludwig@9924
    34
#include <dxgi.h>
dludwig@9924
    35
#include <dxgi1_2.h>
dludwig@9924
    36
using namespace Windows::ApplicationModel::Core;
dludwig@9924
    37
using namespace Windows::Foundation;
dludwig@9925
    38
using namespace Windows::Graphics::Display;
dludwig@8494
    39
using namespace Windows::UI::Core;
dludwig@9924
    40
using namespace Windows::UI::ViewManagement;
dludwig@9924
    41
dludwig@9924
    42
dludwig@9924
    43
/* [re]declare Windows GUIDs locally, to limit the amount of external lib(s) SDL has to link to */
dludwig@9924
    44
static const GUID IID_IDXGIFactory2 = { 0x50c83a1c, 0xe072, 0x4c48,{ 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0 } };
dludwig@8494
    45
dludwig@8494
    46
dludwig@8494
    47
/* SDL includes */
dludwig@8329
    48
extern "C" {
dludwig@8327
    49
#include "SDL_video.h"
dludwig@8327
    50
#include "SDL_mouse.h"
dludwig@8327
    51
#include "../SDL_sysvideo.h"
dludwig@8327
    52
#include "../SDL_pixels_c.h"
dludwig@8327
    53
#include "../../events/SDL_events_c.h"
dludwig@8400
    54
#include "../../render/SDL_sysrender.h"
dludwig@8411
    55
#include "SDL_syswm.h"
dludwig@8541
    56
#include "SDL_winrtopengles.h"
dludwig@9924
    57
#include "../../core/windows/SDL_windows.h"
dludwig@8329
    58
}
dludwig@8327
    59
dludwig@8522
    60
#include "../../core/winrt/SDL_winrtapp_direct3d.h"
dludwig@8522
    61
#include "../../core/winrt/SDL_winrtapp_xaml.h"
dludwig@8512
    62
#include "SDL_winrtvideo_cpp.h"
dludwig@8327
    63
#include "SDL_winrtevents_c.h"
dludwig@8516
    64
#include "SDL_winrtmouse_c.h"
dludwig@8505
    65
#include "SDL_main.h"
dludwig@8505
    66
#include "SDL_system.h"
dludwig@8755
    67
//#include "SDL_log.h"
dludwig@8327
    68
dludwig@8327
    69
dludwig@8327
    70
/* Initialization/Query functions */
dludwig@8327
    71
static int WINRT_VideoInit(_THIS);
dludwig@8374
    72
static int WINRT_InitModes(_THIS);
dludwig@8327
    73
static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
dludwig@8327
    74
static void WINRT_VideoQuit(_THIS);
dludwig@8327
    75
dludwig@8494
    76
dludwig@8333
    77
/* Window functions */
dludwig@8522
    78
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@8333
    79
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    80
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    81
dludwig@8494
    82
dludwig@8515
    83
/* SDL-internal globals: */
dludwig@8497
    84
SDL_Window * WINRT_GlobalSDLWindow = 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@8663
    98
    if (device->driverdata) {
dludwig@8663
    99
        SDL_VideoData * video_data = (SDL_VideoData *)device->driverdata;
dludwig@8663
   100
        if (video_data->winrtEglWindow) {
dludwig@8663
   101
            video_data->winrtEglWindow->Release();
dludwig@8663
   102
        }
dludwig@8663
   103
        SDL_free(video_data);
dludwig@8663
   104
    }
dludwig@8663
   105
dludwig@8327
   106
    SDL_free(device);
dludwig@8327
   107
}
dludwig@8327
   108
dludwig@8327
   109
static SDL_VideoDevice *
dludwig@8327
   110
WINRT_CreateDevice(int devindex)
dludwig@8327
   111
{
dludwig@8327
   112
    SDL_VideoDevice *device;
dludwig@8663
   113
    SDL_VideoData *data;
dludwig@8327
   114
dludwig@8327
   115
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
   116
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
   117
    if (!device) {
dludwig@8327
   118
        SDL_OutOfMemory();
dludwig@8327
   119
        if (device) {
dludwig@8327
   120
            SDL_free(device);
dludwig@8327
   121
        }
dludwig@8327
   122
        return (0);
dludwig@8327
   123
    }
dludwig@8327
   124
dludwig@8663
   125
    data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
dludwig@8663
   126
    if (!data) {
dludwig@8663
   127
        SDL_OutOfMemory();
dludwig@8663
   128
        return (0);
dludwig@8663
   129
    }
dludwig@8663
   130
    SDL_zerop(data);
dludwig@8663
   131
    device->driverdata = data;
dludwig@8663
   132
dludwig@8327
   133
    /* Set the function pointers */
dludwig@8327
   134
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   135
    device->VideoQuit = WINRT_VideoQuit;
dludwig@8333
   136
    device->CreateWindow = WINRT_CreateWindow;
dludwig@8333
   137
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   138
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   139
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8411
   140
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@8541
   141
#ifdef SDL_VIDEO_OPENGL_EGL
slouken@8582
   142
    device->GL_LoadLibrary = WINRT_GLES_LoadLibrary;
slouken@8582
   143
    device->GL_GetProcAddress = WINRT_GLES_GetProcAddress;
slouken@8582
   144
    device->GL_UnloadLibrary = WINRT_GLES_UnloadLibrary;
slouken@8582
   145
    device->GL_CreateContext = WINRT_GLES_CreateContext;
slouken@8582
   146
    device->GL_MakeCurrent = WINRT_GLES_MakeCurrent;
slouken@8582
   147
    device->GL_SetSwapInterval = WINRT_GLES_SetSwapInterval;
slouken@8582
   148
    device->GL_GetSwapInterval = WINRT_GLES_GetSwapInterval;
slouken@8582
   149
    device->GL_SwapWindow = WINRT_GLES_SwapWindow;
dludwig@8541
   150
    device->GL_DeleteContext = WINRT_GLES_DeleteContext;
dludwig@8541
   151
#endif
dludwig@8433
   152
    device->free = WINRT_DeleteDevice;
dludwig@8327
   153
dludwig@8327
   154
    return device;
dludwig@8327
   155
}
dludwig@8327
   156
dludwig@8494
   157
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
   158
VideoBootStrap WINRT_bootstrap = {
dludwig@8500
   159
    WINRTVID_DRIVER_NAME, "SDL WinRT video driver",
dludwig@8327
   160
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   161
};
dludwig@8327
   162
dludwig@8327
   163
int
dludwig@8327
   164
WINRT_VideoInit(_THIS)
dludwig@8327
   165
{
dludwig@8374
   166
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   167
        return -1;
dludwig@8374
   168
    }
dludwig@8374
   169
    WINRT_InitMouse(_this);
dludwig@8515
   170
    WINRT_InitTouch(_this);
dludwig@8522
   171
dludwig@8374
   172
    return 0;
dludwig@8374
   173
}
dludwig@8374
   174
dludwig@9924
   175
extern "C"
dludwig@9924
   176
Uint32 D3D11_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat);
dludwig@9924
   177
dludwig@9924
   178
static void
dludwig@9924
   179
WINRT_DXGIModeToSDLDisplayMode(const DXGI_MODE_DESC * dxgiMode, SDL_DisplayMode * sdlMode)
dludwig@8522
   180
{
dludwig@9924
   181
    SDL_zerop(sdlMode);
dludwig@9924
   182
    sdlMode->w = dxgiMode->Width;
dludwig@9924
   183
    sdlMode->h = dxgiMode->Height;
dludwig@9924
   184
    sdlMode->refresh_rate = dxgiMode->RefreshRate.Numerator / dxgiMode->RefreshRate.Denominator;
dludwig@9924
   185
    sdlMode->format = D3D11_DXGIFormatToSDLPixelFormat(dxgiMode->Format);
dludwig@9924
   186
}
dludwig@8578
   187
dludwig@9924
   188
static int
dludwig@9924
   189
WINRT_AddDisplaysForOutput (_THIS, IDXGIAdapter1 * dxgiAdapter1, int outputIndex)
dludwig@9924
   190
{
dludwig@9924
   191
    HRESULT hr;
dludwig@9924
   192
    IDXGIOutput * dxgiOutput = NULL;
dludwig@9924
   193
    DXGI_OUTPUT_DESC dxgiOutputDesc;
dludwig@9924
   194
    SDL_VideoDisplay display;
dludwig@9924
   195
    char * displayName = NULL;
dludwig@9924
   196
    UINT numModes;
dludwig@9924
   197
    DXGI_MODE_DESC * dxgiModes = NULL;
dludwig@9924
   198
    int functionResult = -1;        /* -1 for failure, 0 for success */
dludwig@9924
   199
    DXGI_MODE_DESC modeToMatch, closestMatch;
dludwig@8522
   200
dludwig@9924
   201
    SDL_zero(display);
dludwig@9924
   202
dludwig@9924
   203
    hr = dxgiAdapter1->EnumOutputs(outputIndex, &dxgiOutput);
dludwig@9924
   204
    if (FAILED(hr)) {
dludwig@9924
   205
        if (hr != DXGI_ERROR_NOT_FOUND) {
dludwig@9924
   206
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIAdapter1::EnumOutputs failed", hr);
dludwig@9924
   207
        }
dludwig@9924
   208
        goto done;
dludwig@8522
   209
    }
dludwig@8522
   210
dludwig@9924
   211
    hr = dxgiOutput->GetDesc(&dxgiOutputDesc);
dludwig@9924
   212
    if (FAILED(hr)) {
dludwig@9924
   213
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDesc failed", hr);
dludwig@9924
   214
        goto done;
dludwig@8579
   215
    }
dludwig@8579
   216
dludwig@9924
   217
    SDL_zero(modeToMatch);
dludwig@9924
   218
    modeToMatch.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@9924
   219
    modeToMatch.Width = (dxgiOutputDesc.DesktopCoordinates.right - dxgiOutputDesc.DesktopCoordinates.left);
dludwig@9924
   220
    modeToMatch.Height = (dxgiOutputDesc.DesktopCoordinates.bottom - dxgiOutputDesc.DesktopCoordinates.top);
dludwig@9924
   221
    hr = dxgiOutput->FindClosestMatchingMode(&modeToMatch, &closestMatch, NULL);
dludwig@9924
   222
    if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) {
dludwig@9924
   223
        /* DXGI_ERROR_NOT_CURRENTLY_AVAILABLE gets returned by IDXGIOutput::FindClosestMatchingMode
dludwig@9924
   224
           when running under the Windows Simulator, which uses Remote Desktop (formerly known as Terminal
dludwig@9924
   225
           Services) under the hood.  According to the MSDN docs for the similar function,
dludwig@9924
   226
           IDXGIOutput::GetDisplayModeList, DXGI_ERROR_NOT_CURRENTLY_AVAILABLE is returned if and
dludwig@9924
   227
           when an app is run under a Terminal Services session, hence the assumption.
dludwig@9924
   228
dludwig@9924
   229
           In this case, just add an SDL display mode, with approximated values.
dludwig@9924
   230
        */
dludwig@9924
   231
        SDL_DisplayMode mode;
dludwig@9924
   232
        SDL_zero(mode);
dludwig@9924
   233
        display.name = "Windows Simulator / Terminal Services Display";
dludwig@9924
   234
        mode.w = (dxgiOutputDesc.DesktopCoordinates.right - dxgiOutputDesc.DesktopCoordinates.left);
dludwig@9924
   235
        mode.h = (dxgiOutputDesc.DesktopCoordinates.bottom - dxgiOutputDesc.DesktopCoordinates.top);
dludwig@9924
   236
        mode.format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@9924
   237
        mode.refresh_rate = 0;  /* Display mode is unknown, so just fill in zero, as specified by SDL's header files */
dludwig@9924
   238
        display.desktop_mode = mode;
dludwig@9924
   239
        display.current_mode = mode;
dludwig@9924
   240
        if ( ! SDL_AddDisplayMode(&display, &mode)) {
dludwig@9924
   241
            goto done;
dludwig@9924
   242
        }
dludwig@9924
   243
    } else if (FAILED(hr)) {
dludwig@9924
   244
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::FindClosestMatchingMode failed", hr);
dludwig@9924
   245
        goto done;
dludwig@9924
   246
    } else {
dludwig@9924
   247
        displayName = WIN_StringToUTF8(dxgiOutputDesc.DeviceName);
dludwig@9924
   248
        display.name = displayName;
dludwig@9924
   249
        WINRT_DXGIModeToSDLDisplayMode(&closestMatch, &display.desktop_mode);
dludwig@9924
   250
        display.current_mode = display.desktop_mode;
dludwig@9924
   251
dludwig@9924
   252
        hr = dxgiOutput->GetDisplayModeList(DXGI_FORMAT_B8G8R8A8_UNORM, 0, &numModes, NULL);
dludwig@9924
   253
        if (FAILED(hr)) {
dludwig@9924
   254
            if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) {
dludwig@9924
   255
                // TODO, WinRT: make sure display mode(s) are added when using Terminal Services / Windows Simulator
dludwig@9924
   256
            }
dludwig@9924
   257
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDisplayModeList [get mode list size] failed", hr);
dludwig@9924
   258
            goto done;
dludwig@9924
   259
        }
dludwig@8578
   260
dludwig@9924
   261
        dxgiModes = (DXGI_MODE_DESC *)SDL_calloc(numModes, sizeof(DXGI_MODE_DESC));
dludwig@9924
   262
        if ( ! dxgiModes) {
dludwig@9924
   263
            SDL_OutOfMemory();
dludwig@9924
   264
            goto done;
dludwig@9924
   265
        }
dludwig@9924
   266
dludwig@9924
   267
        hr = dxgiOutput->GetDisplayModeList(DXGI_FORMAT_B8G8R8A8_UNORM, 0, &numModes, dxgiModes);
dludwig@9924
   268
        if (FAILED(hr)) {
dludwig@9924
   269
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDisplayModeList [get mode contents] failed", hr);
dludwig@9924
   270
            goto done;
dludwig@9924
   271
        }
dludwig@9924
   272
dludwig@9924
   273
        for (UINT i = 0; i < numModes; ++i) {
dludwig@9924
   274
            SDL_DisplayMode sdlMode;
dludwig@9924
   275
            WINRT_DXGIModeToSDLDisplayMode(&dxgiModes[i], &sdlMode);
dludwig@9924
   276
            SDL_AddDisplayMode(&display, &sdlMode);
dludwig@9924
   277
        }
dludwig@9924
   278
    }
dludwig@9924
   279
dludwig@9924
   280
    if (SDL_AddVideoDisplay(&display) < 0) {
dludwig@9924
   281
        goto done;
dludwig@9924
   282
    }
dludwig@8522
   283
dludwig@9924
   284
    functionResult = 0;     /* 0 for Success! */
dludwig@9924
   285
done:
dludwig@9924
   286
    if (dxgiModes) {
dludwig@9924
   287
        SDL_free(dxgiModes);
dludwig@9924
   288
    }
dludwig@9924
   289
    if (dxgiOutput) {
dludwig@9924
   290
        dxgiOutput->Release();
dludwig@9924
   291
    }
dludwig@9924
   292
    if (displayName) {
dludwig@9924
   293
        SDL_free(displayName);
dludwig@9924
   294
    }
dludwig@9924
   295
    return functionResult;
dludwig@9924
   296
}
dludwig@9924
   297
dludwig@9924
   298
static int
dludwig@9924
   299
WINRT_AddDisplaysForAdapter (_THIS, IDXGIFactory2 * dxgiFactory2, int adapterIndex)
dludwig@9924
   300
{
dludwig@9924
   301
    HRESULT hr;
dludwig@9924
   302
    IDXGIAdapter1 * dxgiAdapter1;
dludwig@9924
   303
dludwig@9924
   304
    hr = dxgiFactory2->EnumAdapters1(adapterIndex, &dxgiAdapter1);
dludwig@9924
   305
    if (FAILED(hr)) {
dludwig@9924
   306
        if (hr != DXGI_ERROR_NOT_FOUND) {
dludwig@9924
   307
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory1::EnumAdapters1() failed", hr);
dludwig@9924
   308
        }
dludwig@9924
   309
        return -1;
dludwig@9924
   310
    }
dludwig@9924
   311
dludwig@9924
   312
    for (int outputIndex = 0; ; ++outputIndex) {
dludwig@9924
   313
        if (WINRT_AddDisplaysForOutput(_this, dxgiAdapter1, outputIndex) < 0) {
dludwig@8522
   314
            break;
dludwig@8522
   315
        }
dludwig@8522
   316
    }
dludwig@8578
   317
dludwig@9924
   318
    dxgiAdapter1->Release();
dludwig@8578
   319
    return 0;
dludwig@8522
   320
}
dludwig@8503
   321
dludwig@8505
   322
int
dludwig@8374
   323
WINRT_InitModes(_THIS)
dludwig@8374
   324
{
dludwig@9924
   325
    /* HACK: Initialize a single display, for whatever screen the app's
dludwig@9924
   326
         CoreApplicationView is on.
dludwig@9924
   327
       TODO, WinRT: Try initializing multiple displays, one for each monitor.
dludwig@9924
   328
         Appropriate WinRT APIs for this seem elusive, though.  -- DavidL
dludwig@9924
   329
    */
dludwig@8522
   330
dludwig@9924
   331
    HRESULT hr;
dludwig@9924
   332
    IDXGIFactory2 * dxgiFactory2 = NULL;
dludwig@9924
   333
dludwig@9924
   334
    hr = CreateDXGIFactory1(IID_IDXGIFactory2, (void **)&dxgiFactory2);
dludwig@9924
   335
    if (FAILED(hr)) {
dludwig@9924
   336
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", CreateDXGIFactory1() failed", hr);
dludwig@8522
   337
        return -1;
dludwig@8374
   338
    }
dludwig@8374
   339
dludwig@9924
   340
    int adapterIndex = 0;
dludwig@9924
   341
    for (int adapterIndex = 0; ; ++adapterIndex) {
dludwig@9924
   342
        if (WINRT_AddDisplaysForAdapter(_this, dxgiFactory2, adapterIndex) < 0) {
dludwig@9924
   343
            break;
dludwig@9924
   344
        }
dludwig@9924
   345
    }
dludwig@9924
   346
dludwig@8327
   347
    return 0;
dludwig@8327
   348
}
dludwig@8327
   349
dludwig@8327
   350
static int
dludwig@8327
   351
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   352
{
dludwig@8327
   353
    return 0;
dludwig@8327
   354
}
dludwig@8327
   355
dludwig@8327
   356
void
dludwig@8327
   357
WINRT_VideoQuit(_THIS)
dludwig@8327
   358
{
dludwig@8374
   359
    WINRT_QuitMouse(_this);
dludwig@8327
   360
}
dludwig@8327
   361
dludwig@9924
   362
extern "C" Uint32
dludwig@9924
   363
WINRT_DetectWindowFlags(SDL_Window * window)
dludwig@9924
   364
{
dludwig@9924
   365
    Uint32 latestFlags = 0;
dludwig@9924
   366
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@9924
   367
    bool is_fullscreen = false;
dludwig@9924
   368
dludwig@9924
   369
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9924
   370
    if (data->appView) {
dludwig@9924
   371
        is_fullscreen = data->appView->IsFullScreen;
dludwig@9924
   372
    }
dludwig@9924
   373
#elif (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
dludwig@9924
   374
    is_fullscreen = true;
dludwig@9924
   375
#endif
dludwig@9924
   376
dludwig@9924
   377
    if (data->coreWindow.Get()) {
dludwig@9924
   378
        if (is_fullscreen) {
dludwig@9924
   379
            SDL_VideoDisplay * display = SDL_GetDisplayForWindow(window);
dludwig@9925
   380
            int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9925
   381
            int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@9925
   382
dludwig@9925
   383
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
dludwig@9925
   384
            // On all WinRT platforms, except for WinPhone 8.0, rotate the
dludwig@9925
   385
            // window size.  This is needed to properly calculate
dludwig@9925
   386
            // fullscreen vs. maximized.
dludwig@9925
   387
            const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
dludwig@9925
   388
            switch (currentOrientation) {
dludwig@9925
   389
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
dludwig@9925
   390
                case DisplayOrientations::Landscape:
dludwig@9925
   391
                case DisplayOrientations::LandscapeFlipped:
dludwig@9925
   392
#else
dludwig@9925
   393
                case DisplayOrientations::Portrait:
dludwig@9925
   394
                case DisplayOrientations::PortraitFlipped:
dludwig@9925
   395
#endif
dludwig@9925
   396
                {
dludwig@9925
   397
                    int tmp = w;
dludwig@9925
   398
                    w = h;
dludwig@9925
   399
                    h = tmp;
dludwig@9925
   400
                } break;
dludwig@9925
   401
            }
dludwig@9925
   402
#endif
dludwig@9925
   403
dludwig@9925
   404
            if (display->desktop_mode.w != w || display->desktop_mode.h != h) {
dludwig@9924
   405
                latestFlags |= SDL_WINDOW_MAXIMIZED;
dludwig@9924
   406
            } else {
dludwig@9924
   407
                latestFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
dludwig@9924
   408
            }
dludwig@9924
   409
        }
dludwig@9924
   410
dludwig@9924
   411
        if (data->coreWindow->Visible) {
dludwig@9924
   412
            latestFlags |= SDL_WINDOW_SHOWN;
dludwig@9924
   413
        } else {
dludwig@9924
   414
            latestFlags |= SDL_WINDOW_HIDDEN;
dludwig@9924
   415
        }
dludwig@9924
   416
dludwig@9924
   417
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION < NTDDI_WINBLUE)
dludwig@9924
   418
        // data->coreWindow->PointerPosition is not supported on WinPhone 8.0
dludwig@9924
   419
        latestFlags |= SDL_WINDOW_MOUSE_FOCUS;
dludwig@9924
   420
#else
dludwig@9924
   421
        if (data->coreWindow->Bounds.Contains(data->coreWindow->PointerPosition)) {
dludwig@9924
   422
            latestFlags |= SDL_WINDOW_MOUSE_FOCUS;
dludwig@9924
   423
        }
dludwig@9924
   424
#endif
dludwig@9924
   425
    }
dludwig@9924
   426
dludwig@9924
   427
    return latestFlags;
dludwig@9924
   428
}
dludwig@9924
   429
dludwig@9924
   430
void
dludwig@9924
   431
WINRT_UpdateWindowFlags(SDL_Window * window, Uint32 mask)
dludwig@9924
   432
{
dludwig@9924
   433
    if (window) {
dludwig@9924
   434
        Uint32 apply = WINRT_DetectWindowFlags(window);
dludwig@9924
   435
        if ((apply & mask) & SDL_WINDOW_FULLSCREEN) {
dludwig@9924
   436
            window->last_fullscreen_flags = window->flags;  // seems necessary to programmatically un-fullscreen, via SDL APIs
dludwig@9924
   437
        }
dludwig@9924
   438
        window->flags = (window->flags & ~mask) | (apply & mask);
dludwig@9924
   439
    }
dludwig@9924
   440
}
dludwig@9924
   441
dludwig@8522
   442
int
dludwig@8522
   443
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8522
   444
{
dludwig@8522
   445
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8522
   446
    // support is added.
dludwig@8522
   447
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8522
   448
        SDL_SetError("WinRT only supports one window");
dludwig@8522
   449
        return -1;
dludwig@8522
   450
    }
dludwig@8522
   451
dludwig@9924
   452
    SDL_WindowData *data = new SDL_WindowData;  /* use 'new' here as SDL_WindowData may use WinRT/C++ types */
dludwig@8522
   453
    if (!data) {
dludwig@8522
   454
        SDL_OutOfMemory();
dludwig@8522
   455
        return -1;
dludwig@8522
   456
    }
dludwig@8522
   457
    window->driverdata = data;
dludwig@8522
   458
    data->sdlWindow = window;
dludwig@8522
   459
dludwig@8522
   460
    /* To note, when XAML support is enabled, access to the CoreWindow will not
dludwig@8522
   461
       be possible, at least not via the SDL/XAML thread.  Attempts to access it
dludwig@8522
   462
       from there will throw exceptions.  As such, the SDL_WindowData's
dludwig@8522
   463
       'coreWindow' field will only be set (to a non-null value) if XAML isn't
dludwig@8522
   464
       enabled.
dludwig@8522
   465
    */
dludwig@8522
   466
    if (!WINRT_XAMLWasEnabled) {
dludwig@8522
   467
        data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@9924
   468
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9924
   469
        data->appView = ApplicationView::GetForCurrentView();
dludwig@9924
   470
#endif
dludwig@8522
   471
    }
dludwig@8522
   472
dludwig@8541
   473
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   474
    /* Setup the EGL surface, but only if OpenGL ES 2 was requested. */
dludwig@8541
   475
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
dludwig@8541
   476
        /* OpenGL ES 2 wasn't requested.  Don't set up an EGL surface. */
dludwig@8541
   477
        data->egl_surface = EGL_NO_SURFACE;
dludwig@8541
   478
    } else {
dludwig@8541
   479
        /* OpenGL ES 2 was reuqested.  Set up an EGL surface. */
dludwig@8663
   480
        SDL_VideoData * video_data = (SDL_VideoData *)_this->driverdata;
dludwig@8572
   481
dludwig@8663
   482
        /* Call SDL_EGL_ChooseConfig and eglCreateWindowSurface directly,
dludwig@9212
   483
         * rather than via SDL_EGL_CreateSurface, as older versions of
dludwig@9212
   484
         * ANGLE/WinRT may require that a C++ object, ComPtr<IUnknown>,
dludwig@9212
   485
         * be passed into eglCreateWindowSurface.
dludwig@8572
   486
         */
dludwig@8663
   487
        if (SDL_EGL_ChooseConfig(_this) != 0) {
dludwig@8663
   488
            char buf[512];
dludwig@8663
   489
            SDL_snprintf(buf, sizeof(buf), "SDL_EGL_ChooseConfig failed: %s", SDL_GetError());
philipp@9557
   490
            return SDL_SetError("%s", buf);
dludwig@8663
   491
        }
dludwig@8572
   492
dludwig@9211
   493
        if (video_data->winrtEglWindow) {   /* ... is the 'old' version of ANGLE/WinRT being used? */
dludwig@9211
   494
            /* Attempt to create a window surface using older versions of
dludwig@9211
   495
             * ANGLE/WinRT:
dludwig@9211
   496
             */
dludwig@9211
   497
            Microsoft::WRL::ComPtr<IUnknown> cpp_winrtEglWindow = video_data->winrtEglWindow;
dludwig@9211
   498
            data->egl_surface = ((eglCreateWindowSurface_Old_Function)_this->egl_data->eglCreateWindowSurface)(
dludwig@9211
   499
                _this->egl_data->egl_display,
dludwig@9211
   500
                _this->egl_data->egl_config,
dludwig@9211
   501
                cpp_winrtEglWindow, NULL);
dludwig@9211
   502
            if (data->egl_surface == NULL) {
dludwig@9211
   503
                return SDL_SetError("eglCreateWindowSurface failed");
dludwig@9211
   504
            }
dludwig@9211
   505
        } else if (data->coreWindow.Get() != nullptr) {
dludwig@9211
   506
            /* Attempt to create a window surface using newer versions of
dludwig@9211
   507
             * ANGLE/WinRT:
dludwig@9211
   508
             */
dludwig@9211
   509
            IInspectable * coreWindowAsIInspectable = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@9211
   510
            data->egl_surface = _this->egl_data->eglCreateWindowSurface(
dludwig@9211
   511
                _this->egl_data->egl_display,
dludwig@9211
   512
                _this->egl_data->egl_config,
dludwig@9211
   513
                coreWindowAsIInspectable,
dludwig@9211
   514
                NULL);
dludwig@9211
   515
            if (data->egl_surface == NULL) {
dludwig@9211
   516
                return SDL_SetError("eglCreateWindowSurface failed");
dludwig@9211
   517
            }
dludwig@9211
   518
        } else {
dludwig@9211
   519
            return SDL_SetError("No supported means to create an EGL window surface are available");
dludwig@8541
   520
        }
dludwig@8541
   521
    }
dludwig@8541
   522
#endif
dludwig@8541
   523
dludwig@9924
   524
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9924
   525
    /* Determine as many flags dynamically, as possible. */
dludwig@8522
   526
    window->flags =
dludwig@9924
   527
        SDL_WINDOW_BORDERLESS;
dludwig@9924
   528
#else
dludwig@9924
   529
    /* Set SDL_Window flags for Windows Phone 8.0 */
dludwig@9924
   530
    window->flags =
dludwig@9924
   531
        SDL_WINDOW_FULLSCREEN_DESKTOP |
dludwig@8522
   532
        SDL_WINDOW_BORDERLESS |
dludwig@8522
   533
        SDL_WINDOW_MAXIMIZED |
dludwig@8522
   534
        SDL_WINDOW_INPUT_GRABBED;
dludwig@9924
   535
#endif
dludwig@8522
   536
dludwig@8541
   537
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   538
    if (data->egl_surface) {
dludwig@8541
   539
        window->flags |= SDL_WINDOW_OPENGL;
dludwig@8541
   540
    }
dludwig@8541
   541
#endif
dludwig@8541
   542
dludwig@9924
   543
    if (WINRT_XAMLWasEnabled) {
dludwig@9924
   544
        /* TODO, WinRT: set SDL_Window size, maybe position too, from XAML control */
dludwig@9924
   545
        window->x = 0;
dludwig@9924
   546
        window->y = 0;
dludwig@9924
   547
        window->flags |= SDL_WINDOW_SHOWN;
dludwig@9924
   548
        SDL_SetMouseFocus(NULL);        // TODO: detect this
dludwig@9924
   549
        SDL_SetKeyboardFocus(NULL);     // TODO: detect this
dludwig@9924
   550
    } else {
dludwig@9924
   551
        /* WinRT apps seem to live in an environment where the OS controls the
dludwig@9924
   552
           app's window size, with some apps being fullscreen, depending on
dludwig@9924
   553
           user choice of various things.  For now, just adapt the SDL_Window to
dludwig@9924
   554
           whatever Windows set-up as the native-window's geometry.
dludwig@9924
   555
        */
dludwig@9924
   556
        window->x = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Left);
dludwig@9924
   557
        window->y = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Top);
dludwig@9924
   558
        window->w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9924
   559
        window->h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@8522
   560
dludwig@9924
   561
        WINRT_UpdateWindowFlags(
dludwig@9924
   562
            window,
dludwig@9924
   563
            0xffffffff      /* Update any window flag(s) that WINRT_UpdateWindow can handle */
dludwig@9924
   564
        );
dludwig@8551
   565
dludwig@9924
   566
        /* Try detecting if the window is active */
dludwig@9924
   567
        bool isWindowActive = true;     /* Presume the window is active, unless we've been told otherwise */
dludwig@9924
   568
        if (data->coreWindow->CustomProperties->HasKey("SDLHelperWindowActivationState")) {
dludwig@9924
   569
            CoreWindowActivationState activationState = \
dludwig@9924
   570
                safe_cast<CoreWindowActivationState>(data->coreWindow->CustomProperties->Lookup("SDLHelperWindowActivationState"));
dludwig@9924
   571
            isWindowActive = (activationState != CoreWindowActivationState::Deactivated);
dludwig@9924
   572
        }
dludwig@9924
   573
        if (isWindowActive) {
dludwig@9924
   574
            SDL_SetKeyboardFocus(window);
dludwig@9924
   575
        }
dludwig@9924
   576
    }
dludwig@8522
   577
 
dludwig@8522
   578
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8522
   579
       behalf of SDL:
dludwig@8522
   580
    */
dludwig@8522
   581
    WINRT_GlobalSDLWindow = window;
dludwig@8522
   582
dludwig@8522
   583
    /* All done! */
dludwig@8522
   584
    return 0;
dludwig@8522
   585
}
dludwig@8522
   586
dludwig@8333
   587
void
dludwig@8333
   588
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   589
{
dludwig@8411
   590
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   591
dludwig@8497
   592
    if (WINRT_GlobalSDLWindow == window) {
dludwig@8497
   593
        WINRT_GlobalSDLWindow = NULL;
dludwig@8424
   594
    }
dludwig@8424
   595
dludwig@8411
   596
    if (data) {
dludwig@8411
   597
        // Delete the internal window data:
dludwig@8411
   598
        delete data;
dludwig@8411
   599
        data = NULL;
dludwig@9214
   600
        window->driverdata = NULL;
dludwig@8411
   601
    }
dludwig@8333
   602
}
dludwig@8333
   603
dludwig@8522
   604
SDL_bool
dludwig@8522
   605
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8522
   606
{
dludwig@8522
   607
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8522
   608
dludwig@8522
   609
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8522
   610
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8527
   611
        info->info.winrt.window = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@8522
   612
        return SDL_TRUE;
dludwig@8522
   613
    } else {
dludwig@8522
   614
        SDL_SetError("Application not compiled with SDL %d.%d\n",
dludwig@8522
   615
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8522
   616
        return SDL_FALSE;
dludwig@8522
   617
    }
dludwig@8522
   618
    return SDL_FALSE;
dludwig@8411
   619
}
dludwig@8333
   620
dludwig@8327
   621
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   622
dludwig@8327
   623
/* vi: set ts=4 sw=4 expandtab: */