-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform_mac.mm
205 lines (185 loc) · 7.78 KB
/
platform_mac.mm
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
// Copyright 2022-present Contributors to the jobman project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/jobman
#include "platform.h"
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <os/log.h>
#include <QApplication>
#include <QScreen>
namespace platform
{
namespace {
QPointF toNativeCursor(int x, int y)
{
QScreen* screen = QGuiApplication::primaryScreen();
QPointF cursor = QPointF(x, y);
qreal reverse = screen->geometry().height() - cursor.y();
return QPointF(cursor.x(), reverse);
}
NSWindow* toNativeWindow(WId winId)
{
NSView *view = (NSView*)winId;
return [view window];
}
NSScreen* toNativeScreen(WId winId)
{
NSWindow *window = toNativeWindow(winId);
return [window screen];
}
struct ColorSyncProfile {
uint32_t screenNumber;
CFStringRef displayProfileUrl;
ColorSyncProfile() : screenNumber(0), displayProfileUrl(nullptr) {}
ColorSyncProfile(const ColorSyncProfile& other)
: screenNumber(other.screenNumber) {
displayProfileUrl = other.displayProfileUrl ? static_cast<CFStringRef>(CFRetain(other.displayProfileUrl)) : nullptr;
}
ColorSyncProfile& operator=(const ColorSyncProfile& other) {
if (this != &other) {
screenNumber = other.screenNumber;
if (displayProfileUrl) CFRelease(displayProfileUrl);
displayProfileUrl = other.displayProfileUrl ? static_cast<CFStringRef>(CFRetain(other.displayProfileUrl)) : nullptr;
}
return *this;
}
~ColorSyncProfile() {
if (displayProfileUrl) CFRelease(displayProfileUrl);
}
};
QMap<uint32_t, ColorSyncProfile> colorsynccache;
ColorSyncProfile getColorSyncProfile(NSScreen* screen)
{
ColorSyncProfile colorSyncProfile;
NSDictionary* screenDescription = [screen deviceDescription];
NSNumber* screenID = [screenDescription objectForKey:@"NSScreenNumber"];
colorSyncProfile.screenNumber = [screenID unsignedIntValue];
ColorSyncProfileRef csProfileRef = ColorSyncProfileCreateWithDisplayID((CGDirectDisplayID)colorSyncProfile.screenNumber);
if (csProfileRef) {
CFURLRef iccURLRef = ColorSyncProfileGetURL(csProfileRef, NULL);
if (iccURLRef) {
colorSyncProfile.displayProfileUrl = CFURLCopyFileSystemPath(iccURLRef, kCFURLPOSIXPathStyle);
}
CFRelease(csProfileRef);
}
return colorSyncProfile;
}
ColorSyncProfile getDisplayProfile(NSScreen* screen) {
NSDictionary* screenDescription = [screen deviceDescription];
CGDirectDisplayID displayId = [[screenDescription objectForKey:@"NSScreenNumber"] unsignedIntValue];
if (colorsynccache.contains(displayId)) {
return colorsynccache.value(displayId);
}
ColorSyncProfile colorSyncProfile = getColorSyncProfile(screen);
colorsynccache.insert(displayId, colorSyncProfile);
return colorSyncProfile;
}
}
void setDarkTheme()
{
// we force dark aque no matter appearance set in system settings
[NSApp setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameDarkAqua]];
}
IccProfile getIccProfile(WId wid)
{
NSScreen* screen = toNativeScreen(wid);
ColorSyncProfile colorsyncProfile = getDisplayProfile(screen);
return IccProfile {
int(colorsyncProfile.screenNumber),
QString::fromCFString(colorsyncProfile.displayProfileUrl)
};
}
QString getIccProfileUrl(WId wid)
{
return getIccProfile(wid).displayProfileUrl;
}
QString getApplicationPath()
{
return QApplication::applicationDirPath() + "/..";
}
QString resolveBookmark(const QString& bookmark)
{
if (bookmark.isEmpty()) {
return QString();
}
QByteArray bookmarkData = QByteArray::fromBase64(bookmark.toUtf8());
NSError* error = nil;
BOOL isstale = NO;
NSURL *url = [NSURL URLByResolvingBookmarkData:[NSData dataWithBytes:bookmarkData.data() length:bookmarkData.size()]
options:NSURLBookmarkResolutionWithSecurityScope
relativeToURL:nil
bookmarkDataIsStale:&isstale
error:&error];
if (url && !error) {
if ([url startAccessingSecurityScopedResource]) {
return QString::fromUtf8([[url path] UTF8String]);
} else {
QString();
}
} else {
QString();
}
return QString();
}
QString saveBookmark(const QString& bookmark)
{
if (bookmark.isEmpty()) {
return QString();
}
NSURL* url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:bookmark.toUtf8().constData()]];
NSError* error = nil;
NSData* bookmarkData = [url bookmarkDataWithOptions:NSURLBookmarkCreationWithSecurityScope
includingResourceValuesForKeys:nil
relativeToURL:nil
error:&error];
if (bookmarkData && !error) {
QByteArray bookmark((const char *)[bookmarkData bytes], [bookmarkData length]);
return QString::fromUtf8(bookmark.toBase64());
} else {
return QString();
}
}
double getCpuUsage()
{
natural_t numcpus;
processor_info_array_t cpuinfo;
mach_msg_type_number_t numcpuinfo;
host_t host = mach_host_self();
kern_return_t status;
static processor_info_array_t prevcpuinfo = NULL;
static mach_msg_type_number_t numprevcpuinfo = 0;
static double lastTotalCPU = 0;
status = host_processor_info(host, PROCESSOR_CPU_LOAD_INFO, &numcpus, &cpuinfo, &numcpuinfo);
if (status != KERN_SUCCESS) {
return -1.0;
}
double totalcpu = 0;
for (natural_t i = 0; i < numcpus; ++i) {
unsigned long long user = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER];
unsigned long long system = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM];
unsigned long long idle = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE];
unsigned long long nice = cpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE];
unsigned long long total = user + system + idle + nice;
if (prevcpuinfo) {
unsigned long long prevtotal = prevcpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] +
prevcpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] +
prevcpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE] +
prevcpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE];
unsigned long long totaldiff = total - prevtotal;
unsigned long long idlediff = idle - prevcpuinfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE];
double cpuUsage = (double)(totaldiff - idlediff) / (double)totaldiff;
totalcpu += cpuUsage * 100.0;
}
}
if (prevcpuinfo) {
vm_deallocate(mach_task_self(), (vm_address_t)prevcpuinfo, sizeof(integer_t) * numprevcpuinfo);
}
prevcpuinfo = cpuinfo;
numprevcpuinfo = numcpuinfo;
return totalcpu / numcpus;
}
void console(const QString& log)
{
NSLog(@"%@", log.toNSString());
}
}