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 "string_view.hh"
8 #include <map>
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(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  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  std::string help(const std::vector<std::string>& tokens) const override;
80  private:
81  std::string formatBinding(const HotKey::BindMap::value_type& info);
82 
83  HotKey& hotKey;
84  const bool defaultCmd;
85  };
86  BindCmd bindCmd;
87  BindCmd bindDefaultCmd;
88 
89  class UnbindCmd final : public Command {
90  public:
91  UnbindCmd(CommandController& commandController, HotKey& hotKey,
92  bool defaultCmd);
93  void execute(span<const TclObject> tokens, TclObject& result) override;
94  std::string help(const std::vector<std::string>& tokens) const override;
95  private:
96  HotKey& hotKey;
97  const bool defaultCmd;
98  };
99  UnbindCmd unbindCmd;
100  UnbindCmd unbindDefaultCmd;
101 
102  struct ActivateCmd final : Command {
103  explicit ActivateCmd(CommandController& commandController);
104  void execute(span<const TclObject> tokens, TclObject& result) override;
105  std::string help(const std::vector<std::string>& tokens) const override;
106  } activateCmd;
107 
108  struct DeactivateCmd final : Command {
109  explicit DeactivateCmd(CommandController& commandController);
110  void execute(span<const TclObject> tokens, TclObject& result) override;
111  std::string help(const std::vector<std::string>& tokens) const override;
112  } deactivateCmd;
113 
114  BindMap cmdMap;
115  BindMap defaultMap;
116  std::map<std::string, BindMap> layerMap;
117  std::vector<LayerInfo> activeLayers;
118  KeySet boundKeys;
119  KeySet unboundKeys;
120  GlobalCommandController& commandController;
121  EventDistributor& eventDistributor;
122  EventPtr lastEvent;
123 };
124 
125 } // namespace openmsx
126 
127 #endif
std::vector< HotKeyInfo > BindMap
Definition: HotKey.hh:36
Definition: span.hh:34
STL namespace.
HotKeyInfo(const EventPtr &event_, std::string command_, bool repeat_=false, bool passEvent_=false)
Definition: HotKey.hh:26
void loadBindings(const XMLElement &config)
Definition: HotKey.cc:173
HotKey(RTScheduler &rtScheduler, GlobalCommandController &commandController, EventDistributor &eventDistributor)
Definition: HotKey.cc:39
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
std::shared_ptr< const Event > EventPtr
Definition: HotKey.hh:24
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
std::vector< EventPtr > KeySet
Definition: HotKey.hh:37
void saveBindings(XMLElement &config) const
Definition: HotKey.cc:213