src/video/winrt/SDL_winrtvideo.cpp
author Sam Lantinga <slouken@libsdl.org>
Thu, 04 Jun 2020 12:30:25 -0700
changeset 13898 aa9d7c43a982
parent 13696 ea20a7434b98
child 13946 5acc27d3d654
permissions -rw-r--r--
Fixed exception if getManifestEnvironmentVariables() is called without a current SDL activity
dludwig@8327
     1
/*
dludwig@8327
     2
  Simple DirectMedia Layer
slouken@13422
     3
  Copyright (C) 1997-2020 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@10293
    34
#include <windows.system.display.h>
dludwig@9924
    35
#include <dxgi.h>
dludwig@9924
    36
#include <dxgi1_2.h>
dludwig@9924
    37
using namespace Windows::ApplicationModel::Core;
dludwig@9924
    38
using namespace Windows::Foundation;
dludwig@9925
    39
using namespace Windows::Graphics::Display;
dludwig@8494
    40
using namespace Windows::UI::Core;
dludwig@9924
    41
using namespace Windows::UI::ViewManagement;
dludwig@9924
    42
dludwig@9924
    43
dludwig@9924
    44
/* [re]declare Windows GUIDs locally, to limit the amount of external lib(s) SDL has to link to */
dludwig@10293
    45
static const GUID IID_IDisplayRequest   = { 0xe5732044, 0xf49f, 0x4b60, { 0x8d, 0xd4, 0x5e, 0x7e, 0x3a, 0x63, 0x2a, 0xc0 } };
dludwig@10293
    46
static const GUID IID_IDXGIFactory2     = { 0x50c83a1c, 0xe072, 0x4c48, { 0x87, 0xb0, 0x36, 0x30, 0xfa, 0x36, 0xa6, 0xd0 } };
dludwig@8494
    47
dludwig@8494
    48
dludwig@8494
    49
/* SDL includes */
dludwig@8329
    50
extern "C" {
dludwig@8327
    51
#include "SDL_video.h"
dludwig@8327
    52
#include "SDL_mouse.h"
dludwig@8327
    53
#include "../SDL_sysvideo.h"
dludwig@8327
    54
#include "../SDL_pixels_c.h"
dludwig@8327
    55
#include "../../events/SDL_events_c.h"
dludwig@8400
    56
#include "../../render/SDL_sysrender.h"
dludwig@8411
    57
#include "SDL_syswm.h"
dludwig@8541
    58
#include "SDL_winrtopengles.h"
dludwig@9924
    59
#include "../../core/windows/SDL_windows.h"
dludwig@8329
    60
}
dludwig@8327
    61
dludwig@8522
    62
#include "../../core/winrt/SDL_winrtapp_direct3d.h"
dludwig@8522
    63
#include "../../core/winrt/SDL_winrtapp_xaml.h"
dludwig@8512
    64
#include "SDL_winrtvideo_cpp.h"
dludwig@8327
    65
#include "SDL_winrtevents_c.h"
dludwig@10222
    66
#include "SDL_winrtgamebar_cpp.h"
dludwig@8516
    67
#include "SDL_winrtmouse_c.h"
dludwig@8505
    68
#include "SDL_main.h"
dludwig@8505
    69
#include "SDL_system.h"
dludwig@8327
    70
dludwig@8327
    71
dludwig@8327
    72
/* Initialization/Query functions */
dludwig@8327
    73
static int WINRT_VideoInit(_THIS);
dludwig@8374
    74
static int WINRT_InitModes(_THIS);
dludwig@8327
    75
static int WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
dludwig@8327
    76
static void WINRT_VideoQuit(_THIS);
dludwig@8327
    77
dludwig@8494
    78
dludwig@8333
    79
/* Window functions */
dludwig@8522
    80
static int WINRT_CreateWindow(_THIS, SDL_Window * window);
dludwig@9931
    81
static void WINRT_SetWindowSize(_THIS, SDL_Window * window);
dludwig@9931
    82
static void WINRT_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen);
dludwig@8333
    83
static void WINRT_DestroyWindow(_THIS, SDL_Window * window);
dludwig@8411
    84
static SDL_bool WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info);
dludwig@8333
    85
dludwig@8494
    86
dludwig@10293
    87
/* Misc functions */
dludwig@10293
    88
static ABI::Windows::System::Display::IDisplayRequest * WINRT_CreateDisplayRequest(_THIS);
dludwig@10293
    89
extern void WINRT_SuspendScreenSaver(_THIS);
dludwig@10293
    90
dludwig@10293
    91
dludwig@8515
    92
/* SDL-internal globals: */
dludwig@8497
    93
SDL_Window * WINRT_GlobalSDLWindow = NULL;
dludwig@8498
    94
dludwig@8498
    95
dludwig@8327
    96
/* WinRT driver bootstrap functions */
dludwig@8327
    97
dludwig@8327
    98
static int
dludwig@8327
    99
WINRT_Available(void)
dludwig@8327
   100
{
dludwig@8328
   101
    return (1);
dludwig@8327
   102
}
dludwig@8327
   103
dludwig@8327
   104
static void
dludwig@8327
   105
WINRT_DeleteDevice(SDL_VideoDevice * device)
dludwig@8327
   106
{
dludwig@8663
   107
    if (device->driverdata) {
dludwig@8663
   108
        SDL_VideoData * video_data = (SDL_VideoData *)device->driverdata;
dludwig@8663
   109
        if (video_data->winrtEglWindow) {
dludwig@8663
   110
            video_data->winrtEglWindow->Release();
dludwig@8663
   111
        }
dludwig@8663
   112
        SDL_free(video_data);
dludwig@8663
   113
    }
dludwig@8663
   114
dludwig@8327
   115
    SDL_free(device);
dludwig@8327
   116
}
dludwig@8327
   117
dludwig@8327
   118
static SDL_VideoDevice *
dludwig@8327
   119
WINRT_CreateDevice(int devindex)
dludwig@8327
   120
{
dludwig@8327
   121
    SDL_VideoDevice *device;
dludwig@8663
   122
    SDL_VideoData *data;
dludwig@8327
   123
dludwig@8327
   124
    /* Initialize all variables that we clean on shutdown */
dludwig@8327
   125
    device = (SDL_VideoDevice *) SDL_calloc(1, sizeof(SDL_VideoDevice));
dludwig@8327
   126
    if (!device) {
dludwig@8327
   127
        SDL_OutOfMemory();
dludwig@8327
   128
        return (0);
dludwig@8327
   129
    }
dludwig@8327
   130
dludwig@8663
   131
    data = (SDL_VideoData *) SDL_calloc(1, sizeof(SDL_VideoData));
dludwig@8663
   132
    if (!data) {
dludwig@8663
   133
        SDL_OutOfMemory();
philipp@10112
   134
        SDL_free(device);
dludwig@8663
   135
        return (0);
dludwig@8663
   136
    }
dludwig@8663
   137
    device->driverdata = data;
dludwig@8663
   138
dludwig@8327
   139
    /* Set the function pointers */
dludwig@8327
   140
    device->VideoInit = WINRT_VideoInit;
dludwig@8327
   141
    device->VideoQuit = WINRT_VideoQuit;
slouken@11383
   142
    device->CreateSDLWindow = WINRT_CreateWindow;
dludwig@9931
   143
    device->SetWindowSize = WINRT_SetWindowSize;
dludwig@9931
   144
    device->SetWindowFullscreen = WINRT_SetWindowFullscreen;
dludwig@8333
   145
    device->DestroyWindow = WINRT_DestroyWindow;
dludwig@8327
   146
    device->SetDisplayMode = WINRT_SetDisplayMode;
dludwig@8327
   147
    device->PumpEvents = WINRT_PumpEvents;
dludwig@8411
   148
    device->GetWindowWMInfo = WINRT_GetWindowWMInfo;
dludwig@10293
   149
    device->SuspendScreenSaver = WINRT_SuspendScreenSaver;
dludwig@10163
   150
dludwig@10163
   151
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@10163
   152
    device->HasScreenKeyboardSupport = WINRT_HasScreenKeyboardSupport;
dludwig@10163
   153
    device->ShowScreenKeyboard = WINRT_ShowScreenKeyboard;
dludwig@10163
   154
    device->HideScreenKeyboard = WINRT_HideScreenKeyboard;
dludwig@10163
   155
    device->IsScreenKeyboardShown = WINRT_IsScreenKeyboardShown;
dludwig@10163
   156
#endif
dludwig@10163
   157
dludwig@8541
   158
#ifdef SDL_VIDEO_OPENGL_EGL
slouken@8582
   159
    device->GL_LoadLibrary = WINRT_GLES_LoadLibrary;
slouken@8582
   160
    device->GL_GetProcAddress = WINRT_GLES_GetProcAddress;
slouken@8582
   161
    device->GL_UnloadLibrary = WINRT_GLES_UnloadLibrary;
slouken@8582
   162
    device->GL_CreateContext = WINRT_GLES_CreateContext;
slouken@8582
   163
    device->GL_MakeCurrent = WINRT_GLES_MakeCurrent;
slouken@8582
   164
    device->GL_SetSwapInterval = WINRT_GLES_SetSwapInterval;
slouken@8582
   165
    device->GL_GetSwapInterval = WINRT_GLES_GetSwapInterval;
slouken@8582
   166
    device->GL_SwapWindow = WINRT_GLES_SwapWindow;
dludwig@8541
   167
    device->GL_DeleteContext = WINRT_GLES_DeleteContext;
dludwig@8541
   168
#endif
dludwig@8433
   169
    device->free = WINRT_DeleteDevice;
dludwig@8327
   170
dludwig@8327
   171
    return device;
dludwig@8327
   172
}
dludwig@8327
   173
dludwig@8494
   174
#define WINRTVID_DRIVER_NAME "winrt"
dludwig@8327
   175
VideoBootStrap WINRT_bootstrap = {
dludwig@8500
   176
    WINRTVID_DRIVER_NAME, "SDL WinRT video driver",
dludwig@8327
   177
    WINRT_Available, WINRT_CreateDevice
dludwig@8327
   178
};
dludwig@8327
   179
dludwig@8327
   180
int
dludwig@8327
   181
WINRT_VideoInit(_THIS)
dludwig@8327
   182
{
dludwig@10293
   183
    SDL_VideoData * driverdata = (SDL_VideoData *) _this->driverdata;
dludwig@8374
   184
    if (WINRT_InitModes(_this) < 0) {
dludwig@8374
   185
        return -1;
dludwig@8374
   186
    }
dludwig@8374
   187
    WINRT_InitMouse(_this);
dludwig@8515
   188
    WINRT_InitTouch(_this);
dludwig@10222
   189
    WINRT_InitGameBar(_this);
dludwig@10293
   190
    if (driverdata) {
dludwig@10293
   191
        /* Initialize screensaver-disabling support */
dludwig@10293
   192
        driverdata->displayRequest = WINRT_CreateDisplayRequest(_this);
dludwig@10293
   193
    }
dludwig@8374
   194
    return 0;
dludwig@8374
   195
}
dludwig@8374
   196
dludwig@9924
   197
extern "C"
dludwig@9924
   198
Uint32 D3D11_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat);
dludwig@9924
   199
dludwig@9924
   200
static void
dludwig@9924
   201
WINRT_DXGIModeToSDLDisplayMode(const DXGI_MODE_DESC * dxgiMode, SDL_DisplayMode * sdlMode)
dludwig@8522
   202
{
dludwig@9924
   203
    SDL_zerop(sdlMode);
dludwig@9924
   204
    sdlMode->w = dxgiMode->Width;
dludwig@9924
   205
    sdlMode->h = dxgiMode->Height;
dludwig@9924
   206
    sdlMode->refresh_rate = dxgiMode->RefreshRate.Numerator / dxgiMode->RefreshRate.Denominator;
dludwig@9924
   207
    sdlMode->format = D3D11_DXGIFormatToSDLPixelFormat(dxgiMode->Format);
dludwig@9924
   208
}
dludwig@8578
   209
dludwig@9924
   210
static int
dludwig@9924
   211
WINRT_AddDisplaysForOutput (_THIS, IDXGIAdapter1 * dxgiAdapter1, int outputIndex)
dludwig@9924
   212
{
dludwig@9924
   213
    HRESULT hr;
dludwig@9924
   214
    IDXGIOutput * dxgiOutput = NULL;
dludwig@9924
   215
    DXGI_OUTPUT_DESC dxgiOutputDesc;
dludwig@9924
   216
    SDL_VideoDisplay display;
dludwig@9924
   217
    char * displayName = NULL;
dludwig@9924
   218
    UINT numModes;
dludwig@9924
   219
    DXGI_MODE_DESC * dxgiModes = NULL;
dludwig@9924
   220
    int functionResult = -1;        /* -1 for failure, 0 for success */
dludwig@9924
   221
    DXGI_MODE_DESC modeToMatch, closestMatch;
dludwig@8522
   222
dludwig@9924
   223
    SDL_zero(display);
dludwig@9924
   224
dludwig@9924
   225
    hr = dxgiAdapter1->EnumOutputs(outputIndex, &dxgiOutput);
dludwig@9924
   226
    if (FAILED(hr)) {
dludwig@9924
   227
        if (hr != DXGI_ERROR_NOT_FOUND) {
dludwig@9924
   228
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIAdapter1::EnumOutputs failed", hr);
dludwig@9924
   229
        }
dludwig@9924
   230
        goto done;
dludwig@8522
   231
    }
dludwig@8522
   232
dludwig@9924
   233
    hr = dxgiOutput->GetDesc(&dxgiOutputDesc);
dludwig@9924
   234
    if (FAILED(hr)) {
dludwig@9924
   235
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDesc failed", hr);
dludwig@9924
   236
        goto done;
dludwig@8579
   237
    }
dludwig@8579
   238
dludwig@9924
   239
    SDL_zero(modeToMatch);
dludwig@9924
   240
    modeToMatch.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@9924
   241
    modeToMatch.Width = (dxgiOutputDesc.DesktopCoordinates.right - dxgiOutputDesc.DesktopCoordinates.left);
dludwig@9924
   242
    modeToMatch.Height = (dxgiOutputDesc.DesktopCoordinates.bottom - dxgiOutputDesc.DesktopCoordinates.top);
dludwig@9924
   243
    hr = dxgiOutput->FindClosestMatchingMode(&modeToMatch, &closestMatch, NULL);
dludwig@9924
   244
    if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) {
dludwig@9924
   245
        /* DXGI_ERROR_NOT_CURRENTLY_AVAILABLE gets returned by IDXGIOutput::FindClosestMatchingMode
dludwig@9924
   246
           when running under the Windows Simulator, which uses Remote Desktop (formerly known as Terminal
dludwig@9924
   247
           Services) under the hood.  According to the MSDN docs for the similar function,
dludwig@9924
   248
           IDXGIOutput::GetDisplayModeList, DXGI_ERROR_NOT_CURRENTLY_AVAILABLE is returned if and
dludwig@9924
   249
           when an app is run under a Terminal Services session, hence the assumption.
dludwig@9924
   250
dludwig@9924
   251
           In this case, just add an SDL display mode, with approximated values.
dludwig@9924
   252
        */
dludwig@9924
   253
        SDL_DisplayMode mode;
dludwig@9924
   254
        SDL_zero(mode);
dludwig@9924
   255
        display.name = "Windows Simulator / Terminal Services Display";
dludwig@9924
   256
        mode.w = (dxgiOutputDesc.DesktopCoordinates.right - dxgiOutputDesc.DesktopCoordinates.left);
dludwig@9924
   257
        mode.h = (dxgiOutputDesc.DesktopCoordinates.bottom - dxgiOutputDesc.DesktopCoordinates.top);
dludwig@9924
   258
        mode.format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@9924
   259
        mode.refresh_rate = 0;  /* Display mode is unknown, so just fill in zero, as specified by SDL's header files */
dludwig@9924
   260
        display.desktop_mode = mode;
dludwig@9924
   261
        display.current_mode = mode;
dludwig@9924
   262
        if ( ! SDL_AddDisplayMode(&display, &mode)) {
dludwig@9924
   263
            goto done;
dludwig@9924
   264
        }
dludwig@9924
   265
    } else if (FAILED(hr)) {
dludwig@9924
   266
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::FindClosestMatchingMode failed", hr);
dludwig@9924
   267
        goto done;
dludwig@9924
   268
    } else {
dludwig@9924
   269
        displayName = WIN_StringToUTF8(dxgiOutputDesc.DeviceName);
dludwig@9924
   270
        display.name = displayName;
dludwig@9924
   271
        WINRT_DXGIModeToSDLDisplayMode(&closestMatch, &display.desktop_mode);
dludwig@9924
   272
        display.current_mode = display.desktop_mode;
dludwig@9924
   273
dludwig@9924
   274
        hr = dxgiOutput->GetDisplayModeList(DXGI_FORMAT_B8G8R8A8_UNORM, 0, &numModes, NULL);
dludwig@9924
   275
        if (FAILED(hr)) {
dludwig@9924
   276
            if (hr == DXGI_ERROR_NOT_CURRENTLY_AVAILABLE) {
dludwig@9924
   277
                // TODO, WinRT: make sure display mode(s) are added when using Terminal Services / Windows Simulator
dludwig@9924
   278
            }
dludwig@9924
   279
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDisplayModeList [get mode list size] failed", hr);
dludwig@9924
   280
            goto done;
dludwig@9924
   281
        }
dludwig@8578
   282
dludwig@9924
   283
        dxgiModes = (DXGI_MODE_DESC *)SDL_calloc(numModes, sizeof(DXGI_MODE_DESC));
dludwig@9924
   284
        if ( ! dxgiModes) {
dludwig@9924
   285
            SDL_OutOfMemory();
dludwig@9924
   286
            goto done;
dludwig@9924
   287
        }
dludwig@9924
   288
dludwig@9924
   289
        hr = dxgiOutput->GetDisplayModeList(DXGI_FORMAT_B8G8R8A8_UNORM, 0, &numModes, dxgiModes);
dludwig@9924
   290
        if (FAILED(hr)) {
dludwig@9924
   291
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIOutput::GetDisplayModeList [get mode contents] failed", hr);
dludwig@9924
   292
            goto done;
dludwig@9924
   293
        }
dludwig@9924
   294
dludwig@9924
   295
        for (UINT i = 0; i < numModes; ++i) {
dludwig@9924
   296
            SDL_DisplayMode sdlMode;
dludwig@9924
   297
            WINRT_DXGIModeToSDLDisplayMode(&dxgiModes[i], &sdlMode);
dludwig@9924
   298
            SDL_AddDisplayMode(&display, &sdlMode);
dludwig@9924
   299
        }
dludwig@9924
   300
    }
dludwig@9924
   301
dludwig@9924
   302
    if (SDL_AddVideoDisplay(&display) < 0) {
dludwig@9924
   303
        goto done;
dludwig@9924
   304
    }
dludwig@8522
   305
dludwig@9924
   306
    functionResult = 0;     /* 0 for Success! */
dludwig@9924
   307
done:
dludwig@9924
   308
    if (dxgiModes) {
dludwig@9924
   309
        SDL_free(dxgiModes);
dludwig@9924
   310
    }
dludwig@9924
   311
    if (dxgiOutput) {
dludwig@9924
   312
        dxgiOutput->Release();
dludwig@9924
   313
    }
dludwig@9924
   314
    if (displayName) {
dludwig@9924
   315
        SDL_free(displayName);
dludwig@9924
   316
    }
dludwig@9924
   317
    return functionResult;
dludwig@9924
   318
}
dludwig@9924
   319
dludwig@9924
   320
static int
dludwig@9924
   321
WINRT_AddDisplaysForAdapter (_THIS, IDXGIFactory2 * dxgiFactory2, int adapterIndex)
dludwig@9924
   322
{
dludwig@9924
   323
    HRESULT hr;
dludwig@9924
   324
    IDXGIAdapter1 * dxgiAdapter1;
dludwig@9924
   325
dludwig@9924
   326
    hr = dxgiFactory2->EnumAdapters1(adapterIndex, &dxgiAdapter1);
dludwig@9924
   327
    if (FAILED(hr)) {
dludwig@9924
   328
        if (hr != DXGI_ERROR_NOT_FOUND) {
dludwig@9924
   329
            WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGIFactory1::EnumAdapters1() failed", hr);
dludwig@9924
   330
        }
dludwig@9924
   331
        return -1;
dludwig@9924
   332
    }
dludwig@9924
   333
dludwig@9924
   334
    for (int outputIndex = 0; ; ++outputIndex) {
dludwig@9924
   335
        if (WINRT_AddDisplaysForOutput(_this, dxgiAdapter1, outputIndex) < 0) {
dludwig@9947
   336
            /* HACK: The Windows App Certification Kit 10.0 can fail, when
dludwig@9947
   337
               running the Store Apps' test, "Direct3D Feature Test".  The
dludwig@9947
   338
               certification kit's error is:
dludwig@9947
   339
dludwig@9947
   340
               "Application App was not running at the end of the test. It likely crashed or was terminated for having become unresponsive."
dludwig@9947
   341
dludwig@9947
   342
               This was caused by SDL/WinRT's DXGI failing to report any
dludwig@9947
   343
               outputs.  Attempts to get the 1st display-output from the
dludwig@9947
   344
               1st display-adapter can fail, with IDXGIAdapter::EnumOutputs
dludwig@9947
   345
               returning DXGI_ERROR_NOT_FOUND.  This could be a bug in Windows,
dludwig@9947
   346
               the Windows App Certification Kit, or possibly in SDL/WinRT's
dludwig@9947
   347
               display detection code.  Either way, try to detect when this
dludwig@9947
   348
               happens, and use a hackish means to create a reasonable-as-possible
dludwig@9947
   349
               'display mode'.  -- DavidL
dludwig@9947
   350
            */
dludwig@9947
   351
            if (adapterIndex == 0 && outputIndex == 0) {
dludwig@9947
   352
                SDL_VideoDisplay display;
dludwig@9947
   353
                SDL_DisplayMode mode;
dludwig@9952
   354
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9947
   355
                ApplicationView ^ appView = ApplicationView::GetForCurrentView();
dludwig@9952
   356
#endif
dludwig@9950
   357
                CoreWindow ^ coreWin = CoreWindow::GetForCurrentThread();
dludwig@9947
   358
                SDL_zero(display);
dludwig@9947
   359
                SDL_zero(mode);
dludwig@9947
   360
                display.name = "DXGI Display-detection Workaround";
dludwig@9947
   361
dludwig@9947
   362
                /* HACK: ApplicationView's VisibleBounds property, appeared, via testing, to
dludwig@9947
   363
                   give a better approximation of display-size, than did CoreWindow's
dludwig@9947
   364
                   Bounds property, insofar that ApplicationView::VisibleBounds seems like
dludwig@9947
   365
                   it will, at least some of the time, give the full display size (during the
dludwig@9950
   366
                   failing test), whereas CoreWindow might not.  -- DavidL
dludwig@9947
   367
                */
dludwig@9950
   368
dludwig@9952
   369
#if (NTDDI_VERSION >= NTDDI_WIN10) || (SDL_WINRT_USE_APPLICATIONVIEW && WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
dludwig@9947
   370
                mode.w = WINRT_DIPS_TO_PHYSICAL_PIXELS(appView->VisibleBounds.Width);
dludwig@9947
   371
                mode.h = WINRT_DIPS_TO_PHYSICAL_PIXELS(appView->VisibleBounds.Height);
dludwig@9950
   372
#else
dludwig@9950
   373
                /* On platform(s) that do not support VisibleBounds, such as Windows 8.1,
dludwig@9950
   374
                   fall back to CoreWindow's Bounds property.
dludwig@9950
   375
                */
dludwig@9950
   376
                mode.w = WINRT_DIPS_TO_PHYSICAL_PIXELS(coreWin->Bounds.Width);
dludwig@9950
   377
                mode.h = WINRT_DIPS_TO_PHYSICAL_PIXELS(coreWin->Bounds.Height);
dludwig@9950
   378
#endif
dludwig@9947
   379
dludwig@9947
   380
                mode.format = DXGI_FORMAT_B8G8R8A8_UNORM;
dludwig@9947
   381
                mode.refresh_rate = 0;  /* Display mode is unknown, so just fill in zero, as specified by SDL's header files */
dludwig@9947
   382
                display.desktop_mode = mode;
dludwig@9947
   383
                display.current_mode = mode;
dludwig@9947
   384
                if ((SDL_AddDisplayMode(&display, &mode) < 0) ||
dludwig@9947
   385
                    (SDL_AddVideoDisplay(&display) < 0))
dludwig@9947
   386
                {
dludwig@9947
   387
                    return SDL_SetError("Failed to apply DXGI Display-detection workaround");
dludwig@9947
   388
                }
dludwig@9947
   389
            }
dludwig@9947
   390
dludwig@8522
   391
            break;
dludwig@8522
   392
        }
dludwig@8522
   393
    }
dludwig@8578
   394
dludwig@9924
   395
    dxgiAdapter1->Release();
dludwig@8578
   396
    return 0;
dludwig@8522
   397
}
dludwig@8503
   398
dludwig@8505
   399
int
dludwig@8374
   400
WINRT_InitModes(_THIS)
dludwig@8374
   401
{
dludwig@9924
   402
    /* HACK: Initialize a single display, for whatever screen the app's
dludwig@9924
   403
         CoreApplicationView is on.
dludwig@9924
   404
       TODO, WinRT: Try initializing multiple displays, one for each monitor.
dludwig@9924
   405
         Appropriate WinRT APIs for this seem elusive, though.  -- DavidL
dludwig@9924
   406
    */
dludwig@8522
   407
dludwig@9924
   408
    HRESULT hr;
dludwig@9924
   409
    IDXGIFactory2 * dxgiFactory2 = NULL;
dludwig@9924
   410
dludwig@9924
   411
    hr = CreateDXGIFactory1(IID_IDXGIFactory2, (void **)&dxgiFactory2);
dludwig@9924
   412
    if (FAILED(hr)) {
dludwig@9924
   413
        WIN_SetErrorFromHRESULT(__FUNCTION__ ", CreateDXGIFactory1() failed", hr);
dludwig@8522
   414
        return -1;
dludwig@8374
   415
    }
dludwig@8374
   416
dludwig@9924
   417
    for (int adapterIndex = 0; ; ++adapterIndex) {
dludwig@9924
   418
        if (WINRT_AddDisplaysForAdapter(_this, dxgiFactory2, adapterIndex) < 0) {
dludwig@9924
   419
            break;
dludwig@9924
   420
        }
dludwig@9924
   421
    }
dludwig@9924
   422
dludwig@8327
   423
    return 0;
dludwig@8327
   424
}
dludwig@8327
   425
dludwig@8327
   426
static int
dludwig@8327
   427
WINRT_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode)
dludwig@8327
   428
{
dludwig@8327
   429
    return 0;
dludwig@8327
   430
}
dludwig@8327
   431
dludwig@8327
   432
void
dludwig@8327
   433
WINRT_VideoQuit(_THIS)
dludwig@8327
   434
{
dludwig@10293
   435
    SDL_VideoData * driverdata = (SDL_VideoData *) _this->driverdata;
dludwig@10293
   436
    if (driverdata && driverdata->displayRequest) {
dludwig@10293
   437
        driverdata->displayRequest->Release();
dludwig@10293
   438
        driverdata->displayRequest = NULL;
dludwig@10293
   439
    }
dludwig@10222
   440
    WINRT_QuitGameBar(_this);
dludwig@8374
   441
    WINRT_QuitMouse(_this);
dludwig@8327
   442
}
dludwig@8327
   443
dludwig@9926
   444
static const Uint32 WINRT_DetectableFlags =
dludwig@9926
   445
    SDL_WINDOW_MAXIMIZED |
dludwig@9926
   446
    SDL_WINDOW_FULLSCREEN_DESKTOP |
dludwig@9926
   447
    SDL_WINDOW_SHOWN |
dludwig@9926
   448
    SDL_WINDOW_HIDDEN |
dludwig@9926
   449
    SDL_WINDOW_MOUSE_FOCUS;
dludwig@9926
   450
dludwig@9924
   451
extern "C" Uint32
dludwig@9924
   452
WINRT_DetectWindowFlags(SDL_Window * window)
dludwig@9924
   453
{
dludwig@9924
   454
    Uint32 latestFlags = 0;
dludwig@9924
   455
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@9924
   456
    bool is_fullscreen = false;
dludwig@9924
   457
dludwig@9924
   458
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9924
   459
    if (data->appView) {
dludwig@9924
   460
        is_fullscreen = data->appView->IsFullScreen;
dludwig@9924
   461
    }
dludwig@9927
   462
#elif (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION == NTDDI_WIN8)
dludwig@9924
   463
    is_fullscreen = true;
dludwig@9924
   464
#endif
dludwig@9924
   465
dludwig@9924
   466
    if (data->coreWindow.Get()) {
dludwig@9924
   467
        if (is_fullscreen) {
dludwig@9924
   468
            SDL_VideoDisplay * display = SDL_GetDisplayForWindow(window);
dludwig@9925
   469
            int w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9925
   470
            int h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@9925
   471
dludwig@9925
   472
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
dludwig@9925
   473
            // On all WinRT platforms, except for WinPhone 8.0, rotate the
dludwig@9925
   474
            // window size.  This is needed to properly calculate
dludwig@9925
   475
            // fullscreen vs. maximized.
dludwig@9925
   476
            const DisplayOrientations currentOrientation = WINRT_DISPLAY_PROPERTY(CurrentOrientation);
dludwig@9925
   477
            switch (currentOrientation) {
dludwig@9925
   478
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
dludwig@9925
   479
                case DisplayOrientations::Landscape:
dludwig@9925
   480
                case DisplayOrientations::LandscapeFlipped:
dludwig@9925
   481
#else
dludwig@9925
   482
                case DisplayOrientations::Portrait:
dludwig@9925
   483
                case DisplayOrientations::PortraitFlipped:
dludwig@9925
   484
#endif
dludwig@9925
   485
                {
dludwig@9925
   486
                    int tmp = w;
dludwig@9925
   487
                    w = h;
dludwig@9925
   488
                    h = tmp;
dludwig@9925
   489
                } break;
dludwig@9925
   490
            }
dludwig@9925
   491
#endif
dludwig@9925
   492
dludwig@9925
   493
            if (display->desktop_mode.w != w || display->desktop_mode.h != h) {
dludwig@9924
   494
                latestFlags |= SDL_WINDOW_MAXIMIZED;
dludwig@9924
   495
            } else {
dludwig@9924
   496
                latestFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
dludwig@9924
   497
            }
dludwig@9924
   498
        }
dludwig@9924
   499
dludwig@9924
   500
        if (data->coreWindow->Visible) {
dludwig@9924
   501
            latestFlags |= SDL_WINDOW_SHOWN;
dludwig@9924
   502
        } else {
dludwig@9924
   503
            latestFlags |= SDL_WINDOW_HIDDEN;
dludwig@9924
   504
        }
dludwig@9924
   505
dludwig@9924
   506
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP) && (NTDDI_VERSION < NTDDI_WINBLUE)
dludwig@9924
   507
        // data->coreWindow->PointerPosition is not supported on WinPhone 8.0
dludwig@9924
   508
        latestFlags |= SDL_WINDOW_MOUSE_FOCUS;
dludwig@9924
   509
#else
dludwig@10293
   510
        if (data->coreWindow->Visible && data->coreWindow->Bounds.Contains(data->coreWindow->PointerPosition)) {
dludwig@9924
   511
            latestFlags |= SDL_WINDOW_MOUSE_FOCUS;
dludwig@9924
   512
        }
dludwig@9924
   513
#endif
dludwig@9924
   514
    }
dludwig@9924
   515
dludwig@9924
   516
    return latestFlags;
dludwig@9924
   517
}
dludwig@9924
   518
dludwig@9993
   519
// TODO, WinRT: consider removing WINRT_UpdateWindowFlags, and just calling WINRT_DetectWindowFlags as-appropriate (with appropriate calls to SDL_SendWindowEvent)
dludwig@9924
   520
void
dludwig@9924
   521
WINRT_UpdateWindowFlags(SDL_Window * window, Uint32 mask)
dludwig@9924
   522
{
dludwig@9926
   523
    mask &= WINRT_DetectableFlags;
dludwig@9924
   524
    if (window) {
dludwig@9924
   525
        Uint32 apply = WINRT_DetectWindowFlags(window);
dludwig@9924
   526
        if ((apply & mask) & SDL_WINDOW_FULLSCREEN) {
dludwig@9924
   527
            window->last_fullscreen_flags = window->flags;  // seems necessary to programmatically un-fullscreen, via SDL APIs
dludwig@9924
   528
        }
dludwig@9924
   529
        window->flags = (window->flags & ~mask) | (apply & mask);
dludwig@9924
   530
    }
dludwig@9924
   531
}
dludwig@9924
   532
dludwig@9964
   533
static bool
dludwig@9964
   534
WINRT_IsCoreWindowActive(CoreWindow ^ coreWindow)
dludwig@9964
   535
{
dludwig@9964
   536
    /* WinRT does not appear to offer API(s) to determine window-activation state,
dludwig@9964
   537
       at least not that I am aware of in Win8 - Win10.  As such, SDL tracks this
dludwig@9964
   538
       itself, via window-activation events.
dludwig@9964
   539
       
dludwig@9964
   540
       If there *is* an API to track this, it should probably get used instead
dludwig@9964
   541
       of the following hack (that uses "SDLHelperWindowActivationState").
dludwig@9964
   542
         -- DavidL.
dludwig@9964
   543
    */
dludwig@9964
   544
    if (coreWindow->CustomProperties->HasKey("SDLHelperWindowActivationState")) {
dludwig@9964
   545
        CoreWindowActivationState activationState = \
dludwig@9964
   546
            safe_cast<CoreWindowActivationState>(coreWindow->CustomProperties->Lookup("SDLHelperWindowActivationState"));
dludwig@9964
   547
        return (activationState != CoreWindowActivationState::Deactivated);
dludwig@9964
   548
    }
dludwig@9964
   549
dludwig@9964
   550
    /* Assume that non-SDL tracked windows are active, although this should
dludwig@9964
   551
       probably be avoided, if possible.
dludwig@9964
   552
       
dludwig@9964
   553
       This might not even be possible, in normal SDL use, at least as of
dludwig@9964
   554
       this writing (Dec 22, 2015; via latest hg.libsdl.org/SDL clone)  -- DavidL
dludwig@9964
   555
    */
dludwig@9964
   556
    return true;
dludwig@9964
   557
}
dludwig@9964
   558
dludwig@8522
   559
int
dludwig@8522
   560
WINRT_CreateWindow(_THIS, SDL_Window * window)
dludwig@8522
   561
{
dludwig@8522
   562
    // Make sure that only one window gets created, at least until multimonitor
dludwig@8522
   563
    // support is added.
dludwig@8522
   564
    if (WINRT_GlobalSDLWindow != NULL) {
dludwig@8522
   565
        SDL_SetError("WinRT only supports one window");
dludwig@8522
   566
        return -1;
dludwig@8522
   567
    }
dludwig@8522
   568
dludwig@9924
   569
    SDL_WindowData *data = new SDL_WindowData;  /* use 'new' here as SDL_WindowData may use WinRT/C++ types */
dludwig@8522
   570
    if (!data) {
dludwig@8522
   571
        SDL_OutOfMemory();
dludwig@8522
   572
        return -1;
dludwig@8522
   573
    }
dludwig@8522
   574
    window->driverdata = data;
dludwig@8522
   575
    data->sdlWindow = window;
dludwig@8522
   576
dludwig@8522
   577
    /* To note, when XAML support is enabled, access to the CoreWindow will not
dludwig@8522
   578
       be possible, at least not via the SDL/XAML thread.  Attempts to access it
dludwig@8522
   579
       from there will throw exceptions.  As such, the SDL_WindowData's
dludwig@8522
   580
       'coreWindow' field will only be set (to a non-null value) if XAML isn't
dludwig@8522
   581
       enabled.
dludwig@8522
   582
    */
dludwig@8522
   583
    if (!WINRT_XAMLWasEnabled) {
dludwig@8522
   584
        data->coreWindow = CoreWindow::GetForCurrentThread();
dludwig@9924
   585
#if SDL_WINRT_USE_APPLICATIONVIEW
dludwig@9924
   586
        data->appView = ApplicationView::GetForCurrentView();
dludwig@9924
   587
#endif
dludwig@8522
   588
    }
dludwig@8522
   589
dludwig@9931
   590
    /* Make note of the requested window flags, before they start getting changed. */
dludwig@9931
   591
    const Uint32 requestedFlags = window->flags;
dludwig@9931
   592
dludwig@8541
   593
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   594
    /* Setup the EGL surface, but only if OpenGL ES 2 was requested. */
dludwig@8541
   595
    if (!(window->flags & SDL_WINDOW_OPENGL)) {
dludwig@8541
   596
        /* OpenGL ES 2 wasn't requested.  Don't set up an EGL surface. */
dludwig@8541
   597
        data->egl_surface = EGL_NO_SURFACE;
dludwig@8541
   598
    } else {
dludwig@8541
   599
        /* OpenGL ES 2 was reuqested.  Set up an EGL surface. */
dludwig@8663
   600
        SDL_VideoData * video_data = (SDL_VideoData *)_this->driverdata;
dludwig@8572
   601
dludwig@8663
   602
        /* Call SDL_EGL_ChooseConfig and eglCreateWindowSurface directly,
dludwig@9212
   603
         * rather than via SDL_EGL_CreateSurface, as older versions of
dludwig@9212
   604
         * ANGLE/WinRT may require that a C++ object, ComPtr<IUnknown>,
dludwig@9212
   605
         * be passed into eglCreateWindowSurface.
dludwig@8572
   606
         */
dludwig@8663
   607
        if (SDL_EGL_ChooseConfig(_this) != 0) {
dludwig@8663
   608
            char buf[512];
dludwig@8663
   609
            SDL_snprintf(buf, sizeof(buf), "SDL_EGL_ChooseConfig failed: %s", SDL_GetError());
philipp@9557
   610
            return SDL_SetError("%s", buf);
dludwig@8663
   611
        }
dludwig@8572
   612
dludwig@9211
   613
        if (video_data->winrtEglWindow) {   /* ... is the 'old' version of ANGLE/WinRT being used? */
dludwig@9211
   614
            /* Attempt to create a window surface using older versions of
dludwig@9211
   615
             * ANGLE/WinRT:
dludwig@9211
   616
             */
dludwig@9211
   617
            Microsoft::WRL::ComPtr<IUnknown> cpp_winrtEglWindow = video_data->winrtEglWindow;
dludwig@9211
   618
            data->egl_surface = ((eglCreateWindowSurface_Old_Function)_this->egl_data->eglCreateWindowSurface)(
dludwig@9211
   619
                _this->egl_data->egl_display,
dludwig@9211
   620
                _this->egl_data->egl_config,
dludwig@9211
   621
                cpp_winrtEglWindow, NULL);
dludwig@9211
   622
            if (data->egl_surface == NULL) {
dludwig@10740
   623
                return SDL_EGL_SetError("unable to create EGL native-window surface", "eglCreateWindowSurface");
dludwig@9211
   624
            }
dludwig@9211
   625
        } else if (data->coreWindow.Get() != nullptr) {
dludwig@9211
   626
            /* Attempt to create a window surface using newer versions of
dludwig@9211
   627
             * ANGLE/WinRT:
dludwig@9211
   628
             */
dludwig@9211
   629
            IInspectable * coreWindowAsIInspectable = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@9211
   630
            data->egl_surface = _this->egl_data->eglCreateWindowSurface(
dludwig@9211
   631
                _this->egl_data->egl_display,
dludwig@9211
   632
                _this->egl_data->egl_config,
dludwig@9211
   633
                coreWindowAsIInspectable,
dludwig@9211
   634
                NULL);
dludwig@9211
   635
            if (data->egl_surface == NULL) {
dludwig@10740
   636
                return SDL_EGL_SetError("unable to create EGL native-window surface", "eglCreateWindowSurface");
dludwig@9211
   637
            }
dludwig@9211
   638
        } else {
dludwig@9211
   639
            return SDL_SetError("No supported means to create an EGL window surface are available");
dludwig@8541
   640
        }
dludwig@8541
   641
    }
dludwig@8541
   642
#endif
dludwig@8541
   643
dludwig@9924
   644
    /* Determine as many flags dynamically, as possible. */
dludwig@8522
   645
    window->flags =
dludwig@9928
   646
        SDL_WINDOW_BORDERLESS |
dludwig@9928
   647
        SDL_WINDOW_RESIZABLE;
dludwig@8522
   648
dludwig@8541
   649
#if SDL_VIDEO_OPENGL_EGL
dludwig@8541
   650
    if (data->egl_surface) {
dludwig@8541
   651
        window->flags |= SDL_WINDOW_OPENGL;
dludwig@8541
   652
    }
dludwig@8541
   653
#endif
dludwig@8541
   654
dludwig@9924
   655
    if (WINRT_XAMLWasEnabled) {
dludwig@9924
   656
        /* TODO, WinRT: set SDL_Window size, maybe position too, from XAML control */
dludwig@9924
   657
        window->x = 0;
dludwig@9924
   658
        window->y = 0;
dludwig@9924
   659
        window->flags |= SDL_WINDOW_SHOWN;
dludwig@9924
   660
        SDL_SetMouseFocus(NULL);        // TODO: detect this
dludwig@9924
   661
        SDL_SetKeyboardFocus(NULL);     // TODO: detect this
dludwig@9924
   662
    } else {
dludwig@9931
   663
        /* WinRT 8.x apps seem to live in an environment where the OS controls the
dludwig@9924
   664
           app's window size, with some apps being fullscreen, depending on
dludwig@9924
   665
           user choice of various things.  For now, just adapt the SDL_Window to
dludwig@9924
   666
           whatever Windows set-up as the native-window's geometry.
dludwig@9924
   667
        */
dludwig@9924
   668
        window->x = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Left);
dludwig@9924
   669
        window->y = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Top);
dludwig@9931
   670
#if NTDDI_VERSION < NTDDI_WIN10
dludwig@9931
   671
        /* On WinRT 8.x / pre-Win10, just use the size we were given. */
dludwig@9924
   672
        window->w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9924
   673
        window->h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@9931
   674
#else
dludwig@9931
   675
        /* On Windows 10, we occasionally get control over window size.  For windowed
dludwig@9931
   676
           mode apps, try this.
dludwig@9931
   677
        */
dludwig@9931
   678
        bool didSetSize = false;
dludwig@9931
   679
        if (!(requestedFlags & SDL_WINDOW_FULLSCREEN)) {
dludwig@9931
   680
            const Windows::Foundation::Size size(WINRT_PHYSICAL_PIXELS_TO_DIPS(window->w),
dludwig@9931
   681
                                                 WINRT_PHYSICAL_PIXELS_TO_DIPS(window->h));
dludwig@9931
   682
            didSetSize = data->appView->TryResizeView(size);
dludwig@9931
   683
        }
dludwig@9931
   684
        if (!didSetSize) {
dludwig@9931
   685
            /* We either weren't able to set the window size, or a request for
dludwig@9931
   686
               fullscreen was made.  Get window-size info from the OS.
dludwig@9931
   687
            */
dludwig@9931
   688
            window->w = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Width);
dludwig@9931
   689
            window->h = WINRT_DIPS_TO_PHYSICAL_PIXELS(data->coreWindow->Bounds.Height);
dludwig@9931
   690
        }
dludwig@9931
   691
#endif
dludwig@8522
   692
dludwig@9924
   693
        WINRT_UpdateWindowFlags(
dludwig@9924
   694
            window,
dludwig@9924
   695
            0xffffffff      /* Update any window flag(s) that WINRT_UpdateWindow can handle */
dludwig@9924
   696
        );
dludwig@8551
   697
dludwig@9924
   698
        /* Try detecting if the window is active */
dludwig@9964
   699
        bool isWindowActive = WINRT_IsCoreWindowActive(data->coreWindow.Get());
dludwig@9924
   700
        if (isWindowActive) {
dludwig@9924
   701
            SDL_SetKeyboardFocus(window);
dludwig@9924
   702
        }
dludwig@9924
   703
    }
dludwig@8522
   704
 
dludwig@8522
   705
    /* Make sure the WinRT app's IFramworkView can post events on
dludwig@8522
   706
       behalf of SDL:
dludwig@8522
   707
    */
dludwig@8522
   708
    WINRT_GlobalSDLWindow = window;
dludwig@8522
   709
dludwig@8522
   710
    /* All done! */
dludwig@8522
   711
    return 0;
dludwig@8522
   712
}
dludwig@8522
   713
dludwig@8333
   714
void
dludwig@9931
   715
WINRT_SetWindowSize(_THIS, SDL_Window * window)
dludwig@9931
   716
{
dludwig@9931
   717
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@9931
   718
    SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
dludwig@9931
   719
    const Windows::Foundation::Size size(WINRT_PHYSICAL_PIXELS_TO_DIPS(window->w),
dludwig@9931
   720
                                         WINRT_PHYSICAL_PIXELS_TO_DIPS(window->h));
dludwig@9931
   721
    data->appView->TryResizeView(size); // TODO, WinRT: return failure (to caller?) from TryResizeView()
dludwig@9931
   722
#endif
dludwig@9931
   723
}
dludwig@9931
   724
dludwig@9931
   725
void
dludwig@9931
   726
WINRT_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
dludwig@9931
   727
{
dludwig@9931
   728
#if NTDDI_VERSION >= NTDDI_WIN10
dludwig@9931
   729
    SDL_WindowData * data = (SDL_WindowData *)window->driverdata;
dludwig@9964
   730
    bool isWindowActive = WINRT_IsCoreWindowActive(data->coreWindow.Get());
dludwig@9964
   731
    if (isWindowActive) {
dludwig@9964
   732
        if (fullscreen) {
dludwig@9964
   733
            if (!data->appView->IsFullScreenMode) {
dludwig@9964
   734
                data->appView->TryEnterFullScreenMode();    // TODO, WinRT: return failure (to caller?) from TryEnterFullScreenMode()
dludwig@9964
   735
            }
dludwig@9964
   736
        } else {
dludwig@9964
   737
            if (data->appView->IsFullScreenMode) {
dludwig@9964
   738
                data->appView->ExitFullScreenMode();
dludwig@9964
   739
            }
dludwig@9931
   740
        }
dludwig@9931
   741
    }
dludwig@9931
   742
#endif
dludwig@9931
   743
}
dludwig@9931
   744
dludwig@9931
   745
dludwig@9931
   746
void
dludwig@8333
   747
WINRT_DestroyWindow(_THIS, SDL_Window * window)
dludwig@8333
   748
{
dludwig@8411
   749
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8411
   750
dludwig@8497
   751
    if (WINRT_GlobalSDLWindow == window) {
dludwig@8497
   752
        WINRT_GlobalSDLWindow = NULL;
dludwig@8424
   753
    }
dludwig@8424
   754
dludwig@8411
   755
    if (data) {
dludwig@8411
   756
        // Delete the internal window data:
dludwig@8411
   757
        delete data;
dludwig@8411
   758
        data = NULL;
dludwig@9214
   759
        window->driverdata = NULL;
dludwig@8411
   760
    }
dludwig@8333
   761
}
dludwig@8333
   762
dludwig@8522
   763
SDL_bool
dludwig@8522
   764
WINRT_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
dludwig@8522
   765
{
dludwig@8522
   766
    SDL_WindowData * data = (SDL_WindowData *) window->driverdata;
dludwig@8522
   767
dludwig@8522
   768
    if (info->version.major <= SDL_MAJOR_VERSION) {
dludwig@8522
   769
        info->subsystem = SDL_SYSWM_WINRT;
dludwig@8527
   770
        info->info.winrt.window = reinterpret_cast<IInspectable *>(data->coreWindow.Get());
dludwig@8522
   771
        return SDL_TRUE;
dludwig@8522
   772
    } else {
philipp@10945
   773
        SDL_SetError("Application not compiled with SDL %d.%d",
dludwig@8522
   774
                     SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
dludwig@8522
   775
        return SDL_FALSE;
dludwig@8522
   776
    }
dludwig@8522
   777
    return SDL_FALSE;
dludwig@8411
   778
}
dludwig@8333
   779
dludwig@10293
   780
static ABI::Windows::System::Display::IDisplayRequest *
dludwig@10293
   781
WINRT_CreateDisplayRequest(_THIS)
dludwig@10293
   782
{
dludwig@10293
   783
    /* Setup a WinRT DisplayRequest object, usable for enabling/disabling screensaver requests */
dludwig@10293
   784
    wchar_t *wClassName = L"Windows.System.Display.DisplayRequest";
dludwig@10293
   785
    HSTRING hClassName;
dludwig@10293
   786
    IActivationFactory *pActivationFactory = NULL;
dludwig@10293
   787
    IInspectable * pDisplayRequestRaw = nullptr;
dludwig@10293
   788
    ABI::Windows::System::Display::IDisplayRequest * pDisplayRequest = nullptr;
dludwig@10293
   789
    HRESULT hr;
dludwig@10293
   790
dludwig@10293
   791
    hr = ::WindowsCreateString(wClassName, (UINT32)wcslen(wClassName), &hClassName);
dludwig@10293
   792
    if (FAILED(hr)) {
dludwig@10293
   793
        goto done;
dludwig@10293
   794
    }
dludwig@10293
   795
dludwig@10293
   796
    hr = Windows::Foundation::GetActivationFactory(hClassName, &pActivationFactory);
dludwig@10293
   797
    if (FAILED(hr)) {
dludwig@10293
   798
        goto done;
dludwig@10293
   799
    }
dludwig@10293
   800
dludwig@10293
   801
    hr = pActivationFactory->ActivateInstance(&pDisplayRequestRaw);
dludwig@10293
   802
    if (FAILED(hr)) {
dludwig@10293
   803
        goto done;
dludwig@10293
   804
    }
dludwig@10293
   805
dludwig@10293
   806
    hr = pDisplayRequestRaw->QueryInterface(IID_IDisplayRequest, (void **) &pDisplayRequest);
dludwig@10293
   807
    if (FAILED(hr)) {
dludwig@10293
   808
        goto done;
dludwig@10293
   809
    }
dludwig@10293
   810
dludwig@10293
   811
done:
dludwig@10293
   812
    if (pDisplayRequestRaw) {
dludwig@10293
   813
        pDisplayRequestRaw->Release();
dludwig@10293
   814
    }
dludwig@10293
   815
    if (pActivationFactory) {
dludwig@10293
   816
        pActivationFactory->Release();
dludwig@10293
   817
    }
dludwig@10293
   818
    if (hClassName) {
dludwig@10293
   819
        ::WindowsDeleteString(hClassName);
dludwig@10293
   820
    }
dludwig@10293
   821
dludwig@10293
   822
    return pDisplayRequest;
dludwig@10293
   823
}
dludwig@10293
   824
dludwig@10293
   825
void
dludwig@10293
   826
WINRT_SuspendScreenSaver(_THIS)
dludwig@10293
   827
{
dludwig@10293
   828
    SDL_VideoData *driverdata = (SDL_VideoData *)_this->driverdata;
dludwig@10300
   829
    if (driverdata && driverdata->displayRequest) {
dludwig@10293
   830
        ABI::Windows::System::Display::IDisplayRequest * displayRequest = (ABI::Windows::System::Display::IDisplayRequest *) driverdata->displayRequest;
dludwig@10293
   831
        if (_this->suspend_screensaver) {
dludwig@10293
   832
            displayRequest->RequestActive();
dludwig@10293
   833
        } else {
dludwig@10293
   834
            displayRequest->RequestRelease();
dludwig@10293
   835
        }
dludwig@10293
   836
    }
dludwig@10293
   837
}
dludwig@10293
   838
dludwig@8327
   839
#endif /* SDL_VIDEO_DRIVER_WINRT */
dludwig@8327
   840
dludwig@8327
   841
/* vi: set ts=4 sw=4 expandtab: */