openMSX
HotKey.hh
Go to the documentation of this file.
1 #ifndef HOTKEY_HH
2 #define HOTKEY_HH
3 
4 #include "RTSchedulable.hh"
5 #include "EventListener.hh"
6 #include "Command.hh"
7 #include "Event.hh"
8 #include "TclObject.hh"
9 #include <map>
10 #include <string_view>
11 #include <vector>
12 #include <string>
13 
14 namespace openmsx {
15 
16 class RTScheduler;
17 class GlobalCommandController;
18 class EventDistributor;
19 class XMLElement;
20 
21 class HotKey final : public RTSchedulable, public EventListener
22 {
23 public:
24  struct HotKeyInfo {
25  HotKeyInfo(const Event& event_, std::string command_,
26  bool repeat_ = false, bool passEvent_ = false)
27  : event(event_), command(std::move(command_))
28  , repeat(repeat_)
29  , passEvent(passEvent_) {}
31  std::string command;
32  bool repeat;
33  bool passEvent; // whether to pass event with args back to command
34  };
35  using BindMap = std::vector<HotKeyInfo>; // unsorted
36  using KeySet = std::vector<Event>; // unsorted
37 
38  HotKey(RTScheduler& rtScheduler,
39  GlobalCommandController& commandController,
40  EventDistributor& eventDistributor);
41  ~HotKey();
42 
43  void loadBindings(const XMLElement& config);
44  void saveBindings(XMLElement& config) const;
45 
46 private:
47  struct LayerInfo {
48  std::string layer;
49  bool blocking;
50  };
51 
52  void initDefaultBindings();
53  void bind (HotKeyInfo&& info);
54  void unbind (const Event& event);
55  void bindDefault (HotKeyInfo&& info);
56  void unbindDefault(const Event& event);
57  void bindLayer (HotKeyInfo&& info, const std::string& layer);
58  void unbindLayer (const Event& event, const std::string& layer);
59  void unbindFullLayer(const std::string& layer);
60  void activateLayer (std::string layer, bool blocking);
61  void deactivateLayer(std::string_view layer);
62 
63  int executeEvent(const Event& event);
64  void executeBinding(const Event& event, const HotKeyInfo& info);
65  void startRepeat (const Event& event);
66  void stopRepeat();
67 
68  // EventListener
69  int signalEvent(const Event& event) noexcept override;
70  // RTSchedulable
71  void executeRT() override;
72 
73 private:
74  class BindCmd final : public Command {
75  public:
76  BindCmd(CommandController& commandController, HotKey& hotKey,
77  bool defaultCmd);
78  void execute(span<const TclObject> tokens, TclObject& result) override;
79  [[nodiscard]] std::string help(span<const TclObject> tokens) const override;
80  private:
81  HotKey& hotKey;
82  const bool defaultCmd;
83  };
84  BindCmd bindCmd;
85  BindCmd bindDefaultCmd;
86 
87  class UnbindCmd final : public Command {
88  public:
89  UnbindCmd(CommandController& commandController, HotKey& hotKey,
90  bool defaultCmd);
91  void execute(span<const TclObject> tokens, TclObject& result) override;
92  [[nodiscard]] std::string help(span<const TclObject> tokens) const override;
93  private:
94  HotKey& hotKey;
95  const bool defaultCmd;
96  };
97  UnbindCmd unbindCmd;
98  UnbindCmd unbindDefaultCmd;
99 
100  struct ActivateCmd final : Command {
101  explicit ActivateCmd(CommandController& commandController);
102  void execute(span<const TclObject> tokens, TclObject& result) override;
103  [[nodiscard]] std::string help(span<const TclObject> tokens) const override;
104  } activateCmd;
105 
106  struct DeactivateCmd final : Command {
107  explicit DeactivateCmd(CommandController& commandController);
108  void execute(span<const TclObject> tokens, TclObject& result) override;
109  [[nodiscard]] std::string help(span<const TclObject> tokens) const override;
110  } deactivateCmd;
111 
112  BindMap cmdMap;
113  BindMap defaultMap;
114  std::map<std::string, BindMap> layerMap;
115  std::vector<LayerInfo> activeLayers;
116  KeySet boundKeys;
117  KeySet unboundKeys;
118  GlobalCommandController& commandController;
119  EventDistributor& eventDistributor;
120  Event lastEvent;
121 };
122 
123 } // namespace openmsx
124 
125 #endif
HotKey(RTScheduler &rtScheduler, GlobalCommandController &commandController, EventDistributor &eventDistributor)
Definition: HotKey.cc:39
void saveBindings(XMLElement &config) const
Definition: HotKey.cc:217
std::vector< HotKeyInfo > BindMap
Definition: HotKey.hh:35
std::vector< Event > KeySet
Definition: HotKey.hh:36
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:177
Definition: span.hh:126
This file implemented 3 utility functions:
Definition: Autofire.cc:9
HotKeyInfo(const Event &event_, std::string command_, bool repeat_=false, bool passEvent_=false)
Definition: HotKey.hh:25