|
|
|
|
28
|
XCloseDisplay( m_display );
|
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
|
void slrn::XEngine::addRect( Rectangle* rect ) {
|
32
|
void slrn::XEngine::addRect( Rectangle* rect ) {
|
32
|
m_rects.push_back( rect );
|
33
|
m_rects.push_back( rect );
|
33
|
}
|
34
|
}
|
|
|
|
|
88
|
return 0;
|
89
|
return 0;
|
89
|
}
|
90
|
}
|
90
|
|
91
|
|
|
|
92
|
+// Grabs the cursor, be wary that setCursor changes the mouse masks.
|
91
|
int slrn::XEngine::grabCursor( slrn::CursorType type ) {
|
93
|
int slrn::XEngine::grabCursor( slrn::CursorType type ) {
|
92
|
if ( !m_good ) {
|
94
|
if ( !m_good ) {
|
93
|
return 1;
|
95
|
return 1;
|
|
|
|
|
111
|
XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
|
113
|
XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
|
112
|
m_mousex = mx;
|
114
|
m_mousex = mx;
|
113
|
m_mousey = my;
|
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
|
updateHoverWindow( child );
|
118
|
updateHoverWindow( child );
|
115
|
return 0;
|
119
|
return 0;
|
116
|
}
|
120
|
}
|
|
|
|
|
138
|
break;
|
142
|
break;
|
139
|
}
|
143
|
}
|
140
|
case ButtonPress: {
|
144
|
case ButtonPress: {
|
|
|
145
|
+ // Our pitiful mouse manager--
|
141
|
if ( m_mouse.size() > event.xbutton.button ) {
|
146
|
if ( m_mouse.size() > event.xbutton.button ) {
|
142
|
m_mouse.at( event.xbutton.button ) = true;
|
147
|
m_mouse.at( event.xbutton.button ) = true;
|
143
|
} else {
|
148
|
} else {
|
|
|
|
|
156
|
break;
|
161
|
break;
|
157
|
}
|
162
|
}
|
158
|
// For this particular utility, we only care if a key is pressed.
|
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
|
case KeyPress: {
|
165
|
case KeyPress: {
|
161
|
m_keypressed = true;
|
166
|
m_keypressed = true;
|
162
|
break;
|
167
|
break;
|
163
|
}
|
168
|
}
|
|
|
169
|
+ // Since we also don't care if it's released, do nothing! yay
|
164
|
case KeyRelease: {
|
170
|
case KeyRelease: {
|
165
|
//m_keypressed = false;
|
171
|
//m_keypressed = false;
|
166
|
break;
|
172
|
break;
|
|
|
|
|
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
|
updateHoverWindow();
|
179
|
updateHoverWindow();
|
174
|
}
|
180
|
}
|
175
|
|
181
|
|
|
|
182
|
+// This converts an enum into a preallocated cursor, the cursor will automatically deallocate itself on ~XEngine
|
176
|
Cursor slrn::XEngine::getCursor( slrn::CursorType type ) {
|
183
|
Cursor slrn::XEngine::getCursor( slrn::CursorType type ) {
|
177
|
int xfontcursor;
|
184
|
int xfontcursor;
|
178
|
switch ( type ) {
|
185
|
switch ( type ) {
|
|
|
|
|
197
|
return newcursor;
|
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
|
void slrn::XEngine::setCursor( slrn::CursorType type ) {
|
208
|
void slrn::XEngine::setCursor( slrn::CursorType type ) {
|
201
|
if ( !m_good ) {
|
209
|
if ( !m_good ) {
|
202
|
return;
|
210
|
return;
|
|
|
|
|
228
|
m_padding = padding;
|
236
|
m_padding = padding;
|
229
|
m_window = None;
|
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
|
constrain( width, height );
|
241
|
constrain( width, height );
|
|
|
242
|
+ // If we don't have a border, we don't exist, so just die.
|
232
|
if ( m_border == 0 ) {
|
243
|
if ( m_border == 0 ) {
|
233
|
return;
|
244
|
return;
|
234
|
}
|
245
|
}
|
|
|
|
|
239
|
fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
|
250
|
fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
|
240
|
}
|
251
|
}
|
241
|
XSetWindowAttributes attributes;
|
252
|
XSetWindowAttributes attributes;
|
|
|
253
|
+ // Set up the window so it's our color
|
242
|
attributes.background_pixmap = None;
|
254
|
attributes.background_pixmap = None;
|
243
|
attributes.background_pixel = m_color.pixel;
|
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
|
attributes.override_redirect = True;
|
257
|
attributes.override_redirect = True;
|
|
|
258
|
+ // We must use our color map, because that's where our color is allocated.
|
246
|
attributes.colormap = xengine->m_colormap;
|
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
|
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,
|
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
|
0, CopyFromParent, InputOutput,
|
264
|
0, CopyFromParent, InputOutput,
|
253
|
CopyFromParent, valueMask, &attributes );
|
265
|
CopyFromParent, valueMask, &attributes );
|
|
|
266
|
+
|
|
|
267
|
+ // Now punch a hole into it so it looks like a selection rectangle!
|
254
|
XRectangle rect;
|
268
|
XRectangle rect;
|
255
|
rect.x = rect.y = m_border;
|
269
|
rect.x = rect.y = m_border;
|
256
|
rect.width = m_width;
|
270
|
rect.width = m_width;
|
|
|
|
|
266
|
}
|
280
|
}
|
267
|
m_x = x;
|
281
|
m_x = x;
|
268
|
m_y = y;
|
282
|
m_y = y;
|
|
|
283
|
+ // If we don't have a border, we don't exist, so just die.
|
269
|
if ( m_border == 0 ) {
|
284
|
if ( m_border == 0 ) {
|
270
|
return;
|
285
|
return;
|
271
|
}
|
286
|
}
|
|
|
|
|
278
|
}
|
293
|
}
|
279
|
|
294
|
|
280
|
constrain( w, h );
|
295
|
constrain( w, h );
|
|
|
296
|
+ // If we don't have a border, we don't exist, so just die.
|
281
|
if ( m_border == 0 ) {
|
297
|
if ( m_border == 0 ) {
|
282
|
return;
|
298
|
return;
|
283
|
}
|
299
|
}
|
284
|
|
300
|
|
|
|
301
|
+ // Change the window size and location to our generated offsets (see constrain( float, float ))
|
285
|
XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
|
302
|
XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
|
286
|
XMoveWindow( xengine->m_display, m_window, m_x+m_xoffset, m_y+m_yoffset );
|
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
|
XRectangle rect;
|
305
|
XRectangle rect;
|
289
|
rect.x = rect.y = 0;
|
306
|
rect.x = rect.y = 0;
|
290
|
rect.width = m_width+m_border*2;
|
307
|
rect.width = m_width+m_border*2;
|
291
|
rect.height = m_height+m_border*2;
|
308
|
rect.height = m_height+m_border*2;
|
292
|
XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
|
309
|
XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
|
293
|
- rect;
|
|
|
|
|
310
|
+ // Then punch out another.
|
294
|
rect.x = rect.y = m_border;
|
311
|
rect.x = rect.y = m_border;
|
295
|
rect.width = m_width;
|
312
|
rect.width = m_width;
|
296
|
rect.height = m_height;
|
313
|
rect.height = m_height;
|
|
|
|
|
302
|
int mx, my;
|
319
|
int mx, my;
|
303
|
int wx, wy;
|
320
|
int wx, wy;
|
304
|
unsigned int mask;
|
321
|
unsigned int mask;
|
|
|
322
|
+ // Query the pointer for the child window, the child window is basically the window we're hovering over.
|
305
|
XQueryPointer( m_display, m_root, &root, &child, &mx, &my, &wx, &wy, &mask );
|
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
|
if ( m_hoverXWindow == child ) {
|
325
|
if ( m_hoverXWindow == child ) {
|
307
|
return;
|
326
|
return;
|
308
|
}
|
327
|
}
|
|
|
328
|
+ // Make sure we can't select one of our selection rectangles, that's just weird.
|
309
|
for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
329
|
for ( unsigned int i=0; i<m_rects.size(); i++ ) {
|
310
|
if ( m_rects.at( i )->m_window == child ) {
|
330
|
if ( m_rects.at( i )->m_window == child ) {
|
311
|
return;
|
331
|
return;
|
|
|
|
|
315
|
if ( child == None ) {
|
335
|
if ( child == None ) {
|
316
|
return;
|
336
|
return;
|
317
|
}
|
337
|
}
|
|
|
338
|
+ // Generate the geometry values so we can use them if needed.
|
318
|
unsigned int depth;
|
339
|
unsigned int depth;
|
319
|
XGetGeometry( m_display, child, &root,
|
340
|
XGetGeometry( m_display, child, &root,
|
320
|
&(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
|
341
|
&(m_hoverWindow.m_x), &(m_hoverWindow.m_y),
|
|
|
|
|
323
|
}
|
344
|
}
|
324
|
|
345
|
|
325
|
void slrn::XEngine::updateHoverWindow( Window child ) {
|
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
|
if ( m_hoverXWindow == child ) {
|
350
|
if ( m_hoverXWindow == child ) {
|
327
|
return;
|
351
|
return;
|
328
|
}
|
352
|
}
|
|
|
|
|
344
|
}
|
368
|
}
|
345
|
|
369
|
|
346
|
// Keeps our rectangle's sizes all positive, so Xlib doesn't throw an exception.
|
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
|
void slrn::Rectangle::constrain( int w, int h ) {
|
372
|
void slrn::Rectangle::constrain( int w, int h ) {
|
348
|
int pad = m_padding;
|
373
|
int pad = m_padding;
|
349
|
if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
|
374
|
if ( pad < 0 && std::abs( w ) < std::abs( pad )*2 ) {
|
|
|
|
|
383
|
color.red = red;
|
408
|
color.red = red;
|
384
|
color.green = green;
|
409
|
color.green = green;
|
385
|
color.blue = blue;
|
410
|
color.blue = blue;
|
|
|
411
|
+ // I don't deallocate this anywhere, I think X handles it ???
|
386
|
int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
|
412
|
int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
|
387
|
if ( err == BadColor ) {
|
413
|
if ( err == BadColor ) {
|
388
|
return err;
|
414
|
return err;
|