~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to powerdevil/daemon/actions/dpms/powerdevildpmsaction.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2010 by Dario Freddi <drf@kde.org>                      *
 
3
 *                                                                         *
 
4
 *   This program is free software; you can redistribute it and/or modify  *
 
5
 *   it under the terms of the GNU General Public License as published by  *
 
6
 *   the Free Software Foundation; either version 2 of the License, or     *
 
7
 *   (at your option) any later version.                                   *
 
8
 *                                                                         *
 
9
 *   This program is distributed in the hope that it will be useful,       *
 
10
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 
11
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 
12
 *   GNU General Public License for more details.                          *
 
13
 *                                                                         *
 
14
 *   You should have received a copy of the GNU General Public License     *
 
15
 *   along with this program; if not, write to the                         *
 
16
 *   Free Software Foundation, Inc.,                                       *
 
17
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 
18
 ***************************************************************************/
 
19
 
 
20
 
 
21
#include "powerdevildpmsaction.h"
 
22
 
 
23
#include <powerdevilcore.h>
 
24
 
 
25
#include <config-workspace.h>
 
26
 
 
27
#include <QtGui/QX11Info>
 
28
 
 
29
#include <KConfigGroup>
 
30
#include <KPluginFactory>
 
31
 
 
32
#include <X11/Xmd.h>
 
33
#include <X11/X.h>
 
34
#include <X11/Xlib.h>
 
35
#include <X11/Xatom.h>
 
36
#include <X11/Xutil.h>
 
37
#include <X11/Xos.h>
 
38
extern "C" {
 
39
#include <X11/extensions/dpms.h>
 
40
    int __kde_do_not_unload = 1;
 
41
 
 
42
#ifndef HAVE_DPMSCAPABLE_PROTO
 
43
    Bool DPMSCapable(Display *);
 
44
#endif
 
45
 
 
46
#ifndef HAVE_DPMSINFO_PROTO
 
47
    Status DPMSInfo(Display *, CARD16 *, BOOL *);
 
48
#endif
 
49
 
 
50
int dropError(Display *, XErrorEvent *);
 
51
typedef int (*XErrFunc)(Display *, XErrorEvent *);
 
52
}
 
53
 
 
54
int dropError(Display *, XErrorEvent *)
 
55
{
 
56
    return 0;
 
57
}
 
58
 
 
59
class PowerDevilDPMSAction::Private
 
60
{
 
61
public:
 
62
    XErrorHandler defaultHandler;
 
63
};
 
64
 
 
65
K_PLUGIN_FACTORY(PowerDevilDPMSActionFactory, registerPlugin<PowerDevilDPMSAction>(); )
 
66
K_EXPORT_PLUGIN(PowerDevilDPMSActionFactory("powerdevildpmsaction"))
 
67
 
 
68
PowerDevilDPMSAction::PowerDevilDPMSAction(QObject* parent, const QVariantList& )
 
69
    : Action(parent)
 
70
    , m_hasDPMS(true)
 
71
    , d(new Private)
 
72
{
 
73
    setRequiredPolicies(PowerDevil::PolicyAgent::ChangeScreenSettings);
 
74
 
 
75
    // We want to query for DPMS in the constructor, before anything else happens
 
76
    d->defaultHandler = XSetErrorHandler(dropError);
 
77
 
 
78
    Display *dpy = QX11Info::display();
 
79
 
 
80
    int dummy;
 
81
 
 
82
    if (!DPMSQueryExtension(dpy, &dummy, &dummy) || !DPMSCapable(dpy)) {
 
83
        m_hasDPMS = false;
 
84
        XSetErrorHandler(d->defaultHandler);
 
85
    }
 
86
 
 
87
    // Pretend we're unloading profiles here, as if the action is not enabled, DPMS should be switched off.
 
88
    onProfileUnload();
 
89
}
 
90
 
 
91
PowerDevilDPMSAction::~PowerDevilDPMSAction()
 
92
{
 
93
    delete d;
 
94
}
 
95
 
 
96
void PowerDevilDPMSAction::onProfileUnload()
 
97
{
 
98
    Display *dpy = QX11Info::display();
 
99
    if (m_hasDPMS) {
 
100
        DPMSDisable(dpy);
 
101
    }
 
102
}
 
103
 
 
104
void PowerDevilDPMSAction::onWakeupFromIdle()
 
105
{
 
106
    //
 
107
}
 
108
 
 
109
void PowerDevilDPMSAction::onIdleTimeout(int msec)
 
110
{
 
111
    Q_UNUSED(msec);
 
112
}
 
113
 
 
114
void PowerDevilDPMSAction::onProfileLoad()
 
115
{
 
116
    Display *dpy = QX11Info::display();
 
117
    if (m_hasDPMS) {
 
118
        DPMSEnable(dpy);
 
119
    } else {
 
120
        return;
 
121
    }
 
122
 
 
123
    XFlush(dpy);
 
124
    XSetErrorHandler(d->defaultHandler);
 
125
 
 
126
    DPMSSetTimeouts(dpy, (CARD16)m_idleTime, (CARD16)(m_idleTime * 1.5), (CARD16)(m_idleTime * 2));
 
127
 
 
128
    XFlush(dpy);
 
129
    XSetErrorHandler(d->defaultHandler);
 
130
}
 
131
 
 
132
void PowerDevilDPMSAction::triggerImpl(const QVariantMap& args)
 
133
{
 
134
    CARD16 dummy;
 
135
    BOOL enabled;
 
136
    Display *dpy = QX11Info::display();
 
137
    DPMSInfo(dpy, &dummy, &enabled);
 
138
 
 
139
    // Let's pretend we're resuming
 
140
    core()->onResumeFromSuspend();
 
141
 
 
142
    if (args["Type"].toString() == "TurnOff") {
 
143
        if (enabled) {
 
144
            DPMSForceLevel(dpy, DPMSModeOff);
 
145
        } else {
 
146
            DPMSEnable(dpy);
 
147
            DPMSForceLevel(dpy, DPMSModeOff);
 
148
        }
 
149
    } else if (args["Type"].toString() == "Standby") {
 
150
        if (enabled) {
 
151
            DPMSForceLevel(dpy, DPMSModeStandby);
 
152
        } else {
 
153
            DPMSEnable(dpy);
 
154
            DPMSForceLevel(dpy, DPMSModeStandby);
 
155
        }
 
156
    } else if (args["Type"].toString() == "Suspend") {
 
157
        if (enabled) {
 
158
            DPMSForceLevel(dpy, DPMSModeSuspend);
 
159
        } else {
 
160
            DPMSEnable(dpy);
 
161
            DPMSForceLevel(dpy, DPMSModeSuspend);
 
162
        }
 
163
    }
 
164
}
 
165
 
 
166
bool PowerDevilDPMSAction::loadAction(const KConfigGroup& config)
 
167
{
 
168
    m_idleTime = config.readEntry<int>("idleTime", -1);
 
169
 
 
170
    return true;
 
171
}
 
172
 
 
173
#include "powerdevildpmsaction.moc"