Add af_eq_set_bands slave command support
[mplayer/kovensky.git] / input / input.c
blobf7c7abfb7a0a67cebf28d877bd6b0f1a4525a820
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 "help_mp.h"
44 #include "m_config.h"
45 #include "m_option.h"
46 #include "get_path.h"
47 #include "talloc.h"
48 #include "options.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}}, {-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_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_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
187 { MP_CMD_AF_EQ_SET, "af_eq_set_bands", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}}}}, //turbos
188 #ifdef CONFIG_TV_TELETEXT
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}} } },
191 #endif
193 #ifdef CONFIG_DVDNAV
194 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
195 #endif
197 #ifdef CONFIG_MENU
198 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
199 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
200 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
201 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
202 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
203 #endif
205 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
206 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
207 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
208 { MP_CMD_SET_PROPERTY, "set_property", 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}} } },
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 { 0, NULL, 0, {} }
218 /// The names of the keys as used in input.conf
219 /// If you add some new keys, you also need to add them here
221 static const mp_key_name_t key_names[] = {
223 { CTRL_A, "CTRL_A" },
224 { CTRL_B, "CTRL_B" },
225 { CTRL_C, "CTRL_C" },
226 { CTRL_D, "CTRL_D" },
227 { CTRL_E, "CTRL_E" },
228 { CTRL_F, "CTRL_F" },
229 { CTRL_G, "CTRL_G" },
230 { CTRL_J, "CTRL_J" },
231 { CTRL_K, "CTRL_K" },
232 { CTRL_N, "CTRL_N" },
233 { CTRL_O, "CTRL_O" },
234 { CTRL_P, "CTRL_P" },
235 { CTRL_Q, "CTRL_Q" },
236 { CTRL_L, "CTRL_L" },
237 { CTRL_V, "CTRL_V" },
238 { CTRL_W, "CTRL_W" },
239 { CTRL_R, "CTRL_R" },
240 { CTRL_S, "CTRL_S" },
241 { CTRL_Z, "CTRL_Z" },
242 { CTRL_SO, "CTRL_[" },
243 { CTRL_BSL, "CTRL_\\"},
244 { CTRL_SC, "CTRL_]" },
245 { CTRL_T, "CTRL_T" },
246 { CTRL_U, "CTRL_U" },
247 { CTRL_Y, "CTRL_Y" },
249 { ' ', "SPACE" },
250 { '#', "SHARP" },
251 { KEY_ENTER, "ENTER" },
252 { KEY_TAB, "TAB" },
253 { KEY_CTRL, "CTRL" },
254 { KEY_BACKSPACE, "BS" },
255 { KEY_DELETE, "DEL" },
256 { KEY_INSERT, "INS" },
257 { KEY_HOME, "HOME" },
258 { KEY_END, "END" },
259 { KEY_PAGE_UP, "PGUP" },
260 { KEY_PAGE_DOWN, "PGDWN" },
261 { KEY_ESC, "ESC" },
262 { KEY_RIGHT, "RIGHT" },
263 { KEY_LEFT, "LEFT" },
264 { KEY_DOWN, "DOWN" },
265 { KEY_UP, "UP" },
266 { KEY_F+1, "F1" },
267 { KEY_F+2, "F2" },
268 { KEY_F+3, "F3" },
269 { KEY_F+4, "F4" },
270 { KEY_F+5, "F5" },
271 { KEY_F+6, "F6" },
272 { KEY_F+7, "F7" },
273 { KEY_F+8, "F8" },
274 { KEY_F+9, "F9" },
275 { KEY_F+10, "F10" },
276 { KEY_F+11, "F11" },
277 { KEY_F+12, "F12" },
278 { KEY_KP0, "KP0" },
279 { KEY_KP1, "KP1" },
280 { KEY_KP2, "KP2" },
281 { KEY_KP3, "KP3" },
282 { KEY_KP4, "KP4" },
283 { KEY_KP5, "KP5" },
284 { KEY_KP6, "KP6" },
285 { KEY_KP7, "KP7" },
286 { KEY_KP8, "KP8" },
287 { KEY_KP9, "KP9" },
288 { KEY_KPDEL, "KP_DEL" },
289 { KEY_KPDEC, "KP_DEC" },
290 { KEY_KPINS, "KP_INS" },
291 { KEY_KPENTER, "KP_ENTER" },
292 { MOUSE_BTN0, "MOUSE_BTN0" },
293 { MOUSE_BTN1, "MOUSE_BTN1" },
294 { MOUSE_BTN2, "MOUSE_BTN2" },
295 { MOUSE_BTN3, "MOUSE_BTN3" },
296 { MOUSE_BTN4, "MOUSE_BTN4" },
297 { MOUSE_BTN5, "MOUSE_BTN5" },
298 { MOUSE_BTN6, "MOUSE_BTN6" },
299 { MOUSE_BTN7, "MOUSE_BTN7" },
300 { MOUSE_BTN8, "MOUSE_BTN8" },
301 { MOUSE_BTN9, "MOUSE_BTN9" },
302 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
303 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
304 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
305 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
306 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
307 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
308 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
309 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
310 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
311 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
312 { JOY_AXIS1_MINUS, "JOY_UP" },
313 { JOY_AXIS1_PLUS, "JOY_DOWN" },
314 { JOY_AXIS0_MINUS, "JOY_LEFT" },
315 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
317 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
318 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
319 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
320 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
321 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
322 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
323 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
324 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
325 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
326 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
327 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
328 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
329 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
330 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
331 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
332 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
333 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
334 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
335 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
336 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
338 { JOY_BTN0, "JOY_BTN0" },
339 { JOY_BTN1, "JOY_BTN1" },
340 { JOY_BTN2, "JOY_BTN2" },
341 { JOY_BTN3, "JOY_BTN3" },
342 { JOY_BTN4, "JOY_BTN4" },
343 { JOY_BTN5, "JOY_BTN5" },
344 { JOY_BTN6, "JOY_BTN6" },
345 { JOY_BTN7, "JOY_BTN7" },
346 { JOY_BTN8, "JOY_BTN8" },
347 { JOY_BTN9, "JOY_BTN9" },
349 { AR_PLAY, "AR_PLAY" },
350 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
351 { AR_NEXT, "AR_NEXT" },
352 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
353 { AR_PREV, "AR_PREV" },
354 { AR_PREV_HOLD, "AR_PREV_HOLD" },
355 { AR_MENU, "AR_MENU" },
356 { AR_MENU_HOLD, "AR_MENU_HOLD" },
357 { AR_VUP, "AR_VUP" },
358 { AR_VDOWN, "AR_VDOWN" },
360 { KEY_POWER, "POWER" },
361 { KEY_MENU, "MENU" },
362 { KEY_PLAY, "PLAY" },
363 { KEY_PAUSE, "PAUSE" },
364 { KEY_PLAYPAUSE, "PLAYPAUSE" },
365 { KEY_STOP, "STOP" },
366 { KEY_FORWARD, "FORWARD" },
367 { KEY_REWIND, "REWIND" },
368 { KEY_NEXT, "NEXT" },
369 { KEY_PREV, "PREV" },
370 { KEY_VOLUME_UP, "VOLUME_UP" },
371 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
372 { KEY_MUTE, "MUTE" },
374 // These are kept for backward compatibility
375 { KEY_PAUSE, "XF86_PAUSE" },
376 { KEY_STOP, "XF86_STOP" },
377 { KEY_PREV, "XF86_PREV" },
378 { KEY_NEXT, "XF86_NEXT" },
380 { KEY_CLOSE_WIN, "CLOSE_WIN" },
382 { 0, NULL }
385 // This is the default binding. The content of input.conf overrides these.
386 // The first arg is a null terminated array of key codes.
387 // The second is the command
389 static const mp_cmd_bind_t def_cmd_binds[] = {
391 { { MOUSE_BTN3, 0 }, "seek 10" },
392 { { MOUSE_BTN4, 0 }, "seek -10" },
393 { { MOUSE_BTN5, 0 }, "volume 1" },
394 { { MOUSE_BTN6, 0 }, "volume -1" },
396 #ifdef CONFIG_DVDNAV
397 { { KEY_KP8, 0 }, "dvdnav up" }, // up
398 { { KEY_KP2, 0 }, "dvdnav down" }, // down
399 { { KEY_KP4, 0 }, "dvdnav left" }, // left
400 { { KEY_KP6, 0 }, "dvdnav right" }, // right
401 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
402 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
403 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
404 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
405 #endif
407 { { KEY_RIGHT, 0 }, "seek 10" },
408 { { KEY_LEFT, 0 }, "seek -10" },
409 { { KEY_UP, 0 }, "seek 60" },
410 { { KEY_DOWN, 0 }, "seek -60" },
411 { { KEY_PAGE_UP, 0 }, "seek 600" },
412 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
413 { { '+', 0 }, "audio_delay 0.100" },
414 { { '-', 0 }, "audio_delay -0.100" },
415 { { '[', 0 }, "speed_mult 0.9091" },
416 { { ']', 0 }, "speed_mult 1.1" },
417 { { '{', 0 }, "speed_mult 0.5" },
418 { { '}', 0 }, "speed_mult 2.0" },
419 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
420 { { 'q', 0 }, "quit" },
421 { { KEY_ESC, 0 }, "quit" },
422 { { 'p', 0 }, "pause" },
423 { { ' ', 0 }, "pause" },
424 { { '.', 0 }, "frame_step" },
425 { { KEY_HOME, 0 }, "pt_up_step 1" },
426 { { KEY_END, 0 }, "pt_up_step -1" },
427 { { '>', 0 }, "pt_step 1" },
428 { { KEY_ENTER, 0 }, "pt_step 1 1" },
429 { { '<', 0 }, "pt_step -1" },
430 { { KEY_INS, 0 }, "alt_src_step 1" },
431 { { KEY_DEL, 0 }, "alt_src_step -1" },
432 { { 'o', 0 }, "osd" },
433 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
434 { { 'z', 0 }, "sub_delay -0.1" },
435 { { 'x', 0 }, "sub_delay +0.1" },
436 { { 'g', 0 }, "sub_step -1" },
437 { { 'y', 0 }, "sub_step +1" },
438 { { '9', 0 }, "volume -1" },
439 { { '/', 0 }, "volume -1" },
440 { { '0', 0 }, "volume 1" },
441 { { '*', 0 }, "volume 1" },
442 { { '(', 0 }, "balance -0.1" },
443 { { ')', 0 }, "balance 0.1" },
444 { { 'm', 0 }, "mute" },
445 { { '1', 0 }, "contrast -1" },
446 { { '2', 0 }, "contrast 1" },
447 { { '3', 0 }, "brightness -1" },
448 { { '4', 0 }, "brightness 1" },
449 { { '5', 0 }, "hue -1" },
450 { { '6', 0 }, "hue 1" },
451 { { '7', 0 }, "saturation -1" },
452 { { '8', 0 }, "saturation 1" },
453 { { 'd', 0 }, "frame_drop" },
454 { { 'D', 0 }, "step_property deinterlace" },
455 { { 'r', 0 }, "sub_pos -1" },
456 { { 't', 0 }, "sub_pos +1" },
457 { { 'a', 0 }, "sub_alignment" },
458 { { 'v', 0 }, "sub_visibility" },
459 { { 'j', 0 }, "sub_select" },
460 { { 'F', 0 }, "forced_subs_only" },
461 { { '#', 0 }, "switch_audio" },
462 { { '_', 0 }, "step_property switch_video" },
463 { { KEY_TAB, 0 }, "step_property switch_program" },
464 { { 'i', 0 }, "edl_mark" },
465 #ifdef CONFIG_TV
466 { { 'h', 0 }, "tv_step_channel 1" },
467 { { 'k', 0 }, "tv_step_channel -1" },
468 { { 'n', 0 }, "tv_step_norm" },
469 { { 'u', 0 }, "tv_step_chanlist" },
470 #endif
471 #ifdef CONFIG_TV_TELETEXT
472 { { 'X', 0 }, "step_property teletext_mode 1" },
473 { { 'W', 0 }, "step_property teletext_page 1" },
474 { { 'Q', 0 }, "step_property teletext_page -1" },
475 #endif
476 #ifdef CONFIG_JOYSTICK
477 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
478 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
479 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
480 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
481 { { JOY_BTN0, 0 }, "pause" },
482 { { JOY_BTN1, 0 }, "osd" },
483 { { JOY_BTN2, 0 }, "volume 1"},
484 { { JOY_BTN3, 0 }, "volume -1"},
485 #endif
486 #ifdef CONFIG_APPLE_REMOTE
487 { { AR_PLAY, 0}, "pause" },
488 { { AR_PLAY_HOLD, 0}, "quit" },
489 { { AR_NEXT, 0 }, "seek 30" },
490 { { AR_NEXT_HOLD, 0 }, "seek 120" },
491 { { AR_PREV, 0 }, "seek -10" },
492 { { AR_PREV_HOLD, 0 }, "seek -120" },
493 { { AR_MENU, 0 }, "osd" },
494 { { AR_MENU_HOLD, 0 }, "mute" },
495 { { AR_VUP, 0 }, "volume 1"},
496 { { AR_VDOWN, 0 }, "volume -1"},
497 #endif
498 { { 'T', 0 }, "vo_ontop" },
499 { { 'f', 0 }, "vo_fullscreen" },
500 { { 's', 0 }, "screenshot 0" },
501 { { 'S', 0 }, "screenshot 1" },
502 { { 'w', 0 }, "panscan -0.1" },
503 { { 'e', 0 }, "panscan +0.1" },
505 { { KEY_POWER, 0 }, "quit" },
506 { { KEY_MENU, 0 }, "osd" },
507 { { KEY_PLAY, 0 }, "pause" },
508 { { KEY_PAUSE, 0 }, "pause" },
509 { { KEY_PLAYPAUSE, 0 }, "pause" },
510 { { KEY_STOP, 0 }, "quit" },
511 { { KEY_FORWARD, 0 }, "seek 60" },
512 { { KEY_REWIND, 0 }, "seek -60" },
513 { { KEY_NEXT, 0 }, "pt_step 1" },
514 { { KEY_PREV, 0 }, "pt_step -1" },
515 { { KEY_VOLUME_UP, 0 }, "volume 1" },
516 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
517 { { KEY_MUTE, 0 }, "mute" },
519 { { KEY_CLOSE_WIN, 0 }, "quit" },
521 { { '!', 0 }, "seek_chapter -1" },
522 { { '@', 0 }, "seek_chapter 1" },
523 { { 'A', 0 }, "switch_angle 1" },
524 { { 'U', 0 }, "stop" },
526 { { 0 }, NULL }
530 #ifndef MP_MAX_KEY_FD
531 #define MP_MAX_KEY_FD 10
532 #endif
534 #ifndef MP_MAX_CMD_FD
535 #define MP_MAX_CMD_FD 10
536 #endif
538 #define CMD_QUEUE_SIZE 100
540 typedef struct mp_input_fd {
541 int fd;
542 union {
543 mp_key_func_t key;
544 mp_cmd_func_t cmd;
545 } read_func;
546 mp_close_func_t close_func;
547 void *ctx;
548 unsigned eof : 1;
549 unsigned drop : 1;
550 unsigned dead : 1;
551 unsigned got_cmd : 1;
552 unsigned no_select : 1;
553 // These fields are for the cmd fds.
554 char* buffer;
555 int pos,size;
556 } mp_input_fd_t;
558 typedef struct mp_cmd_filter mp_cmd_filter_t;
560 struct mp_cmd_filter {
561 mp_input_cmd_filter filter;
562 void* ctx;
563 mp_cmd_filter_t* next;
566 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
568 struct mp_cmd_bind_section {
569 mp_cmd_bind_t* cmd_binds;
570 char* section;
571 mp_cmd_bind_section_t* next;
574 struct input_ctx {
575 // Autorepeat stuff
576 short ar_state;
577 mp_cmd_t *ar_cmd;
578 unsigned int last_ar;
579 // Autorepeat config
580 unsigned int ar_delay;
581 unsigned int ar_rate;
583 // these are the keys currently down
584 int key_down[MP_MAX_KEY_DOWN];
585 unsigned int num_key_down;
586 unsigned int last_key_down;
588 bool default_bindings;
589 // List of command binding sections
590 mp_cmd_bind_section_t *cmd_bind_sections;
591 // Name of currently used command section
592 char *section;
593 // The command binds of current section
594 mp_cmd_bind_t *cmd_binds;
595 mp_cmd_bind_t *cmd_binds_default;
597 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
598 unsigned int num_key_fd;
600 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
601 unsigned int num_cmd_fd;
603 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
604 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
608 static mp_cmd_filter_t* cmd_filters = NULL;
610 // Callback to allow the menu filter to grab the incoming keys
611 int (*mp_input_key_cb)(int code) = NULL;
613 int async_quit_request;
615 static int print_key_list(m_option_t* cfg);
616 static int print_cmd_list(m_option_t* cfg);
618 // Our command line options
619 static const m_option_t input_conf[] = {
620 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
621 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
622 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
623 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
624 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
625 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
626 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
627 OPT_STRING("file", input.in_file, CONF_GLOBAL),
628 OPT_FLAG_ON("default-bindings", input.default_bindings, CONF_GLOBAL),
629 OPT_FLAG_OFF("nodefault-bindings", input.default_bindings, CONF_GLOBAL),
630 { NULL, NULL, 0, 0, 0, 0, NULL}
633 static const m_option_t mp_input_opts[] = {
634 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
635 OPT_FLAG_OFF("nojoystick", input.use_joystick, CONF_GLOBAL),
636 OPT_FLAG_ON("joystick", input.use_joystick, CONF_GLOBAL),
637 OPT_FLAG_OFF("nolirc", input.use_lirc, CONF_GLOBAL),
638 OPT_FLAG_ON("lirc", input.use_lirc, CONF_GLOBAL),
639 OPT_FLAG_OFF("nolircc", input.use_lircc, CONF_GLOBAL),
640 OPT_FLAG_ON("lircc", input.use_lircc, CONF_GLOBAL),
641 OPT_FLAG_OFF("noar", input.use_ar, CONF_GLOBAL),
642 OPT_FLAG_ON("ar", input.use_ar, CONF_GLOBAL),
643 { NULL, NULL, 0, 0, 0, 0, NULL}
646 static int default_cmd_func(int fd,char* buf, int l);
648 static char *get_key_name(int key, char buffer[12]);
651 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
652 mp_cmd_func_t read_func, mp_close_func_t close_func)
654 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
655 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",fd);
656 return 0;
658 if (select && fd < 0) {
659 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
660 return 0;
663 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
664 .fd = fd,
665 .read_func.cmd = read_func ? read_func : default_cmd_func,
666 .close_func = close_func,
667 .no_select = !select
669 ictx->num_cmd_fd++;
671 return 1;
674 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
676 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
677 unsigned int i;
679 for (i = 0; i < ictx->num_cmd_fd; i++) {
680 if(cmd_fds[i].fd == fd)
681 break;
683 if (i == ictx->num_cmd_fd)
684 return;
685 if(cmd_fds[i].close_func)
686 cmd_fds[i].close_func(cmd_fds[i].fd);
687 if(cmd_fds[i].buffer)
688 talloc_free(cmd_fds[i].buffer);
690 if (i + 1 < ictx->num_cmd_fd)
691 memmove(&cmd_fds[i], &cmd_fds[i+1],
692 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
693 ictx->num_cmd_fd--;
696 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
698 struct mp_input_fd *key_fds = ictx->key_fds;
699 unsigned int i;
701 for (i = 0; i < ictx->num_key_fd; i++) {
702 if(key_fds[i].fd == fd)
703 break;
705 if (i == ictx->num_key_fd)
706 return;
707 if(key_fds[i].close_func)
708 key_fds[i].close_func(key_fds[i].fd);
710 if(i + 1 < ictx->num_key_fd)
711 memmove(&key_fds[i], &key_fds[i+1],
712 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
713 ictx->num_key_fd--;
716 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
717 mp_key_func_t read_func, mp_close_func_t close_func,
718 void *ctx)
720 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
721 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",fd);
722 return 0;
724 if (select && fd < 0) {
725 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
726 return 0;
729 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
730 .fd = fd,
731 .read_func.key = read_func,
732 .close_func = close_func,
733 .no_select = !select,
734 .ctx = ctx,
736 ictx->num_key_fd++;
738 return 1;
741 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
743 int cmd_num = 0;
745 while (*str == '\n' || *str == '\r' || *str == ' ')
746 ++str;
747 while (*str) {
748 mp_cmd_t *cmd;
749 size_t len = strcspn(str, "\r\n");
750 char *cmdbuf = talloc_size(NULL, len+1);
751 av_strlcpy(cmdbuf, str, len+1);
752 cmd = mp_input_parse_cmd(cmdbuf);
753 if (cmd) {
754 mp_input_queue_cmd(ictx, cmd);
755 ++cmd_num;
757 str += len;
758 while (*str == '\n' || *str == '\r' || *str == ' ')
759 ++str;
760 talloc_free(cmdbuf);
762 return cmd_num;
765 mp_cmd_t*
766 mp_input_parse_cmd(char* str) {
767 int i,l;
768 int pausing = 0;
769 char *ptr,*e;
770 const mp_cmd_t *cmd_def;
772 #ifdef MP_DEBUG
773 assert(str != NULL);
774 #endif
776 // Ignore heading spaces.
777 while (str[0] == ' ' || str[0] == '\t')
778 ++str;
780 if (strncmp(str, "pausing ", 8) == 0) {
781 pausing = 1;
782 str = &str[8];
783 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
784 pausing = 2;
785 str = &str[13];
786 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
787 pausing = 3;
788 str = &str[15];
789 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
790 pausing = 4;
791 str = &str[19];
794 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
795 /* NOTHING */;
796 if(ptr[0] != '\0')
797 l = ptr-str;
798 else
799 l = strlen(str);
801 if(l == 0)
802 return NULL;
804 for(i=0; mp_cmds[i].name != NULL; i++) {
805 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
806 break;
809 if(mp_cmds[i].name == NULL)
810 return NULL;
812 cmd_def = &mp_cmds[i];
814 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
815 *cmd = (mp_cmd_t){
816 .id = cmd_def->id,
817 .name = talloc_strdup(cmd, cmd_def->name),
818 .pausing = pausing,
821 ptr = str;
823 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
824 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
825 if(ptr[0] == '\0') break;
826 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
827 if(ptr[0] == '\0' || ptr[0] == '#') break;
828 cmd->args[i].type = cmd_def->args[i].type;
829 switch(cmd_def->args[i].type) {
830 case MP_CMD_ARG_INT:
831 errno = 0;
832 cmd->args[i].v.i = atoi(ptr);
833 if(errno != 0) {
834 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
835 ptr = NULL;
837 break;
838 case MP_CMD_ARG_FLOAT:
839 errno = 0;
840 cmd->args[i].v.f = atof(ptr);
841 if(errno != 0) {
842 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
843 ptr = NULL;
845 break;
846 case MP_CMD_ARG_STRING: {
847 char term;
848 char* ptr2 = ptr, *start;
850 if(ptr[0] == '\'' || ptr[0] == '"') {
851 term = ptr[0];
852 ptr2++;
853 } else
854 term = ' ';
855 start = ptr2;
856 while(1) {
857 e = strchr(ptr2,term);
858 if(!e) break;
859 if(e <= ptr2 || *(e - 1) != '\\') break;
860 ptr2 = e + 1;
863 if(term != ' ' && (!e || e[0] == '\0')) {
864 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
865 ptr = NULL;
866 break;
867 } else if(!e) e = ptr+strlen(ptr);
868 l = e-start;
869 ptr2 = start;
870 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
871 memmove(e,e+1,strlen(e));
872 ptr2 = e + 1;
873 l--;
875 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
876 if(term != ' ') ptr += l+2;
877 } break;
878 case -1:
879 ptr = NULL;
880 break;
881 default :
882 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
885 cmd->nargs = i;
887 if(cmd_def->nargs > cmd->nargs) {
888 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
889 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);
890 mp_cmd_free(cmd);
891 return NULL;
894 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
895 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
896 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
897 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
900 if(i < MP_CMD_MAX_ARGS)
901 cmd->args[i].type = -1;
903 return cmd;
906 #define MP_CMD_MAX_SIZE 4096
908 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
910 char* end;
911 (*ret) = NULL;
913 // Allocate the buffer if it doesn't exist
914 if(!mp_fd->buffer) {
915 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
916 mp_fd->pos = 0;
917 mp_fd->size = MP_CMD_MAX_SIZE;
920 // Get some data if needed/possible
921 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
922 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
923 mp_fd->size - 1 - mp_fd->pos);
924 // Error ?
925 if(r < 0) {
926 switch(r) {
927 case MP_INPUT_ERROR:
928 case MP_INPUT_DEAD:
929 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",mp_fd->fd,strerror(errno));
930 case MP_INPUT_NOTHING:
931 return r;
932 case MP_INPUT_RETRY:
933 continue;
935 // EOF ?
936 } else if(r == 0) {
937 mp_fd->eof = 1;
938 break;
940 mp_fd->pos += r;
941 break;
944 mp_fd->got_cmd = 0;
946 while(1) {
947 int l = 0;
948 // Find the cmd end
949 mp_fd->buffer[mp_fd->pos] = '\0';
950 end = strchr(mp_fd->buffer,'\n');
951 // No cmd end ?
952 if(!end) {
953 // If buffer is full we must drop all until the next \n
954 if(mp_fd->size - mp_fd->pos <= 1) {
955 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd);
956 mp_fd->pos = 0;
957 mp_fd->drop = 1;
959 break;
961 // We already have a cmd : set the got_cmd flag
962 else if((*ret)) {
963 mp_fd->got_cmd = 1;
964 break;
967 l = end - mp_fd->buffer;
969 // Not dropping : put the cmd in ret
970 if (!mp_fd->drop)
971 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
972 else
973 mp_fd->drop = 0;
974 mp_fd->pos -= l+1;
975 memmove(mp_fd->buffer, end+1, mp_fd->pos);
978 if(*ret)
979 return 1;
980 else
981 return MP_INPUT_NOTHING;
984 static int default_cmd_func(int fd,char* buf, int l)
986 while(1) {
987 int r = read(fd,buf,l);
988 // Error ?
989 if(r < 0) {
990 if(errno == EINTR)
991 continue;
992 else if(errno == EAGAIN)
993 return MP_INPUT_NOTHING;
994 return MP_INPUT_ERROR;
995 // EOF ?
997 return r;
1003 void
1004 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
1005 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1007 filter->filter = func;
1008 filter->ctx = ctx;
1009 filter->next = cmd_filters;
1010 cmd_filters = filter;
1014 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
1016 int j;
1018 if (n <= 0) return NULL;
1019 for(j = 0; binds[j].cmd != NULL; j++) {
1020 int found = 1,s;
1021 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1022 if(binds[j].input[s] != keys[s]) {
1023 found = 0;
1024 break;
1027 if(found && binds[j].input[s] == 0 && s == n)
1028 break;
1030 return binds[j].cmd;
1033 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1034 char *section)
1036 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1038 if (section==NULL) section="default";
1039 while (bind_section) {
1040 if(strcmp(section,bind_section->section)==0) return bind_section;
1041 if(bind_section->next==NULL) break;
1042 bind_section=bind_section->next;
1044 if(bind_section) {
1045 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1046 bind_section=bind_section->next;
1047 } else {
1048 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1049 bind_section = ictx->cmd_bind_sections;
1051 bind_section->cmd_binds=NULL;
1052 bind_section->section = talloc_strdup(bind_section, section);
1053 bind_section->next=NULL;
1054 return bind_section;
1057 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys,
1058 int paused)
1060 char* cmd = NULL;
1061 mp_cmd_t* ret;
1062 char key_buf[12];
1064 if (ictx->cmd_binds)
1065 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1066 if (ictx->cmd_binds_default && cmd == NULL)
1067 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1068 if (ictx->default_bindings && cmd == NULL)
1069 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1071 if(cmd == NULL) {
1072 mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", get_key_name(keys[0],
1073 key_buf));
1074 if(n > 1) {
1075 int s;
1076 for(s=1; s < n; s++)
1077 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", get_key_name(keys[s], key_buf));
1079 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1080 return NULL;
1082 if (strcmp(cmd, "ignore") == 0) return NULL;
1083 ret = mp_input_parse_cmd(cmd);
1084 if(!ret) {
1085 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s",
1086 get_key_name(ictx->key_down[0], key_buf));
1087 if (ictx->num_key_down > 1) {
1088 unsigned int s;
1089 for(s=1; s < ictx->num_key_down; s++)
1090 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(ictx->key_down[s],
1091 key_buf));
1093 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1095 return ret;
1099 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1101 unsigned int j;
1102 mp_cmd_t* ret;
1104 if(mp_input_key_cb) {
1105 if (code & MP_KEY_DOWN)
1106 return NULL;
1107 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1108 if (mp_input_key_cb(code))
1109 return NULL;
1112 if(code & MP_KEY_DOWN) {
1113 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1114 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1115 return NULL;
1117 code &= ~MP_KEY_DOWN;
1118 // Check if we don't already have this key as pushed
1119 for (j = 0; j < ictx->num_key_down; j++) {
1120 if (ictx->key_down[j] == code)
1121 break;
1123 if (j != ictx->num_key_down)
1124 return NULL;
1125 ictx->key_down[ictx->num_key_down] = code;
1126 ictx->num_key_down++;
1127 ictx->last_key_down = GetTimer();
1128 ictx->ar_state = 0;
1129 return NULL;
1131 // key released
1132 // Check if the key is in the down key, driver which can't send push event
1133 // send only release event
1134 for (j = 0; j < ictx->num_key_down; j++) {
1135 if (ictx->key_down[j] == code)
1136 break;
1138 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1139 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1140 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1141 return NULL;
1143 ictx->key_down[ictx->num_key_down] = code;
1144 ictx->num_key_down++;
1145 ictx->last_key_down = 1;
1147 // We ignore key from last combination
1148 ret = ictx->last_key_down ?
1149 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down, paused)
1150 : NULL;
1151 // Remove the key
1152 if (j+1 < ictx->num_key_down)
1153 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1154 (ictx->num_key_down-(j+1))*sizeof(int));
1155 ictx->num_key_down--;
1156 ictx->last_key_down = 0;
1157 ictx->ar_state = -1;
1158 if (ictx->ar_cmd) {
1159 mp_cmd_free(ictx->ar_cmd);
1160 ictx->ar_cmd = NULL;
1162 return ret;
1165 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1167 // No input : autorepeat ?
1168 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1169 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1170 unsigned int t = GetTimer();
1171 // First time : wait delay
1172 if (ictx->ar_state == 0
1173 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1174 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1175 ictx->key_down, paused);
1176 if (!ictx->ar_cmd) {
1177 ictx->ar_state = -1;
1178 return NULL;
1180 ictx->ar_state = 1;
1181 ictx->last_ar = t;
1182 return mp_cmd_clone(ictx->ar_cmd);
1183 // Then send rate / sec event
1184 } else if (ictx->ar_state == 1
1185 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1186 ictx->last_ar = t;
1187 return mp_cmd_clone(ictx->ar_cmd);
1190 return NULL;
1194 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1196 int i;
1197 int got_cmd = 0;
1198 struct mp_input_fd *key_fds = ictx->key_fds;
1199 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1200 for (i = 0; i < ictx->num_key_fd; i++)
1201 if (key_fds[i].dead) {
1202 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1203 i--;
1205 for (i = 0; i < ictx->num_cmd_fd; i++)
1206 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1207 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1208 i--;
1210 else if (cmd_fds[i].got_cmd)
1211 got_cmd = 1;
1212 #ifdef HAVE_POSIX_SELECT
1213 fd_set fds;
1214 FD_ZERO(&fds);
1215 if (!got_cmd) {
1216 int max_fd = 0, num_fd = 0;
1217 for (i = 0; i < ictx->num_key_fd; i++) {
1218 if (key_fds[i].no_select)
1219 continue;
1220 if (key_fds[i].fd > max_fd)
1221 max_fd = key_fds[i].fd;
1222 FD_SET(key_fds[i].fd, &fds);
1223 num_fd++;
1225 for (i = 0; i < ictx->num_cmd_fd; i++) {
1226 if (cmd_fds[i].no_select)
1227 continue;
1228 if (cmd_fds[i].fd > max_fd)
1229 max_fd = cmd_fds[i].fd;
1230 FD_SET(cmd_fds[i].fd, &fds);
1231 num_fd++;
1233 if (num_fd > 0) {
1234 struct timeval tv, *time_val;
1235 if (time >= 0) {
1236 tv.tv_sec = time / 1000;
1237 tv.tv_usec = (time % 1000) * 1000;
1238 time_val = &tv;
1240 else
1241 time_val = NULL;
1242 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1243 if (errno != EINTR)
1244 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1245 strerror(errno));
1246 FD_ZERO(&fds);
1250 #else
1251 if (!got_cmd && time)
1252 usec_sleep(time * 1000);
1253 #endif
1256 for (i = 0; i < ictx->num_key_fd; i++) {
1257 #ifdef HAVE_POSIX_SELECT
1258 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1259 continue;
1260 #endif
1262 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1263 if (code >= 0) {
1264 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1265 if (ret)
1266 return ret;
1268 else if (code == MP_INPUT_ERROR)
1269 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n",
1270 key_fds[i].fd);
1271 else if (code == MP_INPUT_DEAD) {
1272 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n",
1273 key_fds[i].fd);
1274 key_fds[i].dead = 1;
1277 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1278 if (autorepeat_cmd)
1279 return autorepeat_cmd;
1281 for (i = 0; i < ictx->num_cmd_fd; i++) {
1282 #ifdef HAVE_POSIX_SELECT
1283 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1284 !cmd_fds[i].got_cmd)
1285 continue;
1286 #endif
1287 char *cmd;
1288 int r = read_cmd(&cmd_fds[i], &cmd);
1289 if (r >= 0) {
1290 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1291 talloc_free(cmd);
1292 if (ret)
1293 return ret;
1295 else if (r == MP_INPUT_ERROR)
1296 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1297 cmd_fds[i].fd);
1298 else if (r == MP_INPUT_DEAD)
1299 cmd_fds[i].dead = 1;
1302 return NULL;
1306 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1308 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1309 return 0;
1310 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1311 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1312 ictx->cmd_queue_length++;
1313 return 1;
1316 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1318 mp_cmd_t* ret;
1320 if (ictx->cmd_queue_length == 0)
1321 return NULL;
1323 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1325 if (!peek_only) {
1326 ictx->cmd_queue_length--;
1327 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1330 return ret;
1334 * \param peek_only when set, the returned command stays in the queue.
1335 * Do not free the returned cmd whe you set this!
1337 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1338 int peek_only)
1340 mp_cmd_t* ret = NULL;
1341 mp_cmd_filter_t* cf;
1342 int from_queue;
1344 if (async_quit_request)
1345 return mp_input_parse_cmd("quit 1");
1346 while(1) {
1347 from_queue = 1;
1348 ret = get_queued_cmd(ictx, peek_only);
1349 if(ret) break;
1350 from_queue = 0;
1351 ret = read_events(ictx, time, paused);
1352 if (!ret) {
1353 from_queue = 1;
1354 ret = get_queued_cmd(ictx, peek_only);
1356 break;
1358 if(!ret) return NULL;
1360 for(cf = cmd_filters ; cf ; cf = cf->next) {
1361 if(cf->filter(ret,paused,cf->ctx)) {
1362 if (peek_only && from_queue)
1363 // The filter ate the cmd, so we remove it from queue
1364 ret = get_queued_cmd(ictx, 0);
1365 mp_cmd_free(ret);
1366 return NULL;
1370 if (!from_queue && peek_only)
1371 mp_input_queue_cmd(ictx, ret);
1373 return ret;
1376 void
1377 mp_cmd_free(mp_cmd_t* cmd) {
1378 talloc_free(cmd);
1381 mp_cmd_t*
1382 mp_cmd_clone(mp_cmd_t* cmd) {
1383 mp_cmd_t* ret;
1384 int i;
1385 #ifdef MP_DEBUG
1386 assert(cmd != NULL);
1387 #endif
1389 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1390 ret->name = talloc_strdup(ret, cmd->name);
1391 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1392 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1393 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1396 return ret;
1399 static char *get_key_name(int key, char buffer[12])
1401 int i;
1403 for(i = 0; key_names[i].name != NULL; i++) {
1404 if(key_names[i].key == key)
1405 return key_names[i].name;
1408 if(isascii(key)) {
1409 snprintf(buffer, 12, "%c",(char)key);
1410 return buffer;
1413 // Print the hex key code
1414 snprintf(buffer, 12, "%#-8x",key);
1415 return buffer;
1420 mp_input_get_key_from_name(const char *name) {
1421 int i,ret = 0,len = strlen(name);
1422 if(len == 1) { // Direct key code
1423 ret = (unsigned char)name[0];
1424 return ret;
1425 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1426 return strtol(name,NULL,16);
1428 for(i = 0; key_names[i].name != NULL; i++) {
1429 if(strcasecmp(key_names[i].name,name) == 0)
1430 return key_names[i].key;
1433 return -1;
1436 static int get_input_from_name(char* name,int* keys) {
1437 char *end,*ptr;
1438 int n=0;
1440 ptr = name;
1441 n = 0;
1442 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1443 if(end && end[1] != '\0') {
1444 if(end[1] == '-')
1445 end = &end[1];
1446 end[0] = '\0';
1448 keys[n] = mp_input_get_key_from_name(ptr);
1449 if(keys[n] < 0) {
1450 return 0;
1452 n++;
1453 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1454 ptr = &end[1];
1455 else
1456 break;
1458 keys[n] = 0;
1459 return 1;
1462 #define BS_MAX 256
1463 #define SPACE_CHAR " \n\r\t"
1465 static void bind_keys(struct input_ctx *ictx,
1466 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1468 int i = 0,j;
1469 mp_cmd_bind_t* bind = NULL;
1470 mp_cmd_bind_section_t* bind_section = NULL;
1471 char *section=NULL, *p;
1473 #ifdef MP_DEBUG
1474 assert(keys != NULL);
1475 assert(cmd != NULL);
1476 #endif
1478 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1479 *p=0;
1480 section=++cmd;
1481 cmd=++p;
1482 // Jump beginning space
1483 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1484 /* NOTHING */;
1486 bind_section = get_bind_section(ictx, section);
1488 if(bind_section->cmd_binds) {
1489 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1490 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1491 /* NOTHING */;
1492 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1493 bind = &bind_section->cmd_binds[i];
1494 break;
1499 if(!bind) {
1500 bind_section->cmd_binds = talloc_realloc(bind_section,
1501 bind_section->cmd_binds,
1502 mp_cmd_bind_t, i + 2);
1503 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1504 bind = &bind_section->cmd_binds[i];
1506 if(bind->cmd)
1507 talloc_free(bind->cmd);
1508 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1509 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1512 static void add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1514 int i;
1515 for(i = 0 ; list[i].cmd ; i++)
1516 bind_keys(ictx, list[i].input,list[i].cmd);
1519 static int parse_config(struct input_ctx *ictx, char *file)
1521 int fd;
1522 int bs = 0,r,eof = 0,comments = 0;
1523 char *iter,*end;
1524 char buffer[BS_MAX];
1525 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1527 fd = open(file,O_RDONLY);
1529 if(fd < 0) {
1530 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1531 return 0;
1534 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1536 while(1) {
1537 if(! eof && bs < BS_MAX-1) {
1538 if(bs > 0) bs--;
1539 r = read(fd,buffer+bs,BS_MAX-1-bs);
1540 if(r < 0) {
1541 if(errno == EINTR)
1542 continue;
1543 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1544 close(fd);
1545 return 0;
1546 } else if(r == 0) {
1547 eof = 1;
1548 } else {
1549 bs += r+1;
1550 buffer[bs-1] = '\0';
1553 // Empty buffer : return
1554 if(bs <= 1) {
1555 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1556 close(fd);
1557 return 1;
1560 iter = buffer;
1562 if(comments) {
1563 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1564 /* NOTHING */;
1565 if(iter[0] == '\0') { // Buffer was full of comment
1566 bs = 0;
1567 continue;
1569 iter++;
1570 r = strlen(iter);
1571 memmove(buffer,iter,r+1);
1572 bs = r+1;
1573 comments = 0;
1574 continue;
1577 // Find the wanted key
1578 if(keys[0] == 0) {
1579 // Jump beginning space
1580 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1581 /* NOTHING */;
1582 if(iter[0] == '\0') { // Buffer was full of space char
1583 bs = 0;
1584 continue;
1586 if(iter[0] == '#') { // Comments
1587 comments = 1;
1588 continue;
1590 // Find the end of the key code name
1591 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1592 /*NOTHING */;
1593 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1594 if(buffer == iter) {
1595 if(eof && (buffer-iter) == bs)
1596 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1597 else
1598 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1599 return 0;
1601 memmove(buffer,iter,end-iter);
1602 bs = end-iter;
1603 continue;
1606 char name[end-iter+1];
1607 strncpy(name,iter,end-iter);
1608 name[end-iter] = '\0';
1609 if (!get_input_from_name(name,keys)) {
1610 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1611 close(fd);
1612 return 0;
1615 if( bs > (end-buffer))
1616 memmove(buffer,end,bs - (end-buffer));
1617 bs -= end-buffer;
1618 continue;
1619 } else { // Get the command
1620 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1621 // Found new line
1622 if(iter[0] == '\n' || iter[0] == '\r') {
1623 int i;
1624 char key_buf[12];
1625 mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", get_key_name(keys[0], key_buf));
1626 for(i = 1; keys[i] != 0 ; i++)
1627 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(keys[i], key_buf));
1628 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1629 keys[0] = 0;
1630 if(iter > buffer) {
1631 memmove(buffer,iter,bs- (iter-buffer));
1632 bs -= (iter-buffer);
1634 continue;
1636 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1637 /* NOTHING */;
1638 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1639 if(iter == buffer) {
1640 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1641 close(fd);
1642 return 0;
1644 memmove(buffer,iter,end - iter);
1645 bs = end - iter;
1646 continue;
1649 char cmd[end-iter+1];
1650 strncpy(cmd,iter,end-iter);
1651 cmd[end-iter] = '\0';
1652 //printf("Set bind %d => %s\n",keys[0],cmd);
1653 bind_keys(ictx, keys,cmd);
1654 n_binds++;
1656 keys[0] = 0;
1657 end++;
1658 if(bs > (end-buffer))
1659 memmove(buffer,end,bs-(end-buffer));
1660 bs -= (end-buffer);
1661 buffer[bs-1] = '\0';
1662 continue;
1665 mp_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1666 close(fd);
1667 mp_input_set_section(ictx, NULL);
1668 return 0;
1671 void mp_input_set_section(struct input_ctx *ictx, char *name)
1673 mp_cmd_bind_section_t* bind_section = NULL;
1675 ictx->cmd_binds = NULL;
1676 ictx->cmd_binds_default = NULL;
1677 if (ictx->section)
1678 talloc_free(ictx->section);
1679 if (name)
1680 ictx->section = talloc_strdup(ictx, name);
1681 else
1682 ictx->section = talloc_strdup(ictx, "default");
1683 if ((bind_section = get_bind_section(ictx, ictx->section)))
1684 ictx->cmd_binds = bind_section->cmd_binds;
1685 if (strcmp(ictx->section, "default") == 0)
1686 return;
1687 if ((bind_section = get_bind_section(ictx, NULL)))
1688 ictx->cmd_binds_default = bind_section->cmd_binds;
1691 char *mp_input_get_section(struct input_ctx *ictx)
1693 return ictx->section;
1696 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1698 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1699 *ictx = (struct input_ctx){
1700 .ar_state = -1,
1701 .ar_delay = input_conf->ar_delay,
1702 .ar_rate = input_conf->ar_rate,
1703 .default_bindings = input_conf->default_bindings,
1706 char* file;
1707 char *config_file = input_conf->config_file;
1708 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1709 if(!file)
1710 return ictx;
1712 if (!parse_config(ictx, file)) {
1713 // free file if it was allocated by get_path(),
1714 // before it gets overwritten
1715 if( file != config_file)
1717 free(file);
1719 // Try global conf dir
1720 file = MPLAYER_CONFDIR "/input.conf";
1721 if (!parse_config(ictx, file))
1722 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1724 else
1726 // free file if it was allocated by get_path()
1727 if( file != config_file)
1728 free(file);
1731 #ifdef CONFIG_JOYSTICK
1732 if (input_conf->use_joystick) {
1733 int fd = mp_input_joystick_init(input_conf->js_dev);
1734 if(fd < 0)
1735 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1736 else
1737 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1738 (mp_close_func_t)close,NULL);
1740 #endif
1742 #ifdef CONFIG_LIRC
1743 if (input_conf->use_lirc) {
1744 int fd = mp_input_lirc_init();
1745 if(fd > 0)
1746 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1747 mp_input_lirc_close);
1749 #endif
1751 #ifdef CONFIG_LIRCC
1752 if (input_conf->use_lircc) {
1753 int fd = lircc_init("mplayer", NULL);
1754 if(fd >= 0)
1755 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1757 #endif
1759 #ifdef CONFIG_APPLE_REMOTE
1760 if (input_conf->use_ar) {
1761 if(mp_input_ar_init() < 0)
1762 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1763 else
1764 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1765 NULL);
1767 #endif
1769 #ifdef CONFIG_APPLE_IR
1770 if (input_conf->use_ar) {
1771 int fd = mp_input_appleir_init(input_conf->ar_dev);
1772 if(fd < 0)
1773 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1774 else
1775 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1776 (mp_close_func_t)close, NULL);
1778 #endif
1780 if (input_conf->in_file) {
1781 struct stat st;
1782 if (stat(input_conf->in_file, &st))
1783 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't stat %s: %s\n", input_conf->in_file, strerror(errno));
1784 else {
1785 int in_file_fd = open(input_conf->in_file,
1786 S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1787 if(in_file_fd >= 0)
1788 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1789 (mp_close_func_t)close);
1790 else
1791 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1792 input_conf->in_file, strerror(errno));
1795 return ictx;
1798 void mp_input_uninit(struct input_ctx *ictx)
1800 if (!ictx)
1801 return;
1803 unsigned int i;
1805 for (i=0; i < ictx->num_key_fd; i++) {
1806 if (ictx->key_fds[i].close_func)
1807 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1810 for (i = 0; i < ictx->num_cmd_fd; i++) {
1811 if (ictx->cmd_fds[i].close_func)
1812 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1814 talloc_free(ictx);
1817 void
1818 mp_input_register_options(m_config_t* cfg) {
1819 m_config_register_options(cfg,mp_input_opts);
1822 static int print_key_list(m_option_t* cfg)
1824 int i;
1825 printf("\n");
1826 for(i= 0; key_names[i].name != NULL ; i++)
1827 printf("%s\n",key_names[i].name);
1828 exit(0);
1831 static int print_cmd_list(m_option_t* cfg)
1833 const mp_cmd_t *cmd;
1834 int i,j;
1835 const char* type;
1837 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1838 printf("%-20.20s",cmd->name);
1839 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1840 switch(cmd->args[j].type) {
1841 case MP_CMD_ARG_INT:
1842 type = "Integer";
1843 break;
1844 case MP_CMD_ARG_FLOAT:
1845 type = "Float";
1846 break;
1847 case MP_CMD_ARG_STRING:
1848 type = "String";
1849 break;
1850 default:
1851 type = "??";
1853 if(j+1 > cmd->nargs)
1854 printf(" [%s]",type);
1855 else
1856 printf(" %s",type);
1858 printf("\n");
1860 exit(0);
1863 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1865 mp_cmd_t* cmd;
1866 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1867 return 0;
1868 switch(cmd->id) {
1869 case MP_CMD_QUIT:
1870 case MP_CMD_PLAY_TREE_STEP:
1871 case MP_CMD_PLAY_TREE_UP_STEP:
1872 case MP_CMD_PLAY_ALT_SRC_STEP:
1873 // The cmd will be executed when we are back in the main loop
1874 return 1;
1876 // remove the cmd from the queue
1877 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1878 mp_cmd_free(cmd);
1879 return 0;