OSX, input: implement wakeup in response to Cocoa events
[mplayer.git] / input / input.c
blob191b378deb7aa1ed304cc73147edbda29b341159
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"
50 #include "joystick.h"
52 #ifdef CONFIG_LIRC
53 #include "lirc.h"
54 #endif
56 #ifdef CONFIG_LIRCC
57 #include <lirc/lircc.h>
58 #endif
60 #include "ar.h"
62 #ifdef CONFIG_COCOA
63 #include "osdep/cocoa_events.h"
64 #endif
66 #define MP_MAX_KEY_DOWN 32
68 struct cmd_bind {
69 int input[MP_MAX_KEY_DOWN + 1];
70 char *cmd;
73 struct key_name {
74 int key;
75 char *name;
78 /* This array defines all known commands.
79 * The first field is an id used to recognize the command.
80 * The second is the command name used in slave mode and input.conf.
81 * Then comes the definition of each argument, first mandatory arguments
82 * (ARG_INT, ARG_FLOAT, ARG_STRING) if any, then optional arguments
83 * (OARG_INT(default), etc) if any. The command will be given the default
84 * argument value if the user didn't give enough arguments to specify it.
85 * A command can take a maximum of MP_CMD_MAX_ARGS arguments (10).
87 #define ARG_INT { .type = MP_CMD_ARG_INT }
88 #define OARG_INT(def) { .type = MP_CMD_ARG_INT, .optional = true, .v.i = def }
89 #define ARG_FLOAT { .type = MP_CMD_ARG_FLOAT }
90 #define OARG_FLOAT(def) { .type = MP_CMD_ARG_FLOAT, .optional = true, .v.f = def }
91 #define ARG_STRING { .type = MP_CMD_ARG_STRING }
92 #define OARG_STRING(def) { .type = MP_CMD_ARG_STRING, .optional = true, .v.s = def }
94 static const mp_cmd_t mp_cmds[] = {
95 #ifdef CONFIG_RADIO
96 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", { ARG_INT } },
97 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", { ARG_STRING } },
98 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", { ARG_FLOAT } },
99 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", {ARG_FLOAT } },
100 #endif
101 { MP_CMD_SEEK, "seek", { ARG_FLOAT, OARG_INT(0), OARG_INT(0) } },
102 { MP_CMD_EDL_MARK, "edl_mark", },
103 { MP_CMD_AUDIO_DELAY, "audio_delay", { ARG_FLOAT, OARG_INT(0) } },
104 { MP_CMD_SPEED_INCR, "speed_incr", { ARG_FLOAT } },
105 { MP_CMD_SPEED_MULT, "speed_mult", { ARG_FLOAT } },
106 { MP_CMD_SPEED_SET, "speed_set", { ARG_FLOAT } },
107 { MP_CMD_QUIT, "quit", { OARG_INT(0) } },
108 { MP_CMD_STOP, "stop", },
109 { MP_CMD_PAUSE, "pause", },
110 { MP_CMD_FRAME_STEP, "frame_step", },
111 { MP_CMD_PLAY_TREE_STEP, "pt_step", { ARG_INT, OARG_INT(0) } },
112 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step", { ARG_INT, OARG_INT(0) } },
113 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step", { ARG_INT } },
114 { MP_CMD_LOOP, "loop", { ARG_INT, OARG_INT(0) } },
115 { MP_CMD_SUB_DELAY, "sub_delay", { ARG_FLOAT, OARG_INT(0) } },
116 { MP_CMD_SUB_STEP, "sub_step", { ARG_INT, OARG_INT(0) } },
117 { MP_CMD_OSD, "osd", { OARG_INT(-1) } },
118 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", { ARG_STRING, OARG_INT(-1), OARG_INT(0) } },
119 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text", { ARG_STRING, OARG_INT(-1), OARG_INT(0) } },
120 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", },
121 { MP_CMD_VOLUME, "volume", { ARG_FLOAT, OARG_INT(0) } },
122 { MP_CMD_BALANCE, "balance", { ARG_FLOAT, OARG_INT(0) } },
123 { MP_CMD_MIXER_USEMASTER, "use_master", },
124 { MP_CMD_MUTE, "mute", { OARG_INT(-1) } },
125 { MP_CMD_CONTRAST, "contrast", { ARG_INT, OARG_INT(0) } },
126 { MP_CMD_GAMMA, "gamma", { ARG_INT, OARG_INT(0) } },
127 { MP_CMD_BRIGHTNESS, "brightness", { ARG_INT, OARG_INT(0) } },
128 { MP_CMD_HUE, "hue", { ARG_INT, OARG_INT(0) } },
129 { MP_CMD_SATURATION, "saturation", { ARG_INT, OARG_INT(0) } },
130 { MP_CMD_FRAMEDROPPING, "frame_drop", { OARG_INT(-1) } },
131 { MP_CMD_SUB_POS, "sub_pos", { ARG_INT, OARG_INT(0) } },
132 { MP_CMD_SUB_ALIGNMENT, "sub_alignment", { OARG_INT(-1) } },
133 { MP_CMD_SUB_VISIBILITY, "sub_visibility", { OARG_INT(-1) } },
134 { MP_CMD_SUB_LOAD, "sub_load", { ARG_STRING } },
135 { MP_CMD_SUB_REMOVE, "sub_remove", { OARG_INT(-1) } },
136 { MP_CMD_SUB_SELECT, "vobsub_lang", { OARG_INT(-2) } }, // for compatibility
137 { MP_CMD_SUB_SELECT, "sub_select", { OARG_INT(-2) } },
138 { MP_CMD_SUB_SOURCE, "sub_source", { OARG_INT(-2) } },
139 { MP_CMD_SUB_VOB, "sub_vob", { OARG_INT(-2) } },
140 { MP_CMD_SUB_DEMUX, "sub_demux", { OARG_INT(-2) } },
141 { MP_CMD_SUB_FILE, "sub_file", { OARG_INT(-2) } },
142 { MP_CMD_SUB_LOG, "sub_log", },
143 { MP_CMD_SUB_SCALE, "sub_scale", { ARG_FLOAT, OARG_INT(0) } },
144 #ifdef CONFIG_ASS
145 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", { OARG_INT(-1) } },
146 #endif
147 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", },
148 { MP_CMD_GET_TIME_POS, "get_time_pos", },
149 { MP_CMD_GET_TIME_LENGTH, "get_time_length", },
150 { MP_CMD_GET_FILENAME, "get_file_name", },
151 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", },
152 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", },
153 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", },
154 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", },
155 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", },
156 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", },
157 { MP_CMD_GET_META_TITLE, "get_meta_title", },
158 { MP_CMD_GET_META_ARTIST, "get_meta_artist", },
159 { MP_CMD_GET_META_ALBUM, "get_meta_album", },
160 { MP_CMD_GET_META_YEAR, "get_meta_year", },
161 { MP_CMD_GET_META_COMMENT, "get_meta_comment", },
162 { MP_CMD_GET_META_TRACK, "get_meta_track", },
163 { MP_CMD_GET_META_GENRE, "get_meta_genre", },
164 { MP_CMD_SWITCH_AUDIO, "switch_audio", { OARG_INT(-1) } },
165 { MP_CMD_SWITCH_ANGLE, "switch_angle", { OARG_INT(-1) } },
166 { MP_CMD_SWITCH_TITLE, "switch_title", { OARG_INT(-1) } },
167 #ifdef CONFIG_TV
168 { MP_CMD_TV_START_SCAN, "tv_start_scan", },
169 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", { ARG_INT } },
170 { MP_CMD_TV_STEP_NORM, "tv_step_norm", },
171 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", },
172 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", { ARG_STRING } },
173 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", },
174 { MP_CMD_TV_SET_FREQ, "tv_set_freq", { ARG_FLOAT } },
175 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", { ARG_FLOAT } },
176 { MP_CMD_TV_SET_NORM, "tv_set_norm", { ARG_STRING } },
177 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", { ARG_INT, OARG_INT(1) } },
178 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", { ARG_INT, OARG_INT(1) } },
179 { MP_CMD_TV_SET_HUE, "tv_set_hue", { ARG_INT, OARG_INT(1) } },
180 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", { ARG_INT, OARG_INT(1) } },
181 #endif
182 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", { OARG_INT(-1) } },
183 #ifdef CONFIG_DVBIN
184 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", { ARG_INT, ARG_INT } },
185 #endif
186 { MP_CMD_SWITCH_RATIO, "switch_ratio", { OARG_FLOAT(0) } },
187 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", { OARG_INT(-1) } },
188 { MP_CMD_VO_ONTOP, "vo_ontop", { OARG_INT(-1) } },
189 { MP_CMD_VO_ROOTWIN, "vo_rootwin", { OARG_INT(-1) } },
190 { MP_CMD_VO_BORDER, "vo_border", { OARG_INT(-1) } },
191 { MP_CMD_SCREENSHOT, "screenshot", { OARG_INT(0), OARG_INT(0) } },
192 { MP_CMD_PANSCAN, "panscan", { ARG_FLOAT, OARG_INT(0) } },
193 { MP_CMD_SWITCH_VSYNC, "switch_vsync", { OARG_INT(0) } },
194 { MP_CMD_LOADFILE, "loadfile", { ARG_STRING, OARG_INT(0) } },
195 { MP_CMD_LOADLIST, "loadlist", { ARG_STRING, OARG_INT(0) } },
196 { MP_CMD_RUN, "run", { ARG_STRING } },
197 { MP_CMD_CAPTURING, "capturing", },
198 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", { ARG_INT, ARG_INT } },
199 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", { ARG_STRING } },
200 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", { ARG_INT } },
202 #ifdef CONFIG_DVDNAV
203 { MP_CMD_DVDNAV, "dvdnav", { ARG_STRING } },
204 #endif
206 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", },
207 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", },
208 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", { ARG_INT } },
209 { MP_CMD_SET_PROPERTY, "set_property", { ARG_STRING, ARG_STRING } },
210 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", { ARG_STRING, ARG_STRING } },
211 { MP_CMD_GET_PROPERTY, "get_property", { ARG_STRING } },
212 { MP_CMD_STEP_PROPERTY, "step_property", { ARG_STRING, OARG_FLOAT(0), OARG_INT(0) } },
213 { MP_CMD_STEP_PROPERTY_OSD, "step_property_osd", { ARG_STRING, OARG_FLOAT(0), OARG_INT(0) } },
215 { MP_CMD_SEEK_CHAPTER, "seek_chapter", { ARG_INT, OARG_INT(0) } },
216 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", { ARG_INT, ARG_INT } },
218 { MP_CMD_AF_SWITCH, "af_switch", { ARG_STRING } },
219 { MP_CMD_AF_ADD, "af_add", { ARG_STRING } },
220 { MP_CMD_AF_DEL, "af_del", { ARG_STRING } },
221 { MP_CMD_AF_CLR, "af_clr", },
222 { MP_CMD_AF_CMDLINE, "af_cmdline", { ARG_STRING, ARG_STRING } },
226 /// The names of the keys as used in input.conf
227 /// If you add some new keys, you also need to add them here
229 static const struct key_name key_names[] = {
230 { ' ', "SPACE" },
231 { '#', "SHARP" },
232 { KEY_ENTER, "ENTER" },
233 { KEY_TAB, "TAB" },
234 { KEY_BACKSPACE, "BS" },
235 { KEY_DELETE, "DEL" },
236 { KEY_INSERT, "INS" },
237 { KEY_HOME, "HOME" },
238 { KEY_END, "END" },
239 { KEY_PAGE_UP, "PGUP" },
240 { KEY_PAGE_DOWN, "PGDWN" },
241 { KEY_ESC, "ESC" },
242 { KEY_PRINT, "PRINT" },
243 { KEY_RIGHT, "RIGHT" },
244 { KEY_LEFT, "LEFT" },
245 { KEY_DOWN, "DOWN" },
246 { KEY_UP, "UP" },
247 { KEY_F+1, "F1" },
248 { KEY_F+2, "F2" },
249 { KEY_F+3, "F3" },
250 { KEY_F+4, "F4" },
251 { KEY_F+5, "F5" },
252 { KEY_F+6, "F6" },
253 { KEY_F+7, "F7" },
254 { KEY_F+8, "F8" },
255 { KEY_F+9, "F9" },
256 { KEY_F+10, "F10" },
257 { KEY_F+11, "F11" },
258 { KEY_F+12, "F12" },
259 { KEY_KP0, "KP0" },
260 { KEY_KP1, "KP1" },
261 { KEY_KP2, "KP2" },
262 { KEY_KP3, "KP3" },
263 { KEY_KP4, "KP4" },
264 { KEY_KP5, "KP5" },
265 { KEY_KP6, "KP6" },
266 { KEY_KP7, "KP7" },
267 { KEY_KP8, "KP8" },
268 { KEY_KP9, "KP9" },
269 { KEY_KPDEL, "KP_DEL" },
270 { KEY_KPDEC, "KP_DEC" },
271 { KEY_KPINS, "KP_INS" },
272 { KEY_KPENTER, "KP_ENTER" },
273 { MOUSE_BTN0, "MOUSE_BTN0" },
274 { MOUSE_BTN1, "MOUSE_BTN1" },
275 { MOUSE_BTN2, "MOUSE_BTN2" },
276 { MOUSE_BTN3, "MOUSE_BTN3" },
277 { MOUSE_BTN4, "MOUSE_BTN4" },
278 { MOUSE_BTN5, "MOUSE_BTN5" },
279 { MOUSE_BTN6, "MOUSE_BTN6" },
280 { MOUSE_BTN7, "MOUSE_BTN7" },
281 { MOUSE_BTN8, "MOUSE_BTN8" },
282 { MOUSE_BTN9, "MOUSE_BTN9" },
283 { MOUSE_BTN10, "MOUSE_BTN10" },
284 { MOUSE_BTN11, "MOUSE_BTN11" },
285 { MOUSE_BTN12, "MOUSE_BTN12" },
286 { MOUSE_BTN13, "MOUSE_BTN13" },
287 { MOUSE_BTN14, "MOUSE_BTN14" },
288 { MOUSE_BTN15, "MOUSE_BTN15" },
289 { MOUSE_BTN16, "MOUSE_BTN16" },
290 { MOUSE_BTN17, "MOUSE_BTN17" },
291 { MOUSE_BTN18, "MOUSE_BTN18" },
292 { MOUSE_BTN19, "MOUSE_BTN19" },
293 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
294 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
295 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
296 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
297 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
298 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
299 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
300 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
301 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
302 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
303 { MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
304 { MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
305 { MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
306 { MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
307 { MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
308 { MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
309 { MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
310 { MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
311 { MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
312 { MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
313 { JOY_AXIS1_MINUS, "JOY_UP" },
314 { JOY_AXIS1_PLUS, "JOY_DOWN" },
315 { JOY_AXIS0_MINUS, "JOY_LEFT" },
316 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
318 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
319 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
320 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
321 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
322 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
323 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
324 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
325 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
326 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
327 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
328 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
329 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
330 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
331 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
332 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
333 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
334 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
335 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
336 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
337 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
339 { JOY_BTN0, "JOY_BTN0" },
340 { JOY_BTN1, "JOY_BTN1" },
341 { JOY_BTN2, "JOY_BTN2" },
342 { JOY_BTN3, "JOY_BTN3" },
343 { JOY_BTN4, "JOY_BTN4" },
344 { JOY_BTN5, "JOY_BTN5" },
345 { JOY_BTN6, "JOY_BTN6" },
346 { JOY_BTN7, "JOY_BTN7" },
347 { JOY_BTN8, "JOY_BTN8" },
348 { JOY_BTN9, "JOY_BTN9" },
350 { AR_PLAY, "AR_PLAY" },
351 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
352 { AR_NEXT, "AR_NEXT" },
353 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
354 { AR_PREV, "AR_PREV" },
355 { AR_PREV_HOLD, "AR_PREV_HOLD" },
356 { AR_MENU, "AR_MENU" },
357 { AR_MENU_HOLD, "AR_MENU_HOLD" },
358 { AR_VUP, "AR_VUP" },
359 { AR_VDOWN, "AR_VDOWN" },
361 { KEY_POWER, "POWER" },
362 { KEY_MENU, "MENU" },
363 { KEY_PLAY, "PLAY" },
364 { KEY_PAUSE, "PAUSE" },
365 { KEY_PLAYPAUSE, "PLAYPAUSE" },
366 { KEY_STOP, "STOP" },
367 { KEY_FORWARD, "FORWARD" },
368 { KEY_REWIND, "REWIND" },
369 { KEY_NEXT, "NEXT" },
370 { KEY_PREV, "PREV" },
371 { KEY_VOLUME_UP, "VOLUME_UP" },
372 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
373 { KEY_MUTE, "MUTE" },
375 // These are kept for backward compatibility
376 { KEY_PAUSE, "XF86_PAUSE" },
377 { KEY_STOP, "XF86_STOP" },
378 { KEY_PREV, "XF86_PREV" },
379 { KEY_NEXT, "XF86_NEXT" },
381 { KEY_CLOSE_WIN, "CLOSE_WIN" },
383 { 0, NULL }
386 struct key_name modifier_names[] = {
387 { KEY_MODIFIER_SHIFT, "Shift" },
388 { KEY_MODIFIER_CTRL, "Ctrl" },
389 { KEY_MODIFIER_ALT, "Alt" },
390 { KEY_MODIFIER_META, "Meta" },
391 { 0 }
394 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
396 // This is the default binding. The content of input.conf overrides these.
397 // The first arg is a null terminated array of key codes.
398 // The second is the command
400 static const struct cmd_bind def_cmd_binds[] = {
402 { { MOUSE_BTN0_DBL, 0 }, "vo_fullscreen" },
403 { { MOUSE_BTN2, 0 }, "pause" },
404 { { MOUSE_BTN3, 0 }, "seek 10" },
405 { { MOUSE_BTN4, 0 }, "seek -10" },
406 { { MOUSE_BTN5, 0 }, "volume 1" },
407 { { MOUSE_BTN6, 0 }, "volume -1" },
409 #ifdef CONFIG_DVDNAV
410 { { KEY_KP8, 0 }, "dvdnav up" }, // up
411 { { KEY_KP2, 0 }, "dvdnav down" }, // down
412 { { KEY_KP4, 0 }, "dvdnav left" }, // left
413 { { KEY_KP6, 0 }, "dvdnav right" }, // right
414 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
415 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
416 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
417 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
418 #endif
420 { { KEY_RIGHT, 0 }, "seek 10" },
421 { { KEY_LEFT, 0 }, "seek -10" },
422 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
423 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
424 { { KEY_UP, 0 }, "seek 60" },
425 { { KEY_DOWN, 0 }, "seek -60" },
426 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
427 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
428 { { KEY_PAGE_UP, 0 }, "seek 600" },
429 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
430 { { '+', 0 }, "audio_delay 0.100" },
431 { { '-', 0 }, "audio_delay -0.100" },
432 { { '[', 0 }, "speed_mult 0.9091" },
433 { { ']', 0 }, "speed_mult 1.1" },
434 { { '{', 0 }, "speed_mult 0.5" },
435 { { '}', 0 }, "speed_mult 2.0" },
436 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
437 { { 'q', 0 }, "quit" },
438 { { KEY_ESC, 0 }, "quit" },
439 { { 'p', 0 }, "pause" },
440 { { ' ', 0 }, "pause" },
441 { { '.', 0 }, "frame_step" },
442 { { KEY_HOME, 0 }, "pt_up_step 1" },
443 { { KEY_END, 0 }, "pt_up_step -1" },
444 { { '>', 0 }, "pt_step 1" },
445 { { KEY_ENTER, 0 }, "pt_step 1 1" },
446 { { '<', 0 }, "pt_step -1" },
447 { { KEY_INS, 0 }, "alt_src_step 1" },
448 { { KEY_DEL, 0 }, "alt_src_step -1" },
449 { { 'o', 0 }, "osd" },
450 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
451 { { 'P', 0 }, "osd_show_progression" },
452 { { 'z', 0 }, "sub_delay -0.1" },
453 { { 'x', 0 }, "sub_delay +0.1" },
454 { { 'g', 0 }, "sub_step -1" },
455 { { 'y', 0 }, "sub_step +1" },
456 { { '9', 0 }, "volume -1" },
457 { { '/', 0 }, "volume -1" },
458 { { '0', 0 }, "volume 1" },
459 { { '*', 0 }, "volume 1" },
460 { { '(', 0 }, "balance -0.1" },
461 { { ')', 0 }, "balance 0.1" },
462 { { 'm', 0 }, "mute" },
463 { { '1', 0 }, "contrast -1" },
464 { { '2', 0 }, "contrast 1" },
465 { { '3', 0 }, "brightness -1" },
466 { { '4', 0 }, "brightness 1" },
467 { { '5', 0 }, "hue -1" },
468 { { '6', 0 }, "hue 1" },
469 { { '7', 0 }, "saturation -1" },
470 { { '8', 0 }, "saturation 1" },
471 { { 'd', 0 }, "frame_drop" },
472 { { 'D', 0 }, "step_property_osd deinterlace" },
473 { { 'c', 0 }, "step_property_osd colormatrix" },
474 { { 'r', 0 }, "sub_pos -1" },
475 { { 't', 0 }, "sub_pos +1" },
476 { { 'a', 0 }, "sub_alignment" },
477 { { 'v', 0 }, "sub_visibility" },
478 { { 'V', 0 }, "step_property_osd ass_vsfilter_aspect_compat" },
479 { { 'j', 0 }, "sub_select" },
480 { { 'J', 0 }, "sub_select -3" },
481 { { 'F', 0 }, "forced_subs_only" },
482 { { '#', 0 }, "switch_audio" },
483 { { '_', 0 }, "step_property switch_video" },
484 { { KEY_TAB, 0 }, "step_property switch_program" },
485 { { 'i', 0 }, "edl_mark" },
486 #ifdef CONFIG_TV
487 { { 'h', 0 }, "tv_step_channel 1" },
488 { { 'k', 0 }, "tv_step_channel -1" },
489 { { 'n', 0 }, "tv_step_norm" },
490 { { 'u', 0 }, "tv_step_chanlist" },
491 #endif
492 { { 'X', 0 }, "step_property teletext_mode 1" },
493 { { 'W', 0 }, "step_property teletext_page 1" },
494 { { 'Q', 0 }, "step_property teletext_page -1" },
495 #ifdef CONFIG_JOYSTICK
496 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
497 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
498 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
499 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
500 { { JOY_BTN0, 0 }, "pause" },
501 { { JOY_BTN1, 0 }, "osd" },
502 { { JOY_BTN2, 0 }, "volume 1"},
503 { { JOY_BTN3, 0 }, "volume -1"},
504 #endif
505 #ifdef CONFIG_APPLE_REMOTE
506 { { AR_PLAY, 0}, "pause" },
507 { { AR_PLAY_HOLD, 0}, "quit" },
508 { { AR_NEXT, 0 }, "seek 30" },
509 { { AR_NEXT_HOLD, 0 }, "seek 120" },
510 { { AR_PREV, 0 }, "seek -10" },
511 { { AR_PREV_HOLD, 0 }, "seek -120" },
512 { { AR_MENU, 0 }, "osd" },
513 { { AR_MENU_HOLD, 0 }, "mute" },
514 { { AR_VUP, 0 }, "volume 1"},
515 { { AR_VDOWN, 0 }, "volume -1"},
516 #endif
517 { { 'T', 0 }, "vo_ontop" },
518 { { 'f', 0 }, "vo_fullscreen" },
519 { { 'C', 0 }, "step_property_osd capturing" },
520 { { 's', 0 }, "screenshot 0" },
521 { { 'S', 0 }, "screenshot 1" },
522 { { KEY_MODIFIER_ALT + 's', 0 }, "screenshot 0 1" },
523 { { KEY_MODIFIER_ALT + 'S', 0 }, "screenshot 1 1" },
524 { { 'w', 0 }, "panscan -0.1" },
525 { { 'e', 0 }, "panscan +0.1" },
527 { { KEY_POWER, 0 }, "quit" },
528 { { KEY_MENU, 0 }, "osd" },
529 { { KEY_PLAY, 0 }, "pause" },
530 { { KEY_PAUSE, 0 }, "pause" },
531 { { KEY_PLAYPAUSE, 0 }, "pause" },
532 { { KEY_STOP, 0 }, "quit" },
533 { { KEY_FORWARD, 0 }, "seek 60" },
534 { { KEY_REWIND, 0 }, "seek -60" },
535 { { KEY_NEXT, 0 }, "pt_step 1" },
536 { { KEY_PREV, 0 }, "pt_step -1" },
537 { { KEY_VOLUME_UP, 0 }, "volume 1" },
538 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
539 { { KEY_MUTE, 0 }, "mute" },
541 { { KEY_CLOSE_WIN, 0 }, "quit" },
543 { { '!', 0 }, "seek_chapter -1" },
544 { { '@', 0 }, "seek_chapter 1" },
545 { { 'A', 0 }, "switch_angle 1" },
546 { { 'U', 0 }, "stop" },
548 { { 0 }, NULL }
552 #ifndef MP_MAX_KEY_FD
553 #define MP_MAX_KEY_FD 10
554 #endif
556 #ifndef MP_MAX_CMD_FD
557 #define MP_MAX_CMD_FD 10
558 #endif
560 struct input_fd {
561 int fd;
562 union {
563 int (*key)(void *ctx, int fd);
564 int (*cmd)(int fd, char *dest, int size);
565 } read_func;
566 int (*close_func)(int fd);
567 void *ctx;
568 unsigned eof : 1;
569 unsigned drop : 1;
570 unsigned dead : 1;
571 unsigned got_cmd : 1;
572 unsigned no_select : 1;
573 // These fields are for the cmd fds.
574 char *buffer;
575 int pos, size;
578 struct cmd_bind_section {
579 struct cmd_bind *cmd_binds;
580 char *section;
581 struct cmd_bind_section *next;
584 struct cmd_queue {
585 struct mp_cmd *first;
586 struct mp_cmd *last;
587 int num_cmds;
588 int num_abort_cmds;
591 struct input_ctx {
592 // Autorepeat stuff
593 short ar_state;
594 mp_cmd_t *ar_cmd;
595 unsigned int last_ar;
596 // Autorepeat config
597 unsigned int ar_delay;
598 unsigned int ar_rate;
599 // Maximum number of queued commands from keypresses (limit to avoid
600 // repeated slow commands piling up)
601 int key_fifo_size;
603 // these are the keys currently down
604 int key_down[MP_MAX_KEY_DOWN];
605 unsigned int num_key_down;
606 unsigned int last_key_down;
608 bool default_bindings;
609 // List of command binding sections
610 struct cmd_bind_section *cmd_bind_sections;
611 // Name of currently used command section
612 char *section;
613 // The command binds of current section
614 struct cmd_bind *cmd_binds;
615 struct cmd_bind *cmd_binds_default;
617 // Used to track whether we managed to read something while checking
618 // events sources. If yes, the sources may have more queued.
619 bool got_new_events;
621 struct input_fd key_fds[MP_MAX_KEY_FD];
622 unsigned int num_key_fd;
624 struct input_fd cmd_fds[MP_MAX_CMD_FD];
625 unsigned int num_cmd_fd;
627 struct cmd_queue key_cmd_queue;
628 struct cmd_queue control_cmd_queue;
630 int wakeup_pipe[2];
634 int async_quit_request;
636 static int print_key_list(m_option_t *cfg);
637 static int print_cmd_list(m_option_t *cfg);
639 // Our command line options
640 static const m_option_t input_conf[] = {
641 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
642 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
643 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
644 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
645 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
646 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
647 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
648 OPT_STRING("file", input.in_file, CONF_GLOBAL),
649 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
650 { NULL, NULL, 0, 0, 0, 0, NULL}
653 static const m_option_t mp_input_opts[] = {
654 { "input", (void *)&input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
655 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
656 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
657 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
658 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
659 { NULL, NULL, 0, 0, 0, 0, NULL}
662 static int default_cmd_func(int fd, char *buf, int l);
664 // Encode the unicode codepoint as UTF-8, and append to the end of the
665 // talloc'ed buffer.
666 static char *append_utf8_buffer(char *buffer, uint32_t codepoint)
668 char data[8];
669 uint8_t tmp;
670 char *output = data;
671 PUT_UTF8(codepoint, tmp, *output++ = tmp;);
672 return talloc_strndup_append_buffer(buffer, data, output - data);
675 static char *get_key_name(int key, char *ret)
677 for (int i = 0; modifier_names[i].name; i++) {
678 if (modifier_names[i].key & key) {
679 ret = talloc_asprintf_append_buffer(ret, "%s+",
680 modifier_names[i].name);
681 key -= modifier_names[i].key;
684 for (int i = 0; key_names[i].name != NULL; i++) {
685 if (key_names[i].key == key)
686 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
689 // printable, and valid unicode range
690 if (key >= 32 && key <= 0x10FFFF)
691 return append_utf8_buffer(ret, key);
693 // Print the hex key code
694 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
697 static char *get_key_combo_name(int *keys, int max)
699 char *ret = talloc_strdup(NULL, "");
700 while (1) {
701 ret = get_key_name(*keys, ret);
702 if (--max && *++keys)
703 talloc_asprintf_append_buffer(ret, "-");
704 else
705 break;
707 return ret;
710 static bool is_abort_cmd(int cmd_id)
712 switch (cmd_id) {
713 case MP_CMD_QUIT:
714 case MP_CMD_PLAY_TREE_STEP:
715 case MP_CMD_PLAY_TREE_UP_STEP:
716 case MP_CMD_PLAY_ALT_SRC_STEP:
717 return true;
719 return false;
722 static void queue_pop(struct cmd_queue *queue)
724 assert(queue->num_cmds > 0);
725 struct mp_cmd *cmd = queue->first;
726 queue->first = cmd->queue_next;
727 queue->num_cmds--;
728 queue->num_abort_cmds -= is_abort_cmd(cmd->id);
731 static void queue_add(struct cmd_queue *queue, struct mp_cmd *cmd,
732 bool at_head)
734 if (!queue->num_cmds) {
735 queue->first = cmd;
736 queue->last = cmd;
737 } else if (at_head) {
738 queue->first->queue_prev = cmd;
739 cmd->queue_next = queue->first;
740 queue->first = cmd;
741 } else {
742 queue->last->queue_next = cmd;
743 cmd->queue_prev = queue->last;
744 queue->last = cmd;
746 queue->num_cmds++;
747 queue->num_abort_cmds += is_abort_cmd(cmd->id);
750 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
751 int read_func(int fd, char *dest, int size),
752 int close_func(int fd))
754 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
755 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
756 "cannot register file descriptor %d.\n", fd);
757 return 0;
759 if (select && fd < 0) {
760 mp_msg(MSGT_INPUT, MSGL_ERR,
761 "Invalid fd %d in mp_input_add_cmd_fd", fd);
762 return 0;
765 ictx->cmd_fds[ictx->num_cmd_fd] = (struct input_fd){
766 .fd = fd,
767 .read_func.cmd = read_func ? read_func : default_cmd_func,
768 .close_func = close_func,
769 .no_select = !select
771 ictx->num_cmd_fd++;
773 return 1;
776 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
778 struct input_fd *cmd_fds = ictx->cmd_fds;
779 unsigned int i;
781 for (i = 0; i < ictx->num_cmd_fd; i++) {
782 if (cmd_fds[i].fd == fd)
783 break;
785 if (i == ictx->num_cmd_fd)
786 return;
787 if (cmd_fds[i].close_func)
788 cmd_fds[i].close_func(cmd_fds[i].fd);
789 talloc_free(cmd_fds[i].buffer);
791 if (i + 1 < ictx->num_cmd_fd)
792 memmove(&cmd_fds[i], &cmd_fds[i + 1],
793 (ictx->num_cmd_fd - i - 1) * sizeof(struct input_fd));
794 ictx->num_cmd_fd--;
797 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
799 struct input_fd *key_fds = ictx->key_fds;
800 unsigned int i;
802 for (i = 0; i < ictx->num_key_fd; i++) {
803 if (key_fds[i].fd == fd)
804 break;
806 if (i == ictx->num_key_fd)
807 return;
808 if (key_fds[i].close_func)
809 key_fds[i].close_func(key_fds[i].fd);
811 if (i + 1 < ictx->num_key_fd)
812 memmove(&key_fds[i], &key_fds[i + 1],
813 (ictx->num_key_fd - i - 1) * sizeof(struct input_fd));
814 ictx->num_key_fd--;
817 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
818 int read_func(void *ctx, int fd),
819 int close_func(int fd), void *ctx)
821 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
822 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
823 "cannot register file descriptor %d.\n", fd);
824 return 0;
826 if (select && fd < 0) {
827 mp_msg(MSGT_INPUT, MSGL_ERR,
828 "Invalid fd %d in mp_input_add_key_fd", fd);
829 return 0;
832 ictx->key_fds[ictx->num_key_fd] = (struct input_fd){
833 .fd = fd,
834 .read_func.key = read_func,
835 .close_func = close_func,
836 .no_select = !select,
837 .ctx = ctx,
839 ictx->num_key_fd++;
841 return 1;
844 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
846 int cmd_num = 0;
848 while (*str == '\n' || *str == '\r' || *str == ' ')
849 ++str;
850 while (*str) {
851 mp_cmd_t *cmd;
852 size_t len = strcspn(str, "\r\n");
853 char *cmdbuf = talloc_size(NULL, len + 1);
854 av_strlcpy(cmdbuf, str, len + 1);
855 cmd = mp_input_parse_cmd(cmdbuf);
856 if (cmd) {
857 mp_input_queue_cmd(ictx, cmd);
858 ++cmd_num;
860 str += len;
861 while (*str == '\n' || *str == '\r' || *str == ' ')
862 ++str;
863 talloc_free(cmdbuf);
865 return cmd_num;
868 mp_cmd_t *mp_input_parse_cmd(char *str)
870 int i, l;
871 int pausing = 0;
872 char *ptr;
873 const mp_cmd_t *cmd_def;
875 // Ignore heading spaces.
876 while (str[0] == ' ' || str[0] == '\t')
877 ++str;
879 if (strncmp(str, "pausing ", 8) == 0) {
880 pausing = 1;
881 str = &str[8];
882 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
883 pausing = 2;
884 str = &str[13];
885 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
886 pausing = 3;
887 str = &str[15];
888 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
889 pausing = 4;
890 str = &str[19];
893 ptr = str + strcspn(str, "\t ");
894 if (*ptr != 0)
895 l = ptr - str;
896 else
897 l = strlen(str);
899 if (l == 0)
900 return NULL;
902 for (i = 0; mp_cmds[i].name != NULL; i++) {
903 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
904 break;
907 if (mp_cmds[i].name == NULL)
908 return NULL;
910 cmd_def = &mp_cmds[i];
912 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
913 *cmd = (mp_cmd_t){
914 .id = cmd_def->id,
915 .name = talloc_strdup(cmd, cmd_def->name),
916 .pausing = pausing,
919 ptr = str;
921 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
922 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
923 ptr++;
924 if (ptr[0] == '\0')
925 break;
926 while (ptr[0] == ' ' || ptr[0] == '\t')
927 ptr++;
928 if (ptr[0] == '\0' || ptr[0] == '#')
929 break;
930 cmd->args[i].type = cmd_def->args[i].type;
931 switch (cmd_def->args[i].type) {
932 case MP_CMD_ARG_INT:
933 errno = 0;
934 cmd->args[i].v.i = atoi(ptr);
935 if (errno != 0) {
936 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
937 "isn't an integer.\n", cmd_def->name, i + 1);
938 goto error;
940 break;
941 case MP_CMD_ARG_FLOAT:
942 errno = 0;
943 cmd->args[i].v.f = atof(ptr);
944 if (errno != 0) {
945 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
946 "isn't a float.\n", cmd_def->name, i + 1);
947 goto error;
949 break;
950 case MP_CMD_ARG_STRING: {
951 int term = ' ';
952 if (*ptr == '\'' || *ptr == '"')
953 term = *ptr++;
954 char *argptr = talloc_size(cmd, strlen(ptr) + 1);
955 cmd->args[i].v.s = argptr;
956 while (1) {
957 if (*ptr == 0) {
958 if (term == ' ')
959 break;
960 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
961 "unterminated.\n", cmd_def->name, i + 1);
962 goto error;
964 if (*ptr == term)
965 break;
966 if (*ptr == '\\')
967 ptr++;
968 if (*ptr != 0)
969 *argptr++ = *ptr++;
971 *argptr = 0;
972 break;
974 case 0:
975 ptr = NULL;
976 break;
977 default:
978 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
981 cmd->nargs = i;
983 int min_args;
984 for (min_args = 0; min_args < MP_CMD_MAX_ARGS
985 && cmd_def->args[min_args].type
986 && !cmd_def->args[min_args].optional; min_args++);
987 if (cmd->nargs < min_args) {
988 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command \"%s\" requires at least %d "
989 "arguments, we found only %d so far.\n", cmd_def->name,
990 min_args, cmd->nargs);
991 goto error;
994 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type; i++) {
995 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(struct mp_cmd_arg));
996 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
997 && cmd_def->args[i].v.s != NULL)
998 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
1001 if (i < MP_CMD_MAX_ARGS)
1002 cmd->args[i].type = 0;
1004 return cmd;
1006 error:
1007 mp_cmd_free(cmd);
1008 return NULL;
1011 #define MP_CMD_MAX_SIZE 4096
1013 static int read_cmd(struct input_fd *mp_fd, char **ret)
1015 char *end;
1016 *ret = NULL;
1018 // Allocate the buffer if it doesn't exist
1019 if (!mp_fd->buffer) {
1020 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
1021 mp_fd->pos = 0;
1022 mp_fd->size = MP_CMD_MAX_SIZE;
1025 // Get some data if needed/possible
1026 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
1027 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
1028 mp_fd->size - 1 - mp_fd->pos);
1029 // Error ?
1030 if (r < 0) {
1031 switch (r) {
1032 case MP_INPUT_ERROR:
1033 case MP_INPUT_DEAD:
1034 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1035 "command file descriptor %d: %s\n",
1036 mp_fd->fd, strerror(errno));
1037 case MP_INPUT_NOTHING:
1038 return r;
1039 case MP_INPUT_RETRY:
1040 continue;
1042 // EOF ?
1043 } else if (r == 0) {
1044 mp_fd->eof = 1;
1045 break;
1047 mp_fd->pos += r;
1048 break;
1051 mp_fd->got_cmd = 0;
1053 while (1) {
1054 int l = 0;
1055 // Find the cmd end
1056 mp_fd->buffer[mp_fd->pos] = '\0';
1057 end = strchr(mp_fd->buffer, '\r');
1058 if (end)
1059 *end = '\n';
1060 end = strchr(mp_fd->buffer, '\n');
1061 // No cmd end ?
1062 if (!end) {
1063 // If buffer is full we must drop all until the next \n
1064 if (mp_fd->size - mp_fd->pos <= 1) {
1065 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
1066 "descriptor %d is full: dropping content.\n",
1067 mp_fd->fd);
1068 mp_fd->pos = 0;
1069 mp_fd->drop = 1;
1071 break;
1073 // We already have a cmd : set the got_cmd flag
1074 else if ((*ret)) {
1075 mp_fd->got_cmd = 1;
1076 break;
1079 l = end - mp_fd->buffer;
1081 // Not dropping : put the cmd in ret
1082 if (!mp_fd->drop)
1083 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1084 else
1085 mp_fd->drop = 0;
1086 mp_fd->pos -= l + 1;
1087 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1090 if (*ret)
1091 return 1;
1092 else
1093 return MP_INPUT_NOTHING;
1096 static int default_cmd_func(int fd, char *buf, int l)
1098 while (1) {
1099 int r = read(fd, buf, l);
1100 // Error ?
1101 if (r < 0) {
1102 if (errno == EINTR)
1103 continue;
1104 else if (errno == EAGAIN)
1105 return MP_INPUT_NOTHING;
1106 return MP_INPUT_ERROR;
1107 // EOF ?
1109 return r;
1113 static int read_wakeup(void *ctx, int fd)
1115 char buf[100];
1116 read(fd, buf, sizeof(buf));
1117 return MP_INPUT_NOTHING;
1121 static char *find_bind_for_key(const struct cmd_bind *binds, int n, int *keys)
1123 int j;
1125 if (n <= 0)
1126 return NULL;
1127 for (j = 0; binds[j].cmd != NULL; j++) {
1128 int found = 1, s;
1129 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1130 if (binds[j].input[s] != keys[s]) {
1131 found = 0;
1132 break;
1135 if (found && binds[j].input[s] == 0 && s == n)
1136 break;
1138 return binds[j].cmd;
1141 static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
1142 char *section)
1144 struct cmd_bind_section *bind_section = ictx->cmd_bind_sections;
1146 if (section == NULL)
1147 section = "default";
1148 while (bind_section) {
1149 if (strcmp(section, bind_section->section) == 0)
1150 return bind_section;
1151 if (bind_section->next == NULL)
1152 break;
1153 bind_section = bind_section->next;
1155 if (bind_section) {
1156 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1157 bind_section = bind_section->next;
1158 } else {
1159 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1160 bind_section = ictx->cmd_bind_sections;
1162 bind_section->cmd_binds = NULL;
1163 bind_section->section = talloc_strdup(bind_section, section);
1164 bind_section->next = NULL;
1165 return bind_section;
1168 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1170 char *cmd = NULL;
1171 mp_cmd_t *ret;
1173 if (ictx->cmd_binds)
1174 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1175 if (ictx->cmd_binds_default && cmd == NULL)
1176 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1177 if (ictx->default_bindings && cmd == NULL)
1178 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1180 if (cmd == NULL) {
1181 char *key_buf = get_key_combo_name(keys, n);
1182 mp_tmsg(MSGT_INPUT, MSGL_WARN,
1183 "No bind found for key '%s'.\n", key_buf);
1184 talloc_free(key_buf);
1185 return NULL;
1187 if (strcmp(cmd, "ignore") == 0)
1188 return NULL;
1189 ret = mp_input_parse_cmd(cmd);
1190 if (!ret) {
1191 char *key_buf = get_key_combo_name(keys, n);
1192 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1193 "Invalid command for bound key '%s': '%s'\n", key_buf, cmd);
1194 talloc_free(key_buf);
1196 return ret;
1200 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1202 unsigned int j;
1203 mp_cmd_t *ret;
1205 /* On normal keyboards shift changes the character code of non-special
1206 * keys, so don't count the modifier separately for those. In other words
1207 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1208 * shift modifier is still kept for special keys like arrow keys.
1210 int unmod = code & ~KEY_MODIFIER_MASK;
1211 if (unmod >= 32 && unmod < MP_KEY_BASE)
1212 code &= ~KEY_MODIFIER_SHIFT;
1214 if (code & MP_KEY_DOWN) {
1215 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1216 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1217 "at the same time\n");
1218 return NULL;
1220 code &= ~MP_KEY_DOWN;
1221 // Check if we don't already have this key as pushed
1222 for (j = 0; j < ictx->num_key_down; j++) {
1223 if (ictx->key_down[j] == code)
1224 break;
1226 if (j != ictx->num_key_down)
1227 return NULL;
1228 ictx->key_down[ictx->num_key_down] = code;
1229 ictx->num_key_down++;
1230 ictx->last_key_down = GetTimer();
1231 ictx->ar_state = 0;
1232 return NULL;
1234 // button released or press of key with no separate down/up events
1235 for (j = 0; j < ictx->num_key_down; j++) {
1236 if (ictx->key_down[j] == code)
1237 break;
1239 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1240 if (doubleclick) {
1241 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1242 if (!ictx->num_key_down
1243 || ictx->key_down[ictx->num_key_down - 1] != btn)
1244 return NULL;
1245 j = ictx->num_key_down - 1;
1246 ictx->key_down[j] = code;
1248 if (j == ictx->num_key_down) { // was not already down; add temporarily
1249 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1250 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1251 "at the same time\n");
1252 return NULL;
1254 ictx->key_down[ictx->num_key_down] = code;
1255 ictx->num_key_down++;
1256 ictx->last_key_down = 1;
1258 // Interpret only maximal point of multibutton event
1259 ret = ictx->last_key_down ?
1260 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1261 : NULL;
1262 if (doubleclick) {
1263 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1264 return ret;
1266 // Remove the key
1267 if (j + 1 < ictx->num_key_down)
1268 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1269 (ictx->num_key_down - (j + 1)) * sizeof(int));
1270 ictx->num_key_down--;
1271 ictx->last_key_down = 0;
1272 ictx->ar_state = -1;
1273 mp_cmd_free(ictx->ar_cmd);
1274 ictx->ar_cmd = NULL;
1275 return ret;
1278 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1280 // No input : autorepeat ?
1281 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1282 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1283 unsigned int t = GetTimer();
1284 // First time : wait delay
1285 if (ictx->ar_state == 0
1286 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1287 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1288 ictx->key_down);
1289 if (!ictx->ar_cmd) {
1290 ictx->ar_state = -1;
1291 return NULL;
1293 ictx->ar_state = 1;
1294 ictx->last_ar = t;
1295 return mp_cmd_clone(ictx->ar_cmd);
1296 // Then send rate / sec event
1297 } else if (ictx->ar_state == 1
1298 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1299 ictx->last_ar = t;
1300 return mp_cmd_clone(ictx->ar_cmd);
1303 return NULL;
1306 void mp_input_feed_key(struct input_ctx *ictx, int code)
1308 ictx->got_new_events = true;
1309 if (code == MP_INPUT_RELEASE_ALL) {
1310 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1311 ictx->num_key_down = 0;
1312 ictx->last_key_down = 0;
1313 return;
1315 struct mp_cmd *cmd = interpret_key(ictx, code);
1316 if (!cmd)
1317 return;
1318 struct cmd_queue *queue = &ictx->key_cmd_queue;
1319 if (queue->num_cmds >= ictx->key_fifo_size &&
1320 (!is_abort_cmd(cmd->id) || queue->num_abort_cmds))
1321 return;
1322 queue_add(queue, cmd, false);
1325 static void read_cmd_fd(struct input_ctx *ictx, struct input_fd *cmd_fd)
1327 int r;
1328 char *text;
1329 while ((r = read_cmd(cmd_fd, &text)) >= 0) {
1330 ictx->got_new_events = true;
1331 struct mp_cmd *cmd = mp_input_parse_cmd(text);
1332 talloc_free(text);
1333 if (cmd)
1334 queue_add(&ictx->control_cmd_queue, cmd, false);
1335 if (!cmd_fd->got_cmd)
1336 return;
1338 if (r == MP_INPUT_ERROR)
1339 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1340 cmd_fd->fd);
1341 else if (r == MP_INPUT_DEAD)
1342 cmd_fd->dead = true;
1345 static void read_key_fd(struct input_ctx *ictx, struct input_fd *key_fd)
1347 int code = key_fd->read_func.key(key_fd->ctx, key_fd->fd);
1348 if (code >= 0 || code == MP_INPUT_RELEASE_ALL) {
1349 mp_input_feed_key(ictx, code);
1350 return;
1353 if (code == MP_INPUT_ERROR)
1354 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1355 "Error on key input file descriptor %d\n", key_fd->fd);
1356 else if (code == MP_INPUT_DEAD) {
1357 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1358 "Dead key input on file descriptor %d\n", key_fd->fd);
1359 key_fd->dead = true;
1364 * \param time time to wait at most for an event in milliseconds
1366 static void read_events(struct input_ctx *ictx, int time)
1368 ictx->got_new_events = false;
1369 struct input_fd *key_fds = ictx->key_fds;
1370 struct input_fd *cmd_fds = ictx->cmd_fds;
1371 for (int i = 0; i < ictx->num_key_fd; i++)
1372 if (key_fds[i].dead) {
1373 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1374 i--;
1375 } else if (time && key_fds[i].no_select)
1376 read_key_fd(ictx, &key_fds[i]);
1377 for (int i = 0; i < ictx->num_cmd_fd; i++)
1378 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1379 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1380 i--;
1381 } else if (time && cmd_fds[i].no_select)
1382 read_cmd_fd(ictx, &cmd_fds[i]);
1383 if (ictx->got_new_events)
1384 time = 0;
1385 #ifdef HAVE_POSIX_SELECT
1386 fd_set fds;
1387 FD_ZERO(&fds);
1388 int max_fd = 0;
1389 for (int i = 0; i < ictx->num_key_fd; i++) {
1390 if (key_fds[i].no_select)
1391 continue;
1392 if (key_fds[i].fd > max_fd)
1393 max_fd = key_fds[i].fd;
1394 FD_SET(key_fds[i].fd, &fds);
1396 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1397 if (cmd_fds[i].no_select)
1398 continue;
1399 if (cmd_fds[i].fd > max_fd)
1400 max_fd = cmd_fds[i].fd;
1401 FD_SET(cmd_fds[i].fd, &fds);
1403 struct timeval tv, *time_val;
1404 if (time >= 0) {
1405 tv.tv_sec = time / 1000;
1406 tv.tv_usec = (time % 1000) * 1000;
1407 time_val = &tv;
1408 } else
1409 time_val = NULL;
1410 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1411 if (errno != EINTR)
1412 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1413 strerror(errno));
1414 FD_ZERO(&fds);
1416 #else
1417 if (time)
1418 usec_sleep(time * 1000);
1419 #endif
1422 for (int i = 0; i < ictx->num_key_fd; i++) {
1423 #ifdef HAVE_POSIX_SELECT
1424 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1425 continue;
1426 #endif
1427 read_key_fd(ictx, &key_fds[i]);
1430 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1431 #ifdef HAVE_POSIX_SELECT
1432 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds))
1433 continue;
1434 #endif
1435 read_cmd_fd(ictx, &cmd_fds[i]);
1439 /* To support blocking file descriptors we don't loop the read over
1440 * every source until it's known to be empty. Instead we use this wrapper
1441 * to run select() again.
1443 static void read_all_fd_events(struct input_ctx *ictx, int time)
1445 while (1) {
1446 read_events(ictx, time);
1447 if (!ictx->got_new_events)
1448 return;
1449 time = 0;
1453 static void read_all_events(struct input_ctx *ictx, int time)
1455 #ifdef CONFIG_COCOA
1456 cocoa_events_read_all_events(ictx, time);
1457 #else
1458 read_all_fd_events(ictx, time);
1459 #endif
1462 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1464 ictx->got_new_events = true;
1465 if (!cmd)
1466 return 0;
1467 queue_add(&ictx->control_cmd_queue, cmd, true);
1468 return 1;
1472 * \param peek_only when set, the returned command stays in the queue.
1473 * Do not free the returned cmd whe you set this!
1475 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1477 if (async_quit_request)
1478 return mp_input_parse_cmd("quit 1");
1480 if (ictx->control_cmd_queue.num_cmds || ictx->key_cmd_queue.num_cmds)
1481 time = 0;
1482 read_all_events(ictx, time);
1483 struct mp_cmd *ret;
1484 struct cmd_queue *queue = &ictx->control_cmd_queue;
1485 if (!queue->num_cmds)
1486 queue = &ictx->key_cmd_queue;
1487 if (!queue->num_cmds) {
1488 ret = check_autorepeat(ictx);
1489 if (!ret)
1490 return NULL;
1491 queue_add(queue, ret, false);
1492 } else
1493 ret = queue->first;
1495 if (!peek_only)
1496 queue_pop(queue);
1498 return ret;
1501 void mp_cmd_free(mp_cmd_t *cmd)
1503 talloc_free(cmd);
1506 mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
1508 mp_cmd_t *ret;
1509 int i;
1511 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1512 ret->name = talloc_strdup(ret, cmd->name);
1513 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type; i++) {
1514 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1515 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1518 return ret;
1521 int mp_input_get_key_from_name(const char *name)
1523 int modifiers = 0;
1524 const char *p;
1525 while ((p = strchr(name, '+'))) {
1526 for (struct key_name *m = modifier_names; m->name; m++)
1527 if (!bstrcasecmp(bstr(m->name),
1528 (struct bstr){(char *)name, p - name})) {
1529 modifiers |= m->key;
1530 goto found;
1532 if (!strcmp(name, "+"))
1533 return '+' + modifiers;
1534 return -1;
1535 found:
1536 name = p + 1;
1539 struct bstr bname = bstr(name);
1541 struct bstr rest;
1542 int code = bstr_decode_utf8(bname, &rest);
1543 if (code >= 0 && rest.len == 0)
1544 return code + modifiers;
1546 if (bstr_startswith0(bname, "0x"))
1547 return strtol(name, NULL, 16) + modifiers;
1549 for (int i = 0; key_names[i].name != NULL; i++) {
1550 if (strcasecmp(key_names[i].name, name) == 0)
1551 return key_names[i].key + modifiers;
1554 return -1;
1557 static int get_input_from_name(char *name, int *keys)
1559 char *end, *ptr;
1560 int n = 0;
1562 ptr = name;
1563 n = 0;
1564 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1565 if (end && end[1] != '\0') {
1566 if (end[1] == '-')
1567 end = &end[1];
1568 end[0] = '\0';
1570 keys[n] = mp_input_get_key_from_name(ptr);
1571 if (keys[n] < 0)
1572 return 0;
1573 n++;
1574 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1575 ptr = &end[1];
1576 else
1577 break;
1579 keys[n] = 0;
1580 return 1;
1583 #define SPACE_CHAR " \n\r\t"
1585 static void bind_keys(struct input_ctx *ictx,
1586 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1588 int i = 0, j;
1589 struct cmd_bind *bind = NULL;
1590 struct cmd_bind_section *bind_section = NULL;
1591 char *section = NULL, *p;
1593 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1594 *p = 0;
1595 section = ++cmd;
1596 cmd = ++p;
1597 // Jump beginning space
1598 cmd += strspn(cmd, SPACE_CHAR);
1600 bind_section = get_bind_section(ictx, section);
1602 if (bind_section->cmd_binds) {
1603 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1604 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1605 /* NOTHING */;
1606 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1607 bind = &bind_section->cmd_binds[i];
1608 break;
1613 if (!bind) {
1614 bind_section->cmd_binds = talloc_realloc(bind_section,
1615 bind_section->cmd_binds,
1616 struct cmd_bind, i + 2);
1617 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(struct cmd_bind));
1618 bind = &bind_section->cmd_binds[i];
1620 talloc_free(bind->cmd);
1621 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1622 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1625 static int parse_config(struct input_ctx *ictx, char *file)
1627 int fd = open(file, O_RDONLY);
1629 if (fd < 0) {
1630 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1631 file, strerror(errno));
1632 return 0;
1635 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1638 unsigned char buffer[512];
1639 struct bstr buf = { buffer, 0 };
1640 bool eof = false, comments = false;
1641 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1];
1643 while (1) {
1644 if (buf.start != buffer) {
1645 memmove(buffer, buf.start, buf.len);
1646 buf.start = buffer;
1648 if (!eof && buf.len < sizeof(buffer)) {
1649 int r = read(fd, buffer + buf.len, sizeof(buffer) - buf.len);
1650 if (r < 0) {
1651 if (errno == EINTR)
1652 continue;
1653 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1654 "input config file %s: %s\n", file, strerror(errno));
1655 close(fd);
1656 return 0;
1657 } else if (r == 0) {
1658 eof = true;
1659 } else {
1660 buf.len += r;
1661 continue;
1664 if (buf.len == 0) {
1665 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1666 "%d binds\n", file, n_binds);
1667 close(fd);
1668 return 1;
1670 if (comments) {
1671 int idx = bstrchr(buf, '\n');
1672 if (idx >= 0) {
1673 buf = bstr_cut(buf, idx + 1);
1674 comments = false;
1675 } else
1676 buf = bstr_cut(buf, buf.len);
1677 continue;
1679 buf = bstr_lstrip(buf);
1680 if (buf.start != buffer)
1681 continue;
1682 if (buf.start[0] == '#') {
1683 comments = true;
1684 continue;
1686 int eol = bstrchr(buf, '\n');
1687 if (eol < 0) {
1688 if (eof) {
1689 eol = buf.len;
1690 } else {
1691 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1692 "Key binding is too long: %.*s\n", BSTR_P(buf));
1693 comments = true;
1694 continue;
1697 struct bstr line = bstr_splice(buf, 0, eol);
1698 buf = bstr_cut(buf, eol);
1699 struct bstr command;
1700 // Find the key name starting a line
1701 struct bstr keyname = bstr_split(line, SPACE_CHAR, &command);
1702 command = bstr_strip(command);
1703 if (command.len == 0) {
1704 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1705 "Unfinished key binding: %.*s\n", BSTR_P(line));
1706 continue;
1708 char *name = bstrdup0(NULL, keyname);
1709 if (!get_input_from_name(name, keys)) {
1710 talloc_free(name);
1711 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1712 "Unknown key '%.*s'\n", BSTR_P(keyname));
1713 continue;
1715 talloc_free(name);
1716 char *cmd = bstrdup0(NULL, command);
1717 bind_keys(ictx, keys, cmd);
1718 n_binds++;
1719 talloc_free(cmd);
1723 void mp_input_set_section(struct input_ctx *ictx, char *name)
1725 struct cmd_bind_section *bind_section = NULL;
1727 ictx->cmd_binds = NULL;
1728 ictx->cmd_binds_default = NULL;
1729 talloc_free(ictx->section);
1730 if (name)
1731 ictx->section = talloc_strdup(ictx, name);
1732 else
1733 ictx->section = talloc_strdup(ictx, "default");
1734 if ((bind_section = get_bind_section(ictx, ictx->section)))
1735 ictx->cmd_binds = bind_section->cmd_binds;
1736 if (strcmp(ictx->section, "default") == 0)
1737 return;
1738 if ((bind_section = get_bind_section(ictx, NULL)))
1739 ictx->cmd_binds_default = bind_section->cmd_binds;
1742 char *mp_input_get_section(struct input_ctx *ictx)
1744 return ictx->section;
1747 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1749 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1750 *ictx = (struct input_ctx){
1751 .key_fifo_size = input_conf->key_fifo_size,
1752 .ar_state = -1,
1753 .ar_delay = input_conf->ar_delay,
1754 .ar_rate = input_conf->ar_rate,
1755 .default_bindings = input_conf->default_bindings,
1756 .wakeup_pipe = {-1, -1},
1759 #ifdef CONFIG_COCOA
1760 cocoa_events_init(ictx, read_all_fd_events);
1761 #endif
1763 #ifndef __MINGW32__
1764 long ret = pipe(ictx->wakeup_pipe);
1765 for (int i = 0; i < 2 && ret >= 0; i++) {
1766 ret = fcntl(ictx->wakeup_pipe[i], F_GETFL);
1767 if (ret < 0)
1768 break;
1769 ret = fcntl(ictx->wakeup_pipe[i], F_SETFL, ret | O_NONBLOCK);
1771 if (ret < 0)
1772 mp_msg(MSGT_INPUT, MSGL_ERR,
1773 "Failed to initialize wakeup pipe: %s\n", strerror(errno));
1774 else
1775 mp_input_add_key_fd(ictx, ictx->wakeup_pipe[0], true, read_wakeup,
1776 NULL, NULL);
1777 #endif
1779 char *file;
1780 char *config_file = input_conf->config_file;
1781 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1782 if (!file)
1783 return ictx;
1785 if (!parse_config(ictx, file)) {
1786 // free file if it was allocated by get_path(),
1787 // before it gets overwritten
1788 if (file != config_file)
1789 free(file);
1790 // Try global conf dir
1791 file = MPLAYER_CONFDIR "/input.conf";
1792 if (!parse_config(ictx, file))
1793 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1794 "input config\n");
1795 } else {
1796 // free file if it was allocated by get_path()
1797 if (file != config_file)
1798 free(file);
1801 #ifdef CONFIG_JOYSTICK
1802 if (input_conf->use_joystick) {
1803 int fd = mp_input_joystick_init(input_conf->js_dev);
1804 if (fd < 0)
1805 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1806 else
1807 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1808 close, NULL);
1810 #endif
1812 #ifdef CONFIG_LIRC
1813 if (input_conf->use_lirc) {
1814 int fd = mp_input_lirc_init();
1815 if (fd > 0)
1816 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1817 mp_input_lirc_close);
1819 #endif
1821 #ifdef CONFIG_LIRCC
1822 if (input_conf->use_lircc) {
1823 int fd = lircc_init("mplayer", NULL);
1824 if (fd >= 0)
1825 mp_input_add_cmd_fd(ictx, fd, 1, NULL, lircc_cleanup);
1827 #endif
1829 #ifdef CONFIG_APPLE_REMOTE
1830 if (input_conf->use_ar) {
1831 if (mp_input_ar_init() < 0)
1832 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1833 else
1834 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1835 mp_input_ar_close, NULL);
1837 #endif
1839 #ifdef CONFIG_APPLE_IR
1840 if (input_conf->use_ar) {
1841 int fd = mp_input_appleir_init(input_conf->ar_dev);
1842 if (fd < 0)
1843 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1844 else
1845 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1846 close, NULL);
1848 #endif
1850 if (input_conf->in_file) {
1851 int mode = O_RDONLY;
1852 #ifndef __MINGW32__
1853 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1854 // Note that on Windows due to how the API works, using RDONLY should
1855 // be ok.
1856 struct stat st;
1857 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1858 mode = O_RDWR;
1859 mode |= O_NONBLOCK;
1860 #endif
1861 int in_file_fd = open(input_conf->in_file, mode);
1862 if (in_file_fd >= 0)
1863 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, close);
1864 else
1865 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1866 input_conf->in_file, strerror(errno));
1869 return ictx;
1872 void mp_input_uninit(struct input_ctx *ictx)
1874 #ifdef CONFIG_COCOA
1875 cocoa_events_uninit();
1876 #endif
1878 if (!ictx)
1879 return;
1881 for (int i = 0; i < ictx->num_key_fd; i++) {
1882 if (ictx->key_fds[i].close_func)
1883 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1885 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1886 if (ictx->cmd_fds[i].close_func)
1887 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1889 for (int i = 0; i < 2; i++)
1890 if (ictx->wakeup_pipe[i] != -1)
1891 close(ictx->wakeup_pipe[i]);
1892 talloc_free(ictx);
1895 void mp_input_register_options(m_config_t *cfg)
1897 m_config_register_options(cfg, mp_input_opts);
1900 static int print_key_list(m_option_t *cfg)
1902 int i;
1903 printf("\n");
1904 for (i = 0; key_names[i].name != NULL; i++)
1905 printf("%s\n", key_names[i].name);
1906 exit(0);
1909 static int print_cmd_list(m_option_t *cfg)
1911 const mp_cmd_t *cmd;
1912 int i, j;
1913 const char *type;
1915 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1916 printf("%-20.20s", cmd->name);
1917 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type; j++) {
1918 switch (cmd->args[j].type) {
1919 case MP_CMD_ARG_INT:
1920 type = "Integer";
1921 break;
1922 case MP_CMD_ARG_FLOAT:
1923 type = "Float";
1924 break;
1925 case MP_CMD_ARG_STRING:
1926 type = "String";
1927 break;
1928 default:
1929 type = "??";
1931 if (cmd->args[j].optional)
1932 printf(" [%s]", type);
1933 else
1934 printf(" %s", type);
1936 printf("\n");
1938 exit(0);
1941 void mp_input_wakeup(struct input_ctx *ictx)
1943 if (ictx->wakeup_pipe[1] >= 0)
1944 write(ictx->wakeup_pipe[1], &(char){0}, 1);
1948 * \param time time to wait for an interruption in milliseconds
1950 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1952 for (int i = 0; ; i++) {
1953 if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds ||
1954 ictx->control_cmd_queue.num_abort_cmds) {
1955 mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to "
1956 "another file. Aborting current processing.\n");
1957 return true;
1959 if (i)
1960 return false;
1961 read_all_events(ictx, time);