Ticket #8357: screensaver-xdg.cpp

File screensaver-xdg.cpp, 3.7 KB (added by Johnny Walker <johnnyjboss@…>, 10 years ago)
Line 
1
2// Own header
3#include "screensaver-xdg.h"
4
5// QT headers
6#include <QWidget>
7#include <QDateTime>
8#include <QTimer>
9#include <QX11Info>
10#include <QString>
11
12// Mythdb headers
13#include "mythverbose.h"
14#include "mythdb.h"
15
16// Mythui headers
17#include "mythsystem.h"
18#include "mythxdisplay.h"
19#include "mythmainwindow.h"
20
21// X11 headers
22#include <X11/Xlib.h>
23
24extern "C" {
25#include <X11/extensions/dpms.h>
26}
27
28#define LOC      QString("ScreenSaverXDGPrivate: ")
29#define LOC_WARN QString("ScreenSaverXDGPrivate, Warning: ")
30#define LOC_ERR  QString("ScreenSaverXDGPrivate, Error: ")
31
32class ScreenSaverXDGPrivate
33{
34    friend class ScreenSaverXDG;
35
36  public:
37    ScreenSaverXDGPrivate(ScreenSaverXDG *outer) :
38        m_xdgscreensaverRunning(false),
39        m_display(NULL)
40    {
41        m_xdgscreensaverRunning =
42                myth_system("xdg-screensaver --version >&- 2>&-") == 0;
43
44        if (IsScreenSaverRunning())
45        {
46            if (m_xdgscreensaverRunning){
47                VERBOSE(VB_GENERAL, LOC + "XDGScreenSaver support enabled");
48            }
49
50        }
51
52        m_display = OpenMythXDisplay();
53        if (!m_display)
54            VERBOSE(VB_IMPORTANT, LOC_ERR + "Failed to open connection to X11 server");
55    }
56
57    ~ScreenSaverXDGPrivate()
58    {
59        // m_resetTimer deleted by ScreenSaverX11 QObject dtor
60        delete m_display;
61    }
62
63    bool IsScreenSaverRunning(void) const
64    {
65        return m_xdgscreensaverRunning;
66    }
67
68    QString getXWindowId(void)
69    {
70        //return window id
71        WId myWId = GetMythMainWindow()->winId();
72        QString windowId = QString("0x%1").arg(myWId, 0, 16);
73        //log it
74        VERBOSE(VB_GENERAL, QString("XDG: xwindowid of MythMainWindow at this time is: %1").arg(windowId));
75        return windowId;
76    }
77
78    void ResetScreenSaver(void)
79    {
80        VERBOSE(VB_GENERAL, "XDG: ScreenSaverXDG::ResetScreenSaver - doing nothing...");
81    }
82
83  private:
84    class ScreenSaverState
85    {
86      public:
87        ScreenSaverState() :
88            saved(false), timeout(-1), interval(-1),
89            preferblank(-1), allowexposure(-1) {}
90        bool saved;
91        int timeout;
92        int interval;
93        int preferblank;
94        int allowexposure;
95    };
96
97  private:
98    bool m_xdgscreensaverRunning;
99
100    ScreenSaverState m_state;
101    MythXDisplay *m_display;
102};
103
104ScreenSaverXDG::ScreenSaverXDG() :
105    d(new ScreenSaverXDGPrivate(this))
106{
107}
108
109ScreenSaverXDG::~ScreenSaverXDG()
110{
111    delete d;
112}
113
114bool ScreenSaverXDG::isXDGInstalled(void)
115{
116        bool result = myth_system("xdg-screensaver --version >&- 2>&-") == 0;
117        if (result){
118                VERBOSE(VB_GENERAL, "XDG: XDG-screensaver is supported.");
119        }
120        else {
121                VERBOSE(VB_GENERAL, "XDG: xdg-utils are NOT installed. Failing back to timers and pokes");
122        }
123        return result;
124}
125
126void ScreenSaverXDG::Disable(void)
127{
128        if (d->m_display)
129        {
130                if (!d->IsScreenSaverRunning())
131                        return;
132                {
133                        if (d->m_xdgscreensaverRunning)
134                        {
135                                VERBOSE(VB_GENERAL, "XDG: Calling xdg-screensaver suspend windowID");
136                                myth_system(QString("xdg-screensaver suspend %1 >&- 2>&- &").arg(d->getXWindowId()));
137                        }
138                }
139        }
140}
141
142void ScreenSaverXDG::Restore(void)
143{
144        if (d->m_display)
145        {
146                if (!d->IsScreenSaverRunning())
147                        return;
148                {
149                        if (d->m_xdgscreensaverRunning)
150                        {
151                                VERBOSE(VB_GENERAL, "XDG: Calling xdg-screensaver resume windowID");
152                                myth_system(QString("xdg-screensaver resume %1 >&- 2>&- &").arg(d->getXWindowId()));
153                        }
154                }
155        }
156}
157
158void ScreenSaverXDG::Reset(void)
159{
160    //nothing to see here people
161        //go back to your boxed wines and episodes of House
162}
163
164bool ScreenSaverXDG::Asleep(void)
165{
166        //my understanding was only use this as a fallback.
167        //the logic here remains in the ScreenSaverX11 class.
168    return false;
169}
170
171void ScreenSaverXDG::resetSlot(void)
172{
173    d->ResetScreenSaver();
174}