upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput.cpp
diff options
context:
space:
mode:
authorItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:16:16 -0500
committerItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com>2026-11-22 13:16:16 -0500
commit8599c9e64c40219a9e60e567102ea4a2252bc7dd (patch)
tree7c6e8f8a0e30e0b60ee81b61c654159fdd6f0f41 /runtime/shims/gameinput.cpp
parentcd7bcc34a33163f9a082e0864415112ec6181e53 (diff)
parent2aed56c6e50c4fa577c50e5b10976ba9f9a7379f (diff)
Merge pull request #2567 from marquezini
# Conflicts: # docs/gameinput.md
Diffstat (limited to 'runtime/shims/gameinput.cpp')
-rw-r--r--runtime/shims/gameinput.cpp74
1 files changed, 54 insertions, 20 deletions
diff --git a/runtime/shims/gameinput.cpp b/runtime/shims/gameinput.cpp
index 6f7e57f01..89b66364d 100644
--- a/runtime/shims/gameinput.cpp
+++ b/runtime/shims/gameinput.cpp
@@ -35,6 +35,7 @@
35 35
36#include <algorithm> 36#include <algorithm>
37#include <atomic> 37#include <atomic>
38#include <chrono>
38#include <cmath> 39#include <cmath>
39#include <cstdio> 40#include <cstdio>
40#include <cstdlib> 41#include <cstdlib>
@@ -660,6 +661,28 @@ void call(const Callback& c, const Event& e) {
660 } 661 }
661} 662}
662 663
664// Runs one queued event's callbacks on the calling thread, without holding the state lock.
665void deliver_event(const Event& e) {
666 State& s = g();
667 std::vector<Callback> targets;
668 {
669 std::lock_guard lock(s.mu);
670 for (const Callback& c : s.callbacks)
671 if (matches(c, e)) targets.push_back(c);
672 }
673 for (const Callback& c : targets) {
674 bool still = false;
675 {
676 std::lock_guard lock(s.mu);
677 for (const Callback& now : s.callbacks)
678 if (now.token == c.token) still = now.active;
679 }
680 if (still) call(c, e);
681 }
682 if (e.device) e.device->Release();
683 if (e.reading) e.reading->Release();
684}
685
663// Runs queued callbacks on the calling thread, without holding the state lock. Returns whether more events arrived 686// Runs queued callbacks on the calling thread, without holding the state lock. Returns whether more events arrived
664// meanwhile. 687// meanwhile.
665bool deliver() { 688bool deliver() {
@@ -670,30 +693,41 @@ bool deliver() {
670 std::lock_guard lock(s.mu); 693 std::lock_guard lock(s.mu);
671 events.swap(s.queue); 694 events.swap(s.queue);
672 } 695 }
673 const DWORD self = GetCurrentThreadId(); 696 s.delivering_thread = GetCurrentThreadId();
674 s.delivering_thread = self; 697 for (const Event& e : events) deliver_event(e);
675 for (const Event& e : events) { 698 s.delivering_thread = 0;
676 std::vector<Callback> targets; 699 std::lock_guard lock(s.mu);
700 return !s.queue.empty();
701}
702
703// IGameInputDispatcher::Dispatch: runs queued events oldest first, at least one even with a quota of 0, and takes no
704// further event once quota_us microseconds have passed (an event already started runs to the end, so the quota is a
705// target, as on Windows). While events remain the wait handle is signalled again, so a title that waits on it before
706// each call comes back for the rest. Returns whether events remain.
707bool deliver_within(uint64_t quota_us) {
708 State& s = g();
709 std::lock_guard dl(s.deliver_mu);
710 const auto start = std::chrono::steady_clock::now();
711 s.delivering_thread = GetCurrentThreadId();
712 for (bool first = true;; first = false) {
713 if (!first) {
714 const auto spent = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start);
715 if (static_cast<uint64_t>(spent.count()) >= quota_us) break;
716 }
717 Event e;
677 { 718 {
678 std::lock_guard lock(s.mu); 719 std::lock_guard lock(s.mu);
679 for (const Callback& c : s.callbacks) 720 if (s.queue.empty()) break;
680 if (matches(c, e)) targets.push_back(c); 721 e = s.queue.front();
722 s.queue.erase(s.queue.begin());
681 } 723 }
682 for (const Callback& c : targets) { 724 deliver_event(e);
683 bool still = false;
684 {
685 std::lock_guard lock(s.mu);
686 for (const Callback& now : s.callbacks)
687 if (now.token == c.token) still = now.active;
688 }
689 if (still) call(c, e);
690 }
691 if (e.device) e.device->Release();
692 if (e.reading) e.reading->Release();
693 } 725 }
694 s.delivering_thread = 0; 726 s.delivering_thread = 0;
695 std::lock_guard lock(s.mu); 727 std::lock_guard lock(s.mu);
696 return !s.queue.empty(); 728 const bool remaining = !s.queue.empty();
729 if (remaining && s.wait_event) SetEvent(s.wait_event);
730 return remaining;
697} 731}
698 732
699// ---- SDL backend (poll thread only) ----------------------------------------------------------------------------- 733// ---- SDL backend (poll thread only) -----------------------------------------------------------------------------
@@ -1801,8 +1835,8 @@ public:
1801 } 1835 }
1802 return r; 1836 return r;
1803 } 1837 }
1804 // Runs the queued callbacks on the caller's thread. The quota is not enforced: the queue is short. 1838 // Runs the queued callbacks on the caller's thread within the quota (see deliver_within).
1805 bool STDMETHODCALLTYPE Dispatch(uint64_t) override { return deliver(); } 1839 bool STDMETHODCALLTYPE Dispatch(uint64_t quotaInMicroseconds) override { return deliver_within(quotaInMicroseconds); }
1806 HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override { 1840 HRESULT STDMETHODCALLTYPE OpenWaitHandle(HANDLE* handle) override {
1807 if (!handle) return E_POINTER; 1841 if (!handle) return E_POINTER;
1808 *handle = nullptr; 1842 *handle = nullptr;