d1149269b9312e512a5148b034b6c2f0e4704ce8
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 */
15 #include "user_list.h"
16 #include "playlist_selector_command_list.h"
19 * structure used for transmission of the playlist
21 * There's one such struct which gets initialized during startup. It lives in
22 * shared memory and is used by com_lpl().
24 struct pls_client_data
{
25 /** allocated and set by com_lpl() (child) */
27 /** the size of the shared memory area identified by \a shm_id */
29 /** initially locked, gets unlocked by parent when it is done */
31 /** return value, set by parent */
35 /** data specific to the playlist selector */
36 struct private_pls_data
{
37 /** guards against concurrent client access */
39 /** guards against concurrent parent-child access */
41 /** pointer to the client data */
42 struct pls_client_data
*client_data
;
43 /** id of the shm corresponding to \a client_data */
44 int client_data_shm_id
;
47 /** we refuse to load playlists bigger than that */
48 #define MAX_PLAYLIST_BYTES (1024 * 1024)
50 static unsigned playlist_len
, playlist_size
, current_playlist_entry
;
51 static char **playlist
;
52 static struct audio_file_selector
*self
;
54 extern struct misc_meta_data
*mmd
;
56 static void playlist_add(char *path
)
58 if (playlist_len
>= playlist_size
) {
59 playlist_size
= 2 * playlist_size
+ 1;
60 playlist
= para_realloc(playlist
, playlist_size
* sizeof(char *));
62 PARA_DEBUG_LOG("adding #%d/%d: %s\n", playlist_len
, playlist_size
, path
);
63 playlist
[playlist_len
++] = para_strdup(path
);
66 static int send_playlist_to_server(const char *buf
, size_t size
)
68 struct private_pls_data
*ppd
= self
->private_data
;
69 int ret
, shm_mutex
= -1, shm_id
= -1;
72 PARA_DEBUG_LOG("new playlist (%zd bytes)\n", size
);
84 ret
= shm_attach(shm_id
, ATTACH_RW
, &shm
);
87 mutex_lock(shm_mutex
);
88 memcpy(shm
, buf
, size
);
89 mutex_lock(ppd
->client_mutex
);
90 mutex_lock(ppd
->server_mutex
);
91 ppd
->client_data
->size
= size
;
92 ppd
->client_data
->shm_id
= shm_id
;
93 ppd
->client_data
->mutex
= shm_mutex
;
94 kill(getppid(), SIGUSR1
); /* wake up the server */
95 mutex_unlock(ppd
->server_mutex
);
96 mutex_lock(shm_mutex
); /* wait until server is done */
97 mutex_unlock(shm_mutex
);
98 ret
= ppd
->client_data
->retval
;
99 mutex_unlock(ppd
->client_mutex
);
104 mutex_destroy(shm_mutex
);
105 PARA_DEBUG_LOG("returning %d\n", ret
);
109 int com_lpl(int fd
, __a_unused
int argc
, __a_unused
char *argv
[])
112 size_t bufsize
= 4096; /* guess that's enough */
113 char *buf
= para_malloc(bufsize
);
115 ret
= send_buffer(fd
, AWAITING_DATA_MSG
);
119 ret
= recv_bin_buffer(fd
, buf
+ loaded
, bufsize
- loaded
);
123 ret
= send_playlist_to_server(buf
, loaded
);
127 ret
= -E_LOAD_PLAYLIST
;
128 if (loaded
>= MAX_PLAYLIST_BYTES
)
130 if (loaded
>= bufsize
) {
132 buf
= para_realloc(buf
, bufsize
);
140 int com_ppl(int fd
, __a_unused
int argc
, __a_unused
char *argv
[])
144 PARA_DEBUG_LOG("sending playlist to client (%d entries)\n", playlist_len
);
145 for (i
= 0; i
< playlist_len
; i
++) {
146 int ret
= send_va_buffer(fd
, "%s\n", playlist
[
147 (i
+ current_playlist_entry
) % playlist_len
]);
154 static char **pls_get_audio_file_list(unsigned int num
)
159 num
= PARA_MIN(num
, playlist_len
);
162 file_list
= para_malloc((num
+ 1) * sizeof(char *));
163 for (i
= 0; i
< num
; i
++) {
164 unsigned j
= (current_playlist_entry
+ i
+ 1) % playlist_len
;
165 file_list
[i
] = para_strdup(playlist
[j
]);
171 static void free_playlist_contents(void)
175 PARA_DEBUG_LOG("freeing playlist (%d entries)\n", playlist_len
);
176 for (i
= 0; i
< playlist_len
; i
++)
178 current_playlist_entry
= 0;
182 static void pls_shutdown(void)
184 struct private_pls_data
*ppd
= self
->private_data
;
186 shm_detach(ppd
->client_data
);
187 shm_destroy(ppd
->client_data_shm_id
);
188 mutex_destroy(ppd
->server_mutex
);
189 mutex_destroy(ppd
->client_mutex
);
191 free_playlist_contents();
198 static void pls_post_select(__a_unused fd_set
*rfds
, __a_unused fd_set
*wfds
)
200 struct private_pls_data
*ppd
= self
->private_data
;
201 struct pls_client_data
*pcd
= ppd
->client_data
;
205 mutex_lock(ppd
->server_mutex
);
208 free_playlist_contents();
209 ret
= shm_attach(pcd
->shm_id
, ATTACH_RW
, &shm
);
211 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret
));
214 PARA_DEBUG_LOG("loading new playlist (%zd bytes)\n", pcd
->size
);
215 ret
= for_each_line((char *)shm
, pcd
->size
, &playlist_add
);
217 PARA_NOTICE_LOG("new playlist (%d entries)\n", playlist_len
);
218 sprintf(mmd
->selector_info
, "dbinfo1:new playlist: %d files\n"
219 "dbinfo2:\ndbinfo3:\n", playlist_len
);
222 mutex_unlock(pcd
->mutex
);
224 mutex_unlock(ppd
->server_mutex
);
227 static size_t string_offset(const char *str
, size_t max
)
229 size_t l
= strlen(str
);
236 static void pls_update_audio_file(char *audio_file
)
239 char *dir
= para_dirname(audio_file
),
240 *prev
= playlist
[current_playlist_entry
% playlist_len
];
241 size_t dir_off
= string_offset(dir
, 50),
242 prev_off
= string_offset(prev
, 70);
244 for (i
= 1; i
<= playlist_len
; i
++) {
247 unsigned j
= (current_playlist_entry
+ i
) % playlist_len
;
248 if (strcmp(playlist
[j
], audio_file
))
250 current_playlist_entry
= j
;
251 next
= playlist
[(j
+ 1) %playlist_len
];
252 next_off
= string_offset(next
, 70);
253 snprintf(mmd
->selector_info
, MMD_INFO_SIZE
,
254 "dbinfo1: %d files, current dir: %s%s\n"
255 "dbinfo2: prev: %s%s\n"
256 "dbinfo3: next: %s%s\n",
258 dir_off
? "... " : "", dir
+ dir_off
,
259 prev_off
? "... " : "", prev
+ prev_off
,
260 next_off
? "... " : "", next
+ next_off
268 * the init function for the playlist selector
270 * \param afs pointer to the struct to initialize
272 * Init all function pointers of \a afs
274 * \sa struct audio_file_selector, misc_meta_data::selector_info, mysql.c
277 int playlist_selector_init(struct audio_file_selector
*afs
)
280 struct private_pls_data
*ppd
= NULL
;
284 afs
->cmd_list
= playlist_selector_cmds
;
285 afs
->get_audio_file_list
= pls_get_audio_file_list
;
286 afs
->shutdown
= pls_shutdown
;
287 afs
->post_select
= pls_post_select
;
288 afs
->update_audio_file
= pls_update_audio_file
;
289 ppd
= para_calloc(sizeof(struct private_pls_data
));
290 afs
->private_data
= ppd
;
292 ppd
->client_mutex
= -1;
293 ppd
->server_mutex
= -1;
294 ppd
->client_data_shm_id
= -1;
295 ppd
->client_data
= NULL
;
300 ppd
->client_mutex
= ret
;
305 ppd
->server_mutex
= ret
;
307 ret
= shm_new(sizeof(struct pls_client_data
));
310 ppd
->client_data_shm_id
= ret
;
312 ret
= shm_attach(ppd
->client_data_shm_id
, ATTACH_RW
, &shm
);
315 ppd
->client_data
= shm
;
316 ppd
->client_data
->size
= 0;
317 sprintf(mmd
->selector_info
, "dbinfo1: Welcome to the playlist "
318 "selector\ndbinfo2: no playlist loaded\ndbinfo3:\n");
321 if (ppd
->client_data_shm_id
>= 0)
322 shm_destroy(ppd
->client_data_shm_id
);
323 if (ppd
->client_mutex
>= 0)
324 mutex_destroy(ppd
->client_mutex
);
325 if (ppd
->server_mutex
>= 0)
326 mutex_destroy(ppd
->server_mutex
);