-
Notifications
You must be signed in to change notification settings - Fork 1
/
AXApplication.m
164 lines (139 loc) · 4.23 KB
/
AXApplication.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
//
// AXApplication.m
// FMN
//
// Created by David Noblet on 7/19/06.
// Copyright 2006 __MyCompanyName__. All rights reserved.
//
#import "AXApplication.h"
#import "AXWindow.h"
@implementation AXApplication
+ (id) configWithPSN : (ProcessSerialNumber) processSerialNumber
appName : (NSString *)name
origin : (NSPoint) inOrigin
{
return [[[AXApplication alloc] initWithPSN : processSerialNumber
appName : name
origin : inOrigin]
autorelease];
}
- (NSString *) description
{
return [NSString stringWithString:appName];
}
- (id) init { [self release]; return nil; }
- (id) initWithPSN : (ProcessSerialNumber) processSerialNumber
appName : (NSString *)name
origin : (NSPoint) inOrigin
{
if(![super init])
return nil;
psn = processSerialNumber;
pid = 0;
GetProcessPID(&psn, &pid);
// Create the accessibility UI element for the given app
appElement = AXUIElementCreateApplication(pid);
// Create the observer that will receive events
if (!appElement)
{
[self release];
@throw
[NSException
exceptionWithName : FMNWindowGroupException
reason : @"Unable to create observer for pid"
userInfo : nil
];
}
origin = inOrigin;
appName = [name retain];
return self;
}
- (NSArray*) getWindows
{
CFTypeRef value;
if (AXUIElementCopyAttributeValue(appElement,kAXWindowsAttribute,
&value) != kAXErrorSuccess)
{
@throw
[NSException
exceptionWithName : FMNWindowException
reason : @"Unable get AX windows for app"
userInfo : nil
];
}
NSArray* windowArray = (NSArray*) value;
NSMutableArray* windows =
[NSMutableArray arrayWithCapacity : [windowArray count]];
NSEnumerator* enumerator = [windowArray objectEnumerator];
AXUIElementRef elt;
while (elt = (AXUIElementRef)[enumerator nextObject]) {
[windows addObject:
[[[AXWindow alloc] initWithAXElement:elt ofApp:self origin:origin] autorelease]];
}
[windowArray release];
return windows;
}
- (NSArray*) getCurrentWindowOrientations
{
NSArray* windows = [self getWindows];
NSMutableArray* windowOrientations =
[NSMutableArray arrayWithCapacity : [windows count]];
NSEnumerator* enumerator = [windows objectEnumerator];
FMNWindowRef window;
FMNWindowOrientation* windowOrientation;
//NSLog(@"Getting window orientations");
while(window = (FMNWindowRef)[enumerator nextObject])
{
@try
{
windowOrientation = [[[FMNWindowOrientation alloc]
initWithWindow : window] autorelease];
[windowOrientations addObject : windowOrientation];
}
@catch (NSException* ex)
{
NSLog(@"Error getting window orientation %@", [ex reason]);
}
}
return windowOrientations;
}
- (void) dealloc
{
if (appElement)
{
CFRelease(appElement);
}
if (appName)
{
[appName release];
}
[super dealloc];
}
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:appName forKey:@"AXAappName"];
[encoder encodeBytes:(const uint8_t*)&appElement
length:sizeof(AXUIElementRef)
forKey:@"AXAappElement"];
[encoder encodeInt64:(int64_t)pid forKey:@"AXApid"];
[encoder encodeBytes:(const uint8_t*)&psn
length:sizeof(ProcessSerialNumber)
forKey:@"AXApsn"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
unsigned dummy;
self = [super init];
appName = [[decoder decodeObjectForKey:@"AXAappName"] retain];
appElement = *(AXUIElementRef *)[decoder decodeBytesForKey:@"AXAappElement"
returnedLength:&dummy];
pid = (pid_t)[decoder decodeInt64ForKey:@"AXApid"];
psn = *(ProcessSerialNumber *)[decoder decodeBytesForKey:@"AXApsn"
returnedLength:&dummy];
return self;
}
- (NSString *)name
{
return appName;
}
@end