| 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 | |
| 45 | /** @brief Callbacks for server state monitoring */ |
| 46 | const disorder_eclient_log_callbacks log_callbacks = { |
| 47 | log_connected, |
| 48 | log_completed, |
| 49 | log_failed, |
| 50 | log_moved, |
| 51 | log_playing, |
| 52 | log_queue, |
| 53 | log_recent_added, |
| 54 | log_recent_removed, |
| 55 | log_removed, |
| 56 | log_scratched, |
| 57 | log_state, |
| 58 | log_volume |
| 59 | }; |
| 60 | |
| 61 | /** @brief State monitor |
| 62 | * |
| 63 | * We keep a linked list of everything that is interested in state changes. |
| 64 | */ |
| 65 | struct monitor { |
| 66 | /** @brief Next monitor */ |
| 67 | struct monitor *next; |
| 68 | |
| 69 | /** @brief State bits of interest */ |
| 70 | unsigned long mask; |
| 71 | |
| 72 | /** @brief Function to call if any of @c mask change */ |
| 73 | monitor_callback *callback; |
| 74 | |
| 75 | /** @brief User data for callback */ |
| 76 | void *u; |
| 77 | }; |
| 78 | |
| 79 | /** @brief List of monitors */ |
| 80 | static struct monitor *monitors; |
| 81 | |
| 82 | /** @brief Update everything */ |
| 83 | void all_update(void) { |
| 84 | queue_update(); |
| 85 | recent_update(); |
| 86 | volume_update(); |
| 87 | } |
| 88 | |
| 89 | /** @brief Called when the client connects |
| 90 | * |
| 91 | * Depending on server and network state the TCP connection to the server may |
| 92 | * go up or down many times during the lifetime of Disobedience. This function |
| 93 | * is called whenever it connects. |
| 94 | * |
| 95 | * The intent is to use the monitor logic to achieve this in future. |
| 96 | */ |
| 97 | static void log_connected(void attribute((unused)) *v) { |
| 98 | /* Don't know what we might have missed while disconnected so update |
| 99 | * everything. We get this at startup too and this is how we do the initial |
| 100 | * state fetch. */ |
| 101 | all_update(); |
| 102 | } |
| 103 | |
| 104 | /** @brief Called when the current track finishes playing */ |
| 105 | static void log_completed(void attribute((unused)) *v, |
| 106 | const char attribute((unused)) *track) { |
| 107 | } |
| 108 | |
| 109 | /** @brief Called when the current track fails */ |
| 110 | static void log_failed(void attribute((unused)) *v, |
| 111 | const char attribute((unused)) *track, |
| 112 | const char attribute((unused)) *status) { |
| 113 | } |
| 114 | |
| 115 | /** @brief Called when some track is moved within the queue */ |
| 116 | static void log_moved(void attribute((unused)) *v, |
| 117 | const char attribute((unused)) *user) { |
| 118 | queue_update(); |
| 119 | } |
| 120 | |
| 121 | static void log_playing(void attribute((unused)) *v, |
| 122 | const char attribute((unused)) *track, |
| 123 | const char attribute((unused)) *user) { |
| 124 | } |
| 125 | |
| 126 | /** @brief Called when a track is added to the queue */ |
| 127 | static void log_queue(void attribute((unused)) *v, |
| 128 | struct queue_entry attribute((unused)) *q) { |
| 129 | queue_update(); |
| 130 | } |
| 131 | |
| 132 | /** @brief Called when a track is added to the recently-played list */ |
| 133 | static void log_recent_added(void attribute((unused)) *v, |
| 134 | struct queue_entry attribute((unused)) *q) { |
| 135 | recent_update(); |
| 136 | } |
| 137 | |
| 138 | /** @brief Called when a track is removed from the recently-played list |
| 139 | * |
| 140 | * We do nothing here - log_recent_added() suffices. |
| 141 | */ |
| 142 | static void log_recent_removed(void attribute((unused)) *v, |
| 143 | const char attribute((unused)) *id) { |
| 144 | /* nothing - log_recent_added() will trigger the relevant update */ |
| 145 | } |
| 146 | |
| 147 | /** @brief Called when a track is removed from the queue */ |
| 148 | static void log_removed(void attribute((unused)) *v, |
| 149 | const char attribute((unused)) *id, |
| 150 | const char attribute((unused)) *user) { |
| 151 | |
| 152 | queue_update(); |
| 153 | } |
| 154 | |
| 155 | /** @brief Called when the current track is scratched */ |
| 156 | static void log_scratched(void attribute((unused)) *v, |
| 157 | const char attribute((unused)) *track, |
| 158 | const char attribute((unused)) *user) { |
| 159 | } |
| 160 | |
| 161 | /** @brief Called when a state change occurs */ |
| 162 | static void log_state(void attribute((unused)) *v, |
| 163 | unsigned long state) { |
| 164 | const struct monitor *m; |
| 165 | unsigned long changes = state ^ last_state; |
| 166 | static int first = 1; |
| 167 | |
| 168 | if(first) { |
| 169 | changes = -1UL; |
| 170 | first = 0; |
| 171 | } |
| 172 | D(("log_state old=%s new=%s changed=%s", |
| 173 | disorder_eclient_interpret_state(last_state), |
| 174 | disorder_eclient_interpret_state(state), |
| 175 | disorder_eclient_interpret_state(changes))); |
| 176 | last_state = state; |
| 177 | /* Tell anything that cares about the state change */ |
| 178 | for(m = monitors; m; m = m->next) { |
| 179 | if(changes & m->mask) |
| 180 | m->callback(m->u); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** @brief Called when volume changes */ |
| 185 | static void log_volume(void attribute((unused)) *v, |
| 186 | int l, int r) { |
| 187 | if(volume_l != l || volume_r != r) { |
| 188 | volume_l = l; |
| 189 | volume_r = r; |
| 190 | volume_update(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** @brief Add a monitor to the list |
| 195 | * @param callback Function to call |
| 196 | * @param u User data to pass to @p callback |
| 197 | * @param mask Mask of flags that @p callback cares about |
| 198 | * |
| 199 | * Pass @p mask as -1UL to match all flags. |
| 200 | */ |
| 201 | void register_monitor(monitor_callback *callback, |
| 202 | void *u, |
| 203 | unsigned long mask) { |
| 204 | struct monitor *m = xmalloc(sizeof *m); |
| 205 | |
| 206 | m->next = monitors; |
| 207 | m->mask = mask; |
| 208 | m->callback = callback; |
| 209 | m->u = u; |
| 210 | monitors = m; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | Local Variables: |
| 215 | c-basic-offset:2 |
| 216 | comment-column:40 |
| 217 | fill-column:79 |
| 218 | indent-tabs-mode:nil |
| 219 | End: |
| 220 | */ |