diff --git a/delphifmx/__init__.py b/delphifmx/__init__.py index cb654f3..25cd238 100644 --- a/delphifmx/__init__.py +++ b/delphifmx/__init__.py @@ -60,8 +60,16 @@ def findmodule(): else: raise ValueError("Unsupported platform.") -def new_import(): - modulefullpath = findmodule() +def init_plat(): + if (platform.system() == "Darwin") and (platform.machine() == "arm64"): + try: + from . import darwin_arm + except Exception as e: + print("Darwin util has failed with message \'%s\'." % (str(e),)) + +def new_import(): + init_plat() + modulefullpath = findmodule() loader = importlib.machinery.ExtensionFileLoader("DelphiFMX", modulefullpath) spec = importlib.util.spec_from_file_location("DelphiFMX", modulefullpath, loader=loader, submodule_search_locations=None) diff --git a/delphifmx/darwin_arm.py b/delphifmx/darwin_arm.py new file mode 100644 index 0000000..b3864d2 --- /dev/null +++ b/delphifmx/darwin_arm.py @@ -0,0 +1,45 @@ +# THIS SCRIPT ONLY WORKS ON DARWIN + +# We are using this script to initialize GUI before loading the DelphiFMX library. +# This approach prevents the dead-lock that happens when we start GUI with the dynamic-linker initializer. + +try: + import ctypes +except ImportError: + raise NotImplementedError("DelphiFMX requires ctypes, which doesn't seem to be available.") + +from ctypes import cdll, c_void_p, c_char_p + +framework_name = '/System/Library/Frameworks/AppKit.framework/AppKit' +class_name = 'NSScreen' +method_name = 'mainScreen' + +try: + c = cdll.LoadLibrary(framework_name) +except OSError: + raise ValueError('No framework named \'%s\' found.' %(framework_name,)) + +objc_getClass = c.objc_getClass +objc_getClass.argtypes = [c_char_p] +objc_getClass.restype = c_void_p + +class_getClassMethod = c.class_getClassMethod +class_getClassMethod.restype = c_void_p +class_getClassMethod.argtypes = [c_void_p, c_void_p] + +sel_registerName = c.sel_registerName +sel_registerName.restype = c_void_p +sel_registerName.argtypes = [c_char_p] + +ptr = objc_getClass(class_name.encode('ascii')) +if ptr is None: + raise ValueError('No Objective-C class named \'%s\' found.' % (class_name,)) + +method = class_getClassMethod(ptr, sel_registerName(method_name.encode('ascii'))) +if not method: + raise AttributeError('No class method found for selector "%s".' % (sel_registerName(method_name.encode('ascii')))) + +objc_msgSend = c['objc_msgSend'] +objc_msgSend.argtypes = [c_void_p, c_void_p] +objc_msgSend.restype = c_void_p +objc_msgSend(ptr, sel_registerName(method_name.encode('ascii')), None) \ No newline at end of file