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); | |
10e226b3 RK |
51 | |
52 | /** @brief Callbacks for server state monitoring */ | |
53 | const disorder_eclient_log_callbacks log_callbacks = { | |
58d2aad2 RK |
54 | .connected = log_connected, |
55 | .completed = log_completed, | |
56 | .failed = log_failed, | |
57 | .moved = log_moved, | |
58 | .playing = log_playing, | |
59 | .queue = log_queue, | |
60 | .recent_added = log_recent_added, | |
61 | .recent_removed = log_recent_removed, | |
62 | .removed = log_removed, | |
63 | .scratched = log_scratched, | |
64 | .state = log_state, | |
65 | .volume = log_volume, | |
45e6d04a | 66 | .rescanned = log_rescanned, |
9433fabf | 67 | .rights_changed = log_rights_changed, |
812b526d | 68 | .adopted = log_adopted, |
fc36ecb7 RK |
69 | .playlist_created = log_playlist_created, |
70 | .playlist_modified = log_playlist_modified, | |
71 | .playlist_deleted = log_playlist_deleted, | |
10e226b3 RK |
72 | }; |
73 | ||
219dc95c | 74 | /** @brief Update everything */ |
10e226b3 | 75 | void all_update(void) { |
fb44b59e | 76 | ++suppress_actions; |
3569ddb8 RK |
77 | event_raise("queue-changed", 0); |
78 | event_raise("recent-changed", 0); | |
1f868ca3 | 79 | event_raise("volume-changed", 0); |
00e0c652 | 80 | event_raise("rescan-complete", 0); |
fb44b59e | 81 | --suppress_actions; |
10e226b3 RK |
82 | } |
83 | ||
219dc95c RK |
84 | /** @brief Called when the client connects |
85 | * | |
86 | * Depending on server and network state the TCP connection to the server may | |
87 | * go up or down many times during the lifetime of Disobedience. This function | |
88 | * is called whenever it connects. | |
219dc95c | 89 | */ |
10e226b3 | 90 | static void log_connected(void attribute((unused)) *v) { |
10e226b3 RK |
91 | /* Don't know what we might have missed while disconnected so update |
92 | * everything. We get this at startup too and this is how we do the initial | |
93 | * state fetch. */ | |
94 | all_update(); | |
00e0c652 | 95 | event_raise("log-connected", 0); |
10e226b3 RK |
96 | } |
97 | ||
219dc95c | 98 | /** @brief Called when the current track finishes playing */ |
10e226b3 RK |
99 | static void log_completed(void attribute((unused)) *v, |
100 | const char attribute((unused)) *track) { | |
10e226b3 RK |
101 | } |
102 | ||
219dc95c | 103 | /** @brief Called when the current track fails */ |
10e226b3 RK |
104 | static void log_failed(void attribute((unused)) *v, |
105 | const char attribute((unused)) *track, | |
106 | const char attribute((unused)) *status) { | |
10e226b3 RK |
107 | } |
108 | ||
219dc95c | 109 | /** @brief Called when some track is moved within the queue */ |
10e226b3 RK |
110 | static void log_moved(void attribute((unused)) *v, |
111 | const char attribute((unused)) *user) { | |
3569ddb8 | 112 | event_raise("queue-changed", 0); |
10e226b3 RK |
113 | } |
114 | ||
115 | static void log_playing(void attribute((unused)) *v, | |
116 | const char attribute((unused)) *track, | |
117 | const char attribute((unused)) *user) { | |
443c003c | 118 | event_raise("playing-started", 0); |
10e226b3 RK |
119 | } |
120 | ||
219dc95c | 121 | /** @brief Called when a track is added to the queue */ |
10e226b3 RK |
122 | static void log_queue(void attribute((unused)) *v, |
123 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 124 | event_raise("queue-changed", 0); |
10e226b3 RK |
125 | } |
126 | ||
219dc95c | 127 | /** @brief Called when a track is added to the recently-played list */ |
10e226b3 RK |
128 | static void log_recent_added(void attribute((unused)) *v, |
129 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 130 | event_raise("recent-changed", 0); |
10e226b3 RK |
131 | } |
132 | ||
219dc95c RK |
133 | /** @brief Called when a track is removed from the recently-played list |
134 | * | |
135 | * We do nothing here - log_recent_added() suffices. | |
136 | */ | |
10e226b3 RK |
137 | static void log_recent_removed(void attribute((unused)) *v, |
138 | const char attribute((unused)) *id) { | |
139 | /* nothing - log_recent_added() will trigger the relevant update */ | |
140 | } | |
141 | ||
219dc95c | 142 | /** @brief Called when a track is removed from the queue */ |
10e226b3 RK |
143 | static void log_removed(void attribute((unused)) *v, |
144 | const char attribute((unused)) *id, | |
145 | const char attribute((unused)) *user) { | |
3569ddb8 | 146 | event_raise("queue-changed", 0); |
10e226b3 RK |
147 | } |
148 | ||
219dc95c | 149 | /** @brief Called when the current track is scratched */ |
10e226b3 RK |
150 | static void log_scratched(void attribute((unused)) *v, |
151 | const char attribute((unused)) *track, | |
152 | const char attribute((unused)) *user) { | |
10e226b3 RK |
153 | } |
154 | ||
a8cd6f84 RK |
155 | /** @brief Map from state bits to state change events */ |
156 | static const struct { | |
157 | unsigned long bit; | |
158 | const char *event; | |
159 | } state_events[] = { | |
160 | { DISORDER_PLAYING_ENABLED, "enabled-changed" }, | |
161 | { DISORDER_RANDOM_ENABLED, "random-changed" }, | |
162 | { DISORDER_TRACK_PAUSED, "pause-changed" }, | |
163 | { DISORDER_PLAYING, "playing-changed" }, | |
f513e538 | 164 | { DISORDER_CONNECTED, "connected-changed" }, |
a8cd6f84 RK |
165 | }; |
166 | #define NSTATE_EVENTS (sizeof state_events / sizeof *state_events) | |
167 | ||
219dc95c | 168 | /** @brief Called when a state change occurs */ |
10e226b3 RK |
169 | static void log_state(void attribute((unused)) *v, |
170 | unsigned long state) { | |
6d1302f0 RK |
171 | unsigned long changes = state ^ last_state; |
172 | static int first = 1; | |
fb44b59e RK |
173 | |
174 | ++suppress_actions; | |
6d1302f0 RK |
175 | if(first) { |
176 | changes = -1UL; | |
177 | first = 0; | |
178 | } | |
179 | D(("log_state old=%s new=%s changed=%s", | |
180 | disorder_eclient_interpret_state(last_state), | |
181 | disorder_eclient_interpret_state(state), | |
182 | disorder_eclient_interpret_state(changes))); | |
00959300 | 183 | last_state = state; |
a8cd6f84 RK |
184 | /* Notify interested parties what has changed */ |
185 | for(unsigned n = 0; n < NSTATE_EVENTS; ++n) | |
186 | if(changes & state_events[n].bit) | |
187 | event_raise(state_events[n].event, 0); | |
fb44b59e | 188 | --suppress_actions; |
10e226b3 RK |
189 | } |
190 | ||
219dc95c | 191 | /** @brief Called when volume changes */ |
10e226b3 RK |
192 | static void log_volume(void attribute((unused)) *v, |
193 | int l, int r) { | |
321f7536 | 194 | if(!rtp_supported && (volume_l != l || volume_r != r)) { |
10e226b3 RK |
195 | volume_l = l; |
196 | volume_r = r; | |
fb44b59e | 197 | ++suppress_actions; |
1f868ca3 | 198 | event_raise("volume-changed", 0); |
fb44b59e | 199 | --suppress_actions; |
10e226b3 RK |
200 | } |
201 | } | |
202 | ||
e025abff RK |
203 | /** @brief Called when a rescan completes */ |
204 | static void log_rescanned(void attribute((unused)) *v) { | |
00e0c652 | 205 | event_raise("rescan-complete", 0); |
e025abff RK |
206 | } |
207 | ||
45e6d04a RK |
208 | /** @brief Called when our rights change */ |
209 | static void log_rights_changed(void attribute((unused)) *v, | |
210 | rights_type new_rights) { | |
211 | ++suppress_actions; | |
212 | last_rights = new_rights; | |
213 | event_raise("rights-changed", 0); | |
214 | --suppress_actions; | |
215 | } | |
216 | ||
9433fabf RK |
217 | /** @brief Called when a track is adopted */ |
218 | static void log_adopted(void attribute((unused)) *v, | |
219 | const char attribute((unused)) *id, | |
220 | const char attribute((unused)) *who) { | |
221 | event_raise("queue-changed", 0); | |
222 | } | |
223 | ||
fc36ecb7 RK |
224 | static void log_playlist_created(void attribute((unused)) *v, |
225 | const char *playlist, | |
226 | const char attribute((unused)) *sharing) { | |
227 | event_raise("playlist-created", (void *)playlist); | |
228 | } | |
229 | ||
230 | static void log_playlist_modified(void attribute((unused)) *v, | |
231 | const char *playlist, | |
232 | const char attribute((unused)) *sharing) { | |
233 | event_raise("playlist-modified", (void *)playlist); | |
234 | } | |
235 | ||
236 | static void log_playlist_deleted(void attribute((unused)) *v, | |
237 | const char *playlist) { | |
238 | event_raise("playlist-deleted", (void *)playlist); | |
239 | } | |
240 | ||
10e226b3 RK |
241 | /* |
242 | Local Variables: | |
243 | c-basic-offset:2 | |
244 | comment-column:40 | |
245 | fill-column:79 | |
246 | indent-tabs-mode:nil | |
247 | End: | |
248 | */ |