sync with en/mplayer.1 rev. 30822
[mplayer/glamo.git] / mp_fifo.c
blob2f7d888706e769180bd2cc628af54b3e14769f1c
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <stdlib.h>
20 #include "osdep/timer.h"
21 #include "input/input.h"
22 #include "input/mouse.h"
23 #include "mp_fifo.h"
25 int key_fifo_size = 7;
26 static int *key_fifo_data = NULL;
27 static int key_fifo_read=0;
28 static int key_fifo_write=0;
30 static void mplayer_put_key_internal(int code){
31 int fifo_free = key_fifo_read - key_fifo_write - 1;
32 if (fifo_free < 0) fifo_free += key_fifo_size;
33 // printf("mplayer_put_key(%d)\n",code);
34 if (key_fifo_data == NULL)
35 key_fifo_data = malloc(key_fifo_size * sizeof(int));
36 if(!fifo_free) return; // FIFO FULL!!
37 // reserve some space for key release events to avoid stuck keys
38 if((code & MP_KEY_DOWN) && fifo_free < (key_fifo_size >> 1))
39 return;
40 key_fifo_data[key_fifo_write]=code;
41 key_fifo_write=(key_fifo_write+1)%key_fifo_size;
44 int mplayer_get_key(int fd){
45 int key;
46 // printf("mplayer_get_key(%d)\n",fd);
47 if (key_fifo_data == NULL)
48 return MP_INPUT_NOTHING;
49 if(key_fifo_write==key_fifo_read) return MP_INPUT_NOTHING;
50 key=key_fifo_data[key_fifo_read];
51 key_fifo_read=(key_fifo_read+1)%key_fifo_size;
52 // printf("mplayer_get_key => %d\n",key);
53 return key;
57 unsigned doubleclick_time = 300;
59 static void put_double(int code) {
60 if (code >= MOUSE_BTN0 && code <= MOUSE_BTN9)
61 mplayer_put_key_internal(code - MOUSE_BTN0 + MOUSE_BTN0_DBL);
64 void mplayer_put_key(int code) {
65 static unsigned last_key_time[2];
66 static int last_key[2];
67 unsigned now = GetTimerMS();
68 // ignore system-doubleclick if we generate these events ourselves
69 if (doubleclick_time &&
70 (code & ~MP_KEY_DOWN) >= MOUSE_BTN0_DBL &&
71 (code & ~MP_KEY_DOWN) <= MOUSE_BTN9_DBL)
72 return;
73 mplayer_put_key_internal(code);
74 if (code & MP_KEY_DOWN) {
75 code &= ~MP_KEY_DOWN;
76 last_key[1] = last_key[0];
77 last_key[0] = code;
78 last_key_time[1] = last_key_time[0];
79 last_key_time[0] = now;
80 if (last_key[1] == code &&
81 now - last_key_time[1] < doubleclick_time)
82 put_double(code);
83 return;
85 if (last_key[0] == code && last_key[1] == code &&
86 now - last_key_time[1] < doubleclick_time)
87 put_double(code);