From 8173e34351795c4b189fad8824fa82b688bd0f31 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Sep 2009 18:10:58 +0200 Subject: [PATCH] moved the idle timer to player_command.c Create it only on demand, while seeking. --- src/main.c | 24 ++++--------------- src/player_command.c | 57 ++++++++++++++++++++++++++++++++++++++++---- src/player_command.h | 7 ++++++ src/screen.c | 15 ------------ src/screen.h | 3 --- 5 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/main.c b/src/main.c index 8313d01..8d0d4b2 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ #include "screen_utils.h" #include "strfsong.h" #include "i18n.h" +#include "player_command.h" #ifndef NCMPC_MINI #include "conf.h" @@ -57,12 +58,10 @@ static const guint update_interval = 500; #define BUFSIZE 1024 -static const guint idle_interval = 500; - static struct mpdclient *mpd = NULL; static gboolean connected = FALSE; static GMainLoop *main_loop; -static guint reconnect_source_id, idle_source_id, update_source_id; +static guint reconnect_source_id, update_source_id; #ifndef NCMPC_MINI static guint check_key_bindings_source_id; @@ -273,28 +272,13 @@ timer_mpd_update(gpointer data) return GPOINTER_TO_INT(data); } -/** - * This idle timer is invoked when the user hasn't typed a key for - * 500ms. It is used for delayed seeking. - */ -static gboolean -timer_idle(G_GNUC_UNUSED gpointer data) -{ - screen_idle(mpd); - return TRUE; -} - void begin_input_event(void) { - /* remove the idle timeout; add it later with fresh interval */ - g_source_remove(idle_source_id); } void end_input_event(void) { screen_update(mpd); - - idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL); } int do_input_event(command_t cmd) @@ -515,7 +499,6 @@ main(int argc, const char *argv[]) #ifndef NCMPC_MINI check_key_bindings_source_id = g_timeout_add(10000, timer_check_key_bindings, NULL); #endif - idle_source_id = g_timeout_add(idle_interval, timer_idle, NULL); screen_paint(mpd); @@ -523,8 +506,9 @@ main(int argc, const char *argv[]) /* cleanup */ + cancel_seek_timer(); + g_source_remove(update_source_id); - g_source_remove(idle_source_id); #ifndef NCMPC_MINI if (check_key_bindings_source_id != 0) diff --git a/src/player_command.c b/src/player_command.c index 478d090..dab3d52 100644 --- a/src/player_command.c +++ b/src/player_command.c @@ -28,7 +28,52 @@ #define IS_STOPPED(s) (!(IS_PLAYING(s) | IS_PAUSED(s))) int seek_id = -1; -int seek_target_time = 0; +int seek_target_time; + +static guint seek_source_id; + +static void +commit_seek(struct mpdclient *c) +{ + if (seek_id < 0) + return; + + if (c->song != NULL && (unsigned)seek_id == mpd_song_get_id(c->song)) + mpdclient_cmd_seek(c, seek_id, seek_target_time); + + seek_id = -1; +} + +/** + * This timer is invoked after seeking when the user hasn't typed a + * key for 500ms. It is used to do the real seeking. + */ +static gboolean +seek_timer(gpointer data) +{ + struct mpdclient *c = data; + + seek_source_id = 0; + commit_seek(c); + return false; +} + +static void +schedule_seek_timer(struct mpdclient *c) +{ + assert(seek_source_id == 0); + + seek_source_id = g_timeout_add(500, seek_timer, c); +} + +void +cancel_seek_timer(void) +{ + if (seek_source_id != 0) { + g_source_remove(seek_source_id); + seek_source_id = 0; + } +} bool handle_player_command(struct mpdclient *c, command_t cmd) @@ -36,6 +81,8 @@ handle_player_command(struct mpdclient *c, command_t cmd) if (c->connection == NULL || c->status == NULL) return false; + cancel_seek_timer(); + switch(cmd) { /* case CMD_PLAY: @@ -59,10 +106,9 @@ handle_player_command(struct mpdclient *c, command_t cmd) 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)) - break; - seek_target_time = mpd_status_get_total_time(c->status); - /* seek_target_time=0; */ + 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... */ @@ -79,6 +125,7 @@ handle_player_command(struct mpdclient *c, command_t cmd) seek_target_time-=options.seek_time; if (seek_target_time < 0) seek_target_time=0; + schedule_seek_timer(c); } break; case CMD_TRACK_PREVIOUS: diff --git a/src/player_command.h b/src/player_command.h index d854515..689116f 100644 --- a/src/player_command.h +++ b/src/player_command.h @@ -29,6 +29,13 @@ struct mpdclient; extern int seek_id; extern int seek_target_time; +/** + * Call this before exiting; it will unschedule the timer for delayed + * seeking. + */ +void +cancel_seek_timer(void); + bool handle_player_command(struct mpdclient *c, command_t cmd); diff --git a/src/screen.c b/src/screen.c index 0545bd0..13dd117 100644 --- a/src/screen.c +++ b/src/screen.c @@ -557,7 +557,6 @@ screen_init(struct mpdclient *c) screen.buf_size = screen.cols; screen.findbuf = NULL; screen.start_timestamp = time(NULL); - screen.last_cmd = CMD_NONE; /* create top window */ screen.top_window.rows = 2; @@ -729,7 +728,6 @@ screen_update(struct mpdclient *c) /* update title/header window */ if (welcome && options.welcome_screen_list && - screen.last_cmd==CMD_NONE && time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME) paint_top_window("", c, 0); else @@ -763,18 +761,6 @@ screen_update(struct mpdclient *c) doupdate(); } -void -screen_idle(struct mpdclient *c) -{ - if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song) && - (screen.last_cmd == CMD_SEEK_FORWARD || - screen.last_cmd == CMD_SEEK_BACKWARD)) - mpdclient_cmd_seek(c, seek_id, seek_target_time); - - screen.last_cmd = CMD_NONE; - seek_id = -1; -} - #ifdef HAVE_GETMOUSE int screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row) @@ -800,7 +786,6 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row) void screen_cmd(struct mpdclient *c, command_t cmd) { - screen.last_cmd = cmd; #ifndef NCMPC_MINI welcome = FALSE; #endif diff --git a/src/screen.h b/src/screen.h index b22eae4..32d57b0 100644 --- a/src/screen.h +++ b/src/screen.h @@ -56,8 +56,6 @@ struct screen { GTime start_timestamp; GTime status_timestamp; - command_t last_cmd; - unsigned cols, rows; char *buf; @@ -112,7 +110,6 @@ void screen_status_printf(const char *format, ...); char *screen_error(void); void screen_paint(struct mpdclient *c); void screen_update(struct mpdclient *c); -void screen_idle(struct mpdclient *c); void screen_cmd(struct mpdclient *c, command_t cmd); gint screen_get_id(const char *name); -- 2.30.2