1
/***************************************************************************
2
* Copyright (C) 2010 by Dario Freddi <drf@kde.org> *
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. *
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. *
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
***************************************************************************/
21
#include "powerdevildpmsaction.h"
23
#include <powerdevilcore.h>
25
#include <config-workspace.h>
27
#include <QtGui/QX11Info>
29
#include <KConfigGroup>
30
#include <KPluginFactory>
35
#include <X11/Xatom.h>
36
#include <X11/Xutil.h>
39
#include <X11/extensions/dpms.h>
40
int __kde_do_not_unload = 1;
42
#ifndef HAVE_DPMSCAPABLE_PROTO
43
Bool DPMSCapable(Display *);
46
#ifndef HAVE_DPMSINFO_PROTO
47
Status DPMSInfo(Display *, CARD16 *, BOOL *);
50
int dropError(Display *, XErrorEvent *);
51
typedef int (*XErrFunc)(Display *, XErrorEvent *);
54
int dropError(Display *, XErrorEvent *)
59
class PowerDevilDPMSAction::Private
62
XErrorHandler defaultHandler;
65
K_PLUGIN_FACTORY(PowerDevilDPMSActionFactory, registerPlugin<PowerDevilDPMSAction>(); )
66
K_EXPORT_PLUGIN(PowerDevilDPMSActionFactory("powerdevildpmsaction"))
68
PowerDevilDPMSAction::PowerDevilDPMSAction(QObject* parent, const QVariantList& )
73
setRequiredPolicies(PowerDevil::PolicyAgent::ChangeScreenSettings);
75
// We want to query for DPMS in the constructor, before anything else happens
76
d->defaultHandler = XSetErrorHandler(dropError);
78
Display *dpy = QX11Info::display();
82
if (!DPMSQueryExtension(dpy, &dummy, &dummy) || !DPMSCapable(dpy)) {
84
XSetErrorHandler(d->defaultHandler);
87
// Pretend we're unloading profiles here, as if the action is not enabled, DPMS should be switched off.
91
PowerDevilDPMSAction::~PowerDevilDPMSAction()
96
void PowerDevilDPMSAction::onProfileUnload()
98
Display *dpy = QX11Info::display();
104
void PowerDevilDPMSAction::onWakeupFromIdle()
109
void PowerDevilDPMSAction::onIdleTimeout(int msec)
114
void PowerDevilDPMSAction::onProfileLoad()
116
Display *dpy = QX11Info::display();
124
XSetErrorHandler(d->defaultHandler);
126
DPMSSetTimeouts(dpy, (CARD16)m_idleTime, (CARD16)(m_idleTime * 1.5), (CARD16)(m_idleTime * 2));
129
XSetErrorHandler(d->defaultHandler);
132
void PowerDevilDPMSAction::triggerImpl(const QVariantMap& args)
136
Display *dpy = QX11Info::display();
137
DPMSInfo(dpy, &dummy, &enabled);
139
// Let's pretend we're resuming
140
core()->onResumeFromSuspend();
142
if (args["Type"].toString() == "TurnOff") {
144
DPMSForceLevel(dpy, DPMSModeOff);
147
DPMSForceLevel(dpy, DPMSModeOff);
149
} else if (args["Type"].toString() == "Standby") {
151
DPMSForceLevel(dpy, DPMSModeStandby);
154
DPMSForceLevel(dpy, DPMSModeStandby);
156
} else if (args["Type"].toString() == "Suspend") {
158
DPMSForceLevel(dpy, DPMSModeSuspend);
161
DPMSForceLevel(dpy, DPMSModeSuspend);
166
bool PowerDevilDPMSAction::loadAction(const KConfigGroup& config)
168
m_idleTime = config.readEntry<int>("idleTime", -1);
173
#include "powerdevildpmsaction.moc"