-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBRawAPILoader.cpp
171 lines (159 loc) · 6.01 KB
/
BRawAPILoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2022-2024 WangBin <wbsecg1 at gmail.com>
*/
#include "BlackmagicRawAPI.h"
#include "mdk/global.h"
#include <cassert>
# if __has_include("cppcompat/cstdlib.hpp")
# include "cppcompat/cstdlib.hpp"
# else
# include <cstdlib>
# endif
#if defined(_WIN32)
# ifndef UNICODE
# define UNICODE 1
# endif
# include <windows.h>
# ifdef WINAPI_FAMILY
# include <winapifamily.h>
# if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
# define BRAW_WINRT 1
# endif
# endif
# if (BRAW_WINRT+0)
# define dlopen(filename, flags) LoadPackagedLibrary(filename, 0)
# else
# define dlopen(filename, flags) LoadLibraryA(filename)
# endif
# define dlsym(handle, symbol) GetProcAddress((HMODULE)handle, symbol)
# define dlclose(handle) FreeLibrary((HMODULE)handle)
#elif defined(__APPLE__)
# include <CoreFoundation/CoreFoundation.h>
# define dlopen(filename, flags) load_bundle(filename)
# define dlsym(handle, symbol) ((handle) ? CFBundleGetFunctionPointerForName(handle, CFSTR(symbol)) : nullptr)
# define dlclose(handle) CFRelease(handle)
#else
# include <dlfcn.h>
#endif
#include <iostream>
using namespace std;
#define BRAW_ARG0() (), (), ()
#define BRAW_ARG1(P1) (P1), (P1 p1), (p1)
#define BRAW_ARG2(P1, P2) (P1, P2), (P1 p1, P2 p2), (p1, p2)
#define BRAW_ARG3(P1, P2, P3) (P1, P2, P3), (P1 p1, P2 p2, P3 p3), (p1, p2, p3)
#define _BRAW_API(R, NAME, ...) BRAW_API_EXPAND(BRAW_API_EXPAND_T_V(R, NAME, __VA_ARGS__))
#define BRAW_API_EXPAND(EXPR) EXPR
#define BRAW_API_EXPAND_T_V(R, F, ARG_T, ARG_T_V, ARG_V) \
R F ARG_T_V { \
static auto fp = (decltype(&F))dlsym(load_once(), #F); \
if (!fp) \
return default_rv<R>(); \
return fp ARG_V; \
}
template<typename T> static T default_rv() {return {};}
template<> [[maybe_unused]] void default_rv<void>() {}
#if (__APPLE__ + 0)
static CFBundleRef load_bundle(const char* fwkName)
{
if (strchr(fwkName, '/')) { // framework dir is set by user
auto name = CFStringCreateWithCString(kCFAllocatorDefault, fwkName, kCFStringEncodingUTF8);
auto fwkUrl = CFURLCreateWithString(nullptr, name, nullptr);
CFRelease(name);
if (!fwkUrl)
return nullptr;
auto s = CFStringCreateWithFormat(nullptr, nullptr, CFSTR("%@"), (CFTypeRef)fwkUrl);
clog << "braw bundle url: " << CFStringGetCStringPtr(s, kCFStringEncodingUTF8) << endl;
CFRelease(s);
auto b = CFBundleCreate(kCFAllocatorDefault, fwkUrl);
CFRelease(fwkUrl);
return b;
}
if (auto m = CFBundleGetMainBundle()) {
if (auto url = CFBundleCopyBundleURL(m)) {
if (auto ext = CFURLCopyPathExtension(url); ext) {
if (CFStringCompare(ext, CFSTR("appex"), 0) == kCFCompareEqualTo) {
// MY_APP.app/PlugIns/MY_APP_EXTENSION.appex
// appex is sandboxed, so main app/framework MUST link to fwkName if current module is dynamic loaded
if (auto appUrl = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, url); appUrl) {
CFRelease(url);
url = appUrl;
appUrl = CFURLCreateCopyDeletingLastPathComponent(kCFAllocatorDefault, url);
CFRelease(url);
url = appUrl;
if (auto appM = CFBundleCreate(kCFAllocatorDefault, url); appM) {
CFRelease(m);
m = appM;
}
}
}
CFRelease(ext);
}
CFRelease(url);
}
if (auto url = CFBundleCopyPrivateFrameworksURL(m)) {
auto name = CFStringCreateWithCString(kCFAllocatorDefault, fwkName, kCFStringEncodingUTF8);
auto fwkUrl = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault, url, name, false);
CFRelease(name);
CFRelease(url);
if (fwkUrl) {
auto s = CFStringCreateWithFormat(nullptr, nullptr, CFSTR("%@"), (CFTypeRef)fwkUrl);
clog << "braw bundle url: " << CFStringGetCStringPtr(s, kCFStringEncodingUTF8) << endl;
CFRelease(s);
auto b = CFBundleCreate(kCFAllocatorDefault, fwkUrl);
CFRelease(fwkUrl);
return b;
}
}
}
return nullptr;
}
#endif
inline string to_string(const wchar_t* ws)
{
string s(snprintf(nullptr, 0, "%ls", ws), 0);
snprintf(&s[0], s.size() + 1, "%ls", ws);
return s;
}
inline string to_string(const char* s) { return s;}
static auto load()
{
const auto name_default =
#if (_WIN32+0)
"BlackmagicRawAPI.dll"
#elif (__APPLE__+0)
"BlackmagicRawAPI.framework"
#else
"libBlackmagicRawAPI.so"
#endif
;
const auto v = mdk::GetGlobalOption("BRAWSDK_DIR");
if (const auto s = get_if<string>(&v)) {
if (const auto dso = dlopen((*s + "/" + name_default).data(), RTLD_NOW | RTLD_LOCAL)) {
return dso;
}
clog << "Failed to load BRAW runtime in $BRAWSDK_DIR: " + *s << endl;
}
auto dso = dlopen(name_default, RTLD_NOW | RTLD_LOCAL);
if (!dso)
clog << "Failed to load BRAW runtime: " + to_string(name_default) << endl;
return dso;
}
static auto load_once()
{
static const auto dso = load();
return dso;
}
extern "C" {
_BRAW_API(IBlackmagicRawFactory*, CreateBlackmagicRawFactoryInstance, BRAW_ARG0())
#ifndef _WIN32
_BRAW_API(HRESULT, VariantInit, BRAW_ARG1(Variant*))
_BRAW_API(HRESULT, VariantClear, BRAW_ARG1(Variant*))
_BRAW_API(SafeArray*, SafeArrayCreate, BRAW_ARG3(BlackmagicRawVariantType, uint32_t, SafeArrayBound*))
_BRAW_API(HRESULT, SafeArrayGetVartype, BRAW_ARG2(SafeArray*, BlackmagicRawVariantType*))
_BRAW_API(HRESULT, SafeArrayGetLBound, BRAW_ARG3(SafeArray*, uint32_t, long*))
_BRAW_API(HRESULT, SafeArrayGetUBound, BRAW_ARG3(SafeArray*, uint32_t, long*))
_BRAW_API(HRESULT, SafeArrayAccessData, BRAW_ARG2(SafeArray*, void**))
_BRAW_API(HRESULT, SafeArrayUnaccessData, BRAW_ARG1(SafeArray*))
_BRAW_API(HRESULT, SafeArrayDestroy, BRAW_ARG1(SafeArray*))
#endif
}