Debian Codegraph @

src/platform/mac/macplatform.mm

raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "macplatform.h"
4 #include "cfref.h"
5
6 #include "app/applicationexceptionhandler.h"
7 #include "common/log.h"
8 #include "copyqpasteboardmime.h"
9 #include "foregroundbackgroundfilter.h"
10 #include "macplatformwindow.h"
11 #include "platform/mac/macactivity.h"
12 #include "urlpasteboardmime.h"
13 #include "macclipboard.h"
14
15 #include <libproc.h>
16 #include <QApplication>
17 #include <QCoreApplication>
18 #include <QDir>
19 #include <QGuiApplication>
20 #include <QScopedPointer>
21 #include <QStringList>
22
23 #include <Cocoa/Cocoa.h>
24 #include <Carbon/Carbon.h>
25 #include <mach/mach.h>
26
27 namespace {
28 class ClipboardApplication : public QApplication
29 {
30 public:
31 ClipboardApplication(int &argc, char **argv)
32 : QApplication(argc, argv)
33 , m_pasteboardMime()
34 , m_pasteboardMimeUrl(QLatin1String("public.url"))
35 , m_pasteboardMimeFileUrl(QLatin1String("public.file-url"))
36 {
37 }
38
39 private:
40 CopyQPasteboardMime m_pasteboardMime;
41 UrlPasteboardMime m_pasteboardMimeUrl;
42 UrlPasteboardMime m_pasteboardMimeFileUrl;
43 };
44
45 template<typename T> inline T* objc_cast(id from)
46 {
47 if (from && [from isKindOfClass:[T class]]) {
48 return static_cast<T*>(from);
49 }
50 return nil;
51 }
52
53 bool isApplicationInItemList(LSSharedFileListRef list) {
54 bool flag = false;
55 UInt32 seed;
56 CFRef<CFArrayRef> items = LSSharedFileListCopySnapshot(list, &seed);
57 if (items) {
58 CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
59 if (url) {
60 for (id item in(__bridge NSArray *) items.get()) {
61 LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
62 if (LSSharedFileListItemResolve(itemRef, 0, &url, NULL) == noErr) {
63 if ([[(__bridge NSURL *) url path] hasPrefix:[[NSBundle mainBundle] bundlePath]]) {
64 flag = true;
65 break;
66 }
67 }
68 }
69 }
70 }
71 return flag;
72 }
73
74 void addToLoginItems()
75 {
76 CFRef<LSSharedFileListRef> list = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL);
77 if (list) {
78 if (!isApplicationInItemList(list)) {
79 CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
80 if (url) {
81 // Don't "Hide on Launch", as we don't have a window to show anyway
82 NSDictionary *properties = [NSDictionary
83 dictionaryWithObject: [NSNumber numberWithBool:NO]
84 forKey: @"com.apple.loginitem.HideOnLaunch"];
85 CFRef<LSSharedFileListItemRef> item = LSSharedFileListInsertItemURL(list, kLSSharedFileListItemLast, NULL, NULL, url, (__bridge CFDictionaryRef)properties, NULL);
86 (void)item; // released automatically
87 } else {
88 ::log("Unable to find url for bundle, can't auto-load app", LogWarning);
89 }
90 }
91 } else {
92 ::log("Unable to access shared file list, can't auto-load app", LogWarning);
93 }
94 }
95
96 void removeFromLoginItems()
97 {
98 CFRef<LSSharedFileListRef> list = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL);
99 if (list) {
100 if (isApplicationInItemList(list)) {
101 CFURLRef url = (__bridge CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
102 if (url) {
103 UInt32 seed;
104 CFRef<CFArrayRef> items = LSSharedFileListCopySnapshot(list, &seed);
105 if (items) {
106 for (id item in(__bridge NSArray *) items.get()) {
107 LSSharedFileListItemRef itemRef = (__bridge LSSharedFileListItemRef)item;
108 if (LSSharedFileListItemResolve(itemRef, 0, &url, NULL) == noErr)
109 if ([[(__bridge NSURL *) url path] hasPrefix:[[NSBundle mainBundle] bundlePath]])
110 LSSharedFileListItemRemove(list, itemRef);
111 }
112 } else {
113 ::log("No items in list of auto-loaded apps, can't stop auto-load of app", LogWarning);
114 }
115 } else {
116 ::log("Unable to find url for bundle, can't stop auto-load of app", LogWarning);
117 }
118 }
119 } else {
120 ::log("Unable to access shared file list, can't stop auto-load of app", LogWarning);
121 }
122 }
123
124 QString absoluteResourcesePath(const QString &path)
125 {
126 return QCoreApplication::applicationDirPath() + "/../Resources/" + path;
127 }
128
129 template <typename QtApplication>
130 class Activity
131 : public MacActivity
132 , public ApplicationExceptionHandler<QtApplication>
133 {
134 public:
135 Activity(int &argc, char **argv, const QString &reason)
136 : MacActivity(reason)
137 , ApplicationExceptionHandler<QtApplication>(argc, argv)
138 {
139 [NSApp setActivationPolicy:NSApplicationActivationPolicyProhibited];
140 }
141 };
142
143 } // namespace
144
145 PlatformNativeInterface *platformNativeInterface()
146 {
147 static MacPlatform platform;
148 return &platform;
149 }
150
151 MacPlatform::MacPlatform()
152 {
153 }
154
155 QCoreApplication *MacPlatform::createConsoleApplication(int &argc, char **argv)
156 {
157 return new ApplicationExceptionHandler<QCoreApplication>(argc, argv);
158 }
159
160 QApplication *MacPlatform::createServerApplication(int &argc, char **argv)
161 {
162 QApplication *app = new Activity<ClipboardApplication>(argc, argv, "CopyQ Server");
163
164 // Switch the app to foreground when in foreground
165 ForegroundBackgroundFilter::installFilter(app);
166
167 return app;
168 }
169
170 QGuiApplication *MacPlatform::createClipboardProviderApplication(int &argc, char **argv)
171 {
172 return new Activity<ClipboardApplication>(argc, argv, "CopyQ clipboard provider");
173 }
174
175 QCoreApplication *MacPlatform::createClientApplication(int &argc, char **argv)
176 {
177 return new Activity<QCoreApplication>(argc, argv, "CopyQ Client");
178 }
179
180 QGuiApplication *MacPlatform::createTestApplication(int &argc, char **argv)
181 {
182 return new Activity<QGuiApplication>(argc, argv, "CopyQ Tests");
183 }
184
185 PlatformClipboardPtr MacPlatform::clipboard()
186 {
187 return PlatformClipboardPtr(new MacClipboard());
188 }
189
190 QStringList MacPlatform::getCommandLineArguments(int argc, char **argv)
191 {
192 QStringList arguments;
193
194 for (int i = 1; i < argc; ++i)
195 arguments.append( QString::fromUtf8(argv[i]) );
196
197 return arguments;
198 }
199
200 bool MacPlatform::findPluginDir(QDir *pluginsDir)
201 {
202 pluginsDir->setPath( qApp->applicationDirPath() );
203 if (pluginsDir->dirName() != "MacOS") {
204 return pluginsDir->cd("plugins");
205 }
206
207 if ( pluginsDir->cdUp() // Contents
208 && pluginsDir->cd("PlugIns")
209 && pluginsDir->cd("copyq"))
210 {
211 return true;
212 }
213
214 pluginsDir->setPath( qApp->applicationDirPath() );
215
216 if ( pluginsDir->cdUp() // Contents
217 && pluginsDir->cdUp() // copyq.app
218 && pluginsDir->cdUp() // repo root
219 && pluginsDir->cd("plugins"))
220 {
221 return true;
222 }
223
224 return false;
225 }
226
227 QString MacPlatform::defaultEditorCommand()
228 {
229 return "open -t -W -n %1";
230 }
231
232 QString MacPlatform::translationPrefix()
233 {
234 return absoluteResourcesePath("translations");
235 }
236
237 QString MacPlatform::themePrefix()
238 {
239 return absoluteResourcesePath("themes");
240 }
241
242 PlatformWindowPtr MacPlatform::getCurrentWindow()
243 {
244 // FIXME: frontmostApplication doesn't seem to work well for own windows (at least in tests).
245 auto window = QApplication::activeWindow();
246 if (window == nullptr)
247 window = QApplication::activeModalWidget();
248 if (window != nullptr)
249 return PlatformWindowPtr(new MacPlatformWindow(window->winId()));
250
251 NSRunningApplication *runningApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
252 return PlatformWindowPtr(new MacPlatformWindow(runningApp));
253 }
254
255 PlatformWindowPtr MacPlatform::getWindow(WId winId) {
256 return PlatformWindowPtr(new MacPlatformWindow(winId));
257 }
258
259 bool MacPlatform::isAutostartEnabled()
260 {
261 // Note that this will need to be done differently if CopyQ goes into
262 // the App Store.
263 // http://rhult.github.io/articles/sandboxed-launch-on-login/
264 bool isInList = false;
265 CFRef<LSSharedFileListRef> list = LSSharedFileListCreate(kCFAllocatorDefault, kLSSharedFileListSessionLoginItems, NULL);
266 if (list) {
267 isInList = isApplicationInItemList(list);
268 }
269 return isInList;
270 }
271
272 void MacPlatform::setAutostartEnabled(bool shouldEnable)
273 {
274 if (shouldEnable != isAutostartEnabled()) {
275 if (shouldEnable) {
276 addToLoginItems();
277 } else {
278 removeFromLoginItems();
279 }
280 }
281 }
282 qint64 MacPlatform::processResidentMemoryBytes(qint64 pid)
283 {
284 struct proc_taskinfo pti;
285 const int size = proc_pidinfo(static_cast<int>(pid), PROC_PIDTASKINFO, 0, &pti, sizeof(pti));
286 if (size != sizeof(pti))
287 return -1;
288 return static_cast<qint64>(pti.pti_resident_size);
289 }