summaryrefslogtreecommitdiffstats
path: root/kcontrol/energy/energy.cpp
blob: 60be2cc1f074309790e5bfdfe81deef8ff4539d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/* vi: ts=8 sts=4 sw=4
 *
 *
 *
 * This file is part of the KDE project, module kcontrol.
 * Copyright (C) 1999 Geert Jansen <g.t.jansen@stud.tue.nl>
 *
 * You can Freely distribute this program under the GNU General Public
 * License. See the file "COPYING" for the exact licensing terms.
 *
 * Based on kcontrol1 energy.cpp, Copyright (c) 1999 Tom Vijlbrief
 */


/*
 * KDE Energy setup module.
 */

#include <config.h>

#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqwhatsthis.h>
#include <tqpushbutton.h>

#include <tdeconfig.h>
#include <kcursor.h>
#include <kdialog.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <knuminput.h>
#include <krun.h>
#include <kstandarddirs.h>
#include <kurllabel.h>
#include <dcopref.h>

#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>

#include "energy.h"


#ifdef HAVE_DPMS
#include <X11/Xmd.h>
extern "C" {
#include <X11/extensions/dpms.h>
Status DPMSInfo ( Display *, CARD16 *, BOOL * );
Bool DPMSCapable( Display * );
int __kde_do_not_unload = 1;

#ifndef HAVE_DPMSCAPABLE_PROTO
Bool DPMSCapable ( Display * );
#endif

#ifndef HAVE_DPMSINFO_PROTO
Status DPMSInfo ( Display *, CARD16 *, BOOL * );
#endif
}

#if defined(XIMStringConversionRetrival) || defined (__sun) || defined(__hpux)
extern "C" {
#endif
    Bool DPMSQueryExtension(Display *, int *, int *);
    Status DPMSEnable(Display *);
    Status DPMSDisable(Display *);
    Bool DPMSGetTimeouts(Display *, CARD16 *, CARD16 *, CARD16 *);
    Bool DPMSSetTimeouts(Display *, CARD16, CARD16, CARD16);
#if defined(XIMStringConversionRetrival) || defined (__sun) || defined(__hpux)
}
#endif
#endif

static const int DFLT_STANDBY   = 0;
static const int DFLT_SUSPEND   = 30;
static const int DFLT_OFF   = 60;


/**** DLL Interface ****/

extern "C" {

    KDE_EXPORT TDECModule *create_energy(TQWidget *parent, char *) {
	return new KEnergy(parent, "kcmenergy");
    }

    KDE_EXPORT void init_energy() {
#ifdef HAVE_DPMS
        TDEConfig *cfg = new TDEConfig("kcmdisplayrc", true /*readonly*/, false /*no globals*/);
        cfg->setGroup("DisplayEnergy");

	Display *dpy = tqt_xdisplay();
	CARD16 pre_configured_status;
	BOOL pre_configured_enabled;
	CARD16 pre_configured_standby;
	CARD16 pre_configured_suspend;
	CARD16 pre_configured_off;
        bool enabled;
        CARD16 standby;
        CARD16 suspend;
        CARD16 off;
	int dummy;
	/* query the running X server if DPMS is supported */
	if (DPMSQueryExtension(dpy, &dummy, &dummy) && DPMSCapable(dpy)) {
	    DPMSGetTimeouts(dpy, &pre_configured_standby, &pre_configured_suspend, &pre_configured_off);
	    DPMSInfo(dpy, &pre_configured_status, &pre_configured_enabled);
	    /* let the user override the settings */
	    enabled = cfg->readBoolEntry("displayEnergySaving", pre_configured_enabled);
	    standby = cfg->readNumEntry("displayStandby", pre_configured_standby/60);
	    suspend = cfg->readNumEntry("displaySuspend", pre_configured_suspend/60);
	    off = cfg->readNumEntry("displayPowerOff", pre_configured_off/60);
	} else {
	/* provide our defauts */
	    enabled = true;
	    standby = DFLT_STANDBY;
	    suspend = DFLT_SUSPEND;
	    off = DFLT_OFF;
	}

        delete cfg;

        KEnergy::applySettings(enabled, standby, suspend, off);
#endif
    }
}

/**** KEnergy ****/

KEnergy::KEnergy(TQWidget *parent, const char *name)
    : TDECModule(parent, name)
{
    m_bChanged = false;
    m_bEnabled = false;
    m_Standby = DFLT_STANDBY;
    m_Suspend = DFLT_SUSPEND;
    m_Off = DFLT_OFF;
    m_bDPMS = false;
    m_bKPowersave = false;
    m_bTDEPowersave = false;
    m_bMaintainSanity = true;

    setQuickHelp( i18n("<h1>Display Power Control</h1> If your display supports"
      " power saving features, you can configure them using this module.<p>"
      " There are three levels of power saving: standby, suspend, and off."
      " The greater the level of power saving, the longer it takes for the"
      " display to return to an active state.<p>"
      " To wake up the display from a power saving mode, you can make a small"
      " movement with the mouse, or press a key that is not likely to cause"
      " any unintentional side-effects, for example, the \"Shift\" key."));

#ifdef HAVE_DPMS
    int dummy;
    m_bDPMS = DPMSQueryExtension(tqt_xdisplay(), &dummy, &dummy);

    DCOPRef kpowersave("kpowersave", "KPowersaveIface");
    DCOPReply managingDPMS = kpowersave.call("currentSchemeManagesDPMS()");
    if (managingDPMS.isValid()) {
        m_bKPowersave = managingDPMS;
        m_bDPMS = !m_bKPowersave;
    }

    DCOPRef tdepowersave("tdepowersave", "tdepowersaveIface");
    managingDPMS = tdepowersave.call("currentSchemeManagesDPMS()");
    if (managingDPMS.isValid()) {
        m_bTDEPowersave = managingDPMS;
        m_bDPMS = !m_bTDEPowersave;
    }
#endif

    TQVBoxLayout *top = new TQVBoxLayout(this, 0, KDialog::spacingHint());
    TQHBoxLayout *hbox = new TQHBoxLayout();
    top->addLayout(hbox);

    TQLabel *lbl;
    if (m_bDPMS) {
        TDEGlobal::locale()->insertCatalogue("kpowersave");
        // ### these i18n strings need to be synced with kpowersave !!
        m_pCBEnable= new TQCheckBox(i18n("&Enable display power management" ), this);
        connect(m_pCBEnable, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotChangeEnable(bool)));
        hbox->addWidget(m_pCBEnable);

        TQWhatsThis::add( m_pCBEnable, i18n("Check this option to enable the"
           " power saving features of your display.") );
        // ###
    } else if(m_bKPowersave || m_bTDEPowersave) {
        m_pCBEnable = new TQCheckBox(i18n("&Enable specific display power management"), this);
        hbox->addWidget(m_pCBEnable);
        m_bEnabled = false;
        m_pCBEnable->setChecked(true);
        m_pCBEnable->setEnabled(false);
    } else {
        lbl = new TQLabel(i18n("Your display does not support power saving."), this);
        hbox->addWidget(lbl);
    }

    KURLLabel *logo = new KURLLabel(this);
    logo->setURL("http://www.energystar.gov");
    logo->setPixmap(TQPixmap(locate("data", "kcontrol/pics/energybig.png")));
    logo->setTipText(i18n("Learn more about the Energy Star program"));
    logo->setUseTips(true); 
connect(logo, TQT_SIGNAL(leftClickedURL(const TQString&)), TQT_SLOT(openURL(const TQString &)));

    hbox->addStretch();
    hbox->addWidget(logo);

    // Sliders
    if (!m_bKPowersave && !m_bTDEPowersave) {
    m_pStandbySlider = new KIntNumInput(m_Standby, this);
    m_pStandbySlider->setLabel(i18n("&Standby after:"));
    m_pStandbySlider->setRange(0, 120, 10);
    m_pStandbySlider->setSuffix(i18n(" min"));
    m_pStandbySlider->setSpecialValueText(i18n("Disabled"));
    connect(m_pStandbySlider, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotChangeStandby(int)));
    top->addWidget(m_pStandbySlider);
    TQWhatsThis::add( m_pStandbySlider, i18n("Choose the period of inactivity"
       " after which the display should enter \"standby\" mode. This is the"
       " first level of power saving.") );

    m_pSuspendSlider = new KIntNumInput(m_pStandbySlider, m_Suspend, this);
    m_pSuspendSlider->setLabel(i18n("S&uspend after:"));
    m_pSuspendSlider->setRange(0, 120, 10);
    m_pSuspendSlider->setSuffix(i18n(" min"));
    m_pSuspendSlider->setSpecialValueText(i18n("Disabled"));
    connect(m_pSuspendSlider, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotChangeSuspend(int)));
    top->addWidget(m_pSuspendSlider);
    TQWhatsThis::add( m_pSuspendSlider, i18n("Choose the period of inactivity"
       " after which the display should enter \"suspend\" mode. This is the"
       " second level of power saving, but may not be different from the first"
       " level for some displays.") );

    m_pOffSlider = new KIntNumInput(m_pSuspendSlider, m_Off, this);
    m_pOffSlider->setLabel(i18n("&Power off after:"));
    m_pOffSlider->setRange(0, 120, 10);
    m_pOffSlider->setSuffix(i18n(" min"));
    m_pOffSlider->setSpecialValueText(i18n("Disabled"));
    connect(m_pOffSlider, TQT_SIGNAL(valueChanged(int)), TQT_SLOT(slotChangeOff(int)));
    top->addWidget(m_pOffSlider);
    TQWhatsThis::add( m_pOffSlider, i18n("Choose the period of inactivity"
       " after which the display should be powered off. This is the"
       " greatest level of power saving that can be achieved while the"
       " display is still physically turned on.") );

    } else {
        m_pStandbySlider = 0;
        m_pSuspendSlider = 0;
        m_pOffSlider = 0;
        if(m_bKPowersave) {
            TQPushButton* btnKPowersave = new TQPushButton(this);
            btnKPowersave->setText(i18n("Configure KPowersave..."));
            connect(btnKPowersave, TQT_SIGNAL(clicked()), TQT_SLOT(slotLaunchKPowersave()));
            top->addWidget(btnKPowersave);
        }
        if(m_bTDEPowersave) {
            TQPushButton* btnTDEPowersave = new TQPushButton(this);
            btnTDEPowersave->setText(i18n("Configure TDEPowersave..."));
            connect(btnTDEPowersave, TQT_SIGNAL(clicked()), TQT_SLOT(slotLaunchTDEPowersave()));
            top->addWidget(btnTDEPowersave);
		}
    }

    top->addStretch();

    if (m_bDPMS)
       setButtons( TDECModule::Help | TDECModule::Default | TDECModule::Apply );
    else
       setButtons( TDECModule::Help );

    m_pConfig = new TDEConfig("kcmdisplayrc", false /*readwrite*/, false /*no globals*/);
    m_pConfig->setGroup("DisplayEnergy");

    load();
}


KEnergy::~KEnergy()
{
    delete m_pConfig;
}


void KEnergy::load()
{
    load( false );
}

void KEnergy::load( bool useDefaults )
{
    m_pConfig->setReadDefaults( useDefaults );
    readSettings();
    showSettings();

    emit changed( useDefaults );
}


void KEnergy::save()
{
    writeSettings();
    applySettings(m_bEnabled, m_Standby, m_Suspend, m_Off);

    emit changed(false);
}


void KEnergy::defaults()
{
    load( true );
}


void KEnergy::readSettings()
{
    if (m_bDPMS) {
        m_bEnabled = m_pConfig->readBoolEntry("displayEnergySaving", false);
    }
    m_Standby = m_pConfig->readNumEntry("displayStandby", DFLT_STANDBY);
    m_Suspend = m_pConfig->readNumEntry("displaySuspend", DFLT_SUSPEND);
    m_Off = m_pConfig->readNumEntry("displayPowerOff", DFLT_OFF);

    m_StandbyDesired = m_Standby;
    m_SuspendDesired = m_Suspend;
    m_OffDesired = m_Off;

    m_bChanged = false;
}


void KEnergy::writeSettings()
{
    if (!m_bChanged)
     return;

    m_pConfig->writeEntry( "displayEnergySaving", m_bEnabled);
    m_pConfig->writeEntry("displayStandby", m_Standby);
    m_pConfig->writeEntry("displaySuspend", m_Suspend);
    m_pConfig->writeEntry("displayPowerOff", m_Off);

    m_pConfig->sync();
    m_bChanged = false;
}


void KEnergy::slotLaunchKPowersave()
{
    DCOPRef r("kpowersave", "KPowersaveIface");
    r.send("openConfigureDialog()");
}

void KEnergy::slotLaunchTDEPowersave()
{
    DCOPRef r("tdepowersave", "tdepowersaveIface");
    r.send("openConfigureDialog()");
}

void KEnergy::showSettings()
{
    m_bMaintainSanity = false;

    if (m_bDPMS) {
        m_pCBEnable->setChecked(m_bEnabled);
    }

    if (!m_bKPowersave && !m_bTDEPowersave) {
        m_pStandbySlider->setEnabled(m_bEnabled);
        m_pStandbySlider->setValue(m_Standby);
        m_pSuspendSlider->setEnabled(m_bEnabled);
        m_pSuspendSlider->setValue(m_Suspend);
        m_pOffSlider->setEnabled(m_bEnabled);
        m_pOffSlider->setValue(m_Off);
    }

    m_bMaintainSanity = true;
}


extern "C" {
  int dropError(Display *, XErrorEvent *);
  typedef int (*XErrFunc) (Display *, XErrorEvent *);
}

int dropError(Display *, XErrorEvent *)
{
    return 0;
}

/* static */
void KEnergy::applySettings(bool enable, int standby, int suspend, int off)
{
#ifdef HAVE_DPMS
    XErrFunc defaultHandler;
    defaultHandler = XSetErrorHandler(dropError);

    Display *dpy = tqt_xdisplay();

    int dummy;
    bool hasDPMS = DPMSQueryExtension(dpy, &dummy, &dummy);
    if (hasDPMS) {
        if (enable) {
            DPMSEnable(dpy);
            DPMSSetTimeouts(dpy, 60*standby, 60*suspend, 60*off);
        } else
            DPMSDisable(dpy);
    } else
        tqWarning("Server has no DPMS extension");

    XFlush(dpy);
    XSetErrorHandler(defaultHandler);
#else
    /* keep gcc silent */
    if (enable | standby | suspend | off)
    /* nothing */ ;
#endif
}


void KEnergy::slotChangeEnable(bool ena)
{
    m_bEnabled = ena;
    m_bChanged = true;

    m_pStandbySlider->setEnabled(ena);
    m_pSuspendSlider->setEnabled(ena);
    m_pOffSlider->setEnabled(ena);

    emit changed(true);
}


void KEnergy::slotChangeStandby(int value)
{
    m_Standby = value;

    if ( m_bMaintainSanity ) {
	m_bMaintainSanity = false;
	m_StandbyDesired = value;
        if ((m_Suspend > 0 && m_Standby > m_Suspend) ||
	    (m_SuspendDesired && m_Standby >= m_SuspendDesired) )
            m_pSuspendSlider->setValue(m_Standby);
        if ((m_Off > 0 && m_Standby > m_Off) ||
	    (m_OffDesired && m_Standby >= m_OffDesired) )
            m_pOffSlider->setValue(m_Standby);
	m_bMaintainSanity = true;
    }

    m_bChanged = true;
    emit changed(true);
}


void KEnergy::slotChangeSuspend(int value)
{
    m_Suspend = value;

    if ( m_bMaintainSanity ) {
	m_bMaintainSanity = false;
	m_SuspendDesired = value;
	if (m_Suspend == 0 && m_StandbyDesired > 0)
	    m_pStandbySlider->setValue( m_StandbyDesired );
        else if (m_Suspend < m_Standby || m_Suspend <= m_StandbyDesired )
            m_pStandbySlider->setValue(m_Suspend);
        if ((m_Off > 0 && m_Suspend > m_Off) ||
	    (m_OffDesired && m_Suspend >= m_OffDesired) )
            m_pOffSlider->setValue(m_Suspend);
	m_bMaintainSanity = true;
    }

    m_bChanged = true;
    emit changed(true);
}


void KEnergy::slotChangeOff(int value)
{
    m_Off = value;

    if ( m_bMaintainSanity ) {
	m_bMaintainSanity = false;
	m_OffDesired = value;
	if (m_Off == 0 && m_StandbyDesired > 0)
	    m_pStandbySlider->setValue( m_StandbyDesired );
        else if (m_Off < m_Standby || m_Off <= m_StandbyDesired )
            m_pStandbySlider->setValue(m_Off);
	if (m_Off == 0 && m_SuspendDesired > 0)
	    m_pSuspendSlider->setValue( m_SuspendDesired );
        else if (m_Off < m_Suspend || m_Off <= m_SuspendDesired )
            m_pSuspendSlider->setValue(m_Off);
	m_bMaintainSanity = true;
    }

    m_bChanged = true;
    emit changed(true);
}

void KEnergy::openURL(const TQString &URL)
{
      new KRun(KURL( URL ));
}

#include "energy.moc"