audio: add af_lavrresample, remove old resampling filters
[mplayer.git] / input / input.c
blobed4e933bf48b283b29ad5a55e8acea5d368b376d
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 "osdep/io.h"
36 #include "input.h"
37 #include "mp_fifo.h"
38 #include "keycodes.h"
39 #include "osdep/timer.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/common.h"
42 #include "mp_msg.h"
43 #include "m_config.h"
44 #include "m_option.h"
45 #include "path.h"
46 #include "talloc.h"
47 #include "options.h"
48 #include "bstr.h"
49 #include "stream/stream.h"
51 #include "joystick.h"
53 #ifdef CONFIG_LIRC
54 #include "lirc.h"
55 #endif
57 #ifdef CONFIG_LIRCC
58 #include <lirc/lircc.h>
59 #endif
61 #include "ar.h"
63 #ifdef CONFIG_COCOA
64 #include "osdep/cocoa_events.h"
65 #endif
67 static const char embedded_file[] =
68 #include "input.conf.h"
70 static const struct bstr builtin_input_conf = {
71 .start = (char *)embedded_file, .len = sizeof(embedded_file) - 1
74 #define MP_MAX_KEY_DOWN 32
76 struct cmd_bind {
77 int input[MP_MAX_KEY_DOWN + 1];
78 char *cmd;
81 struct key_name {
82 int key;
83 char *name;
86 /* This array defines all known commands.
87 * The first field is an id used to recognize the command.
88 * The second is the command name used in slave mode and input.conf.
89 * Then comes the definition of each argument, first mandatory arguments
90 * (ARG_INT, ARG_FLOAT, ARG_STRING) if any, then optional arguments
91 * (OARG_INT(default), etc) if any. The command will be given the default
92 * argument value if the user didn't give enough arguments to specify it.
93 * A command can take a maximum of MP_CMD_MAX_ARGS arguments (10).
95 #define ARG_INT { .type = MP_CMD_ARG_INT }
96 #define OARG_INT(def) { .type = MP_CMD_ARG_INT, .optional = true, .v.i = def }
97 #define ARG_FLOAT { .type = MP_CMD_ARG_FLOAT }
98 #define OARG_FLOAT(def) { .type = MP_CMD_ARG_FLOAT, .optional = true, .v.f = def }
99 #define ARG_STRING { .type = MP_CMD_ARG_STRING }
100 #define OARG_STRING(def) { .type = MP_CMD_ARG_STRING, .optional = true, .v.s = def }
102 static const mp_cmd_t mp_cmds[] = {
103 #ifdef CONFIG_RADIO
104 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", { ARG_INT } },
105 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", { ARG_STRING } },
106 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", { ARG_FLOAT } },
107 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", {ARG_FLOAT } },
108 #endif
109 { MP_CMD_SEEK, "seek", { ARG_FLOAT, OARG_INT(0), OARG_INT(0) } },
110 { MP_CMD_EDL_MARK, "edl_mark", },
111 { MP_CMD_AUDIO_DELAY, "audio_delay", { ARG_FLOAT, OARG_INT(0) } },
112 { MP_CMD_SPEED_INCR, "speed_incr", { ARG_FLOAT } },
113 { MP_CMD_SPEED_MULT, "speed_mult", { ARG_FLOAT } },
114 { MP_CMD_SPEED_SET, "speed_set", { ARG_FLOAT } },
115 { MP_CMD_QUIT, "quit", { OARG_INT(0) } },
116 { MP_CMD_STOP, "stop", },
117 { MP_CMD_PAUSE, "pause", },
118 { MP_CMD_FRAME_STEP, "frame_step", },
119 { MP_CMD_PLAY_TREE_STEP, "pt_step", { ARG_INT, OARG_INT(0) } },
120 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step", { ARG_INT, OARG_INT(0) } },
121 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step", { ARG_INT } },
122 { MP_CMD_LOOP, "loop", { ARG_INT, OARG_INT(0) } },
123 { MP_CMD_SUB_DELAY, "sub_delay", { ARG_FLOAT, OARG_INT(0) } },
124 { MP_CMD_OSD, "osd", { OARG_INT(-1) } },
125 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", { ARG_STRING, OARG_INT(-1), OARG_INT(0) } },
126 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text", { ARG_STRING, OARG_INT(-1), OARG_INT(0) } },
127 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", },
128 { MP_CMD_VOLUME, "volume", { ARG_FLOAT, OARG_INT(0) } },
129 { MP_CMD_BALANCE, "balance", { ARG_FLOAT, OARG_INT(0) } },
130 { MP_CMD_MUTE, "mute", { OARG_INT(-1) } },
131 { MP_CMD_CONTRAST, "contrast", { ARG_INT, OARG_INT(0) } },
132 { MP_CMD_GAMMA, "gamma", { ARG_INT, OARG_INT(0) } },
133 { MP_CMD_BRIGHTNESS, "brightness", { ARG_INT, OARG_INT(0) } },
134 { MP_CMD_HUE, "hue", { ARG_INT, OARG_INT(0) } },
135 { MP_CMD_SATURATION, "saturation", { ARG_INT, OARG_INT(0) } },
136 { MP_CMD_FRAMEDROPPING, "frame_drop", { OARG_INT(-1) } },
137 { MP_CMD_SUB_POS, "sub_pos", { ARG_INT, OARG_INT(0) } },
138 { MP_CMD_SUB_ALIGNMENT, "sub_alignment", { OARG_INT(-1) } },
139 { MP_CMD_SUB_VISIBILITY, "sub_visibility", { OARG_INT(-1) } },
140 { MP_CMD_SUB_LOAD, "sub_load", { ARG_STRING } },
141 { MP_CMD_SUB_REMOVE, "sub_remove", { OARG_INT(-1) } },
142 { MP_CMD_SUB_SELECT, "vobsub_lang", { OARG_INT(-2) } }, // for compatibility
143 { MP_CMD_SUB_SELECT, "sub_select", { OARG_INT(-2) } },
144 { MP_CMD_SUB_SOURCE, "sub_source", { OARG_INT(-2) } },
145 { MP_CMD_SUB_VOB, "sub_vob", { OARG_INT(-2) } },
146 { MP_CMD_SUB_DEMUX, "sub_demux", { OARG_INT(-2) } },
147 { MP_CMD_SUB_FILE, "sub_file", { OARG_INT(-2) } },
148 { MP_CMD_SUB_LOG, "sub_log", },
149 { MP_CMD_SUB_SCALE, "sub_scale", { ARG_FLOAT, OARG_INT(0) } },
150 #ifdef CONFIG_ASS
151 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", { OARG_INT(-1) } },
152 #endif
153 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", },
154 { MP_CMD_GET_TIME_POS, "get_time_pos", },
155 { MP_CMD_GET_TIME_LENGTH, "get_time_length", },
156 { MP_CMD_GET_FILENAME, "get_file_name", },
157 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", },
158 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", },
159 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", },
160 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", },
161 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", },
162 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", },
163 { MP_CMD_GET_META_TITLE, "get_meta_title", },
164 { MP_CMD_GET_META_ARTIST, "get_meta_artist", },
165 { MP_CMD_GET_META_ALBUM, "get_meta_album", },
166 { MP_CMD_GET_META_YEAR, "get_meta_year", },
167 { MP_CMD_GET_META_COMMENT, "get_meta_comment", },
168 { MP_CMD_GET_META_TRACK, "get_meta_track", },
169 { MP_CMD_GET_META_GENRE, "get_meta_genre", },
170 { MP_CMD_SWITCH_AUDIO, "switch_audio", { OARG_INT(-1) } },
171 { MP_CMD_SWITCH_ANGLE, "switch_angle", { OARG_INT(-1) } },
172 { MP_CMD_SWITCH_TITLE, "switch_title", { OARG_INT(-1) } },
173 #ifdef CONFIG_TV
174 { MP_CMD_TV_START_SCAN, "tv_start_scan", },
175 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", { ARG_INT } },
176 { MP_CMD_TV_STEP_NORM, "tv_step_norm", },
177 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", },
178 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", { ARG_STRING } },
179 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", },
180 { MP_CMD_TV_SET_FREQ, "tv_set_freq", { ARG_FLOAT } },
181 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", { ARG_FLOAT } },
182 { MP_CMD_TV_SET_NORM, "tv_set_norm", { ARG_STRING } },
183 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", { ARG_INT, OARG_INT(1) } },
184 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", { ARG_INT, OARG_INT(1) } },
185 { MP_CMD_TV_SET_HUE, "tv_set_hue", { ARG_INT, OARG_INT(1) } },
186 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", { ARG_INT, OARG_INT(1) } },
187 #endif
188 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", { OARG_INT(-1) } },
189 #ifdef CONFIG_DVBIN
190 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", { ARG_INT, ARG_INT } },
191 #endif
192 { MP_CMD_SWITCH_RATIO, "switch_ratio", { OARG_FLOAT(0) } },
193 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", { OARG_INT(-1) } },
194 { MP_CMD_VO_ONTOP, "vo_ontop", { OARG_INT(-1) } },
195 { MP_CMD_VO_ROOTWIN, "vo_rootwin", { OARG_INT(-1) } },
196 { MP_CMD_VO_BORDER, "vo_border", { OARG_INT(-1) } },
197 { MP_CMD_SCREENSHOT, "screenshot", { OARG_INT(0), OARG_INT(0) } },
198 { MP_CMD_PANSCAN, "panscan", { ARG_FLOAT, OARG_INT(0) } },
199 { MP_CMD_SWITCH_VSYNC, "switch_vsync", { OARG_INT(0) } },
200 { MP_CMD_LOADFILE, "loadfile", { ARG_STRING, OARG_INT(0) } },
201 { MP_CMD_LOADLIST, "loadlist", { ARG_STRING, OARG_INT(0) } },
202 { MP_CMD_RUN, "run", { ARG_STRING } },
203 { MP_CMD_CAPTURING, "capturing", },
204 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", { ARG_INT, ARG_INT } },
205 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", { ARG_STRING } },
206 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", { ARG_INT } },
208 #ifdef CONFIG_DVDNAV
209 { MP_CMD_DVDNAV, "dvdnav", { ARG_STRING } },
210 #endif
212 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", },
213 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", },
214 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", { ARG_INT } },
215 { MP_CMD_SET_PROPERTY, "set_property", { ARG_STRING, ARG_STRING } },
216 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", { ARG_STRING, ARG_STRING } },
217 { MP_CMD_GET_PROPERTY, "get_property", { ARG_STRING } },
218 { MP_CMD_STEP_PROPERTY, "step_property", { ARG_STRING, OARG_FLOAT(0) } },
219 { MP_CMD_STEP_PROPERTY_OSD, "step_property_osd", { ARG_STRING, OARG_FLOAT(0) } },
221 { MP_CMD_SEEK_CHAPTER, "seek_chapter", { ARG_INT, OARG_INT(0) } },
222 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", { ARG_INT, ARG_INT } },
224 { MP_CMD_AF_SWITCH, "af_switch", { ARG_STRING } },
225 { MP_CMD_AF_ADD, "af_add", { ARG_STRING } },
226 { MP_CMD_AF_DEL, "af_del", { ARG_STRING } },
227 { MP_CMD_AF_CLR, "af_clr", },
228 { MP_CMD_AF_CMDLINE, "af_cmdline", { ARG_STRING, ARG_STRING } },
232 /// The names of the keys as used in input.conf
233 /// If you add some new keys, you also need to add them here
235 static const struct key_name key_names[] = {
236 { ' ', "SPACE" },
237 { '#', "SHARP" },
238 { KEY_ENTER, "ENTER" },
239 { KEY_TAB, "TAB" },
240 { KEY_BACKSPACE, "BS" },
241 { KEY_DELETE, "DEL" },
242 { KEY_INSERT, "INS" },
243 { KEY_HOME, "HOME" },
244 { KEY_END, "END" },
245 { KEY_PAGE_UP, "PGUP" },
246 { KEY_PAGE_DOWN, "PGDWN" },
247 { KEY_ESC, "ESC" },
248 { KEY_PRINT, "PRINT" },
249 { KEY_RIGHT, "RIGHT" },
250 { KEY_LEFT, "LEFT" },
251 { KEY_DOWN, "DOWN" },
252 { KEY_UP, "UP" },
253 { KEY_F+1, "F1" },
254 { KEY_F+2, "F2" },
255 { KEY_F+3, "F3" },
256 { KEY_F+4, "F4" },
257 { KEY_F+5, "F5" },
258 { KEY_F+6, "F6" },
259 { KEY_F+7, "F7" },
260 { KEY_F+8, "F8" },
261 { KEY_F+9, "F9" },
262 { KEY_F+10, "F10" },
263 { KEY_F+11, "F11" },
264 { KEY_F+12, "F12" },
265 { KEY_KP0, "KP0" },
266 { KEY_KP1, "KP1" },
267 { KEY_KP2, "KP2" },
268 { KEY_KP3, "KP3" },
269 { KEY_KP4, "KP4" },
270 { KEY_KP5, "KP5" },
271 { KEY_KP6, "KP6" },
272 { KEY_KP7, "KP7" },
273 { KEY_KP8, "KP8" },
274 { KEY_KP9, "KP9" },
275 { KEY_KPDEL, "KP_DEL" },
276 { KEY_KPDEC, "KP_DEC" },
277 { KEY_KPINS, "KP_INS" },
278 { KEY_KPENTER, "KP_ENTER" },
279 { MOUSE_BTN0, "MOUSE_BTN0" },
280 { MOUSE_BTN1, "MOUSE_BTN1" },
281 { MOUSE_BTN2, "MOUSE_BTN2" },
282 { MOUSE_BTN3, "MOUSE_BTN3" },
283 { MOUSE_BTN4, "MOUSE_BTN4" },
284 { MOUSE_BTN5, "MOUSE_BTN5" },
285 { MOUSE_BTN6, "MOUSE_BTN6" },
286 { MOUSE_BTN7, "MOUSE_BTN7" },
287 { MOUSE_BTN8, "MOUSE_BTN8" },
288 { MOUSE_BTN9, "MOUSE_BTN9" },
289 { MOUSE_BTN10, "MOUSE_BTN10" },
290 { MOUSE_BTN11, "MOUSE_BTN11" },
291 { MOUSE_BTN12, "MOUSE_BTN12" },
292 { MOUSE_BTN13, "MOUSE_BTN13" },
293 { MOUSE_BTN14, "MOUSE_BTN14" },
294 { MOUSE_BTN15, "MOUSE_BTN15" },
295 { MOUSE_BTN16, "MOUSE_BTN16" },
296 { MOUSE_BTN17, "MOUSE_BTN17" },
297 { MOUSE_BTN18, "MOUSE_BTN18" },
298 { MOUSE_BTN19, "MOUSE_BTN19" },
299 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
300 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
301 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
302 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
303 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
304 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
305 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
306 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
307 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
308 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
309 { MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
310 { MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
311 { MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
312 { MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
313 { MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
314 { MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
315 { MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
316 { MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
317 { MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
318 { MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
319 { JOY_AXIS1_MINUS, "JOY_UP" },
320 { JOY_AXIS1_PLUS, "JOY_DOWN" },
321 { JOY_AXIS0_MINUS, "JOY_LEFT" },
322 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
324 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
325 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
326 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
327 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
328 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
329 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
330 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
331 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
332 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
333 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
334 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
335 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
336 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
337 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
338 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
339 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
340 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
341 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
342 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
343 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
345 { JOY_BTN0, "JOY_BTN0" },
346 { JOY_BTN1, "JOY_BTN1" },
347 { JOY_BTN2, "JOY_BTN2" },
348 { JOY_BTN3, "JOY_BTN3" },
349 { JOY_BTN4, "JOY_BTN4" },
350 { JOY_BTN5, "JOY_BTN5" },
351 { JOY_BTN6, "JOY_BTN6" },
352 { JOY_BTN7, "JOY_BTN7" },
353 { JOY_BTN8, "JOY_BTN8" },
354 { JOY_BTN9, "JOY_BTN9" },
356 { AR_PLAY, "AR_PLAY" },
357 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
358 { AR_NEXT, "AR_NEXT" },
359 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
360 { AR_PREV, "AR_PREV" },
361 { AR_PREV_HOLD, "AR_PREV_HOLD" },
362 { AR_MENU, "AR_MENU" },
363 { AR_MENU_HOLD, "AR_MENU_HOLD" },
364 { AR_VUP, "AR_VUP" },
365 { AR_VDOWN, "AR_VDOWN" },
367 { KEY_POWER, "POWER" },
368 { KEY_MENU, "MENU" },
369 { KEY_PLAY, "PLAY" },
370 { KEY_PAUSE, "PAUSE" },
371 { KEY_PLAYPAUSE, "PLAYPAUSE" },
372 { KEY_STOP, "STOP" },
373 { KEY_FORWARD, "FORWARD" },
374 { KEY_REWIND, "REWIND" },
375 { KEY_NEXT, "NEXT" },
376 { KEY_PREV, "PREV" },
377 { KEY_VOLUME_UP, "VOLUME_UP" },
378 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
379 { KEY_MUTE, "MUTE" },
381 // These are kept for backward compatibility
382 { KEY_PAUSE, "XF86_PAUSE" },
383 { KEY_STOP, "XF86_STOP" },
384 { KEY_PREV, "XF86_PREV" },
385 { KEY_NEXT, "XF86_NEXT" },
387 { KEY_CLOSE_WIN, "CLOSE_WIN" },
389 { 0, NULL }
392 struct key_name modifier_names[] = {
393 { KEY_MODIFIER_SHIFT, "Shift" },
394 { KEY_MODIFIER_CTRL, "Ctrl" },
395 { KEY_MODIFIER_ALT, "Alt" },
396 { KEY_MODIFIER_META, "Meta" },
397 { 0 }
400 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
402 #ifndef MP_MAX_KEY_FD
403 #define MP_MAX_KEY_FD 10
404 #endif
406 #ifndef MP_MAX_CMD_FD
407 #define MP_MAX_CMD_FD 10
408 #endif
410 struct input_fd {
411 int fd;
412 union {
413 int (*key)(void *ctx, int fd);
414 int (*cmd)(int fd, char *dest, int size);
415 } read_func;
416 int (*close_func)(int fd);
417 void *ctx;
418 unsigned eof : 1;
419 unsigned drop : 1;
420 unsigned dead : 1;
421 unsigned got_cmd : 1;
422 unsigned no_select : 1;
423 // These fields are for the cmd fds.
424 char *buffer;
425 int pos, size;
428 struct cmd_bind_section {
429 struct cmd_bind *cmd_binds;
430 struct bstr section;
431 struct cmd_bind_section *next;
434 struct cmd_queue {
435 struct mp_cmd *first;
436 struct mp_cmd *last;
437 int num_cmds;
438 int num_abort_cmds;
441 struct input_ctx {
442 // Autorepeat stuff
443 short ar_state;
444 mp_cmd_t *ar_cmd;
445 unsigned int last_ar;
446 // Autorepeat config
447 unsigned int ar_delay;
448 unsigned int ar_rate;
449 // Maximum number of queued commands from keypresses (limit to avoid
450 // repeated slow commands piling up)
451 int key_fifo_size;
453 // these are the keys currently down
454 int key_down[MP_MAX_KEY_DOWN];
455 unsigned int num_key_down;
456 unsigned int last_key_down;
458 // List of command binding sections
459 struct cmd_bind_section *cmd_bind_sections;
460 // Name of currently used command section
461 struct bstr section;
463 // Used to track whether we managed to read something while checking
464 // events sources. If yes, the sources may have more queued.
465 bool got_new_events;
467 struct input_fd key_fds[MP_MAX_KEY_FD];
468 unsigned int num_key_fd;
470 struct input_fd cmd_fds[MP_MAX_CMD_FD];
471 unsigned int num_cmd_fd;
473 struct cmd_queue key_cmd_queue;
474 struct cmd_queue control_cmd_queue;
476 int wakeup_pipe[2];
478 struct MPOpts *opts;
482 int async_quit_request;
484 static int print_key_list(m_option_t *cfg, char *optname, char *optparam);
485 static int print_cmd_list(m_option_t *cfg, char *optname, char *optparam);
487 // Our command line options
488 static const m_option_t input_conf[] = {
489 OPT_STRING("conf", input.config_file, CONF_GLOBAL, OPTDEF_STR("input.conf")),
490 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
491 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
492 { "keylist", print_key_list, CONF_TYPE_PRINT_FUNC, CONF_NOCFG },
493 { "cmdlist", print_cmd_list, CONF_TYPE_PRINT_FUNC, CONF_NOCFG },
494 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
495 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
496 OPT_STRING("file", input.in_file, CONF_GLOBAL),
497 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
498 { NULL, NULL, 0, 0, 0, 0, NULL}
501 static const m_option_t mp_input_opts[] = {
502 { "input", (void *)&input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
503 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
504 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
505 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
506 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
507 { NULL, NULL, 0, 0, 0, 0, NULL}
510 static int default_cmd_func(int fd, char *buf, int l);
512 // Encode the unicode codepoint as UTF-8, and append to the end of the
513 // talloc'ed buffer.
514 static char *append_utf8_buffer(char *buffer, uint32_t codepoint)
516 char data[8];
517 uint8_t tmp;
518 char *output = data;
519 PUT_UTF8(codepoint, tmp, *output++ = tmp;);
520 return talloc_strndup_append_buffer(buffer, data, output - data);
523 static char *get_key_name(int key, char *ret)
525 for (int i = 0; modifier_names[i].name; i++) {
526 if (modifier_names[i].key & key) {
527 ret = talloc_asprintf_append_buffer(ret, "%s+",
528 modifier_names[i].name);
529 key -= modifier_names[i].key;
532 for (int i = 0; key_names[i].name != NULL; i++) {
533 if (key_names[i].key == key)
534 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
537 // printable, and valid unicode range
538 if (key >= 32 && key <= 0x10FFFF)
539 return append_utf8_buffer(ret, key);
541 // Print the hex key code
542 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
545 static char *get_key_combo_name(int *keys, int max)
547 char *ret = talloc_strdup(NULL, "");
548 while (1) {
549 ret = get_key_name(*keys, ret);
550 if (--max && *++keys)
551 ret = talloc_asprintf_append_buffer(ret, "-");
552 else
553 break;
555 return ret;
558 static bool is_abort_cmd(int cmd_id)
560 switch (cmd_id) {
561 case MP_CMD_QUIT:
562 case MP_CMD_PLAY_TREE_STEP:
563 case MP_CMD_PLAY_TREE_UP_STEP:
564 case MP_CMD_PLAY_ALT_SRC_STEP:
565 return true;
567 return false;
570 static void queue_pop(struct cmd_queue *queue)
572 assert(queue->num_cmds > 0);
573 struct mp_cmd *cmd = queue->first;
574 queue->first = cmd->queue_next;
575 queue->num_cmds--;
576 queue->num_abort_cmds -= is_abort_cmd(cmd->id);
579 static void queue_add(struct cmd_queue *queue, struct mp_cmd *cmd,
580 bool at_head)
582 if (!queue->num_cmds) {
583 queue->first = cmd;
584 queue->last = cmd;
585 } else if (at_head) {
586 queue->first->queue_prev = cmd;
587 cmd->queue_next = queue->first;
588 queue->first = cmd;
589 } else {
590 queue->last->queue_next = cmd;
591 cmd->queue_prev = queue->last;
592 queue->last = cmd;
594 queue->num_cmds++;
595 queue->num_abort_cmds += is_abort_cmd(cmd->id);
598 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
599 int read_func(int fd, char *dest, int size),
600 int close_func(int fd))
602 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
603 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
604 "cannot register file descriptor %d.\n", fd);
605 return 0;
607 if (select && fd < 0) {
608 mp_msg(MSGT_INPUT, MSGL_ERR,
609 "Invalid fd %d in mp_input_add_cmd_fd", fd);
610 return 0;
613 ictx->cmd_fds[ictx->num_cmd_fd] = (struct input_fd){
614 .fd = fd,
615 .read_func.cmd = read_func ? read_func : default_cmd_func,
616 .close_func = close_func,
617 .no_select = !select
619 ictx->num_cmd_fd++;
621 return 1;
624 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
626 struct input_fd *cmd_fds = ictx->cmd_fds;
627 unsigned int i;
629 for (i = 0; i < ictx->num_cmd_fd; i++) {
630 if (cmd_fds[i].fd == fd)
631 break;
633 if (i == ictx->num_cmd_fd)
634 return;
635 if (cmd_fds[i].close_func)
636 cmd_fds[i].close_func(cmd_fds[i].fd);
637 talloc_free(cmd_fds[i].buffer);
639 if (i + 1 < ictx->num_cmd_fd)
640 memmove(&cmd_fds[i], &cmd_fds[i + 1],
641 (ictx->num_cmd_fd - i - 1) * sizeof(struct input_fd));
642 ictx->num_cmd_fd--;
645 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
647 struct input_fd *key_fds = ictx->key_fds;
648 unsigned int i;
650 for (i = 0; i < ictx->num_key_fd; i++) {
651 if (key_fds[i].fd == fd)
652 break;
654 if (i == ictx->num_key_fd)
655 return;
656 if (key_fds[i].close_func)
657 key_fds[i].close_func(key_fds[i].fd);
659 if (i + 1 < ictx->num_key_fd)
660 memmove(&key_fds[i], &key_fds[i + 1],
661 (ictx->num_key_fd - i - 1) * sizeof(struct input_fd));
662 ictx->num_key_fd--;
665 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
666 int read_func(void *ctx, int fd),
667 int close_func(int fd), void *ctx)
669 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
670 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
671 "cannot register file descriptor %d.\n", fd);
672 return 0;
674 if (select && fd < 0) {
675 mp_msg(MSGT_INPUT, MSGL_ERR,
676 "Invalid fd %d in mp_input_add_key_fd", fd);
677 return 0;
680 ictx->key_fds[ictx->num_key_fd] = (struct input_fd){
681 .fd = fd,
682 .read_func.key = read_func,
683 .close_func = close_func,
684 .no_select = !select,
685 .ctx = ctx,
687 ictx->num_key_fd++;
689 return 1;
692 mp_cmd_t *mp_input_parse_cmd(char *str)
694 int i, l;
695 int pausing = 0;
696 char *ptr;
697 const mp_cmd_t *cmd_def;
699 // Ignore heading spaces.
700 while (str[0] == ' ' || str[0] == '\t')
701 ++str;
703 if (strncmp(str, "pausing ", 8) == 0) {
704 pausing = 1;
705 str = &str[8];
706 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
707 pausing = 2;
708 str = &str[13];
709 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
710 pausing = 3;
711 str = &str[15];
712 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
713 pausing = 4;
714 str = &str[19];
717 ptr = str + strcspn(str, "\t ");
718 if (*ptr != 0)
719 l = ptr - str;
720 else
721 l = strlen(str);
723 if (l == 0)
724 return NULL;
726 for (i = 0; mp_cmds[i].name != NULL; i++) {
727 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
728 break;
731 if (mp_cmds[i].name == NULL)
732 return NULL;
734 cmd_def = &mp_cmds[i];
736 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
737 *cmd = (mp_cmd_t){
738 .id = cmd_def->id,
739 .name = talloc_strdup(cmd, cmd_def->name),
740 .pausing = pausing,
743 ptr = str;
745 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
746 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
747 ptr++;
748 if (ptr[0] == '\0')
749 break;
750 while (ptr[0] == ' ' || ptr[0] == '\t')
751 ptr++;
752 if (ptr[0] == '\0' || ptr[0] == '#')
753 break;
754 cmd->args[i].type = cmd_def->args[i].type;
755 switch (cmd_def->args[i].type) {
756 case MP_CMD_ARG_INT:
757 errno = 0;
758 cmd->args[i].v.i = atoi(ptr);
759 if (errno != 0) {
760 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
761 "isn't an integer.\n", cmd_def->name, i + 1);
762 goto error;
764 break;
765 case MP_CMD_ARG_FLOAT:
766 errno = 0;
767 cmd->args[i].v.f = atof(ptr);
768 if (errno != 0) {
769 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
770 "isn't a float.\n", cmd_def->name, i + 1);
771 goto error;
773 break;
774 case MP_CMD_ARG_STRING: {
775 int term = ' ';
776 if (*ptr == '\'' || *ptr == '"')
777 term = *ptr++;
778 char *argptr = talloc_size(cmd, strlen(ptr) + 1);
779 cmd->args[i].v.s = argptr;
780 while (1) {
781 if (*ptr == 0) {
782 if (term == ' ')
783 break;
784 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
785 "unterminated.\n", cmd_def->name, i + 1);
786 goto error;
788 if (*ptr == term)
789 break;
790 if (*ptr == '\\')
791 ptr++;
792 if (*ptr != 0)
793 *argptr++ = *ptr++;
795 *argptr = 0;
796 break;
798 case 0:
799 ptr = NULL;
800 break;
801 default:
802 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
805 cmd->nargs = i;
807 int min_args;
808 for (min_args = 0; min_args < MP_CMD_MAX_ARGS
809 && cmd_def->args[min_args].type
810 && !cmd_def->args[min_args].optional; min_args++);
811 if (cmd->nargs < min_args) {
812 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command \"%s\" requires at least %d "
813 "arguments, we found only %d so far.\n", cmd_def->name,
814 min_args, cmd->nargs);
815 goto error;
818 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type; i++) {
819 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(struct mp_cmd_arg));
820 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
821 && cmd_def->args[i].v.s != NULL)
822 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
825 if (i < MP_CMD_MAX_ARGS)
826 cmd->args[i].type = 0;
828 return cmd;
830 error:
831 mp_cmd_free(cmd);
832 return NULL;
835 #define MP_CMD_MAX_SIZE 4096
837 static int read_cmd(struct input_fd *mp_fd, char **ret)
839 char *end;
840 *ret = NULL;
842 // Allocate the buffer if it doesn't exist
843 if (!mp_fd->buffer) {
844 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
845 mp_fd->pos = 0;
846 mp_fd->size = MP_CMD_MAX_SIZE;
849 // Get some data if needed/possible
850 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
851 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
852 mp_fd->size - 1 - mp_fd->pos);
853 // Error ?
854 if (r < 0) {
855 switch (r) {
856 case MP_INPUT_ERROR:
857 case MP_INPUT_DEAD:
858 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
859 "command file descriptor %d: %s\n",
860 mp_fd->fd, strerror(errno));
861 case MP_INPUT_NOTHING:
862 return r;
863 case MP_INPUT_RETRY:
864 continue;
866 // EOF ?
867 } else if (r == 0) {
868 mp_fd->eof = 1;
869 break;
871 mp_fd->pos += r;
872 break;
875 mp_fd->got_cmd = 0;
877 while (1) {
878 int l = 0;
879 // Find the cmd end
880 mp_fd->buffer[mp_fd->pos] = '\0';
881 end = strchr(mp_fd->buffer, '\r');
882 if (end)
883 *end = '\n';
884 end = strchr(mp_fd->buffer, '\n');
885 // No cmd end ?
886 if (!end) {
887 // If buffer is full we must drop all until the next \n
888 if (mp_fd->size - mp_fd->pos <= 1) {
889 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
890 "descriptor %d is full: dropping content.\n",
891 mp_fd->fd);
892 mp_fd->pos = 0;
893 mp_fd->drop = 1;
895 break;
897 // We already have a cmd : set the got_cmd flag
898 else if ((*ret)) {
899 mp_fd->got_cmd = 1;
900 break;
903 l = end - mp_fd->buffer;
905 // Not dropping : put the cmd in ret
906 if (!mp_fd->drop)
907 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
908 else
909 mp_fd->drop = 0;
910 mp_fd->pos -= l + 1;
911 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
914 if (*ret)
915 return 1;
916 else
917 return MP_INPUT_NOTHING;
920 static int default_cmd_func(int fd, char *buf, int l)
922 while (1) {
923 int r = read(fd, buf, l);
924 // Error ?
925 if (r < 0) {
926 if (errno == EINTR)
927 continue;
928 else if (errno == EAGAIN)
929 return MP_INPUT_NOTHING;
930 return MP_INPUT_ERROR;
931 // EOF ?
933 return r;
937 static int read_wakeup(void *ctx, int fd)
939 char buf[100];
940 read(fd, buf, sizeof(buf));
941 return MP_INPUT_NOTHING;
945 static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
946 struct bstr section)
948 struct cmd_bind_section *bind_section = ictx->cmd_bind_sections;
950 while (bind_section) {
951 if (bstrcmp(section, bind_section->section) == 0)
952 return bind_section;
953 if (bind_section->next == NULL)
954 break;
955 bind_section = bind_section->next;
957 if (bind_section) {
958 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
959 bind_section = bind_section->next;
960 } else {
961 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
962 bind_section = ictx->cmd_bind_sections;
964 bind_section->cmd_binds = NULL;
965 bind_section->section = bstrdup(bind_section, section);
966 bind_section->next = NULL;
967 return bind_section;
970 static char *find_bind_for_key(struct input_ctx *ictx, struct bstr section,
971 int n, int *keys)
973 if (n <= 0)
974 return NULL;
975 struct cmd_bind_section *s = get_bind_section(ictx, section);
976 struct cmd_bind *binds = s->cmd_binds;
977 if (!binds)
978 return NULL;
979 int j;
980 for (j = 0; binds[j].cmd != NULL; j++) {
981 int found = 1, s;
982 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
983 if (binds[j].input[s] != keys[s]) {
984 found = 0;
985 break;
988 if (found && binds[j].input[s] == 0 && s == n)
989 break;
991 return binds[j].cmd;
994 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
996 char *cmd = find_bind_for_key(ictx, ictx->section, n, keys);
997 if (cmd == NULL)
998 cmd = find_bind_for_key(ictx, bstr("default"), n, keys);
999 if (cmd == NULL) {
1000 char *key_buf = get_key_combo_name(keys, n);
1001 mp_tmsg(MSGT_INPUT, MSGL_WARN,
1002 "No bind found for key '%s'.\n", key_buf);
1003 talloc_free(key_buf);
1004 return NULL;
1006 if (strcmp(cmd, "ignore") == 0)
1007 return NULL;
1008 struct mp_cmd *ret = mp_input_parse_cmd(cmd);
1009 if (!ret) {
1010 char *key_buf = get_key_combo_name(keys, n);
1011 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1012 "Invalid command for bound key '%s': '%s'\n", key_buf, cmd);
1013 talloc_free(key_buf);
1015 return ret;
1019 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1021 unsigned int j;
1022 mp_cmd_t *ret;
1024 /* On normal keyboards shift changes the character code of non-special
1025 * keys, so don't count the modifier separately for those. In other words
1026 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1027 * shift modifier is still kept for special keys like arrow keys.
1029 int unmod = code & ~KEY_MODIFIER_MASK;
1030 if (unmod >= 32 && unmod < MP_KEY_BASE)
1031 code &= ~KEY_MODIFIER_SHIFT;
1033 if (code & MP_KEY_DOWN) {
1034 if (ictx->num_key_down >= MP_MAX_KEY_DOWN) {
1035 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1036 "at the same time\n");
1037 return NULL;
1039 code &= ~MP_KEY_DOWN;
1040 // Check if we don't already have this key as pushed
1041 for (j = 0; j < ictx->num_key_down; j++) {
1042 if (ictx->key_down[j] == code)
1043 break;
1045 if (j != ictx->num_key_down)
1046 return NULL;
1047 ictx->key_down[ictx->num_key_down] = code;
1048 ictx->num_key_down++;
1049 ictx->last_key_down = GetTimer();
1050 ictx->ar_state = 0;
1051 return NULL;
1053 // button released or press of key with no separate down/up events
1054 for (j = 0; j < ictx->num_key_down; j++) {
1055 if (ictx->key_down[j] == code)
1056 break;
1058 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1059 if (doubleclick) {
1060 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1061 if (!ictx->num_key_down
1062 || ictx->key_down[ictx->num_key_down - 1] != btn)
1063 return NULL;
1064 j = ictx->num_key_down - 1;
1065 ictx->key_down[j] = code;
1067 if (j == ictx->num_key_down) { // was not already down; add temporarily
1068 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1069 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1070 "at the same time\n");
1071 return NULL;
1073 ictx->key_down[ictx->num_key_down] = code;
1074 ictx->num_key_down++;
1075 ictx->last_key_down = 1;
1077 // Interpret only maximal point of multibutton event
1078 ret = ictx->last_key_down ?
1079 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1080 : NULL;
1081 if (doubleclick) {
1082 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1083 return ret;
1085 // Remove the key
1086 if (j + 1 < ictx->num_key_down)
1087 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1088 (ictx->num_key_down - (j + 1)) * sizeof(int));
1089 ictx->num_key_down--;
1090 ictx->last_key_down = 0;
1091 ictx->ar_state = -1;
1092 mp_cmd_free(ictx->ar_cmd);
1093 ictx->ar_cmd = NULL;
1094 return ret;
1097 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1099 // No input : autorepeat ?
1100 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1101 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1102 unsigned int t = GetTimer();
1103 // First time : wait delay
1104 if (ictx->ar_state == 0
1105 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1106 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1107 ictx->key_down);
1108 if (!ictx->ar_cmd) {
1109 ictx->ar_state = -1;
1110 return NULL;
1112 ictx->ar_state = 1;
1113 ictx->last_ar = t;
1114 return mp_cmd_clone(ictx->ar_cmd);
1115 // Then send rate / sec event
1116 } else if (ictx->ar_state == 1
1117 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1118 ictx->last_ar = t;
1119 return mp_cmd_clone(ictx->ar_cmd);
1122 return NULL;
1125 void mp_input_feed_key(struct input_ctx *ictx, int code)
1127 ictx->got_new_events = true;
1128 if (code == MP_INPUT_RELEASE_ALL) {
1129 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1130 ictx->num_key_down = 0;
1131 ictx->last_key_down = 0;
1132 return;
1134 struct mp_cmd *cmd = interpret_key(ictx, code);
1135 if (!cmd)
1136 return;
1137 struct cmd_queue *queue = &ictx->key_cmd_queue;
1138 if (queue->num_cmds >= ictx->key_fifo_size &&
1139 (!is_abort_cmd(cmd->id) || queue->num_abort_cmds)) {
1140 talloc_free(cmd);
1141 return;
1143 queue_add(queue, cmd, false);
1146 static void read_cmd_fd(struct input_ctx *ictx, struct input_fd *cmd_fd)
1148 int r;
1149 char *text;
1150 while ((r = read_cmd(cmd_fd, &text)) >= 0) {
1151 ictx->got_new_events = true;
1152 struct mp_cmd *cmd = mp_input_parse_cmd(text);
1153 talloc_free(text);
1154 if (cmd)
1155 queue_add(&ictx->control_cmd_queue, cmd, false);
1156 if (!cmd_fd->got_cmd)
1157 return;
1159 if (r == MP_INPUT_ERROR)
1160 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1161 cmd_fd->fd);
1162 else if (r == MP_INPUT_DEAD)
1163 cmd_fd->dead = true;
1166 static void read_key_fd(struct input_ctx *ictx, struct input_fd *key_fd)
1168 int code = key_fd->read_func.key(key_fd->ctx, key_fd->fd);
1169 if (code >= 0 || code == MP_INPUT_RELEASE_ALL) {
1170 mp_input_feed_key(ictx, code);
1171 return;
1174 if (code == MP_INPUT_ERROR)
1175 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1176 "Error on key input file descriptor %d\n", key_fd->fd);
1177 else if (code == MP_INPUT_DEAD) {
1178 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1179 "Dead key input on file descriptor %d\n", key_fd->fd);
1180 key_fd->dead = true;
1185 * \param time time to wait at most for an event in milliseconds
1187 static void read_events(struct input_ctx *ictx, int time)
1189 ictx->got_new_events = false;
1190 struct input_fd *key_fds = ictx->key_fds;
1191 struct input_fd *cmd_fds = ictx->cmd_fds;
1192 for (int i = 0; i < ictx->num_key_fd; i++)
1193 if (key_fds[i].dead) {
1194 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1195 i--;
1196 } else if (time && key_fds[i].no_select)
1197 read_key_fd(ictx, &key_fds[i]);
1198 for (int i = 0; i < ictx->num_cmd_fd; i++)
1199 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1200 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1201 i--;
1202 } else if (time && cmd_fds[i].no_select)
1203 read_cmd_fd(ictx, &cmd_fds[i]);
1204 if (ictx->got_new_events)
1205 time = 0;
1206 #ifdef HAVE_POSIX_SELECT
1207 fd_set fds;
1208 FD_ZERO(&fds);
1209 int max_fd = 0;
1210 for (int i = 0; i < ictx->num_key_fd; i++) {
1211 if (key_fds[i].no_select)
1212 continue;
1213 if (key_fds[i].fd > max_fd)
1214 max_fd = key_fds[i].fd;
1215 FD_SET(key_fds[i].fd, &fds);
1217 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1218 if (cmd_fds[i].no_select)
1219 continue;
1220 if (cmd_fds[i].fd > max_fd)
1221 max_fd = cmd_fds[i].fd;
1222 FD_SET(cmd_fds[i].fd, &fds);
1224 struct timeval tv, *time_val;
1225 if (time >= 0) {
1226 tv.tv_sec = time / 1000;
1227 tv.tv_usec = (time % 1000) * 1000;
1228 time_val = &tv;
1229 } else
1230 time_val = NULL;
1231 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1232 if (errno != EINTR)
1233 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1234 strerror(errno));
1235 FD_ZERO(&fds);
1237 #else
1238 if (time)
1239 usec_sleep(time * 1000);
1240 #endif
1243 for (int i = 0; i < ictx->num_key_fd; i++) {
1244 #ifdef HAVE_POSIX_SELECT
1245 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1246 continue;
1247 #endif
1248 read_key_fd(ictx, &key_fds[i]);
1251 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1252 #ifdef HAVE_POSIX_SELECT
1253 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds))
1254 continue;
1255 #endif
1256 read_cmd_fd(ictx, &cmd_fds[i]);
1260 /* To support blocking file descriptors we don't loop the read over
1261 * every source until it's known to be empty. Instead we use this wrapper
1262 * to run select() again.
1264 static void read_all_fd_events(struct input_ctx *ictx, int time)
1266 while (1) {
1267 read_events(ictx, time);
1268 if (!ictx->got_new_events)
1269 return;
1270 time = 0;
1274 static void read_all_events(struct input_ctx *ictx, int time)
1276 #ifdef CONFIG_COCOA
1277 cocoa_events_read_all_events(ictx, time);
1278 #else
1279 read_all_fd_events(ictx, time);
1280 #endif
1283 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1285 ictx->got_new_events = true;
1286 if (!cmd)
1287 return 0;
1288 queue_add(&ictx->control_cmd_queue, cmd, true);
1289 return 1;
1293 * \param peek_only when set, the returned command stays in the queue.
1294 * Do not free the returned cmd whe you set this!
1296 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1298 if (async_quit_request)
1299 return mp_input_parse_cmd("quit 1");
1301 if (ictx->control_cmd_queue.num_cmds || ictx->key_cmd_queue.num_cmds)
1302 time = 0;
1303 read_all_events(ictx, time);
1304 struct mp_cmd *ret;
1305 struct cmd_queue *queue = &ictx->control_cmd_queue;
1306 if (!queue->num_cmds)
1307 queue = &ictx->key_cmd_queue;
1308 if (!queue->num_cmds) {
1309 ret = check_autorepeat(ictx);
1310 if (!ret)
1311 return NULL;
1312 queue_add(queue, ret, false);
1313 } else
1314 ret = queue->first;
1316 if (!peek_only)
1317 queue_pop(queue);
1319 return ret;
1322 void mp_cmd_free(mp_cmd_t *cmd)
1324 talloc_free(cmd);
1327 mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
1329 mp_cmd_t *ret;
1330 int i;
1332 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1333 ret->name = talloc_strdup(ret, cmd->name);
1334 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type; i++) {
1335 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1336 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1339 return ret;
1342 int mp_input_get_key_from_name(const char *name)
1344 int modifiers = 0;
1345 const char *p;
1346 while ((p = strchr(name, '+'))) {
1347 for (struct key_name *m = modifier_names; m->name; m++)
1348 if (!bstrcasecmp(bstr(m->name),
1349 (struct bstr){(char *)name, p - name})) {
1350 modifiers |= m->key;
1351 goto found;
1353 if (!strcmp(name, "+"))
1354 return '+' + modifiers;
1355 return -1;
1356 found:
1357 name = p + 1;
1360 struct bstr bname = bstr(name);
1362 struct bstr rest;
1363 int code = bstr_decode_utf8(bname, &rest);
1364 if (code >= 0 && rest.len == 0)
1365 return code + modifiers;
1367 if (bstr_startswith0(bname, "0x"))
1368 return strtol(name, NULL, 16) + modifiers;
1370 for (int i = 0; key_names[i].name != NULL; i++) {
1371 if (strcasecmp(key_names[i].name, name) == 0)
1372 return key_names[i].key + modifiers;
1375 return -1;
1378 static int get_input_from_name(char *name, int *keys)
1380 char *end, *ptr;
1381 int n = 0;
1383 ptr = name;
1384 n = 0;
1385 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1386 if (end && end[1] != '\0') {
1387 if (end[1] == '-')
1388 end = &end[1];
1389 end[0] = '\0';
1391 keys[n] = mp_input_get_key_from_name(ptr);
1392 if (keys[n] < 0)
1393 return 0;
1394 n++;
1395 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1396 ptr = &end[1];
1397 else
1398 break;
1400 keys[n] = 0;
1401 return 1;
1404 static void bind_keys(struct input_ctx *ictx,
1405 const int keys[MP_MAX_KEY_DOWN + 1], struct bstr cmd)
1407 int i = 0, j;
1408 struct cmd_bind *bind = NULL;
1409 struct cmd_bind_section *bind_section = NULL;
1410 struct bstr section = bstr("default");
1411 int idx;
1413 if (bstr_startswith0(cmd, "{") && (idx = bstrchr(cmd, '}')) > 0) {
1414 section = bstr_strip(bstr_splice(cmd, 1, idx));
1415 cmd = bstr_strip(bstr_cut(cmd, idx + 1));
1417 bind_section = get_bind_section(ictx, section);
1419 if (bind_section->cmd_binds) {
1420 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1421 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1422 /* NOTHING */;
1423 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1424 bind = &bind_section->cmd_binds[i];
1425 break;
1430 if (!bind) {
1431 bind_section->cmd_binds = talloc_realloc(bind_section,
1432 bind_section->cmd_binds,
1433 struct cmd_bind, i + 2);
1434 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(struct cmd_bind));
1435 bind = &bind_section->cmd_binds[i];
1437 talloc_free(bind->cmd);
1438 bind->cmd = bstrdup0(bind_section->cmd_binds, cmd);
1439 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1442 static int parse_config(struct input_ctx *ictx, struct bstr data)
1444 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1];
1446 while (data.len) {
1447 struct bstr line = bstr_strip(bstr_getline(data, &data));
1448 if (line.len == 0 || bstr_startswith0(line, "#"))
1449 continue;
1450 struct bstr command;
1451 // Find the key name starting a line
1452 struct bstr keyname = bstr_split(line, WHITESPACE, &command);
1453 command = bstr_strip(command);
1454 if (command.len == 0) {
1455 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1456 "Unfinished key binding: %.*s\n", BSTR_P(line));
1457 continue;
1459 char *name = bstrdup0(NULL, keyname);
1460 if (!get_input_from_name(name, keys)) {
1461 talloc_free(name);
1462 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1463 "Unknown key '%.*s'\n", BSTR_P(keyname));
1464 continue;
1466 talloc_free(name);
1467 bind_keys(ictx, keys, command);
1468 n_binds++;
1471 return n_binds;
1474 static int parse_config_file(struct input_ctx *ictx, char *file)
1476 stream_t *s = open_stream(file, ictx->opts, NULL);
1477 if (!s) {
1478 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s.\n", file);
1479 return 0;
1481 struct bstr res = stream_read_complete(s, NULL, 1000000, 0);
1482 free_stream(s);
1483 if (!res.start) {
1484 mp_msg(MSGT_INPUT, MSGL_ERR, "Could not read input config file %s.\n",
1485 file);
1486 return 0;
1488 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1489 int n_binds = parse_config(ictx, res);
1490 talloc_free(res.start);
1491 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: %d binds\n",
1492 file, n_binds);
1493 return 1;
1496 void mp_input_set_section(struct input_ctx *ictx, char *name)
1498 talloc_free(ictx->section.start);
1499 ictx->section = bstrdup(ictx, name ? bstr(name) : bstr("default"));
1502 struct input_ctx *mp_input_init(struct MPOpts *opts)
1504 struct input_conf *input_conf = &opts->input;
1505 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1506 *ictx = (struct input_ctx){
1507 .key_fifo_size = input_conf->key_fifo_size,
1508 .ar_state = -1,
1509 .ar_delay = input_conf->ar_delay,
1510 .ar_rate = input_conf->ar_rate,
1511 .section = bstrdup(ictx, bstr("default")),
1512 .wakeup_pipe = {-1, -1},
1513 .opts = opts,
1516 #ifdef CONFIG_COCOA
1517 cocoa_events_init(ictx, read_all_fd_events);
1518 #endif
1520 #ifndef __MINGW32__
1521 long ret = pipe(ictx->wakeup_pipe);
1522 for (int i = 0; i < 2 && ret >= 0; i++) {
1523 ret = fcntl(ictx->wakeup_pipe[i], F_GETFL);
1524 if (ret < 0)
1525 break;
1526 ret = fcntl(ictx->wakeup_pipe[i], F_SETFL, ret | O_NONBLOCK);
1528 if (ret < 0)
1529 mp_msg(MSGT_INPUT, MSGL_ERR,
1530 "Failed to initialize wakeup pipe: %s\n", strerror(errno));
1531 else
1532 mp_input_add_key_fd(ictx, ictx->wakeup_pipe[0], true, read_wakeup,
1533 NULL, NULL);
1534 #endif
1536 if (input_conf->default_bindings)
1537 parse_config(ictx, builtin_input_conf);
1539 char *file;
1540 char *config_file = input_conf->config_file;
1541 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1543 if (file && !parse_config_file(ictx, file)) {
1544 // free file if it was allocated by get_path(),
1545 // before it gets overwritten
1546 if (file != config_file)
1547 free(file);
1548 // Try global conf dir
1549 file = MPLAYER_CONFDIR "/input.conf";
1550 if (!parse_config_file(ictx, file))
1551 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1552 "input config\n");
1553 } else {
1554 // free file if it was allocated by get_path()
1555 if (file != config_file)
1556 free(file);
1559 #ifdef CONFIG_JOYSTICK
1560 if (input_conf->use_joystick) {
1561 int fd = mp_input_joystick_init(input_conf->js_dev);
1562 if (fd < 0)
1563 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1564 else
1565 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1566 close, NULL);
1568 #endif
1570 #ifdef CONFIG_LIRC
1571 if (input_conf->use_lirc) {
1572 int fd = mp_input_lirc_init();
1573 if (fd > 0)
1574 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1575 mp_input_lirc_close);
1577 #endif
1579 #ifdef CONFIG_LIRCC
1580 if (input_conf->use_lircc) {
1581 int fd = lircc_init("mplayer", NULL);
1582 if (fd >= 0)
1583 mp_input_add_cmd_fd(ictx, fd, 1, NULL, lircc_cleanup);
1585 #endif
1587 #ifdef CONFIG_APPLE_REMOTE
1588 if (input_conf->use_ar) {
1589 if (mp_input_ar_init() < 0)
1590 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1591 else
1592 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1593 mp_input_ar_close, NULL);
1595 #endif
1597 #ifdef CONFIG_APPLE_IR
1598 if (input_conf->use_ar) {
1599 int fd = mp_input_appleir_init(input_conf->ar_dev);
1600 if (fd < 0)
1601 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1602 else
1603 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1604 close, NULL);
1606 #endif
1608 if (input_conf->in_file) {
1609 int mode = O_RDONLY;
1610 #ifndef __MINGW32__
1611 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1612 // Note that on Windows due to how the API works, using RDONLY should
1613 // be ok.
1614 struct stat st;
1615 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1616 mode = O_RDWR;
1617 mode |= O_NONBLOCK;
1618 #endif
1619 int in_file_fd = open(input_conf->in_file, mode);
1620 if (in_file_fd >= 0)
1621 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, close);
1622 else
1623 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1624 input_conf->in_file, strerror(errno));
1627 return ictx;
1630 void mp_input_uninit(struct input_ctx *ictx)
1632 #ifdef CONFIG_COCOA
1633 cocoa_events_uninit();
1634 #endif
1636 if (!ictx)
1637 return;
1639 for (int i = 0; i < ictx->num_key_fd; i++) {
1640 if (ictx->key_fds[i].close_func)
1641 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1643 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1644 if (ictx->cmd_fds[i].close_func)
1645 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1647 for (int i = 0; i < 2; i++)
1648 if (ictx->wakeup_pipe[i] != -1)
1649 close(ictx->wakeup_pipe[i]);
1650 talloc_free(ictx);
1653 void mp_input_register_options(m_config_t *cfg)
1655 m_config_register_options(cfg, mp_input_opts);
1658 static int print_key_list(m_option_t *cfg, char *optname, char *optparam)
1660 int i;
1661 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
1662 for (i = 0; key_names[i].name != NULL; i++)
1663 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%s\n", key_names[i].name);
1664 return M_OPT_EXIT;
1667 static int print_cmd_list(m_option_t *cfg, char *optname, char *optparam)
1669 const mp_cmd_t *cmd;
1670 int i, j;
1671 const char *type;
1673 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1674 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "%-20.20s", cmd->name);
1675 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type; j++) {
1676 switch (cmd->args[j].type) {
1677 case MP_CMD_ARG_INT:
1678 type = "Integer";
1679 break;
1680 case MP_CMD_ARG_FLOAT:
1681 type = "Float";
1682 break;
1683 case MP_CMD_ARG_STRING:
1684 type = "String";
1685 break;
1686 default:
1687 type = "??";
1689 if (cmd->args[j].optional)
1690 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " [%s]", type);
1691 else
1692 mp_msg(MSGT_CFGPARSER, MSGL_INFO, " %s", type);
1694 mp_msg(MSGT_CFGPARSER, MSGL_INFO, "\n");
1696 return M_OPT_EXIT;
1699 void mp_input_wakeup(struct input_ctx *ictx)
1701 if (ictx->wakeup_pipe[1] >= 0)
1702 write(ictx->wakeup_pipe[1], &(char){0}, 1);
1706 * \param time time to wait for an interruption in milliseconds
1708 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1710 for (int i = 0; ; i++) {
1711 if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds ||
1712 ictx->control_cmd_queue.num_abort_cmds) {
1713 mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to "
1714 "another file. Aborting current processing.\n");
1715 return true;
1717 if (i)
1718 return false;
1719 read_all_events(ictx, time);