|
| 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 | +// local includes |
| 14 | +#include "platform/common.h" |
| 15 | + |
| 16 | +// platform-specific includes |
| 17 | +#ifdef _WIN32 |
| 18 | + #include <display_device/windows/settings_manager.h> |
| 19 | + #include <display_device/windows/win_api_layer.h> |
| 20 | + #include <display_device/windows/win_display_device.h> |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace display_device { |
| 24 | + namespace { |
| 25 | + /** |
| 26 | + * @brief A global for the settings manager interface whose lifetime is managed by `display_device::init()`. |
| 27 | + */ |
| 28 | + std::unique_ptr<RetryScheduler<SettingsManagerInterface>> SM_INSTANCE; |
| 29 | + |
| 30 | + /** |
| 31 | + * @brief Construct a settings manager interface to manage display device settings. |
| 32 | + * @return An interface or nullptr if the OS does not support the interface. |
| 33 | + */ |
| 34 | + std::unique_ptr<SettingsManagerInterface> |
| 35 | + make_settings_manager() { |
| 36 | +#ifdef _WIN32 |
| 37 | + // TODO: In the upcoming PR, add audio context capture and settings persistence |
| 38 | + return std::make_unique<SettingsManager>( |
| 39 | + std::make_shared<WinDisplayDevice>(std::make_shared<WinApiLayer>()), |
| 40 | + nullptr, |
| 41 | + std::make_unique<PersistentState>(nullptr), |
| 42 | + WinWorkarounds {}); |
| 43 | +#else |
| 44 | + return nullptr; |
| 45 | +#endif |
| 46 | + } |
| 47 | + } // namespace |
| 48 | + |
| 49 | + std::unique_ptr<platf::deinit_t> |
| 50 | + init() { |
| 51 | + // We can support re-init without any issues, however we should make sure to cleanup first! |
| 52 | + SM_INSTANCE = nullptr; |
| 53 | + |
| 54 | + // If we fail to create settings manager, this means platform is not supported and |
| 55 | + // we will need to provided error-free passtrough in other methods |
| 56 | + if (auto settings_manager { make_settings_manager() }) { |
| 57 | + SM_INSTANCE = std::make_unique<RetryScheduler<SettingsManagerInterface>>(std::move(settings_manager)); |
| 58 | + |
| 59 | + const auto available_devices { SM_INSTANCE->execute([](auto &settings_iface) { return settings_iface.enumAvailableDevices(); }) }; |
| 60 | + BOOST_LOG(info) << "Currently available display devices:\n" |
| 61 | + << toJson(available_devices); |
| 62 | + |
| 63 | + // TODO: In the upcoming PR, schedule recovery here |
| 64 | + } |
| 65 | + |
| 66 | + class deinit_t: public platf::deinit_t { |
| 67 | + public: |
| 68 | + ~deinit_t() override { |
| 69 | + // TODO: In the upcoming PR, execute recovery once here |
| 70 | + SM_INSTANCE = nullptr; |
| 71 | + } |
| 72 | + }; |
| 73 | + return std::make_unique<deinit_t>(); |
| 74 | + } |
| 75 | + |
| 76 | + std::string |
| 77 | + map_output_name(const std::string &output_name) { |
| 78 | + if (!SM_INSTANCE) { |
| 79 | + // Fallback to giving back the output name if the platform is not supported. |
| 80 | + return output_name; |
| 81 | + } |
| 82 | + |
| 83 | + return SM_INSTANCE->execute([&output_name](auto &settings_iface) { return settings_iface.getDisplayName(output_name); }); |
| 84 | + } |
| 85 | +} // namespace display_device |
0 commit comments