src/filesystem/winrt/SDL_sysfilesystem.cpp
author Ryan C. Gordon
Sun, 03 Jan 2016 20:52:44 -0500
changeset 10004 8f2f519d1e61
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
CMake: Don't make a libSDL2.so symlink on Mac OS X (do .dylib instead).
slouken@8616
     1
/*
slouken@8616
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@8616
     4
slouken@8616
     5
  This software is provided 'as-is', without any express or implied
slouken@8616
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8616
     7
  arising from the use of this software.
slouken@8616
     8
slouken@8616
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8616
    10
  including commercial applications, and to alter it and redistribute it
slouken@8616
    11
  freely, subject to the following restrictions:
slouken@8616
    12
slouken@8616
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8616
    14
     claim that you wrote the original software. If you use this software
slouken@8616
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8616
    16
     appreciated but is not required.
slouken@8616
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8616
    18
     misrepresented as being the original software.
slouken@8616
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8616
    20
*/
slouken@8616
    21
#include "../../SDL_internal.h"
slouken@8616
    22
dludwig@9241
    23
/* TODO, WinRT: remove the need to compile this with C++/CX (/ZW) extensions, and if possible, without C++ at all
slouken@8582
    24
*/
slouken@8582
    25
slouken@8582
    26
#ifdef __WINRT__
slouken@8582
    27
slouken@8582
    28
extern "C" {
slouken@8582
    29
#include "SDL_filesystem.h"
slouken@8582
    30
#include "SDL_error.h"
dludwig@9242
    31
#include "SDL_hints.h"
slouken@8582
    32
#include "SDL_stdinc.h"
slouken@8582
    33
#include "SDL_system.h"
slouken@8582
    34
#include "../../core/windows/SDL_windows.h"
slouken@8582
    35
}
slouken@8582
    36
slouken@8582
    37
#include <string>
slouken@8582
    38
#include <unordered_map>
slouken@8582
    39
slouken@8582
    40
using namespace std;
slouken@8582
    41
using namespace Windows::Storage;
slouken@8582
    42
slouken@8582
    43
extern "C" const wchar_t *
slouken@8582
    44
SDL_WinRTGetFSPathUNICODE(SDL_WinRT_Path pathType)
slouken@8582
    45
{
slouken@8582
    46
    switch (pathType) {
slouken@8582
    47
        case SDL_WINRT_PATH_INSTALLED_LOCATION:
slouken@8582
    48
        {
slouken@8582
    49
            static wstring path;
slouken@8582
    50
            if (path.empty()) {
slouken@8582
    51
                path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path->Data();
slouken@8582
    52
            }
slouken@8582
    53
            return path.c_str();
slouken@8582
    54
        }
slouken@8582
    55
slouken@8582
    56
        case SDL_WINRT_PATH_LOCAL_FOLDER:
slouken@8582
    57
        {
slouken@8582
    58
            static wstring path;
slouken@8582
    59
            if (path.empty()) {
slouken@8582
    60
                path = ApplicationData::Current->LocalFolder->Path->Data();
slouken@8582
    61
            }
slouken@8582
    62
            return path.c_str();
slouken@8582
    63
        }
slouken@8582
    64
dludwig@9228
    65
#if (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) || (NTDDI_VERSION > NTDDI_WIN8)
slouken@8582
    66
        case SDL_WINRT_PATH_ROAMING_FOLDER:
slouken@8582
    67
        {
slouken@8582
    68
            static wstring path;
slouken@8582
    69
            if (path.empty()) {
slouken@8582
    70
                path = ApplicationData::Current->RoamingFolder->Path->Data();
slouken@8582
    71
            }
slouken@8582
    72
            return path.c_str();
slouken@8582
    73
        }
slouken@8582
    74
slouken@8582
    75
        case SDL_WINRT_PATH_TEMP_FOLDER:
slouken@8582
    76
        {
slouken@8582
    77
            static wstring path;
slouken@8582
    78
            if (path.empty()) {
slouken@8582
    79
                path = ApplicationData::Current->TemporaryFolder->Path->Data();
slouken@8582
    80
            }
slouken@8582
    81
            return path.c_str();
slouken@8582
    82
        }
slouken@8582
    83
#endif
slouken@8582
    84
slouken@8582
    85
        default:
slouken@8582
    86
            break;
slouken@8582
    87
    }
slouken@8582
    88
slouken@8582
    89
    SDL_Unsupported();
slouken@8582
    90
    return NULL;
slouken@8582
    91
}
slouken@8582
    92
slouken@8582
    93
extern "C" const char *
slouken@8582
    94
SDL_WinRTGetFSPathUTF8(SDL_WinRT_Path pathType)
slouken@8582
    95
{
slouken@8582
    96
    typedef unordered_map<SDL_WinRT_Path, string> UTF8PathMap;
slouken@8582
    97
    static UTF8PathMap utf8Paths;
slouken@8582
    98
slouken@8582
    99
    UTF8PathMap::iterator searchResult = utf8Paths.find(pathType);
slouken@8582
   100
    if (searchResult != utf8Paths.end()) {
slouken@8582
   101
        return searchResult->second.c_str();
slouken@8582
   102
    }
slouken@8582
   103
slouken@8582
   104
    const wchar_t * ucs2Path = SDL_WinRTGetFSPathUNICODE(pathType);
slouken@8582
   105
    if (!ucs2Path) {
slouken@8582
   106
        return NULL;
slouken@8582
   107
    }
slouken@8582
   108
slouken@8582
   109
    char * utf8Path = WIN_StringToUTF8(ucs2Path);
slouken@8582
   110
    utf8Paths[pathType] = utf8Path;
slouken@8582
   111
    SDL_free(utf8Path);
slouken@8582
   112
    return utf8Paths[pathType].c_str();
slouken@8582
   113
}
slouken@8582
   114
slouken@8582
   115
extern "C" char *
slouken@8582
   116
SDL_GetBasePath(void)
slouken@8582
   117
{
slouken@8582
   118
    const char * srcPath = SDL_WinRTGetFSPathUTF8(SDL_WINRT_PATH_INSTALLED_LOCATION);
slouken@8582
   119
    size_t destPathLen;
slouken@8582
   120
    char * destPath = NULL;
slouken@8582
   121
slouken@8582
   122
    if (!srcPath) {
slouken@8582
   123
        SDL_SetError("Couldn't locate our basepath: %s", SDL_GetError());
slouken@8582
   124
        return NULL;
slouken@8582
   125
    }
slouken@8582
   126
slouken@8582
   127
    destPathLen = SDL_strlen(srcPath) + 2;
slouken@8582
   128
    destPath = (char *) SDL_malloc(destPathLen);
slouken@8582
   129
    if (!destPath) {
slouken@8582
   130
        SDL_OutOfMemory();
slouken@8582
   131
        return NULL;
slouken@8582
   132
    }
slouken@8582
   133
slouken@8582
   134
    SDL_snprintf(destPath, destPathLen, "%s\\", srcPath);
slouken@8582
   135
    return destPath;
slouken@8582
   136
}
slouken@8582
   137
slouken@8582
   138
extern "C" char *
slouken@8582
   139
SDL_GetPrefPath(const char *org, const char *app)
slouken@8582
   140
{
slouken@8582
   141
    /* WinRT note: The 'SHGetFolderPath' API that is used in Windows 7 and
slouken@8582
   142
     * earlier is not available on WinRT or Windows Phone.  WinRT provides
slouken@8582
   143
     * a similar API, but SHGetFolderPath can't be called, at least not
slouken@8582
   144
     * without violating Microsoft's app-store requirements.
slouken@8582
   145
     */
slouken@8582
   146
dludwig@9247
   147
    const WCHAR * srcPath = NULL;
dludwig@9247
   148
    WCHAR path[MAX_PATH];
dludwig@9247
   149
    char *retval = NULL;
dludwig@9247
   150
    WCHAR* worg = NULL;
dludwig@9247
   151
    WCHAR* wapp = NULL;
dludwig@9247
   152
    size_t new_wpath_len = 0;
dludwig@9247
   153
    BOOL api_result = FALSE;
slouken@8582
   154
dludwig@9255
   155
    srcPath = SDL_WinRTGetFSPathUNICODE(SDL_WINRT_PATH_LOCAL_FOLDER);
dludwig@9247
   156
    if ( ! srcPath) {
dludwig@9247
   157
        SDL_SetError("Unable to find a source path");
slouken@8582
   158
        return NULL;
slouken@8582
   159
    }
slouken@8582
   160
dludwig@9247
   161
    if (SDL_wcslen(srcPath) >= MAX_PATH) {
dludwig@9247
   162
        SDL_SetError("Path too long.");
dludwig@9247
   163
        return NULL;
dludwig@9247
   164
    }
dludwig@9247
   165
    SDL_wcslcpy(path, srcPath, SDL_arraysize(path));
dludwig@9247
   166
dludwig@9247
   167
    worg = WIN_UTF8ToString(org);
dludwig@9247
   168
    if (worg == NULL) {
slouken@8582
   169
        SDL_OutOfMemory();
slouken@8582
   170
        return NULL;
slouken@8582
   171
    }
slouken@8582
   172
dludwig@9247
   173
    wapp = WIN_UTF8ToString(app);
dludwig@9247
   174
    if (wapp == NULL) {
dludwig@9247
   175
        SDL_free(worg);
dludwig@9247
   176
        SDL_OutOfMemory();
dludwig@9247
   177
        return NULL;
dludwig@9247
   178
    }
dludwig@9247
   179
dludwig@9247
   180
    new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3;
dludwig@9247
   181
dludwig@9247
   182
    if ((new_wpath_len + 1) > MAX_PATH) {
dludwig@9247
   183
        SDL_free(worg);
dludwig@9247
   184
        SDL_free(wapp);
dludwig@9247
   185
        SDL_SetError("Path too long.");
dludwig@9247
   186
        return NULL;
dludwig@9247
   187
    }
dludwig@9247
   188
dludwig@9247
   189
    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
dludwig@9247
   190
    SDL_wcslcat(path, worg, new_wpath_len + 1);
dludwig@9247
   191
    SDL_free(worg);
dludwig@9247
   192
dludwig@9247
   193
    api_result = CreateDirectoryW(path, NULL);
dludwig@9247
   194
    if (api_result == FALSE) {
dludwig@9247
   195
        if (GetLastError() != ERROR_ALREADY_EXISTS) {
dludwig@9247
   196
            SDL_free(wapp);
dludwig@9247
   197
            WIN_SetError("Couldn't create a prefpath.");
dludwig@9247
   198
            return NULL;
dludwig@9247
   199
        }
dludwig@9247
   200
    }
dludwig@9247
   201
dludwig@9247
   202
    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
dludwig@9247
   203
    SDL_wcslcat(path, wapp, new_wpath_len + 1);
dludwig@9247
   204
    SDL_free(wapp);
dludwig@9247
   205
dludwig@9247
   206
    api_result = CreateDirectoryW(path, NULL);
dludwig@9247
   207
    if (api_result == FALSE) {
dludwig@9247
   208
        if (GetLastError() != ERROR_ALREADY_EXISTS) {
dludwig@9247
   209
            WIN_SetError("Couldn't create a prefpath.");
dludwig@9247
   210
            return NULL;
dludwig@9247
   211
        }
dludwig@9247
   212
    }
dludwig@9247
   213
dludwig@9247
   214
    SDL_wcslcat(path, L"\\", new_wpath_len + 1);
dludwig@9247
   215
dludwig@9247
   216
    retval = WIN_StringToUTF8(path);
dludwig@9247
   217
dludwig@9247
   218
    return retval;
slouken@8582
   219
}
slouken@8582
   220
slouken@8582
   221
#endif /* __WINRT__ */