rename 'struct keymap' -> 'struct mp_keymap'
[mplayer/glamo.git] / input / input.c
blob0bcd5752af23d680987faae00e6fa847ce0b61be
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/getch2.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 "path.h"
48 #include "joystick.h"
50 #ifdef CONFIG_LIRC
51 #include "lirc.h"
52 #endif
54 #ifdef CONFIG_LIRCC
55 #include <lirc/lircc.h>
56 #endif
58 #include "ar.h"
60 /// This array defines all known commands.
61 /// The first field is an id used to recognize the command without too many strcmp.
62 /// The second is obviously the command name.
63 /// The third is the minimum number of arguments this command needs.
64 /// Then comes the definition of each argument, terminated with an arg of type -1.
65 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
66 /// the last one) which is actually 9.
68 /// For the args, the first field is the type (actually int, float or string), the second
69 /// is the default value wich is used for optional arguments
71 static const mp_cmd_t mp_cmds[] = {
72 #ifdef CONFIG_RADIO
73 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
74 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
75 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
76 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
77 #endif
78 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
79 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
80 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
81 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
82 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
83 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
84 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
85 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
86 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
87 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
88 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
89 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
90 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
91 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
93 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
94 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
95 { 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}} } },
96 { 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}} } },
97 { MP_CMD_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
98 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
99 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
100 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
101 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
102 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
106 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
108 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
109 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
110 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
111 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
112 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
113 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
114 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
115 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
116 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
117 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
118 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
119 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
120 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
121 #ifdef CONFIG_ASS
122 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
123 #endif
124 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
125 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
126 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
127 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
128 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
129 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
130 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
131 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
132 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
133 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
134 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
135 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
136 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
137 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
138 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
139 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
140 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
141 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
142 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
143 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
144 #ifdef CONFIG_TV
145 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
146 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
147 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
148 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
149 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
150 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
151 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
152 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
153 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
154 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
155 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
156 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
157 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
158 #endif
159 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
160 #ifdef CONFIG_DVBIN
161 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
162 #endif
163 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
165 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
166 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
167 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
168 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
169 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
170 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
171 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
172 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
173 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
174 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
175 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
176 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
177 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
179 #ifdef CONFIG_GUI
180 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
181 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
182 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
183 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
184 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
185 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
186 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
187 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
188 #endif
190 #ifdef CONFIG_DVDNAV
191 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
192 #endif
194 #ifdef CONFIG_MENU
195 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
196 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
197 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
198 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
199 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
200 #endif
202 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
203 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
204 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
205 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
206 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
207 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
209 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
210 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
212 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
213 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
214 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
215 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
217 { 0, NULL, 0, {} }
220 /// The names of the keys as used in input.conf
221 /// If you add some new keys, you also need to add them here
223 static const mp_key_name_t key_names[] = {
224 { ' ', "SPACE" },
225 { '#', "SHARP" },
226 { KEY_ENTER, "ENTER" },
227 { KEY_TAB, "TAB" },
228 { KEY_CTRL, "CTRL" },
229 { KEY_BACKSPACE, "BS" },
230 { KEY_DELETE, "DEL" },
231 { KEY_INSERT, "INS" },
232 { KEY_HOME, "HOME" },
233 { KEY_END, "END" },
234 { KEY_PAGE_UP, "PGUP" },
235 { KEY_PAGE_DOWN, "PGDWN" },
236 { KEY_ESC, "ESC" },
237 { KEY_RIGHT, "RIGHT" },
238 { KEY_LEFT, "LEFT" },
239 { KEY_DOWN, "DOWN" },
240 { KEY_UP, "UP" },
241 { KEY_F+1, "F1" },
242 { KEY_F+2, "F2" },
243 { KEY_F+3, "F3" },
244 { KEY_F+4, "F4" },
245 { KEY_F+5, "F5" },
246 { KEY_F+6, "F6" },
247 { KEY_F+7, "F7" },
248 { KEY_F+8, "F8" },
249 { KEY_F+9, "F9" },
250 { KEY_F+10, "F10" },
251 { KEY_F+11, "F11" },
252 { KEY_F+12, "F12" },
253 { KEY_KP0, "KP0" },
254 { KEY_KP1, "KP1" },
255 { KEY_KP2, "KP2" },
256 { KEY_KP3, "KP3" },
257 { KEY_KP4, "KP4" },
258 { KEY_KP5, "KP5" },
259 { KEY_KP6, "KP6" },
260 { KEY_KP7, "KP7" },
261 { KEY_KP8, "KP8" },
262 { KEY_KP9, "KP9" },
263 { KEY_KPDEL, "KP_DEL" },
264 { KEY_KPDEC, "KP_DEC" },
265 { KEY_KPINS, "KP_INS" },
266 { KEY_KPENTER, "KP_ENTER" },
267 { MOUSE_BTN0, "MOUSE_BTN0" },
268 { MOUSE_BTN1, "MOUSE_BTN1" },
269 { MOUSE_BTN2, "MOUSE_BTN2" },
270 { MOUSE_BTN3, "MOUSE_BTN3" },
271 { MOUSE_BTN4, "MOUSE_BTN4" },
272 { MOUSE_BTN5, "MOUSE_BTN5" },
273 { MOUSE_BTN6, "MOUSE_BTN6" },
274 { MOUSE_BTN7, "MOUSE_BTN7" },
275 { MOUSE_BTN8, "MOUSE_BTN8" },
276 { MOUSE_BTN9, "MOUSE_BTN9" },
277 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
278 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
279 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
280 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
281 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
282 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
283 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
284 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
285 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
286 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
287 { JOY_AXIS1_MINUS, "JOY_UP" },
288 { JOY_AXIS1_PLUS, "JOY_DOWN" },
289 { JOY_AXIS0_MINUS, "JOY_LEFT" },
290 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
292 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
293 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
294 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
295 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
296 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
297 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
298 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
299 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
300 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
301 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
302 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
303 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
304 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
305 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
306 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
307 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
308 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
309 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
310 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
311 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
313 { JOY_BTN0, "JOY_BTN0" },
314 { JOY_BTN1, "JOY_BTN1" },
315 { JOY_BTN2, "JOY_BTN2" },
316 { JOY_BTN3, "JOY_BTN3" },
317 { JOY_BTN4, "JOY_BTN4" },
318 { JOY_BTN5, "JOY_BTN5" },
319 { JOY_BTN6, "JOY_BTN6" },
320 { JOY_BTN7, "JOY_BTN7" },
321 { JOY_BTN8, "JOY_BTN8" },
322 { JOY_BTN9, "JOY_BTN9" },
324 { AR_PLAY, "AR_PLAY" },
325 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
326 { AR_NEXT, "AR_NEXT" },
327 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
328 { AR_PREV, "AR_PREV" },
329 { AR_PREV_HOLD, "AR_PREV_HOLD" },
330 { AR_MENU, "AR_MENU" },
331 { AR_MENU_HOLD, "AR_MENU_HOLD" },
332 { AR_VUP, "AR_VUP" },
333 { AR_VDOWN, "AR_VDOWN" },
335 { KEY_POWER, "POWER" },
336 { KEY_MENU, "MENU" },
337 { KEY_PLAY, "PLAY" },
338 { KEY_PAUSE, "PAUSE" },
339 { KEY_PLAYPAUSE, "PLAYPAUSE" },
340 { KEY_STOP, "STOP" },
341 { KEY_FORWARD, "FORWARD" },
342 { KEY_REWIND, "REWIND" },
343 { KEY_NEXT, "NEXT" },
344 { KEY_PREV, "PREV" },
345 { KEY_VOLUME_UP, "VOLUME_UP" },
346 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
347 { KEY_MUTE, "MUTE" },
349 // These are kept for backward compatibility
350 { KEY_PAUSE, "XF86_PAUSE" },
351 { KEY_STOP, "XF86_STOP" },
352 { KEY_PREV, "XF86_PREV" },
353 { KEY_NEXT, "XF86_NEXT" },
355 { KEY_CLOSE_WIN, "CLOSE_WIN" },
357 { 0, NULL }
360 // This is the default binding. The content of input.conf overrides these.
361 // The first arg is a null terminated array of key codes.
362 // The second is the command
364 static const mp_cmd_bind_t def_cmd_binds[] = {
366 { { MOUSE_BTN3, 0 }, "seek 10" },
367 { { MOUSE_BTN4, 0 }, "seek -10" },
368 { { MOUSE_BTN5, 0 }, "volume 1" },
369 { { MOUSE_BTN6, 0 }, "volume -1" },
371 #ifdef CONFIG_DVDNAV
372 { { KEY_KP8, 0 }, "dvdnav up" }, // up
373 { { KEY_KP2, 0 }, "dvdnav down" }, // down
374 { { KEY_KP4, 0 }, "dvdnav left" }, // left
375 { { KEY_KP6, 0 }, "dvdnav right" }, // right
376 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
377 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
378 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
379 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
380 #endif
382 { { KEY_RIGHT, 0 }, "seek 10" },
383 { { KEY_LEFT, 0 }, "seek -10" },
384 { { KEY_UP, 0 }, "seek 60" },
385 { { KEY_DOWN, 0 }, "seek -60" },
386 { { KEY_PAGE_UP, 0 }, "seek 600" },
387 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
388 { { '+', 0 }, "audio_delay 0.100" },
389 { { '-', 0 }, "audio_delay -0.100" },
390 { { '[', 0 }, "speed_mult 0.9091" },
391 { { ']', 0 }, "speed_mult 1.1" },
392 { { '{', 0 }, "speed_mult 0.5" },
393 { { '}', 0 }, "speed_mult 2.0" },
394 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
395 { { 'q', 0 }, "quit" },
396 { { KEY_ESC, 0 }, "quit" },
397 { { 'p', 0 }, "pause" },
398 { { ' ', 0 }, "pause" },
399 { { '.', 0 }, "frame_step" },
400 { { KEY_HOME, 0 }, "pt_up_step 1" },
401 { { KEY_END, 0 }, "pt_up_step -1" },
402 { { '>', 0 }, "pt_step 1" },
403 { { KEY_ENTER, 0 }, "pt_step 1 1" },
404 { { '<', 0 }, "pt_step -1" },
405 { { KEY_INS, 0 }, "alt_src_step 1" },
406 { { KEY_DEL, 0 }, "alt_src_step -1" },
407 { { 'o', 0 }, "osd" },
408 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
409 { { 'P', 0 }, "osd_show_progression" },
410 { { 'z', 0 }, "sub_delay -0.1" },
411 { { 'x', 0 }, "sub_delay +0.1" },
412 { { 'g', 0 }, "sub_step -1" },
413 { { 'y', 0 }, "sub_step +1" },
414 { { '9', 0 }, "volume -1" },
415 { { '/', 0 }, "volume -1" },
416 { { '0', 0 }, "volume 1" },
417 { { '*', 0 }, "volume 1" },
418 { { '(', 0 }, "balance -0.1" },
419 { { ')', 0 }, "balance 0.1" },
420 { { 'm', 0 }, "mute" },
421 { { '1', 0 }, "contrast -1" },
422 { { '2', 0 }, "contrast 1" },
423 { { '3', 0 }, "brightness -1" },
424 { { '4', 0 }, "brightness 1" },
425 { { '5', 0 }, "hue -1" },
426 { { '6', 0 }, "hue 1" },
427 { { '7', 0 }, "saturation -1" },
428 { { '8', 0 }, "saturation 1" },
429 { { 'd', 0 }, "frame_drop" },
430 { { 'D', 0 }, "step_property deinterlace" },
431 { { 'r', 0 }, "sub_pos -1" },
432 { { 't', 0 }, "sub_pos +1" },
433 { { 'a', 0 }, "sub_alignment" },
434 { { 'v', 0 }, "sub_visibility" },
435 { { 'j', 0 }, "sub_select" },
436 { { 'F', 0 }, "forced_subs_only" },
437 { { '#', 0 }, "switch_audio" },
438 { { '_', 0 }, "step_property switch_video" },
439 { { KEY_TAB, 0 }, "step_property switch_program" },
440 { { 'i', 0 }, "edl_mark" },
441 #ifdef CONFIG_TV
442 { { 'h', 0 }, "tv_step_channel 1" },
443 { { 'k', 0 }, "tv_step_channel -1" },
444 { { 'n', 0 }, "tv_step_norm" },
445 { { 'u', 0 }, "tv_step_chanlist" },
446 #endif
447 { { 'X', 0 }, "step_property teletext_mode 1" },
448 { { 'W', 0 }, "step_property teletext_page 1" },
449 { { 'Q', 0 }, "step_property teletext_page -1" },
450 #ifdef CONFIG_JOYSTICK
451 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
452 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
453 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
454 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
455 { { JOY_BTN0, 0 }, "pause" },
456 { { JOY_BTN1, 0 }, "osd" },
457 { { JOY_BTN2, 0 }, "volume 1"},
458 { { JOY_BTN3, 0 }, "volume -1"},
459 #endif
460 #ifdef CONFIG_APPLE_REMOTE
461 { { AR_PLAY, 0}, "pause" },
462 { { AR_PLAY_HOLD, 0}, "quit" },
463 { { AR_NEXT, 0 }, "seek 30" },
464 { { AR_NEXT_HOLD, 0 }, "seek 120" },
465 { { AR_PREV, 0 }, "seek -10" },
466 { { AR_PREV_HOLD, 0 }, "seek -120" },
467 { { AR_MENU, 0 }, "osd" },
468 { { AR_MENU_HOLD, 0 }, "mute" },
469 { { AR_VUP, 0 }, "volume 1"},
470 { { AR_VDOWN, 0 }, "volume -1"},
471 #endif
472 { { 'T', 0 }, "vo_ontop" },
473 { { 'f', 0 }, "vo_fullscreen" },
474 { { 's', 0 }, "screenshot 0" },
475 { { 'S', 0 }, "screenshot 1" },
476 { { 'w', 0 }, "panscan -0.1" },
477 { { 'e', 0 }, "panscan +0.1" },
479 { { KEY_POWER, 0 }, "quit" },
480 { { KEY_MENU, 0 }, "osd" },
481 { { KEY_PLAY, 0 }, "pause" },
482 { { KEY_PAUSE, 0 }, "pause" },
483 { { KEY_PLAYPAUSE, 0 }, "pause" },
484 { { KEY_STOP, 0 }, "quit" },
485 { { KEY_FORWARD, 0 }, "seek 60" },
486 { { KEY_REWIND, 0 }, "seek -60" },
487 { { KEY_NEXT, 0 }, "pt_step 1" },
488 { { KEY_PREV, 0 }, "pt_step -1" },
489 { { KEY_VOLUME_UP, 0 }, "volume 1" },
490 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
491 { { KEY_MUTE, 0 }, "mute" },
493 { { KEY_CLOSE_WIN, 0 }, "quit" },
495 { { '!', 0 }, "seek_chapter -1" },
496 { { '@', 0 }, "seek_chapter 1" },
497 { { 'A', 0 }, "switch_angle 1" },
498 { { 'U', 0 }, "stop" },
500 { { 0 }, NULL }
504 #ifdef CONFIG_GUI
505 static const mp_cmd_bind_t gui_def_cmd_binds[] = {
507 { { 'l', 0 }, "gui_loadfile" },
508 { { 't', 0 }, "gui_loadsubtitle" },
509 { { KEY_ENTER, 0 }, "gui_play" },
510 { { KEY_ESC, 0 }, "gui_stop" },
511 { { 'p', 0 }, "gui_playlist" },
512 { { 'r', 0 }, "gui_preferences" },
513 { { 'c', 0 }, "gui_skinbrowser" },
515 { { 0 }, NULL }
517 #endif
519 #ifndef MP_MAX_KEY_FD
520 #define MP_MAX_KEY_FD 10
521 #endif
523 #ifndef MP_MAX_CMD_FD
524 #define MP_MAX_CMD_FD 10
525 #endif
527 #define CMD_QUEUE_SIZE 100
529 typedef struct mp_input_fd {
530 int fd;
531 void* read_func;
532 mp_close_func_t close_func;
533 unsigned eof : 1;
534 unsigned drop : 1;
535 unsigned dead : 1;
536 unsigned got_cmd : 1;
537 unsigned no_select : 1;
538 unsigned no_readfunc_retval : 1;
539 // These fields are for the cmd fds.
540 char* buffer;
541 int pos,size;
542 } mp_input_fd_t;
544 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
546 struct mp_cmd_filter_st {
547 mp_input_cmd_filter filter;
548 void* ctx;
549 mp_cmd_filter_t* next;
552 typedef struct mp_cmd_bind_section_st mp_cmd_bind_section_t;
554 struct mp_cmd_bind_section_st {
555 mp_cmd_bind_t* cmd_binds;
556 char* section;
557 mp_cmd_bind_section_t* next;
560 // These are the user defined binds
561 static mp_cmd_bind_section_t* cmd_binds_section = NULL;
562 static char* section = NULL;
563 static mp_cmd_bind_t* cmd_binds = NULL;
564 static mp_cmd_bind_t* cmd_binds_default = NULL;
565 static mp_cmd_filter_t* cmd_filters = NULL;
567 // Callback to allow the menu filter to grab the incoming keys
568 int (*mp_input_key_cb)(int code) = NULL;
570 int async_quit_request;
572 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
573 static unsigned int num_key_fd = 0;
574 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
575 static unsigned int num_cmd_fd = 0;
576 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
577 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
579 // this is the key currently down
580 static int key_down[MP_MAX_KEY_DOWN];
581 static unsigned int num_key_down = 0, last_key_down = 0;
583 // Autorepeat stuff
584 static short ar_state = -1;
585 static mp_cmd_t* ar_cmd = NULL;
586 static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;
588 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
589 static int default_bindings = 1;
590 static char* config_file = "input.conf";
592 /* Apple Remote */
593 #ifdef CONFIG_APPLE_REMOTE
594 static int use_ar = 1;
595 #else
596 static int use_ar = 0;
597 #endif
599 static char* js_dev = NULL;
600 static char* ar_dev = NULL;
602 static char* in_file = NULL;
603 static int in_file_fd = -1;
605 static int mp_input_print_key_list(m_option_t* cfg);
606 static int mp_input_print_cmd_list(m_option_t* cfg);
608 // Our command line options
609 static const m_option_t input_conf[] = {
610 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
611 { "ar-dev", &ar_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
612 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
613 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
614 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
615 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
616 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
617 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
618 { "default-bindings", &default_bindings, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
619 { "nodefault-bindings", &default_bindings, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
620 { NULL, NULL, 0, 0, 0, 0, NULL}
623 static const m_option_t mp_input_opts[] = {
624 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
625 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
626 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
627 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
628 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
629 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
630 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
631 { "noar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
632 { "ar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
633 { NULL, NULL, 0, 0, 0, 0, NULL}
636 static int
637 mp_input_default_cmd_func(int fd,char* buf, int l);
639 static char*
640 mp_input_get_key_name(int key);
644 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
645 if(num_cmd_fd == MP_MAX_CMD_FD) {
646 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
647 return 0;
649 if (select && fd < 0) {
650 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
651 return 0;
654 memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t));
655 cmd_fds[num_cmd_fd].fd = fd;
656 cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func;
657 cmd_fds[num_cmd_fd].close_func = close_func;
658 cmd_fds[num_cmd_fd].no_select = !select;
659 num_cmd_fd++;
661 return 1;
664 void
665 mp_input_rm_cmd_fd(int fd) {
666 unsigned int i;
668 for(i = 0; i < num_cmd_fd; i++) {
669 if(cmd_fds[i].fd == fd)
670 break;
672 if(i == num_cmd_fd)
673 return;
674 if(cmd_fds[i].close_func)
675 cmd_fds[i].close_func(cmd_fds[i].fd);
676 if(cmd_fds[i].buffer)
677 free(cmd_fds[i].buffer);
679 if(i + 1 < num_cmd_fd)
680 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
681 num_cmd_fd--;
684 void
685 mp_input_rm_key_fd(int fd) {
686 unsigned int i;
688 for(i = 0; i < num_key_fd; i++) {
689 if(key_fds[i].fd == fd)
690 break;
692 if(i == num_key_fd)
693 return;
694 if(key_fds[i].close_func)
695 key_fds[i].close_func(key_fds[i].fd);
697 if(i + 1 < num_key_fd)
698 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
699 num_key_fd--;
703 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) {
704 if(num_key_fd == MP_MAX_KEY_FD) {
705 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
706 return 0;
708 if (select && fd < 0) {
709 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
710 return 0;
713 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
714 key_fds[num_key_fd].fd = fd;
715 key_fds[num_key_fd].read_func = read_func;
716 key_fds[num_key_fd].close_func = close_func;
717 key_fds[num_key_fd].no_select = !select;
718 num_key_fd++;
720 return 1;
724 mp_input_add_event_fd(int fd, void (*read_func)(void))
726 if(num_key_fd == MP_MAX_KEY_FD) {
727 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
728 return 0;
730 if (fd < 0) {
731 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_event_fd", fd);
732 return 0;
735 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
736 key_fds[num_key_fd].fd = fd;
737 key_fds[num_key_fd].read_func = read_func;
738 key_fds[num_key_fd].close_func = NULL;
739 key_fds[num_key_fd].no_readfunc_retval = 1;
740 num_key_fd++;
742 return 1;
745 void mp_input_rm_event_fd(int fd)
747 mp_input_rm_key_fd(fd);
750 int mp_input_parse_and_queue_cmds(const char *str) {
751 int cmd_num = 0;
753 while (*str == '\n' || *str == '\r' || *str == ' ')
754 ++str;
755 while (*str) {
756 mp_cmd_t *cmd;
757 size_t len = strcspn(str, "\r\n");
758 char *cmdbuf = malloc(len+1);
759 av_strlcpy(cmdbuf, str, len+1);
760 cmd = mp_input_parse_cmd(cmdbuf);
761 if (cmd) {
762 mp_input_queue_cmd(cmd);
763 ++cmd_num;
765 str += len;
766 while (*str == '\n' || *str == '\r' || *str == ' ')
767 ++str;
768 free(cmdbuf);
770 return cmd_num;
773 mp_cmd_t*
774 mp_input_parse_cmd(char* str) {
775 int i,l;
776 int pausing = -1;
777 char *ptr,*e;
778 mp_cmd_t *cmd;
779 const mp_cmd_t *cmd_def;
781 #ifdef MP_DEBUG
782 assert(str != NULL);
783 #endif
785 // Ignore heading spaces.
786 while (str[0] == ' ' || str[0] == '\t')
787 ++str;
789 if (strncmp(str, "pausing ", 8) == 0) {
790 pausing = 1;
791 str = &str[8];
792 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
793 pausing = 2;
794 str = &str[13];
795 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
796 pausing = 3;
797 str = &str[15];
798 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
799 pausing = 4;
800 str = &str[19];
803 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
804 /* NOTHING */;
805 if(ptr[0] != '\0')
806 l = ptr-str;
807 else
808 l = strlen(str);
810 if(l == 0)
811 return NULL;
813 for(i=0; mp_cmds[i].name != NULL; i++) {
814 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
815 break;
818 if(mp_cmds[i].name == NULL)
819 return NULL;
821 cmd_def = &mp_cmds[i];
823 cmd = calloc(1, sizeof(mp_cmd_t));
824 cmd->id = cmd_def->id;
825 cmd->name = strdup(cmd_def->name);
826 if (pausing == -1) {
827 switch (cmd->id) {
828 case MP_CMD_KEYDOWN_EVENTS:
829 case MP_CMD_SET_MOUSE_POS:
830 pausing = 4; break;
831 default:
832 pausing = 0; break;
835 cmd->pausing = pausing;
837 ptr = str;
839 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
840 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
841 if(ptr[0] == '\0') break;
842 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
843 if(ptr[0] == '\0' || ptr[0] == '#') break;
844 cmd->args[i].type = cmd_def->args[i].type;
845 switch(cmd_def->args[i].type) {
846 case MP_CMD_ARG_INT:
847 errno = 0;
848 cmd->args[i].v.i = atoi(ptr);
849 if(errno != 0) {
850 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
851 ptr = NULL;
853 break;
854 case MP_CMD_ARG_FLOAT:
855 errno = 0;
856 cmd->args[i].v.f = atof(ptr);
857 if(errno != 0) {
858 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
859 ptr = NULL;
861 break;
862 case MP_CMD_ARG_STRING: {
863 char term;
864 char* ptr2 = ptr, *start;
866 if(ptr[0] == '\'' || ptr[0] == '"') {
867 term = ptr[0];
868 ptr2++;
869 } else
870 term = ' ';
871 start = ptr2;
872 while(1) {
873 e = strchr(ptr2,term);
874 if(!e) break;
875 if(e <= ptr2 || *(e - 1) != '\\') break;
876 ptr2 = e + 1;
879 if(term != ' ' && (!e || e[0] == '\0')) {
880 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
881 ptr = NULL;
882 break;
883 } else if(!e) e = ptr+strlen(ptr);
884 l = e-start;
885 ptr2 = start;
886 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
887 memmove(e,e+1,strlen(e));
888 ptr2 = e + 1;
889 l--;
891 cmd->args[i].v.s = malloc(l+1);
892 strncpy(cmd->args[i].v.s,start,l);
893 cmd->args[i].v.s[l] = '\0';
894 if(term != ' ') ptr += l+2;
895 } break;
896 case -1:
897 ptr = NULL;
898 break;
899 default :
900 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
903 cmd->nargs = i;
905 if(cmd_def->nargs > cmd->nargs) {
906 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
907 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
908 mp_cmd_free(cmd);
909 return NULL;
912 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
913 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
914 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
915 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
918 if(i < MP_CMD_MAX_ARGS)
919 cmd->args[i].type = -1;
921 return cmd;
924 #define MP_CMD_MAX_SIZE 4096
926 static int
927 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
928 char* end;
929 (*ret) = NULL;
931 // Allocate the buffer if it doesn't exist
932 if(!mp_fd->buffer) {
933 mp_fd->buffer = malloc(MP_CMD_MAX_SIZE);
934 mp_fd->pos = 0;
935 mp_fd->size = MP_CMD_MAX_SIZE;
938 // Get some data if needed/possible
939 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
940 int r = ((mp_cmd_func_t)mp_fd->read_func)(mp_fd->fd,mp_fd->buffer+mp_fd->pos,mp_fd->size - 1 - mp_fd->pos);
941 // Error ?
942 if(r < 0) {
943 switch(r) {
944 case MP_INPUT_ERROR:
945 case MP_INPUT_DEAD:
946 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
947 case MP_INPUT_NOTHING:
948 return r;
949 case MP_INPUT_RETRY:
950 continue;
952 // EOF ?
953 } else if(r == 0) {
954 mp_fd->eof = 1;
955 break;
957 mp_fd->pos += r;
958 break;
961 mp_fd->got_cmd = 0;
963 while(1) {
964 int l = 0;
965 // Find the cmd end
966 mp_fd->buffer[mp_fd->pos] = '\0';
967 end = strchr(mp_fd->buffer,'\r');
968 if (end) *end = '\n';
969 end = strchr(mp_fd->buffer,'\n');
970 // No cmd end ?
971 if(!end) {
972 // If buffer is full we must drop all until the next \n
973 if(mp_fd->size - mp_fd->pos <= 1) {
974 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
975 mp_fd->pos = 0;
976 mp_fd->drop = 1;
978 break;
980 // We already have a cmd : set the got_cmd flag
981 else if((*ret)) {
982 mp_fd->got_cmd = 1;
983 break;
986 l = end - mp_fd->buffer;
988 // Not dropping : put the cmd in ret
989 if (!mp_fd->drop) {
990 (*ret) = malloc(l+1);
991 strncpy((*ret),mp_fd->buffer,l);
992 (*ret)[l] = '\0';
993 } else { // Remove the dropping flag
994 mp_fd->drop = 0;
996 if( mp_fd->pos - (l+1) > 0)
997 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
998 mp_fd->pos -= l+1;
1001 if(*ret)
1002 return 1;
1003 else
1004 return MP_INPUT_NOTHING;
1007 static int
1008 mp_input_default_cmd_func(int fd,char* buf, int l) {
1010 while(1) {
1011 int r = read(fd,buf,l);
1012 // Error ?
1013 if(r < 0) {
1014 if(errno == EINTR)
1015 continue;
1016 else if(errno == EAGAIN)
1017 return MP_INPUT_NOTHING;
1018 return MP_INPUT_ERROR;
1019 // EOF ?
1021 return r;
1027 void
1028 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
1029 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
1031 filter->filter = func;
1032 filter->ctx = ctx;
1033 filter->next = cmd_filters;
1034 cmd_filters = filter;
1038 static char*
1039 mp_input_find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys) {
1040 int j;
1042 if (n <= 0) return NULL;
1043 for(j = 0; binds[j].cmd != NULL; j++) {
1044 int found = 1,s;
1045 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1046 if(binds[j].input[s] != keys[s]) {
1047 found = 0;
1048 break;
1051 if(found && binds[j].input[s] == 0 && s == n)
1052 break;
1054 return binds[j].cmd;
1057 static mp_cmd_bind_section_t*
1058 mp_input_get_bind_section(char *section) {
1059 mp_cmd_bind_section_t* bind_section = cmd_binds_section;
1061 if (section==NULL) section="default";
1062 while (bind_section) {
1063 if(strcmp(section,bind_section->section)==0) return bind_section;
1064 if(bind_section->next==NULL) break;
1065 bind_section=bind_section->next;
1067 if(bind_section) {
1068 bind_section->next=malloc(sizeof(mp_cmd_bind_section_t));
1069 bind_section=bind_section->next;
1070 } else {
1071 cmd_binds_section=malloc(sizeof(mp_cmd_bind_section_t));
1072 bind_section=cmd_binds_section;
1074 bind_section->cmd_binds=NULL;
1075 bind_section->section=strdup(section);
1076 bind_section->next=NULL;
1077 return bind_section;
1080 static mp_cmd_t*
1081 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
1082 char* cmd = NULL;
1083 mp_cmd_t* ret;
1085 if(cmd_binds)
1086 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
1087 if(cmd_binds_default && cmd == NULL)
1088 cmd = mp_input_find_bind_for_key(cmd_binds_default,n,keys);
1089 if(default_bindings && cmd == NULL)
1090 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
1092 if(cmd == NULL) {
1093 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound,mp_input_get_key_name(keys[0]));
1094 if(n > 1) {
1095 int s;
1096 for(s=1; s < n; s++)
1097 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
1099 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1100 return NULL;
1102 if (strcmp(cmd, "ignore") == 0) return NULL;
1103 ret = mp_input_parse_cmd(cmd);
1104 if(!ret) {
1105 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,mp_input_get_key_name(key_down[0]));
1106 if( num_key_down > 1) {
1107 unsigned int s;
1108 for(s=1; s < num_key_down; s++)
1109 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(key_down[s]));
1111 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1113 return ret;
1117 static mp_cmd_t*
1118 interpret_key(int code, int paused)
1120 unsigned int j;
1121 mp_cmd_t* ret;
1123 if(mp_input_key_cb) {
1124 if (code & MP_KEY_DOWN)
1125 return NULL;
1126 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1127 if (mp_input_key_cb(code))
1128 return NULL;
1131 if(code & MP_KEY_DOWN) {
1132 if(num_key_down > MP_MAX_KEY_DOWN) {
1133 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1134 return NULL;
1136 code &= ~MP_KEY_DOWN;
1137 // Check if we don't already have this key as pushed
1138 for(j = 0; j < num_key_down; j++) {
1139 if(key_down[j] == code)
1140 break;
1142 if(j != num_key_down)
1143 return NULL;
1144 key_down[num_key_down] = code;
1145 num_key_down++;
1146 last_key_down = GetTimer();
1147 ar_state = 0;
1148 return NULL;
1150 // key released
1151 // Check if the key is in the down key, driver which can't send push event
1152 // send only release event
1153 for(j = 0; j < num_key_down; j++) {
1154 if(key_down[j] == code)
1155 break;
1157 if(j == num_key_down) { // key was not in the down keys : add it
1158 if(num_key_down > MP_MAX_KEY_DOWN) {
1159 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1160 return NULL;
1162 key_down[num_key_down] = code;
1163 num_key_down++;
1164 last_key_down = 1;
1166 // We ignore key from last combination
1167 ret = last_key_down ? mp_input_get_cmd_from_keys(num_key_down,key_down,paused) : NULL;
1168 // Remove the key
1169 if(j+1 < num_key_down)
1170 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1171 num_key_down--;
1172 last_key_down = 0;
1173 ar_state = -1;
1174 if(ar_cmd) {
1175 mp_cmd_free(ar_cmd);
1176 ar_cmd = NULL;
1178 return ret;
1181 static mp_cmd_t *check_autorepeat(int paused)
1183 // No input : autorepeat ?
1184 if(ar_rate > 0 && ar_state >=0 && num_key_down > 0 && ! (key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1185 unsigned int t = GetTimer();
1186 // First time : wait delay
1187 if(ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1188 ar_cmd = mp_input_get_cmd_from_keys(num_key_down,key_down,paused);
1189 if(!ar_cmd) {
1190 ar_state = -1;
1191 return NULL;
1193 ar_state = 1;
1194 last_ar = t;
1195 return mp_cmd_clone(ar_cmd);
1196 // Then send rate / sec event
1197 } else if(ar_state == 1 && (t -last_ar) >= 1000000/ar_rate) {
1198 last_ar = t;
1199 return mp_cmd_clone(ar_cmd);
1202 return NULL;
1206 static mp_cmd_t *read_events(int time, int paused)
1208 int i;
1209 int got_cmd = 0;
1210 mp_cmd_t *autorepeat_cmd;
1211 #ifdef HAVE_POSIX_SELECT
1212 fd_set fds;
1213 #endif
1214 for (i = 0; i < num_key_fd; i++)
1215 if (key_fds[i].dead) {
1216 mp_input_rm_key_fd(key_fds[i].fd);
1217 i--;
1219 for (i = 0; i < num_cmd_fd; i++)
1220 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1221 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1222 i--;
1224 else if (cmd_fds[i].got_cmd)
1225 got_cmd = 1;
1226 #ifdef HAVE_POSIX_SELECT
1227 FD_ZERO(&fds);
1228 if (!got_cmd) {
1229 int max_fd = 0, num_fd = 0;
1230 for (i = 0; i < num_key_fd; i++) {
1231 if (key_fds[i].no_select)
1232 continue;
1233 if (key_fds[i].fd > max_fd)
1234 max_fd = key_fds[i].fd;
1235 FD_SET(key_fds[i].fd, &fds);
1236 num_fd++;
1238 for (i = 0; i < num_cmd_fd; i++) {
1239 if (cmd_fds[i].no_select)
1240 continue;
1241 if (cmd_fds[i].fd > max_fd)
1242 max_fd = cmd_fds[i].fd;
1243 FD_SET(cmd_fds[i].fd, &fds);
1244 num_fd++;
1246 if (num_fd > 0) {
1247 struct timeval tv, *time_val;
1248 if (time >= 0) {
1249 tv.tv_sec = time / 1000;
1250 tv.tv_usec = (time % 1000) * 1000;
1251 time_val = &tv;
1253 else
1254 time_val = NULL;
1255 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1256 if (errno != EINTR)
1257 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrSelect,
1258 strerror(errno));
1259 FD_ZERO(&fds);
1263 #else
1264 if (!got_cmd && time)
1265 usec_sleep(time * 1000);
1266 #endif
1269 for (i = 0; i < num_key_fd; i++) {
1270 int code;
1271 #ifdef HAVE_POSIX_SELECT
1272 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1273 continue;
1274 #endif
1276 if (key_fds[i].no_readfunc_retval) { // getch2 handler special-cased for now
1277 ((void (*)(void))key_fds[i].read_func)();
1278 if (cmd_queue_length)
1279 return NULL;
1280 code = mplayer_get_key(0);
1281 if (code < 0)
1282 code = MP_INPUT_NOTHING;
1284 else
1285 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
1286 if (code >= 0) {
1287 mp_cmd_t *ret = interpret_key(code, paused);
1288 if (ret)
1289 return ret;
1291 else if (code == MP_INPUT_ERROR)
1292 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1293 key_fds[i].fd);
1294 else if (code == MP_INPUT_DEAD) {
1295 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1296 key_fds[i].fd);
1297 key_fds[i].dead = 1;
1300 autorepeat_cmd = check_autorepeat(paused);
1301 if (autorepeat_cmd)
1302 return autorepeat_cmd;
1304 for (i = 0; i < num_cmd_fd; i++) {
1305 char *cmd;
1306 int r;
1307 #ifdef HAVE_POSIX_SELECT
1308 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1309 !cmd_fds[i].got_cmd)
1310 continue;
1311 #endif
1312 r = mp_input_read_cmd(&cmd_fds[i], &cmd);
1313 if (r >= 0) {
1314 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1315 free(cmd);
1316 if (ret)
1317 return ret;
1319 else if (r == MP_INPUT_ERROR)
1320 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1321 cmd_fds[i].fd);
1322 else if (r == MP_INPUT_DEAD)
1323 cmd_fds[i].dead = 1;
1326 return NULL;
1331 mp_input_queue_cmd(mp_cmd_t* cmd) {
1332 if(!cmd || cmd_queue_length >= CMD_QUEUE_SIZE)
1333 return 0;
1334 cmd_queue[cmd_queue_end] = cmd;
1335 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1336 cmd_queue_length++;
1337 return 1;
1340 static mp_cmd_t*
1341 mp_input_get_queued_cmd(int peek_only) {
1342 mp_cmd_t* ret;
1344 if(cmd_queue_length == 0)
1345 return NULL;
1347 ret = cmd_queue[cmd_queue_start];
1349 if (!peek_only) {
1350 cmd_queue_length--;
1351 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1354 return ret;
1358 * \param peek_only when set, the returned command stays in the queue.
1359 * Do not free the returned cmd whe you set this!
1361 mp_cmd_t*
1362 mp_input_get_cmd(int time, int paused, int peek_only) {
1363 mp_cmd_t* ret = NULL;
1364 mp_cmd_filter_t* cf;
1365 int from_queue;
1367 if (async_quit_request)
1368 return mp_input_parse_cmd("quit 1");
1369 while(1) {
1370 from_queue = 1;
1371 ret = mp_input_get_queued_cmd(peek_only);
1372 if(ret) break;
1373 from_queue = 0;
1374 ret = read_events(time, paused);
1375 if (!ret) {
1376 from_queue = 1;
1377 ret = mp_input_get_queued_cmd(peek_only);
1379 break;
1381 if(!ret) return NULL;
1383 for(cf = cmd_filters ; cf ; cf = cf->next) {
1384 if(cf->filter(ret,paused,cf->ctx)) {
1385 if (peek_only && from_queue)
1386 // The filter ate the cmd, so we remove it from queue
1387 ret = mp_input_get_queued_cmd(0);
1388 mp_cmd_free(ret);
1389 return NULL;
1393 if (!from_queue && peek_only)
1394 mp_input_queue_cmd(ret);
1396 return ret;
1399 void
1400 mp_cmd_free(mp_cmd_t* cmd) {
1401 int i;
1402 //#ifdef MP_DEBUG
1403 // assert(cmd != NULL);
1404 //#endif
1405 if ( !cmd ) return;
1407 if(cmd->name)
1408 free(cmd->name);
1410 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1411 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1412 free(cmd->args[i].v.s);
1414 free(cmd);
1417 mp_cmd_t*
1418 mp_cmd_clone(mp_cmd_t* cmd) {
1419 mp_cmd_t* ret;
1420 int i;
1421 #ifdef MP_DEBUG
1422 assert(cmd != NULL);
1423 #endif
1425 ret = malloc(sizeof(mp_cmd_t));
1426 memcpy(ret,cmd,sizeof(mp_cmd_t));
1427 if(cmd->name)
1428 ret->name = strdup(cmd->name);
1429 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1430 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1431 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1434 return ret;
1437 static char key_str[12];
1439 static char*
1440 mp_input_get_key_name(int key) {
1441 int i;
1443 for(i = 0; key_names[i].name != NULL; i++) {
1444 if(key_names[i].key == key)
1445 return key_names[i].name;
1448 if(isascii(key)) {
1449 snprintf(key_str,12,"%c",(char)key);
1450 return key_str;
1453 // Print the hex key code
1454 snprintf(key_str,12,"%#-8x",key);
1455 return key_str;
1460 mp_input_get_key_from_name(const char *name) {
1461 int i,ret = 0,len = strlen(name);
1462 if(len == 1) { // Direct key code
1463 ret = (unsigned char)name[0];
1464 return ret;
1465 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1466 return strtol(name,NULL,16);
1468 for(i = 0; key_names[i].name != NULL; i++) {
1469 if(strcasecmp(key_names[i].name,name) == 0)
1470 return key_names[i].key;
1473 return -1;
1476 static int
1477 mp_input_get_input_from_name(char* name,int* keys) {
1478 char *end,*ptr;
1479 int n=0;
1481 ptr = name;
1482 n = 0;
1483 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1484 if(end && end[1] != '\0') {
1485 if(end[1] == '-')
1486 end = &end[1];
1487 end[0] = '\0';
1489 keys[n] = mp_input_get_key_from_name(ptr);
1490 if(keys[n] < 0) {
1491 return 0;
1493 n++;
1494 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1495 ptr = &end[1];
1496 else
1497 break;
1499 keys[n] = 0;
1500 return 1;
1503 #define BS_MAX 256
1504 #define SPACE_CHAR " \n\r\t"
1506 static void
1507 mp_input_bind_keys(const int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1508 int i = 0,j;
1509 mp_cmd_bind_t* bind = NULL;
1510 mp_cmd_bind_section_t* bind_section = NULL;
1511 char *section=NULL, *p;
1513 #ifdef MP_DEBUG
1514 assert(keys != NULL);
1515 assert(cmd != NULL);
1516 #endif
1518 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1519 *p=0;
1520 section=++cmd;
1521 cmd=++p;
1522 // Jump beginning space
1523 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1524 /* NOTHING */;
1526 bind_section=mp_input_get_bind_section(section);
1528 if(bind_section->cmd_binds) {
1529 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1530 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1531 /* NOTHING */;
1532 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1533 bind = &bind_section->cmd_binds[i];
1534 break;
1539 if(!bind) {
1540 bind_section->cmd_binds = realloc(bind_section->cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1541 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1542 bind = &bind_section->cmd_binds[i];
1544 if(bind->cmd)
1545 free(bind->cmd);
1546 bind->cmd = strdup(cmd);
1547 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1550 static void
1551 mp_input_add_binds(const mp_cmd_bind_t* list) {
1552 int i;
1553 for(i = 0 ; list[i].cmd ; i++)
1554 mp_input_bind_keys(list[i].input,list[i].cmd);
1557 static void
1558 mp_input_free_binds(mp_cmd_bind_t* binds) {
1559 int i;
1561 if(!binds)
1562 return;
1564 for(i = 0; binds[i].cmd != NULL; i++)
1565 free(binds[i].cmd);
1567 free(binds);
1571 static int
1572 mp_input_parse_config(char *file) {
1573 int fd;
1574 int bs = 0,r,eof = 0,comments = 0;
1575 char *iter,*end;
1576 char buffer[BS_MAX];
1577 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1579 fd = open(file,O_RDONLY);
1581 if(fd < 0) {
1582 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1583 return 0;
1586 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1588 while(1) {
1589 if(! eof && bs < BS_MAX-1) {
1590 if(bs > 0) bs--;
1591 r = read(fd,buffer+bs,BS_MAX-1-bs);
1592 if(r < 0) {
1593 if(errno == EINTR)
1594 continue;
1595 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1596 close(fd);
1597 return 0;
1598 } else if(r == 0) {
1599 eof = 1;
1600 } else {
1601 bs += r+1;
1602 buffer[bs-1] = '\0';
1605 // Empty buffer : return
1606 if(bs <= 1) {
1607 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1608 close(fd);
1609 return 1;
1612 iter = buffer;
1614 if(comments) {
1615 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1616 /* NOTHING */;
1617 if(iter[0] == '\0') { // Buffer was full of comment
1618 bs = 0;
1619 continue;
1621 iter++;
1622 r = strlen(iter);
1623 memmove(buffer,iter,r+1);
1624 bs = r+1;
1625 comments = 0;
1626 continue;
1629 // Find the wanted key
1630 if(keys[0] == 0) {
1631 // Jump beginning space
1632 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1633 /* NOTHING */;
1634 if(iter[0] == '\0') { // Buffer was full of space char
1635 bs = 0;
1636 continue;
1638 if(iter[0] == '#') { // Comments
1639 comments = 1;
1640 continue;
1642 // Find the end of the key code name
1643 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1644 /*NOTHING */;
1645 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1646 if(buffer == iter) {
1647 if(eof && (buffer-iter) == bs)
1648 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1649 else
1650 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1651 return 0;
1653 memmove(buffer,iter,end-iter);
1654 bs = end-iter;
1655 continue;
1658 char name[end-iter+1];
1659 strncpy(name,iter,end-iter);
1660 name[end-iter] = '\0';
1661 if(! mp_input_get_input_from_name(name,keys)) {
1662 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1663 close(fd);
1664 return 0;
1667 if( bs > (end-buffer))
1668 memmove(buffer,end,bs - (end-buffer));
1669 bs -= end-buffer;
1670 continue;
1671 } else { // Get the command
1672 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1673 // Found new line
1674 if(iter[0] == '\n' || iter[0] == '\r') {
1675 int i;
1676 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey,mp_input_get_key_name(keys[0]));
1677 for(i = 1; keys[i] != 0 ; i++)
1678 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1679 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1680 keys[0] = 0;
1681 if(iter > buffer) {
1682 memmove(buffer,iter,bs- (iter-buffer));
1683 bs -= (iter-buffer);
1685 continue;
1687 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1688 /* NOTHING */;
1689 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1690 if(iter == buffer) {
1691 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1692 close(fd);
1693 return 0;
1695 memmove(buffer,iter,end - iter);
1696 bs = end - iter;
1697 continue;
1700 char cmd[end-iter+1];
1701 strncpy(cmd,iter,end-iter);
1702 cmd[end-iter] = '\0';
1703 //printf("Set bind %d => %s\n",keys[0],cmd);
1704 mp_input_bind_keys(keys,cmd);
1705 n_binds++;
1707 keys[0] = 0;
1708 end++;
1709 if(bs > (end-buffer))
1710 memmove(buffer,end,bs-(end-buffer));
1711 bs -= (end-buffer);
1712 buffer[bs-1] = '\0';
1713 continue;
1716 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1717 close(fd);
1718 mp_input_set_section(NULL);
1719 return 0;
1722 void
1723 mp_input_set_section(char *name) {
1724 mp_cmd_bind_section_t* bind_section = NULL;
1726 cmd_binds=NULL;
1727 cmd_binds_default=NULL;
1728 if(section) free(section);
1729 if(name) section=strdup(name); else section=strdup("default");
1730 if((bind_section=mp_input_get_bind_section(section)))
1731 cmd_binds=bind_section->cmd_binds;
1732 if(strcmp(section,"default")==0) return;
1733 if((bind_section=mp_input_get_bind_section(NULL)))
1734 cmd_binds_default=bind_section->cmd_binds;
1737 char*
1738 mp_input_get_section(void) {
1739 return section;
1742 void
1743 mp_input_init(int use_gui) {
1744 char* file;
1746 #ifdef CONFIG_GUI
1747 if(use_gui)
1748 mp_input_add_binds(gui_def_cmd_binds);
1749 #endif
1751 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1752 if(!file)
1753 return;
1755 if( !mp_input_parse_config(file)) {
1756 // free file if it was allocated by get_path(),
1757 // before it gets overwritten
1758 if( file != config_file)
1760 free(file);
1762 // Try global conf dir
1763 file = MPLAYER_CONFDIR "/input.conf";
1764 if(! mp_input_parse_config(file))
1765 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1767 else
1769 // free file if it was allocated by get_path()
1770 if( file != config_file)
1771 free(file);
1774 #ifdef CONFIG_JOYSTICK
1775 if(use_joystick) {
1776 int fd = mp_input_joystick_init(js_dev);
1777 if(fd < 0)
1778 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1779 else
1780 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1782 #endif
1784 #ifdef CONFIG_LIRC
1785 if(use_lirc) {
1786 int fd = mp_input_lirc_init();
1787 if(fd > 0)
1788 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1790 #endif
1792 #ifdef CONFIG_LIRCC
1793 if(use_lircc) {
1794 int fd = lircc_init("mplayer", NULL);
1795 if(fd >= 0)
1796 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1798 #endif
1800 #ifdef CONFIG_APPLE_REMOTE
1801 if(use_ar) {
1802 if(mp_input_ar_init() < 0)
1803 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1804 else
1805 mp_input_add_key_fd(-1,0,mp_input_ar_read,mp_input_ar_close);
1807 #endif
1809 #ifdef CONFIG_APPLE_IR
1810 if(use_ar) {
1811 int fd = mp_input_appleir_init(ar_dev);
1812 if(fd < 0)
1813 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1814 else
1815 mp_input_add_key_fd(fd,1,mp_input_appleir_read,(mp_close_func_t)close);
1817 #endif
1819 if(in_file) {
1820 struct stat st;
1821 if(stat(in_file,&st))
1822 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,in_file,strerror(errno));
1823 else {
1824 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1825 if(in_file_fd >= 0)
1826 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1827 else
1828 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));
1834 void
1835 mp_input_uninit(void) {
1836 unsigned int i;
1837 mp_cmd_bind_section_t* bind_section;
1839 for(i=0; i < num_key_fd; i++) {
1840 if(key_fds[i].close_func)
1841 key_fds[i].close_func(key_fds[i].fd);
1844 for(i=0; i < num_cmd_fd; i++) {
1845 if(cmd_fds[i].close_func)
1846 cmd_fds[i].close_func(cmd_fds[i].fd);
1848 while (cmd_binds_section) {
1849 mp_input_free_binds(cmd_binds_section->cmd_binds);
1850 free(cmd_binds_section->section);
1851 bind_section=cmd_binds_section->next;
1852 free(cmd_binds_section);
1853 cmd_binds_section=bind_section;
1855 cmd_binds_section=NULL;
1858 void
1859 mp_input_register_options(m_config_t* cfg) {
1860 m_config_register_options(cfg,mp_input_opts);
1863 static int mp_input_print_key_list(m_option_t* cfg) {
1864 int i;
1865 printf("\n");
1866 for(i= 0; key_names[i].name != NULL ; i++)
1867 printf("%s\n",key_names[i].name);
1868 exit(0);
1871 static int mp_input_print_cmd_list(m_option_t* cfg) {
1872 const mp_cmd_t *cmd;
1873 int i,j;
1874 const char* type;
1876 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1877 printf("%-20.20s",cmd->name);
1878 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1879 switch(cmd->args[j].type) {
1880 case MP_CMD_ARG_INT:
1881 type = "Integer";
1882 break;
1883 case MP_CMD_ARG_FLOAT:
1884 type = "Float";
1885 break;
1886 case MP_CMD_ARG_STRING:
1887 type = "String";
1888 break;
1889 default:
1890 type = "??";
1892 if(j+1 > cmd->nargs)
1893 printf(" [%s]",type);
1894 else
1895 printf(" %s",type);
1897 printf("\n");
1899 exit(0);
1903 mp_input_check_interrupt(int time) {
1904 mp_cmd_t* cmd;
1905 if((cmd = mp_input_get_cmd(time,0,1)) == NULL)
1906 return 0;
1907 switch(cmd->id) {
1908 case MP_CMD_QUIT:
1909 case MP_CMD_PLAY_TREE_STEP:
1910 case MP_CMD_PLAY_TREE_UP_STEP:
1911 case MP_CMD_PLAY_ALT_SRC_STEP:
1912 // The cmd will be executed when we are back in the main loop
1913 return 1;
1915 // remove the cmd from the queue
1916 cmd = mp_input_get_cmd(time,0,0);
1917 mp_cmd_free(cmd);
1918 return 0;