stream_cdda: work around libcdparanoia caching issues
[mplayer.git] / input / input.c
blob94c15f42bbf1612d69be24049732a579e07d0768
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_BTN10, "MOUSE_BTN10" },
281 { MOUSE_BTN11, "MOUSE_BTN11" },
282 { MOUSE_BTN12, "MOUSE_BTN12" },
283 { MOUSE_BTN13, "MOUSE_BTN13" },
284 { MOUSE_BTN14, "MOUSE_BTN14" },
285 { MOUSE_BTN15, "MOUSE_BTN15" },
286 { MOUSE_BTN16, "MOUSE_BTN16" },
287 { MOUSE_BTN17, "MOUSE_BTN17" },
288 { MOUSE_BTN18, "MOUSE_BTN18" },
289 { MOUSE_BTN19, "MOUSE_BTN19" },
290 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
291 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
292 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
293 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
294 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
295 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
296 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
297 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
298 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
299 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
300 { MOUSE_BTN10_DBL, "MOUSE_BTN10_DBL" },
301 { MOUSE_BTN11_DBL, "MOUSE_BTN11_DBL" },
302 { MOUSE_BTN12_DBL, "MOUSE_BTN12_DBL" },
303 { MOUSE_BTN13_DBL, "MOUSE_BTN13_DBL" },
304 { MOUSE_BTN14_DBL, "MOUSE_BTN14_DBL" },
305 { MOUSE_BTN15_DBL, "MOUSE_BTN15_DBL" },
306 { MOUSE_BTN16_DBL, "MOUSE_BTN16_DBL" },
307 { MOUSE_BTN17_DBL, "MOUSE_BTN17_DBL" },
308 { MOUSE_BTN18_DBL, "MOUSE_BTN18_DBL" },
309 { MOUSE_BTN19_DBL, "MOUSE_BTN19_DBL" },
310 { JOY_AXIS1_MINUS, "JOY_UP" },
311 { JOY_AXIS1_PLUS, "JOY_DOWN" },
312 { JOY_AXIS0_MINUS, "JOY_LEFT" },
313 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
315 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
316 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
317 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
318 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
319 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
320 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
321 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
322 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
323 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
324 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
325 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
326 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
327 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
328 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
329 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
330 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
331 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
332 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
333 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
334 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
336 { JOY_BTN0, "JOY_BTN0" },
337 { JOY_BTN1, "JOY_BTN1" },
338 { JOY_BTN2, "JOY_BTN2" },
339 { JOY_BTN3, "JOY_BTN3" },
340 { JOY_BTN4, "JOY_BTN4" },
341 { JOY_BTN5, "JOY_BTN5" },
342 { JOY_BTN6, "JOY_BTN6" },
343 { JOY_BTN7, "JOY_BTN7" },
344 { JOY_BTN8, "JOY_BTN8" },
345 { JOY_BTN9, "JOY_BTN9" },
347 { AR_PLAY, "AR_PLAY" },
348 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
349 { AR_NEXT, "AR_NEXT" },
350 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
351 { AR_PREV, "AR_PREV" },
352 { AR_PREV_HOLD, "AR_PREV_HOLD" },
353 { AR_MENU, "AR_MENU" },
354 { AR_MENU_HOLD, "AR_MENU_HOLD" },
355 { AR_VUP, "AR_VUP" },
356 { AR_VDOWN, "AR_VDOWN" },
358 { KEY_POWER, "POWER" },
359 { KEY_MENU, "MENU" },
360 { KEY_PLAY, "PLAY" },
361 { KEY_PAUSE, "PAUSE" },
362 { KEY_PLAYPAUSE, "PLAYPAUSE" },
363 { KEY_STOP, "STOP" },
364 { KEY_FORWARD, "FORWARD" },
365 { KEY_REWIND, "REWIND" },
366 { KEY_NEXT, "NEXT" },
367 { KEY_PREV, "PREV" },
368 { KEY_VOLUME_UP, "VOLUME_UP" },
369 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
370 { KEY_MUTE, "MUTE" },
372 // These are kept for backward compatibility
373 { KEY_PAUSE, "XF86_PAUSE" },
374 { KEY_STOP, "XF86_STOP" },
375 { KEY_PREV, "XF86_PREV" },
376 { KEY_NEXT, "XF86_NEXT" },
378 { KEY_CLOSE_WIN, "CLOSE_WIN" },
380 { 0, NULL }
383 struct mp_key_name modifier_names[] = {
384 { KEY_MODIFIER_SHIFT, "Shift" },
385 { KEY_MODIFIER_CTRL, "Ctrl" },
386 { KEY_MODIFIER_ALT, "Alt" },
387 { KEY_MODIFIER_META, "Meta" },
388 { 0 }
391 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
393 // This is the default binding. The content of input.conf overrides these.
394 // The first arg is a null terminated array of key codes.
395 // The second is the command
397 static const mp_cmd_bind_t def_cmd_binds[] = {
399 { { MOUSE_BTN0_DBL, 0 }, "vo_fullscreen" },
400 { { MOUSE_BTN2, 0 }, "pause" },
401 { { MOUSE_BTN3, 0 }, "seek 10" },
402 { { MOUSE_BTN4, 0 }, "seek -10" },
403 { { MOUSE_BTN5, 0 }, "volume 1" },
404 { { MOUSE_BTN6, 0 }, "volume -1" },
406 #ifdef CONFIG_DVDNAV
407 { { KEY_KP8, 0 }, "dvdnav up" }, // up
408 { { KEY_KP2, 0 }, "dvdnav down" }, // down
409 { { KEY_KP4, 0 }, "dvdnav left" }, // left
410 { { KEY_KP6, 0 }, "dvdnav right" }, // right
411 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
412 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
413 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
414 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
415 #endif
417 { { KEY_RIGHT, 0 }, "seek 10" },
418 { { KEY_LEFT, 0 }, "seek -10" },
419 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
420 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
421 { { KEY_UP, 0 }, "seek 60" },
422 { { KEY_DOWN, 0 }, "seek -60" },
423 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
424 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
425 { { KEY_PAGE_UP, 0 }, "seek 600" },
426 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
427 { { '+', 0 }, "audio_delay 0.100" },
428 { { '-', 0 }, "audio_delay -0.100" },
429 { { '[', 0 }, "speed_mult 0.9091" },
430 { { ']', 0 }, "speed_mult 1.1" },
431 { { '{', 0 }, "speed_mult 0.5" },
432 { { '}', 0 }, "speed_mult 2.0" },
433 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
434 { { 'q', 0 }, "quit" },
435 { { KEY_ESC, 0 }, "quit" },
436 { { 'p', 0 }, "pause" },
437 { { ' ', 0 }, "pause" },
438 { { '.', 0 }, "frame_step" },
439 { { KEY_HOME, 0 }, "pt_up_step 1" },
440 { { KEY_END, 0 }, "pt_up_step -1" },
441 { { '>', 0 }, "pt_step 1" },
442 { { KEY_ENTER, 0 }, "pt_step 1 1" },
443 { { '<', 0 }, "pt_step -1" },
444 { { KEY_INS, 0 }, "alt_src_step 1" },
445 { { KEY_DEL, 0 }, "alt_src_step -1" },
446 { { 'o', 0 }, "osd" },
447 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
448 { { 'P', 0 }, "osd_show_progression" },
449 { { 'z', 0 }, "sub_delay -0.1" },
450 { { 'x', 0 }, "sub_delay +0.1" },
451 { { 'g', 0 }, "sub_step -1" },
452 { { 'y', 0 }, "sub_step +1" },
453 { { '9', 0 }, "volume -1" },
454 { { '/', 0 }, "volume -1" },
455 { { '0', 0 }, "volume 1" },
456 { { '*', 0 }, "volume 1" },
457 { { '(', 0 }, "balance -0.1" },
458 { { ')', 0 }, "balance 0.1" },
459 { { 'm', 0 }, "mute" },
460 { { '1', 0 }, "contrast -1" },
461 { { '2', 0 }, "contrast 1" },
462 { { '3', 0 }, "brightness -1" },
463 { { '4', 0 }, "brightness 1" },
464 { { '5', 0 }, "hue -1" },
465 { { '6', 0 }, "hue 1" },
466 { { '7', 0 }, "saturation -1" },
467 { { '8', 0 }, "saturation 1" },
468 { { 'd', 0 }, "frame_drop" },
469 { { 'D', 0 }, "step_property_osd deinterlace" },
470 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
471 { { 'r', 0 }, "sub_pos -1" },
472 { { 't', 0 }, "sub_pos +1" },
473 { { 'a', 0 }, "sub_alignment" },
474 { { 'v', 0 }, "sub_visibility" },
475 { { 'j', 0 }, "sub_select" },
476 { { 'J', 0 }, "sub_select -3" },
477 { { 'F', 0 }, "forced_subs_only" },
478 { { '#', 0 }, "switch_audio" },
479 { { '_', 0 }, "step_property switch_video" },
480 { { KEY_TAB, 0 }, "step_property switch_program" },
481 { { 'i', 0 }, "edl_mark" },
482 #ifdef CONFIG_TV
483 { { 'h', 0 }, "tv_step_channel 1" },
484 { { 'k', 0 }, "tv_step_channel -1" },
485 { { 'n', 0 }, "tv_step_norm" },
486 { { 'u', 0 }, "tv_step_chanlist" },
487 #endif
488 { { 'X', 0 }, "step_property teletext_mode 1" },
489 { { 'W', 0 }, "step_property teletext_page 1" },
490 { { 'Q', 0 }, "step_property teletext_page -1" },
491 #ifdef CONFIG_JOYSTICK
492 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
493 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
494 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
495 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
496 { { JOY_BTN0, 0 }, "pause" },
497 { { JOY_BTN1, 0 }, "osd" },
498 { { JOY_BTN2, 0 }, "volume 1"},
499 { { JOY_BTN3, 0 }, "volume -1"},
500 #endif
501 #ifdef CONFIG_APPLE_REMOTE
502 { { AR_PLAY, 0}, "pause" },
503 { { AR_PLAY_HOLD, 0}, "quit" },
504 { { AR_NEXT, 0 }, "seek 30" },
505 { { AR_NEXT_HOLD, 0 }, "seek 120" },
506 { { AR_PREV, 0 }, "seek -10" },
507 { { AR_PREV_HOLD, 0 }, "seek -120" },
508 { { AR_MENU, 0 }, "osd" },
509 { { AR_MENU_HOLD, 0 }, "mute" },
510 { { AR_VUP, 0 }, "volume 1"},
511 { { AR_VDOWN, 0 }, "volume -1"},
512 #endif
513 { { 'T', 0 }, "vo_ontop" },
514 { { 'f', 0 }, "vo_fullscreen" },
515 { { 'C', 0 }, "step_property_osd capturing" },
516 { { 's', 0 }, "screenshot 0" },
517 { { 'S', 0 }, "screenshot 1" },
518 { { 'w', 0 }, "panscan -0.1" },
519 { { 'e', 0 }, "panscan +0.1" },
521 { { KEY_POWER, 0 }, "quit" },
522 { { KEY_MENU, 0 }, "osd" },
523 { { KEY_PLAY, 0 }, "pause" },
524 { { KEY_PAUSE, 0 }, "pause" },
525 { { KEY_PLAYPAUSE, 0 }, "pause" },
526 { { KEY_STOP, 0 }, "quit" },
527 { { KEY_FORWARD, 0 }, "seek 60" },
528 { { KEY_REWIND, 0 }, "seek -60" },
529 { { KEY_NEXT, 0 }, "pt_step 1" },
530 { { KEY_PREV, 0 }, "pt_step -1" },
531 { { KEY_VOLUME_UP, 0 }, "volume 1" },
532 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
533 { { KEY_MUTE, 0 }, "mute" },
535 { { KEY_CLOSE_WIN, 0 }, "quit" },
537 { { '!', 0 }, "seek_chapter -1" },
538 { { '@', 0 }, "seek_chapter 1" },
539 { { 'A', 0 }, "switch_angle 1" },
540 { { 'U', 0 }, "stop" },
542 { { 0 }, NULL }
546 #ifndef MP_MAX_KEY_FD
547 #define MP_MAX_KEY_FD 10
548 #endif
550 #ifndef MP_MAX_CMD_FD
551 #define MP_MAX_CMD_FD 10
552 #endif
554 #define CMD_QUEUE_SIZE 100
556 typedef struct mp_input_fd {
557 int fd;
558 union {
559 mp_key_func_t key;
560 mp_cmd_func_t cmd;
561 } read_func;
562 mp_close_func_t close_func;
563 void *ctx;
564 unsigned eof : 1;
565 unsigned drop : 1;
566 unsigned dead : 1;
567 unsigned got_cmd : 1;
568 unsigned no_select : 1;
569 // These fields are for the cmd fds.
570 char *buffer;
571 int pos, size;
572 } mp_input_fd_t;
574 typedef struct mp_cmd_filter mp_cmd_filter_t;
576 struct mp_cmd_filter {
577 mp_input_cmd_filter filter;
578 void *ctx;
579 mp_cmd_filter_t *next;
582 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
584 struct mp_cmd_bind_section {
585 mp_cmd_bind_t *cmd_binds;
586 char *section;
587 mp_cmd_bind_section_t *next;
590 struct input_ctx {
591 // Autorepeat stuff
592 short ar_state;
593 mp_cmd_t *ar_cmd;
594 unsigned int last_ar;
595 // Autorepeat config
596 unsigned int ar_delay;
597 unsigned int ar_rate;
599 // these are the keys currently down
600 int key_down[MP_MAX_KEY_DOWN];
601 unsigned int num_key_down;
602 unsigned int last_key_down;
604 bool default_bindings;
605 // List of command binding sections
606 mp_cmd_bind_section_t *cmd_bind_sections;
607 // Name of currently used command section
608 char *section;
609 // The command binds of current section
610 mp_cmd_bind_t *cmd_binds;
611 mp_cmd_bind_t *cmd_binds_default;
613 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
614 unsigned int num_key_fd;
616 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
617 unsigned int num_cmd_fd;
619 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
620 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
624 static mp_cmd_filter_t *cmd_filters = NULL;
626 // Callback to allow the menu filter to grab the incoming keys
627 int (*mp_input_key_cb)(int code) = NULL;
629 int async_quit_request;
631 static int print_key_list(m_option_t *cfg);
632 static int print_cmd_list(m_option_t *cfg);
634 // Our command line options
635 static const m_option_t input_conf[] = {
636 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
637 OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL),
638 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
639 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
640 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
641 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
642 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
643 OPT_STRING("file", input.in_file, CONF_GLOBAL),
644 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
645 { NULL, NULL, 0, 0, 0, 0, NULL}
648 static const m_option_t mp_input_opts[] = {
649 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
650 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
651 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
652 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
653 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
654 { NULL, NULL, 0, 0, 0, 0, NULL}
657 static int default_cmd_func(int fd, char *buf, int l);
659 static char *get_key_name(int key, char *ret)
661 for (int i = 0; modifier_names[i].name; i++) {
662 if (modifier_names[i].key & key) {
663 ret = talloc_asprintf_append_buffer(ret, "%s+",
664 modifier_names[i].name);
665 key -= modifier_names[i].key;
668 for (int i = 0; key_names[i].name != NULL; i++) {
669 if (key_names[i].key == key)
670 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
673 if (isascii(key))
674 return talloc_asprintf_append_buffer(ret, "%c", key);
676 // Print the hex key code
677 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
680 static char *get_key_combo_name(int *keys, int max)
682 char *ret = talloc_strdup(NULL, "");
683 while (1) {
684 ret = get_key_name(*keys, ret);
685 if (--max && *++keys)
686 talloc_asprintf_append_buffer(ret, "-");
687 else
688 break;
690 return ret;
693 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
694 mp_cmd_func_t read_func, mp_close_func_t close_func)
696 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
697 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, "
698 "cannot register file descriptor %d.\n", fd);
699 return 0;
701 if (select && fd < 0) {
702 mp_msg(MSGT_INPUT, MSGL_ERR,
703 "Invalid fd %d in mp_input_add_cmd_fd", fd);
704 return 0;
707 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
708 .fd = fd,
709 .read_func.cmd = read_func ? read_func : default_cmd_func,
710 .close_func = close_func,
711 .no_select = !select
713 ictx->num_cmd_fd++;
715 return 1;
718 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
720 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
721 unsigned int i;
723 for (i = 0; i < ictx->num_cmd_fd; i++) {
724 if (cmd_fds[i].fd == fd)
725 break;
727 if (i == ictx->num_cmd_fd)
728 return;
729 if (cmd_fds[i].close_func)
730 cmd_fds[i].close_func(cmd_fds[i].fd);
731 talloc_free(cmd_fds[i].buffer);
733 if (i + 1 < ictx->num_cmd_fd)
734 memmove(&cmd_fds[i], &cmd_fds[i + 1],
735 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
736 ictx->num_cmd_fd--;
739 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
741 struct mp_input_fd *key_fds = ictx->key_fds;
742 unsigned int i;
744 for (i = 0; i < ictx->num_key_fd; i++) {
745 if (key_fds[i].fd == fd)
746 break;
748 if (i == ictx->num_key_fd)
749 return;
750 if (key_fds[i].close_func)
751 key_fds[i].close_func(key_fds[i].fd);
753 if (i + 1 < ictx->num_key_fd)
754 memmove(&key_fds[i], &key_fds[i + 1],
755 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
756 ictx->num_key_fd--;
759 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
760 mp_key_func_t read_func, mp_close_func_t close_func,
761 void *ctx)
763 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
764 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, "
765 "cannot register file descriptor %d.\n", fd);
766 return 0;
768 if (select && fd < 0) {
769 mp_msg(MSGT_INPUT, MSGL_ERR,
770 "Invalid fd %d in mp_input_add_key_fd", fd);
771 return 0;
774 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
775 .fd = fd,
776 .read_func.key = read_func,
777 .close_func = close_func,
778 .no_select = !select,
779 .ctx = ctx,
781 ictx->num_key_fd++;
783 return 1;
786 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
788 int cmd_num = 0;
790 while (*str == '\n' || *str == '\r' || *str == ' ')
791 ++str;
792 while (*str) {
793 mp_cmd_t *cmd;
794 size_t len = strcspn(str, "\r\n");
795 char *cmdbuf = talloc_size(NULL, len + 1);
796 av_strlcpy(cmdbuf, str, len + 1);
797 cmd = mp_input_parse_cmd(cmdbuf);
798 if (cmd) {
799 mp_input_queue_cmd(ictx, cmd);
800 ++cmd_num;
802 str += len;
803 while (*str == '\n' || *str == '\r' || *str == ' ')
804 ++str;
805 talloc_free(cmdbuf);
807 return cmd_num;
810 mp_cmd_t *mp_input_parse_cmd(char *str)
812 int i, l;
813 int pausing = 0;
814 char *ptr, *e;
815 const mp_cmd_t *cmd_def;
817 #ifdef MP_DEBUG
818 assert(str != NULL);
819 #endif
821 // Ignore heading spaces.
822 while (str[0] == ' ' || str[0] == '\t')
823 ++str;
825 if (strncmp(str, "pausing ", 8) == 0) {
826 pausing = 1;
827 str = &str[8];
828 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
829 pausing = 2;
830 str = &str[13];
831 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
832 pausing = 3;
833 str = &str[15];
834 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
835 pausing = 4;
836 str = &str[19];
839 ptr = str + strcspn(str, "\t ");
840 if (*ptr != 0)
841 l = ptr - str;
842 else
843 l = strlen(str);
845 if (l == 0)
846 return NULL;
848 for (i = 0; mp_cmds[i].name != NULL; i++) {
849 if (strncasecmp(mp_cmds[i].name, str, l) == 0)
850 break;
853 if (mp_cmds[i].name == NULL)
854 return NULL;
856 cmd_def = &mp_cmds[i];
858 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
859 *cmd = (mp_cmd_t){
860 .id = cmd_def->id,
861 .name = talloc_strdup(cmd, cmd_def->name),
862 .pausing = pausing,
865 ptr = str;
867 for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) {
868 while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0')
869 ptr++;
870 if (ptr[0] == '\0')
871 break;
872 while (ptr[0] == ' ' || ptr[0] == '\t')
873 ptr++;
874 if (ptr[0] == '\0' || ptr[0] == '#')
875 break;
876 cmd->args[i].type = cmd_def->args[i].type;
877 switch (cmd_def->args[i].type) {
878 case MP_CMD_ARG_INT:
879 errno = 0;
880 cmd->args[i].v.i = atoi(ptr);
881 if (errno != 0) {
882 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
883 "isn't an integer.\n", cmd_def->name, i + 1);
884 ptr = NULL;
886 break;
887 case MP_CMD_ARG_FLOAT:
888 errno = 0;
889 cmd->args[i].v.f = atof(ptr);
890 if (errno != 0) {
891 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d "
892 "isn't a float.\n", cmd_def->name, i + 1);
893 ptr = NULL;
895 break;
896 case MP_CMD_ARG_STRING: {
897 char term;
898 char *ptr2 = ptr, *start;
900 if (ptr[0] == '\'' || ptr[0] == '"') {
901 term = ptr[0];
902 ptr2++;
903 } else
904 term = ' ';
905 start = ptr2;
906 while (1) {
907 e = strchr(ptr2, term);
908 if (!e)
909 break;
910 if (e <= ptr2 || *(e - 1) != '\\')
911 break;
912 ptr2 = e + 1;
915 if (term != ' ' && (!e || e[0] == '\0')) {
916 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is "
917 "unterminated.\n", cmd_def->name, i + 1);
918 ptr = NULL;
919 break;
920 } else if (!e)
921 e = ptr + strlen(ptr);
922 l = e - start;
923 ptr2 = start;
924 for (e = strchr(ptr2, '\\'); e && e < start + l; e = strchr(ptr2, '\\')) {
925 memmove(e, e + 1, strlen(e));
926 ptr2 = e + 1;
927 l--;
929 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
930 if (term != ' ')
931 ptr += l + 2;
932 break;
934 case -1:
935 ptr = NULL;
936 break;
937 default:
938 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i);
941 cmd->nargs = i;
943 if (cmd_def->nargs > cmd->nargs) {
944 /* mp_msg(MSGT_INPUT, MSGL_ERR, "Got command '%s' but\n", str); */
945 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d "
946 "arguments, we found only %d so far.\n", cmd_def->name,
947 cmd_def->nargs, cmd->nargs);
948 mp_cmd_free(cmd);
949 return NULL;
952 for (; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1; i++) {
953 memcpy(&cmd->args[i], &cmd_def->args[i], sizeof(mp_cmd_arg_t));
954 if (cmd_def->args[i].type == MP_CMD_ARG_STRING
955 && cmd_def->args[i].v.s != NULL)
956 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
959 if (i < MP_CMD_MAX_ARGS)
960 cmd->args[i].type = -1;
962 return cmd;
965 #define MP_CMD_MAX_SIZE 4096
967 static int read_cmd(mp_input_fd_t *mp_fd, char **ret)
969 char *end;
970 *ret = NULL;
972 // Allocate the buffer if it doesn't exist
973 if (!mp_fd->buffer) {
974 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
975 mp_fd->pos = 0;
976 mp_fd->size = MP_CMD_MAX_SIZE;
979 // Get some data if needed/possible
980 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) {
981 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer + mp_fd->pos,
982 mp_fd->size - 1 - mp_fd->pos);
983 // Error ?
984 if (r < 0) {
985 switch (r) {
986 case MP_INPUT_ERROR:
987 case MP_INPUT_DEAD:
988 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
989 "command file descriptor %d: %s\n",
990 mp_fd->fd, strerror(errno));
991 case MP_INPUT_NOTHING:
992 return r;
993 case MP_INPUT_RETRY:
994 continue;
996 // EOF ?
997 } else if (r == 0) {
998 mp_fd->eof = 1;
999 break;
1001 mp_fd->pos += r;
1002 break;
1005 mp_fd->got_cmd = 0;
1007 while (1) {
1008 int l = 0;
1009 // Find the cmd end
1010 mp_fd->buffer[mp_fd->pos] = '\0';
1011 end = strchr(mp_fd->buffer, '\r');
1012 if (end)
1013 *end = '\n';
1014 end = strchr(mp_fd->buffer, '\n');
1015 // No cmd end ?
1016 if (!end) {
1017 // If buffer is full we must drop all until the next \n
1018 if (mp_fd->size - mp_fd->pos <= 1) {
1019 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file "
1020 "descriptor %d is full: dropping content.\n",
1021 mp_fd->fd);
1022 mp_fd->pos = 0;
1023 mp_fd->drop = 1;
1025 break;
1027 // We already have a cmd : set the got_cmd flag
1028 else if ((*ret)) {
1029 mp_fd->got_cmd = 1;
1030 break;
1033 l = end - mp_fd->buffer;
1035 // Not dropping : put the cmd in ret
1036 if (!mp_fd->drop)
1037 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
1038 else
1039 mp_fd->drop = 0;
1040 mp_fd->pos -= l + 1;
1041 memmove(mp_fd->buffer, end + 1, mp_fd->pos);
1044 if (*ret)
1045 return 1;
1046 else
1047 return MP_INPUT_NOTHING;
1050 static int default_cmd_func(int fd, char *buf, int l)
1052 while (1) {
1053 int r = read(fd, buf, l);
1054 // Error ?
1055 if (r < 0) {
1056 if (errno == EINTR)
1057 continue;
1058 else if (errno == EAGAIN)
1059 return MP_INPUT_NOTHING;
1060 return MP_INPUT_ERROR;
1061 // EOF ?
1063 return r;
1068 void mp_input_add_cmd_filter(mp_input_cmd_filter func, void *ctx)
1070 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1072 filter->filter = func;
1073 filter->ctx = ctx;
1074 filter->next = cmd_filters;
1075 cmd_filters = filter;
1079 static char *find_bind_for_key(const mp_cmd_bind_t *binds, int n, int *keys)
1081 int j;
1083 if (n <= 0)
1084 return NULL;
1085 for (j = 0; binds[j].cmd != NULL; j++) {
1086 int found = 1, s;
1087 for (s = 0; s < n && binds[j].input[s] != 0; s++) {
1088 if (binds[j].input[s] != keys[s]) {
1089 found = 0;
1090 break;
1093 if (found && binds[j].input[s] == 0 && s == n)
1094 break;
1096 return binds[j].cmd;
1099 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1100 char *section)
1102 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1104 if (section == NULL)
1105 section = "default";
1106 while (bind_section) {
1107 if (strcmp(section, bind_section->section) == 0)
1108 return bind_section;
1109 if (bind_section->next == NULL)
1110 break;
1111 bind_section = bind_section->next;
1113 if (bind_section) {
1114 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1115 bind_section = bind_section->next;
1116 } else {
1117 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1118 bind_section = ictx->cmd_bind_sections;
1120 bind_section->cmd_binds = NULL;
1121 bind_section->section = talloc_strdup(bind_section, section);
1122 bind_section->next = NULL;
1123 return bind_section;
1126 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1128 char *cmd = NULL;
1129 mp_cmd_t *ret;
1131 if (ictx->cmd_binds)
1132 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1133 if (ictx->cmd_binds_default && cmd == NULL)
1134 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1135 if (ictx->default_bindings && cmd == NULL)
1136 cmd = find_bind_for_key(def_cmd_binds, n, keys);
1138 if (cmd == NULL) {
1139 char *key_buf = get_key_combo_name(keys, n);
1140 mp_tmsg(MSGT_INPUT, MSGL_WARN,
1141 "No bind found for key '%s'.\n", key_buf);
1142 talloc_free(key_buf);
1143 return NULL;
1145 if (strcmp(cmd, "ignore") == 0)
1146 return NULL;
1147 ret = mp_input_parse_cmd(cmd);
1148 if (!ret) {
1149 char *key_buf = get_key_combo_name(keys, n);
1150 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1151 "Invalid command for bound key '%s': '%s'\n", key_buf, cmd);
1152 talloc_free(key_buf);
1154 return ret;
1158 static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code)
1160 unsigned int j;
1161 mp_cmd_t *ret;
1163 /* On normal keyboards shift changes the character code of non-special
1164 * keys, so don't count the modifier separately for those. In other words
1165 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1166 * shift modifier is still kept for special keys like arrow keys.
1168 int unmod = code & ~KEY_MODIFIER_MASK;
1169 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1170 code &= ~KEY_MODIFIER_SHIFT;
1172 if (mp_input_key_cb) {
1173 if (code & MP_KEY_DOWN)
1174 return NULL;
1175 code &= ~(MP_KEY_DOWN | MP_NO_REPEAT_KEY);
1176 if (mp_input_key_cb(code))
1177 return NULL;
1180 if (code & MP_KEY_DOWN) {
1181 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1182 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1183 "at the same time\n");
1184 return NULL;
1186 code &= ~MP_KEY_DOWN;
1187 // Check if we don't already have this key as pushed
1188 for (j = 0; j < ictx->num_key_down; j++) {
1189 if (ictx->key_down[j] == code)
1190 break;
1192 if (j != ictx->num_key_down)
1193 return NULL;
1194 ictx->key_down[ictx->num_key_down] = code;
1195 ictx->num_key_down++;
1196 ictx->last_key_down = GetTimer();
1197 ictx->ar_state = 0;
1198 return NULL;
1200 // button released or press of key with no separate down/up events
1201 for (j = 0; j < ictx->num_key_down; j++) {
1202 if (ictx->key_down[j] == code)
1203 break;
1205 bool doubleclick = code >= MOUSE_BTN0_DBL && code < MOUSE_BTN_DBL_END;
1206 if (doubleclick) {
1207 int btn = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1208 if (!ictx->num_key_down
1209 || ictx->key_down[ictx->num_key_down - 1] != btn)
1210 return NULL;
1211 j = ictx->num_key_down - 1;
1212 ictx->key_down[j] = code;
1214 if (j == ictx->num_key_down) { // was not already down; add temporarily
1215 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1216 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events "
1217 "at the same time\n");
1218 return NULL;
1220 ictx->key_down[ictx->num_key_down] = code;
1221 ictx->num_key_down++;
1222 ictx->last_key_down = 1;
1224 // Interpret only maximal point of multibutton event
1225 ret = ictx->last_key_down ?
1226 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1227 : NULL;
1228 if (doubleclick) {
1229 ictx->key_down[j] = code - MOUSE_BTN0_DBL + MOUSE_BTN0;
1230 return ret;
1232 // Remove the key
1233 if (j + 1 < ictx->num_key_down)
1234 memmove(&ictx->key_down[j], &ictx->key_down[j + 1],
1235 (ictx->num_key_down - (j + 1)) * sizeof(int));
1236 ictx->num_key_down--;
1237 ictx->last_key_down = 0;
1238 ictx->ar_state = -1;
1239 mp_cmd_free(ictx->ar_cmd);
1240 ictx->ar_cmd = NULL;
1241 return ret;
1244 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1246 // No input : autorepeat ?
1247 if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0
1248 && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) {
1249 unsigned int t = GetTimer();
1250 // First time : wait delay
1251 if (ictx->ar_state == 0
1252 && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) {
1253 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1254 ictx->key_down);
1255 if (!ictx->ar_cmd) {
1256 ictx->ar_state = -1;
1257 return NULL;
1259 ictx->ar_state = 1;
1260 ictx->last_ar = t;
1261 return mp_cmd_clone(ictx->ar_cmd);
1262 // Then send rate / sec event
1263 } else if (ictx->ar_state == 1
1264 && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1265 ictx->last_ar = t;
1266 return mp_cmd_clone(ictx->ar_cmd);
1269 return NULL;
1274 * \param time time to wait at most for an event in milliseconds
1276 static mp_cmd_t *read_events(struct input_ctx *ictx, int time)
1278 int i;
1279 int got_cmd = 0;
1280 struct mp_input_fd *key_fds = ictx->key_fds;
1281 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1282 for (i = 0; i < ictx->num_key_fd; i++)
1283 if (key_fds[i].dead) {
1284 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1285 i--;
1287 for (i = 0; i < ictx->num_cmd_fd; i++)
1288 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1289 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1290 i--;
1291 } else if (cmd_fds[i].got_cmd)
1292 got_cmd = 1;
1293 #ifdef HAVE_POSIX_SELECT
1294 fd_set fds;
1295 FD_ZERO(&fds);
1296 if (!got_cmd) {
1297 int max_fd = 0;
1298 for (i = 0; i < ictx->num_key_fd; i++) {
1299 if (key_fds[i].no_select)
1300 continue;
1301 if (key_fds[i].fd > max_fd)
1302 max_fd = key_fds[i].fd;
1303 FD_SET(key_fds[i].fd, &fds);
1305 for (i = 0; i < ictx->num_cmd_fd; i++) {
1306 if (cmd_fds[i].no_select)
1307 continue;
1308 if (cmd_fds[i].fd > max_fd)
1309 max_fd = cmd_fds[i].fd;
1310 FD_SET(cmd_fds[i].fd, &fds);
1312 struct timeval tv, *time_val;
1313 if (time >= 0) {
1314 tv.tv_sec = time / 1000;
1315 tv.tv_usec = (time % 1000) * 1000;
1316 time_val = &tv;
1317 } else
1318 time_val = NULL;
1319 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1320 if (errno != EINTR)
1321 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1322 strerror(errno));
1323 FD_ZERO(&fds);
1326 #else
1327 if (!got_cmd && time)
1328 usec_sleep(time * 1000);
1329 #endif
1332 for (i = 0; i < ictx->num_key_fd; i++) {
1333 #ifdef HAVE_POSIX_SELECT
1334 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1335 continue;
1336 #endif
1338 int code;
1339 while (1) {
1340 code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1341 if (code < 0) {
1342 if (code == MP_INPUT_RELEASE_ALL) {
1343 memset(ictx->key_down, 0, sizeof(ictx->key_down));
1344 ictx->num_key_down = 0;
1345 ictx->last_key_down = 0;
1346 continue;
1348 break;
1350 mp_cmd_t *ret = interpret_key(ictx, code);
1351 if (ret)
1352 return ret;
1354 if (code == MP_INPUT_ERROR)
1355 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input "
1356 "file descriptor %d\n", key_fds[i].fd);
1357 else if (code == MP_INPUT_DEAD) {
1358 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on "
1359 "file descriptor %d\n", key_fds[i].fd);
1360 key_fds[i].dead = 1;
1363 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx);
1364 if (autorepeat_cmd)
1365 return autorepeat_cmd;
1367 for (i = 0; i < ictx->num_cmd_fd; i++) {
1368 #ifdef HAVE_POSIX_SELECT
1369 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1370 !cmd_fds[i].got_cmd)
1371 continue;
1372 #endif
1373 char *cmd;
1374 int r;
1375 while ((r = read_cmd(&cmd_fds[i], &cmd)) >= 0) {
1376 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1377 talloc_free(cmd);
1378 if (ret)
1379 return ret;
1381 if (r == MP_INPUT_ERROR)
1382 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command "
1383 "file descriptor %d\n", cmd_fds[i].fd);
1384 else if (r == MP_INPUT_DEAD)
1385 cmd_fds[i].dead = 1;
1388 return NULL;
1392 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd)
1394 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1395 return 0;
1396 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1397 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1398 ictx->cmd_queue_length++;
1399 return 1;
1402 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1404 mp_cmd_t *ret;
1406 if (ictx->cmd_queue_length == 0)
1407 return NULL;
1409 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1411 if (!peek_only) {
1412 ictx->cmd_queue_length--;
1413 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1416 return ret;
1420 * \param peek_only when set, the returned command stays in the queue.
1421 * Do not free the returned cmd whe you set this!
1423 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1425 mp_cmd_t *ret = NULL;
1426 mp_cmd_filter_t *cf;
1427 int from_queue;
1429 if (async_quit_request)
1430 return mp_input_parse_cmd("quit 1");
1431 while (1) {
1432 from_queue = 1;
1433 ret = get_queued_cmd(ictx, peek_only);
1434 if (ret)
1435 break;
1436 from_queue = 0;
1437 ret = read_events(ictx, time);
1438 if (!ret) {
1439 from_queue = 1;
1440 ret = get_queued_cmd(ictx, peek_only);
1442 break;
1444 if (!ret)
1445 return NULL;
1447 for (cf = cmd_filters; cf; cf = cf->next) {
1448 if (cf->filter(ret, cf->ctx)) {
1449 if (peek_only && from_queue)
1450 // The filter ate the cmd, so we remove it from queue
1451 ret = get_queued_cmd(ictx, 0);
1452 mp_cmd_free(ret);
1453 return NULL;
1457 if (!from_queue && peek_only)
1458 mp_input_queue_cmd(ictx, ret);
1460 return ret;
1463 void
1464 mp_cmd_free(mp_cmd_t *cmd) {
1465 talloc_free(cmd);
1468 mp_cmd_t *
1469 mp_cmd_clone(mp_cmd_t *cmd) {
1470 mp_cmd_t *ret;
1471 int i;
1472 #ifdef MP_DEBUG
1473 assert(cmd != NULL);
1474 #endif
1476 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1477 ret->name = talloc_strdup(ret, cmd->name);
1478 for (i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1479 if (cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1480 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1483 return ret;
1486 int mp_input_get_key_from_name(const char *name)
1488 int modifiers = 0;
1489 const char *p;
1490 while (p = strchr(name, '+')) {
1491 for (struct mp_key_name *m = modifier_names; m->name; m++)
1492 if (!bstrcasecmp(BSTR(m->name), (struct bstr){(char *)name, p - name})) {
1493 modifiers |= m->key;
1494 goto found;
1496 if (!strcmp(name, "+"))
1497 return '+' + modifiers;
1498 return -1;
1499 found:
1500 name = p + 1;
1502 int len = strlen(name);
1503 if (len == 1) // Direct key code
1504 return (unsigned char)name[0] + modifiers;
1505 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1506 return strtol(name, NULL, 16) + modifiers;
1508 for (int i = 0; key_names[i].name != NULL; i++) {
1509 if (strcasecmp(key_names[i].name, name) == 0)
1510 return key_names[i].key + modifiers;
1513 return -1;
1516 static int get_input_from_name(char *name, int *keys)
1518 char *end, *ptr;
1519 int n = 0;
1521 ptr = name;
1522 n = 0;
1523 for (end = strchr(ptr, '-'); ptr != NULL; end = strchr(ptr, '-')) {
1524 if (end && end[1] != '\0') {
1525 if (end[1] == '-')
1526 end = &end[1];
1527 end[0] = '\0';
1529 keys[n] = mp_input_get_key_from_name(ptr);
1530 if (keys[n] < 0)
1531 return 0;
1532 n++;
1533 if (end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1534 ptr = &end[1];
1535 else
1536 break;
1538 keys[n] = 0;
1539 return 1;
1542 #define BS_MAX 256
1543 #define SPACE_CHAR " \n\r\t"
1545 static void bind_keys(struct input_ctx *ictx,
1546 const int keys[MP_MAX_KEY_DOWN + 1], char *cmd)
1548 int i = 0, j;
1549 mp_cmd_bind_t *bind = NULL;
1550 mp_cmd_bind_section_t *bind_section = NULL;
1551 char *section = NULL, *p;
1553 #ifdef MP_DEBUG
1554 assert(keys != NULL);
1555 assert(cmd != NULL);
1556 #endif
1558 if (*cmd == '{' && (p = strchr(cmd, '}'))) {
1559 *p = 0;
1560 section = ++cmd;
1561 cmd = ++p;
1562 // Jump beginning space
1563 cmd += strspn(cmd, SPACE_CHAR);
1565 bind_section = get_bind_section(ictx, section);
1567 if (bind_section->cmd_binds) {
1568 for (i = 0; bind_section->cmd_binds[i].cmd != NULL; i++) {
1569 for (j = 0; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0; j++)
1570 /* NOTHING */;
1571 if (keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1572 bind = &bind_section->cmd_binds[i];
1573 break;
1578 if (!bind) {
1579 bind_section->cmd_binds = talloc_realloc(bind_section,
1580 bind_section->cmd_binds,
1581 mp_cmd_bind_t, i + 2);
1582 memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(mp_cmd_bind_t));
1583 bind = &bind_section->cmd_binds[i];
1585 talloc_free(bind->cmd);
1586 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1587 memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int));
1590 static int parse_config(struct input_ctx *ictx, char *file)
1592 int fd;
1593 int bs = 0, r, eof = 0, comments = 0;
1594 char *iter, *end;
1595 char buffer[BS_MAX];
1596 int n_binds = 0, keys[MP_MAX_KEY_DOWN + 1] = { 0 };
1598 fd = open(file, O_RDONLY);
1600 if (fd < 0) {
1601 mp_msg(MSGT_INPUT, MSGL_V, "Can't open input config file %s: %s\n",
1602 file, strerror(errno));
1603 return 0;
1606 mp_msg(MSGT_INPUT, MSGL_V, "Parsing input config file %s\n", file);
1608 while (1) {
1609 if (!eof && bs < BS_MAX - 1) {
1610 if (bs > 0)
1611 bs--;
1612 r = read(fd, buffer + bs, BS_MAX - 1 - bs);
1613 if (r < 0) {
1614 if (errno == EINTR)
1615 continue;
1616 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading "
1617 "input config file %s: %s\n", file, strerror(errno));
1618 close(fd);
1619 return 0;
1620 } else if (r == 0) {
1621 eof = 1;
1622 } else {
1623 bs += r + 1;
1624 buffer[bs - 1] = '\0';
1627 // Empty buffer : return
1628 if (bs <= 1) {
1629 mp_msg(MSGT_INPUT, MSGL_V, "Input config file %s parsed: "
1630 "%d binds\n", file, n_binds);
1631 close(fd);
1632 return 1;
1635 iter = buffer;
1637 if (comments) {
1638 iter += strcspn(iter, "\n");
1639 if (*iter == 0) { // Buffer was full of comment
1640 bs = 0;
1641 continue;
1643 iter++;
1644 r = strlen(iter);
1645 memmove(buffer, iter, r + 1);
1646 bs = r + 1;
1647 comments = 0;
1648 continue;
1651 // Find the wanted key
1652 if (keys[0] == 0) {
1653 // Jump beginning space
1654 iter += strspn(iter, SPACE_CHAR);
1655 if (*iter == 0) { // Buffer was full of space char
1656 bs = 0;
1657 continue;
1659 if (iter[0] == '#') { // Comments
1660 comments = 1;
1661 continue;
1663 // Find the end of the key code name
1664 end = iter + strcspn(iter, SPACE_CHAR);
1665 if (*end == 0) { // Key name doesn't fit in the buffer
1666 if (buffer == iter) {
1667 if (eof && (buffer - iter) == bs)
1668 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1669 "Unfinished binding %s\n", iter);
1670 else
1671 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1672 "for this key name: %s\n", iter);
1673 return 0;
1675 memmove(buffer, iter, end - iter);
1676 bs = end - iter;
1677 continue;
1680 char name[end - iter + 1];
1681 strncpy(name, iter, end - iter);
1682 name[end - iter] = '\0';
1683 if (!get_input_from_name(name, keys)) {
1684 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown key '%s'\n", name);
1685 close(fd);
1686 return 0;
1689 if (bs > (end - buffer))
1690 memmove(buffer, end, bs - (end - buffer));
1691 bs -= end - buffer;
1692 continue;
1693 } else { // Get the command
1694 while (iter[0] == ' ' || iter[0] == '\t')
1695 iter++;
1696 // Found new line
1697 if (iter[0] == '\n' || iter[0] == '\r') {
1698 char *key_buf = get_key_combo_name(keys, MP_MAX_KEY_DOWN);
1699 mp_tmsg(MSGT_INPUT, MSGL_ERR,
1700 "No command found for key '%s'.\n", key_buf);
1701 talloc_free(key_buf);
1702 keys[0] = 0;
1703 if (iter > buffer) {
1704 memmove(buffer, iter, bs - (iter - buffer));
1705 bs -= (iter - buffer);
1707 continue;
1709 end = iter + strcspn(iter, "\n\r");
1710 if (*end == 0 && !(eof && ((end + 1) - buffer) == bs)) {
1711 if (iter == buffer) {
1712 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small "
1713 "for command %s\n", buffer);
1714 close(fd);
1715 return 0;
1717 memmove(buffer, iter, end - iter);
1718 bs = end - iter;
1719 continue;
1722 char cmd[end - iter + 1];
1723 strncpy(cmd, iter, end - iter);
1724 cmd[end - iter] = '\0';
1725 //printf("Set bind %d => %s\n",keys[0],cmd);
1726 bind_keys(ictx, keys, cmd);
1727 n_binds++;
1729 keys[0] = 0;
1730 end++;
1731 if (bs > (end - buffer))
1732 memmove(buffer, end, bs - (end - buffer));
1733 bs -= (end - buffer);
1734 buffer[bs - 1] = '\0';
1735 continue;
1738 mp_tmsg(MSGT_INPUT, MSGL_ERR, "What are we doing here?\n");
1739 close(fd);
1740 mp_input_set_section(ictx, NULL);
1741 return 0;
1744 void mp_input_set_section(struct input_ctx *ictx, char *name)
1746 mp_cmd_bind_section_t *bind_section = NULL;
1748 ictx->cmd_binds = NULL;
1749 ictx->cmd_binds_default = NULL;
1750 talloc_free(ictx->section);
1751 if (name)
1752 ictx->section = talloc_strdup(ictx, name);
1753 else
1754 ictx->section = talloc_strdup(ictx, "default");
1755 if ((bind_section = get_bind_section(ictx, ictx->section)))
1756 ictx->cmd_binds = bind_section->cmd_binds;
1757 if (strcmp(ictx->section, "default") == 0)
1758 return;
1759 if ((bind_section = get_bind_section(ictx, NULL)))
1760 ictx->cmd_binds_default = bind_section->cmd_binds;
1763 char *mp_input_get_section(struct input_ctx *ictx)
1765 return ictx->section;
1768 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1770 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1771 *ictx = (struct input_ctx){
1772 .ar_state = -1,
1773 .ar_delay = input_conf->ar_delay,
1774 .ar_rate = input_conf->ar_rate,
1775 .default_bindings = input_conf->default_bindings,
1778 char *file;
1779 char *config_file = input_conf->config_file;
1780 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1781 if (!file)
1782 return ictx;
1784 if (!parse_config(ictx, file)) {
1785 // free file if it was allocated by get_path(),
1786 // before it gets overwritten
1787 if (file != config_file)
1788 free(file);
1789 // Try global conf dir
1790 file = MPLAYER_CONFDIR "/input.conf";
1791 if (!parse_config(ictx, file))
1792 mp_msg(MSGT_INPUT, MSGL_V, "Falling back on default (hardcoded) "
1793 "input config\n");
1794 } else {
1795 // free file if it was allocated by get_path()
1796 if (file != config_file)
1797 free(file);
1800 #ifdef CONFIG_JOYSTICK
1801 if (input_conf->use_joystick) {
1802 int fd = mp_input_joystick_init(input_conf->js_dev);
1803 if (fd < 0)
1804 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n");
1805 else
1806 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1807 (mp_close_func_t)close, NULL);
1809 #endif
1811 #ifdef CONFIG_LIRC
1812 if (input_conf->use_lirc) {
1813 int fd = mp_input_lirc_init();
1814 if (fd > 0)
1815 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1816 mp_input_lirc_close);
1818 #endif
1820 #ifdef CONFIG_LIRCC
1821 if (input_conf->use_lircc) {
1822 int fd = lircc_init("mplayer", NULL);
1823 if (fd >= 0)
1824 mp_input_add_cmd_fd(ictx, fd, 1, NULL,
1825 (mp_close_func_t)lircc_cleanup);
1827 #endif
1829 #ifdef CONFIG_APPLE_REMOTE
1830 if (input_conf->use_ar) {
1831 if (mp_input_ar_init() < 0)
1832 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1833 else
1834 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read,
1835 mp_input_ar_close, NULL);
1837 #endif
1839 #ifdef CONFIG_APPLE_IR
1840 if (input_conf->use_ar) {
1841 int fd = mp_input_appleir_init(input_conf->ar_dev);
1842 if (fd < 0)
1843 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n");
1844 else
1845 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1846 (mp_close_func_t)close, NULL);
1848 #endif
1850 if (input_conf->in_file) {
1851 struct stat st;
1852 int mode = O_RDONLY | O_NONBLOCK;
1853 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1854 // Note that on Windows stat may fail for named pipes,
1855 // but due to how the API works, using RDONLY should be ok.
1856 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1857 mode = O_RDWR | O_NONBLOCK;
1858 int in_file_fd = open(input_conf->in_file, mode);
1859 if (in_file_fd >= 0)
1860 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1861 (mp_close_func_t)close);
1862 else
1863 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1864 input_conf->in_file, strerror(errno));
1866 return ictx;
1869 void mp_input_uninit(struct input_ctx *ictx)
1871 if (!ictx)
1872 return;
1874 unsigned int i;
1876 for (i = 0; i < ictx->num_key_fd; i++) {
1877 if (ictx->key_fds[i].close_func)
1878 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1881 for (i = 0; i < ictx->num_cmd_fd; i++) {
1882 if (ictx->cmd_fds[i].close_func)
1883 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1885 talloc_free(ictx);
1888 void mp_input_register_options(m_config_t *cfg)
1890 m_config_register_options(cfg, mp_input_opts);
1893 static int print_key_list(m_option_t *cfg)
1895 int i;
1896 printf("\n");
1897 for (i = 0; key_names[i].name != NULL; i++)
1898 printf("%s\n", key_names[i].name);
1899 exit(0);
1902 static int print_cmd_list(m_option_t *cfg)
1904 const mp_cmd_t *cmd;
1905 int i, j;
1906 const char *type;
1908 for (i = 0; (cmd = &mp_cmds[i])->name != NULL; i++) {
1909 printf("%-20.20s", cmd->name);
1910 for (j = 0; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1; j++) {
1911 switch (cmd->args[j].type) {
1912 case MP_CMD_ARG_INT:
1913 type = "Integer";
1914 break;
1915 case MP_CMD_ARG_FLOAT:
1916 type = "Float";
1917 break;
1918 case MP_CMD_ARG_STRING:
1919 type = "String";
1920 break;
1921 default:
1922 type = "??";
1924 if (j + 1 > cmd->nargs)
1925 printf(" [%s]", type);
1926 else
1927 printf(" %s", type);
1929 printf("\n");
1931 exit(0);
1935 * \param time time to wait for an interruption in milliseconds
1937 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1939 mp_cmd_t *cmd;
1940 if ((cmd = mp_input_get_cmd(ictx, time, 1)) == NULL)
1941 return 0;
1942 switch (cmd->id) {
1943 case MP_CMD_QUIT:
1944 case MP_CMD_PLAY_TREE_STEP:
1945 case MP_CMD_PLAY_TREE_UP_STEP:
1946 case MP_CMD_PLAY_ALT_SRC_STEP:
1947 // The cmd will be executed when we are back in the main loop
1948 return 1;
1950 // remove the cmd from the queue
1951 cmd = mp_input_get_cmd(ictx, time, 0);
1952 mp_cmd_free(cmd);
1953 return 0;