Skip to content

Commit f9c21cc

Browse files
committed
Make Mapper internally use eventsource/sink instead of input/output devices
1 parent 14d1f08 commit f9c21cc

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

lib/OutputDevice.cpp renamed to lib/EventSink.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
* This source code is licensed under the ISC license found in the LICENSE file
66
* in the root directory of this source tree.
77
*/
8-
#include "OutputDevice.h"
8+
#include "EventSink.h"
99

1010
namespace fredemmott::inputmapping {
1111

12-
OutputDevice::~OutputDevice() {
12+
EventSink::~EventSink() {
1313
}
1414

1515
}// namespace fredemmott::inputmapping

lib/EventSink.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2020-present, Fred Emmott <fred@fredemmott.com>
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the ISC license found in the LICENSE file
6+
* in the root directory of this source tree.
7+
*/
8+
#pragma once
9+
10+
namespace fredemmott::inputmapping {
11+
12+
class EventSink {
13+
public:
14+
virtual ~EventSink();
15+
virtual void flush() = 0;
16+
};
17+
18+
}// namespace fredemmott::inputmapping

lib/Mapper.cpp

+15-12
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ struct ActiveInstanceGuard {
6161
void Mapper::setDevices(
6262
const std::vector<MappableInput> &inputs,
6363
const std::vector<std::shared_ptr<OutputDevice>> &outputs) {
64-
mInputs = inputs;
65-
mOutputs = outputs;
64+
for (const auto &input: inputs) {
65+
mEventSources.push_back(input.getEventSource());
66+
}
67+
mEventSinks
68+
= std::vector<std::shared_ptr<EventSink>>(outputs.begin(), outputs.end());
6669
}
6770

6871
void Mapper::passthrough(MappableInput &s, const MappableVJoyOutput &t) {
@@ -82,15 +85,15 @@ void Mapper::passthrough(MappableInput &s, const MappableVJoyOutput &t) {
8285
}
8386

8487
void Mapper::run() {
85-
if (mInputs.empty()) {
88+
if (mEventSources.empty()) {
8689
printf(
8790
"---\n"
8891
"!!! WARNING !!!\n"
8992
"No inputs were set. No mapping will be updated unless devices are\n"
9093
"manually polled.\n"
9194
"---\n");
9295
}
93-
if (mOutputs.empty()) {
96+
if (mEventSinks.empty()) {
9497
printf(
9598
"---\n"
9699
"!!! WARNING !!!\n"
@@ -105,11 +108,11 @@ void Mapper::run() {
105108
SetConsoleCtrlHandler(&exit_event_handler, true);
106109
std::vector<HANDLE> fixed_events {gExitEvent};
107110

108-
std::map<HANDLE, MappableInput> devices;
109-
for (const auto &input: mInputs) {
110-
auto event = input.getEventSource()->getHandle();
111-
fixed_events.push_back(event);
112-
devices.insert({event, input});
111+
std::map<HANDLE, std::shared_ptr<EventSource>> handle_to_source;
112+
for (const auto &source: mEventSources) {
113+
auto handle = source->getHandle();
114+
fixed_events.push_back(handle);
115+
handle_to_source.insert({handle, source});
113116
}
114117
printf("---\nProfile running, hit Ctrl-C to exit and clean up HidHide.\n");
115118
while (true) {
@@ -138,16 +141,16 @@ void Mapper::run() {
138141
continue;
139142
}
140143

141-
auto device = devices.at(event);
142-
device.getEventSource()->poll();
144+
auto source = handle_to_source.at(event);
145+
source->poll();
143146
gActiveInstance = nullptr;
144147

145148
flush();
146149
}
147150
}
148151

149152
void Mapper::flush() {
150-
for (const auto &output: mOutputs) {
153+
for (const auto &output: mEventSinks) {
151154
output->flush();
152155
}
153156
}

lib/Mapper.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class InputDevice;
1818
}
1919

2020
namespace fredemmott::inputmapping {
21+
class EventSink;
22+
class EventSource;
2123
class MappableInput;
2224
class MappableOutput;
2325
class MappableVJoyOutput;
@@ -39,8 +41,8 @@ class Mapper {
3941
const std::function<void()>& handler);
4042

4143
private:
42-
std::vector<MappableInput> mInputs;
43-
std::vector<std::shared_ptr<OutputDevice>> mOutputs;
44+
std::vector<std::shared_ptr<EventSource>> mEventSources;
45+
std::vector<std::shared_ptr<EventSink>> mEventSinks;
4446
std::map<void*, std::function<void()>> mInjected;
4547

4648
void flush();

lib/OutputDevice.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@
77
*/
88
#pragma once
99

10+
#include "EventSink.h"
11+
1012
namespace fredemmott::inputmapping {
1113

12-
class OutputDevice {
13-
public:
14-
virtual ~OutputDevice();
15-
virtual void flush() = 0;
14+
class OutputDevice : public EventSink {
1615
};
1716

1817
}// namespace fredemmott::inputmapping

0 commit comments

Comments
 (0)