File src/mpd.c added (mode: 100644) (index 0000000..0305d0d) |
|
1 |
|
/* vi:set ts=4 sts=4 sw=4 et: |
|
2 |
|
* |
|
3 |
|
* Practical Music Search |
|
4 |
|
* Copyright (c) 2006-2014 Kim Tore Jensen |
|
5 |
|
* |
|
6 |
|
* This program is free software: you can redistribute it and/or modify |
|
7 |
|
* it under the terms of the GNU General Public License as published by |
|
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
|
9 |
|
* (at your option) any later version. |
|
10 |
|
* |
|
11 |
|
* This program is distributed in the hope that it will be useful, |
|
12 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
|
* GNU General Public License for more details. |
|
15 |
|
* |
|
16 |
|
* You should have received a copy of the GNU General Public License |
|
17 |
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
*/ |
|
19 |
|
|
|
20 |
|
#include "pms.h" |
|
21 |
|
|
|
22 |
|
struct mpd_connection * pms_mpd_connect(const char * server, unsigned int port, unsigned int timeout) { |
|
23 |
|
|
|
24 |
|
enum mpd_error status; |
|
25 |
|
const char * error_msg; |
|
26 |
|
struct mpd_connection * connection; |
|
27 |
|
|
|
28 |
|
console("Connecting to %s...", server); |
|
29 |
|
|
|
30 |
|
connection = mpd_connection_new(server, port, timeout); |
|
31 |
|
if (connection == NULL) { |
|
32 |
|
fatal(PMS_EXIT_MEMORY, "mpd connect: out of memory\n"); |
|
33 |
|
} |
|
34 |
|
|
|
35 |
|
status = mpd_connection_get_error(connection); |
|
36 |
|
if (status == MPD_ERROR_SUCCESS) { |
|
37 |
|
console("Connected to %s", server); |
|
38 |
|
} else { |
|
39 |
|
error_msg = mpd_connection_get_error_message(connection); |
|
40 |
|
console("Error connecting to %s: error %d: %s", server, status, error_msg); |
|
41 |
|
mpd_connection_free(connection); |
|
42 |
|
connection = NULL; |
|
43 |
|
} |
|
44 |
|
|
|
45 |
|
return connection; |
|
46 |
|
} |
|
47 |
|
|
|
48 |
|
void pms_get_mpd_status(struct mpd_connection * connection, struct pms_state_t * state) { |
|
49 |
|
if (state->status) { |
|
50 |
|
mpd_status_free(state->status); |
|
51 |
|
} |
|
52 |
|
state->status = mpd_run_status(connection); |
|
53 |
|
} |
|
54 |
|
|
|
55 |
|
void pms_handle_mpd_idle_update(struct mpd_connection * connection, struct pms_state_t * state, enum mpd_idle flags) { |
|
56 |
|
|
|
57 |
|
console("pms_handle_mpd_idle_update %d", flags); |
|
58 |
|
|
|
59 |
|
if (flags & MPD_IDLE_DATABASE) { |
|
60 |
|
console("Database has been updated."); |
|
61 |
|
} |
|
62 |
|
if (flags & MPD_IDLE_STORED_PLAYLIST) { |
|
63 |
|
console("Stored playlists have been updated."); |
|
64 |
|
} |
|
65 |
|
if (flags & MPD_IDLE_QUEUE) { |
|
66 |
|
console("The queue has been updated."); |
|
67 |
|
} |
|
68 |
|
if (flags & MPD_IDLE_PLAYER) { |
|
69 |
|
console("Player state has changed."); |
|
70 |
|
} |
|
71 |
|
if (flags & MPD_IDLE_MIXER) { |
|
72 |
|
console("Mixer parameters have changed."); |
|
73 |
|
} |
|
74 |
|
if (flags & MPD_IDLE_OUTPUT) { |
|
75 |
|
console("Outputs have changed."); |
|
76 |
|
} |
|
77 |
|
if (flags & MPD_IDLE_OPTIONS) { |
|
78 |
|
console("Options have changed."); |
|
79 |
|
} |
|
80 |
|
if (flags & MPD_IDLE_UPDATE) { |
|
81 |
|
console("Database update has started or finished."); |
|
82 |
|
} |
|
83 |
|
|
|
84 |
|
if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) { |
|
85 |
|
pms_get_mpd_status(connection, state); |
|
86 |
|
} |
|
87 |
|
|
|
88 |
|
topbar_draw(); |
|
89 |
|
|
|
90 |
|
} |
|
91 |
|
|
File src/pms.c changed (mode: 100644) (index 911a4dc..9c8fca3) |
25 |
25 |
#include <sys/select.h> |
#include <sys/select.h> |
26 |
26 |
#include <unistd.h> |
#include <unistd.h> |
27 |
27 |
#include <errno.h> |
#include <errno.h> |
|
28 |
|
|
28 |
29 |
#include "pms.h" |
#include "pms.h" |
29 |
30 |
|
|
30 |
31 |
struct options_t * options = NULL; |
struct options_t * options = NULL; |
|
... |
... |
void shutdown() { |
76 |
77 |
pms_state->running = 0; |
pms_state->running = 0; |
77 |
78 |
} |
} |
78 |
79 |
|
|
79 |
|
static struct mpd_connection * pms_mpd_connect() { |
|
80 |
|
|
|
81 |
|
enum mpd_error status; |
|
82 |
|
const char * error_msg; |
|
83 |
|
struct mpd_connection * connection; |
|
84 |
|
|
|
85 |
|
console("Connecting to %s...", options->server); |
|
86 |
|
|
|
87 |
|
connection = mpd_connection_new(options->server, options->port, options->timeout); |
|
88 |
|
if (connection == NULL) { |
|
89 |
|
fatal(PMS_EXIT_MEMORY, "mpd connect: out of memory\n"); |
|
90 |
|
} |
|
91 |
|
|
|
92 |
|
status = mpd_connection_get_error(connection); |
|
93 |
|
if (status == MPD_ERROR_SUCCESS) { |
|
94 |
|
console("Connected to %s", options->server); |
|
95 |
|
} else { |
|
96 |
|
error_msg = mpd_connection_get_error_message(connection); |
|
97 |
|
console("Error connecting to %s: error %d: %s", options->server, status, error_msg); |
|
98 |
|
mpd_connection_free(connection); |
|
99 |
|
connection = NULL; |
|
100 |
|
} |
|
101 |
|
|
|
102 |
|
return connection; |
|
103 |
|
} |
|
104 |
|
|
|
105 |
|
static void pms_get_mpd_state(struct mpd_connection * connection) { |
|
106 |
|
|
|
107 |
|
pms_status_lock(); |
|
108 |
|
if (pms_state->status) { |
|
109 |
|
mpd_status_free(pms_state->status); |
|
110 |
|
} |
|
111 |
|
pms_state->status = mpd_run_status(connection); |
|
112 |
|
pms_status_unlock(); |
|
113 |
|
|
|
114 |
|
} |
|
115 |
|
|
|
116 |
|
static void pms_handle_mpd_idle_update(struct mpd_connection * connection, enum mpd_idle flags) { |
|
117 |
|
|
|
118 |
|
console("pms_handle_mpd_idle_update %d", flags); |
|
119 |
|
|
|
120 |
|
if (flags & MPD_IDLE_DATABASE) { |
|
121 |
|
console("Database has been updated."); |
|
122 |
|
} |
|
123 |
|
if (flags & MPD_IDLE_STORED_PLAYLIST) { |
|
124 |
|
console("Stored playlists have been updated."); |
|
125 |
|
} |
|
126 |
|
if (flags & MPD_IDLE_QUEUE) { |
|
127 |
|
console("The queue has been updated."); |
|
128 |
|
} |
|
129 |
|
if (flags & MPD_IDLE_PLAYER) { |
|
130 |
|
console("Player state has changed."); |
|
131 |
|
} |
|
132 |
|
if (flags & MPD_IDLE_MIXER) { |
|
133 |
|
console("Mixer parameters have changed."); |
|
134 |
|
} |
|
135 |
|
if (flags & MPD_IDLE_OUTPUT) { |
|
136 |
|
console("Outputs have changed."); |
|
137 |
|
} |
|
138 |
|
if (flags & MPD_IDLE_OPTIONS) { |
|
139 |
|
console("Options have changed."); |
|
140 |
|
} |
|
141 |
|
if (flags & MPD_IDLE_UPDATE) { |
|
142 |
|
console("Database update has started or finished."); |
|
143 |
|
} |
|
144 |
|
|
|
145 |
|
if (flags & (MPD_IDLE_QUEUE | MPD_IDLE_PLAYER | MPD_IDLE_MIXER | MPD_IDLE_OPTIONS)) { |
|
146 |
|
pms_get_mpd_state(connection); |
|
147 |
|
} |
|
148 |
|
|
|
149 |
|
topbar_draw(); |
|
150 |
|
|
|
151 |
|
} |
|
152 |
|
|
|
153 |
80 |
void signal_resize(int signal) { |
void signal_resize(int signal) { |
154 |
81 |
console("Resized to %d x %d", LINES, COLS); |
console("Resized to %d x %d", LINES, COLS); |
155 |
82 |
pms_curses_lock(); |
pms_curses_lock(); |
|
... |
... |
static void signal_init() { |
170 |
97 |
} |
} |
171 |
98 |
|
|
172 |
99 |
int pms_get_pending_input_flags(struct mpd_connection * connection) { |
int pms_get_pending_input_flags(struct mpd_connection * connection) { |
|
100 |
|
|
173 |
101 |
struct timeval tv; |
struct timeval tv; |
174 |
102 |
int mpd_fd = 0; |
int mpd_fd = 0; |
175 |
103 |
int retval; |
int retval; |
|
... |
... |
int main(int argc, char** argv) { |
226 |
154 |
while(pms_state->running) { |
while(pms_state->running) { |
227 |
155 |
|
|
228 |
156 |
if (!connection) { |
if (!connection) { |
229 |
|
if (connection = pms_mpd_connect()) { |
|
230 |
|
pms_handle_mpd_idle_update(connection, -1); |
|
|
157 |
|
if (connection = pms_mpd_connect(options->server, options->port, options->timeout)) { |
|
158 |
|
pms_handle_mpd_idle_update(connection, pms_state, -1); |
231 |
159 |
} |
} |
232 |
160 |
} |
} |
233 |
161 |
|
|
|
... |
... |
int main(int argc, char** argv) { |
241 |
169 |
if (input_flags & PMS_HAS_INPUT_MPD) { |
if (input_flags & PMS_HAS_INPUT_MPD) { |
242 |
170 |
flags = mpd_recv_idle(connection, true); |
flags = mpd_recv_idle(connection, true); |
243 |
171 |
is_idle = false; |
is_idle = false; |
244 |
|
pms_handle_mpd_idle_update(connection, flags); |
|
|
172 |
|
pms_handle_mpd_idle_update(connection, pms_state, -1); |
245 |
173 |
} |
} |
246 |
174 |
|
|
247 |
175 |
if (input_flags & PMS_HAS_INPUT_STDIN) { |
if (input_flags & PMS_HAS_INPUT_STDIN) { |