|
@@ -28,6 +28,7 @@ slrn::XEngine::~XEngine() {
|
28
|
28
|
XCloseDisplay( m_display );
|
29
|
29
|
}
|
30
|
30
|
|
|
31
|
+// We need to keep track of the rectangle windows, so that they don't override our "focus"d windows.
|
31
|
32
|
void slrn::XEngine::addRect( Rectangle* rect ) {
|
32
|
33
|
m_rects.push_back( rect );
|
33
|
34
|
}
|
|
@@ -88,6 +89,7 @@ int slrn::XEngine::releaseKeyboard() {
|
88
|
89
|
return 0;
|
89
|
90
|
}
|
90
|
91
|
|
|
92
|
+// Grabs the cursor, be wary that setCursor changes the mouse masks.
|
91
|
93
|
int slrn::XEngine::grabCursor( slrn::CursorType type ) {
|
92
|
94
|
if ( !m_good ) {
|
93
|
95
|
return 1;
|
|
@@ -111,6 +113,8 @@ int slrn::XEngine::grabCursor( slrn::CursorType type ) {
|
111
|
113
|
XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
|
112
|
114
|
m_mousex = mx;
|
113
|
115
|
m_mousey = my;
|
|
116
|
+
|
|
117
|
+ // Oh and while we're at it, make sure we set the window we're hoving over as well.
|
114
|
118
|
updateHoverWindow( child );
|
115
|
119
|
return 0;
|
116
|
120
|
}
|
|
@@ -138,6 +142,7 @@ void slrn::XEngine::tick() {
|
138
|
142
|
break;
|
139
|
143
|
}
|
140
|
144
|
case ButtonPress: {
|
|
145
|
+ // Our pitiful mouse manager--
|
141
|
146
|
if ( m_mouse.size() > event.xbutton.button ) {
|
142
|
147
|
m_mouse.at( event.xbutton.button ) = true;
|
143
|
148
|
} else {
|
|
@@ -156,11 +161,12 @@ void slrn::XEngine::tick() {
|
156
|
161
|
break;
|
157
|
162
|
}
|
158
|
163
|
// For this particular utility, we only care if a key is pressed.
|
159
|
|
- // I'm too lazy to implement a keyhandler for that.
|
|
164
|
+ // I'm too lazy to implement an actual keyhandler for that.
|
160
|
165
|
case KeyPress: {
|
161
|
166
|
m_keypressed = true;
|
162
|
167
|
break;
|
163
|
168
|
}
|
|
169
|
+ // Since we also don't care if it's released, do nothing! yay
|
164
|
170
|
case KeyRelease: {
|
165
|
171
|
//m_keypressed = false;
|
166
|
172
|
break;
|
|
@@ -169,10 +175,11 @@ void slrn::XEngine::tick() {
|
169
|
175
|
}
|
170
|
176
|
}
|
171
|
177
|
|
172
|
|
- // Since I couldn't get Xlib to send a EnterNotify or LeaveNotify events, we need to query the underlying window every frame.
|
|
178
|
+ // Since I couldn't get Xlib to send EnterNotify or LeaveNotify events, we need to query the underlying window every frame.
|
173
|
179
|
updateHoverWindow();
|
174
|
180
|
}
|
175
|
181
|
|
|
182
|
+// This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
|
176
|
183
|
Cursor slrn::XEngine::getCursor( slrn::CursorType type ) {
|
177
|
184
|
int xfontcursor;
|
178
|
185
|
switch ( type ) {
|
|
@@ -197,6 +204,7 @@ Cursor slrn::XEngine::getCursor( slrn::CursorType type ) {
|
197
|
204
|
return newcursor;
|
198
|
205
|
}
|
199
|
206
|
|
|
207
|
+// Swaps out the current cursor, bewary that XChangeActivePointerGrab also resets masks, so if you change the mouse masks on grab you need to change them here too.
|
200
|
208
|
void slrn::XEngine::setCursor( slrn::CursorType type ) {
|
201
|
209
|
if ( !m_good ) {
|
202
|
210
|
return;
|
|
@@ -228,7 +236,10 @@ slrn::Rectangle::Rectangle( int x, int y, int width, int height, int border, int
|
228
|
236
|
m_padding = padding;
|
229
|
237
|
m_window = None;
|
230
|
238
|
|
|
239
|
+ // Convert the width, height, x, and y to coordinates that don't have negative values.
|
|
240
|
+ // (also adjust for padding and border size.)
|
231
|
241
|
constrain( width, height );
|
|
242
|
+ // If we don't have a border, we don't exist, so just die.
|
232
|
243
|
if ( m_border == 0 ) {
|
233
|
244
|
return;
|
234
|
245
|
}
|
|
@@ -239,18 +250,21 @@ slrn::Rectangle::Rectangle( int x, int y, int width, int height, int border, int
|
239
|
250
|
fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
|
240
|
251
|
}
|
241
|
252
|
XSetWindowAttributes attributes;
|
|
253
|
+ // Set up the window so it's our color
|
242
|
254
|
attributes.background_pixmap = None;
|
243
|
255
|
attributes.background_pixel = m_color.pixel;
|
244
|
|
- attributes.save_under = True;
|
|
256
|
+ // Not actually sure what this does, but it keeps the window from bugging out :u.
|
245
|
257
|
attributes.override_redirect = True;
|
|
258
|
+ // We must use our color map, because that's where our color is allocated.
|
246
|
259
|
attributes.colormap = xengine->m_colormap;
|
247
|
|
- unsigned long valueMask = CWBackPixmap | CWBackPixel |
|
248
|
|
- CWSaveUnder | CWOverrideRedirect |
|
249
|
|
- CWColormap;
|
|
260
|
+ unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap;
|
250
|
261
|
|
|
262
|
+ // Create the window offset by our generated offsets (see constrain( float, float ))
|
251
|
263
|
m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x+m_xoffset, m_y+m_yoffset, m_width+m_border*2, m_height+m_border*2,
|
252
|
264
|
0, CopyFromParent, InputOutput,
|
253
|
265
|
CopyFromParent, valueMask, &attributes );
|
|
266
|
+
|
|
267
|
+ // Now punch a hole into it so it looks like a selection rectangle!
|
254
|
268
|
XRectangle rect;
|
255
|
269
|
rect.x = rect.y = m_border;
|
256
|
270
|
rect.width = m_width;
|
|
@@ -266,6 +280,7 @@ void slrn::Rectangle::setPos( int x, int y ) {
|
266
|
280
|
}
|
267
|
281
|
m_x = x;
|
268
|
282
|
m_y = y;
|
|
283
|
+ // If we don't have a border, we don't exist, so just die.
|
269
|
284
|
if ( m_border == 0 ) {
|
270
|
285
|
return;
|
271
|
286
|
}
|
|
@@ -278,19 +293,21 @@ void slrn::Rectangle::setDim( int w, int h ) {
|
278
|
293
|
}
|
279
|
294
|
|
280
|
295
|
constrain( w, h );
|
|
296
|
+ // If we don't have a border, we don't exist, so just die.
|
281
|
297
|
if ( m_border == 0 ) {
|
282
|
298
|
return;
|
283
|
299
|
}
|
284
|
300
|
|
|
301
|
+ // Change the window size and location to our generated offsets (see constrain( float, float ))
|
285
|
302
|
XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
|
286
|
303
|
XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset, m_y+m_yoffset );
|
287
|
|
- // Now punch another hole in it.
|
|
304
|
+ // Regenerate our hole
|
288
|
305
|
XRectangle rect;
|
289
|
306
|
rect.x = rect.y = 0;
|
290
|
307
|
rect.width = m_width+m_border*2;
|
291
|
308
|
rect.height = m_height+m_border*2;
|
292
|
309
|
XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
|
293
|
|
- rect;
|
|
310
|
+ // Then punch out another.
|
294
|
311
|
rect.x = rect.y = m_border;
|
295
|
312
|
rect.width = m_width;
|
296
|
313
|
rect.height = m_height;
|
|
@@ -302,10 +319,13 @@ void slrn::XEngine::updateHoverWindow() {
|
302
|
319
|
int mx, my;
|
303
|
320
|
int wx, wy;
|
304
|
321
|
unsigned int mask;
|
|
322
|
+ // Query the pointer for the child window, the child window is basically the window we're hovering over.
|
305
|
323
|
XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
|
|
324
|
+ // If we already know that we're hovering over it, do nothing.
|
306
|
325
|
if ( m_hoverXWindow == child ) {
|
307
|
326
|
return;
|
308
|
327
|
}
|
|
328
|
+ // Make sure we can't select one of our selection rectangles, that's just weird.
|
309
|
329
|
for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
310
|
330
|
if ( m_rects.at( i )->m_window == child ) {
|
311
|
331
|
return;
|
|
@@ -315,6 +335,7 @@ void slrn::XEngine::updateHoverWindow() {
|
315
|
335
|
if ( child == None ) {
|
316
|
336
|
return;
|
317
|
337
|
}
|
|
338
|
+ // Generate the geometry values so we can use them if needed.
|
318
|
339
|
unsigned int depth;
|
319
|
340
|
XGetGeometry( m_display, child, &root,
|
320
|
341
|
&(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
|
|
@@ -323,6 +344,9 @@ void slrn::XEngine::updateHoverWindow() {
|
323
|
344
|
}
|
324
|
345
|
|
325
|
346
|
void slrn::XEngine::updateHoverWindow( Window child ) {
|
|
347
|
+ // Same thing as updateHoverWindow but it uses the specified child.
|
|
348
|
+ // It's used when we first grab the cursor so it's slightly more effecient
|
|
349
|
+ // than calling XQueryPointer twice.
|
326
|
350
|
if ( m_hoverXWindow == child ) {
|
327
|
351
|
return;
|
328
|
352
|
}
|
|
@@ -344,6 +368,7 @@ void slrn::XEngine::updateHoverWindow( Window child ) {
|
344
|
368
|
}
|
345
|
369
|
|
346
|
370
|
// Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
|
|
371
|
+// It also keeps our values in absolute coordinates which is nice.
|
347
|
372
|
void slrn::Rectangle::constrain( int w, int h ) {
|
348
|
373
|
int pad = m_padding;
|
349
|
374
|
if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
|
|
@@ -383,6 +408,7 @@ int slrn::Rectangle::convertColor( float r, float g, float b ) {
|
383
|
408
|
color.red = red;
|
384
|
409
|
color.green = green;
|
385
|
410
|
color.blue = blue;
|
|
411
|
+ // I don't deallocate this anywhere, I think X handles it ???
|
386
|
412
|
int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
|
387
|
413
|
if ( err == BadColor ) {
|
388
|
414
|
return err;
|