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
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2014 by Ilia Sergachev
*
* 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 software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <linux/reboot.h>
#include <stdbool.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/limits.h>
#include <sys/mman.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "qdbmp.h"
#define MIN_TIME 1395606821
#define TIME_FILE "/data/time_store"
#define TIME_CHECK_PERIOD 60 /* seconds */
#define VOLD_LINK "/data/vold"
#define PLAYER_FILE "/data/chosen_player"
#define NOASK_FLAG "/data/no_ask_once"
#define POLL_MS 10
#define KEYCODE_HEADPHONES 114
#define KEYCODE_HOLD 115
#define KEYCODE_PWR 116
#define KEYCODE_PWR_LONG 117
#define KEYCODE_SD 143
#define KEYCODE_VOLPLUS 158
#define KEYCODE_VOLMINUS 159
#define KEYCODE_PREV 160
#define KEYCODE_NEXT 162
#define KEYCODE_PLAY 161
#define KEY_HOLD_OFF 16
void checktime()
{
time_t t_stored=0, t_current=time(NULL);
FILE *f = fopen(TIME_FILE, "r");
if(f!=NULL)
{
fscanf(f, "%ld", &t_stored);
fclose(f);
}
printf("stored time: %ld, current time: %ld\n", t_stored, t_current);
if(t_stored<MIN_TIME)
t_stored=MIN_TIME;
if(t_stored<t_current)
{
f = fopen(TIME_FILE, "w");
fprintf(f, "%ld", t_current);
fclose(f);
}
else
{
t_stored += TIME_CHECK_PERIOD;
struct tm *t = localtime(&t_stored);
struct timeval tv = {mktime(t), 0};
settimeofday(&tv, 0);
}
}
static struct pollfd *ufds;
static char **device_names;
static int nfds;
static int open_device(const char *device, int print_flags)
{
int fd;
struct pollfd *new_ufds;
char **new_device_names;
fd = open(device, O_RDWR);
if(fd < 0)
{
fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
return -1;
}
new_ufds = realloc(ufds, sizeof(ufds[0]) * (nfds + 1));
if(new_ufds == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
ufds = new_ufds;
new_device_names = realloc(device_names, sizeof(device_names[0]) * (nfds + 1));
if(new_device_names == NULL)
{
fprintf(stderr, "out of memory\n");
return -1;
}
device_names = new_device_names;
ufds[nfds].fd = fd;
ufds[nfds].events = POLLIN;
device_names[nfds] = strdup(device);
nfds++;
return 0;
}
static int scan_dir(const char *dirname, int print_flags)
{
char devname[PATH_MAX];
char *filename;
DIR *dir;
struct dirent *de;
dir = opendir(dirname);
if(dir == NULL)
return -1;
strcpy(devname, dirname);
filename = devname + strlen(devname);
*filename++ = '/';
while((de = readdir(dir)))
{
if(de->d_name[0] == '.' &&
(de->d_name[1] == '\0' ||
(de->d_name[1] == '.' && de->d_name[2] == '\0')))
continue;
strcpy(filename, de->d_name);
open_device(devname, print_flags);
}
closedir(dir);
return 0;
}
void button_init_device(void)
{
int res;
int print_flags = 0;
const char *device = NULL;
const char *device_path = "/dev/input";
nfds = 1;
ufds = calloc(1, sizeof(ufds[0]));
ufds[0].fd = inotify_init();
ufds[0].events = POLLIN;
if(device)
{
res = open_device(device, print_flags);
if(res < 0) {
fprintf(stderr, "open device failed\n");
}
}
else
{
res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE);
if(res < 0)
{
fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno));
}
res = scan_dir(device_path, print_flags);
if(res < 0)
{
fprintf(stderr, "scan dir failed for %s\n", device_path);
}
}
}
int draw()
{
int fbfd = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize = 0;
char *fbp = 0;
int x = 0, y = 0;
long int location = 0;
/* Open the file for reading and writing */
fbfd = open("/dev/graphics/fb0", O_RDWR);
if (fbfd == -1)
{
perror("Error: cannot open framebuffer device");
exit(1);
}
/* Get fixed screen information */
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1)
{
perror("Error reading fixed information");
exit(2);
}
/* Get variable screen information */
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1)
{
perror("Error reading variable information");
exit(3);
}
/* Figure out the size of the screen in bytes */
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
vinfo.xres = vinfo.xres_virtual = vinfo.width = 320;
vinfo.yres = vinfo.yres_virtual = vinfo.height = 240;
vinfo.xoffset = vinfo.yoffset = vinfo.sync = vinfo.vmode = 0;
vinfo.pixclock = 104377;
vinfo.left_margin = 20;
vinfo.right_margin = 50;
vinfo.upper_margin = 2;
vinfo.lower_margin = 4;
vinfo.hsync_len = 10;
vinfo.vsync_len = 2;
vinfo.red.offset = 11;
vinfo.red.length = 5;
vinfo.red.msb_right = 0;
vinfo.green.offset = 5;
vinfo.green.length = 6;
vinfo.green.msb_right = 0;
vinfo.blue.offset = 0;
vinfo.blue.length = 5;
vinfo.blue.msb_right = 0;
vinfo.transp.offset = vinfo.transp.length = vinfo.transp.msb_right = 0;
vinfo.nonstd = 4;
if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo))
{
perror("fbset(ioctl)");
exit(4);
}
/* Map the device to memory */
fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED,
fbfd, 0);
if ((int)fbp == -1)
{
perror("Error: failed to map framebuffer device to memory");
exit(4);
}
BMP* bmp = BMP_ReadFile("/system/rockbox/chooser.bmp");
BMP_CHECK_ERROR( stderr, -1 );
UCHAR r, g, b;
unsigned short int t;
for (y = 0; y < 240; y++)
for (x = 0; x < 320; x++)
{
location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
(y+vinfo.yoffset) * finfo.line_length;
BMP_GetPixelRGB(bmp, x, y, &r, &g, &b);
t = (r>>3)<<11 | (g>>2) << 5 | (b>>3);
*((unsigned short int*)(fbp + location)) = t;
}
BMP_Free( bmp );
munmap(fbp, screensize);
close(fbfd);
return 0;
}
int choose_player()
{
int i;
int res;
struct input_event event;
while(true)
{
poll(ufds, nfds, POLL_MS);
for(i = 1; i < nfds; i++)
{
if(ufds[i].revents & POLLIN)
{
res = read(ufds[i].fd, &event, sizeof(event));
if(res < (int)sizeof(event))
{
fprintf(stderr, "could not get event\n");
}
if(event.type==1)
{
if(event.code==KEYCODE_NEXT)
{
puts("rockbox!");
return 1;
}
else if(event.code==KEYCODE_PREV)
{
puts("mango!");
return 0;
}
else if(event.code==KEYCODE_PWR || event.code==KEYCODE_PWR_LONG)
{
reboot(LINUX_REBOOT_CMD_POWER_OFF);
}
}
else if(event.type==3)
{
if(event.code==53) //x coord
{
if(event.value<160)
{
puts("mango!");
return 0;
}
else
{
puts("rockbox!");
return 1;
}
}
}
}
}
}
return true;
}
bool check_for_hold()
{
FILE *f = fopen("/sys/class/axppower/holdkey", "r");
char x;
fscanf(f, "%c", &x);
fclose(f);
if(x & KEY_HOLD_OFF)
return false;
else
return true;
}
int main(int argc, char **argv)
{
FILE *f;
int last_chosen_player = -1;
f = fopen(PLAYER_FILE, "r");
if(f!=NULL)
{
fscanf(f, "%d", &last_chosen_player);
fclose(f);
}
bool ask = (access(VOLD_LINK, F_OK) == -1) || ((access(NOASK_FLAG, F_OK) == -1) && check_for_hold()) || (last_chosen_player==-1);
if(ask)
{
draw();
button_init_device();
int player_chosen_now = choose_player();
if(last_chosen_player!=player_chosen_now)
{
f = fopen(PLAYER_FILE, "w");
fprintf(f, "%d", player_chosen_now);
fclose(f);
}
if(last_chosen_player!=player_chosen_now || (access(VOLD_LINK, F_OK) == -1))
{
system("rm "VOLD_LINK);
if(player_chosen_now)
system("ln -s /system/bin/vold_rockbox "VOLD_LINK);
else
system("ln -s /system/bin/vold_original "VOLD_LINK);
system("touch "NOASK_FLAG);
system("reboot");
}
last_chosen_player = player_chosen_now;
}
system("rm "NOASK_FLAG);
while(1)
{
if(last_chosen_player)
{
// system("/system/bin/openadb");
system("/system/rockbox/lib/rockbox");
}
else
// system("/system/bin/closeadb");
system("/system/bin/MangoPlayer_original");
sleep(1);
}
return 0;
}
|