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