-
Notifications
You must be signed in to change notification settings - Fork 9
/
fmc.c
217 lines (193 loc) · 6.21 KB
/
fmc.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
#include <curl/curl.h>
#include <json/json.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netdb.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
typedef struct {
int id;
char name[32];
} fm_channel_t;
#define channel_max 1024
fm_channel_t channels[channel_max];
void read_channels()
{
const char *file = "/tmp/fm_channels";
char buf[4096];
size_t size;
int fd;
int i;
for (i = 0; i < channel_max; i++) {
channels[i].id = -1;
}
fd = open(file, O_RDONLY);
if (fd >= 0) {
memset(buf, 0, sizeof(buf));
size = read(fd, buf, sizeof(buf));
if (size > 0) {
json_object *obj = json_tokener_parse(buf);
if (obj) {
array_list *channel_objs = json_object_get_array(json_object_object_get(obj, "channels"));
if (channel_objs) {
for (i = 0; i < array_list_length(channel_objs); i++) {
json_object *o = (json_object*) array_list_get_idx(channel_objs, i);
int id = json_object_get_int(json_object_object_get(o, "channel_id"));
const char *name = json_object_get_string(json_object_object_get(o, "name"));
channels[id].id = id;
strcpy(channels[id].name, name);
}
}
json_object_put(obj);
}
}
close(fd);
}
else {
FILE *f = fopen(file, "w");
CURL *curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://www.douban.com/j/app/radio/channels?app_name=radio_desktop_win&version=100");
curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(f);
read_channels();
}
}
void print_channels()
{
int i;
printf("%3s %s\n", "id", "name");
for (i = 0; i < channel_max; i++) {
if (channels[i].id >= 0) {
printf("%3d %s\n", channels[i].id, channels[i].name);
}
}
}
void print_usage()
{
printf("Usage: fmc [cmd] [argument]\n"
" fmc help - show this help infomation\n"
" fmc info - show current fmd information\n"
" fmc play - start playback\n"
" fmc pause - pause playback\n"
" fmc toggle - toggle between play and pause\n"
" fmc stop - stop playback\n"
" fmc skip - skip current song\n"
" fmc ban - don't ever play current song again\n"
" fmc rate - mark current song as \"liked\"\n"
" fmc unrate - unmark current song\n"
" fmc channels - list all FM channels\n"
" fmc setch <id> - set channel through channel's id\n"
" fmc end - tell fmd to quit\n"
);
}
void time_str(int time, char *buf)
{
int sec = time % 60;
int min = time / 60;
sprintf(buf, "%d:%02d", min, sec);
}
int main(int argc, char *argv[])
{
read_channels();
char *addr = "localhost";
char *port = "10098";
int c;
while ((c = getopt(argc, argv, "a:p:")) != -1) {
switch (c) {
case 'a':
addr = optarg;
break;
case 'p':
port = optarg;
break;
default:
break;
}
}
char input_buf[64];
char output_buf[1024];
int buf_size;
if (optind < argc) {
strcpy(input_buf, argv[optind]);
int i;
for (i = optind + 1; i < argc; i++) {
strcat(input_buf, " ");
strcat(input_buf, argv[i]);
}
}
else {
strcpy(input_buf, "info");
}
if (strcmp(input_buf, "channels") == 0) {
print_channels();
return 0;
}
else if (strcmp(input_buf, "help") == 0) {
print_usage();
return 0;
}
struct addrinfo hints, *results, *p;
int sock_fd;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (getaddrinfo(addr, port, &hints, &results) != 0) {
return -1;
}
for (p = results; p != NULL; p = p->ai_next) {
sock_fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sock_fd < 0) {
continue;
}
if (connect(sock_fd, p->ai_addr, p->ai_addrlen) == 0) {
break;
}
close(sock_fd);
}
if (p == NULL) {
perror("connect");
return -1;
}
freeaddrinfo(results);
send(sock_fd, input_buf, strlen(input_buf), 0);
buf_size = recv(sock_fd, output_buf, sizeof(output_buf), 0);
if (buf_size == 0) {
close(sock_fd);
return 0;
}
output_buf[buf_size] = '\0';
close(sock_fd);
json_object *obj = json_tokener_parse(output_buf);
const char *status = json_object_get_string(json_object_object_get(obj, "status"));
if (strcmp(status, "error") == 0) {
printf("%s\n", json_object_get_string(json_object_object_get(obj, "message")));
}
else {
printf("FMD %s - ", strcmp(status, "play") == 0? "Playing": (strcmp(status, "pause") == 0? "Paused": "Stopped"));
int c_id = json_object_get_int(json_object_object_get(obj, "channel"));
if (c_id < 0 || c_id >= channel_max || channels[c_id].id < 0) {
printf("未知兆赫\n");
}
else {
printf("%s\n", channels[c_id].name);
}
if (strcmp(status, "stop") != 0) {
char pos[16], len[16];
time_str(json_object_get_int(json_object_object_get(obj, "pos")), pos);
time_str(json_object_get_int(json_object_object_get(obj, "len")), len);
printf("%s%s - %s\n%s / %s\n",
json_object_get_int(json_object_object_get(obj, "like"))? "[Like] ": "",
json_object_get_string(json_object_object_get(obj, "artist")),
json_object_get_string(json_object_object_get(obj, "title")), pos, len);
}
}
json_object_put(obj);
return 0;
}