forked from iPodLinux/podzilla0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pz.c
501 lines (424 loc) · 10.5 KB
/
pz.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*
* Copyright (C) 2004 Bernard Leach
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "pz.h"
#include "browser.h"
#include "slider.h"
#include "ipod.h"
#include "vectorfont.h"
#ifdef MPDC
#include "mpdc/mpdc.h"
#endif
/* globals */
GR_SCREEN_INFO screen_info;
int hw_version;
GR_WINDOW_ID root_wid;
GR_GC_ID root_gc;
static GR_TIMER_ID backlight_tid;
static GR_TIMER_ID startupcontrast_tid;
static GR_TIMER_ID battery_tid;
struct pz_window {
GR_WINDOW_ID wid;
void (*draw)(void);
int (*keystroke)(GR_EVENT * event);
};
static struct pz_window windows[10];
static int n_opened = 0;
/* int BACKLIGHT_TIMER = 10; // in seconds */
#ifdef IPOD
static unsigned long int last_button_event = 0;
static unsigned long int wheel_evt_count = 0;
static int startup_contrast = -1;
#endif
static unsigned long int last_keypress_event = 0;
int battery_is_charging = 0;
static int low_battery_count = 0;
int usb_connected = 0;
int old_usb_connected = 0;
static int usb_dialog_open = 0;
int fw_connected = 0;
int old_fw_connected = 0;
static int fw_dialog_open = 0;
#define WHEEL_EVT_MOD 3
static int hold_is_on = 0;
extern void new_menu_window();
extern void load_font();
extern void beep(void);
extern void header_update_hold_status( int curr );
void header_timer_update( void );
void poweroff_ipod(void)
{
ipod_touch_settings();
#ifdef MPDC
mpdc_destroy();
#endif
GrClose();
printf("\nPowering down.\nPress action to power on.\n");
execl("/bin/poweroff", "poweroff", NULL);
printf("No poweroff binary available. Rebooting.\n");
execl("/bin/reboot", "reboot", NULL);
exit(0);
}
void reboot_ipod(void)
{
ipod_touch_settings();
#ifdef MPDC
mpdc_destroy();
#endif
GrClose();
execl("/bin/reboot", "reboot", NULL);
exit(0);
}
void quit_podzilla(void)
{
ipod_touch_settings();
#ifdef MPDC
mpdc_destroy();
#endif
GrClose();
exit(0);
}
void set_wheeldebounce(void)
{
new_settings_slider_window(_("Wheel Sensitivity"),
WHEEL_DEBOUNCE, 1, 20);
}
void set_buttondebounce(void)
{
new_settings_slider_window(_("Action Debounce"),
ACTION_DEBOUNCE, 100, 500);
}
/* handles default functions for unused keystrokes */
void pz_default_handler(GR_EVENT *event)
{
switch (event->type) {
case GR_EVENT_TYPE_KEY_DOWN:
switch (event->keystroke.ch) {
#ifdef MPDC
case 'f':
case '4':
mpdc_next(mpdz);
break;
case 'w':
case '5':
mpdc_prev(mpdz);
break;
case 'd':
case '1':
mpdc_playpause(mpdz);
break;
#endif /* MPDC */
default:
break;
}
break;
}
}
static inline struct pz_window *pz_find_window(GR_WINDOW_ID wid)
{
int i;
for (i = 0; i < n_opened; i++)
if (windows[i].wid == wid && wid != GR_ROOT_WINDOW_ID)
return &windows[i];
return NULL;
}
void pz_event_handler(GR_EVENT *event)
{
int ret = 0;
unsigned long int curtime;
struct timeval cur_st;
struct pz_window *window;
gettimeofday( &cur_st, NULL);
curtime = (cur_st.tv_sec % 1000) * 1000 + cur_st.tv_usec / 1000;
switch (event->type) {
case GR_EVENT_TYPE_EXPOSURE:
window = pz_find_window(((GR_EVENT_EXPOSURE *)event)->wid);
if (window != NULL)
window->draw();
break;
case GR_EVENT_TYPE_KEY_DOWN:
window = pz_find_window(((GR_EVENT_KEYSTROKE *)event)->wid);
last_keypress_event = curtime;
switch (event->keystroke.ch) {
#ifdef IPOD
case 'm':
if (!backlight_tid)
backlight_tid = GrCreateTimer(root_wid, 500);
if (!startupcontrast_tid)
startupcontrast_tid = GrCreateTimer(
root_wid, 5000);
window = NULL;
case 'q':
case 'w':
case 'f':
case '\r':
if (curtime - last_button_event >
ipod_get_setting(ACTION_DEBOUNCE)) {
last_button_event = curtime;
}
else {
window = NULL;
}
break;
case 'l':
case 'r':
wheel_evt_count++;
if (wheel_evt_count % ipod_get_setting(WHEEL_DEBOUNCE)
!= 0) {
window = NULL;
}
break;
#endif /* IPOD */
case 'h':
if (!hold_is_on) {
hold_is_on = 1;
header_update_hold_status( hold_is_on );
}
break;
}
if (window != NULL) {
ret = window->keystroke(event);
if(KEY_CLICK & ret)
beep();
if(KEY_UNUSED & ret || EVENT_UNUSED & ret)
pz_default_handler(event);
}
break;
case GR_EVENT_TYPE_KEY_UP:
window = pz_find_window(((GR_EVENT_KEYSTROKE *)event)->wid);
switch (event->keystroke.ch) {
case 'h':
if (hold_is_on) {
hold_is_on = 0;
header_update_hold_status( hold_is_on );
}
break;
case 'm':
if (backlight_tid) {
GrDestroyTimer(backlight_tid);
backlight_tid = 0;
event->type = GR_EVENT_TYPE_KEY_DOWN;
if (window) window->keystroke(event);
event->type = GR_EVENT_TYPE_KEY_UP;
}
if (startupcontrast_tid) {
GrDestroyTimer(startupcontrast_tid);
startupcontrast_tid = 0;
}
break;
default:
break;
}
if (window != NULL) {
ret = window->keystroke(event);
if(KEY_UNUSED & ret || EVENT_UNUSED & ret)
pz_default_handler(event);
}
break;
case GR_EVENT_TYPE_TIMER:
window = pz_find_window(((GR_EVENT_TIMER *)event)->wid);
if (((GR_EVENT_TIMER *)event)->tid == backlight_tid) {
GrDestroyTimer(backlight_tid);
backlight_tid = 0;
ipod_set_backlight(!ipod_get_backlight());
}
#ifdef IPOD
else if (((GR_EVENT_TIMER *)event)->tid == startupcontrast_tid) {
GrDestroyTimer(startupcontrast_tid);
startupcontrast_tid = 0;
ipod_set_contrast(startup_contrast);
}
#endif
else if (((GR_EVENT_TIMER *)event)->tid == battery_tid)
{
if ((usb_connected=usb_is_connected())) {
if (!old_usb_connected)
{
old_usb_connected = usb_connected;
if (!usb_dialog_open) {
usb_dialog_open = 1;
usb_check_goto_diskmode();
usb_dialog_open = 0;
}
}
}
old_usb_connected = usb_connected;
if ((fw_connected=fw_is_connected())) {
if (!old_fw_connected)
{
old_fw_connected = fw_connected;
if (!fw_dialog_open) {
fw_dialog_open = 1;
fw_check_goto_diskmode();
fw_dialog_open = 0;
}
}
}
old_fw_connected = fw_connected;
if (!ipod_is_charging() && ipod_get_battery_level() < BATTERY_LEVEL_LOW) {
low_battery_count++;
} else {
low_battery_count = 0;
}
if (low_battery_count > 4) {
ipod_beep();
// This is here to turn off the iPod before disk corruption occurs.
poweroff_ipod();
}
header_timer_update();
}
else if (window != NULL)
window->keystroke(event);
break;
case GR_EVENT_TYPE_TIMEOUT:
break;
case GR_EVENT_TYPE_SCREENSAVER:
if(((GR_EVENT_SCREENSAVER *)event)->activate)
ipod_set_setting(BACKLIGHT, 0);
else
ipod_set_setting(BACKLIGHT, 1);
break;
default:
window = pz_find_window(((GR_EVENT_GENERAL *)event)->wid);
if (window != NULL)
ret = window->keystroke(event);
if(EVENT_UNUSED & ret)
printf("AN UNKNOWN EVENT OCCURED!!\n");
}
}
GR_GC_ID pz_get_gc(int copy)
{
return (copy ? GrCopyGC(root_gc) : root_gc);
}
GR_WINDOW_ID pz_new_window(int x, int y, int w, int h, void(*do_draw)(void), int(*do_keystroke)(GR_EVENT * event))
{
GR_WINDOW_ID new_wid = GrNewWindowEx(GR_WM_PROPS_APPFRAME |
GR_WM_PROPS_CAPTION |
GR_WM_PROPS_CLOSEBOX,
(GR_CHAR *)"podzilla",
root_wid,
x, y, w, h, WHITE);
/* FIXME: assumes always ok */
windows[n_opened].wid = new_wid;
windows[n_opened].draw = do_draw;
windows[n_opened].keystroke = do_keystroke;
n_opened++;
return new_wid;
}
void
pz_close_window(GR_WINDOW_ID wid)
{
int i;
int window_to_remove = -1;
// assert(windows[n_opened-1].wid == wid);
GrUnmapWindow(wid);
GrDestroyWindow(wid);
/* If we are closing a window which wasn't the last one opened
we must shift the contents of the window array */
if (windows[n_opened-1].wid != wid)
{
for (i = 0; i < n_opened; i++)
if (windows[i].wid == wid && wid != GR_ROOT_WINDOW_ID)
window_to_remove = i;
if (window_to_remove != -1)
{
for (i = window_to_remove; i < n_opened-1; i++)
{
windows[i].wid = windows[i+1].wid;
windows[i].draw = windows[i+1].draw;
windows[i].keystroke = windows[i+1].keystroke;
}
}
}
n_opened--;
}
void pz_set_time_from_file(void)
{
#ifdef IPOD
struct timeval tv_s;
struct stat statbuf;
/* find the last modified time of the settings file */
stat( IPOD_SETTINGS_FILE, &statbuf );
/* convert timespec to timeval */
tv_s.tv_sec = statbuf.st_mtime;
tv_s.tv_usec = 0;
settimeofday( &tv_s, NULL );
#endif
}
int
main(int argc, char **argv)
{
#ifdef IPOD
startup_contrast = ipod_get_contrast();
#endif
if (GrOpen() < 0) {
fprintf(stderr, _("GrOpen failed"));
exit(1);
}
#ifdef LOCALE
setlocale(LC_ALL, "");
bindtextdomain("podzilla", LOCALEDIR);
textdomain("podzilla");
#endif
root_gc = GrNewGC();
GrSetGCUseBackground(root_gc, GR_FALSE);
GrSetGCForeground(root_gc, BLACK);
GrGetScreenInfo(&screen_info);
root_wid = GrNewWindowEx(GR_WM_PROPS_APPFRAME |
GR_WM_PROPS_CAPTION |
GR_WM_PROPS_CLOSEBOX,
(GR_CHAR *)"podzilla",
GR_ROOT_WINDOW_ID,
0, 0, screen_info.cols, screen_info.rows, WHITE);
GrSelectEvents(root_wid, GR_EVENT_MASK_EXPOSURE |
GR_EVENT_MASK_CLOSE_REQ | GR_EVENT_MASK_KEY_UP |
GR_EVENT_MASK_KEY_DOWN | GR_EVENT_MASK_TIMER);
GrSelectEvents(GR_ROOT_WINDOW_ID, GR_EVENT_MASK_SCREENSAVER);
GrMapWindow(root_wid);
hw_version = (int)(ipod_get_hw_version() >> 16);
if (hw_version && hw_version < 0x3) {
/* 1g/2g only */
pz_set_time_from_file();
}
ipod_load_settings();
load_font();
appearance_init();
#ifdef MPDC
mpdc_init();
#endif
backlight_tid = 0;
startupcontrast_tid = 0;
battery_tid = GrCreateTimer(root_wid, 1000);
usb_connected = usb_is_connected();
old_usb_connected = usb_connected;
fw_connected = fw_is_connected();
old_fw_connected = fw_connected;
new_menu_window();
while (1) {
GR_EVENT event;
GrGetNextEventTimeout(&event, 1000);
pz_event_handler(&event);
}
return 0;
}