Client: add "playlist" attribute

Reduce access to the global variable "g_playlist".
This commit is contained in:
Max Kellermann 2013-01-04 23:07:11 +01:00
parent b0833084a6
commit fe8e77e556
7 changed files with 84 additions and 64 deletions

View File

@ -27,14 +27,16 @@
#include <stdarg.h>
struct sockaddr;
struct playlist;
struct player_control;
class Client;
void client_manager_init(void);
void client_manager_deinit(void);
void client_new(struct player_control *player_control,
int fd, const struct sockaddr *sa, size_t sa_length, int uid);
void
client_new(struct playlist &playlist, struct player_control *player_control,
int fd, const struct sockaddr *sa, size_t sa_length, int uid);
gcc_pure
bool client_is_expired(const Client *client);

View File

@ -46,6 +46,7 @@ struct deferred_buffer {
class Client {
public:
struct playlist &playlist;
struct player_control *player_control;
GIOChannel *channel;
@ -99,7 +100,8 @@ public:
*/
std::list<ClientMessage> messages;
Client(struct player_control *player_control,
Client(struct playlist &playlist,
struct player_control *player_control,
int fd, int uid, int num);
~Client();

View File

@ -45,9 +45,10 @@ extern "C" {
static const char GREETING[] = "OK MPD " PROTOCOL_VERSION "\n";
Client::Client(struct player_control *_player_control,
Client::Client(struct playlist &_playlist,
struct player_control *_player_control,
int fd, int _uid, int _num)
:player_control(_player_control),
:playlist(_playlist), player_control(_player_control),
input(fifo_buffer_new(4096)),
permission(getDefaultPermissions()),
uid(_uid),
@ -93,7 +94,7 @@ Client::~Client()
}
void
client_new(struct player_control *player_control,
client_new(struct playlist &playlist, struct player_control *player_control,
int fd, const struct sockaddr *sa, size_t sa_length, int uid)
{
static unsigned int next_client_num;
@ -133,7 +134,7 @@ client_new(struct player_control *player_control,
return;
}
Client *client = new Client(player_control, fd, uid,
Client *client = new Client(playlist, player_control, fd, uid,
next_client_num++);
(void)send(fd, GREETING, sizeof(GREETING) - 1, 0);

View File

@ -20,6 +20,7 @@
#include "config.h"
#include "Listen.hxx"
#include "Main.hxx"
#include "Playlist.hxx"
#include "Client.hxx"
extern "C" {
@ -46,7 +47,8 @@ static void
listen_callback(int fd, const struct sockaddr *address,
size_t address_length, int uid, G_GNUC_UNUSED void *ctx)
{
client_new(global_player_control, fd, address, address_length, uid);
client_new(g_playlist, global_player_control,
fd, address, address_length, uid);
}
static bool

View File

@ -65,7 +65,8 @@ handle_play(Client *client, int argc, char *argv[])
if (argc == 2 && !check_int(client, &song, argv[1]))
return COMMAND_RETURN_ERROR;
result = playlist_play(&g_playlist, client->player_control, song);
result = playlist_play(&client->playlist, client->player_control,
song);
return print_playlist_result(client, result);
}
@ -78,15 +79,16 @@ handle_playid(Client *client, int argc, char *argv[])
if (argc == 2 && !check_int(client, &id, argv[1]))
return COMMAND_RETURN_ERROR;
result = playlist_play_id(&g_playlist, client->player_control, id);
result = playlist_play_id(&client->playlist, client->player_control,
id);
return print_playlist_result(client, result);
}
enum command_return
handle_stop(G_GNUC_UNUSED Client *client,
handle_stop(Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist_stop(&g_playlist, client->player_control);
playlist_stop(&client->playlist, client->player_control);
return COMMAND_RETURN_OK;
}
@ -94,7 +96,7 @@ enum command_return
handle_currentsong(Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist_print_current(client, &g_playlist);
playlist_print_current(client, &client->playlist);
return COMMAND_RETURN_OK;
}
@ -138,6 +140,7 @@ handle_status(Client *client,
break;
}
const playlist &playlist = client->playlist;
client_printf(client,
"volume: %i\n"
COMMAND_STATUS_REPEAT ": %i\n"
@ -151,23 +154,23 @@ handle_status(Client *client,
COMMAND_STATUS_MIXRAMPDELAY ": %f\n"
COMMAND_STATUS_STATE ": %s\n",
volume_level_get(),
playlist_get_repeat(&g_playlist),
playlist_get_random(&g_playlist),
playlist_get_single(&g_playlist),
playlist_get_consume(&g_playlist),
playlist_get_version(&g_playlist),
playlist_get_length(&g_playlist),
playlist_get_repeat(&playlist),
playlist_get_random(&playlist),
playlist_get_single(&playlist),
playlist_get_consume(&playlist),
playlist_get_version(&playlist),
playlist_get_length(&playlist),
(int)(pc_get_cross_fade(client->player_control) + 0.5),
pc_get_mixramp_db(client->player_control),
pc_get_mixramp_delay(client->player_control),
state);
song = playlist_get_current_song(&g_playlist);
song = playlist_get_current_song(&playlist);
if (song >= 0) {
client_printf(client,
COMMAND_STATUS_SONG ": %i\n"
COMMAND_STATUS_SONGID ": %u\n",
song, playlist_get_song_id(&g_playlist, song));
song, playlist_get_song_id(&playlist, song));
}
if (player_status.state != PLAYER_STATE_STOP) {
@ -200,37 +203,39 @@ handle_status(Client *client,
g_free(error);
}
song = playlist_get_next_song(&g_playlist);
song = playlist_get_next_song(&playlist);
if (song >= 0) {
client_printf(client,
COMMAND_STATUS_NEXTSONG ": %i\n"
COMMAND_STATUS_NEXTSONGID ": %u\n",
song, playlist_get_song_id(&g_playlist, song));
song, playlist_get_song_id(&playlist, song));
}
return COMMAND_RETURN_OK;
}
enum command_return
handle_next(G_GNUC_UNUSED Client *client,
handle_next(Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist &playlist = client->playlist;
/* single mode is not considered when this is user who
* wants to change song. */
const bool single = g_playlist.queue.single;
g_playlist.queue.single = false;
const bool single = playlist.queue.single;
playlist.queue.single = false;
playlist_next(&g_playlist, client->player_control);
playlist_next(&playlist, client->player_control);
g_playlist.queue.single = single;
playlist.queue.single = single;
return COMMAND_RETURN_OK;
}
enum command_return
handle_previous(G_GNUC_UNUSED Client *client,
handle_previous(Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist_previous(&g_playlist, client->player_control);
playlist_previous(&client->playlist, client->player_control);
return COMMAND_RETURN_OK;
}
@ -241,7 +246,7 @@ handle_repeat(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_bool(client, &status, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_set_repeat(&g_playlist, client->player_control, status);
playlist_set_repeat(&client->playlist, client->player_control, status);
return COMMAND_RETURN_OK;
}
@ -252,7 +257,7 @@ handle_single(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_bool(client, &status, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_set_single(&g_playlist, client->player_control, status);
playlist_set_single(&client->playlist, client->player_control, status);
return COMMAND_RETURN_OK;
}
@ -263,7 +268,7 @@ handle_consume(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_bool(client, &status, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_set_consume(&g_playlist, status);
playlist_set_consume(&client->playlist, status);
return COMMAND_RETURN_OK;
}
@ -274,7 +279,7 @@ handle_random(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_bool(client, &status, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_set_random(&g_playlist, client->player_control, status);
playlist_set_random(&client->playlist, client->player_control, status);
return COMMAND_RETURN_OK;
}
@ -297,7 +302,7 @@ handle_seek(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_unsigned(client, &seek_time, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_seek_song(&g_playlist, client->player_control,
result = playlist_seek_song(&client->playlist, client->player_control,
song, seek_time);
return print_playlist_result(client, result);
}
@ -313,7 +318,8 @@ handle_seekid(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_unsigned(client, &seek_time, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_seek_song_id(&g_playlist, client->player_control,
result = playlist_seek_song_id(&client->playlist,
client->player_control,
id, seek_time);
return print_playlist_result(client, result);
}
@ -328,7 +334,8 @@ handle_seekcur(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
enum playlist_result result =
playlist_seek_current(&g_playlist, client->player_control,
playlist_seek_current(&client->playlist,
client->player_control,
seek_time, relative);
return print_playlist_result(client, result);
}

View File

@ -56,7 +56,7 @@ handle_save(Client *client, G_GNUC_UNUSED int argc, char *argv[])
{
enum playlist_result result;
result = spl_save_playlist(argv[1], &g_playlist);
result = spl_save_playlist(argv[1], &client->playlist);
return print_playlist_result(client, result);
}
@ -75,13 +75,13 @@ handle_load(Client *client, int argc, char *argv[])
result = playlist_open_into_queue(argv[1],
start_index, end_index,
&g_playlist,
&client->playlist,
client->player_control, true);
if (result != PLAYLIST_RESULT_NO_SUCH_LIST)
return print_playlist_result(client, result);
GError *error = NULL;
if (playlist_load_spl(&g_playlist, client->player_control,
if (playlist_load_spl(&client->playlist, client->player_control,
argv[1], start_index, end_index,
&error))
return COMMAND_RETURN_OK;

View File

@ -49,7 +49,7 @@ handle_add(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!client_allow_file(client, path, &error))
return print_error(client, error);
result = playlist_append_file(&g_playlist,
result = playlist_append_file(&client->playlist,
client->player_control,
path,
NULL);
@ -63,7 +63,7 @@ handle_add(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
}
result = playlist_append_uri(&g_playlist,
result = playlist_append_uri(&client->playlist,
client->player_control,
uri, NULL);
return print_playlist_result(client, result);
@ -89,7 +89,7 @@ handle_addid(Client *client, int argc, char *argv[])
if (!client_allow_file(client, path, &error))
return print_error(client, error);
result = playlist_append_file(&g_playlist,
result = playlist_append_file(&client->playlist,
client->player_control,
path,
&added_id);
@ -100,7 +100,7 @@ handle_addid(Client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR;
}
result = playlist_append_uri(&g_playlist,
result = playlist_append_uri(&client->playlist,
client->player_control,
uri, &added_id);
}
@ -112,12 +112,14 @@ handle_addid(Client *client, int argc, char *argv[])
unsigned to;
if (!check_unsigned(client, &to, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_move_id(&g_playlist, client->player_control,
result = playlist_move_id(&client->playlist,
client->player_control,
added_id, to);
if (result != PLAYLIST_RESULT_SUCCESS) {
enum command_return ret =
print_playlist_result(client, result);
playlist_delete_id(&g_playlist, client->player_control,
playlist_delete_id(&client->playlist,
client->player_control,
added_id);
return ret;
}
@ -136,7 +138,8 @@ handle_delete(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_range(client, &start, &end, argv[1]))
return COMMAND_RETURN_ERROR;
result = playlist_delete_range(&g_playlist, client->player_control,
result = playlist_delete_range(&client->playlist,
client->player_control,
start, end);
return print_playlist_result(client, result);
}
@ -150,7 +153,8 @@ handle_deleteid(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_unsigned(client, &id, argv[1]))
return COMMAND_RETURN_ERROR;
result = playlist_delete_id(&g_playlist, client->player_control, id);
result = playlist_delete_id(&client->playlist,
client->player_control, id);
return print_playlist_result(client, result);
}
@ -158,7 +162,7 @@ enum command_return
handle_playlist(Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist_print_uris(client, &g_playlist);
playlist_print_uris(client, &client->playlist);
return COMMAND_RETURN_OK;
}
@ -166,11 +170,12 @@ enum command_return
handle_shuffle(G_GNUC_UNUSED Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
unsigned start = 0, end = queue_length(&g_playlist.queue);
unsigned start = 0, end = queue_length(&client->playlist.queue);
if (argc == 2 && !check_range(client, &start, &end, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_shuffle(&g_playlist, client->player_control, start, end);
playlist_shuffle(&client->playlist, client->player_control,
start, end);
return COMMAND_RETURN_OK;
}
@ -178,7 +183,7 @@ enum command_return
handle_clear(G_GNUC_UNUSED Client *client,
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
{
playlist_clear(&g_playlist, client->player_control);
playlist_clear(&client->playlist, client->player_control);
return COMMAND_RETURN_OK;
}
@ -190,7 +195,7 @@ handle_plchanges(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_uint32(client, &version, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_print_changes_info(client, &g_playlist, version);
playlist_print_changes_info(client, &client->playlist, version);
return COMMAND_RETURN_OK;
}
@ -202,7 +207,7 @@ handle_plchangesposid(Client *client, G_GNUC_UNUSED int argc, char *argv[])
if (!check_uint32(client, &version, argv[1]))
return COMMAND_RETURN_ERROR;
playlist_print_changes_position(client, &g_playlist, version);
playlist_print_changes_position(client, &client->playlist, version);
return COMMAND_RETURN_OK;
}
@ -215,7 +220,7 @@ handle_playlistinfo(Client *client, int argc, char *argv[])
if (argc == 2 && !check_range(client, &start, &end, argv[1]))
return COMMAND_RETURN_ERROR;
ret = playlist_print_info(client, &g_playlist, start, end);
ret = playlist_print_info(client, &client->playlist, start, end);
if (!ret)
return print_playlist_result(client,
PLAYLIST_RESULT_BAD_RANGE);
@ -231,12 +236,12 @@ handle_playlistid(Client *client, int argc, char *argv[])
if (!check_unsigned(client, &id, argv[1]))
return COMMAND_RETURN_ERROR;
bool ret = playlist_print_id(client, &g_playlist, id);
bool ret = playlist_print_id(client, &client->playlist, id);
if (!ret)
return print_playlist_result(client,
PLAYLIST_RESULT_NO_SUCH_SONG);
} else {
playlist_print_info(client, &g_playlist, 0, G_MAXUINT);
playlist_print_info(client, &client->playlist, 0, G_MAXUINT);
}
return COMMAND_RETURN_OK;
@ -252,7 +257,7 @@ handle_playlist_match(Client *client, int argc, char *argv[],
return COMMAND_RETURN_ERROR;
}
playlist_print_find(client, &g_playlist, filter);
playlist_print_find(client, &client->playlist, filter);
return COMMAND_RETURN_OK;
}
@ -289,7 +294,7 @@ handle_prio(Client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR;
enum playlist_result result =
playlist_set_priority(&g_playlist,
playlist_set_priority(&client->playlist,
client->player_control,
start_position, end_position,
priority);
@ -320,7 +325,7 @@ handle_prioid(Client *client, int argc, char *argv[])
return COMMAND_RETURN_ERROR;
enum playlist_result result =
playlist_set_priority_id(&g_playlist,
playlist_set_priority_id(&client->playlist,
client->player_control,
song_id, priority);
if (result != PLAYLIST_RESULT_SUCCESS)
@ -341,7 +346,7 @@ handle_move(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
if (!check_int(client, &to, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_move_range(&g_playlist, client->player_control,
result = playlist_move_range(&client->playlist, client->player_control,
start, end, to);
return print_playlist_result(client, result);
}
@ -357,7 +362,7 @@ handle_moveid(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
if (!check_int(client, &to, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_move_id(&g_playlist, client->player_control,
result = playlist_move_id(&client->playlist, client->player_control,
id, to);
return print_playlist_result(client, result);
}
@ -372,7 +377,7 @@ handle_swap(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
if (!check_unsigned(client, &song2, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_swap_songs(&g_playlist, client->player_control,
result = playlist_swap_songs(&client->playlist, client->player_control,
song1, song2);
return print_playlist_result(client, result);
}
@ -387,7 +392,8 @@ handle_swapid(Client *client, G_GNUC_UNUSED int argc, char *argv[])
return COMMAND_RETURN_ERROR;
if (!check_unsigned(client, &id2, argv[2]))
return COMMAND_RETURN_ERROR;
result = playlist_swap_songs_id(&g_playlist, client->player_control,
result = playlist_swap_songs_id(&client->playlist,
client->player_control,
id1, id2);
return print_playlist_result(client, result);
}