x11: Wrap platform functions in XWindowsScreenSaver class

This commit is contained in:
Monika Kairaityte 2018-11-09 17:33:35 +02:00 committed by Adrian Lucrèce Céleste
parent be45440e84
commit ce3e8243a9
6 changed files with 118 additions and 49 deletions

View File

@ -186,6 +186,15 @@ public:
virtual int XRefreshKeyboardMapping(XMappingEvent* event_map) = 0;
virtual int XISelectEvents(Display* display, Window w, XIEventMask* masks,
int num_masks) = 0;
virtual Atom XInternAtom(Display* display, _Xconst char* atom_name,
Bool only_if_exists) = 0;
virtual int XGetScreenSaver(Display* display, int* timeout_return,
int* interval_return, int* prefer_blanking_return,
int* allow_exposures_return) = 0;
virtual int XSetScreenSaver(Display* display, int timeout, int interval,
int prefer_blanking, int allow_exposures) = 0;
virtual int XForceScreenSaver(Display* display, int mode) = 0;
virtual int XFree(void* data) = 0;
virtual Status DPMSEnable(Display* display) = 0;
virtual Status DPMSDisable(Display* display) = 0;
};

View File

@ -487,3 +487,44 @@ int XWindowsImpl::XISelectEvents(Display* display, Window w, XIEventMask* masks,
return ::XISelectEvents(display, w, masks, num_masks);
}
Atom XWindowsImpl::XInternAtom(Display* display, _Xconst char* atom_name,
Bool only_if_exists)
{
return ::XInternAtom(display, atom_name, only_if_exists);
}
int XWindowsImpl::XGetScreenSaver(Display* display, int* timeout_return,
int* interval_return,
int* prefer_blanking_return,
int* allow_exposures_return)
{
return ::XGetScreenSaver(display, timeout_return, interval_return,
prefer_blanking_return, allow_exposures_return);
}
int XWindowsImpl::XSetScreenSaver(Display* display, int timeout, int interval,
int prefer_blanking, int allow_exposures)
{
return ::XSetScreenSaver(display, timeout, interval, prefer_blanking,
allow_exposures);
}
int XWindowsImpl::XForceScreenSaver(Display* display, int mode)
{
return ::XForceScreenSaver(display, mode);
}
int XWindowsImpl::XFree(void* data)
{
return ::XFree(data);
}
Status XWindowsImpl::DPMSEnable(Display* display)
{
return ::DPMSEnable(display);
}
Status XWindowsImpl::DPMSDisable(Display* display)
{
return ::DPMSDisable(display);
}

View File

@ -148,5 +148,15 @@ public:
virtual int XRefreshKeyboardMapping(XMappingEvent* event_map);
virtual int XISelectEvents(Display* display, Window w, XIEventMask* masks,
int num_masks);
virtual Atom XInternAtom(Display* display, _Xconst char* atom_name,
Bool only_if_exists);
virtual int XGetScreenSaver(Display* display, int* timeout_return,
int* interval_return, int* prefer_blanking_return,
int* allow_exposures_return);
virtual int XSetScreenSaver(Display* display, int timeout, int interval,
int prefer_blanking, int allow_exposures);
virtual int XForceScreenSaver(Display* display, int mode);
virtual int XFree(void* data);
virtual Status DPMSEnable(Display* display);
virtual Status DPMSDisable(Display* display);
};

View File

@ -114,7 +114,7 @@ XWindowsScreen::XWindowsScreen(
m_root = m_impl->do_DefaultRootWindow(m_display);
saveShape();
m_window = openWindow();
m_screensaver = new XWindowsScreenSaver(m_display,
m_screensaver = new XWindowsScreenSaver(m_impl, m_display,
m_window, getEventTarget(), events);
m_keyState = new XWindowsKeyState(m_display, m_xkb, events, m_keyMap);
LOG((CLOG_DEBUG "screen shape: %d,%d %dx%d %s", m_x, m_y, m_w, m_h, m_xinerama ? "(xinerama)" : ""));

View File

@ -58,8 +58,9 @@ extern Status DPMSInfo(Display *, CARD16 *, BOOL *);
// XWindowsScreenSaver
//
XWindowsScreenSaver::XWindowsScreenSaver(
Display* display, Window window, void* eventTarget, IEventQueue* events) :
XWindowsScreenSaver::XWindowsScreenSaver(IXWindowsImpl* impl, Display* display,
Window window, void* eventTarget,
IEventQueue* events) :
m_display(display),
m_xscreensaverSink(window),
m_eventTarget(eventTarget),
@ -72,22 +73,23 @@ XWindowsScreenSaver::XWindowsScreenSaver(
m_disablePos(0),
m_events(events)
{
m_impl = impl;
// get atoms
m_atomScreenSaver = XInternAtom(m_display,
"SCREENSAVER", False);
m_atomScreenSaverVersion = XInternAtom(m_display,
"_SCREENSAVER_VERSION", False);
m_atomScreenSaverActivate = XInternAtom(m_display,
"ACTIVATE", False);
m_atomScreenSaverDeactivate = XInternAtom(m_display,
"DEACTIVATE", False);
m_atomScreenSaver = m_impl->XInternAtom(m_display, "SCREENSAVER", False);
m_atomScreenSaverVersion = m_impl->XInternAtom(m_display,
"_SCREENSAVER_VERSION",
False);
m_atomScreenSaverActivate = m_impl->XInternAtom(m_display, "ACTIVATE",
False);
m_atomScreenSaverDeactivate = m_impl->XInternAtom(m_display, "DEACTIVATE",
False);
// check for DPMS extension. this is an alternative screen saver
// that powers down the display.
#if HAVE_X11_EXTENSIONS_DPMS_H
int eventBase, errorBase;
if (DPMSQueryExtension(m_display, &eventBase, &errorBase)) {
if (DPMSCapable(m_display)) {
if (m_impl->DPMSQueryExtension(m_display, &eventBase, &errorBase)) {
if (m_impl->DPMSCapable(m_display)) {
// we have DPMS
m_dpms = true;
}
@ -98,11 +100,12 @@ XWindowsScreenSaver::XWindowsScreenSaver(
bool error = false;
{
XWindowsUtil::ErrorLock lock(m_display, &error);
Window root = DefaultRootWindow(m_display);
Window root = m_impl->do_DefaultRootWindow(m_display);
XWindowAttributes attr;
XGetWindowAttributes(m_display, root, &attr);
m_impl->XGetWindowAttributes(m_display, root, &attr);
m_rootEventMask = attr.your_event_mask;
XSelectInput(m_display, root, m_rootEventMask | SubstructureNotifyMask);
m_impl->XSelectInput(m_display, root,
m_rootEventMask | SubstructureNotifyMask);
}
if (error) {
LOG((CLOG_DEBUG "didn't set root event mask"));
@ -110,8 +113,8 @@ XWindowsScreenSaver::XWindowsScreenSaver(
}
// get the built-in settings
XGetScreenSaver(m_display, &m_timeout, &m_interval,
&m_preferBlanking, &m_allowExposures);
m_impl->XGetScreenSaver(m_display, &m_timeout, &m_interval,
&m_preferBlanking, &m_allowExposures);
// get the DPMS settings
m_dpmsEnabled = isDPMSEnabled();
@ -137,11 +140,12 @@ XWindowsScreenSaver::~XWindowsScreenSaver()
if (m_display != NULL) {
enableDPMS(m_dpmsEnabled);
XSetScreenSaver(m_display, m_timeout, m_interval,
m_preferBlanking, m_allowExposures);
m_impl->XSetScreenSaver(m_display, m_timeout, m_interval,
m_preferBlanking, m_allowExposures);
clearWatchForXScreenSaver();
XWindowsUtil::ErrorLock lock(m_display);
XSelectInput(m_display, DefaultRootWindow(m_display), m_rootEventMask);
m_impl->XSelectInput(m_display, DefaultRootWindow(m_display),
m_rootEventMask);
}
}
@ -221,8 +225,8 @@ XWindowsScreenSaver::enable()
updateDisableTimer();
// for built-in X screen saver
XSetScreenSaver(m_display, m_timeout, m_interval,
m_preferBlanking, m_allowExposures);
m_impl->XSetScreenSaver(m_display, m_timeout, m_interval, m_preferBlanking,
m_allowExposures);
// for DPMS
enableDPMS(m_dpmsEnabled);
@ -236,10 +240,10 @@ XWindowsScreenSaver::disable()
updateDisableTimer();
// use built-in X screen saver
XGetScreenSaver(m_display, &m_timeout, &m_interval,
&m_preferBlanking, &m_allowExposures);
XSetScreenSaver(m_display, 0, m_interval,
m_preferBlanking, m_allowExposures);
m_impl->XGetScreenSaver(m_display, &m_timeout, &m_interval,
&m_preferBlanking, &m_allowExposures);
m_impl->XSetScreenSaver(m_display, 0, m_interval, m_preferBlanking,
m_allowExposures);
// for DPMS
m_dpmsEnabled = isDPMSEnabled();
@ -267,7 +271,7 @@ XWindowsScreenSaver::activate()
// try built-in X screen saver
if (m_timeout != 0) {
XForceScreenSaver(m_display, ScreenSaverActive);
m_impl->XForceScreenSaver(m_display, ScreenSaverActive);
}
// try DPMS
@ -297,7 +301,7 @@ XWindowsScreenSaver::deactivate()
}
// use built-in X screen saver
XForceScreenSaver(m_display, ScreenSaverReset);
m_impl->XForceScreenSaver(m_display, ScreenSaverReset);
}
bool
@ -326,14 +330,14 @@ XWindowsScreenSaver::findXScreenSaver()
Window root = DefaultRootWindow(m_display);
Window rw, pw, *cw;
unsigned int nc;
if (XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
if (m_impl->XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
for (unsigned int i = 0; i < nc; ++i) {
if (isXScreenSaver(cw[i])) {
setXScreenSaver(cw[i]);
break;
}
}
XFree(cw);
m_impl->XFree(cw);
}
}
@ -357,7 +361,7 @@ XWindowsScreenSaver::setXScreenSaver(Window window)
XWindowAttributes attr;
{
XWindowsUtil::ErrorLock lock(m_display, &error);
XGetWindowAttributes(m_display, m_xscreensaver, &attr);
m_impl->XGetWindowAttributes(m_display, m_xscreensaver, &attr);
}
setXScreenSaverActive(!error && attr.map_state != IsUnmapped);
@ -430,7 +434,7 @@ XWindowsScreenSaver::sendXScreenSaverCommand(Atom cmd, long arg1, long arg2)
bool error = false;
{
XWindowsUtil::ErrorLock lock(m_display, &error);
XSendEvent(m_display, m_xscreensaver, False, 0, &event);
m_impl->XSendEvent(m_display, m_xscreensaver, False, 0, &event);
}
if (error) {
findXScreenSaver();
@ -447,11 +451,11 @@ XWindowsScreenSaver::watchForXScreenSaver()
Window root = DefaultRootWindow(m_display);
Window rw, pw, *cw;
unsigned int nc;
if (XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
if (m_impl->XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
for (unsigned int i = 0; i < nc; ++i) {
addWatchXScreenSaver(cw[i]);
}
XFree(cw);
m_impl->XFree(cw);
}
// now check for xscreensaver window in case it set the property
@ -469,7 +473,7 @@ XWindowsScreenSaver::clearWatchForXScreenSaver()
XWindowsUtil::ErrorLock lock(m_display);
for (WatchList::iterator index = m_watchWindows.begin();
index != m_watchWindows.end(); ++index) {
XSelectInput(m_display, index->first, index->second);
m_impl->XSelectInput(m_display, index->first, index->second);
}
m_watchWindows.clear();
}
@ -482,7 +486,7 @@ XWindowsScreenSaver::addWatchXScreenSaver(Window window)
XWindowAttributes attr;
{
XWindowsUtil::ErrorLock lock(m_display, &error);
XGetWindowAttributes(m_display, window, &attr);
m_impl->XGetWindowAttributes(m_display, window, &attr);
}
// if successful and window uses override_redirect (like xscreensaver
@ -491,8 +495,8 @@ XWindowsScreenSaver::addWatchXScreenSaver(Window window)
error = false;
{
XWindowsUtil::ErrorLock lock(m_display, &error);
XSelectInput(m_display, window,
attr.your_event_mask | PropertyChangeMask);
m_impl->XSelectInput(m_display, window,
attr.your_event_mask | PropertyChangeMask);
}
if (!error) {
// if successful then add the window to our list
@ -535,7 +539,7 @@ XWindowsScreenSaver::handleDisableTimer(const Event&, void*)
event.xmotion.same_screen = True;
XWindowsUtil::ErrorLock lock(m_display);
XSendEvent(m_display, m_xscreensaver, False, 0, &event);
m_impl->XSendEvent(m_display, m_xscreensaver, False, 0, &event);
m_disablePos = 20 - m_disablePos;
}
@ -548,7 +552,8 @@ XWindowsScreenSaver::activateDPMS(bool activate)
if (m_dpms) {
// DPMSForceLevel will generate a BadMatch if DPMS is disabled
XWindowsUtil::ErrorLock lock(m_display);
DPMSForceLevel(m_display, activate ? DPMSModeStandby : DPMSModeOn);
m_impl->DPMSForceLevel(m_display,
activate ? DPMSModeStandby : DPMSModeOn);
}
#endif
}
@ -559,10 +564,10 @@ XWindowsScreenSaver::enableDPMS(bool enable)
#if HAVE_X11_EXTENSIONS_DPMS_H
if (m_dpms) {
if (enable) {
DPMSEnable(m_display);
m_impl->DPMSEnable(m_display);
}
else {
DPMSDisable(m_display);
m_impl->DPMSDisable(m_display);
}
}
#endif
@ -575,7 +580,7 @@ XWindowsScreenSaver::isDPMSEnabled() const
if (m_dpms) {
CARD16 level;
BOOL state;
DPMSInfo(m_display, &level, &state);
m_impl->DPMSInfo(m_display, &level, &state);
return (state != False);
}
else {
@ -593,7 +598,7 @@ XWindowsScreenSaver::isDPMSActivated() const
if (m_dpms) {
CARD16 level;
BOOL state;
DPMSInfo(m_display, &level, &state);
m_impl->DPMSInfo(m_display, &level, &state);
return (level != DPMSModeOn);
}
else {

View File

@ -21,6 +21,7 @@
#include "barrier/IScreenSaver.h"
#include "base/IEventQueue.h"
#include "common/stdmap.h"
#include "XWindowsImpl.h"
#if X_DISPLAY_MISSING
# error X11 is required to build barrier
@ -34,7 +35,8 @@ class EventQueueTimer;
//! X11 screen saver implementation
class XWindowsScreenSaver : public IScreenSaver {
public:
XWindowsScreenSaver(Display*, Window, void* eventTarget, IEventQueue* events);
XWindowsScreenSaver(IXWindowsImpl* impl, Display*, Window,
void* eventTarget, IEventQueue* events);
virtual ~XWindowsScreenSaver();
//! @name manipulators
@ -112,6 +114,8 @@ private:
private:
typedef std::map<Window, long> WatchList;
IXWindowsImpl* m_impl;
// the X display
Display* m_display;