OXYGENHUB
◆ main openmsx / src / input / JoystickPort.cc
1.9 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "JoystickPort.hh"
2
3
#include "DummyJoystick.hh"
4
#include "JoystickDevice.hh"
5
6
#include "PluggingController.hh"
7
#include "serialize.hh"
8
9
#include "checked_cast.hh"
10
11
#include <memory>
12
13
namespace openmsx {
14
15
JoystickPort::JoystickPort(PluggingController& pluggingController_,
16
                           std::string name_, std::string description_)
17
	: Connector(pluggingController_, std::move(name_), std::make_unique<DummyJoystick>())
18
	, description(std::move(description_))
19
{
20
}
21
22
zstring_view JoystickPort::getDescription() const
23
{
24
	return description;
25
}
26
27
zstring_view JoystickPort::getClass() const
28
{
29
	return "Joystick Port";
30
}
31
32
JoystickDevice& JoystickPort::getPluggedJoyDev() const
33
{
34
	return *checked_cast<JoystickDevice*>(&getPlugged());
35
}
36
37
void JoystickPort::plug(Pluggable& device, EmuTime time)
38
{
39
	Connector::plug(device, time);
40
	getPluggedJoyDev().write(lastValue, time);
41
}
42
43
uint8_t JoystickPort::read(EmuTime time)
44
{
45
	return getPluggedJoyDev().read(time);
46
}
47
48
void JoystickPort::write(uint8_t value, EmuTime time)
49
{
50
	if (lastValue != value) writeDirect(value, time);
51
}
52
void JoystickPort::writeDirect(uint8_t value, EmuTime time)
53
{
54
	lastValue = value;
55
	getPluggedJoyDev().write(value, time);
56
}
57
58
template<typename Archive>
59
void JoystickPort::serialize(Archive& ar, unsigned /*version*/)
60
{
61
	ar.template serializeBase<Connector>(*this);
62
	if constexpr (Archive::IS_LOADER) {
63
		// The value of 'lastValue', is already restored via MSXPSG,
64
		// but we still need to re-write this value to the plugged
65
		// devices (do this after those devices have been re-plugged).
66
		writeDirect(lastValue, getPluggingController().getCurrentTime());
67
	}
68
}
69
INSTANTIATE_SERIALIZE_METHODS(JoystickPort);
70
71
72
// class DummyJoystickPort
73
74
uint8_t DummyJoystickPort::read(EmuTime /*time*/)
75
{
76
	return 0x3F; // do as-if nothing is connected
77
}
78
79
void DummyJoystickPort::write(uint8_t /*value*/, EmuTime /*time*/)
80
{
81
	// ignore writes
82
}
83
84
} // namespace openmsx