vo_gl: fix image corruption with PBOs when playing 10 bit video
[mplayer.git] / input / input.c
blob35714820e3551f68fb061ab7aace736d00946fa9
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 "config.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <ctype.h>
32 #include <assert.h>
34 #include "input.h"
35 #include "mp_fifo.h"
36 #include "keycodes.h"
37 #include "osdep/timer.h"
38 #include "libavutil/avstring.h"
39 #include "mp_msg.h"
40 #include "m_config.h"
41 #include "m_option.h"
42 #include "path.h"
43 #include "talloc.h"
44 #include "options.h"
45 #include "bstr.h"
47 #include "joystick.h"
49 #ifdef CONFIG_LIRC
50 #include "lirc.h"
51 #endif
53 #ifdef CONFIG_LIRCC
54 #include <lirc/lircc.h>
55 #endif
57 #include "ar.h"
59 #define MP_MAX_KEY_DOWN 32
61 struct cmd_bind {
62 int input[MP_MAX_KEY_DOWN + 1];
63 char *cmd;
66 struct key_name {
67 int key;
68 char *name;
71 /// This array defines all known commands.
72 /// The first field is an id used to recognize the command without too many strcmp.
73 /// The second is obviously the command name.
74 /// The third is the minimum number of arguments this command needs.
75 /// Then comes the definition of each argument, terminated with an arg of type -1.
76 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
77 /// the last one) which is actually 9.
79 /// For the args, the first field is the type (actually int, float or string), the second
80 /// is the default value wich is used for optional arguments
82 static const mp_cmd_t mp_cmds[] = {
83 #ifdef CONFIG_RADIO
84 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
85 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
87 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 #endif
89 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
90 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
91 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
96 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
97 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
98 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
99 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
102 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
106 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
109 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
112 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
113 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
119 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
120 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
121 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
123 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
124 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
125 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
126 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
131 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
132 #ifdef CONFIG_ASS
133 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
134 #endif
135 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
136 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
137 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
138 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
139 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
141 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
142 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
144 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
145 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
152 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
153 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
155 #ifdef CONFIG_TV
156 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
157 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
158 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
159 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
160 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
161 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
162 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
165 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
166 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 #endif
170 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
171 #ifdef CONFIG_DVBIN
172 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
173 #endif
174 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
175 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
176 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
177 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
178 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
181 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
186 { MP_CMD_CAPTURING, "capturing", 0, { {-1,{0}} } },
187 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
188 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
189 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
191 #ifdef CONFIG_DVDNAV
192 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
193 #endif
195 #ifdef CONFIG_MENU
196 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
197 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
198 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
199 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
200 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
201 #endif
203 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
204 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
205 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
206 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
207 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
209 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
210 { MP_CMD_STEP_PROPERTY_OSD, "step_property_osd", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
212 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
213 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
215 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
216 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
217 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
218 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
219 { MP_CMD_AF_CMDLINE, "af_cmdline", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
221 { 0, NULL, 0, {} }
224 /// The names of the keys as used in input.conf
225 /// If you add some new keys, you also need to add them here
227 static const struct key_name key_names[] = {
228 { ' ', "SPACE" },
229 { '#', "SHARP" },
230 { KEY_ENTER, "ENTER" },
231 { KEY_TAB, "TAB" },
232 { KEY_BACKSPACE, "BS" },
233 { KEY_DELETE, "DEL" },
234 { KEY_INSERT, "INS" },
235 { KEY_HOME, "HOME" },
236 { KEY_END, "END" },
237 { KEY_PAGE_UP, "PGUP" },
238 { KEY_PAGE_DOWN, "PGDWN" },
239 { KEY_ESC, "ESC" },
240 { KEY_RIGHT, "RIGHT" },
241 { KEY_LEFT, "LEFT" },
242 { KEY_DOWN, "DOWN" },
243 { KEY_UP, "UP" },
244 { KEY_F+1, "F1" },
245 { KEY_F+2, "F2" },
246 { KEY_F+3, "F3" },
247 { KEY_F+4, "F4" },
248 { KEY_F+5, "F5" },
249 { KEY_F+6, "F6" },
250 { KEY_F+7, "F7" },
251 { KEY_F+8, "F8" },
252 { KEY_F+9, "F9" },
253 { KEY_F+10, "F10" },
254 { KEY_F+11, "F11" },
255 { KEY_F+12, "F12" },
256 { KEY_KP0, "KP0" },
257 { KEY_KP1, "KP1" },
258 { KEY_KP2, "KP2" },
259 { KEY_KP3, "KP3" },
260 { KEY_KP4, "KP4" },
261 { KEY_KP5, "KP5" },
262 { KEY_KP6, "KP6" },
263 { KEY_KP7, "KP7" },
264 { KEY_KP8, "KP8" },
265 { KEY_KP9, "KP9" },
266 { KEY_KPDEL, "KP_DEL" },
267 { KEY_KPDEC, "KP_DEC" },
268 { KEY_KPINS, "KP_INS" },
269 { KEY_KPENTER, "KP_ENTER" },
270 { MOUSE_BTN0, "MOUSE_BTN0" },
271 { MOUSE_BTN1, "MOUSE_BTN1" },
272 { MOUSE_BTN2, "MOUSE_BTN2" },
273 { MOUSE_BTN3, "MOUSE_BTN3" },
274 { MOUSE_BTN4, "MOUSE_BTN4" },
275 { MOUSE_BTN5, "MOUSE_BTN5" },
276 { MOUSE_BTN6, "MOUSE_BTN6" },
277 { MOUSE_BTN7, "MOUSE_BTN7" },
278 { MOUSE_BTN8, "MOUSE_BTN8" },
279 { MOUSE_BTN9, "MOUSE_BTN9" },
280 { MOUSE_BTN10, "MOUSE_BTN10" },
281 { MOUSE_BTN11, "MOUSE_BTN11" },
282 { MOUSE_BTN12, "MOUSE_BTN12" },
283 { MOUSE_BTN13, "MOUSE_BTN13" },
284 { MOUSE_BTN14, "MOUSE_BTN14" },
285 { MOUSE_BTN15, "MOUSE_BTN15" },
286 { MOUSE_BTN16, "MOUSE_BTN16" },
287 { MOUSE_BTN17, "MOUSE_BTN17" },
288 { MOUSE_BTN18, "MOUSE_BTN18" },
289 { MOUSE_BTN19, "MOUSE_BTN19" },
290 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
291 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
292 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
293 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
294 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
295 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
296 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
297 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
298 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
299 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
300 { MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
301 { MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
302 { MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
303 { MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
304 { MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
305 { MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
306 { MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
307 { MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
308 { MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
309 { MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
310 { JOY_AXIS1_MINUS, "JOY_UP" },
311 { JOY_AXIS1_PLUS, "JOY_DOWN" },
312 { JOY_AXIS0_MINUS, "JOY_LEFT" },
313 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
315 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
316 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
317 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
318 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
319 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
320 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
321 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
322 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
323 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
324 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
325 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
326 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
327 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
328 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
329 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
330 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
331 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
332 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
333 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
334 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
336 { JOY_BTN0, "JOY_BTN0" },
337 { JOY_BTN1, "JOY_BTN1" },
338 { JOY_BTN2, "JOY_BTN2" },
339 { JOY_BTN3, "JOY_BTN3" },
340 { JOY_BTN4, "JOY_BTN4" },
341 { JOY_BTN5, "JOY_BTN5" },
342 { JOY_BTN6, "JOY_BTN6" },
343 { JOY_BTN7, "JOY_BTN7" },
344 { JOY_BTN8, "JOY_BTN8" },
345 { JOY_BTN9, "JOY_BTN9" },
347 { AR_PLAY, "AR_PLAY" },
348 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
349 { AR_NEXT, "AR_NEXT" },
350 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
351 { AR_PREV, "AR_PREV" },
352 { AR_PREV_HOLD, "AR_PREV_HOLD" },
353 { AR_MENU, "AR_MENU" },
354 { AR_MENU_HOLD, "AR_MENU_HOLD" },
355 { AR_VUP, "AR_VUP" },
356 { AR_VDOWN, "AR_VDOWN" },
358 { KEY_POWER, "POWER" },
359 { KEY_MENU, "MENU" },
360 { KEY_PLAY, "PLAY" },
361 { KEY_PAUSE, "PAUSE" },
362 { KEY_PLAYPAUSE, "PLAYPAUSE" },
363 { KEY_STOP, "STOP" },
364 { KEY_FORWARD, "FORWARD" },
365 { KEY_REWIND, "REWIND" },
366 { KEY_NEXT, "NEXT" },
367 { KEY_PREV, "PREV" },
368 { KEY_VOLUME_UP, "VOLUME_UP" },
369 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
370 { KEY_MUTE, "MUTE" },
372 // These are kept for backward compatibility
373 { KEY_PAUSE, "XF86_PAUSE" },
374 { KEY_STOP, "XF86_STOP" },
375 { KEY_PREV, "XF86_PREV" },
376 { KEY_NEXT, "XF86_NEXT" },
378 { KEY_CLOSE_WIN, "CLOSE_WIN" },
380 { 0, NULL }
383 struct key_name modifier_names[] = {
384 { KEY_MODIFIER_SHIFT, "Shift" },
385 { KEY_MODIFIER_CTRL, "Ctrl" },
386 { KEY_MODIFIER_ALT, "Alt" },
387 { KEY_MODIFIER_META, "Meta" },
388 { 0 }
391 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
393 // This is the default binding. The content of input.conf overrides these.
394 // The first arg is a null terminated array of key codes.
395 // The second is the command
397 static const struct cmd_bind def_cmd_binds[] = {
399 { { MOUSE_BTN0_DBL, 0 }, "vo_fullscreen" },
400 { { MOUSE_BTN2, 0 }, "pause" },
401 { { MOUSE_BTN3, 0 }, "seek 10" },
402 { { MOUSE_BTN4, 0 }, "seek -10" },
403 { { MOUSE_BTN5, 0 }, "volume 1" },
404 { { MOUSE_BTN6, 0 }, "volume -1" },
406 #ifdef CONFIG_DVDNAV
407 { { KEY_KP8, 0 }, "dvdnav up" }, // up
408 { { KEY_KP2, 0 }, "dvdnav down" }, // down
409 { { KEY_KP4, 0 }, "dvdnav left" }, // left
410 { { KEY_KP6, 0 }, "dvdnav right" }, // right
411 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
412 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
413 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
414 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
415 #endif
417 { { KEY_RIGHT, 0 }, "seek 10" },
418 { { KEY_LEFT, 0 }, "seek -10" },
419 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
420 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
421 { { KEY_UP, 0 }, "seek 60" },
422 { { KEY_DOWN, 0 }, "seek -60" },
423 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
424 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
425 { { KEY_PAGE_UP, 0 }, "seek 600" },
426 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
427 { { '+', 0 }, "audio_delay 0.100" },
428 { { '-', 0 }, "audio_delay -0.100" },
429 { { '[', 0 }, "speed_mult 0.9091" },
430 { { ']', 0 }, "speed_mult 1.1" },
431 { { '{', 0 }, "speed_mult 0.5" },
432 { { '}', 0 }, "speed_mult 2.0" },
433 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
434 { { 'q', 0 }, "quit" },
435 { { KEY_ESC, 0 }, "quit" },
436 { { 'p', 0 }, "pause" },
437 { { ' ', 0 }, "pause" },
438 { { '.', 0 }, "frame_step" },
439 { { KEY_HOME, 0 }, "pt_up_step 1" },
440 { { KEY_END, 0 }, "pt_up_step -1" },
441 { { '>', 0 }, "pt_step 1" },
442 { { KEY_ENTER, 0 }, "pt_step 1 1" },
443 { { '<', 0 }, "pt_step -1" },
444 { { KEY_INS, 0 }, "alt_src_step 1" },
445 { { KEY_DEL, 0 }, "alt_src_step -1" },
446 { { 'o', 0 }, "osd" },
447 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
448 { { 'P', 0 }, "osd_show_progression" },
449 { { 'z', 0 }, "sub_delay -0.1" },
450 { { 'x', 0 }, "sub_delay +0.1" },
451 { { 'g', 0 }, "sub_step -1" },
452 { { 'y', 0 }, "sub_step +1" },
453 { { '9', 0 }, "volume -1" },
454 { { '/', 0 }, "volume -1" },
455 { { '0', 0 }, "volume 1" },
456 { { '*', 0 }, "volume 1" },
457 { { '(', 0 }, "balance -0.1" },
458 { { ')', 0 }, "balance 0.1" },
459 { { 'm', 0 }, "mute" },
460 { { '1', 0 }, "contrast -1" },
461 { { '2', 0 }, "contrast 1" },
462 { { '3', 0 }, "brightness -1" },
463 { { '4', 0 }, "brightness 1" },
464 { { '5', 0 }, "hue -1" },
465 { { '6', 0 }, "hue 1" },
466 { { '7', 0 }, "saturation -1" },
467 { { '8', 0 }, "saturation 1" },
468 { { 'd', 0 }, "frame_drop" },
469 { { 'D', 0 }, "step_property_osd deinterlace" },
470 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
471 { { 'r', 0 }, "sub_pos -1" },
472 { { 't', 0 }, "sub_pos +1" },
473 { { 'a', 0 }, "sub_alignment" },
474 { { 'v', 0 }, "sub_visibility" },
475 { { 'V', 0 }, "step_property_osd ass_vsfilter_aspect_compat" },
476 { { 'j', 0 }, "sub_select" },
477 { { 'J', 0 }, "sub_select -3" },
478 { { 'F', 0 }, "forced_subs_only" },
479 { { '#', 0 }, "switch_audio" },
480 { { '_', 0 }, "step_property switch_video" },
481 { { KEY_TAB, 0 }, "step_property switch_program" },
482 { { 'i', 0 }, "edl_mark" },
483 #ifdef CONFIG_TV
484 { { 'h', 0 }, "tv_step_channel 1" },
485 { { 'k', 0 }, "tv_step_channel -1" },
486 { { 'n', 0 }, "tv_step_norm" },
487 { { 'u', 0 }, "tv_step_chanlist" },
488 #endif
489 { { 'X', 0 }, "step_property teletext_mode 1" },
490 { { 'W', 0 }, "step_property teletext_page 1" },
491 { { 'Q', 0 }, "step_property teletext_page -1" },
492 #ifdef CONFIG_JOYSTICK
493 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
494 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
495 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
496 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
497 { { JOY_BTN0, 0 }, "pause" },
498 { { JOY_BTN1, 0 }, "osd" },
499 { { JOY_BTN2, 0 }, "volume 1"},
500 { { JOY_BTN3, 0 }, "volume -1"},
501 #endif
502 #ifdef CONFIG_APPLE_REMOTE
503 { { AR_PLAY, 0}, "pause" },
504 { { AR_PLAY_HOLD, 0}, "quit" },
505 { { AR_NEXT, 0 }, "seek 30" },
506 { { AR_NEXT_HOLD, 0 }, "seek 120" },
507 { { AR_PREV, 0 }, "seek -10" },
508 { { AR_PREV_HOLD, 0 }, "seek -120" },
509 { { AR_MENU, 0 }, "osd" },
510 { { AR_MENU_HOLD, 0 }, "mute" },
511 { { AR_VUP, 0 }, "volume 1"},
512 { { AR_VDOWN, 0 }, "volume -1"},
513 #endif
514 { { 'T', 0 }, "vo_ontop" },
515 { { 'f', 0 }, "vo_fullscreen" },
516 { { 'C', 0 }, "step_property_osd capturing" },
517 { { 's', 0 }, "screenshot 0" },
518 { { 'S', 0 }, "screenshot 1" },
519 { { 'w', 0 }, "panscan -0.1" },
520 { { 'e', 0 }, "panscan +0.1" },
522 { { KEY_POWER, 0 }, "quit" },
523 { { KEY_MENU, 0 }, "osd" },
524 { { KEY_PLAY, 0 }, "pause" },
525 { { KEY_PAUSE, 0 }, "pause" },
526 { { KEY_PLAYPAUSE, 0 }, "pause" },
527 { { KEY_STOP, 0 }, "quit" },
528 { { KEY_FORWARD, 0 }, "seek 60" },
529 { { KEY_REWIND, 0 }, "seek -60" },
530 { { KEY_NEXT, 0 }, "pt_step 1" },
531 { { KEY_PREV, 0 }, "pt_step -1" },
532 { { KEY_VOLUME_UP, 0 }, "volume 1" },
533 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
534 { { KEY_MUTE, 0 }, "mute" },
536 { { KEY_CLOSE_WIN, 0 }, "quit" },
538 { { '!', 0 }, "seek_chapter -1" },
539 { { '@', 0 }, "seek_chapter 1" },
540 { { 'A', 0 }, "switch_angle 1" },
541 { { 'U', 0 }, "stop" },
543 { { 0 }, NULL }
547 #ifndef MP_MAX_KEY_FD
548 #define MP_MAX_KEY_FD 10
549 #endif
551 #ifndef MP_MAX_CMD_FD
552 #define MP_MAX_CMD_FD 10
553 #endif
555 struct input_fd {
556 int fd;
557 union {
558 int (*key)(void *ctx, int fd);
559 int (*cmd)(int fd, char *dest, int size);
560 } read_func;
561 int (*close_func)(int fd);
562 void *ctx;
563 unsigned eof : 1;
564 unsigned drop : 1;
565 unsigned dead : 1;
566 unsigned got_cmd : 1;
567 unsigned no_select : 1;
568 // These fields are for the cmd fds.
569 char *buffer;
570 int pos, size;
573 struct cmd_filter {
574 mp_input_cmd_filter filter;
575 void *ctx;
576 struct cmd_filter *next;
579 struct cmd_bind_section {
580 struct cmd_bind *cmd_binds;
581 char *section;
582 struct cmd_bind_section *next;
585 struct cmd_queue {
586 struct mp_cmd *first;
587 struct mp_cmd *last;
588 int num_cmds;
589 int num_abort_cmds;
592 struct input_ctx {
593 // Autorepeat stuff
594 short ar_state;
595 mp_cmd_t *ar_cmd;
596 unsigned int last_ar;
597 // Autorepeat config
598 unsigned int ar_delay;
599 unsigned int ar_rate;
600 // Maximum number of queued commands from keypresses (limit to avoid
601 // repeated slow commands piling up)
602 int key_fifo_size;
604 // these are the keys currently down
605 int key_down[MP_MAX_KEY_DOWN];
606 unsigned int num_key_down;
607 unsigned int last_key_down;
609 bool default_bindings;
610 // List of command binding sections
611 struct cmd_bind_section *cmd_bind_sections;
612 // Name of currently used command section
613 char *section;
614 // The command binds of current section
615 struct cmd_bind *cmd_binds;
616 struct cmd_bind *cmd_binds_default;
618 // Used to track whether we managed to read something while checking
619 // events sources. If yes, the sources may have more queued.
620 bool got_new_events;
622 struct input_fd key_fds[MP_MAX_KEY_FD];
623 unsigned int num_key_fd;
625 struct input_fd cmd_fds[MP_MAX_CMD_FD];
626 unsigned int num_cmd_fd;
628 struct cmd_queue key_cmd_queue;
629 struct cmd_queue control_cmd_queue;
633 static struct cmd_filter *cmd_filters = NULL;
635 // Callback to allow the menu filter to grab the incoming keys
636 int (*mp_input_key_cb)(int code) = NULL;
638 int async_quit_request;
640 static int print_key_list(m_option_t *cfg);
641 static int print_cmd_list(m_option_t *cfg);
643 // Our command line options
644 static const m_option_t input_conf[] = {
645 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
646 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
647 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
648 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
649 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
650 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
651 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
652 OPT_STRING("file", input.in_file, CONF_GLOBAL),
653 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
654 { NULL, NULL, 0, 0, 0, 0, NULL}
657 static const m_option_t mp_input_opts[] = {
658 { "input", (void *)&input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
659 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
660 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
661 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
662 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
663 { NULL, NULL, 0, 0, 0, 0, NULL}
666 static int default_cmd_func(int fd, char *buf, int l);
668 static char *get_key_name(int key, char *ret)
670 for (int i = 0; modifier_names[i].name; i++) {
671 if (modifier_names[i].key & key) {
672 ret = talloc_asprintf_append_buffer(ret, "%s+",
673 modifier_names[i].name);
674 key -= modifier_names[i].key;
677 for (int i = 0; key_names[i].name != NULL; i++) {
678 if (key_names[i].key == key)
679 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
682 if (isascii(key))
683 return talloc_asprintf_append_buffer(ret, "%c", key);
685 // Print the hex key code
686 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
689 static char *get_key_combo_name(int *keys, int max)
691 char *ret = talloc_strdup(NULL, "");
692 while (1) {
693 ret = get_key_name(*keys, ret);
694 if (--max && *++keys)
695 talloc_asprintf_append_buffer(ret, "-");
696 else
697 break;
699 return ret;
702 static bool is_abort_cmd(int cmd_id)
704 switch (cmd_id) {
705 case MP_CMD_QUIT:
706 case MP_CMD_PLAY_TREE_STEP:
707 case MP_CMD_PLAY_TREE_UP_STEP:
708 case MP_CMD_PLAY_ALT_SRC_STEP:
709 return true;
711 return false;
714 static void queue_pop(struct cmd_queue *queue)
716 assert(queue->num_cmds > 0);
717 struct mp_cmd *cmd = queue->first;
718 queue->first = cmd->queue_next;
719 queue->num_cmds--;
720 queue->num_abort_cmds -= is_abort_cmd(cmd->id);
723 static void queue_add(struct cmd_queue *queue, struct mp_cmd *cmd,
724 bool at_head)
726 if (!queue->num_cmds) {
727 queue->first = cmd;
728 queue->last = cmd;
729 } else if (at_head) {
730 queue->first->queue_prev = cmd;
731 cmd->queue_next = queue->first;
732 queue->first = cmd;
733 } else {
734 queue->last->queue_next = cmd;
735 cmd->queue_prev = queue->last;
736 queue->last = cmd;
738 queue->num_cmds++;
739 queue->num_abort_cmds += is_abort_cmd(cmd->id);
742 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
743 int read_func(int fd, char *dest, int size),
744 int close_func(int fd))
746 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
747 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
748 "cannot register file descriptor %d.\n", fd);
749 return 0;
751 if (select && fd < 0) {
752 mp_msg(MSGT_INPUT, MSGL_ERR,
753 "Invalid fd %d in mp_input_add_cmd_fd", fd);
754 return 0;
757 ictx->cmd_fds[ictx->num_cmd_fd] = (struct input_fd){
758 .fd = fd,
759 .read_func.cmd = read_func ? read_func : default_cmd_func,
760 .close_func = close_func,
761 .no_select = !select
763 ictx->num_cmd_fd++;
765 return 1;
768 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
770 struct input_fd *cmd_fds = ictx->cmd_fds;
771 unsigned int i;
773 for (i = 0; i < ictx->num_cmd_fd; i++) {
774 if (cmd_fds[i].fd == fd)
775 break;
777 if (i == ictx->num_cmd_fd)
778 return;
779 if (cmd_fds[i].close_func)
780 cmd_fds[i].close_func(cmd_fds[i].fd);
781 talloc_free(cmd_fds[i].buffer);
783 if (i + 1 < ictx->num_cmd_fd)
784 memmove(&cmd_fds[i], &cmd_fds[i + 1],
785 (ictx->num_cmd_fd - i - 1) * sizeof(struct input_fd));
786 ictx->num_cmd_fd--;
789 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
791 struct input_fd *key_fds = ictx->key_fds;
792 unsigned int i;
794 for (i = 0; i < ictx->num_key_fd; i++) {
795 if (key_fds[i].fd == fd)
796 break;
798 if (i == ictx->num_key_fd)
799 return;
800 if (key_fds[i].close_func)
801 key_fds[i].close_func(key_fds[i].fd);
803 if (i + 1 < ictx->num_key_fd)
804 memmove(&key_fds[i], &key_fds[i + 1],
805 (ictx->num_key_fd - i - 1) * sizeof(struct input_fd));
806 ictx->num_key_fd--;
809 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
810 int read_func(void *ctx, int fd),
811 int close_func(int fd), void *ctx)
813 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
814 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
815 "cannot register file descriptor %d.\n", fd);
816 return 0;
818 if (select && fd < 0) {
819 mp_msg(MSGT_INPUT, MSGL_ERR,
820 "Invalid fd %d in mp_input_add_key_fd", fd);
821 return 0;
824 ictx->key_fds[ictx->num_key_fd] = (struct input_fd){
825 .fd = fd,
826 .read_func.key = read_func,
827 .close_func = close_func,
828 .no_select = !select,
829 .ctx = ctx,
831 ictx->num_key_fd++;
833 return 1;
836 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
838 int cmd_num = 0;
840 while (*str == '\n' || *str == '\r' || *str == ' ')
841 ++str;
842 while (*str) {
843 mp_cmd_t *cmd;
844 size_t len = strcspn(str, "\r\n");
845 char *cmdbuf = talloc_size(NULL, len + 1);
846 av_strlcpy(cmdbuf, str, len + 1);
847 cmd = mp_input_parse_cmd(cmdbuf);
848 if (cmd) {
849 mp_input_queue_cmd(ictx, cmd);
850 ++cmd_num;
852 str += len;
853 while (*str == '\n' || *str == '\r' || *str == ' ')
854 ++str;
855 talloc_free(cmdbuf);
857 return cmd_num;
860 mp_cmd_t *mp_input_parse_cmd(char *str)
862 int i, l;
863 int pausing = 0;
864 char *ptr;
865 const mp_cmd_t *cmd_def;
867 // Ignore heading spaces.
868 while (str[0] == ' ' || str[0] == '\t')
869 ++str;
871 if (strncmp(str, "pausing ", 8) == 0) {
872 pausing = 1;
873 str = &str[8];
874 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
875 pausing = 2;
876 str = &str[13];
877 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
878 pausing = 3;
879 str = &str[15];
880 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
881 pausing = 4;
882 str = &str[19];
885 ptr = str + strcspn(str, "\t ");
886 if (*ptr != 0)
887 l = ptr - str;
888 else
889 l = strlen(str);
891 if (l == 0)
892 return NULL;
894 for (i = 0; mp_cmds[i].name != NULL; i++) {
895 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
896 break;
899 if (mp_cmds[i].name == NULL)
900 return NULL;
902 cmd_def = &mp_cmds[i];
904 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
905 *cmd = (mp_cmd_t){
906 .id = cmd_def->id,
907 .name = talloc_strdup(cmd, cmd_def->name),
908 .pausing = pausing,
911 ptr = str;
913 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
914 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
915 ptr++;
916 if (ptr[0] == '\0')
917 break;
918 while (ptr[0] == ' ' || ptr[0] == '\t')
919 ptr++;
920 if (ptr[0] == '\0' || ptr[0] == '#')
921 break;
922 cmd->args[i].type = cmd_def->args[i].type;
923 switch (cmd_def->args[i].type) {
924 case MP_CMD_ARG_INT:
925 errno = 0;
926 cmd->args[i].v.i = atoi(ptr);
927 if (errno != 0) {
928 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
929 "isn't an integer.\n", cmd_def->name, i + 1);
930 goto error;
932 break;
933 case MP_CMD_ARG_FLOAT:
934 errno = 0;
935 cmd->args[i].v.f = atof(ptr);
936 if (errno != 0) {
937 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
938 "isn't a float.\n", cmd_def->name, i + 1);
939 goto error;
941 break;
942 case MP_CMD_ARG_STRING: {
943 int term = ' ';
944 if (*ptr == '\'' || *ptr == '"')
945 term = *ptr++;
946 char *argptr = talloc_size(cmd, strlen(ptr) + 1);
947 cmd->args[i].v.s = argptr;
948 while (1) {
949 if (*ptr == 0) {
950 if (term == ' ')
951 break;
952 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
953 "unterminated.\n", cmd_def->name, i + 1);
954 goto error;
956 if (*ptr == term)
957 break;
958 if (*ptr == '\\')
959 ptr++;
960 if (*ptr != 0)
961 *argptr++ = *ptr++;
963 *argptr = 0;
964 break;
966 case -1:
967 ptr = NULL;
968 break;
969 default:
970 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
973 cmd->nargs = i;
975 if (cmd_def->nargs > cmd->nargs) {
976 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d "
977 "arguments, we found only %d so far.\n", cmd_def->name,
978 cmd_def->nargs, cmd->nargs);
979 goto error;
982 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1; i++) {
983 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(struct mp_cmd_arg));
984 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
985 && cmd_def->args[i].v.s != NULL)
986 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
989 if (i < MP_CMD_MAX_ARGS)
990 cmd->args[i].type = -1;
992 return cmd;
994 error:
995 mp_cmd_free(cmd);
996 return NULL;
999 #define MP_CMD_MAX_SIZE 4096
1001 static int read_cmd(struct input_fd *mp_fd, char **ret)
1003 char *end;
1004 *ret = NULL;
1006 // Allocate the buffer if it doesn't exist
1007 if (!mp_fd->buffer) {
1008 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
1009 mp_fd->pos = 0;
1010 mp_fd->size = MP_CMD_MAX_SIZE;
1013 // Get some data if needed/possible
1014 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
1015 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
1016 mp_fd->size - 1 - mp_fd->pos);
1017 // Error ?
1018 if (r < 0) {
1019 switch (r) {
1020 case MP_INPUT_ERROR:
1021 case MP_INPUT_DEAD:
1022 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1023 "command file descriptor %d: %s\n",
1024 mp_fd->fd, strerror(errno));
1025 case MP_INPUT_NOTHING:
1026 return r;
1027 case MP_INPUT_RETRY:
1028 continue;
1030 // EOF ?
1031 } else if (r == 0) {
1032 mp_fd->eof = 1;
1033 break;
1035 mp_fd->pos += r;
1036 break;
1039 mp_fd->got_cmd = 0;
1041 while (1) {
1042 int l = 0;
1043 // Find the cmd end
1044 mp_fd->buffer[mp_fd->pos] = '\0';
1045 end = strchr(mp_fd->buffer, '\r');
1046 if (end)
1047 *end = '\n';
1048 end = strchr(mp_fd->buffer, '\n');
1049 // No cmd end ?
1050 if (!end) {
1051 // If buffer is full we must drop all until the next \n
1052 if (mp_fd->size - mp_fd->pos <= 1) {
1053 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
1054 "descriptor %d is full: dropping content.\n",
1055 mp_fd->fd);
1056 mp_fd->pos = 0;
1057 mp_fd->drop = 1;
1059 break;
1061 // We already have a cmd : set the got_cmd flag
1062 else if ((*ret)) {
1063 mp_fd->got_cmd = 1;
1064 break;
1067 l = end - mp_fd->buffer;
1069 // Not dropping : put the cmd in ret
1070 if (!mp_fd->drop)
1071 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1072 else
1073 mp_fd->drop = 0;
1074 mp_fd->pos -= l + 1;
1075 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1078 if (*ret)
1079 return 1;
1080 else
1081 return MP_INPUT_NOTHING;
1084 static int default_cmd_func(int fd, char *buf, int l)
1086 while (1) {
1087 int r = read(fd, buf, l);
1088 // Error ?
1089 if (r < 0) {
1090 if (errno == EINTR)
1091 continue;
1092 else if (errno == EAGAIN)
1093 return MP_INPUT_NOTHING;
1094 return MP_INPUT_ERROR;
1095 // EOF ?
1097 return r;
1102 void mp_input_add_cmd_filter(mp_input_cmd_filter func, void *ctx)
1104 struct cmd_filter *filter = talloc_ptrtype(NULL, filter);
1106 filter->filter = func;
1107 filter->ctx = ctx;
1108 filter->next = cmd_filters;
1109 cmd_filters = filter;
1113 static char *find_bind_for_key(const struct cmd_bind *binds, int n, int *keys)
1115 int j;
1117 if (n <= 0)
1118 return NULL;
1119 for (j = 0; binds[j].cmd != NULL; j++) {
1120 int found = 1, s;
1121 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1122 if (binds[j].input[s] != keys[s]) {
1123 found = 0;
1124 break;
1127 if (found && binds[j].input[s] == 0 && s == n)
1128 break;
1130 return binds[j].cmd;
1133 static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
1134 char *section)
1136 struct cmd_bind_section *bind_section = ictx->cmd_bind_sections;
1138 if (section == NULL)
1139 section = "default";
1140 while (bind_section) {
1141 if (strcmp(section, bind_section->section) == 0)
1142 return bind_section;
1143 if (bind_section->next == NULL)
1144 break;
1145 bind_section = bind_section->next;
1147 if (bind_section) {
1148 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1149 bind_section = bind_section->next;
1150 } else {
1151 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1152 bind_section = ictx->cmd_bind_sections;
1154 bind_section->cmd_binds = NULL;
1155 bind_section->section = talloc_strdup(bind_section, section);
1156 bind_section->next = NULL;
1157 return bind_section;
1160 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1162 char *cmd = NULL;
1163 mp_cmd_t *ret;
1165 if (ictx->cmd_binds)
1166 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1167 if (ictx->cmd_binds_default && cmd == NULL)
1168 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1169 if (ictx->default_bindings && cmd == NULL)
1170 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1172 if (cmd == NULL) {
1173 char *key_buf = get_key_combo_name(keys, n);
1174 mp_tmsg(MSGT_INPUT, MSGL_WARN,
1175 "No bind found for key '%s'.\n", key_buf);
1176 talloc_free(key_buf);
1177 return NULL;
1179 if (strcmp(cmd, "ignore") == 0)
1180 return NULL;
1181 ret = mp_input_parse_cmd(cmd);
1182 if (!ret) {
1183 char *key_buf = get_key_combo_name(keys, n);
1184 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1185 "Invalid command for bound key '%s': '%s'\n", key_buf, cmd);
1186 talloc_free(key_buf);
1188 return ret;
1192 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1194 unsigned int j;
1195 mp_cmd_t *ret;
1197 /* On normal keyboards shift changes the character code of non-special
1198 * keys, so don't count the modifier separately for those. In other words
1199 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1200 * shift modifier is still kept for special keys like arrow keys.
1202 int unmod = code & ~KEY_MODIFIER_MASK;
1203 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1204 code &= ~KEY_MODIFIER_SHIFT;
1206 if (mp_input_key_cb) {
1207 if (code & MP_KEY_DOWN)
1208 return NULL;
1209 code &= ~(MP_KEY_DOWN | MP_NO_REPEAT_KEY);
1210 if (mp_input_key_cb(code))
1211 return NULL;
1214 if (code & MP_KEY_DOWN) {
1215 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1216 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1217 "at the same time\n");
1218 return NULL;
1220 code &= ~MP_KEY_DOWN;
1221 // Check if we don't already have this key as pushed
1222 for (j = 0; j < ictx->num_key_down; j++) {
1223 if (ictx->key_down[j] == code)
1224 break;
1226 if (j != ictx->num_key_down)
1227 return NULL;
1228 ictx->key_down[ictx->num_key_down] = code;
1229 ictx->num_key_down++;
1230 ictx->last_key_down = GetTimer();
1231 ictx->ar_state = 0;
1232 return NULL;
1234 // button released or press of key with no separate down/up events
1235 for (j = 0; j < ictx->num_key_down; j++) {
1236 if (ictx->key_down[j] == code)
1237 break;
1239 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1240 if (doubleclick) {
1241 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1242 if (!ictx->num_key_down
1243 || ictx->key_down[ictx->num_key_down - 1] != btn)
1244 return NULL;
1245 j = ictx->num_key_down - 1;
1246 ictx->key_down[j] = code;
1248 if (j == ictx->num_key_down) { // was not already down; add temporarily
1249 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1250 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1251 "at the same time\n");
1252 return NULL;
1254 ictx->key_down[ictx->num_key_down] = code;
1255 ictx->num_key_down++;
1256 ictx->last_key_down = 1;
1258 // Interpret only maximal point of multibutton event
1259 ret = ictx->last_key_down ?
1260 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1261 : NULL;
1262 if (doubleclick) {
1263 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1264 return ret;
1266 // Remove the key
1267 if (j + 1 < ictx->num_key_down)
1268 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1269 (ictx->num_key_down - (j + 1)) * sizeof(int));
1270 ictx->num_key_down--;
1271 ictx->last_key_down = 0;
1272 ictx->ar_state = -1;
1273 mp_cmd_free(ictx->ar_cmd);
1274 ictx->ar_cmd = NULL;
1275 return ret;
1278 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1280 // No input : autorepeat ?
1281 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1282 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1283 unsigned int t = GetTimer();
1284 // First time : wait delay
1285 if (ictx->ar_state == 0
1286 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1287 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1288 ictx->key_down);
1289 if (!ictx->ar_cmd) {
1290 ictx->ar_state = -1;
1291 return NULL;
1293 ictx->ar_state = 1;
1294 ictx->last_ar = t;
1295 return mp_cmd_clone(ictx->ar_cmd);
1296 // Then send rate / sec event
1297 } else if (ictx->ar_state == 1
1298 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1299 ictx->last_ar = t;
1300 return mp_cmd_clone(ictx->ar_cmd);
1303 return NULL;
1306 void mp_input_feed_key(struct input_ctx *ictx, int code)
1308 ictx->got_new_events = true;
1309 if (code == MP_INPUT_RELEASE_ALL) {
1310 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1311 ictx->num_key_down = 0;
1312 ictx->last_key_down = 0;
1313 return;
1315 struct mp_cmd *cmd = interpret_key(ictx, code);
1316 if (!cmd)
1317 return;
1318 struct cmd_queue *queue = &ictx->key_cmd_queue;
1319 if (queue->num_cmds >= ictx->key_fifo_size &&
1320 (!is_abort_cmd(cmd->id) || queue->num_abort_cmds))
1321 return;
1322 queue_add(queue, cmd, false);
1325 static void read_cmd_fd(struct input_ctx *ictx, struct input_fd *cmd_fd)
1327 int r;
1328 char *text;
1329 while ((r = read_cmd(cmd_fd, &text)) >= 0) {
1330 ictx->got_new_events = true;
1331 struct mp_cmd *cmd = mp_input_parse_cmd(text);
1332 talloc_free(text);
1333 if (cmd)
1334 queue_add(&ictx->control_cmd_queue, cmd, false);
1335 if (!cmd_fd->got_cmd)
1336 return;
1338 if (r == MP_INPUT_ERROR)
1339 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1340 cmd_fd->fd);
1341 else if (r == MP_INPUT_DEAD)
1342 cmd_fd->dead = true;
1345 static void read_key_fd(struct input_ctx *ictx, struct input_fd *key_fd)
1347 int code = key_fd->read_func.key(key_fd->ctx, key_fd->fd);
1348 if (code >= 0 || code == MP_INPUT_RELEASE_ALL) {
1349 mp_input_feed_key(ictx, code);
1350 return;
1353 if (code == MP_INPUT_ERROR)
1354 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1355 "Error on key input file descriptor %d\n", key_fd->fd);
1356 else if (code == MP_INPUT_DEAD) {
1357 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1358 "Dead key input on file descriptor %d\n", key_fd->fd);
1359 key_fd->dead = true;
1364 * \param time time to wait at most for an event in milliseconds
1366 static void read_events(struct input_ctx *ictx, int time)
1368 ictx->got_new_events = false;
1369 struct input_fd *key_fds = ictx->key_fds;
1370 struct input_fd *cmd_fds = ictx->cmd_fds;
1371 for (int i = 0; i < ictx->num_key_fd; i++)
1372 if (key_fds[i].dead) {
1373 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1374 i--;
1375 } else if (time && key_fds[i].no_select)
1376 read_key_fd(ictx, &key_fds[i]);
1377 for (int i = 0; i < ictx->num_cmd_fd; i++)
1378 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1379 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1380 i--;
1381 } else if (time && cmd_fds[i].no_select)
1382 read_cmd_fd(ictx, &cmd_fds[i]);
1383 if (ictx->got_new_events)
1384 time = 0;
1385 #ifdef HAVE_POSIX_SELECT
1386 fd_set fds;
1387 FD_ZERO(&fds);
1388 int max_fd = 0;
1389 for (int i = 0; i < ictx->num_key_fd; i++) {
1390 if (key_fds[i].no_select)
1391 continue;
1392 if (key_fds[i].fd > max_fd)
1393 max_fd = key_fds[i].fd;
1394 FD_SET(key_fds[i].fd, &fds);
1396 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1397 if (cmd_fds[i].no_select)
1398 continue;
1399 if (cmd_fds[i].fd > max_fd)
1400 max_fd = cmd_fds[i].fd;
1401 FD_SET(cmd_fds[i].fd, &fds);
1403 struct timeval tv, *time_val;
1404 if (time >= 0) {
1405 tv.tv_sec = time / 1000;
1406 tv.tv_usec = (time % 1000) * 1000;
1407 time_val = &tv;
1408 } else
1409 time_val = NULL;
1410 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1411 if (errno != EINTR)
1412 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1413 strerror(errno));
1414 FD_ZERO(&fds);
1416 #else
1417 if (time)
1418 usec_sleep(time * 1000);
1419 #endif
1422 for (int i = 0; i < ictx->num_key_fd; i++) {
1423 #ifdef HAVE_POSIX_SELECT
1424 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1425 continue;
1426 #endif
1427 read_key_fd(ictx, &key_fds[i]);
1430 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1431 #ifdef HAVE_POSIX_SELECT
1432 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds))
1433 continue;
1434 #endif
1435 read_cmd_fd(ictx, &cmd_fds[i]);
1439 /* To support blocking file descriptors we don't loop the read over
1440 * every source until it's known to be empty. Instead we use this wrapper
1441 * to run select() again.
1443 static void read_all_events(struct input_ctx *ictx, int time)
1445 while (1) {
1446 read_events(ictx, time);
1447 if (!ictx->got_new_events)
1448 return;
1449 time = 0;
1453 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1455 ictx->got_new_events = true;
1456 if (!cmd)
1457 return 0;
1458 queue_add(&ictx->control_cmd_queue, cmd, true);
1459 return 1;
1463 * \param peek_only when set, the returned command stays in the queue.
1464 * Do not free the returned cmd whe you set this!
1466 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1468 if (async_quit_request)
1469 return mp_input_parse_cmd("quit 1");
1471 if (ictx->control_cmd_queue.num_cmds || ictx->key_cmd_queue.num_cmds)
1472 time = 0;
1473 read_all_events(ictx, time);
1474 struct mp_cmd *ret;
1475 struct cmd_queue *queue = &ictx->control_cmd_queue;
1476 if (!queue->num_cmds)
1477 queue = &ictx->key_cmd_queue;
1478 if (!queue->num_cmds) {
1479 ret = check_autorepeat(ictx);
1480 if (!ret)
1481 return NULL;
1482 queue_add(queue, ret, false);
1483 } else
1484 ret = queue->first;
1486 for (struct cmd_filter *cf = cmd_filters; cf; cf = cf->next) {
1487 if (cf->filter(ret, cf->ctx)) {
1488 // The filter ate the cmd, so remove it from the queue
1489 queue_pop(queue);
1490 mp_cmd_free(ret);
1491 // Retry with next command
1492 return mp_input_get_cmd(ictx, 0, peek_only);
1496 if (!peek_only)
1497 queue_pop(queue);
1499 return ret;
1502 void mp_cmd_free(mp_cmd_t *cmd)
1504 talloc_free(cmd);
1507 mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
1509 mp_cmd_t *ret;
1510 int i;
1512 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1513 ret->name = talloc_strdup(ret, cmd->name);
1514 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1515 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1516 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1519 return ret;
1522 int mp_input_get_key_from_name(const char *name)
1524 int modifiers = 0;
1525 const char *p;
1526 while ((p = strchr(name, '+'))) {
1527 for (struct key_name *m = modifier_names; m->name; m++)
1528 if (!bstrcasecmp(bstr(m->name),
1529 (struct bstr){(char *)name, p - name})) {
1530 modifiers |= m->key;
1531 goto found;
1533 if (!strcmp(name, "+"))
1534 return '+' + modifiers;
1535 return -1;
1536 found:
1537 name = p + 1;
1539 int len = strlen(name);
1540 if (len == 1) // Direct key code
1541 return (unsigned char)name[0] + modifiers;
1542 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1543 return strtol(name, NULL, 16) + modifiers;
1545 for (int i = 0; key_names[i].name != NULL; i++) {
1546 if (strcasecmp(key_names[i].name, name) == 0)
1547 return key_names[i].key + modifiers;
1550 return -1;
1553 static int get_input_from_name(char *name, int *keys)
1555 char *end, *ptr;
1556 int n = 0;
1558 ptr = name;
1559 n = 0;
1560 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1561 if (end && end[1] != '\0') {
1562 if (end[1] == '-')
1563 end = &end[1];
1564 end[0] = '\0';
1566 keys[n] = mp_input_get_key_from_name(ptr);
1567 if (keys[n] < 0)
1568 return 0;
1569 n++;
1570 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1571 ptr = &end[1];
1572 else
1573 break;
1575 keys[n] = 0;
1576 return 1;
1579 #define SPACE_CHAR " \n\r\t"
1581 static void bind_keys(struct input_ctx *ictx,
1582 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1584 int i = 0, j;
1585 struct cmd_bind *bind = NULL;
1586 struct cmd_bind_section *bind_section = NULL;
1587 char *section = NULL, *p;
1589 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1590 *p = 0;
1591 section = ++cmd;
1592 cmd = ++p;
1593 // Jump beginning space
1594 cmd += strspn(cmd, SPACE_CHAR);
1596 bind_section = get_bind_section(ictx, section);
1598 if (bind_section->cmd_binds) {
1599 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1600 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1601 /* NOTHING */;
1602 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1603 bind = &bind_section->cmd_binds[i];
1604 break;
1609 if (!bind) {
1610 bind_section->cmd_binds = talloc_realloc(bind_section,
1611 bind_section->cmd_binds,
1612 struct cmd_bind, i + 2);
1613 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(struct cmd_bind));
1614 bind = &bind_section->cmd_binds[i];
1616 talloc_free(bind->cmd);
1617 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1618 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1621 static int parse_config(struct input_ctx *ictx, char *file)
1623 int fd = open(file, O_RDONLY);
1625 if (fd < 0) {
1626 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1627 file, strerror(errno));
1628 return 0;
1631 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1634 unsigned char buffer[512];
1635 struct bstr buf = { buffer, 0 };
1636 bool eof = false, comments = false;
1637 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1];
1639 while (1) {
1640 if (buf.start != buffer) {
1641 memmove(buffer, buf.start, buf.len);
1642 buf.start = buffer;
1644 if (!eof && buf.len < sizeof(buffer)) {
1645 int r = read(fd, buffer + buf.len, sizeof(buffer) - buf.len);
1646 if (r < 0) {
1647 if (errno == EINTR)
1648 continue;
1649 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1650 "input config file %s: %s\n", file, strerror(errno));
1651 close(fd);
1652 return 0;
1653 } else if (r == 0) {
1654 eof = true;
1655 } else {
1656 buf.len += r;
1657 continue;
1660 if (buf.len == 0) {
1661 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1662 "%d binds\n", file, n_binds);
1663 close(fd);
1664 return 1;
1666 if (comments) {
1667 int idx = bstrchr(buf, '\n');
1668 if (idx >= 0) {
1669 buf = bstr_cut(buf, idx + 1);
1670 comments = false;
1671 } else
1672 buf = bstr_cut(buf, buf.len);
1673 continue;
1675 buf = bstr_lstrip(buf);
1676 if (buf.start != buffer)
1677 continue;
1678 if (buf.start[0] == '#') {
1679 comments = true;
1680 continue;
1682 int eol = bstrchr(buf, '\n');
1683 if (eol < 0) {
1684 if (eof) {
1685 eol = buf.len;
1686 } else {
1687 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1688 "Key binding is too long: %.*s\n", BSTR_P(buf));
1689 comments = true;
1690 continue;
1693 struct bstr line = bstr_splice(buf, 0, eol);
1694 buf = bstr_cut(buf, eol);
1695 struct bstr command;
1696 // Find the key name starting a line
1697 struct bstr keyname = bstr_split(line, SPACE_CHAR, &command);
1698 command = bstr_strip(command);
1699 if (command.len == 0) {
1700 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1701 "Unfinished key binding: %.*s\n", BSTR_P(line));
1702 continue;
1704 char *name = bstrdup0(NULL, keyname);
1705 if (!get_input_from_name(name, keys)) {
1706 talloc_free(name);
1707 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1708 "Unknown key '%.*s'\n", BSTR_P(keyname));
1709 continue;
1711 talloc_free(name);
1712 char *cmd = bstrdup0(NULL, command);
1713 bind_keys(ictx, keys, cmd);
1714 n_binds++;
1715 talloc_free(cmd);
1719 void mp_input_set_section(struct input_ctx *ictx, char *name)
1721 struct cmd_bind_section *bind_section = NULL;
1723 ictx->cmd_binds = NULL;
1724 ictx->cmd_binds_default = NULL;
1725 talloc_free(ictx->section);
1726 if (name)
1727 ictx->section = talloc_strdup(ictx, name);
1728 else
1729 ictx->section = talloc_strdup(ictx, "default");
1730 if ((bind_section = get_bind_section(ictx, ictx->section)))
1731 ictx->cmd_binds = bind_section->cmd_binds;
1732 if (strcmp(ictx->section, "default") == 0)
1733 return;
1734 if ((bind_section = get_bind_section(ictx, NULL)))
1735 ictx->cmd_binds_default = bind_section->cmd_binds;
1738 char *mp_input_get_section(struct input_ctx *ictx)
1740 return ictx->section;
1743 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1745 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1746 *ictx = (struct input_ctx){
1747 .key_fifo_size = input_conf->key_fifo_size,
1748 .ar_state = -1,
1749 .ar_delay = input_conf->ar_delay,
1750 .ar_rate = input_conf->ar_rate,
1751 .default_bindings = input_conf->default_bindings,
1754 char *file;
1755 char *config_file = input_conf->config_file;
1756 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1757 if (!file)
1758 return ictx;
1760 if (!parse_config(ictx, file)) {
1761 // free file if it was allocated by get_path(),
1762 // before it gets overwritten
1763 if (file != config_file)
1764 free(file);
1765 // Try global conf dir
1766 file = MPLAYER_CONFDIR "/input.conf";
1767 if (!parse_config(ictx, file))
1768 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1769 "input config\n");
1770 } else {
1771 // free file if it was allocated by get_path()
1772 if (file != config_file)
1773 free(file);
1776 #ifdef CONFIG_JOYSTICK
1777 if (input_conf->use_joystick) {
1778 int fd = mp_input_joystick_init(input_conf->js_dev);
1779 if (fd < 0)
1780 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1781 else
1782 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1783 close, NULL);
1785 #endif
1787 #ifdef CONFIG_LIRC
1788 if (input_conf->use_lirc) {
1789 int fd = mp_input_lirc_init();
1790 if (fd > 0)
1791 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1792 mp_input_lirc_close);
1794 #endif
1796 #ifdef CONFIG_LIRCC
1797 if (input_conf->use_lircc) {
1798 int fd = lircc_init("mplayer", NULL);
1799 if (fd >= 0)
1800 mp_input_add_cmd_fd(ictx, fd, 1, NULL, lircc_cleanup);
1802 #endif
1804 #ifdef CONFIG_APPLE_REMOTE
1805 if (input_conf->use_ar) {
1806 if (mp_input_ar_init() < 0)
1807 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1808 else
1809 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1810 mp_input_ar_close, NULL);
1812 #endif
1814 #ifdef CONFIG_APPLE_IR
1815 if (input_conf->use_ar) {
1816 int fd = mp_input_appleir_init(input_conf->ar_dev);
1817 if (fd < 0)
1818 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1819 else
1820 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1821 close, NULL);
1823 #endif
1825 if (input_conf->in_file) {
1826 struct stat st;
1827 int mode = O_RDONLY | O_NONBLOCK;
1828 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1829 // Note that on Windows stat may fail for named pipes,
1830 // but due to how the API works, using RDONLY should be ok.
1831 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1832 mode = O_RDWR | O_NONBLOCK;
1833 int in_file_fd = open(input_conf->in_file, mode);
1834 if (in_file_fd >= 0)
1835 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, close);
1836 else
1837 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1838 input_conf->in_file, strerror(errno));
1840 return ictx;
1843 void mp_input_uninit(struct input_ctx *ictx)
1845 if (!ictx)
1846 return;
1848 unsigned int i;
1850 for (i = 0; i < ictx->num_key_fd; i++) {
1851 if (ictx->key_fds[i].close_func)
1852 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1855 for (i = 0; i < ictx->num_cmd_fd; i++) {
1856 if (ictx->cmd_fds[i].close_func)
1857 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1859 talloc_free(ictx);
1862 void mp_input_register_options(m_config_t *cfg)
1864 m_config_register_options(cfg, mp_input_opts);
1867 static int print_key_list(m_option_t *cfg)
1869 int i;
1870 printf("\n");
1871 for (i = 0; key_names[i].name != NULL; i++)
1872 printf("%s\n", key_names[i].name);
1873 exit(0);
1876 static int print_cmd_list(m_option_t *cfg)
1878 const mp_cmd_t *cmd;
1879 int i, j;
1880 const char *type;
1882 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1883 printf("%-20.20s", cmd->name);
1884 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1; j++) {
1885 switch (cmd->args[j].type) {
1886 case MP_CMD_ARG_INT:
1887 type = "Integer";
1888 break;
1889 case MP_CMD_ARG_FLOAT:
1890 type = "Float";
1891 break;
1892 case MP_CMD_ARG_STRING:
1893 type = "String";
1894 break;
1895 default:
1896 type = "??";
1898 if (j + 1 > cmd->nargs)
1899 printf(" [%s]", type);
1900 else
1901 printf(" %s", type);
1903 printf("\n");
1905 exit(0);
1909 * \param time time to wait for an interruption in milliseconds
1911 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1913 for (int i = 0; ; i++) {
1914 if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds ||
1915 ictx->control_cmd_queue.num_abort_cmds) {
1916 mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to "
1917 "another file. Aborting current processing.\n");
1918 return true;
1920 if (i)
1921 return false;
1922 read_all_events(ictx, time);