-
Notifications
You must be signed in to change notification settings - Fork 1
/
ipod.c
206 lines (174 loc) · 4.38 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
/*
* Last updated: July 22, 2008
* ~Keripo
*
* Copyright (C) 2008 Keripo
*
* 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.
*/
/* == Volume code == */
#include <fcntl.h>
#include <unistd.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
static int ipod_mixer;
static int volume_current;
static int ipod_volume;
static void ipod_update_volume() {
if (volume_current != ipod_volume) {
int vol;
volume_current = ipod_volume;
vol = volume_current << 8 | volume_current;
ioctl(ipod_mixer, SOUND_MIXER_WRITE_PCM, &vol);
}
}
static void ipod_init_sound()
{
ipod_mixer = open("/dev/mixer", O_RDWR);
ipod_volume = 50; // Good default
ipod_update_volume();
}
static void ipod_exit_sound()
{
close(ipod_mixer);
}
/* == Backlight code == */
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define BACKLIGHT_OFF 0
#define BACKLIGHT_ON 1
#define FBIOSET_BACKLIGHT _IOW('F', 0x25, int)
static int backlight_current;
static int ipod_ioctl(int request, int *arg)
{
int fd;
fd = open("/dev/fb0", O_NONBLOCK);
if (fd < 0) fd = open("/dev/fb/0", O_NONBLOCK);
if (fd < 0) return -1;
if (ioctl(fd, request, arg) < 0) {
close(fd);
return -1;
}
close(fd);
return 0;
}
static void ipod_set_backlight(int backlight)
{
ipod_ioctl(FBIOSET_BACKLIGHT, (int *)(long)backlight);
}
static void ipod_toggle_backlight()
{
if (backlight_current == 0) {
ipod_set_backlight(BACKLIGHT_ON);
backlight_current = 1;
} else {
ipod_set_backlight(BACKLIGHT_OFF);
backlight_current = 0;
}
}
static void ipod_init_backlight()
{
ipod_set_backlight(BACKLIGHT_ON);
backlight_current = 1;
}
/* == Input code == */
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <linux/kd.h>
#define KEY_MENU 50 // Up
#define KEY_PLAY 32 // Down
#define KEY_REWIND 17 // Left
#define KEY_FORWARD 33 // Right
#define KEY_ACTION 28 // Select
#define KEY_HOLD 35 // Exit
#define SCROLL_L 38 // Counter-clockwise
#define SCROLL_R 19 // Clockwise
#define KEY_NULL -1 // No key event
#define KEYCODE(a) (a & 0x7f) // Use to get keycode of scancode.
#define KEYSTATE(a) (a & 0x80) // Check if key is pressed or lifted
static int console;
static struct termios stored_settings;
static int ipod_get_keypress()
{
int press = 0;
if (read(console, &press, 1) != 1)
return KEY_NULL;
return press;
}
static void ipod_init_input()
{
struct termios new_settings;
console = open("/dev/console", O_RDONLY | O_NONBLOCK);
tcgetattr(console, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= ~(ICANON | ECHO | ISIG);
new_settings.c_iflag &= ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON | BRKINT);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 0;
tcsetattr(console, TCSAFLUSH, &new_settings);
ioctl(console, KDSKBMODE, K_MEDIUMRAW);
}
static void ipod_exit_input()
{
// Causes screwy characters to appear
//tcsetattr(console, TCSAFLUSH, &stored_settings);
close(console);
}
/* == loop() functions == */
void ipod_init()
{
ipod_init_sound();
ipod_init_input();
ipod_init_backlight();
}
void ipod_update()
{
int input;
input = ipod_get_keypress();
if (KEYSTATE(input)) return; // Ignore keylifts
input = KEYCODE(input);
switch (input) {
// Scrol wheel not used for volume due to SBAGen's slow main loop
case KEY_REWIND:
ipod_volume -= 2;
if (ipod_volume < 0)
ipod_volume = 0; // Negative volume DNE!
ipod_update_volume();
break;
case KEY_FORWARD:
ipod_volume += 2;
if (ipod_volume > 70)
ipod_volume = 70; // To be safe - 70 is VERY loud
ipod_update_volume();
break;
case KEY_ACTION:
ipod_toggle_backlight();
break;
case KEY_MENU:
printf("\nExiting...\n");
exit(0);
break;
default:
break;
}
}
void ipod_exit()
{
ipod_exit_sound();
ipod_exit_input();
}