chiark / gitweb /
Synchronize with trunk
[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 3 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,
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  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
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  */
25
26 #include "disobedience.h"
27
28 /* State monitoring -------------------------------------------------------- */
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);
42 static void log_rescanned(void *v);
43 static void log_rights_changed(void *v, rights_type r);
44 static void log_adopted(void *v, const char *id, const char *user);
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);
51
52 /** @brief Callbacks for server state monitoring */
53 const disorder_eclient_log_callbacks log_callbacks = {
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,
66   .rescanned = log_rescanned,
67   .rights_changed = log_rights_changed,
68   .adopted = log_adopted,
69   .playlist_created = log_playlist_created,
70   .playlist_modified = log_playlist_modified,
71   .playlist_deleted = log_playlist_deleted,
72 };
73
74 /** @brief Update everything */
75 void all_update(void) {
76   ++suppress_actions;
77   event_raise("queue-changed", 0);
78   event_raise("recent-changed", 0);
79   event_raise("volume-changed", 0);
80   event_raise("rescan-complete", 0);
81   --suppress_actions;
82 }
83
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.
89  */
90 static void log_connected(void attribute((unused)) *v) {
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();
95   event_raise("log-connected", 0);
96 }
97
98 /** @brief Called when the current track finishes playing */
99 static void log_completed(void attribute((unused)) *v,
100                           const char attribute((unused)) *track) {
101 }
102
103 /** @brief Called when the current track fails */
104 static void log_failed(void attribute((unused)) *v,
105                        const char attribute((unused)) *track,
106                        const char attribute((unused)) *status) {
107 }
108
109 /** @brief Called when some track is moved within the queue */
110 static void log_moved(void attribute((unused)) *v,
111                       const char attribute((unused)) *user) {
112   event_raise("queue-changed", 0);
113 }
114
115 static void log_playing(void attribute((unused)) *v,
116                         const char attribute((unused)) *track,
117                         const char attribute((unused)) *user) {
118 }
119
120 /** @brief Called when a track is added to the queue */
121 static void log_queue(void attribute((unused)) *v,
122                       struct queue_entry attribute((unused)) *q) {
123   event_raise("queue-changed", 0);
124 }
125
126 /** @brief Called when a track is added to the recently-played list */
127 static void log_recent_added(void attribute((unused)) *v,
128                              struct queue_entry attribute((unused)) *q) {
129   event_raise("recent-changed", 0);
130 }
131
132 /** @brief Called when a track is removed from the recently-played list
133  *
134  * We do nothing here - log_recent_added() suffices.
135  */
136 static void log_recent_removed(void attribute((unused)) *v,
137                                const char attribute((unused)) *id) {
138   /* nothing - log_recent_added() will trigger the relevant update */
139 }
140
141 /** @brief Called when a track is removed from the queue */
142 static void log_removed(void attribute((unused)) *v,
143                         const char attribute((unused)) *id,
144                         const char attribute((unused)) *user) {
145   event_raise("queue-changed", 0);
146 }
147
148 /** @brief Called when the current track is scratched */
149 static void log_scratched(void attribute((unused)) *v,
150                           const char attribute((unused)) *track,
151                           const char attribute((unused)) *user) {
152 }
153
154 /** @brief Map from state bits to state change events */
155 static const struct {
156   unsigned long bit;
157   const char *event;
158 } state_events[] = {
159   { DISORDER_PLAYING_ENABLED, "enabled-changed" },
160   { DISORDER_RANDOM_ENABLED, "random-changed" },
161   { DISORDER_TRACK_PAUSED, "pause-changed" },
162   { DISORDER_PLAYING, "playing-changed" },
163   { DISORDER_CONNECTED, "connected-changed" },
164 };
165 #define NSTATE_EVENTS (sizeof state_events / sizeof *state_events)
166
167 /** @brief Called when a state change occurs */
168 static void log_state(void attribute((unused)) *v,
169                       unsigned long state) {
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   /* Notify interested parties what has changed */
184   for(unsigned n = 0; n < NSTATE_EVENTS; ++n)
185     if(changes & state_events[n].bit)
186       event_raise(state_events[n].event, 0);
187   --suppress_actions;
188 }
189
190 /** @brief Called when volume changes */
191 static void log_volume(void attribute((unused)) *v,
192                        int l, int r) {
193   if(!rtp_supported && (volume_l != l || volume_r != r)) {
194     volume_l = l;
195     volume_r = r;
196     ++suppress_actions;
197     event_raise("volume-changed", 0);
198     --suppress_actions;
199   }
200 }
201
202 /** @brief Called when a rescan completes */
203 static void log_rescanned(void attribute((unused)) *v) {
204   event_raise("rescan-complete", 0);
205 }
206
207 /** @brief Called when our rights change */
208 static void log_rights_changed(void attribute((unused)) *v,
209                                rights_type new_rights) {
210   ++suppress_actions;
211   last_rights = new_rights;
212   event_raise("rights-changed", 0);
213   --suppress_actions;
214 }
215
216 /** @brief Called when a track is adopted */
217 static void log_adopted(void attribute((unused)) *v,
218                         const char attribute((unused)) *id,
219                         const char attribute((unused)) *who) {
220   event_raise("queue-changed", 0);
221 }
222
223 static void log_playlist_created(void attribute((unused)) *v,
224                                  const char *playlist,
225                                  const char attribute((unused)) *sharing) {
226   event_raise("playlist-created", (void *)playlist);
227 }
228
229 static void log_playlist_modified(void attribute((unused)) *v,
230                                   const char *playlist,
231                                   const char attribute((unused)) *sharing) {
232   event_raise("playlist-modified", (void *)playlist);
233 }
234
235 static void log_playlist_deleted(void attribute((unused)) *v,
236                                  const char *playlist) {
237   event_raise("playlist-deleted", (void *)playlist);
238 }
239
240 /*
241 Local Variables:
242 c-basic-offset:2
243 comment-column:40
244 fill-column:79
245 indent-tabs-mode:nil
246 End:
247 */