chiark / gitweb /
Disobedience notices when tracks are adopted now.
[disorder] / disobedience / log.c
CommitLineData
10e226b3
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
10e226b3 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
10e226b3
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
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 *
10e226b3 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
10e226b3 17 */
219dc95c
RK
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 */
10e226b3
RK
25
26#include "disobedience.h"
27
186f896b 28/* State monitoring -------------------------------------------------------- */
10e226b3
RK
29
30static void log_connected(void *v);
31static void log_completed(void *v, const char *track);
32static void log_failed(void *v, const char *track, const char *status);
33static void log_moved(void *v, const char *user);
34static void log_playing(void *v, const char *track, const char *user);
35static void log_queue(void *v, struct queue_entry *q);
36static void log_recent_added(void *v, struct queue_entry *q);
37static void log_recent_removed(void *v, const char *id);
38static void log_removed(void *v, const char *id, const char *user);
39static void log_scratched(void *v, const char *track, const char *user);
40static void log_state(void *v, unsigned long state);
41static void log_volume(void *v, int l, int r);
e025abff 42static void log_rescanned(void *v);
45e6d04a 43static void log_rights_changed(void *v, rights_type r);
9433fabf 44static void log_adopted(void *v, const char *id, const char *user);
10e226b3
RK
45
46/** @brief Callbacks for server state monitoring */
47const disorder_eclient_log_callbacks log_callbacks = {
58d2aad2
RK
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,
45e6d04a 60 .rescanned = log_rescanned,
9433fabf
RK
61 .rights_changed = log_rights_changed,
62 .adopted = log_adopted
10e226b3
RK
63};
64
219dc95c 65/** @brief Update everything */
10e226b3 66void all_update(void) {
fb44b59e 67 ++suppress_actions;
3569ddb8
RK
68 event_raise("queue-changed", 0);
69 event_raise("recent-changed", 0);
1f868ca3 70 event_raise("volume-changed", 0);
00e0c652 71 event_raise("rescan-complete", 0);
fb44b59e 72 --suppress_actions;
10e226b3
RK
73}
74
219dc95c
RK
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.
219dc95c 80 */
10e226b3 81static void log_connected(void attribute((unused)) *v) {
10e226b3
RK
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();
00e0c652 86 event_raise("log-connected", 0);
10e226b3
RK
87}
88
219dc95c 89/** @brief Called when the current track finishes playing */
10e226b3
RK
90static void log_completed(void attribute((unused)) *v,
91 const char attribute((unused)) *track) {
10e226b3
RK
92}
93
219dc95c 94/** @brief Called when the current track fails */
10e226b3
RK
95static void log_failed(void attribute((unused)) *v,
96 const char attribute((unused)) *track,
97 const char attribute((unused)) *status) {
10e226b3
RK
98}
99
219dc95c 100/** @brief Called when some track is moved within the queue */
10e226b3
RK
101static void log_moved(void attribute((unused)) *v,
102 const char attribute((unused)) *user) {
3569ddb8 103 event_raise("queue-changed", 0);
10e226b3
RK
104}
105
106static void log_playing(void attribute((unused)) *v,
107 const char attribute((unused)) *track,
108 const char attribute((unused)) *user) {
10e226b3
RK
109}
110
219dc95c 111/** @brief Called when a track is added to the queue */
10e226b3
RK
112static void log_queue(void attribute((unused)) *v,
113 struct queue_entry attribute((unused)) *q) {
3569ddb8 114 event_raise("queue-changed", 0);
10e226b3
RK
115}
116
219dc95c 117/** @brief Called when a track is added to the recently-played list */
10e226b3
RK
118static void log_recent_added(void attribute((unused)) *v,
119 struct queue_entry attribute((unused)) *q) {
3569ddb8 120 event_raise("recent-changed", 0);
10e226b3
RK
121}
122
219dc95c
RK
123/** @brief Called when a track is removed from the recently-played list
124 *
125 * We do nothing here - log_recent_added() suffices.
126 */
10e226b3
RK
127static 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
219dc95c 132/** @brief Called when a track is removed from the queue */
10e226b3
RK
133static void log_removed(void attribute((unused)) *v,
134 const char attribute((unused)) *id,
135 const char attribute((unused)) *user) {
3569ddb8 136 event_raise("queue-changed", 0);
10e226b3
RK
137}
138
219dc95c 139/** @brief Called when the current track is scratched */
10e226b3
RK
140static void log_scratched(void attribute((unused)) *v,
141 const char attribute((unused)) *track,
142 const char attribute((unused)) *user) {
10e226b3
RK
143}
144
a8cd6f84
RK
145/** @brief Map from state bits to state change events */
146static 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" },
f513e538 154 { DISORDER_CONNECTED, "connected-changed" },
a8cd6f84
RK
155};
156#define NSTATE_EVENTS (sizeof state_events / sizeof *state_events)
157
219dc95c 158/** @brief Called when a state change occurs */
10e226b3
RK
159static void log_state(void attribute((unused)) *v,
160 unsigned long state) {
6d1302f0
RK
161 unsigned long changes = state ^ last_state;
162 static int first = 1;
fb44b59e
RK
163
164 ++suppress_actions;
6d1302f0
RK
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)));
00959300 173 last_state = state;
a8cd6f84
RK
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);
fb44b59e 178 --suppress_actions;
10e226b3
RK
179}
180
219dc95c 181/** @brief Called when volume changes */
10e226b3
RK
182static void log_volume(void attribute((unused)) *v,
183 int l, int r) {
321f7536 184 if(!rtp_supported && (volume_l != l || volume_r != r)) {
10e226b3
RK
185 volume_l = l;
186 volume_r = r;
fb44b59e 187 ++suppress_actions;
1f868ca3 188 event_raise("volume-changed", 0);
fb44b59e 189 --suppress_actions;
10e226b3
RK
190 }
191}
192
e025abff
RK
193/** @brief Called when a rescan completes */
194static void log_rescanned(void attribute((unused)) *v) {
00e0c652 195 event_raise("rescan-complete", 0);
e025abff
RK
196}
197
45e6d04a
RK
198/** @brief Called when our rights change */
199static 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
9433fabf
RK
207/** @brief Called when a track is adopted */
208static void log_adopted(void attribute((unused)) *v,
209 const char attribute((unused)) *id,
210 const char attribute((unused)) *who) {
211 event_raise("queue-changed", 0);
212}
213
10e226b3
RK
214/*
215Local Variables:
216c-basic-offset:2
217comment-column:40
218fill-column:79
219indent-tabs-mode:nil
220End:
221*/