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