forked from n8gray/QLColorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathX11BridgeController.m
More file actions
552 lines (475 loc) · 17.3 KB
/
X11BridgeController.m
File metadata and controls
552 lines (475 loc) · 17.3 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#import "X11BridgeController.h"
#import "ForeignWindow.h"
#import "CGSPrivate.h"
#import <X11/Xmu/WinUtil.h> /* For XmuClientWindow */
#import <unistd.h>
/* #define XP_NO_X_HEADERS
#import <Xplugin.h> */
@implementation X11BridgeController
/*****************************************************************************
* Debugging and logging macros
*/
//#define DEBUG_X11
#ifdef DEBUG_X11
#define LOG(cmd) cmd
#else
#define LOG(cmd) do {} while(0)
#endif
//#define SILENT_X11
#ifdef SILENT_X11
#define ERR(cmd) do {} while(0)
#else
#define ERR(cmd) cmd
#endif
/***************************************************************************
** Plugin Methods
*/
- (NSString*) name { return @"X11 Bridge"; }
- (NSString*) description { return @"Integrate Desktop Manager with X11."; }
- (int) interfaceVersion { return 1; }
- (NSBundle*) preferencesBundle { return nil; }
/***************************************************************************
** Global variables (gasp!)
*/
DMController *mDMController;
WorkspaceController *mWSController;
Display *Disp;
Window Root;
NSMutableDictionary *x11_of_native, *native_of_x11;
Atom net_supported;
/* Atom net_supporting_wm_check; */
Atom net_current_desktop;
Atom net_number_of_desktops;
/* Atom net_desktop_names;
Atom net_desktop_layout; */
Atom net_wm_desktop;
/* Atom net_wm_state_sticky;
Atom wm_state; */
Atom native_window_id;
/*****************************************************************************
* Utility Macros
*/
#define SET_CARD32(win,atom,valuePtr) XChangeProperty(Disp, (win), \
(atom), XA_CARDINAL, 32, \
PropModeReplace, (unsigned char *)(valuePtr), 1)
#define SET_DESKTOP(n) \
do { unsigned int v = (n); \
SET_CARD32( Root, net_current_desktop, &v ); } while (0)
#define SET_WIN_DESKTOP(win,n) \
do { unsigned int v = (n); \
SET_CARD32( (win), net_wm_desktop, &v ); } while (0)
/*****************************************************************************
* X11 Error handlers
*/
/* Define our own error handlers that don't crash DM */
int xErrHandler( Display *d, XErrorEvent *error ) {
char message[1024];
XGetErrorText(d, error->error_code, message, 1024);
message[1023] = '\0';
ERR(NSLog( @"%s", message ));
return 0;
}
/*
* For some reason this often gets called twice when the server dies.
* Surprisingly, it all works out ok despite that.
*/
int ioErrHandler( Display *d ) {
Disp = NULL;
ERR(NSLog( @"X11BridgeController: I/O Error. Perhaps the X Server Died.\n" ));
@throw( @"X11 IO Error" );
}
/*****************************************************************************
* Utility functions
*/
/*
* Intern all the atoms we'll need
*/
void intern_atoms()
{
net_supported =
XInternAtom(Disp, "_NET_SUPPORTED", False);
/* net_supporting_wm_check =
XInternAtom(Disp, "_NET_SUPPORTING_WM_CHECK", False); */
net_current_desktop =
XInternAtom(Disp, "_NET_CURRENT_DESKTOP", False);
net_number_of_desktops =
XInternAtom(Disp, "_NET_NUMBER_OF_DESKTOPS", False);
/* net_desktop_names =
XInternAtom(Disp, "_NET_DESKTOP_NAMES", False);
net_desktop_layout =
XInternAtom(Disp, "_NET_DESKTOP_LAYOUT", False);
net_wm_state_sticky =
XInternAtom(Disp, "_NET_WM_STATE_STICKY", False);
*/
net_wm_desktop =
XInternAtom(Disp, "_NET_WM_DESKTOP", False);
/* wm_state =
XInternAtom(Disp, "WM_STATE", False); */
/* This atom is supported by X11.app and XDarwin.app, which is very good
for us! */
native_window_id =
XInternAtom(Disp, "_NATIVE_WINDOW_ID", False);
}
Window x11_window_of_native_window( CGSWindow w_native ) {
NSValue *v_native =
[NSValue value:&w_native withObjCType:@encode(CGSWindow)];
NSValue *v_x11 = [x11_of_native objectForKey:v_native];
/* It's a shame this doesn't work.
{
xp_window_id winid;
xp_bool status;
status = xp_lookup_native_window( (unsigned int)w_native, &winid );
LOG(NSLog(@"xp_lookup_native_window: lookup %u -> %d, 0x%x\n",
(unsigned int)w_native, status, (unsigned int)winid));
} */
if (!v_x11)
return 0;
Window w;
[v_x11 getValue:(void *)(&w)];
return w;
}
CGSWindow native_window_of_x11_window( Window w ) {
Window w_dummy, *wins, found=(Window)0, parent, w_orig = w;
unsigned int n_wins;
Atom type;
int format;
unsigned long n_items, bytes_after;
CGSWindow *win_id_ptr;
XGrabServer(Disp);
/* It's a shame this doesn't work.
{
unsigned int natid;
xp_bool status;
status = xp_get_native_window( (xp_window_id)w, &natid );
LOG(NSLog(@"xp_get_native_window: lookup 0x%x -> %d, %d\n",
(unsigned int)w, status, (unsigned int)natid));
} */
for (; w != Root && !found; w = parent) {
if (XQueryTree( Disp, w, &w_dummy, &parent, &wins, &n_wins )) {
if (n_wins)
XFree(wins);
n_items = 0;
//NSLog(@"0x%x", wins[i]);
XGetWindowProperty( Disp, parent, native_window_id, 0, 1, False,
AnyPropertyType, &type, &format, &n_items,
&bytes_after, (unsigned char **)(&win_id_ptr) );
if (n_items) {
/* native_window_id property was read */
//NSLog(@"= %d", *win_id_ptr);
found = *win_id_ptr;
XFree(win_id_ptr);
}
} else {
ERR(NSLog(@"X11BridgeController: XQueryTree failed\n"));
XUngrabServer(Disp);
return 0;
}
}
if (!found) {
LOG(NSLog(@"X11BridgeController: Failed to find native window for "
@"window 0x%x", w_orig));
}
XUngrabServer(Disp);
return found;
}
void add_to_window_map( Window w ) {
NSValue *n_native,
*n_x11 = [NSValue value:&w withObjCType:@encode(Window)];
if (![native_of_x11 objectForKey:n_x11]) {
CGSWindow native;
if ((native = native_window_of_x11_window( w ))) {
LOG(NSLog(@"Adding 0x%x <-> %d\n", w, native));
n_native = [NSValue value:&native withObjCType:@encode(CGSWindow)];
// Put the windows in both maps
[native_of_x11 setObject:n_native forKey:n_x11];
[x11_of_native setObject:n_x11 forKey:n_native];
}
}
}
void delete_from_window_map( Window w ) {
NSValue *n_native,
*n_x11 = [NSValue value:&w withObjCType:@encode(Window)];
LOG(NSLog(@"Deleting 0x%x\n", w));
n_native = [native_of_x11 objectForKey:n_x11];
if (n_native) {
[x11_of_native removeObjectForKey:n_native];
[native_of_x11 removeObjectForKey:n_x11];
}
}
int init_window_atoms(int nWorkspaces, int currentWorkspace) {
Window w, dummy_w, *wins;
unsigned int i,nwins;
Atom root_atoms[] = {net_wm_desktop, net_current_desktop,
net_number_of_desktops};
CGSConnection conn;
SET_CARD32(Root, net_number_of_desktops, &nWorkspaces);
SET_DESKTOP(currentWorkspace);
XChangeProperty(Disp, Root, net_supported, XA_ATOM, 32, PropModeAppend,
(unsigned char *)(&root_atoms[0]), 3);
XGrabServer( Disp );
if (!XQueryTree(Disp, Root, &dummy_w, &dummy_w, &wins, &nwins)) {
XUngrabServer( Disp );
return 0;
}
conn = _CGSDefaultConnection();
for (i=0; i<nwins; ++i) {
w = XmuClientWindow(Disp, wins[i]);
if ( w != wins[i] ) {
CGSWindow cw;
add_to_window_map( w );
cw = native_window_of_x11_window( w );
CGSGetWindowWorkspace(conn, cw, ¤tWorkspace);
SET_WIN_DESKTOP( w, currentWorkspace-1 );
XSelectInput(Disp, w, StructureNotifyMask);
LOG(NSLog( @"Initializing window 0x%x on desktop %d\n",
w, currentWorkspace-1 ));
} else {
LOG(NSLog( @"No client window for window 0x%x.\n",
(unsigned int)wins[i] ));
}
}
XUngrabServer(Disp);
if (nwins)
XFree(wins);
return 1;
}
/*****************************************************************************
* X11 Event Handlers
*/
// This handles windows that have been iconified then uniconified
void handle_MapNotify( XEvent *event ) {
XMapEvent *e = (XMapEvent *)event;
Window w = e->event;
if (w != Root) {
unsigned int currentWS = [mWSController currentWorkspaceIndex];
LOG(NSLog( @"X11BridgeController: MapNotify event for window 0x%x.\n",
(unsigned int)w ));
SET_WIN_DESKTOP( w, currentWS );
add_to_window_map( w );
} else {
/* LOG(NSLog( @"X11BridgeController: "
@"Ignoring Root MapNotify event for 0x%x.\n",
(unsigned int)e->window )) */
;
}
}
// This handles newly created or displayed windows
void handle_ReparentNotify( XEvent *event ) {
XReparentEvent *e = (XReparentEvent *)event;
Window w = e->window;
unsigned int currentWS = [mWSController currentWorkspaceIndex];
LOG(NSLog( @"ReparentNotify: New Parent = 0x%x, Window = 0x%x, "
@"override_redirect = %d\n",
e->parent, w, e->override_redirect ));
if ( e->parent != Root ) {
SET_WIN_DESKTOP( w, currentWS );
// We want to know when this window gets mapped/unmapped
XSelectInput(Disp, w, StructureNotifyMask);
// This may fail, but if it does then the MapNotify will get it.
// If it succeeds then it would have been too late to catch the
// MapNotify. Thus we (probably) need both.
add_to_window_map( w );
}
}
void handle_UnmapNotify( XEvent *event ) {
XUnmapEvent *e = (XUnmapEvent *)event;
Window w = e->event;
if ( w != Root ) {
LOG(NSLog( @"X11BridgeController: %s UnmapNotify event for window "
@"0x%x.\n",
(e->send_event ? "Synthetic" : ""), (unsigned int)w ));
XDeleteProperty( Disp, w, net_wm_desktop );
delete_from_window_map( w );
} else {
/* LOG(NSLog( @"X11BridgeController: "
@"Ignoring Root UnmapNotify event for 0x%x.\n",
(unsigned int)e->window )) */
;
}
}
/*****************************************************************************
* The X11 Monitor thread
*/
- (void) startX11MonitorThread: (id)argument {
char *dispName;
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
if (!XInitThreads()) {
ERR(NSLog(@"X11BridgeController: "
@"Couldn't initialize multithreaded Xlib.\n"));
return;
}
XSetErrorHandler(xErrHandler);
XSetIOErrorHandler(ioErrHandler);
/* XXX: We should allow another way to set this, like a file or a dialog */
if (!(dispName = getenv("DISPLAY")))
dispName = ":0";
x11_of_native = [NSMutableDictionary dictionaryWithCapacity:128];
native_of_x11 = [NSMutableDictionary dictionaryWithCapacity:128];
/* {
xp_error err;
err = xp_init( XP_IN_BACKGROUND );
if( !err )
LOG(NSLog(@"Error on xp_init: %u\n", err));
} */
while( 1 ) {
LOG(NSLog(@"Waiting for connection to X Server\n"));
Disp = NULL;
[x11_of_native removeAllObjects];
[native_of_x11 removeAllObjects];
@try {
while (!Disp) {
Disp = XOpenDisplay(dispName);
//NSLog(@"X11BridgeController: cannot open display\n");
if(!Disp)
sleep(1);
}
LOG(NSLog(@"Opened connection to X Server\n"));
Root = DefaultRootWindow(Disp);
/* Ask for notification whenever a child of the root window is
reparented, mapped, or unmapped */
XSelectInput(Disp, Root, SubstructureNotifyMask);
// Initialize the atoms on Root and windows that exist now.
intern_atoms();
if (!init_window_atoms( [mWSController workspaceCount],
[mWSController currentWorkspaceIndex] )) {
ERR(NSLog(@"X11BridgeController: Error setting up root atoms\n"));
return;
}
while ( 1 ) {
XEvent event;
NSAutoreleasePool *arp2 = [[NSAutoreleasePool alloc] init];
XNextEvent(Disp, &event);
XLockDisplay(Disp);
switch (event.type) {
case UnmapNotify:
handle_UnmapNotify(&event);
break;
case MapNotify:
handle_MapNotify(&event);
break;
case ReparentNotify:
handle_ReparentNotify(&event);
break;
default:
break;
}
XUnlockDisplay(Disp);
[arp2 release];
}
} @catch( id ex ) {
// If the server exits we should end up here.
;
}
// XCloseDisplay(Disp); We never do this voluntarily.
}
[arp release];
}
/*****************************************************************************
* DM Event handlers
*/
/*
* React to a change of workspace by setting the proper atom on X11's root
* window.
*/
- (void) setRootWorkspace: (NSNotification *)n {
int ws;
if (!Disp)
return;
ws = [mWSController currentWorkspaceIndex];
@try {
XLockDisplay( Disp );
SET_DESKTOP( ws );
XUnlockDisplay( Disp );
} @catch( id ex ) {
return;
}
}
- (void) handleWindowWarp: (NSNotification *)n {
ForeignWindow *fw;
NSString *owner;
if (!Disp)
return;
fw = [n object];
owner = [fw ownerName];
/* XXX: These should be in a plist */
if ( [owner compare:@"X11"] == NSOrderedSame
|| [owner compare:@"XDarwin"] == NSOrderedSame ) {
int ws_index = [fw workspaceNumber] - 1;
CGSWindow native_w = [fw windowNumber];
Window w = (Window)0;
LOG(NSLog(@"Window #%d (%@) warped to workspace %d.\n",
native_w, owner, ws_index));
@try {
XLockDisplay(Disp);
if (( w = x11_window_of_native_window(native_w) )) {
LOG(NSLog(@" -- X11 window: 0x%x\n", w));
SET_WIN_DESKTOP( w, ws_index );
} else {
ERR(NSLog(@"Couldn't convert native window %u to X11 window.\n",
(unsigned int)native_w));
}
XUnlockDisplay(Disp);
} @catch( id ex ) {
return;
}
}
}
#define X11_DESKTOP_STICKY 0xFFFFFFFFL
- (void) handleWindowSticky: (NSNotification *)n {
ForeignWindow *fw;
NSString *owner;
if (!Disp)
return;
fw = [n object];
owner = [fw ownerName];
if ( [owner compare:@"X11"] == NSOrderedSame
|| [owner compare:@"XDarwin"] == NSOrderedSame ) {
@try {
CGSWindow native_w = [fw windowNumber];
Window w = (Window)0;
XLockDisplay(Disp);
if (( w = x11_window_of_native_window(native_w) )) {
long desktop = X11_DESKTOP_STICKY;
LOG(NSLog(@" -- X11 window: 0x%x\n", w));
SET_WIN_DESKTOP( w, desktop );
} else {
ERR(NSLog(@"Couldn't convert native window %u to X11 window.\n",
(unsigned int)native_w));
}
XUnlockDisplay(Disp);
} @catch( id ex ) {
return;
}
}
}
- (void) pluginLoaded: (DMController*) controller withBundle: (NSBundle*) thisBundle {
mDMController = controller;
mWSController = [controller workspaceController];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// Start up the thread that monitors for X11 Events
[NSThread detachNewThreadSelector:@selector(startX11MonitorThread:)
toTarget:self
withObject:nil];
// Register for workspace switch events
// This needs to be done in this thread because notifications are always
// delivered in the same thread they're sent from.
[nc addObserver: self
selector: @selector(setRootWorkspace:)
name: NOTIFICATION_WORKSPACESELECTED object: nil];
// Register for window warp events
[nc addObserver: self
selector: @selector(handleWindowWarp:)
name: NOTIFICATION_WINDOWWARPED object: nil];
// Register for window sticky events
/* Actually, sticky windows work ok for now. They don't get
set to desktop 0xFFFFFFFF, but they always get set to the current
desktop. Later I should fix this, but it's a bit tricky.
[nc addObserver: self
selector: @selector(handleWindowSticky:)
name: NOTIFICATION_WINDOWSTICKY object: nil];
[nc addObserver: self
selector: @selector(handleWindowWarp:) // Yes, that's right
name: NOTIFICATION_WINDOWUNSTICKY object: nil];
*/
}
@end