1 /*
2 * Copyright (c) 2015 Guillaume Piolat
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 *   notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 *   notice, this list of conditions and the following disclaimer in the
14 *   documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the names 'Derelict', 'DerelictSDL', nor the names of its contributors
17 *   may be used to endorse or promote products derived from this software
18 *   without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 module derelict.carbon.carbon;
33 
34 
35 import dplug.core.sharedlib;
36 
37 import derelict.carbon.hitoolbox;
38 
39 import dplug.core.nogc;
40 
41 version(OSX)
42     enum libNames = "/System/Library/Frameworks/Carbon.framework/Carbon";
43 else
44     enum libNames = "";
45 
46 
47 class DerelictCarbonLoader : SharedLibLoader
48 {
49     public
50     {
51         nothrow @nogc:
52         this()
53         {
54             super(libNames);
55         }
56 
57         override void loadSymbols()
58         {
59             // hitoolbox
60             bindFunc(cast(void**)&GetMainEventLoop, "GetMainEventLoop");
61             bindFunc(cast(void**)&InstallEventHandler, "InstallEventHandler");
62             bindFunc(cast(void**)&GetControlEventTarget, "GetControlEventTarget");
63             bindFunc(cast(void**)&GetWindowEventTarget, "GetWindowEventTarget");
64 
65            // Not available in macOS 10.12 in 64-bit
66             static if (size_t.sizeof == 4)
67                 bindFunc(cast(void**)&CreateUserPaneControl, "CreateUserPaneControl");
68 
69             bindFunc(cast(void**)&GetWindowAttributes, "GetWindowAttributes");
70             bindFunc(cast(void**)&HIViewGetRoot, "HIViewGetRoot");
71             bindFunc(cast(void**)&HIViewFindByID, "HIViewFindByID");
72             bindFunc(cast(void**)&HIViewSetNeedsDisplayInRect, "HIViewSetNeedsDisplayInRect");
73             bindFunc(cast(void**)&HIViewAddSubview, "HIViewAddSubview");
74 
75             // Removed for no reason, still here in macOS 10.12
76             //bindFunc(cast(void**)&GetRootControl, "GetRootControl");
77             //bindFunc(cast(void**)&CreateRootControl, "CreateRootControl");
78             //static if (size_t.sizeof == 4)
79             //    bindFunc(cast(void**)&EmbedControl, "EmbedControl");
80 
81             bindFunc(cast(void**)&SizeControl, "SizeControl");
82             bindFunc(cast(void**)&GetEventClass, "GetEventClass");
83             bindFunc(cast(void**)&GetEventKind, "GetEventKind");
84             bindFunc(cast(void**)&GetEventParameter, "GetEventParameter");
85             bindFunc(cast(void**)&RemoveEventLoopTimer, "RemoveEventLoopTimer");
86             bindFunc(cast(void**)&RemoveEventHandler, "RemoveEventHandler");
87             bindFunc(cast(void**)&InstallEventLoopTimer, "InstallEventLoopTimer");
88             bindFunc(cast(void**)&HIPointConvert, "HIPointConvert");
89             bindFunc(cast(void**)&HIViewGetBounds, "HIViewGetBounds");
90         }
91     }
92 }
93 
94 
95 private __gshared DerelictCarbonLoader DerelictCarbon;
96 
97 private __gshared loaderCounterCarbon = 0;
98 
99 // Call this each time a novel owner uses these functions
100 // TODO: hold a mutex, because this isn't thread-safe
101 void acquireCarbonFunctions() nothrow @nogc
102 {
103     if (DerelictCarbon is null)  // You only live once
104     {
105         DerelictCarbon = mallocEmplace!DerelictCarbonLoader();
106         DerelictCarbon.load();
107     }
108 }
109 
110 // Call this each time a novel owner releases a Cocoa functions
111 // TODO: hold a mutex, because this isn't thread-safe
112 void releaseCarbonFunctions() nothrow @nogc
113 {
114     /*if (--loaderCounterCarbon == 0)
115     {
116         DerelictCarbon.unload();
117         DerelictCarbon.destroyFree();
118     }*/
119 }
120 
121 unittest
122 {
123     version(OSX)
124     {
125         acquireCarbonFunctions();
126         releaseCarbonFunctions();
127     }
128 }