chiark / gitweb /
Disobedience now keeps track of known playlists and has a (not yet
[disorder] / disobedience / log.c
CommitLineData
10e226b3
RK
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 */
219dc95c
RK
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 */
10e226b3
RK
27
28#include "disobedience.h"
29
186f896b 30/* State monitoring -------------------------------------------------------- */
10e226b3
RK
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);
e025abff 44static void log_rescanned(void *v);
45e6d04a 45static void log_rights_changed(void *v, rights_type r);
fc36ecb7
RK
46static void log_playlist_created(void *v,
47 const char *playlist, const char *sharing);
48static void log_playlist_modified(void *v,
49 const char *playlist, const char *sharing);
50static void log_playlist_deleted(void *v,
51 const char *playlist);
10e226b3
RK
52
53/** @brief Callbacks for server state monitoring */
54const disorder_eclient_log_callbacks log_callbacks = {
58d2aad2
RK
55 .connected = log_connected,
56 .completed = log_completed,
57 .failed = log_failed,
58 .moved = log_moved,
59 .playing = log_playing,
60 .queue = log_queue,
61 .recent_added = log_recent_added,
62 .recent_removed = log_recent_removed,
63 .removed = log_removed,
64 .scratched = log_scratched,
65 .state = log_state,
66 .volume = log_volume,
45e6d04a 67 .rescanned = log_rescanned,
fc36ecb7
RK
68 .rights_changed = log_rights_changed,
69 .playlist_created = log_playlist_created,
70 .playlist_modified = log_playlist_modified,
71 .playlist_deleted = log_playlist_deleted,
10e226b3
RK
72};
73
219dc95c 74/** @brief Update everything */
10e226b3 75void all_update(void) {
fb44b59e 76 ++suppress_actions;
3569ddb8
RK
77 event_raise("queue-changed", 0);
78 event_raise("recent-changed", 0);
1f868ca3 79 event_raise("volume-changed", 0);
00e0c652 80 event_raise("rescan-complete", 0);
fb44b59e 81 --suppress_actions;
10e226b3
RK
82}
83
219dc95c
RK
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.
219dc95c 89 */
10e226b3 90static void log_connected(void attribute((unused)) *v) {
10e226b3
RK
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();
00e0c652 95 event_raise("log-connected", 0);
10e226b3
RK
96}
97
219dc95c 98/** @brief Called when the current track finishes playing */
10e226b3
RK
99static void log_completed(void attribute((unused)) *v,
100 const char attribute((unused)) *track) {
10e226b3
RK
101}
102
219dc95c 103/** @brief Called when the current track fails */
10e226b3
RK
104static void log_failed(void attribute((unused)) *v,
105 const char attribute((unused)) *track,
106 const char attribute((unused)) *status) {
10e226b3
RK
107}
108
219dc95c 109/** @brief Called when some track is moved within the queue */
10e226b3
RK
110static void log_moved(void attribute((unused)) *v,
111 const char attribute((unused)) *user) {
3569ddb8 112 event_raise("queue-changed", 0);
10e226b3
RK
113}
114
115static void log_playing(void attribute((unused)) *v,
116 const char attribute((unused)) *track,
117 const char attribute((unused)) *user) {
10e226b3
RK
118}
119
219dc95c 120/** @brief Called when a track is added to the queue */
10e226b3
RK
121static void log_queue(void attribute((unused)) *v,
122 struct queue_entry attribute((unused)) *q) {
3569ddb8 123 event_raise("queue-changed", 0);
10e226b3
RK
124}
125
219dc95c 126/** @brief Called when a track is added to the recently-played list */
10e226b3
RK
127static void log_recent_added(void attribute((unused)) *v,
128 struct queue_entry attribute((unused)) *q) {
3569ddb8 129 event_raise("recent-changed", 0);
10e226b3
RK
130}
131
219dc95c
RK
132/** @brief Called when a track is removed from the recently-played list
133 *
134 * We do nothing here - log_recent_added() suffices.
135 */
10e226b3
RK
136static 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
219dc95c 141/** @brief Called when a track is removed from the queue */
10e226b3
RK
142static void log_removed(void attribute((unused)) *v,
143 const char attribute((unused)) *id,
144 const char attribute((unused)) *user) {
3569ddb8 145 event_raise("queue-changed", 0);
10e226b3
RK
146}
147
219dc95c 148/** @brief Called when the current track is scratched */
10e226b3
RK
149static void log_scratched(void attribute((unused)) *v,
150 const char attribute((unused)) *track,
151 const char attribute((unused)) *user) {
10e226b3
RK
152}
153
a8cd6f84
RK
154/** @brief Map from state bits to state change events */
155static 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" },
f513e538 163 { DISORDER_CONNECTED, "connected-changed" },
a8cd6f84
RK
164};
165#define NSTATE_EVENTS (sizeof state_events / sizeof *state_events)
166
219dc95c 167/** @brief Called when a state change occurs */
10e226b3
RK
168static void log_state(void attribute((unused)) *v,
169 unsigned long state) {
6d1302f0
RK
170 unsigned long changes = state ^ last_state;
171 static int first = 1;
fb44b59e
RK
172
173 ++suppress_actions;
6d1302f0
RK
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)));
00959300 182 last_state = state;
a8cd6f84
RK
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);
fb44b59e 187 --suppress_actions;
10e226b3
RK
188}
189
219dc95c 190/** @brief Called when volume changes */
10e226b3
RK
191static void log_volume(void attribute((unused)) *v,
192 int l, int r) {
321f7536 193 if(!rtp_supported && (volume_l != l || volume_r != r)) {
10e226b3
RK
194 volume_l = l;
195 volume_r = r;
fb44b59e 196 ++suppress_actions;
1f868ca3 197 event_raise("volume-changed", 0);
fb44b59e 198 --suppress_actions;
10e226b3
RK
199 }
200}
201
e025abff
RK
202/** @brief Called when a rescan completes */
203static void log_rescanned(void attribute((unused)) *v) {
00e0c652 204 event_raise("rescan-complete", 0);
e025abff
RK
205}
206
45e6d04a
RK
207/** @brief Called when our rights change */
208static 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
fc36ecb7
RK
216static void log_playlist_created(void attribute((unused)) *v,
217 const char *playlist,
218 const char attribute((unused)) *sharing) {
219 event_raise("playlist-created", (void *)playlist);
220}
221
222static void log_playlist_modified(void attribute((unused)) *v,
223 const char *playlist,
224 const char attribute((unused)) *sharing) {
225 event_raise("playlist-modified", (void *)playlist);
226}
227
228static void log_playlist_deleted(void attribute((unused)) *v,
229 const char *playlist) {
230 event_raise("playlist-deleted", (void *)playlist);
231}
232
10e226b3
RK
233/*
234Local Variables:
235c-basic-offset:2
236comment-column:40
237fill-column:79
238indent-tabs-mode:nil
239End:
240*/