chiark / gitweb /
Destroy users table properly
[disorder] / disobedience / log.c
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
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 static void log_rescanned(void *v);
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,
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  */
67 struct 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 */
82 static struct monitor *monitors;
83
84 /** @brief Update everything */
85 void all_update(void) {
86   ++suppress_actions;
87   queue_update();
88   recent_update();
89   volume_update();
90   added_update();
91   --suppress_actions;
92 }
93
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  */
102 static void log_connected(void attribute((unused)) *v) {
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();
107 }
108
109 /** @brief Called when the current track finishes playing */
110 static void log_completed(void attribute((unused)) *v,
111                           const char attribute((unused)) *track) {
112 }
113
114 /** @brief Called when the current track fails */
115 static void log_failed(void attribute((unused)) *v,
116                        const char attribute((unused)) *track,
117                        const char attribute((unused)) *status) {
118 }
119
120 /** @brief Called when some track is moved within the queue */
121 static void log_moved(void attribute((unused)) *v,
122                       const char attribute((unused)) *user) {
123   queue_update();
124 }
125
126 static void log_playing(void attribute((unused)) *v,
127                         const char attribute((unused)) *track,
128                         const char attribute((unused)) *user) {
129 }
130
131 /** @brief Called when a track is added to the queue */
132 static void log_queue(void attribute((unused)) *v,
133                       struct queue_entry attribute((unused)) *q) {
134   queue_update();
135 }
136
137 /** @brief Called when a track is added to the recently-played list */
138 static void log_recent_added(void attribute((unused)) *v,
139                              struct queue_entry attribute((unused)) *q) {
140   recent_update();
141 }
142
143 /** @brief Called when a track is removed from the recently-played list
144  *
145  * We do nothing here - log_recent_added() suffices.
146  */
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
152 /** @brief Called when a track is removed from the queue */
153 static void log_removed(void attribute((unused)) *v,
154                         const char attribute((unused)) *id,
155                         const char attribute((unused)) *user) {
156
157   queue_update();
158 }
159
160 /** @brief Called when the current track is scratched */
161 static void log_scratched(void attribute((unused)) *v,
162                           const char attribute((unused)) *track,
163                           const char attribute((unused)) *user) {
164 }
165
166 /** @brief Called when a state change occurs */
167 static void log_state(void attribute((unused)) *v,
168                       unsigned long state) {
169   const struct monitor *m;
170   unsigned long changes = state ^ last_state;
171   static int first = 1;
172
173   ++suppress_actions;
174   if(first) {
175     changes = -1UL;
176     first = 0;
177   }
178   D(("log_state old=%s new=%s changed=%s",
179      disorder_eclient_interpret_state(last_state),
180      disorder_eclient_interpret_state(state),
181      disorder_eclient_interpret_state(changes)));
182   last_state = state;
183   /* Tell anything that cares about the state change */
184   for(m = monitors; m; m = m->next) {
185     if(changes & m->mask)
186       m->callback(m->u);
187   }
188   --suppress_actions;
189 }
190
191 /** @brief Called when volume changes */
192 static void log_volume(void attribute((unused)) *v,
193                        int l, int r) {
194   if(!rtp_supported && (volume_l != l || volume_r != r)) {
195     volume_l = l;
196     volume_r = r;
197     ++suppress_actions;
198     volume_update();
199     --suppress_actions;
200   }
201 }
202
203 /** @brief Called when a rescan completes */
204 static void log_rescanned(void attribute((unused)) *v) {
205   added_update();
206 }
207
208 /** @brief Add a monitor to the list
209  * @param callback Function to call
210  * @param u User data to pass to @p callback
211  * @param mask Mask of flags that @p callback cares about
212  *
213  * Pass @p mask as -1UL to match all flags.
214  */
215 void register_monitor(monitor_callback *callback,
216                       void *u,
217                       unsigned long mask) {
218   struct monitor *m = xmalloc(sizeof *m);
219
220   m->next = monitors;
221   m->mask = mask;
222   m->callback = callback;
223   m->u = u;
224   monitors = m;
225 }
226
227 /*
228 Local Variables:
229 c-basic-offset:2
230 comment-column:40
231 fill-column:79
232 indent-tabs-mode:nil
233 End:
234 */