openMSX
JoyTap.cc
Go to the documentation of this file.
1 #include "JoyTap.hh"
2 #include "JoystickPort.hh"
3 #include "PluggingController.hh"
4 #include "enumerate.hh"
5 #include "serialize.hh"
6 #include "strCat.hh"
7 #include <memory>
8 
9 namespace openmsx {
10 
11 using std::string;
12 
13 JoyTap::JoyTap(PluggingController& pluggingController_, string name_)
14  : pluggingController(pluggingController_)
15  , name(std::move(name_))
16 {
17 }
18 
19 JoyTap::~JoyTap() = default;
20 
22  for (auto [i, slave] : enumerate(slaves)) {
23  slave = std::make_unique<JoystickPort>(
25  strCat(name, "_port_", char('1' + i)),
26  description);
27  }
28 }
29 
30 std::string_view JoyTap::getDescription() const
31 {
32  return "MSX Joy Tap device";
33 }
34 
35 std::string_view JoyTap::getName() const
36 {
37  return name;
38 }
39 
40 void JoyTap::plugHelper(Connector& /*connector*/, EmuTime::param /*time*/)
41 {
42  createPorts("Joy Tap port");
43 }
44 
45 void JoyTap::unplugHelper(EmuTime::param time)
46 {
47  for (auto& s : slaves) {
48  s->unplug(time);
49  s.reset();
50  }
51 }
52 
53 byte JoyTap::read(EmuTime::param time)
54 {
55  byte value = 255;
56  for (auto& s : slaves) {
57  value &= s->read(time);
58  }
59  return value;
60 }
61 
62 void JoyTap::write(byte value, EmuTime::param time)
63 {
64  for (auto& s : slaves) {
65  s->write(value, time);
66  }
67 }
68 
69 template<typename Archive>
70 void JoyTap::serialize(Archive& ar, unsigned /*version*/)
71 {
72  // saving only happens when plugged in
73  if (!ar.isLoader()) assert(isPluggedIn());
74  // restore plugged state when loading
75  if (ar.isLoader()) {
77  }
78 
79  char tag[6] = { 'p', 'o', 'r', 't', 'X', 0 };
80  for (auto [i, slave] : enumerate(slaves)) {
81  tag[4] = char('0' + i);
82  ar.serialize(tag, *slave);
83  }
84 }
87 
88 } // namespace openmsx
Represents something you can plug devices into.
Definition: Connector.hh:21
This device is plugged in into the joyports and consolidates several other joysticks plugged into it.
Definition: JoyTap.hh:22
void unplugHelper(EmuTime::param time) override
Definition: JoyTap.cc:45
std::unique_ptr< JoystickPort > slaves[4]
Definition: JoyTap.hh:44
byte read(EmuTime::param time) override
Read from the joystick device.
Definition: JoyTap.cc:53
void plugHelper(Connector &connector, EmuTime::param time) override
Definition: JoyTap.cc:40
std::string_view getDescription() const override
Description for this pluggable.
Definition: JoyTap.cc:30
~JoyTap() override
void serialize(Archive &ar, unsigned version)
Definition: JoyTap.cc:70
void createPorts(static_string_view description)
Definition: JoyTap.cc:21
void write(byte value, EmuTime::param time) override
Write a value to the joystick device.
Definition: JoyTap.cc:62
JoyTap(PluggingController &pluggingController, std::string name)
Definition: JoyTap.cc:13
std::string_view getName() const override
Name used to identify this pluggable.
Definition: JoyTap.cc:35
PluggingController & pluggingController
Definition: JoyTap.hh:45
bool isPluggedIn() const
Returns true if this pluggable is currently plugged into a connector.
Definition: Pluggable.hh:49
Connector * getConnector() const
Get the connector this Pluggable is plugged into.
Definition: Pluggable.hh:43
Central administration of Connectors and Pluggables.
EmuTime::param getCurrentTime() const
Convenience method: get current time.
static_string_view
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition: enumerate.hh:28
This file implemented 3 utility functions:
Definition: Autofire.cc:5
REGISTER_POLYMORPHIC_INITIALIZER(Pluggable, CassettePlayer, "CassettePlayer")
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:983
std::string strCat(Ts &&...ts)
Definition: strCat.hh:591