2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file playlist_selector.c The playlist audio file selector of paraslash */
12 #include "afs_common.h"
17 #include "user_list.h"
18 #include "playlist_selector_command_list.h"
21 * structure used for transmission of the playlist
23 * There's one such struct which gets initialized during startup. It lives in
24 * shared memory and is used by com_lpl().
26 struct pls_client_data {
27 /** allocated and set by com_lpl() (child) */
29 /** the size of the shared memory area identified by \a shm_id */
31 /** initially locked, gets unlocked by parent when it is done */
33 /** return value, set by parent */
37 /** data specific to the playlist selector */
38 struct private_pls_data {
39 /** guards against concurrent client access */
41 /** guards against concurrent parent-child access */
43 /** pointer to the client data */
44 struct pls_client_data *client_data;
45 /** id of the shm corresponding to \a client_data */
46 int client_data_shm_id;
49 /** we refuse to load playlists bigger than that */
50 #define MAX_PLAYLIST_BYTES (1024 * 1024)
52 static unsigned playlist_len, playlist_size, current_playlist_entry;
53 static char **playlist;
54 static struct audio_file_selector *self;
56 extern struct misc_meta_data *mmd;
58 static int playlist_add(char *path, __a_unused void *data)
60 if (playlist_len >= playlist_size) {
61 playlist_size = 2 * playlist_size + 1;
62 playlist = para_realloc(playlist, playlist_size * sizeof(char *));
64 PARA_DEBUG_LOG("adding #%d/%d: %s\n", playlist_len, playlist_size, path);
65 playlist[playlist_len++] = para_strdup(path);
69 static int send_playlist_to_server(const char *buf, size_t size)
71 struct private_pls_data *ppd = self->private_data;
72 int ret, shm_mutex = -1, shm_id = -1;
75 PARA_DEBUG_LOG("new playlist (%zd bytes)\n", size);
87 ret = shm_attach(shm_id, ATTACH_RW, &shm);
90 mutex_lock(shm_mutex);
91 memcpy(shm, buf, size);
92 mutex_lock(ppd->client_mutex);
93 mutex_lock(ppd->server_mutex);
94 ppd->client_data->size = size;
95 ppd->client_data->shm_id = shm_id;
96 ppd->client_data->mutex = shm_mutex;
97 kill(getppid(), SIGUSR1); /* wake up the server */
98 mutex_unlock(ppd->server_mutex);
99 mutex_lock(shm_mutex); /* wait until server is done */
100 mutex_unlock(shm_mutex);
101 ret = ppd->client_data->retval;
102 mutex_unlock(ppd->client_mutex);
107 mutex_destroy(shm_mutex);
108 PARA_DEBUG_LOG("returning %d\n", ret);
112 int com_lpl(int fd, __a_unused int argc, __a_unused char *argv[])
115 size_t bufsize = 4096; /* guess that's enough */
116 char *buf = para_malloc(bufsize);
118 ret = send_buffer(fd, AWAITING_DATA_MSG);
122 ret = recv_bin_buffer(fd, buf + loaded, bufsize - loaded);
126 ret = send_playlist_to_server(buf, loaded);
130 ret = -E_LOAD_PLAYLIST;
131 if (loaded >= MAX_PLAYLIST_BYTES)
133 if (loaded >= bufsize) {
135 buf = para_realloc(buf, bufsize);
143 int com_ppl(int fd, __a_unused int argc, __a_unused char *argv[])
147 PARA_DEBUG_LOG("sending playlist to client (%d entries)\n", playlist_len);
148 for (i = 0; i < playlist_len; i++) {
149 int ret = send_va_buffer(fd, "%s\n", playlist[
150 (i + current_playlist_entry) % playlist_len]);
157 static char **pls_get_audio_file_list(unsigned int num)
162 num = PARA_MIN(num, playlist_len);
165 file_list = para_malloc((num + 1) * sizeof(char *));
166 for (i = 0; i < num; i++) {
167 unsigned j = (current_playlist_entry + i + 1) % playlist_len;
168 file_list[i] = para_strdup(playlist[j]);
174 static void free_playlist_contents(void)
178 PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len);
179 for (i = 0; i < playlist_len; i++)
181 current_playlist_entry = 0;
185 static void pls_shutdown(void)
187 struct private_pls_data *ppd = self->private_data;
189 shm_detach(ppd->client_data);
190 shm_destroy(ppd->client_data_shm_id);
191 mutex_destroy(ppd->server_mutex);
192 mutex_destroy(ppd->client_mutex);
194 free_playlist_contents();
201 static void pls_post_select(__a_unused fd_set *rfds, __a_unused fd_set *wfds)
203 struct private_pls_data *ppd = self->private_data;
204 struct pls_client_data *pcd = ppd->client_data;
208 mutex_lock(ppd->server_mutex);
211 free_playlist_contents();
212 ret = shm_attach(pcd->shm_id, ATTACH_RW, &shm);
214 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
217 PARA_DEBUG_LOG("loading new playlist (%zd bytes)\n", pcd->size);
218 ret = for_each_line((char *)shm, pcd->size, &playlist_add, NULL);
220 PARA_NOTICE_LOG("new playlist (%d entries)\n", playlist_len);
221 sprintf(mmd->selector_info, "dbinfo1:new playlist: %d files\n"
222 "dbinfo2:\ndbinfo3:\n", playlist_len);
225 mutex_unlock(pcd->mutex);
227 mutex_unlock(ppd->server_mutex);
230 static size_t string_offset(const char *str, size_t max)
232 size_t l = strlen(str);
239 static void pls_update_audio_file(char *audio_file)
242 char *dir = para_dirname(audio_file),
243 *prev = playlist[current_playlist_entry % playlist_len];
244 size_t dir_off = string_offset(dir, 50),
245 prev_off = string_offset(prev, 70);
247 for (i = 1; i <= playlist_len; i++) {
250 unsigned j = (current_playlist_entry + i) % playlist_len;
251 if (strcmp(playlist[j], audio_file))
253 current_playlist_entry = j;
254 next = playlist[(j + 1) %playlist_len];
255 next_off = string_offset(next, 70);
256 snprintf(mmd->selector_info, MMD_INFO_SIZE,
257 "dbinfo1: %d files, current dir: %s%s\n"
258 "dbinfo2: prev: %s%s\n"
259 "dbinfo3: next: %s%s\n",
261 dir_off? "... " : "", dir + dir_off,
262 prev_off? "... " : "", prev + prev_off,
263 next_off? "... " : "", next + next_off
271 * the init function for the playlist selector
273 * \param afs pointer to the struct to initialize
275 * Init all function pointers of \a afs
277 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
280 int playlist_selector_init(struct audio_file_selector *afs)
283 struct private_pls_data *ppd = NULL;
287 afs->cmd_list = playlist_selector_cmds;
288 afs->get_audio_file_list = pls_get_audio_file_list;
289 afs->shutdown = pls_shutdown;
290 afs->post_select = pls_post_select;
291 afs->update_audio_file = pls_update_audio_file;
292 ppd = para_calloc(sizeof(struct private_pls_data));
293 afs->private_data = ppd;
295 ppd->client_mutex = -1;
296 ppd->server_mutex = -1;
297 ppd->client_data_shm_id = -1;
298 ppd->client_data = NULL;
303 ppd->client_mutex = ret;
308 ppd->server_mutex = ret;
310 ret = shm_new(sizeof(struct pls_client_data));
313 ppd->client_data_shm_id = ret;
315 ret = shm_attach(ppd->client_data_shm_id, ATTACH_RW, &shm);
318 ppd->client_data = shm;
319 ppd->client_data->size = 0;
320 sprintf(mmd->selector_info, "dbinfo1: Welcome to the playlist "
321 "selector\ndbinfo2: no playlist loaded\ndbinfo3:\n");
324 if (ppd->client_data_shm_id >= 0)
325 shm_destroy(ppd->client_data_shm_id);
326 if (ppd->client_mutex >= 0)
327 mutex_destroy(ppd->client_mutex);
328 if (ppd->server_mutex >= 0)
329 mutex_destroy(ppd->server_mutex);