-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathutil-osx-impl.mm
More file actions
249 lines (204 loc) · 6.33 KB
/
Copy pathutil-osx-impl.mm
File metadata and controls
249 lines (204 loc) · 6.33 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/******************************************************************************
Copyright (C) 2016-2020 by Streamlabs (General Workings Inc)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "util-osx-impl.h"
#include "obs.h"
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#import <mach/mach.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
void UtilObjCInt::wait_terminate(void)
{
const char *name = "/tmp/exit-slobs-crash-handler";
std::vector<char> buffer;
size_t count = 10;
buffer.resize(count);
remove(name);
if (mkfifo(name, S_IRUSR | S_IWUSR) < 0)
return;
int file_descriptor = open(name, O_RDONLY | O_NONBLOCK);
if (file_descriptor < 0)
return;
while (true) {
if (state == UtilObjCInt::EState::Stop) {
break;
}
int ret = ::read(file_descriptor, buffer.data(), count);
if (ret > 0) {
bool appCrashed = *reinterpret_cast<bool *>(buffer.data());
if (appCrashed && state != UtilObjCInt::EState::Stop)
this->stopApplication();
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
close(file_descriptor);
remove(name);
}
@implementation UtilImplObj
UtilObjCInt::UtilObjCInt(void) : self(NULL), state(EState::Idle), worker(nullptr) {}
UtilObjCInt::~UtilObjCInt(void)
{
[(id)self dealloc];
}
void UtilObjCInt::init(void)
{
self = [[UtilImplObj alloc] init];
}
std::string UtilObjCInt::getDefaultVideoSavePath(void)
{
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *url = [fm URLForDirectory:NSMoviesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:true error:nil];
if (!url)
return getenv("HOME");
return url.path.fileSystemRepresentation;
}
void UtilObjCInt::runApplication(void)
{
// Wait for OBS_API_initAPI to be invoked
while (!hasInitApi() && isRunning()) {
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
if (isRunning()) {
// Create a dummy source to init obs-browser plugin. Must be init from main thread before [NSApplication run]
obs_source_t *source = obs_source_create("browser_source", "dummy", NULL, NULL);
obs_source_release(source);
nextState();
worker = new std::thread(&UtilObjCInt::wait_terminate, this);
@autoreleasepool {
NSApplication *app = [NSApplication sharedApplication];
[app run];
}
}
}
void UtilObjCInt::stopApplication(void)
{
if (!hasInitCef()) {
state = UtilObjCInt::EState::Stop;
return;
} else {
state = UtilObjCInt::EState::Stop;
}
dispatch_async(dispatch_get_main_queue(), ^{
[[NSApplication sharedApplication] stop:nil];
NSEvent *event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
location:NSZeroPoint
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
subtype:0
data1:0
data2:0];
[NSApp postEvent:event atStart:TRUE];
state = UtilObjCInt::EState::Stop;
if (worker->joinable())
worker->join();
});
}
bool UtilObjCInt::isRunning(void)
{
return state != UtilObjCInt::EState::Stop;
}
unsigned long long UtilObjCInt::getTotalPhysicalMemory(void)
{
return [NSProcessInfo processInfo].physicalMemory;
}
unsigned long long UtilObjCInt::getAvailableMemory(void)
{
mach_port_t host_port;
mach_msg_type_number_t host_size;
vm_size_t pagesize;
host_port = mach_host_self();
host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
host_page_size(host_port, &pagesize);
vm_statistics_data_t vm_stat;
if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS)
return 0;
// uint64_t mem_used = (uint64_t)(vm_stat.active_count +
// vm_stat.inactive_count +
// vm_stat.wire_count) * (uint64_t)pagesize;
uint64_t mem_free = (uint64_t)vm_stat.free_count * (uint64_t)pagesize * 10;
return mem_free;
}
std::vector<std::pair<uint32_t, uint32_t>> UtilObjCInt::getAvailableScreenResolutions(void)
{
std::vector<std::pair<uint32_t, uint32_t>> resolutions;
for (NSScreen *screen : [NSScreen screens]) {
resolutions.push_back(std::make_pair([screen frame].size.width, [screen frame].size.height));
}
return resolutions;
}
std::string UtilObjCInt::getUserDataPath(void)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([paths count] == 0)
return "";
NSString *userPath = [paths objectAtIndex:0];
return std::string([userPath UTF8String]);
}
std::string UtilObjCInt::getWorkingDirectory(void)
{
NSString *workindDirPath = [[NSProcessInfo processInfo] environment][@"PWD"];
return std::string([workindDirPath UTF8String]);
}
std::string UtilObjCInt::getComputerName(void)
{
@autoreleasepool {
NSString *computerName = [[NSHost currentHost] localizedName];
if (computerName) {
return [computerName UTF8String];
}
}
return {};
}
int UtilObjCInt::getPhysicalCores(void)
{
int physical_cores = 0;
size_t size = sizeof(physical_cores);
int ret = sysctlbyname("machdep.cpu.core_count", &physical_cores, &size, NULL, 0);
if (ret != 0)
return 0;
return physical_cores;
}
std::string UtilObjCInt::getCpuName(void)
{
char buffer[256];
size_t size = sizeof(buffer);
std::string name;
if (sysctlbyname("machdep.cpu.brand_string", buffer, &size, nullptr, 0) == 0) {
name = std::string(buffer);
} else if (sysctlbyname("hw.model", buffer, &size, nullptr, 0) == 0) {
name = std::string(buffer);
}
return name;
}
void UtilObjCInt::nextState(void)
{
if (state + 1 < UtilObjCInt::EState::Max) {
state = state + 1;
}
}
bool UtilObjCInt::hasInitApi(void)
{
return state == UtilObjCInt::EState::InitializedApi;
}
bool UtilObjCInt::hasInitCef(void)
{
return state == UtilObjCInt::EState::InitializedCEF;
}
@end