Merge svn changes up to r30448
[mplayer/kovensky.git] / input / input.c
blobc5d93a5f17af6fb2e3da214532aa2a837f284094
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <ctype.h>
33 #include "input.h"
34 #include "mouse.h"
35 #ifdef MP_DEBUG
36 #include <assert.h>
37 #endif
38 #include "mp_fifo.h"
39 #include "osdep/keycodes.h"
40 #include "osdep/timer.h"
41 #include "libavutil/avstring.h"
42 #include "mp_msg.h"
43 #include "help_mp.h"
44 #include "m_config.h"
45 #include "m_option.h"
46 #include "get_path.h"
47 #include "talloc.h"
48 #include "options.h"
50 #include "joystick.h"
52 #ifdef CONFIG_LIRC
53 #include "lirc.h"
54 #endif
56 #ifdef CONFIG_LIRCC
57 #include <lirc/lircc.h>
58 #endif
60 #include "ar.h"
62 typedef struct mp_cmd_bind {
63 int input[MP_MAX_KEY_DOWN+1];
64 char* cmd;
65 } mp_cmd_bind_t;
67 typedef struct mp_key_name {
68 int key;
69 char* name;
70 } mp_key_name_t;
72 /// This array defines all known commands.
73 /// The first field is an id used to recognize the command without too many strcmp.
74 /// The second is obviously the command name.
75 /// The third is the minimum number of arguments this command needs.
76 /// Then comes the definition of each argument, terminated with an arg of type -1.
77 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
78 /// the last one) which is actually 9.
80 /// For the args, the first field is the type (actually int, float or string), the second
81 /// is the default value wich is used for optional arguments
83 static const mp_cmd_t mp_cmds[] = {
84 #ifdef CONFIG_RADIO
85 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
87 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
89 #endif
90 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
91 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
92 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
96 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
97 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
98 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
99 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
102 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
103 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
106 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
107 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
109 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
112 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
113 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
119 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
120 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
121 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
123 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
124 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
125 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
126 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
131 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
132 #ifdef CONFIG_ASS
133 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
134 #endif
135 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
136 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
137 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
138 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
139 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
141 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
142 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
144 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
145 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
152 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
153 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
155 #ifdef CONFIG_TV
156 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
157 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
158 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
159 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
160 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
161 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
162 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
165 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
166 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 #endif
170 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
171 #ifdef CONFIG_DVBIN
172 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
173 #endif
174 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
175 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
176 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
177 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
178 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
181 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
186 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
187 { MP_CMD_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}} } },
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_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
207 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { 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_STEP_PROPERTY_OSD, "step_property_osd", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
211 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
212 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
214 { 0, NULL, 0, {} }
217 /// The names of the keys as used in input.conf
218 /// If you add some new keys, you also need to add them here
220 static const mp_key_name_t key_names[] = {
221 { ' ', "SPACE" },
222 { '#', "SHARP" },
223 { KEY_ENTER, "ENTER" },
224 { KEY_TAB, "TAB" },
225 { KEY_CTRL, "CTRL" },
226 { KEY_BACKSPACE, "BS" },
227 { KEY_DELETE, "DEL" },
228 { KEY_INSERT, "INS" },
229 { KEY_HOME, "HOME" },
230 { KEY_END, "END" },
231 { KEY_PAGE_UP, "PGUP" },
232 { KEY_PAGE_DOWN, "PGDWN" },
233 { KEY_ESC, "ESC" },
234 { KEY_RIGHT, "RIGHT" },
235 { KEY_LEFT, "LEFT" },
236 { KEY_DOWN, "DOWN" },
237 { KEY_UP, "UP" },
238 { KEY_F+1, "F1" },
239 { KEY_F+2, "F2" },
240 { KEY_F+3, "F3" },
241 { KEY_F+4, "F4" },
242 { KEY_F+5, "F5" },
243 { KEY_F+6, "F6" },
244 { KEY_F+7, "F7" },
245 { KEY_F+8, "F8" },
246 { KEY_F+9, "F9" },
247 { KEY_F+10, "F10" },
248 { KEY_F+11, "F11" },
249 { KEY_F+12, "F12" },
250 { KEY_KP0, "KP0" },
251 { KEY_KP1, "KP1" },
252 { KEY_KP2, "KP2" },
253 { KEY_KP3, "KP3" },
254 { KEY_KP4, "KP4" },
255 { KEY_KP5, "KP5" },
256 { KEY_KP6, "KP6" },
257 { KEY_KP7, "KP7" },
258 { KEY_KP8, "KP8" },
259 { KEY_KP9, "KP9" },
260 { KEY_KPDEL, "KP_DEL" },
261 { KEY_KPDEC, "KP_DEC" },
262 { KEY_KPINS, "KP_INS" },
263 { KEY_KPENTER, "KP_ENTER" },
264 { MOUSE_BTN0, "MOUSE_BTN0" },
265 { MOUSE_BTN1, "MOUSE_BTN1" },
266 { MOUSE_BTN2, "MOUSE_BTN2" },
267 { MOUSE_BTN3, "MOUSE_BTN3" },
268 { MOUSE_BTN4, "MOUSE_BTN4" },
269 { MOUSE_BTN5, "MOUSE_BTN5" },
270 { MOUSE_BTN6, "MOUSE_BTN6" },
271 { MOUSE_BTN7, "MOUSE_BTN7" },
272 { MOUSE_BTN8, "MOUSE_BTN8" },
273 { MOUSE_BTN9, "MOUSE_BTN9" },
274 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
275 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
276 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
277 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
278 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
279 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
280 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
281 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
282 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
283 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
284 { JOY_AXIS1_MINUS, "JOY_UP" },
285 { JOY_AXIS1_PLUS, "JOY_DOWN" },
286 { JOY_AXIS0_MINUS, "JOY_LEFT" },
287 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
289 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
290 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
291 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
292 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
293 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
294 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
295 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
296 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
297 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
298 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
299 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
300 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
301 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
302 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
303 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
304 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
305 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
306 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
307 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
308 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
310 { JOY_BTN0, "JOY_BTN0" },
311 { JOY_BTN1, "JOY_BTN1" },
312 { JOY_BTN2, "JOY_BTN2" },
313 { JOY_BTN3, "JOY_BTN3" },
314 { JOY_BTN4, "JOY_BTN4" },
315 { JOY_BTN5, "JOY_BTN5" },
316 { JOY_BTN6, "JOY_BTN6" },
317 { JOY_BTN7, "JOY_BTN7" },
318 { JOY_BTN8, "JOY_BTN8" },
319 { JOY_BTN9, "JOY_BTN9" },
321 { AR_PLAY, "AR_PLAY" },
322 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
323 { AR_NEXT, "AR_NEXT" },
324 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
325 { AR_PREV, "AR_PREV" },
326 { AR_PREV_HOLD, "AR_PREV_HOLD" },
327 { AR_MENU, "AR_MENU" },
328 { AR_MENU_HOLD, "AR_MENU_HOLD" },
329 { AR_VUP, "AR_VUP" },
330 { AR_VDOWN, "AR_VDOWN" },
332 { KEY_POWER, "POWER" },
333 { KEY_MENU, "MENU" },
334 { KEY_PLAY, "PLAY" },
335 { KEY_PAUSE, "PAUSE" },
336 { KEY_PLAYPAUSE, "PLAYPAUSE" },
337 { KEY_STOP, "STOP" },
338 { KEY_FORWARD, "FORWARD" },
339 { KEY_REWIND, "REWIND" },
340 { KEY_NEXT, "NEXT" },
341 { KEY_PREV, "PREV" },
342 { KEY_VOLUME_UP, "VOLUME_UP" },
343 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
344 { KEY_MUTE, "MUTE" },
346 // These are kept for backward compatibility
347 { KEY_PAUSE, "XF86_PAUSE" },
348 { KEY_STOP, "XF86_STOP" },
349 { KEY_PREV, "XF86_PREV" },
350 { KEY_NEXT, "XF86_NEXT" },
352 { KEY_CLOSE_WIN, "CLOSE_WIN" },
354 { 0, NULL }
357 // This is the default binding. The content of input.conf overrides these.
358 // The first arg is a null terminated array of key codes.
359 // The second is the command
361 static const mp_cmd_bind_t def_cmd_binds[] = {
363 { { MOUSE_BTN3, 0 }, "seek 10" },
364 { { MOUSE_BTN4, 0 }, "seek -10" },
365 { { MOUSE_BTN5, 0 }, "volume 1" },
366 { { MOUSE_BTN6, 0 }, "volume -1" },
368 #ifdef CONFIG_DVDNAV
369 { { KEY_KP8, 0 }, "dvdnav up" }, // up
370 { { KEY_KP2, 0 }, "dvdnav down" }, // down
371 { { KEY_KP4, 0 }, "dvdnav left" }, // left
372 { { KEY_KP6, 0 }, "dvdnav right" }, // right
373 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
374 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
375 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
376 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
377 #endif
379 { { KEY_RIGHT, 0 }, "seek 10" },
380 { { KEY_LEFT, 0 }, "seek -10" },
381 { { KEY_UP, 0 }, "seek 60" },
382 { { KEY_DOWN, 0 }, "seek -60" },
383 { { KEY_PAGE_UP, 0 }, "seek 600" },
384 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
385 { { '+', 0 }, "audio_delay 0.100" },
386 { { '-', 0 }, "audio_delay -0.100" },
387 { { '[', 0 }, "speed_mult 0.9091" },
388 { { ']', 0 }, "speed_mult 1.1" },
389 { { '{', 0 }, "speed_mult 0.5" },
390 { { '}', 0 }, "speed_mult 2.0" },
391 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
392 { { 'q', 0 }, "quit" },
393 { { KEY_ESC, 0 }, "quit" },
394 { { 'p', 0 }, "pause" },
395 { { ' ', 0 }, "pause" },
396 { { '.', 0 }, "frame_step" },
397 { { KEY_HOME, 0 }, "pt_up_step 1" },
398 { { KEY_END, 0 }, "pt_up_step -1" },
399 { { '>', 0 }, "pt_step 1" },
400 { { KEY_ENTER, 0 }, "pt_step 1 1" },
401 { { '<', 0 }, "pt_step -1" },
402 { { KEY_INS, 0 }, "alt_src_step 1" },
403 { { KEY_DEL, 0 }, "alt_src_step -1" },
404 { { 'o', 0 }, "osd" },
405 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
406 { { 'z', 0 }, "sub_delay -0.1" },
407 { { 'x', 0 }, "sub_delay +0.1" },
408 { { 'g', 0 }, "sub_step -1" },
409 { { 'y', 0 }, "sub_step +1" },
410 { { '9', 0 }, "volume -1" },
411 { { '/', 0 }, "volume -1" },
412 { { '0', 0 }, "volume 1" },
413 { { '*', 0 }, "volume 1" },
414 { { '(', 0 }, "balance -0.1" },
415 { { ')', 0 }, "balance 0.1" },
416 { { 'm', 0 }, "mute" },
417 { { '1', 0 }, "contrast -1" },
418 { { '2', 0 }, "contrast 1" },
419 { { '3', 0 }, "brightness -1" },
420 { { '4', 0 }, "brightness 1" },
421 { { '5', 0 }, "hue -1" },
422 { { '6', 0 }, "hue 1" },
423 { { '7', 0 }, "saturation -1" },
424 { { '8', 0 }, "saturation 1" },
425 { { 'd', 0 }, "frame_drop" },
426 { { 'D', 0 }, "step_property_osd deinterlace" },
427 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
428 { { 'r', 0 }, "sub_pos -1" },
429 { { 't', 0 }, "sub_pos +1" },
430 { { 'a', 0 }, "sub_alignment" },
431 { { 'v', 0 }, "sub_visibility" },
432 { { 'j', 0 }, "sub_select" },
433 { { 'F', 0 }, "forced_subs_only" },
434 { { '#', 0 }, "switch_audio" },
435 { { '_', 0 }, "step_property switch_video" },
436 { { KEY_TAB, 0 }, "step_property switch_program" },
437 { { 'i', 0 }, "edl_mark" },
438 #ifdef CONFIG_TV
439 { { 'h', 0 }, "tv_step_channel 1" },
440 { { 'k', 0 }, "tv_step_channel -1" },
441 { { 'n', 0 }, "tv_step_norm" },
442 { { 'u', 0 }, "tv_step_chanlist" },
443 #endif
444 { { 'X', 0 }, "step_property teletext_mode 1" },
445 { { 'W', 0 }, "step_property teletext_page 1" },
446 { { 'Q', 0 }, "step_property teletext_page -1" },
447 #ifdef CONFIG_JOYSTICK
448 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
449 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
450 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
451 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
452 { { JOY_BTN0, 0 }, "pause" },
453 { { JOY_BTN1, 0 }, "osd" },
454 { { JOY_BTN2, 0 }, "volume 1"},
455 { { JOY_BTN3, 0 }, "volume -1"},
456 #endif
457 #ifdef CONFIG_APPLE_REMOTE
458 { { AR_PLAY, 0}, "pause" },
459 { { AR_PLAY_HOLD, 0}, "quit" },
460 { { AR_NEXT, 0 }, "seek 30" },
461 { { AR_NEXT_HOLD, 0 }, "seek 120" },
462 { { AR_PREV, 0 }, "seek -10" },
463 { { AR_PREV_HOLD, 0 }, "seek -120" },
464 { { AR_MENU, 0 }, "osd" },
465 { { AR_MENU_HOLD, 0 }, "mute" },
466 { { AR_VUP, 0 }, "volume 1"},
467 { { AR_VDOWN, 0 }, "volume -1"},
468 #endif
469 { { 'T', 0 }, "vo_ontop" },
470 { { 'f', 0 }, "vo_fullscreen" },
471 { { 's', 0 }, "screenshot 0" },
472 { { 'S', 0 }, "screenshot 1" },
473 { { 'w', 0 }, "panscan -0.1" },
474 { { 'e', 0 }, "panscan +0.1" },
476 { { KEY_POWER, 0 }, "quit" },
477 { { KEY_MENU, 0 }, "osd" },
478 { { KEY_PLAY, 0 }, "pause" },
479 { { KEY_PAUSE, 0 }, "pause" },
480 { { KEY_PLAYPAUSE, 0 }, "pause" },
481 { { KEY_STOP, 0 }, "quit" },
482 { { KEY_FORWARD, 0 }, "seek 60" },
483 { { KEY_REWIND, 0 }, "seek -60" },
484 { { KEY_NEXT, 0 }, "pt_step 1" },
485 { { KEY_PREV, 0 }, "pt_step -1" },
486 { { KEY_VOLUME_UP, 0 }, "volume 1" },
487 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
488 { { KEY_MUTE, 0 }, "mute" },
490 { { KEY_CLOSE_WIN, 0 }, "quit" },
492 { { '!', 0 }, "seek_chapter -1" },
493 { { '@', 0 }, "seek_chapter 1" },
494 { { 'A', 0 }, "switch_angle 1" },
495 { { 'U', 0 }, "stop" },
497 { { 0 }, NULL }
501 #ifndef MP_MAX_KEY_FD
502 #define MP_MAX_KEY_FD 10
503 #endif
505 #ifndef MP_MAX_CMD_FD
506 #define MP_MAX_CMD_FD 10
507 #endif
509 #define CMD_QUEUE_SIZE 100
511 typedef struct mp_input_fd {
512 int fd;
513 union {
514 mp_key_func_t key;
515 mp_cmd_func_t cmd;
516 } read_func;
517 mp_close_func_t close_func;
518 void *ctx;
519 unsigned eof : 1;
520 unsigned drop : 1;
521 unsigned dead : 1;
522 unsigned got_cmd : 1;
523 unsigned no_select : 1;
524 // These fields are for the cmd fds.
525 char* buffer;
526 int pos,size;
527 } mp_input_fd_t;
529 typedef struct mp_cmd_filter mp_cmd_filter_t;
531 struct mp_cmd_filter {
532 mp_input_cmd_filter filter;
533 void* ctx;
534 mp_cmd_filter_t* next;
537 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
539 struct mp_cmd_bind_section {
540 mp_cmd_bind_t* cmd_binds;
541 char* section;
542 mp_cmd_bind_section_t* next;
545 struct input_ctx {
546 // Autorepeat stuff
547 short ar_state;
548 mp_cmd_t *ar_cmd;
549 unsigned int last_ar;
550 // Autorepeat config
551 unsigned int ar_delay;
552 unsigned int ar_rate;
554 // these are the keys currently down
555 int key_down[MP_MAX_KEY_DOWN];
556 unsigned int num_key_down;
557 unsigned int last_key_down;
559 bool default_bindings;
560 // List of command binding sections
561 mp_cmd_bind_section_t *cmd_bind_sections;
562 // Name of currently used command section
563 char *section;
564 // The command binds of current section
565 mp_cmd_bind_t *cmd_binds;
566 mp_cmd_bind_t *cmd_binds_default;
568 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
569 unsigned int num_key_fd;
571 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
572 unsigned int num_cmd_fd;
574 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
575 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
579 static mp_cmd_filter_t* cmd_filters = NULL;
581 // Callback to allow the menu filter to grab the incoming keys
582 int (*mp_input_key_cb)(int code) = NULL;
584 int async_quit_request;
586 static int print_key_list(m_option_t* cfg);
587 static int print_cmd_list(m_option_t* cfg);
589 // Our command line options
590 static const m_option_t input_conf[] = {
591 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
592 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
593 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
594 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
595 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
596 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
597 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
598 OPT_STRING("file", input.in_file, CONF_GLOBAL),
599 OPT_FLAG_ON("default-bindings", input.default_bindings, CONF_GLOBAL),
600 OPT_FLAG_OFF("nodefault-bindings", input.default_bindings, CONF_GLOBAL),
601 { NULL, NULL, 0, 0, 0, 0, NULL}
604 static const m_option_t mp_input_opts[] = {
605 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
606 OPT_FLAG_OFF("nojoystick", input.use_joystick, CONF_GLOBAL),
607 OPT_FLAG_ON("joystick", input.use_joystick, CONF_GLOBAL),
608 OPT_FLAG_OFF("nolirc", input.use_lirc, CONF_GLOBAL),
609 OPT_FLAG_ON("lirc", input.use_lirc, CONF_GLOBAL),
610 OPT_FLAG_OFF("nolircc", input.use_lircc, CONF_GLOBAL),
611 OPT_FLAG_ON("lircc", input.use_lircc, CONF_GLOBAL),
612 OPT_FLAG_OFF("noar", input.use_ar, CONF_GLOBAL),
613 OPT_FLAG_ON("ar", input.use_ar, CONF_GLOBAL),
614 { NULL, NULL, 0, 0, 0, 0, NULL}
617 static int default_cmd_func(int fd,char* buf, int l);
619 static char *get_key_name(int key, char buffer[12]);
622 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
623 mp_cmd_func_t read_func, mp_close_func_t close_func)
625 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
626 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",fd);
627 return 0;
629 if (select && fd < 0) {
630 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
631 return 0;
634 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
635 .fd = fd,
636 .read_func.cmd = read_func ? read_func : default_cmd_func,
637 .close_func = close_func,
638 .no_select = !select
640 ictx->num_cmd_fd++;
642 return 1;
645 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
647 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
648 unsigned int i;
650 for (i = 0; i < ictx->num_cmd_fd; i++) {
651 if(cmd_fds[i].fd == fd)
652 break;
654 if (i == ictx->num_cmd_fd)
655 return;
656 if(cmd_fds[i].close_func)
657 cmd_fds[i].close_func(cmd_fds[i].fd);
658 if(cmd_fds[i].buffer)
659 talloc_free(cmd_fds[i].buffer);
661 if (i + 1 < ictx->num_cmd_fd)
662 memmove(&cmd_fds[i], &cmd_fds[i+1],
663 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
664 ictx->num_cmd_fd--;
667 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
669 struct mp_input_fd *key_fds = ictx->key_fds;
670 unsigned int i;
672 for (i = 0; i < ictx->num_key_fd; i++) {
673 if(key_fds[i].fd == fd)
674 break;
676 if (i == ictx->num_key_fd)
677 return;
678 if(key_fds[i].close_func)
679 key_fds[i].close_func(key_fds[i].fd);
681 if(i + 1 < ictx->num_key_fd)
682 memmove(&key_fds[i], &key_fds[i+1],
683 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
684 ictx->num_key_fd--;
687 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
688 mp_key_func_t read_func, mp_close_func_t close_func,
689 void *ctx)
691 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
692 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",fd);
693 return 0;
695 if (select && fd < 0) {
696 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
697 return 0;
700 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
701 .fd = fd,
702 .read_func.key = read_func,
703 .close_func = close_func,
704 .no_select = !select,
705 .ctx = ctx,
707 ictx->num_key_fd++;
709 return 1;
712 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
714 int cmd_num = 0;
716 while (*str == '\n' || *str == '\r' || *str == ' ')
717 ++str;
718 while (*str) {
719 mp_cmd_t *cmd;
720 size_t len = strcspn(str, "\r\n");
721 char *cmdbuf = talloc_size(NULL, len+1);
722 av_strlcpy(cmdbuf, str, len+1);
723 cmd = mp_input_parse_cmd(cmdbuf);
724 if (cmd) {
725 mp_input_queue_cmd(ictx, cmd);
726 ++cmd_num;
728 str += len;
729 while (*str == '\n' || *str == '\r' || *str == ' ')
730 ++str;
731 talloc_free(cmdbuf);
733 return cmd_num;
736 mp_cmd_t*
737 mp_input_parse_cmd(char* str) {
738 int i,l;
739 int pausing = 0;
740 char *ptr,*e;
741 const mp_cmd_t *cmd_def;
743 #ifdef MP_DEBUG
744 assert(str != NULL);
745 #endif
747 // Ignore heading spaces.
748 while (str[0] == ' ' || str[0] == '\t')
749 ++str;
751 if (strncmp(str, "pausing ", 8) == 0) {
752 pausing = 1;
753 str = &str[8];
754 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
755 pausing = 2;
756 str = &str[13];
757 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
758 pausing = 3;
759 str = &str[15];
760 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
761 pausing = 4;
762 str = &str[19];
765 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
766 /* NOTHING */;
767 if(ptr[0] != '\0')
768 l = ptr-str;
769 else
770 l = strlen(str);
772 if(l == 0)
773 return NULL;
775 for(i=0; mp_cmds[i].name != NULL; i++) {
776 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
777 break;
780 if(mp_cmds[i].name == NULL)
781 return NULL;
783 cmd_def = &mp_cmds[i];
785 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
786 *cmd = (mp_cmd_t){
787 .id = cmd_def->id,
788 .name = talloc_strdup(cmd, cmd_def->name),
789 .pausing = pausing,
792 ptr = str;
794 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
795 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
796 if(ptr[0] == '\0') break;
797 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
798 if(ptr[0] == '\0' || ptr[0] == '#') break;
799 cmd->args[i].type = cmd_def->args[i].type;
800 switch(cmd_def->args[i].type) {
801 case MP_CMD_ARG_INT:
802 errno = 0;
803 cmd->args[i].v.i = atoi(ptr);
804 if(errno != 0) {
805 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
806 ptr = NULL;
808 break;
809 case MP_CMD_ARG_FLOAT:
810 errno = 0;
811 cmd->args[i].v.f = atof(ptr);
812 if(errno != 0) {
813 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
814 ptr = NULL;
816 break;
817 case MP_CMD_ARG_STRING: {
818 char term;
819 char* ptr2 = ptr, *start;
821 if(ptr[0] == '\'' || ptr[0] == '"') {
822 term = ptr[0];
823 ptr2++;
824 } else
825 term = ' ';
826 start = ptr2;
827 while(1) {
828 e = strchr(ptr2,term);
829 if(!e) break;
830 if(e <= ptr2 || *(e - 1) != '\\') break;
831 ptr2 = e + 1;
834 if(term != ' ' && (!e || e[0] == '\0')) {
835 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
836 ptr = NULL;
837 break;
838 } else if(!e) e = ptr+strlen(ptr);
839 l = e-start;
840 ptr2 = start;
841 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
842 memmove(e,e+1,strlen(e));
843 ptr2 = e + 1;
844 l--;
846 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
847 if(term != ' ') ptr += l+2;
848 } break;
849 case -1:
850 ptr = NULL;
851 break;
852 default :
853 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
856 cmd->nargs = i;
858 if(cmd_def->nargs > cmd->nargs) {
859 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
860 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s requires at least %d arguments, we found only %d so far.\n",cmd_def->name,cmd_def->nargs,cmd->nargs);
861 mp_cmd_free(cmd);
862 return NULL;
865 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
866 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
867 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
868 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
871 if(i < MP_CMD_MAX_ARGS)
872 cmd->args[i].type = -1;
874 return cmd;
877 #define MP_CMD_MAX_SIZE 4096
879 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
881 char* end;
882 (*ret) = NULL;
884 // Allocate the buffer if it doesn't exist
885 if(!mp_fd->buffer) {
886 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
887 mp_fd->pos = 0;
888 mp_fd->size = MP_CMD_MAX_SIZE;
891 // Get some data if needed/possible
892 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
893 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
894 mp_fd->size - 1 - mp_fd->pos);
895 // Error ?
896 if(r < 0) {
897 switch(r) {
898 case MP_INPUT_ERROR:
899 case MP_INPUT_DEAD:
900 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",mp_fd->fd,strerror(errno));
901 case MP_INPUT_NOTHING:
902 return r;
903 case MP_INPUT_RETRY:
904 continue;
906 // EOF ?
907 } else if(r == 0) {
908 mp_fd->eof = 1;
909 break;
911 mp_fd->pos += r;
912 break;
915 mp_fd->got_cmd = 0;
917 while(1) {
918 int l = 0;
919 // Find the cmd end
920 mp_fd->buffer[mp_fd->pos] = '\0';
921 end = strchr(mp_fd->buffer,'\n');
922 // No cmd end ?
923 if(!end) {
924 // If buffer is full we must drop all until the next \n
925 if(mp_fd->size - mp_fd->pos <= 1) {
926 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd);
927 mp_fd->pos = 0;
928 mp_fd->drop = 1;
930 break;
932 // We already have a cmd : set the got_cmd flag
933 else if((*ret)) {
934 mp_fd->got_cmd = 1;
935 break;
938 l = end - mp_fd->buffer;
940 // Not dropping : put the cmd in ret
941 if (!mp_fd->drop)
942 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
943 else
944 mp_fd->drop = 0;
945 mp_fd->pos -= l+1;
946 memmove(mp_fd->buffer, end+1, mp_fd->pos);
949 if(*ret)
950 return 1;
951 else
952 return MP_INPUT_NOTHING;
955 static int default_cmd_func(int fd,char* buf, int l)
957 while(1) {
958 int r = read(fd,buf,l);
959 // Error ?
960 if(r < 0) {
961 if(errno == EINTR)
962 continue;
963 else if(errno == EAGAIN)
964 return MP_INPUT_NOTHING;
965 return MP_INPUT_ERROR;
966 // EOF ?
968 return r;
974 void
975 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
976 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
978 filter->filter = func;
979 filter->ctx = ctx;
980 filter->next = cmd_filters;
981 cmd_filters = filter;
985 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
987 int j;
989 if (n <= 0) return NULL;
990 for(j = 0; binds[j].cmd != NULL; j++) {
991 int found = 1,s;
992 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
993 if(binds[j].input[s] != keys[s]) {
994 found = 0;
995 break;
998 if(found && binds[j].input[s] == 0 && s == n)
999 break;
1001 return binds[j].cmd;
1004 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1005 char *section)
1007 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1009 if (section==NULL) section="default";
1010 while (bind_section) {
1011 if(strcmp(section,bind_section->section)==0) return bind_section;
1012 if(bind_section->next==NULL) break;
1013 bind_section=bind_section->next;
1015 if(bind_section) {
1016 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1017 bind_section=bind_section->next;
1018 } else {
1019 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1020 bind_section = ictx->cmd_bind_sections;
1022 bind_section->cmd_binds=NULL;
1023 bind_section->section = talloc_strdup(bind_section, section);
1024 bind_section->next=NULL;
1025 return bind_section;
1028 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys,
1029 int paused)
1031 char* cmd = NULL;
1032 mp_cmd_t* ret;
1033 char key_buf[12];
1035 if (ictx->cmd_binds)
1036 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1037 if (ictx->cmd_binds_default && cmd == NULL)
1038 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1039 if (ictx->default_bindings && cmd == NULL)
1040 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1042 if(cmd == NULL) {
1043 mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", get_key_name(keys[0],
1044 key_buf));
1045 if(n > 1) {
1046 int s;
1047 for(s=1; s < n; s++)
1048 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", get_key_name(keys[s], key_buf));
1050 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1051 return NULL;
1053 if (strcmp(cmd, "ignore") == 0) return NULL;
1054 ret = mp_input_parse_cmd(cmd);
1055 if(!ret) {
1056 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s",
1057 get_key_name(ictx->key_down[0], key_buf));
1058 if (ictx->num_key_down > 1) {
1059 unsigned int s;
1060 for(s=1; s < ictx->num_key_down; s++)
1061 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(ictx->key_down[s],
1062 key_buf));
1064 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1066 return ret;
1070 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1072 unsigned int j;
1073 mp_cmd_t* ret;
1075 if(mp_input_key_cb) {
1076 if (code & MP_KEY_DOWN)
1077 return NULL;
1078 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1079 if (mp_input_key_cb(code))
1080 return NULL;
1083 if(code & MP_KEY_DOWN) {
1084 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1085 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1086 return NULL;
1088 code &= ~MP_KEY_DOWN;
1089 // Check if we don't already have this key as pushed
1090 for (j = 0; j < ictx->num_key_down; j++) {
1091 if (ictx->key_down[j] == code)
1092 break;
1094 if (j != ictx->num_key_down)
1095 return NULL;
1096 ictx->key_down[ictx->num_key_down] = code;
1097 ictx->num_key_down++;
1098 ictx->last_key_down = GetTimer();
1099 ictx->ar_state = 0;
1100 return NULL;
1102 // key released
1103 // Check if the key is in the down key, driver which can't send push event
1104 // send only release event
1105 for (j = 0; j < ictx->num_key_down; j++) {
1106 if (ictx->key_down[j] == code)
1107 break;
1109 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1110 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1111 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1112 return NULL;
1114 ictx->key_down[ictx->num_key_down] = code;
1115 ictx->num_key_down++;
1116 ictx->last_key_down = 1;
1118 // We ignore key from last combination
1119 ret = ictx->last_key_down ?
1120 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down, paused)
1121 : NULL;
1122 // Remove the key
1123 if (j+1 < ictx->num_key_down)
1124 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1125 (ictx->num_key_down-(j+1))*sizeof(int));
1126 ictx->num_key_down--;
1127 ictx->last_key_down = 0;
1128 ictx->ar_state = -1;
1129 if (ictx->ar_cmd) {
1130 mp_cmd_free(ictx->ar_cmd);
1131 ictx->ar_cmd = NULL;
1133 return ret;
1136 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1138 // No input : autorepeat ?
1139 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1140 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1141 unsigned int t = GetTimer();
1142 // First time : wait delay
1143 if (ictx->ar_state == 0
1144 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1145 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1146 ictx->key_down, paused);
1147 if (!ictx->ar_cmd) {
1148 ictx->ar_state = -1;
1149 return NULL;
1151 ictx->ar_state = 1;
1152 ictx->last_ar = t;
1153 return mp_cmd_clone(ictx->ar_cmd);
1154 // Then send rate / sec event
1155 } else if (ictx->ar_state == 1
1156 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1157 ictx->last_ar = t;
1158 return mp_cmd_clone(ictx->ar_cmd);
1161 return NULL;
1165 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1167 int i;
1168 int got_cmd = 0;
1169 struct mp_input_fd *key_fds = ictx->key_fds;
1170 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1171 for (i = 0; i < ictx->num_key_fd; i++)
1172 if (key_fds[i].dead) {
1173 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1174 i--;
1176 for (i = 0; i < ictx->num_cmd_fd; i++)
1177 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1178 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1179 i--;
1181 else if (cmd_fds[i].got_cmd)
1182 got_cmd = 1;
1183 #ifdef HAVE_POSIX_SELECT
1184 fd_set fds;
1185 FD_ZERO(&fds);
1186 if (!got_cmd) {
1187 int max_fd = 0, num_fd = 0;
1188 for (i = 0; i < ictx->num_key_fd; i++) {
1189 if (key_fds[i].no_select)
1190 continue;
1191 if (key_fds[i].fd > max_fd)
1192 max_fd = key_fds[i].fd;
1193 FD_SET(key_fds[i].fd, &fds);
1194 num_fd++;
1196 for (i = 0; i < ictx->num_cmd_fd; i++) {
1197 if (cmd_fds[i].no_select)
1198 continue;
1199 if (cmd_fds[i].fd > max_fd)
1200 max_fd = cmd_fds[i].fd;
1201 FD_SET(cmd_fds[i].fd, &fds);
1202 num_fd++;
1204 if (num_fd > 0) {
1205 struct timeval tv, *time_val;
1206 if (time >= 0) {
1207 tv.tv_sec = time / 1000;
1208 tv.tv_usec = (time % 1000) * 1000;
1209 time_val = &tv;
1211 else
1212 time_val = NULL;
1213 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1214 if (errno != EINTR)
1215 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1216 strerror(errno));
1217 FD_ZERO(&fds);
1221 #else
1222 if (!got_cmd && time)
1223 usec_sleep(time * 1000);
1224 #endif
1227 for (i = 0; i < ictx->num_key_fd; i++) {
1228 #ifdef HAVE_POSIX_SELECT
1229 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1230 continue;
1231 #endif
1233 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1234 if (code >= 0) {
1235 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1236 if (ret)
1237 return ret;
1239 else if (code == MP_INPUT_ERROR)
1240 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n",
1241 key_fds[i].fd);
1242 else if (code == MP_INPUT_DEAD) {
1243 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n",
1244 key_fds[i].fd);
1245 key_fds[i].dead = 1;
1248 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1249 if (autorepeat_cmd)
1250 return autorepeat_cmd;
1252 for (i = 0; i < ictx->num_cmd_fd; i++) {
1253 #ifdef HAVE_POSIX_SELECT
1254 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1255 !cmd_fds[i].got_cmd)
1256 continue;
1257 #endif
1258 char *cmd;
1259 int r = read_cmd(&cmd_fds[i], &cmd);
1260 if (r >= 0) {
1261 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1262 talloc_free(cmd);
1263 if (ret)
1264 return ret;
1266 else if (r == MP_INPUT_ERROR)
1267 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1268 cmd_fds[i].fd);
1269 else if (r == MP_INPUT_DEAD)
1270 cmd_fds[i].dead = 1;
1273 return NULL;
1277 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1279 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1280 return 0;
1281 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1282 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1283 ictx->cmd_queue_length++;
1284 return 1;
1287 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1289 mp_cmd_t* ret;
1291 if (ictx->cmd_queue_length == 0)
1292 return NULL;
1294 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1296 if (!peek_only) {
1297 ictx->cmd_queue_length--;
1298 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1301 return ret;
1305 * \param peek_only when set, the returned command stays in the queue.
1306 * Do not free the returned cmd whe you set this!
1308 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1309 int peek_only)
1311 mp_cmd_t* ret = NULL;
1312 mp_cmd_filter_t* cf;
1313 int from_queue;
1315 if (async_quit_request)
1316 return mp_input_parse_cmd("quit 1");
1317 while(1) {
1318 from_queue = 1;
1319 ret = get_queued_cmd(ictx, peek_only);
1320 if(ret) break;
1321 from_queue = 0;
1322 ret = read_events(ictx, time, paused);
1323 if (!ret) {
1324 from_queue = 1;
1325 ret = get_queued_cmd(ictx, peek_only);
1327 break;
1329 if(!ret) return NULL;
1331 for(cf = cmd_filters ; cf ; cf = cf->next) {
1332 if(cf->filter(ret,paused,cf->ctx)) {
1333 if (peek_only && from_queue)
1334 // The filter ate the cmd, so we remove it from queue
1335 ret = get_queued_cmd(ictx, 0);
1336 mp_cmd_free(ret);
1337 return NULL;
1341 if (!from_queue && peek_only)
1342 mp_input_queue_cmd(ictx, ret);
1344 return ret;
1347 void
1348 mp_cmd_free(mp_cmd_t* cmd) {
1349 talloc_free(cmd);
1352 mp_cmd_t*
1353 mp_cmd_clone(mp_cmd_t* cmd) {
1354 mp_cmd_t* ret;
1355 int i;
1356 #ifdef MP_DEBUG
1357 assert(cmd != NULL);
1358 #endif
1360 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1361 ret->name = talloc_strdup(ret, cmd->name);
1362 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1363 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1364 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1367 return ret;
1370 static char *get_key_name(int key, char buffer[12])
1372 int i;
1374 for(i = 0; key_names[i].name != NULL; i++) {
1375 if(key_names[i].key == key)
1376 return key_names[i].name;
1379 if(isascii(key)) {
1380 snprintf(buffer, 12, "%c",(char)key);
1381 return buffer;
1384 // Print the hex key code
1385 snprintf(buffer, 12, "%#-8x",key);
1386 return buffer;
1391 mp_input_get_key_from_name(const char *name) {
1392 int i,ret = 0,len = strlen(name);
1393 if(len == 1) { // Direct key code
1394 ret = (unsigned char)name[0];
1395 return ret;
1396 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1397 return strtol(name,NULL,16);
1399 for(i = 0; key_names[i].name != NULL; i++) {
1400 if(strcasecmp(key_names[i].name,name) == 0)
1401 return key_names[i].key;
1404 return -1;
1407 static int get_input_from_name(char* name,int* keys) {
1408 char *end,*ptr;
1409 int n=0;
1411 ptr = name;
1412 n = 0;
1413 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1414 if(end && end[1] != '\0') {
1415 if(end[1] == '-')
1416 end = &end[1];
1417 end[0] = '\0';
1419 keys[n] = mp_input_get_key_from_name(ptr);
1420 if(keys[n] < 0) {
1421 return 0;
1423 n++;
1424 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1425 ptr = &end[1];
1426 else
1427 break;
1429 keys[n] = 0;
1430 return 1;
1433 #define BS_MAX 256
1434 #define SPACE_CHAR " \n\r\t"
1436 static void bind_keys(struct input_ctx *ictx,
1437 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1439 int i = 0,j;
1440 mp_cmd_bind_t* bind = NULL;
1441 mp_cmd_bind_section_t* bind_section = NULL;
1442 char *section=NULL, *p;
1444 #ifdef MP_DEBUG
1445 assert(keys != NULL);
1446 assert(cmd != NULL);
1447 #endif
1449 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1450 *p=0;
1451 section=++cmd;
1452 cmd=++p;
1453 // Jump beginning space
1454 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1455 /* NOTHING */;
1457 bind_section = get_bind_section(ictx, section);
1459 if(bind_section->cmd_binds) {
1460 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1461 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1462 /* NOTHING */;
1463 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1464 bind = &bind_section->cmd_binds[i];
1465 break;
1470 if(!bind) {
1471 bind_section->cmd_binds = talloc_realloc(bind_section,
1472 bind_section->cmd_binds,
1473 mp_cmd_bind_t, i + 2);
1474 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1475 bind = &bind_section->cmd_binds[i];
1477 if(bind->cmd)
1478 talloc_free(bind->cmd);
1479 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1480 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1483 static void add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1485 int i;
1486 for(i = 0 ; list[i].cmd ; i++)
1487 bind_keys(ictx, list[i].input,list[i].cmd);
1490 static int parse_config(struct input_ctx *ictx, char *file)
1492 int fd;
1493 int bs = 0,r,eof = 0,comments = 0;
1494 char *iter,*end;
1495 char buffer[BS_MAX];
1496 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1498 fd = open(file,O_RDONLY);
1500 if(fd < 0) {
1501 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1502 return 0;
1505 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1507 while(1) {
1508 if(! eof && bs < BS_MAX-1) {
1509 if(bs > 0) bs--;
1510 r = read(fd,buffer+bs,BS_MAX-1-bs);
1511 if(r < 0) {
1512 if(errno == EINTR)
1513 continue;
1514 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1515 close(fd);
1516 return 0;
1517 } else if(r == 0) {
1518 eof = 1;
1519 } else {
1520 bs += r+1;
1521 buffer[bs-1] = '\0';
1524 // Empty buffer : return
1525 if(bs <= 1) {
1526 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1527 close(fd);
1528 return 1;
1531 iter = buffer;
1533 if(comments) {
1534 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1535 /* NOTHING */;
1536 if(iter[0] == '\0') { // Buffer was full of comment
1537 bs = 0;
1538 continue;
1540 iter++;
1541 r = strlen(iter);
1542 memmove(buffer,iter,r+1);
1543 bs = r+1;
1544 comments = 0;
1545 continue;
1548 // Find the wanted key
1549 if(keys[0] == 0) {
1550 // Jump beginning space
1551 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1552 /* NOTHING */;
1553 if(iter[0] == '\0') { // Buffer was full of space char
1554 bs = 0;
1555 continue;
1557 if(iter[0] == '#') { // Comments
1558 comments = 1;
1559 continue;
1561 // Find the end of the key code name
1562 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1563 /*NOTHING */;
1564 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1565 if(buffer == iter) {
1566 if(eof && (buffer-iter) == bs)
1567 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1568 else
1569 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1570 return 0;
1572 memmove(buffer,iter,end-iter);
1573 bs = end-iter;
1574 continue;
1577 char name[end-iter+1];
1578 strncpy(name,iter,end-iter);
1579 name[end-iter] = '\0';
1580 if (!get_input_from_name(name,keys)) {
1581 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1582 close(fd);
1583 return 0;
1586 if( bs > (end-buffer))
1587 memmove(buffer,end,bs - (end-buffer));
1588 bs -= end-buffer;
1589 continue;
1590 } else { // Get the command
1591 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1592 // Found new line
1593 if(iter[0] == '\n' || iter[0] == '\r') {
1594 int i;
1595 char key_buf[12];
1596 mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", get_key_name(keys[0], key_buf));
1597 for(i = 1; keys[i] != 0 ; i++)
1598 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(keys[i], key_buf));
1599 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1600 keys[0] = 0;
1601 if(iter > buffer) {
1602 memmove(buffer,iter,bs- (iter-buffer));
1603 bs -= (iter-buffer);
1605 continue;
1607 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1608 /* NOTHING */;
1609 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1610 if(iter == buffer) {
1611 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1612 close(fd);
1613 return 0;
1615 memmove(buffer,iter,end - iter);
1616 bs = end - iter;
1617 continue;
1620 char cmd[end-iter+1];
1621 strncpy(cmd,iter,end-iter);
1622 cmd[end-iter] = '\0';
1623 //printf("Set bind %d => %s\n",keys[0],cmd);
1624 bind_keys(ictx, keys,cmd);
1625 n_binds++;
1627 keys[0] = 0;
1628 end++;
1629 if(bs > (end-buffer))
1630 memmove(buffer,end,bs-(end-buffer));
1631 bs -= (end-buffer);
1632 buffer[bs-1] = '\0';
1633 continue;
1636 mp_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1637 close(fd);
1638 mp_input_set_section(ictx, NULL);
1639 return 0;
1642 void mp_input_set_section(struct input_ctx *ictx, char *name)
1644 mp_cmd_bind_section_t* bind_section = NULL;
1646 ictx->cmd_binds = NULL;
1647 ictx->cmd_binds_default = NULL;
1648 if (ictx->section)
1649 talloc_free(ictx->section);
1650 if (name)
1651 ictx->section = talloc_strdup(ictx, name);
1652 else
1653 ictx->section = talloc_strdup(ictx, "default");
1654 if ((bind_section = get_bind_section(ictx, ictx->section)))
1655 ictx->cmd_binds = bind_section->cmd_binds;
1656 if (strcmp(ictx->section, "default") == 0)
1657 return;
1658 if ((bind_section = get_bind_section(ictx, NULL)))
1659 ictx->cmd_binds_default = bind_section->cmd_binds;
1662 char *mp_input_get_section(struct input_ctx *ictx)
1664 return ictx->section;
1667 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1669 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1670 *ictx = (struct input_ctx){
1671 .ar_state = -1,
1672 .ar_delay = input_conf->ar_delay,
1673 .ar_rate = input_conf->ar_rate,
1674 .default_bindings = input_conf->default_bindings,
1677 char* file;
1678 char *config_file = input_conf->config_file;
1679 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1680 if(!file)
1681 return ictx;
1683 if (!parse_config(ictx, file)) {
1684 // free file if it was allocated by get_path(),
1685 // before it gets overwritten
1686 if( file != config_file)
1688 free(file);
1690 // Try global conf dir
1691 file = MPLAYER_CONFDIR "/input.conf";
1692 if (!parse_config(ictx, file))
1693 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1695 else
1697 // free file if it was allocated by get_path()
1698 if( file != config_file)
1699 free(file);
1702 #ifdef CONFIG_JOYSTICK
1703 if (input_conf->use_joystick) {
1704 int fd = mp_input_joystick_init(input_conf->js_dev);
1705 if(fd < 0)
1706 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1707 else
1708 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1709 (mp_close_func_t)close,NULL);
1711 #endif
1713 #ifdef CONFIG_LIRC
1714 if (input_conf->use_lirc) {
1715 int fd = mp_input_lirc_init();
1716 if(fd > 0)
1717 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1718 mp_input_lirc_close);
1720 #endif
1722 #ifdef CONFIG_LIRCC
1723 if (input_conf->use_lircc) {
1724 int fd = lircc_init("mplayer", NULL);
1725 if(fd >= 0)
1726 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1728 #endif
1730 #ifdef CONFIG_APPLE_REMOTE
1731 if (input_conf->use_ar) {
1732 if(mp_input_ar_init() < 0)
1733 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1734 else
1735 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1736 NULL);
1738 #endif
1740 #ifdef CONFIG_APPLE_IR
1741 if (input_conf->use_ar) {
1742 int fd = mp_input_appleir_init(input_conf->ar_dev);
1743 if(fd < 0)
1744 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1745 else
1746 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1747 (mp_close_func_t)close, NULL);
1749 #endif
1751 if (input_conf->in_file) {
1752 struct stat st;
1753 if (stat(input_conf->in_file, &st))
1754 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't stat %s: %s\n", input_conf->in_file, strerror(errno));
1755 else {
1756 int in_file_fd = open(input_conf->in_file,
1757 S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1758 if(in_file_fd >= 0)
1759 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1760 (mp_close_func_t)close);
1761 else
1762 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1763 input_conf->in_file, strerror(errno));
1766 return ictx;
1769 void mp_input_uninit(struct input_ctx *ictx)
1771 if (!ictx)
1772 return;
1774 unsigned int i;
1776 for (i=0; i < ictx->num_key_fd; i++) {
1777 if (ictx->key_fds[i].close_func)
1778 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1781 for (i = 0; i < ictx->num_cmd_fd; i++) {
1782 if (ictx->cmd_fds[i].close_func)
1783 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1785 talloc_free(ictx);
1788 void
1789 mp_input_register_options(m_config_t* cfg) {
1790 m_config_register_options(cfg,mp_input_opts);
1793 static int print_key_list(m_option_t* cfg)
1795 int i;
1796 printf("\n");
1797 for(i= 0; key_names[i].name != NULL ; i++)
1798 printf("%s\n",key_names[i].name);
1799 exit(0);
1802 static int print_cmd_list(m_option_t* cfg)
1804 const mp_cmd_t *cmd;
1805 int i,j;
1806 const char* type;
1808 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1809 printf("%-20.20s",cmd->name);
1810 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1811 switch(cmd->args[j].type) {
1812 case MP_CMD_ARG_INT:
1813 type = "Integer";
1814 break;
1815 case MP_CMD_ARG_FLOAT:
1816 type = "Float";
1817 break;
1818 case MP_CMD_ARG_STRING:
1819 type = "String";
1820 break;
1821 default:
1822 type = "??";
1824 if(j+1 > cmd->nargs)
1825 printf(" [%s]",type);
1826 else
1827 printf(" %s",type);
1829 printf("\n");
1831 exit(0);
1834 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1836 mp_cmd_t* cmd;
1837 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1838 return 0;
1839 switch(cmd->id) {
1840 case MP_CMD_QUIT:
1841 case MP_CMD_PLAY_TREE_STEP:
1842 case MP_CMD_PLAY_TREE_UP_STEP:
1843 case MP_CMD_PLAY_ALT_SRC_STEP:
1844 // The cmd will be executed when we are back in the main loop
1845 return 1;
1847 // remove the cmd from the queue
1848 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1849 mp_cmd_free(cmd);
1850 return 0;