Commit | Line | Data |
---|---|---|
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 | |
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); | |
e025abff | 42 | static void log_rescanned(void *v); |
45e6d04a | 43 | static void log_rights_changed(void *v, rights_type r); |
10e226b3 RK |
44 | |
45 | /** @brief Callbacks for server state monitoring */ | |
46 | const disorder_eclient_log_callbacks log_callbacks = { | |
58d2aad2 RK |
47 | .connected = log_connected, |
48 | .completed = log_completed, | |
49 | .failed = log_failed, | |
50 | .moved = log_moved, | |
51 | .playing = log_playing, | |
52 | .queue = log_queue, | |
53 | .recent_added = log_recent_added, | |
54 | .recent_removed = log_recent_removed, | |
55 | .removed = log_removed, | |
56 | .scratched = log_scratched, | |
57 | .state = log_state, | |
58 | .volume = log_volume, | |
45e6d04a RK |
59 | .rescanned = log_rescanned, |
60 | .rights_changed = log_rights_changed | |
10e226b3 RK |
61 | }; |
62 | ||
219dc95c | 63 | /** @brief Update everything */ |
10e226b3 | 64 | void all_update(void) { |
fb44b59e | 65 | ++suppress_actions; |
3569ddb8 RK |
66 | event_raise("queue-changed", 0); |
67 | event_raise("recent-changed", 0); | |
1f868ca3 | 68 | event_raise("volume-changed", 0); |
00e0c652 | 69 | event_raise("rescan-complete", 0); |
fb44b59e | 70 | --suppress_actions; |
10e226b3 RK |
71 | } |
72 | ||
219dc95c RK |
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. | |
219dc95c | 78 | */ |
10e226b3 | 79 | static void log_connected(void attribute((unused)) *v) { |
10e226b3 RK |
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(); | |
00e0c652 | 84 | event_raise("log-connected", 0); |
10e226b3 RK |
85 | } |
86 | ||
219dc95c | 87 | /** @brief Called when the current track finishes playing */ |
10e226b3 RK |
88 | static void log_completed(void attribute((unused)) *v, |
89 | const char attribute((unused)) *track) { | |
10e226b3 RK |
90 | } |
91 | ||
219dc95c | 92 | /** @brief Called when the current track fails */ |
10e226b3 RK |
93 | static void log_failed(void attribute((unused)) *v, |
94 | const char attribute((unused)) *track, | |
95 | const char attribute((unused)) *status) { | |
10e226b3 RK |
96 | } |
97 | ||
219dc95c | 98 | /** @brief Called when some track is moved within the queue */ |
10e226b3 RK |
99 | static void log_moved(void attribute((unused)) *v, |
100 | const char attribute((unused)) *user) { | |
3569ddb8 | 101 | event_raise("queue-changed", 0); |
10e226b3 RK |
102 | } |
103 | ||
104 | static void log_playing(void attribute((unused)) *v, | |
105 | const char attribute((unused)) *track, | |
106 | const char attribute((unused)) *user) { | |
10e226b3 RK |
107 | } |
108 | ||
219dc95c | 109 | /** @brief Called when a track is added to the queue */ |
10e226b3 RK |
110 | static void log_queue(void attribute((unused)) *v, |
111 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 112 | event_raise("queue-changed", 0); |
10e226b3 RK |
113 | } |
114 | ||
219dc95c | 115 | /** @brief Called when a track is added to the recently-played list */ |
10e226b3 RK |
116 | static void log_recent_added(void attribute((unused)) *v, |
117 | struct queue_entry attribute((unused)) *q) { | |
3569ddb8 | 118 | event_raise("recent-changed", 0); |
10e226b3 RK |
119 | } |
120 | ||
219dc95c RK |
121 | /** @brief Called when a track is removed from the recently-played list |
122 | * | |
123 | * We do nothing here - log_recent_added() suffices. | |
124 | */ | |
10e226b3 RK |
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 | ||
219dc95c | 130 | /** @brief Called when a track is removed from the queue */ |
10e226b3 RK |
131 | static void log_removed(void attribute((unused)) *v, |
132 | const char attribute((unused)) *id, | |
133 | const char attribute((unused)) *user) { | |
3569ddb8 | 134 | event_raise("queue-changed", 0); |
10e226b3 RK |
135 | } |
136 | ||
219dc95c | 137 | /** @brief Called when the current track is scratched */ |
10e226b3 RK |
138 | static void log_scratched(void attribute((unused)) *v, |
139 | const char attribute((unused)) *track, | |
140 | const char attribute((unused)) *user) { | |
10e226b3 RK |
141 | } |
142 | ||
a8cd6f84 RK |
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" }, | |
f513e538 | 152 | { DISORDER_CONNECTED, "connected-changed" }, |
a8cd6f84 RK |
153 | }; |
154 | #define NSTATE_EVENTS (sizeof state_events / sizeof *state_events) | |
155 | ||
219dc95c | 156 | /** @brief Called when a state change occurs */ |
10e226b3 RK |
157 | static void log_state(void attribute((unused)) *v, |
158 | unsigned long state) { | |
6d1302f0 RK |
159 | unsigned long changes = state ^ last_state; |
160 | static int first = 1; | |
fb44b59e RK |
161 | |
162 | ++suppress_actions; | |
6d1302f0 RK |
163 | if(first) { |
164 | changes = -1UL; | |
165 | first = 0; | |
166 | } | |
167 | D(("log_state old=%s new=%s changed=%s", | |
168 | disorder_eclient_interpret_state(last_state), | |
169 | disorder_eclient_interpret_state(state), | |
170 | disorder_eclient_interpret_state(changes))); | |
00959300 | 171 | last_state = state; |
a8cd6f84 RK |
172 | /* Notify interested parties what has changed */ |
173 | for(unsigned n = 0; n < NSTATE_EVENTS; ++n) | |
174 | if(changes & state_events[n].bit) | |
175 | event_raise(state_events[n].event, 0); | |
fb44b59e | 176 | --suppress_actions; |
10e226b3 RK |
177 | } |
178 | ||
219dc95c | 179 | /** @brief Called when volume changes */ |
10e226b3 RK |
180 | static void log_volume(void attribute((unused)) *v, |
181 | int l, int r) { | |
321f7536 | 182 | if(!rtp_supported && (volume_l != l || volume_r != r)) { |
10e226b3 RK |
183 | volume_l = l; |
184 | volume_r = r; | |
fb44b59e | 185 | ++suppress_actions; |
1f868ca3 | 186 | event_raise("volume-changed", 0); |
fb44b59e | 187 | --suppress_actions; |
10e226b3 RK |
188 | } |
189 | } | |
190 | ||
e025abff RK |
191 | /** @brief Called when a rescan completes */ |
192 | static void log_rescanned(void attribute((unused)) *v) { | |
00e0c652 | 193 | event_raise("rescan-complete", 0); |
e025abff RK |
194 | } |
195 | ||
45e6d04a RK |
196 | /** @brief Called when our rights change */ |
197 | static void log_rights_changed(void attribute((unused)) *v, | |
198 | rights_type new_rights) { | |
199 | ++suppress_actions; | |
200 | last_rights = new_rights; | |
201 | event_raise("rights-changed", 0); | |
202 | --suppress_actions; | |
203 | } | |
204 | ||
10e226b3 RK |
205 | /* |
206 | Local Variables: | |
207 | c-basic-offset:2 | |
208 | comment-column:40 | |
209 | fill-column:79 | |
210 | indent-tabs-mode:nil | |
211 | End: | |
212 | */ |