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.
27 #include <sys/types.h>
39 #include "osdep/timer.h"
40 #include "libavutil/avstring.h"
41 #include "libavutil/common.h"
49 #include "stream/stream.h"
58 #include <lirc/lircc.h>
64 #include "osdep/cocoa_events.h"
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
77 int input
[MP_MAX_KEY_DOWN
+ 1];
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
[] = {
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
} },
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_SUB_STEP
, "sub_step", { ARG_INT
, OARG_INT(0) } },
125 { MP_CMD_OSD
, "osd", { OARG_INT(-1) } },
126 { MP_CMD_OSD_SHOW_TEXT
, "osd_show_text", { ARG_STRING
, OARG_INT(-1), OARG_INT(0) } },
127 { MP_CMD_OSD_SHOW_PROPERTY_TEXT
, "osd_show_property_text", { ARG_STRING
, OARG_INT(-1), OARG_INT(0) } },
128 { MP_CMD_OSD_SHOW_PROGRESSION
, "osd_show_progression", },
129 { MP_CMD_VOLUME
, "volume", { ARG_FLOAT
, OARG_INT(0) } },
130 { MP_CMD_BALANCE
, "balance", { ARG_FLOAT
, OARG_INT(0) } },
131 { MP_CMD_MUTE
, "mute", { OARG_INT(-1) } },
132 { MP_CMD_CONTRAST
, "contrast", { ARG_INT
, OARG_INT(0) } },
133 { MP_CMD_GAMMA
, "gamma", { ARG_INT
, OARG_INT(0) } },
134 { MP_CMD_BRIGHTNESS
, "brightness", { ARG_INT
, OARG_INT(0) } },
135 { MP_CMD_HUE
, "hue", { ARG_INT
, OARG_INT(0) } },
136 { MP_CMD_SATURATION
, "saturation", { ARG_INT
, OARG_INT(0) } },
137 { MP_CMD_FRAMEDROPPING
, "frame_drop", { OARG_INT(-1) } },
138 { MP_CMD_SUB_POS
, "sub_pos", { ARG_INT
, OARG_INT(0) } },
139 { MP_CMD_SUB_ALIGNMENT
, "sub_alignment", { OARG_INT(-1) } },
140 { MP_CMD_SUB_VISIBILITY
, "sub_visibility", { OARG_INT(-1) } },
141 { MP_CMD_SUB_LOAD
, "sub_load", { ARG_STRING
} },
142 { MP_CMD_SUB_REMOVE
, "sub_remove", { OARG_INT(-1) } },
143 { MP_CMD_SUB_SELECT
, "vobsub_lang", { OARG_INT(-2) } }, // for compatibility
144 { MP_CMD_SUB_SELECT
, "sub_select", { OARG_INT(-2) } },
145 { MP_CMD_SUB_SOURCE
, "sub_source", { OARG_INT(-2) } },
146 { MP_CMD_SUB_VOB
, "sub_vob", { OARG_INT(-2) } },
147 { MP_CMD_SUB_DEMUX
, "sub_demux", { OARG_INT(-2) } },
148 { MP_CMD_SUB_FILE
, "sub_file", { OARG_INT(-2) } },
149 { MP_CMD_SUB_LOG
, "sub_log", },
150 { MP_CMD_SUB_SCALE
, "sub_scale", { ARG_FLOAT
, OARG_INT(0) } },
152 { MP_CMD_ASS_USE_MARGINS
, "ass_use_margins", { OARG_INT(-1) } },
154 { MP_CMD_GET_PERCENT_POS
, "get_percent_pos", },
155 { MP_CMD_GET_TIME_POS
, "get_time_pos", },
156 { MP_CMD_GET_TIME_LENGTH
, "get_time_length", },
157 { MP_CMD_GET_FILENAME
, "get_file_name", },
158 { MP_CMD_GET_VIDEO_CODEC
, "get_video_codec", },
159 { MP_CMD_GET_VIDEO_BITRATE
, "get_video_bitrate", },
160 { MP_CMD_GET_VIDEO_RESOLUTION
, "get_video_resolution", },
161 { MP_CMD_GET_AUDIO_CODEC
, "get_audio_codec", },
162 { MP_CMD_GET_AUDIO_BITRATE
, "get_audio_bitrate", },
163 { MP_CMD_GET_AUDIO_SAMPLES
, "get_audio_samples", },
164 { MP_CMD_GET_META_TITLE
, "get_meta_title", },
165 { MP_CMD_GET_META_ARTIST
, "get_meta_artist", },
166 { MP_CMD_GET_META_ALBUM
, "get_meta_album", },
167 { MP_CMD_GET_META_YEAR
, "get_meta_year", },
168 { MP_CMD_GET_META_COMMENT
, "get_meta_comment", },
169 { MP_CMD_GET_META_TRACK
, "get_meta_track", },
170 { MP_CMD_GET_META_GENRE
, "get_meta_genre", },
171 { MP_CMD_SWITCH_AUDIO
, "switch_audio", { OARG_INT(-1) } },
172 { MP_CMD_SWITCH_ANGLE
, "switch_angle", { OARG_INT(-1) } },
173 { MP_CMD_SWITCH_TITLE
, "switch_title", { OARG_INT(-1) } },
175 { MP_CMD_TV_START_SCAN
, "tv_start_scan", },
176 { MP_CMD_TV_STEP_CHANNEL
, "tv_step_channel", { ARG_INT
} },
177 { MP_CMD_TV_STEP_NORM
, "tv_step_norm", },
178 { MP_CMD_TV_STEP_CHANNEL_LIST
, "tv_step_chanlist", },
179 { MP_CMD_TV_SET_CHANNEL
, "tv_set_channel", { ARG_STRING
} },
180 { MP_CMD_TV_LAST_CHANNEL
, "tv_last_channel", },
181 { MP_CMD_TV_SET_FREQ
, "tv_set_freq", { ARG_FLOAT
} },
182 { MP_CMD_TV_STEP_FREQ
, "tv_step_freq", { ARG_FLOAT
} },
183 { MP_CMD_TV_SET_NORM
, "tv_set_norm", { ARG_STRING
} },
184 { MP_CMD_TV_SET_BRIGHTNESS
, "tv_set_brightness", { ARG_INT
, OARG_INT(1) } },
185 { MP_CMD_TV_SET_CONTRAST
, "tv_set_contrast", { ARG_INT
, OARG_INT(1) } },
186 { MP_CMD_TV_SET_HUE
, "tv_set_hue", { ARG_INT
, OARG_INT(1) } },
187 { MP_CMD_TV_SET_SATURATION
, "tv_set_saturation", { ARG_INT
, OARG_INT(1) } },
189 { MP_CMD_SUB_FORCED_ONLY
, "forced_subs_only", { OARG_INT(-1) } },
191 { MP_CMD_DVB_SET_CHANNEL
, "dvb_set_channel", { ARG_INT
, ARG_INT
} },
193 { MP_CMD_SWITCH_RATIO
, "switch_ratio", { OARG_FLOAT(0) } },
194 { MP_CMD_VO_FULLSCREEN
, "vo_fullscreen", { OARG_INT(-1) } },
195 { MP_CMD_VO_ONTOP
, "vo_ontop", { OARG_INT(-1) } },
196 { MP_CMD_VO_ROOTWIN
, "vo_rootwin", { OARG_INT(-1) } },
197 { MP_CMD_VO_BORDER
, "vo_border", { OARG_INT(-1) } },
198 { MP_CMD_SCREENSHOT
, "screenshot", { OARG_INT(0), OARG_INT(0) } },
199 { MP_CMD_PANSCAN
, "panscan", { ARG_FLOAT
, OARG_INT(0) } },
200 { MP_CMD_SWITCH_VSYNC
, "switch_vsync", { OARG_INT(0) } },
201 { MP_CMD_LOADFILE
, "loadfile", { ARG_STRING
, OARG_INT(0) } },
202 { MP_CMD_LOADLIST
, "loadlist", { ARG_STRING
, OARG_INT(0) } },
203 { MP_CMD_RUN
, "run", { ARG_STRING
} },
204 { MP_CMD_CAPTURING
, "capturing", },
205 { MP_CMD_VF_CHANGE_RECTANGLE
, "change_rectangle", { ARG_INT
, ARG_INT
} },
206 { MP_CMD_TV_TELETEXT_ADD_DEC
, "teletext_add_dec", { ARG_STRING
} },
207 { MP_CMD_TV_TELETEXT_GO_LINK
, "teletext_go_link", { ARG_INT
} },
210 { MP_CMD_DVDNAV
, "dvdnav", { ARG_STRING
} },
213 { MP_CMD_GET_VO_FULLSCREEN
, "get_vo_fullscreen", },
214 { MP_CMD_GET_SUB_VISIBILITY
, "get_sub_visibility", },
215 { MP_CMD_KEYDOWN_EVENTS
, "key_down_event", { ARG_INT
} },
216 { MP_CMD_SET_PROPERTY
, "set_property", { ARG_STRING
, ARG_STRING
} },
217 { MP_CMD_SET_PROPERTY_OSD
, "set_property_osd", { ARG_STRING
, ARG_STRING
} },
218 { MP_CMD_GET_PROPERTY
, "get_property", { ARG_STRING
} },
219 { MP_CMD_STEP_PROPERTY
, "step_property", { ARG_STRING
, OARG_FLOAT(0) } },
220 { MP_CMD_STEP_PROPERTY_OSD
, "step_property_osd", { ARG_STRING
, OARG_FLOAT(0) } },
222 { MP_CMD_SEEK_CHAPTER
, "seek_chapter", { ARG_INT
, OARG_INT(0) } },
223 { MP_CMD_SET_MOUSE_POS
, "set_mouse_pos", { ARG_INT
, ARG_INT
} },
225 { MP_CMD_AF_SWITCH
, "af_switch", { ARG_STRING
} },
226 { MP_CMD_AF_ADD
, "af_add", { ARG_STRING
} },
227 { MP_CMD_AF_DEL
, "af_del", { ARG_STRING
} },
228 { MP_CMD_AF_CLR
, "af_clr", },
229 { MP_CMD_AF_CMDLINE
, "af_cmdline", { ARG_STRING
, ARG_STRING
} },
233 /// The names of the keys as used in input.conf
234 /// If you add some new keys, you also need to add them here
236 static const struct key_name key_names
[] = {
239 { KEY_ENTER
, "ENTER" },
241 { KEY_BACKSPACE
, "BS" },
242 { KEY_DELETE
, "DEL" },
243 { KEY_INSERT
, "INS" },
244 { KEY_HOME
, "HOME" },
246 { KEY_PAGE_UP
, "PGUP" },
247 { KEY_PAGE_DOWN
, "PGDWN" },
249 { KEY_PRINT
, "PRINT" },
250 { KEY_RIGHT
, "RIGHT" },
251 { KEY_LEFT
, "LEFT" },
252 { KEY_DOWN
, "DOWN" },
276 { KEY_KPDEL
, "KP_DEL" },
277 { KEY_KPDEC
, "KP_DEC" },
278 { KEY_KPINS
, "KP_INS" },
279 { KEY_KPENTER
, "KP_ENTER" },
280 { MOUSE_BTN0
, "MOUSE_BTN0" },
281 { MOUSE_BTN1
, "MOUSE_BTN1" },
282 { MOUSE_BTN2
, "MOUSE_BTN2" },
283 { MOUSE_BTN3
, "MOUSE_BTN3" },
284 { MOUSE_BTN4
, "MOUSE_BTN4" },
285 { MOUSE_BTN5
, "MOUSE_BTN5" },
286 { MOUSE_BTN6
, "MOUSE_BTN6" },
287 { MOUSE_BTN7
, "MOUSE_BTN7" },
288 { MOUSE_BTN8
, "MOUSE_BTN8" },
289 { MOUSE_BTN9
, "MOUSE_BTN9" },
290 { MOUSE_BTN10
, "MOUSE_BTN10" },
291 { MOUSE_BTN11
, "MOUSE_BTN11" },
292 { MOUSE_BTN12
, "MOUSE_BTN12" },
293 { MOUSE_BTN13
, "MOUSE_BTN13" },
294 { MOUSE_BTN14
, "MOUSE_BTN14" },
295 { MOUSE_BTN15
, "MOUSE_BTN15" },
296 { MOUSE_BTN16
, "MOUSE_BTN16" },
297 { MOUSE_BTN17
, "MOUSE_BTN17" },
298 { MOUSE_BTN18
, "MOUSE_BTN18" },
299 { MOUSE_BTN19
, "MOUSE_BTN19" },
300 { MOUSE_BTN0_DBL
, "MOUSE_BTN0_DBL" },
301 { MOUSE_BTN1_DBL
, "MOUSE_BTN1_DBL" },
302 { MOUSE_BTN2_DBL
, "MOUSE_BTN2_DBL" },
303 { MOUSE_BTN3_DBL
, "MOUSE_BTN3_DBL" },
304 { MOUSE_BTN4_DBL
, "MOUSE_BTN4_DBL" },
305 { MOUSE_BTN5_DBL
, "MOUSE_BTN5_DBL" },
306 { MOUSE_BTN6_DBL
, "MOUSE_BTN6_DBL" },
307 { MOUSE_BTN7_DBL
, "MOUSE_BTN7_DBL" },
308 { MOUSE_BTN8_DBL
, "MOUSE_BTN8_DBL" },
309 { MOUSE_BTN9_DBL
, "MOUSE_BTN9_DBL" },
310 { MOUSE_BTN10_DBL
, "MOUSE_BTN10_DBL" },
311 { MOUSE_BTN11_DBL
, "MOUSE_BTN11_DBL" },
312 { MOUSE_BTN12_DBL
, "MOUSE_BTN12_DBL" },
313 { MOUSE_BTN13_DBL
, "MOUSE_BTN13_DBL" },
314 { MOUSE_BTN14_DBL
, "MOUSE_BTN14_DBL" },
315 { MOUSE_BTN15_DBL
, "MOUSE_BTN15_DBL" },
316 { MOUSE_BTN16_DBL
, "MOUSE_BTN16_DBL" },
317 { MOUSE_BTN17_DBL
, "MOUSE_BTN17_DBL" },
318 { MOUSE_BTN18_DBL
, "MOUSE_BTN18_DBL" },
319 { MOUSE_BTN19_DBL
, "MOUSE_BTN19_DBL" },
320 { JOY_AXIS1_MINUS
, "JOY_UP" },
321 { JOY_AXIS1_PLUS
, "JOY_DOWN" },
322 { JOY_AXIS0_MINUS
, "JOY_LEFT" },
323 { JOY_AXIS0_PLUS
, "JOY_RIGHT" },
325 { JOY_AXIS0_PLUS
, "JOY_AXIS0_PLUS" },
326 { JOY_AXIS0_MINUS
, "JOY_AXIS0_MINUS" },
327 { JOY_AXIS1_PLUS
, "JOY_AXIS1_PLUS" },
328 { JOY_AXIS1_MINUS
, "JOY_AXIS1_MINUS" },
329 { JOY_AXIS2_PLUS
, "JOY_AXIS2_PLUS" },
330 { JOY_AXIS2_MINUS
, "JOY_AXIS2_MINUS" },
331 { JOY_AXIS3_PLUS
, "JOY_AXIS3_PLUS" },
332 { JOY_AXIS3_MINUS
, "JOY_AXIS3_MINUS" },
333 { JOY_AXIS4_PLUS
, "JOY_AXIS4_PLUS" },
334 { JOY_AXIS4_MINUS
, "JOY_AXIS4_MINUS" },
335 { JOY_AXIS5_PLUS
, "JOY_AXIS5_PLUS" },
336 { JOY_AXIS5_MINUS
, "JOY_AXIS5_MINUS" },
337 { JOY_AXIS6_PLUS
, "JOY_AXIS6_PLUS" },
338 { JOY_AXIS6_MINUS
, "JOY_AXIS6_MINUS" },
339 { JOY_AXIS7_PLUS
, "JOY_AXIS7_PLUS" },
340 { JOY_AXIS7_MINUS
, "JOY_AXIS7_MINUS" },
341 { JOY_AXIS8_PLUS
, "JOY_AXIS8_PLUS" },
342 { JOY_AXIS8_MINUS
, "JOY_AXIS8_MINUS" },
343 { JOY_AXIS9_PLUS
, "JOY_AXIS9_PLUS" },
344 { JOY_AXIS9_MINUS
, "JOY_AXIS9_MINUS" },
346 { JOY_BTN0
, "JOY_BTN0" },
347 { JOY_BTN1
, "JOY_BTN1" },
348 { JOY_BTN2
, "JOY_BTN2" },
349 { JOY_BTN3
, "JOY_BTN3" },
350 { JOY_BTN4
, "JOY_BTN4" },
351 { JOY_BTN5
, "JOY_BTN5" },
352 { JOY_BTN6
, "JOY_BTN6" },
353 { JOY_BTN7
, "JOY_BTN7" },
354 { JOY_BTN8
, "JOY_BTN8" },
355 { JOY_BTN9
, "JOY_BTN9" },
357 { AR_PLAY
, "AR_PLAY" },
358 { AR_PLAY_HOLD
, "AR_PLAY_HOLD" },
359 { AR_NEXT
, "AR_NEXT" },
360 { AR_NEXT_HOLD
, "AR_NEXT_HOLD" },
361 { AR_PREV
, "AR_PREV" },
362 { AR_PREV_HOLD
, "AR_PREV_HOLD" },
363 { AR_MENU
, "AR_MENU" },
364 { AR_MENU_HOLD
, "AR_MENU_HOLD" },
365 { AR_VUP
, "AR_VUP" },
366 { AR_VDOWN
, "AR_VDOWN" },
368 { KEY_POWER
, "POWER" },
369 { KEY_MENU
, "MENU" },
370 { KEY_PLAY
, "PLAY" },
371 { KEY_PAUSE
, "PAUSE" },
372 { KEY_PLAYPAUSE
, "PLAYPAUSE" },
373 { KEY_STOP
, "STOP" },
374 { KEY_FORWARD
, "FORWARD" },
375 { KEY_REWIND
, "REWIND" },
376 { KEY_NEXT
, "NEXT" },
377 { KEY_PREV
, "PREV" },
378 { KEY_VOLUME_UP
, "VOLUME_UP" },
379 { KEY_VOLUME_DOWN
, "VOLUME_DOWN" },
380 { KEY_MUTE
, "MUTE" },
382 // These are kept for backward compatibility
383 { KEY_PAUSE
, "XF86_PAUSE" },
384 { KEY_STOP
, "XF86_STOP" },
385 { KEY_PREV
, "XF86_PREV" },
386 { KEY_NEXT
, "XF86_NEXT" },
388 { KEY_CLOSE_WIN
, "CLOSE_WIN" },
393 struct key_name modifier_names
[] = {
394 { KEY_MODIFIER_SHIFT
, "Shift" },
395 { KEY_MODIFIER_CTRL
, "Ctrl" },
396 { KEY_MODIFIER_ALT
, "Alt" },
397 { KEY_MODIFIER_META
, "Meta" },
401 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
403 #ifndef MP_MAX_KEY_FD
404 #define MP_MAX_KEY_FD 10
407 #ifndef MP_MAX_CMD_FD
408 #define MP_MAX_CMD_FD 10
414 int (*key
)(void *ctx
, int fd
);
415 int (*cmd
)(int fd
, char *dest
, int size
);
417 int (*close_func
)(int fd
);
422 unsigned got_cmd
: 1;
423 unsigned no_select
: 1;
424 // These fields are for the cmd fds.
429 struct cmd_bind_section
{
430 struct cmd_bind
*cmd_binds
;
432 struct cmd_bind_section
*next
;
436 struct mp_cmd
*first
;
446 unsigned int last_ar
;
448 unsigned int ar_delay
;
449 unsigned int ar_rate
;
450 // Maximum number of queued commands from keypresses (limit to avoid
451 // repeated slow commands piling up)
454 // these are the keys currently down
455 int key_down
[MP_MAX_KEY_DOWN
];
456 unsigned int num_key_down
;
457 unsigned int last_key_down
;
459 // List of command binding sections
460 struct cmd_bind_section
*cmd_bind_sections
;
461 // Name of currently used command section
464 // Used to track whether we managed to read something while checking
465 // events sources. If yes, the sources may have more queued.
468 struct input_fd key_fds
[MP_MAX_KEY_FD
];
469 unsigned int num_key_fd
;
471 struct input_fd cmd_fds
[MP_MAX_CMD_FD
];
472 unsigned int num_cmd_fd
;
474 struct cmd_queue key_cmd_queue
;
475 struct cmd_queue control_cmd_queue
;
483 int async_quit_request
;
485 static int print_key_list(m_option_t
*cfg
, char *optname
, char *optparam
);
486 static int print_cmd_list(m_option_t
*cfg
, char *optname
, char *optparam
);
488 // Our command line options
489 static const m_option_t input_conf
[] = {
490 OPT_STRING("conf", input
.config_file
, CONF_GLOBAL
, OPTDEF_STR("input.conf")),
491 OPT_INT("ar-delay", input
.ar_delay
, CONF_GLOBAL
),
492 OPT_INT("ar-rate", input
.ar_rate
, CONF_GLOBAL
),
493 { "keylist", print_key_list
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
494 { "cmdlist", print_cmd_list
, CONF_TYPE_PRINT_FUNC
, CONF_NOCFG
},
495 OPT_STRING("js-dev", input
.js_dev
, CONF_GLOBAL
),
496 OPT_STRING("ar-dev", input
.ar_dev
, CONF_GLOBAL
),
497 OPT_STRING("file", input
.in_file
, CONF_GLOBAL
),
498 OPT_MAKE_FLAGS("default-bindings", input
.default_bindings
, CONF_GLOBAL
),
499 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
502 static const m_option_t mp_input_opts
[] = {
503 { "input", (void *)&input_conf
, CONF_TYPE_SUBCONFIG
, 0, 0, 0, NULL
},
504 OPT_MAKE_FLAGS("joystick", input
.use_joystick
, CONF_GLOBAL
),
505 OPT_MAKE_FLAGS("lirc", input
.use_lirc
, CONF_GLOBAL
),
506 OPT_MAKE_FLAGS("lircc", input
.use_lircc
, CONF_GLOBAL
),
507 OPT_MAKE_FLAGS("ar", input
.use_ar
, CONF_GLOBAL
),
508 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
511 static int default_cmd_func(int fd
, char *buf
, int l
);
513 // Encode the unicode codepoint as UTF-8, and append to the end of the
515 static char *append_utf8_buffer(char *buffer
, uint32_t codepoint
)
520 PUT_UTF8(codepoint
, tmp
, *output
++ = tmp
;);
521 return talloc_strndup_append_buffer(buffer
, data
, output
- data
);
524 static char *get_key_name(int key
, char *ret
)
526 for (int i
= 0; modifier_names
[i
].name
; i
++) {
527 if (modifier_names
[i
].key
& key
) {
528 ret
= talloc_asprintf_append_buffer(ret
, "%s+",
529 modifier_names
[i
].name
);
530 key
-= modifier_names
[i
].key
;
533 for (int i
= 0; key_names
[i
].name
!= NULL
; i
++) {
534 if (key_names
[i
].key
== key
)
535 return talloc_asprintf_append_buffer(ret
, "%s", key_names
[i
].name
);
538 // printable, and valid unicode range
539 if (key
>= 32 && key
<= 0x10FFFF)
540 return append_utf8_buffer(ret
, key
);
542 // Print the hex key code
543 return talloc_asprintf_append_buffer(ret
, "%#-8x", key
);
546 static char *get_key_combo_name(int *keys
, int max
)
548 char *ret
= talloc_strdup(NULL
, "");
550 ret
= get_key_name(*keys
, ret
);
551 if (--max
&& *++keys
)
552 talloc_asprintf_append_buffer(ret
, "-");
559 static bool is_abort_cmd(int cmd_id
)
563 case MP_CMD_PLAY_TREE_STEP
:
564 case MP_CMD_PLAY_TREE_UP_STEP
:
565 case MP_CMD_PLAY_ALT_SRC_STEP
:
571 static void queue_pop(struct cmd_queue
*queue
)
573 assert(queue
->num_cmds
> 0);
574 struct mp_cmd
*cmd
= queue
->first
;
575 queue
->first
= cmd
->queue_next
;
577 queue
->num_abort_cmds
-= is_abort_cmd(cmd
->id
);
580 static void queue_add(struct cmd_queue
*queue
, struct mp_cmd
*cmd
,
583 if (!queue
->num_cmds
) {
586 } else if (at_head
) {
587 queue
->first
->queue_prev
= cmd
;
588 cmd
->queue_next
= queue
->first
;
591 queue
->last
->queue_next
= cmd
;
592 cmd
->queue_prev
= queue
->last
;
596 queue
->num_abort_cmds
+= is_abort_cmd(cmd
->id
);
599 int mp_input_add_cmd_fd(struct input_ctx
*ictx
, int fd
, int select
,
600 int read_func(int fd
, char *dest
, int size
),
601 int close_func(int fd
))
603 if (ictx
->num_cmd_fd
== MP_MAX_CMD_FD
) {
604 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Too many command file descriptors, "
605 "cannot register file descriptor %d.\n", fd
);
608 if (select
&& fd
< 0) {
609 mp_msg(MSGT_INPUT
, MSGL_ERR
,
610 "Invalid fd %d in mp_input_add_cmd_fd", fd
);
614 ictx
->cmd_fds
[ictx
->num_cmd_fd
] = (struct input_fd
){
616 .read_func
.cmd
= read_func
? read_func
: default_cmd_func
,
617 .close_func
= close_func
,
625 void mp_input_rm_cmd_fd(struct input_ctx
*ictx
, int fd
)
627 struct input_fd
*cmd_fds
= ictx
->cmd_fds
;
630 for (i
= 0; i
< ictx
->num_cmd_fd
; i
++) {
631 if (cmd_fds
[i
].fd
== fd
)
634 if (i
== ictx
->num_cmd_fd
)
636 if (cmd_fds
[i
].close_func
)
637 cmd_fds
[i
].close_func(cmd_fds
[i
].fd
);
638 talloc_free(cmd_fds
[i
].buffer
);
640 if (i
+ 1 < ictx
->num_cmd_fd
)
641 memmove(&cmd_fds
[i
], &cmd_fds
[i
+ 1],
642 (ictx
->num_cmd_fd
- i
- 1) * sizeof(struct input_fd
));
646 void mp_input_rm_key_fd(struct input_ctx
*ictx
, int fd
)
648 struct input_fd
*key_fds
= ictx
->key_fds
;
651 for (i
= 0; i
< ictx
->num_key_fd
; i
++) {
652 if (key_fds
[i
].fd
== fd
)
655 if (i
== ictx
->num_key_fd
)
657 if (key_fds
[i
].close_func
)
658 key_fds
[i
].close_func(key_fds
[i
].fd
);
660 if (i
+ 1 < ictx
->num_key_fd
)
661 memmove(&key_fds
[i
], &key_fds
[i
+ 1],
662 (ictx
->num_key_fd
- i
- 1) * sizeof(struct input_fd
));
666 int mp_input_add_key_fd(struct input_ctx
*ictx
, int fd
, int select
,
667 int read_func(void *ctx
, int fd
),
668 int close_func(int fd
), void *ctx
)
670 if (ictx
->num_key_fd
== MP_MAX_KEY_FD
) {
671 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Too many key file descriptors, "
672 "cannot register file descriptor %d.\n", fd
);
675 if (select
&& fd
< 0) {
676 mp_msg(MSGT_INPUT
, MSGL_ERR
,
677 "Invalid fd %d in mp_input_add_key_fd", fd
);
681 ictx
->key_fds
[ictx
->num_key_fd
] = (struct input_fd
){
683 .read_func
.key
= read_func
,
684 .close_func
= close_func
,
685 .no_select
= !select
,
693 mp_cmd_t
*mp_input_parse_cmd(char *str
)
698 const mp_cmd_t
*cmd_def
;
700 // Ignore heading spaces.
701 while (str
[0] == ' ' || str
[0] == '\t')
704 if (strncmp(str
, "pausing ", 8) == 0) {
707 } else if (strncmp(str
, "pausing_keep ", 13) == 0) {
710 } else if (strncmp(str
, "pausing_toggle ", 15) == 0) {
713 } else if (strncmp(str
, "pausing_keep_force ", 19) == 0) {
718 ptr
= str
+ strcspn(str
, "\t ");
727 for (i
= 0; mp_cmds
[i
].name
!= NULL
; i
++) {
728 if (strncasecmp(mp_cmds
[i
].name
, str
, l
) == 0)
732 if (mp_cmds
[i
].name
== NULL
)
735 cmd_def
= &mp_cmds
[i
];
737 mp_cmd_t
*cmd
= talloc_ptrtype(NULL
, cmd
);
740 .name
= talloc_strdup(cmd
, cmd_def
->name
),
746 for (i
= 0; ptr
&& i
< MP_CMD_MAX_ARGS
; i
++) {
747 while (ptr
[0] != ' ' && ptr
[0] != '\t' && ptr
[0] != '\0')
751 while (ptr
[0] == ' ' || ptr
[0] == '\t')
753 if (ptr
[0] == '\0' || ptr
[0] == '#')
755 cmd
->args
[i
].type
= cmd_def
->args
[i
].type
;
756 switch (cmd_def
->args
[i
].type
) {
759 cmd
->args
[i
].v
.i
= atoi(ptr
);
761 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Command %s: argument %d "
762 "isn't an integer.\n", cmd_def
->name
, i
+ 1);
766 case MP_CMD_ARG_FLOAT
:
768 cmd
->args
[i
].v
.f
= atof(ptr
);
770 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Command %s: argument %d "
771 "isn't a float.\n", cmd_def
->name
, i
+ 1);
775 case MP_CMD_ARG_STRING
: {
777 if (*ptr
== '\'' || *ptr
== '"')
779 char *argptr
= talloc_size(cmd
, strlen(ptr
) + 1);
780 cmd
->args
[i
].v
.s
= argptr
;
785 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Command %s: argument %d is "
786 "unterminated.\n", cmd_def
->name
, i
+ 1);
803 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Unknown argument %d\n", i
);
809 for (min_args
= 0; min_args
< MP_CMD_MAX_ARGS
810 && cmd_def
->args
[min_args
].type
811 && !cmd_def
->args
[min_args
].optional
; min_args
++);
812 if (cmd
->nargs
< min_args
) {
813 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Command \"%s\" requires at least %d "
814 "arguments, we found only %d so far.\n", cmd_def
->name
,
815 min_args
, cmd
->nargs
);
819 for (; i
< MP_CMD_MAX_ARGS
&& cmd_def
->args
[i
].type
; i
++) {
820 memcpy(&cmd
->args
[i
], &cmd_def
->args
[i
], sizeof(struct mp_cmd_arg
));
821 if (cmd_def
->args
[i
].type
== MP_CMD_ARG_STRING
822 && cmd_def
->args
[i
].v
.s
!= NULL
)
823 cmd
->args
[i
].v
.s
= talloc_strdup(cmd
, cmd_def
->args
[i
].v
.s
);
826 if (i
< MP_CMD_MAX_ARGS
)
827 cmd
->args
[i
].type
= 0;
836 #define MP_CMD_MAX_SIZE 4096
838 static int read_cmd(struct input_fd
*mp_fd
, char **ret
)
843 // Allocate the buffer if it doesn't exist
844 if (!mp_fd
->buffer
) {
845 mp_fd
->buffer
= talloc_size(NULL
, MP_CMD_MAX_SIZE
);
847 mp_fd
->size
= MP_CMD_MAX_SIZE
;
850 // Get some data if needed/possible
851 while (!mp_fd
->got_cmd
&& !mp_fd
->eof
&& (mp_fd
->size
- mp_fd
->pos
> 1)) {
852 int r
= mp_fd
->read_func
.cmd(mp_fd
->fd
, mp_fd
->buffer
+ mp_fd
->pos
,
853 mp_fd
->size
- 1 - mp_fd
->pos
);
859 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Error while reading "
860 "command file descriptor %d: %s\n",
861 mp_fd
->fd
, strerror(errno
));
862 case MP_INPUT_NOTHING
:
881 mp_fd
->buffer
[mp_fd
->pos
] = '\0';
882 end
= strchr(mp_fd
->buffer
, '\r');
885 end
= strchr(mp_fd
->buffer
, '\n');
888 // If buffer is full we must drop all until the next \n
889 if (mp_fd
->size
- mp_fd
->pos
<= 1) {
890 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Command buffer of file "
891 "descriptor %d is full: dropping content.\n",
898 // We already have a cmd : set the got_cmd flag
904 l
= end
- mp_fd
->buffer
;
906 // Not dropping : put the cmd in ret
908 *ret
= talloc_strndup(NULL
, mp_fd
->buffer
, l
);
912 memmove(mp_fd
->buffer
, end
+ 1, mp_fd
->pos
);
918 return MP_INPUT_NOTHING
;
921 static int default_cmd_func(int fd
, char *buf
, int l
)
924 int r
= read(fd
, buf
, l
);
929 else if (errno
== EAGAIN
)
930 return MP_INPUT_NOTHING
;
931 return MP_INPUT_ERROR
;
938 static int read_wakeup(void *ctx
, int fd
)
941 read(fd
, buf
, sizeof(buf
));
942 return MP_INPUT_NOTHING
;
946 static struct cmd_bind_section
*get_bind_section(struct input_ctx
*ictx
,
949 struct cmd_bind_section
*bind_section
= ictx
->cmd_bind_sections
;
951 while (bind_section
) {
952 if (bstrcmp(section
, bind_section
->section
) == 0)
954 if (bind_section
->next
== NULL
)
956 bind_section
= bind_section
->next
;
959 bind_section
->next
= talloc_ptrtype(ictx
, bind_section
->next
);
960 bind_section
= bind_section
->next
;
962 ictx
->cmd_bind_sections
= talloc_ptrtype(ictx
, ictx
->cmd_bind_sections
);
963 bind_section
= ictx
->cmd_bind_sections
;
965 bind_section
->cmd_binds
= NULL
;
966 bind_section
->section
= bstrdup(bind_section
, section
);
967 bind_section
->next
= NULL
;
971 static char *find_bind_for_key(struct input_ctx
*ictx
, struct bstr section
,
976 struct cmd_bind_section
*s
= get_bind_section(ictx
, section
);
977 struct cmd_bind
*binds
= s
->cmd_binds
;
981 for (j
= 0; binds
[j
].cmd
!= NULL
; j
++) {
983 for (s
= 0; s
< n
&& binds
[j
].input
[s
] != 0; s
++) {
984 if (binds
[j
].input
[s
] != keys
[s
]) {
989 if (found
&& binds
[j
].input
[s
] == 0 && s
== n
)
995 static mp_cmd_t
*get_cmd_from_keys(struct input_ctx
*ictx
, int n
, int *keys
)
997 char *cmd
= find_bind_for_key(ictx
, ictx
->section
, n
, keys
);
999 cmd
= find_bind_for_key(ictx
, bstr("default"), n
, keys
);
1001 char *key_buf
= get_key_combo_name(keys
, n
);
1002 mp_tmsg(MSGT_INPUT
, MSGL_WARN
,
1003 "No bind found for key '%s'.\n", key_buf
);
1004 talloc_free(key_buf
);
1007 if (strcmp(cmd
, "ignore") == 0)
1009 struct mp_cmd
*ret
= mp_input_parse_cmd(cmd
);
1011 char *key_buf
= get_key_combo_name(keys
, n
);
1012 mp_tmsg(MSGT_INPUT
, MSGL_ERR
,
1013 "Invalid command for bound key '%s': '%s'\n", key_buf
, cmd
);
1014 talloc_free(key_buf
);
1020 static mp_cmd_t
*interpret_key(struct input_ctx
*ictx
, int code
)
1025 /* On normal keyboards shift changes the character code of non-special
1026 * keys, so don't count the modifier separately for those. In other words
1027 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1028 * shift modifier is still kept for special keys like arrow keys.
1030 int unmod
= code
& ~KEY_MODIFIER_MASK
;
1031 if (unmod
>= 32 && unmod
< MP_KEY_BASE
)
1032 code
&= ~KEY_MODIFIER_SHIFT
;
1034 if (code
& MP_KEY_DOWN
) {
1035 if (ictx
->num_key_down
>= MP_MAX_KEY_DOWN
) {
1036 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Too many key down events "
1037 "at the same time\n");
1040 code
&= ~MP_KEY_DOWN
;
1041 // Check if we don't already have this key as pushed
1042 for (j
= 0; j
< ictx
->num_key_down
; j
++) {
1043 if (ictx
->key_down
[j
] == code
)
1046 if (j
!= ictx
->num_key_down
)
1048 ictx
->key_down
[ictx
->num_key_down
] = code
;
1049 ictx
->num_key_down
++;
1050 ictx
->last_key_down
= GetTimer();
1054 // button released or press of key with no separate down/up events
1055 for (j
= 0; j
< ictx
->num_key_down
; j
++) {
1056 if (ictx
->key_down
[j
] == code
)
1059 bool doubleclick
= code
>= MOUSE_BTN0_DBL
&& code
< MOUSE_BTN_DBL_END
;
1061 int btn
= code
- MOUSE_BTN0_DBL
+ MOUSE_BTN0
;
1062 if (!ictx
->num_key_down
1063 || ictx
->key_down
[ictx
->num_key_down
- 1] != btn
)
1065 j
= ictx
->num_key_down
- 1;
1066 ictx
->key_down
[j
] = code
;
1068 if (j
== ictx
->num_key_down
) { // was not already down; add temporarily
1069 if (ictx
->num_key_down
> MP_MAX_KEY_DOWN
) {
1070 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Too many key down events "
1071 "at the same time\n");
1074 ictx
->key_down
[ictx
->num_key_down
] = code
;
1075 ictx
->num_key_down
++;
1076 ictx
->last_key_down
= 1;
1078 // Interpret only maximal point of multibutton event
1079 ret
= ictx
->last_key_down
?
1080 get_cmd_from_keys(ictx
, ictx
->num_key_down
, ictx
->key_down
)
1083 ictx
->key_down
[j
] = code
- MOUSE_BTN0_DBL
+ MOUSE_BTN0
;
1087 if (j
+ 1 < ictx
->num_key_down
)
1088 memmove(&ictx
->key_down
[j
], &ictx
->key_down
[j
+ 1],
1089 (ictx
->num_key_down
- (j
+ 1)) * sizeof(int));
1090 ictx
->num_key_down
--;
1091 ictx
->last_key_down
= 0;
1092 ictx
->ar_state
= -1;
1093 mp_cmd_free(ictx
->ar_cmd
);
1094 ictx
->ar_cmd
= NULL
;
1098 static mp_cmd_t
*check_autorepeat(struct input_ctx
*ictx
)
1100 // No input : autorepeat ?
1101 if (ictx
->ar_rate
> 0 && ictx
->ar_state
>= 0 && ictx
->num_key_down
> 0
1102 && !(ictx
->key_down
[ictx
->num_key_down
- 1] & MP_NO_REPEAT_KEY
)) {
1103 unsigned int t
= GetTimer();
1104 // First time : wait delay
1105 if (ictx
->ar_state
== 0
1106 && (t
- ictx
->last_key_down
) >= ictx
->ar_delay
* 1000) {
1107 ictx
->ar_cmd
= get_cmd_from_keys(ictx
, ictx
->num_key_down
,
1109 if (!ictx
->ar_cmd
) {
1110 ictx
->ar_state
= -1;
1115 return mp_cmd_clone(ictx
->ar_cmd
);
1116 // Then send rate / sec event
1117 } else if (ictx
->ar_state
== 1
1118 && (t
- ictx
->last_ar
) >= 1000000 / ictx
->ar_rate
) {
1120 return mp_cmd_clone(ictx
->ar_cmd
);
1126 void mp_input_feed_key(struct input_ctx
*ictx
, int code
)
1128 ictx
->got_new_events
= true;
1129 if (code
== MP_INPUT_RELEASE_ALL
) {
1130 memset(ictx
->key_down
, 0, sizeof(ictx
->key_down
));
1131 ictx
->num_key_down
= 0;
1132 ictx
->last_key_down
= 0;
1135 struct mp_cmd
*cmd
= interpret_key(ictx
, code
);
1138 struct cmd_queue
*queue
= &ictx
->key_cmd_queue
;
1139 if (queue
->num_cmds
>= ictx
->key_fifo_size
&&
1140 (!is_abort_cmd(cmd
->id
) || queue
->num_abort_cmds
)) {
1144 queue_add(queue
, cmd
, false);
1147 static void read_cmd_fd(struct input_ctx
*ictx
, struct input_fd
*cmd_fd
)
1151 while ((r
= read_cmd(cmd_fd
, &text
)) >= 0) {
1152 ictx
->got_new_events
= true;
1153 struct mp_cmd
*cmd
= mp_input_parse_cmd(text
);
1156 queue_add(&ictx
->control_cmd_queue
, cmd
, false);
1157 if (!cmd_fd
->got_cmd
)
1160 if (r
== MP_INPUT_ERROR
)
1161 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Error on command file descriptor %d\n",
1163 else if (r
== MP_INPUT_DEAD
)
1164 cmd_fd
->dead
= true;
1167 static void read_key_fd(struct input_ctx
*ictx
, struct input_fd
*key_fd
)
1169 int code
= key_fd
->read_func
.key(key_fd
->ctx
, key_fd
->fd
);
1170 if (code
>= 0 || code
== MP_INPUT_RELEASE_ALL
) {
1171 mp_input_feed_key(ictx
, code
);
1175 if (code
== MP_INPUT_ERROR
)
1176 mp_tmsg(MSGT_INPUT
, MSGL_ERR
,
1177 "Error on key input file descriptor %d\n", key_fd
->fd
);
1178 else if (code
== MP_INPUT_DEAD
) {
1179 mp_tmsg(MSGT_INPUT
, MSGL_ERR
,
1180 "Dead key input on file descriptor %d\n", key_fd
->fd
);
1181 key_fd
->dead
= true;
1186 * \param time time to wait at most for an event in milliseconds
1188 static void read_events(struct input_ctx
*ictx
, int time
)
1190 ictx
->got_new_events
= false;
1191 struct input_fd
*key_fds
= ictx
->key_fds
;
1192 struct input_fd
*cmd_fds
= ictx
->cmd_fds
;
1193 for (int i
= 0; i
< ictx
->num_key_fd
; i
++)
1194 if (key_fds
[i
].dead
) {
1195 mp_input_rm_key_fd(ictx
, key_fds
[i
].fd
);
1197 } else if (time
&& key_fds
[i
].no_select
)
1198 read_key_fd(ictx
, &key_fds
[i
]);
1199 for (int i
= 0; i
< ictx
->num_cmd_fd
; i
++)
1200 if (cmd_fds
[i
].dead
|| cmd_fds
[i
].eof
) {
1201 mp_input_rm_cmd_fd(ictx
, cmd_fds
[i
].fd
);
1203 } else if (time
&& cmd_fds
[i
].no_select
)
1204 read_cmd_fd(ictx
, &cmd_fds
[i
]);
1205 if (ictx
->got_new_events
)
1207 #ifdef HAVE_POSIX_SELECT
1211 for (int i
= 0; i
< ictx
->num_key_fd
; i
++) {
1212 if (key_fds
[i
].no_select
)
1214 if (key_fds
[i
].fd
> max_fd
)
1215 max_fd
= key_fds
[i
].fd
;
1216 FD_SET(key_fds
[i
].fd
, &fds
);
1218 for (int i
= 0; i
< ictx
->num_cmd_fd
; i
++) {
1219 if (cmd_fds
[i
].no_select
)
1221 if (cmd_fds
[i
].fd
> max_fd
)
1222 max_fd
= cmd_fds
[i
].fd
;
1223 FD_SET(cmd_fds
[i
].fd
, &fds
);
1225 struct timeval tv
, *time_val
;
1227 tv
.tv_sec
= time
/ 1000;
1228 tv
.tv_usec
= (time
% 1000) * 1000;
1232 if (select(max_fd
+ 1, &fds
, NULL
, NULL
, time_val
) < 0) {
1234 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Select error: %s\n",
1240 usec_sleep(time
* 1000);
1244 for (int i
= 0; i
< ictx
->num_key_fd
; i
++) {
1245 #ifdef HAVE_POSIX_SELECT
1246 if (!key_fds
[i
].no_select
&& !FD_ISSET(key_fds
[i
].fd
, &fds
))
1249 read_key_fd(ictx
, &key_fds
[i
]);
1252 for (int i
= 0; i
< ictx
->num_cmd_fd
; i
++) {
1253 #ifdef HAVE_POSIX_SELECT
1254 if (!cmd_fds
[i
].no_select
&& !FD_ISSET(cmd_fds
[i
].fd
, &fds
))
1257 read_cmd_fd(ictx
, &cmd_fds
[i
]);
1261 /* To support blocking file descriptors we don't loop the read over
1262 * every source until it's known to be empty. Instead we use this wrapper
1263 * to run select() again.
1265 static void read_all_fd_events(struct input_ctx
*ictx
, int time
)
1268 read_events(ictx
, time
);
1269 if (!ictx
->got_new_events
)
1275 static void read_all_events(struct input_ctx
*ictx
, int time
)
1278 cocoa_events_read_all_events(ictx
, time
);
1280 read_all_fd_events(ictx
, time
);
1284 int mp_input_queue_cmd(struct input_ctx
*ictx
, mp_cmd_t
*cmd
)
1286 ictx
->got_new_events
= true;
1289 queue_add(&ictx
->control_cmd_queue
, cmd
, true);
1294 * \param peek_only when set, the returned command stays in the queue.
1295 * Do not free the returned cmd whe you set this!
1297 mp_cmd_t
*mp_input_get_cmd(struct input_ctx
*ictx
, int time
, int peek_only
)
1299 if (async_quit_request
)
1300 return mp_input_parse_cmd("quit 1");
1302 if (ictx
->control_cmd_queue
.num_cmds
|| ictx
->key_cmd_queue
.num_cmds
)
1304 read_all_events(ictx
, time
);
1306 struct cmd_queue
*queue
= &ictx
->control_cmd_queue
;
1307 if (!queue
->num_cmds
)
1308 queue
= &ictx
->key_cmd_queue
;
1309 if (!queue
->num_cmds
) {
1310 ret
= check_autorepeat(ictx
);
1313 queue_add(queue
, ret
, false);
1323 void mp_cmd_free(mp_cmd_t
*cmd
)
1328 mp_cmd_t
*mp_cmd_clone(mp_cmd_t
*cmd
)
1333 ret
= talloc_memdup(NULL
, cmd
, sizeof(mp_cmd_t
));
1334 ret
->name
= talloc_strdup(ret
, cmd
->name
);
1335 for (i
= 0; i
< MP_CMD_MAX_ARGS
&& cmd
->args
[i
].type
; i
++) {
1336 if (cmd
->args
[i
].type
== MP_CMD_ARG_STRING
&& cmd
->args
[i
].v
.s
!= NULL
)
1337 ret
->args
[i
].v
.s
= talloc_strdup(ret
, cmd
->args
[i
].v
.s
);
1343 int mp_input_get_key_from_name(const char *name
)
1347 while ((p
= strchr(name
, '+'))) {
1348 for (struct key_name
*m
= modifier_names
; m
->name
; m
++)
1349 if (!bstrcasecmp(bstr(m
->name
),
1350 (struct bstr
){(char *)name
, p
- name
})) {
1351 modifiers
|= m
->key
;
1354 if (!strcmp(name
, "+"))
1355 return '+' + modifiers
;
1361 struct bstr bname
= bstr(name
);
1364 int code
= bstr_decode_utf8(bname
, &rest
);
1365 if (code
>= 0 && rest
.len
== 0)
1366 return code
+ modifiers
;
1368 if (bstr_startswith0(bname
, "0x"))
1369 return strtol(name
, NULL
, 16) + modifiers
;
1371 for (int i
= 0; key_names
[i
].name
!= NULL
; i
++) {
1372 if (strcasecmp(key_names
[i
].name
, name
) == 0)
1373 return key_names
[i
].key
+ modifiers
;
1379 static int get_input_from_name(char *name
, int *keys
)
1386 for (end
= strchr(ptr
, '-'); ptr
!= NULL
; end
= strchr(ptr
, '-')) {
1387 if (end
&& end
[1] != '\0') {
1392 keys
[n
] = mp_input_get_key_from_name(ptr
);
1396 if (end
&& end
[1] != '\0' && n
< MP_MAX_KEY_DOWN
)
1405 static void bind_keys(struct input_ctx
*ictx
,
1406 const int keys
[MP_MAX_KEY_DOWN
+ 1], struct bstr cmd
)
1409 struct cmd_bind
*bind
= NULL
;
1410 struct cmd_bind_section
*bind_section
= NULL
;
1411 struct bstr section
= bstr("default");
1414 if (bstr_startswith0(cmd
, "{") && (idx
= bstrchr(cmd
, '}')) > 0) {
1415 section
= bstr_strip(bstr_splice(cmd
, 1, idx
));
1416 cmd
= bstr_strip(bstr_cut(cmd
, idx
+ 1));
1418 bind_section
= get_bind_section(ictx
, section
);
1420 if (bind_section
->cmd_binds
) {
1421 for (i
= 0; bind_section
->cmd_binds
[i
].cmd
!= NULL
; i
++) {
1422 for (j
= 0; bind_section
->cmd_binds
[i
].input
[j
] == keys
[j
] && keys
[j
] != 0; j
++)
1424 if (keys
[j
] == 0 && bind_section
->cmd_binds
[i
].input
[j
] == 0 ) {
1425 bind
= &bind_section
->cmd_binds
[i
];
1432 bind_section
->cmd_binds
= talloc_realloc(bind_section
,
1433 bind_section
->cmd_binds
,
1434 struct cmd_bind
, i
+ 2);
1435 memset(&bind_section
->cmd_binds
[i
], 0, 2 * sizeof(struct cmd_bind
));
1436 bind
= &bind_section
->cmd_binds
[i
];
1438 talloc_free(bind
->cmd
);
1439 bind
->cmd
= bstrdup0(bind_section
->cmd_binds
, cmd
);
1440 memcpy(bind
->input
, keys
, (MP_MAX_KEY_DOWN
+ 1) * sizeof(int));
1443 static int parse_config(struct input_ctx
*ictx
, struct bstr data
)
1445 int n_binds
= 0, keys
[MP_MAX_KEY_DOWN
+ 1];
1448 struct bstr line
= bstr_strip(bstr_getline(data
, &data
));
1449 if (line
.len
== 0 || bstr_startswith0(line
, "#"))
1451 struct bstr command
;
1452 // Find the key name starting a line
1453 struct bstr keyname
= bstr_split(line
, WHITESPACE
, &command
);
1454 command
= bstr_strip(command
);
1455 if (command
.len
== 0) {
1456 mp_tmsg(MSGT_INPUT
, MSGL_ERR
,
1457 "Unfinished key binding: %.*s\n", BSTR_P(line
));
1460 char *name
= bstrdup0(NULL
, keyname
);
1461 if (!get_input_from_name(name
, keys
)) {
1463 mp_tmsg(MSGT_INPUT
, MSGL_ERR
,
1464 "Unknown key '%.*s'\n", BSTR_P(keyname
));
1468 bind_keys(ictx
, keys
, command
);
1475 static int parse_config_file(struct input_ctx
*ictx
, char *file
)
1477 stream_t
*s
= open_stream(file
, ictx
->opts
, NULL
);
1479 mp_msg(MSGT_INPUT
, MSGL_V
, "Can't open input config file %s.\n", file
);
1482 struct bstr res
= stream_read_complete(s
, NULL
, 1000000, 0);
1485 mp_msg(MSGT_INPUT
, MSGL_ERR
, "Could not read input config file %s.\n",
1489 mp_msg(MSGT_INPUT
, MSGL_V
, "Parsing input config file %s\n", file
);
1490 int n_binds
= parse_config(ictx
, res
);
1491 talloc_free(res
.start
);
1492 mp_msg(MSGT_INPUT
, MSGL_V
, "Input config file %s parsed: %d binds\n",
1497 void mp_input_set_section(struct input_ctx
*ictx
, char *name
)
1499 talloc_free(ictx
->section
.start
);
1500 ictx
->section
= bstrdup(ictx
, name
? bstr(name
) : bstr("default"));
1503 struct input_ctx
*mp_input_init(struct MPOpts
*opts
)
1505 struct input_conf
*input_conf
= &opts
->input
;
1506 struct input_ctx
*ictx
= talloc_ptrtype(NULL
, ictx
);
1507 *ictx
= (struct input_ctx
){
1508 .key_fifo_size
= input_conf
->key_fifo_size
,
1510 .ar_delay
= input_conf
->ar_delay
,
1511 .ar_rate
= input_conf
->ar_rate
,
1512 .section
= bstrdup(ictx
, bstr("default")),
1513 .wakeup_pipe
= {-1, -1},
1518 cocoa_events_init(ictx
, read_all_fd_events
);
1522 long ret
= pipe(ictx
->wakeup_pipe
);
1523 for (int i
= 0; i
< 2 && ret
>= 0; i
++) {
1524 ret
= fcntl(ictx
->wakeup_pipe
[i
], F_GETFL
);
1527 ret
= fcntl(ictx
->wakeup_pipe
[i
], F_SETFL
, ret
| O_NONBLOCK
);
1530 mp_msg(MSGT_INPUT
, MSGL_ERR
,
1531 "Failed to initialize wakeup pipe: %s\n", strerror(errno
));
1533 mp_input_add_key_fd(ictx
, ictx
->wakeup_pipe
[0], true, read_wakeup
,
1537 if (input_conf
->default_bindings
)
1538 parse_config(ictx
, builtin_input_conf
);
1541 char *config_file
= input_conf
->config_file
;
1542 file
= config_file
[0] != '/' ? get_path(config_file
) : config_file
;
1544 if (file
&& !parse_config_file(ictx
, file
)) {
1545 // free file if it was allocated by get_path(),
1546 // before it gets overwritten
1547 if (file
!= config_file
)
1549 // Try global conf dir
1550 file
= MPLAYER_CONFDIR
"/input.conf";
1551 if (!parse_config_file(ictx
, file
))
1552 mp_msg(MSGT_INPUT
, MSGL_V
, "Falling back on default (hardcoded) "
1555 // free file if it was allocated by get_path()
1556 if (file
!= config_file
)
1560 #ifdef CONFIG_JOYSTICK
1561 if (input_conf
->use_joystick
) {
1562 int fd
= mp_input_joystick_init(input_conf
->js_dev
);
1564 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Can't init input joystick\n");
1566 mp_input_add_key_fd(ictx
, fd
, 1, mp_input_joystick_read
,
1572 if (input_conf
->use_lirc
) {
1573 int fd
= mp_input_lirc_init();
1575 mp_input_add_cmd_fd(ictx
, fd
, 0, mp_input_lirc_read
,
1576 mp_input_lirc_close
);
1581 if (input_conf
->use_lircc
) {
1582 int fd
= lircc_init("mplayer", NULL
);
1584 mp_input_add_cmd_fd(ictx
, fd
, 1, NULL
, lircc_cleanup
);
1588 #ifdef CONFIG_APPLE_REMOTE
1589 if (input_conf
->use_ar
) {
1590 if (mp_input_ar_init() < 0)
1591 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Can't init Apple Remote.\n");
1593 mp_input_add_key_fd(ictx
, -1, 0, mp_input_ar_read
,
1594 mp_input_ar_close
, NULL
);
1598 #ifdef CONFIG_APPLE_IR
1599 if (input_conf
->use_ar
) {
1600 int fd
= mp_input_appleir_init(input_conf
->ar_dev
);
1602 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Can't init Apple Remote.\n");
1604 mp_input_add_key_fd(ictx
, fd
, 1, mp_input_appleir_read
,
1609 if (input_conf
->in_file
) {
1610 int mode
= O_RDONLY
;
1612 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1613 // Note that on Windows due to how the API works, using RDONLY should
1616 if (stat(input_conf
->in_file
, &st
) == 0 && S_ISFIFO(st
.st_mode
))
1620 int in_file_fd
= open(input_conf
->in_file
, mode
);
1621 if (in_file_fd
>= 0)
1622 mp_input_add_cmd_fd(ictx
, in_file_fd
, 1, NULL
, close
);
1624 mp_tmsg(MSGT_INPUT
, MSGL_ERR
, "Can't open %s: %s\n",
1625 input_conf
->in_file
, strerror(errno
));
1631 void mp_input_uninit(struct input_ctx
*ictx
)
1634 cocoa_events_uninit();
1640 for (int i
= 0; i
< ictx
->num_key_fd
; i
++) {
1641 if (ictx
->key_fds
[i
].close_func
)
1642 ictx
->key_fds
[i
].close_func(ictx
->key_fds
[i
].fd
);
1644 for (int i
= 0; i
< ictx
->num_cmd_fd
; i
++) {
1645 if (ictx
->cmd_fds
[i
].close_func
)
1646 ictx
->cmd_fds
[i
].close_func(ictx
->cmd_fds
[i
].fd
);
1648 for (int i
= 0; i
< 2; i
++)
1649 if (ictx
->wakeup_pipe
[i
] != -1)
1650 close(ictx
->wakeup_pipe
[i
]);
1654 void mp_input_register_options(m_config_t
*cfg
)
1656 m_config_register_options(cfg
, mp_input_opts
);
1659 static int print_key_list(m_option_t
*cfg
, char *optname
, char *optparam
)
1663 for (i
= 0; key_names
[i
].name
!= NULL
; i
++)
1664 printf("%s\n", key_names
[i
].name
);
1668 static int print_cmd_list(m_option_t
*cfg
, char *optname
, char *optparam
)
1670 const mp_cmd_t
*cmd
;
1674 for (i
= 0; (cmd
= &mp_cmds
[i
])->name
!= NULL
; i
++) {
1675 printf("%-20.20s", cmd
->name
);
1676 for (j
= 0; j
< MP_CMD_MAX_ARGS
&& cmd
->args
[j
].type
; j
++) {
1677 switch (cmd
->args
[j
].type
) {
1678 case MP_CMD_ARG_INT
:
1681 case MP_CMD_ARG_FLOAT
:
1684 case MP_CMD_ARG_STRING
:
1690 if (cmd
->args
[j
].optional
)
1691 printf(" [%s]", type
);
1693 printf(" %s", type
);
1700 void mp_input_wakeup(struct input_ctx
*ictx
)
1702 if (ictx
->wakeup_pipe
[1] >= 0)
1703 write(ictx
->wakeup_pipe
[1], &(char){0}, 1);
1707 * \param time time to wait for an interruption in milliseconds
1709 int mp_input_check_interrupt(struct input_ctx
*ictx
, int time
)
1711 for (int i
= 0; ; i
++) {
1712 if (async_quit_request
|| ictx
->key_cmd_queue
.num_abort_cmds
||
1713 ictx
->control_cmd_queue
.num_abort_cmds
) {
1714 mp_tmsg(MSGT_INPUT
, MSGL_WARN
, "Received command to move to "
1715 "another file. Aborting current processing.\n");
1720 read_all_events(ictx
, time
);