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