X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fplayer_command.c;h=a366b2a3003cccd08cdd3c8a0593b54ccc665f14;hb=eb4967eab943e8ba74279864c23d980ac10c753c;hp=337d7677d6d7334fc4453be4bf05b691614f9457;hpb=fca2567482e65fe3bf3bc9f04015fab6ea27a692;p=ncmpc.git diff --git a/src/player_command.c b/src/player_command.c index 337d767..a366b2a 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -32,16 +32,19 @@ static guint seek_source_id; static void commit_seek(struct mpdclient *c) { + struct mpd_connection *connection; + if (seek_id < 0) return; - if (!mpdclient_is_connected(c)) { + connection = mpdclient_get_connection(c); + if (connection == NULL) { seek_id = -1; return; } if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song)) - if (!mpd_run_seek_id(c->connection, seek_id, seek_target_time)) + if (!mpd_run_seek_id(connection, seek_id, seek_target_time)) mpdclient_handle_error(c); seek_id = -1; @@ -58,6 +61,7 @@ seek_timer(gpointer data) seek_source_id = 0; commit_seek(c); + mpdclient_put_connection(c); return false; } @@ -78,10 +82,26 @@ cancel_seek_timer(void) } } +static bool +setup_seek(struct mpdclient *c) +{ + if (!mpdclient_is_playing(c)) + return false; + + if (seek_id != (int)mpd_status_get_song_id(c->status)) { + seek_id = mpd_status_get_song_id(c->status); + seek_target_time = mpd_status_get_elapsed_time(c->status); + } + + schedule_seek_timer(c); + + return true; +} + bool handle_player_command(struct mpdclient *c, command_t cmd) { - const struct mpd_song *song; + struct mpd_connection *connection; if (!mpdclient_is_connected(c) || c->status == NULL) return false; @@ -95,84 +115,119 @@ handle_player_command(struct mpdclient *c, command_t cmd) break; */ case CMD_PAUSE: - if (!mpd_run_pause(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_pause(connection, mpd_status_get_state(c->status) != MPD_STATE_PAUSE)) mpdclient_handle_error(c); break; case CMD_STOP: - if (!mpd_run_stop(c->connection)) + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_stop(connection)) mpdclient_handle_error(c); break; case CMD_CROP: mpdclient_cmd_crop(c); break; case CMD_SEEK_FORWARD: - song = mpdclient_get_current_song(c); - if (song != NULL) { - if (seek_id != (int)mpd_song_get_id(song)) { - seek_id = mpd_song_get_id(song); - seek_target_time = mpd_status_get_elapsed_time(c->status); - } - seek_target_time+=options.seek_time; - if (seek_target_time > (int)mpd_status_get_total_time(c->status)) - seek_target_time = mpd_status_get_total_time(c->status); - schedule_seek_timer(c); - } - break; - /* fall through... */ + if (!setup_seek(c)) + break; + + seek_target_time += options.seek_time; + if (seek_target_time > (int)mpd_status_get_total_time(c->status)) + seek_target_time = mpd_status_get_total_time(c->status); + break; + case CMD_TRACK_NEXT: - if (!mpd_run_next(c->connection)) + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_next(connection)) mpdclient_handle_error(c); break; case CMD_SEEK_BACKWARD: - song = mpdclient_get_current_song(c); - if (song != NULL) { - if (seek_id != (int)mpd_song_get_id(song)) { - seek_id = mpd_song_get_id(c->song); - seek_target_time = mpd_status_get_elapsed_time(c->status); - } - seek_target_time-=options.seek_time; - if (seek_target_time < 0) - seek_target_time=0; - schedule_seek_timer(c); - } + if (!setup_seek(c)) + break; + + seek_target_time -= options.seek_time; + if (seek_target_time < 0) + seek_target_time = 0; break; + case CMD_TRACK_PREVIOUS: - if (!mpd_run_previous(c->connection)) + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_previous(connection)) mpdclient_handle_error(c); break; case CMD_SHUFFLE: - if (mpd_run_shuffle(c->connection)) + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (mpd_run_shuffle(connection)) screen_status_message(_("Shuffled playlist")); else mpdclient_handle_error(c); break; case CMD_CLEAR: + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + if (mpdclient_cmd_clear(c)) screen_status_message(_("Cleared playlist")); break; case CMD_REPEAT: - if (!mpd_run_repeat(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_repeat(connection, !mpd_status_get_repeat(c->status))) mpdclient_handle_error(c); break; case CMD_RANDOM: - if (!mpd_run_random(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_random(connection, !mpd_status_get_random(c->status))) mpdclient_handle_error(c); break; case CMD_SINGLE: - if (!mpd_run_single(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_single(connection, !mpd_status_get_single(c->status))) mpdclient_handle_error(c); break; case CMD_CONSUME: - if (!mpd_run_consume(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_consume(connection, !mpd_status_get_consume(c->status))) mpdclient_handle_error(c); break; case CMD_CROSSFADE: - if (!mpd_run_crossfade(c->connection, + connection = mpdclient_get_connection(c); + if (connection == NULL) + break; + + if (!mpd_run_crossfade(connection, mpd_status_get_crossfade(c->status) > 0 ? 0 : options.crossfade_time)) mpdclient_handle_error(c);