| 1 |
/* |
| 2 |
* Copyright (C) 2005-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 "WinSystemWin32.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "VideoSyncD3D.h" |
| 13 |
#include "WIN32Util.h" |
| 14 |
#include "WinEventsWin32.h" |
| 15 |
#include "application/Application.h" |
| 16 |
#include "cores/AudioEngine/AESinkFactory.h" |
| 17 |
#include "cores/AudioEngine/Sinks/AESinkDirectSound.h" |
| 18 |
#include "cores/AudioEngine/Sinks/AESinkWASAPI.h" |
| 19 |
#include "filesystem/File.h" |
| 20 |
#include "filesystem/SpecialProtocol.h" |
| 21 |
#include "messaging/ApplicationMessenger.h" |
| 22 |
#include "platform/Environment.h" |
| 23 |
#include "rendering/dx/ScreenshotSurfaceWindows.h" |
| 24 |
#include "resource.h" |
| 25 |
#include "settings/AdvancedSettings.h" |
| 26 |
#include "settings/DisplaySettings.h" |
| 27 |
#include "settings/Settings.h" |
| 28 |
#include "settings/SettingsComponent.h" |
| 29 |
#include "utils/StringUtils.h" |
| 30 |
#include "utils/SystemInfo.h" |
| 31 |
#include "utils/log.h" |
| 32 |
#include "windowing/GraphicContext.h" |
| 33 |
#include "windowing/windows/Win32DPMSSupport.h" |
| 34 |
|
| 35 |
#include "platform/win32/CharsetConverter.h" |
| 36 |
#include "platform/win32/input/IRServerSuite.h" |
| 37 |
|
| 38 |
#include <algorithm> |
| 39 |
#include <cmath> |
| 40 |
#include <mutex> |
| 41 |
|
| 42 |
#include <tpcshrd.h> |
| 43 |
|
| 44 |
using namespace std::chrono_literals; |
| 45 |
|
| 46 |
const char* CWinSystemWin32::SETTING_WINDOW_TOP = "window.top"; |
| 47 |
const char* CWinSystemWin32::SETTING_WINDOW_LEFT = "window.left"; |
| 48 |
|
| 49 |
CWinSystemWin32::CWinSystemWin32() |
| 50 |
: CWinSystemBase() |
| 51 |
, m_hWnd(nullptr) |
| 52 |
, m_hMonitor(nullptr) |
| 53 |
, m_hInstance(nullptr) |
| 54 |
, m_hIcon(nullptr) |
| 55 |
, m_ValidWindowedPosition(false) |
| 56 |
, m_IsAlteringWindow(false) |
| 57 |
, m_delayDispReset(false) |
| 58 |
, m_state(WINDOW_STATE_WINDOWED) |
| 59 |
, m_fullscreenState(WINDOW_FULLSCREEN_STATE_FULLSCREEN_WINDOW) |
| 60 |
, m_windowState(WINDOW_WINDOW_STATE_WINDOWED) |
| 61 |
, m_windowStyle(WINDOWED_STYLE) |
| 62 |
, m_windowExStyle(WINDOWED_EX_STYLE) |
| 63 |
, m_inFocus(false) |
| 64 |
, m_bMinimized(false) |
| 65 |
{ |
| 66 |
std::string cacert = CEnvironment::getenv("SSL_CERT_FILE"); |
| 67 |
if (cacert.empty() || !XFILE::CFile::Exists(cacert)) |
| 68 |
{ |
| 69 |
cacert = CSpecialProtocol::TranslatePath("special://xbmc/system/certs/cacert.pem"); |
| 70 |
if (XFILE::CFile::Exists(cacert)) |
| 71 |
CEnvironment::setenv("SSL_CERT_FILE", cacert.c_str(), 1); |
| 72 |
} |
| 73 |
|
| 74 |
m_winEvents.reset(new CWinEventsWin32()); |
| 75 |
AE::CAESinkFactory::ClearSinks(); |
| 76 |
CAESinkDirectSound::Register(); |
| 77 |
CAESinkWASAPI::Register(); |
| 78 |
CScreenshotSurfaceWindows::Register(); |
| 79 |
|
| 80 |
if (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bScanIRServer) |
| 81 |
{ |
| 82 |
m_irss.reset(new CIRServerSuite()); |
| 83 |
m_irss->Initialize(); |
| 84 |
} |
| 85 |
m_dpms = std::make_shared<CWin32DPMSSupport>(); |
| 86 |
} |
| 87 |
|
| 88 |
CWinSystemWin32::~CWinSystemWin32() |
| 89 |
{ |
| 90 |
if (m_hIcon) |
| 91 |
{ |
| 92 |
DestroyIcon(m_hIcon); |
| 93 |
m_hIcon = nullptr; |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
bool CWinSystemWin32::InitWindowSystem() |
| 98 |
{ |
| 99 |
if(!CWinSystemBase::InitWindowSystem()) |
| 100 |
return false; |
| 101 |
|
| 102 |
return true; |
| 103 |
} |
| 104 |
|
| 105 |
bool CWinSystemWin32::DestroyWindowSystem() |
| 106 |
{ |
| 107 |
if (m_hMonitor) |
| 108 |
{ |
| 109 |
MONITOR_DETAILS* details = GetDisplayDetails(m_hMonitor); |
| 110 |
if (details) |
| 111 |
RestoreDesktopResolution(details); |
| 112 |
} |
| 113 |
return true; |
| 114 |
} |
| 115 |
|
| 116 |
bool CWinSystemWin32::CreateNewWindow(const std::string& name, bool fullScreen, RESOLUTION_INFO& res) |
| 117 |
{ |
| 118 |
using KODI::PLATFORM::WINDOWS::ToW; |
| 119 |
auto nameW = ToW(name); |
| 120 |
|
| 121 |
m_hInstance = static_cast<HINSTANCE>(GetModuleHandle(nullptr)); |
| 122 |
if(m_hInstance == nullptr) |
| 123 |
CLog::LogF(LOGDEBUG, " GetModuleHandle failed with {}", GetLastError()); |
| 124 |
|
| 125 |
UpdateStates(fullScreen); |
| 126 |
// initialize the state |
| 127 |
WINDOW_STATE state = GetState(fullScreen); |
| 128 |
|
| 129 |
m_nWidth = res.iWidth; |
| 130 |
m_nHeight = res.iHeight; |
| 131 |
m_bFullScreen = fullScreen; |
| 132 |
m_fRefreshRate = res.fRefreshRate; |
| 133 |
|
| 134 |
m_hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON)); |
| 135 |
|
| 136 |
// Register the windows class |
| 137 |
WNDCLASSEX wndClass = {}; |
| 138 |
wndClass.cbSize = sizeof(wndClass); |
| 139 |
wndClass.style = CS_HREDRAW | CS_VREDRAW; |
| 140 |
wndClass.lpfnWndProc = CWinEventsWin32::WndProc; |
| 141 |
wndClass.cbClsExtra = 0; |
| 142 |
wndClass.cbWndExtra = 0; |
| 143 |
wndClass.hInstance = m_hInstance; |
| 144 |
wndClass.hIcon = m_hIcon; |
| 145 |
wndClass.hCursor = LoadCursor(nullptr, IDC_ARROW ); |
| 146 |
wndClass.hbrBackground = static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)); |
| 147 |
wndClass.lpszMenuName = nullptr; |
| 148 |
wndClass.lpszClassName = nameW.c_str(); |
| 149 |
|
| 150 |
if( !RegisterClassExW( &wndClass ) ) |
| 151 |
{ |
| 152 |
CLog::LogF(LOGERROR, " RegisterClassExW failed with {}", GetLastError()); |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
// put the window at desired display |
| 157 |
RECT screenRect = ScreenRect(m_hMonitor); |
| 158 |
m_nLeft = screenRect.left; |
| 159 |
m_nTop = screenRect.top; |
| 160 |
|
| 161 |
if (state == WINDOW_STATE_WINDOWED) |
| 162 |
{ |
| 163 |
const auto settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 164 |
const int top = settings->GetInt(SETTING_WINDOW_TOP); |
| 165 |
const int left = settings->GetInt(SETTING_WINDOW_LEFT); |
| 166 |
const RECT vsRect = GetVirtualScreenRect(); |
| 167 |
|
| 168 |
// we check that window is inside of virtual screen rect (sum of all monitors) |
| 169 |
// top 0 left 0 is a special position that centers the window on the screen |
| 170 |
if ((top != 0 || left != 0) && top >= vsRect.top && top + m_nHeight <= vsRect.bottom && |
| 171 |
left >= vsRect.left && left + m_nWidth <= vsRect.right) |
| 172 |
{ |
| 173 |
// restore previous window position |
| 174 |
m_nLeft = left; |
| 175 |
m_nTop = top; |
| 176 |
} |
| 177 |
else |
| 178 |
{ |
| 179 |
// Windowed mode: position and size in settings and most places in Kodi |
| 180 |
// are for the client part of the window. |
| 181 |
RECT rcWorkArea = GetScreenWorkArea(m_hMonitor); |
| 182 |
|
| 183 |
RECT rcNcArea = GetNcAreaOffsets(m_windowStyle, false, m_windowExStyle); |
| 184 |
int maxClientWidth = (rcWorkArea.right - rcNcArea.right) - (rcWorkArea.left - rcNcArea.left); |
| 185 |
int maxClientHeight = (rcWorkArea.bottom - rcNcArea.bottom) - (rcWorkArea.top - rcNcArea.top); |
| 186 |
|
| 187 |
m_nWidth = std::min(m_nWidth, maxClientWidth); |
| 188 |
m_nHeight = std::min(m_nHeight, maxClientHeight); |
| 189 |
CWinSystemBase::SetWindowResolution(m_nWidth, m_nHeight); |
| 190 |
|
| 191 |
// center window on desktop |
| 192 |
m_nLeft = rcWorkArea.left - rcNcArea.left + (maxClientWidth - m_nWidth) / 2; |
| 193 |
m_nTop = rcWorkArea.top - rcNcArea.top + (maxClientHeight - m_nHeight) / 2; |
| 194 |
} |
| 195 |
m_ValidWindowedPosition = true; |
| 196 |
} |
| 197 |
|
| 198 |
HWND hWnd = CreateWindowExW( |
| 199 |
m_windowExStyle, |
| 200 |
nameW.c_str(), |
| 201 |
nameW.c_str(), |
| 202 |
m_windowStyle, |
| 203 |
m_nLeft, |
| 204 |
m_nTop, |
| 205 |
m_nWidth, |
| 206 |
m_nHeight, |
| 207 |
nullptr, |
| 208 |
nullptr, |
| 209 |
m_hInstance, |
| 210 |
nullptr |
| 211 |
); |
| 212 |
|
| 213 |
if( hWnd == nullptr ) |
| 214 |
{ |
| 215 |
CLog::LogF(LOGERROR, " CreateWindow failed with {}", GetLastError()); |
| 216 |
return false; |
| 217 |
} |
| 218 |
|
| 219 |
m_inFocus = true; |
| 220 |
|
| 221 |
DWORD dwHwndTabletProperty = |
| 222 |
TABLET_DISABLE_PENBARRELFEEDBACK | // disables UI feedback on pen button down (circle) |
| 223 |
TABLET_DISABLE_FLICKS; // disables pen flicks (back, forward, drag down, drag up) |
| 224 |
|
| 225 |
SetProp(hWnd, MICROSOFT_TABLETPENSERVICE_PROPERTY, &dwHwndTabletProperty); |
| 226 |
|
| 227 |
m_hWnd = hWnd; |
| 228 |
m_bWindowCreated = true; |
| 229 |
|
| 230 |
CreateBlankWindows(); |
| 231 |
|
| 232 |
m_state = state; |
| 233 |
AdjustWindow(true); |
| 234 |
|
| 235 |
// Show the window |
| 236 |
ShowWindow( m_hWnd, SW_SHOWDEFAULT ); |
| 237 |
UpdateWindow( m_hWnd ); |
| 238 |
|
| 239 |
// Configure the tray icon. |
| 240 |
m_trayIcon.cbSize = sizeof(m_trayIcon); |
| 241 |
m_trayIcon.hWnd = m_hWnd; |
| 242 |
m_trayIcon.hIcon = m_hIcon; |
| 243 |
wcsncpy(m_trayIcon.szTip, nameW.c_str(), sizeof(m_trayIcon.szTip) / sizeof(WCHAR)); |
| 244 |
m_trayIcon.uCallbackMessage = TRAY_ICON_NOTIFY; |
| 245 |
m_trayIcon.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE; |
| 246 |
|
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
bool CWinSystemWin32::CreateBlankWindows() |
| 251 |
{ |
| 252 |
WNDCLASSEX wcex; |
| 253 |
|
| 254 |
wcex.cbSize = sizeof(WNDCLASSEX); |
| 255 |
wcex.style= CS_HREDRAW | CS_VREDRAW; |
| 256 |
wcex.lpfnWndProc= DefWindowProc; |
| 257 |
wcex.cbClsExtra= 0; |
| 258 |
wcex.cbWndExtra= 0; |
| 259 |
wcex.hInstance= nullptr; |
| 260 |
wcex.hIcon= nullptr; |
| 261 |
wcex.hCursor= nullptr; |
| 262 |
wcex.hbrBackground= static_cast<HBRUSH>(CreateSolidBrush(RGB(0, 0, 0))); |
| 263 |
wcex.lpszMenuName= nullptr; |
| 264 |
wcex.lpszClassName= L"BlankWindowClass"; |
| 265 |
wcex.hIconSm= nullptr; |
| 266 |
|
| 267 |
// Now we can go ahead and register our new window class |
| 268 |
if(!RegisterClassEx(&wcex)) |
| 269 |
{ |
| 270 |
CLog::LogF(LOGERROR, "RegisterClass failed with {}", GetLastError()); |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
// We need as many blank windows as there are screens (minus 1) |
| 275 |
for (size_t i = 0; i < m_displays.size() - 1; i++) |
| 276 |
{ |
| 277 |
HWND hBlankWindow = CreateWindowEx(WS_EX_TOPMOST, L"BlankWindowClass", L"", WS_POPUP | WS_DISABLED, |
| 278 |
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr, nullptr, nullptr); |
| 279 |
|
| 280 |
if (hBlankWindow == nullptr) |
| 281 |
{ |
| 282 |
CLog::LogF(LOGERROR, "CreateWindowEx failed with {}", GetLastError()); |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
m_hBlankWindows.push_back(hBlankWindow); |
| 287 |
} |
| 288 |
|
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
bool CWinSystemWin32::BlankNonActiveMonitors(bool bBlank) |
| 293 |
{ |
| 294 |
if (m_hBlankWindows.empty()) |
| 295 |
return false; |
| 296 |
|
| 297 |
if (bBlank == false) |
| 298 |
{ |
| 299 |
for (unsigned int i=0; i < m_hBlankWindows.size(); i++) |
| 300 |
ShowWindow(m_hBlankWindows[i], SW_HIDE); |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
// Move a blank window in front of every display, except the current display. |
| 305 |
for (size_t i = 0, j = 0; i < m_displays.size(); ++i) |
| 306 |
{ |
| 307 |
MONITOR_DETAILS& details = m_displays[i]; |
| 308 |
if (details.hMonitor == m_hMonitor) |
| 309 |
continue; |
| 310 |
|
| 311 |
RECT rBounds = ScreenRect(details.hMonitor); |
| 312 |
// move and resize the window |
| 313 |
SetWindowPos(m_hBlankWindows[j], nullptr, rBounds.left, rBounds.top, |
| 314 |
rBounds.right - rBounds.left, rBounds.bottom - rBounds.top, |
| 315 |
SWP_NOACTIVATE); |
| 316 |
|
| 317 |
ShowWindow(m_hBlankWindows[j], SW_SHOW | SW_SHOWNOACTIVATE); |
| 318 |
j++; |
| 319 |
} |
| 320 |
|
| 321 |
if (m_hWnd) |
| 322 |
SetForegroundWindow(m_hWnd); |
| 323 |
|
| 324 |
return true; |
| 325 |
} |
| 326 |
|
| 327 |
bool CWinSystemWin32::CenterWindow() |
| 328 |
{ |
| 329 |
RESOLUTION_INFO DesktopRes = CDisplaySettings::GetInstance().GetResolutionInfo(RES_DESKTOP); |
| 330 |
|
| 331 |
m_nLeft = (DesktopRes.iWidth / 2) - (m_nWidth / 2); |
| 332 |
m_nTop = (DesktopRes.iHeight / 2) - (m_nHeight / 2); |
| 333 |
|
| 334 |
RECT rc; |
| 335 |
rc.left = m_nLeft; |
| 336 |
rc.top = m_nTop; |
| 337 |
rc.right = rc.left + m_nWidth; |
| 338 |
rc.bottom = rc.top + m_nHeight; |
| 339 |
AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, false ); |
| 340 |
|
| 341 |
SetWindowPos(m_hWnd, nullptr, rc.left, rc.top, 0, 0, SWP_NOSIZE); |
| 342 |
|
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
bool CWinSystemWin32::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop) |
| 347 |
{ |
| 348 |
m_nWidth = newWidth; |
| 349 |
m_nHeight = newHeight; |
| 350 |
|
| 351 |
if (newLeft > 0) |
| 352 |
m_nLeft = newLeft; |
| 353 |
|
| 354 |
if (newTop > 0) |
| 355 |
m_nTop = newTop; |
| 356 |
|
| 357 |
AdjustWindow(); |
| 358 |
|
| 359 |
return true; |
| 360 |
} |
| 361 |
|
| 362 |
void CWinSystemWin32::FinishWindowResize(int newWidth, int newHeight) |
| 363 |
{ |
| 364 |
m_nWidth = newWidth; |
| 365 |
m_nHeight = newHeight; |
| 366 |
} |
| 367 |
|
| 368 |
void CWinSystemWin32::ForceFullScreen(const RESOLUTION_INFO& resInfo) |
| 369 |
{ |
| 370 |
ResizeWindow(resInfo.iScreenWidth, resInfo.iScreenHeight, 0, 0); |
| 371 |
} |
| 372 |
|
| 373 |
void CWinSystemWin32::AdjustWindow(bool forceResize) |
| 374 |
{ |
| 375 |
CLog::LogF(LOGDEBUG, "adjusting window if required."); |
| 376 |
|
| 377 |
HWND windowAfter; |
| 378 |
RECT rc; |
| 379 |
|
| 380 |
if (m_state == WINDOW_STATE_FULLSCREEN_WINDOW || m_state == WINDOW_STATE_FULLSCREEN) |
| 381 |
{ |
| 382 |
windowAfter = HWND_TOP; |
| 383 |
rc = ScreenRect(m_hMonitor); |
| 384 |
} |
| 385 |
else // m_state == WINDOW_STATE_WINDOWED |
| 386 |
{ |
| 387 |
windowAfter = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST; |
| 388 |
|
| 389 |
if (!m_ValidWindowedPosition) |
| 390 |
{ |
| 391 |
const auto settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 392 |
const int top = settings->GetInt(SETTING_WINDOW_TOP); |
| 393 |
const int left = settings->GetInt(SETTING_WINDOW_LEFT); |
| 394 |
const RECT vsRect = GetVirtualScreenRect(); |
| 395 |
|
| 396 |
// we check that window is inside of virtual screen rect (sum of all monitors) |
| 397 |
// top 0 left 0 is a special position that centers the window on the screen |
| 398 |
if ((top != 0 || left != 0) && top >= vsRect.top && top + m_nHeight <= vsRect.bottom && |
| 399 |
left >= vsRect.left && left + m_nWidth <= vsRect.right) |
| 400 |
{ |
| 401 |
// restore previous window position |
| 402 |
m_nTop = top; |
| 403 |
m_nLeft = left; |
| 404 |
} |
| 405 |
else |
| 406 |
{ |
| 407 |
// Windowed mode: position and size in settings and most places in Kodi |
| 408 |
// are for the client part of the window. |
| 409 |
RECT rcWorkArea = GetScreenWorkArea(m_hMonitor); |
| 410 |
|
| 411 |
RECT rcNcArea = GetNcAreaOffsets(m_windowStyle, false, m_windowExStyle); |
| 412 |
int maxClientWidth = |
| 413 |
(rcWorkArea.right - rcNcArea.right) - (rcWorkArea.left - rcNcArea.left); |
| 414 |
int maxClientHeight = |
| 415 |
(rcWorkArea.bottom - rcNcArea.bottom) - (rcWorkArea.top - rcNcArea.top); |
| 416 |
|
| 417 |
m_nWidth = std::min(m_nWidth, maxClientWidth); |
| 418 |
m_nHeight = std::min(m_nHeight, maxClientHeight); |
| 419 |
CWinSystemBase::SetWindowResolution(m_nWidth, m_nHeight); |
| 420 |
|
| 421 |
// center window on desktop |
| 422 |
m_nLeft = rcWorkArea.left - rcNcArea.left + (maxClientWidth - m_nWidth) / 2; |
| 423 |
m_nTop = rcWorkArea.top - rcNcArea.top + (maxClientHeight - m_nHeight) / 2; |
| 424 |
} |
| 425 |
m_ValidWindowedPosition = true; |
| 426 |
} |
| 427 |
|
| 428 |
rc.left = m_nLeft; |
| 429 |
rc.right = m_nLeft + m_nWidth; |
| 430 |
rc.top = m_nTop; |
| 431 |
rc.bottom = m_nTop + m_nHeight; |
| 432 |
|
| 433 |
HMONITOR hMon = MonitorFromRect(&rc, MONITOR_DEFAULTTONULL); |
| 434 |
HMONITOR hMon2 = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTOPRIMARY); |
| 435 |
|
| 436 |
if (!m_ValidWindowedPosition || hMon == nullptr || hMon != hMon2) |
| 437 |
{ |
| 438 |
// centering window at desktop |
| 439 |
RECT newScreenRect = ScreenRect(hMon2); |
| 440 |
rc.left = m_nLeft = newScreenRect.left + ((newScreenRect.right - newScreenRect.left) / 2) - (m_nWidth / 2); |
| 441 |
rc.top = m_nTop = newScreenRect.top + ((newScreenRect.bottom - newScreenRect.top) / 2) - (m_nHeight / 2); |
| 442 |
rc.right = m_nLeft + m_nWidth; |
| 443 |
rc.bottom = m_nTop + m_nHeight; |
| 444 |
m_ValidWindowedPosition = true; |
| 445 |
} |
| 446 |
AdjustWindowRectEx(&rc, m_windowStyle, false, m_windowExStyle); |
| 447 |
} |
| 448 |
|
| 449 |
WINDOWINFO wi; |
| 450 |
wi.cbSize = sizeof(WINDOWINFO); |
| 451 |
if (!GetWindowInfo(m_hWnd, &wi)) |
| 452 |
{ |
| 453 |
CLog::LogF(LOGERROR, "GetWindowInfo failed with {}", GetLastError()); |
| 454 |
return; |
| 455 |
} |
| 456 |
RECT wr = wi.rcWindow; |
| 457 |
|
| 458 |
if ( wr.bottom - wr.top == rc.bottom - rc.top |
| 459 |
&& wr.right - wr.left == rc.right - rc.left |
| 460 |
&& (wi.dwStyle & WS_CAPTION) == (m_windowStyle & WS_CAPTION) |
| 461 |
&& !forceResize) |
| 462 |
{ |
| 463 |
return; |
| 464 |
} |
| 465 |
|
| 466 |
//Sets the window style |
| 467 |
SetLastError(0); |
| 468 |
SetWindowLongPtr( m_hWnd, GWL_STYLE, m_windowStyle ); |
| 469 |
|
| 470 |
//Sets the window ex style |
| 471 |
SetLastError(0); |
| 472 |
SetWindowLongPtr( m_hWnd, GWL_EXSTYLE, m_windowExStyle ); |
| 473 |
|
| 474 |
// resize window |
| 475 |
CLog::LogF(LOGDEBUG, "resizing due to size change ({},{},{},{}{})->({},{},{},{}{})", wr.left, |
| 476 |
wr.top, wr.right, wr.bottom, (wi.dwStyle & WS_CAPTION) ? "" : " fullscreen", rc.left, |
| 477 |
rc.top, rc.right, rc.bottom, (m_windowStyle & WS_CAPTION) ? "" : " fullscreen"); |
| 478 |
SetWindowPos( |
| 479 |
m_hWnd, |
| 480 |
windowAfter, |
| 481 |
rc.left, |
| 482 |
rc.top, |
| 483 |
rc.right - rc.left, |
| 484 |
rc.bottom - rc.top, |
| 485 |
SWP_SHOWWINDOW | SWP_DRAWFRAME |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
void CWinSystemWin32::CenterCursor() const |
| 490 |
{ |
| 491 |
RECT rect; |
| 492 |
POINT point = {}; |
| 493 |
|
| 494 |
//Gets the client rect, then translates it to screen coordinates |
| 495 |
//so that SetCursorPos isn't called with relative x and y values |
| 496 |
GetClientRect(m_hWnd, &rect); |
| 497 |
ClientToScreen(m_hWnd, &point); |
| 498 |
|
| 499 |
rect.left += point.x; |
| 500 |
rect.right += point.x; |
| 501 |
rect.top += point.y; |
| 502 |
rect.bottom += point.y; |
| 503 |
|
| 504 |
int x = rect.left + (rect.right - rect.left) / 2; |
| 505 |
int y = rect.top + (rect.bottom - rect.top) / 2; |
| 506 |
|
| 507 |
SetCursorPos(x, y); |
| 508 |
} |
| 509 |
|
| 510 |
bool CWinSystemWin32::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays) |
| 511 |
{ |
| 512 |
// Initialisation not finished, pretend the function succeeded |
| 513 |
if (!m_hWnd) |
| 514 |
return true; |
| 515 |
|
| 516 |
CWinSystemWin32::UpdateStates(fullScreen); |
| 517 |
WINDOW_STATE state = GetState(fullScreen); |
| 518 |
|
| 519 |
CLog::LogF(LOGDEBUG, "({}) with size {}x{}, refresh {:f}{}", window_state_names[state], |
| 520 |
res.iWidth, res.iHeight, res.fRefreshRate, |
| 521 |
(res.dwFlags & D3DPRESENTFLAG_INTERLACED) ? "i" : ""); |
| 522 |
|
| 523 |
// oldMonitor may be NULL if it's powered off or not available due windows settings |
| 524 |
MONITOR_DETAILS* oldMonitor = GetDisplayDetails(m_hMonitor); |
| 525 |
MONITOR_DETAILS* newMonitor = GetDisplayDetails(res.strOutput); |
| 526 |
|
| 527 |
bool forceChange = false; // resolution/display is changed but window state isn't changed |
| 528 |
bool changeScreen = false; // display is changed |
| 529 |
bool stereoChange = IsStereoEnabled() != (CServiceBroker::GetWinSystem()->GetGfxContext().GetStereoMode() == RENDER_STEREO_MODE_HARDWAREBASED); |
| 530 |
|
| 531 |
if (m_nWidth != res.iWidth || m_nHeight != res.iHeight || m_fRefreshRate != res.fRefreshRate || |
| 532 |
!oldMonitor || oldMonitor->hMonitor != newMonitor->hMonitor || stereoChange || |
| 533 |
m_bFirstResChange) |
| 534 |
{ |
| 535 |
if (!oldMonitor || oldMonitor->hMonitor != newMonitor->hMonitor) |
| 536 |
changeScreen = true; |
| 537 |
forceChange = true; |
| 538 |
} |
| 539 |
|
| 540 |
if (state == m_state && !forceChange) |
| 541 |
{ |
| 542 |
m_bBlankOtherDisplay = blankOtherDisplays; |
| 543 |
BlankNonActiveMonitors(m_bBlankOtherDisplay); |
| 544 |
return true; |
| 545 |
} |
| 546 |
|
| 547 |
// entering to stereo mode, limit resolution to [email protected] |
| 548 |
if (stereoChange && !IsStereoEnabled() && res.iWidth > 1280) |
| 549 |
{ |
| 550 |
res = CDisplaySettings::GetInstance().GetResolutionInfo( |
| 551 |
CResolutionUtils::ChooseBestResolution(24.f / 1.001f, 1920, 1080, true)); |
| 552 |
} |
| 553 |
|
| 554 |
if (m_state == WINDOW_STATE_WINDOWED) |
| 555 |
{ |
| 556 |
WINDOWINFO wi = {}; |
| 557 |
wi.cbSize = sizeof(WINDOWINFO); |
| 558 |
if (GetWindowInfo(m_hWnd, &wi) && wi.rcClient.top > 0) |
| 559 |
{ |
| 560 |
m_nLeft = wi.rcClient.left; |
| 561 |
m_nTop = wi.rcClient.top; |
| 562 |
m_ValidWindowedPosition = true; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
m_IsAlteringWindow = true; |
| 567 |
ReleaseBackBuffer(); |
| 568 |
|
| 569 |
if (changeScreen) |
| 570 |
{ |
| 571 |
// before we changing display we have to leave exclusive mode on "old" display |
| 572 |
if (m_state == WINDOW_STATE_FULLSCREEN) |
| 573 |
SetDeviceFullScreen(false, res); |
| 574 |
|
| 575 |
// restoring native resolution on "old" display |
| 576 |
RestoreDesktopResolution(oldMonitor); |
| 577 |
|
| 578 |
// notify about screen change (it may require recreate rendering device) |
| 579 |
m_fRefreshRate = res.fRefreshRate; // use desired refresh for driver hook |
| 580 |
OnScreenChange(newMonitor->hMonitor); |
| 581 |
} |
| 582 |
|
| 583 |
m_bFirstResChange = false; |
| 584 |
m_bFullScreen = fullScreen; |
| 585 |
m_hMonitor = newMonitor->hMonitor; |
| 586 |
m_nWidth = res.iWidth; |
| 587 |
m_nHeight = res.iHeight; |
| 588 |
m_bBlankOtherDisplay = blankOtherDisplays; |
| 589 |
m_fRefreshRate = res.fRefreshRate; |
| 590 |
|
| 591 |
if (state == WINDOW_STATE_FULLSCREEN) |
| 592 |
{ |
| 593 |
SetForegroundWindowInternal(m_hWnd); |
| 594 |
|
| 595 |
m_state = state; |
| 596 |
AdjustWindow(changeScreen); |
| 597 |
|
| 598 |
// enter in exclusive mode, this will change resolution if we already in |
| 599 |
SetDeviceFullScreen(true, res); |
| 600 |
} |
| 601 |
else if (m_state == WINDOW_STATE_FULLSCREEN || m_state == WINDOW_STATE_FULLSCREEN_WINDOW) // we're in fullscreen state now |
| 602 |
{ |
| 603 |
// guess we are leaving exclusive mode, this will not an effect if we already not in |
| 604 |
SetDeviceFullScreen(false, res); |
| 605 |
|
| 606 |
if (state == WINDOW_STATE_WINDOWED) // go to a windowed state |
| 607 |
{ |
| 608 |
// need to restore resolution if it was changed to not native |
| 609 |
// because we do not support resolution change in windowed mode |
| 610 |
RestoreDesktopResolution(newMonitor); |
| 611 |
} |
| 612 |
else if (state == WINDOW_STATE_FULLSCREEN_WINDOW) // enter fullscreen window instead |
| 613 |
{ |
| 614 |
ChangeResolution(res, stereoChange); |
| 615 |
} |
| 616 |
|
| 617 |
m_state = state; |
| 618 |
AdjustWindow(changeScreen); |
| 619 |
} |
| 620 |
else // we're in windowed state now |
| 621 |
{ |
| 622 |
if (state == WINDOW_STATE_FULLSCREEN_WINDOW) |
| 623 |
{ |
| 624 |
ChangeResolution(res, stereoChange); |
| 625 |
|
| 626 |
m_state = state; |
| 627 |
AdjustWindow(changeScreen); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
if (changeScreen) |
| 632 |
CenterCursor(); |
| 633 |
|
| 634 |
CreateBackBuffer(); |
| 635 |
|
| 636 |
BlankNonActiveMonitors(m_bBlankOtherDisplay); |
| 637 |
m_IsAlteringWindow = false; |
| 638 |
|
| 639 |
return true; |
| 640 |
} |
| 641 |
|
| 642 |
bool CWinSystemWin32::DPIChanged(WORD dpi, RECT windowRect) const |
| 643 |
{ |
| 644 |
(void)dpi; |
| 645 |
RECT resizeRect = windowRect; |
| 646 |
HMONITOR hMon = MonitorFromRect(&resizeRect, MONITOR_DEFAULTTONULL); |
| 647 |
if (hMon == nullptr) |
| 648 |
{ |
| 649 |
hMon = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTOPRIMARY); |
| 650 |
} |
| 651 |
|
| 652 |
if (hMon) |
| 653 |
{ |
| 654 |
MONITORINFOEX monitorInfo; |
| 655 |
monitorInfo.cbSize = sizeof(MONITORINFOEX); |
| 656 |
GetMonitorInfoW(hMon, &monitorInfo); |
| 657 |
|
| 658 |
if (m_state == WINDOW_STATE_FULLSCREEN_WINDOW || |
| 659 |
m_state == WINDOW_STATE_FULLSCREEN) |
| 660 |
{ |
| 661 |
resizeRect = monitorInfo.rcMonitor; // the whole screen |
| 662 |
} |
| 663 |
else |
| 664 |
{ |
| 665 |
RECT wr = monitorInfo.rcWork; // it excludes task bar |
| 666 |
long wrWidth = wr.right - wr.left; |
| 667 |
long wrHeight = wr.bottom - wr.top; |
| 668 |
long resizeWidth = resizeRect.right - resizeRect.left; |
| 669 |
long resizeHeight = resizeRect.bottom - resizeRect.top; |
| 670 |
|
| 671 |
if (resizeWidth > wrWidth) |
| 672 |
{ |
| 673 |
resizeRect.right = resizeRect.left + wrWidth; |
| 674 |
} |
| 675 |
|
| 676 |
// make sure suggested windows size is not taller or wider than working area of new monitor (considers the toolbar) |
| 677 |
if (resizeHeight > wrHeight) |
| 678 |
{ |
| 679 |
resizeRect.bottom = resizeRect.top + wrHeight; |
| 680 |
} |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
// resize the window to the suggested size. Will generate a WM_SIZE event |
| 685 |
SetWindowPos(m_hWnd, |
| 686 |
nullptr, |
| 687 |
resizeRect.left, |
| 688 |
resizeRect.top, |
| 689 |
resizeRect.right - resizeRect.left, |
| 690 |
resizeRect.bottom - resizeRect.top, |
| 691 |
SWP_NOZORDER | SWP_NOACTIVATE); |
| 692 |
|
| 693 |
return true; |
| 694 |
} |
| 695 |
|
| 696 |
void CWinSystemWin32::SetMinimized(bool minimized) |
| 697 |
{ |
| 698 |
const auto advancedSettings = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings(); |
| 699 |
|
| 700 |
if (advancedSettings->m_minimizeToTray) |
| 701 |
{ |
| 702 |
if (minimized) |
| 703 |
{ |
| 704 |
Shell_NotifyIcon(NIM_ADD, &m_trayIcon); |
| 705 |
ShowWindow(m_hWnd, SW_HIDE); |
| 706 |
} |
| 707 |
else |
| 708 |
{ |
| 709 |
Shell_NotifyIcon(NIM_DELETE, &m_trayIcon); |
| 710 |
ShowWindow(m_hWnd, SW_RESTORE); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
m_bMinimized = minimized; |
| 715 |
} |
| 716 |
|
| 717 |
std::vector<std::string> CWinSystemWin32::GetConnectedOutputs() |
| 718 |
{ |
| 719 |
std::vector<std::string> outputs; |
| 720 |
|
| 721 |
for (auto& display : m_displays) |
| 722 |
{ |
| 723 |
outputs.emplace_back(KODI::PLATFORM::WINDOWS::FromW(display.MonitorNameW)); |
| 724 |
} |
| 725 |
|
| 726 |
return outputs; |
| 727 |
} |
| 728 |
|
| 729 |
void CWinSystemWin32::RestoreDesktopResolution(MONITOR_DETAILS* details) |
| 730 |
{ |
| 731 |
if (!details) |
| 732 |
return; |
| 733 |
|
| 734 |
RESOLUTION_INFO info; |
| 735 |
info.iWidth = details->ScreenWidth; |
| 736 |
info.iHeight = details->ScreenHeight; |
| 737 |
if ((details->RefreshRate + 1) % 24 == 0 || (details->RefreshRate + 1) % 30 == 0) |
| 738 |
info.fRefreshRate = static_cast<float>(details->RefreshRate + 1) / 1.001f; |
| 739 |
else |
| 740 |
info.fRefreshRate = static_cast<float>(details->RefreshRate); |
| 741 |
info.strOutput = KODI::PLATFORM::WINDOWS::FromW(details->DeviceNameW); |
| 742 |
info.dwFlags = details->Interlaced ? D3DPRESENTFLAG_INTERLACED : 0; |
| 743 |
|
| 744 |
CLog::LogF(LOGDEBUG, "restoring desktop resolution for '{}'.", KODI::PLATFORM::WINDOWS::FromW(details->MonitorNameW)); |
| 745 |
ChangeResolution(info); |
| 746 |
} |
| 747 |
|
| 748 |
MONITOR_DETAILS* CWinSystemWin32::GetDisplayDetails(const std::string& name) |
| 749 |
{ |
| 750 |
using KODI::PLATFORM::WINDOWS::ToW; |
| 751 |
|
| 752 |
if (!name.empty() && name != "Default") |
| 753 |
{ |
| 754 |
std::wstring nameW = ToW(name); |
| 755 |
auto it = std::find_if(m_displays.begin(), m_displays.end(), [&nameW](MONITOR_DETAILS& m) |
| 756 |
{ |
| 757 |
if (nameW[0] == '\\') // name is device name |
| 758 |
return m.DeviceNameW == nameW; |
| 759 |
else if (m.MonitorNameW == nameW) |
| 760 |
return true; |
| 761 |
else if (m.DeviceStringW == nameW) |
| 762 |
return true; |
| 763 |
return false; |
| 764 |
}); |
| 765 |
if (it != m_displays.end()) |
| 766 |
return &(*it); |
| 767 |
} |
| 768 |
|
| 769 |
// fallback to primary |
| 770 |
auto it = std::find_if(m_displays.begin(), m_displays.end(), [](MONITOR_DETAILS& m) |
| 771 |
{ |
| 772 |
return m.IsPrimary; |
| 773 |
}); |
| 774 |
if (it != m_displays.end()) |
| 775 |
return &(*it); |
| 776 |
|
| 777 |
// nothing found |
| 778 |
return nullptr; |
| 779 |
} |
| 780 |
|
| 781 |
MONITOR_DETAILS* CWinSystemWin32::GetDisplayDetails(HMONITOR handle) |
| 782 |
{ |
| 783 |
auto it = std::find_if(m_displays.begin(), m_displays.end(), [&handle](MONITOR_DETAILS& m) |
| 784 |
{ |
| 785 |
return m.hMonitor == handle; |
| 786 |
}); |
| 787 |
if (it != m_displays.end()) |
| 788 |
return &(*it); |
| 789 |
|
| 790 |
return nullptr; |
| 791 |
} |
| 792 |
|
| 793 |
RECT CWinSystemWin32::ScreenRect(HMONITOR handle) |
| 794 |
{ |
| 795 |
const MONITOR_DETAILS* details = GetDisplayDetails(handle); |
| 796 |
if (!details) |
| 797 |
{ |
| 798 |
CLog::LogF(LOGERROR, "no monitor found for handle"); |
| 799 |
return RECT(); |
| 800 |
} |
| 801 |
|
| 802 |
DEVMODEW sDevMode = {}; |
| 803 |
sDevMode.dmSize = sizeof(sDevMode); |
| 804 |
if(!EnumDisplaySettingsW(details->DeviceNameW.c_str(), ENUM_CURRENT_SETTINGS, &sDevMode)) |
| 805 |
CLog::LogF(LOGERROR, " EnumDisplaySettings failed with {}", GetLastError()); |
| 806 |
|
| 807 |
RECT rc; |
| 808 |
rc.left = sDevMode.dmPosition.x; |
| 809 |
rc.right = sDevMode.dmPosition.x + sDevMode.dmPelsWidth; |
| 810 |
rc.top = sDevMode.dmPosition.y; |
| 811 |
rc.bottom = sDevMode.dmPosition.y + sDevMode.dmPelsHeight; |
| 812 |
|
| 813 |
return rc; |
| 814 |
} |
| 815 |
|
| 816 |
void CWinSystemWin32::GetConnectedDisplays(std::vector<MONITOR_DETAILS>& outputs) |
| 817 |
{ |
| 818 |
using KODI::PLATFORM::WINDOWS::FromW; |
| 819 |
|
| 820 |
DISPLAY_DEVICEW ddAdapter = {}; |
| 821 |
ddAdapter.cb = sizeof(ddAdapter); |
| 822 |
|
| 823 |
for (DWORD adapter = 0; EnumDisplayDevicesW(nullptr, adapter, &ddAdapter, 0); ++adapter) |
| 824 |
{ |
| 825 |
// Exclude displays that are not part of the windows desktop. Using them is too different: no windows, |
| 826 |
// direct access with GDI CreateDC() or DirectDraw for example. So it may be possible to play video, but GUI? |
| 827 |
if ((ddAdapter.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) |
| 828 |
|| !(ddAdapter.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) |
| 829 |
continue; |
| 830 |
|
| 831 |
DISPLAY_DEVICEW ddMon = {}; |
| 832 |
ddMon.cb = sizeof(ddMon); |
| 833 |
bool foundScreen = false; |
| 834 |
|
| 835 |
DWORD screen = 0; |
| 836 |
// Just look for the first active output, we're actually only interested in the information at the adapter level. |
| 837 |
for (; EnumDisplayDevicesW(ddAdapter.DeviceName, screen, &ddMon, 0); ++screen) |
| 838 |
{ |
| 839 |
if (ddMon.StateFlags & (DISPLAY_DEVICE_ACTIVE | DISPLAY_DEVICE_ATTACHED)) |
| 840 |
{ |
| 841 |
foundScreen = true; |
| 842 |
break; |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
// Remoting returns no screens. Handle with a dummy screen. |
| 847 |
if (!foundScreen && screen == 0) |
| 848 |
{ |
| 849 |
lstrcpyW(ddMon.DeviceString, L"Dummy Monitor"); // safe: large static array |
| 850 |
foundScreen = true; |
| 851 |
} |
| 852 |
|
| 853 |
if (foundScreen) |
| 854 |
{ |
| 855 |
// get information about the display's current position and display mode |
| 856 |
DEVMODEW dm = {}; |
| 857 |
dm.dmSize = sizeof(dm); |
| 858 |
if (EnumDisplaySettingsExW(ddAdapter.DeviceName, ENUM_CURRENT_SETTINGS, &dm, 0) == FALSE) |
| 859 |
EnumDisplaySettingsExW(ddAdapter.DeviceName, ENUM_REGISTRY_SETTINGS, &dm, 0); |
| 860 |
|
| 861 |
POINT pt = { dm.dmPosition.x, dm.dmPosition.y }; |
| 862 |
HMONITOR hm = MonitorFromPoint(pt, MONITOR_DEFAULTTONULL); |
| 863 |
|
| 864 |
MONITOR_DETAILS md = {}; |
| 865 |
uint8_t num = 1; |
| 866 |
do |
| 867 |
{ |
| 868 |
// `Monitor #N` |
| 869 |
md.DeviceStringW = std::wstring(ddMon.DeviceString) + L" #" + std::to_wstring(num++); |
| 870 |
} while (std::any_of(outputs.begin(), outputs.end(), [&](MONITOR_DETAILS& m) { |
| 871 |
return m.DeviceStringW == md.DeviceStringW; |
| 872 |
})); |
| 873 |
|
| 874 |
std::wstring displayName = CWIN32Util::GetDisplayFriendlyName(ddAdapter.DeviceName); |
| 875 |
if (displayName.empty()) |
| 876 |
displayName = std::wstring(ddMon.DeviceString); |
| 877 |
num = 1; |
| 878 |
do |
| 879 |
{ |
| 880 |
// `Pretty Monitor Name #N` |
| 881 |
md.MonitorNameW = displayName + L" #" + std::to_wstring(num++); |
| 882 |
} while (std::any_of(outputs.begin(), outputs.end(), |
| 883 |
[&](MONITOR_DETAILS& m) { return m.MonitorNameW == md.MonitorNameW; })); |
| 884 |
|
| 885 |
md.CardNameW = ddAdapter.DeviceString; |
| 886 |
md.DeviceNameW = ddAdapter.DeviceName; |
| 887 |
md.ScreenWidth = dm.dmPelsWidth; |
| 888 |
md.ScreenHeight = dm.dmPelsHeight; |
| 889 |
md.hMonitor = hm; |
| 890 |
md.RefreshRate = dm.dmDisplayFrequency; |
| 891 |
md.Bpp = dm.dmBitsPerPel; |
| 892 |
md.Interlaced = (dm.dmDisplayFlags & DM_INTERLACED) ? true : false; |
| 893 |
|
| 894 |
MONITORINFO mi = {}; |
| 895 |
mi.cbSize = sizeof(mi); |
| 896 |
if (GetMonitorInfoW(hm, &mi) && (mi.dwFlags & MONITORINFOF_PRIMARY)) |
| 897 |
md.IsPrimary = true; |
| 898 |
|
| 899 |
outputs.push_back(md); |
| 900 |
} |
| 901 |
} |
| 902 |
} |
| 903 |
|
| 904 |
bool CWinSystemWin32::ChangeResolution(const RESOLUTION_INFO& res, bool forceChange /*= false*/) |
| 905 |
{ |
| 906 |
using KODI::PLATFORM::WINDOWS::ToW; |
| 907 |
std::wstring outputW = ToW(res.strOutput); |
| 908 |
|
| 909 |
DEVMODEW sDevMode = {}; |
| 910 |
sDevMode.dmSize = sizeof(sDevMode); |
| 911 |
|
| 912 |
// If we can't read the current resolution or any detail of the resolution is different than res |
| 913 |
if (!EnumDisplaySettingsW(outputW.c_str(), ENUM_CURRENT_SETTINGS, &sDevMode) || |
| 914 |
sDevMode.dmPelsWidth != res.iWidth || sDevMode.dmPelsHeight != res.iHeight || |
| 915 |
sDevMode.dmDisplayFrequency != static_cast<int>(res.fRefreshRate) || |
| 916 |
((sDevMode.dmDisplayFlags & DM_INTERLACED) && !(res.dwFlags & D3DPRESENTFLAG_INTERLACED)) || |
| 917 |
(!(sDevMode.dmDisplayFlags & DM_INTERLACED) && (res.dwFlags & D3DPRESENTFLAG_INTERLACED)) |
| 918 |
|| forceChange) |
| 919 |
{ |
| 920 |
ZeroMemory(&sDevMode, sizeof(sDevMode)); |
| 921 |
sDevMode.dmSize = sizeof(sDevMode); |
| 922 |
sDevMode.dmDriverExtra = 0; |
| 923 |
sDevMode.dmPelsWidth = res.iWidth; |
| 924 |
sDevMode.dmPelsHeight = res.iHeight; |
| 925 |
sDevMode.dmDisplayFrequency = static_cast<int>(res.fRefreshRate); |
| 926 |
sDevMode.dmDisplayFlags = (res.dwFlags & D3DPRESENTFLAG_INTERLACED) ? DM_INTERLACED : 0; |
| 927 |
sDevMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS; |
| 928 |
|
| 929 |
LONG rc; |
| 930 |
bool bResChanged = false; |
| 931 |
|
| 932 |
// Windows 8 refresh rate workaround for 24.0, 48.0 and 60.0 Hz |
| 933 |
if (res.fRefreshRate == 24.0 || res.fRefreshRate == 48.0 || res.fRefreshRate == 60.0) |
| 934 |
{ |
| 935 |
CLog::LogF(LOGDEBUG, "Using Windows 8+ workaround for refresh rate {} Hz", |
| 936 |
static_cast<int>(res.fRefreshRate)); |
| 937 |
|
| 938 |
// Get current resolution stored in registry |
| 939 |
DEVMODEW sDevModeRegistry = {}; |
| 940 |
sDevModeRegistry.dmSize = sizeof(sDevModeRegistry); |
| 941 |
if (EnumDisplaySettingsW(outputW.c_str(), ENUM_REGISTRY_SETTINGS, &sDevModeRegistry)) |
| 942 |
{ |
| 943 |
// Set requested mode in registry without actually changing resolution |
| 944 |
rc = ChangeDisplaySettingsExW(outputW.c_str(), &sDevMode, nullptr, CDS_UPDATEREGISTRY | CDS_NORESET, nullptr); |
| 945 |
if (rc == DISP_CHANGE_SUCCESSFUL) |
| 946 |
{ |
| 947 |
// Change resolution based on registry setting |
| 948 |
rc = ChangeDisplaySettingsExW(outputW.c_str(), nullptr, nullptr, CDS_FULLSCREEN, nullptr); |
| 949 |
if (rc == DISP_CHANGE_SUCCESSFUL) |
| 950 |
bResChanged = true; |
| 951 |
else |
| 952 |
CLog::LogF( |
| 953 |
LOGERROR, |
| 954 |
"ChangeDisplaySettingsEx (W8+ change resolution) failed with {}, using fallback", |
| 955 |
rc); |
| 956 |
|
| 957 |
// Restore registry with original values |
| 958 |
sDevModeRegistry.dmSize = sizeof(sDevModeRegistry); |
| 959 |
sDevModeRegistry.dmDriverExtra = 0; |
| 960 |
sDevModeRegistry.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY | DM_DISPLAYFLAGS; |
| 961 |
rc = ChangeDisplaySettingsExW(outputW.c_str(), &sDevModeRegistry, nullptr, CDS_UPDATEREGISTRY | CDS_NORESET, nullptr); |
| 962 |
if (rc != DISP_CHANGE_SUCCESSFUL) |
| 963 |
CLog::LogF(LOGERROR, "ChangeDisplaySettingsEx (W8+ restore registry) failed with {}", |
| 964 |
rc); |
| 965 |
} |
| 966 |
else |
| 967 |
CLog::LogF(LOGERROR, |
| 968 |
"ChangeDisplaySettingsEx (W8+ set registry) failed with {}, using fallback", |
| 969 |
rc); |
| 970 |
} |
| 971 |
else |
| 972 |
CLog::LogF(LOGERROR, "Unable to retrieve registry settings for Windows 8+ workaround, using fallback"); |
| 973 |
} |
| 974 |
|
| 975 |
// Standard resolution change/fallback for Windows 8+ workaround |
| 976 |
if (!bResChanged) |
| 977 |
{ |
| 978 |
// CDS_FULLSCREEN is for temporary fullscreen mode and prevents icons and windows from moving |
| 979 |
// to fit within the new dimensions of the desktop |
| 980 |
rc = ChangeDisplaySettingsExW(outputW.c_str(), &sDevMode, nullptr, CDS_FULLSCREEN, nullptr); |
| 981 |
if (rc == DISP_CHANGE_SUCCESSFUL) |
| 982 |
bResChanged = true; |
| 983 |
else |
| 984 |
CLog::LogF(LOGERROR, "ChangeDisplaySettingsEx failed with {}", rc); |
| 985 |
} |
| 986 |
|
| 987 |
if (bResChanged) |
| 988 |
ResolutionChanged(); |
| 989 |
|
| 990 |
return bResChanged; |
| 991 |
} |
| 992 |
|
| 993 |
// nothing to do, return success |
| 994 |
return true; |
| 995 |
} |
| 996 |
|
| 997 |
void CWinSystemWin32::UpdateResolutions() |
| 998 |
{ |
| 999 |
using KODI::PLATFORM::WINDOWS::FromW; |
| 1000 |
|
| 1001 |
m_displays.clear(); |
| 1002 |
|
| 1003 |
CWinSystemBase::UpdateResolutions(); |
| 1004 |
GetConnectedDisplays(m_displays); |
| 1005 |
|
| 1006 |
const auto settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 1007 |
std::string settingsMonitor = settings->GetString(CSettings::SETTING_VIDEOSCREEN_MONITOR); |
| 1008 |
MONITOR_DETAILS* details = GetDisplayDetails(settingsMonitor); |
| 1009 |
if (!details) |
| 1010 |
return; |
| 1011 |
// Migrate the setting to EDID-based name for connected and recognized screens. |
| 1012 |
// Other screens may be temporarily turned off, ignore so they're used once available again |
| 1013 |
if (settingsMonitor != FromW(details->MonitorNameW) && |
| 1014 |
(settingsMonitor == FromW(details->DeviceStringW) || |
| 1015 |
settingsMonitor == FromW(details->DeviceNameW))) |
| 1016 |
CDisplaySettings::GetInstance().SetMonitor(FromW(details->MonitorNameW)); |
| 1017 |
|
| 1018 |
float refreshRate; |
| 1019 |
int w = details->ScreenWidth; |
| 1020 |
int h = details->ScreenHeight; |
| 1021 |
if ((details->RefreshRate + 1) % 24 == 0 || (details->RefreshRate + 1) % 30 == 0) |
| 1022 |
refreshRate = static_cast<float>(details->RefreshRate + 1) / 1.001f; |
| 1023 |
else |
| 1024 |
refreshRate = static_cast<float>(details->RefreshRate); |
| 1025 |
|
| 1026 |
std::string strOutput = FromW(details->DeviceNameW); |
| 1027 |
std::string monitorName = FromW(details->MonitorNameW); |
| 1028 |
|
| 1029 |
uint32_t dwFlags = details->Interlaced ? D3DPRESENTFLAG_INTERLACED : 0; |
| 1030 |
|
| 1031 |
RESOLUTION_INFO& info = CDisplaySettings::GetInstance().GetResolutionInfo(RES_DESKTOP); |
| 1032 |
UpdateDesktopResolution(info, monitorName, w, h, refreshRate, dwFlags); |
| 1033 |
info.strOutput = strOutput; |
| 1034 |
|
| 1035 |
CLog::Log(LOGINFO, "Primary mode: {}", info.strMode); |
| 1036 |
|
| 1037 |
// erase previous stored modes |
| 1038 |
CDisplaySettings::GetInstance().ClearCustomResolutions(); |
| 1039 |
|
| 1040 |
for(int mode = 0;; mode++) |
| 1041 |
{ |
| 1042 |
DEVMODEW devmode = {}; |
| 1043 |
devmode.dmSize = sizeof(devmode); |
| 1044 |
if(EnumDisplaySettingsW(details->DeviceNameW.c_str(), mode, &devmode) == 0) |
| 1045 |
break; |
| 1046 |
if(devmode.dmBitsPerPel != 32) |
| 1047 |
continue; |
| 1048 |
|
| 1049 |
float refresh; |
| 1050 |
if ((devmode.dmDisplayFrequency + 1) % 24 == 0 || (devmode.dmDisplayFrequency + 1) % 30 == 0) |
| 1051 |
refresh = static_cast<float>(devmode.dmDisplayFrequency + 1) / 1.001f; |
| 1052 |
else |
| 1053 |
refresh = static_cast<float>(devmode.dmDisplayFrequency); |
| 1054 |
dwFlags = (devmode.dmDisplayFlags & DM_INTERLACED) ? D3DPRESENTFLAG_INTERLACED : 0; |
| 1055 |
|
| 1056 |
RESOLUTION_INFO res; |
| 1057 |
res.iWidth = devmode.dmPelsWidth; |
| 1058 |
res.iHeight = devmode.dmPelsHeight; |
| 1059 |
res.bFullScreen = true; |
| 1060 |
res.dwFlags = dwFlags; |
| 1061 |
res.fRefreshRate = refresh; |
| 1062 |
res.fPixelRatio = 1.0f; |
| 1063 |
res.iScreenWidth = res.iWidth; |
| 1064 |
res.iScreenHeight = res.iHeight; |
| 1065 |
res.iSubtitles = res.iHeight; |
| 1066 |
res.strMode = StringUtils::Format("{}: {}x{} @ {:.2f}Hz", monitorName, res.iWidth, res.iHeight, |
| 1067 |
res.fRefreshRate); |
| 1068 |
GetGfxContext().ResetOverscan(res); |
| 1069 |
res.strOutput = strOutput; |
| 1070 |
|
| 1071 |
if (AddResolution(res)) |
| 1072 |
CLog::Log(LOGINFO, "Additional mode: {}", res.strMode); |
| 1073 |
} |
| 1074 |
|
| 1075 |
CDisplaySettings::GetInstance().ApplyCalibrations(); |
| 1076 |
} |
| 1077 |
|
| 1078 |
bool CWinSystemWin32::AddResolution(const RESOLUTION_INFO &res) |
| 1079 |
{ |
| 1080 |
for (unsigned int i = RES_CUSTOM; i < CDisplaySettings::GetInstance().ResolutionInfoSize(); i++) |
| 1081 |
{ |
| 1082 |
RESOLUTION_INFO& info = CDisplaySettings::GetInstance().GetResolutionInfo(i); |
| 1083 |
if (info.iWidth == res.iWidth |
| 1084 |
&& info.iHeight == res.iHeight |
| 1085 |
&& info.iScreenWidth == res.iScreenWidth |
| 1086 |
&& info.iScreenHeight == res.iScreenHeight |
| 1087 |
&& info.fRefreshRate == res.fRefreshRate |
| 1088 |
&& info.dwFlags == res.dwFlags) |
| 1089 |
return false; // already have this resolution |
| 1090 |
} |
| 1091 |
|
| 1092 |
CDisplaySettings::GetInstance().AddResolutionInfo(res); |
| 1093 |
return true; |
| 1094 |
} |
| 1095 |
|
| 1096 |
void CWinSystemWin32::ShowOSMouse(bool show) |
| 1097 |
{ |
| 1098 |
static int counter = 0; |
| 1099 |
if ((counter < 0 && show) || (counter >= 0 && !show)) |
| 1100 |
counter = ShowCursor(show); |
| 1101 |
} |
| 1102 |
|
| 1103 |
bool CWinSystemWin32::Minimize() |
| 1104 |
{ |
| 1105 |
ShowWindow(m_hWnd, SW_MINIMIZE); |
| 1106 |
return true; |
| 1107 |
} |
| 1108 |
bool CWinSystemWin32::Restore() |
| 1109 |
{ |
| 1110 |
ShowWindow(m_hWnd, SW_RESTORE); |
| 1111 |
return true; |
| 1112 |
} |
| 1113 |
bool CWinSystemWin32::Hide() |
| 1114 |
{ |
| 1115 |
ShowWindow(m_hWnd, SW_HIDE); |
| 1116 |
return true; |
| 1117 |
} |
| 1118 |
bool CWinSystemWin32::Show(bool raise) |
| 1119 |
{ |
| 1120 |
HWND windowAfter = HWND_BOTTOM; |
| 1121 |
if (raise) |
| 1122 |
{ |
| 1123 |
if (m_bFullScreen) |
| 1124 |
windowAfter = HWND_TOP; |
| 1125 |
else |
| 1126 |
windowAfter = CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST; |
| 1127 |
} |
| 1128 |
|
| 1129 |
SetWindowPos(m_hWnd, windowAfter, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_ASYNCWINDOWPOS); |
| 1130 |
UpdateWindow(m_hWnd); |
| 1131 |
|
| 1132 |
if (raise) |
| 1133 |
{ |
| 1134 |
SetForegroundWindow(m_hWnd); |
| 1135 |
SetFocus(m_hWnd); |
| 1136 |
} |
| 1137 |
return true; |
| 1138 |
} |
| 1139 |
|
| 1140 |
void CWinSystemWin32::Register(IDispResource *resource) |
| 1141 |
{ |
| 1142 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 1143 |
m_resources.push_back(resource); |
| 1144 |
} |
| 1145 |
|
| 1146 |
void CWinSystemWin32::Unregister(IDispResource* resource) |
| 1147 |
{ |
| 1148 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 1149 |
std::vector<IDispResource*>::iterator i = find(m_resources.begin(), m_resources.end(), resource); |
| 1150 |
if (i != m_resources.end()) |
| 1151 |
m_resources.erase(i); |
| 1152 |
} |
| 1153 |
|
| 1154 |
void CWinSystemWin32::OnDisplayLost() |
| 1155 |
{ |
| 1156 |
CLog::LogF(LOGDEBUG, "notify display lost event"); |
| 1157 |
|
| 1158 |
{ |
| 1159 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 1160 |
for (std::vector<IDispResource *>::iterator i = m_resources.begin(); i != m_resources.end(); ++i) |
| 1161 |
(*i)->OnLostDisplay(); |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
void CWinSystemWin32::OnDisplayReset() |
| 1166 |
{ |
| 1167 |
if (!m_delayDispReset) |
| 1168 |
{ |
| 1169 |
CLog::LogF(LOGDEBUG, "notify display reset event"); |
| 1170 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 1171 |
for (std::vector<IDispResource *>::iterator i = m_resources.begin(); i != m_resources.end(); ++i) |
| 1172 |
(*i)->OnResetDisplay(); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
void CWinSystemWin32::OnDisplayBack() |
| 1177 |
{ |
| 1178 |
auto delay = |
| 1179 |
std::chrono::milliseconds(CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt( |
| 1180 |
"videoscreen.delayrefreshchange") * |
| 1181 |
100); |
| 1182 |
if (delay > 0ms) |
| 1183 |
{ |
| 1184 |
m_delayDispReset = true; |
| 1185 |
m_dispResetTimer.Set(delay); |
| 1186 |
} |
| 1187 |
OnDisplayReset(); |
| 1188 |
} |
| 1189 |
|
| 1190 |
void CWinSystemWin32::ResolutionChanged() |
| 1191 |
{ |
| 1192 |
OnDisplayLost(); |
| 1193 |
OnDisplayBack(); |
| 1194 |
} |
| 1195 |
|
| 1196 |
void CWinSystemWin32::SetForegroundWindowInternal(HWND hWnd) |
| 1197 |
{ |
| 1198 |
if (!IsWindow(hWnd)) return; |
| 1199 |
|
| 1200 |
// if the window isn't focused, bring it to front or SetFullScreen will fail |
| 1201 |
BYTE keyState[256] = {}; |
| 1202 |
// to unlock SetForegroundWindow we need to imitate Alt pressing |
| 1203 |
if (GetKeyboardState(reinterpret_cast<LPBYTE>(&keyState)) && !(keyState[VK_MENU] & 0x80)) |
| 1204 |
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | 0, 0); |
| 1205 |
|
| 1206 |
BOOL res = SetForegroundWindow(hWnd); |
| 1207 |
|
| 1208 |
if (GetKeyboardState(reinterpret_cast<LPBYTE>(&keyState)) && !(keyState[VK_MENU] & 0x80)) |
| 1209 |
keybd_event(VK_MENU, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); |
| 1210 |
|
| 1211 |
if (!res) |
| 1212 |
{ |
| 1213 |
//relation time of SetForegroundWindow lock |
| 1214 |
DWORD lockTimeOut = 0; |
| 1215 |
HWND hCurrWnd = GetForegroundWindow(); |
| 1216 |
DWORD dwThisTID = GetCurrentThreadId(), |
| 1217 |
dwCurrTID = GetWindowThreadProcessId(hCurrWnd, nullptr); |
| 1218 |
|
| 1219 |
// we need to bypass some limitations from Microsoft |
| 1220 |
if (dwThisTID != dwCurrTID) |
| 1221 |
{ |
| 1222 |
AttachThreadInput(dwThisTID, dwCurrTID, TRUE); |
| 1223 |
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &lockTimeOut, 0); |
| 1224 |
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, nullptr, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE); |
| 1225 |
AllowSetForegroundWindow(ASFW_ANY); |
| 1226 |
} |
| 1227 |
|
| 1228 |
SetForegroundWindow(hWnd); |
| 1229 |
|
| 1230 |
if (dwThisTID != dwCurrTID) |
| 1231 |
{ |
| 1232 |
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, &lockTimeOut, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE); |
| 1233 |
AttachThreadInput(dwThisTID, dwCurrTID, FALSE); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
std::unique_ptr<CVideoSync> CWinSystemWin32::GetVideoSync(CVideoReferenceClock* clock) |
| 1239 |
{ |
| 1240 |
std::unique_ptr<CVideoSync> pVSync(new CVideoSyncD3D(clock)); |
| 1241 |
return pVSync; |
| 1242 |
} |
| 1243 |
|
| 1244 |
std::string CWinSystemWin32::GetClipboardText() |
| 1245 |
{ |
| 1246 |
std::wstring unicode_text; |
| 1247 |
std::string utf8_text; |
| 1248 |
|
| 1249 |
if (OpenClipboard(nullptr)) |
| 1250 |
{ |
| 1251 |
HGLOBAL hglb = GetClipboardData(CF_UNICODETEXT); |
| 1252 |
if (hglb != nullptr) |
| 1253 |
{ |
| 1254 |
LPWSTR lpwstr = static_cast<LPWSTR>(GlobalLock(hglb)); |
| 1255 |
if (lpwstr != nullptr) |
| 1256 |
{ |
| 1257 |
unicode_text = lpwstr; |
| 1258 |
GlobalUnlock(hglb); |
| 1259 |
} |
| 1260 |
} |
| 1261 |
CloseClipboard(); |
| 1262 |
} |
| 1263 |
|
| 1264 |
return KODI::PLATFORM::WINDOWS::FromW(unicode_text); |
| 1265 |
} |
| 1266 |
|
| 1267 |
bool CWinSystemWin32::UseLimitedColor() |
| 1268 |
{ |
| 1269 |
return CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_VIDEOSCREEN_LIMITEDRANGE); |
| 1270 |
} |
| 1271 |
|
| 1272 |
void CWinSystemWin32::NotifyAppFocusChange(bool bGaining) |
| 1273 |
{ |
| 1274 |
if (m_state == WINDOW_STATE_FULLSCREEN && !m_IsAlteringWindow) |
| 1275 |
{ |
| 1276 |
m_IsAlteringWindow = true; |
| 1277 |
ReleaseBackBuffer(); |
| 1278 |
|
| 1279 |
if (bGaining) |
| 1280 |
SetWindowPos(m_hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW); |
| 1281 |
|
| 1282 |
RESOLUTION_INFO res = {}; |
| 1283 |
const RESOLUTION resolution = CServiceBroker::GetWinSystem()->GetGfxContext().GetVideoResolution(); |
| 1284 |
if (bGaining && resolution > RES_INVALID) |
| 1285 |
res = CDisplaySettings::GetInstance().GetResolutionInfo(resolution); |
| 1286 |
|
| 1287 |
SetDeviceFullScreen(bGaining, res); |
| 1288 |
|
| 1289 |
if (!bGaining) |
| 1290 |
SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOREDRAW); |
| 1291 |
|
| 1292 |
CreateBackBuffer(); |
| 1293 |
m_IsAlteringWindow = false; |
| 1294 |
} |
| 1295 |
m_inFocus = bGaining; |
| 1296 |
} |
| 1297 |
|
| 1298 |
void CWinSystemWin32::UpdateStates(bool fullScreen) |
| 1299 |
{ |
| 1300 |
m_fullscreenState = CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_VIDEOSCREEN_FAKEFULLSCREEN) |
| 1301 |
? WINDOW_FULLSCREEN_STATE_FULLSCREEN_WINDOW |
| 1302 |
: WINDOW_FULLSCREEN_STATE_FULLSCREEN; |
| 1303 |
m_windowState = WINDOW_WINDOW_STATE_WINDOWED; // currently only this allowed |
| 1304 |
|
| 1305 |
// set the appropriate window style |
| 1306 |
if (fullScreen) |
| 1307 |
{ |
| 1308 |
m_windowStyle = FULLSCREEN_WINDOW_STYLE; |
| 1309 |
m_windowExStyle = FULLSCREEN_WINDOW_EX_STYLE; |
| 1310 |
} |
| 1311 |
else |
| 1312 |
{ |
| 1313 |
m_windowStyle = WINDOWED_STYLE; |
| 1314 |
m_windowExStyle = WINDOWED_EX_STYLE; |
| 1315 |
} |
| 1316 |
} |
| 1317 |
|
| 1318 |
WINDOW_STATE CWinSystemWin32::GetState(bool fullScreen) const |
| 1319 |
{ |
| 1320 |
return static_cast<WINDOW_STATE>(fullScreen ? m_fullscreenState : m_windowState); |
| 1321 |
} |
| 1322 |
|
| 1323 |
bool CWinSystemWin32::MessagePump() |
| 1324 |
{ |
| 1325 |
return m_winEvents->MessagePump(); |
| 1326 |
} |
| 1327 |
|
| 1328 |
void CWinSystemWin32::SetTogglingHDR(bool toggling) |
| 1329 |
{ |
| 1330 |
if (toggling) |
| 1331 |
SetTimer(m_hWnd, ID_TIMER_HDR, 6000U, nullptr); |
| 1332 |
|
| 1333 |
m_IsTogglingHDR = toggling; |
| 1334 |
} |
| 1335 |
|
| 1336 |
RECT CWinSystemWin32::GetVirtualScreenRect() |
| 1337 |
{ |
| 1338 |
RECT rect = {}; |
| 1339 |
rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN); |
| 1340 |
rect.right = GetSystemMetrics(SM_CXVIRTUALSCREEN) + rect.left; |
| 1341 |
rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN); |
| 1342 |
rect.bottom = GetSystemMetrics(SM_CYVIRTUALSCREEN) + rect.top; |
| 1343 |
|
| 1344 |
return rect; |
| 1345 |
} |
| 1346 |
|
| 1347 |
/*! |
| 1348 |
* \brief Max luminance for GUI SDR content in HDR mode. |
| 1349 |
* \return Max luminance in nits, lower than 10000. |
| 1350 |
*/ |
| 1351 |
float CWinSystemWin32::GetGuiSdrPeakLuminance() const |
| 1352 |
{ |
| 1353 |
const auto settings = CServiceBroker::GetSettingsComponent()->GetSettings(); |
| 1354 |
|
| 1355 |
// use cached system value as this is called for each frame |
| 1356 |
if (settings->GetBool(CSettings::SETTING_VIDEOSCREEN_USESYSTEMSDRPEAKLUMINANCE) && |
| 1357 |
m_validSystemSdrPeakLuminance) |
| 1358 |
return m_systemSdrPeakLuminance; |
| 1359 |
|
| 1360 |
// Max nits for 100% UI setting = 1000 nits, < 10000 nits, min 80 nits for 0% |
| 1361 |
const int guiSdrPeak = settings->GetInt(CSettings::SETTING_VIDEOSCREEN_GUISDRPEAKLUMINANCE); |
| 1362 |
return (80.0f * std::pow(std::exp(1.0f), 0.025257f * guiSdrPeak)); |
| 1363 |
} |
| 1364 |
|
| 1365 |
/*! |
| 1366 |
* \brief Test support of the OS for a SDR max luminance in HDR mode setting |
| 1367 |
* \return true when the OS supports that setting, false otherwise |
| 1368 |
*/ |
| 1369 |
bool CWinSystemWin32::HasSystemSdrPeakLuminance() |
| 1370 |
{ |
| 1371 |
MONITOR_DETAILS* md = GetDisplayDetails(m_hMonitor); |
| 1372 |
if (md) |
| 1373 |
return CWIN32Util::GetSystemSdrWhiteLevel(md->DeviceNameW, nullptr); |
| 1374 |
|
| 1375 |
return false; |
| 1376 |
} |
| 1377 |
|
| 1378 |
/*! |
| 1379 |
* \brief Cache the system HDR/SDR balance for use during rendering, instead of querying the API |
| 1380 |
for each frame. |
| 1381 |
*/ |
| 1382 |
void CWinSystemWin32::CacheSystemSdrPeakLuminance() |
| 1383 |
{ |
| 1384 |
m_validSystemSdrPeakLuminance = false; |
| 1385 |
|
| 1386 |
MONITOR_DETAILS* md = GetDisplayDetails(m_hMonitor); |
| 1387 |
if (md) |
| 1388 |
{ |
| 1389 |
m_validSystemSdrPeakLuminance = |
| 1390 |
CWIN32Util::GetSystemSdrWhiteLevel(md->DeviceNameW, &m_systemSdrPeakLuminance); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
|
| 1394 |
RECT CWinSystemWin32::GetScreenWorkArea(HMONITOR handle) const |
| 1395 |
{ |
| 1396 |
MONITORINFO monitorInfo{}; |
| 1397 |
monitorInfo.cbSize = sizeof(MONITORINFO); |
| 1398 |
if (!GetMonitorInfoW(handle, &monitorInfo)) |
| 1399 |
{ |
| 1400 |
CLog::LogF(LOGERROR, "GetMonitorInfoW failed with {}", GetLastError()); |
| 1401 |
return RECT(); |
| 1402 |
} |
| 1403 |
return monitorInfo.rcWork; |
| 1404 |
} |
| 1405 |
|
| 1406 |
RECT CWinSystemWin32::GetNcAreaOffsets(DWORD dwStyle, BOOL bMenu, DWORD dwExStyle) const |
| 1407 |
{ |
| 1408 |
RECT rcNcArea{}; |
| 1409 |
SetRectEmpty(&rcNcArea); |
| 1410 |
|
| 1411 |
if (!AdjustWindowRectEx(&rcNcArea, dwStyle, false, dwExStyle)) |
| 1412 |
{ |
| 1413 |
CLog::LogF(LOGERROR, "AdjustWindowRectEx failed with {}", GetLastError()); |
| 1414 |
return RECT(); |
| 1415 |
} |
| 1416 |
return rcNcArea; |
| 1417 |
} |