Gamp v0.0.7-54-gccdc599
Gamp: Graphics, Audio, Multimedia and Processing
Loading...
Searching...
No Matches
KeyEventMngr.hpp
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2025 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24#ifndef GAMP_WTKEYBOARD_HPP_
25#define GAMP_WTKEYBOARD_HPP_
26
27#include <jau/bitfield.hpp>
28#include <jau/int_types.hpp>
29#include <jau/enum_util.hpp>
30#include <jau/int_math_ct.hpp>
31#include <jau/fraction_type.hpp>
32#include <jau/cow_darray.hpp>
33
36
37namespace gamp::wt::event {
38 /** \addtogroup Gamp_WT
39 *
40 * @{
41 */
42
44 private:
46 PressedKeyCodes m_keybuffer;
47 InputModifier m_modifiers;
48
49 bool isTracked(VKeyCode keyCode) {
50 return ( 0xFFFF & *keyCode ) < m_keybuffer.bit_size;
51 }
52
53 bool setPressed(VKeyCode keyCode, bool pressed) {
54 const size_t v = 0xFFFFU & *keyCode;
55 if( v < m_keybuffer.bit_size ) {
56 return m_keybuffer.put(v, pressed);
57 }
58 return false;
59 }
60
61 public:
62 KeyEventManager() noexcept
63 : m_modifiers(InputModifier::none) { }
64
65 InputModifier modifier() const noexcept override { return m_modifiers; }
66
67 bool isPressed(VKeyCode keyCode) const noexcept override {
68 const size_t v = 0xFFFFU & *keyCode;
69 if( v < m_keybuffer.bit_size ) {
70 return m_keybuffer.get(v);
71 }
72 return false;
73 }
74
75 const PressedKeyCodes& pressedKeyCodes() const noexcept override { return m_keybuffer; }
76
77 void dispatchPressed(const jau::fraction_timespec& when, const WindowRef& source,
78 VKeyCode keySym, InputModifier keySymMods, uint16_t keyChar) noexcept
79 {
80 const InputModifier clrRepeatMod = is_set(m_modifiers, InputModifier::repeat) &&
82 m_modifiers |= keySymMods;
83 m_modifiers &= ~clrRepeatMod;
84 KeyEvent ke(EVENT_KEY_PRESSED, when, source, m_modifiers, keySym, keySymMods, keyChar);
85 setPressed(keySym, true);
86 for(const KeyListenerRef& kl : *m_keyListener.snapshot()) {
87 try {
88 kl->keyPressed(ke, *this);
89 } catch (std::exception &err) {
90 ERR_PRINT("KeyboardManager::dispatch (p): %s: Caught exception %s", ke.toString().c_str(), err.what());
91 }
92 if( ke.consumed() ) { break; }
93 }
94 }
95 void dispatchReleased(const jau::fraction_timespec& when, const WindowRef& source,
96 VKeyCode keySym, InputModifier keySymMods, uint16_t keyChar) noexcept
97 {
98 const InputModifier clrRepeatMod = is_set(m_modifiers, InputModifier::repeat) &&
100 m_modifiers &= ~clrRepeatMod;
101 KeyEvent ke(EVENT_KEY_RELEASED, when, source, m_modifiers, keySym, keySymMods, keyChar);
102 m_modifiers &= ~keySymMods;
103 setPressed(keySym, false);
104 for(const KeyListenerRef& kl : *m_keyListener.snapshot()) {
105 try {
106 kl->keyReleased(ke, *this);
107 } catch (std::exception &err) {
108 ERR_PRINT("KeyboardManager::dispatch (r): %s: Caught exception %s", ke.toString().c_str(), err.what());
109 }
110 if( ke.consumed() ) { break; }
111 }
112 }
113
114 void addListener(const KeyListenerRef& l) { m_keyListener.push_back(l); }
115
117 return m_keyListener.erase_matching(l, true,
118 [](const KeyListenerRef& a, const KeyListenerRef& b) noexcept -> bool { return a.get() == b.get(); } );
119 }
120
122 const size_t count = m_keyListener.size();
123 m_keyListener.clear(true);
124 return count;
125 }
126
127 size_t listenerCount() const noexcept { return m_keyListener.size(); }
128 };
129
130 /**@}*/
131}
132
133#endif /* GAMP_WTKEYBOARD_HPP_ */
const PressedKeyCodes & pressedKeyCodes() const noexcept override
size_t removeListener(const KeyListenerRef &l)
void addListener(const KeyListenerRef &l)
bool isPressed(VKeyCode keyCode) const noexcept override
void dispatchReleased(const jau::fraction_timespec &when, const WindowRef &source, VKeyCode keySym, InputModifier keySymMods, uint16_t keyChar) noexcept
size_t listenerCount() const noexcept
InputModifier modifier() const noexcept override
void dispatchPressed(const jau::fraction_timespec &when, const WindowRef &source, VKeyCode keySym, InputModifier keySymMods, uint16_t keyChar) noexcept
std::string toString() const noexcept
Definition KeyEvent.hpp:855
jau::bitfield< 256 > PressedKeyCodes
Definition KeyEvent.hpp:880
constexpr bool consumed() const noexcept
Consumed events will stop traversing through listener.
Definition Event.hpp:88
Implementation of a Copy-On-Write (CoW) using jau::darray as the underlying storage,...
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
Definition debug.hpp:122
constexpr bool is_set(const E mask, const E bits) noexcept
std::shared_ptr< KeyListener > KeyListenerRef
Definition KeyEvent.hpp:909
VKeyCode
Virtual key code following UTF16 specification.
Definition KeyEvent.hpp:45
static constexpr uint16_t EVENT_KEY_PRESSED
A key has been pressed, excluding auto-repeat-modifier keys.
Definition Event.hpp:57
std::shared_ptr< Window > WindowRef
Definition Event.hpp:36
static constexpr uint16_t EVENT_KEY_RELEASED
A key has been released, excluding auto-repeat-modifier keys.
Definition Event.hpp:59
@ repeat
Event is caused by auto-repeat.
Definition Event.hpp:175
Timespec structure using int64_t for its components in analogy to struct timespec_t on 64-bit platfor...