-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathg510s-threads.c
268 lines (235 loc) · 7.68 KB
/
g510s-threads.c
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
/*
* This file is part of g510s.
*
* g510s 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 3 of the License, or (at your
* option) any later version.
*
* g510s 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 g510s; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA
*
* Copyright © 2015 John Augustine
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <libg15.h>
#include <libappindicator/app-indicator.h>
#include "g510s.h"
extern AppIndicator *indicator;
void *lcd_client_function(void *display) {
lcdnode_t *g15node = display;
lcd_t *client_lcd = g15node->lcd;
int retval;
unsigned int width, height, buflen, header = 4;
int client_sock = client_lcd->connection;
char helo[] = SERV_HELO;
unsigned char *tmpbuf = malloc(6880);
connected_clients++;
if (g15_send(client_sock, (char*)helo, strlen(SERV_HELO)) < 0) {
goto exitthread;
}
if (g15_recv(g15node, client_sock, (char*)tmpbuf, 4) < 4) {
goto exitthread;
}
if (tmpbuf[0] == 'G') {
while (leaving == 0) {
retval = g15_recv(g15node, client_sock, (char *)tmpbuf, 6880);
if (retval != 6880) {
break;
}
//pthread_mutex_lock(&lcdlist_mutex);
memset(client_lcd->buf, 0, 1024);
convert_buf(client_lcd, tmpbuf);
client_lcd->ident = random();
//pthread_mutex_unlock(&lcdlist_mutex);
}
} else if (tmpbuf[0] == 'R') {
while (leaving == 0) {
retval = g15_recv(g15node, client_sock, (char *)tmpbuf, 1048);
if (retval != 1048) {
break;
}
//pthread_mutex_lock(&lcdlist_mutex);
memcpy(client_lcd->buf, tmpbuf, sizeof(client_lcd->buf));
client_lcd->ident = random();
//pthread_mutex_unlock(&lcdlist_mutex);
}
} else if (tmpbuf[0] == 'W') {
while (leaving == 0) {
retval = g15_recv(g15node, client_sock, (char*)tmpbuf, 865);
if (!retval) {
break;
}
if (tmpbuf[2] & 1) {
width = ((unsigned char)tmpbuf[2] ^ 1) | (unsigned char)tmpbuf[3];
height = tmpbuf[4];
header = 5;
} else {
width = tmpbuf[2];
height = tmpbuf[3];
header = 4;
}
buflen = (width / 8) * height;
if (buflen > 860) {
retval = g15_recv(g15node, client_sock, NULL, buflen-860);
buflen = 860;
}
if (width != 160) {
goto exitthread;
}
//pthread_mutex_lock(&lcdlist_mutex);
memcpy(client_lcd->buf, tmpbuf + header, buflen + header);
client_lcd->ident = random();
//pthread_mutex_unlock(&lcdlist_mutex);
}
}
exitthread:
close(client_sock);
free(tmpbuf);
lcdnode_remove(display);
connected_clients--;
pthread_exit(NULL);
}
void *key_function(void *lcdlist) {
int keyreturn = 0;
unsigned int key = 0;
static unsigned int key_state = 0;
lcdlist_t *displaylist = (lcdlist_t*)(lcdlist);
while (!leaving) {
if (device_found) {
keyreturn = getPressedKeys(&key, 0);
// dont process normal keys
while (keyreturn == G15_ERROR_TRY_AGAIN) {
keyreturn = getPressedKeys(&key, 0);
}
// process extra keys
if ((keyreturn == G15_NO_ERROR) && (key != key_state)) {
current_key_state = key;
process_keys(displaylist, key, key_state);
key_state = key;
}
// handle hotplugging of keyboard or sound devices
if (keyreturn == -ENODEV) {
device_found = 0;
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ATTENTION);
exit_uinput();
exitLibG15();
while (!device_found) {
printf("G510s: device disconnected, retrying...\n");
if (setupLibG15(0x46d, 0xc22d, 0) == G15_NO_ERROR) {
printf("G510s: found device 046d:c22d\n");
device_found = 1;
} else if (setupLibG15(0x46d, 0xc22e, 0) == G15_NO_ERROR) {
printf("G510s: found device 046d:c22e\n");
device_found = 1;
}
sleep(1);
}
if (init_uinput() != 0) {
printf("G510s: failed to initialize uinput, media keys not available\n");
}
set_mkey_state(g510s_data.mkey_state);
if (displaylist->tail == displaylist->current) {
displaylist->current->lcd->ident = 0;
}
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
}
} else { // device was not found
// wait for a device
while (!device_found) {
printf("G510s: waiting for device...\n");
if (setupLibG15(0x46d, 0xc22d, 0) == G15_NO_ERROR) {
printf("G510s: found device 046d:c22d\n");
device_found = 1;
} else if (setupLibG15(0x46d, 0xc22e, 0) == G15_NO_ERROR) {
printf("G510s: found device 046d:c22e\n");
device_found = 1;
}
sleep(1);
}
if (init_uinput() != 0) {
printf("G510s: failed to initialize uinput, media keys not available\n");
}
set_mkey_state(g510s_data.mkey_state);
if (displaylist->tail == displaylist->current) {
displaylist->current->lcd->ident = 0;
}
app_indicator_set_status(indicator, APP_INDICATOR_STATUS_ACTIVE);
}
}
return NULL;
}
void *update_function(void *lcdlist) {
lcdlist_t *displaylist = (lcdlist_t*)(lcdlist);
static long int lastlcd = 1;
lcd_t *displaying = displaylist->tail->lcd;
memset(displaying->buf, 0, 1024);
displaying->ident = 0;
while (!leaving) {
if (device_found) {
// only update the color if the changes will be visible
if (update == g510s_data.mkey_state) {
switch (g510s_data.mkey_state) {
case 1:
setG510LEDColor(g510s_data.m1.red, g510s_data.m1.green, g510s_data.m1.blue);
break;
case 2:
setG510LEDColor(g510s_data.m2.red, g510s_data.m2.green, g510s_data.m2.blue);
break;
case 3:
setG510LEDColor(g510s_data.m3.red, g510s_data.m3.green, g510s_data.m3.blue);
break;
case 4:
setG510LEDColor(g510s_data.mr.red, g510s_data.mr.green, g510s_data.mr.blue);
break;
default:
printf("G510s: invalide mkey_state!!\n");
break;
}
update = 0;
}
displaying = displaylist->current->lcd;
if (displaylist->tail == displaylist->current) {
digital_clock(displaying);
}
if (displaying->ident != lastlcd) {
writePixmapToLCD(displaying->buf);
lastlcd = displaying->ident;
}
// we dont do anything here
if (displaying->state_changed) {
displaying->state_changed = 0;
}
}
usleep(50000);
}
return NULL;
}
void *server_function(void *lcdlist) {
lcdlist_t *displaylist = (lcdlist_t*)(lcdlist);
int g15_socket = -1;
if ((g15_socket = init_sockserver()) < 0) {
printf("G510s: unable to initialise server at port %i\n", LISTEN_PORT);
return NULL;
}
if (fcntl(g15_socket, F_SETFL, O_NONBLOCK) < 0) {
printf("G510s: unable to set socket to nonblocking\n");
}
while (!leaving) {
client_connect(&displaylist, g15_socket);
}
close(g15_socket);
return NULL;
}