Vuo 2.4.4
Loading...
Searching...
No Matches
libmodule.c
Go to the documentation of this file.
1
10#include <dlfcn.h>
11#include <limits.h>
12#include <mach-o/dyld.h>
13
14#include "VuoCompositionState.h"
15
16#include "module.h"
17
19
24{
25 typedef void * (*funcType)(void);
26 funcType vuoCopyCompositionStateFromThreadLocalStorage = (funcType) dlsym(RTLD_SELF, "vuoCopyCompositionStateFromThreadLocalStorage");
28 vuoCopyCompositionStateFromThreadLocalStorage = (funcType) dlsym(RTLD_DEFAULT, "vuoCopyCompositionStateFromThreadLocalStorage");
29
31}
32
38dispatch_once_t VuoGetWorkingDirectoryOnce = 0;
39
45const char *VuoGetWorkingDirectory(void)
46{
47 static char *workingDirectoryResult = NULL;
48 dispatch_once(&VuoGetWorkingDirectoryOnce, ^{
49 typedef char * (*vuoGetWorkingDirectoryType)(struct VuoCompositionState *);
50 vuoGetWorkingDirectoryType vuoGetWorkingDirectory = (vuoGetWorkingDirectoryType) dlsym(RTLD_SELF, "vuoGetWorkingDirectory");
52 vuoGetWorkingDirectory = (vuoGetWorkingDirectoryType) dlsym(RTLD_DEFAULT, "vuoGetWorkingDirectory");
53
55 {
56 void *compositionState = copyCompositionStateFromThreadLocalStorage();
57 workingDirectoryResult = vuoGetWorkingDirectory((struct VuoCompositionState *)compositionState);
58 free(compositionState);
59 }
60 else
61 {
62 // Keep consistent with VuoRuntimePersistentState::getCurrentWorkingDirectory().
63 char currentWorkingDirectory[PATH_MAX+1];
64 getcwd(currentWorkingDirectory, PATH_MAX+1);
65 workingDirectoryResult = strdup(currentWorkingDirectory);
66 }
67 });
68 return workingDirectoryResult;
69}
70
71static pthread_once_t VuoFrameworkPathsOnce = PTHREAD_ONCE_INIT;
73static char *VuoFrameworkPath = NULL;
74static char *VuoRunnerFrameworkPath = NULL;
75
81void VuoFrameworkPaths_dylibLoaded(const struct mach_header *mh32, intptr_t vmaddr_slide)
82{
84 return;
85
86 const struct mach_header_64 *mh = (const struct mach_header_64 *)mh32;
87
88 // Ignore system libraries.
89 if (mh->flags & MH_DYLIB_IN_CACHE)
90 return;
91
92 // Get the file path of the current dylib.
93 Dl_info info = {"", NULL, "", NULL};
94 dladdr((void *)vmaddr_slide, &info);
95
96 // Check whether it's one of the dylibs we're looking for.
97
98 char * (^getMatchingPath)(const char *) = ^(const char *fragment) {
99 const char *found = strstr(info.dli_fname, fragment);
100 if (found)
101 {
102 char *pathCandidate = strndup(info.dli_fname, found - info.dli_fname + strlen(fragment));
103 if (access(pathCandidate, 0) == 0)
104 return pathCandidate;
105 else
106 free(pathCandidate);
107 }
108 return (char *)NULL;
109 };
110
111 char *possibleVuoFramework = getMatchingPath("/Vuo.framework");
112 if (possibleVuoFramework)
113 VuoFrameworkPath = possibleVuoFramework;
114
115 char *possibleVuoRunnerFramework = getMatchingPath("/VuoRunner.framework");
116 if (possibleVuoRunnerFramework)
117 VuoRunnerFrameworkPath = possibleVuoRunnerFramework;
118}
119
124{
125 // Check whether Vuo.framework is in the list of loaded dynamic libraries.
126 _dyld_register_func_for_add_image(&VuoFrameworkPaths_dylibLoaded);
128 // The above function invokes the callback for each already-loaded dylib, then returns.
129
131 {
132 // Check for VuoRunner.framework alongside Vuo.framework.
133 const char *runnerSuffix = "/../VuoRunner.framework";
134 char *pathCandidate = malloc(strlen(VuoFrameworkPath) + strlen(runnerSuffix) + 1);
135 strcpy(pathCandidate, VuoFrameworkPath);
136 strcat(pathCandidate, runnerSuffix);
137 if (access(pathCandidate, 0) == 0)
138 VuoRunnerFrameworkPath = pathCandidate;
139 else
140 free(pathCandidate);
141 }
142}
143
154const char *VuoGetFrameworkPath(void)
155{
157 return VuoFrameworkPath;
158}
159
176
186{
189 return VuoFrameworkPath;
190 else
192}
193
195{
196 typedef pid_t (*vuoGetRunnerPidType)(struct VuoCompositionState *);
197 vuoGetRunnerPidType vuoGetRunnerPid = (vuoGetRunnerPidType) dlsym(RTLD_SELF, "vuoGetRunnerPid");
198 if (!vuoGetRunnerPid)
199 vuoGetRunnerPid = (vuoGetRunnerPidType) dlsym(RTLD_DEFAULT, "vuoGetRunnerPid");
200
201 void *compositionState = copyCompositionStateFromThreadLocalStorage();
202 pid_t runnerPid = vuoGetRunnerPid((struct VuoCompositionState *)compositionState);
203 free(compositionState);
204 return runnerPid;
205}
206
208{
209 typedef void (*vuoStopCompositionType)(struct VuoCompositionState *);
210 vuoStopCompositionType vuoStopComposition = (vuoStopCompositionType) dlsym(RTLD_SELF, "vuoStopComposition");
212 vuoStopComposition = (vuoStopCompositionType) dlsym(RTLD_DEFAULT, "vuoStopComposition");
213
214 void *compositionState = copyCompositionStateFromThreadLocalStorage();
215 vuoStopComposition((struct VuoCompositionState *)compositionState);
216 free(compositionState);
217}
218
220{
221 typedef void (*vuoStopCompositionType)(struct VuoCompositionState *);
222 vuoStopCompositionType vuoStopComposition = (vuoStopCompositionType) dlsym(RTLD_SELF, "vuoStopComposition");
224 vuoStopComposition = (vuoStopCompositionType) dlsym(RTLD_DEFAULT, "vuoStopComposition");
225
226 vuoStopComposition(NULL);
227}
228
230{
231 typedef void (*vuoDisableTerminationType)(struct VuoCompositionState *);
232 vuoDisableTerminationType vuoDisableTermination = (vuoDisableTerminationType) dlsym(RTLD_SELF, "vuoDisableTermination");
234 vuoDisableTermination = (vuoDisableTerminationType) dlsym(RTLD_DEFAULT, "vuoDisableTermination");
235
236 void *compositionState = copyCompositionStateFromThreadLocalStorage();
237 vuoDisableTermination((struct VuoCompositionState *)compositionState);
238 free(compositionState);
239}
240
242{
243 typedef void (*vuoEnableTerminationType)(struct VuoCompositionState *);
244 vuoEnableTerminationType vuoEnableTermination = (vuoEnableTerminationType) dlsym(RTLD_SELF, "vuoEnableTermination");
246 vuoEnableTermination = (vuoEnableTerminationType) dlsym(RTLD_DEFAULT, "vuoEnableTermination");
247
248 void *compositionState = copyCompositionStateFromThreadLocalStorage();
249 vuoEnableTermination((struct VuoCompositionState *)compositionState);
250 free(compositionState);
251}
252
254{
255 typedef bool (*vuoShouldShowSplashWindowType)(void);
256 vuoShouldShowSplashWindowType vuoShouldShowSplashWindow = (vuoShouldShowSplashWindowType) dlsym(RTLD_DEFAULT, "vuoShouldShowSplashWindow");
257 if (!vuoShouldShowSplashWindow)
258 {
259// VLog("Warning: Couldn't find symbol VuoShouldShowSplashWindow.");
260 return false;
261 }
262// VLog("shouldShowSplashWindow = %d",vuoShouldShowSplashWindow());
263 return vuoShouldShowSplashWindow();
264}
265
266bool VuoIsPro(void)
267{
268 static bool proEnabledResult = false;
269 static dispatch_once_t once = 0;
270 dispatch_once(&once, ^{
271 bool *proEnabled = (bool *) dlsym(RTLD_SELF, "VuoProEnabled");
272 if (!proEnabled)
273 proEnabled = (bool *) dlsym(RTLD_DEFAULT, "VuoProEnabled");
274 if (!proEnabled)
275 return;
276 proEnabledResult = *proEnabled;
277 });
278 return proEnabledResult;
279}
280
285{
286#if __x86_64__
287 // Based on https://software.intel.com/en-us/articles/how-to-detect-new-instruction-support-in-the-4th-generation-intel-core-processor-family
288 uint32_t eax = 7, ebx, ecx = 0, edx;
289 __asm__ ( "cpuid" : "+b" (ebx), "+a" (eax), "+c" (ecx), "=d" (edx) );
290 uint32_t avx2_mask = 1 << 5;
291 return (ebx & avx2_mask) == avx2_mask;
292#else
293 return false;
294#endif
295}