cosmetics, fix indentation
[mplayer/glamo.git] / fifo.c
blobbf365f66b48d017eb8f959ade7f19da56f2db243
1 #include "input/mouse.h"
4 int key_fifo_size = 7;
5 static int *key_fifo_data = NULL;
6 static int key_fifo_read=0;
7 static int key_fifo_write=0;
9 static void mplayer_put_key_internal(int code){
10 int fifo_free = key_fifo_read - key_fifo_write - 1;
11 if (fifo_free < 0) fifo_free += key_fifo_size;
12 // printf("mplayer_put_key(%d)\n",code);
13 if (key_fifo_data == NULL)
14 key_fifo_data = malloc(key_fifo_size * sizeof(int));
15 if(!fifo_free) return; // FIFO FULL!!
16 // reserve some space for key release events to avoid stuck keys
17 if((code & MP_KEY_DOWN) && fifo_free < (key_fifo_size >> 1))
18 return;
19 key_fifo_data[key_fifo_write]=code;
20 key_fifo_write=(key_fifo_write+1)%key_fifo_size;
23 int mplayer_get_key(int fd){
24 int key;
25 // printf("mplayer_get_key(%d)\n",fd);
26 if (key_fifo_data == NULL)
27 return MP_INPUT_NOTHING;
28 if(key_fifo_write==key_fifo_read) return MP_INPUT_NOTHING;
29 key=key_fifo_data[key_fifo_read];
30 key_fifo_read=(key_fifo_read+1)%key_fifo_size;
31 // printf("mplayer_get_key => %d\n",key);
32 return key;
36 static unsigned doubleclick_time = 300;
38 static void put_double(int code) {
39 if (code >= MOUSE_BTN0 && code <= MOUSE_BTN9)
40 mplayer_put_key_internal(code - MOUSE_BTN0 + MOUSE_BTN0_DBL);
43 void mplayer_put_key(int code) {
44 static unsigned last_key_time[2];
45 static int last_key[2];
46 unsigned now = GetTimerMS();
47 // ignore system-doubleclick if we generate these events ourselves
48 if (doubleclick_time &&
49 (code & ~MP_KEY_DOWN) >= MOUSE_BTN0_DBL &&
50 (code & ~MP_KEY_DOWN) <= MOUSE_BTN9_DBL)
51 return;
52 mplayer_put_key_internal(code);
53 if (code & MP_KEY_DOWN) {
54 code &= ~MP_KEY_DOWN;
55 last_key[1] = last_key[0];
56 last_key[0] = code;
57 last_key_time[1] = last_key_time[0];
58 last_key_time[0] = now;
59 if (last_key[1] == code &&
60 now - last_key_time[1] < doubleclick_time)
61 put_double(code);
62 return;
64 if (last_key[0] == code && last_key[1] == code &&
65 now - last_key_time[1] < doubleclick_time)
66 put_double(code);