| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "controlmode.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | ControlMode::ControlMode(std::string_view id, |
| 10 | std::vector<std::unique_ptr<IControl>> &&controls, |
| 11 | bool active) noexcept |
| 12 | : Control(active, true) |
| 13 | , id_(id) |
| 14 | , controls_(std::move(controls)) |
| 15 | { |
| 16 | } |
| 17 | |
| 18 | void ControlMode::preInit(ICommandQueue &ctlCmds) |
| 19 | { |
| 20 | for (auto &control : controls_) |
| 21 | control->preInit(ctlCmds); |
| 22 | } |
| 23 | |
| 24 | void ControlMode::postInit(ICommandQueue &ctlCmds) |
| 25 | { |
| 26 | for (auto &control : controls_) |
| 27 | control->postInit(ctlCmds); |
| 28 | } |
| 29 | |
| 30 | void ControlMode::init() |
| 31 | { |
| 32 | // Set the first active control as the active mode |
| 33 | // or the first one when no active controls are found. |
| 34 | |
| 35 | bool activeFound{false}; |
| 36 | for (auto &control : controls_) { |
| 37 | control->init(); |
| 38 | |
| 39 | if (!activeFound && control->active()) { |
| 40 | mode_ = control->ID(); |
| 41 | activeFound = true; |
| 42 | } |
| 43 | else if (activeFound && control->active()) |
| 44 | control->activate(false); |
| 45 | } |
| 46 | |
| 47 | if (!activeFound && !controls_.empty()) { |
| 48 | auto &control = controls_.front(); |
| 49 | control->activate(true); |
| 50 | mode_ = control->ID(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | std::string const &ControlMode::ID() const |
| 55 | { |
| 56 | return id_; |
| 57 | } |
| 58 | |
| 59 | void ControlMode::importControl(IControl::Importer &i) |
| 60 | { |
| 61 | auto &pModeImporter = dynamic_cast<ControlMode::Importer &>(i); |
| 62 | mode(pModeImporter.provideMode()); |
| 63 | |
| 64 | for (auto &control : controls_) { |
| 65 | control->importWith(i); |
| 66 | |
| 67 | // ensure that only the selected mode is active |
| 68 | control->activate(control->ID() == mode()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void ControlMode::exportControl(IControl::Exporter &e) const |
| 73 | { |
| 74 | std::vector<std::string> modes; |
| 75 | for (auto const &control : controls_) { |
| 76 | modes.emplace_back(control->ID()); |
| 77 | control->exportWith(e); |
| 78 | } |
| 79 | |
| 80 | auto &pModeExporter = dynamic_cast<ControlMode::Exporter &>(e); |
| 81 | pModeExporter.takeModes(modes); |
| 82 | pModeExporter.takeMode(mode()); |
| 83 | } |
| 84 | |
| 85 | void ControlMode::cleanControl(ICommandQueue &ctlCmds) |
| 86 | { |
| 87 | for (auto &control : controls_) |
| 88 | control->clean(ctlCmds); |
| 89 | } |
| 90 | |
| 91 | void ControlMode::syncControl(ICommandQueue &ctlCmds) |
| 92 | { |
| 93 | for (auto &control : controls_) |
| 94 | control->sync(ctlCmds); |
| 95 | } |
| 96 | |
| 97 | std::string const &ControlMode::mode() const |
| 98 | { |
| 99 | return mode_; |
| 100 | } |
| 101 | |
| 102 | void ControlMode::mode(std::string const &mode) |
| 103 | { |
| 104 | auto iter = std::find_if( |
| 105 | controls_.cbegin(), controls_.cend(), |
| 106 | [&](auto const &control) { return mode == control->ID(); }); |
| 107 | if (iter != controls_.cend()) |
| 108 | mode_ = mode; |
| 109 | } |