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