2 * This file is part of DisOrder.
3 * Copyright (C) 2006, 2007 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/log.c
21 * @brief State monitoring
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
28 #include "disobedience.h"
30 /* State monitoring -------------------------------------------------------- */
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
);
46 /** @brief Callbacks for server state monitoring */
47 const disorder_eclient_log_callbacks log_callbacks
= {
63 /** @brief Update everything */
64 void all_update(void) {
66 event_raise("queue-changed", 0);
67 event_raise("recent-changed", 0);
68 event_raise("volume-changed", 0);
69 event_raise("added-changed", 0);
73 /** @brief Called when the client connects
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.
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
86 /** @brief Called when the current track finishes playing */
87 static void log_completed(void attribute((unused
)) *v
,
88 const char attribute((unused
)) *track
) {
91 /** @brief Called when the current track fails */
92 static void log_failed(void attribute((unused
)) *v
,
93 const char attribute((unused
)) *track
,
94 const char attribute((unused
)) *status
) {
97 /** @brief Called when some track is moved within the queue */
98 static void log_moved(void attribute((unused
)) *v
,
99 const char attribute((unused
)) *user
) {
100 event_raise("queue-changed", 0);
103 static void log_playing(void attribute((unused
)) *v
,
104 const char attribute((unused
)) *track
,
105 const char attribute((unused
)) *user
) {
108 /** @brief Called when a track is added to the queue */
109 static void log_queue(void attribute((unused
)) *v
,
110 struct queue_entry
attribute((unused
)) *q
) {
111 event_raise("queue-changed", 0);
114 /** @brief Called when a track is added to the recently-played list */
115 static void log_recent_added(void attribute((unused
)) *v
,
116 struct queue_entry
attribute((unused
)) *q
) {
117 event_raise("recent-changed", 0);
120 /** @brief Called when a track is removed from the recently-played list
122 * We do nothing here - log_recent_added() suffices.
124 static void log_recent_removed(void attribute((unused
)) *v
,
125 const char attribute((unused
)) *id
) {
126 /* nothing - log_recent_added() will trigger the relevant update */
129 /** @brief Called when a track is removed from the queue */
130 static void log_removed(void attribute((unused
)) *v
,
131 const char attribute((unused
)) *id
,
132 const char attribute((unused
)) *user
) {
133 event_raise("queue-changed", 0);
136 /** @brief Called when the current track is scratched */
137 static void log_scratched(void attribute((unused
)) *v
,
138 const char attribute((unused
)) *track
,
139 const char attribute((unused
)) *user
) {
142 /** @brief Map from state bits to state change events */
143 static const struct {
147 { DISORDER_PLAYING_ENABLED
, "enabled-changed" },
148 { DISORDER_RANDOM_ENABLED
, "random-changed" },
149 { DISORDER_TRACK_PAUSED
, "pause-changed" },
150 { DISORDER_PLAYING
, "playing-changed" },
152 #define NSTATE_EVENTS (sizeof state_events / sizeof *state_events)
154 /** @brief Called when a state change occurs */
155 static void log_state(void attribute((unused
)) *v
,
156 unsigned long state
) {
157 unsigned long changes
= state
^ last_state
;
158 static int first
= 1;
165 D(("log_state old=%s new=%s changed=%s",
166 disorder_eclient_interpret_state(last_state
),
167 disorder_eclient_interpret_state(state
),
168 disorder_eclient_interpret_state(changes
)));
170 /* Notify interested parties what has changed */
171 for(unsigned n
= 0; n
< NSTATE_EVENTS
; ++n
)
172 if(changes
& state_events
[n
].bit
)
173 event_raise(state_events
[n
].event
, 0);
177 /** @brief Called when volume changes */
178 static void log_volume(void attribute((unused
)) *v
,
180 if(!rtp_supported
&& (volume_l
!= l
|| volume_r
!= r
)) {
184 event_raise("volume-changed", 0);
189 /** @brief Called when a rescan completes */
190 static void log_rescanned(void attribute((unused
)) *v
) {
191 event_raise("added-changed", 0);