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 /**
33     Dynamic bindings to the Carbon framework.
34 */
35 module derelict.carbon.carbon;
36 
37 
38 import dplug.core.sharedlib;
39 
40 import derelict.carbon.hitoolbox;
41 
42 import dplug.core.nogc;
43 
44 version(OSX)
45     enum libNames = "/System/Library/Frameworks/Carbon.framework/Carbon";
46 else
47     enum libNames = "";
48 
49 
50 class DerelictCarbonLoader : SharedLibLoader
51 {
52     public
53     {
54         nothrow @nogc:
55         this()
56         {
57             super(libNames);
58         }
59 
60         override void loadSymbols()
61         {
62             // hitoolbox
63             bindFunc(cast(void**)&GetMainEventLoop, "GetMainEventLoop");
64             bindFunc(cast(void**)&InstallEventHandler, "InstallEventHandler");
65             bindFunc(cast(void**)&GetControlEventTarget, "GetControlEventTarget");
66             bindFunc(cast(void**)&GetWindowEventTarget, "GetWindowEventTarget");
67 
68            // Not available in macOS 10.12 in 64-bit
69             static if (size_t.sizeof == 4)
70                 bindFunc(cast(void**)&CreateUserPaneControl, "CreateUserPaneControl");
71 
72             bindFunc(cast(void**)&GetWindowAttributes, "GetWindowAttributes");
73             bindFunc(cast(void**)&HIViewGetRoot, "HIViewGetRoot");
74             bindFunc(cast(void**)&HIViewFindByID, "HIViewFindByID");
75             bindFunc(cast(void**)&HIViewSetNeedsDisplayInRect, "HIViewSetNeedsDisplayInRect");
76             bindFunc(cast(void**)&HIViewAddSubview, "HIViewAddSubview");
77 
78             // Removed for no reason, still here in macOS 10.12
79             //bindFunc(cast(void**)&GetRootControl, "GetRootControl");
80             //bindFunc(cast(void**)&CreateRootControl, "CreateRootControl");
81             //static if (size_t.sizeof == 4)
82             //    bindFunc(cast(void**)&EmbedControl, "EmbedControl");
83 
84             bindFunc(cast(void**)&SizeControl, "SizeControl");
85             bindFunc(cast(void**)&GetEventClass, "GetEventClass");
86             bindFunc(cast(void**)&GetEventKind, "GetEventKind");
87             bindFunc(cast(void**)&GetEventParameter, "GetEventParameter");
88             bindFunc(cast(void**)&RemoveEventLoopTimer, "RemoveEventLoopTimer");
89             bindFunc(cast(void**)&RemoveEventHandler, "RemoveEventHandler");
90             bindFunc(cast(void**)&InstallEventLoopTimer, "InstallEventLoopTimer");
91             bindFunc(cast(void**)&HIPointConvert, "HIPointConvert");
92             bindFunc(cast(void**)&HIViewGetBounds, "HIViewGetBounds");
93         }
94     }
95 }
96 
97 
98 private __gshared DerelictCarbonLoader DerelictCarbon;
99 
100 private __gshared loaderCounterCarbon = 0;
101 
102 // Call this each time a novel owner uses these functions
103 // TODO: hold a mutex, because this isn't thread-safe
104 void acquireCarbonFunctions() nothrow @nogc
105 {
106     if (DerelictCarbon is null)  // You only live once
107     {
108         DerelictCarbon = mallocNew!DerelictCarbonLoader();
109         DerelictCarbon.load();
110     }
111 }
112 
113 // Call this each time a novel owner releases a Cocoa functions
114 // TODO: hold a mutex, because this isn't thread-safe
115 void releaseCarbonFunctions() nothrow @nogc
116 {
117     /*if (--loaderCounterCarbon == 0)
118     {
119         DerelictCarbon.unload();
120         DerelictCarbon.destroyFree();
121     }*/
122 }
123 
124 unittest
125 {
126     version(OSX)
127     {
128         acquireCarbonFunctions();
129         releaseCarbonFunctions();
130     }
131 }