Windows: ao_pcm: add io.h include for MinGW64
[mplayer.git] / input / input.c
blob7cb498921e5c19ed70645bde9bcc70783d113927
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>
33 #include "input.h"
34 #ifdef MP_DEBUG
35 #include <assert.h>
36 #endif
37 #include "mp_fifo.h"
38 #include "keycodes.h"
39 #include "osdep/timer.h"
40 #include "libavutil/avstring.h"
41 #include "mp_msg.h"
42 #include "m_config.h"
43 #include "m_option.h"
44 #include "path.h"
45 #include "talloc.h"
46 #include "options.h"
47 #include "bstr.h"
49 #include "joystick.h"
51 #ifdef CONFIG_LIRC
52 #include "lirc.h"
53 #endif
55 #ifdef CONFIG_LIRCC
56 #include <lirc/lircc.h>
57 #endif
59 #include "ar.h"
61 typedef struct mp_cmd_bind {
62 int input[MP_MAX_KEY_DOWN + 1];
63 char *cmd;
64 } mp_cmd_bind_t;
66 typedef struct mp_key_name {
67 int key;
68 char *name;
69 } mp_key_name_t;
71 /// This array defines all known commands.
72 /// The first field is an id used to recognize the command without too many strcmp.
73 /// The second is obviously the command name.
74 /// The third is the minimum number of arguments this command needs.
75 /// Then comes the definition of each argument, terminated with an arg of type -1.
76 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
77 /// the last one) which is actually 9.
79 /// For the args, the first field is the type (actually int, float or string), the second
80 /// is the default value wich is used for optional arguments
82 static const mp_cmd_t mp_cmds[] = {
83 #ifdef CONFIG_RADIO
84 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
85 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
87 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 #endif
89 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
90 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
91 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
96 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
97 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
98 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
99 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
102 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
106 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
109 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
112 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
113 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
119 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
120 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
121 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
123 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
124 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
125 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
126 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
131 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
132 #ifdef CONFIG_ASS
133 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
134 #endif
135 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
136 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
137 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
138 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
139 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
141 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
142 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
144 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
145 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
152 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
153 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
155 #ifdef CONFIG_TV
156 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
157 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
158 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
159 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
160 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
161 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
162 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
165 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
166 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 #endif
170 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
171 #ifdef CONFIG_DVBIN
172 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
173 #endif
174 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
175 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
176 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
177 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
178 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
181 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
186 { MP_CMD_CAPTURING, "capturing", 0, { {-1,{0}} } },
187 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
188 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
189 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
191 #ifdef CONFIG_DVDNAV
192 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
193 #endif
195 #ifdef CONFIG_MENU
196 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
197 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
198 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
199 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
200 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
201 #endif
203 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
204 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
205 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
206 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
207 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
209 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
210 { MP_CMD_STEP_PROPERTY_OSD, "step_property_osd", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
212 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
213 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
215 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
216 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
217 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
218 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
219 { MP_CMD_AF_CMDLINE, "af_cmdline", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
221 { 0, NULL, 0, {} }
224 /// The names of the keys as used in input.conf
225 /// If you add some new keys, you also need to add them here
227 static const mp_key_name_t key_names[] = {
228 { ' ', "SPACE" },
229 { '#', "SHARP" },
230 { KEY_ENTER, "ENTER" },
231 { KEY_TAB, "TAB" },
232 { KEY_BACKSPACE, "BS" },
233 { KEY_DELETE, "DEL" },
234 { KEY_INSERT, "INS" },
235 { KEY_HOME, "HOME" },
236 { KEY_END, "END" },
237 { KEY_PAGE_UP, "PGUP" },
238 { KEY_PAGE_DOWN, "PGDWN" },
239 { KEY_ESC, "ESC" },
240 { KEY_RIGHT, "RIGHT" },
241 { KEY_LEFT, "LEFT" },
242 { KEY_DOWN, "DOWN" },
243 { KEY_UP, "UP" },
244 { KEY_F+1, "F1" },
245 { KEY_F+2, "F2" },
246 { KEY_F+3, "F3" },
247 { KEY_F+4, "F4" },
248 { KEY_F+5, "F5" },
249 { KEY_F+6, "F6" },
250 { KEY_F+7, "F7" },
251 { KEY_F+8, "F8" },
252 { KEY_F+9, "F9" },
253 { KEY_F+10, "F10" },
254 { KEY_F+11, "F11" },
255 { KEY_F+12, "F12" },
256 { KEY_KP0, "KP0" },
257 { KEY_KP1, "KP1" },
258 { KEY_KP2, "KP2" },
259 { KEY_KP3, "KP3" },
260 { KEY_KP4, "KP4" },
261 { KEY_KP5, "KP5" },
262 { KEY_KP6, "KP6" },
263 { KEY_KP7, "KP7" },
264 { KEY_KP8, "KP8" },
265 { KEY_KP9, "KP9" },
266 { KEY_KPDEL, "KP_DEL" },
267 { KEY_KPDEC, "KP_DEC" },
268 { KEY_KPINS, "KP_INS" },
269 { KEY_KPENTER, "KP_ENTER" },
270 { MOUSE_BTN0, "MOUSE_BTN0" },
271 { MOUSE_BTN1, "MOUSE_BTN1" },
272 { MOUSE_BTN2, "MOUSE_BTN2" },
273 { MOUSE_BTN3, "MOUSE_BTN3" },
274 { MOUSE_BTN4, "MOUSE_BTN4" },
275 { MOUSE_BTN5, "MOUSE_BTN5" },
276 { MOUSE_BTN6, "MOUSE_BTN6" },
277 { MOUSE_BTN7, "MOUSE_BTN7" },
278 { MOUSE_BTN8, "MOUSE_BTN8" },
279 { MOUSE_BTN9, "MOUSE_BTN9" },
280 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
281 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
282 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
283 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
284 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
285 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
286 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
287 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
288 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
289 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
290 { JOY_AXIS1_MINUS, "JOY_UP" },
291 { JOY_AXIS1_PLUS, "JOY_DOWN" },
292 { JOY_AXIS0_MINUS, "JOY_LEFT" },
293 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
295 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
296 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
297 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
298 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
299 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
300 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
301 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
302 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
303 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
304 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
305 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
306 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
307 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
308 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
309 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
310 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
311 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
312 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
313 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
314 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
316 { JOY_BTN0, "JOY_BTN0" },
317 { JOY_BTN1, "JOY_BTN1" },
318 { JOY_BTN2, "JOY_BTN2" },
319 { JOY_BTN3, "JOY_BTN3" },
320 { JOY_BTN4, "JOY_BTN4" },
321 { JOY_BTN5, "JOY_BTN5" },
322 { JOY_BTN6, "JOY_BTN6" },
323 { JOY_BTN7, "JOY_BTN7" },
324 { JOY_BTN8, "JOY_BTN8" },
325 { JOY_BTN9, "JOY_BTN9" },
327 { AR_PLAY, "AR_PLAY" },
328 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
329 { AR_NEXT, "AR_NEXT" },
330 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
331 { AR_PREV, "AR_PREV" },
332 { AR_PREV_HOLD, "AR_PREV_HOLD" },
333 { AR_MENU, "AR_MENU" },
334 { AR_MENU_HOLD, "AR_MENU_HOLD" },
335 { AR_VUP, "AR_VUP" },
336 { AR_VDOWN, "AR_VDOWN" },
338 { KEY_POWER, "POWER" },
339 { KEY_MENU, "MENU" },
340 { KEY_PLAY, "PLAY" },
341 { KEY_PAUSE, "PAUSE" },
342 { KEY_PLAYPAUSE, "PLAYPAUSE" },
343 { KEY_STOP, "STOP" },
344 { KEY_FORWARD, "FORWARD" },
345 { KEY_REWIND, "REWIND" },
346 { KEY_NEXT, "NEXT" },
347 { KEY_PREV, "PREV" },
348 { KEY_VOLUME_UP, "VOLUME_UP" },
349 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
350 { KEY_MUTE, "MUTE" },
352 // These are kept for backward compatibility
353 { KEY_PAUSE, "XF86_PAUSE" },
354 { KEY_STOP, "XF86_STOP" },
355 { KEY_PREV, "XF86_PREV" },
356 { KEY_NEXT, "XF86_NEXT" },
358 { KEY_CLOSE_WIN, "CLOSE_WIN" },
360 { 0, NULL }
363 struct mp_key_name modifier_names[] = {
364 { KEY_MODIFIER_SHIFT, "Shift" },
365 { KEY_MODIFIER_CTRL, "Ctrl" },
366 { KEY_MODIFIER_ALT, "Alt" },
367 { KEY_MODIFIER_META, "Meta" },
368 { 0 }
371 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
373 // This is the default binding. The content of input.conf overrides these.
374 // The first arg is a null terminated array of key codes.
375 // The second is the command
377 static const mp_cmd_bind_t def_cmd_binds[] = {
379 { { MOUSE_BTN0_DBL, 0 }, "vo_fullscreen" },
380 { { MOUSE_BTN2, 0 }, "pause" },
381 { { MOUSE_BTN3, 0 }, "seek 10" },
382 { { MOUSE_BTN4, 0 }, "seek -10" },
383 { { MOUSE_BTN5, 0 }, "volume 1" },
384 { { MOUSE_BTN6, 0 }, "volume -1" },
386 #ifdef CONFIG_DVDNAV
387 { { KEY_KP8, 0 }, "dvdnav up" }, // up
388 { { KEY_KP2, 0 }, "dvdnav down" }, // down
389 { { KEY_KP4, 0 }, "dvdnav left" }, // left
390 { { KEY_KP6, 0 }, "dvdnav right" }, // right
391 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
392 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
393 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
394 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
395 #endif
397 { { KEY_RIGHT, 0 }, "seek 10" },
398 { { KEY_LEFT, 0 }, "seek -10" },
399 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
400 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
401 { { KEY_UP, 0 }, "seek 60" },
402 { { KEY_DOWN, 0 }, "seek -60" },
403 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
404 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
405 { { KEY_PAGE_UP, 0 }, "seek 600" },
406 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
407 { { '+', 0 }, "audio_delay 0.100" },
408 { { '-', 0 }, "audio_delay -0.100" },
409 { { '[', 0 }, "speed_mult 0.9091" },
410 { { ']', 0 }, "speed_mult 1.1" },
411 { { '{', 0 }, "speed_mult 0.5" },
412 { { '}', 0 }, "speed_mult 2.0" },
413 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
414 { { 'q', 0 }, "quit" },
415 { { KEY_ESC, 0 }, "quit" },
416 { { 'p', 0 }, "pause" },
417 { { ' ', 0 }, "pause" },
418 { { '.', 0 }, "frame_step" },
419 { { KEY_HOME, 0 }, "pt_up_step 1" },
420 { { KEY_END, 0 }, "pt_up_step -1" },
421 { { '>', 0 }, "pt_step 1" },
422 { { KEY_ENTER, 0 }, "pt_step 1 1" },
423 { { '<', 0 }, "pt_step -1" },
424 { { KEY_INS, 0 }, "alt_src_step 1" },
425 { { KEY_DEL, 0 }, "alt_src_step -1" },
426 { { 'o', 0 }, "osd" },
427 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
428 { { 'P', 0 }, "osd_show_progression" },
429 { { 'z', 0 }, "sub_delay -0.1" },
430 { { 'x', 0 }, "sub_delay +0.1" },
431 { { 'g', 0 }, "sub_step -1" },
432 { { 'y', 0 }, "sub_step +1" },
433 { { '9', 0 }, "volume -1" },
434 { { '/', 0 }, "volume -1" },
435 { { '0', 0 }, "volume 1" },
436 { { '*', 0 }, "volume 1" },
437 { { '(', 0 }, "balance -0.1" },
438 { { ')', 0 }, "balance 0.1" },
439 { { 'm', 0 }, "mute" },
440 { { '1', 0 }, "contrast -1" },
441 { { '2', 0 }, "contrast 1" },
442 { { '3', 0 }, "brightness -1" },
443 { { '4', 0 }, "brightness 1" },
444 { { '5', 0 }, "hue -1" },
445 { { '6', 0 }, "hue 1" },
446 { { '7', 0 }, "saturation -1" },
447 { { '8', 0 }, "saturation 1" },
448 { { 'd', 0 }, "frame_drop" },
449 { { 'D', 0 }, "step_property_osd deinterlace" },
450 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
451 { { 'r', 0 }, "sub_pos -1" },
452 { { 't', 0 }, "sub_pos +1" },
453 { { 'a', 0 }, "sub_alignment" },
454 { { 'v', 0 }, "sub_visibility" },
455 { { 'j', 0 }, "sub_select" },
456 { { 'J', 0 }, "sub_select -3" },
457 { { 'F', 0 }, "forced_subs_only" },
458 { { '#', 0 }, "switch_audio" },
459 { { '_', 0 }, "step_property switch_video" },
460 { { KEY_TAB, 0 }, "step_property switch_program" },
461 { { 'i', 0 }, "edl_mark" },
462 #ifdef CONFIG_TV
463 { { 'h', 0 }, "tv_step_channel 1" },
464 { { 'k', 0 }, "tv_step_channel -1" },
465 { { 'n', 0 }, "tv_step_norm" },
466 { { 'u', 0 }, "tv_step_chanlist" },
467 #endif
468 { { 'X', 0 }, "step_property teletext_mode 1" },
469 { { 'W', 0 }, "step_property teletext_page 1" },
470 { { 'Q', 0 }, "step_property teletext_page -1" },
471 #ifdef CONFIG_JOYSTICK
472 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
473 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
474 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
475 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
476 { { JOY_BTN0, 0 }, "pause" },
477 { { JOY_BTN1, 0 }, "osd" },
478 { { JOY_BTN2, 0 }, "volume 1"},
479 { { JOY_BTN3, 0 }, "volume -1"},
480 #endif
481 #ifdef CONFIG_APPLE_REMOTE
482 { { AR_PLAY, 0}, "pause" },
483 { { AR_PLAY_HOLD, 0}, "quit" },
484 { { AR_NEXT, 0 }, "seek 30" },
485 { { AR_NEXT_HOLD, 0 }, "seek 120" },
486 { { AR_PREV, 0 }, "seek -10" },
487 { { AR_PREV_HOLD, 0 }, "seek -120" },
488 { { AR_MENU, 0 }, "osd" },
489 { { AR_MENU_HOLD, 0 }, "mute" },
490 { { AR_VUP, 0 }, "volume 1"},
491 { { AR_VDOWN, 0 }, "volume -1"},
492 #endif
493 { { 'T', 0 }, "vo_ontop" },
494 { { 'f', 0 }, "vo_fullscreen" },
495 { { 'C', 0 }, "step_property_osd capturing" },
496 { { 's', 0 }, "screenshot 0" },
497 { { 'S', 0 }, "screenshot 1" },
498 { { 'w', 0 }, "panscan -0.1" },
499 { { 'e', 0 }, "panscan +0.1" },
501 { { KEY_POWER, 0 }, "quit" },
502 { { KEY_MENU, 0 }, "osd" },
503 { { KEY_PLAY, 0 }, "pause" },
504 { { KEY_PAUSE, 0 }, "pause" },
505 { { KEY_PLAYPAUSE, 0 }, "pause" },
506 { { KEY_STOP, 0 }, "quit" },
507 { { KEY_FORWARD, 0 }, "seek 60" },
508 { { KEY_REWIND, 0 }, "seek -60" },
509 { { KEY_NEXT, 0 }, "pt_step 1" },
510 { { KEY_PREV, 0 }, "pt_step -1" },
511 { { KEY_VOLUME_UP, 0 }, "volume 1" },
512 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
513 { { KEY_MUTE, 0 }, "mute" },
515 { { KEY_CLOSE_WIN, 0 }, "quit" },
517 { { '!', 0 }, "seek_chapter -1" },
518 { { '@', 0 }, "seek_chapter 1" },
519 { { 'A', 0 }, "switch_angle 1" },
520 { { 'U', 0 }, "stop" },
522 { { 0 }, NULL }
526 #ifndef MP_MAX_KEY_FD
527 #define MP_MAX_KEY_FD 10
528 #endif
530 #ifndef MP_MAX_CMD_FD
531 #define MP_MAX_CMD_FD 10
532 #endif
534 #define CMD_QUEUE_SIZE 100
536 typedef struct mp_input_fd {
537 int fd;
538 union {
539 mp_key_func_t key;
540 mp_cmd_func_t cmd;
541 } read_func;
542 mp_close_func_t close_func;
543 void *ctx;
544 unsigned eof : 1;
545 unsigned drop : 1;
546 unsigned dead : 1;
547 unsigned got_cmd : 1;
548 unsigned no_select : 1;
549 // These fields are for the cmd fds.
550 char *buffer;
551 int pos, size;
552 } mp_input_fd_t;
554 typedef struct mp_cmd_filter mp_cmd_filter_t;
556 struct mp_cmd_filter {
557 mp_input_cmd_filter filter;
558 void *ctx;
559 mp_cmd_filter_t *next;
562 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
564 struct mp_cmd_bind_section {
565 mp_cmd_bind_t *cmd_binds;
566 char *section;
567 mp_cmd_bind_section_t *next;
570 struct input_ctx {
571 // Autorepeat stuff
572 short ar_state;
573 mp_cmd_t *ar_cmd;
574 unsigned int last_ar;
575 // Autorepeat config
576 unsigned int ar_delay;
577 unsigned int ar_rate;
579 // these are the keys currently down
580 int key_down[MP_MAX_KEY_DOWN];
581 unsigned int num_key_down;
582 unsigned int last_key_down;
584 bool default_bindings;
585 // List of command binding sections
586 mp_cmd_bind_section_t *cmd_bind_sections;
587 // Name of currently used command section
588 char *section;
589 // The command binds of current section
590 mp_cmd_bind_t *cmd_binds;
591 mp_cmd_bind_t *cmd_binds_default;
593 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
594 unsigned int num_key_fd;
596 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
597 unsigned int num_cmd_fd;
599 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
600 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
604 static mp_cmd_filter_t *cmd_filters = NULL;
606 // Callback to allow the menu filter to grab the incoming keys
607 int (*mp_input_key_cb)(int code) = NULL;
609 int async_quit_request;
611 static int print_key_list(m_option_t *cfg);
612 static int print_cmd_list(m_option_t *cfg);
614 // Our command line options
615 static const m_option_t input_conf[] = {
616 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
617 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
618 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
619 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
620 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
621 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
622 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
623 OPT_STRING("file", input.in_file, CONF_GLOBAL),
624 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
625 { NULL, NULL, 0, 0, 0, 0, NULL}
628 static const m_option_t mp_input_opts[] = {
629 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
630 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
631 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
632 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
633 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
634 { NULL, NULL, 0, 0, 0, 0, NULL}
637 static int default_cmd_func(int fd, char *buf, int l);
639 static char *get_key_name(int key)
641 char *ret = talloc_strdup(NULL, "");
642 for (int i = 0; modifier_names[i].name; i++) {
643 if (modifier_names[i].key & key) {
644 ret = talloc_asprintf_append_buffer(ret, "%s+",
645 modifier_names[i].name);
646 key -= modifier_names[i].key;
649 for (int i = 0; key_names[i].name != NULL; i++) {
650 if (key_names[i].key == key)
651 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
654 if (isascii(key))
655 return talloc_asprintf_append_buffer(ret, "%c", key);
657 // Print the hex key code
658 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
661 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
662 mp_cmd_func_t read_func, mp_close_func_t close_func)
664 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
665 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
666 "cannot register file descriptor %d.\n", fd);
667 return 0;
669 if (select && fd < 0) {
670 mp_msg(MSGT_INPUT, MSGL_ERR,
671 "Invalid fd %d in mp_input_add_cmd_fd", fd);
672 return 0;
675 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
676 .fd = fd,
677 .read_func.cmd = read_func ? read_func : default_cmd_func,
678 .close_func = close_func,
679 .no_select = !select
681 ictx->num_cmd_fd++;
683 return 1;
686 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
688 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
689 unsigned int i;
691 for (i = 0; i < ictx->num_cmd_fd; i++) {
692 if (cmd_fds[i].fd == fd)
693 break;
695 if (i == ictx->num_cmd_fd)
696 return;
697 if (cmd_fds[i].close_func)
698 cmd_fds[i].close_func(cmd_fds[i].fd);
699 talloc_free(cmd_fds[i].buffer);
701 if (i + 1 < ictx->num_cmd_fd)
702 memmove(&cmd_fds[i], &cmd_fds[i + 1],
703 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
704 ictx->num_cmd_fd--;
707 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
709 struct mp_input_fd *key_fds = ictx->key_fds;
710 unsigned int i;
712 for (i = 0; i < ictx->num_key_fd; i++) {
713 if (key_fds[i].fd == fd)
714 break;
716 if (i == ictx->num_key_fd)
717 return;
718 if (key_fds[i].close_func)
719 key_fds[i].close_func(key_fds[i].fd);
721 if (i + 1 < ictx->num_key_fd)
722 memmove(&key_fds[i], &key_fds[i + 1],
723 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
724 ictx->num_key_fd--;
727 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
728 mp_key_func_t read_func, mp_close_func_t close_func,
729 void *ctx)
731 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
732 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
733 "cannot register file descriptor %d.\n", fd);
734 return 0;
736 if (select && fd < 0) {
737 mp_msg(MSGT_INPUT, MSGL_ERR,
738 "Invalid fd %d in mp_input_add_key_fd", fd);
739 return 0;
742 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
743 .fd = fd,
744 .read_func.key = read_func,
745 .close_func = close_func,
746 .no_select = !select,
747 .ctx = ctx,
749 ictx->num_key_fd++;
751 return 1;
754 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
756 int cmd_num = 0;
758 while (*str == '\n' || *str == '\r' || *str == ' ')
759 ++str;
760 while (*str) {
761 mp_cmd_t *cmd;
762 size_t len = strcspn(str, "\r\n");
763 char *cmdbuf = talloc_size(NULL, len + 1);
764 av_strlcpy(cmdbuf, str, len + 1);
765 cmd = mp_input_parse_cmd(cmdbuf);
766 if (cmd) {
767 mp_input_queue_cmd(ictx, cmd);
768 ++cmd_num;
770 str += len;
771 while (*str == '\n' || *str == '\r' || *str == ' ')
772 ++str;
773 talloc_free(cmdbuf);
775 return cmd_num;
778 mp_cmd_t *mp_input_parse_cmd(char *str)
780 int i, l;
781 int pausing = 0;
782 char *ptr, *e;
783 const mp_cmd_t *cmd_def;
785 #ifdef MP_DEBUG
786 assert(str != NULL);
787 #endif
789 // Ignore heading spaces.
790 while (str[0] == ' ' || str[0] == '\t')
791 ++str;
793 if (strncmp(str, "pausing ", 8) == 0) {
794 pausing = 1;
795 str = &str[8];
796 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
797 pausing = 2;
798 str = &str[13];
799 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
800 pausing = 3;
801 str = &str[15];
802 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
803 pausing = 4;
804 str = &str[19];
807 ptr = str + strcspn(str, "\t ");
808 if (*ptr != 0)
809 l = ptr - str;
810 else
811 l = strlen(str);
813 if (l == 0)
814 return NULL;
816 for (i = 0; mp_cmds[i].name != NULL; i++) {
817 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
818 break;
821 if (mp_cmds[i].name == NULL)
822 return NULL;
824 cmd_def = &mp_cmds[i];
826 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
827 *cmd = (mp_cmd_t){
828 .id = cmd_def->id,
829 .name = talloc_strdup(cmd, cmd_def->name),
830 .pausing = pausing,
833 ptr = str;
835 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
836 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
837 ptr++;
838 if (ptr[0] == '\0')
839 break;
840 while (ptr[0] == ' ' || ptr[0] == '\t')
841 ptr++;
842 if (ptr[0] == '\0' || ptr[0] == '#')
843 break;
844 cmd->args[i].type = cmd_def->args[i].type;
845 switch (cmd_def->args[i].type) {
846 case MP_CMD_ARG_INT:
847 errno = 0;
848 cmd->args[i].v.i = atoi(ptr);
849 if (errno != 0) {
850 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
851 "isn't an integer.\n", cmd_def->name, i + 1);
852 ptr = NULL;
854 break;
855 case MP_CMD_ARG_FLOAT:
856 errno = 0;
857 cmd->args[i].v.f = atof(ptr);
858 if (errno != 0) {
859 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
860 "isn't a float.\n", cmd_def->name, i + 1);
861 ptr = NULL;
863 break;
864 case MP_CMD_ARG_STRING: {
865 char term;
866 char *ptr2 = ptr, *start;
868 if (ptr[0] == '\'' || ptr[0] == '"') {
869 term = ptr[0];
870 ptr2++;
871 } else
872 term = ' ';
873 start = ptr2;
874 while (1) {
875 e = strchr(ptr2, term);
876 if (!e)
877 break;
878 if (e <= ptr2 || *(e - 1) != '\\')
879 break;
880 ptr2 = e + 1;
883 if (term != ' ' && (!e || e[0] == '\0')) {
884 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
885 "unterminated.\n", cmd_def->name, i + 1);
886 ptr = NULL;
887 break;
888 } else if (!e)
889 e = ptr + strlen(ptr);
890 l = e - start;
891 ptr2 = start;
892 for (e = strchr(ptr2, '\\'); e && e < start + l; e = strchr(ptr2, '\\')) {
893 memmove(e, e + 1, strlen(e));
894 ptr2 = e + 1;
895 l--;
897 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
898 if (term != ' ')
899 ptr += l + 2;
900 break;
902 case -1:
903 ptr = NULL;
904 break;
905 default:
906 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
909 cmd->nargs = i;
911 if (cmd_def->nargs > cmd->nargs) {
912 /* mp_msg(MSGT_INPUT, MSGL_ERR, "Got command '%s' but\n", str); */
913 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d "
914 "arguments, we found only %d so far.\n", cmd_def->name,
915 cmd_def->nargs, cmd->nargs);
916 mp_cmd_free(cmd);
917 return NULL;
920 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1; i++) {
921 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(mp_cmd_arg_t));
922 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
923 && cmd_def->args[i].v.s != NULL)
924 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
927 if (i < MP_CMD_MAX_ARGS)
928 cmd->args[i].type = -1;
930 return cmd;
933 #define MP_CMD_MAX_SIZE 4096
935 static int read_cmd(mp_input_fd_t *mp_fd, char **ret)
937 char *end;
938 *ret = NULL;
940 // Allocate the buffer if it doesn't exist
941 if (!mp_fd->buffer) {
942 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
943 mp_fd->pos = 0;
944 mp_fd->size = MP_CMD_MAX_SIZE;
947 // Get some data if needed/possible
948 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
949 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
950 mp_fd->size - 1 - mp_fd->pos);
951 // Error ?
952 if (r < 0) {
953 switch (r) {
954 case MP_INPUT_ERROR:
955 case MP_INPUT_DEAD:
956 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
957 "command file descriptor %d: %s\n",
958 mp_fd->fd, strerror(errno));
959 case MP_INPUT_NOTHING:
960 return r;
961 case MP_INPUT_RETRY:
962 continue;
964 // EOF ?
965 } else if (r == 0) {
966 mp_fd->eof = 1;
967 break;
969 mp_fd->pos += r;
970 break;
973 mp_fd->got_cmd = 0;
975 while (1) {
976 int l = 0;
977 // Find the cmd end
978 mp_fd->buffer[mp_fd->pos] = '\0';
979 end = strchr(mp_fd->buffer, '\r');
980 if (end)
981 *end = '\n';
982 end = strchr(mp_fd->buffer, '\n');
983 // No cmd end ?
984 if (!end) {
985 // If buffer is full we must drop all until the next \n
986 if (mp_fd->size - mp_fd->pos <= 1) {
987 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
988 "descriptor %d is full: dropping content.\n",
989 mp_fd->fd);
990 mp_fd->pos = 0;
991 mp_fd->drop = 1;
993 break;
995 // We already have a cmd : set the got_cmd flag
996 else if ((*ret)) {
997 mp_fd->got_cmd = 1;
998 break;
1001 l = end - mp_fd->buffer;
1003 // Not dropping : put the cmd in ret
1004 if (!mp_fd->drop)
1005 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1006 else
1007 mp_fd->drop = 0;
1008 mp_fd->pos -= l + 1;
1009 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1012 if (*ret)
1013 return 1;
1014 else
1015 return MP_INPUT_NOTHING;
1018 static int default_cmd_func(int fd, char *buf, int l)
1020 while (1) {
1021 int r = read(fd, buf, l);
1022 // Error ?
1023 if (r < 0) {
1024 if (errno == EINTR)
1025 continue;
1026 else if (errno == EAGAIN)
1027 return MP_INPUT_NOTHING;
1028 return MP_INPUT_ERROR;
1029 // EOF ?
1031 return r;
1036 void mp_input_add_cmd_filter(mp_input_cmd_filter func, void *ctx)
1038 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1040 filter->filter = func;
1041 filter->ctx = ctx;
1042 filter->next = cmd_filters;
1043 cmd_filters = filter;
1047 static char *find_bind_for_key(const mp_cmd_bind_t *binds, int n, int *keys)
1049 int j;
1051 if (n <= 0)
1052 return NULL;
1053 for (j = 0; binds[j].cmd != NULL; j++) {
1054 int found = 1, s;
1055 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1056 if (binds[j].input[s] != keys[s]) {
1057 found = 0;
1058 break;
1061 if (found && binds[j].input[s] == 0 && s == n)
1062 break;
1064 return binds[j].cmd;
1067 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1068 char *section)
1070 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1072 if (section == NULL)
1073 section = "default";
1074 while (bind_section) {
1075 if (strcmp(section, bind_section->section) == 0)
1076 return bind_section;
1077 if (bind_section->next == NULL)
1078 break;
1079 bind_section = bind_section->next;
1081 if (bind_section) {
1082 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1083 bind_section = bind_section->next;
1084 } else {
1085 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1086 bind_section = ictx->cmd_bind_sections;
1088 bind_section->cmd_binds = NULL;
1089 bind_section->section = talloc_strdup(bind_section, section);
1090 bind_section->next = NULL;
1091 return bind_section;
1094 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1096 char *cmd = NULL;
1097 mp_cmd_t *ret;
1099 if (ictx->cmd_binds)
1100 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1101 if (ictx->cmd_binds_default && cmd == NULL)
1102 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1103 if (ictx->default_bindings && cmd == NULL)
1104 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1106 if (cmd == NULL) {
1107 char *key_buf = get_key_name(keys[0]);
1108 mp_tmsg(MSGT_INPUT, MSGL_WARN, "No bind found for key '%s'.", key_buf);
1109 talloc_free(key_buf);
1110 if (n > 1) {
1111 for (int s = 1; s < n; s++) {
1112 key_buf = get_key_name(keys[s]);
1113 mp_msg(MSGT_INPUT, MSGL_WARN, "-%s", key_buf);
1114 talloc_free(key_buf);
1117 mp_msg(MSGT_INPUT, MSGL_WARN, " \n");
1118 return NULL;
1120 if (strcmp(cmd, "ignore") == 0)
1121 return NULL;
1122 ret = mp_input_parse_cmd(cmd);
1123 if (!ret) {
1124 char *key_buf = get_key_name(ictx->key_down[0]);
1125 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Invalid command for bound key %s",
1126 key_buf);
1127 talloc_free(key_buf);
1128 if (ictx->num_key_down > 1) {
1129 unsigned int s;
1130 for (s = 1; s < ictx->num_key_down; s++) {
1131 char *key_buf = get_key_name(ictx->key_down[s]);
1132 mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf);
1133 talloc_free(key_buf);
1136 mp_msg(MSGT_INPUT, MSGL_ERR, " : %s \n", cmd);
1138 return ret;
1142 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1144 unsigned int j;
1145 mp_cmd_t *ret;
1147 /* On normal keyboards shift changes the character code of non-special
1148 * keys, so don't count the modifier separately for those. In other words
1149 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1150 * shift modifier is still kept for special keys like arrow keys.
1152 int unmod = code & ~KEY_MODIFIER_MASK;
1153 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1154 code &= ~KEY_MODIFIER_SHIFT;
1156 if (mp_input_key_cb) {
1157 if (code & MP_KEY_DOWN)
1158 return NULL;
1159 code &= ~(MP_KEY_DOWN | MP_NO_REPEAT_KEY);
1160 if (mp_input_key_cb(code))
1161 return NULL;
1164 if (code & MP_KEY_DOWN) {
1165 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1166 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1167 "at the same time\n");
1168 return NULL;
1170 code &= ~MP_KEY_DOWN;
1171 // Check if we don't already have this key as pushed
1172 for (j = 0; j < ictx->num_key_down; j++) {
1173 if (ictx->key_down[j] == code)
1174 break;
1176 if (j != ictx->num_key_down)
1177 return NULL;
1178 ictx->key_down[ictx->num_key_down] = code;
1179 ictx->num_key_down++;
1180 ictx->last_key_down = GetTimer();
1181 ictx->ar_state = 0;
1182 return NULL;
1184 // button released or press of key with no separate down/up events
1185 for (j = 0; j < ictx->num_key_down; j++) {
1186 if (ictx->key_down[j] == code)
1187 break;
1189 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1190 if (doubleclick) {
1191 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1192 if (!ictx->num_key_down
1193 || ictx->key_down[ictx->num_key_down - 1] != btn)
1194 return NULL;
1195 j = ictx->num_key_down - 1;
1196 ictx->key_down[j] = code;
1198 if (j == ictx->num_key_down) { // was not already down; add temporarily
1199 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1200 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1201 "at the same time\n");
1202 return NULL;
1204 ictx->key_down[ictx->num_key_down] = code;
1205 ictx->num_key_down++;
1206 ictx->last_key_down = 1;
1208 // Interpret only maximal point of multibutton event
1209 ret = ictx->last_key_down ?
1210 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1211 : NULL;
1212 if (doubleclick) {
1213 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1214 return ret;
1216 // Remove the key
1217 if (j + 1 < ictx->num_key_down)
1218 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1219 (ictx->num_key_down - (j + 1)) * sizeof(int));
1220 ictx->num_key_down--;
1221 ictx->last_key_down = 0;
1222 ictx->ar_state = -1;
1223 mp_cmd_free(ictx->ar_cmd);
1224 ictx->ar_cmd = NULL;
1225 return ret;
1228 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1230 // No input : autorepeat ?
1231 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1232 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1233 unsigned int t = GetTimer();
1234 // First time : wait delay
1235 if (ictx->ar_state == 0
1236 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1237 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1238 ictx->key_down);
1239 if (!ictx->ar_cmd) {
1240 ictx->ar_state = -1;
1241 return NULL;
1243 ictx->ar_state = 1;
1244 ictx->last_ar = t;
1245 return mp_cmd_clone(ictx->ar_cmd);
1246 // Then send rate / sec event
1247 } else if (ictx->ar_state == 1
1248 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1249 ictx->last_ar = t;
1250 return mp_cmd_clone(ictx->ar_cmd);
1253 return NULL;
1258 * \param time time to wait at most for an event in milliseconds
1260 static mp_cmd_t *read_events(struct input_ctx *ictx, int time)
1262 int i;
1263 int got_cmd = 0;
1264 struct mp_input_fd *key_fds = ictx->key_fds;
1265 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1266 for (i = 0; i < ictx->num_key_fd; i++)
1267 if (key_fds[i].dead) {
1268 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1269 i--;
1271 for (i = 0; i < ictx->num_cmd_fd; i++)
1272 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1273 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1274 i--;
1275 } else if (cmd_fds[i].got_cmd)
1276 got_cmd = 1;
1277 #ifdef HAVE_POSIX_SELECT
1278 fd_set fds;
1279 FD_ZERO(&fds);
1280 if (!got_cmd) {
1281 int max_fd = 0;
1282 for (i = 0; i < ictx->num_key_fd; i++) {
1283 if (key_fds[i].no_select)
1284 continue;
1285 if (key_fds[i].fd > max_fd)
1286 max_fd = key_fds[i].fd;
1287 FD_SET(key_fds[i].fd, &fds);
1289 for (i = 0; i < ictx->num_cmd_fd; i++) {
1290 if (cmd_fds[i].no_select)
1291 continue;
1292 if (cmd_fds[i].fd > max_fd)
1293 max_fd = cmd_fds[i].fd;
1294 FD_SET(cmd_fds[i].fd, &fds);
1296 struct timeval tv, *time_val;
1297 if (time >= 0) {
1298 tv.tv_sec = time / 1000;
1299 tv.tv_usec = (time % 1000) * 1000;
1300 time_val = &tv;
1301 } else
1302 time_val = NULL;
1303 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1304 if (errno != EINTR)
1305 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1306 strerror(errno));
1307 FD_ZERO(&fds);
1310 #else
1311 if (!got_cmd && time)
1312 usec_sleep(time * 1000);
1313 #endif
1316 for (i = 0; i < ictx->num_key_fd; i++) {
1317 #ifdef HAVE_POSIX_SELECT
1318 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1319 continue;
1320 #endif
1322 int code;
1323 while (1) {
1324 code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1325 if (code < 0) {
1326 if (code == MP_INPUT_RELEASE_ALL) {
1327 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1328 ictx->num_key_down = 0;
1329 ictx->last_key_down = 0;
1330 continue;
1332 break;
1334 mp_cmd_t *ret = interpret_key(ictx, code);
1335 if (ret)
1336 return ret;
1338 if (code == MP_INPUT_ERROR)
1339 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input "
1340 "file descriptor %d\n", key_fds[i].fd);
1341 else if (code == MP_INPUT_DEAD) {
1342 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on "
1343 "file descriptor %d\n", key_fds[i].fd);
1344 key_fds[i].dead = 1;
1347 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx);
1348 if (autorepeat_cmd)
1349 return autorepeat_cmd;
1351 for (i = 0; i < ictx->num_cmd_fd; i++) {
1352 #ifdef HAVE_POSIX_SELECT
1353 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1354 !cmd_fds[i].got_cmd)
1355 continue;
1356 #endif
1357 char *cmd;
1358 int r;
1359 while ((r = read_cmd(&cmd_fds[i], &cmd)) >= 0) {
1360 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1361 talloc_free(cmd);
1362 if (ret)
1363 return ret;
1365 if (r == MP_INPUT_ERROR)
1366 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command "
1367 "file descriptor %d\n", cmd_fds[i].fd);
1368 else if (r == MP_INPUT_DEAD)
1369 cmd_fds[i].dead = 1;
1372 return NULL;
1376 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1378 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1379 return 0;
1380 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1381 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1382 ictx->cmd_queue_length++;
1383 return 1;
1386 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1388 mp_cmd_t *ret;
1390 if (ictx->cmd_queue_length == 0)
1391 return NULL;
1393 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1395 if (!peek_only) {
1396 ictx->cmd_queue_length--;
1397 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1400 return ret;
1404 * \param peek_only when set, the returned command stays in the queue.
1405 * Do not free the returned cmd whe you set this!
1407 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1409 mp_cmd_t *ret = NULL;
1410 mp_cmd_filter_t *cf;
1411 int from_queue;
1413 if (async_quit_request)
1414 return mp_input_parse_cmd("quit 1");
1415 while (1) {
1416 from_queue = 1;
1417 ret = get_queued_cmd(ictx, peek_only);
1418 if (ret)
1419 break;
1420 from_queue = 0;
1421 ret = read_events(ictx, time);
1422 if (!ret) {
1423 from_queue = 1;
1424 ret = get_queued_cmd(ictx, peek_only);
1426 break;
1428 if (!ret)
1429 return NULL;
1431 for (cf = cmd_filters; cf; cf = cf->next) {
1432 if (cf->filter(ret, cf->ctx)) {
1433 if (peek_only && from_queue)
1434 // The filter ate the cmd, so we remove it from queue
1435 ret = get_queued_cmd(ictx, 0);
1436 mp_cmd_free(ret);
1437 return NULL;
1441 if (!from_queue && peek_only)
1442 mp_input_queue_cmd(ictx, ret);
1444 return ret;
1447 void
1448 mp_cmd_free(mp_cmd_t *cmd) {
1449 talloc_free(cmd);
1452 mp_cmd_t *
1453 mp_cmd_clone(mp_cmd_t *cmd) {
1454 mp_cmd_t *ret;
1455 int i;
1456 #ifdef MP_DEBUG
1457 assert(cmd != NULL);
1458 #endif
1460 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1461 ret->name = talloc_strdup(ret, cmd->name);
1462 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1463 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1464 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1467 return ret;
1470 int mp_input_get_key_from_name(const char *name)
1472 int modifiers = 0;
1473 const char *p;
1474 while (p = strchr(name, '+')) {
1475 for (struct mp_key_name *m = modifier_names; m->name; m++)
1476 if (!bstrcasecmp(BSTR(m->name), (struct bstr){(char *)name, p - name})) {
1477 modifiers |= m->key;
1478 goto found;
1480 if (!strcmp(name, "+"))
1481 return '+' + modifiers;
1482 return -1;
1483 found:
1484 name = p + 1;
1486 int len = strlen(name);
1487 if (len == 1) // Direct key code
1488 return (unsigned char)name[0] + modifiers;
1489 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1490 return strtol(name, NULL, 16) + modifiers;
1492 for (int i = 0; key_names[i].name != NULL; i++) {
1493 if (strcasecmp(key_names[i].name, name) == 0)
1494 return key_names[i].key + modifiers;
1497 return -1;
1500 static int get_input_from_name(char *name, int *keys)
1502 char *end, *ptr;
1503 int n = 0;
1505 ptr = name;
1506 n = 0;
1507 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1508 if (end && end[1] != '\0') {
1509 if (end[1] == '-')
1510 end = &end[1];
1511 end[0] = '\0';
1513 keys[n] = mp_input_get_key_from_name(ptr);
1514 if (keys[n] < 0)
1515 return 0;
1516 n++;
1517 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1518 ptr = &end[1];
1519 else
1520 break;
1522 keys[n] = 0;
1523 return 1;
1526 #define BS_MAX 256
1527 #define SPACE_CHAR " \n\r\t"
1529 static void bind_keys(struct input_ctx *ictx,
1530 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1532 int i = 0, j;
1533 mp_cmd_bind_t *bind = NULL;
1534 mp_cmd_bind_section_t *bind_section = NULL;
1535 char *section = NULL, *p;
1537 #ifdef MP_DEBUG
1538 assert(keys != NULL);
1539 assert(cmd != NULL);
1540 #endif
1542 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1543 *p = 0;
1544 section = ++cmd;
1545 cmd = ++p;
1546 // Jump beginning space
1547 cmd += strspn(cmd, SPACE_CHAR);
1549 bind_section = get_bind_section(ictx, section);
1551 if (bind_section->cmd_binds) {
1552 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1553 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1554 /* NOTHING */;
1555 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1556 bind = &bind_section->cmd_binds[i];
1557 break;
1562 if (!bind) {
1563 bind_section->cmd_binds = talloc_realloc(bind_section,
1564 bind_section->cmd_binds,
1565 mp_cmd_bind_t, i + 2);
1566 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(mp_cmd_bind_t));
1567 bind = &bind_section->cmd_binds[i];
1569 talloc_free(bind->cmd);
1570 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1571 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1574 static int parse_config(struct input_ctx *ictx, char *file)
1576 int fd;
1577 int bs = 0, r, eof = 0, comments = 0;
1578 char *iter, *end;
1579 char buffer[BS_MAX];
1580 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1] = { 0 };
1582 fd = open(file, O_RDONLY);
1584 if (fd < 0) {
1585 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1586 file, strerror(errno));
1587 return 0;
1590 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1592 while (1) {
1593 if (!eof && bs < BS_MAX - 1) {
1594 if (bs > 0)
1595 bs--;
1596 r = read(fd, buffer + bs, BS_MAX - 1 - bs);
1597 if (r < 0) {
1598 if (errno == EINTR)
1599 continue;
1600 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1601 "input config file %s: %s\n", file, strerror(errno));
1602 close(fd);
1603 return 0;
1604 } else if (r == 0) {
1605 eof = 1;
1606 } else {
1607 bs += r + 1;
1608 buffer[bs - 1] = '\0';
1611 // Empty buffer : return
1612 if (bs <= 1) {
1613 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1614 "%d binds\n", file, n_binds);
1615 close(fd);
1616 return 1;
1619 iter = buffer;
1621 if (comments) {
1622 iter += strcspn(iter, "\n");
1623 if (*iter == 0) { // Buffer was full of comment
1624 bs = 0;
1625 continue;
1627 iter++;
1628 r = strlen(iter);
1629 memmove(buffer, iter, r + 1);
1630 bs = r + 1;
1631 comments = 0;
1632 continue;
1635 // Find the wanted key
1636 if (keys[0] == 0) {
1637 // Jump beginning space
1638 iter += strspn(iter, SPACE_CHAR);
1639 if (*iter == 0) { // Buffer was full of space char
1640 bs = 0;
1641 continue;
1643 if (iter[0] == '#') { // Comments
1644 comments = 1;
1645 continue;
1647 // Find the end of the key code name
1648 end = iter + strcspn(iter, SPACE_CHAR);
1649 if (*end == 0) { // Key name doesn't fit in the buffer
1650 if (buffer == iter) {
1651 if (eof && (buffer - iter) == bs)
1652 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1653 "Unfinished binding %s\n", iter);
1654 else
1655 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1656 "for this key name: %s\n", iter);
1657 return 0;
1659 memmove(buffer, iter, end - iter);
1660 bs = end - iter;
1661 continue;
1664 char name[end - iter + 1];
1665 strncpy(name, iter, end - iter);
1666 name[end - iter] = '\0';
1667 if (!get_input_from_name(name, keys)) {
1668 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown key '%s'\n", name);
1669 close(fd);
1670 return 0;
1673 if (bs > (end - buffer))
1674 memmove(buffer, end, bs - (end - buffer));
1675 bs -= end - buffer;
1676 continue;
1677 } else { // Get the command
1678 while (iter[0] == ' ' || iter[0] == '\t')
1679 iter++;
1680 // Found new line
1681 if (iter[0] == '\n' || iter[0] == '\r') {
1682 int i;
1683 char *key_buf = get_key_name(keys[0]);
1684 mp_tmsg(MSGT_INPUT, MSGL_ERR, "No command found for key %s",
1685 key_buf);
1686 talloc_free(key_buf);
1687 for (i = 1; keys[i] != 0; i++) {
1688 char *key_buf = get_key_name(keys[i]);
1689 mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf);
1690 talloc_free(key_buf);
1692 mp_msg(MSGT_INPUT, MSGL_ERR, "\n");
1693 keys[0] = 0;
1694 if (iter > buffer) {
1695 memmove(buffer, iter, bs - (iter - buffer));
1696 bs -= (iter - buffer);
1698 continue;
1700 end = iter + strcspn(iter, "\n\r");
1701 if (*end == 0 && !(eof && ((end + 1) - buffer) == bs)) {
1702 if (iter == buffer) {
1703 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1704 "for command %s\n", buffer);
1705 close(fd);
1706 return 0;
1708 memmove(buffer, iter, end - iter);
1709 bs = end - iter;
1710 continue;
1713 char cmd[end - iter + 1];
1714 strncpy(cmd, iter, end - iter);
1715 cmd[end - iter] = '\0';
1716 //printf("Set bind %d => %s\n",keys[0],cmd);
1717 bind_keys(ictx, keys, cmd);
1718 n_binds++;
1720 keys[0] = 0;
1721 end++;
1722 if (bs > (end - buffer))
1723 memmove(buffer, end, bs - (end - buffer));
1724 bs -= (end - buffer);
1725 buffer[bs - 1] = '\0';
1726 continue;
1729 mp_tmsg(MSGT_INPUT, MSGL_ERR, "What are we doing here?\n");
1730 close(fd);
1731 mp_input_set_section(ictx, NULL);
1732 return 0;
1735 void mp_input_set_section(struct input_ctx *ictx, char *name)
1737 mp_cmd_bind_section_t *bind_section = NULL;
1739 ictx->cmd_binds = NULL;
1740 ictx->cmd_binds_default = NULL;
1741 talloc_free(ictx->section);
1742 if (name)
1743 ictx->section = talloc_strdup(ictx, name);
1744 else
1745 ictx->section = talloc_strdup(ictx, "default");
1746 if ((bind_section = get_bind_section(ictx, ictx->section)))
1747 ictx->cmd_binds = bind_section->cmd_binds;
1748 if (strcmp(ictx->section, "default") == 0)
1749 return;
1750 if ((bind_section = get_bind_section(ictx, NULL)))
1751 ictx->cmd_binds_default = bind_section->cmd_binds;
1754 char *mp_input_get_section(struct input_ctx *ictx)
1756 return ictx->section;
1759 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1761 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1762 *ictx = (struct input_ctx){
1763 .ar_state = -1,
1764 .ar_delay = input_conf->ar_delay,
1765 .ar_rate = input_conf->ar_rate,
1766 .default_bindings = input_conf->default_bindings,
1769 char *file;
1770 char *config_file = input_conf->config_file;
1771 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1772 if (!file)
1773 return ictx;
1775 if (!parse_config(ictx, file)) {
1776 // free file if it was allocated by get_path(),
1777 // before it gets overwritten
1778 if (file != config_file)
1779 free(file);
1780 // Try global conf dir
1781 file = MPLAYER_CONFDIR "/input.conf";
1782 if (!parse_config(ictx, file))
1783 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1784 "input config\n");
1785 } else {
1786 // free file if it was allocated by get_path()
1787 if (file != config_file)
1788 free(file);
1791 #ifdef CONFIG_JOYSTICK
1792 if (input_conf->use_joystick) {
1793 int fd = mp_input_joystick_init(input_conf->js_dev);
1794 if (fd < 0)
1795 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1796 else
1797 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1798 (mp_close_func_t)close, NULL);
1800 #endif
1802 #ifdef CONFIG_LIRC
1803 if (input_conf->use_lirc) {
1804 int fd = mp_input_lirc_init();
1805 if (fd > 0)
1806 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1807 mp_input_lirc_close);
1809 #endif
1811 #ifdef CONFIG_LIRCC
1812 if (input_conf->use_lircc) {
1813 int fd = lircc_init("mplayer", NULL);
1814 if (fd >= 0)
1815 mp_input_add_cmd_fd(ictx, fd, 1, NULL,
1816 (mp_close_func_t)lircc_cleanup);
1818 #endif
1820 #ifdef CONFIG_APPLE_REMOTE
1821 if (input_conf->use_ar) {
1822 if (mp_input_ar_init() < 0)
1823 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1824 else
1825 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1826 mp_input_ar_close, NULL);
1828 #endif
1830 #ifdef CONFIG_APPLE_IR
1831 if (input_conf->use_ar) {
1832 int fd = mp_input_appleir_init(input_conf->ar_dev);
1833 if (fd < 0)
1834 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1835 else
1836 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1837 (mp_close_func_t)close, NULL);
1839 #endif
1841 if (input_conf->in_file) {
1842 struct stat st;
1843 int mode = O_RDONLY | O_NONBLOCK;
1844 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1845 // Note that on Windows stat may fail for named pipes,
1846 // but due to how the API works, using RDONLY should be ok.
1847 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1848 mode = O_RDWR | O_NONBLOCK;
1849 int in_file_fd = open(input_conf->in_file, mode);
1850 if (in_file_fd >= 0)
1851 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1852 (mp_close_func_t)close);
1853 else
1854 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1855 input_conf->in_file, strerror(errno));
1857 return ictx;
1860 void mp_input_uninit(struct input_ctx *ictx)
1862 if (!ictx)
1863 return;
1865 unsigned int i;
1867 for (i = 0; i < ictx->num_key_fd; i++) {
1868 if (ictx->key_fds[i].close_func)
1869 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1872 for (i = 0; i < ictx->num_cmd_fd; i++) {
1873 if (ictx->cmd_fds[i].close_func)
1874 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1876 talloc_free(ictx);
1879 void mp_input_register_options(m_config_t *cfg)
1881 m_config_register_options(cfg, mp_input_opts);
1884 static int print_key_list(m_option_t *cfg)
1886 int i;
1887 printf("\n");
1888 for (i = 0; key_names[i].name != NULL; i++)
1889 printf("%s\n", key_names[i].name);
1890 exit(0);
1893 static int print_cmd_list(m_option_t *cfg)
1895 const mp_cmd_t *cmd;
1896 int i, j;
1897 const char *type;
1899 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1900 printf("%-20.20s", cmd->name);
1901 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1; j++) {
1902 switch (cmd->args[j].type) {
1903 case MP_CMD_ARG_INT:
1904 type = "Integer";
1905 break;
1906 case MP_CMD_ARG_FLOAT:
1907 type = "Float";
1908 break;
1909 case MP_CMD_ARG_STRING:
1910 type = "String";
1911 break;
1912 default:
1913 type = "??";
1915 if (j + 1 > cmd->nargs)
1916 printf(" [%s]", type);
1917 else
1918 printf(" %s", type);
1920 printf("\n");
1922 exit(0);
1926 * \param time time to wait for an interruption in milliseconds
1928 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1930 mp_cmd_t *cmd;
1931 if ((cmd = mp_input_get_cmd(ictx, time, 1)) == NULL)
1932 return 0;
1933 switch (cmd->id) {
1934 case MP_CMD_QUIT:
1935 case MP_CMD_PLAY_TREE_STEP:
1936 case MP_CMD_PLAY_TREE_UP_STEP:
1937 case MP_CMD_PLAY_ALT_SRC_STEP:
1938 // The cmd will be executed when we are back in the main loop
1939 return 1;
1941 // remove the cmd from the queue
1942 cmd = mp_input_get_cmd(ictx, time, 0);
1943 mp_cmd_free(cmd);
1944 return 0;