openMSX
JoystickPort.cc
Go to the documentation of this file.
1 #include "JoystickPort.hh"
2 #include "JoystickDevice.hh"
3 #include "DummyJoystick.hh"
4 #include "PluggingController.hh"
5 #include "checked_cast.hh"
6 #include "serialize.hh"
7 #include <memory>
8 
9 using std::string;
10 
11 namespace openmsx {
12 
14  std::string name_, std::string description_)
15  : Connector(pluggingController_, std::move(name_), std::make_unique<DummyJoystick>())
16  , lastValue(255) // != 0
17  , description(std::move(description_))
18 {
19 }
20 
21 std::string_view JoystickPort::getDescription() const
22 {
23  return description;
24 }
25 
26 std::string_view JoystickPort::getClass() const
27 {
28  return "Joystick Port";
29 }
30 
32 {
33  return *checked_cast<JoystickDevice*>(&getPlugged());
34 }
35 
36 void JoystickPort::plug(Pluggable& device, EmuTime::param time)
37 {
38  Connector::plug(device, time);
39  getPluggedJoyDev().write(lastValue, time);
40 }
41 
42 byte JoystickPort::read(EmuTime::param time)
43 {
44  return getPluggedJoyDev().read(time);
45 }
46 
47 void JoystickPort::write(byte value, EmuTime::param time)
48 {
49  if (lastValue != value) writeDirect(value, time);
50 }
51 void JoystickPort::writeDirect(byte value, EmuTime::param time)
52 {
53  lastValue = value;
54  getPluggedJoyDev().write(value, time);
55 }
56 
57 template<typename Archive>
58 void JoystickPort::serialize(Archive& ar, unsigned /*version*/)
59 {
60  ar.template serializeBase<Connector>(*this);
61  if (ar.isLoader()) {
62  // The value of 'lastValue', is already restored via MSXPSG,
63  // but we still need to re-write this value to the plugged
64  // devices (do this after those devices have been re-plugged).
65  writeDirect(lastValue, getPluggingController().getCurrentTime());
66  }
67 }
69 
70 
71 // class DummyJoystickPort
72 
73 byte DummyJoystickPort::read(EmuTime::param /*time*/)
74 {
75  return 0x3F; // do as-if nothing is connected
76 }
77 
78 void DummyJoystickPort::write(byte /*value*/, EmuTime::param /*time*/)
79 {
80  // ignore writes
81 }
82 
83 } // namespace openmsx
openmsx::DummyJoystickPort::write
void write(byte value, EmuTime::param time) override
Definition: JoystickPort.cc:78
openmsx::JoystickPort::getDescription
std::string_view getDescription() const override
Get a description for this connector.
Definition: JoystickPort.cc:21
serialize.hh
openmsx::JoystickDevice
Definition: JoystickDevice.hh:10
openmsx::JoystickPort
Definition: JoystickPort.hh:23
openmsx::JoystickPort::getPluggedJoyDev
JoystickDevice & getPluggedJoyDev() const
Definition: JoystickPort.cc:31
JoystickDevice.hh
JoystickPort.hh
PluggingController.hh
openmsx::Pluggable
Definition: Pluggable.hh:12
openmsx::Connector::getPluggingController
PluggingController & getPluggingController() const
Definition: Connector.hh:61
openmsx::JoystickDevice::read
virtual byte read(EmuTime::param time)=0
Read from the joystick device.
openmsx::JoystickPort::serialize
void serialize(Archive &ar, unsigned version)
Definition: JoystickPort.cc:58
INSTANTIATE_SERIALIZE_METHODS
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:982
openmsx::Connector::plug
virtual void plug(Pluggable &device, EmuTime::param time)
This plugs a Pluggable in this Connector.
Definition: Connector.cc:25
openmsx::DummyJoystick
Definition: DummyJoystick.hh:9
openmsx::JoystickDevice::write
virtual void write(byte value, EmuTime::param time)=0
Write a value to the joystick device.
checked_cast.hh
DummyJoystick.hh
openmsx::JoystickPort::plug
void plug(Pluggable &device, EmuTime::param time) override
This plugs a Pluggable in this Connector.
Definition: JoystickPort.cc:36
openmsx::JoystickPort::getClass
std::string_view getClass() const override
A Connector belong to a certain class.
Definition: JoystickPort.cc:26
openmsx::Connector
Represents something you can plug devices into.
Definition: Connector.hh:21
openmsx::DummyJoystickPort::read
byte read(EmuTime::param time) override
Definition: JoystickPort.cc:73
openmsx::JoystickPort::write
void write(byte value, EmuTime::param time) override
Definition: JoystickPort.cc:47
openmsx::JoystickPort::JoystickPort
JoystickPort(PluggingController &pluggingController, std::string name, std::string description)
Definition: JoystickPort.cc:13
openmsx::PluggingController
Central administration of Connectors and Pluggables.
Definition: PluggingController.hh:22
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::Connector::getPlugged
Pluggable & getPlugged() const
Returns the Pluggable currently plugged in.
Definition: Connector.hh:59
openmsx::JoystickPort::read
byte read(EmuTime::param time) override
Definition: JoystickPort.cc:42