Skip to content

Commit fc5cf3f

Browse files
committed
GPU Display: Modernize GLUT backend: Use callbacks with user-data for this ptr, update key constants
1 parent 7bd62cf commit fc5cf3f

3 files changed

Lines changed: 125 additions & 151 deletions

File tree

‎GPU/GPUTracking/display/CMakeLists.txt‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ if(NOT GPUCA_EVENT_DISPLAY_OPENGL)
6969
set(X11_FOUND 0)
7070
endif()
7171

72+
if(GLUT_FOUND)
73+
include(CheckSymbolExists)
74+
set(CMAKE_REQUIRED_INCLUDES ${GLUT_INCLUDE_DIR})
75+
set(CMAKE_REQUIRED_LIBRARIES ${GLUT_glut_LIBRARY})
76+
check_symbol_exists(glutDisplayFuncUcall "GL/freeglut.h;GL/freeglut_ucall.h" GPUCA_GLUT_HAS_UCALL) # Callbacks with user data, freeglut >= 3.2 (modifier key codes exist since 2.8)
77+
unset(CMAKE_REQUIRED_INCLUDES)
78+
unset(CMAKE_REQUIRED_LIBRARIES)
79+
if(NOT GPUCA_GLUT_HAS_UCALL)
80+
if(ALIGPU_BUILD_TYPE STREQUAL "Standalone")
81+
message(FATAL_ERROR "GLUT event display frontend needs freeglut >= 3.2")
82+
endif()
83+
message(STATUS "GLUT event display frontend disabled: needs freeglut >= 3.2")
84+
set(GLUT_FOUND 0)
85+
endif()
86+
endif()
87+
7288
if(Vulkan_FOUND)
7389
string(FIND "${Vulkan_VERSION}" "." vulkan_dot_index)
7490
if(NOT ${Vulkan_VERSION} VERSION_GREATER_EQUAL "1.4.328" OR vulkan_dot_index EQUAL -1 OR NOT GPUCA_GLSLC)

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendGlut.cxx‎

Lines changed: 101 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <cstdio>
2121
#include <cstring>
2222
#include <GL/freeglut.h>
23+
#include <GL/freeglut_ucall.h>
2324
#include <unistd.h>
2425
#ifdef GPUCA_BUILD_EVENT_DISPLAY_X11
2526
#include <GL/glx.h>
@@ -29,111 +30,90 @@
2930

3031
#include <pthread.h>
3132
using namespace o2::gpu;
32-
static GPUDisplayFrontendGlut* me = nullptr;
3333

3434
GPUDisplayFrontendGlut::GPUDisplayFrontendGlut()
3535
{
3636
mFrontendType = TYPE_GLUT;
3737
mFrontendName = "GLUT";
3838
}
3939

40-
void GPUDisplayFrontendGlut::displayFunc() // TODO: Functions should be lambdas, to get rid of the static me variable!
40+
void GPUDisplayFrontendGlut::displayFunc()
4141
{
42-
me->mRedisplayPending = false;
43-
me->HandleUpdatesAndDrawGLScene();
42+
mRedisplayPending = false;
43+
HandleUpdatesAndDrawGLScene();
4444
glutSwapBuffers();
45-
me->waitForFPSLimit();
45+
waitForFPSLimit();
4646
}
4747

4848
void GPUDisplayFrontendGlut::glutLoopFunc()
4949
{
50-
if (me->mRedisplayPending) { // Last redisplay not served (window not visible): handle updates without drawing
51-
me->HandleUpdatesAndDrawGLScene(true);
52-
me->waitForFPSLimit();
50+
if (mRedisplayPending) { // Last redisplay not served (window not visible): handle updates without drawing
51+
HandleUpdatesAndDrawGLScene(true);
52+
waitForFPSLimit();
5353
}
54-
me->mRedisplayPending = true;
54+
mRedisplayPending = true;
5555
glutPostRedisplay(); // Drawing only in displayFunc, so that expose events do not draw twice
5656
}
5757

5858
int32_t GPUDisplayFrontendGlut::GetKey(int32_t key)
5959
{
60-
if (key == GLUT_KEY_UP) {
61-
return KEY_UP;
60+
switch (key) {
61+
case GLUT_KEY_UP:
62+
return KEY_UP;
63+
case GLUT_KEY_DOWN:
64+
return KEY_DOWN;
65+
case GLUT_KEY_LEFT:
66+
return KEY_LEFT;
67+
case GLUT_KEY_RIGHT:
68+
return KEY_RIGHT;
69+
case GLUT_KEY_PAGE_UP:
70+
return KEY_PAGEUP;
71+
case GLUT_KEY_PAGE_DOWN:
72+
return KEY_PAGEDOWN;
73+
case GLUT_KEY_HOME:
74+
return KEY_HOME;
75+
case GLUT_KEY_END:
76+
return KEY_END;
77+
case GLUT_KEY_INSERT:
78+
return KEY_INSERT;
79+
case GLUT_KEY_F1:
80+
return KEY_F1;
81+
case GLUT_KEY_F2:
82+
return KEY_F2;
83+
case GLUT_KEY_F3:
84+
return KEY_F3;
85+
case GLUT_KEY_F4:
86+
return KEY_F4;
87+
case GLUT_KEY_F5:
88+
return KEY_F5;
89+
case GLUT_KEY_F6:
90+
return KEY_F6;
91+
case GLUT_KEY_F7:
92+
return KEY_F7;
93+
case GLUT_KEY_F8:
94+
return KEY_F8;
95+
case GLUT_KEY_F9:
96+
return KEY_F9;
97+
case GLUT_KEY_F10:
98+
return KEY_F10;
99+
case GLUT_KEY_F11:
100+
return KEY_F11;
101+
case GLUT_KEY_F12:
102+
return KEY_F12;
103+
case GLUT_KEY_SHIFT_L:
104+
case GLUT_KEY_SHIFT_R:
105+
return KEY_SHIFT;
106+
case GLUT_KEY_CTRL_L:
107+
return KEY_CTRL;
108+
case GLUT_KEY_CTRL_R:
109+
return KEY_RCTRL;
110+
case GLUT_KEY_ALT_L:
111+
return KEY_ALT;
112+
case GLUT_KEY_ALT_R:
113+
return KEY_RALT;
114+
default:
115+
return 0;
62116
}
63-
if (key == GLUT_KEY_DOWN) {
64-
return KEY_DOWN;
65-
}
66-
if (key == GLUT_KEY_LEFT) {
67-
return KEY_LEFT;
68-
}
69-
if (key == GLUT_KEY_RIGHT) {
70-
return KEY_RIGHT;
71-
}
72-
if (key == GLUT_KEY_PAGE_UP) {
73-
return KEY_PAGEUP;
74-
}
75-
if (key == GLUT_KEY_PAGE_DOWN) {
76-
return KEY_PAGEDOWN;
77-
}
78-
if (key == GLUT_KEY_HOME) {
79-
return KEY_HOME;
80-
}
81-
if (key == GLUT_KEY_END) {
82-
return KEY_END;
83-
}
84-
if (key == GLUT_KEY_INSERT) {
85-
return KEY_INSERT;
86-
}
87-
if (key == GLUT_KEY_F1) {
88-
return KEY_F1;
89-
}
90-
if (key == GLUT_KEY_F2) {
91-
return KEY_F2;
92-
}
93-
if (key == GLUT_KEY_F3) {
94-
return KEY_F3;
95-
}
96-
if (key == GLUT_KEY_F4) {
97-
return KEY_F4;
98-
}
99-
if (key == GLUT_KEY_F5) {
100-
return KEY_F5;
101-
}
102-
if (key == GLUT_KEY_F6) {
103-
return KEY_F6;
104-
}
105-
if (key == GLUT_KEY_F7) {
106-
return KEY_F7;
107-
}
108-
if (key == GLUT_KEY_F8) {
109-
return KEY_F8;
110-
}
111-
if (key == GLUT_KEY_F9) {
112-
return KEY_F9;
113-
}
114-
if (key == GLUT_KEY_F10) {
115-
return KEY_F10;
116-
}
117-
if (key == GLUT_KEY_F11) {
118-
return KEY_F11;
119-
}
120-
if (key == GLUT_KEY_F12) {
121-
return KEY_F12;
122-
}
123-
if (key == 112 || key == 113) {
124-
return KEY_SHIFT;
125-
}
126-
if (key == 114) {
127-
return KEY_CTRL;
128-
}
129-
if (key == 115) {
130-
return KEY_RCTRL;
131-
}
132-
if (key == 116) {
133-
return KEY_ALT;
134-
}
135-
136-
return (0);
137117
}
138118

139119
void GPUDisplayFrontendGlut::GetKey(int32_t key, int32_t& keyOut, int32_t& keyPressOut, bool special)
@@ -151,43 +131,23 @@ void GPUDisplayFrontendGlut::GetKey(int32_t key, int32_t& keyOut, int32_t& keyPr
151131
}
152132
}
153133

154-
void GPUDisplayFrontendGlut::keyboardDownFunc(uint8_t key, int32_t x, int32_t y)
155-
{
156-
int32_t handleKey = 0, keyPress = 0;
157-
GetKey(key, handleKey, keyPress, false);
158-
me->mKeys[keyPress] = true;
159-
me->HandleKey(handleKey);
160-
}
161-
162-
void GPUDisplayFrontendGlut::keyboardUpFunc(uint8_t key, int32_t x, int32_t y)
134+
void GPUDisplayFrontendGlut::keyboardFunc(int32_t key, bool special, bool down)
163135
{
164136
int32_t handleKey = 0, keyPress = 0;
165-
GetKey(key, handleKey, keyPress, false);
166-
me->mKeys[keyPress] = false;
167-
}
168-
169-
void GPUDisplayFrontendGlut::specialDownFunc(int32_t key, int32_t x, int32_t y)
170-
{
171-
int32_t handleKey = 0, keyPress = 0;
172-
GetKey(key, handleKey, keyPress, true);
173-
me->mKeys[keyPress] = true;
174-
me->HandleKey(handleKey);
175-
}
176-
177-
void GPUDisplayFrontendGlut::specialUpFunc(int32_t key, int32_t x, int32_t y)
178-
{
179-
int32_t handleKey = 0, keyPress = 0;
180-
GetKey(key, handleKey, keyPress, true);
181-
me->mKeys[keyPress] = false;
137+
GetKey(key, handleKey, keyPress, special);
138+
mKeys[keyPress] = down;
139+
if (down) {
140+
HandleKey(handleKey);
141+
}
182142
}
183143

184144
void GPUDisplayFrontendGlut::ResizeSceneWrapper(int32_t width, int32_t height)
185145
{
186-
if (!me->mFullScreen) {
187-
me->mWidth = width;
188-
me->mHeight = height;
146+
if (!mFullScreen) {
147+
mWidth = width;
148+
mHeight = height;
189149
}
190-
me->ResizeScene(width, height);
150+
ResizeScene(width, height);
191151
#ifdef GPUCA_BUILD_EVENT_DISPLAY_X11
192152
Display* display = glXGetCurrentDisplay(); // Maximizing / unmaximizing by the window manager changes the size, read the state as freeglut does not report it (fullscreen stays as requested, freeglut tracks it itself)
193153
Atom type;
@@ -204,52 +164,51 @@ void GPUDisplayFrontendGlut::ResizeSceneWrapper(int32_t width, int32_t height)
204164
isMaxVert |= ((Atom*)data)[i] == maxVert;
205165
}
206166
XFree(data);
207-
me->setWindowState(me->mFullScreen, isMaxHorz && isMaxVert);
167+
setWindowState(mFullScreen, isMaxHorz && isMaxVert);
208168
#endif
209169
}
210170

211171
void GPUDisplayFrontendGlut::mouseFunc(int32_t button, int32_t state, int32_t x, int32_t y)
212172
{
213173
if (button == 3) {
214-
me->mMouseWheel += 100;
174+
mMouseWheel += 100;
215175
} else if (button == 4) {
216-
me->mMouseWheel -= 100;
176+
mMouseWheel -= 100;
217177
} else if (state == GLUT_DOWN) {
218178
if (button == GLUT_LEFT_BUTTON) {
219-
me->mMouseDn = true;
179+
mMouseDn = true;
220180
} else if (button == GLUT_RIGHT_BUTTON) {
221-
me->mMouseDnR = true;
181+
mMouseDnR = true;
222182
} else if (button == GLUT_MIDDLE_BUTTON) {
223-
me->mMouseDnM = true;
183+
mMouseDnM = true;
224184
}
225-
me->mMouseDnX = x;
226-
me->mMouseDnY = y;
185+
mMouseDnX = x;
186+
mMouseDnY = y;
227187
} else if (state == GLUT_UP) {
228188
if (button == GLUT_LEFT_BUTTON) {
229-
me->mMouseDn = false;
189+
mMouseDn = false;
230190
} else if (button == GLUT_RIGHT_BUTTON) {
231-
me->mMouseDnR = false;
191+
mMouseDnR = false;
232192
} else if (button == GLUT_MIDDLE_BUTTON) {
233-
me->mMouseDnM = false;
193+
mMouseDnM = false;
234194
}
235195
}
236196
}
237197

238198
void GPUDisplayFrontendGlut::mouseMoveFunc(int32_t x, int32_t y)
239199
{
240-
me->mMouseMvX = x;
241-
me->mMouseMvY = y;
200+
mMouseMvX = x;
201+
mMouseMvY = y;
242202
}
243203

244-
void GPUDisplayFrontendGlut::mMouseWheelFunc(int32_t button, int32_t dir, int32_t x, int32_t y) { me->mMouseWheel += dir; }
204+
void GPUDisplayFrontendGlut::mMouseWheelFunc(int32_t button, int32_t dir, int32_t x, int32_t y) { mMouseWheel += dir; }
245205

246206
int32_t GPUDisplayFrontendGlut::FrontendMain()
247207
{
248208
if (backend()->backendType() != GPUDisplayBackend::TYPE_OPENGL) {
249209
fprintf(stderr, "Only OpenGL backend supported\n");
250210
return 1;
251211
}
252-
me = this;
253212
mCanDrawText = 1;
254213
if (drawTextFontSize() == 0) {
255214
drawTextFontSize() = 12;
@@ -276,17 +235,18 @@ int32_t GPUDisplayFrontendGlut::FrontendMain()
276235
return (1);
277236
}
278237

279-
glutDisplayFunc(displayFunc);
280-
glutIdleFunc(glutLoopFunc);
281-
glutReshapeFunc(ResizeSceneWrapper);
282-
glutKeyboardFunc(keyboardDownFunc);
283-
glutKeyboardUpFunc(keyboardUpFunc);
284-
glutSpecialFunc(specialDownFunc);
285-
glutSpecialUpFunc(specialUpFunc);
286-
glutMouseFunc(mouseFunc);
287-
glutMotionFunc(mouseMoveFunc);
288-
glutPassiveMotionFunc(mouseMoveFunc);
289-
glutMouseWheelFunc(mMouseWheelFunc);
238+
auto mouseMoveFunc = [](int32_t x, int32_t y, void* p) { self(p)->mouseMoveFunc(x, y); };
239+
glutDisplayFuncUcall([](void* p) { self(p)->displayFunc(); }, this); // Callbacks with this as user data, freeglut >= 3.2
240+
glutIdleFuncUcall([](void* p) { self(p)->glutLoopFunc(); }, this);
241+
glutReshapeFuncUcall([](int32_t width, int32_t height, void* p) { self(p)->ResizeSceneWrapper(width, height); }, this);
242+
glutKeyboardFuncUcall([](uint8_t key, int32_t x, int32_t y, void* p) { self(p)->keyboardFunc(key, false, true); }, this);
243+
glutKeyboardUpFuncUcall([](uint8_t key, int32_t x, int32_t y, void* p) { self(p)->keyboardFunc(key, false, false); }, this);
244+
glutSpecialFuncUcall([](int32_t key, int32_t x, int32_t y, void* p) { self(p)->keyboardFunc(key, true, true); }, this);
245+
glutSpecialUpFuncUcall([](int32_t key, int32_t x, int32_t y, void* p) { self(p)->keyboardFunc(key, true, false); }, this);
246+
glutMouseFuncUcall([](int32_t button, int32_t state, int32_t x, int32_t y, void* p) { self(p)->mouseFunc(button, state, x, y); }, this);
247+
glutMotionFuncUcall(mouseMoveFunc, this);
248+
glutPassiveMotionFuncUcall(mouseMoveFunc, this);
249+
glutMouseWheelFuncUcall([](int32_t button, int32_t dir, int32_t x, int32_t y, void* p) { self(p)->mMouseWheelFunc(button, dir, x, y); }, this);
290250
applyInitialWindowState();
291251
SetVSync(vsyncEnabled());
292252

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendGlut.h‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,14 @@ class GPUDisplayFrontendGlut : public GPUDisplayFrontend
3535
private:
3636
int32_t FrontendMain() override;
3737

38-
static void displayFunc();
39-
static void glutLoopFunc();
40-
static void keyboardUpFunc(uint8_t key, int32_t x, int32_t y);
41-
static void keyboardDownFunc(uint8_t key, int32_t x, int32_t y);
42-
static void specialUpFunc(int32_t key, int32_t x, int32_t y);
43-
static void specialDownFunc(int32_t key, int32_t x, int32_t y);
44-
static void mouseMoveFunc(int32_t x, int32_t y);
45-
static void mMouseWheelFunc(int32_t button, int32_t dir, int32_t x, int32_t y);
46-
static void mouseFunc(int32_t button, int32_t state, int32_t x, int32_t y);
47-
static void ResizeSceneWrapper(int32_t width, int32_t height);
38+
static GPUDisplayFrontendGlut* self(void* p) { return static_cast<GPUDisplayFrontendGlut*>(p); } // GLUT callback user data
39+
void displayFunc();
40+
void glutLoopFunc();
41+
void keyboardFunc(int32_t key, bool special, bool down);
42+
void mouseMoveFunc(int32_t x, int32_t y);
43+
void mMouseWheelFunc(int32_t button, int32_t dir, int32_t x, int32_t y);
44+
void mouseFunc(int32_t button, int32_t state, int32_t x, int32_t y);
45+
void ResizeSceneWrapper(int32_t width, int32_t height);
4846
static int32_t GetKey(int32_t key);
4947
static void GetKey(int32_t keyin, int32_t& keyOut, int32_t& keyPressOut, bool special);
5048

0 commit comments

Comments
 (0)