x11: input: add print and menu keys
[mplayer.git] / input / input.c
blob6f3335d8db29f1e73bfd4a8b2d37a69b8a63121a
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 #define MP_MAX_KEY_DOWN 32
64 struct cmd_bind {
65 int input[MP_MAX_KEY_DOWN + 1];
66 char *cmd;
69 struct key_name {
70 int key;
71 char *name;
74 /// This array defines all known commands.
75 /// The first field is an id used to recognize the command without too many strcmp.
76 /// The second is obviously the command name.
77 /// The third is the minimum number of arguments this command needs.
78 /// Then comes the definition of each argument, terminated with an arg of type -1.
79 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
80 /// the last one) which is actually 9.
82 /// For the args, the first field is the type (actually int, float or string), the second
83 /// is the default value wich is used for optional arguments
85 static const mp_cmd_t mp_cmds[] = {
86 #ifdef CONFIG_RADIO
87 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
88 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
89 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
90 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
91 #endif
92 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
93 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
94 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
95 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
96 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
97 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
98 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
99 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
100 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
101 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
102 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
103 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
104 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
105 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
106 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
109 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
112 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
113 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
115 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
116 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
119 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
120 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
121 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
122 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
123 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
124 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
125 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
126 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
127 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
128 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
131 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
132 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
133 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
134 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
135 #ifdef CONFIG_ASS
136 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
137 #endif
138 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
139 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
140 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
141 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
142 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
143 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
144 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
145 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
146 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
147 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
152 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
153 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
154 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
155 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
156 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
157 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
158 #ifdef CONFIG_TV
159 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
160 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
161 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
162 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
163 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
164 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
165 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
166 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
167 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
168 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
170 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
171 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
172 #endif
173 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
174 #ifdef CONFIG_DVBIN
175 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
176 #endif
177 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
178 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
181 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
182 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
186 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
187 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
188 { MP_CMD_CAPTURING, "capturing", 0, { {-1,{0}} } },
189 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
190 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
191 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
193 #ifdef CONFIG_DVDNAV
194 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
195 #endif
197 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
198 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
199 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
200 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
201 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
202 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
203 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
204 { MP_CMD_STEP_PROPERTY_OSD, "step_property_osd", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
206 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
207 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
209 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
210 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
211 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
212 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
213 { MP_CMD_AF_CMDLINE, "af_cmdline", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
215 { 0, NULL, 0, {} }
218 /// The names of the keys as used in input.conf
219 /// If you add some new keys, you also need to add them here
221 static const struct key_name key_names[] = {
222 { ' ', "SPACE" },
223 { '#', "SHARP" },
224 { KEY_ENTER, "ENTER" },
225 { KEY_TAB, "TAB" },
226 { KEY_BACKSPACE, "BS" },
227 { KEY_DELETE, "DEL" },
228 { KEY_INSERT, "INS" },
229 { KEY_HOME, "HOME" },
230 { KEY_END, "END" },
231 { KEY_PAGE_UP, "PGUP" },
232 { KEY_PAGE_DOWN, "PGDWN" },
233 { KEY_ESC, "ESC" },
234 { KEY_PRINT, "PRINT" },
235 { KEY_RIGHT, "RIGHT" },
236 { KEY_LEFT, "LEFT" },
237 { KEY_DOWN, "DOWN" },
238 { KEY_UP, "UP" },
239 { KEY_F+1, "F1" },
240 { KEY_F+2, "F2" },
241 { KEY_F+3, "F3" },
242 { KEY_F+4, "F4" },
243 { KEY_F+5, "F5" },
244 { KEY_F+6, "F6" },
245 { KEY_F+7, "F7" },
246 { KEY_F+8, "F8" },
247 { KEY_F+9, "F9" },
248 { KEY_F+10, "F10" },
249 { KEY_F+11, "F11" },
250 { KEY_F+12, "F12" },
251 { KEY_KP0, "KP0" },
252 { KEY_KP1, "KP1" },
253 { KEY_KP2, "KP2" },
254 { KEY_KP3, "KP3" },
255 { KEY_KP4, "KP4" },
256 { KEY_KP5, "KP5" },
257 { KEY_KP6, "KP6" },
258 { KEY_KP7, "KP7" },
259 { KEY_KP8, "KP8" },
260 { KEY_KP9, "KP9" },
261 { KEY_KPDEL, "KP_DEL" },
262 { KEY_KPDEC, "KP_DEC" },
263 { KEY_KPINS, "KP_INS" },
264 { KEY_KPENTER, "KP_ENTER" },
265 { MOUSE_BTN0, "MOUSE_BTN0" },
266 { MOUSE_BTN1, "MOUSE_BTN1" },
267 { MOUSE_BTN2, "MOUSE_BTN2" },
268 { MOUSE_BTN3, "MOUSE_BTN3" },
269 { MOUSE_BTN4, "MOUSE_BTN4" },
270 { MOUSE_BTN5, "MOUSE_BTN5" },
271 { MOUSE_BTN6, "MOUSE_BTN6" },
272 { MOUSE_BTN7, "MOUSE_BTN7" },
273 { MOUSE_BTN8, "MOUSE_BTN8" },
274 { MOUSE_BTN9, "MOUSE_BTN9" },
275 { MOUSE_BTN10, "MOUSE_BTN10" },
276 { MOUSE_BTN11, "MOUSE_BTN11" },
277 { MOUSE_BTN12, "MOUSE_BTN12" },
278 { MOUSE_BTN13, "MOUSE_BTN13" },
279 { MOUSE_BTN14, "MOUSE_BTN14" },
280 { MOUSE_BTN15, "MOUSE_BTN15" },
281 { MOUSE_BTN16, "MOUSE_BTN16" },
282 { MOUSE_BTN17, "MOUSE_BTN17" },
283 { MOUSE_BTN18, "MOUSE_BTN18" },
284 { MOUSE_BTN19, "MOUSE_BTN19" },
285 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
286 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
287 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
288 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
289 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
290 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
291 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
292 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
293 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
294 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
295 { MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
296 { MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
297 { MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
298 { MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
299 { MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
300 { MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
301 { MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
302 { MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
303 { MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
304 { MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
305 { JOY_AXIS1_MINUS, "JOY_UP" },
306 { JOY_AXIS1_PLUS, "JOY_DOWN" },
307 { JOY_AXIS0_MINUS, "JOY_LEFT" },
308 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
310 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
311 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
312 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
313 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
314 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
315 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
316 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
317 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
318 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
319 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
320 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
321 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
322 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
323 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
324 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
325 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
326 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
327 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
328 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
329 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
331 { JOY_BTN0, "JOY_BTN0" },
332 { JOY_BTN1, "JOY_BTN1" },
333 { JOY_BTN2, "JOY_BTN2" },
334 { JOY_BTN3, "JOY_BTN3" },
335 { JOY_BTN4, "JOY_BTN4" },
336 { JOY_BTN5, "JOY_BTN5" },
337 { JOY_BTN6, "JOY_BTN6" },
338 { JOY_BTN7, "JOY_BTN7" },
339 { JOY_BTN8, "JOY_BTN8" },
340 { JOY_BTN9, "JOY_BTN9" },
342 { AR_PLAY, "AR_PLAY" },
343 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
344 { AR_NEXT, "AR_NEXT" },
345 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
346 { AR_PREV, "AR_PREV" },
347 { AR_PREV_HOLD, "AR_PREV_HOLD" },
348 { AR_MENU, "AR_MENU" },
349 { AR_MENU_HOLD, "AR_MENU_HOLD" },
350 { AR_VUP, "AR_VUP" },
351 { AR_VDOWN, "AR_VDOWN" },
353 { KEY_POWER, "POWER" },
354 { KEY_MENU, "MENU" },
355 { KEY_PLAY, "PLAY" },
356 { KEY_PAUSE, "PAUSE" },
357 { KEY_PLAYPAUSE, "PLAYPAUSE" },
358 { KEY_STOP, "STOP" },
359 { KEY_FORWARD, "FORWARD" },
360 { KEY_REWIND, "REWIND" },
361 { KEY_NEXT, "NEXT" },
362 { KEY_PREV, "PREV" },
363 { KEY_VOLUME_UP, "VOLUME_UP" },
364 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
365 { KEY_MUTE, "MUTE" },
367 // These are kept for backward compatibility
368 { KEY_PAUSE, "XF86_PAUSE" },
369 { KEY_STOP, "XF86_STOP" },
370 { KEY_PREV, "XF86_PREV" },
371 { KEY_NEXT, "XF86_NEXT" },
373 { KEY_CLOSE_WIN, "CLOSE_WIN" },
375 { 0, NULL }
378 struct key_name modifier_names[] = {
379 { KEY_MODIFIER_SHIFT, "Shift" },
380 { KEY_MODIFIER_CTRL, "Ctrl" },
381 { KEY_MODIFIER_ALT, "Alt" },
382 { KEY_MODIFIER_META, "Meta" },
383 { 0 }
386 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
388 // This is the default binding. The content of input.conf overrides these.
389 // The first arg is a null terminated array of key codes.
390 // The second is the command
392 static const struct cmd_bind def_cmd_binds[] = {
394 { { MOUSE_BTN0_DBL, 0 }, "vo_fullscreen" },
395 { { MOUSE_BTN2, 0 }, "pause" },
396 { { MOUSE_BTN3, 0 }, "seek 10" },
397 { { MOUSE_BTN4, 0 }, "seek -10" },
398 { { MOUSE_BTN5, 0 }, "volume 1" },
399 { { MOUSE_BTN6, 0 }, "volume -1" },
401 #ifdef CONFIG_DVDNAV
402 { { KEY_KP8, 0 }, "dvdnav up" }, // up
403 { { KEY_KP2, 0 }, "dvdnav down" }, // down
404 { { KEY_KP4, 0 }, "dvdnav left" }, // left
405 { { KEY_KP6, 0 }, "dvdnav right" }, // right
406 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
407 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
408 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
409 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
410 #endif
412 { { KEY_RIGHT, 0 }, "seek 10" },
413 { { KEY_LEFT, 0 }, "seek -10" },
414 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
415 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
416 { { KEY_UP, 0 }, "seek 60" },
417 { { KEY_DOWN, 0 }, "seek -60" },
418 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
419 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
420 { { KEY_PAGE_UP, 0 }, "seek 600" },
421 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
422 { { '+', 0 }, "audio_delay 0.100" },
423 { { '-', 0 }, "audio_delay -0.100" },
424 { { '[', 0 }, "speed_mult 0.9091" },
425 { { ']', 0 }, "speed_mult 1.1" },
426 { { '{', 0 }, "speed_mult 0.5" },
427 { { '}', 0 }, "speed_mult 2.0" },
428 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
429 { { 'q', 0 }, "quit" },
430 { { KEY_ESC, 0 }, "quit" },
431 { { 'p', 0 }, "pause" },
432 { { ' ', 0 }, "pause" },
433 { { '.', 0 }, "frame_step" },
434 { { KEY_HOME, 0 }, "pt_up_step 1" },
435 { { KEY_END, 0 }, "pt_up_step -1" },
436 { { '>', 0 }, "pt_step 1" },
437 { { KEY_ENTER, 0 }, "pt_step 1 1" },
438 { { '<', 0 }, "pt_step -1" },
439 { { KEY_INS, 0 }, "alt_src_step 1" },
440 { { KEY_DEL, 0 }, "alt_src_step -1" },
441 { { 'o', 0 }, "osd" },
442 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
443 { { 'P', 0 }, "osd_show_progression" },
444 { { 'z', 0 }, "sub_delay -0.1" },
445 { { 'x', 0 }, "sub_delay +0.1" },
446 { { 'g', 0 }, "sub_step -1" },
447 { { 'y', 0 }, "sub_step +1" },
448 { { '9', 0 }, "volume -1" },
449 { { '/', 0 }, "volume -1" },
450 { { '0', 0 }, "volume 1" },
451 { { '*', 0 }, "volume 1" },
452 { { '(', 0 }, "balance -0.1" },
453 { { ')', 0 }, "balance 0.1" },
454 { { 'm', 0 }, "mute" },
455 { { '1', 0 }, "contrast -1" },
456 { { '2', 0 }, "contrast 1" },
457 { { '3', 0 }, "brightness -1" },
458 { { '4', 0 }, "brightness 1" },
459 { { '5', 0 }, "hue -1" },
460 { { '6', 0 }, "hue 1" },
461 { { '7', 0 }, "saturation -1" },
462 { { '8', 0 }, "saturation 1" },
463 { { 'd', 0 }, "frame_drop" },
464 { { 'D', 0 }, "step_property_osd deinterlace" },
465 { { 'c', 0 }, "step_property_osd colormatrix" },
466 { { 'r', 0 }, "sub_pos -1" },
467 { { 't', 0 }, "sub_pos +1" },
468 { { 'a', 0 }, "sub_alignment" },
469 { { 'v', 0 }, "sub_visibility" },
470 { { 'V', 0 }, "step_property_osd ass_vsfilter_aspect_compat" },
471 { { 'j', 0 }, "sub_select" },
472 { { 'J', 0 }, "sub_select -3" },
473 { { 'F', 0 }, "forced_subs_only" },
474 { { '#', 0 }, "switch_audio" },
475 { { '_', 0 }, "step_property switch_video" },
476 { { KEY_TAB, 0 }, "step_property switch_program" },
477 { { 'i', 0 }, "edl_mark" },
478 #ifdef CONFIG_TV
479 { { 'h', 0 }, "tv_step_channel 1" },
480 { { 'k', 0 }, "tv_step_channel -1" },
481 { { 'n', 0 }, "tv_step_norm" },
482 { { 'u', 0 }, "tv_step_chanlist" },
483 #endif
484 { { 'X', 0 }, "step_property teletext_mode 1" },
485 { { 'W', 0 }, "step_property teletext_page 1" },
486 { { 'Q', 0 }, "step_property teletext_page -1" },
487 #ifdef CONFIG_JOYSTICK
488 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
489 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
490 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
491 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
492 { { JOY_BTN0, 0 }, "pause" },
493 { { JOY_BTN1, 0 }, "osd" },
494 { { JOY_BTN2, 0 }, "volume 1"},
495 { { JOY_BTN3, 0 }, "volume -1"},
496 #endif
497 #ifdef CONFIG_APPLE_REMOTE
498 { { AR_PLAY, 0}, "pause" },
499 { { AR_PLAY_HOLD, 0}, "quit" },
500 { { AR_NEXT, 0 }, "seek 30" },
501 { { AR_NEXT_HOLD, 0 }, "seek 120" },
502 { { AR_PREV, 0 }, "seek -10" },
503 { { AR_PREV_HOLD, 0 }, "seek -120" },
504 { { AR_MENU, 0 }, "osd" },
505 { { AR_MENU_HOLD, 0 }, "mute" },
506 { { AR_VUP, 0 }, "volume 1"},
507 { { AR_VDOWN, 0 }, "volume -1"},
508 #endif
509 { { 'T', 0 }, "vo_ontop" },
510 { { 'f', 0 }, "vo_fullscreen" },
511 { { 'C', 0 }, "step_property_osd capturing" },
512 { { 's', 0 }, "screenshot 0" },
513 { { 'S', 0 }, "screenshot 1" },
514 { { KEY_MODIFIER_ALT + 's', 0 }, "screenshot 0 1" },
515 { { KEY_MODIFIER_ALT + 'S', 0 }, "screenshot 1 1" },
516 { { 'w', 0 }, "panscan -0.1" },
517 { { 'e', 0 }, "panscan +0.1" },
519 { { KEY_POWER, 0 }, "quit" },
520 { { KEY_MENU, 0 }, "osd" },
521 { { KEY_PLAY, 0 }, "pause" },
522 { { KEY_PAUSE, 0 }, "pause" },
523 { { KEY_PLAYPAUSE, 0 }, "pause" },
524 { { KEY_STOP, 0 }, "quit" },
525 { { KEY_FORWARD, 0 }, "seek 60" },
526 { { KEY_REWIND, 0 }, "seek -60" },
527 { { KEY_NEXT, 0 }, "pt_step 1" },
528 { { KEY_PREV, 0 }, "pt_step -1" },
529 { { KEY_VOLUME_UP, 0 }, "volume 1" },
530 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
531 { { KEY_MUTE, 0 }, "mute" },
533 { { KEY_CLOSE_WIN, 0 }, "quit" },
535 { { '!', 0 }, "seek_chapter -1" },
536 { { '@', 0 }, "seek_chapter 1" },
537 { { 'A', 0 }, "switch_angle 1" },
538 { { 'U', 0 }, "stop" },
540 { { 0 }, NULL }
544 #ifndef MP_MAX_KEY_FD
545 #define MP_MAX_KEY_FD 10
546 #endif
548 #ifndef MP_MAX_CMD_FD
549 #define MP_MAX_CMD_FD 10
550 #endif
552 struct input_fd {
553 int fd;
554 union {
555 int (*key)(void *ctx, int fd);
556 int (*cmd)(int fd, char *dest, int size);
557 } read_func;
558 int (*close_func)(int fd);
559 void *ctx;
560 unsigned eof : 1;
561 unsigned drop : 1;
562 unsigned dead : 1;
563 unsigned got_cmd : 1;
564 unsigned no_select : 1;
565 // These fields are for the cmd fds.
566 char *buffer;
567 int pos, size;
570 struct cmd_bind_section {
571 struct cmd_bind *cmd_binds;
572 char *section;
573 struct cmd_bind_section *next;
576 struct cmd_queue {
577 struct mp_cmd *first;
578 struct mp_cmd *last;
579 int num_cmds;
580 int num_abort_cmds;
583 struct input_ctx {
584 // Autorepeat stuff
585 short ar_state;
586 mp_cmd_t *ar_cmd;
587 unsigned int last_ar;
588 // Autorepeat config
589 unsigned int ar_delay;
590 unsigned int ar_rate;
591 // Maximum number of queued commands from keypresses (limit to avoid
592 // repeated slow commands piling up)
593 int key_fifo_size;
595 // these are the keys currently down
596 int key_down[MP_MAX_KEY_DOWN];
597 unsigned int num_key_down;
598 unsigned int last_key_down;
600 bool default_bindings;
601 // List of command binding sections
602 struct cmd_bind_section *cmd_bind_sections;
603 // Name of currently used command section
604 char *section;
605 // The command binds of current section
606 struct cmd_bind *cmd_binds;
607 struct cmd_bind *cmd_binds_default;
609 // Used to track whether we managed to read something while checking
610 // events sources. If yes, the sources may have more queued.
611 bool got_new_events;
613 struct input_fd key_fds[MP_MAX_KEY_FD];
614 unsigned int num_key_fd;
616 struct input_fd cmd_fds[MP_MAX_CMD_FD];
617 unsigned int num_cmd_fd;
619 struct cmd_queue key_cmd_queue;
620 struct cmd_queue control_cmd_queue;
624 int async_quit_request;
626 static int print_key_list(m_option_t *cfg);
627 static int print_cmd_list(m_option_t *cfg);
629 // Our command line options
630 static const m_option_t input_conf[] = {
631 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
632 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
633 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
634 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
635 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
636 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
637 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
638 OPT_STRING("file", input.in_file, CONF_GLOBAL),
639 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
640 { NULL, NULL, 0, 0, 0, 0, NULL}
643 static const m_option_t mp_input_opts[] = {
644 { "input", (void *)&input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
645 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
646 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
647 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
648 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
649 { NULL, NULL, 0, 0, 0, 0, NULL}
652 static int default_cmd_func(int fd, char *buf, int l);
654 // Encode the unicode codepoint as UTF-8, and append to the end of the
655 // talloc'ed buffer.
656 static char *append_utf8_buffer(char *buffer, uint32_t codepoint)
658 char data[8];
659 uint8_t tmp;
660 char *output = data;
661 PUT_UTF8(codepoint, tmp, *output++ = tmp;);
662 return talloc_strndup_append_buffer(buffer, data, output - data);
665 static char *get_key_name(int key, char *ret)
667 for (int i = 0; modifier_names[i].name; i++) {
668 if (modifier_names[i].key & key) {
669 ret = talloc_asprintf_append_buffer(ret, "%s+",
670 modifier_names[i].name);
671 key -= modifier_names[i].key;
674 for (int i = 0; key_names[i].name != NULL; i++) {
675 if (key_names[i].key == key)
676 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
679 // printable, and valid unicode range
680 if (key >= 32 && key <= 0x10FFFF)
681 return append_utf8_buffer(ret, key);
683 // Print the hex key code
684 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
687 static char *get_key_combo_name(int *keys, int max)
689 char *ret = talloc_strdup(NULL, "");
690 while (1) {
691 ret = get_key_name(*keys, ret);
692 if (--max && *++keys)
693 talloc_asprintf_append_buffer(ret, "-");
694 else
695 break;
697 return ret;
700 static bool is_abort_cmd(int cmd_id)
702 switch (cmd_id) {
703 case MP_CMD_QUIT:
704 case MP_CMD_PLAY_TREE_STEP:
705 case MP_CMD_PLAY_TREE_UP_STEP:
706 case MP_CMD_PLAY_ALT_SRC_STEP:
707 return true;
709 return false;
712 static void queue_pop(struct cmd_queue *queue)
714 assert(queue->num_cmds > 0);
715 struct mp_cmd *cmd = queue->first;
716 queue->first = cmd->queue_next;
717 queue->num_cmds--;
718 queue->num_abort_cmds -= is_abort_cmd(cmd->id);
721 static void queue_add(struct cmd_queue *queue, struct mp_cmd *cmd,
722 bool at_head)
724 if (!queue->num_cmds) {
725 queue->first = cmd;
726 queue->last = cmd;
727 } else if (at_head) {
728 queue->first->queue_prev = cmd;
729 cmd->queue_next = queue->first;
730 queue->first = cmd;
731 } else {
732 queue->last->queue_next = cmd;
733 cmd->queue_prev = queue->last;
734 queue->last = cmd;
736 queue->num_cmds++;
737 queue->num_abort_cmds += is_abort_cmd(cmd->id);
740 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
741 int read_func(int fd, char *dest, int size),
742 int close_func(int fd))
744 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
745 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
746 "cannot register file descriptor %d.\n", fd);
747 return 0;
749 if (select && fd < 0) {
750 mp_msg(MSGT_INPUT, MSGL_ERR,
751 "Invalid fd %d in mp_input_add_cmd_fd", fd);
752 return 0;
755 ictx->cmd_fds[ictx->num_cmd_fd] = (struct input_fd){
756 .fd = fd,
757 .read_func.cmd = read_func ? read_func : default_cmd_func,
758 .close_func = close_func,
759 .no_select = !select
761 ictx->num_cmd_fd++;
763 return 1;
766 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
768 struct input_fd *cmd_fds = ictx->cmd_fds;
769 unsigned int i;
771 for (i = 0; i < ictx->num_cmd_fd; i++) {
772 if (cmd_fds[i].fd == fd)
773 break;
775 if (i == ictx->num_cmd_fd)
776 return;
777 if (cmd_fds[i].close_func)
778 cmd_fds[i].close_func(cmd_fds[i].fd);
779 talloc_free(cmd_fds[i].buffer);
781 if (i + 1 < ictx->num_cmd_fd)
782 memmove(&cmd_fds[i], &cmd_fds[i + 1],
783 (ictx->num_cmd_fd - i - 1) * sizeof(struct input_fd));
784 ictx->num_cmd_fd--;
787 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
789 struct input_fd *key_fds = ictx->key_fds;
790 unsigned int i;
792 for (i = 0; i < ictx->num_key_fd; i++) {
793 if (key_fds[i].fd == fd)
794 break;
796 if (i == ictx->num_key_fd)
797 return;
798 if (key_fds[i].close_func)
799 key_fds[i].close_func(key_fds[i].fd);
801 if (i + 1 < ictx->num_key_fd)
802 memmove(&key_fds[i], &key_fds[i + 1],
803 (ictx->num_key_fd - i - 1) * sizeof(struct input_fd));
804 ictx->num_key_fd--;
807 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
808 int read_func(void *ctx, int fd),
809 int close_func(int fd), void *ctx)
811 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
812 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
813 "cannot register file descriptor %d.\n", fd);
814 return 0;
816 if (select && fd < 0) {
817 mp_msg(MSGT_INPUT, MSGL_ERR,
818 "Invalid fd %d in mp_input_add_key_fd", fd);
819 return 0;
822 ictx->key_fds[ictx->num_key_fd] = (struct input_fd){
823 .fd = fd,
824 .read_func.key = read_func,
825 .close_func = close_func,
826 .no_select = !select,
827 .ctx = ctx,
829 ictx->num_key_fd++;
831 return 1;
834 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
836 int cmd_num = 0;
838 while (*str == '\n' || *str == '\r' || *str == ' ')
839 ++str;
840 while (*str) {
841 mp_cmd_t *cmd;
842 size_t len = strcspn(str, "\r\n");
843 char *cmdbuf = talloc_size(NULL, len + 1);
844 av_strlcpy(cmdbuf, str, len + 1);
845 cmd = mp_input_parse_cmd(cmdbuf);
846 if (cmd) {
847 mp_input_queue_cmd(ictx, cmd);
848 ++cmd_num;
850 str += len;
851 while (*str == '\n' || *str == '\r' || *str == ' ')
852 ++str;
853 talloc_free(cmdbuf);
855 return cmd_num;
858 mp_cmd_t *mp_input_parse_cmd(char *str)
860 int i, l;
861 int pausing = 0;
862 char *ptr;
863 const mp_cmd_t *cmd_def;
865 // Ignore heading spaces.
866 while (str[0] == ' ' || str[0] == '\t')
867 ++str;
869 if (strncmp(str, "pausing ", 8) == 0) {
870 pausing = 1;
871 str = &str[8];
872 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
873 pausing = 2;
874 str = &str[13];
875 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
876 pausing = 3;
877 str = &str[15];
878 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
879 pausing = 4;
880 str = &str[19];
883 ptr = str + strcspn(str, "\t ");
884 if (*ptr != 0)
885 l = ptr - str;
886 else
887 l = strlen(str);
889 if (l == 0)
890 return NULL;
892 for (i = 0; mp_cmds[i].name != NULL; i++) {
893 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
894 break;
897 if (mp_cmds[i].name == NULL)
898 return NULL;
900 cmd_def = &mp_cmds[i];
902 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
903 *cmd = (mp_cmd_t){
904 .id = cmd_def->id,
905 .name = talloc_strdup(cmd, cmd_def->name),
906 .pausing = pausing,
909 ptr = str;
911 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
912 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
913 ptr++;
914 if (ptr[0] == '\0')
915 break;
916 while (ptr[0] == ' ' || ptr[0] == '\t')
917 ptr++;
918 if (ptr[0] == '\0' || ptr[0] == '#')
919 break;
920 cmd->args[i].type = cmd_def->args[i].type;
921 switch (cmd_def->args[i].type) {
922 case MP_CMD_ARG_INT:
923 errno = 0;
924 cmd->args[i].v.i = atoi(ptr);
925 if (errno != 0) {
926 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
927 "isn't an integer.\n", cmd_def->name, i + 1);
928 goto error;
930 break;
931 case MP_CMD_ARG_FLOAT:
932 errno = 0;
933 cmd->args[i].v.f = atof(ptr);
934 if (errno != 0) {
935 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
936 "isn't a float.\n", cmd_def->name, i + 1);
937 goto error;
939 break;
940 case MP_CMD_ARG_STRING: {
941 int term = ' ';
942 if (*ptr == '\'' || *ptr == '"')
943 term = *ptr++;
944 char *argptr = talloc_size(cmd, strlen(ptr) + 1);
945 cmd->args[i].v.s = argptr;
946 while (1) {
947 if (*ptr == 0) {
948 if (term == ' ')
949 break;
950 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
951 "unterminated.\n", cmd_def->name, i + 1);
952 goto error;
954 if (*ptr == term)
955 break;
956 if (*ptr == '\\')
957 ptr++;
958 if (*ptr != 0)
959 *argptr++ = *ptr++;
961 *argptr = 0;
962 break;
964 case -1:
965 ptr = NULL;
966 break;
967 default:
968 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
971 cmd->nargs = i;
973 if (cmd_def->nargs > cmd->nargs) {
974 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d "
975 "arguments, we found only %d so far.\n", cmd_def->name,
976 cmd_def->nargs, cmd->nargs);
977 goto error;
980 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1; i++) {
981 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(struct mp_cmd_arg));
982 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
983 && cmd_def->args[i].v.s != NULL)
984 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
987 if (i < MP_CMD_MAX_ARGS)
988 cmd->args[i].type = -1;
990 return cmd;
992 error:
993 mp_cmd_free(cmd);
994 return NULL;
997 #define MP_CMD_MAX_SIZE 4096
999 static int read_cmd(struct input_fd *mp_fd, char **ret)
1001 char *end;
1002 *ret = NULL;
1004 // Allocate the buffer if it doesn't exist
1005 if (!mp_fd->buffer) {
1006 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
1007 mp_fd->pos = 0;
1008 mp_fd->size = MP_CMD_MAX_SIZE;
1011 // Get some data if needed/possible
1012 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
1013 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
1014 mp_fd->size - 1 - mp_fd->pos);
1015 // Error ?
1016 if (r < 0) {
1017 switch (r) {
1018 case MP_INPUT_ERROR:
1019 case MP_INPUT_DEAD:
1020 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1021 "command file descriptor %d: %s\n",
1022 mp_fd->fd, strerror(errno));
1023 case MP_INPUT_NOTHING:
1024 return r;
1025 case MP_INPUT_RETRY:
1026 continue;
1028 // EOF ?
1029 } else if (r == 0) {
1030 mp_fd->eof = 1;
1031 break;
1033 mp_fd->pos += r;
1034 break;
1037 mp_fd->got_cmd = 0;
1039 while (1) {
1040 int l = 0;
1041 // Find the cmd end
1042 mp_fd->buffer[mp_fd->pos] = '\0';
1043 end = strchr(mp_fd->buffer, '\r');
1044 if (end)
1045 *end = '\n';
1046 end = strchr(mp_fd->buffer, '\n');
1047 // No cmd end ?
1048 if (!end) {
1049 // If buffer is full we must drop all until the next \n
1050 if (mp_fd->size - mp_fd->pos <= 1) {
1051 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
1052 "descriptor %d is full: dropping content.\n",
1053 mp_fd->fd);
1054 mp_fd->pos = 0;
1055 mp_fd->drop = 1;
1057 break;
1059 // We already have a cmd : set the got_cmd flag
1060 else if ((*ret)) {
1061 mp_fd->got_cmd = 1;
1062 break;
1065 l = end - mp_fd->buffer;
1067 // Not dropping : put the cmd in ret
1068 if (!mp_fd->drop)
1069 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1070 else
1071 mp_fd->drop = 0;
1072 mp_fd->pos -= l + 1;
1073 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1076 if (*ret)
1077 return 1;
1078 else
1079 return MP_INPUT_NOTHING;
1082 static int default_cmd_func(int fd, char *buf, int l)
1084 while (1) {
1085 int r = read(fd, buf, l);
1086 // Error ?
1087 if (r < 0) {
1088 if (errno == EINTR)
1089 continue;
1090 else if (errno == EAGAIN)
1091 return MP_INPUT_NOTHING;
1092 return MP_INPUT_ERROR;
1093 // EOF ?
1095 return r;
1100 static char *find_bind_for_key(const struct cmd_bind *binds, int n, int *keys)
1102 int j;
1104 if (n <= 0)
1105 return NULL;
1106 for (j = 0; binds[j].cmd != NULL; j++) {
1107 int found = 1, s;
1108 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1109 if (binds[j].input[s] != keys[s]) {
1110 found = 0;
1111 break;
1114 if (found && binds[j].input[s] == 0 && s == n)
1115 break;
1117 return binds[j].cmd;
1120 static struct cmd_bind_section *get_bind_section(struct input_ctx *ictx,
1121 char *section)
1123 struct cmd_bind_section *bind_section = ictx->cmd_bind_sections;
1125 if (section == NULL)
1126 section = "default";
1127 while (bind_section) {
1128 if (strcmp(section, bind_section->section) == 0)
1129 return bind_section;
1130 if (bind_section->next == NULL)
1131 break;
1132 bind_section = bind_section->next;
1134 if (bind_section) {
1135 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1136 bind_section = bind_section->next;
1137 } else {
1138 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1139 bind_section = ictx->cmd_bind_sections;
1141 bind_section->cmd_binds = NULL;
1142 bind_section->section = talloc_strdup(bind_section, section);
1143 bind_section->next = NULL;
1144 return bind_section;
1147 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1149 char *cmd = NULL;
1150 mp_cmd_t *ret;
1152 if (ictx->cmd_binds)
1153 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1154 if (ictx->cmd_binds_default && cmd == NULL)
1155 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1156 if (ictx->default_bindings && cmd == NULL)
1157 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1159 if (cmd == NULL) {
1160 char *key_buf = get_key_combo_name(keys, n);
1161 mp_tmsg(MSGT_INPUT, MSGL_WARN,
1162 "No bind found for key '%s'.\n", key_buf);
1163 talloc_free(key_buf);
1164 return NULL;
1166 if (strcmp(cmd, "ignore") == 0)
1167 return NULL;
1168 ret = mp_input_parse_cmd(cmd);
1169 if (!ret) {
1170 char *key_buf = get_key_combo_name(keys, n);
1171 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1172 "Invalid command for bound key '%s': '%s'\n", key_buf, cmd);
1173 talloc_free(key_buf);
1175 return ret;
1179 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1181 unsigned int j;
1182 mp_cmd_t *ret;
1184 /* On normal keyboards shift changes the character code of non-special
1185 * keys, so don't count the modifier separately for those. In other words
1186 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1187 * shift modifier is still kept for special keys like arrow keys.
1189 int unmod = code & ~KEY_MODIFIER_MASK;
1190 if (unmod >= 32 && unmod < MP_KEY_BASE)
1191 code &= ~KEY_MODIFIER_SHIFT;
1193 if (code & MP_KEY_DOWN) {
1194 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1195 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1196 "at the same time\n");
1197 return NULL;
1199 code &= ~MP_KEY_DOWN;
1200 // Check if we don't already have this key as pushed
1201 for (j = 0; j < ictx->num_key_down; j++) {
1202 if (ictx->key_down[j] == code)
1203 break;
1205 if (j != ictx->num_key_down)
1206 return NULL;
1207 ictx->key_down[ictx->num_key_down] = code;
1208 ictx->num_key_down++;
1209 ictx->last_key_down = GetTimer();
1210 ictx->ar_state = 0;
1211 return NULL;
1213 // button released or press of key with no separate down/up events
1214 for (j = 0; j < ictx->num_key_down; j++) {
1215 if (ictx->key_down[j] == code)
1216 break;
1218 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1219 if (doubleclick) {
1220 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1221 if (!ictx->num_key_down
1222 || ictx->key_down[ictx->num_key_down - 1] != btn)
1223 return NULL;
1224 j = ictx->num_key_down - 1;
1225 ictx->key_down[j] = code;
1227 if (j == ictx->num_key_down) { // was not already down; add temporarily
1228 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1229 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1230 "at the same time\n");
1231 return NULL;
1233 ictx->key_down[ictx->num_key_down] = code;
1234 ictx->num_key_down++;
1235 ictx->last_key_down = 1;
1237 // Interpret only maximal point of multibutton event
1238 ret = ictx->last_key_down ?
1239 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1240 : NULL;
1241 if (doubleclick) {
1242 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1243 return ret;
1245 // Remove the key
1246 if (j + 1 < ictx->num_key_down)
1247 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1248 (ictx->num_key_down - (j + 1)) * sizeof(int));
1249 ictx->num_key_down--;
1250 ictx->last_key_down = 0;
1251 ictx->ar_state = -1;
1252 mp_cmd_free(ictx->ar_cmd);
1253 ictx->ar_cmd = NULL;
1254 return ret;
1257 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1259 // No input : autorepeat ?
1260 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1261 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1262 unsigned int t = GetTimer();
1263 // First time : wait delay
1264 if (ictx->ar_state == 0
1265 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1266 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1267 ictx->key_down);
1268 if (!ictx->ar_cmd) {
1269 ictx->ar_state = -1;
1270 return NULL;
1272 ictx->ar_state = 1;
1273 ictx->last_ar = t;
1274 return mp_cmd_clone(ictx->ar_cmd);
1275 // Then send rate / sec event
1276 } else if (ictx->ar_state == 1
1277 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1278 ictx->last_ar = t;
1279 return mp_cmd_clone(ictx->ar_cmd);
1282 return NULL;
1285 void mp_input_feed_key(struct input_ctx *ictx, int code)
1287 ictx->got_new_events = true;
1288 if (code == MP_INPUT_RELEASE_ALL) {
1289 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1290 ictx->num_key_down = 0;
1291 ictx->last_key_down = 0;
1292 return;
1294 struct mp_cmd *cmd = interpret_key(ictx, code);
1295 if (!cmd)
1296 return;
1297 struct cmd_queue *queue = &ictx->key_cmd_queue;
1298 if (queue->num_cmds >= ictx->key_fifo_size &&
1299 (!is_abort_cmd(cmd->id) || queue->num_abort_cmds))
1300 return;
1301 queue_add(queue, cmd, false);
1304 static void read_cmd_fd(struct input_ctx *ictx, struct input_fd *cmd_fd)
1306 int r;
1307 char *text;
1308 while ((r = read_cmd(cmd_fd, &text)) >= 0) {
1309 ictx->got_new_events = true;
1310 struct mp_cmd *cmd = mp_input_parse_cmd(text);
1311 talloc_free(text);
1312 if (cmd)
1313 queue_add(&ictx->control_cmd_queue, cmd, false);
1314 if (!cmd_fd->got_cmd)
1315 return;
1317 if (r == MP_INPUT_ERROR)
1318 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1319 cmd_fd->fd);
1320 else if (r == MP_INPUT_DEAD)
1321 cmd_fd->dead = true;
1324 static void read_key_fd(struct input_ctx *ictx, struct input_fd *key_fd)
1326 int code = key_fd->read_func.key(key_fd->ctx, key_fd->fd);
1327 if (code >= 0 || code == MP_INPUT_RELEASE_ALL) {
1328 mp_input_feed_key(ictx, code);
1329 return;
1332 if (code == MP_INPUT_ERROR)
1333 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1334 "Error on key input file descriptor %d\n", key_fd->fd);
1335 else if (code == MP_INPUT_DEAD) {
1336 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1337 "Dead key input on file descriptor %d\n", key_fd->fd);
1338 key_fd->dead = true;
1343 * \param time time to wait at most for an event in milliseconds
1345 static void read_events(struct input_ctx *ictx, int time)
1347 ictx->got_new_events = false;
1348 struct input_fd *key_fds = ictx->key_fds;
1349 struct input_fd *cmd_fds = ictx->cmd_fds;
1350 for (int i = 0; i < ictx->num_key_fd; i++)
1351 if (key_fds[i].dead) {
1352 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1353 i--;
1354 } else if (time && key_fds[i].no_select)
1355 read_key_fd(ictx, &key_fds[i]);
1356 for (int i = 0; i < ictx->num_cmd_fd; i++)
1357 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1358 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1359 i--;
1360 } else if (time && cmd_fds[i].no_select)
1361 read_cmd_fd(ictx, &cmd_fds[i]);
1362 if (ictx->got_new_events)
1363 time = 0;
1364 #ifdef HAVE_POSIX_SELECT
1365 fd_set fds;
1366 FD_ZERO(&fds);
1367 int max_fd = 0;
1368 for (int i = 0; i < ictx->num_key_fd; i++) {
1369 if (key_fds[i].no_select)
1370 continue;
1371 if (key_fds[i].fd > max_fd)
1372 max_fd = key_fds[i].fd;
1373 FD_SET(key_fds[i].fd, &fds);
1375 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1376 if (cmd_fds[i].no_select)
1377 continue;
1378 if (cmd_fds[i].fd > max_fd)
1379 max_fd = cmd_fds[i].fd;
1380 FD_SET(cmd_fds[i].fd, &fds);
1382 struct timeval tv, *time_val;
1383 if (time >= 0) {
1384 tv.tv_sec = time / 1000;
1385 tv.tv_usec = (time % 1000) * 1000;
1386 time_val = &tv;
1387 } else
1388 time_val = NULL;
1389 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1390 if (errno != EINTR)
1391 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1392 strerror(errno));
1393 FD_ZERO(&fds);
1395 #else
1396 if (time)
1397 usec_sleep(time * 1000);
1398 #endif
1401 for (int i = 0; i < ictx->num_key_fd; i++) {
1402 #ifdef HAVE_POSIX_SELECT
1403 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1404 continue;
1405 #endif
1406 read_key_fd(ictx, &key_fds[i]);
1409 for (int i = 0; i < ictx->num_cmd_fd; i++) {
1410 #ifdef HAVE_POSIX_SELECT
1411 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds))
1412 continue;
1413 #endif
1414 read_cmd_fd(ictx, &cmd_fds[i]);
1418 /* To support blocking file descriptors we don't loop the read over
1419 * every source until it's known to be empty. Instead we use this wrapper
1420 * to run select() again.
1422 static void read_all_events(struct input_ctx *ictx, int time)
1424 while (1) {
1425 read_events(ictx, time);
1426 if (!ictx->got_new_events)
1427 return;
1428 time = 0;
1432 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1434 ictx->got_new_events = true;
1435 if (!cmd)
1436 return 0;
1437 queue_add(&ictx->control_cmd_queue, cmd, true);
1438 return 1;
1442 * \param peek_only when set, the returned command stays in the queue.
1443 * Do not free the returned cmd whe you set this!
1445 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1447 if (async_quit_request)
1448 return mp_input_parse_cmd("quit 1");
1450 if (ictx->control_cmd_queue.num_cmds || ictx->key_cmd_queue.num_cmds)
1451 time = 0;
1452 read_all_events(ictx, time);
1453 struct mp_cmd *ret;
1454 struct cmd_queue *queue = &ictx->control_cmd_queue;
1455 if (!queue->num_cmds)
1456 queue = &ictx->key_cmd_queue;
1457 if (!queue->num_cmds) {
1458 ret = check_autorepeat(ictx);
1459 if (!ret)
1460 return NULL;
1461 queue_add(queue, ret, false);
1462 } else
1463 ret = queue->first;
1465 if (!peek_only)
1466 queue_pop(queue);
1468 return ret;
1471 void mp_cmd_free(mp_cmd_t *cmd)
1473 talloc_free(cmd);
1476 mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
1478 mp_cmd_t *ret;
1479 int i;
1481 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1482 ret->name = talloc_strdup(ret, cmd->name);
1483 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1484 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1485 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1488 return ret;
1491 int mp_input_get_key_from_name(const char *name)
1493 int modifiers = 0;
1494 const char *p;
1495 while ((p = strchr(name, '+'))) {
1496 for (struct key_name *m = modifier_names; m->name; m++)
1497 if (!bstrcasecmp(bstr(m->name),
1498 (struct bstr){(char *)name, p - name})) {
1499 modifiers |= m->key;
1500 goto found;
1502 if (!strcmp(name, "+"))
1503 return '+' + modifiers;
1504 return -1;
1505 found:
1506 name = p + 1;
1509 struct bstr bname = bstr(name);
1511 struct bstr rest;
1512 int code = bstr_decode_utf8(bname, &rest);
1513 if (code >= 0 && rest.len == 0)
1514 return code + modifiers;
1516 if (bstr_startswith0(bname, "0x"))
1517 return strtol(name, NULL, 16) + modifiers;
1519 for (int i = 0; key_names[i].name != NULL; i++) {
1520 if (strcasecmp(key_names[i].name, name) == 0)
1521 return key_names[i].key + modifiers;
1524 return -1;
1527 static int get_input_from_name(char *name, int *keys)
1529 char *end, *ptr;
1530 int n = 0;
1532 ptr = name;
1533 n = 0;
1534 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1535 if (end && end[1] != '\0') {
1536 if (end[1] == '-')
1537 end = &end[1];
1538 end[0] = '\0';
1540 keys[n] = mp_input_get_key_from_name(ptr);
1541 if (keys[n] < 0)
1542 return 0;
1543 n++;
1544 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1545 ptr = &end[1];
1546 else
1547 break;
1549 keys[n] = 0;
1550 return 1;
1553 #define SPACE_CHAR " \n\r\t"
1555 static void bind_keys(struct input_ctx *ictx,
1556 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1558 int i = 0, j;
1559 struct cmd_bind *bind = NULL;
1560 struct cmd_bind_section *bind_section = NULL;
1561 char *section = NULL, *p;
1563 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1564 *p = 0;
1565 section = ++cmd;
1566 cmd = ++p;
1567 // Jump beginning space
1568 cmd += strspn(cmd, SPACE_CHAR);
1570 bind_section = get_bind_section(ictx, section);
1572 if (bind_section->cmd_binds) {
1573 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1574 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1575 /* NOTHING */;
1576 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1577 bind = &bind_section->cmd_binds[i];
1578 break;
1583 if (!bind) {
1584 bind_section->cmd_binds = talloc_realloc(bind_section,
1585 bind_section->cmd_binds,
1586 struct cmd_bind, i + 2);
1587 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(struct cmd_bind));
1588 bind = &bind_section->cmd_binds[i];
1590 talloc_free(bind->cmd);
1591 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1592 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1595 static int parse_config(struct input_ctx *ictx, char *file)
1597 int fd = open(file, O_RDONLY);
1599 if (fd < 0) {
1600 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1601 file, strerror(errno));
1602 return 0;
1605 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1608 unsigned char buffer[512];
1609 struct bstr buf = { buffer, 0 };
1610 bool eof = false, comments = false;
1611 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1];
1613 while (1) {
1614 if (buf.start != buffer) {
1615 memmove(buffer, buf.start, buf.len);
1616 buf.start = buffer;
1618 if (!eof && buf.len < sizeof(buffer)) {
1619 int r = read(fd, buffer + buf.len, sizeof(buffer) - buf.len);
1620 if (r < 0) {
1621 if (errno == EINTR)
1622 continue;
1623 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1624 "input config file %s: %s\n", file, strerror(errno));
1625 close(fd);
1626 return 0;
1627 } else if (r == 0) {
1628 eof = true;
1629 } else {
1630 buf.len += r;
1631 continue;
1634 if (buf.len == 0) {
1635 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1636 "%d binds\n", file, n_binds);
1637 close(fd);
1638 return 1;
1640 if (comments) {
1641 int idx = bstrchr(buf, '\n');
1642 if (idx >= 0) {
1643 buf = bstr_cut(buf, idx + 1);
1644 comments = false;
1645 } else
1646 buf = bstr_cut(buf, buf.len);
1647 continue;
1649 buf = bstr_lstrip(buf);
1650 if (buf.start != buffer)
1651 continue;
1652 if (buf.start[0] == '#') {
1653 comments = true;
1654 continue;
1656 int eol = bstrchr(buf, '\n');
1657 if (eol < 0) {
1658 if (eof) {
1659 eol = buf.len;
1660 } else {
1661 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1662 "Key binding is too long: %.*s\n", BSTR_P(buf));
1663 comments = true;
1664 continue;
1667 struct bstr line = bstr_splice(buf, 0, eol);
1668 buf = bstr_cut(buf, eol);
1669 struct bstr command;
1670 // Find the key name starting a line
1671 struct bstr keyname = bstr_split(line, SPACE_CHAR, &command);
1672 command = bstr_strip(command);
1673 if (command.len == 0) {
1674 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1675 "Unfinished key binding: %.*s\n", BSTR_P(line));
1676 continue;
1678 char *name = bstrdup0(NULL, keyname);
1679 if (!get_input_from_name(name, keys)) {
1680 talloc_free(name);
1681 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1682 "Unknown key '%.*s'\n", BSTR_P(keyname));
1683 continue;
1685 talloc_free(name);
1686 char *cmd = bstrdup0(NULL, command);
1687 bind_keys(ictx, keys, cmd);
1688 n_binds++;
1689 talloc_free(cmd);
1693 void mp_input_set_section(struct input_ctx *ictx, char *name)
1695 struct cmd_bind_section *bind_section = NULL;
1697 ictx->cmd_binds = NULL;
1698 ictx->cmd_binds_default = NULL;
1699 talloc_free(ictx->section);
1700 if (name)
1701 ictx->section = talloc_strdup(ictx, name);
1702 else
1703 ictx->section = talloc_strdup(ictx, "default");
1704 if ((bind_section = get_bind_section(ictx, ictx->section)))
1705 ictx->cmd_binds = bind_section->cmd_binds;
1706 if (strcmp(ictx->section, "default") == 0)
1707 return;
1708 if ((bind_section = get_bind_section(ictx, NULL)))
1709 ictx->cmd_binds_default = bind_section->cmd_binds;
1712 char *mp_input_get_section(struct input_ctx *ictx)
1714 return ictx->section;
1717 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1719 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1720 *ictx = (struct input_ctx){
1721 .key_fifo_size = input_conf->key_fifo_size,
1722 .ar_state = -1,
1723 .ar_delay = input_conf->ar_delay,
1724 .ar_rate = input_conf->ar_rate,
1725 .default_bindings = input_conf->default_bindings,
1728 char *file;
1729 char *config_file = input_conf->config_file;
1730 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1731 if (!file)
1732 return ictx;
1734 if (!parse_config(ictx, file)) {
1735 // free file if it was allocated by get_path(),
1736 // before it gets overwritten
1737 if (file != config_file)
1738 free(file);
1739 // Try global conf dir
1740 file = MPLAYER_CONFDIR "/input.conf";
1741 if (!parse_config(ictx, file))
1742 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1743 "input config\n");
1744 } else {
1745 // free file if it was allocated by get_path()
1746 if (file != config_file)
1747 free(file);
1750 #ifdef CONFIG_JOYSTICK
1751 if (input_conf->use_joystick) {
1752 int fd = mp_input_joystick_init(input_conf->js_dev);
1753 if (fd < 0)
1754 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1755 else
1756 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1757 close, NULL);
1759 #endif
1761 #ifdef CONFIG_LIRC
1762 if (input_conf->use_lirc) {
1763 int fd = mp_input_lirc_init();
1764 if (fd > 0)
1765 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1766 mp_input_lirc_close);
1768 #endif
1770 #ifdef CONFIG_LIRCC
1771 if (input_conf->use_lircc) {
1772 int fd = lircc_init("mplayer", NULL);
1773 if (fd >= 0)
1774 mp_input_add_cmd_fd(ictx, fd, 1, NULL, lircc_cleanup);
1776 #endif
1778 #ifdef CONFIG_APPLE_REMOTE
1779 if (input_conf->use_ar) {
1780 if (mp_input_ar_init() < 0)
1781 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1782 else
1783 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1784 mp_input_ar_close, NULL);
1786 #endif
1788 #ifdef CONFIG_APPLE_IR
1789 if (input_conf->use_ar) {
1790 int fd = mp_input_appleir_init(input_conf->ar_dev);
1791 if (fd < 0)
1792 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1793 else
1794 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1795 close, NULL);
1797 #endif
1799 if (input_conf->in_file) {
1800 int mode = O_RDONLY;
1801 #ifndef __MINGW32__
1802 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1803 // Note that on Windows due to how the API works, using RDONLY should
1804 // be ok.
1805 struct stat st;
1806 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1807 mode = O_RDWR;
1808 mode |= O_NONBLOCK;
1809 #endif
1810 int in_file_fd = open(input_conf->in_file, mode);
1811 if (in_file_fd >= 0)
1812 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, close);
1813 else
1814 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1815 input_conf->in_file, strerror(errno));
1817 return ictx;
1820 void mp_input_uninit(struct input_ctx *ictx)
1822 if (!ictx)
1823 return;
1825 unsigned int i;
1827 for (i = 0; i < ictx->num_key_fd; i++) {
1828 if (ictx->key_fds[i].close_func)
1829 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1832 for (i = 0; i < ictx->num_cmd_fd; i++) {
1833 if (ictx->cmd_fds[i].close_func)
1834 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1836 talloc_free(ictx);
1839 void mp_input_register_options(m_config_t *cfg)
1841 m_config_register_options(cfg, mp_input_opts);
1844 static int print_key_list(m_option_t *cfg)
1846 int i;
1847 printf("\n");
1848 for (i = 0; key_names[i].name != NULL; i++)
1849 printf("%s\n", key_names[i].name);
1850 exit(0);
1853 static int print_cmd_list(m_option_t *cfg)
1855 const mp_cmd_t *cmd;
1856 int i, j;
1857 const char *type;
1859 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1860 printf("%-20.20s", cmd->name);
1861 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1; j++) {
1862 switch (cmd->args[j].type) {
1863 case MP_CMD_ARG_INT:
1864 type = "Integer";
1865 break;
1866 case MP_CMD_ARG_FLOAT:
1867 type = "Float";
1868 break;
1869 case MP_CMD_ARG_STRING:
1870 type = "String";
1871 break;
1872 default:
1873 type = "??";
1875 if (j + 1 > cmd->nargs)
1876 printf(" [%s]", type);
1877 else
1878 printf(" %s", type);
1880 printf("\n");
1882 exit(0);
1886 * \param time time to wait for an interruption in milliseconds
1888 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1890 for (int i = 0; ; i++) {
1891 if (async_quit_request || ictx->key_cmd_queue.num_abort_cmds ||
1892 ictx->control_cmd_queue.num_abort_cmds) {
1893 mp_tmsg(MSGT_INPUT, MSGL_WARN, "Received command to move to "
1894 "another file. Aborting current processing.\n");
1895 return true;
1897 if (i)
1898 return false;
1899 read_all_events(ictx, time);