Skip to content

Commit c892c7f

Browse files
committed
GPU Display: Better handling of key presses from the console
1 parent bd430ea commit c892c7f

9 files changed

Lines changed: 28 additions & 23 deletions

‎GPU/GPUTracking/Global/GPUChainTracking.cxx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,7 @@ int32_t GPUChainTracking::RunChainFinalize()
861861
} else if (iKey == 'n') {
862862
break;
863863
} else if (iKey) {
864-
while (GetProcessingSettings().eventDisplay->getSendKey() != 0) {
865-
usleep(1000);
866-
}
867-
GetProcessingSettings().eventDisplay->setSendKey(iKey);
864+
GetProcessingSettings().eventDisplay->sendKey(iKey);
868865
}
869866
}
870867
} while (GetProcessingSettings().eventDisplay->getDisplayControl() == 0);

‎GPU/GPUTracking/display/GPUDisplayInterface.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,9 @@ class GPUDisplayFrontendInterface
5959
virtual void DisplayExit() = 0;
6060
virtual bool EnableSendKey() = 0;
6161
virtual int32_t getDisplayControl() const = 0;
62-
virtual int32_t getSendKey() const = 0;
6362
virtual void setDisplayControl(int32_t v) = 0;
6463
virtual bool IsRunning() const = 0;
65-
virtual void setSendKey(int32_t v) = 0;
64+
virtual void sendKey(int32_t v) = 0;
6665
virtual const char* frontendName() const = 0;
6766

6867
protected:

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,19 @@ void GPUDisplayFrontend::waitForFPSLimit()
8181
std::this_thread::sleep_until(mNextFrameTime);
8282
}
8383

84-
void GPUDisplayFrontend::HandleSendKey()
84+
void GPUDisplayFrontend::HandleSendKeys()
8585
{
86-
if (mSendKey) {
87-
mDisplay->HandleSendKey(mSendKey);
88-
mSendKey = 0;
86+
std::lock_guard lock(mSendKeyMutex);
87+
for (auto key : mSendKeys) {
88+
mDisplay->HandleSendKey(key);
8989
}
90+
mSendKeys.clear();
91+
}
92+
93+
void GPUDisplayFrontend::sendKey(int32_t v)
94+
{
95+
std::lock_guard lock(mSendKeyMutex);
96+
mSendKeys.emplace_back(v);
9097
}
9198

9299
void GPUDisplayFrontend::HandleKey(uint8_t key) { mDisplay->HandleKey(key); }

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

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "GPUDisplayInterface.h"
2020
#include <memory>
2121
#include <chrono>
22+
#include <mutex>
23+
#include <vector>
2224

2325
namespace o2::gpu
2426
{
@@ -64,9 +66,8 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
6466
virtual uint32_t getReqVulkanExtensions(const char**& p) { return 0; };
6567

6668
int32_t getDisplayControl() const override { return mDisplayControl; }
67-
int32_t getSendKey() const override { return mSendKey; }
6869
void setDisplayControl(int32_t v) override { mDisplayControl = v; }
69-
void setSendKey(int32_t v) override { mSendKey = v; }
70+
void sendKey(int32_t v) override;
7071

7172
frontendTypes frontendType() const { return mFrontendType; }
7273
const char* frontendName() const override { return mFrontendName; }
@@ -75,11 +76,6 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
7576
void stopGUI();
7677
bool isGUIRunning();
7778

78-
// volatile variables to exchange control informations between display and backend
79-
volatile int32_t mDisplayControl = 0; // Control for next event (=1) or quit (=2)
80-
volatile int32_t mSendKey = 0; // Key sent by external entity (usually console), may be ignored by backend.
81-
volatile int32_t mNeedUpdate = 0; // flag that backend shall update the GL window, and call DrawGLScene
82-
8379
protected:
8480
virtual int32_t FrontendMain() = 0;
8581
static void* FrontendThreadWrapper(void*);
@@ -118,6 +114,12 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
118114
static constexpr int32_t KEY_END = 29;
119115
static constexpr int32_t KEY_SPACE = 32; // fixed at 32
120116

117+
// volatile variables to exchange control informations between display and backend
118+
volatile int32_t mDisplayControl = 0; // Control for next event (=1) or quit (=2)
119+
volatile int32_t mNeedUpdate = 0; // flag that backend shall update the GL window, and call DrawGLScene
120+
std::mutex mSendKeyMutex;
121+
std::vector<int32_t> mSendKeys; // Keys sent by external entity (usually console), may be ignored by backend.
122+
121123
// Keyboard / Mouse actions
122124
bool mMouseDn = false; // Mouse button down
123125
bool mMouseDnR = false; // Right mouse button down
@@ -143,7 +145,7 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
143145

144146
void HandleKey(uint8_t key); // Callback for handling key presses
145147
int32_t DrawGLScene(); // Callback to draw the GL scene
146-
void HandleSendKey(); // Optional callback to handle key press from external source (e.g. stdin by default)
148+
void HandleSendKeys(); // Optional callback to handle key press from external source (e.g. stdin by default)
147149
void ResizeScene(int32_t width, int32_t height); // Callback when GL window is resized
148150
int32_t InitDisplay(bool initFailure = false); // Callback to initialize the GL Display (to be called in StartDisplay)
149151
void ExitDisplay(); // Callback to clean up the GL Display

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ int32_t GPUDisplayFrontendGlfw::FrontendMain()
296296
}
297297

298298
while (!glfwWindowShouldClose(mWindow)) {
299-
HandleSendKey();
299+
HandleSendKeys();
300300
if (DrawGLScene()) {
301301
fprintf(stderr, "Error drawing GL scene\n");
302302
return (1);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void GPUDisplayFrontendGlut::displayFunc() // TODO: Functions should be lambdas,
4141

4242
void GPUDisplayFrontendGlut::glutLoopFunc()
4343
{
44-
me->HandleSendKey();
44+
me->HandleSendKeys();
4545
displayFunc();
4646
}
4747

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int32_t GPUDisplayFrontendNone::FrontendMain()
3030
do {
3131
DrawGLScene();
3232
waitForFPSLimit();
33-
HandleSendKey();
33+
HandleSendKeys();
3434
} while (mDisplayControl != 2);
3535
return 0;
3636
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ int32_t GPUDisplayFrontendWayland::FrontendMain()
414414
if (wl_display_dispatch_pending(mWayland) == -1 || mDisplayControl == 2) {
415415
break;
416416
}
417-
HandleSendKey();
417+
HandleSendKeys();
418418
DrawGLScene();
419419
waitForFPSLimit();
420420
wl_surface_damage(mSurface, 0, 0, mDisplayWidth, mDisplayHeight);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ int32_t GPUDisplayFrontendX11::FrontendMain()
334334
if (mDisplayControl == 2) {
335335
break;
336336
}
337-
HandleSendKey();
337+
HandleSendKeys();
338338
if (!XPending(mDisplay)) {
339339
event.type = Expose;
340340
} else {

0 commit comments

Comments
 (0)