Skip to content

Commit 5ba9710

Browse files
author
Lukas Thomann
committed
refactored cth::win keyboard support and added ability to listen to keypresses
1 parent f7340a2 commit 5ba9710

9 files changed

Lines changed: 700 additions & 191 deletions

File tree

cth/incl/cth/io/keybd/keys.hpp

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "cth/os/osdef.hpp"
77
#include "cth/string/format.hpp"
88

9+
#include <chrono>
910

1011
namespace cth::io {
1112
/**
@@ -261,24 +262,63 @@ cxpr std::string_view to_utf8_string(Key key) {
261262

262263
CTH_GEN_ENUM_DEREF_OVERLOAD(cth::io::Key)
263264

265+
CTH_FORMAT_CLASS_ATTRIBUTES(
266+
cth::io::Key,
267+
"{}",
268+
([](cth::io::Key const& key) { return cth::io::to_utf8_string(key); })
269+
);
270+
264271
namespace cth::io {
265272

266273
struct ex_key {
267274
Key key;
268275
bool right = false;
276+
277+
[[nodiscard]] constexpr bool operator==(ex_key const&) const = default;
269278
};
270279

271280
struct key_state {
272-
key_state(ex_key ex_key, bool down) : exKey{ex_key}, down{down} {}
273-
key_state(Key key, bool right, bool down) : key_state{{key, right}, down} {}
281+
constexpr key_state(ex_key ex_key, bool down) : exKey{ex_key}, down{down} {}
282+
constexpr key_state(Key key, bool right, bool down) : key_state{{key, right}, down} {}
274283

275284
ex_key exKey;
276285
bool down;
286+
287+
[[nodiscard]] constexpr bool operator==(key_state const&) const = default;
277288
};
278289

279290
struct key_update {
280291
key_state data;
281-
co::time_point_t time;
292+
chrono::time_point_t time;
293+
294+
[[nodiscard]] constexpr bool operator==(key_update const&) const = default;
282295
};
283296

284297
}
298+
299+
CTH_FORMAT_CLASS_ATTRIBUTES(
300+
cth::io::ex_key,
301+
"{}, right: {}",
302+
([](cth::io::ex_key const& key) { return std::tie(key.key, key.right); })
303+
);
304+
305+
306+
CTH_FORMAT_CLASS_ATTRIBUTES(
307+
cth::io::key_state,
308+
"{}, down: {}",
309+
([](cth::io::key_state const& key){ return std::tie(key.exKey, key.down); })
310+
);
311+
312+
313+
CTH_FORMAT_CLASS_ATTRIBUTES(
314+
cth::io::key_update,
315+
"{}, time: {}",
316+
(
317+
[](cth::io::key_update const& update){
318+
return std::forward_as_tuple(
319+
update.data,
320+
std::chrono::duration_cast<std::chrono::seconds>(update.time - cth::chrono::session_start)
321+
);
322+
}
323+
)
324+
);

cth/incl/cth/string/format.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ struct std::formatter<type> {
5454
} \
5555
}
5656

57+
/**
58+
* creates a format overload for a class, implicitly includes type name with type[<fmt_string>]
59+
* @param type The class/struct type to format
60+
* @param fmt_string The format string (e.g., "ID: {}, Val: {}")
61+
* @param tie_func An expression involving 'obj' that returns a tuple/tie
62+
*/
63+
#define CTH_FORMAT_CLASS_ATTRIBUTES(type, fmt_string, tie_func)\
64+
CTH_FORMAT_CLASS(type, #type "[" fmt_string "]", tie_func)
5765

5866
/**
5967
* @brief creates a format overload for the concept

cth_win/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cth_target_enable_build_cache(cth_win OPTIONAL)
99
cth_pkg_target_include_directories(
1010
cth_win
1111
PUBLIC incl
12+
PRIVATE src
1213
)
1314

1415
target_link_libraries(cth_win PUBLIC cth::cth)
Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,64 @@
11
#pragma once
22
#include "cth/io/keybd/keys.hpp"
33

4+
#include <mutex>
5+
#include <queue>
46
#include <span>
7+
#include <vector>
58

69

7-
namespace cth::win {
8-
class keybd {
10+
namespace cth::win::keybd {
11+
12+
void send(::cth::io::key_state);
13+
void send(std::span<::cth::io::key_state const> states);
14+
15+
16+
struct event_queue {
17+
using event_t = ::cth::io::key_update;
18+
19+
event_queue();
20+
event_queue(event_queue const&) = delete;
21+
event_queue(event_queue&&) = delete;
22+
~event_queue();
23+
24+
/**
25+
* peeks the queues front
26+
*/
27+
[[nodiscard]] event_t front() const;
28+
/**
29+
* pops the queues front
30+
*/
31+
[[nodiscard]] event_t pop();
32+
/**
33+
* pops the entire queue
34+
*/
35+
[[nodiscard]] std::vector<event_t> pop_queue();
36+
37+
/**
38+
* flushes the queue
39+
*/
40+
void clear();
41+
42+
/**
43+
* pushes onto the queue
44+
* @param event to push
45+
*/
46+
void push(event_t event);
47+
48+
private:
49+
std::queue<event_t> _queue{};
50+
mutable std::mutex _queueMtx{};
51+
952
public:
10-
static void send(io::key_state);
11-
static void send(std::span<io::key_state const> states);
53+
/**
54+
* size of current buffer queue
55+
*/
56+
[[nodiscard]] size_t size() const { return _queue.size(); }
57+
58+
/**
59+
* true if empty
60+
*/
61+
[[nodiscard]] bool empty() const;
1262
};
1363

14-
}
64+
} // namespace cth::win::io

0 commit comments

Comments
 (0)