[feat] extend keyboard and pointer control with bounded held input

This commit is contained in:
dijunkun
2026-11-06 20:54:28 +08:00
parent dd4baf2fc4
commit 12afcce690
11 changed files with 141 additions and 11 deletions
@@ -74,6 +74,10 @@ public:
int max_bitrate_bps = -1);
void SetControl(bool enabled);
void ReleaseInput();
void ReleaseActionInput();
bool KeyPressed(int key) const { return keys_.count(key) != 0; }
bool ButtonPressed(int flag) const { return buttons_.count(flag) != 0; }
bool HasPressedInput() const { return !keys_.empty() || !buttons_.empty(); }
bool Text(const std::string &utf8);
bool Key(int key, bool down);
bool Pointer(float x, float y, int flag, int wheel = 0);
@@ -137,6 +141,7 @@ private:
HostPlatform host_platform_ = HostPlatform::Unknown;
std::vector<HostDisplay> displays_;
std::set<int> keys_, buttons_;
std::set<int> action_keys_, action_buttons_;
float pointer_x_ = .5f, pointer_y_ = .5f;
std::chrono::steady_clock::time_point deadline_;
mutable std::mutex mutex_;
@@ -26,6 +26,8 @@ std::optional<json> ControllerSession::TakeRemoteResult(uint64_t request) {
}
void ControllerSession::BeginInput() {
action_keys_ = keys_;
action_buttons_ = buttons_;
for (const auto id : input_requests_) remote_results_.erase(id);
input_requests_.clear();
input_result_ = "applied";
@@ -476,6 +476,13 @@ bool ControllerSession::Key(int key, bool down) {
keys_.erase(key);
return sent;
}
void ControllerSession::ReleaseActionInput() {
const auto buttons = buttons_, keys = keys_;
for (int button : buttons)
if (!action_buttons_.count(button)) Pointer(pointer_x_, pointer_y_, button + 1);
for (auto it = keys.rbegin(); it != keys.rend(); ++it)
if (!action_keys_.count(*it)) Key(*it, false);
}
bool ControllerSession::Pointer(float x, float y, int flag, int wheel) {
const bool releasing =
flag >= 2 && flag <= 6 && !(flag & 1) && buttons_.count(flag - 1);
@@ -523,6 +530,8 @@ json ControllerSession::Status() const {
{"error", error_},
{"generation", generation_},
{"control", control_},
{"held_key_codes", keys_},
{"held_mouse_flags", buttons_},
{"displays", ds},
{"platform", HostPlatformName(host_platform_)},
{"display", options_.display},