-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
159 lines (136 loc) · 4.84 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
#include <getopt.h>
#include <mpd/client.h>
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
#include "log.h"
#include "misc.h"
#include "player.h"
#include "queue.h"
#include "search.h"
#include "song.h"
#include "util.h"
#define HOST "127.0.0.1"
#define PORT 6600
int main(int argc, char *argv[]) {
int opt;
struct mpd_connection *connection;
connection = NULL;
connection = mpd_connection_new(HOST, PORT, 0);
if (connection != NULL) {
#ifdef DEBUG
log_info(MSG_CONNECTION_SUCC);
#endif
if (mpd_connection_get_error(connection) != MPD_ERROR_SUCCESS) {
die(connection, NULL, NULL, MSG_CONNECTION_FAIL);
}
while ((opt = getopt(argc, argv, "0a:cCd:hl:Lmn:p:P:rRs:tx:")) != -1) {
switch (opt) {
case '0':
tab_complete_list(connection);
break;
case 'a':
if (optarg) {
optind--;
for (; optind < argc && *argv[optind] != '-';
optind++) {
search_all_tags(connection, argv[optind], true);
}
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'c':
clear_queue(connection);
break;
case 'C':
print_current_song(connection);
break;
case 'd':
if (optarg) {
delete_from_queue(connection, atoi(optarg));
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'h':
print_help();
break;
case 'l':
if (optarg) {
loop_current_song(connection, atoi(optarg));
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'L':
print_queue(connection);
break;
case 'm':
print_current_song_metadata(connection);
break;
case 'n':
if (optarg) {
queue_relative_change(connection, atoi(optarg));
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'p':
if (optarg) {
song_set_position(connection, atoi(optarg));
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'P':
if (optarg) {
song_set_relative_position(connection, atoi(optarg));
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 'r':
toggle_repeat_mode(connection);
break;
case 'R':
shuffle_queue(connection);
break;
case 's':
if (optarg) {
search_all_tags(connection, optarg, false);
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
case 't':
toggle_playing(connection);
break;
case 'x':
if (optarg) {
clear_queue(connection);
optind--;
for (; optind < argc && *argv[optind] != '-'; optind++) {
search_all_tags(connection, argv[optind], true);
}
/*
for some reason after clearing queue
it turns off playing mode, so we
re-enable it with this
*/
toggle_playing(connection);
} else {
die(connection, NULL, NULL, ADDITIONAL_ARGUMENT_FAIL);
}
break;
default:
die(connection, NULL, NULL, UNKNOWN_ARGUMENT_FAIL);
break;
}
}
mpd_connection_free(connection);
} else {
die(NULL, NULL, NULL, MSG_CONNECTION_FAIL);
}
exit(EXIT_SUCCESS);
}