changeset 33014:55a5a5e334dc

Change fifo to not waste one entry needlessly.
author reimar
date Thu, 24 Mar 2011 21:56:34 +0000
parents aab47f5fdb9b
children 56f979f98698
files mp_fifo.c
diffstat 1 files changed, 7 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mp_fifo.c	Thu Mar 24 20:25:45 2011 +0000
+++ b/mp_fifo.c	Thu Mar 24 21:56:34 2011 +0000
@@ -24,12 +24,11 @@
 
 int key_fifo_size = 7;
 static int *key_fifo_data = NULL;
-static int key_fifo_read=0;
-static int key_fifo_write=0;
+static unsigned key_fifo_read=0;
+static unsigned key_fifo_write=0;
 
 static void mplayer_put_key_internal(int code){
-  int fifo_free = key_fifo_read - key_fifo_write - 1;
-  if (fifo_free < 0) fifo_free += key_fifo_size;
+  int fifo_free = key_fifo_read + key_fifo_size - key_fifo_write;
 //  printf("mplayer_put_key(%d)\n",code);
   if (key_fifo_data == NULL)
     key_fifo_data = malloc(key_fifo_size * sizeof(int));
@@ -37,8 +36,8 @@
   // reserve some space for key release events to avoid stuck keys
   if((code & MP_KEY_DOWN) && fifo_free < (key_fifo_size >> 1))
     return;
-  key_fifo_data[key_fifo_write]=code;
-  key_fifo_write=(key_fifo_write+1)%key_fifo_size;
+  key_fifo_data[key_fifo_write % key_fifo_size]=code;
+  key_fifo_write++;
 }
 
 int mplayer_get_key(int fd){
@@ -47,8 +46,8 @@
   if (key_fifo_data == NULL)
     return MP_INPUT_NOTHING;
   if(key_fifo_write==key_fifo_read) return MP_INPUT_NOTHING;
-  key=key_fifo_data[key_fifo_read];
-  key_fifo_read=(key_fifo_read+1)%key_fifo_size;
+  key=key_fifo_data[key_fifo_read % key_fifo_size];
+  key_fifo_read++;
 //  printf("mplayer_get_key => %d\n",key);
   return key;
 }