xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
Commit 51833fece026c759a947a5aa96f9ea00dfe4be0e

Use select() to check whether MPD or stdin has input data.
Author: Kim Tore Jensen
Author date (UTC): 2014-09-27 19:26
Committer name: Kim Tore Jensen
Committer date (UTC): 2014-09-27 19:26
Parent(s): 619db1491808b24be6a6292d2b19ee16aa10dfdb
Signing key:
Tree: 8434844d4ef1cadc313e160bc6dab8fc8f1b5b23
File Lines added Lines deleted
src/pms.c 64 38
src/pms.h 3 0
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();
File src/pms.h changed (mode: 100644) (index 67040dd..7bd5503)
38 38 #define PMS_EXIT_THREAD 3 #define PMS_EXIT_THREAD 3
39 39 #define PMS_EXIT_KILLED 4 #define PMS_EXIT_KILLED 4
40 40
41 #define PMS_HAS_INPUT_STDIN 1
42 #define PMS_HAS_INPUT_MPD 2
43
41 44 struct options_t { struct options_t {
42 45 char * server; char * server;
43 46 unsigned int port; unsigned int port;
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master