-
Notifications
You must be signed in to change notification settings - Fork 3
/
menu.c
343 lines (299 loc) · 8.1 KB
/
menu.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
/*
* =====================================================================================
*
* Filename: menu.c
*
* Description: 菜单
*
* Version: 1.0
* Created: 2014/07/24
* Revision: none
* Compiler: gcc
*
* Author: Occam's Razor
* Organization:
*
* =====================================================================================
*/
#include "menu.h"
#include "config.h"
#include "music_tmux.h"
#define TEL_IAC "\377"
#define TEL_WILL "\373"
#define TEL_ECHO "\001"
#define MUSIC_LIST_COUNT 10
enum MENU_TYPE {
MAIN_MENU,
MUSIC_LIST,
SETTING,
HELP
};
struct winsize window_size;
static enum MENU_TYPE menu_type;
static int cursor, key_up, key_down, key_right, key_left;
static char *base_menu[] = {
"列表",
"设置",
"帮助",
};
static void show_music_list();
static void show_setting_menu();
static void show_help_info();
static void sig_quit(int signo)
{
fprintf(stdout, "sigquit:%d", signo);
}
void init_menu()
{
hide_cursor();
if(signal(SIGINT, sig_quit) == SIG_ERR)
fprintf(stdout, "can't sigquit");
cursor = 0;
cls();
menu_type = MAIN_MENU;
if (ioctl(0, TIOCGWINSZ, &window_size) != 0) {
fprintf(stderr, "TIOCGWINSZ:%s/n", strerror(errno));
//exit(1);
}
}
/*
* 设置操作按键
*/
void init_handle_key(int up, int down, int left, int right)
{
key_up = up;
key_down = down;
key_left = left;
key_right = right;
}
int show_menu(char userKey)
{
int i, size;
int row = window_size.ws_row;
int col = window_size.ws_col;
int key = -1;
//if(userKey > 'A' && userKey <= 'Z') userKey += 32;
if(userKey == key_up)
key = KEYUP;
else if(userKey == key_down)
key = KEYDOWN;
else if(userKey == key_left)
key = KEYLEFT;
else if(userKey == key_right)
key = KEYRIGHT;
else if(userKey == 'q')
key = KEYQUIT;
else if(userKey == '[')
key = VOLUME_ADD;
else if(userKey == ']')
key = VOLUME_DECRE;
size = sizeof(base_menu) / sizeof(base_menu[0]);
cls();
for (i = 0 ; i < (row / 2) - 10; i++)
fprintf(stdout, "\n");
switch(key) {
case KEYUP: {
cursor--;
break;
}
case KEYDOWN: {
cursor++;
break;
}
case KEYRIGHT:
{
switch(menu_type)
{
case MAIN_MENU: {
menu_type = cursor + 1;
cursor = 0;
break;
}
case MUSIC_LIST:
{
music_file* music = get_index_music_path(cursor);
if(music != NULL) {
play_music_file(music);
}
break;
}
case SETTING:
{
if(cursor == 1) {
struct passwd *pw = getpwuid(getuid());
char shell[2048];
snprintf(shell, sizeof(shell), "vi %s/%s", pw->pw_dir, CONFIG_FILE);
system(shell);
hide_cursor();
} else if (cursor == 0) {
//reload library
reload_library();
print_center_string("音乐库已经重新加载!");
}
}
default:
break;
}
break;
}
case KEYLEFT: {
menu_type = MAIN_MENU;
cursor = 0;
break;
}
case KEYQUIT: {
return 1;
break;
}
case VOLUME_ADD: {
add_play_volume();
break;
}
case VOLUME_DECRE: {
decre_play_volume();
break;
}
default:
break;
}
switch(menu_type) {
case MAIN_MENU: {
if(cursor >= size)
cursor = 0;
else if(cursor < 0)
cursor = size - 1;
set_cursor_point(col / 2 - 10, (row / 2) - size - 5);
int color = rand() % 7 + 1;
char c[1024];
get_color(c,color);
textcolor("♪♪v(⌒o⌒)v♪♪イエーイ", c);
char menu_item[1024];
for(i = 0 ; i < size; i++) {
if(i == cursor) {
snprintf(menu_item, sizeof(menu_item), "->%d.%s\n", i + 1, base_menu[i]);
set_cursor_point(col / 2 - 6, (row / 2) - size + i);
textcolor(menu_item, TEXT_DARKGREEN);
}
else {
snprintf(menu_item, sizeof(menu_item), " %d.%s\n", i + 1, base_menu[i]);
set_cursor_point(col / 2 - 6, (row / 2) - size + i);
fprintf(stdout, "%s", menu_item);
}
}
break;
}
case MUSIC_LIST: {
show_music_list();
break;
}
case SETTING: {
show_setting_menu();
break;
}
case HELP: {
show_help_info();
break;
}
default:
break;
}
music_file* music = get_current_music();
if (music != NULL) {
set_cursor_point(0, row);
backgroud_color(BACKGRPUND_GREEN);
for(int i = 0; i < col / 2; i++)
printf(" ");
char current_info[1024];
snprintf(current_info, sizeof(current_info), "当前播放:%s", music->filename);
set_cursor_point(col / 2 - (strlen(current_info) / 2), row);
textcolor(current_info, TEXT_WHITE);
fprintf(stdout, "\033[1;0H");//设置到开始
fprintf(stdout, "\033[0m");//reset
}
return 0;
}
void print_center_string(char *str)
{
int row = window_size.ws_row;
int col = window_size.ws_col;
set_cursor_point(col / 2 - (strlen(str) / 2), row / 2);
fprintf(stdout, "%s", str);
}
static void show_setting_menu()
{
int row = window_size.ws_row;
int col = window_size.ws_col;
int i = 0;
char help_array[][50] = {
"重新加载配置",
"歌曲路径设置",
};
char setting_item[50];
int len = dim(help_array);
int size = sizeof(help_array) / sizeof(help_array[0]);
if(cursor >= size)
cursor = 0;
else if(cursor < 0)
cursor = size - 1;
set_cursor_point(col / 2 - 13, row / 2 - 10);
fprintf(stdout, "系统设置\n");
for( i = 0 ; i < len; i++) {
set_cursor_point(col / 2 - 15, row / 2 - 9 + i);
if(i == cursor) {
snprintf(setting_item, sizeof(setting_item), "->%d.%s\n", i + 1, help_array[i]);
textcolor(setting_item, TEXT_DARKGREEN);
}
else
{
snprintf(setting_item, sizeof(setting_item), " %d.%s\n", i + 1, help_array[i]);
fprintf(stdout, "%s", setting_item);
}
}
//绿色填充最后一行
set_cursor_point(0, row);
backgroud_color(BACKGRPUND_BLUE);
for(int i = 0; i < col / 2; i++)
printf(" ");
char current_info[1024];
snprintf(current_info, sizeof(current_info), "请输入1-2,进行操作");
set_cursor_point(col / 2 - (strlen(current_info) / 2), row);
textcolor(current_info, TEXT_WHITE);
fprintf(stdout, "\033[0m");//reset
}
static void show_music_list()
{
cls();
print_library_music(&cursor, MUSIC_LIST_COUNT, window_size.ws_col, window_size.ws_row);
}
static void show_help_info()
{
cls();
int row = window_size.ws_row;
int col = window_size.ws_col;
int i = 0;
char help_array[][50] = {
"终端音乐播放器: v1.0",
"Author: Yang Bing",
"Email: [email protected]",
"github: https://github.com/beyondblog",
"blog: https://beyondblog.github.io",
"source: https://github.com/beyondblog/music-tmux",
"\n\n",
" 2014年10月\n",
};
int len = dim(help_array);
for( i = 0 ; i < len; i++) {
set_cursor_point(col / 2 - 20, row / 2 - 10 + i);
fprintf(stdout, "%s\n", help_array[i]);
}
//绿色填充最后一行
set_cursor_point(0, row);
backgroud_color(BACKGRPUND_BLUE);
for(int i = 0; i < col / 2; i++)
printf(" ");
char current_info[1024];
snprintf(current_info, sizeof(current_info), "退出请按:q 音量增大:[ 音量减小:] 下一首:n 上一首:p");
set_cursor_point(col / 2 - (strlen(current_info) / 2), row);
textcolor(current_info, TEXT_WHITE);
fprintf(stdout, "\033[0m");//reset
}