2018-01-29 03:22:21 +00:00
|
|
|
//
|
|
|
|
// MultiMachine.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 28/01/2018.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2018 Thomas Harte. All rights reserved.
|
2018-01-29 03:22:21 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "MultiMachine.hpp"
|
2019-03-02 23:07:05 +00:00
|
|
|
#include "../../../Outputs/Log.hpp"
|
2018-01-29 03:22:21 +00:00
|
|
|
|
2018-02-19 13:13:41 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2018-01-29 03:22:21 +00:00
|
|
|
using namespace Analyser::Dynamic;
|
|
|
|
|
|
|
|
MultiMachine::MultiMachine(std::vector<std::unique_ptr<DynamicMachine>> &&machines) :
|
2018-01-30 02:49:49 +00:00
|
|
|
machines_(std::move(machines)),
|
2018-02-09 21:31:05 +00:00
|
|
|
configurable_(machines_),
|
2020-04-02 03:19:34 +00:00
|
|
|
timed_machine_(machines_, machines_mutex_),
|
|
|
|
scan_producer_(machines_, machines_mutex_),
|
|
|
|
audio_producer_(machines_, machines_mutex_),
|
|
|
|
joystick_machine_(machines_),
|
2018-07-11 01:32:28 +00:00
|
|
|
keyboard_machine_(machines_),
|
|
|
|
media_target_(machines_) {
|
2020-04-02 03:19:34 +00:00
|
|
|
timed_machine_.set_delegate(this);
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
|
2018-05-11 01:54:10 +00:00
|
|
|
Activity::Source *MultiMachine::activity_source() {
|
2018-05-08 01:57:54 +00:00
|
|
|
return nullptr; // TODO
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
#define Provider(type, name, member) \
|
|
|
|
type *MultiMachine::name() { \
|
|
|
|
if(has_picked_) { \
|
|
|
|
return machines_.front()->name(); \
|
|
|
|
} else { \
|
|
|
|
return &member; \
|
|
|
|
} \
|
2018-02-12 01:24:08 +00:00
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
Provider(Configurable::Device, configurable_device, configurable_)
|
|
|
|
Provider(MachineTypes::TimedMachine, timed_machine, timed_machine_)
|
|
|
|
Provider(MachineTypes::ScanProducer, scan_producer, scan_producer_)
|
|
|
|
Provider(MachineTypes::AudioProducer, audio_producer, audio_producer_)
|
|
|
|
Provider(MachineTypes::JoystickMachine, joystick_machine, joystick_machine_)
|
|
|
|
Provider(MachineTypes::KeyboardMachine, keyboard_machine, keyboard_machine_)
|
|
|
|
Provider(MachineTypes::MediaTarget, media_target, media_target_)
|
2018-01-29 03:22:21 +00:00
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
MachineTypes::MouseMachine *MultiMachine::mouse_machine() {
|
2019-06-11 22:21:56 +00:00
|
|
|
// TODO.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
#undef Provider
|
2018-02-01 12:53:52 +00:00
|
|
|
|
2018-03-07 00:08:02 +00:00
|
|
|
bool MultiMachine::would_collapse(const std::vector<std::unique_ptr<DynamicMachine>> &machines) {
|
|
|
|
return
|
2020-04-02 03:19:34 +00:00
|
|
|
(machines.front()->timed_machine()->get_confidence() > 0.9f) ||
|
|
|
|
(machines.front()->timed_machine()->get_confidence() >= 2.0f * machines[1]->timed_machine()->get_confidence());
|
2018-03-07 00:08:02 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 03:19:34 +00:00
|
|
|
void MultiMachine::did_run_machines(MultiTimedMachine *) {
|
2019-02-28 03:39:33 +00:00
|
|
|
std::lock_guard<decltype(machines_mutex_)> machines_lock(machines_mutex_);
|
2019-03-02 23:07:05 +00:00
|
|
|
#ifndef NDEBUG
|
2018-02-12 01:24:08 +00:00
|
|
|
for(const auto &machine: machines_) {
|
2020-04-02 03:19:34 +00:00
|
|
|
auto timed_machine = machine->timed_machine();
|
|
|
|
LOGNBR(PADHEX(2) << timed_machine->get_confidence() << " " << timed_machine->debug_type() << "; ");
|
2018-02-12 01:24:08 +00:00
|
|
|
}
|
2019-03-02 23:07:05 +00:00
|
|
|
LOGNBR(std::endl);
|
2018-03-07 00:08:02 +00:00
|
|
|
#endif
|
2018-02-11 00:38:26 +00:00
|
|
|
|
2018-02-18 21:37:07 +00:00
|
|
|
DynamicMachine *front = machines_.front().get();
|
2018-02-19 13:13:41 +00:00
|
|
|
std::stable_sort(machines_.begin(), machines_.end(),
|
2018-05-05 23:32:20 +00:00
|
|
|
[] (const std::unique_ptr<DynamicMachine> &lhs, const std::unique_ptr<DynamicMachine> &rhs){
|
2020-04-02 03:19:34 +00:00
|
|
|
auto lhs_timed = lhs->timed_machine();
|
|
|
|
auto rhs_timed = rhs->timed_machine();
|
|
|
|
return lhs_timed->get_confidence() > rhs_timed->get_confidence();
|
2018-05-05 23:32:20 +00:00
|
|
|
});
|
2018-02-01 12:53:52 +00:00
|
|
|
|
|
|
|
if(machines_.front().get() != front) {
|
2020-04-02 03:19:34 +00:00
|
|
|
scan_producer_.did_change_machine_order();
|
|
|
|
audio_producer_.did_change_machine_order();
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
2018-02-12 01:24:08 +00:00
|
|
|
|
2018-03-07 00:08:02 +00:00
|
|
|
if(would_collapse(machines_)) {
|
2018-02-12 01:24:08 +00:00
|
|
|
pick_first();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MultiMachine::pick_first() {
|
|
|
|
has_picked_ = true;
|
|
|
|
// machines_.erase(machines_.begin() + 1, machines_.end());
|
|
|
|
// TODO: this isn't quite correct, because it may leak OpenGL/etc resources through failure to
|
|
|
|
// request a close_output while the context is active.
|
2018-02-01 12:53:52 +00:00
|
|
|
}
|
2018-02-13 02:46:21 +00:00
|
|
|
|
|
|
|
void *MultiMachine::raw_pointer() {
|
|
|
|
return nullptr;
|
|
|
|
}
|