Skip to content

Commit 154be55

Browse files
add queueing system to fix AI generated control slop. works for movement. need to replace hotkeys and such
1 parent 7bf219f commit 154be55

6 files changed

Lines changed: 86 additions & 31 deletions

File tree

include/WindowManager/WindowManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class WindowManagerInterface
5959
virtual void createAndRegisterApps(char** envp) = 0;
6060
virtual optional<entt::entity> getCurrentlyFocusedApp() = 0;
6161
virtual optional<entt::entity> getPendingFocusedApp() = 0;
62+
virtual bool hasCurrentOrPendingFocus() = 0;
6263
virtual void focusApp(entt::entity) = 0;
6364
virtual void wire(shared_ptr<WindowManagerInterface>, Camera* camera, Renderer* renderer) = 0;
6465
virtual void handleSubstructure() = 0;
@@ -179,6 +180,7 @@ class WindowManager : public WindowManagerInterface
179180
~WindowManager();
180181
optional<entt::entity> getCurrentlyFocusedApp() override;
181182
optional<entt::entity> getPendingFocusedApp() override { return pendingFocusedApp; }
183+
bool hasCurrentOrPendingFocus() override;
182184
void focusApp(entt::entity) override;
183185
void wire(WindowManagerPtr sharedThis, Camera* camera, Renderer* renderer) override;
184186
void handleSubstructure() override;

include/controls.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#pragma once
22

3-
#include "blocks.h"
4-
#include "camera.h"
53
#include <functional>
64
#include <mutex>
5+
#include <queue>
6+
#include <unordered_set>
77
#include <glad/glad.h>
88
#include <GLFW/glfw3.h>
99
#include <xkbcommon/xkbcommon.h>
1010
#include <memory>
11+
#include "blocks.h"
12+
#include "camera.h"
1113
#include "app.h"
1214
#include "world.h"
1315
#include "WindowManager/WindowManager.h"
@@ -54,7 +56,16 @@ class Controls
5456
shared_ptr<WindowManager::Space> windowManagerSpace;
5557
bool keysEnabled = true;
5658

57-
void handleControls(GLFWwindow* window, Camera* camera);
59+
std::unordered_set<xkb_keysym_t> pressed;
60+
61+
struct KeysymEvent {
62+
xkb_keysym_t sym;
63+
bool pressed;
64+
};
65+
std::queue<KeysymEvent> keysymQueue;
66+
std::mutex keysymMutex;
67+
68+
void handleMovement();
5869
void handleQuit(GLFWwindow* window);
5970
void handleModEscape(GLFWwindow* window);
6071
void handleToggleCursor(GLFWwindow* window);
@@ -75,6 +86,7 @@ class Controls
7586
void handleMakeWindowBootable(GLFWwindow* window);
7687

7788
void handleKeys(GLFWwindow* window, Camera* camera, World* world);
89+
void handleKeys();
7890
void handleClicks(GLFWwindow* window, World* world);
7991
void doAfter(shared_ptr<bool> isDone, function<void()> actionFn);
8092
void doDeferedActions();
@@ -95,6 +107,8 @@ class Controls
95107
lastWaylandFocusActive = false;
96108
}
97109
void poll(GLFWwindow* window, Camera* camera, World* world);
110+
void poll();
111+
void pollPressedKeys();
98112
void mouseCallback(GLFWwindow* window, double xpos, double ypos);
99113
void goToApp(entt::entity);
100114
void moveTo(glm::vec3 pos,

src/WindowManager/WindowManager.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,20 @@ WindowManager::wire(WindowManagerPtr sharedThis,
339339
this->camera = camera;
340340
}
341341

342+
bool
343+
WindowManager::hasCurrentOrPendingFocus()
344+
{
345+
auto rv = false;
346+
std::optional<entt::entity> focusCandidate = getPendingFocusedApp();
347+
if (!focusCandidate) {
348+
focusCandidate = getCurrentlyFocusedApp();
349+
}
350+
if (focusCandidate) {
351+
rv = true;
352+
}
353+
return rv;
354+
}
355+
342356
optional<entt::entity> WindowManager::getCurrentlyFocusedApp() {
343357
return currentlyFocusedApp;
344358
}

src/controls.cpp

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
6067
void
6168
Controls::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+
6983
void
7084
Controls::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

256269
void
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+
474501
ControlResponse
475502
Controls::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);

src/engine.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ Engine::frame(double frameStart)
267267
api->mutateEntities();
268268
}
269269

270-
271-
270+
controls->pollPressedKeys();
272271
renderer->render();
273272

274273
if (engineGui) {
@@ -310,10 +309,7 @@ Engine::frame(double frameStart)
310309
disableKeysIfImguiActive();
311310
}
312311
if (controls) {
313-
controls->runQueuedActions();
314-
if (window != nullptr) {
315-
controls->poll(window, camera, world);
316-
}
312+
controls->poll();
317313
}
318314
multiplayerClientIteration(frameStart);
319315

src/wayland/wlr_compositor.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -854,20 +854,10 @@ process_key_sym(WlrServer* server,
854854
bool waylandFocusActive = false;
855855
if (server->engine) {
856856
if (auto wm = server->engine->getWindowManager()) {
857-
std::optional<entt::entity> focusCandidate = wm->getPendingFocusedApp();
858-
if (!focusCandidate) {
859-
focusCandidate = wm->getCurrentlyFocusedApp();
860-
}
861-
if (focusCandidate) {
862-
if (server->registry &&
863-
server->registry->all_of<WaylandApp::Component>(*focusCandidate)) {
864-
waylandFocusActive = true;
865-
}
866-
}
857+
waylandFocusActive = wm->hasCurrentOrPendingFocus();
867858
}
868859
}
869-
870-
if (pressed && server->engine) {
860+
if (server->engine) {
871861
Controls* controls = server->engine->getControls();
872862
uint32_t mods = keyboard ? wlr_keyboard_get_modifiers(keyboard) : 0;
873863
bool modifierHeld =
@@ -897,6 +887,7 @@ process_key_sym(WlrServer* server,
897887
}
898888
}
899889
}
890+
/*
900891
switch (sym) {
901892
case XKB_KEY_w:
902893
case XKB_KEY_W:
@@ -933,6 +924,8 @@ process_key_sym(WlrServer* server,
933924
default:
934925
break;
935926
}
927+
*/
928+
936929
if constexpr (kWlrootsDebugLogs) {
937930
FILE* f = std::fopen("/tmp/matrix-wlroots-wm.log", "a");
938931
if (f) {

0 commit comments

Comments
 (0)