Fcitx
event.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 
8 #include "event.h"
9 #include <algorithm>
10 #include <cstdint>
11 #include <memory>
12 #include <string>
13 #include <span>
14 #include "fcitx-utils/key.h"
15 #include "fcitx-utils/keysym.h"
16 #include "fcitx-utils/macros.h"
17 
18 namespace fcitx {
19 
21 public:
22  VirtualKeyboardEventPrivate(bool isRelease, int time)
23  : isRelease_(isRelease), time_(time) {}
24 
25  Key key_;
26  bool isRelease_ = false;
27  int time_ = 0;
28  uint64_t userAction_ = 0;
29  std::string text_;
30  bool isLongPress_ = false;
31  float x_ = 0.0F, y_ = 0.0F;
32 };
33 
34 Event::~Event() {}
35 
36 KeyEventBase::KeyEventBase(EventType type, InputContext *context, Key rawKey,
37  bool isRelease, int time)
38  : InputContextEvent(context, type), key_(rawKey.normalize()),
39  origKey_(rawKey), rawKey_(rawKey), isRelease_(isRelease), time_(time) {}
40 
41 bool KeyEventBase::check(const Key &key, KeyEventMatchingModes modes) const {
42  if (modes.test(KeyEventMatchingMode::MatchNormalizedKey) && !key.code() &&
43  key_.check(key)) {
44  return true;
45  }
46 
47  if ((modes.test(KeyEventMatchingMode::MatchRawKey) ||
49  key.code())) &&
50  rawKey_.check(key)) {
51  return true;
52  }
53 
54  if ((modes.test(KeyEventMatchingMode::MatchOrigKey) ||
56  key.code())) &&
57  origKey_.check(key)) {
58  return true;
59  }
60 
62  !key.code() && origKey_.normalize().check(key)) {
63  return true;
64  }
65  return false;
66 }
67 
68 bool KeyEventBase::checkKeyList(std::span<const Key> keys,
69  KeyEventMatchingModes mode) const {
70  return std::ranges::any_of(
71  keys, [this, mode](const Key &key) { return check(key, mode); });
72 }
73 
74 VirtualKeyboardEvent::VirtualKeyboardEvent(InputContext *context,
75  bool isRelease, int time)
76  : InputContextEvent(context, EventType::InputContextVirtualKeyboardEvent),
77  d_ptr(std::make_unique<VirtualKeyboardEventPrivate>(isRelease, time)) {}
78 
79 FCITX_DEFINE_DEFAULT_DTOR(VirtualKeyboardEvent);
80 
81 FCITX_DEFINE_PROPERTY_PRIVATE(VirtualKeyboardEvent, Key, key, setKey);
82 FCITX_DEFINE_PROPERTY_PRIVATE(VirtualKeyboardEvent, uint64_t, userAction,
83  setUserAction);
84 FCITX_DEFINE_PROPERTY_PRIVATE(VirtualKeyboardEvent, std::string, text, setText);
85 FCITX_DEFINE_PROPERTY_PRIVATE(VirtualKeyboardEvent, bool, isLongPress,
86  setLongPress);
87 FCITX_DEFINE_READ_ONLY_PROPERTY_PRIVATE(VirtualKeyboardEvent, float, x);
88 FCITX_DEFINE_READ_ONLY_PROPERTY_PRIVATE(VirtualKeyboardEvent, float, y);
89 FCITX_DEFINE_READ_ONLY_PROPERTY_PRIVATE(VirtualKeyboardEvent, int, time);
90 
91 void VirtualKeyboardEvent::setPosition(float x, float y) {
92  FCITX_D();
93  d->x_ = x;
94  d->y_ = y;
95 }
96 
97 std::unique_ptr<KeyEvent> fcitx::VirtualKeyboardEvent::toKeyEvent() const {
98  FCITX_D();
99  if (!d->key_.isValid()) {
100  return nullptr;
101  }
102 
103  Key key{d->key_.sym(), d->key_.states() | KeyState::Virtual,
104  d->key_.code()};
105  return std::make_unique<KeyEvent>(inputContext(), key, d->isRelease_,
106  time());
107 }
108 
109 } // namespace fcitx
bool check(const Key &key) const
Check if current key match the key.
Definition: key.cpp:385
Describe a Key in fcitx.
Definition: key.h:42
bool checkKeyList(std::span< const Key > keys, KeyEventMatchingModes mode=KeyEventMatchingMode::MatchNormalizedKey) const
Check if the key event matches any key in the given list according to the specified mode...
Definition: event.cpp:68
Match the original key event.
EventType
Type of input method events.
Definition: event.h:67
Definition: action.cpp:17
Match the original key event before layout conversion, after normalization.
Match the normalized key event.
Class provides bit flag support for Enum.
Definition: flags.h:33
bool check(const Key &key, KeyEventMatchingModes mode=KeyEventMatchingMode::MatchNormalizedKey) const
Check if the key event matches the given key according to the specified mode.
Definition: event.cpp:41
Key sym related types.
An input context represents a client of Fcitx.
Definition: inputcontext.h:50
Class to represent a key.
Key normalize() const
Normalize a key, usually used when key is from frontend.
Definition: key.cpp:493