diff --git a/.gitignore b/.gitignore index d81492bf0..2910d7f1d 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,5 @@ legacy/project/obj .vscode/settings.json package-lock.json node_modules -!templates/**/node_modules \ No newline at end of file +!templates/**/node_modules +.vscode/ diff --git a/lime/_backend/native/NativeCFFI.hx b/lime/_backend/native/NativeCFFI.hx index 75fab4744..460c00e03 100644 --- a/lime/_backend/native/NativeCFFI.hx +++ b/lime/_backend/native/NativeCFFI.hx @@ -136,8 +136,11 @@ class NativeCFFI { @:cffi private static function lime_system_get_directory (type:Int, company:String, title:String):Dynamic; @:cffi private static function lime_system_get_display (index:Int):Dynamic; @:cffi private static function lime_system_get_ios_tablet ():Bool; + @:cffi private static function lime_system_get_manufacturer ():Dynamic; + @:cffi private static function lime_system_get_model ():Dynamic; @:cffi private static function lime_system_get_num_displays ():Int; @:cffi private static function lime_system_get_timer ():Float; + @:cffi private static function lime_system_get_version ():Dynamic; @:cffi private static function lime_system_open_file (path:String):Void; @:cffi private static function lime_system_open_url (url:String, target:String):Void; @:cffi private static function lime_text_event_manager_register (callback:Dynamic, eventObject:Dynamic):Void; diff --git a/lime/system/System.hx b/lime/system/System.hx index db2bedda5..53b52dddb 100644 --- a/lime/system/System.hx +++ b/lime/system/System.hx @@ -25,6 +25,10 @@ import js.html.Element; import js.Browser; #end +#if sys +import sys.io.Process; +#end + #if !lime_debug @:fileXml('tags="haxe,release"') @:noDebug @@ -57,8 +61,11 @@ class System { public static var documentsDirectory (get, null):String; public static var endianness (get, null):Endian; public static var fontsDirectory (get, null):String; + public static var manufacturer (get, null):String; + public static var model (get, null):String; public static var numDisplays (get, null):Int; public static var userDirectory (get, null):String; + public static var version (get, null):String; @:noCompletion private static var __applicationConfig:Map; @:noCompletion private static var __applicationEntryPoint:Map; @@ -514,6 +521,25 @@ class System { } + private static function __runProcess (command:String, args:Array = null):String { + + #if sys + try { + + if (args == null) args = []; + + var process = new Process (command, args); + var value = StringTools.trim (process.stdout.readLine ().toString ()); + process.close (); + return value; + + } catch (e:Dynamic) {} + #end + return null; + + } + + // Get & Set Methods @@ -619,6 +645,72 @@ class System { } + private static function get_manufacturer ():String { + + #if android + var manufacturer:String = JNI.createStaticField ("android/os/Build", "MANUFACTURER", "Ljava/lang/String;").get (); + if (manufacturer != null) { + return manufacturer.charAt (0).toUpperCase () + manufacturer.substr (1); + } + #elseif (ios || mac) + return "Apple"; + #elseif linux + return __runProcess ("cat", [ "/sys/devices/virtual/dmi/id/product_name" ]); + #end + return null; + + } + + + private static function get_model ():String { + + #if android + var manufacturer:String = JNI.createStaticField ("android/os/Build", "MANUFACTURER", "Ljava/lang/String;").get (); + var model:String = JNI.createStaticField ("android/os/Build", "MODEL", "Ljava/lang/String;").get (); + if (manufacturer != null && model != null) { + if (StringTools.startsWith (model.toLowerCase (), manufacturer.toLowerCase ())) { + model = StringTools.trim (model.substr (manufacturer.length)); + while (StringTools.startsWith (model, "-")) { + model = StringTools.trim (model.substr (1)); + } + } + return model; + } + #elseif ios + return NativeCFFI.lime_system_get_model (); + #elseif mac + return __runProcess ("sysctl", [ "-n", "hw.model" ]); + #elseif linux + return __runProcess ("cat", [ "/sys/devices/virtual/dmi/id/sys_vendor" ]); + #end + return null; + + } + + + private static function get_version ():String { + + #if android + var release = JNI.createStaticField ("android/os/Build$VERSION", "RELEASE", "Ljava/lang/String;").get (); + var api = JNI.createStaticField ("android/os/Build$VERSION", "SDK_INT", "I").get (); + if (release != null && api != null) return "Android " + release + " (API " + api + ")"; + #elseif ios + var name = "iOS"; + var version:String = NativeCFFI.lime_system_get_version (); + if (name != null && version != null) return name + " " + version; + #elseif mac + //var name = __runProcess ("sw_vers", [ "-productName" ]); + var name = "macOS"; + var version = __runProcess ("sw_vers", [ "-productVersion" ]); + if (name != null && version != null) return name + " " + version; + #elseif linux + return __runProcess ("lsb_release", [ "-ds" ]); + #end + return null; + + } + + } diff --git a/project/include/system/System.h b/project/include/system/System.h index ff0f9693f..916471761 100644 --- a/project/include/system/System.h +++ b/project/include/system/System.h @@ -32,6 +32,9 @@ namespace lime { static std::wstring* GetIOSDirectory (SystemDirectory type); static bool GetIOSTablet (); #endif + static std::wstring* GetManufacturer (); + static std::wstring* GetModel (); + static std::wstring* GetVersion (); #ifdef HX_WINDOWS static int GetWindowsConsoleMode (int handleType); #endif diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index dd7797688..6c1b1e300 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -1481,6 +1481,44 @@ namespace lime { } + value lime_system_get_manufacturer () { + + std::wstring* manufacturer = System::GetManufacturer (); + + if (manufacturer) { + + value result = alloc_wstring (manufacturer->c_str ()); + delete manufacturer; + return result; + + } else { + + return alloc_null (); + + } + + } + + + value lime_system_get_model () { + + std::wstring* model = System::GetModel (); + + if (model) { + + value result = alloc_wstring (model->c_str ()); + delete model; + return result; + + } else { + + return alloc_null (); + + } + + } + + int lime_system_get_num_displays () { return System::GetNumDisplays (); @@ -1495,6 +1533,25 @@ namespace lime { } + value lime_system_get_version () { + + std::wstring* version = System::GetVersion (); + + if (version) { + + value result = alloc_wstring (version->c_str ()); + delete version; + return result; + + } else { + + return alloc_null (); + + } + + } + + int lime_system_get_windows_console_mode (int handleType) { #ifdef HX_WINDOWS @@ -1976,8 +2033,11 @@ namespace lime { DEFINE_PRIME3 (lime_system_get_directory); DEFINE_PRIME1 (lime_system_get_display); DEFINE_PRIME0 (lime_system_get_ios_tablet); + DEFINE_PRIME0 (lime_system_get_manufacturer); + DEFINE_PRIME0 (lime_system_get_model); DEFINE_PRIME0 (lime_system_get_num_displays); DEFINE_PRIME0 (lime_system_get_timer); + DEFINE_PRIME0 (lime_system_get_version); DEFINE_PRIME1 (lime_system_get_windows_console_mode); DEFINE_PRIME1v (lime_system_open_file); DEFINE_PRIME2v (lime_system_open_url); diff --git a/project/src/system/System.cpp b/project/src/system/System.cpp index ea8e5df5b..7e4a246f7 100644 --- a/project/src/system/System.cpp +++ b/project/src/system/System.cpp @@ -8,6 +8,27 @@ namespace lime { + std::wstring* System::GetManufacturer () { + + return NULL; + + } + + + std::wstring* System::GetModel () { + + return NULL; + + } + + + std::wstring* System::GetVersion () { + + return NULL; + + } + + #ifdef HX_WINDOWS int System::GetWindowsConsoleMode (int handleType) { diff --git a/project/src/system/System.mm b/project/src/system/System.mm index ba3c25bd8..018210fd0 100644 --- a/project/src/system/System.mm +++ b/project/src/system/System.mm @@ -2,6 +2,7 @@ #import #endif +#import #include @@ -61,6 +62,41 @@ namespace lime { } + std::wstring* System::GetManufacturer () { + + return NULL; + + } + + + std::wstring* System::GetModel () { + + #ifdef IPHONE + struct utsname systemInfo; + uname (&systemInfo); + + std::string model = std::string (systemInfo.machine); + return new std::wstring (model.begin (), model.end ()); + #else + return NULL; + #endif + + } + + + std::wstring* System::GetVersion () { + + #ifdef IPHONE + NSString *versionString = [[UIDevice currentDevice] systemVersion]; + std::string result = std::string ([versionString UTF8String]); + return new std::wstring (result.begin (), result.end ()); + #else + return NULL; + #endif + + } + + void System::OpenFile (const char* path) { OpenURL (path, NULL);