src/core/windows/SDL_windows.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 26 May 2015 21:19:23 -0400
changeset 9649 d7762e30ba24
parent 9619 b94b6d0bff0f
child 9709 cc44b8a34b2d
permissions -rw-r--r--
Stack hint should look for 0, not -1, and not care about environment variables.
slouken@5090
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 Sam Lantinga <slouken@libsdl.org>
slouken@5090
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@5090
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@5090
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@5090
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@5090
    22
dludwig@8382
    23
#if defined(__WIN32__) || defined(__WINRT__)
slouken@6044
    24
slouken@7828
    25
#include "SDL_windows.h"
slouken@5090
    26
#include "SDL_error.h"
icculus@6990
    27
#include "SDL_assert.h"
slouken@5090
    28
dludwig@8382
    29
#include <objbase.h>  /* for CoInitialize/CoUninitialize (Win32 only) */
icculus@5591
    30
slouken@8737
    31
#ifndef _WIN32_WINNT_VISTA
slouken@8737
    32
#define _WIN32_WINNT_VISTA  0x0600
slouken@8737
    33
#endif
slouken@8737
    34
slouken@8737
    35
slouken@5090
    36
/* Sets an error message based on GetLastError() */
icculus@7037
    37
int
dludwig@8401
    38
WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr)
slouken@5090
    39
{
slouken@5090
    40
    TCHAR buffer[1024];
slouken@5090
    41
    char *message;
dludwig@8401
    42
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0,
slouken@5090
    43
                  buffer, SDL_arraysize(buffer), NULL);
slouken@5090
    44
    message = WIN_StringToUTF8(buffer);
slouken@5090
    45
    SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
slouken@5090
    46
    SDL_free(message);
icculus@7037
    47
    return -1;
slouken@5090
    48
}
slouken@5090
    49
dludwig@8401
    50
/* Sets an error message based on GetLastError() */
dludwig@8460
    51
int
dludwig@8401
    52
WIN_SetError(const char *prefix)
dludwig@8401
    53
{
dludwig@8460
    54
    return WIN_SetErrorFromHRESULT(prefix, GetLastError());
dludwig@8401
    55
}
dludwig@8401
    56
icculus@5591
    57
HRESULT
icculus@5591
    58
WIN_CoInitialize(void)
icculus@5591
    59
{
slouken@8085
    60
    /* SDL handles any threading model, so initialize with the default, which
slouken@8085
    61
       is compatible with OLE and if that doesn't work, try multi-threaded mode.
slouken@8085
    62
slouken@8085
    63
       If you need multi-threaded mode, call CoInitializeEx() before SDL_Init()
slouken@8085
    64
    */
dludwig@8382
    65
#ifdef __WINRT__
dludwig@8382
    66
    /* DLudwig: On WinRT, it is assumed that COM was initialized in main().
dludwig@8382
    67
       CoInitializeEx is available (not CoInitialize though), however
dludwig@8382
    68
       on WinRT, main() is typically declared with the [MTAThread]
dludwig@8382
    69
       attribute, which, AFAIK, should initialize COM.
dludwig@8382
    70
    */
dludwig@8382
    71
    return S_OK;
dludwig@8382
    72
#else
slouken@8085
    73
    HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
slouken@8085
    74
    if (hr == RPC_E_CHANGED_MODE) {
slouken@8085
    75
        hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
slouken@8085
    76
    }
icculus@6031
    77
icculus@5591
    78
    /* S_FALSE means success, but someone else already initialized. */
icculus@5591
    79
    /* You still need to call CoUninitialize in this case! */
icculus@6031
    80
    if (hr == S_FALSE) {
icculus@5591
    81
        return S_OK;
icculus@5591
    82
    }
icculus@5591
    83
icculus@5591
    84
    return hr;
dludwig@8382
    85
#endif
icculus@5591
    86
}
icculus@5591
    87
icculus@5591
    88
void
icculus@5591
    89
WIN_CoUninitialize(void)
icculus@5591
    90
{
dludwig@8382
    91
#ifndef __WINRT__
icculus@5591
    92
    CoUninitialize();
dludwig@8382
    93
#endif
icculus@5591
    94
}
icculus@5591
    95
dludwig@8746
    96
#ifndef __WINRT__
slouken@8733
    97
static BOOL
slouken@8733
    98
IsWindowsVersionOrGreater(WORD wMajorVersion, WORD wMinorVersion, WORD wServicePackMajor)
slouken@8733
    99
{
slouken@8733
   100
    OSVERSIONINFOEXW osvi;
slouken@8733
   101
    DWORDLONG const dwlConditionMask = VerSetConditionMask(
slouken@8733
   102
        VerSetConditionMask(
slouken@8733
   103
        VerSetConditionMask(
slouken@8733
   104
        0, VER_MAJORVERSION, VER_GREATER_EQUAL ),
slouken@8733
   105
        VER_MINORVERSION, VER_GREATER_EQUAL ),
slouken@8733
   106
        VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL );
slouken@8733
   107
slouken@8733
   108
    SDL_zero(osvi);
slouken@8733
   109
    osvi.dwOSVersionInfoSize = sizeof(osvi);
slouken@8733
   110
    osvi.dwMajorVersion = wMajorVersion;
slouken@8733
   111
    osvi.dwMinorVersion = wMinorVersion;
slouken@8733
   112
    osvi.wServicePackMajor = wServicePackMajor;
slouken@8733
   113
slouken@8733
   114
    return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
slouken@8733
   115
}
dludwig@8746
   116
#endif
slouken@8733
   117
slouken@8733
   118
BOOL WIN_IsWindowsVistaOrGreater()
slouken@8733
   119
{
dludwig@8746
   120
#ifdef __WINRT__
dludwig@8746
   121
    return TRUE;
dludwig@8746
   122
#else
slouken@8733
   123
    return IsWindowsVersionOrGreater(HIBYTE(_WIN32_WINNT_VISTA), LOBYTE(_WIN32_WINNT_VISTA), 0);
dludwig@8746
   124
#endif
slouken@8733
   125
}
slouken@8733
   126
slouken@6044
   127
#endif /* __WIN32__ */
slouken@6044
   128
slouken@5090
   129
/* vi: set ts=4 sw=4 expandtab: */