Skip to content

Commit d6df03e

Browse files
committed
Fixing the Mac M1 issue #38
1 parent 2dfb88a commit d6df03e

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

delphifmx/__init__.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,16 @@ def findmodule():
6060
else:
6161
raise ValueError("Unsupported platform.")
6262

63-
def new_import():
64-
modulefullpath = findmodule()
63+
def init_plat():
64+
if (platform.system() == "Darwin") and (platform.machine() == "arm64"):
65+
try:
66+
from . import darwin_arm
67+
except Exception as e:
68+
print("Darwin util has failed with message \'%s\'." % (str(e),))
69+
70+
def new_import():
71+
init_plat()
72+
modulefullpath = findmodule()
6573
loader = importlib.machinery.ExtensionFileLoader("DelphiFMX", modulefullpath)
6674
spec = importlib.util.spec_from_file_location("DelphiFMX", modulefullpath,
6775
loader=loader, submodule_search_locations=None)

delphifmx/darwin_arm.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# THIS SCRIPT ONLY WORKS ON DARWIN
2+
3+
# We are using this script to initialize GUI before loading the DelphiFMX library.
4+
# This approach prevents the dead-lock that happens when we start GUI with the dynamic-linker initializer.
5+
6+
try:
7+
import ctypes
8+
except ImportError:
9+
raise NotImplementedError("DelphiFMX requires ctypes, which doesn't seem to be available.")
10+
11+
from ctypes import cdll, c_void_p, c_char_p
12+
13+
framework_name = '/System/Library/Frameworks/AppKit.framework/AppKit'
14+
class_name = 'NSScreen'
15+
method_name = 'mainScreen'
16+
17+
try:
18+
c = cdll.LoadLibrary(framework_name)
19+
except OSError:
20+
raise ValueError('No framework named \'%s\' found.' %(framework_name,))
21+
22+
objc_getClass = c.objc_getClass
23+
objc_getClass.argtypes = [c_char_p]
24+
objc_getClass.restype = c_void_p
25+
26+
class_getClassMethod = c.class_getClassMethod
27+
class_getClassMethod.restype = c_void_p
28+
class_getClassMethod.argtypes = [c_void_p, c_void_p]
29+
30+
sel_registerName = c.sel_registerName
31+
sel_registerName.restype = c_void_p
32+
sel_registerName.argtypes = [c_char_p]
33+
34+
ptr = objc_getClass(class_name.encode('ascii'))
35+
if ptr is None:
36+
raise ValueError('No Objective-C class named \'%s\' found.' % (class_name,))
37+
38+
method = class_getClassMethod(ptr, sel_registerName(method_name.encode('ascii')))
39+
if not method:
40+
raise AttributeError('No class method found for selector "%s".' % (sel_registerName(method_name.encode('ascii'))))
41+
42+
objc_msgSend = c['objc_msgSend']
43+
objc_msgSend.argtypes = [c_void_p, c_void_p]
44+
objc_msgSend.restype = c_void_p
45+
objc_msgSend(ptr, sel_registerName(method_name.encode('ascii')), None)

0 commit comments

Comments
 (0)