Code

Started to use glibs neat timer functionality in the main loop.
authorKalle Wallin <kaw@linux.se>
Sun, 28 Mar 2004 23:25:00 +0000 (23:25 +0000)
committerKalle Wallin <kaw@linux.se>
Sun, 28 Mar 2004 23:25:00 +0000 (23:25 +0000)
git-svn-id: https://svn.musicpd.org/ncmpc/trunk@527 09075e82-0dd4-0310-85a5-a0d7c8717e4f

main.c

diff --git a/main.c b/main.c
index 56c18a31652bc4cebb16bb0d37075a7c60eca3be..92e40b6dda539f92833a144d3594bb3ec3a28a6e 100644 (file)
--- a/main.c
+++ b/main.c
 #include "conf.h"
 
 
-static mpd_client_t *mpc = NULL;
+#define MPD_UPDATE_TIME  1.0
+
 
+static mpd_client_t *mpc = NULL;
+static GTimer       *timer = NULL;
 
 void
 exit_and_cleanup(void)
@@ -29,6 +32,8 @@ exit_and_cleanup(void)
       mpc_close(mpc);
     }
   g_free(options.host);
+  if( timer )
+    g_timer_destroy(timer);
 }
 
 void
@@ -43,7 +48,7 @@ main(int argc, const char *argv[])
 {
   options_t *options;
   struct sigaction act;
-  int counter, connected;
+  int connected;
 
   /* initialize options */
   options = options_init();
@@ -100,14 +105,16 @@ main(int argc, const char *argv[])
 
   /* initialize curses */
   screen_init();
-  
-  counter=0;
+
+  /* initialize timer */
+  timer = g_timer_new();
+
   connected=1;
   while( connected || options->reconnect )
     {
-      command_t cmd;
+      static gdouble t = G_MAXDOUBLE;
 
-      if( connected && counter==0  )
+      if( connected && t>=MPD_UPDATE_TIME )
        {
          mpc_update(mpc);
          if( mpc_error(mpc) )
@@ -120,20 +127,21 @@ main(int argc, const char *argv[])
            }
          else
            mpd_finishCommand(mpc->connection);
-         counter=10;
+         g_timer_start(timer);
        }
 
 
       if( connected )
        {
+         command_t cmd;
+
          screen_update(mpc);
          if( (cmd=get_keyboard_command()) != CMD_NONE )
            {
              screen_cmd(mpc, cmd);
              if( cmd==CMD_VOLUME_UP || cmd==CMD_VOLUME_DOWN)
-               counter=10; /* make shure we dont update the volume yet */
-             else
-               counter=0;
+               /* make shure we dont update the volume yet */
+               g_timer_start(timer);
            }
        }
       else if( options->reconnect )
@@ -145,13 +153,11 @@ main(int argc, const char *argv[])
            {
              screen_status_printf("Connected to %s!", options->host);
              connected=1;
-             counter=0;
            }
          doupdate();
        }
 
-      if( counter>0 )
-       counter--;
+      t = g_timer_elapsed(timer, NULL);
 
     }
   exit(EXIT_FAILURE);