1 module bindbc.cocoa.dynloadfoundation;
2 
3 import bindbc.cocoa.foundation;
4 import bindbc.cocoa.appkit;
5 
6 import bindbc.loader;
7 
8 private {
9     SharedLib lib;
10     FoundationSupport loadedVersion;
11 }
12 
13 enum FoundationSupport {
14     noLibrary,
15     badLibrary,
16     foundationLibrary
17 }
18 
19 void unloadFoundation()
20 {
21     if(lib != invalidHandle) {
22         lib.unload();
23     }
24 }
25 
26 FoundationSupport loadedFoundationVersion() { return loadedVersion; }
27 
28 bool isFoundationLoaded() { return lib != invalidHandle; }
29 
30 FoundationSupport loadFoundation(const(char)* libName)
31 {
32     // If the library isn't yet loaded, load it now.
33     if(lib == invalidHandle) {
34         lib = load(libName);
35         if(lib == invalidHandle) {
36             return FoundationSupport.noLibrary;
37         }
38     }
39 
40     auto errCount = errorCount();
41 
42     lib.bindSymbol(cast(void**)&NSLog, "NSLog");
43     lib.bindSymbol(cast(void**)&NSAllocateMemoryPages, "NSAllocateMemoryPages");
44     lib.bindSymbol(cast(void**)&NSDeallocateMemoryPages, "NSDeallocateMemoryPages");
45 
46     lib.bindSymbol(cast(void**)&NSClassFromString, "NSClassFromString");
47     lib.bindSymbol(cast(void**)&NSStringFromClass, "NSStringFromClass");
48     lib.bindSymbol(cast(void**)&NSSelectorFromString, "NSSelectorFromString");
49     lib.bindSymbol(cast(void**)&NSStringFromSelector, "NSStringFromSelector");
50     lib.bindSymbol(cast(void**)&NSStringFromProtocol, "NSStringFromProtocol");
51     lib.bindSymbol(cast(void**)&NSProtocolFromString, "NSProtocolFromString");
52 
53     // MAYDO: load from proper global variables
54     NSRunLoopCommonModes = NSString.stringWith("kCFRunLoopCommonModes"w);
55 
56     kCAContentsFormatRGBA8Uint = NSString.stringWith("RGBA8"w); // value from Big Sur
57 
58     // For debugging purpose
59     //NSLog(NSString.stringWith("%@\n")._id, NSDefaultRunLoopMode._id);
60     //NSLog(NSString.stringWith("%@\n")._id, NSRunLoopCommonModes._id);
61     
62     if(errorCount() != errCount) loadedVersion = FoundationSupport.badLibrary;
63     else loadedVersion = FoundationSupport.foundationLibrary;
64 
65     return loadedVersion;
66 }