src/audio/xaudio2/SDL_xaudio2_winrthelpers.cpp
author David Ludwig
Fri, 06 Sep 2013 19:07:15 -0400
changeset 8519 0a334fc866b0
parent 8397 8cd319461894
child 8582 c3e9a2b93517
permissions -rw-r--r--
WinRT: made SDL_xaudio2.c compile as C code when building for WinRT

XAudio2 2.8's header file, xaudio2.h, doesn't compile in plain C code for WinRT
apps, not automatically at least. Initially, this file was adapted to compile
as C++, however these changes are now deprecated in favor of some preprocessor
based hacks that should get xaudio2.h to compile (while making sure XAudio2
still works).
dludwig@8353
     1
dludwig@8353
     2
#include <xaudio2.h>
dludwig@8353
     3
#include "SDL_xaudio2_winrthelpers.h"
dludwig@8353
     4
dludwig@8397
     5
#if WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
dludwig@8353
     6
using Windows::Devices::Enumeration::DeviceClass;
dludwig@8353
     7
using Windows::Devices::Enumeration::DeviceInformation;
dludwig@8353
     8
using Windows::Devices::Enumeration::DeviceInformationCollection;
dludwig@8397
     9
#endif
dludwig@8353
    10
dludwig@8519
    11
extern "C" HRESULT __cdecl IXAudio2_GetDeviceCount(IXAudio2 * ixa2, UINT32 * devcount)
dludwig@8353
    12
{
dludwig@8397
    13
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8397
    14
    // There doesn't seem to be any audio device enumeration on Windows Phone.
dludwig@8397
    15
    // In lieu of this, just treat things as if there is one and only one
dludwig@8397
    16
    // audio device.
dludwig@8397
    17
    *devcount = 1;
dludwig@8397
    18
    return S_OK;
dludwig@8397
    19
#else
dludwig@8361
    20
    // TODO, WinRT: make xaudio2 device enumeration only happen once, and in the background
dludwig@8353
    21
    auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
dludwig@8353
    22
    while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
dludwig@8353
    23
    {
dludwig@8353
    24
    }
dludwig@8353
    25
 
dludwig@8353
    26
    DeviceInformationCollection^ devices = operation->GetResults();
dludwig@8353
    27
    *devcount = devices->Size;
dludwig@8353
    28
    return S_OK;
dludwig@8397
    29
#endif
dludwig@8353
    30
}
dludwig@8353
    31
dludwig@8519
    32
extern "C" HRESULT IXAudio2_GetDeviceDetails(IXAudio2 * unused, UINT32 index, XAUDIO2_DEVICE_DETAILS * details)
dludwig@8353
    33
{
dludwig@8397
    34
#if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
dludwig@8397
    35
    // Windows Phone doesn't seem to have the same device enumeration APIs that
dludwig@8397
    36
    // Windows 8/RT has, or it doesn't have them at all.  In lieu of this,
dludwig@8397
    37
    // just treat things as if there is one, and only one, default device.
dludwig@8397
    38
    if (index != 0)
dludwig@8397
    39
    {
dludwig@8397
    40
        return XAUDIO2_E_INVALID_CALL;
dludwig@8397
    41
    }
dludwig@8397
    42
dludwig@8397
    43
    if (details)
dludwig@8397
    44
    {
dludwig@8397
    45
        wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), L"default", _TRUNCATE);
dludwig@8397
    46
        wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), L"default", _TRUNCATE);
dludwig@8397
    47
    }
dludwig@8397
    48
    return S_OK;
dludwig@8397
    49
#else
dludwig@8353
    50
    auto operation = DeviceInformation::FindAllAsync(DeviceClass::AudioRender);
dludwig@8353
    51
    while (operation->Status != Windows::Foundation::AsyncStatus::Completed)
dludwig@8353
    52
    {
dludwig@8353
    53
    }
dludwig@8353
    54
 
dludwig@8353
    55
    DeviceInformationCollection^ devices = operation->GetResults();
dludwig@8353
    56
    if (index >= devices->Size)
dludwig@8353
    57
    {
dludwig@8353
    58
        return XAUDIO2_E_INVALID_CALL;
dludwig@8353
    59
    }
dludwig@8353
    60
dludwig@8353
    61
    DeviceInformation^ d = devices->GetAt(index);
dludwig@8353
    62
    if (details)
dludwig@8353
    63
    {
dludwig@8353
    64
        wcsncpy_s(details->DeviceID, ARRAYSIZE(details->DeviceID), d->Id->Data(), _TRUNCATE);
dludwig@8353
    65
        wcsncpy_s(details->DisplayName, ARRAYSIZE(details->DisplayName), d->Name->Data(), _TRUNCATE);
dludwig@8353
    66
    }
dludwig@8353
    67
    return S_OK;
dludwig@8397
    68
#endif
dludwig@8353
    69
}