Remove INITIALIZED_INPUT from mpctx->initialized_flags
[mplayer.git] / input / input.c
blob3d898aa0af1c657eb8caef4ce3e00c60020142e1
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"
30 #include "joystick.h"
32 #ifdef HAVE_LIRC
33 #include "lirc.h"
34 #endif
36 #ifdef HAVE_LIRCC
37 #include <lirc/lircc.h>
38 #endif
40 #include "ar.h"
42 /// This array defines all known commands.
43 /// The first field is an id used to recognize the command without too many strcmp.
44 /// The second is obviously the command name.
45 /// The third is the minimum number of arguments this command needs.
46 /// Then comes the definition of each argument, terminated with an arg of type -1.
47 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
48 /// the last one) which is actually 9.
50 /// For the args, the first field is the type (actually int, float or string), the second
51 /// is the default value wich is used for optional arguments
53 static const mp_cmd_t mp_cmds[] = {
54 #ifdef USE_RADIO
55 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
56 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
57 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
58 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
59 #endif
60 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
61 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
62 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
63 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
64 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
65 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
66 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
67 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
68 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
69 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
70 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
71 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
72 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
73 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
74 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
75 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
76 { 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}} } },
77 { 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}} } },
78 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
79 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
80 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
81 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
82 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
83 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
84 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
85 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
86 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
87 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
88 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
89 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
90 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
91 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
92 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
93 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
94 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
95 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
96 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
97 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
98 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
99 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
100 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
101 #ifdef USE_ASS
102 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
103 #endif
104 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
105 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
106 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
107 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
108 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
109 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
110 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
111 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
112 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
113 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
114 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
115 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
116 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
117 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
118 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
119 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
120 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
121 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
122 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
123 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
124 #ifdef USE_TV
125 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
126 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
127 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
128 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
129 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
130 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
131 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
132 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
133 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
134 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
135 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
136 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
137 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
138 #endif
139 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
140 #ifdef HAS_DVBIN_SUPPORT
141 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
142 #endif
143 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
144 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
145 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
146 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
147 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
148 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
149 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
150 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
151 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
152 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
153 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
154 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
155 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
156 #ifdef HAVE_TV_TELETEXT
157 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
158 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
159 #endif
161 #ifdef HAVE_NEW_GUI
162 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
163 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
164 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
165 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
166 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
167 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
168 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
169 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
170 #endif
172 #ifdef USE_DVDNAV
173 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
174 #endif
176 #ifdef HAVE_MENU
177 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
178 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
179 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
180 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
181 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
182 #endif
184 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
185 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
186 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
187 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
188 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
189 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
191 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
192 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
194 { 0, NULL, 0, {} }
197 /// The names of the keys as used in input.conf
198 /// If you add some new keys, you also need to add them here
200 static const mp_key_name_t key_names[] = {
201 { ' ', "SPACE" },
202 { '#', "SHARP" },
203 { KEY_ENTER, "ENTER" },
204 { KEY_TAB, "TAB" },
205 { KEY_CTRL, "CTRL" },
206 { KEY_BACKSPACE, "BS" },
207 { KEY_DELETE, "DEL" },
208 { KEY_INSERT, "INS" },
209 { KEY_HOME, "HOME" },
210 { KEY_END, "END" },
211 { KEY_PAGE_UP, "PGUP" },
212 { KEY_PAGE_DOWN, "PGDWN" },
213 { KEY_ESC, "ESC" },
214 { KEY_RIGHT, "RIGHT" },
215 { KEY_LEFT, "LEFT" },
216 { KEY_DOWN, "DOWN" },
217 { KEY_UP, "UP" },
218 { KEY_F+1, "F1" },
219 { KEY_F+2, "F2" },
220 { KEY_F+3, "F3" },
221 { KEY_F+4, "F4" },
222 { KEY_F+5, "F5" },
223 { KEY_F+6, "F6" },
224 { KEY_F+7, "F7" },
225 { KEY_F+8, "F8" },
226 { KEY_F+9, "F9" },
227 { KEY_F+10, "F10" },
228 { KEY_F+11, "F11" },
229 { KEY_F+12, "F12" },
230 { KEY_KP0, "KP0" },
231 { KEY_KP1, "KP1" },
232 { KEY_KP2, "KP2" },
233 { KEY_KP3, "KP3" },
234 { KEY_KP4, "KP4" },
235 { KEY_KP5, "KP5" },
236 { KEY_KP6, "KP6" },
237 { KEY_KP7, "KP7" },
238 { KEY_KP8, "KP8" },
239 { KEY_KP9, "KP9" },
240 { KEY_KPDEL, "KP_DEL" },
241 { KEY_KPDEC, "KP_DEC" },
242 { KEY_KPINS, "KP_INS" },
243 { KEY_KPENTER, "KP_ENTER" },
244 { MOUSE_BTN0, "MOUSE_BTN0" },
245 { MOUSE_BTN1, "MOUSE_BTN1" },
246 { MOUSE_BTN2, "MOUSE_BTN2" },
247 { MOUSE_BTN3, "MOUSE_BTN3" },
248 { MOUSE_BTN4, "MOUSE_BTN4" },
249 { MOUSE_BTN5, "MOUSE_BTN5" },
250 { MOUSE_BTN6, "MOUSE_BTN6" },
251 { MOUSE_BTN7, "MOUSE_BTN7" },
252 { MOUSE_BTN8, "MOUSE_BTN8" },
253 { MOUSE_BTN9, "MOUSE_BTN9" },
254 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
255 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
256 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
257 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
258 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
259 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
260 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
261 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
262 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
263 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
264 { JOY_AXIS1_MINUS, "JOY_UP" },
265 { JOY_AXIS1_PLUS, "JOY_DOWN" },
266 { JOY_AXIS0_MINUS, "JOY_LEFT" },
267 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
269 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
270 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
271 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
272 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
273 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
274 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
275 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
276 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
277 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
278 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
279 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
280 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
281 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
282 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
283 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
284 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
285 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
286 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
287 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
288 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
290 { JOY_BTN0, "JOY_BTN0" },
291 { JOY_BTN1, "JOY_BTN1" },
292 { JOY_BTN2, "JOY_BTN2" },
293 { JOY_BTN3, "JOY_BTN3" },
294 { JOY_BTN4, "JOY_BTN4" },
295 { JOY_BTN5, "JOY_BTN5" },
296 { JOY_BTN6, "JOY_BTN6" },
297 { JOY_BTN7, "JOY_BTN7" },
298 { JOY_BTN8, "JOY_BTN8" },
299 { JOY_BTN9, "JOY_BTN9" },
301 { AR_PLAY, "AR_PLAY" },
302 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
303 { AR_NEXT, "AR_NEXT" },
304 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
305 { AR_PREV, "AR_PREV" },
306 { AR_PREV_HOLD, "AR_PREV_HOLD" },
307 { AR_MENU, "AR_MENU" },
308 { AR_MENU_HOLD, "AR_MENU_HOLD" },
309 { AR_VUP, "AR_VUP" },
310 { AR_VDOWN, "AR_VDOWN" },
312 { KEY_POWER, "POWER" },
313 { KEY_MENU, "MENU" },
314 { KEY_PLAY, "PLAY" },
315 { KEY_PAUSE, "PAUSE" },
316 { KEY_PLAYPAUSE, "PLAYPAUSE" },
317 { KEY_STOP, "STOP" },
318 { KEY_FORWARD, "FORWARD" },
319 { KEY_REWIND, "REWIND" },
320 { KEY_NEXT, "NEXT" },
321 { KEY_PREV, "PREV" },
322 { KEY_VOLUME_UP, "VOLUME_UP" },
323 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
324 { KEY_MUTE, "MUTE" },
326 // These are kept for backward compatibility
327 { KEY_PAUSE, "XF86_PAUSE" },
328 { KEY_STOP, "XF86_STOP" },
329 { KEY_PREV, "XF86_PREV" },
330 { KEY_NEXT, "XF86_NEXT" },
332 { KEY_CLOSE_WIN, "CLOSE_WIN" },
334 { 0, NULL }
337 // This is the default binding. The content of input.conf overrides these.
338 // The first arg is a null terminated array of key codes.
339 // The second is the command
341 static const mp_cmd_bind_t def_cmd_binds[] = {
343 { { MOUSE_BTN3, 0 }, "seek 10" },
344 { { MOUSE_BTN4, 0 }, "seek -10" },
345 { { MOUSE_BTN5, 0 }, "volume 1" },
346 { { MOUSE_BTN6, 0 }, "volume -1" },
348 #ifdef USE_DVDNAV
349 { { KEY_KP8, 0 }, "dvdnav 1" }, // up
350 { { KEY_KP2, 0 }, "dvdnav 2" }, // down
351 { { KEY_KP4, 0 }, "dvdnav 3" }, // left
352 { { KEY_KP6, 0 }, "dvdnav 4" }, // right
353 { { KEY_KP5, 0 }, "dvdnav 5" }, // menu
354 { { KEY_KPENTER, 0 }, "dvdnav 6" }, // select
355 { { MOUSE_BTN0, 0 }, "dvdnav 8" }, //select
356 { { KEY_KP7, 0 }, "dvdnav 7" }, // previous menu
357 #endif
359 { { KEY_RIGHT, 0 }, "seek 10" },
360 { { KEY_LEFT, 0 }, "seek -10" },
361 { { KEY_UP, 0 }, "seek 60" },
362 { { KEY_DOWN, 0 }, "seek -60" },
363 { { KEY_PAGE_UP, 0 }, "seek 600" },
364 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
365 { { '+', 0 }, "audio_delay 0.100" },
366 { { '-', 0 }, "audio_delay -0.100" },
367 { { '[', 0 }, "speed_mult 0.9091" },
368 { { ']', 0 }, "speed_mult 1.1" },
369 { { '{', 0 }, "speed_mult 0.5" },
370 { { '}', 0 }, "speed_mult 2.0" },
371 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
372 { { 'q', 0 }, "quit" },
373 { { KEY_ESC, 0 }, "quit" },
374 { { 'p', 0 }, "pause" },
375 { { ' ', 0 }, "pause" },
376 { { '.', 0 }, "frame_step" },
377 { { KEY_HOME, 0 }, "pt_up_step 1" },
378 { { KEY_END, 0 }, "pt_up_step -1" },
379 { { '>', 0 }, "pt_step 1" },
380 { { KEY_ENTER, 0 }, "pt_step 1 1" },
381 { { '<', 0 }, "pt_step -1" },
382 { { KEY_INS, 0 }, "alt_src_step 1" },
383 { { KEY_DEL, 0 }, "alt_src_step -1" },
384 { { 'o', 0 }, "osd" },
385 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
386 { { 'z', 0 }, "sub_delay -0.1" },
387 { { 'x', 0 }, "sub_delay +0.1" },
388 { { 'g', 0 }, "sub_step -1" },
389 { { 'y', 0 }, "sub_step +1" },
390 { { '9', 0 }, "volume -1" },
391 { { '/', 0 }, "volume -1" },
392 { { '0', 0 }, "volume 1" },
393 { { '*', 0 }, "volume 1" },
394 { { '(', 0 }, "balance -0.1" },
395 { { ')', 0 }, "balance 0.1" },
396 { { 'm', 0 }, "mute" },
397 { { '1', 0 }, "contrast -1" },
398 { { '2', 0 }, "contrast 1" },
399 { { '3', 0 }, "brightness -1" },
400 { { '4', 0 }, "brightness 1" },
401 { { '5', 0 }, "hue -1" },
402 { { '6', 0 }, "hue 1" },
403 { { '7', 0 }, "saturation -1" },
404 { { '8', 0 }, "saturation 1" },
405 { { 'd', 0 }, "frame_drop" },
406 { { 'D', 0 }, "step_property deinterlace" },
407 { { 'r', 0 }, "sub_pos -1" },
408 { { 't', 0 }, "sub_pos +1" },
409 { { 'a', 0 }, "sub_alignment" },
410 { { 'v', 0 }, "sub_visibility" },
411 { { 'j', 0 }, "sub_select" },
412 { { 'F', 0 }, "forced_subs_only" },
413 { { '#', 0 }, "switch_audio" },
414 { { '_', 0 }, "step_property switch_video" },
415 { { KEY_TAB, 0 }, "step_property switch_program" },
416 { { 'i', 0 }, "edl_mark" },
417 #ifdef USE_TV
418 { { 'h', 0 }, "tv_step_channel 1" },
419 { { 'k', 0 }, "tv_step_channel -1" },
420 { { 'n', 0 }, "tv_step_norm" },
421 { { 'u', 0 }, "tv_step_chanlist" },
422 #endif
423 #ifdef HAVE_TV_TELETEXT
424 { { 'X', 0 }, "step_property teletext_mode 1" },
425 { { 'W', 0 }, "step_property teletext_page 1" },
426 { { 'Q', 0 }, "step_property teletext_page -1" },
427 #endif
428 #ifdef HAVE_JOYSTICK
429 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
430 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
431 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
432 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
433 { { JOY_BTN0, 0 }, "pause" },
434 { { JOY_BTN1, 0 }, "osd" },
435 { { JOY_BTN2, 0 }, "volume 1"},
436 { { JOY_BTN3, 0 }, "volume -1"},
437 #endif
438 #ifdef HAVE_APPLE_REMOTE
439 { { AR_PLAY, 0}, "pause" },
440 { { AR_PLAY_HOLD, 0}, "quit" },
441 { { AR_NEXT, 0 }, "seek 30" },
442 { { AR_NEXT_HOLD, 0 }, "seek 120" },
443 { { AR_PREV, 0 }, "seek -10" },
444 { { AR_PREV_HOLD, 0 }, "seek -120" },
445 { { AR_MENU, 0 }, "osd" },
446 { { AR_MENU_HOLD, 0 }, "mute" },
447 { { AR_VUP, 0 }, "volume 1"},
448 { { AR_VDOWN, 0 }, "volume -1"},
449 #endif
450 { { 'T', 0 }, "vo_ontop" },
451 { { 'f', 0 }, "vo_fullscreen" },
452 { { 's', 0 }, "screenshot 0" },
453 { { 'S', 0 }, "screenshot 1" },
454 { { 'w', 0 }, "panscan -0.1" },
455 { { 'e', 0 }, "panscan +0.1" },
457 { { KEY_POWER, 0 }, "quit" },
458 { { KEY_MENU, 0 }, "osd" },
459 { { KEY_PLAY, 0 }, "pause" },
460 { { KEY_PAUSE, 0 }, "pause" },
461 { { KEY_PLAYPAUSE, 0 }, "pause" },
462 { { KEY_STOP, 0 }, "quit" },
463 { { KEY_FORWARD, 0 }, "seek 60" },
464 { { KEY_REWIND, 0 }, "seek -60" },
465 { { KEY_NEXT, 0 }, "pt_step 1" },
466 { { KEY_PREV, 0 }, "pt_step -1" },
467 { { KEY_VOLUME_UP, 0 }, "volume 1" },
468 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
469 { { KEY_MUTE, 0 }, "mute" },
471 { { KEY_CLOSE_WIN, 0 }, "quit" },
473 { { '!', 0 }, "seek_chapter -1" },
474 { { '@', 0 }, "seek_chapter 1" },
475 { { 'A', 0 }, "switch_angle 1" },
477 { { 0 }, NULL }
481 #ifdef HAVE_NEW_GUI
482 static const mp_cmd_bind_t gui_def_cmd_binds[] = {
484 { { 'l', 0 }, "gui_loadfile" },
485 { { 't', 0 }, "gui_loadsubtitle" },
486 { { KEY_ENTER, 0 }, "gui_play" },
487 { { KEY_ESC, 0 }, "gui_stop" },
488 { { 'p', 0 }, "gui_playlist" },
489 { { 'r', 0 }, "gui_preferences" },
490 { { 'c', 0 }, "gui_skinbrowser" },
492 { { 0 }, NULL }
494 #endif
496 #ifndef MP_MAX_KEY_FD
497 #define MP_MAX_KEY_FD 10
498 #endif
500 #ifndef MP_MAX_CMD_FD
501 #define MP_MAX_CMD_FD 10
502 #endif
504 #define CMD_QUEUE_SIZE 100
506 typedef struct mp_input_fd {
507 int fd;
508 union {
509 mp_key_func_t key;
510 mp_cmd_func_t cmd;
511 } read_func;
512 mp_close_func_t close_func;
513 void *ctx;
514 unsigned eof : 1;
515 unsigned drop : 1;
516 unsigned dead : 1;
517 unsigned got_cmd : 1;
518 unsigned no_select : 1;
519 // These fields are for the cmd fds.
520 char* buffer;
521 int pos,size;
522 } mp_input_fd_t;
524 typedef struct mp_cmd_filter mp_cmd_filter_t;
526 struct mp_cmd_filter {
527 mp_input_cmd_filter filter;
528 void* ctx;
529 mp_cmd_filter_t* next;
532 typedef struct mp_cmd_bind_section mp_cmd_bind_section_t;
534 struct mp_cmd_bind_section {
535 mp_cmd_bind_t* cmd_binds;
536 char* section;
537 mp_cmd_bind_section_t* next;
540 struct input_ctx {
541 // Autorepeat stuff
542 short ar_state;
543 mp_cmd_t *ar_cmd;
544 unsigned int last_ar;
546 // List of command binding sections
547 mp_cmd_bind_section_t *cmd_bind_sections;
548 // Name of currently used command section
549 char *section;
550 // The command binds of current section
551 mp_cmd_bind_t *cmd_binds;
552 mp_cmd_bind_t *cmd_binds_default;
556 static mp_cmd_filter_t* cmd_filters = NULL;
558 // Callback to allow the menu filter to grab the incoming keys
559 int (*mp_input_key_cb)(int code) = NULL;
561 int async_quit_request;
563 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
564 static unsigned int num_key_fd = 0;
565 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
566 static unsigned int num_cmd_fd = 0;
567 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
568 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
570 // this is the key currently down
571 static int key_down[MP_MAX_KEY_DOWN];
572 static unsigned int num_key_down = 0, last_key_down = 0;
574 static unsigned int ar_delay = 100, ar_rate = 8;
576 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
577 static char* config_file = "input.conf";
579 /* Apple Remote */
580 static int use_ar = 1;
582 static char* js_dev = NULL;
584 static char* in_file = NULL;
585 static int in_file_fd = -1;
587 static int mp_input_print_key_list(m_option_t* cfg);
588 static int mp_input_print_cmd_list(m_option_t* cfg);
590 // Our command line options
591 static const m_option_t input_conf[] = {
592 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
593 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
594 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
595 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
596 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
597 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
598 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
599 { NULL, NULL, 0, 0, 0, 0, NULL}
602 static const m_option_t mp_input_opts[] = {
603 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
604 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
605 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
606 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
607 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
608 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
609 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
610 { "noar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
611 { "ar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
612 { NULL, NULL, 0, 0, 0, 0, NULL}
615 static int
616 mp_input_default_cmd_func(int fd,char* buf, int l);
618 static char*
619 mp_input_get_key_name(int key);
623 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
624 if(num_cmd_fd == MP_MAX_CMD_FD) {
625 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
626 return 0;
628 if (select && fd < 0) {
629 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
630 return 0;
633 cmd_fds[num_cmd_fd] = (struct mp_input_fd){
634 .fd = fd,
635 .read_func.cmd = read_func ? read_func : mp_input_default_cmd_func,
636 .close_func = close_func,
637 .no_select = !select
639 num_cmd_fd++;
641 return 1;
644 void
645 mp_input_rm_cmd_fd(int fd) {
646 unsigned int i;
648 for(i = 0; i < num_cmd_fd; i++) {
649 if(cmd_fds[i].fd == fd)
650 break;
652 if(i == num_cmd_fd)
653 return;
654 if(cmd_fds[i].close_func)
655 cmd_fds[i].close_func(cmd_fds[i].fd);
656 if(cmd_fds[i].buffer)
657 free(cmd_fds[i].buffer);
659 if(i + 1 < num_cmd_fd)
660 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
661 num_cmd_fd--;
664 void
665 mp_input_rm_key_fd(int fd) {
666 unsigned int i;
668 for(i = 0; i < num_key_fd; i++) {
669 if(key_fds[i].fd == fd)
670 break;
672 if(i == num_key_fd)
673 return;
674 if(key_fds[i].close_func)
675 key_fds[i].close_func(key_fds[i].fd);
677 if(i + 1 < num_key_fd)
678 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
679 num_key_fd--;
683 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func,
684 mp_close_func_t close_func, void *ctx)
686 if(num_key_fd == MP_MAX_KEY_FD) {
687 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
688 return 0;
690 if (select && fd < 0) {
691 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
692 return 0;
695 key_fds[num_key_fd] = (struct mp_input_fd){
696 .fd = fd,
697 .read_func.key = read_func,
698 .close_func = close_func,
699 .no_select = !select,
700 .ctx = ctx,
702 num_key_fd++;
704 return 1;
707 int mp_input_parse_and_queue_cmds(const char *str) {
708 int cmd_num = 0;
710 while (*str == '\n' || *str == '\r' || *str == ' ')
711 ++str;
712 while (*str) {
713 mp_cmd_t *cmd;
714 size_t len = strcspn(str, "\r\n");
715 char *cmdbuf = malloc(len+1);
716 av_strlcpy(cmdbuf, str, len+1);
717 cmd = mp_input_parse_cmd(cmdbuf);
718 if (cmd) {
719 mp_input_queue_cmd(cmd);
720 ++cmd_num;
722 str += len;
723 while (*str == '\n' || *str == '\r' || *str == ' ')
724 ++str;
725 free(cmdbuf);
727 return cmd_num;
730 mp_cmd_t*
731 mp_input_parse_cmd(char* str) {
732 int i,l;
733 int pausing = 0;
734 char *ptr,*e;
735 mp_cmd_t *cmd;
736 const mp_cmd_t *cmd_def;
738 #ifdef MP_DEBUG
739 assert(str != NULL);
740 #endif
742 // Ignore heading spaces.
743 while (str[0] == ' ' || str[0] == '\t')
744 ++str;
746 if (strncmp(str, "pausing ", 8) == 0) {
747 pausing = 1;
748 str = &str[8];
749 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
750 pausing = 2;
751 str = &str[13];
752 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
753 pausing = 3;
754 str = &str[15];
757 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
758 /* NOTHING */;
759 if(ptr[0] != '\0')
760 l = ptr-str;
761 else
762 l = strlen(str);
764 if(l == 0)
765 return NULL;
767 for(i=0; mp_cmds[i].name != NULL; i++) {
768 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
769 break;
772 if(mp_cmds[i].name == NULL)
773 return NULL;
775 cmd_def = &mp_cmds[i];
777 cmd = calloc(1, sizeof(mp_cmd_t));
778 cmd->id = cmd_def->id;
779 cmd->name = strdup(cmd_def->name);
780 cmd->pausing = pausing;
782 ptr = str;
784 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
785 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
786 if(ptr[0] == '\0') break;
787 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
788 if(ptr[0] == '\0' || ptr[0] == '#') break;
789 cmd->args[i].type = cmd_def->args[i].type;
790 switch(cmd_def->args[i].type) {
791 case MP_CMD_ARG_INT:
792 errno = 0;
793 cmd->args[i].v.i = atoi(ptr);
794 if(errno != 0) {
795 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
796 ptr = NULL;
798 break;
799 case MP_CMD_ARG_FLOAT:
800 errno = 0;
801 cmd->args[i].v.f = atof(ptr);
802 if(errno != 0) {
803 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
804 ptr = NULL;
806 break;
807 case MP_CMD_ARG_STRING: {
808 char term;
809 char* ptr2 = ptr, *start;
811 if(ptr[0] == '\'' || ptr[0] == '"') {
812 term = ptr[0];
813 ptr2++;
814 } else
815 term = ' ';
816 start = ptr2;
817 while(1) {
818 e = strchr(ptr2,term);
819 if(!e) break;
820 if(e <= ptr2 || *(e - 1) != '\\') break;
821 ptr2 = e + 1;
824 if(term != ' ' && (!e || e[0] == '\0')) {
825 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
826 ptr = NULL;
827 break;
828 } else if(!e) e = ptr+strlen(ptr);
829 l = e-start;
830 ptr2 = start;
831 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
832 memmove(e,e+1,strlen(e));
833 ptr2 = e + 1;
834 l--;
836 cmd->args[i].v.s = malloc(l+1);
837 strncpy(cmd->args[i].v.s,start,l);
838 cmd->args[i].v.s[l] = '\0';
839 if(term != ' ') ptr += l+2;
840 } break;
841 case -1:
842 ptr = NULL;
843 break;
844 default :
845 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
848 cmd->nargs = i;
850 if(cmd_def->nargs > cmd->nargs) {
851 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
852 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
853 mp_cmd_free(cmd);
854 return NULL;
857 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
858 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
859 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
860 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
863 if(i < MP_CMD_MAX_ARGS)
864 cmd->args[i].type = -1;
866 return cmd;
869 #define MP_CMD_MAX_SIZE 256
871 static int
872 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
873 char* end;
874 (*ret) = NULL;
876 // Allocate the buffer if it doesn't exist
877 if(!mp_fd->buffer) {
878 mp_fd->buffer = malloc(MP_CMD_MAX_SIZE);
879 mp_fd->pos = 0;
880 mp_fd->size = MP_CMD_MAX_SIZE;
883 // Get some data if needed/possible
884 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
885 int r = mp_fd->read_func.cmd(mp_fd->fd, mp_fd->buffer+mp_fd->pos,
886 mp_fd->size - 1 - mp_fd->pos);
887 // Error ?
888 if(r < 0) {
889 switch(r) {
890 case MP_INPUT_ERROR:
891 case MP_INPUT_DEAD:
892 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
893 case MP_INPUT_NOTHING:
894 return r;
895 case MP_INPUT_RETRY:
896 continue;
898 // EOF ?
899 } else if(r == 0) {
900 mp_fd->eof = 1;
901 break;
903 mp_fd->pos += r;
904 break;
907 mp_fd->got_cmd = 0;
909 while(1) {
910 int l = 0;
911 // Find the cmd end
912 mp_fd->buffer[mp_fd->pos] = '\0';
913 end = strchr(mp_fd->buffer,'\n');
914 // No cmd end ?
915 if(!end) {
916 // If buffer is full we must drop all until the next \n
917 if(mp_fd->size - mp_fd->pos <= 1) {
918 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
919 mp_fd->pos = 0;
920 mp_fd->drop = 1;
922 break;
924 // We already have a cmd : set the got_cmd flag
925 else if((*ret)) {
926 mp_fd->got_cmd = 1;
927 break;
930 l = end - mp_fd->buffer;
932 // Not dropping : put the cmd in ret
933 if (!mp_fd->drop) {
934 (*ret) = malloc(l+1);
935 strncpy((*ret),mp_fd->buffer,l);
936 (*ret)[l] = '\0';
937 } else { // Remove the dropping flag
938 mp_fd->drop = 0;
940 if( mp_fd->pos - (l+1) > 0)
941 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
942 mp_fd->pos -= l+1;
945 if(*ret)
946 return 1;
947 else
948 return MP_INPUT_NOTHING;
951 static int
952 mp_input_default_cmd_func(int fd,char* buf, int l) {
954 while(1) {
955 int r = read(fd,buf,l);
956 // Error ?
957 if(r < 0) {
958 if(errno == EINTR)
959 continue;
960 else if(errno == EAGAIN)
961 return MP_INPUT_NOTHING;
962 return MP_INPUT_ERROR;
963 // EOF ?
965 return r;
971 void
972 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
973 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
975 filter->filter = func;
976 filter->ctx = ctx;
977 filter->next = cmd_filters;
978 cmd_filters = filter;
982 static char*
983 mp_input_find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys) {
984 int j;
986 if (n <= 0) return NULL;
987 for(j = 0; binds[j].cmd != NULL; j++) {
988 int found = 1,s;
989 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
990 if(binds[j].input[s] != keys[s]) {
991 found = 0;
992 break;
995 if(found && binds[j].input[s] == 0 && s == n)
996 break;
998 return binds[j].cmd;
1001 static mp_cmd_bind_section_t *mp_input_get_bind_section(struct input_ctx *ictx,
1002 char *section)
1004 mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections;
1006 if (section==NULL) section="default";
1007 while (bind_section) {
1008 if(strcmp(section,bind_section->section)==0) return bind_section;
1009 if(bind_section->next==NULL) break;
1010 bind_section=bind_section->next;
1012 if(bind_section) {
1013 bind_section->next=malloc(sizeof(mp_cmd_bind_section_t));
1014 bind_section=bind_section->next;
1015 } else {
1016 ictx->cmd_bind_sections = malloc(sizeof(mp_cmd_bind_section_t));
1017 bind_section = ictx->cmd_bind_sections;
1019 bind_section->cmd_binds=NULL;
1020 bind_section->section=strdup(section);
1021 bind_section->next=NULL;
1022 return bind_section;
1025 static mp_cmd_t *mp_input_get_cmd_from_keys(struct input_ctx *ictx,
1026 int n, int *keys, int paused)
1028 char* cmd = NULL;
1029 mp_cmd_t* ret;
1031 if (ictx->cmd_binds)
1032 cmd = mp_input_find_bind_for_key(ictx->cmd_binds, n, keys);
1033 if (ictx->cmd_binds_default && cmd == NULL)
1034 cmd = mp_input_find_bind_for_key(ictx->cmd_binds_default, n, keys);
1035 if(cmd == NULL)
1036 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
1038 if(cmd == NULL) {
1039 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound,mp_input_get_key_name(keys[0]));
1040 if(n > 1) {
1041 int s;
1042 for(s=1; s < n; s++)
1043 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
1045 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1046 return NULL;
1048 if (strcmp(cmd, "ignore") == 0) return NULL;
1049 ret = mp_input_parse_cmd(cmd);
1050 if(!ret) {
1051 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,mp_input_get_key_name(key_down[0]));
1052 if( num_key_down > 1) {
1053 unsigned int s;
1054 for(s=1; s < num_key_down; s++)
1055 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(key_down[s]));
1057 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1059 return ret;
1063 static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code, int paused)
1065 unsigned int j;
1066 mp_cmd_t* ret;
1068 if(mp_input_key_cb) {
1069 if (code & MP_KEY_DOWN)
1070 return NULL;
1071 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1072 if (mp_input_key_cb(code))
1073 return NULL;
1076 if(code & MP_KEY_DOWN) {
1077 if(num_key_down > MP_MAX_KEY_DOWN) {
1078 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1079 return NULL;
1081 code &= ~MP_KEY_DOWN;
1082 // Check if we don't already have this key as pushed
1083 for(j = 0; j < num_key_down; j++) {
1084 if(key_down[j] == code)
1085 break;
1087 if(j != num_key_down)
1088 return NULL;
1089 key_down[num_key_down] = code;
1090 num_key_down++;
1091 last_key_down = GetTimer();
1092 ictx->ar_state = 0;
1093 return NULL;
1095 // key released
1096 // Check if the key is in the down key, driver which can't send push event
1097 // send only release event
1098 for(j = 0; j < num_key_down; j++) {
1099 if(key_down[j] == code)
1100 break;
1102 if(j == num_key_down) { // key was not in the down keys : add it
1103 if(num_key_down > MP_MAX_KEY_DOWN) {
1104 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1105 return NULL;
1107 key_down[num_key_down] = code;
1108 num_key_down++;
1109 last_key_down = 1;
1111 // We ignore key from last combination
1112 ret = last_key_down ?
1113 mp_input_get_cmd_from_keys(ictx, num_key_down, key_down, paused)
1114 : NULL;
1115 // Remove the key
1116 if(j+1 < num_key_down)
1117 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1118 num_key_down--;
1119 last_key_down = 0;
1120 ictx->ar_state = -1;
1121 if (ictx->ar_cmd) {
1122 mp_cmd_free(ictx->ar_cmd);
1123 ictx->ar_cmd = NULL;
1125 return ret;
1128 static mp_cmd_t *check_autorepeat(struct input_ctx *ictx, int paused)
1130 // No input : autorepeat ?
1131 if (ar_rate > 0 && ictx->ar_state >=0 && num_key_down > 0
1132 && !(key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1133 unsigned int t = GetTimer();
1134 // First time : wait delay
1135 if (ictx->ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1136 ictx->ar_cmd = mp_input_get_cmd_from_keys(ictx, num_key_down,
1137 key_down, paused);
1138 if (!ictx->ar_cmd) {
1139 ictx->ar_state = -1;
1140 return NULL;
1142 ictx->ar_state = 1;
1143 ictx->last_ar = t;
1144 return mp_cmd_clone(ictx->ar_cmd);
1145 // Then send rate / sec event
1146 } else if (ictx->ar_state == 1 && (t -ictx->last_ar) >= 1000000/ar_rate) {
1147 ictx->last_ar = t;
1148 return mp_cmd_clone(ictx->ar_cmd);
1151 return NULL;
1155 static mp_cmd_t *read_events(struct input_ctx *ictx, int time, int paused)
1157 int i;
1158 int got_cmd = 0;
1159 for (i = 0; i < num_key_fd; i++)
1160 if (key_fds[i].dead) {
1161 mp_input_rm_key_fd(key_fds[i].fd);
1162 i--;
1164 for (i = 0; i < num_cmd_fd; i++)
1165 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1166 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1167 i--;
1169 else if (cmd_fds[i].got_cmd)
1170 got_cmd = 1;
1171 #ifdef HAVE_POSIX_SELECT
1172 fd_set fds;
1173 FD_ZERO(&fds);
1174 if (!got_cmd) {
1175 int max_fd = 0, num_fd = 0;
1176 for (i = 0; i < num_key_fd; i++) {
1177 if (key_fds[i].no_select)
1178 continue;
1179 if (key_fds[i].fd > max_fd)
1180 max_fd = key_fds[i].fd;
1181 FD_SET(key_fds[i].fd, &fds);
1182 num_fd++;
1184 for (i = 0; i < num_cmd_fd; i++) {
1185 if (cmd_fds[i].no_select)
1186 continue;
1187 if (cmd_fds[i].fd > max_fd)
1188 max_fd = cmd_fds[i].fd;
1189 FD_SET(cmd_fds[i].fd, &fds);
1190 num_fd++;
1192 if (num_fd > 0) {
1193 struct timeval tv, *time_val;
1194 if (time >= 0) {
1195 tv.tv_sec = time / 1000;
1196 tv.tv_usec = (time % 1000) * 1000;
1197 time_val = &tv;
1199 else
1200 time_val = NULL;
1201 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1202 if (errno != EINTR)
1203 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrSelect,
1204 strerror(errno));
1205 FD_ZERO(&fds);
1209 #else
1210 if (!got_cmd)
1211 usec_sleep(time * 1000);
1212 #endif
1215 for (i = 0; i < num_key_fd; i++) {
1216 #ifdef HAVE_POSIX_SELECT
1217 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1218 continue;
1219 #endif
1221 int code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd);
1222 if (code >= 0) {
1223 mp_cmd_t *ret = interpret_key(ictx, code, paused);
1224 if (ret)
1225 return ret;
1227 else if (code == MP_INPUT_ERROR)
1228 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1229 key_fds[i].fd);
1230 else if (code == MP_INPUT_DEAD) {
1231 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1232 key_fds[i].fd);
1233 key_fds[i].dead = 1;
1236 mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx, paused);
1237 if (autorepeat_cmd)
1238 return autorepeat_cmd;
1240 for (i = 0; i < num_cmd_fd; i++) {
1241 #ifdef HAVE_POSIX_SELECT
1242 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1243 !cmd_fds[i].got_cmd)
1244 continue;
1245 #endif
1246 char *cmd;
1247 int r = mp_input_read_cmd(&cmd_fds[i], &cmd);
1248 if (r >= 0) {
1249 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1250 free(cmd);
1251 if (ret)
1252 return ret;
1254 else if (r == MP_INPUT_ERROR)
1255 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1256 cmd_fds[i].fd);
1257 else if (r == MP_INPUT_DEAD)
1258 cmd_fds[i].dead = 1;
1261 return NULL;
1266 mp_input_queue_cmd(mp_cmd_t* cmd) {
1267 if(!cmd || cmd_queue_length >= CMD_QUEUE_SIZE)
1268 return 0;
1269 cmd_queue[cmd_queue_end] = cmd;
1270 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1271 cmd_queue_length++;
1272 return 1;
1275 static mp_cmd_t*
1276 mp_input_get_queued_cmd(int peek_only) {
1277 mp_cmd_t* ret;
1279 if(cmd_queue_length == 0)
1280 return NULL;
1282 ret = cmd_queue[cmd_queue_start];
1284 if (!peek_only) {
1285 cmd_queue_length--;
1286 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1289 return ret;
1293 * \param peek_only when set, the returned command stays in the queue.
1294 * Do not free the returned cmd whe you set this!
1296 mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int paused,
1297 int peek_only)
1299 mp_cmd_t* ret = NULL;
1300 mp_cmd_filter_t* cf;
1301 int from_queue;
1303 if (async_quit_request)
1304 return mp_input_parse_cmd("quit 1");
1305 while(1) {
1306 from_queue = 1;
1307 ret = mp_input_get_queued_cmd(peek_only);
1308 if(ret) break;
1309 from_queue = 0;
1310 ret = read_events(ictx, time, paused);
1311 if (!ret) {
1312 from_queue = 1;
1313 ret = mp_input_get_queued_cmd(peek_only);
1315 break;
1317 if(!ret) return NULL;
1319 for(cf = cmd_filters ; cf ; cf = cf->next) {
1320 if(cf->filter(ret,paused,cf->ctx)) {
1321 if (peek_only && from_queue)
1322 // The filter ate the cmd, so we remove it from queue
1323 ret = mp_input_get_queued_cmd(0);
1324 mp_cmd_free(ret);
1325 return NULL;
1329 if (!from_queue && peek_only)
1330 mp_input_queue_cmd(ret);
1332 return ret;
1335 void
1336 mp_cmd_free(mp_cmd_t* cmd) {
1337 int i;
1338 //#ifdef MP_DEBUG
1339 // assert(cmd != NULL);
1340 //#endif
1341 if ( !cmd ) return;
1343 if(cmd->name)
1344 free(cmd->name);
1346 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1347 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1348 free(cmd->args[i].v.s);
1350 free(cmd);
1353 mp_cmd_t*
1354 mp_cmd_clone(mp_cmd_t* cmd) {
1355 mp_cmd_t* ret;
1356 int i;
1357 #ifdef MP_DEBUG
1358 assert(cmd != NULL);
1359 #endif
1361 ret = malloc(sizeof(mp_cmd_t));
1362 memcpy(ret,cmd,sizeof(mp_cmd_t));
1363 if(cmd->name)
1364 ret->name = strdup(cmd->name);
1365 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1366 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1367 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1370 return ret;
1373 static char key_str[12];
1375 static char*
1376 mp_input_get_key_name(int key) {
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(key_str,12,"%c",(char)key);
1386 return key_str;
1389 // Print the hex key code
1390 snprintf(key_str,12,"%#-8x",key);
1391 return key_str;
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
1413 mp_input_get_input_from_name(char* name,int* keys) {
1414 char *end,*ptr;
1415 int n=0;
1417 ptr = name;
1418 n = 0;
1419 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1420 if(end && end[1] != '\0') {
1421 if(end[1] == '-')
1422 end = &end[1];
1423 end[0] = '\0';
1425 keys[n] = mp_input_get_key_from_name(ptr);
1426 if(keys[n] < 0) {
1427 return 0;
1429 n++;
1430 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1431 ptr = &end[1];
1432 else
1433 break;
1435 keys[n] = 0;
1436 return 1;
1439 #define BS_MAX 256
1440 #define SPACE_CHAR " \n\r\t"
1442 void mp_input_bind_keys(struct input_ctx *ictx,
1443 const int keys[MP_MAX_KEY_DOWN+1], char* cmd)
1445 int i = 0,j;
1446 mp_cmd_bind_t* bind = NULL;
1447 mp_cmd_bind_section_t* bind_section = NULL;
1448 char *section=NULL, *p;
1450 #ifdef MP_DEBUG
1451 assert(keys != NULL);
1452 assert(cmd != NULL);
1453 #endif
1455 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1456 *p=0;
1457 section=++cmd;
1458 cmd=++p;
1459 // Jump beginning space
1460 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1461 /* NOTHING */;
1463 bind_section = mp_input_get_bind_section(ictx, section);
1465 if(bind_section->cmd_binds) {
1466 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1467 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1468 /* NOTHING */;
1469 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1470 bind = &bind_section->cmd_binds[i];
1471 break;
1476 if(!bind) {
1477 bind_section->cmd_binds = realloc(bind_section->cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1478 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1479 bind = &bind_section->cmd_binds[i];
1481 if(bind->cmd)
1482 free(bind->cmd);
1483 bind->cmd = strdup(cmd);
1484 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1487 void mp_input_add_binds(struct input_ctx *ictx, const mp_cmd_bind_t* list)
1489 int i;
1490 for(i = 0 ; list[i].cmd ; i++)
1491 mp_input_bind_keys(ictx, list[i].input,list[i].cmd);
1494 static void
1495 mp_input_free_binds(mp_cmd_bind_t* binds) {
1496 int i;
1498 if(!binds)
1499 return;
1501 for(i = 0; binds[i].cmd != NULL; i++)
1502 free(binds[i].cmd);
1504 free(binds);
1508 static int mp_input_parse_config(struct input_ctx *ictx, char *file)
1510 int fd;
1511 int bs = 0,r,eof = 0,comments = 0;
1512 char *iter,*end;
1513 char buffer[BS_MAX];
1514 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1516 fd = open(file,O_RDONLY);
1518 if(fd < 0) {
1519 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1520 return 0;
1523 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1525 while(1) {
1526 if(! eof && bs < BS_MAX-1) {
1527 if(bs > 0) bs--;
1528 r = read(fd,buffer+bs,BS_MAX-1-bs);
1529 if(r < 0) {
1530 if(errno == EINTR)
1531 continue;
1532 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1533 close(fd);
1534 return 0;
1535 } else if(r == 0) {
1536 eof = 1;
1537 } else {
1538 bs += r+1;
1539 buffer[bs-1] = '\0';
1542 // Empty buffer : return
1543 if(bs <= 1) {
1544 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1545 close(fd);
1546 return 1;
1549 iter = buffer;
1551 if(comments) {
1552 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1553 /* NOTHING */;
1554 if(iter[0] == '\0') { // Buffer was full of comment
1555 bs = 0;
1556 continue;
1558 iter++;
1559 r = strlen(iter);
1560 memmove(buffer,iter,r+1);
1561 bs = r+1;
1562 comments = 0;
1563 continue;
1566 // Find the wanted key
1567 if(keys[0] == 0) {
1568 // Jump beginning space
1569 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1570 /* NOTHING */;
1571 if(iter[0] == '\0') { // Buffer was full of space char
1572 bs = 0;
1573 continue;
1575 if(iter[0] == '#') { // Comments
1576 comments = 1;
1577 continue;
1579 // Find the end of the key code name
1580 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1581 /*NOTHING */;
1582 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1583 if(buffer == iter) {
1584 if(eof && (buffer-iter) == bs)
1585 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1586 else
1587 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1588 return 0;
1590 memmove(buffer,iter,end-iter);
1591 bs = end-iter;
1592 continue;
1595 char name[end-iter+1];
1596 strncpy(name,iter,end-iter);
1597 name[end-iter] = '\0';
1598 if(! mp_input_get_input_from_name(name,keys)) {
1599 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1600 close(fd);
1601 return 0;
1604 if( bs > (end-buffer))
1605 memmove(buffer,end,bs - (end-buffer));
1606 bs -= end-buffer;
1607 continue;
1608 } else { // Get the command
1609 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1610 // Found new line
1611 if(iter[0] == '\n' || iter[0] == '\r') {
1612 int i;
1613 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey,mp_input_get_key_name(keys[0]));
1614 for(i = 1; keys[i] != 0 ; i++)
1615 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1616 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1617 keys[0] = 0;
1618 if(iter > buffer) {
1619 memmove(buffer,iter,bs- (iter-buffer));
1620 bs -= (iter-buffer);
1622 continue;
1624 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1625 /* NOTHING */;
1626 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1627 if(iter == buffer) {
1628 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1629 close(fd);
1630 return 0;
1632 memmove(buffer,iter,end - iter);
1633 bs = end - iter;
1634 continue;
1637 char cmd[end-iter+1];
1638 strncpy(cmd,iter,end-iter);
1639 cmd[end-iter] = '\0';
1640 //printf("Set bind %d => %s\n",keys[0],cmd);
1641 mp_input_bind_keys(ictx, keys,cmd);
1642 n_binds++;
1644 keys[0] = 0;
1645 end++;
1646 if(bs > (end-buffer))
1647 memmove(buffer,end,bs-(end-buffer));
1648 bs -= (end-buffer);
1649 buffer[bs-1] = '\0';
1650 continue;
1653 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1654 close(fd);
1655 mp_input_set_section(ictx, NULL);
1656 return 0;
1659 void mp_input_set_section(struct input_ctx *ictx, char *name)
1661 mp_cmd_bind_section_t* bind_section = NULL;
1663 ictx->cmd_binds = NULL;
1664 ictx->cmd_binds_default = NULL;
1665 if (ictx->section)
1666 free(ictx->section);
1667 if (name)
1668 ictx->section = strdup(name);
1669 else
1670 ictx->section = strdup("default");
1671 if ((bind_section = mp_input_get_bind_section(ictx, ictx->section)))
1672 ictx->cmd_binds = bind_section->cmd_binds;
1673 if (strcmp(ictx->section, "default") == 0)
1674 return;
1675 if ((bind_section = mp_input_get_bind_section(ictx, NULL)))
1676 ictx->cmd_binds_default = bind_section->cmd_binds;
1679 char *mp_input_get_section(struct input_ctx *ictx)
1681 return ictx->section;
1684 struct input_ctx *mp_input_init(int use_gui)
1686 struct input_ctx *ictx = talloc_ptrtype(NULL, ictx);
1687 *ictx = (struct input_ctx){
1688 .ar_state = -1,
1691 char* file;
1693 #ifdef HAVE_NEW_GUI
1694 if(use_gui)
1695 mp_input_add_binds(ictx, gui_def_cmd_binds);
1696 #endif
1698 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1699 if(!file)
1700 return ictx;
1702 if (!mp_input_parse_config(ictx, file)) {
1703 // free file if it was allocated by get_path(),
1704 // before it gets overwritten
1705 if( file != config_file)
1707 free(file);
1709 // Try global conf dir
1710 file = MPLAYER_CONFDIR "/input.conf";
1711 if (!mp_input_parse_config(ictx, file))
1712 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1714 else
1716 // free file if it was allocated by get_path()
1717 if( file != config_file)
1718 free(file);
1721 #ifdef HAVE_JOYSTICK
1722 if(use_joystick) {
1723 int fd = mp_input_joystick_init(js_dev);
1724 if(fd < 0)
1725 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1726 else
1727 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close,NULL);
1729 #endif
1731 #ifdef HAVE_LIRC
1732 if(use_lirc) {
1733 int fd = mp_input_lirc_init();
1734 if(fd > 0)
1735 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1737 #endif
1739 #ifdef HAVE_LIRCC
1740 if(use_lircc) {
1741 int fd = lircc_init("mplayer", NULL);
1742 if(fd >= 0)
1743 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1745 #endif
1747 #ifdef HAVE_APPLE_REMOTE
1748 if(use_ar) {
1749 if(mp_input_ar_init() < 0)
1750 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1751 else
1752 mp_input_add_key_fd(-1,0,mp_input_ar_read,mp_input_ar_close, NULL);
1754 #endif
1756 if(in_file) {
1757 struct stat st;
1758 if(stat(in_file,&st))
1759 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,in_file,strerror(errno));
1760 else {
1761 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1762 if(in_file_fd >= 0)
1763 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1764 else
1765 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));
1768 return ictx;
1771 void mp_input_uninit(struct input_ctx *ictx)
1773 if (!ictx)
1774 return;
1776 unsigned int i;
1777 mp_cmd_bind_section_t* bind_section;
1779 for(i=0; i < num_key_fd; i++) {
1780 if(key_fds[i].close_func)
1781 key_fds[i].close_func(key_fds[i].fd);
1784 for(i=0; i < num_cmd_fd; i++) {
1785 if(cmd_fds[i].close_func)
1786 cmd_fds[i].close_func(cmd_fds[i].fd);
1788 while (ictx->cmd_bind_sections) {
1789 mp_input_free_binds(ictx->cmd_bind_sections->cmd_binds);
1790 free(ictx->cmd_bind_sections->section);
1791 bind_section=ictx->cmd_bind_sections->next;
1792 free(ictx->cmd_bind_sections);
1793 ictx->cmd_bind_sections=bind_section;
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 mp_input_print_key_list(m_option_t* cfg) {
1804 int i;
1805 printf("\n");
1806 for(i= 0; key_names[i].name != NULL ; i++)
1807 printf("%s\n",key_names[i].name);
1808 exit(0);
1811 static int mp_input_print_cmd_list(m_option_t* cfg) {
1812 const mp_cmd_t *cmd;
1813 int i,j;
1814 const char* type;
1816 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1817 printf("%-20.20s",cmd->name);
1818 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1819 switch(cmd->args[j].type) {
1820 case MP_CMD_ARG_INT:
1821 type = "Integer";
1822 break;
1823 case MP_CMD_ARG_FLOAT:
1824 type = "Float";
1825 break;
1826 case MP_CMD_ARG_STRING:
1827 type = "String";
1828 break;
1829 default:
1830 type = "??";
1832 if(j+1 > cmd->nargs)
1833 printf(" [%s]",type);
1834 else
1835 printf(" %s",type);
1837 printf("\n");
1839 exit(0);
1842 int mp_input_check_interrupt(struct input_ctx *ictx, int time)
1844 mp_cmd_t* cmd;
1845 if ((cmd = mp_input_get_cmd(ictx, time, 0, 1)) == NULL)
1846 return 0;
1847 switch(cmd->id) {
1848 case MP_CMD_QUIT:
1849 case MP_CMD_PLAY_TREE_STEP:
1850 case MP_CMD_PLAY_TREE_UP_STEP:
1851 case MP_CMD_PLAY_ALT_SRC_STEP:
1852 // The cmd will be executed when we are back in the main loop
1853 return 1;
1855 // remove the cmd from the queue
1856 cmd = mp_input_get_cmd(ictx, time, 0, 0);
1857 mp_cmd_free(cmd);
1858 return 0;