vo_xv: Reformat code
[mplayer.git] / input / input.c
blob3e917afa49f868d8e1f21e416d732fb2e035f157
1 #include "config.h"
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <sys/time.h>
11 #include <fcntl.h>
12 #include <ctype.h>
14 #include "input.h"
15 #include "mouse.h"
16 #ifdef MP_DEBUG
17 #include <assert.h>
18 #endif
19 #include "mp_fifo.h"
20 #include "osdep/keycodes.h"
21 #include "osdep/timer.h"
22 #include "libavutil/avstring.h"
23 #include "mp_msg.h"
24 #include "help_mp.h"
25 #include "m_config.h"
26 #include "m_option.h"
27 #include "get_path.h"
28 #include "talloc.h"
29 #include "options.h"
31 #include "joystick.h"
33 #ifdef CONFIG_LIRC
34 #include "lirc.h"
35 #endif
37 #ifdef CONFIG_LIRCC
38 #include <lirc/lircc.h>
39 #endif
41 #include "ar.h"
43 typedef struct mp_cmd_bind {
44 int input[MP_MAX_KEY_DOWN+1];
45 char* cmd;
46 } mp_cmd_bind_t;
48 typedef struct mp_key_name {
49 int key;
50 char* name;
51 } mp_key_name_t;
53 /// This array defines all known commands.
54 /// The first field is an id used to recognize the command without too many strcmp.
55 /// The second is obviously the command name.
56 /// The third is the minimum number of arguments this command needs.
57 /// Then comes the definition of each argument, terminated with an arg of type -1.
58 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
59 /// the last one) which is actually 9.
61 /// For the args, the first field is the type (actually int, float or string), the second
62 /// is the default value wich is used for optional arguments
64 static const mp_cmd_t mp_cmds[] = {
65 #ifdef CONFIG_RADIO
66 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
67 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
68 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
69 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
70 #endif
71 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
72 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
73 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
74 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
75 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
76 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
77 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
78 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
79 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
80 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
81 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
82 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
83 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
84 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
85 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
86 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
87 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
88 { 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}} } },
89 { 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}} } },
90 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
91 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
93 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
94 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
95 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
96 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
97 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
98 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
99 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
100 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
101 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
102 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
103 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
104 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
105 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
106 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
107 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
108 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
109 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
110 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
111 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
112 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
113 #ifdef CONFIG_ASS
114 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
115 #endif
116 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
117 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
118 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
119 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
120 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
121 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
122 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
123 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
124 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
125 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
126 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
127 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
128 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
129 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
130 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
131 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
132 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
133 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
134 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
135 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
136 #ifdef CONFIG_TV
137 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
138 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
139 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
140 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
141 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
142 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
143 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
144 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
145 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
146 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
147 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
148 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
149 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
150 #endif
151 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
152 #ifdef CONFIG_DVBIN
153 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
154 #endif
155 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
156 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
157 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
158 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
159 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
160 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
161 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
162 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
163 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
164 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
165 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
166 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
167 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
168 #ifdef CONFIG_TV_TELETEXT
169 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
170 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
171 #endif
173 #ifdef CONFIG_GUI
174 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
175 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
176 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
177 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
178 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
179 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
180 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
181 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
182 #endif
184 #ifdef CONFIG_DVDNAV
185 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
186 #endif
188 #ifdef CONFIG_MENU
189 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
190 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
191 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
192 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
193 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
194 #endif
196 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
197 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
198 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
199 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
200 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
201 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
203 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
204 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
206 { 0, NULL, 0, {} }
209 /// The names of the keys as used in input.conf
210 /// If you add some new keys, you also need to add them here
212 static const mp_key_name_t key_names[] = {
213 { ' ', "SPACE" },
214 { '#', "SHARP" },
215 { KEY_ENTER, "ENTER" },
216 { KEY_TAB, "TAB" },
217 { KEY_CTRL, "CTRL" },
218 { KEY_BACKSPACE, "BS" },
219 { KEY_DELETE, "DEL" },
220 { KEY_INSERT, "INS" },
221 { KEY_HOME, "HOME" },
222 { KEY_END, "END" },
223 { KEY_PAGE_UP, "PGUP" },
224 { KEY_PAGE_DOWN, "PGDWN" },
225 { KEY_ESC, "ESC" },
226 { KEY_RIGHT, "RIGHT" },
227 { KEY_LEFT, "LEFT" },
228 { KEY_DOWN, "DOWN" },
229 { KEY_UP, "UP" },
230 { KEY_F+1, "F1" },
231 { KEY_F+2, "F2" },
232 { KEY_F+3, "F3" },
233 { KEY_F+4, "F4" },
234 { KEY_F+5, "F5" },
235 { KEY_F+6, "F6" },
236 { KEY_F+7, "F7" },
237 { KEY_F+8, "F8" },
238 { KEY_F+9, "F9" },
239 { KEY_F+10, "F10" },
240 { KEY_F+11, "F11" },
241 { KEY_F+12, "F12" },
242 { KEY_KP0, "KP0" },
243 { KEY_KP1, "KP1" },
244 { KEY_KP2, "KP2" },
245 { KEY_KP3, "KP3" },
246 { KEY_KP4, "KP4" },
247 { KEY_KP5, "KP5" },
248 { KEY_KP6, "KP6" },
249 { KEY_KP7, "KP7" },
250 { KEY_KP8, "KP8" },
251 { KEY_KP9, "KP9" },
252 { KEY_KPDEL, "KP_DEL" },
253 { KEY_KPDEC, "KP_DEC" },
254 { KEY_KPINS, "KP_INS" },
255 { KEY_KPENTER, "KP_ENTER" },
256 { MOUSE_BTN0, "MOUSE_BTN0" },
257 { MOUSE_BTN1, "MOUSE_BTN1" },
258 { MOUSE_BTN2, "MOUSE_BTN2" },
259 { MOUSE_BTN3, "MOUSE_BTN3" },
260 { MOUSE_BTN4, "MOUSE_BTN4" },
261 { MOUSE_BTN5, "MOUSE_BTN5" },
262 { MOUSE_BTN6, "MOUSE_BTN6" },
263 { MOUSE_BTN7, "MOUSE_BTN7" },
264 { MOUSE_BTN8, "MOUSE_BTN8" },
265 { MOUSE_BTN9, "MOUSE_BTN9" },
266 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
267 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
268 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
269 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
270 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
271 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
272 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
273 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
274 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
275 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
276 { JOY_AXIS1_MINUS, "JOY_UP" },
277 { JOY_AXIS1_PLUS, "JOY_DOWN" },
278 { JOY_AXIS0_MINUS, "JOY_LEFT" },
279 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
281 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
282 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
283 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
284 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
285 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
286 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
287 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
288 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
289 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
290 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
291 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
292 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
293 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
294 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
295 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
296 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
297 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
298 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
299 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
300 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
302 { JOY_BTN0, "JOY_BTN0" },
303 { JOY_BTN1, "JOY_BTN1" },
304 { JOY_BTN2, "JOY_BTN2" },
305 { JOY_BTN3, "JOY_BTN3" },
306 { JOY_BTN4, "JOY_BTN4" },
307 { JOY_BTN5, "JOY_BTN5" },
308 { JOY_BTN6, "JOY_BTN6" },
309 { JOY_BTN7, "JOY_BTN7" },
310 { JOY_BTN8, "JOY_BTN8" },
311 { JOY_BTN9, "JOY_BTN9" },
313 { AR_PLAY, "AR_PLAY" },
314 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
315 { AR_NEXT, "AR_NEXT" },
316 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
317 { AR_PREV, "AR_PREV" },
318 { AR_PREV_HOLD, "AR_PREV_HOLD" },
319 { AR_MENU, "AR_MENU" },
320 { AR_MENU_HOLD, "AR_MENU_HOLD" },
321 { AR_VUP, "AR_VUP" },
322 { AR_VDOWN, "AR_VDOWN" },
324 { KEY_POWER, "POWER" },
325 { KEY_MENU, "MENU" },
326 { KEY_PLAY, "PLAY" },
327 { KEY_PAUSE, "PAUSE" },
328 { KEY_PLAYPAUSE, "PLAYPAUSE" },
329 { KEY_STOP, "STOP" },
330 { KEY_FORWARD, "FORWARD" },
331 { KEY_REWIND, "REWIND" },
332 { KEY_NEXT, "NEXT" },
333 { KEY_PREV, "PREV" },
334 { KEY_VOLUME_UP, "VOLUME_UP" },
335 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
336 { KEY_MUTE, "MUTE" },
338 // These are kept for backward compatibility
339 { KEY_PAUSE, "XF86_PAUSE" },
340 { KEY_STOP, "XF86_STOP" },
341 { KEY_PREV, "XF86_PREV" },
342 { KEY_NEXT, "XF86_NEXT" },
344 { KEY_CLOSE_WIN, "CLOSE_WIN" },
346 { 0, NULL }
349 // This is the default binding. The content of input.conf overrides these.
350 // The first arg is a null terminated array of key codes.
351 // The second is the command
353 static const mp_cmd_bind_t def_cmd_binds[] = {
355 { { MOUSE_BTN3, 0 }, "seek 10" },
356 { { MOUSE_BTN4, 0 }, "seek -10" },
357 { { MOUSE_BTN5, 0 }, "volume 1" },
358 { { MOUSE_BTN6, 0 }, "volume -1" },
360 #ifdef CONFIG_DVDNAV
361 { { KEY_KP8, 0 }, "dvdnav up" }, // up
362 { { KEY_KP2, 0 }, "dvdnav down" }, // down
363 { { KEY_KP4, 0 }, "dvdnav left" }, // left
364 { { KEY_KP6, 0 }, "dvdnav right" }, // right
365 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
366 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
367 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
368 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
369 #endif
371 { { KEY_RIGHT, 0 }, "seek 10" },
372 { { KEY_LEFT, 0 }, "seek -10" },
373 { { KEY_UP, 0 }, "seek 60" },
374 { { KEY_DOWN, 0 }, "seek -60" },
375 { { KEY_PAGE_UP, 0 }, "seek 600" },
376 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
377 { { '+', 0 }, "audio_delay 0.100" },
378 { { '-', 0 }, "audio_delay -0.100" },
379 { { '[', 0 }, "speed_mult 0.9091" },
380 { { ']', 0 }, "speed_mult 1.1" },
381 { { '{', 0 }, "speed_mult 0.5" },
382 { { '}', 0 }, "speed_mult 2.0" },
383 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
384 { { 'q', 0 }, "quit" },
385 { { KEY_ESC, 0 }, "quit" },
386 { { 'p', 0 }, "pause" },
387 { { ' ', 0 }, "pause" },
388 { { '.', 0 }, "frame_step" },
389 { { KEY_HOME, 0 }, "pt_up_step 1" },
390 { { KEY_END, 0 }, "pt_up_step -1" },
391 { { '>', 0 }, "pt_step 1" },
392 { { KEY_ENTER, 0 }, "pt_step 1 1" },
393 { { '<', 0 }, "pt_step -1" },
394 { { KEY_INS, 0 }, "alt_src_step 1" },
395 { { KEY_DEL, 0 }, "alt_src_step -1" },
396 { { 'o', 0 }, "osd" },
397 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
398 { { 'z', 0 }, "sub_delay -0.1" },
399 { { 'x', 0 }, "sub_delay +0.1" },
400 { { 'g', 0 }, "sub_step -1" },
401 { { 'y', 0 }, "sub_step +1" },
402 { { '9', 0 }, "volume -1" },
403 { { '/', 0 }, "volume -1" },
404 { { '0', 0 }, "volume 1" },
405 { { '*', 0 }, "volume 1" },
406 { { '(', 0 }, "balance -0.1" },
407 { { ')', 0 }, "balance 0.1" },
408 { { 'm', 0 }, "mute" },
409 { { '1', 0 }, "contrast -1" },
410 { { '2', 0 }, "contrast 1" },
411 { { '3', 0 }, "brightness -1" },
412 { { '4', 0 }, "brightness 1" },
413 { { '5', 0 }, "hue -1" },
414 { { '6', 0 }, "hue 1" },
415 { { '7', 0 }, "saturation -1" },
416 { { '8', 0 }, "saturation 1" },
417 { { 'd', 0 }, "frame_drop" },
418 { { 'D', 0 }, "step_property deinterlace" },
419 { { 'r', 0 }, "sub_pos -1" },
420 { { 't', 0 }, "sub_pos +1" },
421 { { 'a', 0 }, "sub_alignment" },
422 { { 'v', 0 }, "sub_visibility" },
423 { { 'j', 0 }, "sub_select" },
424 { { 'F', 0 }, "forced_subs_only" },
425 { { '#', 0 }, "switch_audio" },
426 { { '_', 0 }, "step_property switch_video" },
427 { { KEY_TAB, 0 }, "step_property switch_program" },
428 { { 'i', 0 }, "edl_mark" },
429 #ifdef CONFIG_TV
430 { { 'h', 0 }, "tv_step_channel 1" },
431 { { 'k', 0 }, "tv_step_channel -1" },
432 { { 'n', 0 }, "tv_step_norm" },
433 { { 'u', 0 }, "tv_step_chanlist" },
434 #endif
435 #ifdef CONFIG_TV_TELETEXT
436 { { 'X', 0 }, "step_property teletext_mode 1" },
437 { { 'W', 0 }, "step_property teletext_page 1" },
438 { { 'Q', 0 }, "step_property teletext_page -1" },
439 #endif
440 #ifdef CONFIG_JOYSTICK
441 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
442 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
443 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
444 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
445 { { JOY_BTN0, 0 }, "pause" },
446 { { JOY_BTN1, 0 }, "osd" },
447 { { JOY_BTN2, 0 }, "volume 1"},
448 { { JOY_BTN3, 0 }, "volume -1"},
449 #endif
450 #ifdef CONFIG_APPLE_REMOTE
451 { { AR_PLAY, 0}, "pause" },
452 { { AR_PLAY_HOLD, 0}, "quit" },
453 { { AR_NEXT, 0 }, "seek 30" },
454 { { AR_NEXT_HOLD, 0 }, "seek 120" },
455 { { AR_PREV, 0 }, "seek -10" },
456 { { AR_PREV_HOLD, 0 }, "seek -120" },
457 { { AR_MENU, 0 }, "osd" },
458 { { AR_MENU_HOLD, 0 }, "mute" },
459 { { AR_VUP, 0 }, "volume 1"},
460 { { AR_VDOWN, 0 }, "volume -1"},
461 #endif
462 { { 'T', 0 }, "vo_ontop" },
463 { { 'f', 0 }, "vo_fullscreen" },
464 { { 's', 0 }, "screenshot 0" },
465 { { 'S', 0 }, "screenshot 1" },
466 { { 'w', 0 }, "panscan -0.1" },
467 { { 'e', 0 }, "panscan +0.1" },
469 { { KEY_POWER, 0 }, "quit" },
470 { { KEY_MENU, 0 }, "osd" },
471 { { KEY_PLAY, 0 }, "pause" },
472 { { KEY_PAUSE, 0 }, "pause" },
473 { { KEY_PLAYPAUSE, 0 }, "pause" },
474 { { KEY_STOP, 0 }, "quit" },
475 { { KEY_FORWARD, 0 }, "seek 60" },
476 { { KEY_REWIND, 0 }, "seek -60" },
477 { { KEY_NEXT, 0 }, "pt_step 1" },
478 { { KEY_PREV, 0 }, "pt_step -1" },
479 { { KEY_VOLUME_UP, 0 }, "volume 1" },
480 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
481 { { KEY_MUTE, 0 }, "mute" },
483 { { KEY_CLOSE_WIN, 0 }, "quit" },
485 { { '!', 0 }, "seek_chapter -1" },
486 { { '@', 0 }, "seek_chapter 1" },
487 { { 'A', 0 }, "switch_angle 1" },
488 { { 'U', 0 }, "stop" },
490 { { 0 }, NULL }
494 #ifdef CONFIG_GUI
495 static const mp_cmd_bind_t gui_def_cmd_binds[] = {
497 { { 'l', 0 }, "gui_loadfile" },
498 { { 't', 0 }, "gui_loadsubtitle" },
499 { { KEY_ENTER, 0 }, "gui_play" },
500 { { KEY_ESC, 0 }, "gui_stop" },
501 { { 'p', 0 }, "gui_playlist" },
502 { { 'r', 0 }, "gui_preferences" },
503 { { 'c', 0 }, "gui_skinbrowser" },
505 { { 0 }, NULL }
507 #endif
509 #ifndef MP_MAX_KEY_FD
510 #define MP_MAX_KEY_FD 10
511 #endif
513 #ifndef MP_MAX_CMD_FD
514 #define MP_MAX_CMD_FD 10
515 #endif
517 #define CMD_QUEUE_SIZE 100
519 typedef struct mp_input_fd {
520 int fd;
521 union {
522 mp_key_func_t key;
523 mp_cmd_func_t cmd;
524 } read_func;
525 mp_close_func_t close_func;
526 void *ctx;
527 unsigned eof : 1;
528 unsigned drop : 1;
529 unsigned dead : 1;
530 unsigned got_cmd : 1;
531 unsigned no_select : 1;
532 // These fields are for the cmd fds.
533 char* buffer;
534 int pos,size;
535 } mp_input_fd_t;
537 typedef struct mp_cmd_filter mp_cmd_filter_t;
539 struct mp_cmd_filter {
540 mp_input_cmd_filter filter;
541 void* ctx;
542 mp_cmd_filter_t* next;
545 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
547 struct mp_cmd_bind_section {
548 mp_cmd_bind_t* cmd_binds;
549 char* section;
550 mp_cmd_bind_section_t* next;
553 struct input_ctx {
554 // Autorepeat stuff
555 short ar_state;
556 mp_cmd_t *ar_cmd;
557 unsigned int last_ar;
558 // Autorepeat config
559 unsigned int ar_delay;
560 unsigned int ar_rate;
562 // these are the keys currently down
563 int key_down[MP_MAX_KEY_DOWN];
564 unsigned int num_key_down;
565 unsigned int last_key_down;
567 // List of command binding sections
568 mp_cmd_bind_section_t *cmd_bind_sections;
569 // Name of currently used command section
570 char *section;
571 // The command binds of current section
572 mp_cmd_bind_t *cmd_binds;
573 mp_cmd_bind_t *cmd_binds_default;
575 mp_input_fd_t key_fds[MP_MAX_KEY_FD];
576 unsigned int num_key_fd;
578 mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
579 unsigned int num_cmd_fd;
581 mp_cmd_t *cmd_queue[CMD_QUEUE_SIZE];
582 unsigned int cmd_queue_length, cmd_queue_start, cmd_queue_end;
586 static mp_cmd_filter_t* cmd_filters = NULL;
588 // Callback to allow the menu filter to grab the incoming keys
589 int (*mp_input_key_cb)(int code) = NULL;
591 int async_quit_request;
593 static int print_key_list(m_option_t* cfg);
594 static int print_cmd_list(m_option_t* cfg);
596 // Our command line options
597 static const m_option_t input_conf[] = {
598 OPT_STRING("conf", input.config_file, CONF_GLOBAL),
599 OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL),
600 OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL),
601 { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
602 { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
603 OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL),
604 OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL),
605 OPT_STRING("file", input.in_file, CONF_GLOBAL),
606 { NULL, NULL, 0, 0, 0, 0, NULL}
609 static const m_option_t mp_input_opts[] = {
610 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
611 OPT_FLAG_OFF("nojoystick", input.use_joystick, CONF_GLOBAL),
612 OPT_FLAG_ON("joystick", input.use_joystick, CONF_GLOBAL),
613 OPT_FLAG_OFF("nolirc", input.use_lirc, CONF_GLOBAL),
614 OPT_FLAG_ON("lirc", input.use_lirc, CONF_GLOBAL),
615 OPT_FLAG_OFF("nolircc", input.use_lircc, CONF_GLOBAL),
616 OPT_FLAG_ON("lircc", input.use_lircc, CONF_GLOBAL),
617 OPT_FLAG_OFF("noar", input.use_ar, CONF_GLOBAL),
618 OPT_FLAG_ON("ar", input.use_ar, CONF_GLOBAL),
619 { NULL, NULL, 0, 0, 0, 0, NULL}
622 static int default_cmd_func(int fd,char* buf, int l);
624 static char *get_key_name(int key, char buffer[12]);
627 int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select,
628 mp_cmd_func_t read_func, mp_close_func_t close_func)
630 if (ictx->num_cmd_fd == MP_MAX_CMD_FD) {
631 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
632 return 0;
634 if (select && fd < 0) {
635 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
636 return 0;
639 ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){
640 .fd = fd,
641 .read_func.cmd = read_func ? read_func : default_cmd_func,
642 .close_func = close_func,
643 .no_select = !select
645 ictx->num_cmd_fd++;
647 return 1;
650 void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd)
652 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
653 unsigned int i;
655 for (i = 0; i < ictx->num_cmd_fd; i++) {
656 if(cmd_fds[i].fd == fd)
657 break;
659 if (i == ictx->num_cmd_fd)
660 return;
661 if(cmd_fds[i].close_func)
662 cmd_fds[i].close_func(cmd_fds[i].fd);
663 if(cmd_fds[i].buffer)
664 talloc_free(cmd_fds[i].buffer);
666 if (i + 1 < ictx->num_cmd_fd)
667 memmove(&cmd_fds[i], &cmd_fds[i+1],
668 (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t));
669 ictx->num_cmd_fd--;
672 void mp_input_rm_key_fd(struct input_ctx *ictx, int fd)
674 struct mp_input_fd *key_fds = ictx->key_fds;
675 unsigned int i;
677 for (i = 0; i < ictx->num_key_fd; i++) {
678 if(key_fds[i].fd == fd)
679 break;
681 if (i == ictx->num_key_fd)
682 return;
683 if(key_fds[i].close_func)
684 key_fds[i].close_func(key_fds[i].fd);
686 if(i + 1 < ictx->num_key_fd)
687 memmove(&key_fds[i], &key_fds[i+1],
688 (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t));
689 ictx->num_key_fd--;
692 int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select,
693 mp_key_func_t read_func, mp_close_func_t close_func,
694 void *ctx)
696 if (ictx->num_key_fd == MP_MAX_KEY_FD) {
697 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
698 return 0;
700 if (select && fd < 0) {
701 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
702 return 0;
705 ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){
706 .fd = fd,
707 .read_func.key = read_func,
708 .close_func = close_func,
709 .no_select = !select,
710 .ctx = ctx,
712 ictx->num_key_fd++;
714 return 1;
717 int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str)
719 int cmd_num = 0;
721 while (*str == '\n' || *str == '\r' || *str == ' ')
722 ++str;
723 while (*str) {
724 mp_cmd_t *cmd;
725 size_t len = strcspn(str, "\r\n");
726 char *cmdbuf = talloc_size(NULL, len+1);
727 av_strlcpy(cmdbuf, str, len+1);
728 cmd = mp_input_parse_cmd(cmdbuf);
729 if (cmd) {
730 mp_input_queue_cmd(ictx, cmd);
731 ++cmd_num;
733 str += len;
734 while (*str == '\n' || *str == '\r' || *str == ' ')
735 ++str;
736 talloc_free(cmdbuf);
738 return cmd_num;
741 mp_cmd_t*
742 mp_input_parse_cmd(char* str) {
743 int i,l;
744 int pausing = 0;
745 char *ptr,*e;
746 const mp_cmd_t *cmd_def;
748 #ifdef MP_DEBUG
749 assert(str != NULL);
750 #endif
752 // Ignore heading spaces.
753 while (str[0] == ' ' || str[0] == '\t')
754 ++str;
756 if (strncmp(str, "pausing ", 8) == 0) {
757 pausing = 1;
758 str = &str[8];
759 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
760 pausing = 2;
761 str = &str[13];
762 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
763 pausing = 3;
764 str = &str[15];
765 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
766 pausing = 4;
767 str = &str[19];
770 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
771 /* NOTHING */;
772 if(ptr[0] != '\0')
773 l = ptr-str;
774 else
775 l = strlen(str);
777 if(l == 0)
778 return NULL;
780 for(i=0; mp_cmds[i].name != NULL; i++) {
781 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
782 break;
785 if(mp_cmds[i].name == NULL)
786 return NULL;
788 cmd_def = &mp_cmds[i];
790 mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd);
791 *cmd = (mp_cmd_t){
792 .id = cmd_def->id,
793 .name = talloc_strdup(cmd, cmd_def->name),
794 .pausing = pausing,
797 ptr = str;
799 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
800 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
801 if(ptr[0] == '\0') break;
802 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
803 if(ptr[0] == '\0' || ptr[0] == '#') break;
804 cmd->args[i].type = cmd_def->args[i].type;
805 switch(cmd_def->args[i].type) {
806 case MP_CMD_ARG_INT:
807 errno = 0;
808 cmd->args[i].v.i = atoi(ptr);
809 if(errno != 0) {
810 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
811 ptr = NULL;
813 break;
814 case MP_CMD_ARG_FLOAT:
815 errno = 0;
816 cmd->args[i].v.f = atof(ptr);
817 if(errno != 0) {
818 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
819 ptr = NULL;
821 break;
822 case MP_CMD_ARG_STRING: {
823 char term;
824 char* ptr2 = ptr, *start;
826 if(ptr[0] == '\'' || ptr[0] == '"') {
827 term = ptr[0];
828 ptr2++;
829 } else
830 term = ' ';
831 start = ptr2;
832 while(1) {
833 e = strchr(ptr2,term);
834 if(!e) break;
835 if(e <= ptr2 || *(e - 1) != '\\') break;
836 ptr2 = e + 1;
839 if(term != ' ' && (!e || e[0] == '\0')) {
840 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
841 ptr = NULL;
842 break;
843 } else if(!e) e = ptr+strlen(ptr);
844 l = e-start;
845 ptr2 = start;
846 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
847 memmove(e,e+1,strlen(e));
848 ptr2 = e + 1;
849 l--;
851 cmd->args[i].v.s = talloc_strndup(cmd, start, l);
852 if(term != ' ') ptr += l+2;
853 } break;
854 case -1:
855 ptr = NULL;
856 break;
857 default :
858 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
861 cmd->nargs = i;
863 if(cmd_def->nargs > cmd->nargs) {
864 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
865 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
866 mp_cmd_free(cmd);
867 return NULL;
870 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
871 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
872 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
873 cmd->args[i].v.s = talloc_strdup(cmd, cmd_def->args[i].v.s);
876 if(i < MP_CMD_MAX_ARGS)
877 cmd->args[i].type = -1;
879 return cmd;
882 #define MP_CMD_MAX_SIZE 256
884 static int read_cmd(mp_input_fd_t* mp_fd, char** ret)
886 char* end;
887 (*ret) = NULL;
889 // Allocate the buffer if it doesn't exist
890 if(!mp_fd->buffer) {
891 mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE);
892 mp_fd->pos = 0;
893 mp_fd->size = MP_CMD_MAX_SIZE;
896 // Get some data if needed/possible
897 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
898 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
899 mp_fd->size - 1 - mp_fd->pos);
900 // Error ?
901 if(r < 0) {
902 switch(r) {
903 case MP_INPUT_ERROR:
904 case MP_INPUT_DEAD:
905 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
906 case MP_INPUT_NOTHING:
907 return r;
908 case MP_INPUT_RETRY:
909 continue;
911 // EOF ?
912 } else if(r == 0) {
913 mp_fd->eof = 1;
914 break;
916 mp_fd->pos += r;
917 break;
920 mp_fd->got_cmd = 0;
922 while(1) {
923 int l = 0;
924 // Find the cmd end
925 mp_fd->buffer[mp_fd->pos] = '\0';
926 end = strchr(mp_fd->buffer,'\n');
927 // No cmd end ?
928 if(!end) {
929 // If buffer is full we must drop all until the next \n
930 if(mp_fd->size - mp_fd->pos <= 1) {
931 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
932 mp_fd->pos = 0;
933 mp_fd->drop = 1;
935 break;
937 // We already have a cmd : set the got_cmd flag
938 else if((*ret)) {
939 mp_fd->got_cmd = 1;
940 break;
943 l = end - mp_fd->buffer;
945 // Not dropping : put the cmd in ret
946 if (!mp_fd->drop)
947 *ret = talloc_strndup(NULL, mp_fd->buffer, l);
948 else
949 mp_fd->drop = 0;
950 mp_fd->pos -= l+1;
951 memmove(mp_fd->buffer, end+1, mp_fd->pos);
954 if(*ret)
955 return 1;
956 else
957 return MP_INPUT_NOTHING;
960 static int default_cmd_func(int fd,char* buf, int l)
962 while(1) {
963 int r = read(fd,buf,l);
964 // Error ?
965 if(r < 0) {
966 if(errno == EINTR)
967 continue;
968 else if(errno == EAGAIN)
969 return MP_INPUT_NOTHING;
970 return MP_INPUT_ERROR;
971 // EOF ?
973 return r;
979 void
980 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
981 mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter);
983 filter->filter = func;
984 filter->ctx = ctx;
985 filter->next = cmd_filters;
986 cmd_filters = filter;
990 static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys)
992 int j;
994 if (n <= 0) return NULL;
995 for(j = 0; binds[j].cmd != NULL; j++) {
996 int found = 1,s;
997 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
998 if(binds[j].input[s] != keys[s]) {
999 found = 0;
1000 break;
1003 if(found && binds[j].input[s] == 0 && s == n)
1004 break;
1006 return binds[j].cmd;
1009 static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx,
1010 char *section)
1012 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1014 if (section==NULL) section="default";
1015 while (bind_section) {
1016 if(strcmp(section,bind_section->section)==0) return bind_section;
1017 if(bind_section->next==NULL) break;
1018 bind_section=bind_section->next;
1020 if(bind_section) {
1021 bind_section->next = talloc_ptrtype(ictx, bind_section->next);
1022 bind_section=bind_section->next;
1023 } else {
1024 ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections);
1025 bind_section = ictx->cmd_bind_sections;
1027 bind_section->cmd_binds=NULL;
1028 bind_section->section = talloc_strdup(bind_section, section);
1029 bind_section->next=NULL;
1030 return bind_section;
1033 static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys,
1034 int paused)
1036 char* cmd = NULL;
1037 mp_cmd_t* ret;
1038 char key_buf[12];
1040 if (ictx->cmd_binds)
1041 cmd = find_bind_for_key(ictx->cmd_binds, n, keys);
1042 if (ictx->cmd_binds_default && cmd == NULL)
1043 cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys);
1044 if(cmd == NULL)
1045 cmd = find_bind_for_key(def_cmd_binds,n,keys);
1047 if(cmd == NULL) {
1048 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound, get_key_name(keys[0],
1049 key_buf));
1050 if(n > 1) {
1051 int s;
1052 for(s=1; s < n; s++)
1053 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", get_key_name(keys[s], key_buf));
1055 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1056 return NULL;
1058 if (strcmp(cmd, "ignore") == 0) return NULL;
1059 ret = mp_input_parse_cmd(cmd);
1060 if(!ret) {
1061 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,
1062 get_key_name(ictx->key_down[0], key_buf));
1063 if (ictx->num_key_down > 1) {
1064 unsigned int s;
1065 for(s=1; s < ictx->num_key_down; s++)
1066 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(ictx->key_down[s],
1067 key_buf));
1069 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1071 return ret;
1075 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1077 unsigned int j;
1078 mp_cmd_t* ret;
1080 if(mp_input_key_cb) {
1081 if (code & MP_KEY_DOWN)
1082 return NULL;
1083 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1084 if (mp_input_key_cb(code))
1085 return NULL;
1088 if(code & MP_KEY_DOWN) {
1089 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1090 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1091 return NULL;
1093 code &= ~MP_KEY_DOWN;
1094 // Check if we don't already have this key as pushed
1095 for (j = 0; j < ictx->num_key_down; j++) {
1096 if (ictx->key_down[j] == code)
1097 break;
1099 if (j != ictx->num_key_down)
1100 return NULL;
1101 ictx->key_down[ictx->num_key_down] = code;
1102 ictx->num_key_down++;
1103 ictx->last_key_down = GetTimer();
1104 ictx->ar_state = 0;
1105 return NULL;
1107 // key released
1108 // Check if the key is in the down key, driver which can't send push event
1109 // send only release event
1110 for (j = 0; j < ictx->num_key_down; j++) {
1111 if (ictx->key_down[j] == code)
1112 break;
1114 if (j == ictx->num_key_down) { // key was not in the down keys : add it
1115 if (ictx->num_key_down > MP_MAX_KEY_DOWN) {
1116 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1117 return NULL;
1119 ictx->key_down[ictx->num_key_down] = code;
1120 ictx->num_key_down++;
1121 ictx->last_key_down = 1;
1123 // We ignore key from last combination
1124 ret = ictx->last_key_down ?
1125 get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down, paused)
1126 : NULL;
1127 // Remove the key
1128 if (j+1 < ictx->num_key_down)
1129 memmove(&ictx->key_down[j], &ictx->key_down[j+1],
1130 (ictx->num_key_down-(j+1))*sizeof(int));
1131 ictx->num_key_down--;
1132 ictx->last_key_down = 0;
1133 ictx->ar_state = -1;
1134 if (ictx->ar_cmd) {
1135 mp_cmd_free(ictx->ar_cmd);
1136 ictx->ar_cmd = NULL;
1138 return ret;
1141 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1143 // No input : autorepeat ?
1144 if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0
1145 && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) {
1146 unsigned int t = GetTimer();
1147 // First time : wait delay
1148 if (ictx->ar_state == 0
1149 && (t - ictx->last_key_down) >= ictx->ar_delay*1000) {
1150 ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down,
1151 ictx->key_down, paused);
1152 if (!ictx->ar_cmd) {
1153 ictx->ar_state = -1;
1154 return NULL;
1156 ictx->ar_state = 1;
1157 ictx->last_ar = t;
1158 return mp_cmd_clone(ictx->ar_cmd);
1159 // Then send rate / sec event
1160 } else if (ictx->ar_state == 1
1161 && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) {
1162 ictx->last_ar = t;
1163 return mp_cmd_clone(ictx->ar_cmd);
1166 return NULL;
1170 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1172 int i;
1173 int got_cmd = 0;
1174 struct mp_input_fd *key_fds = ictx->key_fds;
1175 struct mp_input_fd *cmd_fds = ictx->cmd_fds;
1176 for (i = 0; i < ictx->num_key_fd; i++)
1177 if (key_fds[i].dead) {
1178 mp_input_rm_key_fd(ictx, key_fds[i].fd);
1179 i--;
1181 for (i = 0; i < ictx->num_cmd_fd; i++)
1182 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1183 mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd);
1184 i--;
1186 else if (cmd_fds[i].got_cmd)
1187 got_cmd = 1;
1188 #ifdef HAVE_POSIX_SELECT
1189 fd_set fds;
1190 FD_ZERO(&fds);
1191 if (!got_cmd) {
1192 int max_fd = 0, num_fd = 0;
1193 for (i = 0; i < ictx->num_key_fd; i++) {
1194 if (key_fds[i].no_select)
1195 continue;
1196 if (key_fds[i].fd > max_fd)
1197 max_fd = key_fds[i].fd;
1198 FD_SET(key_fds[i].fd, &fds);
1199 num_fd++;
1201 for (i = 0; i < ictx->num_cmd_fd; i++) {
1202 if (cmd_fds[i].no_select)
1203 continue;
1204 if (cmd_fds[i].fd > max_fd)
1205 max_fd = cmd_fds[i].fd;
1206 FD_SET(cmd_fds[i].fd, &fds);
1207 num_fd++;
1209 if (num_fd > 0) {
1210 struct timeval tv, *time_val;
1211 if (time >= 0) {
1212 tv.tv_sec = time / 1000;
1213 tv.tv_usec = (time % 1000) * 1000;
1214 time_val = &tv;
1216 else
1217 time_val = NULL;
1218 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1219 if (errno != EINTR)
1220 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrSelect,
1221 strerror(errno));
1222 FD_ZERO(&fds);
1226 #else
1227 if (!got_cmd && time)
1228 usec_sleep(time * 1000);
1229 #endif
1232 for (i = 0; i < ictx->num_key_fd; i++) {
1233 #ifdef HAVE_POSIX_SELECT
1234 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1235 continue;
1236 #endif
1238 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1239 if (code >= 0) {
1240 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1241 if (ret)
1242 return ret;
1244 else if (code == MP_INPUT_ERROR)
1245 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1246 key_fds[i].fd);
1247 else if (code == MP_INPUT_DEAD) {
1248 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1249 key_fds[i].fd);
1250 key_fds[i].dead = 1;
1253 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1254 if (autorepeat_cmd)
1255 return autorepeat_cmd;
1257 for (i = 0; i < ictx->num_cmd_fd; i++) {
1258 #ifdef HAVE_POSIX_SELECT
1259 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1260 !cmd_fds[i].got_cmd)
1261 continue;
1262 #endif
1263 char *cmd;
1264 int r = read_cmd(&cmd_fds[i], &cmd);
1265 if (r >= 0) {
1266 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1267 talloc_free(cmd);
1268 if (ret)
1269 return ret;
1271 else if (r == MP_INPUT_ERROR)
1272 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1273 cmd_fds[i].fd);
1274 else if (r == MP_INPUT_DEAD)
1275 cmd_fds[i].dead = 1;
1278 return NULL;
1282 int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd)
1284 if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE)
1285 return 0;
1286 ictx->cmd_queue[ictx->cmd_queue_end] = cmd;
1287 ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1288 ictx->cmd_queue_length++;
1289 return 1;
1292 static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only)
1294 mp_cmd_t* ret;
1296 if (ictx->cmd_queue_length == 0)
1297 return NULL;
1299 ret = ictx->cmd_queue[ictx->cmd_queue_start];
1301 if (!peek_only) {
1302 ictx->cmd_queue_length--;
1303 ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1306 return ret;
1310 * \param peek_only when set, the returned command stays in the queue.
1311 * Do not free the returned cmd whe you set this!
1313 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1314 int peek_only)
1316 mp_cmd_t* ret = NULL;
1317 mp_cmd_filter_t* cf;
1318 int from_queue;
1320 if (async_quit_request)
1321 return mp_input_parse_cmd("quit 1");
1322 while(1) {
1323 from_queue = 1;
1324 ret = get_queued_cmd(ictx, peek_only);
1325 if(ret) break;
1326 from_queue = 0;
1327 ret = read_events(ictx, time, paused);
1328 if (!ret) {
1329 from_queue = 1;
1330 ret = get_queued_cmd(ictx, peek_only);
1332 break;
1334 if(!ret) return NULL;
1336 for(cf = cmd_filters ; cf ; cf = cf->next) {
1337 if(cf->filter(ret,paused,cf->ctx)) {
1338 if (peek_only && from_queue)
1339 // The filter ate the cmd, so we remove it from queue
1340 ret = get_queued_cmd(ictx, 0);
1341 mp_cmd_free(ret);
1342 return NULL;
1346 if (!from_queue && peek_only)
1347 mp_input_queue_cmd(ictx, ret);
1349 return ret;
1352 void
1353 mp_cmd_free(mp_cmd_t* cmd) {
1354 talloc_free(cmd);
1357 mp_cmd_t*
1358 mp_cmd_clone(mp_cmd_t* cmd) {
1359 mp_cmd_t* ret;
1360 int i;
1361 #ifdef MP_DEBUG
1362 assert(cmd != NULL);
1363 #endif
1365 ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t));
1366 ret->name = talloc_strdup(ret, cmd->name);
1367 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1368 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1369 ret->args[i].v.s = talloc_strdup(ret, cmd->args[i].v.s);
1372 return ret;
1375 static char *get_key_name(int key, char buffer[12])
1377 int i;
1379 for(i = 0; key_names[i].name != NULL; i++) {
1380 if(key_names[i].key == key)
1381 return key_names[i].name;
1384 if(isascii(key)) {
1385 snprintf(buffer, 12, "%c",(char)key);
1386 return buffer;
1389 // Print the hex key code
1390 snprintf(buffer, 12, "%#-8x",key);
1391 return buffer;
1396 mp_input_get_key_from_name(const char *name) {
1397 int i,ret = 0,len = strlen(name);
1398 if(len == 1) { // Direct key code
1399 ret = (unsigned char)name[0];
1400 return ret;
1401 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1402 return strtol(name,NULL,16);
1404 for(i = 0; key_names[i].name != NULL; i++) {
1405 if(strcasecmp(key_names[i].name,name) == 0)
1406 return key_names[i].key;
1409 return -1;
1412 static int get_input_from_name(char* name,int* keys) {
1413 char *end,*ptr;
1414 int n=0;
1416 ptr = name;
1417 n = 0;
1418 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1419 if(end && end[1] != '\0') {
1420 if(end[1] == '-')
1421 end = &end[1];
1422 end[0] = '\0';
1424 keys[n] = mp_input_get_key_from_name(ptr);
1425 if(keys[n] < 0) {
1426 return 0;
1428 n++;
1429 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1430 ptr = &end[1];
1431 else
1432 break;
1434 keys[n] = 0;
1435 return 1;
1438 #define BS_MAX 256
1439 #define SPACE_CHAR " \n\r\t"
1441 static void bind_keys(struct input_ctx *ictx,
1442 const int keys[MP_MAX_KEY_DOWN+1], char *cmd)
1444 int i = 0,j;
1445 mp_cmd_bind_t* bind = NULL;
1446 mp_cmd_bind_section_t* bind_section = NULL;
1447 char *section=NULL, *p;
1449 #ifdef MP_DEBUG
1450 assert(keys != NULL);
1451 assert(cmd != NULL);
1452 #endif
1454 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1455 *p=0;
1456 section=++cmd;
1457 cmd=++p;
1458 // Jump beginning space
1459 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1460 /* NOTHING */;
1462 bind_section = get_bind_section(ictx, section);
1464 if(bind_section->cmd_binds) {
1465 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1466 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1467 /* NOTHING */;
1468 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1469 bind = &bind_section->cmd_binds[i];
1470 break;
1475 if(!bind) {
1476 bind_section->cmd_binds = talloc_realloc(bind_section,
1477 bind_section->cmd_binds,
1478 mp_cmd_bind_t, i + 2);
1479 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1480 bind = &bind_section->cmd_binds[i];
1482 if(bind->cmd)
1483 talloc_free(bind->cmd);
1484 bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd);
1485 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1488 static void add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1490 int i;
1491 for(i = 0 ; list[i].cmd ; i++)
1492 bind_keys(ictx, list[i].input,list[i].cmd);
1495 static int parse_config(struct input_ctx *ictx, char *file)
1497 int fd;
1498 int bs = 0,r,eof = 0,comments = 0;
1499 char *iter,*end;
1500 char buffer[BS_MAX];
1501 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1503 fd = open(file,O_RDONLY);
1505 if(fd < 0) {
1506 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1507 return 0;
1510 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1512 while(1) {
1513 if(! eof && bs < BS_MAX-1) {
1514 if(bs > 0) bs--;
1515 r = read(fd,buffer+bs,BS_MAX-1-bs);
1516 if(r < 0) {
1517 if(errno == EINTR)
1518 continue;
1519 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1520 close(fd);
1521 return 0;
1522 } else if(r == 0) {
1523 eof = 1;
1524 } else {
1525 bs += r+1;
1526 buffer[bs-1] = '\0';
1529 // Empty buffer : return
1530 if(bs <= 1) {
1531 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1532 close(fd);
1533 return 1;
1536 iter = buffer;
1538 if(comments) {
1539 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1540 /* NOTHING */;
1541 if(iter[0] == '\0') { // Buffer was full of comment
1542 bs = 0;
1543 continue;
1545 iter++;
1546 r = strlen(iter);
1547 memmove(buffer,iter,r+1);
1548 bs = r+1;
1549 comments = 0;
1550 continue;
1553 // Find the wanted key
1554 if(keys[0] == 0) {
1555 // Jump beginning space
1556 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1557 /* NOTHING */;
1558 if(iter[0] == '\0') { // Buffer was full of space char
1559 bs = 0;
1560 continue;
1562 if(iter[0] == '#') { // Comments
1563 comments = 1;
1564 continue;
1566 // Find the end of the key code name
1567 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1568 /*NOTHING */;
1569 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1570 if(buffer == iter) {
1571 if(eof && (buffer-iter) == bs)
1572 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1573 else
1574 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1575 return 0;
1577 memmove(buffer,iter,end-iter);
1578 bs = end-iter;
1579 continue;
1582 char name[end-iter+1];
1583 strncpy(name,iter,end-iter);
1584 name[end-iter] = '\0';
1585 if (!get_input_from_name(name,keys)) {
1586 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1587 close(fd);
1588 return 0;
1591 if( bs > (end-buffer))
1592 memmove(buffer,end,bs - (end-buffer));
1593 bs -= end-buffer;
1594 continue;
1595 } else { // Get the command
1596 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1597 // Found new line
1598 if(iter[0] == '\n' || iter[0] == '\r') {
1599 int i;
1600 char key_buf[12];
1601 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey, get_key_name(keys[0], key_buf));
1602 for(i = 1; keys[i] != 0 ; i++)
1603 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", get_key_name(keys[i], key_buf));
1604 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1605 keys[0] = 0;
1606 if(iter > buffer) {
1607 memmove(buffer,iter,bs- (iter-buffer));
1608 bs -= (iter-buffer);
1610 continue;
1612 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1613 /* NOTHING */;
1614 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1615 if(iter == buffer) {
1616 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1617 close(fd);
1618 return 0;
1620 memmove(buffer,iter,end - iter);
1621 bs = end - iter;
1622 continue;
1625 char cmd[end-iter+1];
1626 strncpy(cmd,iter,end-iter);
1627 cmd[end-iter] = '\0';
1628 //printf("Set bind %d => %s\n",keys[0],cmd);
1629 bind_keys(ictx, keys,cmd);
1630 n_binds++;
1632 keys[0] = 0;
1633 end++;
1634 if(bs > (end-buffer))
1635 memmove(buffer,end,bs-(end-buffer));
1636 bs -= (end-buffer);
1637 buffer[bs-1] = '\0';
1638 continue;
1641 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1642 close(fd);
1643 mp_input_set_section(ictx, NULL);
1644 return 0;
1647 void mp_input_set_section(struct input_ctx *ictx, char *name)
1649 mp_cmd_bind_section_t* bind_section = NULL;
1651 ictx->cmd_binds = NULL;
1652 ictx->cmd_binds_default = NULL;
1653 if (ictx->section)
1654 talloc_free(ictx->section);
1655 if (name)
1656 ictx->section = talloc_strdup(ictx, name);
1657 else
1658 ictx->section = talloc_strdup(ictx, "default");
1659 if ((bind_section = get_bind_section(ictx, ictx->section)))
1660 ictx->cmd_binds = bind_section->cmd_binds;
1661 if (strcmp(ictx->section, "default") == 0)
1662 return;
1663 if ((bind_section = get_bind_section(ictx, NULL)))
1664 ictx->cmd_binds_default = bind_section->cmd_binds;
1667 char *mp_input_get_section(struct input_ctx *ictx)
1669 return ictx->section;
1672 struct input_ctx *mp_input_init(struct input_conf *input_conf, int use_gui)
1674 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1675 *ictx = (struct input_ctx){
1676 .ar_state = -1,
1677 .ar_delay = input_conf->ar_delay,
1678 .ar_rate = input_conf->ar_rate,
1681 char* file;
1683 #ifdef CONFIG_GUI
1684 if(use_gui)
1685 add_binds(ictx, gui_def_cmd_binds);
1686 #endif
1688 char *config_file = input_conf->config_file;
1689 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1690 if(!file)
1691 return ictx;
1693 if (!parse_config(ictx, file)) {
1694 // free file if it was allocated by get_path(),
1695 // before it gets overwritten
1696 if( file != config_file)
1698 free(file);
1700 // Try global conf dir
1701 file = MPLAYER_CONFDIR "/input.conf";
1702 if (!parse_config(ictx, file))
1703 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1705 else
1707 // free file if it was allocated by get_path()
1708 if( file != config_file)
1709 free(file);
1712 #ifdef CONFIG_JOYSTICK
1713 if (input_conf->use_joystick) {
1714 int fd = mp_input_joystick_init(input_conf->js_dev);
1715 if(fd < 0)
1716 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1717 else
1718 mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read,
1719 (mp_close_func_t)close,NULL);
1721 #endif
1723 #ifdef CONFIG_LIRC
1724 if (input_conf->use_lirc) {
1725 int fd = mp_input_lirc_init();
1726 if(fd > 0)
1727 mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read,
1728 mp_input_lirc_close);
1730 #endif
1732 #ifdef CONFIG_LIRCC
1733 if (input_conf->use_lircc) {
1734 int fd = lircc_init("mplayer", NULL);
1735 if(fd >= 0)
1736 mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup);
1738 #endif
1740 #ifdef CONFIG_APPLE_REMOTE
1741 if (input_conf->use_ar) {
1742 if(mp_input_ar_init() < 0)
1743 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1744 else
1745 mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close,
1746 NULL);
1748 #endif
1750 #ifdef CONFIG_APPLE_IR
1751 if (input_conf->use_ar) {
1752 int fd = mp_input_appleir_init(input_conf->ar_dev);
1753 if(fd < 0)
1754 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1755 else
1756 mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read,
1757 (mp_close_func_t)close, NULL);
1759 #endif
1761 if (input_conf->in_file) {
1762 struct stat st;
1763 if (stat(input_conf->in_file, &st))
1764 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrCantStatFile, input_conf->in_file, strerror(errno));
1765 else {
1766 int in_file_fd = open(input_conf->in_file,
1767 S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1768 if(in_file_fd >= 0)
1769 mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL,
1770 (mp_close_func_t)close);
1771 else
1772 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrCantOpenFile,
1773 input_conf->in_file, strerror(errno));
1776 return ictx;
1779 void mp_input_uninit(struct input_ctx *ictx)
1781 if (!ictx)
1782 return;
1784 unsigned int i;
1786 for (i=0; i < ictx->num_key_fd; i++) {
1787 if (ictx->key_fds[i].close_func)
1788 ictx->key_fds[i].close_func(ictx->key_fds[i].fd);
1791 for (i = 0; i < ictx->num_cmd_fd; i++) {
1792 if (ictx->cmd_fds[i].close_func)
1793 ictx->cmd_fds[i].close_func(ictx->cmd_fds[i].fd);
1795 talloc_free(ictx);
1798 void
1799 mp_input_register_options(m_config_t* cfg) {
1800 m_config_register_options(cfg,mp_input_opts);
1803 static int print_key_list(m_option_t* cfg)
1805 int i;
1806 printf("\n");
1807 for(i= 0; key_names[i].name != NULL ; i++)
1808 printf("%s\n",key_names[i].name);
1809 exit(0);
1812 static int print_cmd_list(m_option_t* cfg)
1814 const mp_cmd_t *cmd;
1815 int i,j;
1816 const char* type;
1818 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1819 printf("%-20.20s",cmd->name);
1820 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1821 switch(cmd->args[j].type) {
1822 case MP_CMD_ARG_INT:
1823 type = "Integer";
1824 break;
1825 case MP_CMD_ARG_FLOAT:
1826 type = "Float";
1827 break;
1828 case MP_CMD_ARG_STRING:
1829 type = "String";
1830 break;
1831 default:
1832 type = "??";
1834 if(j+1 > cmd->nargs)
1835 printf(" [%s]",type);
1836 else
1837 printf(" %s",type);
1839 printf("\n");
1841 exit(0);
1844 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1846 mp_cmd_t* cmd;
1847 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1848 return 0;
1849 switch(cmd->id) {
1850 case MP_CMD_QUIT:
1851 case MP_CMD_PLAY_TREE_STEP:
1852 case MP_CMD_PLAY_TREE_UP_STEP:
1853 case MP_CMD_PLAY_ALT_SRC_STEP:
1854 // The cmd will be executed when we are back in the main loop
1855 return 1;
1857 // remove the cmd from the queue
1858 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1859 mp_cmd_free(cmd);
1860 return 0;