Skip to content

Commit

Permalink
Fixing the Mac M1 issue #38
Browse files Browse the repository at this point in the history
  • Loading branch information
lmbelo committed Mar 15, 2023
1 parent 2dfb88a commit d6df03e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 10 additions & 2 deletions delphifmx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
45 changes: 45 additions & 0 deletions delphifmx/darwin_arm.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d6df03e

Please sign in to comment.