[go: up one dir, main page]

Menu

[12172e]: / src / aapm.cc  Maximize  Restore  History

Download this file

206 lines (172 with data), 4.6 kB

  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
/*
* IceWM
*
* Copyright (C) 1999 Andreas Hinz
*
* Mostly derrived from aclock.cc and aclock.h by Marko Macek
*
* Apm status
*/
#define NEED_TIME_H
#include "config.h"
#include "ylib.h"
#include "sysdep.h"
#include "aapm.h"
#include "aclock.h"
#include "yapp.h"
#include "prefs.h"
#include "intl.h"
#include <string.h>
#include <stdio.h>
#ifdef CONFIG_APPLET_APM
YColor *YApm::apmBg = 0;
YColor *YApm::apmFg = 0;
YFont *YApm::apmFont = 0;
void ApmStr(char *s, bool Tool) {
char buf[45];
int len, i, fd = open("/proc/apm", O_RDONLY);
char driver[16];
char apmver[16];
int apmflags;
int ACstatus;
int BATstatus;
int BATflag;
int BATlife;
int BATtime;
char units[16];
if (fd == -1) {
return ;
}
len = read(fd, buf, sizeof(buf) - 1);
close(fd);
buf[len] = 0;
if ((i = sscanf(buf, "%s %s 0x%x 0x%x 0x%x 0x%x %d%% %d %s",
driver, apmver, &apmflags,
&ACstatus, &BATstatus, &BATflag, &BATlife,
&BATtime, units)) != 9)
{
static int error = 1;
if (error) {
error = 0;
warn(_("/proc/apm - unknown format (%d)"), i);
}
return ;
}
if (BATlife == -1)
BATlife = 0;
if (!Tool) {
if (taskBarShowApmTime) { // mschy
if (BATtime == -1) {
// -1 indicates that apm-bios can't
// calculate time for akku
// no wonder -> we're plugged!
sprintf(s, "%02d", BATlife);
} else
sprintf(s, "%d:%02d", BATtime/60,BATtime%60);
} else
sprintf(s, "%02d", BATlife);
#if 0
while ((i < 3) && (buf[28 + i] != '%'))
i++;
for (len = 2; i; i--, len--) {
*(s + len) = buf[28 + i - 1];
}
#endif
} else {
sprintf(s, "%d%%", BATlife);
}
if (ACstatus == 0x1)
if (Tool)
strcat(s,_(" - Power"));
else
strcat(s,_("P"));
if ((BATflag & 8))
if (Tool)
strcat(s,_(" - Charging"));
else
strcat(s,_("M"));
}
YApm::YApm(YWindow *aParent): YWindow(aParent) {
if (apmBg == 0) apmBg = new YColor(clrApm);
if (apmFg == 0) apmFg = new YColor(clrApmText);
if (apmFont == 0) apmFont = YFont::getFont(apmFontName);
apmTimer = new YTimer(2000);
apmTimer->setTimerListener(this);
apmTimer->startTimer();
autoSize();
// setDND(true);
}
YApm::~YApm() {
delete apmTimer; apmTimer = 0;
}
void YApm::updateToolTip() {
char s[30]={' ',' ',' ', 0, 0, 0, 0};
ApmStr(s,1);
setToolTip(s);
}
void YApm::autoSize() {
int maxWidth=54;
if (!prettyClock) maxWidth += 4;
setSize(maxWidth, 20);
}
void YApm::paint(Graphics &g, int /*x*/, int /*y*/, unsigned int /*width*/, unsigned int /*height*/) {
unsigned int x = 0;
char s[30]={' ',' ',' ',0,0,0,0,0};
int len,i;
ApmStr(s,0); len=strlen(s);
if (prettyClock) {
YPixmap *p;
for (i = 0; x < width(); i++) {
if (i < len) {
p = getPixmap(s[i]);
} else p = PixSpace;
if (p) {
g.drawPixmap(p, x, 0);
x += p->width();
} else if (i >= len) {
g.setColor(apmBg);
g.fillRect(x, 0, width() - x, height());
break;
}
}
} else {
int y = (height() - 1 - apmFont->height()) / 2 + apmFont->ascent();
g.setColor(apmBg);
g.fillRect(0, 0, width(), 21);
g.setColor(apmFg);
g.setFont(apmFont);
g.drawChars(s, 0, len, 2, y);
}
}
bool YApm::handleTimer(YTimer *t) {
if (t != apmTimer) return false;
if (toolTipVisible())
updateToolTip();
repaint();
return true;
}
YPixmap *YApm::getPixmap(char c) {
YPixmap *pix = 0;
switch (c) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': pix = PixNum[c - '0']; break;
case ' ': pix = PixSpace; break;
case ':': pix = PixColon; break;
case '.': pix = PixDot; break;
case 'P': pix = PixP; break;
case 'M': pix = PixM; break;
}
return pix;
}
int YApm::calcWidth(const char *s, int count) {
if (!prettyClock) return apmFont->textWidth(s, count);
else {
int len = 0;
while (count--) {
YPixmap *p = getPixmap(*s++);
if (p) len += p->width();
}
return len;
}
}
#endif