summaryrefslogtreecommitdiffstats
path: root/tray/main.cpp
blob: 52f75f35c72884c7bb6dc0f569b092bd1d28d973 (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
/*
 *   Copyright (C) 2010-2015 Michael Buesch <m@bues.ch>
 *
 *   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.
 */

#include "main.h"
#include "backend.h"
#include "util.h"

#include <QtGui/QApplication>
#include <QtGui/QMessageBox>
#include <QtGui/QGridLayout>
#include <QtGui/QCursor>

#include <iostream>

#include <stdint.h>
#include <sys/signal.h>


using namespace std;


TrayWindow::TrayWindow(TrayIcon *_tray)
 : QMenu(NULL)
 , tray (_tray)
 , blockBrightnessChange (false)
 , realBrightnessMinVal (0)
{
	struct pt_message m;
	int err;
	QLabel *label;
	QGridLayout *l = new QGridLayout(this);
	setLayout(l);

	label = new QLabel("LCD:", this);
	l->addWidget(label, 0, 0);
	brightness = new QSlider(this);
	brightness->setOrientation(Qt::Horizontal);
	l->addWidget(brightness, 0, 1, 1, 2);

	brAutoAdj = new QCheckBox("Auto", this);
	l->addWidget(brAutoAdj, 1, 1);

	brAutoAdjAC = new QCheckBox("Auto on AC", this);
	l->addWidget(brAutoAdjAC, 1, 2);

	battLabel = new QLabel(this);
	l->addWidget(battLabel, 3, 0);
	battBar = new QProgressBar(this);
	l->addWidget(battBar, 3, 1, 1, 2);

	update();

	err = tray->getBackend()->getBacklightState(&m);
	if (err) {
		cerr << "Failed to fetch initial backlight state" << endl;
	} else {
		bool autodim = !!(m.bl_stat.flags & htonl(PT_BL_FLG_AUTODIM));
		bool autoac = !!(m.bl_stat.flags & htonl(PT_BL_FLG_AUTODIM_AC));
		brAutoAdj->setCheckState(autodim ? Qt::Checked : Qt::Unchecked);
		brAutoAdjAC->setCheckState(autoac ? Qt::Checked : Qt::Unchecked);
		brightness->setValue(ntohl(m.bl_stat.brightness));
		//TODO slider min/max?
		updateBacklightToolTip(autodim);
	}

	connect(brightness, SIGNAL(valueChanged(int)),
		this, SLOT(desiredBrightnessChanged(int)));

	connect(brAutoAdj, SIGNAL(stateChanged(int)),
		this, SLOT(brightnessAutoAdjChanged(int)));
	connect(brAutoAdjAC, SIGNAL(stateChanged(int)),
		this, SLOT(brightnessAutoAdjChanged(int)));
}

TrayWindow::~TrayWindow()
{
}

void TrayWindow::updateBacklightToolTip(bool autodim)
{
	if (autodim)
		tray->setBacklightToolTip("LCD in autodim mode");
	else
		tray->setBacklightToolTip("");
}

void TrayWindow::brightnessAutoAdjChanged(int unused)
{
	bool on = (brAutoAdj->checkState() == Qt::Checked);
	bool on_ac = (brAutoAdjAC->checkState() == Qt::Checked);
	int err;
	int minval, maxval, range, val;

	if (blockBrightnessChange)
		return;

	if (on) {
		/* Auto-adjust on */
		minval = realBrightnessMinVal;
		maxval = brightness->maximum();
		range = maxval - minval;
		val = brightness->value();
		if (range) {
			val = div_round(static_cast<int64_t>(val) * 100,
					static_cast<int64_t>(range));
		} else
			val = 0;
		err = tray->getBackend()->setBacklightAutodim(true, on_ac, val);
		if (err)
			cerr << "Failed to enable auto-dimming" << endl;
		brAutoAdjAC->setEnabled(true);
	} else {
		/* Auto-adjust off */
		err = tray->getBackend()->setBacklightAutodim(false, on_ac, 0);
		if (err)
			cerr << "Failed to disable auto-dimming" << endl;
		brAutoAdjAC->setEnabled(false);
	}
	updateBacklightSlider();
	updateBacklightToolTip(on);
}

void TrayWindow::update()
{
	updateBacklightSlider();
	updateBattBar();
}

void TrayWindow::updateBattBar(struct pt_message *msg)
{
	struct pt_message m;
	int err;
	unsigned int minval, maxval, curval;
	unsigned int range, percent;
	QString battText("No bat. info");

	if (!msg) {
		err = tray->getBackend()->getBatteryState(&m);
		if (err) {
			cerr << "Failed to fetch battery state" << endl;
			return;
		}
		msg = &m;
	}

	minval = ntohl(msg->bat_stat.min_level);
	maxval = ntohl(msg->bat_stat.max_level);
	curval = ntohl(msg->bat_stat.level);
	if (minval == maxval) {
		/* No batt info */
		minval = 0;
		maxval = 1;
		curval = 0;
	} else {
		battText = "Bat.";
		if (msg->bat_stat.flags & htonl(PT_BAT_FLG_ACUNKNOWN))
			battText += " / AC?";
		else if (msg->bat_stat.flags & htonl(PT_BAT_FLG_ONAC))
			battText += " / AC";
		if (msg->bat_stat.flags & htonl(PT_BAT_FLG_CHUNKNOWN))
			battText += " / charg.?";
		else if (msg->bat_stat.flags & htonl(PT_BAT_FLG_CHARGING))
			battText += " / charg.";
		battText += ":";
	}
	battBar->setMinimum(minval);
	battBar->setMaximum(maxval);
	battBar->setValue(curval);
	battLabel->setText(battText);

	range = maxval - minval;
	if (range)
		percent = div_round(static_cast<uint64_t>(curval - minval) * 100,
				    static_cast<uint64_t>(range));
	else
		percent = 0;
	tray->setBatteryToolTip(QString("%1 %2%").arg(battText).arg(percent));
}

void TrayWindow::updateBacklightSlider(struct pt_message *msg)
{
	struct pt_message m;
	int err;
	unsigned int step, flags, val, minval, maxval;

	if (!msg) {
		err = tray->getBackend()->getBacklightState(&m);
		if (err) {
			cerr << "Failed to fetch backlight state" << endl;
			return;
		}
		msg = &m;
	}

	step = ntohl(msg->bl_stat.brightness_step);
	flags = ntohl(msg->bl_stat.flags);
	val = ntohl(msg->bl_stat.brightness);
	minval = ntohl(msg->bl_stat.min_brightness);
	maxval = ntohl(msg->bl_stat.max_brightness);
	/* Force minval to >= 1%, but save the original minval. */
	realBrightnessMinVal = minval;
	if (flags & PT_BL_FLG_AUTODIM)
		minval = max(minval, 1u);
	else
		minval = div_round((maxval - minval) * 1, 100) + minval;
	minval = round_up(minval, step);

	blockBrightnessChange = true;

	brightness->setSingleStep(step);
	brightness->setMinimum(minval);
	brightness->setMaximum(maxval);
	brightness->setValue(val);

	blockBrightnessChange = false;
}

void TrayWindow::desiredBrightnessChanged(int newVal)
{
	bool autodim = (brAutoAdj->checkState() == Qt::Checked);
	bool autodim_ac = (brAutoAdjAC->checkState() == Qt::Checked);
	int err;

	if (blockBrightnessChange)
		return;

	if (autodim) {
		err = tray->getBackend()->setBacklightAutodim(true, autodim_ac, newVal);
		if (err)
			cerr << "Failed to set autodim max" << endl;
	} else {
		tray->getBackend()->setBacklight(newVal);
	}
}

void TrayWindow::showEvent(QShowEvent *event)
{
	QPoint cursor(QCursor::pos());
	int x = cursor.x() - width();
	int y = cursor.y() - height();
	move(max(x, 0), max(y, 0));
	QMenu::showEvent(event);
}

TrayIcon::TrayIcon()
 : window (NULL)
 , backend (NULL)
{
	setIcon(QIcon(stringify(PREFIX) "/share/pwrtray/bulb.png"));
}

TrayIcon::~TrayIcon()
{
	delete window;
	delete backend;
}

bool TrayIcon::init()
{
	int err;

	backend = new Backend();
	err = backend->connectToBackend();
	if (err) {
		delete backend;
		backend = NULL;
		return false;
	}

	window = new TrayWindow(this);

	connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
		this, SLOT(wasActivated(QSystemTrayIcon::ActivationReason)));
	connect(backend, SIGNAL(batteryStateChanged(struct pt_message *)),
		this, SLOT(batteryStateChanged(struct pt_message *)));
	connect(backend, SIGNAL(backlightStateChanged(struct pt_message *)),
		this, SLOT(backlightStateChanged(struct pt_message *)));

	return true;
}

void TrayIcon::setBacklightToolTip(const QString &text)
{
	backlightToolTip = text;
	updateToolTip();
}

void TrayIcon::setBatteryToolTip(const QString &text)
{
	batteryToolTip = text;
	updateToolTip();
}

void TrayIcon::updateToolTip()
{
	QString tt(backlightToolTip);
	if (!tt.isEmpty())
		tt += "\n";
	tt += batteryToolTip;
	setToolTip(tt);
}

void TrayIcon::wasActivated(ActivationReason reason)
{
	window->show();
}

void TrayIcon::batteryStateChanged(struct pt_message *msg)
{
	window->updateBattBar(msg);
}

void TrayIcon::backlightStateChanged(struct pt_message *msg)
{
	window->updateBacklightSlider(msg);
}

static bool waitForSystray()
{
	unsigned int count = 0;

	cout << "PwrTray: Waiting for systray ..." << flush;
	while (!QSystemTrayIcon::isSystemTrayAvailable()) {
		if (count++ >= 240) {
			QMessageBox::critical(NULL, "PwrTray: No systray found",
					      "PwrTray: Could not find a systray.");
			return false;
		}
		msleep(500);
		cout << "." << flush;
	}
	cout << " found" << endl;

	return true;
}

static void signal_terminate(int signal)
{
	cout << "Terminating signal received." << endl;
	QApplication::exit(0);
}

static int install_sighandler(int signal, void (*handler)(int))
{
	struct sigaction act;

	memset(&act, 0, sizeof(act));
	sigemptyset(&act.sa_mask);
	act.sa_flags = 0;
	act.sa_handler = handler;

	return sigaction(signal, &act, NULL);
}

static int setup_signal_handlers(void)
{
	int err = 0;

	err |= install_sighandler(SIGINT, signal_terminate);
	err |= install_sighandler(SIGTERM, signal_terminate);

	return err ? -1 : 0;
}

int main(int argc, char **argv)
{
	int err;

	QApplication app(argc, argv);
	err = setup_signal_handlers();
	if (err) {
		cerr << "Failed to initilize signal handlers" << endl;
		return 1;
	}
	if (!waitForSystray())
		return 1;
	TrayIcon icon;
	icon.show();
	if (!icon.init())
		return 1;
	return app.exec();
}

#include "moc/main.moc"
bues.ch cgit interface