Commit | Line | Data |
---|---|---|
10e226b3 RK |
1 | /* |
2 | * This file is part of DisOrder. | |
3 | * Copyright (C) 2006, 2007 Richard Kettlewell | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
219dc95c RK |
20 | /** @file disobedience/log.c |
21 | * @brief State monitoring | |
22 | * | |
23 | * Disobedience relies on the server to tell when essentially anything changes, | |
24 | * even if it initiated the change itself. It uses the @c log command to | |
25 | * achieve this. | |
26 | */ | |
10e226b3 RK |
27 | |
28 | #include "disobedience.h" | |
29 | ||
186f896b | 30 | /* State monitoring -------------------------------------------------------- */ |
10e226b3 RK |
31 | |
32 | static void log_connected(void *v); | |
33 | static void log_completed(void *v, const char *track); | |
34 | static void log_failed(void *v, const char *track, const char *status); | |
35 | static void log_moved(void *v, const char *user); | |
36 | static void log_playing(void *v, const char *track, const char *user); | |
37 | static void log_queue(void *v, struct queue_entry *q); | |
38 | static void log_recent_added(void *v, struct queue_entry *q); | |
39 | static void log_recent_removed(void *v, const char *id); | |
40 | static void log_removed(void *v, const char *id, const char *user); | |
41 | static void log_scratched(void *v, const char *track, const char *user); | |
42 | static void log_state(void *v, unsigned long state); | |
43 | static void log_volume(void *v, int l, int r); | |
e025abff | 44 | static void log_rescanned(void *v); |
10e226b3 RK |
45 | |
46 | /** @brief Callbacks for server state monitoring */ | |
47 | const disorder_eclient_log_callbacks log_callbacks = { | |
48 | log_connected, | |
49 | log_completed, | |
50 | log_failed, | |
51 | log_moved, | |
52 | log_playing, | |
53 | log_queue, | |
54 | log_recent_added, | |
55 | log_recent_removed, | |
56 | log_removed, | |
57 | log_scratched, | |
58 | log_state, | |
e025abff RK |
59 | log_volume, |
60 | log_rescanned | |
10e226b3 RK |
61 | }; |
62 | ||
219dc95c RK |
63 | /** @brief State monitor |
64 | * | |
65 | * We keep a linked list of everything that is interested in state changes. | |
66 | */ | |
186f896b | 67 | struct monitor { |
219dc95c | 68 | /** @brief Next monitor */ |
186f896b | 69 | struct monitor *next; |
219dc95c RK |
70 | |
71 | /** @brief State bits of interest */ | |
186f896b | 72 | unsigned long mask; |
219dc95c RK |
73 | |
74 | /** @brief Function to call if any of @c mask change */ | |
186f896b | 75 | monitor_callback *callback; |
219dc95c RK |
76 | |
77 | /** @brief User data for callback */ | |
186f896b RK |
78 | void *u; |
79 | }; | |
80 | ||
219dc95c | 81 | /** @brief List of monitors */ |
186f896b | 82 | static struct monitor *monitors; |
10e226b3 | 83 | |
219dc95c | 84 | /** @brief Update everything */ |
10e226b3 | 85 | void all_update(void) { |
fb44b59e | 86 | ++suppress_actions; |
3569ddb8 RK |
87 | event_raise("queue-changed", 0); |
88 | event_raise("recent-changed", 0); | |
1f868ca3 | 89 | event_raise("volume-changed", 0); |
3569ddb8 | 90 | event_raise("added-changed", 0); |
fb44b59e | 91 | --suppress_actions; |
10e226b3 RK |
92 | } |
93 | ||
219dc95c RK |
94 | /** @brief Called when the client connects |
95 | * | |
96 | * Depending on server and network state the TCP connection to the server may | |
97 | * go up or down many times during the lifetime of Disobedience. This function | |
98 | * is called whenever it connects. | |
99 | * | |
100 | * The intent is to use the monitor logic to achieve this in future. | |
101 | */ | |
10e226b3 | 102 | static void log_connected(void attribute((unused)) *v) { |
10e226b3 RK |
103 | /* Don't know what we might have missed while disconnected so update |
104 | * everything. We get this at startup too and this is how we do the initial | |
105 | * state fetch. */ | |
106 | all_update(); | |
10e226b3 RK |
107 | } |
108 | ||
219dc95c | 109 | /** @brief Called when the current track finishes playing */ |
10e226b3 RK |
110 | static void log_completed(void attribute((unused)) *v, |
111 | const char attribute((unused)) *track) { | |
10e226b3 RK |
112 | } |
113 | ||
219dc95c | 114 | /** @brief Called when the current track fails */ |
10e226b3 RK |
115 | static void log_failed(void attribute((unused)) *v, |
116 | const char attribute((unused)) *track, | |
117 | const char attribute((unused)) *status) { | |
10e226b3 RK |
118 | } |
119 | ||
219dc95c | 120 | /** @brief Called when some track is moved within the queue */ |
10e226b3 RK |
121 | static void log_moved(void attribute((unused)) *v, |
122 | const char attribute((unused)) *user) { | |
3569ddb8 | 123 | event_raise("queue-changed", 0); |
10e226b3 RK |
124 | } |
125 | ||
126 | static void log_playing(void attribute((unused)) *v, | |
127 | const char attribute((unused)) *track, | |
128 | const char attribute((unused)) *user) { | |
10e226b3 RK |
129 | } |
130 | ||
219dc95c | 131 | /** @brief Called when a track is added to the queue */ |
10e226b3 RK |
132 | static void log_queue(void attribute((unused)) *v, |
133 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 134 | event_raise("queue-changed", 0); |
10e226b3 RK |
135 | } |
136 | ||
219dc95c | 137 | /** @brief Called when a track is added to the recently-played list */ |
10e226b3 RK |
138 | static void log_recent_added(void attribute((unused)) *v, |
139 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 140 | event_raise("recent-changed", 0); |
10e226b3 RK |
141 | } |
142 | ||
219dc95c RK |
143 | /** @brief Called when a track is removed from the recently-played list |
144 | * | |
145 | * We do nothing here - log_recent_added() suffices. | |
146 | */ | |
10e226b3 RK |
147 | static void log_recent_removed(void attribute((unused)) *v, |
148 | const char attribute((unused)) *id) { | |
149 | /* nothing - log_recent_added() will trigger the relevant update */ | |
150 | } | |
151 | ||
219dc95c | 152 | /** @brief Called when a track is removed from the queue */ |
10e226b3 RK |
153 | static void log_removed(void attribute((unused)) *v, |
154 | const char attribute((unused)) *id, | |
155 | const char attribute((unused)) *user) { | |
3569ddb8 | 156 | event_raise("queue-changed", 0); |
10e226b3 RK |
157 | } |
158 | ||
219dc95c | 159 | /** @brief Called when the current track is scratched */ |
10e226b3 RK |
160 | static void log_scratched(void attribute((unused)) *v, |
161 | const char attribute((unused)) *track, | |
162 | const char attribute((unused)) *user) { | |
10e226b3 RK |
163 | } |
164 | ||
a8cd6f84 RK |
165 | /** @brief Map from state bits to state change events */ |
166 | static const struct { | |
167 | unsigned long bit; | |
168 | const char *event; | |
169 | } state_events[] = { | |
170 | { DISORDER_PLAYING_ENABLED, "enabled-changed" }, | |
171 | { DISORDER_RANDOM_ENABLED, "random-changed" }, | |
172 | { DISORDER_TRACK_PAUSED, "pause-changed" }, | |
173 | { DISORDER_PLAYING, "playing-changed" }, | |
174 | }; | |
175 | #define NSTATE_EVENTS (sizeof state_events / sizeof *state_events) | |
176 | ||
219dc95c | 177 | /** @brief Called when a state change occurs */ |
10e226b3 RK |
178 | static void log_state(void attribute((unused)) *v, |
179 | unsigned long state) { | |
186f896b | 180 | const struct monitor *m; |
6d1302f0 RK |
181 | unsigned long changes = state ^ last_state; |
182 | static int first = 1; | |
fb44b59e RK |
183 | |
184 | ++suppress_actions; | |
6d1302f0 RK |
185 | if(first) { |
186 | changes = -1UL; | |
187 | first = 0; | |
188 | } | |
189 | D(("log_state old=%s new=%s changed=%s", | |
190 | disorder_eclient_interpret_state(last_state), | |
191 | disorder_eclient_interpret_state(state), | |
192 | disorder_eclient_interpret_state(changes))); | |
00959300 | 193 | last_state = state; |
a8cd6f84 RK |
194 | /* Notify interested parties what has changed */ |
195 | for(unsigned n = 0; n < NSTATE_EVENTS; ++n) | |
196 | if(changes & state_events[n].bit) | |
197 | event_raise(state_events[n].event, 0); | |
186f896b RK |
198 | /* Tell anything that cares about the state change */ |
199 | for(m = monitors; m; m = m->next) { | |
00959300 RK |
200 | if(changes & m->mask) |
201 | m->callback(m->u); | |
186f896b | 202 | } |
fb44b59e | 203 | --suppress_actions; |
10e226b3 RK |
204 | } |
205 | ||
219dc95c | 206 | /** @brief Called when volume changes */ |
10e226b3 RK |
207 | static void log_volume(void attribute((unused)) *v, |
208 | int l, int r) { | |
321f7536 | 209 | if(!rtp_supported && (volume_l != l || volume_r != r)) { |
10e226b3 RK |
210 | volume_l = l; |
211 | volume_r = r; | |
fb44b59e | 212 | ++suppress_actions; |
1f868ca3 | 213 | event_raise("volume-changed", 0); |
fb44b59e | 214 | --suppress_actions; |
10e226b3 RK |
215 | } |
216 | } | |
217 | ||
e025abff RK |
218 | /** @brief Called when a rescan completes */ |
219 | static void log_rescanned(void attribute((unused)) *v) { | |
3569ddb8 | 220 | event_raise("added-changed", 0); |
e025abff RK |
221 | } |
222 | ||
3ffa2d15 RK |
223 | /** @brief Add a monitor to the list |
224 | * @param callback Function to call | |
225 | * @param u User data to pass to @p callback | |
226 | * @param mask Mask of flags that @p callback cares about | |
227 | * | |
228 | * Pass @p mask as -1UL to match all flags. | |
229 | */ | |
186f896b RK |
230 | void register_monitor(monitor_callback *callback, |
231 | void *u, | |
232 | unsigned long mask) { | |
233 | struct monitor *m = xmalloc(sizeof *m); | |
234 | ||
235 | m->next = monitors; | |
236 | m->mask = mask; | |
237 | m->callback = callback; | |
238 | m->u = u; | |
239 | monitors = m; | |
240 | } | |
241 | ||
10e226b3 RK |
242 | /* |
243 | Local Variables: | |
244 | c-basic-offset:2 | |
245 | comment-column:40 | |
246 | fill-column:79 | |
247 | indent-tabs-mode:nil | |
248 | End: | |
249 | */ |