Project
Loading...
Searching...
No Matches
GPUDisplayFrontendGlut.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15// Now the other headers
19#include "GPULogging.h"
20#include <cstdio>
21#include <cstring>
22#include <GL/freeglut.h>
23#include <unistd.h>
24
25#include <pthread.h>
26using namespace o2::gpu;
27static GPUDisplayFrontendGlut* me = nullptr;
28
34
35void GPUDisplayFrontendGlut::displayFunc()
36{
37 me->DrawGLScene();
38 glutSwapBuffers();
39}
40
41void GPUDisplayFrontendGlut::glutLoopFunc()
42{
43 me->HandleSendKey();
44 displayFunc();
45}
46
47int32_t GPUDisplayFrontendGlut::GetKey(int32_t key)
48{
49 if (key == GLUT_KEY_UP) {
50 return KEY_UP;
51 }
52 if (key == GLUT_KEY_DOWN) {
53 return KEY_DOWN;
54 }
55 if (key == GLUT_KEY_LEFT) {
56 return KEY_LEFT;
57 }
58 if (key == GLUT_KEY_RIGHT) {
59 return KEY_RIGHT;
60 }
61 if (key == GLUT_KEY_PAGE_UP) {
62 return KEY_PAGEUP;
63 }
64 if (key == GLUT_KEY_PAGE_DOWN) {
65 return KEY_PAGEDOWN;
66 }
67 if (key == GLUT_KEY_HOME) {
68 return KEY_HOME;
69 }
70 if (key == GLUT_KEY_END) {
71 return KEY_END;
72 }
73 if (key == GLUT_KEY_INSERT) {
74 return KEY_INSERT;
75 }
76 if (key == GLUT_KEY_F1) {
77 return KEY_F1;
78 }
79 if (key == GLUT_KEY_F2) {
80 return KEY_F2;
81 }
82 if (key == GLUT_KEY_F3) {
83 return KEY_F3;
84 }
85 if (key == GLUT_KEY_F4) {
86 return KEY_F4;
87 }
88 if (key == GLUT_KEY_F5) {
89 return KEY_F5;
90 }
91 if (key == GLUT_KEY_F6) {
92 return KEY_F6;
93 }
94 if (key == GLUT_KEY_F7) {
95 return KEY_F7;
96 }
97 if (key == GLUT_KEY_F8) {
98 return KEY_F8;
99 }
100 if (key == GLUT_KEY_F9) {
101 return KEY_F9;
102 }
103 if (key == GLUT_KEY_F10) {
104 return KEY_F10;
105 }
106 if (key == GLUT_KEY_F11) {
107 return KEY_F11;
108 }
109 if (key == GLUT_KEY_F12) {
110 return KEY_F12;
111 }
112 if (key == 112 || key == 113) {
113 return KEY_SHIFT;
114 }
115 if (key == 114) {
116 return KEY_CTRL;
117 }
118 if (key == 115) {
119 return KEY_RCTRL;
120 }
121 if (key == 116) {
122 return KEY_ALT;
123 }
124
125 return (0);
126}
127
128void GPUDisplayFrontendGlut::GetKey(int32_t key, int32_t& keyOut, int32_t& keyPressOut, bool special)
129{
130 int32_t specialKey = special ? GetKey(key) : 0;
131 // GPUInfo("Key: key %d (%c) (special %d) -> %d (%c) special %d (%c)", key, (char) key, (int32_t) special, (int32_t) key, key, specialKey, (char) specialKey);
132
133 if (specialKey) {
134 keyOut = keyPressOut = specialKey;
135 } else {
136 keyOut = keyPressOut = key;
137 if (keyPressOut >= 'a' && keyPressOut <= 'z') {
138 keyPressOut += 'A' - 'a';
139 }
140 }
141}
142
143void GPUDisplayFrontendGlut::keyboardDownFunc(uint8_t key, int32_t x, int32_t y)
144{
145 int32_t handleKey = 0, keyPress = 0;
146 GetKey(key, handleKey, keyPress, false);
147 me->mKeysShift[keyPress] = glutGetModifiers() & GLUT_ACTIVE_SHIFT;
148 me->mKeys[keyPress] = true;
149 me->HandleKey(handleKey);
150}
151
152void GPUDisplayFrontendGlut::keyboardUpFunc(uint8_t key, int32_t x, int32_t y)
153{
154 int32_t handleKey = 0, keyPress = 0;
155 GetKey(key, handleKey, keyPress, false);
156 me->mKeys[keyPress] = false;
157 me->mKeysShift[keyPress] = false;
158}
159
160void GPUDisplayFrontendGlut::specialDownFunc(int32_t key, int32_t x, int32_t y)
161{
162 int32_t handleKey = 0, keyPress = 0;
163 GetKey(key, handleKey, keyPress, true);
164 me->mKeysShift[keyPress] = glutGetModifiers() & GLUT_ACTIVE_SHIFT;
165 me->mKeys[keyPress] = true;
166 me->HandleKey(handleKey);
167}
168
169void GPUDisplayFrontendGlut::specialUpFunc(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] = false;
174 me->mKeysShift[keyPress] = false;
175}
176
177void GPUDisplayFrontendGlut::ResizeSceneWrapper(int32_t width, int32_t height)
178{
179 if (!me->mFullScreen) {
180 me->mWidth = width;
181 me->mHeight = height;
182 }
183 me->ResizeScene(width, height);
184}
185
186void GPUDisplayFrontendGlut::mouseFunc(int32_t button, int32_t state, int32_t x, int32_t y)
187{
188 if (button == 3) {
189 me->mMouseWheel += 100;
190 } else if (button == 4) {
191 me->mMouseWheel -= 100;
192 } else if (state == GLUT_DOWN) {
193 if (button == GLUT_LEFT_BUTTON) {
194 me->mMouseDn = true;
195 } else if (button == GLUT_RIGHT_BUTTON) {
196 me->mMouseDnR = true;
197 }
198 me->mMouseDnX = x;
199 me->mMouseDnY = y;
200 } else if (state == GLUT_UP) {
201 if (button == GLUT_LEFT_BUTTON) {
202 me->mMouseDn = false;
203 } else if (button == GLUT_RIGHT_BUTTON) {
204 me->mMouseDnR = false;
205 }
206 }
207}
208
209void GPUDisplayFrontendGlut::mouseMoveFunc(int32_t x, int32_t y)
210{
211 me->mMouseMvX = x;
212 me->mMouseMvY = y;
213}
214
215void GPUDisplayFrontendGlut::mMouseWheelFunc(int32_t button, int32_t dir, int32_t x, int32_t y) { me->mMouseWheel += dir; }
216
217int32_t GPUDisplayFrontendGlut::FrontendMain()
218{
219 if (backend()->backendType() != GPUDisplayBackend::TYPE_OPENGL) {
220 fprintf(stderr, "Only OpenGL backend supported\n");
221 return 1;
222 }
223 me = this;
224 mCanDrawText = 1;
225 if (drawTextFontSize() == 0) {
226 drawTextFontSize() = 12;
227 }
228
229 int32_t nopts = 2;
230 char opt1[] = "progname";
231 char opt2[] = "-direct";
232 char* opts[] = {opt1, opt2};
233 glutInit(&nopts, opts);
234 glutInitContextVersion(GL_MIN_VERSION_MAJOR, GL_MIN_VERSION_MINOR);
235 glutInitContextProfile(mBackend->CoreProfile() ? GLUT_CORE_PROFILE : GLUT_COMPATIBILITY_PROFILE);
236 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
237 glutInitWindowSize(INIT_WIDTH, INIT_HEIGHT);
238 glutCreateWindow(DISPLAY_WINDOW_NAME);
239 glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
240
241 if (mBackend->ExtInit()) {
242 fprintf(stderr, "Error initializing GL extension wrapper\n");
243 return (-1);
244 }
245 if (InitDisplay()) {
246 fprintf(stderr, "Error in OpenGL initialization\n");
247 return (1);
248 }
249
250 glutDisplayFunc(displayFunc);
251 glutIdleFunc(glutLoopFunc);
252 glutReshapeFunc(ResizeSceneWrapper);
253 glutKeyboardFunc(keyboardDownFunc);
254 glutKeyboardUpFunc(keyboardUpFunc);
255 glutSpecialFunc(specialDownFunc);
256 glutSpecialUpFunc(specialUpFunc);
257 glutMouseFunc(mouseFunc);
258 glutMotionFunc(mouseMoveFunc);
259 glutPassiveMotionFunc(mouseMoveFunc);
260 glutMouseWheelFunc(mMouseWheelFunc);
261 ToggleMaximized(true);
262
263 pthread_mutex_lock(&mSemLockExit);
264 mGlutRunning = true;
265 pthread_mutex_unlock(&mSemLockExit);
266 glutMainLoop();
267 mDisplayControl = 2;
268 pthread_mutex_lock(&mSemLockExit);
269 mGlutRunning = false;
270 pthread_mutex_unlock(&mSemLockExit);
271
272 ExitDisplay();
273 return 0;
274}
275
277{
278 pthread_mutex_lock(&mSemLockExit);
279 if (mGlutRunning) {
280 glutLeaveMainLoop();
281 }
282 pthread_mutex_unlock(&mSemLockExit);
283 while (mGlutRunning) {
284 usleep(10000);
285 }
286}
287
288void GPUDisplayFrontendGlut::OpenGLPrint(const char* s, float x, float y, float r, float g, float b, float a, bool fromBotton)
289{
290#ifndef GPUCA_DISPLAY_OPENGL_CORE
291 if (!fromBotton) {
292 y = mDisplayHeight - y;
293 }
294 glColor4f(r, g, b, a);
295 glRasterPos2f(x, y);
296 glutBitmapString(GLUT_BITMAP_HELVETICA_12, (const uint8_t*)s);
297#endif
298}
299
301{
302 mFullScreen = set;
303 if (set) {
304 glutFullScreen();
305 } else {
306 glutReshapeWindow(mWidth, mHeight);
307 }
308}
309
312
314{
315 static pthread_t hThread;
316 if (pthread_create(&hThread, nullptr, FrontendThreadWrapper, this)) {
317 GPUError("Coult not Create GL Thread...");
318 return (1);
319 }
320 return (0);
321}
int32_t GetKey(int32_t key)
StringRef key
void OpenGLPrint(const char *s, float x, float y, float r, float g, float b, float a, bool fromBotton=true) override
static constexpr int32_t KEY_F9
static constexpr int32_t KEY_F2
static void * FrontendThreadWrapper(void *)
static constexpr int32_t KEY_ALT
static constexpr int32_t KEY_F12
static constexpr int32_t KEY_F7
static constexpr int32_t KEY_END
static constexpr int32_t KEY_PAGEDOWN
static constexpr int32_t KEY_F10
static constexpr int32_t KEY_F4
static constexpr int32_t KEY_LEFT
static constexpr int32_t KEY_F6
static constexpr int32_t KEY_CTRL
static constexpr int32_t KEY_F1
static constexpr const char * DISPLAY_WINDOW_NAME
static constexpr int32_t INIT_WIDTH
static constexpr int32_t GL_MIN_VERSION_MAJOR
static constexpr int32_t KEY_SHIFT
static constexpr int32_t KEY_F3
static constexpr int32_t KEY_INSERT
static constexpr int32_t KEY_F11
static constexpr int32_t KEY_F5
static constexpr int32_t KEY_PAGEUP
static constexpr int32_t KEY_HOME
static constexpr int32_t GL_MIN_VERSION_MINOR
int32_t InitDisplay(bool initFailure=false)
static constexpr int32_t KEY_UP
static constexpr int32_t KEY_DOWN
static constexpr int32_t KEY_F8
static constexpr int32_t INIT_HEIGHT
static constexpr int32_t KEY_RIGHT
static constexpr int32_t KEY_RCTRL
GLint GLenum GLint x
Definition glcorearb.h:403
GLint GLsizei GLsizei height
Definition glcorearb.h:270
GLint GLsizei width
Definition glcorearb.h:270
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLint y
Definition glcorearb.h:270
GLboolean GLboolean g
Definition glcorearb.h:1233
GLboolean enable
Definition glcorearb.h:3991
GLboolean r
Definition glcorearb.h:1233
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
constexpr value_T me
Definition TrackUtils.h:128