annotate mp_fifo.c @ 33119:8835e684a41d

Move common pixel shifting statement Since both branches of the condition perform it, do it right before. Besides, use more compact notation.
author ib
date Mon, 04 Apr 2011 14:06:08 +0000
parents 03ed899a72bc
children 1e637ada9003
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
30429
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
1 /*
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
2 * This file is part of MPlayer.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
3 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
4 * MPlayer is free software; you can redistribute it and/or modify
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
5 * it under the terms of the GNU General Public License as published by
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
6 * the Free Software Foundation; either version 2 of the License, or
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
7 * (at your option) any later version.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
8 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
9 * MPlayer is distributed in the hope that it will be useful,
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
12 * GNU General Public License for more details.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
13 *
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
14 * You should have received a copy of the GNU General Public License along
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
17 */
c1a3f1bbba26 Add license header to all top-level files missing them.
diego
parents: 22823
diff changeset
18
22823
98eaf29b5dee Code cleanup: don't include a .c file in mplayer.c and fix a few
rathann
parents: 22313
diff changeset
19 #include <stdlib.h>
98eaf29b5dee Code cleanup: don't include a .c file in mplayer.c and fix a few
rathann
parents: 22313
diff changeset
20 #include "osdep/timer.h"
98eaf29b5dee Code cleanup: don't include a .c file in mplayer.c and fix a few
rathann
parents: 22313
diff changeset
21 #include "input/input.h"
21941
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
22 #include "input/mouse.h"
30554
321e9ea69b9f #include corresponding .h files in .c files.
diego
parents: 30429
diff changeset
23 #include "mp_fifo.h"
9831
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
24
22131
934010b90043 Reserve half of fifo for key release events to help avoiding stop buttons
reimar
parents: 21964
diff changeset
25 int key_fifo_size = 7;
33015
56f979f98698 Remove pointless initializers.
reimar
parents: 33014
diff changeset
26 static int *key_fifo_data;
56f979f98698 Remove pointless initializers.
reimar
parents: 33014
diff changeset
27 static unsigned key_fifo_read;
56f979f98698 Remove pointless initializers.
reimar
parents: 33014
diff changeset
28 static unsigned key_fifo_write;
33019
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
29 static int previous_down_key;
9831
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
30
21941
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
31 static void mplayer_put_key_internal(int code){
33014
55a5a5e334dc Change fifo to not waste one entry needlessly.
reimar
parents: 30554
diff changeset
32 int fifo_free = key_fifo_read + key_fifo_size - key_fifo_write;
14077
3d3f3cc8494a use a configurable-size ringbuffer instead of a pipe for buffering key events.
reimar
parents: 13872
diff changeset
33 if (key_fifo_data == NULL)
3d3f3cc8494a use a configurable-size ringbuffer instead of a pipe for buffering key events.
reimar
parents: 13872
diff changeset
34 key_fifo_data = malloc(key_fifo_size * sizeof(int));
22131
934010b90043 Reserve half of fifo for key release events to help avoiding stop buttons
reimar
parents: 21964
diff changeset
35 if(!fifo_free) return; // FIFO FULL!!
934010b90043 Reserve half of fifo for key release events to help avoiding stop buttons
reimar
parents: 21964
diff changeset
36 // reserve some space for key release events to avoid stuck keys
33018
d88b8a9e989e Change condition to avoid needless key state resets with very small
reimar
parents: 33017
diff changeset
37 // Make sure we do not reset key state because of a down event
d88b8a9e989e Change condition to avoid needless key state resets with very small
reimar
parents: 33017
diff changeset
38 if((code & MP_KEY_DOWN) && fifo_free <= (key_fifo_size >> 1))
22131
934010b90043 Reserve half of fifo for key release events to help avoiding stop buttons
reimar
parents: 21964
diff changeset
39 return;
33017
cc8cef372901 Make "stuck keys" problem impossibly by resetting the internal
reimar
parents: 33016
diff changeset
40 // in the worst case, just reset key state
33019
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
41 if (fifo_free == 1) {
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
42 // HACK: this ensures that a fifo size of 2 does
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
43 // not queue any key presses while still allowing
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
44 // the mouse wheel to work (which sends down and up
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
45 // at nearly the same time
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
46 if (code != previous_down_key)
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
47 code = 0;
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
48 code |= MP_KEY_RELEASE_ALL;
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
49 }
33014
55a5a5e334dc Change fifo to not waste one entry needlessly.
reimar
parents: 30554
diff changeset
50 key_fifo_data[key_fifo_write % key_fifo_size]=code;
55a5a5e334dc Change fifo to not waste one entry needlessly.
reimar
parents: 30554
diff changeset
51 key_fifo_write++;
33019
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
52 if (code & MP_KEY_DOWN)
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
53 previous_down_key = code & ~MP_KEY_DOWN;
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
54 else
03ed899a72bc Allow mouse wheel to work with a key fifo size of 2.
reimar
parents: 33018
diff changeset
55 previous_down_key = 0;
9831
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
56 }
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
57
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
58 int mplayer_get_key(int fd){
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
59 int key;
14077
3d3f3cc8494a use a configurable-size ringbuffer instead of a pipe for buffering key events.
reimar
parents: 13872
diff changeset
60 if (key_fifo_data == NULL)
3d3f3cc8494a use a configurable-size ringbuffer instead of a pipe for buffering key events.
reimar
parents: 13872
diff changeset
61 return MP_INPUT_NOTHING;
9831
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
62 if(key_fifo_write==key_fifo_read) return MP_INPUT_NOTHING;
33014
55a5a5e334dc Change fifo to not waste one entry needlessly.
reimar
parents: 30554
diff changeset
63 key=key_fifo_data[key_fifo_read % key_fifo_size];
55a5a5e334dc Change fifo to not waste one entry needlessly.
reimar
parents: 30554
diff changeset
64 key_fifo_read++;
9831
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
65 return key;
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
66 }
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
67
0397b461f0fb MINGW32 port and select()less fifocode by Arpi
faust3
parents: 7483
diff changeset
68
22823
98eaf29b5dee Code cleanup: don't include a .c file in mplayer.c and fix a few
rathann
parents: 22313
diff changeset
69 unsigned doubleclick_time = 300;
21941
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
70
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
71 static void put_double(int code) {
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
72 if (code >= MOUSE_BTN0 && code <= MOUSE_BTN9)
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
73 mplayer_put_key_internal(code - MOUSE_BTN0 + MOUSE_BTN0_DBL);
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
74 }
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
75
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
76 void mplayer_put_key(int code) {
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
77 static unsigned last_key_time[2];
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
78 static int last_key[2];
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
79 unsigned now = GetTimerMS();
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
80 // ignore system-doubleclick if we generate these events ourselves
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
81 if (doubleclick_time &&
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
82 (code & ~MP_KEY_DOWN) >= MOUSE_BTN0_DBL &&
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
83 (code & ~MP_KEY_DOWN) <= MOUSE_BTN9_DBL)
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
84 return;
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
85 mplayer_put_key_internal(code);
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
86 if (code & MP_KEY_DOWN) {
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
87 code &= ~MP_KEY_DOWN;
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
88 last_key[1] = last_key[0];
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
89 last_key[0] = code;
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
90 last_key_time[1] = last_key_time[0];
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
91 last_key_time[0] = now;
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
92 if (last_key[1] == code &&
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
93 now - last_key_time[1] < doubleclick_time)
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
94 put_double(code);
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
95 return;
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
96 }
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
97 if (last_key[0] == code && last_key[1] == code &&
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
98 now - last_key_time[1] < doubleclick_time)
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
99 put_double(code);
32c3d5e3a682 Apply ancient double-click patch that nobody cares to comment on.
reimar
parents: 14077
diff changeset
100 }