From 4a8fe40024611ba6c6a9c8d09f07aa31810d49c7 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Wed, 23 Sep 2015 09:19:45 -0700 Subject: [PATCH] Add lime.system.CFFIPointer --- lime/audio/AudioManager.hx | 4 +- lime/audio/openal/ALC.hx | 26 +++++----- lime/audio/openal/ALContext.hx | 6 ++- lime/audio/openal/ALDevice.hx | 6 ++- lime/system/CFFI.hx | 2 +- lime/system/CFFIPointer.hx | 55 +++++++++++++++++++++ project/Build.xml | 1 + project/include/system/CFFIPointer.h | 25 ++++++++++ project/src/ExternalInterface.cpp | 29 ++++++----- project/src/audio/openal/OpenALBindings.cpp | 45 ++--------------- project/src/system/CFFIPointer.cpp | 30 +++++++++++ 11 files changed, 155 insertions(+), 74 deletions(-) create mode 100644 lime/system/CFFIPointer.hx create mode 100644 project/include/system/CFFIPointer.h create mode 100644 project/src/system/CFFIPointer.cpp diff --git a/lime/audio/AudioManager.hx b/lime/audio/AudioManager.hx index c76283446..5b6d5e4be 100644 --- a/lime/audio/AudioManager.hx +++ b/lime/audio/AudioManager.hx @@ -41,9 +41,9 @@ class AudioManager { AudioManager.context = FLASH (new FlashAudioContext ()); #elseif lime_console - + // TODO - AudioManager.context = CUSTOM (null); + AudioManager.context = CUSTOM (null); #else diff --git a/lime/audio/openal/ALC.hx b/lime/audio/openal/ALC.hx index 4db8bf195..d7852b1de 100644 --- a/lime/audio/openal/ALC.hx +++ b/lime/audio/openal/ALC.hx @@ -1,6 +1,8 @@ package lime.audio.openal; +import lime.system.CFFIPointer; + #if !macro @:build(lime.system.CFFI.build()) #end @@ -199,18 +201,18 @@ class ALC { #if ((cpp || neko || nodejs) && lime_openal && !macro) - @:cffi private static function lime_alc_close_device (device:Dynamic):Bool; - @:cffi private static function lime_alc_create_context (device:Dynamic, attrlist:Dynamic):Dynamic; - @:cffi private static function lime_alc_destroy_context (context:Dynamic):Void; - @:cffi private static function lime_alc_get_contexts_device (context:Dynamic):Dynamic; - @:cffi private static function lime_alc_get_current_context ():Dynamic; - @:cffi private static function lime_alc_get_error (device:Dynamic):Int; - @:cffi private static function lime_alc_get_integerv (device:Dynamic, param:Int, size:Int):Dynamic; - @:cffi private static function lime_alc_get_string (device:Dynamic, param:Int):Dynamic; - @:cffi private static function lime_alc_make_context_current (context:Dynamic):Bool; - @:cffi private static function lime_alc_open_device (devicename:String):Dynamic; - @:cffi private static function lime_alc_process_context (context:Dynamic):Void; - @:cffi private static function lime_alc_suspend_context (context:Dynamic):Void; + @:cffi private static function lime_alc_close_device (device:CFFIPointer):Bool; + @:cffi private static function lime_alc_create_context (device:CFFIPointer, attrlist:Dynamic):CFFIPointer; + @:cffi private static function lime_alc_destroy_context (context:CFFIPointer):Void; + @:cffi private static function lime_alc_get_contexts_device (context:CFFIPointer):CFFIPointer; + @:cffi private static function lime_alc_get_current_context ():CFFIPointer; + @:cffi private static function lime_alc_get_error (device:CFFIPointer):Int; + @:cffi private static function lime_alc_get_integerv (device:CFFIPointer, param:Int, size:Int):Dynamic; + @:cffi private static function lime_alc_get_string (device:CFFIPointer, param:Int):Dynamic; + @:cffi private static function lime_alc_make_context_current (context:CFFIPointer):Bool; + @:cffi private static function lime_alc_open_device (devicename:String):CFFIPointer; + @:cffi private static function lime_alc_process_context (context:CFFIPointer):Void; + @:cffi private static function lime_alc_suspend_context (context:CFFIPointer):Void; #end diff --git a/lime/audio/openal/ALContext.hx b/lime/audio/openal/ALContext.hx index 2c227b270..743e4e59f 100644 --- a/lime/audio/openal/ALContext.hx +++ b/lime/audio/openal/ALContext.hx @@ -1,14 +1,16 @@ package lime.audio.openal; +import lime.system.CFFIPointer; + @:allow(lime.audio.openal.AL) @:allow(lime.audio.openal.ALC) -abstract ALContext(Dynamic) { +abstract ALContext(CFFIPointer) from CFFIPointer to CFFIPointer { - private function new (handle:Dynamic) { + private function new (handle:CFFIPointer) { this = handle; diff --git a/lime/audio/openal/ALDevice.hx b/lime/audio/openal/ALDevice.hx index bb2b26605..00005cceb 100644 --- a/lime/audio/openal/ALDevice.hx +++ b/lime/audio/openal/ALDevice.hx @@ -1,14 +1,16 @@ package lime.audio.openal; +import lime.system.CFFIPointer; + @:allow(lime.audio.openal.AL) @:allow(lime.audio.openal.ALC) -abstract ALDevice(Dynamic) { +abstract ALDevice(CFFIPointer) from CFFIPointer to CFFIPointer { - private function new (handle:Dynamic) { + private function new (handle:CFFIPointer) { this = handle; diff --git a/lime/system/CFFI.hx b/lime/system/CFFI.hx index 3d333aad1..be82e8451 100644 --- a/lime/system/CFFI.hx +++ b/lime/system/CFFI.hx @@ -645,7 +645,7 @@ class CFFI { } typeSignature += "v"; - + default: if (useCPPTypes) { diff --git a/lime/system/CFFIPointer.hx b/lime/system/CFFIPointer.hx new file mode 100644 index 000000000..da1213f58 --- /dev/null +++ b/lime/system/CFFIPointer.hx @@ -0,0 +1,55 @@ +package lime.system; + + +abstract CFFIPointer(Dynamic) { + + + public function new (handle:Dynamic) { + + this = handle; + + } + + + public function get ():Float { + + if (this != null) { + + #if ((cpp || neko || nodejs) && !macro) + return lime_cffi_get_native_pointer (this); + #end + + } + + return 0; + + } + + + @:noCompletion @:op(A == B) private static inline function equals (a:CFFIPointer, b:Int):Bool { return a.get () == b; } + @:noCompletion @:op(A == B) private static inline function equalsPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () == b.get (); } + @:noCompletion @:op(A > B) private static inline function greaterThan (a:CFFIPointer, b:Int):Bool { return a.get () > b; } + @:noCompletion @:op(A > B) private static inline function greaterThanPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () > b.get (); } + @:noCompletion @:op(A >= B) private static inline function greaterThanOrEqual (a:CFFIPointer, b:Int):Bool { return a.get () >= b; } + @:noCompletion @:op(A >= B) private static inline function greaterThanOrEqualPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () >= b.get (); } + @:noCompletion @:op(A < B) private static inline function lessThan (a:CFFIPointer, b:Int):Bool { return a.get () < b; } + @:noCompletion @:op(A < B) private static inline function lessThanPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () < b.get (); } + @:noCompletion @:op(A <= B) private static inline function lessThanOrEqual (a:CFFIPointer, b:Int):Bool { return a.get () <= b; } + @:noCompletion @:op(A <= B) private static inline function lessThanOrEqualPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () <= b.get (); } + @:noCompletion @:op(A != B) private static inline function notEquals (a:CFFIPointer, b:Int):Bool { return a.get () != b; } + @:noCompletion @:op(A != B) private static inline function notEqualsPointer (a:CFFIPointer, b:CFFIPointer):Bool { return a.get () != b.get (); } + + + + + // Native Methods + + + + + #if ((cpp || neko || nodejs) && !macro) + private static var lime_cffi_get_native_pointer = CFFI.load ("lime", "lime_cffi_get_native_pointer", 1); + #end + + +} \ No newline at end of file diff --git a/project/Build.xml b/project/Build.xml index d2380759d..16e71b58e 100644 --- a/project/Build.xml +++ b/project/Build.xml @@ -215,6 +215,7 @@ + diff --git a/project/include/system/CFFIPointer.h b/project/include/system/CFFIPointer.h new file mode 100644 index 000000000..5e7131c81 --- /dev/null +++ b/project/include/system/CFFIPointer.h @@ -0,0 +1,25 @@ +#ifndef LIME_SYSTEM_CFFI_POINTER_H +#define LIME_SYSTEM_CFFI_POINTER_H + + +#include + + +namespace hx { + + class Object; + typedef void (*finalizer)(value v); + +} + + +namespace lime { + + + value CFFIPointer (void* ptr, hx::finalizer finalizer = 0); + + +} + + +#endif \ No newline at end of file diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index d36de5f37..42cca9d94 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -94,9 +95,7 @@ namespace lime { Application* application = CreateApplication (); Application::callback = new AutoGCRoot (callback); - value handle = cffi::alloc_pointer (application); - val_gc (handle, gc_application); - return handle; + return CFFIPointer (application, gc_application); } @@ -216,6 +215,13 @@ namespace lime { } + double lime_cffi_get_native_pointer (value handle) { + + return (intptr_t)val_data (handle); + + } + + void lime_cffi_finalizer (value abstract) { val_call0 ((value)val_data (abstract)); @@ -457,9 +463,7 @@ namespace lime { if (font->face) { - value handle = cffi::alloc_pointer (font); - val_gc (handle, gc_font); - return handle; + return CFFIPointer (font, gc_font); } else { @@ -966,9 +970,7 @@ namespace lime { value lime_renderer_create (value window) { Renderer* renderer = CreateRenderer ((Window*)val_data (window)); - value handle = cffi::alloc_pointer (renderer); - val_gc (handle, gc_renderer); - return handle; + return CFFIPointer (renderer, gc_renderer); } @@ -1068,9 +1070,7 @@ namespace lime { #if defined(LIME_FREETYPE) && defined(LIME_HARFBUZZ) TextLayout *text = new TextLayout (direction, script.__s, language.__s); - value handle = cffi::alloc_pointer (text); - val_gc (handle, gc_text_layout); - return handle; + return CFFIPointer (text, gc_text_layout); #else @@ -1155,9 +1155,7 @@ namespace lime { value lime_window_create (value application, int width, int height, int flags, HxString title) { Window* window = CreateWindow ((Application*)val_data (application), width, height, flags, title.__s); - value handle = cffi::alloc_pointer (window); - val_gc (handle, gc_window); - return handle; + return CFFIPointer (window, gc_window); } @@ -1295,6 +1293,7 @@ namespace lime { DEFINE_PRIME2 (lime_bytes_from_data_pointer); DEFINE_PRIME1 (lime_bytes_get_data_pointer); DEFINE_PRIME1 (lime_bytes_read_file); + DEFINE_PRIME1 (lime_cffi_get_native_pointer); DEFINE_PRIME1 (lime_cffi_set_finalizer); DEFINE_PRIME0 (lime_clipboard_get_text); DEFINE_PRIME1v (lime_clipboard_set_text); diff --git a/project/src/audio/openal/OpenALBindings.cpp b/project/src/audio/openal/OpenALBindings.cpp index 0a7e34ceb..2455ec4a9 100644 --- a/project/src/audio/openal/OpenALBindings.cpp +++ b/project/src/audio/openal/OpenALBindings.cpp @@ -7,6 +7,7 @@ #endif #include +#include #include @@ -890,16 +891,7 @@ namespace lime { int* list = val_array_int (attrlist); ALCcontext* alcContext = alcCreateContext (alcDevice, list); - - if (alcContext) { - - return cffi::alloc_pointer (alcContext); - - } else { - - return alloc_null (); - - } + return CFFIPointer (alcContext); } @@ -916,16 +908,7 @@ namespace lime { ALCcontext* alcContext = (ALCcontext*)val_data (context); ALCdevice* alcDevice = alcGetContextsDevice (alcContext); - - if (alcDevice) { - - return cffi::alloc_pointer (alcDevice); - - } else { - - return alloc_null (); - - } + return CFFIPointer (alcDevice); } @@ -933,16 +916,7 @@ namespace lime { value lime_alc_get_current_context () { ALCcontext* alcContext = alcGetCurrentContext (); - - if (alcContext) { - - return cffi::alloc_pointer (alcContext); - - } else { - - return alloc_null (); - - } + return CFFIPointer (alcContext); } @@ -997,16 +971,7 @@ namespace lime { ALCdevice* alcDevice = alcOpenDevice (devicename.__s); atexit (lime_al_cleanup); - - if (alcDevice) { - - return cffi::alloc_pointer (alcDevice); - - } else { - - return alloc_null (); - - } + return CFFIPointer (alcDevice); } diff --git a/project/src/system/CFFIPointer.cpp b/project/src/system/CFFIPointer.cpp new file mode 100644 index 000000000..10ad3d7e2 --- /dev/null +++ b/project/src/system/CFFIPointer.cpp @@ -0,0 +1,30 @@ +#include + + +namespace lime { + + + value CFFIPointer (void* ptr, hx::finalizer finalizer) { + + if (ptr) { + + value handle = cffi::alloc_pointer (ptr); + + if (finalizer) { + + val_gc (handle, finalizer); + + } + + return handle; + + } else { + + return alloc_null (); + + } + + } + + +} \ No newline at end of file