| 1 |
/* |
| 2 |
* Copyright (C) 2009-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 "X11DPMSSupport.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "utils/log.h" |
| 13 |
#include "windowing/X11/WinSystemX11.h" |
| 14 |
|
| 15 |
#include <X11/Xlib.h> |
| 16 |
#include <X11/extensions/dpms.h> |
| 17 |
|
| 18 |
using namespace KODI::WINDOWING::X11; |
| 19 |
|
| 20 |
namespace |
| 21 |
{ |
| 22 |
// Mapping of PowerSavingMode to X11's mode constants. |
| 23 |
const CARD16 X_DPMS_MODES[] = |
| 24 |
{ |
| 25 |
DPMSModeStandby, |
| 26 |
DPMSModeSuspend, |
| 27 |
DPMSModeOff |
| 28 |
}; |
| 29 |
} |
| 30 |
|
| 31 |
CX11DPMSSupport::CX11DPMSSupport() |
| 32 |
{ |
| 33 |
CWinSystemX11* winSystem = dynamic_cast<CWinSystemX11*>(CServiceBroker::GetWinSystem()); |
| 34 |
if (!winSystem) |
| 35 |
return; |
| 36 |
|
| 37 |
Display* dpy = winSystem->GetDisplay(); |
| 38 |
if (!dpy) |
| 39 |
return; |
| 40 |
|
| 41 |
int event_base, error_base; // we ignore these |
| 42 |
if (!DPMSQueryExtension(dpy, &event_base, &error_base)) |
| 43 |
{ |
| 44 |
CLog::Log(LOGINFO, "DPMS: X11 extension not present, power-saving will not be available"); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if (!DPMSCapable(dpy)) |
| 49 |
{ |
| 50 |
CLog::Log(LOGINFO, "DPMS: display does not support power-saving"); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
m_supportedModes.push_back(SUSPEND); // best compromise |
| 55 |
m_supportedModes.push_back(OFF); // next best |
| 56 |
m_supportedModes.push_back(STANDBY); // rather lame, < 80% power according to DPMS spec |
| 57 |
} |
| 58 |
|
| 59 |
bool CX11DPMSSupport::EnablePowerSaving(PowerSavingMode mode) |
| 60 |
{ |
| 61 |
CWinSystemX11* winSystem = dynamic_cast<CWinSystemX11*>(CServiceBroker::GetWinSystem()); |
| 62 |
if (!winSystem) |
| 63 |
return false; |
| 64 |
|
| 65 |
Display* dpy = winSystem->GetDisplay(); |
| 66 |
if (!dpy) |
| 67 |
return false; |
| 68 |
|
| 69 |
// This is not needed on my ATI Radeon, but the docs say that DPMSForceLevel |
| 70 |
// after a DPMSDisable (from SDL) should not normally work. |
| 71 |
DPMSEnable(dpy); |
| 72 |
DPMSForceLevel(dpy, X_DPMS_MODES[mode]); |
| 73 |
// There shouldn't be any errors if we called DPMSEnable; if they do happen, |
| 74 |
// they're asynchronous and messy to detect. |
| 75 |
XFlush(dpy); |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
bool CX11DPMSSupport::DisablePowerSaving() |
| 80 |
{ |
| 81 |
CWinSystemX11* winSystem = dynamic_cast<CWinSystemX11*>(CServiceBroker::GetWinSystem()); |
| 82 |
if (!winSystem) |
| 83 |
return false; |
| 84 |
|
| 85 |
Display* dpy = winSystem->GetDisplay(); |
| 86 |
if (!dpy) |
| 87 |
return false; |
| 88 |
|
| 89 |
DPMSForceLevel(dpy, DPMSModeOn); |
| 90 |
DPMSDisable(dpy); |
| 91 |
XFlush(dpy); |
| 92 |
|
| 93 |
winSystem->RecreateWindow(); |
| 94 |
|
| 95 |
return true; |
| 96 |
} |