|
@@ -2,14 +2,11 @@
|
2
|
2
|
|
3
|
3
|
slop::XEngine* xengine = new slop::XEngine();
|
4
|
4
|
|
5
|
|
-static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
|
6
|
|
- return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
|
7
|
|
-}
|
8
|
|
-
|
9
|
5
|
int slop::XEngineErrorHandler( Display* dpy, XErrorEvent* event ) {
|
10
|
6
|
// Ignore XGrabKeyboard BadAccess errors, we can work without it.
|
11
|
7
|
// 31 = XGrabKeyboard's request code
|
12
|
8
|
if ( event->request_code == 31 && event->error_code == BadAccess ) {
|
|
9
|
+ fprintf( stderr, "_X Error \"BadAccess\" for XGrabKeyboard ignored...\n" );
|
13
|
10
|
return 0;
|
14
|
11
|
}
|
15
|
12
|
// Everything else should be fatal as I don't like undefined behavior.
|
|
@@ -42,28 +39,9 @@ slop::XEngine::~XEngine() {
|
42
|
39
|
XFreeCursor( m_display, m_cursors[i] );
|
43
|
40
|
}
|
44
|
41
|
}
|
45
|
|
- for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
46
|
|
- delete m_rects.at( i );
|
47
|
|
- }
|
48
|
42
|
XCloseDisplay( m_display );
|
49
|
43
|
}
|
50
|
44
|
|
51
|
|
-// We need to keep track of the rectangle windows, so that they don't override our "focus"d windows.
|
52
|
|
-void slop::XEngine::addRect( Rectangle* rect ) {
|
53
|
|
- m_rects.push_back( rect );
|
54
|
|
-}
|
55
|
|
-
|
56
|
|
-void slop::XEngine::removeRect( Rectangle* rect ) {
|
57
|
|
- for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
58
|
|
- if ( m_rects.at( i ) == rect ) {
|
59
|
|
- m_rects.erase( m_rects.begin() + i );
|
60
|
|
- i--;
|
61
|
|
- delete rect;
|
62
|
|
- return;
|
63
|
|
- }
|
64
|
|
- }
|
65
|
|
-}
|
66
|
|
-
|
67
|
45
|
bool slop::XEngine::mouseDown( unsigned int button ) {
|
68
|
46
|
if ( button >= m_mouse.size() ) {
|
69
|
47
|
return false;
|
|
@@ -86,6 +64,7 @@ int slop::XEngine::init( std::string display ) {
|
86
|
64
|
|
87
|
65
|
m_good = true;
|
88
|
66
|
XSetErrorHandler( slop::XEngineErrorHandler );
|
|
67
|
+ selectAllInputs( m_root, EnterWindowMask );
|
89
|
68
|
return 0;
|
90
|
69
|
}
|
91
|
70
|
|
|
@@ -111,7 +90,13 @@ int slop::XEngine::grabKeyboard() {
|
111
|
90
|
if ( !m_good ) {
|
112
|
91
|
return 1;
|
113
|
92
|
}
|
114
|
|
- XGrabKeyboard( m_display, m_root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
|
|
93
|
+ int err = XGrabKeyboard( m_display, m_root, False, GrabModeAsync, GrabModeAsync, CurrentTime );
|
|
94
|
+ if ( err != GrabSuccess ) {
|
|
95
|
+ fprintf( stderr, "Warning: Failed to grab X keyboard.\n" );
|
|
96
|
+ fprintf( stderr, " This happens when something's already grabbed your keybaord.\n" );
|
|
97
|
+ fprintf( stderr, " slop should still run properly though.\n" );
|
|
98
|
+ return 1;
|
|
99
|
+ }
|
115
|
100
|
return 0;
|
116
|
101
|
}
|
117
|
102
|
|
|
@@ -123,22 +108,31 @@ int slop::XEngine::releaseKeyboard() {
|
123
|
108
|
return 0;
|
124
|
109
|
}
|
125
|
110
|
|
|
111
|
+void slop::XEngine::selectAllInputs( Window win, long event_mask) {
|
|
112
|
+ Window root, parent;
|
|
113
|
+ Window* children;
|
|
114
|
+ unsigned int nchildren;
|
|
115
|
+ XQueryTree( m_display, win, &root, &parent, &children, &nchildren );
|
|
116
|
+ for ( unsigned int i=0;i<nchildren;i++ ) {
|
|
117
|
+ XSelectInput( m_display, children[ i ], event_mask );
|
|
118
|
+ selectAllInputs( children[ i ], event_mask );
|
|
119
|
+ }
|
|
120
|
+}
|
|
121
|
+
|
126
|
122
|
// Grabs the cursor, be wary that setCursor changes the mouse masks.
|
127
|
123
|
int slop::XEngine::grabCursor( slop::CursorType type ) {
|
128
|
124
|
if ( !m_good ) {
|
129
|
125
|
return 1;
|
130
|
126
|
}
|
131
|
127
|
int xfontcursor = getCursor( type );
|
132
|
|
- int err = XGrabPointer( m_display, m_root, False,
|
133
|
|
- PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
|
134
|
|
- GrabModeAsync, GrabModeAsync, m_root, xfontcursor, CurrentTime );
|
|
128
|
+ int err = XGrabPointer( m_display, m_root, True,
|
|
129
|
+ PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask,
|
|
130
|
+ GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
|
135
|
131
|
if ( err != GrabSuccess ) {
|
136
|
132
|
fprintf( stderr, "Error: Failed to grab X cursor.\n" );
|
137
|
|
- fprintf( stderr, "This can be caused by launching slop incorrectly.\n" );
|
138
|
|
- fprintf( stderr, "gnome-session launches it fine from keyboard binds.\n" );
|
|
133
|
+ fprintf( stderr, " This can be caused by launching slop weirdly.\n" );
|
139
|
134
|
return 1;
|
140
|
135
|
}
|
141
|
|
-
|
142
|
136
|
// Quickly set the mouse position so we don't have to worry about x11 generating an event.
|
143
|
137
|
Window root, child;
|
144
|
138
|
int mx, my;
|
|
@@ -148,8 +142,13 @@ int slop::XEngine::grabCursor( slop::CursorType type ) {
|
148
|
142
|
m_mousex = mx;
|
149
|
143
|
m_mousey = my;
|
150
|
144
|
|
151
|
|
- // Oh and while we're at it, make sure we set the window we're hoving over as well.
|
152
|
|
- updateHoverWindow( child );
|
|
145
|
+ // Get the deepest available window.
|
|
146
|
+ Window test = child;
|
|
147
|
+ while( test ) {
|
|
148
|
+ child = test;
|
|
149
|
+ XQueryPointer( m_display, child, &root, &test, &mx, &my, &wx, &wy, &mask );
|
|
150
|
+ }
|
|
151
|
+ m_hoverWindow = child;
|
153
|
152
|
return 0;
|
154
|
153
|
}
|
155
|
154
|
|
|
@@ -185,6 +184,17 @@ void slop::XEngine::tick() {
|
185
|
184
|
}
|
186
|
185
|
break;
|
187
|
186
|
}
|
|
187
|
+ case EnterNotify: {
|
|
188
|
+ if ( event.xcrossing.subwindow != None ) {
|
|
189
|
+ m_hoverWindow = event.xcrossing.subwindow;
|
|
190
|
+ } else {
|
|
191
|
+ m_hoverWindow = event.xcrossing.window;
|
|
192
|
+ }
|
|
193
|
+ break;
|
|
194
|
+ }
|
|
195
|
+ case LeaveNotify: {
|
|
196
|
+ break;
|
|
197
|
+ }
|
188
|
198
|
case ButtonRelease: {
|
189
|
199
|
if ( m_mouse.size() > event.xbutton.button ) {
|
190
|
200
|
m_mouse.at( event.xbutton.button ) = false;
|
|
@@ -204,9 +214,6 @@ void slop::XEngine::tick() {
|
204
|
214
|
default: break;
|
205
|
215
|
}
|
206
|
216
|
}
|
207
|
|
-
|
208
|
|
- // Since I couldn't get Xlib to send EnterNotify or LeaveNotify events, we need to query the underlying window every frame.
|
209
|
|
- updateHoverWindow();
|
210
|
217
|
}
|
211
|
218
|
|
212
|
219
|
// This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
|
|
@@ -246,31 +253,37 @@ void slop::XEngine::setCursor( slop::CursorType type ) {
|
246
|
253
|
}
|
247
|
254
|
|
248
|
255
|
void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
|
249
|
|
- Window junk;
|
250
|
256
|
if ( decorations ) {
|
|
257
|
+ Window root, parent, test, junk;
|
|
258
|
+ Window* childlist;
|
|
259
|
+ unsigned int ujunk;
|
251
|
260
|
unsigned int depth;
|
252
|
|
- XGetGeometry( xengine->m_display, win, &junk,
|
253
|
|
- &(m_x), &(m_y),
|
254
|
|
- &(m_width), &(m_height),
|
255
|
|
- &(m_border), &depth );
|
256
|
|
- // We make sure we include borders, since we want decorations.
|
257
|
|
- m_width += m_border * 2;
|
258
|
|
- m_height += m_border * 2;
|
259
|
|
- m_decorations = true;
|
|
261
|
+ // Try to find the actual decorations.
|
|
262
|
+ test = win;
|
|
263
|
+ int status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
|
|
264
|
+ while( parent != root ) {
|
|
265
|
+ if ( !parent || !status ) {
|
|
266
|
+ break;
|
|
267
|
+ }
|
|
268
|
+ test = parent;
|
|
269
|
+ status = XQueryTree( xengine->m_display, test, &root, &parent, &childlist, &ujunk);
|
|
270
|
+ }
|
|
271
|
+ // Once found, proceed normally.
|
|
272
|
+ if ( test && parent == root && status ) {
|
|
273
|
+ XWindowAttributes attr;
|
|
274
|
+ XGetWindowAttributes( xengine->m_display, test, &attr );
|
|
275
|
+ m_width = attr.width;
|
|
276
|
+ m_height = attr.height;
|
|
277
|
+ m_border = attr.border_width;
|
|
278
|
+ XTranslateCoordinates( xengine->m_display, test, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
|
|
279
|
+ // We make sure we include borders, since we want decorations.
|
|
280
|
+ m_width += m_border * 2;
|
|
281
|
+ m_height += m_border * 2;
|
|
282
|
+ }
|
260
|
283
|
return;
|
261
|
284
|
}
|
262
|
|
- Window root;
|
263
|
|
- Window* children = NULL;
|
264
|
|
- unsigned int childcount;
|
265
|
|
- // Try to get the first child of the specified window, to avoid decorations.
|
266
|
|
- XQueryTree( xengine->m_display, win, &root, &junk, &children, &childcount );
|
267
|
|
- if ( childcount == 1 && children ) {
|
268
|
|
- win = children[ 0 ];
|
269
|
|
- m_decorations = false;
|
270
|
|
- } else {
|
271
|
|
- //fprintf( stderr, "Warning: slop couldn't determine how to remove decorations, continuing without removing decorations...\n" );
|
272
|
|
- m_decorations = true;
|
273
|
|
- }
|
|
285
|
+ Window junk;
|
|
286
|
+ // Now here we should be able to just use whatever we get.
|
274
|
287
|
XWindowAttributes attr;
|
275
|
288
|
// We use XGetWindowAttributes to know our root window.
|
276
|
289
|
XGetWindowAttributes( xengine->m_display, win, &attr );
|
|
@@ -281,201 +294,3 @@ void slop::WindowRectangle::setGeometry( Window win, bool decorations ) {
|
281
|
294
|
m_border = attr.border_width;
|
282
|
295
|
XTranslateCoordinates( xengine->m_display, win, attr.root, -attr.border_width, -attr.border_width, &(m_x), &(m_y), &junk );
|
283
|
296
|
}
|
284
|
|
-
|
285
|
|
-slop::Rectangle::~Rectangle() {
|
286
|
|
- //XFreeGC( xengine->m_display, m_gc );
|
287
|
|
- if ( m_window == None ) {
|
288
|
|
- return;
|
289
|
|
- }
|
290
|
|
- //XFreeColors( xengine->m_display, xengine->m_colormap, m_color.pixel, 1,
|
291
|
|
- // Attempt to move window offscreen before trying to remove it.
|
292
|
|
- //XResizeWindow( xengine->m_display, m_window, 1, 1 );
|
293
|
|
- //XMoveWindow( xengine->m_display, m_window, 0, 0 );
|
294
|
|
- //XUnmapWindow( xengine->m_display, m_window );
|
295
|
|
- XDestroyWindow( xengine->m_display, m_window );
|
296
|
|
- XEvent event;
|
297
|
|
- // Block until the window is actually completely removed.
|
298
|
|
- XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
|
299
|
|
-}
|
300
|
|
-
|
301
|
|
-slop::Rectangle::Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b ) {
|
302
|
|
- m_xoffset = 0;
|
303
|
|
- m_yoffset = 0;
|
304
|
|
- m_x = x;
|
305
|
|
- m_y = y;
|
306
|
|
- m_width = width;
|
307
|
|
- m_height = height;
|
308
|
|
- m_border = border;
|
309
|
|
- m_padding = padding;
|
310
|
|
- m_window = None;
|
311
|
|
-
|
312
|
|
- // Convert the width, height, x, and y to coordinates that don't have negative values.
|
313
|
|
- // (also adjust for padding and border size.)
|
314
|
|
- constrain( width, height );
|
315
|
|
- // If we don't have a border, we don't exist, so just die.
|
316
|
|
- if ( m_border == 0 ) {
|
317
|
|
- return;
|
318
|
|
- }
|
319
|
|
-
|
320
|
|
- // This sets up m_color
|
321
|
|
- int err = convertColor( r, g, b );
|
322
|
|
- if ( err ) {
|
323
|
|
- fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
|
324
|
|
- }
|
325
|
|
- XSetWindowAttributes attributes;
|
326
|
|
- // Set up the window so it's our color
|
327
|
|
- attributes.background_pixmap = None;
|
328
|
|
- attributes.background_pixel = m_color.pixel;
|
329
|
|
- // Not actually sure what this does, but it keeps the window from bugging out :u.
|
330
|
|
- attributes.override_redirect = True;
|
331
|
|
- // We must use our color map, because that's where our color is allocated.
|
332
|
|
- attributes.colormap = xengine->m_colormap;
|
333
|
|
- // Make sure we know when we've been successfully destroyed later!
|
334
|
|
- attributes.event_mask = StructureNotifyMask;
|
335
|
|
- unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
|
336
|
|
-
|
337
|
|
- // Create the window offset by our generated offsets (see constrain( float, float ))
|
338
|
|
- m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border, m_width+m_border*2, m_height+m_border*2,
|
339
|
|
- 0, CopyFromParent, InputOutput,
|
340
|
|
- CopyFromParent, valueMask, &attributes );
|
341
|
|
-
|
342
|
|
- // Now punch a hole into it so it looks like a selection rectangle!
|
343
|
|
- XRectangle rect;
|
344
|
|
- rect.x = rect.y = m_border;
|
345
|
|
- rect.width = m_width;
|
346
|
|
- rect.height = m_height;
|
347
|
|
-
|
348
|
|
- XClassHint classhints;
|
349
|
|
- char name[] = "slop";
|
350
|
|
- classhints.res_name = name;
|
351
|
|
- classhints.res_class = name;
|
352
|
|
- XSetClassHint( xengine->m_display, m_window, &classhints );
|
353
|
|
-
|
354
|
|
- XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
|
355
|
|
- XMapWindow( xengine->m_display, m_window );
|
356
|
|
-}
|
357
|
|
-
|
358
|
|
-void slop::Rectangle::setPos( int x, int y ) {
|
359
|
|
- if ( m_x == x && m_y == y ) {
|
360
|
|
- return;
|
361
|
|
- }
|
362
|
|
- m_x = x;
|
363
|
|
- m_y = y;
|
364
|
|
- // If we don't have a border, we don't exist, so just die.
|
365
|
|
- if ( m_border == 0 ) {
|
366
|
|
- return;
|
367
|
|
- }
|
368
|
|
- XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
|
369
|
|
-}
|
370
|
|
-
|
371
|
|
-void slop::Rectangle::setDim( int w, int h ) {
|
372
|
|
- if ( m_width == w && m_height == h ) {
|
373
|
|
- return;
|
374
|
|
- }
|
375
|
|
-
|
376
|
|
- constrain( w, h );
|
377
|
|
- // If we don't have a border, we don't exist, so just die.
|
378
|
|
- if ( m_border == 0 ) {
|
379
|
|
- return;
|
380
|
|
- }
|
381
|
|
-
|
382
|
|
- // Change the window size and location to our generated offsets (see constrain( float, float ))
|
383
|
|
- XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
|
384
|
|
- XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset-m_border, m_y+m_yoffset-m_border );
|
385
|
|
- // Regenerate our hole
|
386
|
|
- XRectangle rect;
|
387
|
|
- rect.x = rect.y = 0;
|
388
|
|
- rect.width = m_width+m_border*2;
|
389
|
|
- rect.height = m_height+m_border*2;
|
390
|
|
- XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
|
391
|
|
- // Then punch out another.
|
392
|
|
- rect.x = rect.y = m_border;
|
393
|
|
- rect.width = m_width;
|
394
|
|
- rect.height = m_height;
|
395
|
|
- XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
|
396
|
|
-}
|
397
|
|
-
|
398
|
|
-void slop::XEngine::updateHoverWindow() {
|
399
|
|
- Window root, hoverwin;
|
400
|
|
- int mx, my;
|
401
|
|
- int wx, wy;
|
402
|
|
- unsigned int mask;
|
403
|
|
- // Query the pointer for the child window, the child window is basically the window we're hovering over.
|
404
|
|
- XQueryPointer( m_display, m_root, &root, &hoverwin, &mx, &my, &wx, &wy, &mask );
|
405
|
|
- // If we already know that we're hovering over it, do nothing.
|
406
|
|
- if ( m_hoverWindow == hoverwin ) {
|
407
|
|
- return;
|
408
|
|
- }
|
409
|
|
- // Make sure we can't select one of our selection rectangles, that's just weird.
|
410
|
|
- for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
411
|
|
- if ( m_rects.at( i )->m_window == hoverwin ) {
|
412
|
|
- return;
|
413
|
|
- }
|
414
|
|
- }
|
415
|
|
- m_hoverWindow = hoverwin;
|
416
|
|
-}
|
417
|
|
-
|
418
|
|
-void slop::XEngine::updateHoverWindow( Window hoverwin ) {
|
419
|
|
- // Same thing as updateHoverWindow but it uses the specified child.
|
420
|
|
- // It's used when we first grab the cursor so it's slightly more effecient
|
421
|
|
- // than calling XQueryPointer twice.
|
422
|
|
- if ( m_hoverWindow == hoverwin ) {
|
423
|
|
- return;
|
424
|
|
- }
|
425
|
|
- for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
426
|
|
- if ( m_rects.at( i )->m_window == hoverwin ) {
|
427
|
|
- return;
|
428
|
|
- }
|
429
|
|
- }
|
430
|
|
- m_hoverWindow = hoverwin;
|
431
|
|
-}
|
432
|
|
-
|
433
|
|
-// Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
|
434
|
|
-// It also keeps our values in absolute coordinates which is nice.
|
435
|
|
-void slop::Rectangle::constrain( int w, int h ) {
|
436
|
|
- int pad = m_padding;
|
437
|
|
- if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
|
438
|
|
- pad = 0;
|
439
|
|
- }
|
440
|
|
- if ( w < 0 ) {
|
441
|
|
- m_flippedx = true;
|
442
|
|
- m_xoffset = w - pad;
|
443
|
|
- m_width = -w + pad*2;
|
444
|
|
- } else {
|
445
|
|
- m_flippedx = false;
|
446
|
|
- m_xoffset = -pad;
|
447
|
|
- m_width = w + pad*2;
|
448
|
|
- }
|
449
|
|
-
|
450
|
|
- pad = m_padding;
|
451
|
|
- if ( pad < 0 && std::abs( h ) < std::abs( pad )*2 ) {
|
452
|
|
- pad = 0;
|
453
|
|
- }
|
454
|
|
- if ( h < 0 ) {
|
455
|
|
- m_flippedy = true;
|
456
|
|
- m_yoffset = h - pad;
|
457
|
|
- m_height = -h + pad*2;
|
458
|
|
- } else {
|
459
|
|
- m_flippedy = false;
|
460
|
|
- m_yoffset = -pad;
|
461
|
|
- m_height = h + pad*2;
|
462
|
|
- }
|
463
|
|
-}
|
464
|
|
-
|
465
|
|
-int slop::Rectangle::convertColor( float r, float g, float b ) {
|
466
|
|
- // Convert float colors to shorts.
|
467
|
|
- short red = short( floor( r * 65535.f ) );
|
468
|
|
- short green = short( floor( g * 65535.f ) );
|
469
|
|
- short blue = short( floor( b * 65535.f ) );
|
470
|
|
- XColor color;
|
471
|
|
- color.red = red;
|
472
|
|
- color.green = green;
|
473
|
|
- color.blue = blue;
|
474
|
|
- // I don't deallocate this anywhere, I think X handles it ???
|
475
|
|
- int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
|
476
|
|
- if ( err == BadColor ) {
|
477
|
|
- return err;
|
478
|
|
- }
|
479
|
|
- m_color = color;
|
480
|
|
- return 0;
|
481
|
|
-}
|