-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
297 lines (273 loc) · 10.5 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <linux/ioctl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <getopt.h>
#include <libgen.h>
#include <errno.h>
#define MIYOO_VIR_SET_MODE _IOWR(0x100, 0, unsigned long)
#define MIYOO_VIR_SET_VER _IOWR(0x101, 0, unsigned long)
#define MIYOO_SND_SET_VOLUME _IOWR(0x100, 0, unsigned long)
#define MIYOO_SND_GET_VOLUME _IOWR(0x101, 0, unsigned long)
#define MIYOO_KBD_GET_HOTKEY _IOWR(0x100, 0, unsigned long)
#define MIYOO_KBD_SET_VER _IOWR(0x101, 0, unsigned long)
#define MIYOO_KBD_LOCK_KEY _IOWR(0x102, 0, unsigned long) //unused
#define MIYOO_LAY_SET_VER _IOWR(0x103, 0, unsigned long)
#define MIYOO_KBD_GET_VER _IOWR(0x104, 0, unsigned long)
#define MIYOO_LAY_GET_VER _IOWR(0x105, 0, unsigned long)
#define MIYOO_KBD_SET_HOTKEY _IOWR(0x106, 0, unsigned long)
#define MIYOO_FB0_PUT_OSD _IOWR(0x100, 0, unsigned long)
#define MIYOO_FB0_SET_MODE _IOWR(0x101, 0, unsigned long)
#define MIYOO_FB0_GET_VER _IOWR(0x102, 0, unsigned long)
#define MIYOO_FB0_SET_FLIP _IOWR(0x103, 0, unsigned long) //unused
#define MIYOO_FB0_SET_FPBP _IOWR(0x104, 0, unsigned long)
#define MIYOO_FB0_GET_FPBP _IOWR(0x105, 0, unsigned long)
#define MIYOO_FB0_SET_TEFIX _IOWR(0x106, 0, unsigned long)
#define MIYOO_FB0_GET_TEFIX _IOWR(0x107, 0, unsigned long)
#define MIYOO_FBP_FILE "/mnt/.fpbp.conf"
#define MIYOO_LID_FILE "/mnt/.backlight.conf"
#define MIYOO_VOL_FILE "/mnt/.volume.conf"
#define MIYOO_BUTTON_FILE "/mnt/.buttons.conf"
#define MIYOO_BATTERY_FILE "/mnt/.batterylow.conf"
#define MIYOO_LID_CONF "/sys/devices/platform/backlight/backlight/backlight/brightness"
#define MIYOO_BATTERY "/sys/class/power_supply/miyoo-battery/voltage_now"
#define MIYOO_SND_FILE "/dev/miyoo_snd"
#define MIYOO_FB0_FILE "/dev/miyoo_fb0"
#define MIYOO_KBD_FILE "/dev/miyoo_kbd"
#define MIYOO_VIR_FILE "/dev/miyoo_vir"
#define OPTSTR "hivV:k:l:m:M:s:f:t:"
#define USAGE_FMT "%s [-h] [-i] [-v] [-V volume(0-10)] [-m rumble_ver(1-4)] [-M rumble_mode(0-1)] [-s screen_ver(1-4)]\n [-f fpbp_hexbyte]\n [-k keypad_ver(1-7)]\n [-l layout_ver(1-6)]\n [-t tefix(0-3)]\n"
#define DEFAULT_PROGNAME "miyooctl"
#define ERR_DO_THE_DEED "the main action went wrong somehow"
#define ERR_OPEN_FILE(x) "open('"x"')"
extern int errno;
extern char *optarg;
extern int opterr, optind;
static void usage(char *progname, int opt) {
if (opt == '?') fprintf(stderr, "Unknown option '-%c'\n", optopt);
if (opt == ':') fprintf(stderr, "Missing argument for option '-%c'\n", optopt);
fprintf(stderr, USAGE_FMT, (progname ? progname : DEFAULT_PROGNAME));
exit(EXIT_FAILURE);
/* NOTREACHED */
}
static int parse_int(const char *s, int base, int min, int max) {
char *endptr;
int res = (int)strtol(s, &endptr, base);
if(endptr == s || *endptr != '\0') {
fprintf(stderr, DEFAULT_PROGNAME ": error parsing number: got %s\n", s);
return -1;
} else if (errno == ERANGE || res < min || res > max) {
fprintf(stderr, DEFAULT_PROGNAME ": number out of range %s\n", s);
return -1;
}
return res;
}
typedef struct {
int just_want_info;
int verbose;
int screen_ver;
int fpbp;
int tefix;
int keypad_ver;
int layout_ver;
int rumble_ver;
int rumble_mode;
int volume;
char *progname;
} options_t;
int do_the_deed(options_t *opts);
int main(int argc, char** argv) {
int opt;
options_t options = { 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, basename(argv[0]) };
opterr = 0;
while ((opt = getopt(argc, argv, OPTSTR)) != EOF) {
switch(opt) {
case 'i':
options.just_want_info = 1;
break;
case 'v':
options.verbose = 1;
break;
case 'V':
options.volume = parse_int(optarg, 10, 0, 10);
break;
case 'k':
options.keypad_ver = parse_int(optarg, 10, 1, 7);
break;
case 'l':
options.layout_ver = parse_int(optarg, 10, 1, 6);
break;
case 't':
options.tefix = parse_int(optarg, 10, 0, 3);
break;
case 'm':
options.rumble_ver = parse_int(optarg, 10, 1, 4);
break;
case 'M':
options.rumble_mode = parse_int(optarg, 10, 0, 1);
break;
case 's':
options.screen_ver = parse_int(optarg, 10, 1, 4);
break;
case 'f':
options.fpbp = parse_int(optarg, 16, 1, 4);
break;
case 'h':
default:
usage(options.progname, opt);
/* NOTREACHED */
break;
}
}
if(do_the_deed(&options) != EXIT_SUCCESS) {
perror(ERR_DO_THE_DEED);
exit(EXIT_FAILURE);
/* NOTREACHED */
}
return EXIT_SUCCESS;
}
int do_the_deed(options_t *opts) {
options_t current = { 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, NULL };
int f;
if(opts->volume != -1) {
if(!(f = open(MIYOO_SND_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_SND_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_SND_GET_VOLUME, &(current.volume));
if(opts->volume != -1) {
if(opts->verbose) {
fprintf(stdout, "%s: setting volume to %d\n", opts->progname, opts->volume);
}
ioctl(f, MIYOO_SND_SET_VOLUME, opts->volume);
}
close(f);
}
if(opts->screen_ver != -1 || opts->fpbp != -1) {
if(!(f = open(MIYOO_FB0_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_FB0_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_FB0_GET_VER, &(current.screen_ver));
ioctl(f, MIYOO_FB0_GET_FPBP, &(current.fpbp));
if(opts->screen_ver != -1) {
if(opts->verbose) {
fprintf(stdout, "%s: setting screen version to %d\n", opts->progname, opts->screen_ver);
}
ioctl(f, MIYOO_FB0_SET_MODE, opts->screen_ver);
}
if(opts->fpbp != -1) {
if(opts->verbose) {
fprintf(stdout, "%s: setting fp to 0x%x and bp to 0x%x\n", opts->progname, ((uint8_t)opts->fpbp&0xF0)>>4, (uint8_t)opts->fpbp&0xF);
}
ioctl(f, MIYOO_FB0_SET_FPBP, (uint8_t)opts->fpbp);
}
close(f);
}
if(opts->tefix != -1) {
if(!(f = open(MIYOO_FB0_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_FB0_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_FB0_GET_TEFIX, &(current.tefix));
if(opts->verbose) {
fprintf(stdout, "%s: setting TEfix to %d\n", opts->progname, opts->tefix);
}
ioctl(f, MIYOO_FB0_SET_TEFIX, opts->tefix);
close(f);
}
if(opts->keypad_ver != -1) {
if(!(f = open(MIYOO_KBD_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_KBD_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
if(opts->verbose) {
fprintf(stdout, "%s: setting keypad version to %d\n", opts->progname, opts->keypad_ver);
}
ioctl(f, MIYOO_KBD_SET_VER, opts->keypad_ver);
close(f);
}
if(opts->layout_ver != -1) {
if(!(f = open(MIYOO_KBD_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_KBD_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
if(opts->verbose) {
fprintf(stdout, "%s: setting keypad layout version to %d\n", opts->progname, opts->layout_ver);
}
ioctl(f, MIYOO_LAY_SET_VER, opts->layout_ver);
close(f);
}
if(opts->just_want_info) {
if(!(f = open(MIYOO_KBD_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_KBD_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_KBD_GET_VER, &(current.keypad_ver));
ioctl(f, MIYOO_LAY_GET_VER, &(current.layout_ver));
close(f);
if(!(f = open(MIYOO_SND_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_SND_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_SND_GET_VOLUME, &(current.volume));
close(f);
if(!(f = open(MIYOO_FB0_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_FB0_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
ioctl(f, MIYOO_FB0_GET_TEFIX, &(current.tefix));
close(f);
}
if(opts->rumble_ver != -1 || opts->rumble_mode != -1) {
if(!(f = open(MIYOO_VIR_FILE, O_RDWR))) {
perror(ERR_OPEN_FILE(MIYOO_VIR_FILE));
return EXIT_FAILURE;
/* NOTREACHED */
}
if(opts->rumble_ver != -1) {
if(opts->verbose) {
fprintf(stdout, "%s: setting rumble motor version to %d\n", opts->progname, opts->rumble_ver);
}
ioctl(f, MIYOO_VIR_SET_VER, opts->rumble_ver);
}
if(opts->rumble_mode != -1) {
if(opts->verbose) {
fprintf(stdout, "%s: setting rumble motor mode to %d\n", opts->progname, opts->rumble_mode);
}
ioctl(f, MIYOO_VIR_SET_MODE, opts->rumble_mode);
}
close(f);
}
if(opts->just_want_info) {
// fprintf(stdout, "%s: current screen version: %d\n", opts->progname, current.screen_ver);
// fprintf(stdout, "%s: def_fb 0x%x; def_bp 0x%x; cur_fb 0x%x; cur_bp 0x%x\n", opts->progname,
// ((uint16_t)current.fpbp&0xF000)>>12,
// ((uint16_t)current.fpbp&0x0F00)>>8,
// ((uint16_t)current.fpbp&0x00F0)>>4,
// ((uint16_t)current.fpbp&0x000F));
fprintf(stdout, "%s: current TEfix method: %d\n", opts->progname, current.tefix);
fprintf(stdout, "%s: current keypad version: %d\n", opts->progname, current.keypad_ver);
fprintf(stdout, "%s: current keypad layout version: %d\n", opts->progname, current.layout_ver);
fprintf(stdout, "%s: current volume: %d\n", opts->progname, current.volume);
}
return EXIT_SUCCESS;
}