-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAXWindow.m
216 lines (191 loc) · 6.57 KB
/
AXWindow.m
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
//
// AXWindow.m
// FMN
//
// Created by David Noblet on 7/18/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "AXWindow.h"
#import "FMNDisplayConfiguration.h"
#import "FMNDisplay.h"
@implementation AXWindow
- (id) init { [self release]; return nil; }
- (id) initWithAXElement : (AXUIElementRef) windowElem
ofApp : (AXApplication *) app
origin : (NSPoint) inOrigin
{
if (![super init])
return nil;
CFRetain(windowElem);
windowElement = windowElem;
windowApp = [app retain];
origin = inOrigin;
return self;
}
- (NSString *) description
{
return [NSString stringWithFormat:
@"AX Window 0x%x of app: %@", (long)windowElement, windowApp];
}
- (NSPoint) getWindowPosition
{
NSPoint point;
CFTypeRef value;
if (AXUIElementCopyAttributeValue(windowElement,kAXPositionAttribute,
&value) != kAXErrorSuccess)
{
[NSException raise:FMNWindowException
format:@"Unable get position of %@", self];
}
if (!AXValueGetValue(value,AXValueGetType(value),&point))
{
[NSException raise:FMNWindowException
format:@"Unable get position of %@", self];
}
CFRelease(value);
// HACK: XXX: Work around accessibility API bogosity in Leopard
// For now I'm discarding anything with x = -40, but should filter on
// screen height as well. But need to find out *which* screen height...
if ((point.x == -40.0))// && (point.y == ...))
[NSException raise:FMNWindowException
format:@"Got bogus position for %@", self];
point.x -= origin.x;
point.y -= origin.y;
return point;
}
- (NSSize) getWindowSize
{
NSSize size;
CFTypeRef value;
if (AXUIElementCopyAttributeValue(windowElement,kAXSizeAttribute,
&value) != kAXErrorSuccess)
{
[NSException raise:FMNWindowException
format:@"Unable get size of %@", self];
}
if (!AXValueGetValue(value,AXValueGetType(value),&size))
{
[NSException raise:FMNWindowException
format:@"Unable get size of %@", self];
}
CFRelease(value);
return size;
}
- (void) setWindowPosition : (NSPoint) pos
{
CFTypeRef value;
value = AXValueCreate(kAXValueCGPointType,&pos);
if(AXUIElementSetAttributeValue(windowElement,kAXPositionAttribute,value)
!= kAXErrorSuccess)
{
CFRelease(value);
[NSException raise:FMNWindowException
format:@"Unable set position to %f, %f for %@",
pos.x, pos.y, self];
}
NSPoint afterPos = [self getWindowPosition];
if (afterPos.x != pos.x || afterPos.y != pos.y)
{
NSLog(@"Failed to set position of %@ ((%f, %f) != (%f, %f)), and Accessibility API lied.",
self, pos.x, pos.y, afterPos.x, afterPos.y);
//NSLog(@"x-offset = %f, y-offset = %f",[off_x floatValue], [off_y floatValue]);
}
CFRelease(value);
}
- (void) setWindowPosition : (NSPoint) pos Context : (NSDictionary*) context
{
[self setWindowPosition:pos];
}
- (void) setWindowSize : (NSSize) size Context : (NSDictionary*) context
{
CFTypeRef value;
value = AXValueCreate(kAXValueCGSizeType,&size);
if(AXUIElementSetAttributeValue(windowElement,kAXSizeAttribute,value)
!= kAXErrorSuccess)
{
CFRelease(value);
[NSException raise:FMNWindowException
format:@"Unable set size to %fx%f for %@",
size.width, size.height, self];
}
NSSize afterSize = [self getWindowSize];
if (afterSize.width != size.width || afterSize.height != size.height)
{
NSLog(@"Failed to set size of %@ ((%f, %f) != (%f, %f)), and Accessibility API lied.",
self, size.width, size.height, afterSize.width, afterSize.height);
}
CFRelease(value);
}
- (BOOL) sanityCheckForSize:(NSSize)size
Position:(NSPoint)pos
Context:(NSDictionary*) context
{
FMNDisplayConfigurationRef dc = [context objectForKey:@"fmn_new_display_configuration"];
NSLog(@"Checking %.2fx%.2f window at (%.2f, %.2f)",
size.width, size.height, pos.x, pos.y);
if (dc != nil) {
NSRect winRect;
winRect.origin = pos; winRect.size = size;
float onArea = 0.0;
float winArea = size.width * size.height;
int i=0;
for (; i < [dc getDisplayCount]; ++i) {
/* BUG: This assumes screens don't overlap. */
FMNDisplayRef dr = [dc getDisplay:i];
NSRect screenRect = [dr getDisplayOrientation];
NSRect onRect = NSIntersectionRect(screenRect, winRect);
float thisArea = onRect.size.width * onRect.size.height;
onArea += thisArea;
NSLog(@"... %.2f%% overlap with disp %.2fx%.2f@(%.2f, %.2f)",
(thisArea/winArea)*100.0, screenRect.size.width,
screenRect.size.height, screenRect.origin.x, screenRect.origin.y);
}
/* Bail out if the onscreen area would be too small */
if (2*onArea < (winArea))
return NO;
} else {
NSLog(@"Couldn't get display configuration from context. No sanity checking!");
}
return YES;
}
- (void) setWindowSize : (NSSize) size Position : (NSPoint) pos Context : (NSDictionary*) context
{
if (![self sanityCheckForSize:size Position:pos Context:context]) {
NSLog(@"Refusing to put %.2fx%.2f window at (%.2f, %.2f): It would be over 1/2 offscreen",
size.width, size.height, pos.x, pos.y);
return;
}
/* Check to make sure the given size/pos doesn't put over half the window
offscreen */
[self setWindowPosition : pos Context : context];
[self setWindowSize : size Context : context];
}
- (void) dealloc
{
if (windowElement)
{
CFRelease(windowElement);
}
if (windowApp)
{
[windowApp release];
}
[super dealloc];
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:windowApp forKey:@"AXWwindowApp"];
[encoder encodeBytes:(const uint8_t*)&windowElement
length:sizeof(AXUIElementRef)
forKey:@"AXWwindowElement"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
unsigned dummy;
self = [super init];
windowApp = [[decoder decodeObjectForKey:@"AXWwindowApp"] retain];
windowElement = *(AXUIElementRef *)[decoder decodeBytesForKey:@"AXWwindowElement"
returnedLength:&dummy];
return self;
}
@end