-
Notifications
You must be signed in to change notification settings - Fork 0
/
PuckUIHandler.m
500 lines (380 loc) · 15.8 KB
/
PuckUIHandler.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//
// PuckUIHandler
// Puck
//
// Created by slex on 28/05/21.
#import "PuckUIHandler.h"
#import <XCBKit/XCBFrame.h>
#import "functions/Functions.h"
#import "utils/EncapsulatedWindow.h"
@implementation PuckUIHandler
@synthesize dockWindow;
@synthesize separatorWindow;
@synthesize clientList;
@synthesize puckUtils;
@synthesize connection;
@synthesize iconizedWindows;
@synthesize iconizedWindowsContainer;
- (instancetype)init
{
self = [super init];
if (self == nil)
{
NSLog(@"Unable to init...");
return nil;
}
iconizedWindows = [[NSMutableArray alloc] init];
return self;
}
- (id)initWithConnection:(XCBConnection*)aConnection
{
self = [super init];
if (self == nil)
{
NSLog(@"Unable to init...");
return nil;
}
connection = aConnection;
puckUtils = [[PuckUtils alloc] initWhitConnection:connection];
clientList = [puckUtils queryForNetClientList]; /** we have clientList in the connection too. it could be reused **/
iconizedWindows = [[NSMutableArray alloc] init];
int size = [puckUtils clientListSize];
for (int i = 0; i < size; ++i)
{
EncapsulatedWindow *encapsulatedWindow = [puckUtils encapsulateWindow:clientList[i]];
[puckUtils registerWindow:[encapsulatedWindow window]];
encapsulatedWindow = nil;
}
return self;
}
- (void)drawDock:(CGFloat)width andHeigth:(CGFloat)height
{
XCBScreen *screen = [[connection screens] objectAtIndex:0];
XCBWindow *rootWindow = [screen rootWindow];
XCBVisual *visual = [[XCBVisual alloc] initWithVisualId:[screen screen]->root_visual];
[visual setVisualTypeForScreen:screen];
uint32_t values[] = {[screen screen]->white_pixel, FRAMEMASK};
XCBCreateWindowTypeRequest *request = [[XCBCreateWindowTypeRequest alloc] initForWindowType:XCBWindowRequest];
[request setParentWindow:rootWindow];
[request setDepth:[screen screen]->root_depth];
[request setXPosition:[screen width] / 2 - width / 2];
[request setYPosition:[screen height] - height];
[request setWidth:width];
[request setHeight:height];
[request setBorderWidth:0];
[request setXcbClass:XCB_WINDOW_CLASS_INPUT_OUTPUT];
[request setValueMask:XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK];
[request setValueList:values];
XCBWindowTypeResponse *response = [connection createWindowForRequest:request registerWindow:NO];
dockWindow = [response window];
values[0] = 1;
values[1] = 0;
[dockWindow changeAttributes:values withMask:XCB_CW_OVERRIDE_REDIRECT checked:NO];
uint32_t val[] = {DOCKMASK};
[rootWindow changeAttributes:val withMask:XCB_CW_EVENT_MASK checked:NO];
[dockWindow stackAbove];
EWMHService *ewmhService = [EWMHService sharedInstanceWithConnection:connection];
[ewmhService updateNetWmState:dockWindow];
[ewmhService updateNetWmWindowTypeDockForWindow:dockWindow];
/*** Request for the iconized windows container ***/
response = nil;
[request setParentWindow:dockWindow];
[request setXPosition:width - 58];
[request setYPosition:height - 58];
[request setWidth:56];
[request setHeight:height];
values[0] = [screen screen]->white_pixel;
values[1] = FRAMEMASK;
response = [connection createWindowForRequest:request registerWindow:NO];
iconizedWindowsContainer = [response window];
[ewmhService updateNetWmWindowTypeDockForWindow:iconizedWindowsContainer];
void *windowTypeReply = [ewmhService getProperty:[ewmhService EWMHWMWindowType]
propertyType:XCB_ATOM_ATOM
forWindow:dockWindow
delete:NO
length:1];
if (windowTypeReply)
{
xcb_atom_t *atom = (xcb_atom_t *) xcb_get_property_value(windowTypeReply);
if (*atom == [[ewmhService atomService] atomFromCachedAtomsWithKey:[ewmhService EWMHWMWindowTypeDock]])
{
NSLog(@"SCIABOLATA MORBIDA %u", [dockWindow window]);
}
}
/*** Request for separator window ***/
response = nil;
[request setParentWindow:dockWindow];
[request setXPosition:width - 62];
[request setYPosition:height - 58];
[request setWidth:2];
[request setHeight:56];
values[0] = [screen screen]->black_pixel;
values[1] = FRAMEMASK;
response = [connection createWindowForRequest:request registerWindow:NO];
separatorWindow = [response window];
[connection mapWindow:dockWindow];
[connection mapWindow:iconizedWindowsContainer];
[connection mapWindow:separatorWindow];
[connection flush];
screen = nil;
request = nil;
response = nil;
visual = nil;
rootWindow = nil;
ewmhService = nil;
}
- (void)resizeToPosition:(XCBPoint)aPosition andSize:(XCBSize)aSize resize:(Resize)aResize
{
/*** for the main dockbar dockWindow we calculate the new size, while the position is given by the aPosition argument ***/
XCBSize newMainSize;
if (aResize == Enlarge)
newMainSize = XCBMakeSize([dockWindow windowRect].size.width + 50 + OFFSET * 2 + 3, aSize.height);
else
newMainSize = XCBMakeSize([dockWindow windowRect].size.width - 50 - OFFSET * 2 - 3, aSize.height);
[dockWindow maximizeToSize:newMainSize andPosition:aPosition];
[dockWindow setIsMaximized:NO];
[dockWindow setFullScreen:NO];
[dockWindow setMaximizedVertically:NO];
[dockWindow setMaximizedHorizontally:NO];
/*** for the iconified container we keep the same position and give to it the size from the aSize paramenter ***/
[iconizedWindowsContainer maximizeToSize:aSize andPosition:[iconizedWindowsContainer windowRect].position];
[iconizedWindowsContainer setIsMaximized:NO];
[iconizedWindowsContainer setFullScreen:NO];
[iconizedWindowsContainer setMaximizedVertically:NO];
[iconizedWindowsContainer setMaximizedHorizontally:NO];
}
- (void)addToIconizedWindowsContainer:(XCBWindow*)aWindow
{
NSLog(@"Adding window %u", [aWindow window]);
BOOL needResize = NO;
if ([iconizedWindows count] != 0)
needResize = YES;
[iconizedWindows addObject:aWindow];
[aWindow setIsMinimized:YES];
NSLog(@"Frame %u minimized %d", [aWindow window], [aWindow isMinimized]);
if (needResize)
{
XCBRect rect = [iconizedWindowsContainer windowRect];
XCBPoint newMainWindowPos = XCBMakePoint(([dockWindow windowRect].position.x - 50) + OFFSET * 2 + 3, [dockWindow windowRect].position.y); //TODO: CHECK If bette + OFFSET etc etc or - OFFSET etc etc
XCBSize newContainerWindowSize = XCBMakeSize(rect.size.width + 50 + OFFSET * 2 + 3, rect.size.height);
[self resizeToPosition:newMainWindowPos andSize:newContainerWindowSize resize:Enlarge];
XCBPoint repPos = XCBMakePoint([iconizedWindowsContainer windowRect].size.width - 50 - OFFSET, 0);
[connection reparentWindow:aWindow toWindow:iconizedWindowsContainer position:repPos];
[connection flush];
}
else
[connection reparentWindow:aWindow toWindow:iconizedWindowsContainer position:XCBMakePoint(0,0)];
}
- (void)removeFromIconizedWindowsById:(xcb_window_t)winId
{
int size = [iconizedWindows count];
for (int i = 0; i < size; ++i)
{
XCBWindow *win = [iconizedWindows objectAtIndex:i];
if ([win window] == winId)
{
[iconizedWindows removeObjectAtIndex:i];
win = nil;
break;
}
win = nil;
}
}
- (XCBWindow *)windowFromIconizedById:(xcb_window_t)winId
{
XCBWindow *win;
int size = [iconizedWindows count];
for (int i = 0; i < size; ++i)
{
win = [iconizedWindows objectAtIndex:i];
if ([win window] == winId)
return win;
win = nil;
}
return win;
}
- (BOOL)isIconizedInFirstOrLastPosition:(XCBWindow *)aWindow
{
XCBWindow *first;
XCBWindow *last;
BOOL firstOrLast = NO;
first = [iconizedWindows firstObject];
last = [iconizedWindows lastObject];
if ([first window] == [aWindow window] || [last window] == [aWindow window])
firstOrLast = YES;
return firstOrLast;
}
- (BOOL)isFollowedByAnotherWindow:(XCBWindow *)originWindow
{
BOOL followed = NO;
int arraySize = [iconizedWindows count];
if (arraySize == 1)
return followed;
if (arraySize > 1)
{
for (int i = 0; i < arraySize; ++i)
{
XCBWindow *auxWin = [iconizedWindows objectAtIndex:i];
XCBWindow *lastWindow = [iconizedWindows lastObject];
if ([originWindow window] == [auxWin window] && [originWindow window] != [lastWindow window])
{
XCBWindow *following = [iconizedWindows objectAtIndex:i+1];
if (following != nil)
{
followed = YES;
following = nil;
auxWin = nil;
lastWindow = nil;
break;
}
}
else if ([originWindow window] == [lastWindow window])
{
auxWin = nil;
lastWindow = nil;
return followed;
}
auxWin = nil;
lastWindow = nil;
}
}
return followed;
}
- (NSInteger) countFollowingWindowsForWindow:(XCBWindow *)aWindow
{
NSInteger following = 0;
int arraySize = [iconizedWindows count];
XCBWindow *lastWindow = [iconizedWindows lastObject];
if (arraySize == 1 || [aWindow window] == [lastWindow window])
{
lastWindow = nil;
return following;
}
if (arraySize > 1)
{
for (int i = 0; i < arraySize; ++i)
{
XCBWindow *auxWin = [iconizedWindows objectAtIndex:i];
if ([aWindow window] == [auxWin window])
{
following = arraySize - (i + 1);
auxWin = nil;
break;
}
}
}
lastWindow = nil;
return following;
}
- (void) moveFollowingWindows:(NSInteger) followingQuantity forWindow:(XCBWindow *)originWindow
{
NSInteger winPosition = FnIndexOfWindow(iconizedWindows, originWindow);
for (int i = 0; i < followingQuantity; ++i)
{
winPosition++;
XCBWindow *moving = [iconizedWindows objectAtIndex:winPosition];
XCBPoint newPosition = FnCalculateNewPosition(moving, OFFSET);
[self moveWindow:moving toPosition:newPosition];
moving = nil;
}
}
- (void)moveWindow:(XCBWindow *)aWindow toPosition:(XCBPoint)aPosition
{
int32_t values[] = {aPosition.x, aPosition.y};
xcb_configure_window([connection connection], [aWindow window], XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, values);
XCBRect newRect = XCBMakeRect(aPosition, XCBMakeSize([aWindow windowRect].size.width, [aWindow windowRect].size.height));
[aWindow setWindowRect:newRect];
[aWindow setOriginalRect:XCBMakeRect(XCBMakePoint(aPosition.x, aPosition.y),
XCBMakeSize([aWindow originalRect].size.width,
[aWindow originalRect].size.height))];
}
- (NSString *)description
{
NSMutableString *description = [NSMutableString new];
int size = [puckUtils clientListSize];
for (int i = 0; i < size; i++)
{
NSString *str = [NSString stringWithFormat:@"%u", clientList[i]];
[description appendString:str];
if ((i+1) != size)
[description appendString:@" "];
str = nil;
}
return description;
}
/*** TODO: EVALUATE IF I CAN USE UPDATEcLIENTlIST DIRECTLY IN THE INIT METHOD ***/
- (void)updateClientList
{
if (clientList)
free(clientList);
clientList = [puckUtils queryForNetClientList];
/*[[connection windowsMap] removeAllObjects];
int size = [puckUtils clientListSize];
for (int i = 0; i < size; ++i)
if (clientList[i] != 0)
[puckUtils encapsulateWindow:clientList[i]];*/
}
- (void)resize:(BOOL)firstOrLastPos needResize:(BOOL)needResize withFrame:(XCBFrame*)aFrame
{
NSInteger followingWinsCount = [self countFollowingWindowsForWindow:aFrame];
/*** if forl, is followed and need resize, then it is in first position! ***/
if (needResize && [self isFollowedByAnotherWindow:aFrame] && firstOrLastPos)
{
[self moveFollowingWindows:followingWinsCount forWindow:aFrame];
[self removeFromIconizedWindowsById:[aFrame window]];
[aFrame setIsMinimized:NO];
/*** resize the dock ***/
XCBRect iconizedContainerRect = [iconizedWindowsContainer windowRect];
XCBPoint newMainWindowPos = XCBMakePoint(([dockWindow windowRect].position.x + 50) - OFFSET * 2 - 3, [dockWindow windowRect].position.y);
XCBSize newIconizedContainerSize = XCBMakeSize(iconizedContainerRect.size.width - 50 - OFFSET * 2 - 3, iconizedContainerRect.size.height);
[self resizeToPosition:newMainWindowPos andSize:newIconizedContainerSize resize:Reduce];
[connection flush];
needResize = NO;
}
/*** if forl and is not followed by other windows then it is in first position so just remove the window without resizing the dockbar ***/
if (!needResize && ![self isFollowedByAnotherWindow:aFrame] && firstOrLastPos)
{
[self removeFromIconizedWindowsById:[aFrame window]];
[aFrame setIsMinimized:NO];
[connection flush];
needResize = NO;
}
/*** if forl and need resize and the window is not followed by other windows then it is in last position, so resize the dockbar ***/
if (firstOrLastPos && needResize && ![self isFollowedByAnotherWindow:aFrame])
{
[self removeFromIconizedWindowsById:[aFrame window]];
[aFrame setIsMinimized:NO];
/*** resize the dock ***/
XCBRect iconizedContainerRect = [iconizedWindowsContainer windowRect];
XCBPoint newMainWindowPos = XCBMakePoint(([dockWindow windowRect].position.x + 50) - OFFSET * 2 - 3, [dockWindow windowRect].position.y);
XCBSize newIconizedContainerSize = XCBMakeSize(iconizedContainerRect.size.width - 50 - OFFSET * 2 - 3, iconizedContainerRect.size.height);
[self resizeToPosition:newMainWindowPos andSize:newIconizedContainerSize resize:Reduce];
[connection flush];
needResize = NO;
}
/*** if not forl, is followed and need resize then the window is in the middle; resize the dockbar and move the followers ***/
if (!firstOrLastPos && needResize && [self isFollowedByAnotherWindow:aFrame])
{
[self moveFollowingWindows:followingWinsCount forWindow:aFrame];
[self removeFromIconizedWindowsById:[aFrame window]];
[aFrame setIsMinimized:NO];
/*** resize the dock ***/
XCBRect iconizedContainerRect = [iconizedWindowsContainer windowRect];
XCBPoint newMainWindowPos = XCBMakePoint(([dockWindow windowRect].position.x + 50) - OFFSET * 2 - 3, [dockWindow windowRect].position.y);
XCBSize newIconizedContainerSize = XCBMakeSize(iconizedContainerRect.size.width - 50 - OFFSET * 2 - 3, iconizedContainerRect.size.height);
[self resizeToPosition:newMainWindowPos andSize:newIconizedContainerSize resize:Reduce];
[connection flush];
}
}
- (void)dealloc
{
connection = nil;
dockWindow = nil;
iconizedWindowsContainer = nil;
iconizedWindows = nil;
puckUtils = nil;
separatorWindow = nil;
if (clientList)
free(clientList); // is pure C, but inside there are unsigned int values
}
@end