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