Add a comment that explains why this header has no multiple inclusion guards.
[mplayer/greg.git] / input / input.c
blob8e2d30a4dc1c4fb5cf95ee2d9634fc94df4d01ac
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 <signal.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <sys/time.h>
12 #include <fcntl.h>
13 #include <ctype.h>
15 #include "input.h"
16 #include "mouse.h"
17 #ifdef MP_DEBUG
18 #include <assert.h>
19 #endif
20 #include "mp_fifo.h"
21 #include "osdep/getch2.h"
22 #include "osdep/keycodes.h"
23 #include "osdep/timer.h"
24 #include "avstring.h"
25 #include "mp_msg.h"
26 #include "help_mp.h"
27 #include "m_config.h"
28 #include "m_option.h"
29 #include "get_path.h"
31 #include "joystick.h"
33 #ifdef HAVE_LIRC
34 #include "lirc.h"
35 #endif
37 #ifdef HAVE_LIRCC
38 #include <lirc/lircc.h>
39 #endif
41 #include "ar.h"
43 /// This array defines all known commands.
44 /// The first field is an id used to recognize the command without too many strcmp.
45 /// The second is obviously the command name.
46 /// The third is the minimum number of arguments this command needs.
47 /// Then comes the definition of each argument, terminated with an arg of type -1.
48 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
49 /// the last one) which is actually 9.
51 /// For the args, the first field is the type (actually int, float or string), the second
52 /// is the default value wich is used for optional arguments
54 static mp_cmd_t mp_cmds[] = {
55 #ifdef USE_RADIO
56 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
57 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
58 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
59 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
60 #endif
61 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
62 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
63 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
64 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
65 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
66 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
67 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
68 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
69 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
70 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
71 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
72 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
73 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
74 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
75 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
76 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
77 { 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}} } },
78 { 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}} } },
79 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
80 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
81 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
82 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
83 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
84 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
85 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
86 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
87 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
88 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
89 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
90 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
91 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
92 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
93 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
94 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
95 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
96 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
97 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
98 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
99 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
100 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
101 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
102 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
103 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
104 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
105 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
106 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
107 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
108 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
109 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
110 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
111 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
112 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
113 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
114 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
115 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
116 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
117 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
118 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
119 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
120 #ifdef USE_TV
121 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
122 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
123 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
124 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
125 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
126 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
127 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
128 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
129 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
130 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
131 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
132 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
133 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
134 #endif
135 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
136 #ifdef HAS_DVBIN_SUPPORT
137 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
138 #endif
139 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
140 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
141 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
142 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
143 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
144 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
145 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
146 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
147 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
148 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
149 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
150 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
151 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
152 #ifdef HAVE_TV_TELETEXT
153 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
154 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
155 #endif
157 #ifdef HAVE_NEW_GUI
158 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
159 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
160 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
161 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
162 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
163 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
164 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
165 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
166 #endif
168 #ifdef USE_DVDNAV
169 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
170 #endif
172 #ifdef HAVE_MENU
173 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
174 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
175 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
176 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
177 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
178 #endif
180 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
181 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
182 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
183 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
184 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
185 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
187 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
188 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
190 { 0, NULL, 0, {} }
193 /// The names of the keys as used in input.conf
194 /// If you add some new keys, you also need to add them here
196 static mp_key_name_t key_names[] = {
197 { ' ', "SPACE" },
198 { '#', "SHARP" },
199 { KEY_ENTER, "ENTER" },
200 { KEY_TAB, "TAB" },
201 { KEY_CTRL, "CTRL" },
202 { KEY_BACKSPACE, "BS" },
203 { KEY_DELETE, "DEL" },
204 { KEY_INSERT, "INS" },
205 { KEY_HOME, "HOME" },
206 { KEY_END, "END" },
207 { KEY_PAGE_UP, "PGUP" },
208 { KEY_PAGE_DOWN, "PGDWN" },
209 { KEY_ESC, "ESC" },
210 { KEY_RIGHT, "RIGHT" },
211 { KEY_LEFT, "LEFT" },
212 { KEY_DOWN, "DOWN" },
213 { KEY_UP, "UP" },
214 { KEY_F+1, "F1" },
215 { KEY_F+2, "F2" },
216 { KEY_F+3, "F3" },
217 { KEY_F+4, "F4" },
218 { KEY_F+5, "F5" },
219 { KEY_F+6, "F6" },
220 { KEY_F+7, "F7" },
221 { KEY_F+8, "F8" },
222 { KEY_F+9, "F9" },
223 { KEY_F+10, "F10" },
224 { KEY_F+11, "F11" },
225 { KEY_F+12, "F12" },
226 { KEY_KP0, "KP0" },
227 { KEY_KP1, "KP1" },
228 { KEY_KP2, "KP2" },
229 { KEY_KP3, "KP3" },
230 { KEY_KP4, "KP4" },
231 { KEY_KP5, "KP5" },
232 { KEY_KP6, "KP6" },
233 { KEY_KP7, "KP7" },
234 { KEY_KP8, "KP8" },
235 { KEY_KP9, "KP9" },
236 { KEY_KPDEL, "KP_DEL" },
237 { KEY_KPDEC, "KP_DEC" },
238 { KEY_KPINS, "KP_INS" },
239 { KEY_KPENTER, "KP_ENTER" },
240 { MOUSE_BTN0, "MOUSE_BTN0" },
241 { MOUSE_BTN1, "MOUSE_BTN1" },
242 { MOUSE_BTN2, "MOUSE_BTN2" },
243 { MOUSE_BTN3, "MOUSE_BTN3" },
244 { MOUSE_BTN4, "MOUSE_BTN4" },
245 { MOUSE_BTN5, "MOUSE_BTN5" },
246 { MOUSE_BTN6, "MOUSE_BTN6" },
247 { MOUSE_BTN7, "MOUSE_BTN7" },
248 { MOUSE_BTN8, "MOUSE_BTN8" },
249 { MOUSE_BTN9, "MOUSE_BTN9" },
250 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
251 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
252 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
253 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
254 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
255 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
256 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
257 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
258 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
259 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
260 { JOY_AXIS1_MINUS, "JOY_UP" },
261 { JOY_AXIS1_PLUS, "JOY_DOWN" },
262 { JOY_AXIS0_MINUS, "JOY_LEFT" },
263 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
265 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
266 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
267 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
268 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
269 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
270 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
271 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
272 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
273 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
274 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
275 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
276 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
277 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
278 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
279 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
280 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
281 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
282 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
283 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
284 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
286 { JOY_BTN0, "JOY_BTN0" },
287 { JOY_BTN1, "JOY_BTN1" },
288 { JOY_BTN2, "JOY_BTN2" },
289 { JOY_BTN3, "JOY_BTN3" },
290 { JOY_BTN4, "JOY_BTN4" },
291 { JOY_BTN5, "JOY_BTN5" },
292 { JOY_BTN6, "JOY_BTN6" },
293 { JOY_BTN7, "JOY_BTN7" },
294 { JOY_BTN8, "JOY_BTN8" },
295 { JOY_BTN9, "JOY_BTN9" },
297 { AR_PLAY, "AR_PLAY" },
298 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
299 { AR_NEXT, "AR_NEXT" },
300 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
301 { AR_PREV, "AR_PREV" },
302 { AR_PREV_HOLD, "AR_PREV_HOLD" },
303 { AR_MENU, "AR_MENU" },
304 { AR_MENU_HOLD, "AR_MENU_HOLD" },
305 { AR_VUP, "AR_VUP" },
306 { AR_VDOWN, "AR_VDOWN" },
308 { KEY_POWER, "POWER" },
309 { KEY_MENU, "MENU" },
310 { KEY_PLAY, "PLAY" },
311 { KEY_PAUSE, "PAUSE" },
312 { KEY_PLAYPAUSE, "PLAYPAUSE" },
313 { KEY_STOP, "STOP" },
314 { KEY_FORWARD, "FORWARD" },
315 { KEY_REWIND, "REWIND" },
316 { KEY_NEXT, "NEXT" },
317 { KEY_PREV, "PREV" },
318 { KEY_VOLUME_UP, "VOLUME_UP" },
319 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
320 { KEY_MUTE, "MUTE" },
322 // These are kept for backward compatibility
323 { KEY_PAUSE, "XF86_PAUSE" },
324 { KEY_STOP, "XF86_STOP" },
325 { KEY_PREV, "XF86_PREV" },
326 { KEY_NEXT, "XF86_NEXT" },
328 { KEY_CLOSE_WIN, "CLOSE_WIN" },
330 { 0, NULL }
333 // This is the default binding. The content of input.conf overrides these.
334 // The first arg is a null terminated array of key codes.
335 // The second is the command
337 static mp_cmd_bind_t def_cmd_binds[] = {
339 { { MOUSE_BTN3, 0 }, "seek 10" },
340 { { MOUSE_BTN4, 0 }, "seek -10" },
341 { { MOUSE_BTN5, 0 }, "volume 1" },
342 { { MOUSE_BTN6, 0 }, "volume -1" },
344 #ifdef USE_DVDNAV
345 { { KEY_KP8, 0 }, "dvdnav 1" }, // up
346 { { KEY_KP2, 0 }, "dvdnav 2" }, // down
347 { { KEY_KP4, 0 }, "dvdnav 3" }, // left
348 { { KEY_KP6, 0 }, "dvdnav 4" }, // right
349 { { KEY_KP5, 0 }, "dvdnav 5" }, // menu
350 { { KEY_KPENTER, 0 }, "dvdnav 6" }, // select
351 { { MOUSE_BTN0, 0 }, "dvdnav 8" }, //select
352 { { KEY_KP7, 0 }, "dvdnav 7" }, // previous menu
353 #endif
355 { { KEY_RIGHT, 0 }, "seek 10" },
356 { { KEY_LEFT, 0 }, "seek -10" },
357 { { KEY_UP, 0 }, "seek 60" },
358 { { KEY_DOWN, 0 }, "seek -60" },
359 { { KEY_PAGE_UP, 0 }, "seek 600" },
360 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
361 { { '+', 0 }, "audio_delay 0.100" },
362 { { '-', 0 }, "audio_delay -0.100" },
363 { { '[', 0 }, "speed_mult 0.9091" },
364 { { ']', 0 }, "speed_mult 1.1" },
365 { { '{', 0 }, "speed_mult 0.5" },
366 { { '}', 0 }, "speed_mult 2.0" },
367 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
368 { { 'q', 0 }, "quit" },
369 { { KEY_ESC, 0 }, "quit" },
370 { { 'p', 0 }, "pause" },
371 { { ' ', 0 }, "pause" },
372 { { '.', 0 }, "frame_step" },
373 { { KEY_HOME, 0 }, "pt_up_step 1" },
374 { { KEY_END, 0 }, "pt_up_step -1" },
375 { { '>', 0 }, "pt_step 1" },
376 { { KEY_ENTER, 0 }, "pt_step 1 1" },
377 { { '<', 0 }, "pt_step -1" },
378 { { KEY_INS, 0 }, "alt_src_step 1" },
379 { { KEY_DEL, 0 }, "alt_src_step -1" },
380 { { 'o', 0 }, "osd" },
381 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
382 { { 'z', 0 }, "sub_delay -0.1" },
383 { { 'x', 0 }, "sub_delay +0.1" },
384 { { 'g', 0 }, "sub_step -1" },
385 { { 'y', 0 }, "sub_step +1" },
386 { { '9', 0 }, "volume -1" },
387 { { '/', 0 }, "volume -1" },
388 { { '0', 0 }, "volume 1" },
389 { { '*', 0 }, "volume 1" },
390 { { '(', 0 }, "balance -0.1" },
391 { { ')', 0 }, "balance 0.1" },
392 { { 'm', 0 }, "mute" },
393 { { '1', 0 }, "contrast -1" },
394 { { '2', 0 }, "contrast 1" },
395 { { '3', 0 }, "brightness -1" },
396 { { '4', 0 }, "brightness 1" },
397 { { '5', 0 }, "hue -1" },
398 { { '6', 0 }, "hue 1" },
399 { { '7', 0 }, "saturation -1" },
400 { { '8', 0 }, "saturation 1" },
401 { { 'd', 0 }, "frame_drop" },
402 { { 'D', 0 }, "step_property deinterlace" },
403 { { 'r', 0 }, "sub_pos -1" },
404 { { 't', 0 }, "sub_pos +1" },
405 { { 'a', 0 }, "sub_alignment" },
406 { { 'v', 0 }, "sub_visibility" },
407 { { 'j', 0 }, "sub_select" },
408 { { 'F', 0 }, "forced_subs_only" },
409 { { '#', 0 }, "switch_audio" },
410 { { '_', 0 }, "step_property switch_video" },
411 { { KEY_TAB, 0 }, "step_property switch_program" },
412 { { 'i', 0 }, "edl_mark" },
413 #ifdef USE_TV
414 { { 'h', 0 }, "tv_step_channel 1" },
415 { { 'k', 0 }, "tv_step_channel -1" },
416 { { 'n', 0 }, "tv_step_norm" },
417 { { 'u', 0 }, "tv_step_chanlist" },
418 #endif
419 #ifdef HAVE_TV_TELETEXT
420 { { 'X', 0 }, "step_property teletext_mode 1" },
421 { { 'W', 0 }, "step_property teletext_page 1" },
422 { { 'Q', 0 }, "step_property teletext_page -1" },
423 #endif
424 #ifdef HAVE_JOYSTICK
425 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
426 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
427 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
428 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
429 { { JOY_BTN0, 0 }, "pause" },
430 { { JOY_BTN1, 0 }, "osd" },
431 { { JOY_BTN2, 0 }, "volume 1"},
432 { { JOY_BTN3, 0 }, "volume -1"},
433 #endif
434 #ifdef HAVE_APPLE_REMOTE
435 { { AR_PLAY, 0}, "pause" },
436 { { AR_PLAY_HOLD, 0}, "quit" },
437 { { AR_NEXT, 0 }, "seek 30" },
438 { { AR_NEXT_HOLD, 0 }, "seek 120" },
439 { { AR_PREV, 0 }, "seek -10" },
440 { { AR_PREV_HOLD, 0 }, "seek -120" },
441 { { AR_MENU, 0 }, "osd" },
442 { { AR_MENU_HOLD, 0 }, "mute" },
443 { { AR_VUP, 0 }, "volume 1"},
444 { { AR_VDOWN, 0 }, "volume -1"},
445 #endif
446 { { 'T', 0 }, "vo_ontop" },
447 { { 'f', 0 }, "vo_fullscreen" },
448 { { 's', 0 }, "screenshot 0" },
449 { { 'S', 0 }, "screenshot 1" },
450 { { 'w', 0 }, "panscan -0.1" },
451 { { 'e', 0 }, "panscan +0.1" },
453 { { KEY_POWER, 0 }, "quit" },
454 { { KEY_MENU, 0 }, "osd" },
455 { { KEY_PLAY, 0 }, "pause" },
456 { { KEY_PAUSE, 0 }, "pause" },
457 { { KEY_PLAYPAUSE, 0 }, "pause" },
458 { { KEY_STOP, 0 }, "quit" },
459 { { KEY_FORWARD, 0 }, "seek 60" },
460 { { KEY_REWIND, 0 }, "seek -60" },
461 { { KEY_NEXT, 0 }, "pt_step 1" },
462 { { KEY_PREV, 0 }, "pt_step -1" },
463 { { KEY_VOLUME_UP, 0 }, "volume 1" },
464 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
465 { { KEY_MUTE, 0 }, "mute" },
467 { { KEY_CLOSE_WIN, 0 }, "quit" },
469 { { '!', 0 }, "seek_chapter -1" },
470 { { '@', 0 }, "seek_chapter 1" },
472 { { 0 }, NULL }
476 #ifdef HAVE_NEW_GUI
477 static mp_cmd_bind_t gui_def_cmd_binds[] = {
479 { { 'l', 0 }, "gui_loadfile" },
480 { { 't', 0 }, "gui_loadsubtitle" },
481 { { KEY_ENTER, 0 }, "gui_play" },
482 { { KEY_ESC, 0 }, "gui_stop" },
483 { { 'p', 0 }, "gui_playlist" },
484 { { 'r', 0 }, "gui_preferences" },
485 { { 'c', 0 }, "gui_skinbrowser" },
487 { { 0 }, NULL }
489 #endif
491 #ifndef MP_MAX_KEY_FD
492 #define MP_MAX_KEY_FD 10
493 #endif
495 #ifndef MP_MAX_CMD_FD
496 #define MP_MAX_CMD_FD 10
497 #endif
499 #define CMD_QUEUE_SIZE 100
501 typedef struct mp_input_fd {
502 int fd;
503 void* read_func;
504 mp_close_func_t close_func;
505 unsigned eof : 1;
506 unsigned drop : 1;
507 unsigned dead : 1;
508 unsigned got_cmd : 1;
509 unsigned no_select : 1;
510 unsigned no_readfunc_retval : 1;
511 // These fields are for the cmd fds.
512 char* buffer;
513 int pos,size;
514 } mp_input_fd_t;
516 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
518 struct mp_cmd_filter_st {
519 mp_input_cmd_filter filter;
520 void* ctx;
521 mp_cmd_filter_t* next;
524 typedef struct mp_cmd_bind_section_st mp_cmd_bind_section_t;
526 struct mp_cmd_bind_section_st {
527 mp_cmd_bind_t* cmd_binds;
528 char* section;
529 mp_cmd_bind_section_t* next;
532 // These are the user defined binds
533 static mp_cmd_bind_section_t* cmd_binds_section = NULL;
534 static char* section = NULL;
535 static mp_cmd_bind_t* cmd_binds = NULL;
536 static mp_cmd_bind_t* cmd_binds_default = NULL;
537 static mp_cmd_filter_t* cmd_filters = NULL;
539 // Callback to allow the menu filter to grab the incoming keys
540 int (*mp_input_key_cb)(int code) = NULL;
542 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
543 static unsigned int num_key_fd = 0;
544 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
545 static unsigned int num_cmd_fd = 0;
546 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
547 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
549 // this is the key currently down
550 static int key_down[MP_MAX_KEY_DOWN];
551 static unsigned int num_key_down = 0, last_key_down = 0;
553 // Autorepeat stuff
554 static short ar_state = -1;
555 static mp_cmd_t* ar_cmd = NULL;
556 static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;
558 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
559 static char* config_file = "input.conf";
561 /* Apple Remote */
562 static int use_ar = 1;
564 static char* js_dev = NULL;
566 static char* in_file = NULL;
567 static int in_file_fd = -1;
569 static int mp_input_print_key_list(m_option_t* cfg);
570 static int mp_input_print_cmd_list(m_option_t* cfg);
572 // Our command line options
573 static m_option_t input_conf[] = {
574 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
575 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
576 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
577 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
578 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
579 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
580 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
581 { NULL, NULL, 0, 0, 0, 0, NULL}
584 static m_option_t mp_input_opts[] = {
585 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
586 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
587 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
588 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
589 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
590 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
591 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
592 { "noar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
593 { "ar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
594 { NULL, NULL, 0, 0, 0, 0, NULL}
597 static int
598 mp_input_default_cmd_func(int fd,char* buf, int l);
600 static char*
601 mp_input_get_key_name(int key);
605 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
606 if(num_cmd_fd == MP_MAX_CMD_FD) {
607 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
608 return 0;
610 if (select && fd < 0) {
611 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
612 return 0;
615 memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t));
616 cmd_fds[num_cmd_fd].fd = fd;
617 cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func;
618 cmd_fds[num_cmd_fd].close_func = close_func;
619 cmd_fds[num_cmd_fd].no_select = !select;
620 num_cmd_fd++;
622 return 1;
625 void
626 mp_input_rm_cmd_fd(int fd) {
627 unsigned int i;
629 for(i = 0; i < num_cmd_fd; i++) {
630 if(cmd_fds[i].fd == fd)
631 break;
633 if(i == num_cmd_fd)
634 return;
635 if(cmd_fds[i].close_func)
636 cmd_fds[i].close_func(cmd_fds[i].fd);
637 if(cmd_fds[i].buffer)
638 free(cmd_fds[i].buffer);
640 if(i + 1 < num_cmd_fd)
641 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
642 num_cmd_fd--;
645 void
646 mp_input_rm_key_fd(int fd) {
647 unsigned int i;
649 for(i = 0; i < num_key_fd; i++) {
650 if(key_fds[i].fd == fd)
651 break;
653 if(i == num_key_fd)
654 return;
655 if(key_fds[i].close_func)
656 key_fds[i].close_func(key_fds[i].fd);
658 if(i + 1 < num_key_fd)
659 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
660 num_key_fd--;
664 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) {
665 if(num_key_fd == MP_MAX_KEY_FD) {
666 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
667 return 0;
669 if (select && fd < 0) {
670 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
671 return 0;
674 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
675 key_fds[num_key_fd].fd = fd;
676 key_fds[num_key_fd].read_func = read_func;
677 key_fds[num_key_fd].close_func = close_func;
678 key_fds[num_key_fd].no_select = !select;
679 num_key_fd++;
681 return 1;
685 mp_input_add_event_fd(int fd, void (*read_func)(void))
687 if(num_key_fd == MP_MAX_KEY_FD) {
688 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
689 return 0;
691 if (fd < 0) {
692 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_event_fd", fd);
693 return 0;
696 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
697 key_fds[num_key_fd].fd = fd;
698 key_fds[num_key_fd].read_func = read_func;
699 key_fds[num_key_fd].close_func = NULL;
700 key_fds[num_key_fd].no_readfunc_retval = 1;
701 num_key_fd++;
703 return 1;
706 void mp_input_rm_event_fd(int fd)
708 mp_input_rm_key_fd(fd);
711 int mp_input_parse_and_queue_cmds(const char *str) {
712 int cmd_num = 0;
714 while (*str == '\n' || *str == '\r' || *str == ' ')
715 ++str;
716 while (*str) {
717 mp_cmd_t *cmd;
718 size_t len = strcspn(str, "\r\n");
719 char *cmdbuf = malloc(len+1);
720 av_strlcpy(cmdbuf, str, len+1);
721 cmd = mp_input_parse_cmd(cmdbuf);
722 if (cmd) {
723 mp_input_queue_cmd(cmd);
724 ++cmd_num;
726 str += len;
727 while (*str == '\n' || *str == '\r' || *str == ' ')
728 ++str;
729 free(cmdbuf);
731 return cmd_num;
734 mp_cmd_t*
735 mp_input_parse_cmd(char* str) {
736 int i,l;
737 int pausing = 0;
738 char *ptr,*e;
739 mp_cmd_t *cmd, *cmd_def;
741 #ifdef MP_DEBUG
742 assert(str != NULL);
743 #endif
745 // Ignore heading spaces.
746 while (str[0] == ' ' || str[0] == '\t')
747 ++str;
749 if (strncmp(str, "pausing ", 8) == 0) {
750 pausing = 1;
751 str = &str[8];
752 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
753 pausing = 2;
754 str = &str[13];
755 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
756 pausing = 3;
757 str = &str[15];
760 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
761 /* NOTHING */;
762 if(ptr[0] != '\0')
763 l = ptr-str;
764 else
765 l = strlen(str);
767 if(l == 0)
768 return NULL;
770 for(i=0; mp_cmds[i].name != NULL; i++) {
771 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
772 break;
775 if(mp_cmds[i].name == NULL)
776 return NULL;
778 cmd_def = &mp_cmds[i];
780 cmd = calloc(1, sizeof(mp_cmd_t));
781 cmd->id = cmd_def->id;
782 cmd->name = strdup(cmd_def->name);
783 cmd->pausing = pausing;
785 ptr = str;
787 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
788 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
789 if(ptr[0] == '\0') break;
790 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
791 if(ptr[0] == '\0' || ptr[0] == '#') break;
792 cmd->args[i].type = cmd_def->args[i].type;
793 switch(cmd_def->args[i].type) {
794 case MP_CMD_ARG_INT:
795 errno = 0;
796 cmd->args[i].v.i = atoi(ptr);
797 if(errno != 0) {
798 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
799 ptr = NULL;
801 break;
802 case MP_CMD_ARG_FLOAT:
803 errno = 0;
804 cmd->args[i].v.f = atof(ptr);
805 if(errno != 0) {
806 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
807 ptr = NULL;
809 break;
810 case MP_CMD_ARG_STRING: {
811 char term;
812 char* ptr2 = ptr, *start;
814 if(ptr[0] == '\'' || ptr[0] == '"') {
815 term = ptr[0];
816 ptr2++;
817 } else
818 term = ' ';
819 start = ptr2;
820 while(1) {
821 e = strchr(ptr2,term);
822 if(!e) break;
823 if(e <= ptr2 || *(e - 1) != '\\') break;
824 ptr2 = e + 1;
827 if(term != ' ' && (!e || e[0] == '\0')) {
828 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
829 ptr = NULL;
830 break;
831 } else if(!e) e = ptr+strlen(ptr);
832 l = e-start;
833 ptr2 = start;
834 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
835 memmove(e,e+1,strlen(e));
836 ptr2 = e + 1;
837 l--;
839 cmd->args[i].v.s = malloc(l+1);
840 strncpy(cmd->args[i].v.s,start,l);
841 cmd->args[i].v.s[l] = '\0';
842 if(term != ' ') ptr += l+2;
843 } break;
844 case -1:
845 ptr = NULL;
846 break;
847 default :
848 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
851 cmd->nargs = i;
853 if(cmd_def->nargs > cmd->nargs) {
854 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
855 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
856 mp_cmd_free(cmd);
857 return NULL;
860 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
861 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
862 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
863 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
866 if(i < MP_CMD_MAX_ARGS)
867 cmd->args[i].type = -1;
869 return cmd;
872 #define MP_CMD_MAX_SIZE 256
874 static int
875 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
876 char* end;
877 (*ret) = NULL;
879 // Allocate the buffer if it doesn't exist
880 if(!mp_fd->buffer) {
881 mp_fd->buffer = malloc(MP_CMD_MAX_SIZE);
882 mp_fd->pos = 0;
883 mp_fd->size = MP_CMD_MAX_SIZE;
886 // Get some data if needed/possible
887 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
888 int r = ((mp_cmd_func_t)mp_fd->read_func)(mp_fd->fd,mp_fd->buffer+mp_fd->pos,mp_fd->size - 1 - mp_fd->pos);
889 // Error ?
890 if(r < 0) {
891 switch(r) {
892 case MP_INPUT_ERROR:
893 case MP_INPUT_DEAD:
894 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
895 case MP_INPUT_NOTHING:
896 return r;
897 case MP_INPUT_RETRY:
898 continue;
900 // EOF ?
901 } else if(r == 0) {
902 mp_fd->eof = 1;
903 break;
905 mp_fd->pos += r;
906 break;
909 mp_fd->got_cmd = 0;
911 while(1) {
912 int l = 0;
913 // Find the cmd end
914 mp_fd->buffer[mp_fd->pos] = '\0';
915 end = strchr(mp_fd->buffer,'\n');
916 // No cmd end ?
917 if(!end) {
918 // If buffer is full we must drop all until the next \n
919 if(mp_fd->size - mp_fd->pos <= 1) {
920 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
921 mp_fd->pos = 0;
922 mp_fd->drop = 1;
924 break;
926 // We already have a cmd : set the got_cmd flag
927 else if((*ret)) {
928 mp_fd->got_cmd = 1;
929 break;
932 l = end - mp_fd->buffer;
934 // Not dropping : put the cmd in ret
935 if (!mp_fd->drop) {
936 (*ret) = malloc(l+1);
937 strncpy((*ret),mp_fd->buffer,l);
938 (*ret)[l] = '\0';
939 } else { // Remove the dropping flag
940 mp_fd->drop = 0;
942 if( mp_fd->pos - (l+1) > 0)
943 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
944 mp_fd->pos -= l+1;
947 if(*ret)
948 return 1;
949 else
950 return MP_INPUT_NOTHING;
953 static int
954 mp_input_default_cmd_func(int fd,char* buf, int l) {
956 while(1) {
957 int r = read(fd,buf,l);
958 // Error ?
959 if(r < 0) {
960 if(errno == EINTR)
961 continue;
962 else if(errno == EAGAIN)
963 return MP_INPUT_NOTHING;
964 return MP_INPUT_ERROR;
965 // EOF ?
967 return r;
973 void
974 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
975 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
977 filter->filter = func;
978 filter->ctx = ctx;
979 filter->next = cmd_filters;
980 cmd_filters = filter;
984 static char*
985 mp_input_find_bind_for_key(mp_cmd_bind_t* binds, int n,int* keys) {
986 int j;
988 if (n <= 0) return NULL;
989 for(j = 0; binds[j].cmd != NULL; j++) {
990 int found = 1,s;
991 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
992 if(binds[j].input[s] != keys[s]) {
993 found = 0;
994 break;
997 if(found && binds[j].input[s] == 0 && s == n)
998 break;
1000 return binds[j].cmd;
1003 static mp_cmd_bind_section_t*
1004 mp_input_get_bind_section(char *section) {
1005 mp_cmd_bind_section_t* bind_section = cmd_binds_section;
1007 if (section==NULL) section="default";
1008 while (bind_section) {
1009 if(strcmp(section,bind_section->section)==0) return bind_section;
1010 if(bind_section->next==NULL) break;
1011 bind_section=bind_section->next;
1013 if(bind_section) {
1014 bind_section->next=malloc(sizeof(mp_cmd_bind_section_t));
1015 bind_section=bind_section->next;
1016 } else {
1017 cmd_binds_section=malloc(sizeof(mp_cmd_bind_section_t));
1018 bind_section=cmd_binds_section;
1020 bind_section->cmd_binds=NULL;
1021 bind_section->section=strdup(section);
1022 bind_section->next=NULL;
1023 return bind_section;
1026 static mp_cmd_t*
1027 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
1028 char* cmd = NULL;
1029 mp_cmd_t* ret;
1031 if(cmd_binds)
1032 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
1033 if(cmd_binds_default && cmd == NULL)
1034 cmd = mp_input_find_bind_for_key(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*
1064 interpret_key(int code, int paused)
1066 unsigned int j;
1067 mp_cmd_t* ret;
1069 if(mp_input_key_cb) {
1070 if (code & MP_KEY_DOWN)
1071 return NULL;
1072 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1073 if (mp_input_key_cb(code))
1074 return NULL;
1077 if(code & MP_KEY_DOWN) {
1078 if(num_key_down > MP_MAX_KEY_DOWN) {
1079 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1080 return NULL;
1082 code &= ~MP_KEY_DOWN;
1083 // Check if we don't already have this key as pushed
1084 for(j = 0; j < num_key_down; j++) {
1085 if(key_down[j] == code)
1086 break;
1088 if(j != num_key_down)
1089 return NULL;
1090 key_down[num_key_down] = code;
1091 num_key_down++;
1092 last_key_down = GetTimer();
1093 ar_state = 0;
1094 return NULL;
1096 // key released
1097 // Check if the key is in the down key, driver which can't send push event
1098 // send only release event
1099 for(j = 0; j < num_key_down; j++) {
1100 if(key_down[j] == code)
1101 break;
1103 if(j == num_key_down) { // key was not in the down keys : add it
1104 if(num_key_down > MP_MAX_KEY_DOWN) {
1105 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1106 return NULL;
1108 key_down[num_key_down] = code;
1109 num_key_down++;
1110 last_key_down = 1;
1112 // We ignore key from last combination
1113 ret = last_key_down ? mp_input_get_cmd_from_keys(num_key_down,key_down,paused) : NULL;
1114 // Remove the key
1115 if(j+1 < num_key_down)
1116 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1117 num_key_down--;
1118 last_key_down = 0;
1119 ar_state = -1;
1120 if(ar_cmd) {
1121 mp_cmd_free(ar_cmd);
1122 ar_cmd = NULL;
1124 return ret;
1127 static mp_cmd_t *check_autorepeat(int paused)
1129 // No input : autorepeat ?
1130 if(ar_rate > 0 && ar_state >=0 && num_key_down > 0 && ! (key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1131 unsigned int t = GetTimer();
1132 // First time : wait delay
1133 if(ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1134 ar_cmd = mp_input_get_cmd_from_keys(num_key_down,key_down,paused);
1135 if(!ar_cmd) {
1136 ar_state = -1;
1137 return NULL;
1139 ar_state = 1;
1140 last_ar = t;
1141 return mp_cmd_clone(ar_cmd);
1142 // Then send rate / sec event
1143 } else if(ar_state == 1 && (t -last_ar) >= 1000000/ar_rate) {
1144 last_ar = t;
1145 return mp_cmd_clone(ar_cmd);
1148 return NULL;
1152 static mp_cmd_t *read_events(int time, int paused)
1154 int i;
1155 int got_cmd = 0;
1156 mp_cmd_t *autorepeat_cmd;
1157 #ifdef HAVE_POSIX_SELECT
1158 fd_set fds;
1159 #endif
1160 for (i = 0; i < num_key_fd; i++)
1161 if (key_fds[i].dead) {
1162 mp_input_rm_key_fd(key_fds[i].fd);
1163 i--;
1165 for (i = 0; i < num_cmd_fd; i++)
1166 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1167 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1168 i--;
1170 else if (cmd_fds[i].got_cmd)
1171 got_cmd = 1;
1172 #ifdef HAVE_POSIX_SELECT
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 int code;
1217 #ifdef HAVE_POSIX_SELECT
1218 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1219 continue;
1220 #endif
1222 if (key_fds[i].no_readfunc_retval) { // getch2 handler special-cased for now
1223 ((void (*)(void))key_fds[i].read_func)();
1224 if (cmd_queue_length)
1225 return NULL;
1226 code = mplayer_get_key(0);
1227 if (code < 0)
1228 code = MP_INPUT_NOTHING;
1230 else
1231 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
1232 if (code >= 0) {
1233 mp_cmd_t *ret = interpret_key(code, paused);
1234 if (ret)
1235 return ret;
1237 else if (code == MP_INPUT_ERROR)
1238 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1239 key_fds[i].fd);
1240 else if (code == MP_INPUT_DEAD) {
1241 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1242 key_fds[i].fd);
1243 key_fds[i].dead = 1;
1246 autorepeat_cmd = check_autorepeat(paused);
1247 if (autorepeat_cmd)
1248 return autorepeat_cmd;
1250 for (i = 0; i < num_cmd_fd; i++) {
1251 char *cmd;
1252 int r;
1253 #ifdef HAVE_POSIX_SELECT
1254 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1255 !cmd_fds[i].got_cmd)
1256 continue;
1257 #endif
1258 r = mp_input_read_cmd(&cmd_fds[i], &cmd);
1259 if (r >= 0) {
1260 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1261 free(cmd);
1262 if (ret)
1263 return ret;
1265 else if (r == MP_INPUT_ERROR)
1266 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1267 cmd_fds[i].fd);
1268 else if (r == MP_INPUT_DEAD)
1269 cmd_fds[i].dead = 1;
1272 return NULL;
1277 mp_input_queue_cmd(mp_cmd_t* cmd) {
1278 if(!cmd || cmd_queue_length >= CMD_QUEUE_SIZE)
1279 return 0;
1280 cmd_queue[cmd_queue_end] = cmd;
1281 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1282 cmd_queue_length++;
1283 return 1;
1286 static mp_cmd_t*
1287 mp_input_get_queued_cmd(int peek_only) {
1288 mp_cmd_t* ret;
1290 if(cmd_queue_length == 0)
1291 return NULL;
1293 ret = cmd_queue[cmd_queue_start];
1295 if (!peek_only) {
1296 cmd_queue_length--;
1297 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1300 return ret;
1304 * \param peek_only when set, the returned command stays in the queue.
1305 * Do not free the returned cmd whe you set this!
1307 mp_cmd_t*
1308 mp_input_get_cmd(int time, int paused, int peek_only) {
1309 mp_cmd_t* ret = NULL;
1310 mp_cmd_filter_t* cf;
1311 int from_queue;
1313 while(1) {
1314 from_queue = 1;
1315 ret = mp_input_get_queued_cmd(peek_only);
1316 if(ret) break;
1317 from_queue = 0;
1318 ret = read_events(time, paused);
1319 if (!ret) {
1320 from_queue = 1;
1321 ret = mp_input_get_queued_cmd(peek_only);
1323 break;
1325 if(!ret) return NULL;
1327 for(cf = cmd_filters ; cf ; cf = cf->next) {
1328 if(cf->filter(ret,paused,cf->ctx)) {
1329 if (peek_only && from_queue)
1330 // The filter ate the cmd, so we remove it from queue
1331 ret = mp_input_get_queued_cmd(0);
1332 mp_cmd_free(ret);
1333 return NULL;
1337 if (!from_queue && peek_only)
1338 mp_input_queue_cmd(ret);
1340 return ret;
1343 void
1344 mp_cmd_free(mp_cmd_t* cmd) {
1345 int i;
1346 //#ifdef MP_DEBUG
1347 // assert(cmd != NULL);
1348 //#endif
1349 if ( !cmd ) return;
1351 if(cmd->name)
1352 free(cmd->name);
1354 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1355 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1356 free(cmd->args[i].v.s);
1358 free(cmd);
1361 mp_cmd_t*
1362 mp_cmd_clone(mp_cmd_t* cmd) {
1363 mp_cmd_t* ret;
1364 int i;
1365 #ifdef MP_DEBUG
1366 assert(cmd != NULL);
1367 #endif
1369 ret = malloc(sizeof(mp_cmd_t));
1370 memcpy(ret,cmd,sizeof(mp_cmd_t));
1371 if(cmd->name)
1372 ret->name = strdup(cmd->name);
1373 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1374 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1375 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1378 return ret;
1381 static char key_str[12];
1383 static char*
1384 mp_input_get_key_name(int key) {
1385 int i;
1387 for(i = 0; key_names[i].name != NULL; i++) {
1388 if(key_names[i].key == key)
1389 return key_names[i].name;
1392 if(isascii(key)) {
1393 snprintf(key_str,12,"%c",(char)key);
1394 return key_str;
1397 // Print the hex key code
1398 snprintf(key_str,12,"%#-8x",key);
1399 return key_str;
1404 mp_input_get_key_from_name(const char *name) {
1405 int i,ret = 0,len = strlen(name);
1406 if(len == 1) { // Direct key code
1407 ret = (unsigned char)name[0];
1408 return ret;
1409 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1410 return strtol(name,NULL,16);
1412 for(i = 0; key_names[i].name != NULL; i++) {
1413 if(strcasecmp(key_names[i].name,name) == 0)
1414 return key_names[i].key;
1417 return -1;
1420 static int
1421 mp_input_get_input_from_name(char* name,int* keys) {
1422 char *end,*ptr;
1423 int n=0;
1425 ptr = name;
1426 n = 0;
1427 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1428 if(end && end[1] != '\0') {
1429 if(end[1] == '-')
1430 end = &end[1];
1431 end[0] = '\0';
1433 keys[n] = mp_input_get_key_from_name(ptr);
1434 if(keys[n] < 0) {
1435 return 0;
1437 n++;
1438 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1439 ptr = &end[1];
1440 else
1441 break;
1443 keys[n] = 0;
1444 return 1;
1447 #define BS_MAX 256
1448 #define SPACE_CHAR " \n\r\t"
1450 void
1451 mp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1452 int i = 0,j;
1453 mp_cmd_bind_t* bind = NULL;
1454 mp_cmd_bind_section_t* bind_section = NULL;
1455 char *section=NULL, *p;
1457 #ifdef MP_DEBUG
1458 assert(keys != NULL);
1459 assert(cmd != NULL);
1460 #endif
1462 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1463 *p=0;
1464 section=++cmd;
1465 cmd=++p;
1466 // Jump beginning space
1467 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1468 /* NOTHING */;
1470 bind_section=mp_input_get_bind_section(section);
1472 if(bind_section->cmd_binds) {
1473 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1474 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1475 /* NOTHING */;
1476 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1477 bind = &bind_section->cmd_binds[i];
1478 break;
1483 if(!bind) {
1484 bind_section->cmd_binds = (mp_cmd_bind_t*)realloc(bind_section->cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1485 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1486 bind = &bind_section->cmd_binds[i];
1488 if(bind->cmd)
1489 free(bind->cmd);
1490 bind->cmd = strdup(cmd);
1491 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1494 void
1495 mp_input_add_binds(mp_cmd_bind_t* list) {
1496 int i;
1497 for(i = 0 ; list[i].cmd ; i++)
1498 mp_input_bind_keys(list[i].input,list[i].cmd);
1501 static void
1502 mp_input_free_binds(mp_cmd_bind_t* binds) {
1503 int i;
1505 if(!binds)
1506 return;
1508 for(i = 0; binds[i].cmd != NULL; i++)
1509 free(binds[i].cmd);
1511 free(binds);
1515 static int
1516 mp_input_parse_config(char *file) {
1517 int fd;
1518 int bs = 0,r,eof = 0,comments = 0;
1519 char *iter,*end;
1520 char buffer[BS_MAX];
1521 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1523 fd = open(file,O_RDONLY);
1525 if(fd < 0) {
1526 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1527 return 0;
1530 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1532 while(1) {
1533 if(! eof && bs < BS_MAX-1) {
1534 if(bs > 0) bs--;
1535 r = read(fd,buffer+bs,BS_MAX-1-bs);
1536 if(r < 0) {
1537 if(errno == EINTR)
1538 continue;
1539 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1540 close(fd);
1541 return 0;
1542 } else if(r == 0) {
1543 eof = 1;
1544 } else {
1545 bs += r+1;
1546 buffer[bs-1] = '\0';
1549 // Empty buffer : return
1550 if(bs <= 1) {
1551 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1552 close(fd);
1553 return 1;
1556 iter = buffer;
1558 if(comments) {
1559 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1560 /* NOTHING */;
1561 if(iter[0] == '\0') { // Buffer was full of comment
1562 bs = 0;
1563 continue;
1565 iter++;
1566 r = strlen(iter);
1567 if(r)
1568 memmove(buffer,iter,r+1);
1569 bs = r+1;
1570 if(iter[0] != '#')
1571 comments = 0;
1572 continue;
1575 // Find the wanted key
1576 if(keys[0] == 0) {
1577 // Jump beginning space
1578 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1579 /* NOTHING */;
1580 if(iter[0] == '\0') { // Buffer was full of space char
1581 bs = 0;
1582 continue;
1584 if(iter[0] == '#') { // Comments
1585 comments = 1;
1586 continue;
1588 // Find the end of the key code name
1589 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1590 /*NOTHING */;
1591 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1592 if(buffer == iter) {
1593 if(eof && (buffer-iter) == bs)
1594 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1595 else
1596 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1597 return 0;
1599 memmove(buffer,iter,end-iter);
1600 bs = end-iter;
1601 continue;
1604 char name[end-iter+1];
1605 strncpy(name,iter,end-iter);
1606 name[end-iter] = '\0';
1607 if(! mp_input_get_input_from_name(name,keys)) {
1608 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1609 close(fd);
1610 return 0;
1613 if( bs > (end-buffer))
1614 memmove(buffer,end,bs - (end-buffer));
1615 bs -= end-buffer;
1616 continue;
1617 } else { // Get the command
1618 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1619 // Found new line
1620 if(iter[0] == '\n' || iter[0] == '\r') {
1621 int i;
1622 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey,mp_input_get_key_name(keys[0]));
1623 for(i = 1; keys[i] != 0 ; i++)
1624 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1625 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1626 keys[0] = 0;
1627 if(iter > buffer) {
1628 memmove(buffer,iter,bs- (iter-buffer));
1629 bs -= (iter-buffer);
1631 continue;
1633 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1634 /* NOTHING */;
1635 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1636 if(iter == buffer) {
1637 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1638 close(fd);
1639 return 0;
1641 memmove(buffer,iter,end - iter);
1642 bs = end - iter;
1643 continue;
1646 char cmd[end-iter+1];
1647 strncpy(cmd,iter,end-iter);
1648 cmd[end-iter] = '\0';
1649 //printf("Set bind %d => %s\n",keys[0],cmd);
1650 mp_input_bind_keys(keys,cmd);
1651 n_binds++;
1653 keys[0] = 0;
1654 end++;
1655 if(bs > (end-buffer))
1656 memmove(buffer,end,bs-(end-buffer));
1657 bs -= (end-buffer);
1658 buffer[bs-1] = '\0';
1659 continue;
1662 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1663 close(fd);
1664 mp_input_set_section(NULL);
1665 return 0;
1668 void
1669 mp_input_set_section(char *name) {
1670 mp_cmd_bind_section_t* bind_section = NULL;
1672 cmd_binds=NULL;
1673 cmd_binds_default=NULL;
1674 if(section) free(section);
1675 if(name) section=strdup(name); else section=strdup("default");
1676 if((bind_section=mp_input_get_bind_section(section)))
1677 cmd_binds=bind_section->cmd_binds;
1678 if(strcmp(section,"default")==0) return;
1679 if((bind_section=mp_input_get_bind_section(NULL)))
1680 cmd_binds_default=bind_section->cmd_binds;
1683 char*
1684 mp_input_get_section(void) {
1685 return section;
1688 void
1689 mp_input_init(int use_gui) {
1690 char* file;
1692 #ifdef HAVE_NEW_GUI
1693 if(use_gui)
1694 mp_input_add_binds(gui_def_cmd_binds);
1695 #endif
1697 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1698 if(!file)
1699 return;
1701 if( !mp_input_parse_config(file)) {
1702 // free file if it was allocated by get_path(),
1703 // before it gets overwritten
1704 if( file != config_file)
1706 free(file);
1708 // Try global conf dir
1709 file = MPLAYER_CONFDIR "/input.conf";
1710 if(! mp_input_parse_config(file))
1711 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1713 else
1715 // free file if it was allocated by get_path()
1716 if( file != config_file)
1717 free(file);
1720 #ifdef HAVE_JOYSTICK
1721 if(use_joystick) {
1722 int fd = mp_input_joystick_init(js_dev);
1723 if(fd < 0)
1724 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1725 else
1726 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1728 #endif
1730 #ifdef HAVE_LIRC
1731 if(use_lirc) {
1732 int fd = mp_input_lirc_init();
1733 if(fd > 0)
1734 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1736 #endif
1738 #ifdef HAVE_LIRCC
1739 if(use_lircc) {
1740 int fd = lircc_init("mplayer", NULL);
1741 if(fd >= 0)
1742 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1744 #endif
1746 #ifdef HAVE_APPLE_REMOTE
1747 if(use_ar) {
1748 if(mp_input_ar_init() < 0)
1749 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1750 else
1751 mp_input_add_key_fd(-1,0,mp_input_ar_read,mp_input_ar_close);
1753 #endif
1755 if(in_file) {
1756 struct stat st;
1757 if(stat(in_file,&st))
1758 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,in_file,strerror(errno));
1759 else {
1760 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1761 if(in_file_fd >= 0)
1762 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1763 else
1764 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));
1770 void
1771 mp_input_uninit(void) {
1772 unsigned int i;
1773 mp_cmd_bind_section_t* bind_section;
1775 for(i=0; i < num_key_fd; i++) {
1776 if(key_fds[i].close_func)
1777 key_fds[i].close_func(key_fds[i].fd);
1780 for(i=0; i < num_cmd_fd; i++) {
1781 if(cmd_fds[i].close_func)
1782 cmd_fds[i].close_func(cmd_fds[i].fd);
1784 while (cmd_binds_section) {
1785 mp_input_free_binds(cmd_binds_section->cmd_binds);
1786 free(cmd_binds_section->section);
1787 bind_section=cmd_binds_section->next;
1788 free(cmd_binds_section);
1789 cmd_binds_section=bind_section;
1791 cmd_binds_section=NULL;
1794 void
1795 mp_input_register_options(m_config_t* cfg) {
1796 m_config_register_options(cfg,mp_input_opts);
1799 static int mp_input_print_key_list(m_option_t* cfg) {
1800 int i;
1801 printf("\n");
1802 for(i= 0; key_names[i].name != NULL ; i++)
1803 printf("%s\n",key_names[i].name);
1804 exit(0);
1807 static int mp_input_print_cmd_list(m_option_t* cfg) {
1808 mp_cmd_t *cmd;
1809 int i,j;
1810 const char* type;
1812 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1813 printf("%-20.20s",cmd->name);
1814 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1815 switch(cmd->args[j].type) {
1816 case MP_CMD_ARG_INT:
1817 type = "Integer";
1818 break;
1819 case MP_CMD_ARG_FLOAT:
1820 type = "Float";
1821 break;
1822 case MP_CMD_ARG_STRING:
1823 type = "String";
1824 break;
1825 default:
1826 type = "??";
1828 if(j+1 > cmd->nargs)
1829 printf(" [%s]",type);
1830 else
1831 printf(" %s",type);
1833 printf("\n");
1835 exit(0);
1839 mp_input_check_interrupt(int time) {
1840 mp_cmd_t* cmd;
1841 if((cmd = mp_input_get_cmd(time,0,1)) == NULL)
1842 return 0;
1843 switch(cmd->id) {
1844 case MP_CMD_QUIT:
1845 case MP_CMD_PLAY_TREE_STEP:
1846 case MP_CMD_PLAY_TREE_UP_STEP:
1847 case MP_CMD_PLAY_ALT_SRC_STEP:
1848 // The cmd will be executed when we are back in the main loop
1849 return 1;
1851 // remove the cmd from the queue
1852 cmd = mp_input_get_cmd(time,0,0);
1853 mp_cmd_free(cmd);
1854 return 0;