|
@@ -0,0 +1,204 @@
|
|
1
|
+/* glrectangle.hpp: Handles creating hardware accelerated rectangles on the screen using X11 and OpenGL.
|
|
2
|
+ *
|
|
3
|
+ * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
|
|
4
|
+ *
|
|
5
|
+ * This file is part of Slop.
|
|
6
|
+ *
|
|
7
|
+ * Slop is free software: you can redistribute it and/or modify
|
|
8
|
+ * it under the terms of the GNU General Public License as published by
|
|
9
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+ * (at your option) any later version.
|
|
11
|
+ *
|
|
12
|
+ * Slop is distributed in the hope that it will be useful,
|
|
13
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+ * GNU General Public License for more details.
|
|
16
|
+ *
|
|
17
|
+ * You should have received a copy of the GNU General Public License
|
|
18
|
+ * along with Slop. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+ */
|
|
20
|
+#include "glselectrectangle.hpp"
|
|
21
|
+
|
|
22
|
+static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
|
|
23
|
+ return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+slop::GLSelectRectangle::~GLSelectRectangle() {
|
|
27
|
+ if ( m_window == None ) {
|
|
28
|
+ return;
|
|
29
|
+ }
|
|
30
|
+ // Try to erase the window before destroying it.
|
|
31
|
+ glClearColor( 0, 0, 0, 0 );
|
|
32
|
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
33
|
+ glXSwapBuffers( xengine->m_display, m_glxWindow );
|
|
34
|
+ // Sleep for 0.1 seconds in hope that the rectangle was erased.
|
|
35
|
+ usleep( 10000 );
|
|
36
|
+ XDestroyWindow( xengine->m_display, m_window );
|
|
37
|
+ XEvent event;
|
|
38
|
+ // Block until the window is actually completely removed.
|
|
39
|
+ XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
|
|
40
|
+ // Sleep for 0.1 seconds in hope that the screen actually cleared the window.
|
|
41
|
+ usleep( 10000 );
|
|
42
|
+}
|
|
43
|
+
|
|
44
|
+slop::GLSelectRectangle::GLSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
|
|
45
|
+ m_x = std::min( sx, ex );
|
|
46
|
+ m_y = std::min( sy, ey );
|
|
47
|
+ m_width = std::max( sx, ex ) - m_x;
|
|
48
|
+ m_height = std::max( sy, ey ) - m_y;
|
|
49
|
+ m_r = r;
|
|
50
|
+ m_g = g;
|
|
51
|
+ m_b = b;
|
|
52
|
+ m_a = a;
|
|
53
|
+ m_border = border;
|
|
54
|
+ m_window = None;
|
|
55
|
+ m_highlight = highlight;
|
|
56
|
+
|
|
57
|
+ // If we don't have a border, we don't exist, so just die.
|
|
58
|
+ if ( m_border == 0 ) {
|
|
59
|
+ return;
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ if ( m_highlight ) {
|
|
63
|
+ m_border = 0;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ static int visdata[] = {
|
|
67
|
+ GLX_RENDER_TYPE, GLX_RGBA_BIT,
|
|
68
|
+ GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
|
|
69
|
+ GLX_DOUBLEBUFFER, True,
|
|
70
|
+ GLX_RED_SIZE, 8,
|
|
71
|
+ GLX_GREEN_SIZE, 8,
|
|
72
|
+ GLX_BLUE_SIZE, 8,
|
|
73
|
+ GLX_ALPHA_SIZE, 8,
|
|
74
|
+ GLX_DEPTH_SIZE, 16,
|
|
75
|
+ None
|
|
76
|
+ };
|
|
77
|
+
|
|
78
|
+ int numfbconfigs = 0;
|
|
79
|
+ GLXFBConfig* fbconfigs = glXChooseFBConfig( xengine->m_display, DefaultScreen( xengine->m_display ), visdata, &numfbconfigs );
|
|
80
|
+ m_fbconfig = 0;
|
|
81
|
+ for ( int i=0; i<numfbconfigs; i++ ) {
|
|
82
|
+ m_visual = (XVisualInfo*)glXGetVisualFromFBConfig( xengine->m_display, fbconfigs[i] );
|
|
83
|
+ if ( !m_visual ) {
|
|
84
|
+ continue;
|
|
85
|
+ }
|
|
86
|
+ m_pictFormat = XRenderFindVisualFormat( xengine->m_display, m_visual->visual );
|
|
87
|
+ if ( !m_pictFormat ) {
|
|
88
|
+ continue;
|
|
89
|
+ }
|
|
90
|
+ m_fbconfig = fbconfigs[i];
|
|
91
|
+ if ( m_pictFormat->direct.alphaMask > 0 ) {
|
|
92
|
+ break;
|
|
93
|
+ }
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ if ( !m_fbconfig ) {
|
|
97
|
+ fprintf( stderr, "Couldn't find a matching FB config for a transparent OpenGL window!\n");
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ m_cmap = XCreateColormap( xengine->m_display, xengine->m_root, m_visual->visual, AllocNone );
|
|
101
|
+
|
|
102
|
+ XSetWindowAttributes attributes;
|
|
103
|
+ attributes.colormap = m_cmap;
|
|
104
|
+ attributes.background_pixmap = None;
|
|
105
|
+ attributes.border_pixmap = None;
|
|
106
|
+ attributes.border_pixel = 0;
|
|
107
|
+ // Disable window decorations.
|
|
108
|
+ attributes.override_redirect = True;
|
|
109
|
+ // Make sure we know when we've been successfully destroyed later!
|
|
110
|
+ attributes.event_mask = StructureNotifyMask;
|
|
111
|
+ unsigned long valueMask = CWOverrideRedirect | CWEventMask | CWBackPixmap | CWColormap | CWBorderPixel;
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+ // Create the window
|
|
115
|
+ m_window = XCreateWindow( xengine->m_display, xengine->m_root, 0, 0, xengine->getWidth(), xengine->getHeight(),
|
|
116
|
+ 0, m_visual->depth, InputOutput,
|
|
117
|
+ m_visual->visual, valueMask, &attributes );
|
|
118
|
+
|
|
119
|
+ if ( !m_window ) {
|
|
120
|
+ fprintf( stderr, "Couldn't create a GL window!\n");
|
|
121
|
+ }
|
|
122
|
+
|
|
123
|
+ m_glxWindow = m_window;
|
|
124
|
+
|
|
125
|
+ static char title[] = "OpenGL Slop";
|
|
126
|
+ XWMHints* startup_state = XAllocWMHints();
|
|
127
|
+ startup_state->initial_state = NormalState;
|
|
128
|
+ startup_state->flags = StateHint;
|
|
129
|
+ XTextProperty textprop;
|
|
130
|
+ textprop.value = (unsigned char*)title;
|
|
131
|
+ textprop.encoding = XA_STRING;
|
|
132
|
+ textprop.format = 8;
|
|
133
|
+ textprop.nitems = strlen( title );
|
|
134
|
+ XSizeHints sizehints;
|
|
135
|
+ sizehints.x = 0;
|
|
136
|
+ sizehints.y = 0;
|
|
137
|
+ sizehints.width = xengine->getWidth();
|
|
138
|
+ sizehints.height = xengine->getHeight();
|
|
139
|
+ sizehints.flags = USPosition | USSize;
|
|
140
|
+ XClassHint classhints;
|
|
141
|
+ char name[] = "slop";
|
|
142
|
+ classhints.res_name = name;
|
|
143
|
+ classhints.res_class = name;
|
|
144
|
+ XSetClassHint( xengine->m_display, m_window, &classhints );
|
|
145
|
+ XSetWMProperties( xengine->m_display, m_window, &textprop, &textprop, NULL, 0, &sizehints, startup_state, NULL );
|
|
146
|
+ XFree( startup_state );
|
|
147
|
+
|
|
148
|
+ // Make it so all input falls through
|
|
149
|
+ XRectangle rect;
|
|
150
|
+ rect.x = rect.y = rect.width = rect.height = 0;
|
|
151
|
+ XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
|
|
152
|
+
|
|
153
|
+ XMapWindow( xengine->m_display, m_window );
|
|
154
|
+
|
|
155
|
+ int dummy;
|
|
156
|
+ if ( !glXQueryExtension( xengine->m_display, &dummy, &dummy ) ) {
|
|
157
|
+ fprintf( stderr, "OpenGL is not supported!\n" );
|
|
158
|
+ }
|
|
159
|
+ m_renderContext = glXCreateNewContext( xengine->m_display, m_fbconfig, GLX_RGBA_TYPE, 0, True );
|
|
160
|
+ if ( !m_renderContext ) {
|
|
161
|
+ fprintf( stderr, "Failed to create a GL context.\n" );
|
|
162
|
+ }
|
|
163
|
+ if ( !glXMakeContextCurrent( xengine->m_display, m_glxWindow, m_glxWindow, m_renderContext ) ) {
|
|
164
|
+ fprintf( stderr, "Failed to attach GL context to window!\n" );
|
|
165
|
+ }
|
|
166
|
+ glClearColor( 0, 0, 0, 0 );
|
|
167
|
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
168
|
+ glXSwapBuffers( xengine->m_display, m_glxWindow );
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+void slop::GLSelectRectangle::setGeo( int sx, int sy, int ex, int ey ) {
|
|
172
|
+ int x = std::min( sx, ex );
|
|
173
|
+ int y = std::min( sy, ey );
|
|
174
|
+ int w = std::max( sx, ex ) - x;
|
|
175
|
+ int h = std::max( sy, ey ) - y;
|
|
176
|
+
|
|
177
|
+ m_x = x;
|
|
178
|
+ m_y = y;
|
|
179
|
+ m_width = w;
|
|
180
|
+ m_height = h;
|
|
181
|
+ glDrawBuffer( GL_BACK );
|
|
182
|
+ glViewport( 0, 0, xengine->getWidth(), xengine->getHeight() );
|
|
183
|
+
|
|
184
|
+ glClearColor( 0, 0, 0, 0 );
|
|
185
|
+ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
|
186
|
+
|
|
187
|
+ glMatrixMode( GL_PROJECTION );
|
|
188
|
+ glLoadIdentity();
|
|
189
|
+ glOrtho( 0, xengine->getWidth(), xengine->getHeight(), 0, 1, -1 );
|
|
190
|
+
|
|
191
|
+ glMatrixMode( GL_MODELVIEW );
|
|
192
|
+ glLoadIdentity();
|
|
193
|
+
|
|
194
|
+ glEnable( GL_BLEND );
|
|
195
|
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
196
|
+
|
|
197
|
+ glColor4f( m_r, m_g, m_b, m_a );
|
|
198
|
+ glRecti( m_x-m_border, m_y, m_x+m_width+m_border, m_y-m_border );
|
|
199
|
+ glRecti( m_x-m_border, m_y+m_height, m_x+m_width+m_border, m_y+m_height+m_border );
|
|
200
|
+ glRecti( m_x-m_border, m_y, m_x, m_y+m_height );
|
|
201
|
+ glRecti( m_x+m_width, m_y, m_x+m_width+m_border, m_y+m_height );
|
|
202
|
+
|
|
203
|
+ glXSwapBuffers( xengine->m_display, m_glxWindow );
|
|
204
|
+}
|