mirror of
https://github.com/debauchee/barrier.git
synced 2026-11-27 22:32:02 +08:00
checkpoint. adding screen saver support. only on X so far
and untested. also some known problems: not detecting an xscreensaver started after us and not detecting built-in screen saver activation (not sure if we can without using ugly extensions).
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "CXWindowsScreen.h"
|
||||
#include "CXWindowsClipboard.h"
|
||||
#include "CXWindowsScreenSaver.h"
|
||||
#include "CXWindowsUtil.h"
|
||||
#include "CClipboard.h"
|
||||
#include "XScreen.h"
|
||||
@@ -21,7 +22,8 @@ CXWindowsScreen::CXWindowsScreen() :
|
||||
m_root(None),
|
||||
m_x(0), m_y(0),
|
||||
m_w(0), m_h(0),
|
||||
m_stop(false)
|
||||
m_stop(false),
|
||||
m_screenSaver(NULL)
|
||||
{
|
||||
assert(s_screen == NULL);
|
||||
s_screen = this;
|
||||
@@ -77,6 +79,9 @@ CXWindowsScreen::openDisplay()
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
m_clipboard[id] = createClipboard(id);
|
||||
}
|
||||
|
||||
// initialize the screen saver
|
||||
m_screenSaver = new CXWindowsScreenSaver(m_display);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -87,9 +92,14 @@ CXWindowsScreen::closeDisplay()
|
||||
// let subclass close down display
|
||||
onCloseDisplay(m_display);
|
||||
|
||||
// done with screen saver
|
||||
delete m_screenSaver;
|
||||
m_screenSaver = NULL;
|
||||
|
||||
// destroy clipboards
|
||||
for (ClipboardID id = 0; id < kClipboardEnd; ++id) {
|
||||
delete m_clipboard[id];
|
||||
m_clipboard[id] = NULL;
|
||||
}
|
||||
|
||||
// close the display
|
||||
@@ -285,9 +295,20 @@ CXWindowsScreen::processEvent(XEvent* xevent)
|
||||
return true;
|
||||
}
|
||||
|
||||
// let screen saver have a go
|
||||
if (m_screenSaver->processEvent(xevent)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
CXWindowsScreenSaver*
|
||||
CXWindowsScreen::getScreenSaver() const
|
||||
{
|
||||
return m_screenSaver;
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreen::setDisplayClipboard(ClipboardID id,
|
||||
const IClipboard* clipboard)
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
#endif
|
||||
|
||||
class IClipboard;
|
||||
class IScreenSaver;
|
||||
class CXWindowsClipboard;
|
||||
class CXWindowsScreenSaver;
|
||||
|
||||
class CXWindowsScreen {
|
||||
public:
|
||||
@@ -68,6 +70,10 @@ protected:
|
||||
bool getDisplayClipboard(ClipboardID,
|
||||
IClipboard* clipboard) const;
|
||||
|
||||
// get the screen saver object
|
||||
CXWindowsScreenSaver*
|
||||
getScreenSaver() const;
|
||||
|
||||
// called by openDisplay() to allow subclasses to prepare the display.
|
||||
// the display is locked and passed to the subclass.
|
||||
virtual void onOpenDisplay(Display*) = 0;
|
||||
@@ -116,6 +122,9 @@ private:
|
||||
// clipboards
|
||||
CXWindowsClipboard* m_clipboard[kClipboardEnd];
|
||||
|
||||
// screen saver
|
||||
CXWindowsScreenSaver* m_screenSaver;
|
||||
|
||||
// X is not thread safe
|
||||
CMutex m_mutex;
|
||||
|
||||
|
||||
255
platform/CXWindowsScreenSaver.cpp
Normal file
255
platform/CXWindowsScreenSaver.cpp
Normal file
@@ -0,0 +1,255 @@
|
||||
#include "CXWindowsScreenSaver.h"
|
||||
#include "CXWindowsUtil.h"
|
||||
#include <X11/Xatom.h>
|
||||
|
||||
//
|
||||
// CXWindowsScreenSaver
|
||||
//
|
||||
|
||||
CXWindowsScreenSaver::CXWindowsScreenSaver(Display* display) :
|
||||
m_display(display),
|
||||
m_notify(None),
|
||||
m_xscreensaver(None)
|
||||
{
|
||||
// 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);
|
||||
|
||||
// watch top-level windows for changes
|
||||
{
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(&error);
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(m_display, root, &attr);
|
||||
m_rootEventMask = attr.your_event_mask;
|
||||
XSelectInput(m_display, root, m_rootEventMask | SubstructureNotifyMask);
|
||||
if (error) {
|
||||
m_rootEventMask = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// get the xscreensaver window, if any
|
||||
updateXScreenSaver();
|
||||
|
||||
// get the built-in settings
|
||||
XGetScreenSaver(m_display, &m_timeout, &m_interval,
|
||||
&m_preferBlanking, &m_allowExposures);
|
||||
}
|
||||
|
||||
CXWindowsScreenSaver::~CXWindowsScreenSaver()
|
||||
{
|
||||
// stop watching root for events
|
||||
CXWindowsUtil::CErrorLock lock;
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
XSelectInput(m_display, root, m_rootEventMask);
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::processEvent(XEvent* xevent)
|
||||
{
|
||||
switch (xevent->type) {
|
||||
case DestroyNotify:
|
||||
if (xevent->xdestroywindow.window == m_xscreensaver) {
|
||||
// xscreensaver is gone
|
||||
setXScreenSaver(false);
|
||||
m_xscreensaver = None;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case MapNotify:
|
||||
if (xevent->xmap.window == m_xscreensaver) {
|
||||
// xscreensaver has activated
|
||||
setXScreenSaver(true);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
|
||||
case UnmapNotify:
|
||||
if (xevent->xunmap.window == m_xscreensaver) {
|
||||
// xscreensaver has deactivated
|
||||
setXScreenSaver(false);
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::setNotify(Window notify)
|
||||
{
|
||||
m_notify = notify;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::enable()
|
||||
{
|
||||
// try xscreensaver
|
||||
updateXScreenSaver();
|
||||
if (m_xscreensaver) {
|
||||
// FIXME
|
||||
return;
|
||||
}
|
||||
|
||||
// use built-in X screen saver
|
||||
XSetScreenSaver(m_display, m_timeout, m_interval,
|
||||
m_preferBlanking, m_allowExposures);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::disable()
|
||||
{
|
||||
// try xscreensaver
|
||||
updateXScreenSaver();
|
||||
if (m_xscreensaver) {
|
||||
// FIXME
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
// FIXME -- now deactivate?
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::activate()
|
||||
{
|
||||
// try xscreensaver
|
||||
updateXScreenSaver();
|
||||
if (m_xscreensaver) {
|
||||
sendXScreenSaverCommand(m_atomScreenSaverActivate);
|
||||
return;
|
||||
}
|
||||
|
||||
// use built-in X screen saver
|
||||
XForceScreenSaver(m_display, ScreenSaverActive);
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::deactivate()
|
||||
{
|
||||
// try xscreensaver
|
||||
updateXScreenSaver();
|
||||
if (m_xscreensaver) {
|
||||
sendXScreenSaverCommand(m_atomScreenSaverDeactivate);
|
||||
return;
|
||||
}
|
||||
|
||||
// use built-in X screen saver
|
||||
XForceScreenSaver(m_display, ScreenSaverReset);
|
||||
}
|
||||
|
||||
bool
|
||||
CXWindowsScreenSaver::isActive() const
|
||||
{
|
||||
// check xscreensaver
|
||||
if (m_xscreensaver != None) {
|
||||
return m_xscreensaverActive;
|
||||
}
|
||||
|
||||
// can't check built-in X screen saver activity
|
||||
return false;
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::sendNotify(bool activated)
|
||||
{
|
||||
if (m_notify != None) {
|
||||
XEvent event;
|
||||
event.xclient.type = ClientMessage;
|
||||
event.xclient.display = m_display;
|
||||
event.xclient.window = m_notify;
|
||||
event.xclient.message_type = m_atomScreenSaver;
|
||||
event.xclient.format = 32;
|
||||
event.xclient.data.l[0] = activated ? 1 : 0;
|
||||
event.xclient.data.l[1] = 0;
|
||||
event.xclient.data.l[2] = 0;
|
||||
event.xclient.data.l[3] = 0;
|
||||
event.xclient.data.l[4] = 0;
|
||||
|
||||
CXWindowsUtil::CErrorLock lock;
|
||||
XSendEvent(m_display, m_notify, False, 0, &event);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::setXScreenSaver(bool activated)
|
||||
{
|
||||
if (m_xscreensaverActive != activated) {
|
||||
m_xscreensaverActive = activated;
|
||||
sendNotify(activated);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::updateXScreenSaver()
|
||||
{
|
||||
// do nothing if we've already got the xscreensaver window
|
||||
if (m_xscreensaver != None) {
|
||||
return;
|
||||
}
|
||||
|
||||
// find top-level window with m_atomScreenSaverVersion string property
|
||||
CXWindowsUtil::CErrorLock lock;
|
||||
Window root = DefaultRootWindow(m_display);
|
||||
Window rw, pw, *cw;
|
||||
unsigned int nc;
|
||||
if (XQueryTree(m_display, root, &rw, &pw, &cw, &nc)) {
|
||||
CString data;
|
||||
Atom type;
|
||||
for (unsigned int i = 0; i < nc; ++i) {
|
||||
if (CXWindowsUtil::getWindowProperty(m_display, cw[i],
|
||||
m_atomScreenSaverVersion,
|
||||
&data, &type, NULL, False) &&
|
||||
type == XA_STRING) {
|
||||
m_xscreensaver = cw[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
XFree(cw);
|
||||
}
|
||||
|
||||
// see if xscreensaver is active
|
||||
if (m_xscreensaver != None) {
|
||||
bool error = false;
|
||||
CXWindowsUtil::CErrorLock lock(&error);
|
||||
XWindowAttributes attr;
|
||||
XGetWindowAttributes(m_display, m_xscreensaver, &attr);
|
||||
setXScreenSaver(!error && attr.map_state != IsUnmapped);
|
||||
}
|
||||
else {
|
||||
setXScreenSaver(false);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CXWindowsScreenSaver::sendXScreenSaverCommand(
|
||||
Atom cmd, long arg1, long arg2) const
|
||||
{
|
||||
XEvent event;
|
||||
event.xclient.type = ClientMessage;
|
||||
event.xclient.display = m_display;
|
||||
event.xclient.window = None;
|
||||
event.xclient.message_type = m_atomScreenSaver;
|
||||
event.xclient.format = 32;
|
||||
event.xclient.data.l[0] = static_cast<long>(cmd);
|
||||
event.xclient.data.l[1] = arg1;
|
||||
event.xclient.data.l[2] = arg2;
|
||||
event.xclient.data.l[3] = 0;
|
||||
event.xclient.data.l[4] = 0;
|
||||
|
||||
CXWindowsUtil::CErrorLock lock;
|
||||
XSendEvent(m_display, m_xscreensaver, False, 0, &event);
|
||||
}
|
||||
80
platform/CXWindowsScreenSaver.h
Normal file
80
platform/CXWindowsScreenSaver.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#ifndef CXWINDOWSSCREENSAVER_H
|
||||
#define CXWINDOWSSCREENSAVER_H
|
||||
|
||||
#include "IScreenSaver.h"
|
||||
#if defined(X_DISPLAY_MISSING)
|
||||
# error X11 is required to build synergy
|
||||
#else
|
||||
# include <X11/Xlib.h>
|
||||
#endif
|
||||
|
||||
class CXWindowsScreenSaver : public IScreenSaver {
|
||||
public:
|
||||
// note -- the caller must ensure that Display* passed to c'tor isn't
|
||||
// being used in another call to Xlib when calling any method on this
|
||||
// object (including during the c'tor and d'tor) except processEvent().
|
||||
CXWindowsScreenSaver(Display*);
|
||||
virtual ~CXWindowsScreenSaver();
|
||||
|
||||
// process X event. returns true if the event was handled.
|
||||
bool processEvent(XEvent*);
|
||||
|
||||
// tells this object to send a ClientMessage to the given window
|
||||
// when the screen saver activates or deactivates. only one
|
||||
// window can be notified. the message type is the "SCREENSAVER"
|
||||
// atom, the format is 32, and the data.l[0] member is non-zero
|
||||
// if activated, zero if deactivated. pass in None to disable
|
||||
// notification;
|
||||
void setNotify(Window);
|
||||
|
||||
// IScreenSaver overrides
|
||||
virtual void enable();
|
||||
virtual void disable();
|
||||
virtual void activate();
|
||||
virtual void deactivate();
|
||||
virtual bool isActive() const;
|
||||
|
||||
private:
|
||||
// send a notification
|
||||
void sendNotify(bool activated);
|
||||
|
||||
// set xscreensaver's activation state flag. sends notification
|
||||
// if the state has changed.
|
||||
void setXScreenSaver(bool activated);
|
||||
|
||||
// find the running xscreensaver's window
|
||||
void updateXScreenSaver();
|
||||
|
||||
// send a command to xscreensaver
|
||||
void sendXScreenSaverCommand(Atom, long = 0, long = 0) const;
|
||||
|
||||
private:
|
||||
// the X display
|
||||
Display* m_display;
|
||||
|
||||
// old event mask on root window
|
||||
long m_rootEventMask;
|
||||
|
||||
// window to notify on screen saver activation/deactivation
|
||||
Window m_notify;
|
||||
|
||||
// xscreensaver's window
|
||||
Window m_xscreensaver;
|
||||
|
||||
// xscreensaver activation state
|
||||
bool m_xscreensaverActive;
|
||||
|
||||
// atoms used to communicate with xscreensaver's window
|
||||
Atom m_atomScreenSaver;
|
||||
Atom m_atomScreenSaverVersion;
|
||||
Atom m_atomScreenSaverActivate;
|
||||
Atom m_atomScreenSaverDeactivate;
|
||||
|
||||
// built-in screen saver settings
|
||||
int m_timeout;
|
||||
int m_interval;
|
||||
int m_preferBlanking;
|
||||
int m_allowExposures;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -4,17 +4,19 @@ DEPTH = ..
|
||||
|
||||
# FIXME -- add CUnixPlatform.cpp as an unbuilt source
|
||||
noinst_LIBRARIES = libplatform.a
|
||||
libplatform_a_SOURCES = \
|
||||
CPlatform.cpp \
|
||||
CXWindowsClipboard.cpp \
|
||||
CXWindowsScreen.cpp \
|
||||
CXWindowsUtil.cpp \
|
||||
CPlatform.h \
|
||||
CUnixPlatform.h \
|
||||
CXWindowsClipboard.h \
|
||||
CXWindowsScreen.h \
|
||||
CXWindowsUtil.h \
|
||||
IPlatform.h \
|
||||
libplatform_a_SOURCES = \
|
||||
CPlatform.cpp \
|
||||
CXWindowsClipboard.cpp \
|
||||
CXWindowsScreen.cpp \
|
||||
CXWindowsScreenSaver.cpp \
|
||||
CXWindowsUtil.cpp \
|
||||
CPlatform.h \
|
||||
CUnixPlatform.h \
|
||||
CXWindowsClipboard.h \
|
||||
CXWindowsScreen.h \
|
||||
CXWindowsScreenSaver.h \
|
||||
CXWindowsUtil.h \
|
||||
IPlatform.h \
|
||||
$(NULL)
|
||||
INCLUDES = \
|
||||
-I$(DEPTH)/base \
|
||||
|
||||
Reference in New Issue
Block a user