1 /**
2 Dynamic bindings to the CoreServices framework.
3 
4 Copyright: Guillaume Piolat 2015.
5 License:   $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
6 */
7 module derelict.carbon.coreservices;
8 
9 import core.stdc.config;
10 
11 import dplug.core.sharedlib;
12 
13 import derelict.carbon.corefoundation;
14 
15 import dplug.core.nogc;
16 
17 version(OSX)
18     enum libNames = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
19 else
20     enum libNames = "";
21 
22 
23 class DerelictCoreServicesLoader : SharedLibLoader
24 {
25     public
26     {
27         nothrow @nogc:
28         this()
29         {
30             super(libNames);
31         }
32 
33         override void loadSymbols()
34         {
35             bindFunc(cast(void**)&SetComponentInstanceStorage, "SetComponentInstanceStorage");
36             bindFunc(cast(void**)&GetComponentInstanceStorage, "GetComponentInstanceStorage");
37             bindFunc(cast(void**)&GetComponentInfo, "GetComponentInfo");
38         }
39     }
40 }
41 
42 private __gshared DerelictCoreServicesLoader DerelictCoreServices;
43 
44 private __gshared loaderCounterCS = 0;
45 
46 // Call this each time a novel owner uses these functions
47 // TODO: hold a mutex, because this isn't thread-safe
48 void acquireCoreServicesFunctions() nothrow @nogc
49 {
50     if (DerelictCoreServices is null)  // You only live once
51     {
52         DerelictCoreServices = mallocNew!DerelictCoreServicesLoader();
53         DerelictCoreServices.load();
54     }
55 }
56 
57 // Call this each time a novel owner releases a Cocoa functions
58 // TODO: hold a mutex, because this isn't thread-safe
59 void releaseCoreServicesFunctions() nothrow @nogc
60 {
61     /*if (--loaderCounterCS == 0)
62     {
63         DerelictCoreServices.unload();
64         DerelictCoreServices.destroyFree();
65     }*/
66 }
67 
68 unittest
69 {
70     version(OSX)
71     {
72         acquireCoreServicesFunctions();
73         releaseCoreServicesFunctions();
74     }
75 }
76 
77 enum : int
78 {
79     typeSInt16                 = CCONST('s', 'h', 'o', 'r'),
80     typeUInt16                 = CCONST('u', 's', 'h', 'r'),
81     typeSInt32                 = CCONST('l', 'o', 'n', 'g'),
82     typeUInt32                 = CCONST('m', 'a', 'g', 'n'),
83     typeSInt64                 = CCONST('c', 'o', 'm', 'p'),
84     typeUInt64                 = CCONST('u', 'c', 'o', 'm'),
85     typeIEEE32BitFloatingPoint = CCONST('s', 'i', 'n', 'g'),
86     typeIEEE64BitFloatingPoint = CCONST('d', 'o', 'u', 'b'),
87     type128BitFloatingPoint    = CCONST('l', 'd', 'b', 'l'),
88     typeDecimalStruct          = CCONST('d', 'e', 'c', 'm'),
89     typeChar                   = CCONST('T', 'E', 'X', 'T'),
90 }
91 
92 // <CarbonCore/MacErrors.h>
93 enum : int
94 {
95     badComponentInstance = cast(int)0x80008001,
96     badComponentSelector = cast(int)0x80008002
97 }
98 
99 enum
100 {
101     coreFoundationUnknownErr      = -4960,
102 }
103 
104 
105 // <CarbonCore/Components.h>
106 
107 // LP64 => "long and pointers are 64-bit"
108 static if (size_t.sizeof == 8 && c_long.sizeof == 8)
109     private enum __LP64__ = 1;
110 else
111     private enum __LP64__ = 0;
112 
113 alias Component = void*;
114 alias ComponentResult = int;
115 
116 alias ComponentInstance = void*;
117 
118 struct ComponentParameters
119 {
120     UInt8               flags;
121     UInt8               paramSize;
122     SInt16              what;
123     static if (__LP64__)
124         UInt32          padding;
125     c_long[1]           params;
126 }
127 
128 static if (__LP64__)
129 {
130     static assert(ComponentParameters.sizeof == 16);
131 }
132 
133 enum : int
134 {
135     kComponentOpenSelect          = -1,
136     kComponentCloseSelect         = -2,
137     kComponentCanDoSelect         = -3,
138     kComponentVersionSelect       = -4,
139     kComponentRegisterSelect      = -5,
140     kComponentTargetSelect        = -6,
141     kComponentUnregisterSelect    = -7,
142     kComponentGetMPWorkFunctionSelect = -8,
143     kComponentExecuteWiredActionSelect = -9,
144     kComponentGetPublicResourceSelect = -10
145 };
146 
147 struct ComponentDescription
148 {
149     OSType              componentType;
150     OSType              componentSubType;
151     OSType              componentManufacturer;
152     UInt32              componentFlags;
153     UInt32              componentFlagsMask;
154 }
155 
156 extern(C) nothrow @nogc
157 {
158     alias da_SetComponentInstanceStorage = void function(ComponentInstance, Handle);
159     alias da_GetComponentInfo = OSErr function(Component, ComponentDescription*, Handle, Handle, Handle);
160     alias da_GetComponentInstanceStorage = Handle function(ComponentInstance aComponentInstance);
161 }
162 
163 __gshared
164 {
165     da_SetComponentInstanceStorage SetComponentInstanceStorage;
166     da_GetComponentInfo GetComponentInfo;
167     da_GetComponentInstanceStorage GetComponentInstanceStorage;
168 }
169 
170