Etterna 0.74.4
Loading...
Searching...
No Matches
InputHandler.h
1#ifndef INPUT_HANDLER_H
2#define INPUT_HANDLER_H
3
4/* This is a simple class to handle special input devices. Update() will be
5 * called during the input update; the derived class should send appropriate
6 * events to InputHandler.
7 * Note that, if the underlying device is capable of it, you're free to start
8 * a blocking thread; just store inputs in your class and send them off in a
9 * batch on the next Update. This gets much more accurate timestamps; we get
10 * events more quickly and timestamp them, instead of having a rough timing
11 * granularity due to the framerate.
12 * Send input events for a specific type of device. Only one driver for a given
13 * set of InputDevice types should be loaded for a given arch. For example,
14 * any number of drivers may produce DEVICE_PUMPn events, but only one may be
15 * loaded at a time. (This will be inconvenient if, for example, we have two
16 * completely distinct methods of getting input for the same device; we have no
17 * method to allocate device numbers. We don't need this now; I'll write it
18 * if it becomes needed.) */
19#include "RageUtil/Misc/RageInputDevice.h" // for InputDevice
20#include "arch/RageDriver.h"
23{
24 public:
25 static void Create(const std::string& sDrivers,
26 std::vector<InputHandler*>& apAdd);
27 static DriverList m_pDriverList;
28
30 : m_LastUpdate()
31 {
32 }
33 ~InputHandler() override = default;
34 virtual void Update() {}
35 virtual bool DevicesChanged() { return false; }
36 virtual void GetDevicesAndDescriptions(
37 std::vector<InputDeviceInfo>& vDevicesOut) = 0;
38
39 // Override to return a pretty string that's specific to the controller
40 // type.
41 virtual std::string GetDeviceSpecificInputString(const DeviceInput& di);
42 static wchar_t ApplyKeyModifiers(wchar_t c);
43 virtual std::string GetLocalizedInputString(const DeviceInput& di);
44 virtual wchar_t DeviceButtonToChar(DeviceButton button,
45 bool bUseCurrentKeyModifiers);
46
47 // Override to find out whether the controller is currently plugged in.
48 // Not all InputHandlers will support this. Not applicable to all
49 // InputHandlers.
50 virtual InputDeviceState GetInputDeviceState(InputDevice /* id */)
51 {
52 return InputDeviceState_Connected;
53 }
54
55 /* In Windows, some devices need to be recreated if we recreate our main
56 * window. Override this if you need to do that. */
57 virtual void WindowReset() {}
58
59 virtual void ApplyTemporaryInputSettings() {}
60 virtual void RemoveTemporaryInputSettings() {}
61
62 protected:
63 /* Convenience function: Call this to queue a received event.
64 * This may be called in a thread.
65 *
66 * Important detail: If the timestamp, di.ts, is zero, then it is assumed
67 * that this is not a threaded event handler. In that case, input is being
68 * polled, and the actual time the button was pressed may be any time since
69 * the last poll. In this case, ButtonPressed will pretend the button was
70 * pressed at the midpoint since the last update, which will smooth out the
71 * error.
72 *
73 * Note that timestamps are set to the current time by default, so for this
74 * to happen, you need to explicitly call di.ts.SetZero().
75 *
76 * If the timestamp is set, it'll be left alone. */
77 void ButtonPressed(DeviceInput di);
78
79 /* Call this at the end of polling input. */
80 void UpdateTimer();
81
82 private:
83 std::chrono::time_point<std::chrono::steady_clock> m_LastUpdate;
84 int m_iInputsSinceUpdate{ 0 };
85};
86
87#define REGISTER_INPUT_HANDLER_CLASS2(name, x) \
88 static RegisterRageDriver register_##name( \
89 &InputHandler::m_pDriverList, \
90 #name, \
91 CreateClass<InputHandler_##x, RageDriver>)
92#define REGISTER_INPUT_HANDLER_CLASS(name) \
93 REGISTER_INPUT_HANDLER_CLASS2(name, name)
94
95#endif
A class designed to handle special input devices.
Definition InputHandler.h:23
Definition RageDriver.h:7
Definition RageInputDevice.h:390
Definition RageDriver.h:17