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