Code

moved the idle timer to player_command.c
authorMax Kellermann <max@duempel.org>
Mon, 28 Sep 2009 16:10:58 +0000 (18:10 +0200)
committerMax Kellermann <max@duempel.org>
Mon, 28 Sep 2009 16:10:58 +0000 (18:10 +0200)
Create it only on demand, while seeking.

src/main.c
src/player_command.c
src/player_command.h
src/screen.c
src/screen.h

index 8313d019308a49a0f045683acf6e8bf9c83f2cc6..8d0d4b2f2ef6067d96cfc4db3388a4b8c028f586 100644 (file)
@@ -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)
index 478d09073d5e6841c5e0ef210300daa6cbe28bc1..dab3d525def6462bea0943296843f58a1dd5969c 100644 (file)
 #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:
index d854515003b248df68a228c5bb9c024e9fa294ff..689116fe7eae0cb63a98b998e6cfe9a2f36cb8d4 100644 (file)
@@ -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);
 
index 0545bd079c4b8cb2f11595ae397ce5e58e2e669c..13dd1175ad1f92ddc2ed58271bcfa080d09ced7c 100644 (file)
@@ -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
index b22eae455d93c194129c98ccdcaa2eba3374221b..32d57b03c6e644cb6f91f21ad8de98e381b462ac 100644 (file)
@@ -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);