-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
104 lines (83 loc) · 1.69 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include "playback.h"
#include "control.h"
static struct termios saved_attr;
static pthread_t control_thread;
static void reset_input_mode(void)
{
if (tcsetattr(STDIN_FILENO, TCSANOW, &saved_attr))
tcsetattr(STDIN_FILENO, TCSANOW, &saved_attr);
}
static int init(void)
{
int ret = 0;
struct termios tattr;
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO)) {
ret = -1;
goto fail;
}
ret = tcgetattr(STDIN_FILENO, &tattr);
if (ret) {
perror("tcgetattr");
goto fail;
}
saved_attr = tattr;
atexit(reset_input_mode);
tattr.c_lflag &= ~ECHO;
tattr.c_lflag &= ~ICANON;
ret = tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);
if (ret) {
perror("tcsetattr");
goto fail;
}
init_libav();
memset(&control, 0, sizeof(control_t));
init_sndcard_control();
ret = pthread_create(&control_thread, NULL, control_process, NULL);
if (ret != 0) {
fprintf(stderr, "control thread create failure\n");
goto fail;
}
fail:
return ret;
}
int main(int argc, const char **argv)
{
int i;
int ret = -1;
if (argc < 2) {
fprintf(stderr, "Usage: player <media_filename(s)\n");
return -1;
}
ret = init();
if (ret != 0)
return -1;
for (i = 0;; ) {
if (!control.pre_next_flg)
i++;
if (control.pre_next_flg == 1) {
control.pre_next_flg = 0;
if (i > 1)
i--;
}
if (control.pre_next_flg == 2) {
control.pre_next_flg = 0;
if (i < argc - 1)
i++;
else
break;
}
if (i >= argc)
break;
play(argv[i]);
}
pthread_cancel(control_thread);
pthread_join(control_thread, NULL);
return 0;
}