options: move -chapter values to option struct
[mplayer/glamo.git] / input / input.c
blobc6a255115760b0804e20a5272c31ae1aad4e8b83
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <stdbool.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/time.h>
30 #include <fcntl.h>
31 #include <ctype.h>
33 #include "input.h"
34 #include "mouse.h"
35 #ifdef MP_DEBUG
36 #include <assert.h>
37 #endif
38 #include "mp_fifo.h"
39 #include "osdep/keycodes.h"
40 #include "osdep/timer.h"
41 #include "libavutil/avstring.h"
42 #include "mp_msg.h"
43 #include "m_config.h"
44 #include "m_option.h"
45 #include "get_path.h"
46 #include "talloc.h"
47 #include "options.h"
49 #include "joystick.h"
51 #ifdef CONFIG_LIRC
52 #include "lirc.h"
53 #endif
55 #ifdef CONFIG_LIRCC
56 #include <lirc/lircc.h>
57 #endif
59 #include "ar.h"
61 typedef struct mp_cmd_bind {
62 int input[MP_MAX_KEY_DOWN+1];
63 char* cmd;
64 } mp_cmd_bind_t;
66 typedef struct mp_key_name {
67 int key;
68 char* name;
69 } mp_key_name_t;
71 /// This array defines all known commands.
72 /// The first field is an id used to recognize the command without too many strcmp.
73 /// The second is obviously the command name.
74 /// The third is the minimum number of arguments this command needs.
75 /// Then comes the definition of each argument, terminated with an arg of type -1.
76 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
77 /// the last one) which is actually 9.
79 /// For the args, the first field is the type (actually int, float or string), the second
80 /// is the default value wich is used for optional arguments
82 static const mp_cmd_t mp_cmds[] = {
83 #ifdef CONFIG_RADIO
84 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
85 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
86 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
87 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
88 #endif
89 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
90 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
91 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
93 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
94 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
95 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
96 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
97 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
98 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
99 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
100 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
101 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
102 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
106 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
107 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
109 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
110 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
111 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
112 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
113 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
114 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
118 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
119 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
120 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
121 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
122 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
123 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
124 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
125 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
126 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
130 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
131 #ifdef CONFIG_ASS
132 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
133 #endif
134 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
135 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
136 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
137 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
138 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
139 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
141 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
142 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
144 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
145 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
151 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
152 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
153 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 #ifdef CONFIG_TV
155 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
156 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
157 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
158 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
159 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
160 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
161 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
162 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
165 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
166 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 #endif
169 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
170 #ifdef CONFIG_DVBIN
171 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
172 #endif
173 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
174 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
175 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
176 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
177 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
178 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
179 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
180 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
181 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
185 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
186 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
187 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
189 #ifdef CONFIG_DVDNAV
190 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
191 #endif
193 #ifdef CONFIG_MENU
194 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
195 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
196 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
197 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
198 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
199 #endif
201 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
202 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
203 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
204 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
205 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 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}} } },
208 { 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}} } },
210 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
211 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
213 { 0, NULL, 0, {} }
216 /// The names of the keys as used in input.conf
217 /// If you add some new keys, you also need to add them here
219 static const mp_key_name_t key_names[] = {
220 { ' ', "SPACE" },
221 { '#', "SHARP" },
222 { KEY_ENTER, "ENTER" },
223 { KEY_TAB, "TAB" },
224 { KEY_CTRL, "CTRL" },
225 { KEY_BACKSPACE, "BS" },
226 { KEY_DELETE, "DEL" },
227 { KEY_INSERT, "INS" },
228 { KEY_HOME, "HOME" },
229 { KEY_END, "END" },
230 { KEY_PAGE_UP, "PGUP" },
231 { KEY_PAGE_DOWN, "PGDWN" },
232 { KEY_ESC, "ESC" },
233 { KEY_RIGHT, "RIGHT" },
234 { KEY_LEFT, "LEFT" },
235 { KEY_DOWN, "DOWN" },
236 { KEY_UP, "UP" },
237 { KEY_F+1, "F1" },
238 { KEY_F+2, "F2" },
239 { KEY_F+3, "F3" },
240 { KEY_F+4, "F4" },
241 { KEY_F+5, "F5" },
242 { KEY_F+6, "F6" },
243 { KEY_F+7, "F7" },
244 { KEY_F+8, "F8" },
245 { KEY_F+9, "F9" },
246 { KEY_F+10, "F10" },
247 { KEY_F+11, "F11" },
248 { KEY_F+12, "F12" },
249 { KEY_KP0, "KP0" },
250 { KEY_KP1, "KP1" },
251 { KEY_KP2, "KP2" },
252 { KEY_KP3, "KP3" },
253 { KEY_KP4, "KP4" },
254 { KEY_KP5, "KP5" },
255 { KEY_KP6, "KP6" },
256 { KEY_KP7, "KP7" },
257 { KEY_KP8, "KP8" },
258 { KEY_KP9, "KP9" },
259 { KEY_KPDEL, "KP_DEL" },
260 { KEY_KPDEC, "KP_DEC" },
261 { KEY_KPINS, "KP_INS" },
262 { KEY_KPENTER, "KP_ENTER" },
263 { MOUSE_BTN0, "MOUSE_BTN0" },
264 { MOUSE_BTN1, "MOUSE_BTN1" },
265 { MOUSE_BTN2, "MOUSE_BTN2" },
266 { MOUSE_BTN3, "MOUSE_BTN3" },
267 { MOUSE_BTN4, "MOUSE_BTN4" },
268 { MOUSE_BTN5, "MOUSE_BTN5" },
269 { MOUSE_BTN6, "MOUSE_BTN6" },
270 { MOUSE_BTN7, "MOUSE_BTN7" },
271 { MOUSE_BTN8, "MOUSE_BTN8" },
272 { MOUSE_BTN9, "MOUSE_BTN9" },
273 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
274 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
275 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
276 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
277 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
278 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
279 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
280 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
281 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
282 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
283 { JOY_AXIS1_MINUS, "JOY_UP" },
284 { JOY_AXIS1_PLUS, "JOY_DOWN" },
285 { JOY_AXIS0_MINUS, "JOY_LEFT" },
286 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
288 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
289 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
290 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
291 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
292 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
293 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
294 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
295 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
296 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
297 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
298 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
299 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
300 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
301 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
302 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
303 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
304 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
305 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
306 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
307 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
309 { JOY_BTN0, "JOY_BTN0" },
310 { JOY_BTN1, "JOY_BTN1" },
311 { JOY_BTN2, "JOY_BTN2" },
312 { JOY_BTN3, "JOY_BTN3" },
313 { JOY_BTN4, "JOY_BTN4" },
314 { JOY_BTN5, "JOY_BTN5" },
315 { JOY_BTN6, "JOY_BTN6" },
316 { JOY_BTN7, "JOY_BTN7" },
317 { JOY_BTN8, "JOY_BTN8" },
318 { JOY_BTN9, "JOY_BTN9" },
320 { AR_PLAY, "AR_PLAY" },
321 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
322 { AR_NEXT, "AR_NEXT" },
323 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
324 { AR_PREV, "AR_PREV" },
325 { AR_PREV_HOLD, "AR_PREV_HOLD" },
326 { AR_MENU, "AR_MENU" },
327 { AR_MENU_HOLD, "AR_MENU_HOLD" },
328 { AR_VUP, "AR_VUP" },
329 { AR_VDOWN, "AR_VDOWN" },
331 { KEY_POWER, "POWER" },
332 { KEY_MENU, "MENU" },
333 { KEY_PLAY, "PLAY" },
334 { KEY_PAUSE, "PAUSE" },
335 { KEY_PLAYPAUSE, "PLAYPAUSE" },
336 { KEY_STOP, "STOP" },
337 { KEY_FORWARD, "FORWARD" },
338 { KEY_REWIND, "REWIND" },
339 { KEY_NEXT, "NEXT" },
340 { KEY_PREV, "PREV" },
341 { KEY_VOLUME_UP, "VOLUME_UP" },
342 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
343 { KEY_MUTE, "MUTE" },
345 // These are kept for backward compatibility
346 { KEY_PAUSE, "XF86_PAUSE" },
347 { KEY_STOP, "XF86_STOP" },
348 { KEY_PREV, "XF86_PREV" },
349 { KEY_NEXT, "XF86_NEXT" },
351 { KEY_CLOSE_WIN, "CLOSE_WIN" },
353 { 0, NULL }
356 // This is the default binding. The content of input.conf overrides these.
357 // The first arg is a null terminated array of key codes.
358 // The second is the command
360 static const mp_cmd_bind_t def_cmd_binds[] = {
362 { { MOUSE_BTN3, 0 }, "seek 10" },
363 { { MOUSE_BTN4, 0 }, "seek -10" },
364 { { MOUSE_BTN5, 0 }, "volume 1" },
365 { { MOUSE_BTN6, 0 }, "volume -1" },
367 #ifdef CONFIG_DVDNAV
368 { { KEY_KP8, 0 }, "dvdnav up" }, // up
369 { { KEY_KP2, 0 }, "dvdnav down" }, // down
370 { { KEY_KP4, 0 }, "dvdnav left" }, // left
371 { { KEY_KP6, 0 }, "dvdnav right" }, // right
372 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
373 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
374 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
375 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
376 #endif
378 { { KEY_RIGHT, 0 }, "seek 10" },
379 { { KEY_LEFT, 0 }, "seek -10" },
380 { { KEY_UP, 0 }, "seek 60" },
381 { { KEY_DOWN, 0 }, "seek -60" },
382 { { KEY_PAGE_UP, 0 }, "seek 600" },
383 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
384 { { '+', 0 }, "audio_delay 0.100" },
385 { { '-', 0 }, "audio_delay -0.100" },
386 { { '[', 0 }, "speed_mult 0.9091" },
387 { { ']', 0 }, "speed_mult 1.1" },
388 { { '{', 0 }, "speed_mult 0.5" },
389 { { '}', 0 }, "speed_mult 2.0" },
390 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
391 { { 'q', 0 }, "quit" },
392 { { KEY_ESC, 0 }, "quit" },
393 { { 'p', 0 }, "pause" },
394 { { ' ', 0 }, "pause" },
395 { { '.', 0 }, "frame_step" },
396 { { KEY_HOME, 0 }, "pt_up_step 1" },
397 { { KEY_END, 0 }, "pt_up_step -1" },
398 { { '>', 0 }, "pt_step 1" },
399 { { KEY_ENTER, 0 }, "pt_step 1 1" },
400 { { '<', 0 }, "pt_step -1" },
401 { { KEY_INS, 0 }, "alt_src_step 1" },
402 { { KEY_DEL, 0 }, "alt_src_step -1" },
403 { { 'o', 0 }, "osd" },
404 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
405 { { 'z', 0 }, "sub_delay -0.1" },
406 { { 'x', 0 }, "sub_delay +0.1" },
407 { { 'g', 0 }, "sub_step -1" },
408 { { 'y', 0 }, "sub_step +1" },
409 { { '9', 0 }, "volume -1" },
410 { { '/', 0 }, "volume -1" },
411 { { '0', 0 }, "volume 1" },
412 { { '*', 0 }, "volume 1" },
413 { { '(', 0 }, "balance -0.1" },
414 { { ')', 0 }, "balance 0.1" },
415 { { 'm', 0 }, "mute" },
416 { { '1', 0 }, "contrast -1" },
417 { { '2', 0 }, "contrast 1" },
418 { { '3', 0 }, "brightness -1" },
419 { { '4', 0 }, "brightness 1" },
420 { { '5', 0 }, "hue -1" },
421 { { '6', 0 }, "hue 1" },
422 { { '7', 0 }, "saturation -1" },
423 { { '8', 0 }, "saturation 1" },
424 { { 'd', 0 }, "frame_drop" },
425 { { 'D', 0 }, "step_property_osd deinterlace" },
426 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
427 { { 'r', 0 }, "sub_pos -1" },
428 { { 't', 0 }, "sub_pos +1" },
429 { { 'a', 0 }, "sub_alignment" },
430 { { 'v', 0 }, "sub_visibility" },
431 { { 'j', 0 }, "sub_select" },
432 { { 'F', 0 }, "forced_subs_only" },
433 { { '#', 0 }, "switch_audio" },
434 { { '_', 0 }, "step_property switch_video" },
435 { { KEY_TAB, 0 }, "step_property switch_program" },
436 { { 'i', 0 }, "edl_mark" },
437 #ifdef CONFIG_TV
438 { { 'h', 0 }, "tv_step_channel 1" },
439 { { 'k', 0 }, "tv_step_channel -1" },
440 { { 'n', 0 }, "tv_step_norm" },
441 { { 'u', 0 }, "tv_step_chanlist" },
442 #endif
443 { { 'X', 0 }, "step_property teletext_mode 1" },
444 { { 'W', 0 }, "step_property teletext_page 1" },
445 { { 'Q', 0 }, "step_property teletext_page -1" },
446 #ifdef CONFIG_JOYSTICK
447 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
448 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
449 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
450 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
451 { { JOY_BTN0, 0 }, "pause" },
452 { { JOY_BTN1, 0 }, "osd" },
453 { { JOY_BTN2, 0 }, "volume 1"},
454 { { JOY_BTN3, 0 }, "volume -1"},
455 #endif
456 #ifdef CONFIG_APPLE_REMOTE
457 { { AR_PLAY, 0}, "pause" },
458 { { AR_PLAY_HOLD, 0}, "quit" },
459 { { AR_NEXT, 0 }, "seek 30" },
460 { { AR_NEXT_HOLD, 0 }, "seek 120" },
461 { { AR_PREV, 0 }, "seek -10" },
462 { { AR_PREV_HOLD, 0 }, "seek -120" },
463 { { AR_MENU, 0 }, "osd" },
464 { { AR_MENU_HOLD, 0 }, "mute" },
465 { { AR_VUP, 0 }, "volume 1"},
466 { { AR_VDOWN, 0 }, "volume -1"},
467 #endif
468 { { 'T', 0 }, "vo_ontop" },
469 { { 'f', 0 }, "vo_fullscreen" },
470 { { 's', 0 }, "screenshot 0" },
471 { { 'S', 0 }, "screenshot 1" },
472 { { 'w', 0 }, "panscan -0.1" },
473 { { 'e', 0 }, "panscan +0.1" },
475 { { KEY_POWER, 0 }, "quit" },
476 { { KEY_MENU, 0 }, "osd" },
477 { { KEY_PLAY, 0 }, "pause" },
478 { { KEY_PAUSE, 0 }, "pause" },
479 { { KEY_PLAYPAUSE, 0 }, "pause" },
480 { { KEY_STOP, 0 }, "quit" },
481 { { KEY_FORWARD, 0 }, "seek 60" },
482 { { KEY_REWIND, 0 }, "seek -60" },
483 { { KEY_NEXT, 0 }, "pt_step 1" },
484 { { KEY_PREV, 0 }, "pt_step -1" },
485 { { KEY_VOLUME_UP, 0 }, "volume 1" },
486 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
487 { { KEY_MUTE, 0 }, "mute" },
489 { { KEY_CLOSE_WIN, 0 }, "quit" },
491 { { '!', 0 }, "seek_chapter -1" },
492 { { '@', 0 }, "seek_chapter 1" },
493 { { 'A', 0 }, "switch_angle 1" },
494 { { 'U', 0 }, "stop" },
496 { { 0 }, NULL }
500 #ifndef MP_MAX_KEY_FD
501 #define MP_MAX_KEY_FD 10
502 #endif
504 #ifndef MP_MAX_CMD_FD
505 #define MP_MAX_CMD_FD 10
506 #endif
508 #define CMD_QUEUE_SIZE 100
510 typedef struct mp_input_fd {
511 int fd;
512 union {
513 mp_key_func_t key;
514 mp_cmd_func_t cmd;
515 } read_func;
516 mp_close_func_t close_func;
517 void *ctx;
518 unsigned eof : 1;
519 unsigned drop : 1;
520 unsigned dead : 1;
521 unsigned got_cmd : 1;
522 unsigned no_select : 1;
523 // These fields are for the cmd fds.
524 char* buffer;
525 int pos,size;
526 } mp_input_fd_t;
528 typedef struct mp_cmd_filter mp_cmd_filter_t;
530 struct mp_cmd_filter {
531 mp_input_cmd_filter filter;
532 void* ctx;
533 mp_cmd_filter_t* next;
536 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
538 struct mp_cmd_bind_section {
539 mp_cmd_bind_t* cmd_binds;
540 char* section;
541 mp_cmd_bind_section_t* next;
544 struct input_ctx {
545 // Autorepeat stuff
546 short ar_state;
547 mp_cmd_t *ar_cmd;
548 unsigned int last_ar;
549 // Autorepeat config
550 unsigned int ar_delay;
551 unsigned int ar_rate;
553 // these are the keys currently down
554 int key_down[MP_MAX_KEY_DOWN];
555 unsigned int num_key_down;
556 unsigned int last_key_down;
558 bool default_bindings;
559 // List of command binding sections
560 mp_cmd_bind_section_t *cmd_bind_sections;
561 // Name of currently used command section
562 char *section;
563 // The command binds of current section
564 mp_cmd_bind_t *cmd_binds;
565 mp_cmd_bind_t *cmd_binds_default;
567 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
568 unsigned int num_key_fd;
570 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
571 unsigned int num_cmd_fd;
573 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
574 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
578 static mp_cmd_filter_t* cmd_filters = NULL;
580 // Callback to allow the menu filter to grab the incoming keys
581 int (*mp_input_key_cb)(int code) = NULL;
583 int async_quit_request;
585 static int print_key_list(m_option_t* cfg);
586 static int print_cmd_list(m_option_t* cfg);
588 // Our command line options
589 static const m_option_t input_conf[] = {
590 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
591 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
592 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
593 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
594 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
595 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
596 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
597 OPT_STRING("file", input.in_file, CONF_GLOBAL),
598 OPT_FLAG_ON("default-bindings", input.default_bindings, CONF_GLOBAL),
599 OPT_FLAG_OFF("nodefault-bindings", input.default_bindings, CONF_GLOBAL),
600 { NULL, NULL, 0, 0, 0, 0, NULL}
603 static const m_option_t mp_input_opts[] = {
604 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
605 OPT_FLAG_OFF("nojoystick", input.use_joystick, CONF_GLOBAL),
606 OPT_FLAG_ON("joystick", input.use_joystick, CONF_GLOBAL),
607 OPT_FLAG_OFF("nolirc", input.use_lirc, CONF_GLOBAL),
608 OPT_FLAG_ON("lirc", input.use_lirc, CONF_GLOBAL),
609 OPT_FLAG_OFF("nolircc", input.use_lircc, CONF_GLOBAL),
610 OPT_FLAG_ON("lircc", input.use_lircc, CONF_GLOBAL),
611 OPT_FLAG_OFF("noar", input.use_ar, CONF_GLOBAL),
612 OPT_FLAG_ON("ar", input.use_ar, CONF_GLOBAL),
613 { NULL, NULL, 0, 0, 0, 0, NULL}
616 static int default_cmd_func(int fd,char* buf, int l);
618 static char *get_key_name(int key, char buffer[12]);
621 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
622 mp_cmd_func_t read_func, mp_close_func_t close_func)
624 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
625 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",fd);
626 return 0;
628 if (select && fd < 0) {
629 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
630 return 0;
633 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
634 .fd = fd,
635 .read_func.cmd = read_func ? read_func : default_cmd_func,
636 .close_func = close_func,
637 .no_select = !select
639 ictx->num_cmd_fd++;
641 return 1;
644 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
646 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
647 unsigned int i;
649 for (i = 0; i < ictx->num_cmd_fd; i++) {
650 if(cmd_fds[i].fd == fd)
651 break;
653 if (i == ictx->num_cmd_fd)
654 return;
655 if(cmd_fds[i].close_func)
656 cmd_fds[i].close_func(cmd_fds[i].fd);
657 if(cmd_fds[i].buffer)
658 talloc_free(cmd_fds[i].buffer);
660 if (i + 1 < ictx->num_cmd_fd)
661 memmove(&cmd_fds[i], &cmd_fds[i+1],
662 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
663 ictx->num_cmd_fd--;
666 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
668 struct mp_input_fd *key_fds = ictx->key_fds;
669 unsigned int i;
671 for (i = 0; i < ictx->num_key_fd; i++) {
672 if(key_fds[i].fd == fd)
673 break;
675 if (i == ictx->num_key_fd)
676 return;
677 if(key_fds[i].close_func)
678 key_fds[i].close_func(key_fds[i].fd);
680 if(i + 1 < ictx->num_key_fd)
681 memmove(&key_fds[i], &key_fds[i+1],
682 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
683 ictx->num_key_fd--;
686 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
687 mp_key_func_t read_func, mp_close_func_t close_func,
688 void *ctx)
690 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
691 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",fd);
692 return 0;
694 if (select && fd < 0) {
695 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
696 return 0;
699 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
700 .fd = fd,
701 .read_func.key = read_func,
702 .close_func = close_func,
703 .no_select = !select,
704 .ctx = ctx,
706 ictx->num_key_fd++;
708 return 1;
711 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
713 int cmd_num = 0;
715 while (*str == '\n' || *str == '\r' || *str == ' ')
716 ++str;
717 while (*str) {
718 mp_cmd_t *cmd;
719 size_t len = strcspn(str, "\r\n");
720 char *cmdbuf = talloc_size(NULL, len+1);
721 av_strlcpy(cmdbuf, str, len+1);
722 cmd = mp_input_parse_cmd(cmdbuf);
723 if (cmd) {
724 mp_input_queue_cmd(ictx, cmd);
725 ++cmd_num;
727 str += len;
728 while (*str == '\n' || *str == '\r' || *str == ' ')
729 ++str;
730 talloc_free(cmdbuf);
732 return cmd_num;
735 mp_cmd_t*
736 mp_input_parse_cmd(char* str) {
737 int i,l;
738 int pausing = 0;
739 char *ptr,*e;
740 const mp_cmd_t *cmd_def;
742 #ifdef MP_DEBUG
743 assert(str != NULL);
744 #endif
746 // Ignore heading spaces.
747 while (str[0] == ' ' || str[0] == '\t')
748 ++str;
750 if (strncmp(str, "pausing ", 8) == 0) {
751 pausing = 1;
752 str = &str[8];
753 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
754 pausing = 2;
755 str = &str[13];
756 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
757 pausing = 3;
758 str = &str[15];
759 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
760 pausing = 4;
761 str = &str[19];
764 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
765 /* NOTHING */;
766 if(ptr[0] != '\0')
767 l = ptr-str;
768 else
769 l = strlen(str);
771 if(l == 0)
772 return NULL;
774 for(i=0; mp_cmds[i].name != NULL; i++) {
775 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
776 break;
779 if(mp_cmds[i].name == NULL)
780 return NULL;
782 cmd_def = &mp_cmds[i];
784 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
785 *cmd = (mp_cmd_t){
786 .id = cmd_def->id,
787 .name = talloc_strdup(cmd, cmd_def->name),
788 .pausing = pausing,
791 ptr = str;
793 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
794 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
795 if(ptr[0] == '\0') break;
796 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
797 if(ptr[0] == '\0' || ptr[0] == '#') break;
798 cmd->args[i].type = cmd_def->args[i].type;
799 switch(cmd_def->args[i].type) {
800 case MP_CMD_ARG_INT:
801 errno = 0;
802 cmd->args[i].v.i = atoi(ptr);
803 if(errno != 0) {
804 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
805 ptr = NULL;
807 break;
808 case MP_CMD_ARG_FLOAT:
809 errno = 0;
810 cmd->args[i].v.f = atof(ptr);
811 if(errno != 0) {
812 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
813 ptr = NULL;
815 break;
816 case MP_CMD_ARG_STRING: {
817 char term;
818 char* ptr2 = ptr, *start;
820 if(ptr[0] == '\'' || ptr[0] == '"') {
821 term = ptr[0];
822 ptr2++;
823 } else
824 term = ' ';
825 start = ptr2;
826 while(1) {
827 e = strchr(ptr2,term);
828 if(!e) break;
829 if(e <= ptr2 || *(e - 1) != '\\') break;
830 ptr2 = e + 1;
833 if(term != ' ' && (!e || e[0] == '\0')) {
834 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
835 ptr = NULL;
836 break;
837 } else if(!e) e = ptr+strlen(ptr);
838 l = e-start;
839 ptr2 = start;
840 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
841 memmove(e,e+1,strlen(e));
842 ptr2 = e + 1;
843 l--;
845 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
846 if(term != ' ') ptr += l+2;
847 } break;
848 case -1:
849 ptr = NULL;
850 break;
851 default :
852 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
855 cmd->nargs = i;
857 if(cmd_def->nargs > cmd->nargs) {
858 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
859 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);
860 mp_cmd_free(cmd);
861 return NULL;
864 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
865 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
866 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
867 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
870 if(i < MP_CMD_MAX_ARGS)
871 cmd->args[i].type = -1;
873 return cmd;
876 #define MP_CMD_MAX_SIZE 4096
878 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
880 char* end;
881 (*ret) = NULL;
883 // Allocate the buffer if it doesn't exist
884 if(!mp_fd->buffer) {
885 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
886 mp_fd->pos = 0;
887 mp_fd->size = MP_CMD_MAX_SIZE;
890 // Get some data if needed/possible
891 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
892 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
893 mp_fd->size - 1 - mp_fd->pos);
894 // Error ?
895 if(r < 0) {
896 switch(r) {
897 case MP_INPUT_ERROR:
898 case MP_INPUT_DEAD:
899 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",mp_fd->fd,strerror(errno));
900 case MP_INPUT_NOTHING:
901 return r;
902 case MP_INPUT_RETRY:
903 continue;
905 // EOF ?
906 } else if(r == 0) {
907 mp_fd->eof = 1;
908 break;
910 mp_fd->pos += r;
911 break;
914 mp_fd->got_cmd = 0;
916 while(1) {
917 int l = 0;
918 // Find the cmd end
919 mp_fd->buffer[mp_fd->pos] = '\0';
920 end = strchr(mp_fd->buffer,'\r');
921 if (end) *end = '\n';
922 end = strchr(mp_fd->buffer,'\n');
923 // No cmd end ?
924 if(!end) {
925 // If buffer is full we must drop all until the next \n
926 if(mp_fd->size - mp_fd->pos <= 1) {
927 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd);
928 mp_fd->pos = 0;
929 mp_fd->drop = 1;
931 break;
933 // We already have a cmd : set the got_cmd flag
934 else if((*ret)) {
935 mp_fd->got_cmd = 1;
936 break;
939 l = end - mp_fd->buffer;
941 // Not dropping : put the cmd in ret
942 if (!mp_fd->drop)
943 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
944 else
945 mp_fd->drop = 0;
946 mp_fd->pos -= l+1;
947 memmove(mp_fd->buffer, end+1, mp_fd->pos);
950 if(*ret)
951 return 1;
952 else
953 return MP_INPUT_NOTHING;
956 static int default_cmd_func(int fd,char* buf, int l)
958 while(1) {
959 int r = read(fd,buf,l);
960 // Error ?
961 if(r < 0) {
962 if(errno == EINTR)
963 continue;
964 else if(errno == EAGAIN)
965 return MP_INPUT_NOTHING;
966 return MP_INPUT_ERROR;
967 // EOF ?
969 return r;
975 void
976 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
977 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
979 filter->filter = func;
980 filter->ctx = ctx;
981 filter->next = cmd_filters;
982 cmd_filters = filter;
986 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
988 int j;
990 if (n <= 0) return NULL;
991 for(j = 0; binds[j].cmd != NULL; j++) {
992 int found = 1,s;
993 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
994 if(binds[j].input[s] != keys[s]) {
995 found = 0;
996 break;
999 if(found && binds[j].input[s] == 0 && s == n)
1000 break;
1002 return binds[j].cmd;
1005 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1006 char *section)
1008 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1010 if (section==NULL) section="default";
1011 while (bind_section) {
1012 if(strcmp(section,bind_section->section)==0) return bind_section;
1013 if(bind_section->next==NULL) break;
1014 bind_section=bind_section->next;
1016 if(bind_section) {
1017 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1018 bind_section=bind_section->next;
1019 } else {
1020 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1021 bind_section = ictx->cmd_bind_sections;
1023 bind_section->cmd_binds=NULL;
1024 bind_section->section = talloc_strdup(bind_section, section);
1025 bind_section->next=NULL;
1026 return bind_section;
1029 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys,
1030 int paused)
1032 char* cmd = NULL;
1033 mp_cmd_t* ret;
1034 char key_buf[12];
1036 if (ictx->cmd_binds)
1037 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1038 if (ictx->cmd_binds_default && cmd == NULL)
1039 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1040 if (ictx->default_bindings && cmd == NULL)
1041 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1043 if(cmd == NULL) {
1044 mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", get_key_name(keys[0],
1045 key_buf));
1046 if(n > 1) {
1047 int s;
1048 for(s=1; s < n; s++)
1049 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", get_key_name(keys[s], key_buf));
1051 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1052 return NULL;
1054 if (strcmp(cmd, "ignore") == 0) return NULL;
1055 ret = mp_input_parse_cmd(cmd);
1056 if(!ret) {
1057 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s",
1058 get_key_name(ictx->key_down[0], key_buf));
1059 if (ictx->num_key_down > 1) {
1060 unsigned int s;
1061 for(s=1; s < ictx->num_key_down; s++)
1062 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(ictx->key_down[s],
1063 key_buf));
1065 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1067 return ret;
1071 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1073 unsigned int j;
1074 mp_cmd_t* ret;
1076 if(mp_input_key_cb) {
1077 if (code & MP_KEY_DOWN)
1078 return NULL;
1079 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1080 if (mp_input_key_cb(code))
1081 return NULL;
1084 if(code & MP_KEY_DOWN) {
1085 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1086 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1087 return NULL;
1089 code &= ~MP_KEY_DOWN;
1090 // Check if we don't already have this key as pushed
1091 for (j = 0; j < ictx->num_key_down; j++) {
1092 if (ictx->key_down[j] == code)
1093 break;
1095 if (j != ictx->num_key_down)
1096 return NULL;
1097 ictx->key_down[ictx->num_key_down] = code;
1098 ictx->num_key_down++;
1099 ictx->last_key_down = GetTimer();
1100 ictx->ar_state = 0;
1101 return NULL;
1103 // key released
1104 // Check if the key is in the down key, driver which can't send push event
1105 // send only release event
1106 for (j = 0; j < ictx->num_key_down; j++) {
1107 if (ictx->key_down[j] == code)
1108 break;
1110 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1111 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1112 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1113 return NULL;
1115 ictx->key_down[ictx->num_key_down] = code;
1116 ictx->num_key_down++;
1117 ictx->last_key_down = 1;
1119 // We ignore key from last combination
1120 ret = ictx->last_key_down ?
1121 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down, paused)
1122 : NULL;
1123 // Remove the key
1124 if (j+1 < ictx->num_key_down)
1125 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1126 (ictx->num_key_down-(j+1))*sizeof(int));
1127 ictx->num_key_down--;
1128 ictx->last_key_down = 0;
1129 ictx->ar_state = -1;
1130 if (ictx->ar_cmd) {
1131 mp_cmd_free(ictx->ar_cmd);
1132 ictx->ar_cmd = NULL;
1134 return ret;
1137 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1139 // No input : autorepeat ?
1140 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1141 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1142 unsigned int t = GetTimer();
1143 // First time : wait delay
1144 if (ictx->ar_state == 0
1145 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1146 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1147 ictx->key_down, paused);
1148 if (!ictx->ar_cmd) {
1149 ictx->ar_state = -1;
1150 return NULL;
1152 ictx->ar_state = 1;
1153 ictx->last_ar = t;
1154 return mp_cmd_clone(ictx->ar_cmd);
1155 // Then send rate / sec event
1156 } else if (ictx->ar_state == 1
1157 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1158 ictx->last_ar = t;
1159 return mp_cmd_clone(ictx->ar_cmd);
1162 return NULL;
1166 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1168 int i;
1169 int got_cmd = 0;
1170 struct mp_input_fd *key_fds = ictx->key_fds;
1171 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1172 for (i = 0; i < ictx->num_key_fd; i++)
1173 if (key_fds[i].dead) {
1174 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1175 i--;
1177 for (i = 0; i < ictx->num_cmd_fd; i++)
1178 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1179 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1180 i--;
1182 else if (cmd_fds[i].got_cmd)
1183 got_cmd = 1;
1184 #ifdef HAVE_POSIX_SELECT
1185 fd_set fds;
1186 FD_ZERO(&fds);
1187 if (!got_cmd) {
1188 int max_fd = 0, num_fd = 0;
1189 for (i = 0; i < ictx->num_key_fd; i++) {
1190 if (key_fds[i].no_select)
1191 continue;
1192 if (key_fds[i].fd > max_fd)
1193 max_fd = key_fds[i].fd;
1194 FD_SET(key_fds[i].fd, &fds);
1195 num_fd++;
1197 for (i = 0; i < ictx->num_cmd_fd; i++) {
1198 if (cmd_fds[i].no_select)
1199 continue;
1200 if (cmd_fds[i].fd > max_fd)
1201 max_fd = cmd_fds[i].fd;
1202 FD_SET(cmd_fds[i].fd, &fds);
1203 num_fd++;
1205 if (num_fd > 0) {
1206 struct timeval tv, *time_val;
1207 if (time >= 0) {
1208 tv.tv_sec = time / 1000;
1209 tv.tv_usec = (time % 1000) * 1000;
1210 time_val = &tv;
1212 else
1213 time_val = NULL;
1214 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1215 if (errno != EINTR)
1216 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1217 strerror(errno));
1218 FD_ZERO(&fds);
1222 #else
1223 if (!got_cmd && time)
1224 usec_sleep(time * 1000);
1225 #endif
1228 for (i = 0; i < ictx->num_key_fd; i++) {
1229 #ifdef HAVE_POSIX_SELECT
1230 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1231 continue;
1232 #endif
1234 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1235 if (code >= 0) {
1236 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1237 if (ret)
1238 return ret;
1240 else if (code == MP_INPUT_ERROR)
1241 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n",
1242 key_fds[i].fd);
1243 else if (code == MP_INPUT_DEAD) {
1244 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n",
1245 key_fds[i].fd);
1246 key_fds[i].dead = 1;
1249 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1250 if (autorepeat_cmd)
1251 return autorepeat_cmd;
1253 for (i = 0; i < ictx->num_cmd_fd; i++) {
1254 #ifdef HAVE_POSIX_SELECT
1255 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1256 !cmd_fds[i].got_cmd)
1257 continue;
1258 #endif
1259 char *cmd;
1260 int r = read_cmd(&cmd_fds[i], &cmd);
1261 if (r >= 0) {
1262 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1263 talloc_free(cmd);
1264 if (ret)
1265 return ret;
1267 else if (r == MP_INPUT_ERROR)
1268 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1269 cmd_fds[i].fd);
1270 else if (r == MP_INPUT_DEAD)
1271 cmd_fds[i].dead = 1;
1274 return NULL;
1278 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1280 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1281 return 0;
1282 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1283 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1284 ictx->cmd_queue_length++;
1285 return 1;
1288 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1290 mp_cmd_t* ret;
1292 if (ictx->cmd_queue_length == 0)
1293 return NULL;
1295 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1297 if (!peek_only) {
1298 ictx->cmd_queue_length--;
1299 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1302 return ret;
1306 * \param peek_only when set, the returned command stays in the queue.
1307 * Do not free the returned cmd whe you set this!
1309 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1310 int peek_only)
1312 mp_cmd_t* ret = NULL;
1313 mp_cmd_filter_t* cf;
1314 int from_queue;
1316 if (async_quit_request)
1317 return mp_input_parse_cmd("quit 1");
1318 while(1) {
1319 from_queue = 1;
1320 ret = get_queued_cmd(ictx, peek_only);
1321 if(ret) break;
1322 from_queue = 0;
1323 ret = read_events(ictx, time, paused);
1324 if (!ret) {
1325 from_queue = 1;
1326 ret = get_queued_cmd(ictx, peek_only);
1328 break;
1330 if(!ret) return NULL;
1332 for(cf = cmd_filters ; cf ; cf = cf->next) {
1333 if(cf->filter(ret,paused,cf->ctx)) {
1334 if (peek_only && from_queue)
1335 // The filter ate the cmd, so we remove it from queue
1336 ret = get_queued_cmd(ictx, 0);
1337 mp_cmd_free(ret);
1338 return NULL;
1342 if (!from_queue && peek_only)
1343 mp_input_queue_cmd(ictx, ret);
1345 return ret;
1348 void
1349 mp_cmd_free(mp_cmd_t* cmd) {
1350 talloc_free(cmd);
1353 mp_cmd_t*
1354 mp_cmd_clone(mp_cmd_t* cmd) {
1355 mp_cmd_t* ret;
1356 int i;
1357 #ifdef MP_DEBUG
1358 assert(cmd != NULL);
1359 #endif
1361 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1362 ret->name = talloc_strdup(ret, cmd->name);
1363 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1364 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1365 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1368 return ret;
1371 static char *get_key_name(int key, char buffer[12])
1373 int i;
1375 for(i = 0; key_names[i].name != NULL; i++) {
1376 if(key_names[i].key == key)
1377 return key_names[i].name;
1380 if(isascii(key)) {
1381 snprintf(buffer, 12, "%c",(char)key);
1382 return buffer;
1385 // Print the hex key code
1386 snprintf(buffer, 12, "%#-8x",key);
1387 return buffer;
1392 mp_input_get_key_from_name(const char *name) {
1393 int i,ret = 0,len = strlen(name);
1394 if(len == 1) { // Direct key code
1395 ret = (unsigned char)name[0];
1396 return ret;
1397 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1398 return strtol(name,NULL,16);
1400 for(i = 0; key_names[i].name != NULL; i++) {
1401 if(strcasecmp(key_names[i].name,name) == 0)
1402 return key_names[i].key;
1405 return -1;
1408 static int get_input_from_name(char* name,int* keys) {
1409 char *end,*ptr;
1410 int n=0;
1412 ptr = name;
1413 n = 0;
1414 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1415 if(end && end[1] != '\0') {
1416 if(end[1] == '-')
1417 end = &end[1];
1418 end[0] = '\0';
1420 keys[n] = mp_input_get_key_from_name(ptr);
1421 if(keys[n] < 0) {
1422 return 0;
1424 n++;
1425 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1426 ptr = &end[1];
1427 else
1428 break;
1430 keys[n] = 0;
1431 return 1;
1434 #define BS_MAX 256
1435 #define SPACE_CHAR " \n\r\t"
1437 static void bind_keys(struct input_ctx *ictx,
1438 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1440 int i = 0,j;
1441 mp_cmd_bind_t* bind = NULL;
1442 mp_cmd_bind_section_t* bind_section = NULL;
1443 char *section=NULL, *p;
1445 #ifdef MP_DEBUG
1446 assert(keys != NULL);
1447 assert(cmd != NULL);
1448 #endif
1450 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1451 *p=0;
1452 section=++cmd;
1453 cmd=++p;
1454 // Jump beginning space
1455 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1456 /* NOTHING */;
1458 bind_section = get_bind_section(ictx, section);
1460 if(bind_section->cmd_binds) {
1461 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1462 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1463 /* NOTHING */;
1464 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1465 bind = &bind_section->cmd_binds[i];
1466 break;
1471 if(!bind) {
1472 bind_section->cmd_binds = talloc_realloc(bind_section,
1473 bind_section->cmd_binds,
1474 mp_cmd_bind_t, i + 2);
1475 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1476 bind = &bind_section->cmd_binds[i];
1478 if(bind->cmd)
1479 talloc_free(bind->cmd);
1480 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1481 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1484 static void add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1486 int i;
1487 for(i = 0 ; list[i].cmd ; i++)
1488 bind_keys(ictx, list[i].input,list[i].cmd);
1491 static int parse_config(struct input_ctx *ictx, char *file)
1493 int fd;
1494 int bs = 0,r,eof = 0,comments = 0;
1495 char *iter,*end;
1496 char buffer[BS_MAX];
1497 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1499 fd = open(file,O_RDONLY);
1501 if(fd < 0) {
1502 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1503 return 0;
1506 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1508 while(1) {
1509 if(! eof && bs < BS_MAX-1) {
1510 if(bs > 0) bs--;
1511 r = read(fd,buffer+bs,BS_MAX-1-bs);
1512 if(r < 0) {
1513 if(errno == EINTR)
1514 continue;
1515 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1516 close(fd);
1517 return 0;
1518 } else if(r == 0) {
1519 eof = 1;
1520 } else {
1521 bs += r+1;
1522 buffer[bs-1] = '\0';
1525 // Empty buffer : return
1526 if(bs <= 1) {
1527 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1528 close(fd);
1529 return 1;
1532 iter = buffer;
1534 if(comments) {
1535 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1536 /* NOTHING */;
1537 if(iter[0] == '\0') { // Buffer was full of comment
1538 bs = 0;
1539 continue;
1541 iter++;
1542 r = strlen(iter);
1543 memmove(buffer,iter,r+1);
1544 bs = r+1;
1545 comments = 0;
1546 continue;
1549 // Find the wanted key
1550 if(keys[0] == 0) {
1551 // Jump beginning space
1552 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1553 /* NOTHING */;
1554 if(iter[0] == '\0') { // Buffer was full of space char
1555 bs = 0;
1556 continue;
1558 if(iter[0] == '#') { // Comments
1559 comments = 1;
1560 continue;
1562 // Find the end of the key code name
1563 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1564 /*NOTHING */;
1565 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1566 if(buffer == iter) {
1567 if(eof && (buffer-iter) == bs)
1568 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1569 else
1570 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1571 return 0;
1573 memmove(buffer,iter,end-iter);
1574 bs = end-iter;
1575 continue;
1578 char name[end-iter+1];
1579 strncpy(name,iter,end-iter);
1580 name[end-iter] = '\0';
1581 if (!get_input_from_name(name,keys)) {
1582 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1583 close(fd);
1584 return 0;
1587 if( bs > (end-buffer))
1588 memmove(buffer,end,bs - (end-buffer));
1589 bs -= end-buffer;
1590 continue;
1591 } else { // Get the command
1592 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1593 // Found new line
1594 if(iter[0] == '\n' || iter[0] == '\r') {
1595 int i;
1596 char key_buf[12];
1597 mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", get_key_name(keys[0], key_buf));
1598 for(i = 1; keys[i] != 0 ; i++)
1599 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(keys[i], key_buf));
1600 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1601 keys[0] = 0;
1602 if(iter > buffer) {
1603 memmove(buffer,iter,bs- (iter-buffer));
1604 bs -= (iter-buffer);
1606 continue;
1608 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1609 /* NOTHING */;
1610 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1611 if(iter == buffer) {
1612 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1613 close(fd);
1614 return 0;
1616 memmove(buffer,iter,end - iter);
1617 bs = end - iter;
1618 continue;
1621 char cmd[end-iter+1];
1622 strncpy(cmd,iter,end-iter);
1623 cmd[end-iter] = '\0';
1624 //printf("Set bind %d => %s\n",keys[0],cmd);
1625 bind_keys(ictx, keys,cmd);
1626 n_binds++;
1628 keys[0] = 0;
1629 end++;
1630 if(bs > (end-buffer))
1631 memmove(buffer,end,bs-(end-buffer));
1632 bs -= (end-buffer);
1633 buffer[bs-1] = '\0';
1634 continue;
1637 mp_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1638 close(fd);
1639 mp_input_set_section(ictx, NULL);
1640 return 0;
1643 void mp_input_set_section(struct input_ctx *ictx, char *name)
1645 mp_cmd_bind_section_t* bind_section = NULL;
1647 ictx->cmd_binds = NULL;
1648 ictx->cmd_binds_default = NULL;
1649 if (ictx->section)
1650 talloc_free(ictx->section);
1651 if (name)
1652 ictx->section = talloc_strdup(ictx, name);
1653 else
1654 ictx->section = talloc_strdup(ictx, "default");
1655 if ((bind_section = get_bind_section(ictx, ictx->section)))
1656 ictx->cmd_binds = bind_section->cmd_binds;
1657 if (strcmp(ictx->section, "default") == 0)
1658 return;
1659 if ((bind_section = get_bind_section(ictx, NULL)))
1660 ictx->cmd_binds_default = bind_section->cmd_binds;
1663 char *mp_input_get_section(struct input_ctx *ictx)
1665 return ictx->section;
1668 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1670 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1671 *ictx = (struct input_ctx){
1672 .ar_state = -1,
1673 .ar_delay = input_conf->ar_delay,
1674 .ar_rate = input_conf->ar_rate,
1675 .default_bindings = input_conf->default_bindings,
1678 char* file;
1679 char *config_file = input_conf->config_file;
1680 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1681 if(!file)
1682 return ictx;
1684 if (!parse_config(ictx, file)) {
1685 // free file if it was allocated by get_path(),
1686 // before it gets overwritten
1687 if( file != config_file)
1689 free(file);
1691 // Try global conf dir
1692 file = MPLAYER_CONFDIR "/input.conf";
1693 if (!parse_config(ictx, file))
1694 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1696 else
1698 // free file if it was allocated by get_path()
1699 if( file != config_file)
1700 free(file);
1703 #ifdef CONFIG_JOYSTICK
1704 if (input_conf->use_joystick) {
1705 int fd = mp_input_joystick_init(input_conf->js_dev);
1706 if(fd < 0)
1707 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1708 else
1709 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1710 (mp_close_func_t)close,NULL);
1712 #endif
1714 #ifdef CONFIG_LIRC
1715 if (input_conf->use_lirc) {
1716 int fd = mp_input_lirc_init();
1717 if(fd > 0)
1718 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1719 mp_input_lirc_close);
1721 #endif
1723 #ifdef CONFIG_LIRCC
1724 if (input_conf->use_lircc) {
1725 int fd = lircc_init("mplayer", NULL);
1726 if(fd >= 0)
1727 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1729 #endif
1731 #ifdef CONFIG_APPLE_REMOTE
1732 if (input_conf->use_ar) {
1733 if(mp_input_ar_init() < 0)
1734 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1735 else
1736 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1737 NULL);
1739 #endif
1741 #ifdef CONFIG_APPLE_IR
1742 if (input_conf->use_ar) {
1743 int fd = mp_input_appleir_init(input_conf->ar_dev);
1744 if(fd < 0)
1745 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1746 else
1747 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1748 (mp_close_func_t)close, NULL);
1750 #endif
1752 if (input_conf->in_file) {
1753 struct stat st;
1754 if (stat(input_conf->in_file, &st))
1755 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't stat %s: %s\n", input_conf->in_file, strerror(errno));
1756 else {
1757 int in_file_fd = open(input_conf->in_file,
1758 S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1759 if(in_file_fd >= 0)
1760 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1761 (mp_close_func_t)close);
1762 else
1763 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1764 input_conf->in_file, strerror(errno));
1767 return ictx;
1770 void mp_input_uninit(struct input_ctx *ictx)
1772 if (!ictx)
1773 return;
1775 unsigned int i;
1777 for (i=0; i < ictx->num_key_fd; i++) {
1778 if (ictx->key_fds[i].close_func)
1779 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1782 for (i = 0; i < ictx->num_cmd_fd; i++) {
1783 if (ictx->cmd_fds[i].close_func)
1784 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1786 talloc_free(ictx);
1789 void
1790 mp_input_register_options(m_config_t* cfg) {
1791 m_config_register_options(cfg,mp_input_opts);
1794 static int print_key_list(m_option_t* cfg)
1796 int i;
1797 printf("\n");
1798 for(i= 0; key_names[i].name != NULL ; i++)
1799 printf("%s\n",key_names[i].name);
1800 exit(0);
1803 static int print_cmd_list(m_option_t* cfg)
1805 const mp_cmd_t *cmd;
1806 int i,j;
1807 const char* type;
1809 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1810 printf("%-20.20s",cmd->name);
1811 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1812 switch(cmd->args[j].type) {
1813 case MP_CMD_ARG_INT:
1814 type = "Integer";
1815 break;
1816 case MP_CMD_ARG_FLOAT:
1817 type = "Float";
1818 break;
1819 case MP_CMD_ARG_STRING:
1820 type = "String";
1821 break;
1822 default:
1823 type = "??";
1825 if(j+1 > cmd->nargs)
1826 printf(" [%s]",type);
1827 else
1828 printf(" %s",type);
1830 printf("\n");
1832 exit(0);
1835 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1837 mp_cmd_t* cmd;
1838 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1839 return 0;
1840 switch(cmd->id) {
1841 case MP_CMD_QUIT:
1842 case MP_CMD_PLAY_TREE_STEP:
1843 case MP_CMD_PLAY_TREE_UP_STEP:
1844 case MP_CMD_PLAY_ALT_SRC_STEP:
1845 // The cmd will be executed when we are back in the main loop
1846 return 1;
1848 // remove the cmd from the queue
1849 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1850 mp_cmd_free(cmd);
1851 return 0;