|
| 1 | +/** |
| 2 | + * @file src/display_device.cpp |
| 3 | + * @brief Definitions for display device handling. |
| 4 | + */ |
| 5 | +// header include |
| 6 | +#include "display_device.h" |
| 7 | + |
| 8 | +// lib includes |
| 9 | +#include <display_device/json.h> |
| 10 | +#include <display_device/retry_scheduler.h> |
| 11 | +#include <display_device/settings_manager_interface.h> |
| 12 | + |
| 13 | +// platform-specific includes |
| 14 | +#ifdef _WIN32 |
| 15 | + #include <display_device/windows/settings_manager.h> |
| 16 | + #include <display_device/windows/win_api_layer.h> |
| 17 | + #include <display_device/windows/win_display_device.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +namespace display_device { |
| 21 | + namespace { |
| 22 | + std::unique_ptr<SettingsManagerInterface> |
| 23 | + make_settings_manager() { |
| 24 | +#ifdef _WIN32 |
| 25 | + // TODO: In the upcoming PR, add audio context capture and settings persistence |
| 26 | + return std::make_unique<SettingsManager>( |
| 27 | + std::make_shared<WinDisplayDevice>(std::make_shared<WinApiLayer>()), |
| 28 | + nullptr, |
| 29 | + std::make_unique<PersistentState>(nullptr)); |
| 30 | +#else |
| 31 | + return nullptr; |
| 32 | +#endif |
| 33 | + } |
| 34 | + } // namespace |
| 35 | + |
| 36 | + session_t & |
| 37 | + session_t::get() { |
| 38 | + static session_t session; |
| 39 | + return session; |
| 40 | + } |
| 41 | + |
| 42 | + std::unique_ptr<platf::deinit_t> |
| 43 | + session_t::init() { |
| 44 | + // We can support re-init without any issues, however we should make sure to cleanup first! |
| 45 | + get().impl = nullptr; |
| 46 | + |
| 47 | + // If we fail to create settings manager, this means platform is not supported and |
| 48 | + // we will need to provided error-free passtrough in other methods |
| 49 | + if (auto settings_manager { make_settings_manager() }) { |
| 50 | + get().impl = std::make_unique<RetryScheduler<SettingsManagerInterface>>(std::move(settings_manager)); |
| 51 | + |
| 52 | + const auto available_devices { get().impl->execute([](auto &settings_iface) { return settings_iface.enumAvailableDevices(); }) }; |
| 53 | + BOOST_LOG(info) << "Currently available display devices:\n" |
| 54 | + << toJson(available_devices); |
| 55 | + |
| 56 | + // TODO: In the upcoming PR, schedule recovery here |
| 57 | + } |
| 58 | + |
| 59 | + class deinit_t: public platf::deinit_t { |
| 60 | + public: |
| 61 | + ~deinit_t() override { |
| 62 | + // TODO: In the upcoming PR, execute recovery once here |
| 63 | + get().impl = nullptr; |
| 64 | + } |
| 65 | + }; |
| 66 | + return std::make_unique<deinit_t>(); |
| 67 | + } |
| 68 | + |
| 69 | + std::string |
| 70 | + session_t::map_output_name(const std::string &output_name) const { |
| 71 | + if (impl) { |
| 72 | + return impl->execute([&output_name](auto &settings_iface) { return settings_iface.getDisplayName(output_name); }); |
| 73 | + } |
| 74 | + |
| 75 | + // Fallback to giving back the output name if the platform is not supported. |
| 76 | + return output_name; |
| 77 | + } |
| 78 | + |
| 79 | + session_t::session_t() = default; |
| 80 | +} // namespace display_device |
0 commit comments