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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
|
/*
* laptop_daemon.cpp
* Copyright (C) 1999 Paul Campbell <paul@taniwha.com>
*
* this replaces kcmlaptop - there are 2 parts - one is the panels that
* put the setup configuration in the "kcmlaptop" configm, the other
* is the laptop_daemon (this guy) who watches the battery state
* and does stuff as a result
*
* This file contains the implementation of the main laptop battery monitoring daemon
*
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <fcntl.h>
#include <qtimer.h>
#include <kconfig.h>
#include <klocale.h>
#include <kcmdlineargs.h>
#include "laptop_daemon.h"
#include "portable.h"
#include <kaboutdata.h>
#include <kaudioplayer.h>
#include <kapplication.h>
#include <sys/ioctl.h>
#include <kmessagebox.h>
#include <kpassivepopup.h>
#include <dcopclient.h>
#include <qsocketnotifier.h>
#include <qcursor.h>
#include <unistd.h>
#include <sys/time.h>
extern "C"
{
KDE_EXPORT KDEDModule *create_klaptopdaemon(const QCString& name) {
return new laptop_daemon(name);
}
}
class XWidget: public QWidget {
public:
XWidget(laptop_daemon *p):QWidget(0) { pd = p; }
private:
bool x11Event(XEvent *event);
laptop_daemon *pd;
};
bool XWidget::x11Event(XEvent *event) { return pd->x11Event(event); }
laptop_daemon::laptop_daemon(const QCString& obj): KDEDModule(obj)
{
xwidget = new XWidget(this);
xwidget->hide();
kapp->installX11EventFilter(xwidget);
mLavEnabled = false;
backoffTimer = 0;
need_wait = 0;
saved_brightness = 0;
saved_throttle = 0;
saved_performance = 0;
wake_timer = 0;
button_bright_saved=0;
button_bright_val=0;
button_saved_performance = 0;
button_saved_throttle = 0;
power_button_off = 0;
if (laptop_portable::has_brightness()) {
brightness = laptop_portable::get_brightness();
} else {
brightness = 0;
}
buttonThread.sethandle(this);
triggered[0] = 0;
triggered[1] = 0;
timer = 0;
dock_widget = 0;
oldTimer = 0;
sony_fd = -1;
sony_notifier = 0;
knownFullyCharged = 0;
sony_disp = 0;
connect(this, SIGNAL(signal_checkBattery()), SLOT(checkBatteryNow()));
//hasAudio = (audioServer.serverStatus() == 0) ? true : false;
// FIXME: make these configurable. Some system definitely don't
// use /var/run/stab
if (!access("/var/run/stab", R_OK|F_OK))
_pcmcia = new KPCMCIA(8, "/var/run/stab");
else if (!access("/var/lib/pcmcia/stab", R_OK|F_OK))
_pcmcia = new KPCMCIA(8, "/var/lib/pcmcia/stab");
else _pcmcia = NULL;
if (_pcmcia)
connect(_pcmcia, SIGNAL(cardUpdated(int)), this, SLOT(updatePCMCIA(int)));
connect( &autoLock, SIGNAL(timeout()), this, SLOT(timerDone()) );
}
laptop_daemon::~laptop_daemon()
{
delete xwidget;
delete _pcmcia;
delete dock_widget;
delete sony_notifier;
if (sony_disp)
XCloseDisplay(sony_disp);
}
void
laptop_daemon::quit()
{
deleteLater();
}
void laptop_daemon::restart()
{
if (oldTimer > 0) {
killTimer(oldTimer);
oldTimer=0;
}
if (timer) {
autoLock.stop();
timer = 0;
}
s.load();
if (s.has_brightness) {
brightness = laptop_portable::get_brightness();
} else {
brightness = 0;
}
if (!s.need_to_run()) {
quit();
return;
}
if (sony_fd < 0)
sony_fd = ::open("/dev/sonypi", O_RDONLY|O_NONBLOCK);
if (s.sony_enablescrollbar||s.sony_middleemulation) {
if (sony_disp == 0 && sony_fd >= 0)
sony_disp = XOpenDisplay(NULL);
if (sony_fd < 0 || sony_disp == 0) {
s.sony_enablescrollbar = 0;
s.sony_middleemulation = 0;
}
}
// change the dock state if necessary
if (s.enabled) {
if (!dock_widget) {
dock_widget = new laptop_dock(this);
dock_widget->setPCMCIA(_pcmcia);
dock_widget->show();
}
dock_widget->reload_icon();
dock_widget->SetupPopup();
} else {
if (dock_widget) {
delete dock_widget;
dock_widget = 0;
}
}
if (s.enable_lid_button && (lid_state = laptop_portable::get_button(laptop_portable::LidButton))) {
if (s.button_lid_bright_enabled)
SetBrightness(0, s.button_lid_bright_val);
if (s.button_lid_performance_enabled)
SetPerformance(s.button_lid_performance_val);
if (s.button_lid_throttle_enabled)
SetThrottle(s.button_lid_throttle_val);
switch (s.button_lid) {
case 1: invokeStandby();
break;
case 2: invokeSuspend();
break;
case 3: invokeHibernate();
break;
case 4: invokeLogout();
break;
case 5: invokeShutdown();
break;
}
}
if (s.enable_power_button && (power_state = laptop_portable::get_button(laptop_portable::PowerButton))) {
if (s.button_power_bright_enabled)
SetBrightness(0, s.button_power_bright_val);
if (s.button_power_performance_enabled)
SetPerformance(s.button_power_performance_val);
if (s.button_power_throttle_enabled)
SetThrottle(s.button_power_throttle_val);
switch (s.button_power) {
case 1: invokeStandby();
break;
case 2: invokeSuspend();
break;
case 3: invokeHibernate();
break;
case 4: invokeLogout();
break;
case 5: invokeShutdown();
break;
}
}
if (s.button_power_bright_enabled || s.button_power_performance_enabled || s.button_power_throttle_enabled ||
s.button_lid_bright_enabled || s.button_lid_performance_enabled || s.button_lid_throttle_enabled ||
s.button_lid != 0 || s.button_power != 0) { // need a fast thread to poll every sec
if (!buttonThread.running()) {
buttonThread.start();
}
} else {
if (buttonThread.running()) {
buttonThread.quit();
buttonThread.done();
}
}
// Do setup
struct power_result p = laptop_portable::poll_battery_state();
powered = p.powered;
need_wait = 0;
saved_brightness = 0;
saved_throttle = 0;
saved_performance = 0;
if (s.power_action[0] || s.power_action[1] || s.power_brightness_enabled[0] || s.power_brightness_enabled[0] ||
s.power_performance_enabled[0] || s.power_performance_enabled[1] || s.power_throttle_enabled[0] || s.power_throttle_enabled[1]) {
power_time = s.power_wait[powered?0:1];
timer = 1;
setLoadAverage(s.lav_enabled[powered?0:1], s.lav_val[powered?0:1]);
autoLock.setTimeout(power_time);
autoLock.start();
} else {
timer = 0;
}
if (s.useBlankSaver) {
setBlankSaver(!p.powered);
}
start_monitor();
// brightness control
if (s.has_brightness) {
if (s.bright_pon && powered) {
SetBrightness(0, s.bright_son);
} else
if (s.bright_poff && !powered) {
SetBrightness(0, s.bright_soff);
}
}
if (s.has_performance) {
if (s.performance_pon && powered) {
SetPerformance(s.performance_val_on);
} else
if (s.performance_poff && !powered) {
SetPerformance(s.performance_val_off);
}
}
if (s.has_throttle) {
if (s.throttle_pon && powered) {
SetThrottle(s.throttle_val_on);
} else
if (s.throttle_poff && !powered) {
SetThrottle(s.throttle_val_off);
}
}
// sony support
if (s.sony_enablescrollbar||s.sony_middleemulation) {
if (sony_notifier == 0) {
sony_notifier = new QSocketNotifier( sony_fd, QSocketNotifier::Read, this );
if (sony_notifier)
QObject::connect( sony_notifier, SIGNAL(activated(int)),
this, SLOT(sonyDataReceived()) );
}
} else {
if (sony_notifier) {
delete sony_notifier;
sony_notifier = 0;
}
}
}
void laptop_daemon::setBlankSaver(bool blanked)
{
QByteArray ba;
QDataStream ds(ba, IO_WriteOnly);
ds << bool(blanked);
// can't use kapp->dcopClient() because it breaks KUniqueApplication
DCOPClient c;
c.attach();
c.send("kdesktop", "KScreensaverIface", "setBlankOnly(bool)", ba);
c.detach();
}
void laptop_daemon::timerDone()
{
if (mLavEnabled && laptop_portable::get_load_average() >= mLav) {
autoLock.postpone(); // will call timerDone() again later
return;
}
int action;
timer = 0;
autoLock.stop(); // stop - see the note below about backoff
if (powered) {
action = s.power_action[0];
} else {
action = s.power_action[1];
}
switch (action) {
case 1: invokeStandby();
break;
case 2: invokeSuspend();
break;
case 3: invokeHibernate();
break;
}
if ((powered?s.power_brightness_enabled[0]:s.power_brightness_enabled[1])) {
need_wait = 1;
if (!saved_brightness) {
saved_brightness = 1;
saved_brightness_val = brightness;
}
SetBrightness(1, powered?s.power_brightness[0]:s.power_brightness[1]);
}
if ((powered?s.power_performance_enabled[0]:s.power_performance_enabled[1])) {
need_wait = 1;
if (!saved_performance) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_performance(1, current_profile, profile_list, active_list)) {
saved_performance = 1;
saved_performance_val = profile_list[current_profile];
}
}
SetPerformance(powered?s.power_performance[0]:s.power_performance[1]);
}
if ((powered?s.power_throttle_enabled[0]:s.power_throttle_enabled[1])) {
need_wait = 1;
if (!saved_throttle) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_throttling(1, current_profile, profile_list, active_list)) {
saved_throttle = 1;
saved_throttle_val = profile_list[current_profile];
}
}
SetPerformance(powered?s.power_throttle[0]:s.power_throttle[1]);
}
//
// we must give ourself enough time to handle any necessary evil before we start looking again
// (many of the above things cause unexpected time discontinuities)
//
if (need_wait) {
wakepos.setX(QCursor::pos().x());
wakepos.setY(QCursor::pos().y());
if (!wake_timer) {
wake_timer = new QTimer(this);
connect(wake_timer, SIGNAL(timeout()), this, SLOT(WakeCheck()));
wake_timer->start(1*1000, 1);
}
} else {
if (!backoffTimer) {
backoffTimer = new QTimer(this);
connect(backoffTimer, SIGNAL(timeout()), this, SLOT(BackoffRestart()));
backoffTimer->start(60*1000, 1);
}
}
}
void
laptop_daemon::BackoffRestart()
{
delete backoffTimer;
backoffTimer = 0;
if (!timer) {
timer = 1;
autoLock.start();
}
}
void
laptop_daemon::WakeCheck()
{
if (!wake_timer)
return;
if (!need_wait) {
wake_timer->stop();
delete wake_timer;
wake_timer = 0;
return;
}
if (wakepos.x() != QCursor::pos().x() || wakepos.y() != QCursor::pos().y()) {
wake_timer->stop();
delete wake_timer;
wake_timer = 0;
WakeUpAuto();
}
}
void
laptop_daemon::WakeUpAuto()
{
if (!need_wait)
return;
need_wait = 0;
if (saved_brightness) {
SetBrightness(0, saved_brightness_val);
saved_brightness = 0;
}
if (saved_throttle) {
SetThrottle(saved_throttle_val);
saved_throttle = 0;
}
if (saved_performance) {
SetPerformance(saved_performance_val);
saved_performance = 0;
}
if (!timer) {
timer = 1;
autoLock.start();
}
}
bool
laptop_daemon::x11Event(XEvent *event)
{
switch (event->type) {
case KeyPress:
case ButtonPress:
if (need_wait)
WakeUpAuto();
break;
}
return(0);
}
void laptop_daemon::dock_quit()
{
if (dock_widget)
delete dock_widget;
dock_widget = 0;
}
void laptop_daemon::updatePCMCIA(int num)
{
Q_UNUSED(num);
//kdDebug() << "PCMCIA card " << num << " was updated." << endl;
// Two things we do here. We provide notifications for cards
// being inserted / cards going to sleep / cards waking up
// and cards being safe to eject.
// The second thing we do is provide the desktop icon actions
// via dcop.
}
void laptop_daemon::haveBatteryLow(int t, const int num, const int type)
{
displayPixmap();
// beep if we are allowed to
if (s.systemBeep[t]) {
//KNotifyClient::beep();
(void)kapp->beep();
}
// run a command if we have to
if (s.runCommand[t]) {
// make sure the command exists
if (!s.runCommandPath[t].isEmpty()) {
KProcess command;
command << s.runCommandPath[t];
command.start(KProcess::DontCare);
}
}
if (s.do_brightness[t])
SetBrightness(0, s.val_brightness[t]);
if (s.do_throttle[t])
SetThrottle(s.val_throttle[t]);
if (s.do_performance[t])
SetPerformance(s.val_performance[t]);
// play a sound if we have to
if (s.playSound[t]) {
KAudioPlayer::play(s.playSoundPath[t]);
}
if (s.do_hibernate[t])
invokeHibernate();
if (s.do_suspend[t])
invokeSuspend();
if (s.do_standby[t])
invokeStandby();
if (s.logout[t])
invokeLogout();
if (s.shutdown[t])
invokeShutdown();
// notify if we must (must be last since it's synchronous)
if (s.notify[t]) {
if (type) {
if (s.time_based_action_critical) {
KPassivePopup::message(i18n("Battery power is running out."),
i18n("%1 % charge left.").arg(num),
BarIcon("laptop_battery"), dock_widget,
0, 20000);
} else {
// Will this ever be reached?
KPassivePopup::message(i18n("Battery power is running out."),
i18n("%1 % charge left.").arg(num),
BarIcon("laptop_battery"), dock_widget,
0, 20000);
}
}
else {
if (s.time_based_action_low) {
KPassivePopup::message(i18n("Battery power is running out."),
i18n("1 minute left.","%n minutes left.", num),
BarIcon("laptop_battery"), dock_widget,
0, 20000);
} else {
KPassivePopup::message(i18n("Battery power is running out."),
i18n("1% left.", "%n percent left.", num),
BarIcon("laptop_battery"), dock_widget,
0, 20000);
}
}
}
}
int laptop_daemon::calcBatteryTime(int percent, long time, bool restart)
{
#define MAX_SAMPLES_USED 3
static int percents[MAX_SAMPLES_USED];
static long times[MAX_SAMPLES_USED];
static int lastused=-1;
int r=-1;
if ( (lastused==-1) || restart )
{
percents[0]=percent;
times[0]=time;
lastused=0;
}
else
{
// Add the % and time to the arrays
// (or just update the time if the % hasn't changed)
if (percents[lastused]!=percent)
if (lastused!=MAX_SAMPLES_USED-1)
{
lastused++;
percents[lastused]=percent;
times[lastused]=time;
}
else
{
for (int i=1;i<MAX_SAMPLES_USED;i++)
{ percents[i-1]=percents[i]; times[i-1]=times[i]; };
}
percents[lastused]=percent;
times[lastused]=time;
//Now let's do the real calculations
if (lastused==0) return -1;
// Copy the data to temporary variables
double tp[MAX_SAMPLES_USED];
double tt[MAX_SAMPLES_USED];
for (int i=0;i<=lastused;i++)
{ tp[i]=percents[i]; tt[i]=times[i]; };
for (int c=lastused; c>1; c--)
{
for (int i=0; i<c-1; i++)
{
tp[i]=(tp[i]+tp[i+1])/2;
tt[i]=(tt[i]+tt[i+1])/2;
}
}
// Now we've reduced all the samples to an approximation with just a line
if (tp[1]-tp[0]!=0)
r=static_cast<int>(tt[0]-(tp[0]/(tp[1]-tp[0]))*(tt[1]-tt[0])-time);
}
return r;
}
void laptop_daemon::checkBatteryNow()
{
struct power_result p;
p = laptop_portable::poll_battery_state();
if (s.useBlankSaver && oldpowered != p.powered) {
setBlankSaver(!p.powered);
}
powered = p.powered;
left = p.time;
val = p.percentage;
if (oldpowered != powered && s.has_brightness) {
if (s.bright_pon && powered) {
SetBrightness(0, s.bright_son);
} else
if (s.bright_poff && !powered) {
SetBrightness(0, s.bright_soff);
}
if (s.performance_pon && powered) {
SetPerformance(s.performance_val_on);
} else
if (s.performance_poff && !powered) {
SetPerformance(s.performance_val_off);
}
if (s.throttle_pon && powered) {
SetThrottle(s.throttle_val_on);
} else
if (s.throttle_poff && !powered) {
SetThrottle(s.throttle_val_off);
}
}
if (left==-1) // Let's try to calculate the expected battery time left
{
timeval tv;
gettimeofday(&tv, 0);
left=calcBatteryTime(((powered)?100-val:val), tv.tv_sec, oldpowered!=powered );
}
if (timer && oldpowered != powered) {
need_wait = 0;
saved_brightness = 0;
saved_throttle = 0;
saved_performance = 0;
setLoadAverage(s.lav_enabled[powered?0:1], s.lav_val[powered?0:1]);
if (power_time != s.power_wait[powered?0:1]) {
power_time = s.power_wait[powered?0:1];
autoLock.stop();
autoLock.setTimeout(power_time);
autoLock.start();
}
}
if (!knownFullyCharged) {
knownFullyCharged = 1;
} else
if (s.notifyMeWhenFull && oldval != val && val == 100)
KMessageBox::queuedMessageBox(0, KMessageBox::Information, i18n("Your battery is now fully charged."), i18n("Laptop Battery"));
changed = oldpowered != powered||oldexists != s.exists||oldval != val || oldleft!=left;
oldpowered = powered;
oldexists = s.exists;
oldval = val;
oldleft = left;
if (changed)
displayPixmap();
}
void laptop_daemon::start_monitor()
{
checkBatteryNow();
displayPixmap();
oldTimer = startTimer(s.poll * 1000);
}
void laptop_daemon::setPollInterval(const int interval)
{
s.poll = interval;
// Kill any old timers that may be running
if (oldTimer > 0) {
killTimer(oldTimer);
// Start a new timer will the specified time
oldTimer = startTimer(interval * 1000);
emit(signal_checkBattery());
}
}
void laptop_daemon::timerEvent(QTimerEvent *)
{
emit(signal_checkBattery());
}
void laptop_daemon::displayPixmap()
{
if (s.have_time == 2 && s.exists && !powered) { // in some circumstances
s.have_time = (val < 0 ? 0 : 1); // the battery is not charging
KConfig *config = new KConfig("kcmlaptoprc");
if (config) {
config->setGroup("BatteryLow"); // we can;t figure this out 'till
config->writeEntry("HaveTime", s.have_time);
config->sync();
delete config;
}
}
if (dock_widget)
dock_widget->displayPixmap();
if (left >= 0) {
if (!triggered[0]) {
if (s.time_based_action_low) {
if (s.exists && !powered && left <= s.low[0]) {
triggered[0] = 1;
haveBatteryLow(0, left, 0);
}
} else {
if (s.exists && !powered && val <= s.low[1]) {
triggered[0] = 1;
haveBatteryLow(0, val, 0);
}
}
}
if (!triggered[1]) {
if (s.time_based_action_critical) {
if (s.exists && !powered && left <= s.low[2]) {
triggered[1] = 1;
haveBatteryLow(1, left, 0);
}
} else {
if (s.exists && !powered && val <= s.low[3]) {
triggered[1] = 1;
haveBatteryLow(1, val, 0);
}
}
}
}
if (s.time_based_action_low || s.time_based_action_critical) {
if (left > (s.low[2]+1))
triggered[1] = 0;
if (left > s.low[0])
triggered[0] = 0;
} else {
if (val > (s.low[3]+1))
triggered[1] = 0;
if (val > s.low[1])
triggered[0] = 0;
}
if (s.have_time != 1) {
if (!triggered[0]) {
if (s.exists && !powered && val <= s.low[0]) {
triggered[0] = 1;
haveBatteryLow(0, val, 1);
}
} else {
if (!triggered[1]) {
if (s.exists && !powered && val <= s.low[1]) {
triggered[1] = 1;
haveBatteryLow(1, val, 1);
}
}
if (val > (s.low[1]+1))
triggered[1] = 0;
if (val > s.low[0])
triggered[0] = 0;
}
}
}
void laptop_daemon::invokeStandby()
{
laptop_portable::invoke_standby();
}
void laptop_daemon::invokeSuspend()
{
laptop_portable::invoke_suspend();
}
void laptop_daemon::invokeHibernate()
{
laptop_portable::invoke_hibernation();
}
void laptop_daemon::invokeLogout()
{
bool rc = kapp->requestShutDown(KApplication::ShutdownConfirmNo, KApplication::ShutdownTypeNone, KApplication::ShutdownModeForceNow);
if (!rc)
KMessageBox::sorry(0, i18n("Logout failed."));
}
void laptop_daemon::invokeShutdown()
{
bool rc = kapp->requestShutDown(KApplication::ShutdownConfirmNo, KApplication::ShutdownTypeHalt, KApplication::ShutdownModeForceNow);
if (!rc)
KMessageBox::sorry(0, i18n("Shutdown failed."));
}
/*
* Portions of the following code borrowed with thanks from:
*
* Sony Programmable I/O Control Device driver for VAIO.
* Userspace X11 Daemon Utility
*
* Copyright 2001 Stelian Pop, Alcove
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
static void simulateButton(Display *disp, int button) {
XTestGrabControl(disp, True);
XTestFakeButtonEvent(disp, button, True, 0);
XTestFakeButtonEvent(disp, button, False, 0);
XSync(disp, False);
XTestGrabControl(disp, False);
}
static void simulateButtonDown(Display *disp, int button) {
XTestGrabControl(disp, True);
XTestFakeButtonEvent(disp, button, True, 0);
XSync(disp, False);
XTestGrabControl(disp, False);
}
static void simulateButtonUp(Display *disp, int button) {
XTestGrabControl(disp, True);
XTestFakeButtonEvent(disp, button, False, 0);
XSync(disp, False);
XTestGrabControl(disp, False);
}
#define SONYPI_EVENT_JOGDIAL_DOWN 1
#define SONYPI_EVENT_JOGDIAL_UP 2
#define SONYPI_EVENT_JOGDIAL_DOWN_PRESSED 3
#define SONYPI_EVENT_JOGDIAL_UP_PRESSED 4
#define SONYPI_EVENT_JOGDIAL_PRESSED 5
#define SONYPI_EVENT_JOGDIAL_RELEASED 6
void laptop_daemon::sonyDataReceived()
{
unsigned char event;
if (::read(sony_fd, &event, sizeof(event)) != sizeof(event))
return;
switch(event) {
case SONYPI_EVENT_JOGDIAL_UP:
if (sony_disp && s.sony_enablescrollbar) {
simulateButton(sony_disp, 4);
}
break;
case SONYPI_EVENT_JOGDIAL_DOWN:
if (sony_disp && s.sony_enablescrollbar) {
simulateButton(sony_disp, 5);
}
break;
case SONYPI_EVENT_JOGDIAL_PRESSED:
if (sony_disp && s.sony_middleemulation) {
simulateButtonDown(sony_disp, 2);
}
break;
case SONYPI_EVENT_JOGDIAL_RELEASED:
if (sony_disp && s.sony_middleemulation) {
simulateButtonUp(sony_disp, 2);
}
break;
default:
break;
}
}
void laptop_daemon::SetBrightness(bool blank, int v)
{
if (v < 0)
return;
brightness = v;
laptop_portable::set_brightness(blank, v);
}
void laptop_daemon::SetThrottle(QString v)
{
laptop_portable::set_system_throttling(v);
}
void laptop_daemon::SetPerformance(QString v)
{
laptop_portable::set_system_performance(v);
}
void
ButtonThread::run()
{
while (!quitting) {
handle->ButtonThreadInternals();
msleep(500); // have to run fast because if the power button is held down for too long
} // the system actually gets powered off
}
void
laptop_daemon::ButtonThreadInternals()
{
//
// the lid button turns stuff on when it's down and back off again when it's raised
// (kind of like the fridge door light)
//
if (s.enable_lid_button && lid_state != laptop_portable::get_button(laptop_portable::LidButton)) {
lid_state = !lid_state;
if (lid_state) {
if (s.button_lid_bright_enabled) {
if (!button_bright_val)
button_bright_val = brightness;
button_bright_saved = 1;
SetBrightness(1, s.button_lid_bright_val);
}
if (s.button_lid_performance_enabled) {
if (!button_saved_performance) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_performance(1, current_profile, profile_list, active_list)) {
button_saved_performance = 1;
button_saved_performance_val = profile_list[current_profile];
}
}
SetPerformance(s.button_lid_performance_val);
}
if (s.button_lid_throttle_enabled) {
if (!button_saved_throttle) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_throttling(1, current_profile, profile_list, active_list)) {
button_saved_throttle = 1;
button_saved_throttle_val = profile_list[current_profile];
}
}
SetThrottle(s.button_lid_throttle_val);
}
switch (s.button_lid) {
case 1: invokeStandby();
break;
case 2: invokeSuspend();
break;
case 3: invokeHibernate();
break;
case 4: invokeLogout();
break;
case 5: invokeShutdown();
break;
}
} else {
if (button_bright_saved) {
SetBrightness(0, button_bright_val);
button_bright_saved = 0;
}
if (button_saved_performance) {
button_saved_performance = 0;
SetPerformance( button_saved_performance_val);
}
if (button_saved_throttle) {
button_saved_throttle = 0;
SetThrottle(button_saved_throttle_val);
}
}
}
//
// the power button on the other hand is an off/on switch for non-suspend type ops
//
if (s.enable_power_button && power_state != laptop_portable::get_button(laptop_portable::PowerButton)) {
power_state = !power_state;
if (power_state) {
if (power_button_off) {
if (button_bright_saved) {
SetBrightness(0, button_bright_val);
button_bright_saved = 0;
}
if (button_saved_performance) {
button_saved_performance = 0;
SetPerformance( button_saved_performance_val);
}
if (button_saved_throttle) {
button_saved_throttle = 0;
SetThrottle(button_saved_throttle_val);
}
} else {
if (s.button_power_bright_enabled) {
if (!button_bright_val)
button_bright_val = brightness;
button_bright_saved = 1;
SetBrightness(1, s.button_power_bright_val);
}
if (s.button_power_performance_enabled) {
if (!button_saved_performance) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_performance(1, current_profile, profile_list, active_list)) {
button_saved_performance = 1;
button_saved_performance_val = profile_list[current_profile];
}
}
SetPerformance(s.button_power_performance_val);
}
if (s.button_power_throttle_enabled) {
if (!button_saved_throttle) {
QStringList profile_list;
int current_profile;
bool *active_list;
if (laptop_portable::get_system_throttling(1, current_profile, profile_list, active_list)) {
button_saved_throttle = 1;
button_saved_throttle_val = profile_list[current_profile];
}
}
SetThrottle(s.button_power_throttle_val);
}
}
switch (s.button_power) {
case 1: invokeStandby();
break;
case 2: invokeSuspend();
break;
case 3: invokeHibernate();
break;
case 4: invokeLogout();
break;
case 5: invokeShutdown();
break;
}
power_button_off = !power_button_off;
}
}
}
#include "laptop_daemon.moc"
|