input: modify interpretation of doubleclick events
[mplayer/greg.git] / input / input.c
blob4c69d00400a0afff7cc6a4987b8e02e60124bf7f
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_BTN3, 0 }, "seek 10" },
380 { { MOUSE_BTN4, 0 }, "seek -10" },
381 { { MOUSE_BTN5, 0 }, "volume 1" },
382 { { MOUSE_BTN6, 0 }, "volume -1" },
384 #ifdef CONFIG_DVDNAV
385 { { KEY_KP8, 0 }, "dvdnav up" }, // up
386 { { KEY_KP2, 0 }, "dvdnav down" }, // down
387 { { KEY_KP4, 0 }, "dvdnav left" }, // left
388 { { KEY_KP6, 0 }, "dvdnav right" }, // right
389 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
390 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
391 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
392 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
393 #endif
395 { { KEY_RIGHT, 0 }, "seek 10" },
396 { { KEY_LEFT, 0 }, "seek -10" },
397 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
398 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
399 { { KEY_UP, 0 }, "seek 60" },
400 { { KEY_DOWN, 0 }, "seek -60" },
401 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
402 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
403 { { KEY_PAGE_UP, 0 }, "seek 600" },
404 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
405 { { '+', 0 }, "audio_delay 0.100" },
406 { { '-', 0 }, "audio_delay -0.100" },
407 { { '[', 0 }, "speed_mult 0.9091" },
408 { { ']', 0 }, "speed_mult 1.1" },
409 { { '{', 0 }, "speed_mult 0.5" },
410 { { '}', 0 }, "speed_mult 2.0" },
411 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
412 { { 'q', 0 }, "quit" },
413 { { KEY_ESC, 0 }, "quit" },
414 { { 'p', 0 }, "pause" },
415 { { ' ', 0 }, "pause" },
416 { { '.', 0 }, "frame_step" },
417 { { KEY_HOME, 0 }, "pt_up_step 1" },
418 { { KEY_END, 0 }, "pt_up_step -1" },
419 { { '>', 0 }, "pt_step 1" },
420 { { KEY_ENTER, 0 }, "pt_step 1 1" },
421 { { '<', 0 }, "pt_step -1" },
422 { { KEY_INS, 0 }, "alt_src_step 1" },
423 { { KEY_DEL, 0 }, "alt_src_step -1" },
424 { { 'o', 0 }, "osd" },
425 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
426 { { 'P', 0 }, "osd_show_progression" },
427 { { 'z', 0 }, "sub_delay -0.1" },
428 { { 'x', 0 }, "sub_delay +0.1" },
429 { { 'g', 0 }, "sub_step -1" },
430 { { 'y', 0 }, "sub_step +1" },
431 { { '9', 0 }, "volume -1" },
432 { { '/', 0 }, "volume -1" },
433 { { '0', 0 }, "volume 1" },
434 { { '*', 0 }, "volume 1" },
435 { { '(', 0 }, "balance -0.1" },
436 { { ')', 0 }, "balance 0.1" },
437 { { 'm', 0 }, "mute" },
438 { { '1', 0 }, "contrast -1" },
439 { { '2', 0 }, "contrast 1" },
440 { { '3', 0 }, "brightness -1" },
441 { { '4', 0 }, "brightness 1" },
442 { { '5', 0 }, "hue -1" },
443 { { '6', 0 }, "hue 1" },
444 { { '7', 0 }, "saturation -1" },
445 { { '8', 0 }, "saturation 1" },
446 { { 'd', 0 }, "frame_drop" },
447 { { 'D', 0 }, "step_property_osd deinterlace" },
448 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
449 { { 'r', 0 }, "sub_pos -1" },
450 { { 't', 0 }, "sub_pos +1" },
451 { { 'a', 0 }, "sub_alignment" },
452 { { 'v', 0 }, "sub_visibility" },
453 { { 'j', 0 }, "sub_select" },
454 { { 'J', 0 }, "sub_select -3" },
455 { { 'F', 0 }, "forced_subs_only" },
456 { { '#', 0 }, "switch_audio" },
457 { { '_', 0 }, "step_property switch_video" },
458 { { KEY_TAB, 0 }, "step_property switch_program" },
459 { { 'i', 0 }, "edl_mark" },
460 #ifdef CONFIG_TV
461 { { 'h', 0 }, "tv_step_channel 1" },
462 { { 'k', 0 }, "tv_step_channel -1" },
463 { { 'n', 0 }, "tv_step_norm" },
464 { { 'u', 0 }, "tv_step_chanlist" },
465 #endif
466 { { 'X', 0 }, "step_property teletext_mode 1" },
467 { { 'W', 0 }, "step_property teletext_page 1" },
468 { { 'Q', 0 }, "step_property teletext_page -1" },
469 #ifdef CONFIG_JOYSTICK
470 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
471 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
472 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
473 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
474 { { JOY_BTN0, 0 }, "pause" },
475 { { JOY_BTN1, 0 }, "osd" },
476 { { JOY_BTN2, 0 }, "volume 1"},
477 { { JOY_BTN3, 0 }, "volume -1"},
478 #endif
479 #ifdef CONFIG_APPLE_REMOTE
480 { { AR_PLAY, 0}, "pause" },
481 { { AR_PLAY_HOLD, 0}, "quit" },
482 { { AR_NEXT, 0 }, "seek 30" },
483 { { AR_NEXT_HOLD, 0 }, "seek 120" },
484 { { AR_PREV, 0 }, "seek -10" },
485 { { AR_PREV_HOLD, 0 }, "seek -120" },
486 { { AR_MENU, 0 }, "osd" },
487 { { AR_MENU_HOLD, 0 }, "mute" },
488 { { AR_VUP, 0 }, "volume 1"},
489 { { AR_VDOWN, 0 }, "volume -1"},
490 #endif
491 { { 'T', 0 }, "vo_ontop" },
492 { { 'f', 0 }, "vo_fullscreen" },
493 { { 'C', 0 }, "step_property_osd capturing" },
494 { { 's', 0 }, "screenshot 0" },
495 { { 'S', 0 }, "screenshot 1" },
496 { { 'w', 0 }, "panscan -0.1" },
497 { { 'e', 0 }, "panscan +0.1" },
499 { { KEY_POWER, 0 }, "quit" },
500 { { KEY_MENU, 0 }, "osd" },
501 { { KEY_PLAY, 0 }, "pause" },
502 { { KEY_PAUSE, 0 }, "pause" },
503 { { KEY_PLAYPAUSE, 0 }, "pause" },
504 { { KEY_STOP, 0 }, "quit" },
505 { { KEY_FORWARD, 0 }, "seek 60" },
506 { { KEY_REWIND, 0 }, "seek -60" },
507 { { KEY_NEXT, 0 }, "pt_step 1" },
508 { { KEY_PREV, 0 }, "pt_step -1" },
509 { { KEY_VOLUME_UP, 0 }, "volume 1" },
510 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
511 { { KEY_MUTE, 0 }, "mute" },
513 { { KEY_CLOSE_WIN, 0 }, "quit" },
515 { { '!', 0 }, "seek_chapter -1" },
516 { { '@', 0 }, "seek_chapter 1" },
517 { { 'A', 0 }, "switch_angle 1" },
518 { { 'U', 0 }, "stop" },
520 { { 0 }, NULL }
524 #ifndef MP_MAX_KEY_FD
525 #define MP_MAX_KEY_FD 10
526 #endif
528 #ifndef MP_MAX_CMD_FD
529 #define MP_MAX_CMD_FD 10
530 #endif
532 #define CMD_QUEUE_SIZE 100
534 typedef struct mp_input_fd {
535 int fd;
536 union {
537 mp_key_func_t key;
538 mp_cmd_func_t cmd;
539 } read_func;
540 mp_close_func_t close_func;
541 void *ctx;
542 unsigned eof : 1;
543 unsigned drop : 1;
544 unsigned dead : 1;
545 unsigned got_cmd : 1;
546 unsigned no_select : 1;
547 // These fields are for the cmd fds.
548 char *buffer;
549 int pos, size;
550 } mp_input_fd_t;
552 typedef struct mp_cmd_filter mp_cmd_filter_t;
554 struct mp_cmd_filter {
555 mp_input_cmd_filter filter;
556 void *ctx;
557 mp_cmd_filter_t *next;
560 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
562 struct mp_cmd_bind_section {
563 mp_cmd_bind_t *cmd_binds;
564 char *section;
565 mp_cmd_bind_section_t *next;
568 struct input_ctx {
569 // Autorepeat stuff
570 short ar_state;
571 mp_cmd_t *ar_cmd;
572 unsigned int last_ar;
573 // Autorepeat config
574 unsigned int ar_delay;
575 unsigned int ar_rate;
577 // these are the keys currently down
578 int key_down[MP_MAX_KEY_DOWN];
579 unsigned int num_key_down;
580 unsigned int last_key_down;
582 bool default_bindings;
583 // List of command binding sections
584 mp_cmd_bind_section_t *cmd_bind_sections;
585 // Name of currently used command section
586 char *section;
587 // The command binds of current section
588 mp_cmd_bind_t *cmd_binds;
589 mp_cmd_bind_t *cmd_binds_default;
591 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
592 unsigned int num_key_fd;
594 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
595 unsigned int num_cmd_fd;
597 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
598 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
602 static mp_cmd_filter_t *cmd_filters = NULL;
604 // Callback to allow the menu filter to grab the incoming keys
605 int (*mp_input_key_cb)(int code) = NULL;
607 int async_quit_request;
609 static int print_key_list(m_option_t *cfg);
610 static int print_cmd_list(m_option_t *cfg);
612 // Our command line options
613 static const m_option_t input_conf[] = {
614 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
615 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
616 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
617 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
618 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
619 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
620 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
621 OPT_STRING("file", input.in_file, CONF_GLOBAL),
622 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
623 { NULL, NULL, 0, 0, 0, 0, NULL}
626 static const m_option_t mp_input_opts[] = {
627 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
628 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
629 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
630 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
631 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
632 { NULL, NULL, 0, 0, 0, 0, NULL}
635 static int default_cmd_func(int fd, char *buf, int l);
637 static char *get_key_name(int key)
639 char *ret = talloc_strdup(NULL, "");
640 for (int i = 0; modifier_names[i].name; i++) {
641 if (modifier_names[i].key & key) {
642 ret = talloc_asprintf_append_buffer(ret, "%s+",
643 modifier_names[i].name);
644 key -= modifier_names[i].key;
647 for (int i = 0; key_names[i].name != NULL; i++) {
648 if (key_names[i].key == key)
649 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
652 if (isascii(key))
653 return talloc_asprintf_append_buffer(ret, "%c", key);
655 // Print the hex key code
656 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
659 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
660 mp_cmd_func_t read_func, mp_close_func_t close_func)
662 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
663 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
664 "cannot register file descriptor %d.\n", fd);
665 return 0;
667 if (select && fd < 0) {
668 mp_msg(MSGT_INPUT, MSGL_ERR,
669 "Invalid fd %d in mp_input_add_cmd_fd", fd);
670 return 0;
673 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
674 .fd = fd,
675 .read_func.cmd = read_func ? read_func : default_cmd_func,
676 .close_func = close_func,
677 .no_select = !select
679 ictx->num_cmd_fd++;
681 return 1;
684 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
686 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
687 unsigned int i;
689 for (i = 0; i < ictx->num_cmd_fd; i++) {
690 if (cmd_fds[i].fd == fd)
691 break;
693 if (i == ictx->num_cmd_fd)
694 return;
695 if (cmd_fds[i].close_func)
696 cmd_fds[i].close_func(cmd_fds[i].fd);
697 talloc_free(cmd_fds[i].buffer);
699 if (i + 1 < ictx->num_cmd_fd)
700 memmove(&cmd_fds[i], &cmd_fds[i + 1],
701 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
702 ictx->num_cmd_fd--;
705 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
707 struct mp_input_fd *key_fds = ictx->key_fds;
708 unsigned int i;
710 for (i = 0; i < ictx->num_key_fd; i++) {
711 if (key_fds[i].fd == fd)
712 break;
714 if (i == ictx->num_key_fd)
715 return;
716 if (key_fds[i].close_func)
717 key_fds[i].close_func(key_fds[i].fd);
719 if (i + 1 < ictx->num_key_fd)
720 memmove(&key_fds[i], &key_fds[i + 1],
721 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
722 ictx->num_key_fd--;
725 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
726 mp_key_func_t read_func, mp_close_func_t close_func,
727 void *ctx)
729 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
730 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
731 "cannot register file descriptor %d.\n", fd);
732 return 0;
734 if (select && fd < 0) {
735 mp_msg(MSGT_INPUT, MSGL_ERR,
736 "Invalid fd %d in mp_input_add_key_fd", fd);
737 return 0;
740 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
741 .fd = fd,
742 .read_func.key = read_func,
743 .close_func = close_func,
744 .no_select = !select,
745 .ctx = ctx,
747 ictx->num_key_fd++;
749 return 1;
752 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
754 int cmd_num = 0;
756 while (*str == '\n' || *str == '\r' || *str == ' ')
757 ++str;
758 while (*str) {
759 mp_cmd_t *cmd;
760 size_t len = strcspn(str, "\r\n");
761 char *cmdbuf = talloc_size(NULL, len + 1);
762 av_strlcpy(cmdbuf, str, len + 1);
763 cmd = mp_input_parse_cmd(cmdbuf);
764 if (cmd) {
765 mp_input_queue_cmd(ictx, cmd);
766 ++cmd_num;
768 str += len;
769 while (*str == '\n' || *str == '\r' || *str == ' ')
770 ++str;
771 talloc_free(cmdbuf);
773 return cmd_num;
776 mp_cmd_t *mp_input_parse_cmd(char *str)
778 int i, l;
779 int pausing = 0;
780 char *ptr, *e;
781 const mp_cmd_t *cmd_def;
783 #ifdef MP_DEBUG
784 assert(str != NULL);
785 #endif
787 // Ignore heading spaces.
788 while (str[0] == ' ' || str[0] == '\t')
789 ++str;
791 if (strncmp(str, "pausing ", 8) == 0) {
792 pausing = 1;
793 str = &str[8];
794 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
795 pausing = 2;
796 str = &str[13];
797 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
798 pausing = 3;
799 str = &str[15];
800 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
801 pausing = 4;
802 str = &str[19];
805 ptr = str + strcspn(str, "\t ");
806 if (*ptr != 0)
807 l = ptr - str;
808 else
809 l = strlen(str);
811 if (l == 0)
812 return NULL;
814 for (i = 0; mp_cmds[i].name != NULL; i++) {
815 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
816 break;
819 if (mp_cmds[i].name == NULL)
820 return NULL;
822 cmd_def = &mp_cmds[i];
824 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
825 *cmd = (mp_cmd_t){
826 .id = cmd_def->id,
827 .name = talloc_strdup(cmd, cmd_def->name),
828 .pausing = pausing,
831 ptr = str;
833 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
834 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
835 ptr++;
836 if (ptr[0] == '\0')
837 break;
838 while (ptr[0] == ' ' || ptr[0] == '\t')
839 ptr++;
840 if (ptr[0] == '\0' || ptr[0] == '#')
841 break;
842 cmd->args[i].type = cmd_def->args[i].type;
843 switch (cmd_def->args[i].type) {
844 case MP_CMD_ARG_INT:
845 errno = 0;
846 cmd->args[i].v.i = atoi(ptr);
847 if (errno != 0) {
848 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
849 "isn't an integer.\n", cmd_def->name, i + 1);
850 ptr = NULL;
852 break;
853 case MP_CMD_ARG_FLOAT:
854 errno = 0;
855 cmd->args[i].v.f = atof(ptr);
856 if (errno != 0) {
857 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
858 "isn't a float.\n", cmd_def->name, i + 1);
859 ptr = NULL;
861 break;
862 case MP_CMD_ARG_STRING: {
863 char term;
864 char *ptr2 = ptr, *start;
866 if (ptr[0] == '\'' || ptr[0] == '"') {
867 term = ptr[0];
868 ptr2++;
869 } else
870 term = ' ';
871 start = ptr2;
872 while (1) {
873 e = strchr(ptr2, term);
874 if (!e)
875 break;
876 if (e <= ptr2 || *(e - 1) != '\\')
877 break;
878 ptr2 = e + 1;
881 if (term != ' ' && (!e || e[0] == '\0')) {
882 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
883 "unterminated.\n", cmd_def->name, i + 1);
884 ptr = NULL;
885 break;
886 } else if (!e)
887 e = ptr + strlen(ptr);
888 l = e - start;
889 ptr2 = start;
890 for (e = strchr(ptr2, '\\'); e && e < start + l; e = strchr(ptr2, '\\')) {
891 memmove(e, e + 1, strlen(e));
892 ptr2 = e + 1;
893 l--;
895 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
896 if (term != ' ')
897 ptr += l + 2;
898 break;
900 case -1:
901 ptr = NULL;
902 break;
903 default:
904 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
907 cmd->nargs = i;
909 if (cmd_def->nargs > cmd->nargs) {
910 /* mp_msg(MSGT_INPUT, MSGL_ERR, "Got command '%s' but\n", str); */
911 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d "
912 "arguments, we found only %d so far.\n", cmd_def->name,
913 cmd_def->nargs, cmd->nargs);
914 mp_cmd_free(cmd);
915 return NULL;
918 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1; i++) {
919 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(mp_cmd_arg_t));
920 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
921 && cmd_def->args[i].v.s != NULL)
922 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
925 if (i < MP_CMD_MAX_ARGS)
926 cmd->args[i].type = -1;
928 return cmd;
931 #define MP_CMD_MAX_SIZE 4096
933 static int read_cmd(mp_input_fd_t *mp_fd, char **ret)
935 char *end;
936 *ret = NULL;
938 // Allocate the buffer if it doesn't exist
939 if (!mp_fd->buffer) {
940 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
941 mp_fd->pos = 0;
942 mp_fd->size = MP_CMD_MAX_SIZE;
945 // Get some data if needed/possible
946 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
947 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
948 mp_fd->size - 1 - mp_fd->pos);
949 // Error ?
950 if (r < 0) {
951 switch (r) {
952 case MP_INPUT_ERROR:
953 case MP_INPUT_DEAD:
954 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
955 "command file descriptor %d: %s\n",
956 mp_fd->fd, strerror(errno));
957 case MP_INPUT_NOTHING:
958 return r;
959 case MP_INPUT_RETRY:
960 continue;
962 // EOF ?
963 } else if (r == 0) {
964 mp_fd->eof = 1;
965 break;
967 mp_fd->pos += r;
968 break;
971 mp_fd->got_cmd = 0;
973 while (1) {
974 int l = 0;
975 // Find the cmd end
976 mp_fd->buffer[mp_fd->pos] = '\0';
977 end = strchr(mp_fd->buffer, '\r');
978 if (end)
979 *end = '\n';
980 end = strchr(mp_fd->buffer, '\n');
981 // No cmd end ?
982 if (!end) {
983 // If buffer is full we must drop all until the next \n
984 if (mp_fd->size - mp_fd->pos <= 1) {
985 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
986 "descriptor %d is full: dropping content.\n",
987 mp_fd->fd);
988 mp_fd->pos = 0;
989 mp_fd->drop = 1;
991 break;
993 // We already have a cmd : set the got_cmd flag
994 else if ((*ret)) {
995 mp_fd->got_cmd = 1;
996 break;
999 l = end - mp_fd->buffer;
1001 // Not dropping : put the cmd in ret
1002 if (!mp_fd->drop)
1003 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1004 else
1005 mp_fd->drop = 0;
1006 mp_fd->pos -= l + 1;
1007 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1010 if (*ret)
1011 return 1;
1012 else
1013 return MP_INPUT_NOTHING;
1016 static int default_cmd_func(int fd, char *buf, int l)
1018 while (1) {
1019 int r = read(fd, buf, l);
1020 // Error ?
1021 if (r < 0) {
1022 if (errno == EINTR)
1023 continue;
1024 else if (errno == EAGAIN)
1025 return MP_INPUT_NOTHING;
1026 return MP_INPUT_ERROR;
1027 // EOF ?
1029 return r;
1034 void mp_input_add_cmd_filter(mp_input_cmd_filter func, void *ctx)
1036 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1038 filter->filter = func;
1039 filter->ctx = ctx;
1040 filter->next = cmd_filters;
1041 cmd_filters = filter;
1045 static char *find_bind_for_key(const mp_cmd_bind_t *binds, int n, int *keys)
1047 int j;
1049 if (n <= 0)
1050 return NULL;
1051 for (j = 0; binds[j].cmd != NULL; j++) {
1052 int found = 1, s;
1053 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1054 if (binds[j].input[s] != keys[s]) {
1055 found = 0;
1056 break;
1059 if (found && binds[j].input[s] == 0 && s == n)
1060 break;
1062 return binds[j].cmd;
1065 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1066 char *section)
1068 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1070 if (section == NULL)
1071 section = "default";
1072 while (bind_section) {
1073 if (strcmp(section, bind_section->section) == 0)
1074 return bind_section;
1075 if (bind_section->next == NULL)
1076 break;
1077 bind_section = bind_section->next;
1079 if (bind_section) {
1080 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1081 bind_section = bind_section->next;
1082 } else {
1083 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1084 bind_section = ictx->cmd_bind_sections;
1086 bind_section->cmd_binds = NULL;
1087 bind_section->section = talloc_strdup(bind_section, section);
1088 bind_section->next = NULL;
1089 return bind_section;
1092 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1094 char *cmd = NULL;
1095 mp_cmd_t *ret;
1097 if (ictx->cmd_binds)
1098 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1099 if (ictx->cmd_binds_default && cmd == NULL)
1100 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1101 if (ictx->default_bindings && cmd == NULL)
1102 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1104 if (cmd == NULL) {
1105 char *key_buf = get_key_name(keys[0]);
1106 mp_tmsg(MSGT_INPUT, MSGL_WARN, "No bind found for key '%s'.", key_buf);
1107 talloc_free(key_buf);
1108 if (n > 1) {
1109 for (int s = 1; s < n; s++) {
1110 key_buf = get_key_name(keys[s]);
1111 mp_msg(MSGT_INPUT, MSGL_WARN, "-%s", key_buf);
1112 talloc_free(key_buf);
1115 mp_msg(MSGT_INPUT, MSGL_WARN, " \n");
1116 return NULL;
1118 if (strcmp(cmd, "ignore") == 0)
1119 return NULL;
1120 ret = mp_input_parse_cmd(cmd);
1121 if (!ret) {
1122 char *key_buf = get_key_name(ictx->key_down[0]);
1123 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Invalid command for bound key %s",
1124 key_buf);
1125 talloc_free(key_buf);
1126 if (ictx->num_key_down > 1) {
1127 unsigned int s;
1128 for (s = 1; s < ictx->num_key_down; s++) {
1129 char *key_buf = get_key_name(ictx->key_down[s]);
1130 mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf);
1131 talloc_free(key_buf);
1134 mp_msg(MSGT_INPUT, MSGL_ERR, " : %s \n", cmd);
1136 return ret;
1140 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1142 unsigned int j;
1143 mp_cmd_t *ret;
1145 /* On normal keyboards shift changes the character code of non-special
1146 * keys, so don't count the modifier separately for those. In other words
1147 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1148 * shift modifier is still kept for special keys like arrow keys.
1150 int unmod = code & ~KEY_MODIFIER_MASK;
1151 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1152 code &= ~KEY_MODIFIER_SHIFT;
1154 if (mp_input_key_cb) {
1155 if (code & MP_KEY_DOWN)
1156 return NULL;
1157 code &= ~(MP_KEY_DOWN | MP_NO_REPEAT_KEY);
1158 if (mp_input_key_cb(code))
1159 return NULL;
1162 if (code & MP_KEY_DOWN) {
1163 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1164 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1165 "at the same time\n");
1166 return NULL;
1168 code &= ~MP_KEY_DOWN;
1169 // Check if we don't already have this key as pushed
1170 for (j = 0; j < ictx->num_key_down; j++) {
1171 if (ictx->key_down[j] == code)
1172 break;
1174 if (j != ictx->num_key_down)
1175 return NULL;
1176 ictx->key_down[ictx->num_key_down] = code;
1177 ictx->num_key_down++;
1178 ictx->last_key_down = GetTimer();
1179 ictx->ar_state = 0;
1180 return NULL;
1182 // button released or press of key with no separate down/up events
1183 for (j = 0; j < ictx->num_key_down; j++) {
1184 if (ictx->key_down[j] == code)
1185 break;
1187 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1188 if (doubleclick) {
1189 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1190 if (!ictx->num_key_down
1191 || ictx->key_down[ictx->num_key_down - 1] != btn)
1192 return NULL;
1193 j = ictx->num_key_down - 1;
1194 ictx->key_down[j] = code;
1196 if (j == ictx->num_key_down) { // was not already down; add temporarily
1197 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1198 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1199 "at the same time\n");
1200 return NULL;
1202 ictx->key_down[ictx->num_key_down] = code;
1203 ictx->num_key_down++;
1204 ictx->last_key_down = 1;
1206 // Interpret only maximal point of multibutton event
1207 ret = ictx->last_key_down ?
1208 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1209 : NULL;
1210 if (doubleclick) {
1211 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1212 return ret;
1214 // Remove the key
1215 if (j + 1 < ictx->num_key_down)
1216 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1217 (ictx->num_key_down - (j + 1)) * sizeof(int));
1218 ictx->num_key_down--;
1219 ictx->last_key_down = 0;
1220 ictx->ar_state = -1;
1221 mp_cmd_free(ictx->ar_cmd);
1222 ictx->ar_cmd = NULL;
1223 return ret;
1226 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1228 // No input : autorepeat ?
1229 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1230 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1231 unsigned int t = GetTimer();
1232 // First time : wait delay
1233 if (ictx->ar_state == 0
1234 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1235 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1236 ictx->key_down);
1237 if (!ictx->ar_cmd) {
1238 ictx->ar_state = -1;
1239 return NULL;
1241 ictx->ar_state = 1;
1242 ictx->last_ar = t;
1243 return mp_cmd_clone(ictx->ar_cmd);
1244 // Then send rate / sec event
1245 } else if (ictx->ar_state == 1
1246 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1247 ictx->last_ar = t;
1248 return mp_cmd_clone(ictx->ar_cmd);
1251 return NULL;
1256 * \param time time to wait at most for an event in milliseconds
1258 static mp_cmd_t *read_events(struct input_ctx *ictx, int time)
1260 int i;
1261 int got_cmd = 0;
1262 struct mp_input_fd *key_fds = ictx->key_fds;
1263 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1264 for (i = 0; i < ictx->num_key_fd; i++)
1265 if (key_fds[i].dead) {
1266 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1267 i--;
1269 for (i = 0; i < ictx->num_cmd_fd; i++)
1270 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1271 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1272 i--;
1273 } else if (cmd_fds[i].got_cmd)
1274 got_cmd = 1;
1275 #ifdef HAVE_POSIX_SELECT
1276 fd_set fds;
1277 FD_ZERO(&fds);
1278 if (!got_cmd) {
1279 int max_fd = 0;
1280 for (i = 0; i < ictx->num_key_fd; i++) {
1281 if (key_fds[i].no_select)
1282 continue;
1283 if (key_fds[i].fd > max_fd)
1284 max_fd = key_fds[i].fd;
1285 FD_SET(key_fds[i].fd, &fds);
1287 for (i = 0; i < ictx->num_cmd_fd; i++) {
1288 if (cmd_fds[i].no_select)
1289 continue;
1290 if (cmd_fds[i].fd > max_fd)
1291 max_fd = cmd_fds[i].fd;
1292 FD_SET(cmd_fds[i].fd, &fds);
1294 struct timeval tv, *time_val;
1295 if (time >= 0) {
1296 tv.tv_sec = time / 1000;
1297 tv.tv_usec = (time % 1000) * 1000;
1298 time_val = &tv;
1299 } else
1300 time_val = NULL;
1301 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1302 if (errno != EINTR)
1303 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1304 strerror(errno));
1305 FD_ZERO(&fds);
1308 #else
1309 if (!got_cmd && time)
1310 usec_sleep(time * 1000);
1311 #endif
1314 for (i = 0; i < ictx->num_key_fd; i++) {
1315 #ifdef HAVE_POSIX_SELECT
1316 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1317 continue;
1318 #endif
1320 int code;
1321 while ((code = key_fds[i].read_func.key(key_fds[i].ctx,
1322 key_fds[i].fd)) >= 0) {
1323 mp_cmd_t *ret = interpret_key(ictx, code);
1324 if (ret)
1325 return ret;
1327 if (code == MP_INPUT_ERROR)
1328 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input "
1329 "file descriptor %d\n", key_fds[i].fd);
1330 else if (code == MP_INPUT_DEAD) {
1331 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on "
1332 "file descriptor %d\n", key_fds[i].fd);
1333 key_fds[i].dead = 1;
1336 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx);
1337 if (autorepeat_cmd)
1338 return autorepeat_cmd;
1340 for (i = 0; i < ictx->num_cmd_fd; i++) {
1341 #ifdef HAVE_POSIX_SELECT
1342 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1343 !cmd_fds[i].got_cmd)
1344 continue;
1345 #endif
1346 char *cmd;
1347 int r;
1348 while ((r = read_cmd(&cmd_fds[i], &cmd)) >= 0) {
1349 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1350 talloc_free(cmd);
1351 if (ret)
1352 return ret;
1354 if (r == MP_INPUT_ERROR)
1355 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command "
1356 "file descriptor %d\n", cmd_fds[i].fd);
1357 else if (r == MP_INPUT_DEAD)
1358 cmd_fds[i].dead = 1;
1361 return NULL;
1365 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1367 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1368 return 0;
1369 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1370 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1371 ictx->cmd_queue_length++;
1372 return 1;
1375 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1377 mp_cmd_t *ret;
1379 if (ictx->cmd_queue_length == 0)
1380 return NULL;
1382 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1384 if (!peek_only) {
1385 ictx->cmd_queue_length--;
1386 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1389 return ret;
1393 * \param peek_only when set, the returned command stays in the queue.
1394 * Do not free the returned cmd whe you set this!
1396 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1398 mp_cmd_t *ret = NULL;
1399 mp_cmd_filter_t *cf;
1400 int from_queue;
1402 if (async_quit_request)
1403 return mp_input_parse_cmd("quit 1");
1404 while (1) {
1405 from_queue = 1;
1406 ret = get_queued_cmd(ictx, peek_only);
1407 if (ret)
1408 break;
1409 from_queue = 0;
1410 ret = read_events(ictx, time);
1411 if (!ret) {
1412 from_queue = 1;
1413 ret = get_queued_cmd(ictx, peek_only);
1415 break;
1417 if (!ret)
1418 return NULL;
1420 for (cf = cmd_filters; cf; cf = cf->next) {
1421 if (cf->filter(ret, cf->ctx)) {
1422 if (peek_only && from_queue)
1423 // The filter ate the cmd, so we remove it from queue
1424 ret = get_queued_cmd(ictx, 0);
1425 mp_cmd_free(ret);
1426 return NULL;
1430 if (!from_queue && peek_only)
1431 mp_input_queue_cmd(ictx, ret);
1433 return ret;
1436 void
1437 mp_cmd_free(mp_cmd_t *cmd) {
1438 talloc_free(cmd);
1441 mp_cmd_t *
1442 mp_cmd_clone(mp_cmd_t *cmd) {
1443 mp_cmd_t *ret;
1444 int i;
1445 #ifdef MP_DEBUG
1446 assert(cmd != NULL);
1447 #endif
1449 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1450 ret->name = talloc_strdup(ret, cmd->name);
1451 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1452 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1453 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1456 return ret;
1459 int mp_input_get_key_from_name(const char *name)
1461 int modifiers = 0;
1462 const char *p;
1463 while (p = strchr(name, '+')) {
1464 for (struct mp_key_name *m = modifier_names; m->name; m++)
1465 if (!bstrcasecmp(BSTR(m->name), (struct bstr){name, p - name})) {
1466 modifiers |= m->key;
1467 goto found;
1469 if (!strcmp(name, "+"))
1470 return '+' + modifiers;
1471 return -1;
1472 found:
1473 name = p + 1;
1475 int len = strlen(name);
1476 if (len == 1) // Direct key code
1477 return (unsigned char)name[0] + modifiers;
1478 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1479 return strtol(name, NULL, 16) + modifiers;
1481 for (int i = 0; key_names[i].name != NULL; i++) {
1482 if (strcasecmp(key_names[i].name, name) == 0)
1483 return key_names[i].key + modifiers;
1486 return -1;
1489 static int get_input_from_name(char *name, int *keys)
1491 char *end, *ptr;
1492 int n = 0;
1494 ptr = name;
1495 n = 0;
1496 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1497 if (end && end[1] != '\0') {
1498 if (end[1] == '-')
1499 end = &end[1];
1500 end[0] = '\0';
1502 keys[n] = mp_input_get_key_from_name(ptr);
1503 if (keys[n] < 0)
1504 return 0;
1505 n++;
1506 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1507 ptr = &end[1];
1508 else
1509 break;
1511 keys[n] = 0;
1512 return 1;
1515 #define BS_MAX 256
1516 #define SPACE_CHAR " \n\r\t"
1518 static void bind_keys(struct input_ctx *ictx,
1519 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1521 int i = 0, j;
1522 mp_cmd_bind_t *bind = NULL;
1523 mp_cmd_bind_section_t *bind_section = NULL;
1524 char *section = NULL, *p;
1526 #ifdef MP_DEBUG
1527 assert(keys != NULL);
1528 assert(cmd != NULL);
1529 #endif
1531 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1532 *p = 0;
1533 section = ++cmd;
1534 cmd = ++p;
1535 // Jump beginning space
1536 cmd += strspn(cmd, SPACE_CHAR);
1538 bind_section = get_bind_section(ictx, section);
1540 if (bind_section->cmd_binds) {
1541 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1542 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1543 /* NOTHING */;
1544 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1545 bind = &bind_section->cmd_binds[i];
1546 break;
1551 if (!bind) {
1552 bind_section->cmd_binds = talloc_realloc(bind_section,
1553 bind_section->cmd_binds,
1554 mp_cmd_bind_t, i + 2);
1555 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(mp_cmd_bind_t));
1556 bind = &bind_section->cmd_binds[i];
1558 talloc_free(bind->cmd);
1559 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1560 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1563 static int parse_config(struct input_ctx *ictx, char *file)
1565 int fd;
1566 int bs = 0, r, eof = 0, comments = 0;
1567 char *iter, *end;
1568 char buffer[BS_MAX];
1569 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1] = { 0 };
1571 fd = open(file, O_RDONLY);
1573 if (fd < 0) {
1574 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1575 file, strerror(errno));
1576 return 0;
1579 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1581 while (1) {
1582 if (!eof && bs < BS_MAX - 1) {
1583 if (bs > 0)
1584 bs--;
1585 r = read(fd, buffer + bs, BS_MAX - 1 - bs);
1586 if (r < 0) {
1587 if (errno == EINTR)
1588 continue;
1589 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1590 "input config file %s: %s\n", file, strerror(errno));
1591 close(fd);
1592 return 0;
1593 } else if (r == 0) {
1594 eof = 1;
1595 } else {
1596 bs += r + 1;
1597 buffer[bs - 1] = '\0';
1600 // Empty buffer : return
1601 if (bs <= 1) {
1602 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1603 "%d binds\n", file, n_binds);
1604 close(fd);
1605 return 1;
1608 iter = buffer;
1610 if (comments) {
1611 iter += strcspn(iter, "\n");
1612 if (*iter == 0) { // Buffer was full of comment
1613 bs = 0;
1614 continue;
1616 iter++;
1617 r = strlen(iter);
1618 memmove(buffer, iter, r + 1);
1619 bs = r + 1;
1620 comments = 0;
1621 continue;
1624 // Find the wanted key
1625 if (keys[0] == 0) {
1626 // Jump beginning space
1627 iter += strspn(iter, SPACE_CHAR);
1628 if (*iter == 0) { // Buffer was full of space char
1629 bs = 0;
1630 continue;
1632 if (iter[0] == '#') { // Comments
1633 comments = 1;
1634 continue;
1636 // Find the end of the key code name
1637 end = iter + strcspn(iter, SPACE_CHAR);
1638 if (*end == 0) { // Key name doesn't fit in the buffer
1639 if (buffer == iter) {
1640 if (eof && (buffer - iter) == bs)
1641 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1642 "Unfinished binding %s\n", iter);
1643 else
1644 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1645 "for this key name: %s\n", iter);
1646 return 0;
1648 memmove(buffer, iter, end - iter);
1649 bs = end - iter;
1650 continue;
1653 char name[end - iter + 1];
1654 strncpy(name, iter, end - iter);
1655 name[end - iter] = '\0';
1656 if (!get_input_from_name(name, keys)) {
1657 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown key '%s'\n", name);
1658 close(fd);
1659 return 0;
1662 if (bs > (end - buffer))
1663 memmove(buffer, end, bs - (end - buffer));
1664 bs -= end - buffer;
1665 continue;
1666 } else { // Get the command
1667 while (iter[0] == ' ' || iter[0] == '\t')
1668 iter++;
1669 // Found new line
1670 if (iter[0] == '\n' || iter[0] == '\r') {
1671 int i;
1672 char *key_buf = get_key_name(keys[0]);
1673 mp_tmsg(MSGT_INPUT, MSGL_ERR, "No command found for key %s",
1674 key_buf);
1675 talloc_free(key_buf);
1676 for (i = 1; keys[i] != 0; i++) {
1677 char *key_buf = get_key_name(keys[i]);
1678 mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf);
1679 talloc_free(key_buf);
1681 mp_msg(MSGT_INPUT, MSGL_ERR, "\n");
1682 keys[0] = 0;
1683 if (iter > buffer) {
1684 memmove(buffer, iter, bs - (iter - buffer));
1685 bs -= (iter - buffer);
1687 continue;
1689 end = iter + strcspn(iter, "\n\r");
1690 if (*end == 0 && !(eof && ((end + 1) - buffer) == bs)) {
1691 if (iter == buffer) {
1692 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1693 "for command %s\n", buffer);
1694 close(fd);
1695 return 0;
1697 memmove(buffer, iter, end - iter);
1698 bs = end - iter;
1699 continue;
1702 char cmd[end - iter + 1];
1703 strncpy(cmd, iter, end - iter);
1704 cmd[end - iter] = '\0';
1705 //printf("Set bind %d => %s\n",keys[0],cmd);
1706 bind_keys(ictx, keys, cmd);
1707 n_binds++;
1709 keys[0] = 0;
1710 end++;
1711 if (bs > (end - buffer))
1712 memmove(buffer, end, bs - (end - buffer));
1713 bs -= (end - buffer);
1714 buffer[bs - 1] = '\0';
1715 continue;
1718 mp_tmsg(MSGT_INPUT, MSGL_ERR, "What are we doing here?\n");
1719 close(fd);
1720 mp_input_set_section(ictx, NULL);
1721 return 0;
1724 void mp_input_set_section(struct input_ctx *ictx, char *name)
1726 mp_cmd_bind_section_t *bind_section = NULL;
1728 ictx->cmd_binds = NULL;
1729 ictx->cmd_binds_default = NULL;
1730 talloc_free(ictx->section);
1731 if (name)
1732 ictx->section = talloc_strdup(ictx, name);
1733 else
1734 ictx->section = talloc_strdup(ictx, "default");
1735 if ((bind_section = get_bind_section(ictx, ictx->section)))
1736 ictx->cmd_binds = bind_section->cmd_binds;
1737 if (strcmp(ictx->section, "default") == 0)
1738 return;
1739 if ((bind_section = get_bind_section(ictx, NULL)))
1740 ictx->cmd_binds_default = bind_section->cmd_binds;
1743 char *mp_input_get_section(struct input_ctx *ictx)
1745 return ictx->section;
1748 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1750 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1751 *ictx = (struct input_ctx){
1752 .ar_state = -1,
1753 .ar_delay = input_conf->ar_delay,
1754 .ar_rate = input_conf->ar_rate,
1755 .default_bindings = input_conf->default_bindings,
1758 char *file;
1759 char *config_file = input_conf->config_file;
1760 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1761 if (!file)
1762 return ictx;
1764 if (!parse_config(ictx, file)) {
1765 // free file if it was allocated by get_path(),
1766 // before it gets overwritten
1767 if (file != config_file)
1768 free(file);
1769 // Try global conf dir
1770 file = MPLAYER_CONFDIR "/input.conf";
1771 if (!parse_config(ictx, file))
1772 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1773 "input config\n");
1774 } else {
1775 // free file if it was allocated by get_path()
1776 if (file != config_file)
1777 free(file);
1780 #ifdef CONFIG_JOYSTICK
1781 if (input_conf->use_joystick) {
1782 int fd = mp_input_joystick_init(input_conf->js_dev);
1783 if (fd < 0)
1784 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1785 else
1786 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1787 (mp_close_func_t)close, NULL);
1789 #endif
1791 #ifdef CONFIG_LIRC
1792 if (input_conf->use_lirc) {
1793 int fd = mp_input_lirc_init();
1794 if (fd > 0)
1795 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1796 mp_input_lirc_close);
1798 #endif
1800 #ifdef CONFIG_LIRCC
1801 if (input_conf->use_lircc) {
1802 int fd = lircc_init("mplayer", NULL);
1803 if (fd >= 0)
1804 mp_input_add_cmd_fd(ictx, fd, 1, NULL,
1805 (mp_close_func_t)lircc_cleanup);
1807 #endif
1809 #ifdef CONFIG_APPLE_REMOTE
1810 if (input_conf->use_ar) {
1811 if (mp_input_ar_init() < 0)
1812 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1813 else
1814 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1815 mp_input_ar_close, NULL);
1817 #endif
1819 #ifdef CONFIG_APPLE_IR
1820 if (input_conf->use_ar) {
1821 int fd = mp_input_appleir_init(input_conf->ar_dev);
1822 if (fd < 0)
1823 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1824 else
1825 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1826 (mp_close_func_t)close, NULL);
1828 #endif
1830 if (input_conf->in_file) {
1831 struct stat st;
1832 int mode = O_RDONLY;
1833 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1834 // Note that on Windows stat may fail for named pipes,
1835 // but due to how the API works, using RDONLY should be ok.
1836 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1837 mode = O_RDWR;
1838 int in_file_fd = open(input_conf->in_file, mode);
1839 if (in_file_fd >= 0)
1840 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1841 (mp_close_func_t)close);
1842 else
1843 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1844 input_conf->in_file, strerror(errno));
1846 return ictx;
1849 void mp_input_uninit(struct input_ctx *ictx)
1851 if (!ictx)
1852 return;
1854 unsigned int i;
1856 for (i = 0; i < ictx->num_key_fd; i++) {
1857 if (ictx->key_fds[i].close_func)
1858 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1861 for (i = 0; i < ictx->num_cmd_fd; i++) {
1862 if (ictx->cmd_fds[i].close_func)
1863 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1865 talloc_free(ictx);
1868 void mp_input_register_options(m_config_t *cfg)
1870 m_config_register_options(cfg, mp_input_opts);
1873 static int print_key_list(m_option_t *cfg)
1875 int i;
1876 printf("\n");
1877 for (i = 0; key_names[i].name != NULL; i++)
1878 printf("%s\n", key_names[i].name);
1879 exit(0);
1882 static int print_cmd_list(m_option_t *cfg)
1884 const mp_cmd_t *cmd;
1885 int i, j;
1886 const char *type;
1888 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1889 printf("%-20.20s", cmd->name);
1890 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1; j++) {
1891 switch (cmd->args[j].type) {
1892 case MP_CMD_ARG_INT:
1893 type = "Integer";
1894 break;
1895 case MP_CMD_ARG_FLOAT:
1896 type = "Float";
1897 break;
1898 case MP_CMD_ARG_STRING:
1899 type = "String";
1900 break;
1901 default:
1902 type = "??";
1904 if (j + 1 > cmd->nargs)
1905 printf(" [%s]", type);
1906 else
1907 printf(" %s", type);
1909 printf("\n");
1911 exit(0);
1915 * \param time time to wait for an interruption in milliseconds
1917 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1919 mp_cmd_t *cmd;
1920 if ((cmd = mp_input_get_cmd(ictx, time, 1)) == NULL)
1921 return 0;
1922 switch (cmd->id) {
1923 case MP_CMD_QUIT:
1924 case MP_CMD_PLAY_TREE_STEP:
1925 case MP_CMD_PLAY_TREE_UP_STEP:
1926 case MP_CMD_PLAY_ALT_SRC_STEP:
1927 // The cmd will be executed when we are back in the main loop
1928 return 1;
1930 // remove the cmd from the queue
1931 cmd = mp_input_get_cmd(ictx, time, 0);
1932 mp_cmd_free(cmd);
1933 return 0;