Pool Video Switch v2
Software video switch for distributed remote display in a lecture environment
screensaver.cpp
Go to the documentation of this file.
1 #include "screensaver.h"
2 
3 #include <QX11Info>
4 #include <QStringList>
5 #include <QProcess>
6 #include <QTimer>
7 #include <X11/Xlib.h>
8 
9 #ifdef X11_Xscreensaver_FOUND
10 # include <X11/extensions/scrnsaver.h>
11 #endif
12 #ifdef X11_dpms_FOUND
13 #include <X11/extensions/dpms.h>
14 #endif
15 
16 namespace {
17 
18  Display *display = nullptr;
19  bool extensionSupported = false;
20  bool dpmsSupported = false;
21  QTimer *timer = nullptr;
22 
23  bool init()
24  {
25  if (display != nullptr) {
26  return true;
27  }
28  display = QX11Info::display();
29  if (display == nullptr) {
30  return false;
31  }
32 #ifdef X11_Xscreensaver_FOUND
33  int dummy;
34  extensionSupported = (XScreenSaverQueryExtension(display, &dummy, &dummy) == True);
35 #endif
36 #ifdef X11_dpms_FOUND
37  dpmsSupported = (DPMSCapable(display) == True);
38 #endif
39  return true;
40  }
41 
42 }
43 
44 namespace ScreenSaver {
45 
46  void allowSaverAndStandby(bool allow)
47  {
48  if (!init())
49  return;
50 #ifdef X11_Xscreensaver_FOUND
51  if (extensionSupported) {
52  XScreenSaverSuspend(display, allow ? False : True);
53  return;
54  }
55 #endif
56  if (allow) {
57  if (timer != nullptr) {
58  timer->stop();
59  }
60  } else {
61  if (timer == nullptr) {
62  timer = new QTimer;
63  QObject::connect(timer, &QTimer::timeout, forceUnlockAndScreenOn);
64  }
65  timer->setSingleShot(false);
66  timer->start(55000);
67  }
68  }
69 
71  {
72  if (!init())
73  return;
74  QProcess::startDetached("xscreensaver-command", QStringList() << "-deactivate");
75 #ifdef X11_dpms_FOUND
76  CARD16 power_level;
77  BOOL state;
78  if (DPMSInfo(display, &power_level, &state) && state) {
79  DPMSForceLevel(display, DPMSModeOn);
80  return;
81  }
82 #endif
83  }
84 
85 }
void allowSaverAndStandby(bool allow)
Whether we want to allow the screen saver to activate or the screen to enter standby.
Definition: screensaver.cpp:46
void forceUnlockAndScreenOn()
Disable the screen saver (only if password locking is disabled!), power screen on if it's in standby...
Definition: screensaver.cpp:70