Work on system directories

This commit is contained in:
Joshua Granick
2016-09-02 21:08:21 -07:00
parent 25022dab82
commit cb6d15238d
5 changed files with 127 additions and 48 deletions

View File

@@ -229,6 +229,7 @@
<file name="src/system/Locale.cpp" unless="mac || ios" />
<file name="src/system/Locale.mm" if="mac || ios" />
<file name="src/system/SensorEvent.cpp" />
<file name="src/system/System.mm" if="ios" />
<file name="src/ui/DropEvent.cpp" />
<file name="src/ui/GamepadEvent.cpp" />
<file name="src/ui/JoystickEvent.cpp" />

View File

@@ -3,6 +3,7 @@
#include <hx/CFFI.h>
#include <stdio.h>
#include <string>
namespace lime {
@@ -26,7 +27,10 @@ namespace lime {
public:
static bool GetAllowScreenTimeout ();
static const char* GetDirectory (SystemDirectory type, const char* company, const char* title);
static std::wstring* GetDirectory (SystemDirectory type, const char* company, const char* title);
#ifdef IPHONE
static std::wstring* GetIOSDirectory (SystemDirectory type);
#endif
static value GetDisplay (int id);
static int GetNumDisplays ();
static double GetTimer ();

View File

@@ -1289,25 +1289,13 @@ namespace lime {
value lime_system_get_directory (int type, HxString company, HxString title) {
const char* path = System::GetDirectory ((SystemDirectory)type, company.__s, title.__s);
std::wstring* path = System::GetDirectory ((SystemDirectory)type, company.__s, title.__s);
if (path) {
value _path = alloc_string (path);
if (type != 4) {
// TODO: Make this more consistent
//This free() causes crashes on mac at least. Commenting it out makes it work
//again but may cause a small memory leak. Some more consideration is
//necessary to figure out what to do here
//free ((char*) path);
}
return _path;
value result = alloc_wstring (path->c_str ());
delete path;
return result;
} else {

View File

@@ -88,39 +88,54 @@ namespace lime {
}
const char* System::GetDirectory (SystemDirectory type, const char* company, const char* title) {
std::wstring* System::GetDirectory (SystemDirectory type, const char* company, const char* title) {
switch (type) {
case APPLICATION:
case APPLICATION: {
return SDL_GetBasePath ();
std::string path = std::string (SDL_GetBasePath ());
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
break;
}
case APPLICATION_STORAGE:
case APPLICATION_STORAGE: {
return SDL_GetPrefPath (company, title);
std::string path = std::string (SDL_GetPrefPath (company, title));
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
break;
}
case DESKTOP: {
#if defined (HX_WINRT)
Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::HomeGroup;
std::wstring resultW (folder->Begin ());
std::string result (resultW.begin (), resultW.end ());
return result.c_str ();
std::wstring* result = new std::wstring (folder->Begin ());
return result;
#elif defined (HX_WINDOWS)
char result[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, result);
return WIN_StringToUTF8 (result);
char folderPath[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_DESKTOPDIRECTORY, NULL, SHGFP_TYPE_CURRENT, folderPath);
WIN_StringToUTF8 (folderPath);
std::string path = std::string (folderPath);
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#elif defined (IPHONE)
return System::GetIOSDirectory (type);
#elif !defined (ANDROID)
std::string result = std::string (getenv ("HOME")) + std::string ("/Desktop");
return result.c_str ();
std::string path = std::string (getenv ("HOME")) + std::string ("/Desktop");
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#endif
break;
@@ -132,15 +147,21 @@ namespace lime {
#if defined (HX_WINRT)
Windows::Storage::StorageFolder folder = Windows::Storage::KnownFolders::DocumentsLibrary;
std::wstring resultW (folder->Begin ());
std::string result (resultW.begin (), resultW.end ());
return result.c_str ();
std::wstring* result = std::wstring (folder->Begin ());
return result;
#elif defined (HX_WINDOWS)
char result[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, result);
return WIN_StringToUTF8 (result);
char folderPath[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_MYDOCUMENTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
WIN_StringToUTF8 (folderPath);
std::string path = std::string (folderPath);
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#elif defined (IPHONE)
return System::GetIOSDirectory (type);
#elif defined (ANDROID)
@@ -148,8 +169,9 @@ namespace lime {
#else
std::string result = std::string (getenv ("HOME")) + std::string ("/Documents");
return result.c_str ();
std::string path = std::string (getenv ("HOME")) + std::string ("/Documents");
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#endif
break;
@@ -164,9 +186,12 @@ namespace lime {
#elif defined (HX_WINDOWS)
char result[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, result);
return WIN_StringToUTF8 (result);
char folderPath[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_FONTS, NULL, SHGFP_TYPE_CURRENT, folderPath);
WIN_StringToUTF8 (folderPath);
std::string path = std::string (folderPath);
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#elif defined (HX_MACOS)
@@ -198,15 +223,21 @@ namespace lime {
#if defined (HX_WINRT)
Windows::Storage::StorageFolder folder = Windows::Storage::ApplicationData::Current->RoamingFolder;
std::wstring resultW (folder->Begin ());
std::string result (resultW.begin (), resultW.end ());
return result.c_str ();
std::wstring* result = new std::wstring (folder->Begin ());
return result;
#elif defined (HX_WINDOWS)
char result[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, result);
return WIN_StringToUTF8 (result);
char folderPath[MAX_PATH] = "";
SHGetFolderPath (NULL, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, folderPath);
WIN_StringToUTF8 (folderPath);
std::string path = std::string (folderPath);
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#elif defined (IPHONE)
return System::GetIOSDirectory (type);
#elif defined (ANDROID)
@@ -215,7 +246,8 @@ namespace lime {
#else
std::string result = getenv ("HOME");
return result.c_str ();
std::wstring* result = new std::wstring (path.begin (), path.end ());
return result;
#endif
break;

View File

@@ -0,0 +1,54 @@
#ifdef IPHONE
#import <UIKit/UIKit.h>
#endif
#include <system/System.h>
namespace lime {
std::wstring* System::GetIOSDirectory (SystemDirectory type) {
#ifndef OBJC_ARC
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
#endif
NSUInteger searchType = NSDocumentDirectory;
switch (type) {
case DESKTOP:
searchType = NSDesktopDirectory;
break;
case USER:
searchType = NSUserDirectory;
break;
}
std::wstring* result = 0;
NSArray* paths = NSSearchPathForDirectoriesInDomains (searchType, NSUserDomainMask, YES);
if (paths && [paths count] > 0) {
NSString* basePath = paths.firstObject;
std::string path = std::string ([basePath UTF8String]);
result = new std::wstring (path.begin (), path.end ());
}
#ifndef OBJC_ARC
[pool drain];
#endif
return result;
}
}