summaryrefslogtreecommitdiffstats
path: root/tray/backend.cpp
blob: a814398a167305e4c536a187e453426138ff891d (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
/*
 *   Copyright (C) 2010 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 "backend.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>

#include <iostream>

#include <QApplication>

using namespace std;


Backend::Backend()
 : fd (-1)
 , notifier (NULL)
 , errcount (0)
{
}

Backend::~Backend()
{
	struct pt_message msg;
	int err;

	if (fd != -1) {
		::memset(&msg, 0, sizeof(msg));
		msg.id = htons(PTREQ_XEVREP);
		err = sendMessageSyncReply(&msg);
		if (err)
			cerr << "Failed to disable X11 input event notifications" << endl;

		close(fd);
	}
}

int Backend::connectToBackend()
{
	struct sockaddr_un sockaddr;
	int err;
	struct pt_message msg;

	if (fd >= 0)
		return 0;

	fd = socket(AF_UNIX, SOCK_STREAM, 0);
	if (fd == -1) {
		cerr << "Failed to create backend socket: "
		     << strerror(errno) << endl;
		goto error;
	}
	sockaddr.sun_family = AF_UNIX;
	strncpy(sockaddr.sun_path, PT_SOCKET, sizeof(sockaddr.sun_path) - 1);
	err = ::connect(fd, (struct sockaddr *)&sockaddr, SUN_LEN(&sockaddr));
	if (err) {
		cerr << "Failed to connect to backend socket: "
		     << strerror(errno) << endl;
		goto err_close;
	}

	::memset(&msg, 0, sizeof(msg));
	msg.id = htons(PTREQ_PING);
	err = sendMessageSyncReply(&msg);
	if (err) {
		cerr << "Failed to send ping to backend" << endl;
		goto err_close;
	}

	cout << "Connected to backend" << endl;

	notifier = new QSocketNotifier(fd, QSocketNotifier::Read, this);
	connect(notifier, SIGNAL(activated(int)),
		this, SLOT(readNotification(int)));

	::memset(&msg, 0, sizeof(msg));
	msg.id = htons(PTREQ_WANT_NOTIFY);
	msg.flags = htons(PT_FLG_ENABLE);
	err = sendMessageSyncReply(&msg);
	if (err) {
		cerr << "Failed to enable backend notifications" << endl;
		goto err_free_noti;
	}

	::memset(&msg, 0, sizeof(msg));
	msg.id = htons(PTREQ_XEVREP);
	msg.flags = htons(PT_FLG_ENABLE);
	err = sendMessageSyncReply(&msg);
	if (err)
		cerr << "Failed to enable X11 input event notifications" << endl;

	return 0;

err_free_noti:
	delete notifier;
	notifier = NULL;
err_close:
	close(fd);
	fd = -1;
error:
	return -1;
}

int Backend::sendMessage(struct pt_message *msg)
{
	ssize_t ret;
	size_t count, pos = 0;

	msg->flags |= htons(PT_FLG_OK);
	count = sizeof(*msg);
	while (count) {
		ret = ::send(fd, reinterpret_cast<uint8_t *>(msg) + pos,
			     count, 0);
		if (ret < 0)
			return -1;
		count -= ret;
		pos += ret;
	}

	return 0;
}

int Backend::sendMessageSyncReply(struct pt_message *msg)
{
	uint16_t id = msg->id;
	int err;
	QList<struct pt_message> msgs;

	err = sendMessage(msg);
	if (err)
		return err;
	while (1) {
		err = recvMessage(msg);
		if (err)
			return err;
		if ((msg->id != id) || !(msg->flags & htons(PT_FLG_REPLY))) {
			msgs.append(*msg);
			continue;
		}
		break;
	}

	QList<struct pt_message>::iterator i;
	for (i = msgs.begin(); i != msgs.end(); ++i)
		processReceivedMessage(&(*i));

	if (!(msg->flags & htons(PT_FLG_OK)))
		return -ETXTBSY;

	return 0;
}

int Backend::recvMessage(struct pt_message *msg)
{
	ssize_t count;
	size_t pos = 0;

	::memset(msg, 0, sizeof(*msg));
	do {
		count = ::recv(fd, reinterpret_cast<uint8_t *>(msg) + pos,
			       sizeof(*msg) - pos, 0);
		if (count < 0)
			return -1;
		pos += count;
	} while (count != 0 && pos != sizeof(*msg));
	if (pos != sizeof(*msg))
		return -1;

	return 0;
}

void Backend::processReceivedMessage(struct pt_message *msg)
{
	switch (ntohs(msg->id)) {
	case PTNOTI_SRVDOWN:
		cout << "Backend server is going down." << endl;
		QApplication::instance()->exit(1);
		break;
	case PTNOTI_BL_CHANGED:
		emit backlightStateChanged(msg);
		break;
	case PTNOTI_BAT_CHANGED:
		emit batteryStateChanged(msg);
		break;
	default:
		cerr << "Received unknown message: " << ntohs(msg->id) << endl;
	}
}

void Backend::checkErrorCount()
{
	if (errcount < 25)
		return;
	cerr << "Too many backend errors. Is the backend dead?" << endl;
	QApplication::instance()->exit(1);
}

void Backend::readNotification(int sock)
{
	struct pt_message msg;
	int err;

	err = recvMessage(&msg);
	if (err) {
		cerr << "Got read notification, but failed to read message" << endl;
		errcount++;
		checkErrorCount();
		return;
	}
	errcount = 0;
	processReceivedMessage(&msg);
}

int Backend::getBatteryState(struct pt_message *msg)
{
	int err;

	::memset(msg, 0, sizeof(*msg));
	msg->id = htons(PTREQ_BAT_GETSTATE);
	err = sendMessageSyncReply(msg);

	return err;
}

int Backend::getBacklightState(struct pt_message *msg)
{
	int err;

	::memset(msg, 0, sizeof(*msg));
	msg->id = htons(PTREQ_BL_GETSTATE);
	err = sendMessageSyncReply(msg);

	return err;
}

int Backend::setBacklight(int value)
{
	struct pt_message msg;

	::memset(&msg, 0, sizeof(msg));
	msg.id = htons(PTREQ_BL_SETBRIGHTNESS);
	msg.bl_set.brightness = value;

	return sendMessageSyncReply(&msg);
}

int Backend::setBacklightAutodim(bool enable, bool enable_on_ac, int max_percent)
{
	struct pt_message msg;

	::memset(&msg, 0, sizeof(msg));
	msg.id = htons(PTREQ_BL_AUTODIM);
	msg.bl_autodim.flags = enable ? htonl(PT_AUTODIM_FLG_ENABLE) : 0;
	msg.bl_autodim.flags |= enable_on_ac ? htonl(PT_AUTODIM_FLG_ENABLE_AC) : 0;
	msg.bl_autodim.max_percent = htonl(max_percent);

	return sendMessageSyncReply(&msg);
}

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