openMSX
Joystick.hh
Go to the documentation of this file.
1 #ifndef JOYSTICK_HH
2 #define JOYSTICK_HH
3 
4 #include "JoystickDevice.hh"
5 #include "MSXEventListener.hh"
6 #include "StateChangeListener.hh"
7 #include "StringSetting.hh"
8 #include "serialize_meta.hh"
9 #include <SDL.h>
10 
11 namespace openmsx {
12 
13 class MSXEventDistributor;
14 class StateChangeDistributor;
15 class CommandController;
16 class PluggingController;
17 class GlobalSettings;
18 class IntegerSetting;
19 class TclObject;
20 class Interpreter;
21 
24 class Joystick final
25 #ifndef SDL_JOYSTICK_DISABLED
26  : public JoystickDevice, private MSXEventListener, private StateChangeListener
27 #endif
28 {
29 public:
32  static void registerAll(MSXEventDistributor& eventDistributor,
33  StateChangeDistributor& stateChangeDistributor,
34  CommandController& commandController,
35  GlobalSettings& globalSettings,
36  PluggingController& controller);
37 
38  Joystick(MSXEventDistributor& eventDistributor,
39  StateChangeDistributor& stateChangeDistributor,
40  CommandController& commandController,
41  GlobalSettings& globalSettings,
42  SDL_Joystick* joystick);
43  ~Joystick() override;
44 
45 #ifndef SDL_JOYSTICK_DISABLED
46  // Pluggable
47  const std::string& getName() const override;
48  string_view getDescription() const override;
49  void plugHelper(Connector& connector, EmuTime::param time) override;
50  void unplugHelper(EmuTime::param time) override;
51 
52  // JoystickDevice
53  byte read(EmuTime::param time) override;
54  void write(byte value, EmuTime::param time) override;
55 
56  template<typename Archive>
57  void serialize(Archive& ar, unsigned version);
58 
59 private:
60  void plugHelper2();
61  byte calcState();
62  bool getState(Interpreter& interp, const TclObject& dict, string_view key,
63  int threshold);
64  void createEvent(EmuTime::param time, byte newStatus);
65 
66  // MSXEventListener
67  void signalMSXEvent(const std::shared_ptr<const Event>& event,
68  EmuTime::param time) override;
69  // StateChangeListener
70  void signalStateChange(const std::shared_ptr<StateChange>& event) override;
71  void stopReplay(EmuTime::param time) override;
72 
73  MSXEventDistributor& eventDistributor;
74  StateChangeDistributor& stateChangeDistributor;
75 
76  SDL_Joystick* const joystick;
77  const unsigned joyNum;
78  IntegerSetting& deadSetting; // must come after joyNum
79  const std::string name;
80  const std::string desc;
81  StringSetting configSetting;
82 
83  byte status;
84  bool pin8;
85 #endif // SDL_JOYSTICK_DISABLED
86 };
88 
89 } // namespace openmsx
90 
91 #endif
Represents something you can plug devices into.
Definition: Connector.hh:20
uint8_t byte
8 bit unsigned integer
Definition: openmsx.hh:26
Central administration of Connectors and Pluggables.
byte read(EmuTime::param time) override
Read from the joystick device.
Definition: Joystick.cc:221
void write(byte value, EmuTime::param time) override
Write a value to the joystick device.
Definition: Joystick.cc:226
Joystick(MSXEventDistributor &eventDistributor, StateChangeDistributor &stateChangeDistributor, CommandController &commandController, GlobalSettings &globalSettings, SDL_Joystick *joystick)
Definition: Joystick.cc:155
SERIALIZE_CLASS_VERSION(CassettePlayer, 2)
A Setting with an integer value.
Uses an SDL joystick to emulate an MSX joystick.
Definition: Joystick.hh:24
~Joystick() override
Definition: Joystick.cc:177
string_view getDescription() const override
Description for this pluggable.
Definition: Joystick.cc:193
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: Joystick.cc:198
const std::string & getName() const override
Name used to identify this pluggable.
Definition: Joystick.cc:188
This class implements a (close approximation) of the std::string_view class.
Definition: string_view.hh:16
static void registerAll(MSXEventDistributor &eventDistributor, StateChangeDistributor &stateChangeDistributor, CommandController &commandController, GlobalSettings &globalSettings, PluggingController &controller)
Register all available SDL joysticks.
Definition: Joystick.cc:24
This class contains settings that are used by several other class (including some singletons)...
void serialize(Archive &ar, unsigned version)
Definition: Joystick.cc:360
void unplugHelper(EmuTime::param time) override
Definition: Joystick.cc:213