forked from iPodLinux/podzilla0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipod.c
343 lines (288 loc) · 6.56 KB
/
ipod.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
/*
* 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 <fcntl.h>
#ifdef IPOD
#include <linux/fb.h>
#endif
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include "settings.h"
#include "ipod.h"
#include "pz.h"
#define FBIOGET_CONTRAST _IOR('F', 0x22, int)
#define FBIOSET_CONTRAST _IOW('F', 0x23, int)
#define FBIOGET_BACKLIGHT _IOR('F', 0x24, int)
#define FBIOSET_BACKLIGHT _IOW('F', 0x25, int)
#define FB_DEV_NAME "/dev/fb0"
#define FB_DEVFS_NAME "/dev/fb/0"
#define inb(a) (*(volatile unsigned char *) (a))
#define outb(a,b) (*(volatile unsigned char *) (b) = (a))
#define inl(a) (*(volatile unsigned long *) (a))
#define outl(a,b) (*(volatile unsigned long *) (b) = (a))
static int ipod_ioctl(int request, int *arg)
{
#ifdef IPOD
int fd;
fd = open(FB_DEV_NAME, O_NONBLOCK);
if (fd < 0) fd = open(FB_DEVFS_NAME, O_NONBLOCK);
if (fd < 0) {
return -1;
}
if (ioctl(fd, request, arg) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
#else
return -1;
#endif
}
int ipod_constrain( int min, int max, int val )
{
if( val > max ) val = max;
if( val < min ) val = min;
return( val );
}
int ipod_get_contrast(void)
{
int contrast;
if (ipod_ioctl(FBIOGET_CONTRAST, &contrast) < 0) {
return -1;
}
return contrast;
}
int ipod_set_contrast(int contrast)
{
contrast = ipod_constrain( 0, 128, contrast ); /* just in case */
if (ipod_ioctl(FBIOSET_CONTRAST, (int *) contrast) < 0) {
return -1;
}
return 0;
}
int ipod_get_backlight(void)
{
int backlight;
if (ipod_ioctl(FBIOGET_BACKLIGHT, &backlight) < 0) {
return -1;
}
return backlight;
}
int ipod_set_backlight(int backlight)
{
if (ipod_ioctl(FBIOSET_BACKLIGHT, (int *) backlight) < 0) {
return -1;
}
return 0;
}
int ipod_set_backlight_timer(int timer)
{
int times[] = {0, 1, 2, 5, 10, 30, 60, 0};
GrSetScreenSaverTimeout(times[timer]);
ipod_set_backlight(timer ? 1 : 0);
return 0;
}
int ipod_set_setting(short setting, int value)
{
if (value <= 0) {
value = 0;
}
set_int_setting(setting, value);
switch (setting) {
case CONTRAST:
ipod_set_contrast(value);
break;
case BACKLIGHT:
ipod_set_backlight(value);
break;
case BACKLIGHT_TIMER:
ipod_set_backlight_timer(value);
break;
case COLORSCHEME:
appearance_set_color_scheme(value);
break;
case DECORATIONS:
appearance_set_decorations(value);
break;
}
return 0;
}
int ipod_get_setting(short setting)
{
int value;
value = get_int_setting(setting);
if (value <= 0) {
value = 0;
}
return value;
}
int ipod_load_settings(void)
{
int ret;
if ((ret = load_settings(IPOD_SETTINGS_FILE)) < 0) {
if (ret == -2) {
pz_error(_("Corrupt settings file, blasting."));
unlink(IPOD_SETTINGS_FILE);
}
else {
printf(_("Failed to open %s to read settings, using defaults.\n"), IPOD_SETTINGS_FILE);
}
ipod_set_setting(CONTRAST, ipod_get_contrast());
ipod_set_setting(CLICKER, 1);
ipod_set_setting(WHEEL_DEBOUNCE, 3);
ipod_set_setting(ACTION_DEBOUNCE, 400);
ipod_set_setting(DSPFREQUENCY, 0);
ipod_set_setting(COLORSCHEME, 0);
ipod_set_setting(SLIDE_TRANSIT, 1);
}
ipod_set_contrast(ipod_get_setting(CONTRAST));
ipod_set_backlight_timer(ipod_get_setting(BACKLIGHT_TIMER));
appearance_set_color_scheme(ipod_get_setting(COLORSCHEME));
appearance_set_decorations(ipod_get_setting(DECORATIONS));
return 0;
}
int ipod_save_settings(void)
{
if (save_settings(IPOD_SETTINGS_FILE) < 0) {
pz_error(_("Save failed."));
}
return 0;
}
void ipod_reset_settings(void)
{
unlink(IPOD_SETTINGS_FILE);
free_all_settings();
ipod_load_settings();
ipod_save_settings();
}
void ipod_touch_settings(void)
{
FILE *fp;
if(( fp = fopen(IPOD_SETTINGS_FILE, "a+")) != 0 ){
fclose(fp);
}
}
/*
* 0: screen on
* 1,2: screen off
* 3: screen power down
*/
int ipod_set_blank_mode(int blank)
{
#ifdef IPOD
if (ipod_ioctl(FBIOBLANK, (int *) blank) < 0) {
return -1;
}
#endif
return 0;
}
void ipod_beep(void)
{
#ifdef IPOD
if (hw_version >= 0x4) {
int i, j;
outl(inl(0x70000010) & ~0xc, 0x70000010);
outl(inl(0x6000600c) | 0x20000, 0x6000600c); /* enable device */
for (j = 0; j < 10; j++) {
for (i = 0; i < 0x888; i++ ) {
outl(0x80000000 | 0x800000 | i, 0x7000a000); /* set pitch */
}
}
outl(0x0, 0x7000a000); /* piezo off */
} else {
static int fd = -1;
static char buf;
if (fd == -1 && (fd = open("/dev/ttyS1", O_WRONLY)) == -1
&& (fd = open("/dev/tts/1", O_WRONLY)) == -1) {
return;
}
write(fd, &buf, 1);
}
#else
if (isatty(1)) {
printf("\a");
}
#endif
}
int ipod_read_apm(int *battery, int *charging)
{
#ifdef IPOD
FILE *file;
int ac_line_status = 0xff;
int battery_status = 0xff;
int battery_flag = 0xff;
int percentage = -1;
int time_units = -1;
if ((file = fopen("/proc/apm", "r")) != NULL) {
fscanf(file, "%*s %*d.%*d 0x%*02x 0x%02x 0x%02x 0x%02x %d%% %d", &ac_line_status, &battery_status, &battery_flag, &percentage, &time_units);
fclose(file);
if (battery) {
*battery = time_units;
}
if (charging) {
*charging = (battery_status != 0xff && battery_status & 0x3) ? 1 : 0;
}
return 0;
}
#endif
if (battery) *battery = BATTERY_LEVEL_FULL;
if (charging) *charging = 0;
return 0;
}
int ipod_get_battery_level(void)
{
int battery;
ipod_read_apm(&battery, 0);
return battery;
}
int ipod_is_charging(void)
{
int charging;
ipod_read_apm(0, &charging);
return charging;
}
long ipod_get_hw_version(void)
{
#ifdef IPOD
int i;
char cpuinfo[512];
char *ptr;
FILE *file;
if ((file = fopen("/proc/cpuinfo", "r")) != NULL) {
while (fgets(cpuinfo, sizeof(cpuinfo), file) != NULL)
if (strncmp(cpuinfo, "Revision", 8) == 0)
break;
fclose(file);
} else {
return 0;
}
for (i = 0; !isspace(cpuinfo[i]); i++);
for (; isspace(cpuinfo[i]); i++);
ptr = cpuinfo + i + 2;
return strtol(ptr, NULL, 16);
#else
return 0;
#endif
}