| 1 |
/* |
| 2 |
* Copyright (C) 2017-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "Output.h" |
| 10 |
|
| 11 |
#include <cassert> |
| 12 |
#include <cmath> |
| 13 |
#include <mutex> |
| 14 |
#include <set> |
| 15 |
#include <stdexcept> |
| 16 |
#include <utility> |
| 17 |
|
| 18 |
using namespace KODI::WINDOWING::WAYLAND; |
| 19 |
|
| 20 |
COutput::COutput(std::uint32_t globalName, |
| 21 |
wayland::output_t const& output, |
| 22 |
std::function<void()> doneHandler) |
| 23 |
: m_globalName{globalName}, m_output{output}, m_doneHandler{std::move(doneHandler)} |
| 24 |
{ |
| 25 |
assert(m_output); |
| 26 |
|
| 27 |
m_output.on_geometry() = [this](std::int32_t x, std::int32_t y, std::int32_t physWidth, |
| 28 |
std::int32_t physHeight, wayland::output_subpixel, |
| 29 |
std::string const& make, std::string const& model, |
| 30 |
const wayland::output_transform&) |
| 31 |
{ |
| 32 |
std::unique_lock<CCriticalSection> lock(m_geometryCriticalSection); |
| 33 |
m_position = {x, y}; |
| 34 |
// Some monitors report invalid (non-positive) values that would cause an exception |
| 35 |
// with CSizeInt and/or lead to nonsensical DPI values. |
| 36 |
if (physWidth <= 0 || physHeight <= 0) |
| 37 |
m_physicalSize = {}; |
| 38 |
else |
| 39 |
m_physicalSize = {physWidth, physHeight}; |
| 40 |
m_make = make; |
| 41 |
m_model = model; |
| 42 |
}; |
| 43 |
m_output.on_mode() = [this](const wayland::output_mode& flags, std::int32_t width, |
| 44 |
std::int32_t height, std::int32_t refresh) { |
| 45 |
// std::set.emplace returns pair of iterator to the (possibly) inserted |
| 46 |
// element and boolean information whether the element was actually added |
| 47 |
// which we do not need |
| 48 |
auto modeIterator = m_modes.emplace(CSizeInt{width, height}, refresh).first; |
| 49 |
std::unique_lock<CCriticalSection> lock(m_iteratorCriticalSection); |
| 50 |
// Remember current and preferred mode |
| 51 |
// Current mode is the last one that was sent with current flag set |
| 52 |
if (flags & wayland::output_mode::current) |
| 53 |
{ |
| 54 |
m_currentMode = modeIterator; |
| 55 |
} |
| 56 |
if (flags & wayland::output_mode::preferred) |
| 57 |
{ |
| 58 |
m_preferredMode = modeIterator; |
| 59 |
} |
| 60 |
#ifdef TARGET_WEBOS |
| 61 |
// on_done() is never executed on webOS 3.x |
| 62 |
m_doneHandler(); |
| 63 |
#endif |
| 64 |
}; |
| 65 |
m_output.on_scale() = [this](std::int32_t scale) |
| 66 |
{ |
| 67 |
m_scale = scale; |
| 68 |
}; |
| 69 |
|
| 70 |
m_output.on_done() = [this]() |
| 71 |
{ |
| 72 |
#ifndef TARGET_WEBOS |
| 73 |
m_doneHandler(); |
| 74 |
#endif |
| 75 |
}; |
| 76 |
} |
| 77 |
|
| 78 |
COutput::~COutput() noexcept |
| 79 |
{ |
| 80 |
// Reset event handlers - someone might still hold a reference to the output_t, |
| 81 |
// causing events to be dispatched. They should not go to a deleted class. |
| 82 |
m_output.on_geometry() = nullptr; |
| 83 |
m_output.on_mode() = nullptr; |
| 84 |
m_output.on_done() = nullptr; |
| 85 |
m_output.on_scale() = nullptr; |
| 86 |
} |
| 87 |
|
| 88 |
const COutput::Mode& COutput::GetCurrentMode() const |
| 89 |
{ |
| 90 |
std::unique_lock<CCriticalSection> lock(m_iteratorCriticalSection); |
| 91 |
if (m_currentMode == m_modes.end()) |
| 92 |
{ |
| 93 |
throw std::runtime_error("Current mode not set"); |
| 94 |
} |
| 95 |
return *m_currentMode; |
| 96 |
} |
| 97 |
|
| 98 |
const COutput::Mode& COutput::GetPreferredMode() const |
| 99 |
{ |
| 100 |
std::unique_lock<CCriticalSection> lock(m_iteratorCriticalSection); |
| 101 |
if (m_preferredMode == m_modes.end()) |
| 102 |
{ |
| 103 |
throw std::runtime_error("Preferred mode not set"); |
| 104 |
} |
| 105 |
return *m_preferredMode; |
| 106 |
} |
| 107 |
|
| 108 |
float COutput::GetPixelRatioForMode(const Mode& mode) const |
| 109 |
{ |
| 110 |
std::unique_lock<CCriticalSection> lock(m_geometryCriticalSection); |
| 111 |
if (m_physicalSize.IsZero() || mode.size.IsZero()) |
| 112 |
{ |
| 113 |
return 1.0f; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
return ( |
| 118 |
(static_cast<float> (m_physicalSize.Width()) / static_cast<float> (mode.size.Width())) |
| 119 |
/ |
| 120 |
(static_cast<float> (m_physicalSize.Height()) / static_cast<float> (mode.size.Height())) |
| 121 |
); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
float COutput::GetDpiForMode(const Mode& mode) const |
| 126 |
{ |
| 127 |
std::unique_lock<CCriticalSection> lock(m_geometryCriticalSection); |
| 128 |
if (m_physicalSize.IsZero()) |
| 129 |
{ |
| 130 |
// We really have no idea, so use a "sane" default |
| 131 |
return 96.0; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
constexpr float INCH_MM_RATIO{25.4f}; |
| 136 |
|
| 137 |
float diagonalPixels = std::sqrt(mode.size.Width() * mode.size.Width() + mode.size.Height() * mode.size.Height()); |
| 138 |
// physicalWidth/physicalHeight is in millimeters |
| 139 |
float diagonalInches = |
| 140 |
std::sqrt(static_cast<float>(m_physicalSize.Width() * m_physicalSize.Width() + |
| 141 |
m_physicalSize.Height() * m_physicalSize.Height())) / |
| 142 |
INCH_MM_RATIO; |
| 143 |
|
| 144 |
return diagonalPixels / diagonalInches; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
float COutput::GetCurrentDpi() const |
| 149 |
{ |
| 150 |
return GetDpiForMode(GetCurrentMode()); |
| 151 |
} |