Merge svn changes up to r28712
[mplayer/glamo.git] / input / input.c
blob1f1d4a6ebf43ad7435b64668f758ea271f59510d
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 <unistd.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #include <ctype.h>
32 #include "input.h"
33 #include "mouse.h"
34 #ifdef MP_DEBUG
35 #include <assert.h>
36 #endif
37 #include "mp_fifo.h"
38 #include "osdep/keycodes.h"
39 #include "osdep/timer.h"
40 #include "libavutil/avstring.h"
41 #include "mp_msg.h"
42 #include "help_mp.h"
43 #include "m_config.h"
44 #include "m_option.h"
45 #include "get_path.h"
46 #include "talloc.h"
47 #include "options.h"
49 #include "joystick.h"
51 #ifdef CONFIG_LIRC
52 #include "lirc.h"
53 #endif
55 #ifdef CONFIG_LIRCC
56 #include <lirc/lircc.h>
57 #endif
59 #include "ar.h"
61 typedef struct mp_cmd_bind {
62 int input[MP_MAX_KEY_DOWN+1];
63 char* cmd;
64 } mp_cmd_bind_t;
66 typedef struct mp_key_name {
67 int key;
68 char* name;
69 } mp_key_name_t;
71 /// This array defines all known commands.
72 /// The first field is an id used to recognize the command without too many strcmp.
73 /// The second is obviously the command name.
74 /// The third is the minimum number of arguments this command needs.
75 /// Then comes the definition of each argument, terminated with an arg of type -1.
76 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
77 /// the last one) which is actually 9.
79 /// For the args, the first field is the type (actually int, float or string), the second
80 /// is the default value wich is used for optional arguments
82 static const mp_cmd_t mp_cmds[] = {
83 #ifdef CONFIG_RADIO
84 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
85 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
87 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 #endif
89 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
90 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
91 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
96 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
97 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
98 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
99 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
102 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
106 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
109 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
111 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
112 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
113 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
118 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
119 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
120 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
121 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
122 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
123 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
124 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
125 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
126 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
130 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
131 #ifdef CONFIG_ASS
132 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
133 #endif
134 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
135 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
136 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
137 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
138 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
139 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
141 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
142 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
144 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
145 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
151 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
152 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
153 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 #ifdef CONFIG_TV
155 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
156 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
157 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
158 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
159 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
160 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
161 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
162 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
165 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
166 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 #endif
169 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
170 #ifdef CONFIG_DVBIN
171 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
172 #endif
173 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
174 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
175 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
176 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
177 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
178 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
180 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
181 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
185 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
186 #ifdef CONFIG_TV_TELETEXT
187 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
188 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
189 #endif
191 #ifdef CONFIG_GUI
192 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
193 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
194 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
195 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
196 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
197 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
198 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
199 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
200 #endif
202 #ifdef CONFIG_DVDNAV
203 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
204 #endif
206 #ifdef CONFIG_MENU
207 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
209 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
210 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
211 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
212 #endif
214 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
215 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
216 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
217 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
218 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
219 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
221 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
222 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
224 { 0, NULL, 0, {} }
227 /// The names of the keys as used in input.conf
228 /// If you add some new keys, you also need to add them here
230 static const mp_key_name_t key_names[] = {
231 { ' ', "SPACE" },
232 { '#', "SHARP" },
233 { KEY_ENTER, "ENTER" },
234 { KEY_TAB, "TAB" },
235 { KEY_CTRL, "CTRL" },
236 { KEY_BACKSPACE, "BS" },
237 { KEY_DELETE, "DEL" },
238 { KEY_INSERT, "INS" },
239 { KEY_HOME, "HOME" },
240 { KEY_END, "END" },
241 { KEY_PAGE_UP, "PGUP" },
242 { KEY_PAGE_DOWN, "PGDWN" },
243 { KEY_ESC, "ESC" },
244 { KEY_RIGHT, "RIGHT" },
245 { KEY_LEFT, "LEFT" },
246 { KEY_DOWN, "DOWN" },
247 { KEY_UP, "UP" },
248 { KEY_F+1, "F1" },
249 { KEY_F+2, "F2" },
250 { KEY_F+3, "F3" },
251 { KEY_F+4, "F4" },
252 { KEY_F+5, "F5" },
253 { KEY_F+6, "F6" },
254 { KEY_F+7, "F7" },
255 { KEY_F+8, "F8" },
256 { KEY_F+9, "F9" },
257 { KEY_F+10, "F10" },
258 { KEY_F+11, "F11" },
259 { KEY_F+12, "F12" },
260 { KEY_KP0, "KP0" },
261 { KEY_KP1, "KP1" },
262 { KEY_KP2, "KP2" },
263 { KEY_KP3, "KP3" },
264 { KEY_KP4, "KP4" },
265 { KEY_KP5, "KP5" },
266 { KEY_KP6, "KP6" },
267 { KEY_KP7, "KP7" },
268 { KEY_KP8, "KP8" },
269 { KEY_KP9, "KP9" },
270 { KEY_KPDEL, "KP_DEL" },
271 { KEY_KPDEC, "KP_DEC" },
272 { KEY_KPINS, "KP_INS" },
273 { KEY_KPENTER, "KP_ENTER" },
274 { MOUSE_BTN0, "MOUSE_BTN0" },
275 { MOUSE_BTN1, "MOUSE_BTN1" },
276 { MOUSE_BTN2, "MOUSE_BTN2" },
277 { MOUSE_BTN3, "MOUSE_BTN3" },
278 { MOUSE_BTN4, "MOUSE_BTN4" },
279 { MOUSE_BTN5, "MOUSE_BTN5" },
280 { MOUSE_BTN6, "MOUSE_BTN6" },
281 { MOUSE_BTN7, "MOUSE_BTN7" },
282 { MOUSE_BTN8, "MOUSE_BTN8" },
283 { MOUSE_BTN9, "MOUSE_BTN9" },
284 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
285 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
286 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
287 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
288 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
289 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
290 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
291 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
292 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
293 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
294 { JOY_AXIS1_MINUS, "JOY_UP" },
295 { JOY_AXIS1_PLUS, "JOY_DOWN" },
296 { JOY_AXIS0_MINUS, "JOY_LEFT" },
297 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
299 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
300 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
301 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
302 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
303 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
304 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
305 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
306 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
307 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
308 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
309 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
310 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
311 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
312 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
313 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
314 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
315 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
316 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
317 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
318 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
320 { JOY_BTN0, "JOY_BTN0" },
321 { JOY_BTN1, "JOY_BTN1" },
322 { JOY_BTN2, "JOY_BTN2" },
323 { JOY_BTN3, "JOY_BTN3" },
324 { JOY_BTN4, "JOY_BTN4" },
325 { JOY_BTN5, "JOY_BTN5" },
326 { JOY_BTN6, "JOY_BTN6" },
327 { JOY_BTN7, "JOY_BTN7" },
328 { JOY_BTN8, "JOY_BTN8" },
329 { JOY_BTN9, "JOY_BTN9" },
331 { AR_PLAY, "AR_PLAY" },
332 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
333 { AR_NEXT, "AR_NEXT" },
334 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
335 { AR_PREV, "AR_PREV" },
336 { AR_PREV_HOLD, "AR_PREV_HOLD" },
337 { AR_MENU, "AR_MENU" },
338 { AR_MENU_HOLD, "AR_MENU_HOLD" },
339 { AR_VUP, "AR_VUP" },
340 { AR_VDOWN, "AR_VDOWN" },
342 { KEY_POWER, "POWER" },
343 { KEY_MENU, "MENU" },
344 { KEY_PLAY, "PLAY" },
345 { KEY_PAUSE, "PAUSE" },
346 { KEY_PLAYPAUSE, "PLAYPAUSE" },
347 { KEY_STOP, "STOP" },
348 { KEY_FORWARD, "FORWARD" },
349 { KEY_REWIND, "REWIND" },
350 { KEY_NEXT, "NEXT" },
351 { KEY_PREV, "PREV" },
352 { KEY_VOLUME_UP, "VOLUME_UP" },
353 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
354 { KEY_MUTE, "MUTE" },
356 // These are kept for backward compatibility
357 { KEY_PAUSE, "XF86_PAUSE" },
358 { KEY_STOP, "XF86_STOP" },
359 { KEY_PREV, "XF86_PREV" },
360 { KEY_NEXT, "XF86_NEXT" },
362 { KEY_CLOSE_WIN, "CLOSE_WIN" },
364 { 0, NULL }
367 // This is the default binding. The content of input.conf overrides these.
368 // The first arg is a null terminated array of key codes.
369 // The second is the command
371 static const mp_cmd_bind_t def_cmd_binds[] = {
373 { { MOUSE_BTN3, 0 }, "seek 10" },
374 { { MOUSE_BTN4, 0 }, "seek -10" },
375 { { MOUSE_BTN5, 0 }, "volume 1" },
376 { { MOUSE_BTN6, 0 }, "volume -1" },
378 #ifdef CONFIG_DVDNAV
379 { { KEY_KP8, 0 }, "dvdnav up" }, // up
380 { { KEY_KP2, 0 }, "dvdnav down" }, // down
381 { { KEY_KP4, 0 }, "dvdnav left" }, // left
382 { { KEY_KP6, 0 }, "dvdnav right" }, // right
383 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
384 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
385 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
386 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
387 #endif
389 { { KEY_RIGHT, 0 }, "seek 10" },
390 { { KEY_LEFT, 0 }, "seek -10" },
391 { { KEY_UP, 0 }, "seek 60" },
392 { { KEY_DOWN, 0 }, "seek -60" },
393 { { KEY_PAGE_UP, 0 }, "seek 600" },
394 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
395 { { '+', 0 }, "audio_delay 0.100" },
396 { { '-', 0 }, "audio_delay -0.100" },
397 { { '[', 0 }, "speed_mult 0.9091" },
398 { { ']', 0 }, "speed_mult 1.1" },
399 { { '{', 0 }, "speed_mult 0.5" },
400 { { '}', 0 }, "speed_mult 2.0" },
401 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
402 { { 'q', 0 }, "quit" },
403 { { KEY_ESC, 0 }, "quit" },
404 { { 'p', 0 }, "pause" },
405 { { ' ', 0 }, "pause" },
406 { { '.', 0 }, "frame_step" },
407 { { KEY_HOME, 0 }, "pt_up_step 1" },
408 { { KEY_END, 0 }, "pt_up_step -1" },
409 { { '>', 0 }, "pt_step 1" },
410 { { KEY_ENTER, 0 }, "pt_step 1 1" },
411 { { '<', 0 }, "pt_step -1" },
412 { { KEY_INS, 0 }, "alt_src_step 1" },
413 { { KEY_DEL, 0 }, "alt_src_step -1" },
414 { { 'o', 0 }, "osd" },
415 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
416 { { 'z', 0 }, "sub_delay -0.1" },
417 { { 'x', 0 }, "sub_delay +0.1" },
418 { { 'g', 0 }, "sub_step -1" },
419 { { 'y', 0 }, "sub_step +1" },
420 { { '9', 0 }, "volume -1" },
421 { { '/', 0 }, "volume -1" },
422 { { '0', 0 }, "volume 1" },
423 { { '*', 0 }, "volume 1" },
424 { { '(', 0 }, "balance -0.1" },
425 { { ')', 0 }, "balance 0.1" },
426 { { 'm', 0 }, "mute" },
427 { { '1', 0 }, "contrast -1" },
428 { { '2', 0 }, "contrast 1" },
429 { { '3', 0 }, "brightness -1" },
430 { { '4', 0 }, "brightness 1" },
431 { { '5', 0 }, "hue -1" },
432 { { '6', 0 }, "hue 1" },
433 { { '7', 0 }, "saturation -1" },
434 { { '8', 0 }, "saturation 1" },
435 { { 'd', 0 }, "frame_drop" },
436 { { 'D', 0 }, "step_property deinterlace" },
437 { { 'r', 0 }, "sub_pos -1" },
438 { { 't', 0 }, "sub_pos +1" },
439 { { 'a', 0 }, "sub_alignment" },
440 { { 'v', 0 }, "sub_visibility" },
441 { { 'j', 0 }, "sub_select" },
442 { { 'F', 0 }, "forced_subs_only" },
443 { { '#', 0 }, "switch_audio" },
444 { { '_', 0 }, "step_property switch_video" },
445 { { KEY_TAB, 0 }, "step_property switch_program" },
446 { { 'i', 0 }, "edl_mark" },
447 #ifdef CONFIG_TV
448 { { 'h', 0 }, "tv_step_channel 1" },
449 { { 'k', 0 }, "tv_step_channel -1" },
450 { { 'n', 0 }, "tv_step_norm" },
451 { { 'u', 0 }, "tv_step_chanlist" },
452 #endif
453 #ifdef CONFIG_TV_TELETEXT
454 { { 'X', 0 }, "step_property teletext_mode 1" },
455 { { 'W', 0 }, "step_property teletext_page 1" },
456 { { 'Q', 0 }, "step_property teletext_page -1" },
457 #endif
458 #ifdef CONFIG_JOYSTICK
459 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
460 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
461 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
462 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
463 { { JOY_BTN0, 0 }, "pause" },
464 { { JOY_BTN1, 0 }, "osd" },
465 { { JOY_BTN2, 0 }, "volume 1"},
466 { { JOY_BTN3, 0 }, "volume -1"},
467 #endif
468 #ifdef CONFIG_APPLE_REMOTE
469 { { AR_PLAY, 0}, "pause" },
470 { { AR_PLAY_HOLD, 0}, "quit" },
471 { { AR_NEXT, 0 }, "seek 30" },
472 { { AR_NEXT_HOLD, 0 }, "seek 120" },
473 { { AR_PREV, 0 }, "seek -10" },
474 { { AR_PREV_HOLD, 0 }, "seek -120" },
475 { { AR_MENU, 0 }, "osd" },
476 { { AR_MENU_HOLD, 0 }, "mute" },
477 { { AR_VUP, 0 }, "volume 1"},
478 { { AR_VDOWN, 0 }, "volume -1"},
479 #endif
480 { { 'T', 0 }, "vo_ontop" },
481 { { 'f', 0 }, "vo_fullscreen" },
482 { { 's', 0 }, "screenshot 0" },
483 { { 'S', 0 }, "screenshot 1" },
484 { { 'w', 0 }, "panscan -0.1" },
485 { { 'e', 0 }, "panscan +0.1" },
487 { { KEY_POWER, 0 }, "quit" },
488 { { KEY_MENU, 0 }, "osd" },
489 { { KEY_PLAY, 0 }, "pause" },
490 { { KEY_PAUSE, 0 }, "pause" },
491 { { KEY_PLAYPAUSE, 0 }, "pause" },
492 { { KEY_STOP, 0 }, "quit" },
493 { { KEY_FORWARD, 0 }, "seek 60" },
494 { { KEY_REWIND, 0 }, "seek -60" },
495 { { KEY_NEXT, 0 }, "pt_step 1" },
496 { { KEY_PREV, 0 }, "pt_step -1" },
497 { { KEY_VOLUME_UP, 0 }, "volume 1" },
498 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
499 { { KEY_MUTE, 0 }, "mute" },
501 { { KEY_CLOSE_WIN, 0 }, "quit" },
503 { { '!', 0 }, "seek_chapter -1" },
504 { { '@', 0 }, "seek_chapter 1" },
505 { { 'A', 0 }, "switch_angle 1" },
506 { { 'U', 0 }, "stop" },
508 { { 0 }, NULL }
512 #ifdef CONFIG_GUI
513 static const mp_cmd_bind_t gui_def_cmd_binds[] = {
515 { { 'l', 0 }, "gui_loadfile" },
516 { { 't', 0 }, "gui_loadsubtitle" },
517 { { KEY_ENTER, 0 }, "gui_play" },
518 { { KEY_ESC, 0 }, "gui_stop" },
519 { { 'p', 0 }, "gui_playlist" },
520 { { 'r', 0 }, "gui_preferences" },
521 { { 'c', 0 }, "gui_skinbrowser" },
523 { { 0 }, NULL }
525 #endif
527 #ifndef MP_MAX_KEY_FD
528 #define MP_MAX_KEY_FD 10
529 #endif
531 #ifndef MP_MAX_CMD_FD
532 #define MP_MAX_CMD_FD 10
533 #endif
535 #define CMD_QUEUE_SIZE 100
537 typedef struct mp_input_fd {
538 int fd;
539 union {
540 mp_key_func_t key;
541 mp_cmd_func_t cmd;
542 } read_func;
543 mp_close_func_t close_func;
544 void *ctx;
545 unsigned eof : 1;
546 unsigned drop : 1;
547 unsigned dead : 1;
548 unsigned got_cmd : 1;
549 unsigned no_select : 1;
550 // These fields are for the cmd fds.
551 char* buffer;
552 int pos,size;
553 } mp_input_fd_t;
555 typedef struct mp_cmd_filter mp_cmd_filter_t;
557 struct mp_cmd_filter {
558 mp_input_cmd_filter filter;
559 void* ctx;
560 mp_cmd_filter_t* next;
563 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
565 struct mp_cmd_bind_section {
566 mp_cmd_bind_t* cmd_binds;
567 char* section;
568 mp_cmd_bind_section_t* next;
571 struct input_ctx {
572 // Autorepeat stuff
573 short ar_state;
574 mp_cmd_t *ar_cmd;
575 unsigned int last_ar;
576 // Autorepeat config
577 unsigned int ar_delay;
578 unsigned int ar_rate;
580 // these are the keys currently down
581 int key_down[MP_MAX_KEY_DOWN];
582 unsigned int num_key_down;
583 unsigned int last_key_down;
585 // List of command binding sections
586 mp_cmd_bind_section_t *cmd_bind_sections;
587 // Name of currently used command section
588 char *section;
589 // The command binds of current section
590 mp_cmd_bind_t *cmd_binds;
591 mp_cmd_bind_t *cmd_binds_default;
593 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
594 unsigned int num_key_fd;
596 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
597 unsigned int num_cmd_fd;
599 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
600 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
604 static mp_cmd_filter_t* cmd_filters = NULL;
606 // Callback to allow the menu filter to grab the incoming keys
607 int (*mp_input_key_cb)(int code) = NULL;
609 int async_quit_request;
611 static int print_key_list(m_option_t* cfg);
612 static int print_cmd_list(m_option_t* cfg);
614 // Our command line options
615 static const m_option_t input_conf[] = {
616 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
617 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
618 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
619 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
620 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
621 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
622 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
623 OPT_STRING("file", input.in_file, CONF_GLOBAL),
624 { 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_FLAG_OFF("nojoystick", input.use_joystick, CONF_GLOBAL),
630 OPT_FLAG_ON("joystick", input.use_joystick, CONF_GLOBAL),
631 OPT_FLAG_OFF("nolirc", input.use_lirc, CONF_GLOBAL),
632 OPT_FLAG_ON("lirc", input.use_lirc, CONF_GLOBAL),
633 OPT_FLAG_OFF("nolircc", input.use_lircc, CONF_GLOBAL),
634 OPT_FLAG_ON("lircc", input.use_lircc, CONF_GLOBAL),
635 OPT_FLAG_OFF("noar", input.use_ar, CONF_GLOBAL),
636 OPT_FLAG_ON("ar", input.use_ar, CONF_GLOBAL),
637 { NULL, NULL, 0, 0, 0, 0, NULL}
640 static int default_cmd_func(int fd,char* buf, int l);
642 static char *get_key_name(int key, char buffer[12]);
645 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
646 mp_cmd_func_t read_func, mp_close_func_t close_func)
648 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
649 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
650 return 0;
652 if (select && fd < 0) {
653 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
654 return 0;
657 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
658 .fd = fd,
659 .read_func.cmd = read_func ? read_func : default_cmd_func,
660 .close_func = close_func,
661 .no_select = !select
663 ictx->num_cmd_fd++;
665 return 1;
668 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
670 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
671 unsigned int i;
673 for (i = 0; i < ictx->num_cmd_fd; i++) {
674 if(cmd_fds[i].fd == fd)
675 break;
677 if (i == ictx->num_cmd_fd)
678 return;
679 if(cmd_fds[i].close_func)
680 cmd_fds[i].close_func(cmd_fds[i].fd);
681 if(cmd_fds[i].buffer)
682 talloc_free(cmd_fds[i].buffer);
684 if (i + 1 < ictx->num_cmd_fd)
685 memmove(&cmd_fds[i], &cmd_fds[i+1],
686 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
687 ictx->num_cmd_fd--;
690 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
692 struct mp_input_fd *key_fds = ictx->key_fds;
693 unsigned int i;
695 for (i = 0; i < ictx->num_key_fd; i++) {
696 if(key_fds[i].fd == fd)
697 break;
699 if (i == ictx->num_key_fd)
700 return;
701 if(key_fds[i].close_func)
702 key_fds[i].close_func(key_fds[i].fd);
704 if(i + 1 < ictx->num_key_fd)
705 memmove(&key_fds[i], &key_fds[i+1],
706 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
707 ictx->num_key_fd--;
710 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
711 mp_key_func_t read_func, mp_close_func_t close_func,
712 void *ctx)
714 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
715 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
716 return 0;
718 if (select && fd < 0) {
719 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
720 return 0;
723 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
724 .fd = fd,
725 .read_func.key = read_func,
726 .close_func = close_func,
727 .no_select = !select,
728 .ctx = ctx,
730 ictx->num_key_fd++;
732 return 1;
735 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
737 int cmd_num = 0;
739 while (*str == '\n' || *str == '\r' || *str == ' ')
740 ++str;
741 while (*str) {
742 mp_cmd_t *cmd;
743 size_t len = strcspn(str, "\r\n");
744 char *cmdbuf = talloc_size(NULL, len+1);
745 av_strlcpy(cmdbuf, str, len+1);
746 cmd = mp_input_parse_cmd(cmdbuf);
747 if (cmd) {
748 mp_input_queue_cmd(ictx, cmd);
749 ++cmd_num;
751 str += len;
752 while (*str == '\n' || *str == '\r' || *str == ' ')
753 ++str;
754 talloc_free(cmdbuf);
756 return cmd_num;
759 mp_cmd_t*
760 mp_input_parse_cmd(char* str) {
761 int i,l;
762 int pausing = 0;
763 char *ptr,*e;
764 const mp_cmd_t *cmd_def;
766 #ifdef MP_DEBUG
767 assert(str != NULL);
768 #endif
770 // Ignore heading spaces.
771 while (str[0] == ' ' || str[0] == '\t')
772 ++str;
774 if (strncmp(str, "pausing ", 8) == 0) {
775 pausing = 1;
776 str = &str[8];
777 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
778 pausing = 2;
779 str = &str[13];
780 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
781 pausing = 3;
782 str = &str[15];
783 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
784 pausing = 4;
785 str = &str[19];
788 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
789 /* NOTHING */;
790 if(ptr[0] != '\0')
791 l = ptr-str;
792 else
793 l = strlen(str);
795 if(l == 0)
796 return NULL;
798 for(i=0; mp_cmds[i].name != NULL; i++) {
799 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
800 break;
803 if(mp_cmds[i].name == NULL)
804 return NULL;
806 cmd_def = &mp_cmds[i];
808 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
809 *cmd = (mp_cmd_t){
810 .id = cmd_def->id,
811 .name = talloc_strdup(cmd, cmd_def->name),
812 .pausing = pausing,
815 ptr = str;
817 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
818 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
819 if(ptr[0] == '\0') break;
820 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
821 if(ptr[0] == '\0' || ptr[0] == '#') break;
822 cmd->args[i].type = cmd_def->args[i].type;
823 switch(cmd_def->args[i].type) {
824 case MP_CMD_ARG_INT:
825 errno = 0;
826 cmd->args[i].v.i = atoi(ptr);
827 if(errno != 0) {
828 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
829 ptr = NULL;
831 break;
832 case MP_CMD_ARG_FLOAT:
833 errno = 0;
834 cmd->args[i].v.f = atof(ptr);
835 if(errno != 0) {
836 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
837 ptr = NULL;
839 break;
840 case MP_CMD_ARG_STRING: {
841 char term;
842 char* ptr2 = ptr, *start;
844 if(ptr[0] == '\'' || ptr[0] == '"') {
845 term = ptr[0];
846 ptr2++;
847 } else
848 term = ' ';
849 start = ptr2;
850 while(1) {
851 e = strchr(ptr2,term);
852 if(!e) break;
853 if(e <= ptr2 || *(e - 1) != '\\') break;
854 ptr2 = e + 1;
857 if(term != ' ' && (!e || e[0] == '\0')) {
858 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
859 ptr = NULL;
860 break;
861 } else if(!e) e = ptr+strlen(ptr);
862 l = e-start;
863 ptr2 = start;
864 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
865 memmove(e,e+1,strlen(e));
866 ptr2 = e + 1;
867 l--;
869 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
870 if(term != ' ') ptr += l+2;
871 } break;
872 case -1:
873 ptr = NULL;
874 break;
875 default :
876 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
879 cmd->nargs = i;
881 if(cmd_def->nargs > cmd->nargs) {
882 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
883 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
884 mp_cmd_free(cmd);
885 return NULL;
888 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
889 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
890 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
891 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
894 if(i < MP_CMD_MAX_ARGS)
895 cmd->args[i].type = -1;
897 return cmd;
900 #define MP_CMD_MAX_SIZE 256
902 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
904 char* end;
905 (*ret) = NULL;
907 // Allocate the buffer if it doesn't exist
908 if(!mp_fd->buffer) {
909 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
910 mp_fd->pos = 0;
911 mp_fd->size = MP_CMD_MAX_SIZE;
914 // Get some data if needed/possible
915 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
916 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
917 mp_fd->size - 1 - mp_fd->pos);
918 // Error ?
919 if(r < 0) {
920 switch(r) {
921 case MP_INPUT_ERROR:
922 case MP_INPUT_DEAD:
923 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
924 case MP_INPUT_NOTHING:
925 return r;
926 case MP_INPUT_RETRY:
927 continue;
929 // EOF ?
930 } else if(r == 0) {
931 mp_fd->eof = 1;
932 break;
934 mp_fd->pos += r;
935 break;
938 mp_fd->got_cmd = 0;
940 while(1) {
941 int l = 0;
942 // Find the cmd end
943 mp_fd->buffer[mp_fd->pos] = '\0';
944 end = strchr(mp_fd->buffer,'\n');
945 // No cmd end ?
946 if(!end) {
947 // If buffer is full we must drop all until the next \n
948 if(mp_fd->size - mp_fd->pos <= 1) {
949 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
950 mp_fd->pos = 0;
951 mp_fd->drop = 1;
953 break;
955 // We already have a cmd : set the got_cmd flag
956 else if((*ret)) {
957 mp_fd->got_cmd = 1;
958 break;
961 l = end - mp_fd->buffer;
963 // Not dropping : put the cmd in ret
964 if (!mp_fd->drop)
965 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
966 else
967 mp_fd->drop = 0;
968 mp_fd->pos -= l+1;
969 memmove(mp_fd->buffer, end+1, mp_fd->pos);
972 if(*ret)
973 return 1;
974 else
975 return MP_INPUT_NOTHING;
978 static int default_cmd_func(int fd,char* buf, int l)
980 while(1) {
981 int r = read(fd,buf,l);
982 // Error ?
983 if(r < 0) {
984 if(errno == EINTR)
985 continue;
986 else if(errno == EAGAIN)
987 return MP_INPUT_NOTHING;
988 return MP_INPUT_ERROR;
989 // EOF ?
991 return r;
997 void
998 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
999 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1001 filter->filter = func;
1002 filter->ctx = ctx;
1003 filter->next = cmd_filters;
1004 cmd_filters = filter;
1008 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
1010 int j;
1012 if (n <= 0) return NULL;
1013 for(j = 0; binds[j].cmd != NULL; j++) {
1014 int found = 1,s;
1015 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1016 if(binds[j].input[s] != keys[s]) {
1017 found = 0;
1018 break;
1021 if(found && binds[j].input[s] == 0 && s == n)
1022 break;
1024 return binds[j].cmd;
1027 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1028 char *section)
1030 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1032 if (section==NULL) section="default";
1033 while (bind_section) {
1034 if(strcmp(section,bind_section->section)==0) return bind_section;
1035 if(bind_section->next==NULL) break;
1036 bind_section=bind_section->next;
1038 if(bind_section) {
1039 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1040 bind_section=bind_section->next;
1041 } else {
1042 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1043 bind_section = ictx->cmd_bind_sections;
1045 bind_section->cmd_binds=NULL;
1046 bind_section->section = talloc_strdup(bind_section, section);
1047 bind_section->next=NULL;
1048 return bind_section;
1051 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys,
1052 int paused)
1054 char* cmd = NULL;
1055 mp_cmd_t* ret;
1056 char key_buf[12];
1058 if (ictx->cmd_binds)
1059 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1060 if (ictx->cmd_binds_default && cmd == NULL)
1061 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1062 if(cmd == NULL)
1063 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1065 if(cmd == NULL) {
1066 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound, get_key_name(keys[0],
1067 key_buf));
1068 if(n > 1) {
1069 int s;
1070 for(s=1; s < n; s++)
1071 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", get_key_name(keys[s], key_buf));
1073 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1074 return NULL;
1076 if (strcmp(cmd, "ignore") == 0) return NULL;
1077 ret = mp_input_parse_cmd(cmd);
1078 if(!ret) {
1079 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,
1080 get_key_name(ictx->key_down[0], key_buf));
1081 if (ictx->num_key_down > 1) {
1082 unsigned int s;
1083 for(s=1; s < ictx->num_key_down; s++)
1084 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(ictx->key_down[s],
1085 key_buf));
1087 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1089 return ret;
1093 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1095 unsigned int j;
1096 mp_cmd_t* ret;
1098 if(mp_input_key_cb) {
1099 if (code & MP_KEY_DOWN)
1100 return NULL;
1101 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1102 if (mp_input_key_cb(code))
1103 return NULL;
1106 if(code & MP_KEY_DOWN) {
1107 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1108 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1109 return NULL;
1111 code &= ~MP_KEY_DOWN;
1112 // Check if we don't already have this key as pushed
1113 for (j = 0; j < ictx->num_key_down; j++) {
1114 if (ictx->key_down[j] == code)
1115 break;
1117 if (j != ictx->num_key_down)
1118 return NULL;
1119 ictx->key_down[ictx->num_key_down] = code;
1120 ictx->num_key_down++;
1121 ictx->last_key_down = GetTimer();
1122 ictx->ar_state = 0;
1123 return NULL;
1125 // key released
1126 // Check if the key is in the down key, driver which can't send push event
1127 // send only release event
1128 for (j = 0; j < ictx->num_key_down; j++) {
1129 if (ictx->key_down[j] == code)
1130 break;
1132 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1133 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1134 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1135 return NULL;
1137 ictx->key_down[ictx->num_key_down] = code;
1138 ictx->num_key_down++;
1139 ictx->last_key_down = 1;
1141 // We ignore key from last combination
1142 ret = ictx->last_key_down ?
1143 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down, paused)
1144 : NULL;
1145 // Remove the key
1146 if (j+1 < ictx->num_key_down)
1147 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1148 (ictx->num_key_down-(j+1))*sizeof(int));
1149 ictx->num_key_down--;
1150 ictx->last_key_down = 0;
1151 ictx->ar_state = -1;
1152 if (ictx->ar_cmd) {
1153 mp_cmd_free(ictx->ar_cmd);
1154 ictx->ar_cmd = NULL;
1156 return ret;
1159 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1161 // No input : autorepeat ?
1162 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1163 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1164 unsigned int t = GetTimer();
1165 // First time : wait delay
1166 if (ictx->ar_state == 0
1167 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1168 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1169 ictx->key_down, paused);
1170 if (!ictx->ar_cmd) {
1171 ictx->ar_state = -1;
1172 return NULL;
1174 ictx->ar_state = 1;
1175 ictx->last_ar = t;
1176 return mp_cmd_clone(ictx->ar_cmd);
1177 // Then send rate / sec event
1178 } else if (ictx->ar_state == 1
1179 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1180 ictx->last_ar = t;
1181 return mp_cmd_clone(ictx->ar_cmd);
1184 return NULL;
1188 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1190 int i;
1191 int got_cmd = 0;
1192 struct mp_input_fd *key_fds = ictx->key_fds;
1193 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1194 for (i = 0; i < ictx->num_key_fd; i++)
1195 if (key_fds[i].dead) {
1196 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1197 i--;
1199 for (i = 0; i < ictx->num_cmd_fd; i++)
1200 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1201 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1202 i--;
1204 else if (cmd_fds[i].got_cmd)
1205 got_cmd = 1;
1206 #ifdef HAVE_POSIX_SELECT
1207 fd_set fds;
1208 FD_ZERO(&fds);
1209 if (!got_cmd) {
1210 int max_fd = 0, num_fd = 0;
1211 for (i = 0; i < ictx->num_key_fd; i++) {
1212 if (key_fds[i].no_select)
1213 continue;
1214 if (key_fds[i].fd > max_fd)
1215 max_fd = key_fds[i].fd;
1216 FD_SET(key_fds[i].fd, &fds);
1217 num_fd++;
1219 for (i = 0; i < ictx->num_cmd_fd; i++) {
1220 if (cmd_fds[i].no_select)
1221 continue;
1222 if (cmd_fds[i].fd > max_fd)
1223 max_fd = cmd_fds[i].fd;
1224 FD_SET(cmd_fds[i].fd, &fds);
1225 num_fd++;
1227 if (num_fd > 0) {
1228 struct timeval tv, *time_val;
1229 if (time >= 0) {
1230 tv.tv_sec = time / 1000;
1231 tv.tv_usec = (time % 1000) * 1000;
1232 time_val = &tv;
1234 else
1235 time_val = NULL;
1236 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1237 if (errno != EINTR)
1238 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrSelect,
1239 strerror(errno));
1240 FD_ZERO(&fds);
1244 #else
1245 if (!got_cmd && time)
1246 usec_sleep(time * 1000);
1247 #endif
1250 for (i = 0; i < ictx->num_key_fd; i++) {
1251 #ifdef HAVE_POSIX_SELECT
1252 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1253 continue;
1254 #endif
1256 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1257 if (code >= 0) {
1258 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1259 if (ret)
1260 return ret;
1262 else if (code == MP_INPUT_ERROR)
1263 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1264 key_fds[i].fd);
1265 else if (code == MP_INPUT_DEAD) {
1266 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1267 key_fds[i].fd);
1268 key_fds[i].dead = 1;
1271 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1272 if (autorepeat_cmd)
1273 return autorepeat_cmd;
1275 for (i = 0; i < ictx->num_cmd_fd; i++) {
1276 #ifdef HAVE_POSIX_SELECT
1277 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1278 !cmd_fds[i].got_cmd)
1279 continue;
1280 #endif
1281 char *cmd;
1282 int r = read_cmd(&cmd_fds[i], &cmd);
1283 if (r >= 0) {
1284 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1285 talloc_free(cmd);
1286 if (ret)
1287 return ret;
1289 else if (r == MP_INPUT_ERROR)
1290 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1291 cmd_fds[i].fd);
1292 else if (r == MP_INPUT_DEAD)
1293 cmd_fds[i].dead = 1;
1296 return NULL;
1300 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1302 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1303 return 0;
1304 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1305 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1306 ictx->cmd_queue_length++;
1307 return 1;
1310 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1312 mp_cmd_t* ret;
1314 if (ictx->cmd_queue_length == 0)
1315 return NULL;
1317 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1319 if (!peek_only) {
1320 ictx->cmd_queue_length--;
1321 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1324 return ret;
1328 * \param peek_only when set, the returned command stays in the queue.
1329 * Do not free the returned cmd whe you set this!
1331 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1332 int peek_only)
1334 mp_cmd_t* ret = NULL;
1335 mp_cmd_filter_t* cf;
1336 int from_queue;
1338 if (async_quit_request)
1339 return mp_input_parse_cmd("quit 1");
1340 while(1) {
1341 from_queue = 1;
1342 ret = get_queued_cmd(ictx, peek_only);
1343 if(ret) break;
1344 from_queue = 0;
1345 ret = read_events(ictx, time, paused);
1346 if (!ret) {
1347 from_queue = 1;
1348 ret = get_queued_cmd(ictx, peek_only);
1350 break;
1352 if(!ret) return NULL;
1354 for(cf = cmd_filters ; cf ; cf = cf->next) {
1355 if(cf->filter(ret,paused,cf->ctx)) {
1356 if (peek_only && from_queue)
1357 // The filter ate the cmd, so we remove it from queue
1358 ret = get_queued_cmd(ictx, 0);
1359 mp_cmd_free(ret);
1360 return NULL;
1364 if (!from_queue && peek_only)
1365 mp_input_queue_cmd(ictx, ret);
1367 return ret;
1370 void
1371 mp_cmd_free(mp_cmd_t* cmd) {
1372 talloc_free(cmd);
1375 mp_cmd_t*
1376 mp_cmd_clone(mp_cmd_t* cmd) {
1377 mp_cmd_t* ret;
1378 int i;
1379 #ifdef MP_DEBUG
1380 assert(cmd != NULL);
1381 #endif
1383 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1384 ret->name = talloc_strdup(ret, cmd->name);
1385 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1386 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1387 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1390 return ret;
1393 static char *get_key_name(int key, char buffer[12])
1395 int i;
1397 for(i = 0; key_names[i].name != NULL; i++) {
1398 if(key_names[i].key == key)
1399 return key_names[i].name;
1402 if(isascii(key)) {
1403 snprintf(buffer, 12, "%c",(char)key);
1404 return buffer;
1407 // Print the hex key code
1408 snprintf(buffer, 12, "%#-8x",key);
1409 return buffer;
1414 mp_input_get_key_from_name(const char *name) {
1415 int i,ret = 0,len = strlen(name);
1416 if(len == 1) { // Direct key code
1417 ret = (unsigned char)name[0];
1418 return ret;
1419 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1420 return strtol(name,NULL,16);
1422 for(i = 0; key_names[i].name != NULL; i++) {
1423 if(strcasecmp(key_names[i].name,name) == 0)
1424 return key_names[i].key;
1427 return -1;
1430 static int get_input_from_name(char* name,int* keys) {
1431 char *end,*ptr;
1432 int n=0;
1434 ptr = name;
1435 n = 0;
1436 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1437 if(end && end[1] != '\0') {
1438 if(end[1] == '-')
1439 end = &end[1];
1440 end[0] = '\0';
1442 keys[n] = mp_input_get_key_from_name(ptr);
1443 if(keys[n] < 0) {
1444 return 0;
1446 n++;
1447 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1448 ptr = &end[1];
1449 else
1450 break;
1452 keys[n] = 0;
1453 return 1;
1456 #define BS_MAX 256
1457 #define SPACE_CHAR " \n\r\t"
1459 static void bind_keys(struct input_ctx *ictx,
1460 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1462 int i = 0,j;
1463 mp_cmd_bind_t* bind = NULL;
1464 mp_cmd_bind_section_t* bind_section = NULL;
1465 char *section=NULL, *p;
1467 #ifdef MP_DEBUG
1468 assert(keys != NULL);
1469 assert(cmd != NULL);
1470 #endif
1472 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1473 *p=0;
1474 section=++cmd;
1475 cmd=++p;
1476 // Jump beginning space
1477 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1478 /* NOTHING */;
1480 bind_section = get_bind_section(ictx, section);
1482 if(bind_section->cmd_binds) {
1483 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1484 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1485 /* NOTHING */;
1486 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1487 bind = &bind_section->cmd_binds[i];
1488 break;
1493 if(!bind) {
1494 bind_section->cmd_binds = talloc_realloc(bind_section,
1495 bind_section->cmd_binds,
1496 mp_cmd_bind_t, i + 2);
1497 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1498 bind = &bind_section->cmd_binds[i];
1500 if(bind->cmd)
1501 talloc_free(bind->cmd);
1502 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1503 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1506 static void add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1508 int i;
1509 for(i = 0 ; list[i].cmd ; i++)
1510 bind_keys(ictx, list[i].input,list[i].cmd);
1513 static int parse_config(struct input_ctx *ictx, char *file)
1515 int fd;
1516 int bs = 0,r,eof = 0,comments = 0;
1517 char *iter,*end;
1518 char buffer[BS_MAX];
1519 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1521 fd = open(file,O_RDONLY);
1523 if(fd < 0) {
1524 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1525 return 0;
1528 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1530 while(1) {
1531 if(! eof && bs < BS_MAX-1) {
1532 if(bs > 0) bs--;
1533 r = read(fd,buffer+bs,BS_MAX-1-bs);
1534 if(r < 0) {
1535 if(errno == EINTR)
1536 continue;
1537 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1538 close(fd);
1539 return 0;
1540 } else if(r == 0) {
1541 eof = 1;
1542 } else {
1543 bs += r+1;
1544 buffer[bs-1] = '\0';
1547 // Empty buffer : return
1548 if(bs <= 1) {
1549 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1550 close(fd);
1551 return 1;
1554 iter = buffer;
1556 if(comments) {
1557 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1558 /* NOTHING */;
1559 if(iter[0] == '\0') { // Buffer was full of comment
1560 bs = 0;
1561 continue;
1563 iter++;
1564 r = strlen(iter);
1565 memmove(buffer,iter,r+1);
1566 bs = r+1;
1567 comments = 0;
1568 continue;
1571 // Find the wanted key
1572 if(keys[0] == 0) {
1573 // Jump beginning space
1574 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1575 /* NOTHING */;
1576 if(iter[0] == '\0') { // Buffer was full of space char
1577 bs = 0;
1578 continue;
1580 if(iter[0] == '#') { // Comments
1581 comments = 1;
1582 continue;
1584 // Find the end of the key code name
1585 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1586 /*NOTHING */;
1587 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1588 if(buffer == iter) {
1589 if(eof && (buffer-iter) == bs)
1590 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1591 else
1592 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1593 return 0;
1595 memmove(buffer,iter,end-iter);
1596 bs = end-iter;
1597 continue;
1600 char name[end-iter+1];
1601 strncpy(name,iter,end-iter);
1602 name[end-iter] = '\0';
1603 if (!get_input_from_name(name,keys)) {
1604 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1605 close(fd);
1606 return 0;
1609 if( bs > (end-buffer))
1610 memmove(buffer,end,bs - (end-buffer));
1611 bs -= end-buffer;
1612 continue;
1613 } else { // Get the command
1614 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1615 // Found new line
1616 if(iter[0] == '\n' || iter[0] == '\r') {
1617 int i;
1618 char key_buf[12];
1619 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey, get_key_name(keys[0], key_buf));
1620 for(i = 1; keys[i] != 0 ; i++)
1621 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(keys[i], key_buf));
1622 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1623 keys[0] = 0;
1624 if(iter > buffer) {
1625 memmove(buffer,iter,bs- (iter-buffer));
1626 bs -= (iter-buffer);
1628 continue;
1630 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1631 /* NOTHING */;
1632 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1633 if(iter == buffer) {
1634 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1635 close(fd);
1636 return 0;
1638 memmove(buffer,iter,end - iter);
1639 bs = end - iter;
1640 continue;
1643 char cmd[end-iter+1];
1644 strncpy(cmd,iter,end-iter);
1645 cmd[end-iter] = '\0';
1646 //printf("Set bind %d => %s\n",keys[0],cmd);
1647 bind_keys(ictx, keys,cmd);
1648 n_binds++;
1650 keys[0] = 0;
1651 end++;
1652 if(bs > (end-buffer))
1653 memmove(buffer,end,bs-(end-buffer));
1654 bs -= (end-buffer);
1655 buffer[bs-1] = '\0';
1656 continue;
1659 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1660 close(fd);
1661 mp_input_set_section(ictx, NULL);
1662 return 0;
1665 void mp_input_set_section(struct input_ctx *ictx, char *name)
1667 mp_cmd_bind_section_t* bind_section = NULL;
1669 ictx->cmd_binds = NULL;
1670 ictx->cmd_binds_default = NULL;
1671 if (ictx->section)
1672 talloc_free(ictx->section);
1673 if (name)
1674 ictx->section = talloc_strdup(ictx, name);
1675 else
1676 ictx->section = talloc_strdup(ictx, "default");
1677 if ((bind_section = get_bind_section(ictx, ictx->section)))
1678 ictx->cmd_binds = bind_section->cmd_binds;
1679 if (strcmp(ictx->section, "default") == 0)
1680 return;
1681 if ((bind_section = get_bind_section(ictx, NULL)))
1682 ictx->cmd_binds_default = bind_section->cmd_binds;
1685 char *mp_input_get_section(struct input_ctx *ictx)
1687 return ictx->section;
1690 struct input_ctx *mp_input_init(struct input_conf *input_conf, int use_gui)
1692 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1693 *ictx = (struct input_ctx){
1694 .ar_state = -1,
1695 .ar_delay = input_conf->ar_delay,
1696 .ar_rate = input_conf->ar_rate,
1699 char* file;
1701 #ifdef CONFIG_GUI
1702 if(use_gui)
1703 add_binds(ictx, gui_def_cmd_binds);
1704 #endif
1706 char *config_file = input_conf->config_file;
1707 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1708 if(!file)
1709 return ictx;
1711 if (!parse_config(ictx, file)) {
1712 // free file if it was allocated by get_path(),
1713 // before it gets overwritten
1714 if( file != config_file)
1716 free(file);
1718 // Try global conf dir
1719 file = MPLAYER_CONFDIR "/input.conf";
1720 if (!parse_config(ictx, file))
1721 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1723 else
1725 // free file if it was allocated by get_path()
1726 if( file != config_file)
1727 free(file);
1730 #ifdef CONFIG_JOYSTICK
1731 if (input_conf->use_joystick) {
1732 int fd = mp_input_joystick_init(input_conf->js_dev);
1733 if(fd < 0)
1734 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1735 else
1736 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1737 (mp_close_func_t)close,NULL);
1739 #endif
1741 #ifdef CONFIG_LIRC
1742 if (input_conf->use_lirc) {
1743 int fd = mp_input_lirc_init();
1744 if(fd > 0)
1745 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1746 mp_input_lirc_close);
1748 #endif
1750 #ifdef CONFIG_LIRCC
1751 if (input_conf->use_lircc) {
1752 int fd = lircc_init("mplayer", NULL);
1753 if(fd >= 0)
1754 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1756 #endif
1758 #ifdef CONFIG_APPLE_REMOTE
1759 if (input_conf->use_ar) {
1760 if(mp_input_ar_init() < 0)
1761 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1762 else
1763 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1764 NULL);
1766 #endif
1768 #ifdef CONFIG_APPLE_IR
1769 if (input_conf->use_ar) {
1770 int fd = mp_input_appleir_init(input_conf->ar_dev);
1771 if(fd < 0)
1772 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1773 else
1774 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1775 (mp_close_func_t)close, NULL);
1777 #endif
1779 if (input_conf->in_file) {
1780 struct stat st;
1781 if (stat(input_conf->in_file, &st))
1782 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrCantStatFile, input_conf->in_file, strerror(errno));
1783 else {
1784 int in_file_fd = open(input_conf->in_file,
1785 S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1786 if(in_file_fd >= 0)
1787 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1788 (mp_close_func_t)close);
1789 else
1790 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrCantOpenFile,
1791 input_conf->in_file, strerror(errno));
1794 return ictx;
1797 void mp_input_uninit(struct input_ctx *ictx)
1799 if (!ictx)
1800 return;
1802 unsigned int i;
1804 for (i=0; i < ictx->num_key_fd; i++) {
1805 if (ictx->key_fds[i].close_func)
1806 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1809 for (i = 0; i < ictx->num_cmd_fd; i++) {
1810 if (ictx->cmd_fds[i].close_func)
1811 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1813 talloc_free(ictx);
1816 void
1817 mp_input_register_options(m_config_t* cfg) {
1818 m_config_register_options(cfg,mp_input_opts);
1821 static int print_key_list(m_option_t* cfg)
1823 int i;
1824 printf("\n");
1825 for(i= 0; key_names[i].name != NULL ; i++)
1826 printf("%s\n",key_names[i].name);
1827 exit(0);
1830 static int print_cmd_list(m_option_t* cfg)
1832 const mp_cmd_t *cmd;
1833 int i,j;
1834 const char* type;
1836 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1837 printf("%-20.20s",cmd->name);
1838 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1839 switch(cmd->args[j].type) {
1840 case MP_CMD_ARG_INT:
1841 type = "Integer";
1842 break;
1843 case MP_CMD_ARG_FLOAT:
1844 type = "Float";
1845 break;
1846 case MP_CMD_ARG_STRING:
1847 type = "String";
1848 break;
1849 default:
1850 type = "??";
1852 if(j+1 > cmd->nargs)
1853 printf(" [%s]",type);
1854 else
1855 printf(" %s",type);
1857 printf("\n");
1859 exit(0);
1862 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1864 mp_cmd_t* cmd;
1865 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1866 return 0;
1867 switch(cmd->id) {
1868 case MP_CMD_QUIT:
1869 case MP_CMD_PLAY_TREE_STEP:
1870 case MP_CMD_PLAY_TREE_UP_STEP:
1871 case MP_CMD_PLAY_ALT_SRC_STEP:
1872 // The cmd will be executed when we are back in the main loop
1873 return 1;
1875 // remove the cmd from the queue
1876 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1877 mp_cmd_free(cmd);
1878 return 0;