Makefile: Don't use "install -d" on existing directories
[mplayer/glamo.git] / mp_fifo.c
blob62228e8558d067a2117859b95cf00815cb633113
1 #include <stdlib.h>
2 #include "osdep/timer.h"
3 #include "input/input.h"
4 #include "input/mouse.h"
5 #include "mp_fifo.h"
6 #include "talloc.h"
7 #include "options.h"
10 struct mp_fifo {
11 struct MPOpts *opts;
12 int *data;
13 int readpos;
14 int writepos;
15 int size;
16 unsigned last_key_time[2];
17 int last_key[2];
20 struct mp_fifo *mp_fifo_create(struct MPOpts *opts)
22 struct mp_fifo *fifo = talloc_zero(NULL, struct mp_fifo);
23 fifo->opts = opts;
24 fifo->size = opts->key_fifo_size;
25 fifo->data = talloc_array_ptrtype(fifo, fifo->data, fifo->size);
26 return fifo;
29 static void mplayer_put_key_internal(struct mp_fifo *fifo, int code)
31 int fifo_free = fifo->readpos - fifo->writepos - 1;
32 if (fifo_free < 0)
33 fifo_free += fifo->size;
34 if (!fifo_free)
35 return; // FIFO FULL!!
36 // reserve some space for key release events to avoid stuck keys
37 if((code & MP_KEY_DOWN) && fifo_free < (fifo->size >> 1))
38 return;
39 fifo->data[fifo->writepos++] = code;
40 fifo->writepos %= fifo->size;
43 int mplayer_get_key(void *ctx, int fd)
45 struct mp_fifo *fifo = ctx;
46 if (fifo->writepos == fifo->readpos)
47 return MP_INPUT_NOTHING;
48 int key = fifo->data[fifo->readpos++];
49 fifo->readpos %= fifo->size;
50 return key;
53 static void put_double(struct mp_fifo *fifo, int code)
55 if (code >= MOUSE_BTN0 && code <= MOUSE_BTN9)
56 mplayer_put_key_internal(fifo, code - MOUSE_BTN0 + MOUSE_BTN0_DBL);
59 void mplayer_put_key(struct mp_fifo *fifo, int code)
61 unsigned now = GetTimerMS();
62 int doubleclick_time = fifo->opts->doubleclick_time;
63 // ignore system-doubleclick if we generate these events ourselves
64 if (doubleclick_time
65 && (code & ~MP_KEY_DOWN) >= MOUSE_BTN0_DBL
66 && (code & ~MP_KEY_DOWN) <= MOUSE_BTN9_DBL)
67 return;
68 mplayer_put_key_internal(fifo, code);
69 if (code & MP_KEY_DOWN) {
70 code &= ~MP_KEY_DOWN;
71 fifo->last_key[1] = fifo->last_key[0];
72 fifo->last_key[0] = code;
73 fifo->last_key_time[1] = fifo->last_key_time[0];
74 fifo->last_key_time[0] = now;
75 if (fifo->last_key[1] == code
76 && now - fifo->last_key_time[1] < doubleclick_time)
77 put_double(fifo, code);
78 return;
80 if (fifo->last_key[0] == code && fifo->last_key[1] == code
81 && now - fifo->last_key_time[1] < doubleclick_time)
82 put_double(fifo, code);