codecs.conf: add Deluxe Paint Animation decoder from libavcodec
[mplayer.git] / input / input.c
blobf201160e05be788d88d196ddcd6243681b261c4e
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 "path.h"
46 #include "talloc.h"
47 #include "options.h"
48 #include "bstr.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}}, {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_OSD_SHOW_PROGRESSION, "osd_show_progression", 0, { {-1,{0}} } },
110 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
111 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
112 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
113 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
114 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
115 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
116 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
117 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
118 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
119 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
120 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
121 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
123 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
124 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
125 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
126 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
127 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
128 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
129 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
130 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
131 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
132 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
133 #ifdef CONFIG_ASS
134 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
135 #endif
136 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
137 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
138 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
139 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
140 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
141 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
142 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
143 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
144 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
145 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
146 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
147 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
148 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
149 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
150 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
151 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
152 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
153 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
154 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
155 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
156 #ifdef CONFIG_TV
157 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
158 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
159 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
160 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
161 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
162 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
163 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
164 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
165 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
166 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
167 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
168 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
169 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
170 #endif
171 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
172 #ifdef CONFIG_DVBIN
173 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
174 #endif
175 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
176 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
177 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
178 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
179 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
180 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
181 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
182 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
184 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
185 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
186 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
187 { MP_CMD_CAPTURING, "capturing", 0, { {-1,{0}} } },
188 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
189 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
190 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
192 #ifdef CONFIG_DVDNAV
193 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
194 #endif
196 #ifdef CONFIG_MENU
197 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
198 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
199 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
200 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
201 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
202 #endif
204 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
205 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
206 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
207 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_SET_PROPERTY_OSD, "set_property_osd", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
209 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
210 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
211 { 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}} } },
213 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
214 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
216 { MP_CMD_AF_SWITCH, "af_switch", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
217 { MP_CMD_AF_ADD, "af_add", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
218 { MP_CMD_AF_DEL, "af_del", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
219 { MP_CMD_AF_CLR, "af_clr", 0, { {-1,{0}} } },
220 { MP_CMD_AF_CMDLINE, "af_cmdline", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
222 { 0, NULL, 0, {} }
225 /// The names of the keys as used in input.conf
226 /// If you add some new keys, you also need to add them here
228 static const mp_key_name_t key_names[] = {
229 { ' ', "SPACE" },
230 { '#', "SHARP" },
231 { KEY_ENTER, "ENTER" },
232 { KEY_TAB, "TAB" },
233 { KEY_BACKSPACE, "BS" },
234 { KEY_DELETE, "DEL" },
235 { KEY_INSERT, "INS" },
236 { KEY_HOME, "HOME" },
237 { KEY_END, "END" },
238 { KEY_PAGE_UP, "PGUP" },
239 { KEY_PAGE_DOWN, "PGDWN" },
240 { KEY_ESC, "ESC" },
241 { KEY_RIGHT, "RIGHT" },
242 { KEY_LEFT, "LEFT" },
243 { KEY_DOWN, "DOWN" },
244 { KEY_UP, "UP" },
245 { KEY_F+1, "F1" },
246 { KEY_F+2, "F2" },
247 { KEY_F+3, "F3" },
248 { KEY_F+4, "F4" },
249 { KEY_F+5, "F5" },
250 { KEY_F+6, "F6" },
251 { KEY_F+7, "F7" },
252 { KEY_F+8, "F8" },
253 { KEY_F+9, "F9" },
254 { KEY_F+10, "F10" },
255 { KEY_F+11, "F11" },
256 { KEY_F+12, "F12" },
257 { KEY_KP0, "KP0" },
258 { KEY_KP1, "KP1" },
259 { KEY_KP2, "KP2" },
260 { KEY_KP3, "KP3" },
261 { KEY_KP4, "KP4" },
262 { KEY_KP5, "KP5" },
263 { KEY_KP6, "KP6" },
264 { KEY_KP7, "KP7" },
265 { KEY_KP8, "KP8" },
266 { KEY_KP9, "KP9" },
267 { KEY_KPDEL, "KP_DEL" },
268 { KEY_KPDEC, "KP_DEC" },
269 { KEY_KPINS, "KP_INS" },
270 { KEY_KPENTER, "KP_ENTER" },
271 { MOUSE_BTN0, "MOUSE_BTN0" },
272 { MOUSE_BTN1, "MOUSE_BTN1" },
273 { MOUSE_BTN2, "MOUSE_BTN2" },
274 { MOUSE_BTN3, "MOUSE_BTN3" },
275 { MOUSE_BTN4, "MOUSE_BTN4" },
276 { MOUSE_BTN5, "MOUSE_BTN5" },
277 { MOUSE_BTN6, "MOUSE_BTN6" },
278 { MOUSE_BTN7, "MOUSE_BTN7" },
279 { MOUSE_BTN8, "MOUSE_BTN8" },
280 { MOUSE_BTN9, "MOUSE_BTN9" },
281 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
282 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
283 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
284 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
285 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
286 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
287 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
288 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
289 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
290 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
291 { JOY_AXIS1_MINUS, "JOY_UP" },
292 { JOY_AXIS1_PLUS, "JOY_DOWN" },
293 { JOY_AXIS0_MINUS, "JOY_LEFT" },
294 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
296 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
297 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
298 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
299 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
300 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
301 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
302 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
303 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
304 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
305 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
306 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
307 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
308 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
309 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
310 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
311 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
312 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
313 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
314 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
315 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
317 { JOY_BTN0, "JOY_BTN0" },
318 { JOY_BTN1, "JOY_BTN1" },
319 { JOY_BTN2, "JOY_BTN2" },
320 { JOY_BTN3, "JOY_BTN3" },
321 { JOY_BTN4, "JOY_BTN4" },
322 { JOY_BTN5, "JOY_BTN5" },
323 { JOY_BTN6, "JOY_BTN6" },
324 { JOY_BTN7, "JOY_BTN7" },
325 { JOY_BTN8, "JOY_BTN8" },
326 { JOY_BTN9, "JOY_BTN9" },
328 { AR_PLAY, "AR_PLAY" },
329 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
330 { AR_NEXT, "AR_NEXT" },
331 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
332 { AR_PREV, "AR_PREV" },
333 { AR_PREV_HOLD, "AR_PREV_HOLD" },
334 { AR_MENU, "AR_MENU" },
335 { AR_MENU_HOLD, "AR_MENU_HOLD" },
336 { AR_VUP, "AR_VUP" },
337 { AR_VDOWN, "AR_VDOWN" },
339 { KEY_POWER, "POWER" },
340 { KEY_MENU, "MENU" },
341 { KEY_PLAY, "PLAY" },
342 { KEY_PAUSE, "PAUSE" },
343 { KEY_PLAYPAUSE, "PLAYPAUSE" },
344 { KEY_STOP, "STOP" },
345 { KEY_FORWARD, "FORWARD" },
346 { KEY_REWIND, "REWIND" },
347 { KEY_NEXT, "NEXT" },
348 { KEY_PREV, "PREV" },
349 { KEY_VOLUME_UP, "VOLUME_UP" },
350 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
351 { KEY_MUTE, "MUTE" },
353 // These are kept for backward compatibility
354 { KEY_PAUSE, "XF86_PAUSE" },
355 { KEY_STOP, "XF86_STOP" },
356 { KEY_PREV, "XF86_PREV" },
357 { KEY_NEXT, "XF86_NEXT" },
359 { KEY_CLOSE_WIN, "CLOSE_WIN" },
361 { 0, NULL }
364 struct mp_key_name modifier_names[] = {
365 { KEY_MODIFIER_SHIFT, "Shift" },
366 { KEY_MODIFIER_CTRL, "Ctrl" },
367 { KEY_MODIFIER_ALT, "Alt" },
368 { KEY_MODIFIER_META, "Meta" },
369 { 0 }
372 #define KEY_MODIFIER_MASK (KEY_MODIFIER_SHIFT | KEY_MODIFIER_CTRL | KEY_MODIFIER_ALT | KEY_MODIFIER_META)
374 // This is the default binding. The content of input.conf overrides these.
375 // The first arg is a null terminated array of key codes.
376 // The second is the command
378 static const mp_cmd_bind_t def_cmd_binds[] = {
380 { { MOUSE_BTN3, 0 }, "seek 10" },
381 { { MOUSE_BTN4, 0 }, "seek -10" },
382 { { MOUSE_BTN5, 0 }, "volume 1" },
383 { { MOUSE_BTN6, 0 }, "volume -1" },
385 #ifdef CONFIG_DVDNAV
386 { { KEY_KP8, 0 }, "dvdnav up" }, // up
387 { { KEY_KP2, 0 }, "dvdnav down" }, // down
388 { { KEY_KP4, 0 }, "dvdnav left" }, // left
389 { { KEY_KP6, 0 }, "dvdnav right" }, // right
390 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
391 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
392 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
393 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
394 #endif
396 { { KEY_RIGHT, 0 }, "seek 10" },
397 { { KEY_LEFT, 0 }, "seek -10" },
398 { { KEY_MODIFIER_SHIFT + KEY_RIGHT, 0 }, "seek 1 0 1" },
399 { { KEY_MODIFIER_SHIFT + KEY_LEFT, 0 }, "seek -1 0 1" },
400 { { KEY_UP, 0 }, "seek 60" },
401 { { KEY_DOWN, 0 }, "seek -60" },
402 { { KEY_MODIFIER_SHIFT + KEY_UP, 0 }, "seek 5 0 1" },
403 { { KEY_MODIFIER_SHIFT + KEY_DOWN, 0 }, "seek -5 0 1" },
404 { { KEY_PAGE_UP, 0 }, "seek 600" },
405 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
406 { { '+', 0 }, "audio_delay 0.100" },
407 { { '-', 0 }, "audio_delay -0.100" },
408 { { '[', 0 }, "speed_mult 0.9091" },
409 { { ']', 0 }, "speed_mult 1.1" },
410 { { '{', 0 }, "speed_mult 0.5" },
411 { { '}', 0 }, "speed_mult 2.0" },
412 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
413 { { 'q', 0 }, "quit" },
414 { { KEY_ESC, 0 }, "quit" },
415 { { 'p', 0 }, "pause" },
416 { { ' ', 0 }, "pause" },
417 { { '.', 0 }, "frame_step" },
418 { { KEY_HOME, 0 }, "pt_up_step 1" },
419 { { KEY_END, 0 }, "pt_up_step -1" },
420 { { '>', 0 }, "pt_step 1" },
421 { { KEY_ENTER, 0 }, "pt_step 1 1" },
422 { { '<', 0 }, "pt_step -1" },
423 { { KEY_INS, 0 }, "alt_src_step 1" },
424 { { KEY_DEL, 0 }, "alt_src_step -1" },
425 { { 'o', 0 }, "osd" },
426 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
427 { { 'P', 0 }, "osd_show_progression" },
428 { { 'z', 0 }, "sub_delay -0.1" },
429 { { 'x', 0 }, "sub_delay +0.1" },
430 { { 'g', 0 }, "sub_step -1" },
431 { { 'y', 0 }, "sub_step +1" },
432 { { '9', 0 }, "volume -1" },
433 { { '/', 0 }, "volume -1" },
434 { { '0', 0 }, "volume 1" },
435 { { '*', 0 }, "volume 1" },
436 { { '(', 0 }, "balance -0.1" },
437 { { ')', 0 }, "balance 0.1" },
438 { { 'm', 0 }, "mute" },
439 { { '1', 0 }, "contrast -1" },
440 { { '2', 0 }, "contrast 1" },
441 { { '3', 0 }, "brightness -1" },
442 { { '4', 0 }, "brightness 1" },
443 { { '5', 0 }, "hue -1" },
444 { { '6', 0 }, "hue 1" },
445 { { '7', 0 }, "saturation -1" },
446 { { '8', 0 }, "saturation 1" },
447 { { 'd', 0 }, "frame_drop" },
448 { { 'D', 0 }, "step_property_osd deinterlace" },
449 { { 'c', 0 }, "step_property_osd yuv_colorspace" },
450 { { 'r', 0 }, "sub_pos -1" },
451 { { 't', 0 }, "sub_pos +1" },
452 { { 'a', 0 }, "sub_alignment" },
453 { { 'v', 0 }, "sub_visibility" },
454 { { 'j', 0 }, "sub_select" },
455 { { 'J', 0 }, "sub_select -3" },
456 { { 'F', 0 }, "forced_subs_only" },
457 { { '#', 0 }, "switch_audio" },
458 { { '_', 0 }, "step_property switch_video" },
459 { { KEY_TAB, 0 }, "step_property switch_program" },
460 { { 'i', 0 }, "edl_mark" },
461 #ifdef CONFIG_TV
462 { { 'h', 0 }, "tv_step_channel 1" },
463 { { 'k', 0 }, "tv_step_channel -1" },
464 { { 'n', 0 }, "tv_step_norm" },
465 { { 'u', 0 }, "tv_step_chanlist" },
466 #endif
467 { { 'X', 0 }, "step_property teletext_mode 1" },
468 { { 'W', 0 }, "step_property teletext_page 1" },
469 { { 'Q', 0 }, "step_property teletext_page -1" },
470 #ifdef CONFIG_JOYSTICK
471 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
472 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
473 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
474 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
475 { { JOY_BTN0, 0 }, "pause" },
476 { { JOY_BTN1, 0 }, "osd" },
477 { { JOY_BTN2, 0 }, "volume 1"},
478 { { JOY_BTN3, 0 }, "volume -1"},
479 #endif
480 #ifdef CONFIG_APPLE_REMOTE
481 { { AR_PLAY, 0}, "pause" },
482 { { AR_PLAY_HOLD, 0}, "quit" },
483 { { AR_NEXT, 0 }, "seek 30" },
484 { { AR_NEXT_HOLD, 0 }, "seek 120" },
485 { { AR_PREV, 0 }, "seek -10" },
486 { { AR_PREV_HOLD, 0 }, "seek -120" },
487 { { AR_MENU, 0 }, "osd" },
488 { { AR_MENU_HOLD, 0 }, "mute" },
489 { { AR_VUP, 0 }, "volume 1"},
490 { { AR_VDOWN, 0 }, "volume -1"},
491 #endif
492 { { 'T', 0 }, "vo_ontop" },
493 { { 'f', 0 }, "vo_fullscreen" },
494 { { 'C', 0 }, "step_property_osd capturing" },
495 { { 's', 0 }, "screenshot 0" },
496 { { 'S', 0 }, "screenshot 1" },
497 { { 'w', 0 }, "panscan -0.1" },
498 { { 'e', 0 }, "panscan +0.1" },
500 { { KEY_POWER, 0 }, "quit" },
501 { { KEY_MENU, 0 }, "osd" },
502 { { KEY_PLAY, 0 }, "pause" },
503 { { KEY_PAUSE, 0 }, "pause" },
504 { { KEY_PLAYPAUSE, 0 }, "pause" },
505 { { KEY_STOP, 0 }, "quit" },
506 { { KEY_FORWARD, 0 }, "seek 60" },
507 { { KEY_REWIND, 0 }, "seek -60" },
508 { { KEY_NEXT, 0 }, "pt_step 1" },
509 { { KEY_PREV, 0 }, "pt_step -1" },
510 { { KEY_VOLUME_UP, 0 }, "volume 1" },
511 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
512 { { KEY_MUTE, 0 }, "mute" },
514 { { KEY_CLOSE_WIN, 0 }, "quit" },
516 { { '!', 0 }, "seek_chapter -1" },
517 { { '@', 0 }, "seek_chapter 1" },
518 { { 'A', 0 }, "switch_angle 1" },
519 { { 'U', 0 }, "stop" },
521 { { 0 }, NULL }
525 #ifndef MP_MAX_KEY_FD
526 #define MP_MAX_KEY_FD 10
527 #endif
529 #ifndef MP_MAX_CMD_FD
530 #define MP_MAX_CMD_FD 10
531 #endif
533 #define CMD_QUEUE_SIZE 100
535 typedef struct mp_input_fd {
536 int fd;
537 union {
538 mp_key_func_t key;
539 mp_cmd_func_t cmd;
540 } read_func;
541 mp_close_func_t close_func;
542 void *ctx;
543 unsigned eof : 1;
544 unsigned drop : 1;
545 unsigned dead : 1;
546 unsigned got_cmd : 1;
547 unsigned no_select : 1;
548 // These fields are for the cmd fds.
549 char* buffer;
550 int pos,size;
551 } mp_input_fd_t;
553 typedef struct mp_cmd_filter mp_cmd_filter_t;
555 struct mp_cmd_filter {
556 mp_input_cmd_filter filter;
557 void* ctx;
558 mp_cmd_filter_t* next;
561 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
563 struct mp_cmd_bind_section {
564 mp_cmd_bind_t* cmd_binds;
565 char* section;
566 mp_cmd_bind_section_t* next;
569 struct input_ctx {
570 // Autorepeat stuff
571 short ar_state;
572 mp_cmd_t *ar_cmd;
573 unsigned int last_ar;
574 // Autorepeat config
575 unsigned int ar_delay;
576 unsigned int ar_rate;
578 // these are the keys currently down
579 int key_down[MP_MAX_KEY_DOWN];
580 unsigned int num_key_down;
581 unsigned int last_key_down;
583 bool default_bindings;
584 // List of command binding sections
585 mp_cmd_bind_section_t *cmd_bind_sections;
586 // Name of currently used command section
587 char *section;
588 // The command binds of current section
589 mp_cmd_bind_t *cmd_binds;
590 mp_cmd_bind_t *cmd_binds_default;
592 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
593 unsigned int num_key_fd;
595 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
596 unsigned int num_cmd_fd;
598 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
599 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
603 static mp_cmd_filter_t* cmd_filters = NULL;
605 // Callback to allow the menu filter to grab the incoming keys
606 int (*mp_input_key_cb)(int code) = NULL;
608 int async_quit_request;
610 static int print_key_list(m_option_t* cfg);
611 static int print_cmd_list(m_option_t* cfg);
613 // Our command line options
614 static const m_option_t input_conf[] = {
615 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
616 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
617 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
618 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
619 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
620 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
621 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
622 OPT_STRING("file", input.in_file, CONF_GLOBAL),
623 OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL),
624 { NULL, NULL, 0, 0, 0, 0, NULL}
627 static const m_option_t mp_input_opts[] = {
628 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
629 OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL),
630 OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL),
631 OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL),
632 OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL),
633 { NULL, NULL, 0, 0, 0, 0, NULL}
636 static int default_cmd_func(int fd,char* buf, int l);
638 static char *get_key_name(int key)
640 char *ret = talloc_strdup(NULL, "");
641 for (int i = 0; modifier_names[i].name; i++) {
642 if (modifier_names[i].key & key) {
643 ret = talloc_asprintf_append_buffer(ret, "%s+",
644 modifier_names[i].name);
645 key -= modifier_names[i].key;
648 for (int i = 0; key_names[i].name != NULL; i++) {
649 if (key_names[i].key == key)
650 return talloc_asprintf_append_buffer(ret, "%s", key_names[i].name);
653 if (isascii(key))
654 return talloc_asprintf_append_buffer(ret, "%c", key);
656 // Print the hex key code
657 return talloc_asprintf_append_buffer(ret, "%#-8x", key);
660 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
661 mp_cmd_func_t read_func, mp_close_func_t close_func)
663 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
664 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",fd);
665 return 0;
667 if (select && fd < 0) {
668 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
669 return 0;
672 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
673 .fd = fd,
674 .read_func.cmd = read_func ? read_func : default_cmd_func,
675 .close_func = close_func,
676 .no_select = !select
678 ictx->num_cmd_fd++;
680 return 1;
683 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
685 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
686 unsigned int i;
688 for (i = 0; i < ictx->num_cmd_fd; i++) {
689 if(cmd_fds[i].fd == fd)
690 break;
692 if (i == ictx->num_cmd_fd)
693 return;
694 if(cmd_fds[i].close_func)
695 cmd_fds[i].close_func(cmd_fds[i].fd);
696 talloc_free(cmd_fds[i].buffer);
698 if (i + 1 < ictx->num_cmd_fd)
699 memmove(&cmd_fds[i], &cmd_fds[i+1],
700 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
701 ictx->num_cmd_fd--;
704 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
706 struct mp_input_fd *key_fds = ictx->key_fds;
707 unsigned int i;
709 for (i = 0; i < ictx->num_key_fd; i++) {
710 if(key_fds[i].fd == fd)
711 break;
713 if (i == ictx->num_key_fd)
714 return;
715 if(key_fds[i].close_func)
716 key_fds[i].close_func(key_fds[i].fd);
718 if(i + 1 < ictx->num_key_fd)
719 memmove(&key_fds[i], &key_fds[i+1],
720 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
721 ictx->num_key_fd--;
724 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
725 mp_key_func_t read_func, mp_close_func_t close_func,
726 void *ctx)
728 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
729 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",fd);
730 return 0;
732 if (select && fd < 0) {
733 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
734 return 0;
737 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
738 .fd = fd,
739 .read_func.key = read_func,
740 .close_func = close_func,
741 .no_select = !select,
742 .ctx = ctx,
744 ictx->num_key_fd++;
746 return 1;
749 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
751 int cmd_num = 0;
753 while (*str == '\n' || *str == '\r' || *str == ' ')
754 ++str;
755 while (*str) {
756 mp_cmd_t *cmd;
757 size_t len = strcspn(str, "\r\n");
758 char *cmdbuf = talloc_size(NULL, len+1);
759 av_strlcpy(cmdbuf, str, len+1);
760 cmd = mp_input_parse_cmd(cmdbuf);
761 if (cmd) {
762 mp_input_queue_cmd(ictx, cmd);
763 ++cmd_num;
765 str += len;
766 while (*str == '\n' || *str == '\r' || *str == ' ')
767 ++str;
768 talloc_free(cmdbuf);
770 return cmd_num;
773 mp_cmd_t*
774 mp_input_parse_cmd(char* str) {
775 int i,l;
776 int pausing = 0;
777 char *ptr,*e;
778 const mp_cmd_t *cmd_def;
780 #ifdef MP_DEBUG
781 assert(str != NULL);
782 #endif
784 // Ignore heading spaces.
785 while (str[0] == ' ' || str[0] == '\t')
786 ++str;
788 if (strncmp(str, "pausing ", 8) == 0) {
789 pausing = 1;
790 str = &str[8];
791 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
792 pausing = 2;
793 str = &str[13];
794 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
795 pausing = 3;
796 str = &str[15];
797 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
798 pausing = 4;
799 str = &str[19];
802 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
803 /* NOTHING */;
804 if(ptr[0] != '\0')
805 l = ptr-str;
806 else
807 l = strlen(str);
809 if(l == 0)
810 return NULL;
812 for(i=0; mp_cmds[i].name != NULL; i++) {
813 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
814 break;
817 if(mp_cmds[i].name == NULL)
818 return NULL;
820 cmd_def = &mp_cmds[i];
822 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
823 *cmd = (mp_cmd_t){
824 .id = cmd_def->id,
825 .name = talloc_strdup(cmd, cmd_def->name),
826 .pausing = pausing,
829 ptr = str;
831 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
832 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
833 if(ptr[0] == '\0') break;
834 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
835 if(ptr[0] == '\0' || ptr[0] == '#') break;
836 cmd->args[i].type = cmd_def->args[i].type;
837 switch(cmd_def->args[i].type) {
838 case MP_CMD_ARG_INT:
839 errno = 0;
840 cmd->args[i].v.i = atoi(ptr);
841 if(errno != 0) {
842 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
843 ptr = NULL;
845 break;
846 case MP_CMD_ARG_FLOAT:
847 errno = 0;
848 cmd->args[i].v.f = atof(ptr);
849 if(errno != 0) {
850 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
851 ptr = NULL;
853 break;
854 case MP_CMD_ARG_STRING: {
855 char term;
856 char* ptr2 = ptr, *start;
858 if(ptr[0] == '\'' || ptr[0] == '"') {
859 term = ptr[0];
860 ptr2++;
861 } else
862 term = ' ';
863 start = ptr2;
864 while(1) {
865 e = strchr(ptr2,term);
866 if(!e) break;
867 if(e <= ptr2 || *(e - 1) != '\\') break;
868 ptr2 = e + 1;
871 if(term != ' ' && (!e || e[0] == '\0')) {
872 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
873 ptr = NULL;
874 break;
875 } else if(!e) e = ptr+strlen(ptr);
876 l = e-start;
877 ptr2 = start;
878 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
879 memmove(e,e+1,strlen(e));
880 ptr2 = e + 1;
881 l--;
883 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
884 if(term != ' ') ptr += l+2;
885 } break;
886 case -1:
887 ptr = NULL;
888 break;
889 default :
890 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
893 cmd->nargs = i;
895 if(cmd_def->nargs > cmd->nargs) {
896 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
897 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);
898 mp_cmd_free(cmd);
899 return NULL;
902 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
903 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
904 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
905 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
908 if(i < MP_CMD_MAX_ARGS)
909 cmd->args[i].type = -1;
911 return cmd;
914 #define MP_CMD_MAX_SIZE 4096
916 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
918 char* end;
919 (*ret) = NULL;
921 // Allocate the buffer if it doesn't exist
922 if(!mp_fd->buffer) {
923 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
924 mp_fd->pos = 0;
925 mp_fd->size = MP_CMD_MAX_SIZE;
928 // Get some data if needed/possible
929 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
930 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
931 mp_fd->size - 1 - mp_fd->pos);
932 // Error ?
933 if(r < 0) {
934 switch(r) {
935 case MP_INPUT_ERROR:
936 case MP_INPUT_DEAD:
937 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",mp_fd->fd,strerror(errno));
938 case MP_INPUT_NOTHING:
939 return r;
940 case MP_INPUT_RETRY:
941 continue;
943 // EOF ?
944 } else if(r == 0) {
945 mp_fd->eof = 1;
946 break;
948 mp_fd->pos += r;
949 break;
952 mp_fd->got_cmd = 0;
954 while(1) {
955 int l = 0;
956 // Find the cmd end
957 mp_fd->buffer[mp_fd->pos] = '\0';
958 end = strchr(mp_fd->buffer,'\r');
959 if (end) *end = '\n';
960 end = strchr(mp_fd->buffer,'\n');
961 // No cmd end ?
962 if(!end) {
963 // If buffer is full we must drop all until the next \n
964 if(mp_fd->size - mp_fd->pos <= 1) {
965 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd);
966 mp_fd->pos = 0;
967 mp_fd->drop = 1;
969 break;
971 // We already have a cmd : set the got_cmd flag
972 else if((*ret)) {
973 mp_fd->got_cmd = 1;
974 break;
977 l = end - mp_fd->buffer;
979 // Not dropping : put the cmd in ret
980 if (!mp_fd->drop)
981 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
982 else
983 mp_fd->drop = 0;
984 mp_fd->pos -= l+1;
985 memmove(mp_fd->buffer, end+1, mp_fd->pos);
988 if(*ret)
989 return 1;
990 else
991 return MP_INPUT_NOTHING;
994 static int default_cmd_func(int fd,char* buf, int l)
996 while(1) {
997 int r = read(fd,buf,l);
998 // Error ?
999 if(r < 0) {
1000 if(errno == EINTR)
1001 continue;
1002 else if(errno == EAGAIN)
1003 return MP_INPUT_NOTHING;
1004 return MP_INPUT_ERROR;
1005 // EOF ?
1007 return r;
1013 void
1014 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
1015 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
1017 filter->filter = func;
1018 filter->ctx = ctx;
1019 filter->next = cmd_filters;
1020 cmd_filters = filter;
1024 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
1026 int j;
1028 if (n <= 0) return NULL;
1029 for(j = 0; binds[j].cmd != NULL; j++) {
1030 int found = 1,s;
1031 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1032 if(binds[j].input[s] != keys[s]) {
1033 found = 0;
1034 break;
1037 if(found && binds[j].input[s] == 0 && s == n)
1038 break;
1040 return binds[j].cmd;
1043 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1044 char *section)
1046 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1048 if (section==NULL) section="default";
1049 while (bind_section) {
1050 if(strcmp(section,bind_section->section)==0) return bind_section;
1051 if(bind_section->next==NULL) break;
1052 bind_section=bind_section->next;
1054 if(bind_section) {
1055 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1056 bind_section=bind_section->next;
1057 } else {
1058 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1059 bind_section = ictx->cmd_bind_sections;
1061 bind_section->cmd_binds=NULL;
1062 bind_section->section = talloc_strdup(bind_section, section);
1063 bind_section->next=NULL;
1064 return bind_section;
1067 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys)
1069 char* cmd = NULL;
1070 mp_cmd_t* ret;
1072 if (ictx->cmd_binds)
1073 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1074 if (ictx->cmd_binds_default && cmd == NULL)
1075 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1076 if (ictx->default_bindings && cmd == NULL)
1077 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1079 if(cmd == NULL) {
1080 char *key_buf = get_key_name(keys[0]);
1081 mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", key_buf);
1082 talloc_free(key_buf);
1083 if(n > 1) {
1084 int s;
1085 for(s=1; s < n; s++) {
1086 key_buf = get_key_name(keys[s]);
1087 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", key_buf);
1088 talloc_free(key_buf);
1091 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1092 return NULL;
1094 if (strcmp(cmd, "ignore") == 0) return NULL;
1095 ret = mp_input_parse_cmd(cmd);
1096 if(!ret) {
1097 char *key_buf = get_key_name(ictx->key_down[0]);
1098 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s", key_buf);
1099 talloc_free(key_buf);
1100 if (ictx->num_key_down > 1) {
1101 unsigned int s;
1102 for(s=1; s < ictx->num_key_down; s++) {
1103 char *key_buf = get_key_name(ictx->key_down[s]);
1104 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf);
1105 talloc_free(key_buf);
1108 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1110 return ret;
1114 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code)
1116 unsigned int j;
1117 mp_cmd_t* ret;
1119 /* On normal keyboards shift changes the character code of non-special
1120 * keys, so don't count the modifier separately for those. In other words
1121 * we want to have "a" and "A" instead of "a" and "Shift+A"; but a separate
1122 * shift modifier is still kept for special keys like arrow keys.
1124 int unmod = code & ~KEY_MODIFIER_MASK;
1125 if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB)
1126 code &= ~KEY_MODIFIER_SHIFT;
1128 if(mp_input_key_cb) {
1129 if (code & MP_KEY_DOWN)
1130 return NULL;
1131 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1132 if (mp_input_key_cb(code))
1133 return NULL;
1136 if(code & MP_KEY_DOWN) {
1137 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1138 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1139 return NULL;
1141 code &= ~MP_KEY_DOWN;
1142 // Check if we don't already have this key as pushed
1143 for (j = 0; j < ictx->num_key_down; j++) {
1144 if (ictx->key_down[j] == code)
1145 break;
1147 if (j != ictx->num_key_down)
1148 return NULL;
1149 ictx->key_down[ictx->num_key_down] = code;
1150 ictx->num_key_down++;
1151 ictx->last_key_down = GetTimer();
1152 ictx->ar_state = 0;
1153 return NULL;
1155 // key released
1156 // Check if the key is in the down key, driver which can't send push event
1157 // send only release event
1158 for (j = 0; j < ictx->num_key_down; j++) {
1159 if (ictx->key_down[j] == code)
1160 break;
1162 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1163 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1164 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
1165 return NULL;
1167 ictx->key_down[ictx->num_key_down] = code;
1168 ictx->num_key_down++;
1169 ictx->last_key_down = 1;
1171 // We ignore key from last combination
1172 ret = ictx->last_key_down ?
1173 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down)
1174 : NULL;
1175 // Remove the key
1176 if (j+1 < ictx->num_key_down)
1177 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1178 (ictx->num_key_down-(j+1))*sizeof(int));
1179 ictx->num_key_down--;
1180 ictx->last_key_down = 0;
1181 ictx->ar_state = -1;
1182 mp_cmd_free(ictx->ar_cmd);
1183 ictx->ar_cmd = NULL;
1184 return ret;
1187 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx)
1189 // No input : autorepeat ?
1190 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1191 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1192 unsigned int t = GetTimer();
1193 // First time : wait delay
1194 if (ictx->ar_state == 0
1195 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1196 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1197 ictx->key_down);
1198 if (!ictx->ar_cmd) {
1199 ictx->ar_state = -1;
1200 return NULL;
1202 ictx->ar_state = 1;
1203 ictx->last_ar = t;
1204 return mp_cmd_clone(ictx->ar_cmd);
1205 // Then send rate / sec event
1206 } else if (ictx->ar_state == 1
1207 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1208 ictx->last_ar = t;
1209 return mp_cmd_clone(ictx->ar_cmd);
1212 return NULL;
1217 * \param time time to wait at most for an event in milliseconds
1219 static mp_cmd_t *read_events(struct input_ctx *ictx, int time)
1221 int i;
1222 int got_cmd = 0;
1223 struct mp_input_fd *key_fds = ictx->key_fds;
1224 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1225 for (i = 0; i < ictx->num_key_fd; i++)
1226 if (key_fds[i].dead) {
1227 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1228 i--;
1230 for (i = 0; i < ictx->num_cmd_fd; i++)
1231 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1232 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1233 i--;
1235 else if (cmd_fds[i].got_cmd)
1236 got_cmd = 1;
1237 #ifdef HAVE_POSIX_SELECT
1238 fd_set fds;
1239 FD_ZERO(&fds);
1240 if (!got_cmd) {
1241 int max_fd = 0, num_fd = 0;
1242 for (i = 0; i < ictx->num_key_fd; i++) {
1243 if (key_fds[i].no_select)
1244 continue;
1245 if (key_fds[i].fd > max_fd)
1246 max_fd = key_fds[i].fd;
1247 FD_SET(key_fds[i].fd, &fds);
1248 num_fd++;
1250 for (i = 0; i < ictx->num_cmd_fd; i++) {
1251 if (cmd_fds[i].no_select)
1252 continue;
1253 if (cmd_fds[i].fd > max_fd)
1254 max_fd = cmd_fds[i].fd;
1255 FD_SET(cmd_fds[i].fd, &fds);
1256 num_fd++;
1258 if (num_fd > 0) {
1259 struct timeval tv, *time_val;
1260 if (time >= 0) {
1261 tv.tv_sec = time / 1000;
1262 tv.tv_usec = (time % 1000) * 1000;
1263 time_val = &tv;
1265 else
1266 time_val = NULL;
1267 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1268 if (errno != EINTR)
1269 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Select error: %s\n",
1270 strerror(errno));
1271 FD_ZERO(&fds);
1275 #else
1276 if (!got_cmd && time)
1277 usec_sleep(time * 1000);
1278 #endif
1281 for (i = 0; i < ictx->num_key_fd; i++) {
1282 #ifdef HAVE_POSIX_SELECT
1283 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1284 continue;
1285 #endif
1287 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1288 if (code >= 0) {
1289 mp_cmd_t *ret = interpret_key(ictx, code);
1290 if (ret)
1291 return ret;
1293 else if (code == MP_INPUT_ERROR)
1294 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n",
1295 key_fds[i].fd);
1296 else if (code == MP_INPUT_DEAD) {
1297 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n",
1298 key_fds[i].fd);
1299 key_fds[i].dead = 1;
1302 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx);
1303 if (autorepeat_cmd)
1304 return autorepeat_cmd;
1306 for (i = 0; i < ictx->num_cmd_fd; i++) {
1307 #ifdef HAVE_POSIX_SELECT
1308 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1309 !cmd_fds[i].got_cmd)
1310 continue;
1311 #endif
1312 char *cmd;
1313 int r = read_cmd(&cmd_fds[i], &cmd);
1314 if (r >= 0) {
1315 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1316 talloc_free(cmd);
1317 if (ret)
1318 return ret;
1320 else if (r == MP_INPUT_ERROR)
1321 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n",
1322 cmd_fds[i].fd);
1323 else if (r == MP_INPUT_DEAD)
1324 cmd_fds[i].dead = 1;
1327 return NULL;
1331 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1333 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1334 return 0;
1335 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1336 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1337 ictx->cmd_queue_length++;
1338 return 1;
1341 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1343 mp_cmd_t* ret;
1345 if (ictx->cmd_queue_length == 0)
1346 return NULL;
1348 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1350 if (!peek_only) {
1351 ictx->cmd_queue_length--;
1352 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1355 return ret;
1359 * \param peek_only when set, the returned command stays in the queue.
1360 * Do not free the returned cmd whe you set this!
1362 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only)
1364 mp_cmd_t* ret = NULL;
1365 mp_cmd_filter_t* cf;
1366 int from_queue;
1368 if (async_quit_request)
1369 return mp_input_parse_cmd("quit 1");
1370 while(1) {
1371 from_queue = 1;
1372 ret = get_queued_cmd(ictx, peek_only);
1373 if(ret) break;
1374 from_queue = 0;
1375 ret = read_events(ictx, time);
1376 if (!ret) {
1377 from_queue = 1;
1378 ret = get_queued_cmd(ictx, peek_only);
1380 break;
1382 if(!ret) return NULL;
1384 for(cf = cmd_filters ; cf ; cf = cf->next) {
1385 if (cf->filter(ret, cf->ctx)) {
1386 if (peek_only && from_queue)
1387 // The filter ate the cmd, so we remove it from queue
1388 ret = get_queued_cmd(ictx, 0);
1389 mp_cmd_free(ret);
1390 return NULL;
1394 if (!from_queue && peek_only)
1395 mp_input_queue_cmd(ictx, ret);
1397 return ret;
1400 void
1401 mp_cmd_free(mp_cmd_t* cmd) {
1402 talloc_free(cmd);
1405 mp_cmd_t*
1406 mp_cmd_clone(mp_cmd_t* cmd) {
1407 mp_cmd_t* ret;
1408 int i;
1409 #ifdef MP_DEBUG
1410 assert(cmd != NULL);
1411 #endif
1413 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1414 ret->name = talloc_strdup(ret, cmd->name);
1415 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1416 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1417 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1420 return ret;
1423 int mp_input_get_key_from_name(const char *name)
1425 int modifiers = 0;
1426 const char *p;
1427 while (p = strchr(name, '+')) {
1428 for (struct mp_key_name *m = modifier_names; m->name; m++)
1429 if (!bstrcasecmp(BSTR(m->name), (struct bstr){name, p - name})) {
1430 modifiers |= m->key;
1431 goto found;
1433 if (!strcmp(name, "+"))
1434 return '+' + modifiers;
1435 return -1;
1436 found:
1437 name = p + 1;
1439 int len = strlen(name);
1440 if (len == 1) // Direct key code
1441 return (unsigned char)name[0] + modifiers;
1442 else if (len > 2 && strncasecmp("0x", name, 2) == 0)
1443 return strtol(name, NULL, 16) + modifiers;
1445 for (int i = 0; key_names[i].name != NULL; i++) {
1446 if (strcasecmp(key_names[i].name, name) == 0)
1447 return key_names[i].key + modifiers;
1450 return -1;
1453 static int get_input_from_name(char* name,int* keys) {
1454 char *end,*ptr;
1455 int n=0;
1457 ptr = name;
1458 n = 0;
1459 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1460 if(end && end[1] != '\0') {
1461 if(end[1] == '-')
1462 end = &end[1];
1463 end[0] = '\0';
1465 keys[n] = mp_input_get_key_from_name(ptr);
1466 if(keys[n] < 0) {
1467 return 0;
1469 n++;
1470 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1471 ptr = &end[1];
1472 else
1473 break;
1475 keys[n] = 0;
1476 return 1;
1479 #define BS_MAX 256
1480 #define SPACE_CHAR " \n\r\t"
1482 static void bind_keys(struct input_ctx *ictx,
1483 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1485 int i = 0,j;
1486 mp_cmd_bind_t* bind = NULL;
1487 mp_cmd_bind_section_t* bind_section = NULL;
1488 char *section=NULL, *p;
1490 #ifdef MP_DEBUG
1491 assert(keys != NULL);
1492 assert(cmd != NULL);
1493 #endif
1495 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1496 *p=0;
1497 section=++cmd;
1498 cmd=++p;
1499 // Jump beginning space
1500 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1501 /* NOTHING */;
1503 bind_section = get_bind_section(ictx, section);
1505 if(bind_section->cmd_binds) {
1506 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1507 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1508 /* NOTHING */;
1509 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1510 bind = &bind_section->cmd_binds[i];
1511 break;
1516 if(!bind) {
1517 bind_section->cmd_binds = talloc_realloc(bind_section,
1518 bind_section->cmd_binds,
1519 mp_cmd_bind_t, i + 2);
1520 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1521 bind = &bind_section->cmd_binds[i];
1523 talloc_free(bind->cmd);
1524 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1525 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1528 static int parse_config(struct input_ctx *ictx, char *file)
1530 int fd;
1531 int bs = 0,r,eof = 0,comments = 0;
1532 char *iter,*end;
1533 char buffer[BS_MAX];
1534 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1536 fd = open(file,O_RDONLY);
1538 if(fd < 0) {
1539 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1540 return 0;
1543 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1545 while(1) {
1546 if(! eof && bs < BS_MAX-1) {
1547 if(bs > 0) bs--;
1548 r = read(fd,buffer+bs,BS_MAX-1-bs);
1549 if(r < 0) {
1550 if(errno == EINTR)
1551 continue;
1552 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1553 close(fd);
1554 return 0;
1555 } else if(r == 0) {
1556 eof = 1;
1557 } else {
1558 bs += r+1;
1559 buffer[bs-1] = '\0';
1562 // Empty buffer : return
1563 if(bs <= 1) {
1564 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1565 close(fd);
1566 return 1;
1569 iter = buffer;
1571 if(comments) {
1572 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1573 /* NOTHING */;
1574 if(iter[0] == '\0') { // Buffer was full of comment
1575 bs = 0;
1576 continue;
1578 iter++;
1579 r = strlen(iter);
1580 memmove(buffer,iter,r+1);
1581 bs = r+1;
1582 comments = 0;
1583 continue;
1586 // Find the wanted key
1587 if(keys[0] == 0) {
1588 // Jump beginning space
1589 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1590 /* NOTHING */;
1591 if(iter[0] == '\0') { // Buffer was full of space char
1592 bs = 0;
1593 continue;
1595 if(iter[0] == '#') { // Comments
1596 comments = 1;
1597 continue;
1599 // Find the end of the key code name
1600 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1601 /*NOTHING */;
1602 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1603 if(buffer == iter) {
1604 if(eof && (buffer-iter) == bs)
1605 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1606 else
1607 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1608 return 0;
1610 memmove(buffer,iter,end-iter);
1611 bs = end-iter;
1612 continue;
1615 char name[end-iter+1];
1616 strncpy(name,iter,end-iter);
1617 name[end-iter] = '\0';
1618 if (!get_input_from_name(name,keys)) {
1619 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1620 close(fd);
1621 return 0;
1624 if( bs > (end-buffer))
1625 memmove(buffer,end,bs - (end-buffer));
1626 bs -= end-buffer;
1627 continue;
1628 } else { // Get the command
1629 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1630 // Found new line
1631 if(iter[0] == '\n' || iter[0] == '\r') {
1632 int i;
1633 char *key_buf = get_key_name(keys[0]);
1634 mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", key_buf);
1635 talloc_free(key_buf);
1636 for(i = 1; keys[i] != 0 ; i++) {
1637 char *key_buf = get_key_name(keys[i]);
1638 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf);
1639 talloc_free(key_buf);
1641 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1642 keys[0] = 0;
1643 if(iter > buffer) {
1644 memmove(buffer,iter,bs- (iter-buffer));
1645 bs -= (iter-buffer);
1647 continue;
1649 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1650 /* NOTHING */;
1651 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1652 if(iter == buffer) {
1653 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1654 close(fd);
1655 return 0;
1657 memmove(buffer,iter,end - iter);
1658 bs = end - iter;
1659 continue;
1662 char cmd[end-iter+1];
1663 strncpy(cmd,iter,end-iter);
1664 cmd[end-iter] = '\0';
1665 //printf("Set bind %d => %s\n",keys[0],cmd);
1666 bind_keys(ictx, keys,cmd);
1667 n_binds++;
1669 keys[0] = 0;
1670 end++;
1671 if(bs > (end-buffer))
1672 memmove(buffer,end,bs-(end-buffer));
1673 bs -= (end-buffer);
1674 buffer[bs-1] = '\0';
1675 continue;
1678 mp_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1679 close(fd);
1680 mp_input_set_section(ictx, NULL);
1681 return 0;
1684 void mp_input_set_section(struct input_ctx *ictx, char *name)
1686 mp_cmd_bind_section_t* bind_section = NULL;
1688 ictx->cmd_binds = NULL;
1689 ictx->cmd_binds_default = NULL;
1690 talloc_free(ictx->section);
1691 if (name)
1692 ictx->section = talloc_strdup(ictx, name);
1693 else
1694 ictx->section = talloc_strdup(ictx, "default");
1695 if ((bind_section = get_bind_section(ictx, ictx->section)))
1696 ictx->cmd_binds = bind_section->cmd_binds;
1697 if (strcmp(ictx->section, "default") == 0)
1698 return;
1699 if ((bind_section = get_bind_section(ictx, NULL)))
1700 ictx->cmd_binds_default = bind_section->cmd_binds;
1703 char *mp_input_get_section(struct input_ctx *ictx)
1705 return ictx->section;
1708 struct input_ctx *mp_input_init(struct input_conf *input_conf)
1710 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1711 *ictx = (struct input_ctx){
1712 .ar_state = -1,
1713 .ar_delay = input_conf->ar_delay,
1714 .ar_rate = input_conf->ar_rate,
1715 .default_bindings = input_conf->default_bindings,
1718 char* file;
1719 char *config_file = input_conf->config_file;
1720 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1721 if(!file)
1722 return ictx;
1724 if (!parse_config(ictx, file)) {
1725 // free file if it was allocated by get_path(),
1726 // before it gets overwritten
1727 if( file != config_file)
1729 free(file);
1731 // Try global conf dir
1732 file = MPLAYER_CONFDIR "/input.conf";
1733 if (!parse_config(ictx, file))
1734 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1736 else
1738 // free file if it was allocated by get_path()
1739 if( file != config_file)
1740 free(file);
1743 #ifdef CONFIG_JOYSTICK
1744 if (input_conf->use_joystick) {
1745 int fd = mp_input_joystick_init(input_conf->js_dev);
1746 if(fd < 0)
1747 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1748 else
1749 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1750 (mp_close_func_t)close,NULL);
1752 #endif
1754 #ifdef CONFIG_LIRC
1755 if (input_conf->use_lirc) {
1756 int fd = mp_input_lirc_init();
1757 if(fd > 0)
1758 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1759 mp_input_lirc_close);
1761 #endif
1763 #ifdef CONFIG_LIRCC
1764 if (input_conf->use_lircc) {
1765 int fd = lircc_init("mplayer", NULL);
1766 if(fd >= 0)
1767 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1769 #endif
1771 #ifdef CONFIG_APPLE_REMOTE
1772 if (input_conf->use_ar) {
1773 if(mp_input_ar_init() < 0)
1774 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1775 else
1776 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1777 NULL);
1779 #endif
1781 #ifdef CONFIG_APPLE_IR
1782 if (input_conf->use_ar) {
1783 int fd = mp_input_appleir_init(input_conf->ar_dev);
1784 if(fd < 0)
1785 mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n");
1786 else
1787 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1788 (mp_close_func_t)close, NULL);
1790 #endif
1792 if (input_conf->in_file) {
1793 struct stat st;
1794 int mode = O_RDONLY;
1795 // Use RDWR for FIFOs to ensure they stay open over multiple accesses.
1796 // Note that on Windows stat may fail for named pipes, but due to how the
1797 // API works, using RDONLY should be ok.
1798 if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode))
1799 mode = O_RDWR;
1800 int in_file_fd = open(input_conf->in_file, mode);
1801 if(in_file_fd >= 0)
1802 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1803 (mp_close_func_t)close);
1804 else
1805 mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n",
1806 input_conf->in_file, strerror(errno));
1808 return ictx;
1811 void mp_input_uninit(struct input_ctx *ictx)
1813 if (!ictx)
1814 return;
1816 unsigned int i;
1818 for (i=0; i < ictx->num_key_fd; i++) {
1819 if (ictx->key_fds[i].close_func)
1820 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1823 for (i = 0; i < ictx->num_cmd_fd; i++) {
1824 if (ictx->cmd_fds[i].close_func)
1825 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1827 talloc_free(ictx);
1830 void
1831 mp_input_register_options(m_config_t* cfg) {
1832 m_config_register_options(cfg,mp_input_opts);
1835 static int print_key_list(m_option_t* cfg)
1837 int i;
1838 printf("\n");
1839 for(i= 0; key_names[i].name != NULL ; i++)
1840 printf("%s\n",key_names[i].name);
1841 exit(0);
1844 static int print_cmd_list(m_option_t* cfg)
1846 const mp_cmd_t *cmd;
1847 int i,j;
1848 const char* type;
1850 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1851 printf("%-20.20s",cmd->name);
1852 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1853 switch(cmd->args[j].type) {
1854 case MP_CMD_ARG_INT:
1855 type = "Integer";
1856 break;
1857 case MP_CMD_ARG_FLOAT:
1858 type = "Float";
1859 break;
1860 case MP_CMD_ARG_STRING:
1861 type = "String";
1862 break;
1863 default:
1864 type = "??";
1866 if(j+1 > cmd->nargs)
1867 printf(" [%s]",type);
1868 else
1869 printf(" %s",type);
1871 printf("\n");
1873 exit(0);
1877 * \param time time to wait for an interruption in milliseconds
1879 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1881 mp_cmd_t* cmd;
1882 if ((cmd = mp_input_get_cmd(ictx, time, 1)) == NULL)
1883 return 0;
1884 switch(cmd->id) {
1885 case MP_CMD_QUIT:
1886 case MP_CMD_PLAY_TREE_STEP:
1887 case MP_CMD_PLAY_TREE_UP_STEP:
1888 case MP_CMD_PLAY_ALT_SRC_STEP:
1889 // The cmd will be executed when we are back in the main loop
1890 return 1;
1892 // remove the cmd from the queue
1893 cmd = mp_input_get_cmd(ictx, time, 0);
1894 mp_cmd_free(cmd);
1895 return 0;