@@ -57,6 +57,13 @@ Controls::mouseCallback(GLFWwindow* window, double xpos, double ypos)
5757 }
5858}
5959
60+ void
61+ Controls::poll () {
62+ runQueuedActions ();
63+ pollPressedKeys ();
64+ handleKeys ();
65+ }
66+
6067void
6168Controls::poll (GLFWwindow* window, Camera* camera, World* world)
6269{
@@ -66,13 +73,19 @@ Controls::poll(GLFWwindow* window, Camera* camera, World* world)
6673 doDeferedActions ();
6774}
6875
76+ void Controls::handleKeys () {
77+ auto appFocused = wm->hasCurrentOrPendingFocus ();
78+ if (!appFocused){
79+ handleMovement ();
80+ }
81+ }
82+
6983void
7084Controls::handleKeys (GLFWwindow* window, Camera* camera, World* world)
7185{
7286 handleQuit (window);
7387 if (keysEnabled) {
7488 handleModEscape (window);
75- handleControls (window, camera);
7689 handleToggleCursor (window);
7790 handleToggleApp (window, world, camera);
7891 handleScreenshot (window);
@@ -254,13 +267,13 @@ Controls::handleClicks(GLFWwindow* window, World* world)
254267}
255268
256269void
257- Controls::handleControls (GLFWwindow* window, Camera* camera )
270+ Controls::handleMovement ( )
258271{
259272
260- bool up = glfwGetKey (window, GLFW_KEY_W) == GLFW_PRESS ;
261- bool down = glfwGetKey (window, GLFW_KEY_S) == GLFW_PRESS ;
262- bool left = glfwGetKey (window, GLFW_KEY_A) == GLFW_PRESS ;
263- bool right = glfwGetKey (window, GLFW_KEY_D) == GLFW_PRESS ;
273+ bool up = pressed. count (XKB_KEY_w) > 0 ;
274+ bool down = pressed. count (XKB_KEY_s) > 0 ;
275+ bool left = pressed. count (XKB_KEY_a) > 0 ;
276+ bool right = pressed. count (XKB_KEY_d) > 0 ;
264277 camera->handleTranslateForce (up, down, left, right);
265278}
266279
@@ -471,19 +484,42 @@ Controls::handlePointerButton(uint32_t button, bool pressed)
471484 return false ;
472485}
473486
487+ void Controls::pollPressedKeys () {
488+ std::lock_guard<std::mutex> lock (keysymMutex);
489+ KeysymEvent keysymEvent;
490+ while (keysymQueue.size () > 0 ) {
491+ keysymEvent = keysymQueue.front ();
492+ if (keysymEvent.pressed ) {
493+ pressed.insert (keysymEvent.sym );
494+ } else {
495+ pressed.erase (keysymEvent.sym );
496+ }
497+ keysymQueue.pop ();
498+ }
499+ }
500+
474501ControlResponse
475502Controls::handleKeySym (xkb_keysym_t sym,
476- bool pressed ,
503+ bool is_pressed ,
477504 bool modifierHeld,
478505 bool shiftHeld,
479506 bool waylandFocusActive)
480507{
481- ControlResponse resp;
508+
509+ {
510+ std::lock_guard<std::mutex> lock (keysymMutex);
511+ keysymQueue.push (KeysymEvent{sym, is_pressed});
512+ }
513+
514+
482515 lastWaylandFocusActive = waylandFocusActive;
483- if (!pressed) {
516+ ControlResponse resp;
517+ if (!is_pressed) {
484518 return resp;
485519 }
486520
521+ // handleControls(sym);
522+
487523 if (sym == XKB_KEY_Shift_L || sym == XKB_KEY_Shift_R) {
488524 lastShiftPressTime = nowSeconds ();
489525 log_controls (" controls: shift pressed\n " );
@@ -497,7 +533,7 @@ Controls::handleKeySym(xkb_keysym_t sym,
497533 sym == XKB_KEY_underscore || sym == XKB_KEY_0 || sym == XKB_KEY_9) {
498534 log_controls (" controls: debug sym=%u pressed=%d modifier=%d shift=%d focus=%d\n " ,
499535 sym,
500- pressed ? 1 : 0 ,
536+ is_pressed ? 1 : 0 ,
501537 modifierHeld ? 1 : 0 ,
502538 shiftHeld ? 1 : 0 ,
503539 waylandFocusActive ? 1 : 0 );
0 commit comments