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); | |
44 | ||
45 | /** @brief Callbacks for server state monitoring */ | |
46 | const disorder_eclient_log_callbacks log_callbacks = { | |
47 | log_connected, | |
48 | log_completed, | |
49 | log_failed, | |
50 | log_moved, | |
51 | log_playing, | |
52 | log_queue, | |
53 | log_recent_added, | |
54 | log_recent_removed, | |
55 | log_removed, | |
56 | log_scratched, | |
57 | log_state, | |
58 | log_volume | |
59 | }; | |
60 | ||
219dc95c RK |
61 | /** @brief State monitor |
62 | * | |
63 | * We keep a linked list of everything that is interested in state changes. | |
64 | */ | |
186f896b | 65 | struct monitor { |
219dc95c | 66 | /** @brief Next monitor */ |
186f896b | 67 | struct monitor *next; |
219dc95c RK |
68 | |
69 | /** @brief State bits of interest */ | |
186f896b | 70 | unsigned long mask; |
219dc95c RK |
71 | |
72 | /** @brief Function to call if any of @c mask change */ | |
186f896b | 73 | monitor_callback *callback; |
219dc95c RK |
74 | |
75 | /** @brief User data for callback */ | |
186f896b RK |
76 | void *u; |
77 | }; | |
78 | ||
219dc95c | 79 | /** @brief List of monitors */ |
186f896b | 80 | static struct monitor *monitors; |
10e226b3 | 81 | |
219dc95c | 82 | /** @brief Update everything */ |
10e226b3 | 83 | void all_update(void) { |
10e226b3 RK |
84 | queue_update(); |
85 | recent_update(); | |
6d1302f0 | 86 | volume_update(); |
4eb1f430 | 87 | added_update(); |
10e226b3 RK |
88 | } |
89 | ||
219dc95c RK |
90 | /** @brief Called when the client connects |
91 | * | |
92 | * Depending on server and network state the TCP connection to the server may | |
93 | * go up or down many times during the lifetime of Disobedience. This function | |
94 | * is called whenever it connects. | |
95 | * | |
96 | * The intent is to use the monitor logic to achieve this in future. | |
97 | */ | |
10e226b3 | 98 | static void log_connected(void attribute((unused)) *v) { |
10e226b3 RK |
99 | /* Don't know what we might have missed while disconnected so update |
100 | * everything. We get this at startup too and this is how we do the initial | |
101 | * state fetch. */ | |
102 | all_update(); | |
10e226b3 RK |
103 | } |
104 | ||
219dc95c | 105 | /** @brief Called when the current track finishes playing */ |
10e226b3 RK |
106 | static void log_completed(void attribute((unused)) *v, |
107 | const char attribute((unused)) *track) { | |
10e226b3 RK |
108 | } |
109 | ||
219dc95c | 110 | /** @brief Called when the current track fails */ |
10e226b3 RK |
111 | static void log_failed(void attribute((unused)) *v, |
112 | const char attribute((unused)) *track, | |
113 | const char attribute((unused)) *status) { | |
10e226b3 RK |
114 | } |
115 | ||
219dc95c | 116 | /** @brief Called when some track is moved within the queue */ |
10e226b3 RK |
117 | static void log_moved(void attribute((unused)) *v, |
118 | const char attribute((unused)) *user) { | |
219dc95c | 119 | queue_update(); |
10e226b3 RK |
120 | } |
121 | ||
122 | static void log_playing(void attribute((unused)) *v, | |
123 | const char attribute((unused)) *track, | |
124 | const char attribute((unused)) *user) { | |
10e226b3 RK |
125 | } |
126 | ||
219dc95c | 127 | /** @brief Called when a track is added to the queue */ |
10e226b3 RK |
128 | static void log_queue(void attribute((unused)) *v, |
129 | struct queue_entry attribute((unused)) *q) { | |
130 | queue_update(); | |
131 | } | |
132 | ||
219dc95c | 133 | /** @brief Called when a track is added to the recently-played list */ |
10e226b3 RK |
134 | static void log_recent_added(void attribute((unused)) *v, |
135 | struct queue_entry attribute((unused)) *q) { | |
136 | recent_update(); | |
137 | } | |
138 | ||
219dc95c RK |
139 | /** @brief Called when a track is removed from the recently-played list |
140 | * | |
141 | * We do nothing here - log_recent_added() suffices. | |
142 | */ | |
10e226b3 RK |
143 | static void log_recent_removed(void attribute((unused)) *v, |
144 | const char attribute((unused)) *id) { | |
145 | /* nothing - log_recent_added() will trigger the relevant update */ | |
146 | } | |
147 | ||
219dc95c | 148 | /** @brief Called when a track is removed from the queue */ |
10e226b3 RK |
149 | static void log_removed(void attribute((unused)) *v, |
150 | const char attribute((unused)) *id, | |
151 | const char attribute((unused)) *user) { | |
348ef780 | 152 | |
10e226b3 RK |
153 | queue_update(); |
154 | } | |
155 | ||
219dc95c | 156 | /** @brief Called when the current track is scratched */ |
10e226b3 RK |
157 | static void log_scratched(void attribute((unused)) *v, |
158 | const char attribute((unused)) *track, | |
159 | const char attribute((unused)) *user) { | |
10e226b3 RK |
160 | } |
161 | ||
219dc95c | 162 | /** @brief Called when a state change occurs */ |
10e226b3 RK |
163 | static void log_state(void attribute((unused)) *v, |
164 | unsigned long state) { | |
186f896b | 165 | const struct monitor *m; |
6d1302f0 RK |
166 | unsigned long changes = state ^ last_state; |
167 | static int first = 1; | |
168 | ||
169 | if(first) { | |
170 | changes = -1UL; | |
171 | first = 0; | |
172 | } | |
173 | D(("log_state old=%s new=%s changed=%s", | |
174 | disorder_eclient_interpret_state(last_state), | |
175 | disorder_eclient_interpret_state(state), | |
176 | disorder_eclient_interpret_state(changes))); | |
00959300 | 177 | last_state = state; |
186f896b RK |
178 | /* Tell anything that cares about the state change */ |
179 | for(m = monitors; m; m = m->next) { | |
00959300 RK |
180 | if(changes & m->mask) |
181 | m->callback(m->u); | |
186f896b | 182 | } |
10e226b3 RK |
183 | } |
184 | ||
219dc95c | 185 | /** @brief Called when volume changes */ |
10e226b3 RK |
186 | static void log_volume(void attribute((unused)) *v, |
187 | int l, int r) { | |
188 | if(volume_l != l || volume_r != r) { | |
189 | volume_l = l; | |
190 | volume_r = r; | |
6d1302f0 | 191 | volume_update(); |
10e226b3 RK |
192 | } |
193 | } | |
194 | ||
3ffa2d15 RK |
195 | /** @brief Add a monitor to the list |
196 | * @param callback Function to call | |
197 | * @param u User data to pass to @p callback | |
198 | * @param mask Mask of flags that @p callback cares about | |
199 | * | |
200 | * Pass @p mask as -1UL to match all flags. | |
201 | */ | |
186f896b RK |
202 | void register_monitor(monitor_callback *callback, |
203 | void *u, | |
204 | unsigned long mask) { | |
205 | struct monitor *m = xmalloc(sizeof *m); | |
206 | ||
207 | m->next = monitors; | |
208 | m->mask = mask; | |
209 | m->callback = callback; | |
210 | m->u = u; | |
211 | monitors = m; | |
212 | } | |
213 | ||
10e226b3 RK |
214 | /* |
215 | Local Variables: | |
216 | c-basic-offset:2 | |
217 | comment-column:40 | |
218 | fill-column:79 | |
219 | indent-tabs-mode:nil | |
220 | End: | |
221 | */ |