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 */
16 #include "afs_common.h"
20 #include "user_list.h"
21 #include "playlist_selector_command_list.h"
24 * structure used for transmission of the playlist
26 * There's one such struct which gets initialized during startup. It lives in
27 * shared memory and is used by com_lpl().
29 struct pls_client_data {
30 /** allocated and set by com_lpl() (child) */
32 /** the size of the shared memory area identified by \a shm_id */
34 /** initially locked, gets unlocked by parent when it is done */
36 /** return value, set by parent */
40 /** data specific to the playlist selector */
41 struct private_pls_data {
42 /** guards against concurrent client access */
44 /** guards against concurrent parent-child access */
46 /** pointer to the client data */
47 struct pls_client_data *client_data;
48 /** id of the shm corresponding to \a client_data */
49 int client_data_shm_id;
52 /** we refuse to load playlists bigger than that */
53 #define MAX_PLAYLIST_BYTES (1024 * 1024)
55 static unsigned playlist_len, playlist_size, current_playlist_entry;
56 static char **playlist;
57 static struct audio_file_selector *self;
59 extern struct misc_meta_data *mmd;
61 static int playlist_add(char *path, __a_unused void *data)
63 if (playlist_len >= playlist_size) {
64 playlist_size = 2 * playlist_size + 1;
65 playlist = para_realloc(playlist, playlist_size * sizeof(char *));
67 PARA_DEBUG_LOG("adding #%d/%d: %s\n", playlist_len, playlist_size, path);
68 playlist[playlist_len++] = para_strdup(path);
72 static int send_playlist_to_server(const char *buf, size_t size)
74 struct private_pls_data *ppd = self->private_data;
75 int ret, shm_mutex = -1, shm_id = -1;
78 PARA_DEBUG_LOG("new playlist (%zd bytes)\n", size);
90 ret = shm_attach(shm_id, ATTACH_RW, &shm);
93 mutex_lock(shm_mutex);
94 memcpy(shm, buf, size);
95 mutex_lock(ppd->client_mutex);
96 mutex_lock(ppd->server_mutex);
97 ppd->client_data->size = size;
98 ppd->client_data->shm_id = shm_id;
99 ppd->client_data->mutex = shm_mutex;
100 kill(getppid(), SIGUSR1); /* wake up the server */
101 mutex_unlock(ppd->server_mutex);
102 mutex_lock(shm_mutex); /* wait until server is done */
103 mutex_unlock(shm_mutex);
104 ret = ppd->client_data->retval;
105 mutex_unlock(ppd->client_mutex);
110 mutex_destroy(shm_mutex);
111 PARA_DEBUG_LOG("returning %d\n", ret);
115 int com_lpl(int fd, __a_unused int argc, __a_unused char * const * const argv)
118 size_t bufsize = 4096; /* guess that's enough */
119 char *buf = para_malloc(bufsize);
121 ret = send_buffer(fd, AWAITING_DATA_MSG);
125 ret = recv_bin_buffer(fd, buf + loaded, bufsize - loaded);
129 ret = send_playlist_to_server(buf, loaded);
133 ret = -E_LOAD_PLAYLIST;
134 if (loaded >= MAX_PLAYLIST_BYTES)
136 if (loaded >= bufsize) {
138 buf = para_realloc(buf, bufsize);
146 int com_ppl(int fd, __a_unused int argc, __a_unused char * const * const argv)
150 PARA_DEBUG_LOG("sending playlist to client (%d entries)\n", playlist_len);
151 for (i = 0; i < playlist_len; i++) {
152 int ret = send_va_buffer(fd, "%s\n", playlist[
153 (i + current_playlist_entry) % playlist_len]);
160 static char **pls_get_audio_file_list(unsigned int num)
165 num = PARA_MIN(num, playlist_len);
168 file_list = para_malloc((num + 1) * sizeof(char *));
169 for (i = 0; i < num; i++) {
170 unsigned j = (current_playlist_entry + i + 1) % playlist_len;
171 file_list[i] = para_strdup(playlist[j]);
177 static void free_playlist_contents(void)
181 PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len);
182 for (i = 0; i < playlist_len; i++)
184 current_playlist_entry = 0;
188 static void pls_shutdown(void)
190 struct private_pls_data *ppd = self->private_data;
192 shm_detach(ppd->client_data);
193 shm_destroy(ppd->client_data_shm_id);
194 mutex_destroy(ppd->server_mutex);
195 mutex_destroy(ppd->client_mutex);
197 free_playlist_contents();
204 static void pls_post_select(__a_unused fd_set *rfds, __a_unused fd_set *wfds)
206 struct private_pls_data *ppd = self->private_data;
207 struct pls_client_data *pcd = ppd->client_data;
211 mutex_lock(ppd->server_mutex);
214 free_playlist_contents();
215 ret = shm_attach(pcd->shm_id, ATTACH_RW, &shm);
217 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
220 PARA_DEBUG_LOG("loading new playlist (%zd bytes)\n", pcd->size);
221 ret = for_each_line((char *)shm, pcd->size, &playlist_add, NULL);
223 PARA_NOTICE_LOG("new playlist (%d entries)\n", playlist_len);
224 sprintf(mmd->selector_info, "dbinfo1:new playlist: %d files\n"
225 "dbinfo2:\ndbinfo3:\n", playlist_len);
228 mutex_unlock(pcd->mutex);
230 mutex_unlock(ppd->server_mutex);
233 static size_t string_offset(const char *str, size_t max)
235 size_t l = strlen(str);
242 static void pls_update_audio_file(char *audio_file)
245 char *dir = para_dirname(audio_file),
246 *prev = playlist[current_playlist_entry % playlist_len];
247 size_t dir_off = string_offset(dir, 50),
248 prev_off = string_offset(prev, 70);
250 for (i = 1; i <= playlist_len; i++) {
253 unsigned j = (current_playlist_entry + i) % playlist_len;
254 if (strcmp(playlist[j], audio_file))
256 current_playlist_entry = j;
257 next = playlist[(j + 1) %playlist_len];
258 next_off = string_offset(next, 70);
259 snprintf(mmd->selector_info, MMD_INFO_SIZE,
260 "dbinfo1: %d files, current dir: %s%s\n"
261 "dbinfo2: prev: %s%s\n"
262 "dbinfo3: next: %s%s\n",
264 dir_off? "... " : "", dir + dir_off,
265 prev_off? "... " : "", prev + prev_off,
266 next_off? "... " : "", next + next_off
274 * the init function for the playlist selector
276 * \param afs pointer to the struct to initialize
278 * Init all function pointers of \a afs
280 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
283 int playlist_selector_init(struct audio_file_selector *afs)
286 struct private_pls_data *ppd = NULL;
290 afs->cmd_list = playlist_selector_cmds;
291 afs->get_audio_file_list = pls_get_audio_file_list;
292 afs->shutdown = pls_shutdown;
293 afs->post_select = pls_post_select;
294 afs->update_audio_file = pls_update_audio_file;
295 ppd = para_calloc(sizeof(struct private_pls_data));
296 afs->private_data = ppd;
298 ppd->client_mutex = -1;
299 ppd->server_mutex = -1;
300 ppd->client_data_shm_id = -1;
301 ppd->client_data = NULL;
306 ppd->client_mutex = ret;
311 ppd->server_mutex = ret;
313 ret = shm_new(sizeof(struct pls_client_data));
316 ppd->client_data_shm_id = ret;
318 ret = shm_attach(ppd->client_data_shm_id, ATTACH_RW, &shm);
321 ppd->client_data = shm;
322 ppd->client_data->size = 0;
323 sprintf(mmd->selector_info, "dbinfo1: Welcome to the playlist "
324 "selector\ndbinfo2: no playlist loaded\ndbinfo3:\n");
327 if (ppd->client_data_shm_id >= 0)
328 shm_destroy(ppd->client_data_shm_id);
329 if (ppd->client_mutex >= 0)
330 mutex_destroy(ppd->client_mutex);
331 if (ppd->server_mutex >= 0)
332 mutex_destroy(ppd->server_mutex);