-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowTracker.m
354 lines (277 loc) · 12.2 KB
/
WindowTracker.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
//
// WindowTracker.m
// TrackingFramework
//
// Created by Sylvain Malacria on 10/07/15.
// Copyright (c) 2015 Sylvain Malacria. All rights reserved.
//
#import "WindowTracker.h"
#import "WindowInfoEvent.h"
#import "WindowGrabber.h"
#import "VnrWindowInfo.h"
#include "platform.h"
@implementation WindowTracker{
NSArray* windowsInfoArray;
}
-(id)initWithDelay:(float)delay andXMLFileAccess:(XMLFileAccessMethods*)xml{
self = [super init];
if(self){
NSLog(@"init windowTracker");
//windowsInfoArray = [WindowGrabber getWindowList]; /// to send a windowEvent at first call of update
self.xmlFileAccess = xml;
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(processTimer) userInfo:nil repeats:YES];
}
}
return self;
}
-(void)printWindows{
int depth=0;
NSMutableString* print = [NSMutableString new];
[print appendString:@"\n"];
for(VnrWindowInfo* window in windowsInfoArray){
CGRect frame= window.frame;
[print appendString:[NSString stringWithFormat:@"%d - %@ - fr:%@ \n",++depth,[window ownerName],NSStringFromRect(frame)]];
}
[print appendString:@"\n\n\n"];
NSLog(@"%@",print);
}
+(NSArray*)getCurrentWindowsInfo{
return [WindowGrabber getWindowList];
}
-(void)processTimer{
[self update];
}
# pragma mark called periodically to check wether or not events should be fired
-(void)update{
uint64 clock = os_gettime_ns();
NSArray* newWindowsInfoArray = [WindowGrabber getWindowList];
NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
if([newWindowsInfoArray count]<[windowsInfoArray count]){
// here window(s) closed
// find windows that are gone and send events
NSArray* disposedWindowsEvents = [WindowTracker getDisposedWindowsBetween:newWindowsInfoArray and:windowsInfoArray atTime:time atClock:clock];
for(WindowInfoEvent* event in disposedWindowsEvents){
if([self.windowTrackerDelegate respondsToSelector:@selector(windowInfoEventHappened:)]){
[self.windowTrackerDelegate windowInfoEventHappened:event];
}
}
}
else if([newWindowsInfoArray count]>[windowsInfoArray count]){
// here window(s) opened
// find windows that are gone and send events
NSArray* openedWindowsEvents = [WindowTracker getOpenedWindowsBetween:newWindowsInfoArray and:windowsInfoArray atTime:time atClock:clock];
for(WindowInfoEvent* event in openedWindowsEvents){
if([self.windowTrackerDelegate respondsToSelector:@selector(windowInfoEventHappened:)]){
[self.windowTrackerDelegate windowInfoEventHappened:event];
}
}
}
else {
// test if order of windows has changed
// if yes, window(s)reordered
NSArray* reorderedSnaps =[WindowTracker orderDiffersBetween:newWindowsInfoArray and:windowsInfoArray atTime:time atClock:clock];
if([reorderedSnaps count]>0){
for (WindowInfoEvent *event in reorderedSnaps){
if([self.windowTrackerDelegate respondsToSelector:@selector(windowInfoEventHappened:)]){
[self.windowTrackerDelegate windowInfoEventHappened:event];
}
}
}
else {
// test if position of a window has changed
// if yes, window is currently moving. Wait for stable position or write everything????
WindowInfoEvent * movingWindow = [WindowTracker getIDofMovingWindowBetween:newWindowsInfoArray and:windowsInfoArray atTime:time atClock:clock];
if(movingWindow){
if([self.windowTrackerDelegate respondsToSelector:@selector(windowInfoEventHappened:)]){
[self.windowTrackerDelegate windowInfoEventHappened:movingWindow];
}
}
WindowInfoEvent * resizedWindow = [WindowTracker getIDofResizedWindowBetween:newWindowsInfoArray and:windowsInfoArray atTime:time atClock:clock];
if(resizedWindow){
if([self.windowTrackerDelegate respondsToSelector:@selector(windowInfoEventHappened:)]){
[self.windowTrackerDelegate windowInfoEventHappened:resizedWindow];
}
}
}
}
windowsInfoArray = newWindowsInfoArray;
}
// Method that returns an array of AYWindowSnapshot that are contained in one array but not in the other
// if no winwow was found, return nil
+(NSArray*)getDisposedWindowsBetween:(NSArray*)newWindowsInfos
and:(NSArray*)oldWindowsInfos
atTime:(NSTimeInterval)time
atClock:(uint64)clock
{
NSMutableArray* result = [NSMutableArray array];
for (int i=0; i<[oldWindowsInfos count];i++) {
VnrWindowInfo *info1 = [oldWindowsInfos objectAtIndex:i];
BOOL contains = NO;
for (VnrWindowInfo *info2 in newWindowsInfos) {
if([info1 windowID]==[info2 windowID]){
contains = YES;
break;
}
}
if(!contains){
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:info1 atLayerNumber:i forEventType:vnrWindowDisappeared atTimestamp:time atClock:clock andAllWindows:newWindowsInfos];
[result addObject:event];
}
}
if([result count]>0){
return result;
}
return nil;
}
// Method that returns an array of AYWindowSnapshot that are contained in one array but not in the other
// if no winwow was found, return nil
+(NSArray*)getOpenedWindowsBetween:(NSArray*)newWindowsInfos
and:(NSArray*)oldWindowsInfos
atTime:(NSTimeInterval)time
atClock:(uint64)clock
{
NSMutableArray* result = [NSMutableArray array];
for (int i=0; i<[oldWindowsInfos count];i++) {
VnrWindowInfo *info1 = [newWindowsInfos objectAtIndex:i];
BOOL contains = NO;
for (VnrWindowInfo *info2 in oldWindowsInfos) {
if([info1 windowID]==[info2 windowID]){
contains = YES;
break;
}
}
if(!contains){
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:info1 atLayerNumber:i forEventType:vnrWindowAppeared atTimestamp:time atClock:clock andAllWindows:newWindowsInfos];
[result addObject:event];
}
}
if([oldWindowsInfos count]==0 && [newWindowsInfos count]>0){
VnrWindowInfo *info1 = [newWindowsInfos objectAtIndex:0];
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:info1 atLayerNumber:0 forEventType:vnrWindowAppeared atTimestamp:time atClock:clock andAllWindows:newWindowsInfos];
[result addObject:event];
}
if([result count]>0){
return result;
}
return nil;
}
// Should be called only if same number of items
// and windows not reordered
// returns 0 if no window moving
// return the id of the moving window otherwise
+(WindowInfoEvent* )getIDofMovingWindowBetween:(NSArray*)newArray
and:(NSArray*)previousArray
atTime:(NSTimeInterval)time
atClock:(uint64)clock
{
NSInteger nbNew = [newArray count];
for(int i=0; i <nbNew;i++){
VnrWindowInfo* info1 =[newArray objectAtIndex:i];
VnrWindowInfo* info2 =[previousArray objectAtIndex:i];
CGPoint location1 =[info1 frame].origin;
CGPoint location2 =[info2 frame].origin;
if((location1.x!=location2.x)||(location1.y!=location2.y)){
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:[newArray objectAtIndex:i] atLayerNumber:i forEventType:vnrWindowMoved atTimestamp:time atClock:clock andAllWindows:newArray];
return event ;
}
}
return nil;
}
// Should be called only if same number of items
// and windows not reordered
// returns 0 if no window moving
// return the id of the moving window otherwise
+(WindowInfoEvent*)getIDofResizedWindowBetween:(NSArray*)newArray
and:(NSArray*)previousArray
atTime:(NSTimeInterval)time
atClock:(uint64)clock
{
NSInteger nbNew = [newArray count];
for(int i=0; i <nbNew;i++){
CGSize size1 =[((VnrWindowInfo *)[newArray objectAtIndex:i]) frame].size;
CGSize size2 =[((VnrWindowInfo *)[previousArray objectAtIndex:i]) frame].size;
if((size1.height!=size2.height)||(size1.width!=size2.width)){
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:[newArray objectAtIndex:i] atLayerNumber:i forEventType:vnrWindowResized atTimestamp:time atClock:clock andAllWindows:newArray];
return event ;
}
}
return nil;
}
/**
return true if the order between the two arrays differ
should be called only if same number of items, but still testing
and return nil if different size of arrays
empty array if same order
array with reordered snaps if reordered
@param two arrays of windowInfos we want to compare
@return true if the order between the two arrays differ
*/
+(NSArray*)orderDiffersBetween:(NSArray*)newArray
and:(NSArray*)previousArray
atTime:(NSTimeInterval)time
atClock:(uint64)clock
{
NSInteger nbNew = [newArray count];
NSInteger nbPrevious = [previousArray count];
NSMutableArray* reorderedSnapsList = [NSMutableArray array];
NSMutableArray* reorderedWindowId = [NSMutableArray array];
if(nbNew!=nbPrevious){
return nil;
}
else {
for(int i=0; i<nbNew;i++){
int idNew = (int)[((VnrWindowInfo *)[newArray objectAtIndex:i]) windowID];
int idOld = (int)[((VnrWindowInfo *)[previousArray objectAtIndex:i]) windowID];
if(idNew!=idOld){
// case of a reorder
[reorderedWindowId addObject:[NSNumber numberWithInt:idNew]];
}
}
//at this point, we should have alist of window ID that were reordered
// we browse newsnapshot and feed the result array
for(int i=0; i<nbNew;i++){
VnrWindowInfo * wInfo = [newArray objectAtIndex:i];
for(NSNumber* numberId in reorderedWindowId){
if(((int)[wInfo windowID])==[numberId intValue]){
WindowInfoEvent* event = [[WindowInfoEvent alloc] initWith:wInfo atLayerNumber:i forEventType:vnrWindowReordered atTimestamp:time atClock:clock andAllWindows:newArray];
[reorderedSnapsList addObject:event];
break;
}
}
}
return reorderedSnapsList;
}
return reorderedSnapsList;
}
# pragma mark unused/deprecated below
//// return true if there is at least two snapshots with at least one different feature
//+(BOOL)differenceBetween:(NSArray*)newArray and:(NSArray*)previousArray{
// if(previousArray){
// NSEnumerator *otherEnum = [newArray objectEnumerator];
// for (AYWindowInfo *info1 in previousArray) {
//
// AYWindowInfo *info2 =[otherEnum nextObject];
// BOOL difference =[WindowTracker differenceBetweenWindow:info1 andWindow:info2];
// if (difference) {
// //We have found a pair of two different objects.
// return YES;
// }
// }
// return NO;
// }
// else {
// return YES;
// }
//}
//// return true if at least one of the feature of these snapshots differ
//+(BOOL)differenceBetweenWindow:(AYWindowInfo*)info1 andWindow:(AYWindowInfo*)info2{
// return (!CGPointEqualToPoint(info1.rect.origin,info2.rect.origin)||!CGSizeEqualToSize(info1.rect.size,info2.rect.size)||![info1.ownerApplication.localizedName isEqualToString:info2.ownerApplication.localizedName]);
//}
- (void)stop{
if ([_timer isValid]) {
[_timer invalidate];
}
_timer = nil;
}
@end