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_),
|
2018-02-11 00:38:26 +00:00
|
|
|
crt_machine_(machines_, machines_mutex_),
|
2018-02-09 21:31:05 +00:00
|
|
|
joystick_machine_(machines),
|
2018-07-11 01:32:28 +00:00
|
|
|
keyboard_machine_(machines_),
|
|
|
|
media_target_(machines_) {
|
2018-02-01 12:53:52 +00:00
|
|
|
crt_machine_.set_delegate(this);
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2018-07-11 01:32:28 +00:00
|
|
|
MediaTarget::Machine *MultiMachine::media_target() {
|
2018-02-12 01:24:08 +00:00
|
|
|
if(has_picked_) {
|
2018-07-11 01:32:28 +00:00
|
|
|
return machines_.front()->media_target();
|
2018-02-12 01:24:08 +00:00
|
|
|
} else {
|
2018-07-11 01:32:28 +00:00
|
|
|
return &media_target_;
|
2018-02-12 01:24:08 +00:00
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CRTMachine::Machine *MultiMachine::crt_machine() {
|
2018-02-12 01:24:08 +00:00
|
|
|
if(has_picked_) {
|
|
|
|
return machines_.front()->crt_machine();
|
|
|
|
} else {
|
|
|
|
return &crt_machine_;
|
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JoystickMachine::Machine *MultiMachine::joystick_machine() {
|
2018-02-12 01:24:08 +00:00
|
|
|
if(has_picked_) {
|
|
|
|
return machines_.front()->joystick_machine();
|
|
|
|
} else {
|
|
|
|
return &joystick_machine_;
|
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardMachine::Machine *MultiMachine::keyboard_machine() {
|
2018-02-12 01:24:08 +00:00
|
|
|
if(has_picked_) {
|
|
|
|
return machines_.front()->keyboard_machine();
|
|
|
|
} else {
|
|
|
|
return &keyboard_machine_;
|
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Configurable::Device *MultiMachine::configurable_device() {
|
2018-02-12 01:24:08 +00:00
|
|
|
if(has_picked_) {
|
|
|
|
return machines_.front()->configurable_device();
|
|
|
|
} else {
|
|
|
|
return &configurable_;
|
|
|
|
}
|
2018-01-29 03:22:21 +00:00
|
|
|
}
|
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
|
|
|
|
(machines.front()->crt_machine()->get_confidence() > 0.9f) ||
|
|
|
|
(machines.front()->crt_machine()->get_confidence() >= 2.0f * machines[1]->crt_machine()->get_confidence());
|
|
|
|
}
|
|
|
|
|
2018-02-01 12:53:52 +00:00
|
|
|
void MultiMachine::multi_crt_did_run_machines() {
|
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_) {
|
|
|
|
CRTMachine::Machine *crt = machine->crt_machine();
|
2019-03-02 23:07:05 +00:00
|
|
|
LOGNBR(PADHEX(2) << crt->get_confidence() << " " << crt->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){
|
|
|
|
CRTMachine::Machine *lhs_crt = lhs->crt_machine();
|
|
|
|
CRTMachine::Machine *rhs_crt = rhs->crt_machine();
|
|
|
|
return lhs_crt->get_confidence() > rhs_crt->get_confidence();
|
|
|
|
});
|
2018-02-01 12:53:52 +00:00
|
|
|
|
|
|
|
if(machines_.front().get() != front) {
|
|
|
|
crt_machine_.did_change_machine_order();
|
|
|
|
}
|
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;
|
|
|
|
}
|