File src/pms.c changed (mode: 100644) (index 7c8b99e..5a2841d) |
22 |
22 |
#include <stdlib.h> |
#include <stdlib.h> |
23 |
23 |
#include <string.h> |
#include <string.h> |
24 |
24 |
#include <signal.h> |
#include <signal.h> |
|
25 |
|
#include <sys/select.h> |
|
26 |
|
#include <unistd.h> |
25 |
27 |
#include "pms.h" |
#include "pms.h" |
26 |
28 |
|
|
27 |
29 |
struct options_t * options = NULL; |
struct options_t * options = NULL; |
|
... |
... |
void reset_options() { |
43 |
45 |
options->server = "localhost"; |
options->server = "localhost"; |
44 |
46 |
options->port = 0; |
options->port = 0; |
45 |
47 |
options->timeout = 200; |
options->timeout = 200; |
46 |
|
options->console_size = 1000; |
|
|
48 |
|
options->console_size = 1024; |
47 |
49 |
|
|
48 |
50 |
} |
} |
49 |
51 |
|
|
|
... |
... |
static void pms_handle_mpd_idle_update(struct mpd_connection * connection, enum |
133 |
135 |
|
|
134 |
136 |
} |
} |
135 |
137 |
|
|
136 |
|
static void * pms_thread_mpd(void * threadid) { |
|
137 |
|
|
|
138 |
|
struct mpd_connection * connection = NULL; |
|
139 |
|
bool is_idle = false; |
|
140 |
|
enum mpd_idle flags = -1; |
|
141 |
|
|
|
142 |
|
connection = pms_mpd_connect(); |
|
143 |
|
|
|
144 |
|
while(pms_state->running) { |
|
145 |
|
if (connection) { |
|
146 |
|
if (pms_state->status != NULL && flags == 0) { |
|
147 |
|
mpd_send_idle(connection); |
|
148 |
|
flags = mpd_recv_idle(connection, true); |
|
149 |
|
} else { |
|
150 |
|
pms_handle_mpd_idle_update(connection, flags); |
|
151 |
|
topbar_draw(); |
|
152 |
|
flags = 0; |
|
153 |
|
} |
|
154 |
|
} else { |
|
155 |
|
console("No connection, sleeping..."); |
|
156 |
|
sleep(1); |
|
157 |
|
} |
|
158 |
|
} |
|
159 |
|
|
|
160 |
|
pthread_exit(NULL); |
|
161 |
|
|
|
162 |
|
} |
|
163 |
|
|
|
164 |
|
static void pms_start_threads() { |
|
165 |
|
pthread_t thread_mpd; |
|
166 |
|
if (pthread_create(&thread_mpd, NULL, pms_thread_mpd, NULL)) { |
|
167 |
|
fatal(PMS_EXIT_THREAD, "Cannot create MPD thread\n"); |
|
168 |
|
} |
|
169 |
|
} |
|
170 |
|
|
|
171 |
138 |
void signal_resize(int signal) { |
void signal_resize(int signal) { |
172 |
139 |
console("Resized to %d x %d", LINES, COLS); |
console("Resized to %d x %d", LINES, COLS); |
173 |
140 |
pms_curses_lock(); |
pms_curses_lock(); |
|
... |
... |
static void signal_init() { |
187 |
154 |
signal(SIGTERM, signal_kill); |
signal(SIGTERM, signal_kill); |
188 |
155 |
} |
} |
189 |
156 |
|
|
|
157 |
|
int get_input(struct mpd_connection * connection) { |
|
158 |
|
struct timeval tv; |
|
159 |
|
int mpd_fd; |
|
160 |
|
int retval; |
|
161 |
|
int flags = 0; |
|
162 |
|
fd_set fds; |
|
163 |
|
|
|
164 |
|
tv.tv_sec = 1; |
|
165 |
|
tv.tv_usec = 0; |
|
166 |
|
mpd_fd = mpd_connection_get_fd(connection); |
|
167 |
|
|
|
168 |
|
FD_ZERO(&fds); |
|
169 |
|
FD_SET(STDIN_FILENO, &fds); |
|
170 |
|
FD_SET(mpd_fd, &fds); |
|
171 |
|
|
|
172 |
|
retval = select(mpd_fd+1, &fds, NULL, NULL, &tv); |
|
173 |
|
|
|
174 |
|
if (retval == -1) { |
|
175 |
|
// FIXME |
|
176 |
|
} else if (retval > 0) { |
|
177 |
|
if (FD_ISSET(mpd_fd, &fds)) { |
|
178 |
|
console("Got something from MPD"); |
|
179 |
|
flags |= PMS_HAS_INPUT_MPD; |
|
180 |
|
} |
|
181 |
|
if (FD_ISSET(STDIN_FILENO, &fds)) { |
|
182 |
|
console("Console key input"); |
|
183 |
|
flags |= PMS_HAS_INPUT_STDIN; |
|
184 |
|
} |
|
185 |
|
} |
|
186 |
|
|
|
187 |
|
return flags; |
|
188 |
|
} |
|
189 |
|
|
190 |
190 |
int main(int argc, char** argv) { |
int main(int argc, char** argv) { |
191 |
191 |
|
|
|
192 |
|
struct mpd_connection * connection = NULL; |
|
193 |
|
bool is_idle = false; |
|
194 |
|
enum mpd_idle flags = -1; |
|
195 |
|
int input_flags = 0; |
|
196 |
|
|
192 |
197 |
reset_options(); |
reset_options(); |
193 |
198 |
pms_state = malloc(sizeof(struct pms_state_t)); |
pms_state = malloc(sizeof(struct pms_state_t)); |
194 |
199 |
memset(pms_state, 0, sizeof(struct pms_state_t)); |
memset(pms_state, 0, sizeof(struct pms_state_t)); |
|
... |
... |
int main(int argc, char** argv) { |
198 |
203 |
console_init(options->console_size); |
console_init(options->console_size); |
199 |
204 |
signal_init(); |
signal_init(); |
200 |
205 |
console("%s %s (c) 2006-2014 Kim Tore Jensen <%s>", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT); |
console("%s %s (c) 2006-2014 Kim Tore Jensen <%s>", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT); |
201 |
|
pms_start_threads(); |
|
202 |
206 |
|
|
203 |
207 |
while(pms_state->running) { |
while(pms_state->running) { |
204 |
|
curses_get_input(); |
|
|
208 |
|
|
|
209 |
|
if (!connection) { |
|
210 |
|
connection = pms_mpd_connect(); |
|
211 |
|
} |
|
212 |
|
|
|
213 |
|
if (connection && !is_idle) { |
|
214 |
|
mpd_send_idle(connection); |
|
215 |
|
is_idle = true; |
|
216 |
|
} |
|
217 |
|
|
|
218 |
|
input_flags = get_input(connection); |
|
219 |
|
|
|
220 |
|
if (input_flags & PMS_HAS_INPUT_MPD) { |
|
221 |
|
flags = mpd_recv_idle(connection, true); |
|
222 |
|
is_idle = false; |
|
223 |
|
pms_handle_mpd_idle_update(connection, flags); |
|
224 |
|
topbar_draw(); |
|
225 |
|
} |
|
226 |
|
|
|
227 |
|
if (input_flags & PMS_HAS_INPUT_STDIN) { |
|
228 |
|
curses_get_input(); |
|
229 |
|
} |
|
230 |
|
|
205 |
231 |
} |
} |
206 |
232 |
|
|
207 |
233 |
curses_shutdown(); |
curses_shutdown(); |