openMSX
Joystick.cc
Go to the documentation of this file.
1 #include "Joystick.hh"
2 #include "PluggingController.hh"
3 #include "PlugException.hh"
4 #include "MSXEventDistributor.hh"
6 #include "InputEvents.hh"
7 #include "InputEventGenerator.hh"
8 #include "StateChange.hh"
9 #include "TclObject.hh"
10 #include "GlobalSettings.hh"
11 #include "IntegerSetting.hh"
12 #include "CommandController.hh"
13 #include "CommandException.hh"
14 #include "serialize.hh"
15 #include "serialize_meta.hh"
16 #include "StringOp.hh"
17 #include "one_of.hh"
18 #include "xrange.hh"
19 #include <memory>
20 
21 using std::string;
22 using std::shared_ptr;
23 
24 namespace openmsx {
25 
27  StateChangeDistributor& stateChangeDistributor,
28  CommandController& commandController,
29  GlobalSettings& globalSettings,
30  PluggingController& controller)
31 {
32 #ifdef SDL_JOYSTICK_DISABLED
33  (void)eventDistributor;
34  (void)stateChangeDistributor;
35  (void)commandController;
36  (void)globalSettings;
37  (void)controller;
38 #else
39  for (auto i : xrange(SDL_NumJoysticks())) {
40  if (SDL_Joystick* joystick = SDL_JoystickOpen(i)) {
41  // Avoid devices that have axes but no buttons, like accelerometers.
42  // SDL 1.2.14 in Linux has an issue where it rejects a device from
43  // /dev/input/event* if it has no buttons but does not reject a
44  // device from /dev/input/js* if it has no buttons, while
45  // accelerometers do end up being symlinked as a joystick in
46  // practice.
47  if (InputEventGenerator::joystickNumButtons(joystick) != 0) {
48  controller.registerPluggable(
49  std::make_unique<Joystick>(
50  eventDistributor,
51  stateChangeDistributor,
52  commandController,
53  globalSettings,
54  joystick));
55  }
56  }
57  }
58 #endif
59 }
60 
61 
62 class JoyState final : public StateChange
63 {
64 public:
65  JoyState() = default; // for serialize
66  JoyState(EmuTime::param time_, unsigned joyNum_, byte press_, byte release_)
67  : StateChange(time_)
68  , joyNum(joyNum_), press(press_), release(release_)
69  {
70  assert((press != 0) || (release != 0));
71  assert((press & release) == 0);
72  }
73  [[nodiscard]] unsigned getJoystick() const { return joyNum; }
74  [[nodiscard]] byte getPress() const { return press; }
75  [[nodiscard]] byte getRelease() const { return release; }
76 
77  template<typename Archive> void serialize(Archive& ar, unsigned /*version*/)
78  {
79  ar.template serializeBase<StateChange>(*this);
80  ar.serialize("joyNum", joyNum,
81  "press", press,
82  "release", release);
83  }
84 private:
85  unsigned joyNum;
86  byte press, release;
87 };
89 
90 
91 #ifndef SDL_JOYSTICK_DISABLED
92 static void checkJoystickConfig(Interpreter& interp, TclObject& newValue)
93 {
94  unsigned n = newValue.getListLength(interp);
95  if (n & 1) {
96  throw CommandException("Need an even number of elements");
97  }
98  for (unsigned i = 0; i < n; i += 2) {
99  std::string_view key = newValue.getListIndex(interp, i + 0).getString();
100  TclObject value = newValue.getListIndex(interp, i + 1);
101  if (key != one_of("A", "B", "LEFT", "RIGHT", "UP", "DOWN")) {
102  throw CommandException(
103  "Invalid MSX joystick action: must be one of "
104  "'A', 'B', 'LEFT', 'RIGHT', 'UP', 'DOWN'.");
105  }
106  for (auto j : xrange(value.getListLength(interp))) {
107  std::string_view host = value.getListIndex(interp, j).getString();
108  if (!StringOp::startsWith(host, "button") &&
109  !StringOp::startsWith(host, "+axis") &&
110  !StringOp::startsWith(host, "-axis") &&
111  !StringOp::startsWith(host, "L_hat") &&
112  !StringOp::startsWith(host, "R_hat") &&
113  !StringOp::startsWith(host, "U_hat") &&
114  !StringOp::startsWith(host, "D_hat")) {
115  throw CommandException(
116  "Invalid host joystick action: must be "
117  "one of 'button<N>', '+axis<N>', '-axis<N>', "
118  "'L_hat<N>', 'R_hat<N>', 'U_hat<N>', 'D_hat<N>'");
119  }
120  }
121  }
122 }
123 
124 [[nodiscard]] static string getJoystickName(unsigned joyNum)
125 {
126  return strCat("joystick", char('1' + joyNum));
127 }
128 
129 [[nodiscard]] static TclObject getConfigValue(SDL_Joystick* joystick)
130 {
131  TclObject listA, listB;
132  for (auto i : xrange(InputEventGenerator::joystickNumButtons(joystick))) {
133  auto button = tmpStrCat("button", i);
134  //std::string_view button = tmp;
135  if (i & 1) {
136  listB.addListElement(button);
137  } else {
138  listA.addListElement(button);
139  }
140  }
141  TclObject value;
142  value.addDictKeyValues("LEFT", makeTclList("-axis0", "L_hat0"),
143  "RIGHT", makeTclList("+axis0", "R_hat0"),
144  "UP", makeTclList("-axis1", "U_hat0"),
145  "DOWN", makeTclList("+axis1", "D_hat0"),
146  "A", listA,
147  "B", listB);
148  return value;
149 }
150 
151 // Note: It's OK to open/close the same SDL_Joystick multiple times (we open it
152 // once per MSX machine). The SDL documentation doesn't state this, but I
153 // checked the implementation and a SDL_Joystick uses a 'reference count' on
154 // the open/close calls.
156  StateChangeDistributor& stateChangeDistributor_,
157  CommandController& commandController,
158  GlobalSettings& globalSettings,
159  SDL_Joystick* joystick_)
160  : eventDistributor(eventDistributor_)
161  , stateChangeDistributor(stateChangeDistributor_)
162  , joystick(joystick_)
163  , joyNum(SDL_JoystickInstanceID(joystick_))
164  , deadSetting(globalSettings.getJoyDeadzoneSetting(joyNum))
165  , name(getJoystickName(joyNum))
166  , desc(SDL_JoystickName(joystick_))
167  , configSetting(commandController, tmpStrCat(name, "_config"),
168  "joystick configuration", getConfigValue(joystick).getString())
169 {
170  auto& interp = commandController.getInterpreter();
171  configSetting.setChecker([&interp](TclObject& newValue) {
172  checkJoystickConfig(interp, newValue); });
173 
174  pin8 = false; // avoid UMR
175 }
176 
178 {
179  if (isPluggedIn()) {
180  Joystick::unplugHelper(EmuTime::dummy());
181  }
182  if (joystick) {
183  SDL_JoystickClose(joystick);
184  }
185 }
186 
187 // Pluggable
188 std::string_view Joystick::getName() const
189 {
190  return name;
191 }
192 
193 std::string_view Joystick::getDescription() const
194 {
195  return desc;
196 }
197 
198 void Joystick::plugHelper(Connector& /*connector*/, EmuTime::param /*time*/)
199 {
200  if (!joystick) {
201  throw PlugException("Failed to open joystick device");
202  }
203  plugHelper2();
204  status = calcState();
205 }
206 
207 void Joystick::plugHelper2()
208 {
209  eventDistributor.registerEventListener(*this);
210  stateChangeDistributor.registerListener(*this);
211 }
212 
213 void Joystick::unplugHelper(EmuTime::param /*time*/)
214 {
215  stateChangeDistributor.unregisterListener(*this);
216  eventDistributor.unregisterEventListener(*this);
217 }
218 
219 
220 // JoystickDevice
221 byte Joystick::read(EmuTime::param /*time*/)
222 {
223  return pin8 ? 0x3F : status;
224 }
225 
226 void Joystick::write(byte value, EmuTime::param /*time*/)
227 {
228  pin8 = (value & 0x04) != 0;
229 }
230 
231 byte Joystick::calcState()
232 {
233  byte result = JOY_UP | JOY_DOWN | JOY_LEFT | JOY_RIGHT |
235  if (joystick) {
236  int threshold = (deadSetting.getInt() * 32768) / 100;
237  auto& interp = configSetting.getInterpreter();
238  const auto& dict = configSetting.getValue();
239  if (getState(interp, dict, "A" , threshold)) result &= ~JOY_BUTTONA;
240  if (getState(interp, dict, "B" , threshold)) result &= ~JOY_BUTTONB;
241  if (getState(interp, dict, "UP" , threshold)) result &= ~JOY_UP;
242  if (getState(interp, dict, "DOWN" , threshold)) result &= ~JOY_DOWN;
243  if (getState(interp, dict, "LEFT" , threshold)) result &= ~JOY_LEFT;
244  if (getState(interp, dict, "RIGHT", threshold)) result &= ~JOY_RIGHT;
245  }
246  return result;
247 }
248 
249 bool Joystick::getState(Interpreter& interp, const TclObject& dict,
250  std::string_view key, int threshold)
251 {
252  try {
253  const auto& list = dict.getDictValue(interp, key);
254  for (auto i : xrange(list.getListLength(interp))) {
255  const auto& elem = list.getListIndex(interp, i).getString();
256  if (StringOp::startsWith(elem, "button")) {
257  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(6))) {
258  if (InputEventGenerator::joystickGetButton(joystick, *n)) {
259  return true;
260  }
261  }
262  } else if (StringOp::startsWith(elem, "+axis")) {
263  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
264  if (SDL_JoystickGetAxis(joystick, *n) > threshold) {
265  return true;
266  }
267  }
268  } else if (StringOp::startsWith(elem, "-axis")) {
269  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
270  if (SDL_JoystickGetAxis(joystick, *n) < -threshold) {
271  return true;
272  }
273  }
274  } else if (StringOp::startsWith(elem, "L_hat")) {
275  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
276  if (SDL_JoystickGetHat(joystick, *n) & SDL_HAT_LEFT) {
277  return true;
278  }
279  }
280  } else if (StringOp::startsWith(elem, "R_hat")) {
281  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
282  if (SDL_JoystickGetHat(joystick, *n) & SDL_HAT_RIGHT) {
283  return true;
284  }
285  }
286  } else if (StringOp::startsWith(elem, "U_hat")) {
287  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
288  if (SDL_JoystickGetHat(joystick, *n) & SDL_HAT_UP) {
289  return true;
290  }
291  }
292  } else if (StringOp::startsWith(elem, "D_hat")) {
293  if (auto n = StringOp::stringToBase<10, unsigned>(elem.substr(5))) {
294  if (SDL_JoystickGetHat(joystick, *n) & SDL_HAT_DOWN) {
295  return true;
296  }
297  }
298  }
299  }
300  } catch (...) {
301  // Error, in getListLength() or getListIndex().
302  // In either case we can't do anything about it here, so ignore.
303  }
304  return false;
305 }
306 
307 // MSXEventListener
308 void Joystick::signalMSXEvent(const shared_ptr<const Event>& event,
309  EmuTime::param time) noexcept
310 {
311  const auto* joyEvent = dynamic_cast<const JoystickEvent*>(event.get());
312  if (!joyEvent) return;
313 
314  // TODO: It would be more efficient to make a dispatcher instead of
315  // sending the event to all joysticks.
316  if (joyEvent->getJoystick() != joyNum) return;
317 
318  // TODO: Currently this recalculates the whole joystick state. It might
319  // be possible to implement this more efficiently by using the specific
320  // event information. Though that's not trivial because e.g. multiple
321  // host buttons can map to the same MSX button. Also calcState()
322  // involves some string processing. It might be possible to only parse
323  // the config once (per setting change). Though this solution is likely
324  // good enough.
325  createEvent(time, calcState());
326 }
327 
328 void Joystick::createEvent(EmuTime::param time, byte newStatus)
329 {
330  byte diff = status ^ newStatus;
331  if (!diff) {
332  // event won't actually change the status, so ignore it
333  return;
334  }
335  // make sure we create an event with minimal changes
336  byte press = status & diff;
337  byte release = newStatus & diff;
338  stateChangeDistributor.distributeNew(std::make_shared<JoyState>(
339  time, joyNum, press, release));
340 }
341 
342 // StateChangeListener
343 void Joystick::signalStateChange(const shared_ptr<StateChange>& event)
344 {
345  const auto* js = dynamic_cast<const JoyState*>(event.get());
346  if (!js) return;
347 
348  // TODO: It would be more efficient to make a dispatcher instead of
349  // sending the event to all joysticks.
350  // TODO an alternative is to log events based on the connector instead
351  // of the joystick. That would make it possible to replay on a
352  // different host without an actual SDL joystick connected.
353  if (js->getJoystick() != joyNum) return;
354 
355  status = (status & ~js->getPress()) | js->getRelease();
356 }
357 
358 void Joystick::stopReplay(EmuTime::param time) noexcept
359 {
360  createEvent(time, calcState());
361 }
362 
363 // version 1: Initial version, the variable status was not serialized.
364 // version 2: Also serialize the above variable, this is required for
365 // record/replay, see comment in Keyboard.cc for more details.
366 template<typename Archive>
367 void Joystick::serialize(Archive& ar, unsigned version)
368 {
369  if (ar.versionAtLeast(version, 2)) {
370  ar.serialize("status", status);
371  }
372  if constexpr (Archive::IS_LOADER) {
373  if (joystick && isPluggedIn()) {
374  plugHelper2();
375  }
376  }
377  // no need to serialize 'pin8' it's automatically restored via write()
378 }
381 
382 #endif // SDL_JOYSTICK_DISABLED
383 
384 } // namespace openmsx
Definition: one_of.hh:7
virtual Interpreter & getInterpreter()=0
Represents something you can plug devices into.
Definition: Connector.hh:21
This class contains settings that are used by several other class (including some singletons).
static int joystickNumButtons(SDL_Joystick *joystick)
Normally the following two functions simply delegate to SDL_JoystickNumButtons() and SDL_JoystickGetB...
static bool joystickGetButton(SDL_Joystick *joystick, int button)
int getInt() const noexcept
unsigned getJoystick() const
Definition: Joystick.cc:73
byte getRelease() const
Definition: Joystick.cc:75
void serialize(Archive &ar, unsigned)
Definition: Joystick.cc:77
JoyState(EmuTime::param time_, unsigned joyNum_, byte press_, byte release_)
Definition: Joystick.cc:66
JoyState()=default
byte getPress() const
Definition: Joystick.cc:74
static constexpr int JOY_BUTTONA
static constexpr int JOY_RIGHT
static constexpr int JOY_LEFT
static constexpr int JOY_DOWN
static constexpr int JOY_BUTTONB
static constexpr int JOY_UP
Uses an SDL joystick to emulate an MSX joystick.
Definition: Joystick.hh:28
std::string_view getName() const override
Name used to identify this pluggable.
Definition: Joystick.cc:188
void unplugHelper(EmuTime::param time) override
Definition: Joystick.cc:213
std::string_view getDescription() const override
Description for this pluggable.
Definition: Joystick.cc:193
~Joystick() override
Definition: Joystick.cc:177
void write(byte value, EmuTime::param time) override
Write a value to the joystick device.
Definition: Joystick.cc:226
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: Joystick.cc:198
static void registerAll(MSXEventDistributor &eventDistributor, StateChangeDistributor &stateChangeDistributor, CommandController &commandController, GlobalSettings &globalSettings, PluggingController &controller)
Register all available SDL joysticks.
Definition: Joystick.cc:26
void serialize(Archive &ar, unsigned version)
Definition: Joystick.cc:367
byte read(EmuTime::param time) override
Read from the joystick device.
Definition: Joystick.cc:221
Joystick(MSXEventDistributor &eventDistributor, StateChangeDistributor &stateChangeDistributor, CommandController &commandController, GlobalSettings &globalSettings, SDL_Joystick *joystick)
Definition: Joystick.cc:155
void registerEventListener(MSXEventListener &listener)
Registers a given object to receive certain events.
void unregisterEventListener(MSXEventListener &listener)
Unregisters a previously registered event listener.
Thrown when a plug action fails.
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
Central administration of Connectors and Pluggables.
void registerPluggable(std::unique_ptr< Pluggable > pluggable)
Add a Pluggable to the registry.
void setChecker(std::function< void(TclObject &)> checkFunc_)
Set value-check-callback.
Definition: Setting.hh:160
const TclObject & getValue() const final
Gets the current value of this setting as a TclObject.
Definition: Setting.hh:142
Interpreter & getInterpreter() const
Definition: Setting.cc:162
void registerListener(StateChangeListener &listener)
(Un)registers the given object to receive state change events.
void distributeNew(const EventPtr &event)
Deliver the event to all registered listeners MSX input devices should call the distributeNew() versi...
void unregisterListener(StateChangeListener &listener)
Base class for all external MSX state changing events.
Definition: StateChange.hh:14
unsigned getListLength(Interpreter &interp) const
Definition: TclObject.cc:125
TclObject getListIndex(Interpreter &interp, unsigned index) const
Definition: TclObject.cc:143
zstring_view getString() const
Definition: TclObject.cc:111
bool startsWith(string_view total, string_view part)
Definition: StringOp.cc:33
This file implemented 3 utility functions:
Definition: Autofire.cc:9
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
REGISTER_POLYMORPHIC_CLASS(StateChange, AutofireStateChange, "AutofireStateChange")
TclObject makeTclList(Args &&... args)
Definition: TclObject.hh:291
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:983
TemporaryString tmpStrCat(Ts &&... ts)
Definition: strCat.hh:659
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591
constexpr auto xrange(T e)
Definition: xrange.hh:155