input: sleep in event loop even if there are no input fds
[mplayer.git] / input / input.c
blobaeefec25f1357a27dc5f88c9b3585e27d4cee767
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 #include "mouse.h"
35 #ifdef MP_DEBUG
36 #include <assert.h>
37 #endif
38 #include "mp_fifo.h"
39 #include "osdep/keycodes.h"
40 #include "osdep/timer.h"
41 #include "libavutil/avstring.h"
42 #include "mp_msg.h"
43 #include "m_config.h"
44 #include "m_option.h"
45 #include "path.h"
46 #include "talloc.h"
47 #include "options.h"
48 #include "bstr.h"
50 #include "joystick.h"
52 #ifdef CONFIG_LIRC
53 #include "lirc.h"
54 #endif
56 #ifdef CONFIG_LIRCC
57 #include <lirc/lircc.h>
58 #endif
60 #include "ar.h"
62 typedef struct mp_cmd_bind {
63 int input[MP_MAX_KEY_DOWN+1];
64 char* cmd;
65 } mp_cmd_bind_t;
67 typedef struct mp_key_name {
68 int key;
69 char* name;
70 } mp_key_name_t;
72 /// This array defines all known commands.
73 /// The first field is an id used to recognize the command without too many strcmp.
74 /// The second is obviously the command name.
75 /// The third is the minimum number of arguments this command needs.
76 /// Then comes the definition of each argument, terminated with an arg of type -1.
77 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
78 /// the last one) which is actually 9.
80 /// For the args, the first field is the type (actually int, float or string), the second
81 /// is the default value wich is used for optional arguments
83 static const mp_cmd_t mp_cmds[] = {
84 #ifdef CONFIG_RADIO
85 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
87 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
89 #endif
90 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
91 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
92 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
96 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
97 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
98 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
99 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
102 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
103 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
106 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
107 { 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}} } },
108 { 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}} } },
109 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
110 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
112 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
113 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
114 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
119 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
120 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
121 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
123 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
124 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
125 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
126 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
131 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
132 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
133 #ifdef CONFIG_ASS
134 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
135 #endif
136 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
137 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
138 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
139 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
141 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
142 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
144 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
145 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
152 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
153 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
155 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
156 #ifdef CONFIG_TV
157 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
158 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
159 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
160 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
161 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
162 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
163 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
165 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
166 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
170 #endif
171 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
172 #ifdef CONFIG_DVBIN
173 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
174 #endif
175 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
176 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
177 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
178 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
179 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
181 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
186 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
187 { MP_CMD_CAPTURING, "capturing", 0, { {-1,{0}} } },
188 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
189 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
190 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
192 #ifdef CONFIG_DVDNAV
193 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
194 #endif
196 #ifdef CONFIG_MENU
197 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
198 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
199 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
200 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
201 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
202 #endif
204 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
205 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
206 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
207 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
209 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
210 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
211 { 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}} } },
213 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
214 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
216 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
217 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
218 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
219 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
220 { MP_CMD_AF_CMDLINE, "af_cmdline", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
222 { 0, NULL, 0, {} }
225 /// The names of the keys as used in input.conf
226 /// If you add some new keys, you also need to add them here
228 static const mp_key_name_t key_names[] = {
229 { ' ', "SPACE" },
230 { '#', "SHARP" },
231 { KEY_ENTER, "ENTER" },
232 { KEY_TAB, "TAB" },
233 { KEY_BACKSPACE, "BS" },
234 { KEY_DELETE, "DEL" },
235 { KEY_INSERT, "INS" },
236 { KEY_HOME, "HOME" },
237 { KEY_END, "END" },
238 { KEY_PAGE_UP, "PGUP" },
239 { KEY_PAGE_DOWN, "PGDWN" },
240 { KEY_ESC, "ESC" },
241 { KEY_RIGHT, "RIGHT" },
242 { KEY_LEFT, "LEFT" },
243 { KEY_DOWN, "DOWN" },
244 { KEY_UP, "UP" },
245 { KEY_F+1, "F1" },
246 { KEY_F+2, "F2" },
247 { KEY_F+3, "F3" },
248 { KEY_F+4, "F4" },
249 { KEY_F+5, "F5" },
250 { KEY_F+6, "F6" },
251 { KEY_F+7, "F7" },
252 { KEY_F+8, "F8" },
253 { KEY_F+9, "F9" },
254 { KEY_F+10, "F10" },
255 { KEY_F+11, "F11" },
256 { KEY_F+12, "F12" },
257 { KEY_KP0, "KP0" },
258 { KEY_KP1, "KP1" },
259 { KEY_KP2, "KP2" },
260 { KEY_KP3, "KP3" },
261 { KEY_KP4, "KP4" },
262 { KEY_KP5, "KP5" },
263 { KEY_KP6, "KP6" },
264 { KEY_KP7, "KP7" },
265 { KEY_KP8, "KP8" },
266 { KEY_KP9, "KP9" },
267 { KEY_KPDEL, "KP_DEL" },
268 { KEY_KPDEC, "KP_DEC" },
269 { KEY_KPINS, "KP_INS" },
270 { KEY_KPENTER, "KP_ENTER" },
271 { MOUSE_BTN0, "MOUSE_BTN0" },
272 { MOUSE_BTN1, "MOUSE_BTN1" },
273 { MOUSE_BTN2, "MOUSE_BTN2" },
274 { MOUSE_BTN3, "MOUSE_BTN3" },
275 { MOUSE_BTN4, "MOUSE_BTN4" },
276 { MOUSE_BTN5, "MOUSE_BTN5" },
277 { MOUSE_BTN6, "MOUSE_BTN6" },
278 { MOUSE_BTN7, "MOUSE_BTN7" },
279 { MOUSE_BTN8, "MOUSE_BTN8" },
280 { MOUSE_BTN9, "MOUSE_BTN9" },
281 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
282 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
283 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
284 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
285 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
286 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
287 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
288 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
289 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
290 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
291 { JOY_AXIS1_MINUS, "JOY_UP" },
292 { JOY_AXIS1_PLUS, "JOY_DOWN" },
293 { JOY_AXIS0_MINUS, "JOY_LEFT" },
294 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
296 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
297 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
298 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
299 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
300 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
301 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
302 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
303 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
304 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
305 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
306 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
307 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
308 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
309 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
310 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
311 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
312 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
313 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
314 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
315 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
317 { JOY_BTN0, "JOY_BTN0" },
318 { JOY_BTN1, "JOY_BTN1" },
319 { JOY_BTN2, "JOY_BTN2" },
320 { JOY_BTN3, "JOY_BTN3" },
321 { JOY_BTN4, "JOY_BTN4" },
322 { JOY_BTN5, "JOY_BTN5" },
323 { JOY_BTN6, "JOY_BTN6" },
324 { JOY_BTN7, "JOY_BTN7" },
325 { JOY_BTN8, "JOY_BTN8" },
326 { JOY_BTN9, "JOY_BTN9" },
328 { AR_PLAY, "AR_PLAY" },
329 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
330 { AR_NEXT, "AR_NEXT" },
331 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
332 { AR_PREV, "AR_PREV" },
333 { AR_PREV_HOLD, "AR_PREV_HOLD" },
334 { AR_MENU, "AR_MENU" },
335 { AR_MENU_HOLD, "AR_MENU_HOLD" },
336 { AR_VUP, "AR_VUP" },
337 { AR_VDOWN, "AR_VDOWN" },
339 { KEY_POWER, "POWER" },
340 { KEY_MENU, "MENU" },
341 { KEY_PLAY, "PLAY" },
342 { KEY_PAUSE, "PAUSE" },
343 { KEY_PLAYPAUSE, "PLAYPAUSE" },
344 { KEY_STOP, "STOP" },
345 { KEY_FORWARD, "FORWARD" },
346 { KEY_REWIND, "REWIND" },
347 { KEY_NEXT, "NEXT" },
348 { KEY_PREV, "PREV" },
349 { KEY_VOLUME_UP, "VOLUME_UP" },
350 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
351 { KEY_MUTE, "MUTE" },
353 // These are kept for backward compatibility
354 { KEY_PAUSE, "XF86_PAUSE" },
355 { KEY_STOP, "XF86_STOP" },
356 { KEY_PREV, "XF86_PREV" },
357 { KEY_NEXT, "XF86_NEXT" },
359 { KEY_CLOSE_WIN, "CLOSE_WIN" },
361 { 0, NULL }
364 struct mp_key_name modifier_names[] = {
365 { KEY_MODIFIER_SHIFT, "Shift" },
366 { KEY_MODIFIER_CTRL, "Ctrl" },
367 { KEY_MODIFIER_ALT, "Alt" },
368 { KEY_MODIFIER_META, "Meta" },
369 { 0 }
372 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
374 // This is the default binding. The content of input.conf overrides these.
375 // The first arg is a null terminated array of key codes.
376 // The second is the command
378 static const mp_cmd_bind_t def_cmd_binds[] = {
380 { { MOUSE_BTN3, 0 }, "seek 10" },
381 { { MOUSE_BTN4, 0 }, "seek -10" },
382 { { MOUSE_BTN5, 0 }, "volume 1" },
383 { { MOUSE_BTN6, 0 }, "volume -1" },
385 #ifdef CONFIG_DVDNAV
386 { { KEY_KP8, 0 }, "dvdnav up" }, // up
387 { { KEY_KP2, 0 }, "dvdnav down" }, // down
388 { { KEY_KP4, 0 }, "dvdnav left" }, // left
389 { { KEY_KP6, 0 }, "dvdnav right" }, // right
390 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
391 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
392 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
393 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
394 #endif
396 { { KEY_RIGHT, 0 }, "seek 10" },
397 { { KEY_LEFT, 0 }, "seek -10" },
398 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
399 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
400 { { KEY_UP, 0 }, "seek 60" },
401 { { KEY_DOWN, 0 }, "seek -60" },
402 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
403 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
404 { { KEY_PAGE_UP, 0 }, "seek 600" },
405 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
406 { { '+', 0 }, "audio_delay 0.100" },
407 { { '-', 0 }, "audio_delay -0.100" },
408 { { '[', 0 }, "speed_mult 0.9091" },
409 { { ']', 0 }, "speed_mult 1.1" },
410 { { '{', 0 }, "speed_mult 0.5" },
411 { { '}', 0 }, "speed_mult 2.0" },
412 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
413 { { 'q', 0 }, "quit" },
414 { { KEY_ESC, 0 }, "quit" },
415 { { 'p', 0 }, "pause" },
416 { { ' ', 0 }, "pause" },
417 { { '.', 0 }, "frame_step" },
418 { { KEY_HOME, 0 }, "pt_up_step 1" },
419 { { KEY_END, 0 }, "pt_up_step -1" },
420 { { '>', 0 }, "pt_step 1" },
421 { { KEY_ENTER, 0 }, "pt_step 1 1" },
422 { { '<', 0 }, "pt_step -1" },
423 { { KEY_INS, 0 }, "alt_src_step 1" },
424 { { KEY_DEL, 0 }, "alt_src_step -1" },
425 { { 'o', 0 }, "osd" },
426 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
427 { { 'P', 0 }, "osd_show_progression" },
428 { { 'z', 0 }, "sub_delay -0.1" },
429 { { 'x', 0 }, "sub_delay +0.1" },
430 { { 'g', 0 }, "sub_step -1" },
431 { { 'y', 0 }, "sub_step +1" },
432 { { '9', 0 }, "volume -1" },
433 { { '/', 0 }, "volume -1" },
434 { { '0', 0 }, "volume 1" },
435 { { '*', 0 }, "volume 1" },
436 { { '(', 0 }, "balance -0.1" },
437 { { ')', 0 }, "balance 0.1" },
438 { { 'm', 0 }, "mute" },
439 { { '1', 0 }, "contrast -1" },
440 { { '2', 0 }, "contrast 1" },
441 { { '3', 0 }, "brightness -1" },
442 { { '4', 0 }, "brightness 1" },
443 { { '5', 0 }, "hue -1" },
444 { { '6', 0 }, "hue 1" },
445 { { '7', 0 }, "saturation -1" },
446 { { '8', 0 }, "saturation 1" },
447 { { 'd', 0 }, "frame_drop" },
448 { { 'D', 0 }, "step_property_osd deinterlace" },
449 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
450 { { 'r', 0 }, "sub_pos -1" },
451 { { 't', 0 }, "sub_pos +1" },
452 { { 'a', 0 }, "sub_alignment" },
453 { { 'v', 0 }, "sub_visibility" },
454 { { 'j', 0 }, "sub_select" },
455 { { 'J', 0 }, "sub_select -3" },
456 { { 'F', 0 }, "forced_subs_only" },
457 { { '#', 0 }, "switch_audio" },
458 { { '_', 0 }, "step_property switch_video" },
459 { { KEY_TAB, 0 }, "step_property switch_program" },
460 { { 'i', 0 }, "edl_mark" },
461 #ifdef CONFIG_TV
462 { { 'h', 0 }, "tv_step_channel 1" },
463 { { 'k', 0 }, "tv_step_channel -1" },
464 { { 'n', 0 }, "tv_step_norm" },
465 { { 'u', 0 }, "tv_step_chanlist" },
466 #endif
467 { { 'X', 0 }, "step_property teletext_mode 1" },
468 { { 'W', 0 }, "step_property teletext_page 1" },
469 { { 'Q', 0 }, "step_property teletext_page -1" },
470 #ifdef CONFIG_JOYSTICK
471 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
472 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
473 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
474 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
475 { { JOY_BTN0, 0 }, "pause" },
476 { { JOY_BTN1, 0 }, "osd" },
477 { { JOY_BTN2, 0 }, "volume 1"},
478 { { JOY_BTN3, 0 }, "volume -1"},
479 #endif
480 #ifdef CONFIG_APPLE_REMOTE
481 { { AR_PLAY, 0}, "pause" },
482 { { AR_PLAY_HOLD, 0}, "quit" },
483 { { AR_NEXT, 0 }, "seek 30" },
484 { { AR_NEXT_HOLD, 0 }, "seek 120" },
485 { { AR_PREV, 0 }, "seek -10" },
486 { { AR_PREV_HOLD, 0 }, "seek -120" },
487 { { AR_MENU, 0 }, "osd" },
488 { { AR_MENU_HOLD, 0 }, "mute" },
489 { { AR_VUP, 0 }, "volume 1"},
490 { { AR_VDOWN, 0 }, "volume -1"},
491 #endif
492 { { 'T', 0 }, "vo_ontop" },
493 { { 'f', 0 }, "vo_fullscreen" },
494 { { 'C', 0 }, "step_property_osd capturing" },
495 { { 's', 0 }, "screenshot 0" },
496 { { 'S', 0 }, "screenshot 1" },
497 { { 'w', 0 }, "panscan -0.1" },
498 { { 'e', 0 }, "panscan +0.1" },
500 { { KEY_POWER, 0 }, "quit" },
501 { { KEY_MENU, 0 }, "osd" },
502 { { KEY_PLAY, 0 }, "pause" },
503 { { KEY_PAUSE, 0 }, "pause" },
504 { { KEY_PLAYPAUSE, 0 }, "pause" },
505 { { KEY_STOP, 0 }, "quit" },
506 { { KEY_FORWARD, 0 }, "seek 60" },
507 { { KEY_REWIND, 0 }, "seek -60" },
508 { { KEY_NEXT, 0 }, "pt_step 1" },
509 { { KEY_PREV, 0 }, "pt_step -1" },
510 { { KEY_VOLUME_UP, 0 }, "volume 1" },
511 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
512 { { KEY_MUTE, 0 }, "mute" },
514 { { KEY_CLOSE_WIN, 0 }, "quit" },
516 { { '!', 0 }, "seek_chapter -1" },
517 { { '@', 0 }, "seek_chapter 1" },
518 { { 'A', 0 }, "switch_angle 1" },
519 { { 'U', 0 }, "stop" },
521 { { 0 }, NULL }
525 #ifndef MP_MAX_KEY_FD
526 #define MP_MAX_KEY_FD 10
527 #endif
529 #ifndef MP_MAX_CMD_FD
530 #define MP_MAX_CMD_FD 10
531 #endif
533 #define CMD_QUEUE_SIZE 100
535 typedef struct mp_input_fd {
536 int fd;
537 union {
538 mp_key_func_t key;
539 mp_cmd_func_t cmd;
540 } read_func;
541 mp_close_func_t close_func;
542 void *ctx;
543 unsigned eof : 1;
544 unsigned drop : 1;
545 unsigned dead : 1;
546 unsigned got_cmd : 1;
547 unsigned no_select : 1;
548 // These fields are for the cmd fds.
549 char* buffer;
550 int pos,size;
551 } mp_input_fd_t;
553 typedef struct mp_cmd_filter mp_cmd_filter_t;
555 struct mp_cmd_filter {
556 mp_input_cmd_filter filter;
557 void* ctx;
558 mp_cmd_filter_t* next;
561 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
563 struct mp_cmd_bind_section {
564 mp_cmd_bind_t* cmd_binds;
565 char* section;
566 mp_cmd_bind_section_t* next;
569 struct input_ctx {
570 // Autorepeat stuff
571 short ar_state;
572 mp_cmd_t *ar_cmd;
573 unsigned int last_ar;
574 // Autorepeat config
575 unsigned int ar_delay;
576 unsigned int ar_rate;
578 // these are the keys currently down
579 int key_down[MP_MAX_KEY_DOWN];
580 unsigned int num_key_down;
581 unsigned int last_key_down;
583 bool default_bindings;
584 // List of command binding sections
585 mp_cmd_bind_section_t *cmd_bind_sections;
586 // Name of currently used command section
587 char *section;
588 // The command binds of current section
589 mp_cmd_bind_t *cmd_binds;
590 mp_cmd_bind_t *cmd_binds_default;
592 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
593 unsigned int num_key_fd;
595 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
596 unsigned int num_cmd_fd;
598 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
599 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
603 static mp_cmd_filter_t* cmd_filters = NULL;
605 // Callback to allow the menu filter to grab the incoming keys
606 int (*mp_input_key_cb)(int code) = NULL;
608 int async_quit_request;
610 static int print_key_list(m_option_t* cfg);
611 static int print_cmd_list(m_option_t* cfg);
613 // Our command line options
614 static const m_option_t input_conf[] = {
615 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
616 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
617 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
618 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
619 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
620 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
621 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
622 OPT_STRING("file", input.in_file, CONF_GLOBAL),
623 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
624 { NULL, NULL, 0, 0, 0, 0, NULL}
627 static const m_option_t mp_input_opts[] = {
628 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
629 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
630 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
631 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
632 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
633 { NULL, NULL, 0, 0, 0, 0, NULL}
636 static int default_cmd_func(int fd,char* buf, int l);
638 static char *get_key_name(int key)
640 char *ret = talloc_strdup(NULL, "");
641 for (int i = 0; modifier_names[i].name; i++) {
642 if (modifier_names[i].key & key) {
643 ret = talloc_asprintf_append_buffer(ret, "%s+",
644 modifier_names[i].name);
645 key -= modifier_names[i].key;
648 for (int i = 0; key_names[i].name != NULL; i++) {
649 if (key_names[i].key == key)
650 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
653 if (isascii(key))
654 return talloc_asprintf_append_buffer(ret, "%c", key);
656 // Print the hex key code
657 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
660 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
661 mp_cmd_func_t read_func, mp_close_func_t close_func)
663 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
664 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",fd);
665 return 0;
667 if (select && fd < 0) {
668 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
669 return 0;
672 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
673 .fd = fd,
674 .read_func.cmd = read_func ? read_func : default_cmd_func,
675 .close_func = close_func,
676 .no_select = !select
678 ictx->num_cmd_fd++;
680 return 1;
683 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
685 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
686 unsigned int i;
688 for (i = 0; i < ictx->num_cmd_fd; i++) {
689 if(cmd_fds[i].fd == fd)
690 break;
692 if (i == ictx->num_cmd_fd)
693 return;
694 if(cmd_fds[i].close_func)
695 cmd_fds[i].close_func(cmd_fds[i].fd);
696 talloc_free(cmd_fds[i].buffer);
698 if (i + 1 < ictx->num_cmd_fd)
699 memmove(&cmd_fds[i], &cmd_fds[i+1],
700 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
701 ictx->num_cmd_fd--;
704 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
706 struct mp_input_fd *key_fds = ictx->key_fds;
707 unsigned int i;
709 for (i = 0; i < ictx->num_key_fd; i++) {
710 if(key_fds[i].fd == fd)
711 break;
713 if (i == ictx->num_key_fd)
714 return;
715 if(key_fds[i].close_func)
716 key_fds[i].close_func(key_fds[i].fd);
718 if(i + 1 < ictx->num_key_fd)
719 memmove(&key_fds[i], &key_fds[i+1],
720 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
721 ictx->num_key_fd--;
724 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
725 mp_key_func_t read_func, mp_close_func_t close_func,
726 void *ctx)
728 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
729 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",fd);
730 return 0;
732 if (select && fd < 0) {
733 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
734 return 0;
737 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
738 .fd = fd,
739 .read_func.key = read_func,
740 .close_func = close_func,
741 .no_select = !select,
742 .ctx = ctx,
744 ictx->num_key_fd++;
746 return 1;
749 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
751 int cmd_num = 0;
753 while (*str == '\n' || *str == '\r' || *str == ' ')
754 ++str;
755 while (*str) {
756 mp_cmd_t *cmd;
757 size_t len = strcspn(str, "\r\n");
758 char *cmdbuf = talloc_size(NULL, len+1);
759 av_strlcpy(cmdbuf, str, len+1);
760 cmd = mp_input_parse_cmd(cmdbuf);
761 if (cmd) {
762 mp_input_queue_cmd(ictx, cmd);
763 ++cmd_num;
765 str += len;
766 while (*str == '\n' || *str == '\r' || *str == ' ')
767 ++str;
768 talloc_free(cmdbuf);
770 return cmd_num;
773 mp_cmd_t*
774 mp_input_parse_cmd(char* str) {
775 int i,l;
776 int pausing = 0;
777 char *ptr,*e;
778 const mp_cmd_t *cmd_def;
780 #ifdef MP_DEBUG
781 assert(str != NULL);
782 #endif
784 // Ignore heading spaces.
785 while (str[0] == ' ' || str[0] == '\t')
786 ++str;
788 if (strncmp(str, "pausing ", 8) == 0) {
789 pausing = 1;
790 str = &str[8];
791 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
792 pausing = 2;
793 str = &str[13];
794 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
795 pausing = 3;
796 str = &str[15];
797 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
798 pausing = 4;
799 str = &str[19];
802 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
803 /* NOTHING */;
804 if(ptr[0] != '\0')
805 l = ptr-str;
806 else
807 l = strlen(str);
809 if(l == 0)
810 return NULL;
812 for(i=0; mp_cmds[i].name != NULL; i++) {
813 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
814 break;
817 if(mp_cmds[i].name == NULL)
818 return NULL;
820 cmd_def = &mp_cmds[i];
822 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
823 *cmd = (mp_cmd_t){
824 .id = cmd_def->id,
825 .name = talloc_strdup(cmd, cmd_def->name),
826 .pausing = pausing,
829 ptr = str;
831 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
832 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
833 if(ptr[0] == '\0') break;
834 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
835 if(ptr[0] == '\0' || ptr[0] == '#') break;
836 cmd->args[i].type = cmd_def->args[i].type;
837 switch(cmd_def->args[i].type) {
838 case MP_CMD_ARG_INT:
839 errno = 0;
840 cmd->args[i].v.i = atoi(ptr);
841 if(errno != 0) {
842 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
843 ptr = NULL;
845 break;
846 case MP_CMD_ARG_FLOAT:
847 errno = 0;
848 cmd->args[i].v.f = atof(ptr);
849 if(errno != 0) {
850 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
851 ptr = NULL;
853 break;
854 case MP_CMD_ARG_STRING: {
855 char term;
856 char* ptr2 = ptr, *start;
858 if(ptr[0] == '\'' || ptr[0] == '"') {
859 term = ptr[0];
860 ptr2++;
861 } else
862 term = ' ';
863 start = ptr2;
864 while(1) {
865 e = strchr(ptr2,term);
866 if(!e) break;
867 if(e <= ptr2 || *(e - 1) != '\\') break;
868 ptr2 = e + 1;
871 if(term != ' ' && (!e || e[0] == '\0')) {
872 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
873 ptr = NULL;
874 break;
875 } else if(!e) e = ptr+strlen(ptr);
876 l = e-start;
877 ptr2 = start;
878 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
879 memmove(e,e+1,strlen(e));
880 ptr2 = e + 1;
881 l--;
883 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
884 if(term != ' ') ptr += l+2;
885 } break;
886 case -1:
887 ptr = NULL;
888 break;
889 default :
890 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
893 cmd->nargs = i;
895 if(cmd_def->nargs > cmd->nargs) {
896 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
897 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s requires at least %d arguments, we found only %d so far.\n",cmd_def->name,cmd_def->nargs,cmd->nargs);
898 mp_cmd_free(cmd);
899 return NULL;
902 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
903 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
904 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
905 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
908 if(i < MP_CMD_MAX_ARGS)
909 cmd->args[i].type = -1;
911 return cmd;
914 #define MP_CMD_MAX_SIZE 4096
916 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
918 char* end;
919 (*ret) = NULL;
921 // Allocate the buffer if it doesn't exist
922 if(!mp_fd->buffer) {
923 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
924 mp_fd->pos = 0;
925 mp_fd->size = MP_CMD_MAX_SIZE;
928 // Get some data if needed/possible
929 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
930 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
931 mp_fd->size - 1 - mp_fd->pos);
932 // Error ?
933 if(r < 0) {
934 switch(r) {
935 case MP_INPUT_ERROR:
936 case MP_INPUT_DEAD:
937 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",mp_fd->fd,strerror(errno));
938 case MP_INPUT_NOTHING:
939 return r;
940 case MP_INPUT_RETRY:
941 continue;
943 // EOF ?
944 } else if(r == 0) {
945 mp_fd->eof = 1;
946 break;
948 mp_fd->pos += r;
949 break;
952 mp_fd->got_cmd = 0;
954 while(1) {
955 int l = 0;
956 // Find the cmd end
957 mp_fd->buffer[mp_fd->pos] = '\0';
958 end = strchr(mp_fd->buffer,'\r');
959 if (end) *end = '\n';
960 end = strchr(mp_fd->buffer,'\n');
961 // No cmd end ?
962 if(!end) {
963 // If buffer is full we must drop all until the next \n
964 if(mp_fd->size - mp_fd->pos <= 1) {
965 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd);
966 mp_fd->pos = 0;
967 mp_fd->drop = 1;
969 break;
971 // We already have a cmd : set the got_cmd flag
972 else if((*ret)) {
973 mp_fd->got_cmd = 1;
974 break;
977 l = end - mp_fd->buffer;
979 // Not dropping : put the cmd in ret
980 if (!mp_fd->drop)
981 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
982 else
983 mp_fd->drop = 0;
984 mp_fd->pos -= l+1;
985 memmove(mp_fd->buffer, end+1, mp_fd->pos);
988 if(*ret)
989 return 1;
990 else
991 return MP_INPUT_NOTHING;
994 static int default_cmd_func(int fd,char* buf, int l)
996 while(1) {
997 int r = read(fd,buf,l);
998 // Error ?
999 if(r < 0) {
1000 if(errno == EINTR)
1001 continue;
1002 else if(errno == EAGAIN)
1003 return MP_INPUT_NOTHING;
1004 return MP_INPUT_ERROR;
1005 // EOF ?
1007 return r;
1013 void
1014 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
1015 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1017 filter->filter = func;
1018 filter->ctx = ctx;
1019 filter->next = cmd_filters;
1020 cmd_filters = filter;
1024 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
1026 int j;
1028 if (n <= 0) return NULL;
1029 for(j = 0; binds[j].cmd != NULL; j++) {
1030 int found = 1,s;
1031 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1032 if(binds[j].input[s] != keys[s]) {
1033 found = 0;
1034 break;
1037 if(found && binds[j].input[s] == 0 && s == n)
1038 break;
1040 return binds[j].cmd;
1043 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1044 char *section)
1046 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1048 if (section==NULL) section="default";
1049 while (bind_section) {
1050 if(strcmp(section,bind_section->section)==0) return bind_section;
1051 if(bind_section->next==NULL) break;
1052 bind_section=bind_section->next;
1054 if(bind_section) {
1055 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1056 bind_section=bind_section->next;
1057 } else {
1058 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1059 bind_section = ictx->cmd_bind_sections;
1061 bind_section->cmd_binds=NULL;
1062 bind_section->section = talloc_strdup(bind_section, section);
1063 bind_section->next=NULL;
1064 return bind_section;
1067 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1069 char* cmd = NULL;
1070 mp_cmd_t* ret;
1072 if (ictx->cmd_binds)
1073 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1074 if (ictx->cmd_binds_default && cmd == NULL)
1075 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1076 if (ictx->default_bindings && cmd == NULL)
1077 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1079 if(cmd == NULL) {
1080 char *key_buf = get_key_name(keys[0]);
1081 mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", key_buf);
1082 talloc_free(key_buf);
1083 if(n > 1) {
1084 int s;
1085 for(s=1; s < n; s++) {
1086 key_buf = get_key_name(keys[s]);
1087 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", key_buf);
1088 talloc_free(key_buf);
1091 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1092 return NULL;
1094 if (strcmp(cmd, "ignore") == 0) return NULL;
1095 ret = mp_input_parse_cmd(cmd);
1096 if(!ret) {
1097 char *key_buf = get_key_name(ictx->key_down[0]);
1098 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s", key_buf);
1099 talloc_free(key_buf);
1100 if (ictx->num_key_down > 1) {
1101 unsigned int s;
1102 for(s=1; s < ictx->num_key_down; s++) {
1103 char *key_buf = get_key_name(ictx->key_down[s]);
1104 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf);
1105 talloc_free(key_buf);
1108 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1110 return ret;
1114 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code)
1116 unsigned int j;
1117 mp_cmd_t* ret;
1119 /* On normal keyboards shift changes the character code of non-special
1120 * keys, so don't count the modifier separately for those. In other words
1121 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1122 * shift modifier is still kept for special keys like arrow keys.
1124 int unmod = code & ~KEY_MODIFIER_MASK;
1125 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1126 code &= ~KEY_MODIFIER_SHIFT;
1128 if(mp_input_key_cb) {
1129 if (code & MP_KEY_DOWN)
1130 return NULL;
1131 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1132 if (mp_input_key_cb(code))
1133 return NULL;
1136 if(code & MP_KEY_DOWN) {
1137 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1138 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1139 return NULL;
1141 code &= ~MP_KEY_DOWN;
1142 // Check if we don't already have this key as pushed
1143 for (j = 0; j < ictx->num_key_down; j++) {
1144 if (ictx->key_down[j] == code)
1145 break;
1147 if (j != ictx->num_key_down)
1148 return NULL;
1149 ictx->key_down[ictx->num_key_down] = code;
1150 ictx->num_key_down++;
1151 ictx->last_key_down = GetTimer();
1152 ictx->ar_state = 0;
1153 return NULL;
1155 // key released
1156 // Check if the key is in the down key, driver which can't send push event
1157 // send only release event
1158 for (j = 0; j < ictx->num_key_down; j++) {
1159 if (ictx->key_down[j] == code)
1160 break;
1162 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1163 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1164 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1165 return NULL;
1167 ictx->key_down[ictx->num_key_down] = code;
1168 ictx->num_key_down++;
1169 ictx->last_key_down = 1;
1171 // We ignore key from last combination
1172 ret = ictx->last_key_down ?
1173 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1174 : NULL;
1175 // Remove the key
1176 if (j+1 < ictx->num_key_down)
1177 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1178 (ictx->num_key_down-(j+1))*sizeof(int));
1179 ictx->num_key_down--;
1180 ictx->last_key_down = 0;
1181 ictx->ar_state = -1;
1182 mp_cmd_free(ictx->ar_cmd);
1183 ictx->ar_cmd = NULL;
1184 return ret;
1187 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1189 // No input : autorepeat ?
1190 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1191 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1192 unsigned int t = GetTimer();
1193 // First time : wait delay
1194 if (ictx->ar_state == 0
1195 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1196 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1197 ictx->key_down);
1198 if (!ictx->ar_cmd) {
1199 ictx->ar_state = -1;
1200 return NULL;
1202 ictx->ar_state = 1;
1203 ictx->last_ar = t;
1204 return mp_cmd_clone(ictx->ar_cmd);
1205 // Then send rate / sec event
1206 } else if (ictx->ar_state == 1
1207 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1208 ictx->last_ar = t;
1209 return mp_cmd_clone(ictx->ar_cmd);
1212 return NULL;
1217 * \param time time to wait at most for an event in milliseconds
1219 static mp_cmd_t *read_events(struct input_ctx *ictx, int time)
1221 int i;
1222 int got_cmd = 0;
1223 struct mp_input_fd *key_fds = ictx->key_fds;
1224 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1225 for (i = 0; i < ictx->num_key_fd; i++)
1226 if (key_fds[i].dead) {
1227 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1228 i--;
1230 for (i = 0; i < ictx->num_cmd_fd; i++)
1231 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1232 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1233 i--;
1235 else if (cmd_fds[i].got_cmd)
1236 got_cmd = 1;
1237 #ifdef HAVE_POSIX_SELECT
1238 fd_set fds;
1239 FD_ZERO(&fds);
1240 if (!got_cmd) {
1241 int max_fd = 0;
1242 for (i = 0; i < ictx->num_key_fd; i++) {
1243 if (key_fds[i].no_select)
1244 continue;
1245 if (key_fds[i].fd > max_fd)
1246 max_fd = key_fds[i].fd;
1247 FD_SET(key_fds[i].fd, &fds);
1249 for (i = 0; i < ictx->num_cmd_fd; i++) {
1250 if (cmd_fds[i].no_select)
1251 continue;
1252 if (cmd_fds[i].fd > max_fd)
1253 max_fd = cmd_fds[i].fd;
1254 FD_SET(cmd_fds[i].fd, &fds);
1256 struct timeval tv, *time_val;
1257 if (time >= 0) {
1258 tv.tv_sec = time / 1000;
1259 tv.tv_usec = (time % 1000) * 1000;
1260 time_val = &tv;
1261 } else
1262 time_val = NULL;
1263 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1264 if (errno != EINTR)
1265 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1266 strerror(errno));
1267 FD_ZERO(&fds);
1270 #else
1271 if (!got_cmd && time)
1272 usec_sleep(time * 1000);
1273 #endif
1276 for (i = 0; i < ictx->num_key_fd; i++) {
1277 #ifdef HAVE_POSIX_SELECT
1278 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1279 continue;
1280 #endif
1282 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1283 if (code >= 0) {
1284 mp_cmd_t *ret = interpret_key(ictx, code);
1285 if (ret)
1286 return ret;
1288 else if (code == MP_INPUT_ERROR)
1289 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n",
1290 key_fds[i].fd);
1291 else if (code == MP_INPUT_DEAD) {
1292 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n",
1293 key_fds[i].fd);
1294 key_fds[i].dead = 1;
1297 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx);
1298 if (autorepeat_cmd)
1299 return autorepeat_cmd;
1301 for (i = 0; i < ictx->num_cmd_fd; i++) {
1302 #ifdef HAVE_POSIX_SELECT
1303 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1304 !cmd_fds[i].got_cmd)
1305 continue;
1306 #endif
1307 char *cmd;
1308 int r = read_cmd(&cmd_fds[i], &cmd);
1309 if (r >= 0) {
1310 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1311 talloc_free(cmd);
1312 if (ret)
1313 return ret;
1315 else if (r == MP_INPUT_ERROR)
1316 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1317 cmd_fds[i].fd);
1318 else if (r == MP_INPUT_DEAD)
1319 cmd_fds[i].dead = 1;
1322 return NULL;
1326 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1328 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1329 return 0;
1330 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1331 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1332 ictx->cmd_queue_length++;
1333 return 1;
1336 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1338 mp_cmd_t* ret;
1340 if (ictx->cmd_queue_length == 0)
1341 return NULL;
1343 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1345 if (!peek_only) {
1346 ictx->cmd_queue_length--;
1347 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1350 return ret;
1354 * \param peek_only when set, the returned command stays in the queue.
1355 * Do not free the returned cmd whe you set this!
1357 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1359 mp_cmd_t* ret = NULL;
1360 mp_cmd_filter_t* cf;
1361 int from_queue;
1363 if (async_quit_request)
1364 return mp_input_parse_cmd("quit 1");
1365 while(1) {
1366 from_queue = 1;
1367 ret = get_queued_cmd(ictx, peek_only);
1368 if(ret) break;
1369 from_queue = 0;
1370 ret = read_events(ictx, time);
1371 if (!ret) {
1372 from_queue = 1;
1373 ret = get_queued_cmd(ictx, peek_only);
1375 break;
1377 if(!ret) return NULL;
1379 for(cf = cmd_filters ; cf ; cf = cf->next) {
1380 if (cf->filter(ret, cf->ctx)) {
1381 if (peek_only && from_queue)
1382 // The filter ate the cmd, so we remove it from queue
1383 ret = get_queued_cmd(ictx, 0);
1384 mp_cmd_free(ret);
1385 return NULL;
1389 if (!from_queue && peek_only)
1390 mp_input_queue_cmd(ictx, ret);
1392 return ret;
1395 void
1396 mp_cmd_free(mp_cmd_t* cmd) {
1397 talloc_free(cmd);
1400 mp_cmd_t*
1401 mp_cmd_clone(mp_cmd_t* cmd) {
1402 mp_cmd_t* ret;
1403 int i;
1404 #ifdef MP_DEBUG
1405 assert(cmd != NULL);
1406 #endif
1408 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1409 ret->name = talloc_strdup(ret, cmd->name);
1410 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1411 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1412 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1415 return ret;
1418 int mp_input_get_key_from_name(const char *name)
1420 int modifiers = 0;
1421 const char *p;
1422 while (p = strchr(name, '+')) {
1423 for (struct mp_key_name *m = modifier_names; m->name; m++)
1424 if (!bstrcasecmp(BSTR(m->name), (struct bstr){name, p - name})) {
1425 modifiers |= m->key;
1426 goto found;
1428 if (!strcmp(name, "+"))
1429 return '+' + modifiers;
1430 return -1;
1431 found:
1432 name = p + 1;
1434 int len = strlen(name);
1435 if (len == 1) // Direct key code
1436 return (unsigned char)name[0] + modifiers;
1437 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1438 return strtol(name, NULL, 16) + modifiers;
1440 for (int i = 0; key_names[i].name != NULL; i++) {
1441 if (strcasecmp(key_names[i].name, name) == 0)
1442 return key_names[i].key + modifiers;
1445 return -1;
1448 static int get_input_from_name(char* name,int* keys) {
1449 char *end,*ptr;
1450 int n=0;
1452 ptr = name;
1453 n = 0;
1454 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1455 if(end && end[1] != '\0') {
1456 if(end[1] == '-')
1457 end = &end[1];
1458 end[0] = '\0';
1460 keys[n] = mp_input_get_key_from_name(ptr);
1461 if(keys[n] < 0) {
1462 return 0;
1464 n++;
1465 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1466 ptr = &end[1];
1467 else
1468 break;
1470 keys[n] = 0;
1471 return 1;
1474 #define BS_MAX 256
1475 #define SPACE_CHAR " \n\r\t"
1477 static void bind_keys(struct input_ctx *ictx,
1478 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1480 int i = 0,j;
1481 mp_cmd_bind_t* bind = NULL;
1482 mp_cmd_bind_section_t* bind_section = NULL;
1483 char *section=NULL, *p;
1485 #ifdef MP_DEBUG
1486 assert(keys != NULL);
1487 assert(cmd != NULL);
1488 #endif
1490 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1491 *p=0;
1492 section=++cmd;
1493 cmd=++p;
1494 // Jump beginning space
1495 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1496 /* NOTHING */;
1498 bind_section = get_bind_section(ictx, section);
1500 if(bind_section->cmd_binds) {
1501 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1502 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1503 /* NOTHING */;
1504 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1505 bind = &bind_section->cmd_binds[i];
1506 break;
1511 if(!bind) {
1512 bind_section->cmd_binds = talloc_realloc(bind_section,
1513 bind_section->cmd_binds,
1514 mp_cmd_bind_t, i + 2);
1515 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1516 bind = &bind_section->cmd_binds[i];
1518 talloc_free(bind->cmd);
1519 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1520 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1523 static int parse_config(struct input_ctx *ictx, char *file)
1525 int fd;
1526 int bs = 0,r,eof = 0,comments = 0;
1527 char *iter,*end;
1528 char buffer[BS_MAX];
1529 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1531 fd = open(file,O_RDONLY);
1533 if(fd < 0) {
1534 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1535 return 0;
1538 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1540 while(1) {
1541 if(! eof && bs < BS_MAX-1) {
1542 if(bs > 0) bs--;
1543 r = read(fd,buffer+bs,BS_MAX-1-bs);
1544 if(r < 0) {
1545 if(errno == EINTR)
1546 continue;
1547 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1548 close(fd);
1549 return 0;
1550 } else if(r == 0) {
1551 eof = 1;
1552 } else {
1553 bs += r+1;
1554 buffer[bs-1] = '\0';
1557 // Empty buffer : return
1558 if(bs <= 1) {
1559 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1560 close(fd);
1561 return 1;
1564 iter = buffer;
1566 if(comments) {
1567 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1568 /* NOTHING */;
1569 if(iter[0] == '\0') { // Buffer was full of comment
1570 bs = 0;
1571 continue;
1573 iter++;
1574 r = strlen(iter);
1575 memmove(buffer,iter,r+1);
1576 bs = r+1;
1577 comments = 0;
1578 continue;
1581 // Find the wanted key
1582 if(keys[0] == 0) {
1583 // Jump beginning space
1584 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1585 /* NOTHING */;
1586 if(iter[0] == '\0') { // Buffer was full of space char
1587 bs = 0;
1588 continue;
1590 if(iter[0] == '#') { // Comments
1591 comments = 1;
1592 continue;
1594 // Find the end of the key code name
1595 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1596 /*NOTHING */;
1597 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1598 if(buffer == iter) {
1599 if(eof && (buffer-iter) == bs)
1600 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1601 else
1602 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1603 return 0;
1605 memmove(buffer,iter,end-iter);
1606 bs = end-iter;
1607 continue;
1610 char name[end-iter+1];
1611 strncpy(name,iter,end-iter);
1612 name[end-iter] = '\0';
1613 if (!get_input_from_name(name,keys)) {
1614 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1615 close(fd);
1616 return 0;
1619 if( bs > (end-buffer))
1620 memmove(buffer,end,bs - (end-buffer));
1621 bs -= end-buffer;
1622 continue;
1623 } else { // Get the command
1624 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1625 // Found new line
1626 if(iter[0] == '\n' || iter[0] == '\r') {
1627 int i;
1628 char *key_buf = get_key_name(keys[0]);
1629 mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", key_buf);
1630 talloc_free(key_buf);
1631 for(i = 1; keys[i] != 0 ; i++) {
1632 char *key_buf = get_key_name(keys[i]);
1633 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf);
1634 talloc_free(key_buf);
1636 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1637 keys[0] = 0;
1638 if(iter > buffer) {
1639 memmove(buffer,iter,bs- (iter-buffer));
1640 bs -= (iter-buffer);
1642 continue;
1644 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1645 /* NOTHING */;
1646 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1647 if(iter == buffer) {
1648 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1649 close(fd);
1650 return 0;
1652 memmove(buffer,iter,end - iter);
1653 bs = end - iter;
1654 continue;
1657 char cmd[end-iter+1];
1658 strncpy(cmd,iter,end-iter);
1659 cmd[end-iter] = '\0';
1660 //printf("Set bind %d => %s\n",keys[0],cmd);
1661 bind_keys(ictx, keys,cmd);
1662 n_binds++;
1664 keys[0] = 0;
1665 end++;
1666 if(bs > (end-buffer))
1667 memmove(buffer,end,bs-(end-buffer));
1668 bs -= (end-buffer);
1669 buffer[bs-1] = '\0';
1670 continue;
1673 mp_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1674 close(fd);
1675 mp_input_set_section(ictx, NULL);
1676 return 0;
1679 void mp_input_set_section(struct input_ctx *ictx, char *name)
1681 mp_cmd_bind_section_t* bind_section = NULL;
1683 ictx->cmd_binds = NULL;
1684 ictx->cmd_binds_default = NULL;
1685 talloc_free(ictx->section);
1686 if (name)
1687 ictx->section = talloc_strdup(ictx, name);
1688 else
1689 ictx->section = talloc_strdup(ictx, "default");
1690 if ((bind_section = get_bind_section(ictx, ictx->section)))
1691 ictx->cmd_binds = bind_section->cmd_binds;
1692 if (strcmp(ictx->section, "default") == 0)
1693 return;
1694 if ((bind_section = get_bind_section(ictx, NULL)))
1695 ictx->cmd_binds_default = bind_section->cmd_binds;
1698 char *mp_input_get_section(struct input_ctx *ictx)
1700 return ictx->section;
1703 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1705 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1706 *ictx = (struct input_ctx){
1707 .ar_state = -1,
1708 .ar_delay = input_conf->ar_delay,
1709 .ar_rate = input_conf->ar_rate,
1710 .default_bindings = input_conf->default_bindings,
1713 char* file;
1714 char *config_file = input_conf->config_file;
1715 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1716 if(!file)
1717 return ictx;
1719 if (!parse_config(ictx, file)) {
1720 // free file if it was allocated by get_path(),
1721 // before it gets overwritten
1722 if( file != config_file)
1724 free(file);
1726 // Try global conf dir
1727 file = MPLAYER_CONFDIR "/input.conf";
1728 if (!parse_config(ictx, file))
1729 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1731 else
1733 // free file if it was allocated by get_path()
1734 if( file != config_file)
1735 free(file);
1738 #ifdef CONFIG_JOYSTICK
1739 if (input_conf->use_joystick) {
1740 int fd = mp_input_joystick_init(input_conf->js_dev);
1741 if(fd < 0)
1742 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1743 else
1744 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1745 (mp_close_func_t)close,NULL);
1747 #endif
1749 #ifdef CONFIG_LIRC
1750 if (input_conf->use_lirc) {
1751 int fd = mp_input_lirc_init();
1752 if(fd > 0)
1753 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1754 mp_input_lirc_close);
1756 #endif
1758 #ifdef CONFIG_LIRCC
1759 if (input_conf->use_lircc) {
1760 int fd = lircc_init("mplayer", NULL);
1761 if(fd >= 0)
1762 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1764 #endif
1766 #ifdef CONFIG_APPLE_REMOTE
1767 if (input_conf->use_ar) {
1768 if(mp_input_ar_init() < 0)
1769 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1770 else
1771 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1772 NULL);
1774 #endif
1776 #ifdef CONFIG_APPLE_IR
1777 if (input_conf->use_ar) {
1778 int fd = mp_input_appleir_init(input_conf->ar_dev);
1779 if(fd < 0)
1780 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1781 else
1782 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1783 (mp_close_func_t)close, NULL);
1785 #endif
1787 if (input_conf->in_file) {
1788 struct stat st;
1789 int mode = O_RDONLY;
1790 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1791 // Note that on Windows stat may fail for named pipes, but due to how the
1792 // API works, using RDONLY should be ok.
1793 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1794 mode = O_RDWR;
1795 int in_file_fd = open(input_conf->in_file, mode);
1796 if(in_file_fd >= 0)
1797 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1798 (mp_close_func_t)close);
1799 else
1800 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1801 input_conf->in_file, strerror(errno));
1803 return ictx;
1806 void mp_input_uninit(struct input_ctx *ictx)
1808 if (!ictx)
1809 return;
1811 unsigned int i;
1813 for (i=0; i < ictx->num_key_fd; i++) {
1814 if (ictx->key_fds[i].close_func)
1815 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1818 for (i = 0; i < ictx->num_cmd_fd; i++) {
1819 if (ictx->cmd_fds[i].close_func)
1820 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1822 talloc_free(ictx);
1825 void
1826 mp_input_register_options(m_config_t* cfg) {
1827 m_config_register_options(cfg,mp_input_opts);
1830 static int print_key_list(m_option_t* cfg)
1832 int i;
1833 printf("\n");
1834 for(i= 0; key_names[i].name != NULL ; i++)
1835 printf("%s\n",key_names[i].name);
1836 exit(0);
1839 static int print_cmd_list(m_option_t* cfg)
1841 const mp_cmd_t *cmd;
1842 int i,j;
1843 const char* type;
1845 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1846 printf("%-20.20s",cmd->name);
1847 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1848 switch(cmd->args[j].type) {
1849 case MP_CMD_ARG_INT:
1850 type = "Integer";
1851 break;
1852 case MP_CMD_ARG_FLOAT:
1853 type = "Float";
1854 break;
1855 case MP_CMD_ARG_STRING:
1856 type = "String";
1857 break;
1858 default:
1859 type = "??";
1861 if(j+1 > cmd->nargs)
1862 printf(" [%s]",type);
1863 else
1864 printf(" %s",type);
1866 printf("\n");
1868 exit(0);
1872 * \param time time to wait for an interruption in milliseconds
1874 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1876 mp_cmd_t* cmd;
1877 if ((cmd = mp_input_get_cmd(ictx, time, 1)) == NULL)
1878 return 0;
1879 switch(cmd->id) {
1880 case MP_CMD_QUIT:
1881 case MP_CMD_PLAY_TREE_STEP:
1882 case MP_CMD_PLAY_TREE_UP_STEP:
1883 case MP_CMD_PLAY_ALT_SRC_STEP:
1884 // The cmd will be executed when we are back in the main loop
1885 return 1;
1887 // remove the cmd from the queue
1888 cmd = mp_input_get_cmd(ictx, time, 0);
1889 mp_cmd_free(cmd);
1890 return 0;