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
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/nswindow_additions.h"
#include <dlfcn.h>
#include "base/logging.h"
typedef void* CGSConnectionID;
typedef int CGSWindowID;
typedef int CGSError;
typedef int CGSWorkspaceID;
// These are private APIs we look up at run time.
typedef CGSConnectionID (*CGSDefaultConnectionFunc)(void);
typedef CGSError (*CGSGetWindowWorkspaceFunc)(const CGSConnectionID cid,
CGSWindowID wid,
CGSWorkspaceID* workspace);
typedef CGSError (*CGSMoveWorkspaceWindowListFunc)(const CGSConnectionID cid,
CGSWindowID* wids,
int count,
CGSWorkspaceID workspace);
static CGSDefaultConnectionFunc sCGSDefaultConnection;
static CGSGetWindowWorkspaceFunc sCGSGetWindowWorkspace;
static CGSMoveWorkspaceWindowListFunc sCGSMoveWorkspaceWindowList;
@implementation NSWindow(ChromeAdditions)
// Looks up private Spaces APIs using dlsym.
- (BOOL)cr_initializeWorkspaceAPIs {
static BOOL shouldInitialize = YES;
if (shouldInitialize) {
shouldInitialize = NO;
NSBundle* coreGraphicsBundle =
[NSBundle bundleWithIdentifier:@"com.apple.CoreGraphics"];
NSString* coreGraphicsPath = [[coreGraphicsBundle bundlePath]
stringByAppendingPathComponent:@"CoreGraphics"];
void* coreGraphicsLibrary = dlopen([coreGraphicsPath UTF8String],
RTLD_GLOBAL | RTLD_LAZY);
if (coreGraphicsLibrary) {
sCGSDefaultConnection =
(CGSDefaultConnectionFunc)dlsym(coreGraphicsLibrary,
"_CGSDefaultConnection");
if (!sCGSDefaultConnection) {
LOG(ERROR) << "Failed to lookup _CGSDefaultConnection API" << dlerror();
}
sCGSGetWindowWorkspace =
(CGSGetWindowWorkspaceFunc)dlsym(coreGraphicsLibrary,
"CGSGetWindowWorkspace");
if (!sCGSGetWindowWorkspace) {
LOG(ERROR) << "Failed to lookup CGSGetWindowWorkspace API" << dlerror();
}
sCGSMoveWorkspaceWindowList =
(CGSMoveWorkspaceWindowListFunc)dlsym(coreGraphicsLibrary,
"CGSMoveWorkspaceWindowList");
if (!sCGSMoveWorkspaceWindowList) {
LOG(ERROR) << "Failed to lookup CGSMoveWorkspaceWindowList API"
<< dlerror();
}
} else {
LOG(ERROR) << "Failed to load CoreGraphics lib" << dlerror();
}
}
return sCGSDefaultConnection != NULL &&
sCGSGetWindowWorkspace != NULL &&
sCGSMoveWorkspaceWindowList != NULL;
}
- (BOOL)cr_workspace:(CGSWorkspaceID*)outWorkspace {
if (![self cr_initializeWorkspaceAPIs]) {
return NO;
}
// If this ASSERT fails then consider using CGSDefaultConnectionForThread()
// instead of CGSDefaultConnection().
DCHECK([NSThread isMainThread]);
CGSConnectionID cid = sCGSDefaultConnection();
CGSWindowID wid = [self windowNumber];
CGSError err = sCGSGetWindowWorkspace(cid, wid, outWorkspace);
return err == 0;
}
- (BOOL)cr_moveToWorkspace:(CGSWorkspaceID)workspace {
if (![self cr_initializeWorkspaceAPIs]) {
return NO;
}
// If this ASSERT fails then consider using CGSDefaultConnectionForThread()
// instead of CGSDefaultConnection().
DCHECK([NSThread isMainThread]);
CGSConnectionID cid = sCGSDefaultConnection();
CGSWindowID wid = [self windowNumber];
// CGSSetWorkspaceForWindow doesn't seem to work for some reason.
CGSError err = sCGSMoveWorkspaceWindowList(cid, &wid, 1, workspace);
return err == 0;
}
@end
|