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>
2930
3031#include < pthread.h>
3132using namespace o2 ::gpu;
32- static GPUDisplayFrontendGlut* me = nullptr ;
3333
3434GPUDisplayFrontendGlut::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
4848void 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
5858int32_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
139119void 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
184144void 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
211171void 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
238198void 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
246206int32_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
0 commit comments