Some people confuse vidix with kernel drivers, so let's add a note about it
[mplayer/glamo.git] / input / input.c
blob04b7bff576877ab14713e1daf6b31c2fdedfd255
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 "../osdep/getch2.h"
21 #include "../osdep/keycodes.h"
22 #include "../osdep/timer.h"
23 #include "../mp_msg.h"
24 #include "../m_config.h"
25 #include "../m_option.h"
27 #include "joystick.h"
29 #ifdef HAVE_LIRC
30 #include "lirc.h"
31 #endif
33 #ifdef HAVE_LIRCC
34 #include <lirc/lircc.h>
35 #endif
37 /// This array defines all known commands.
38 /// The first field is an id used to recognize the command without too many strcmp.
39 /// The second is obviously the command name.
40 /// The third is the minimum number of arguments this command needs.
41 /// Then comes the definition of each argument, terminated with an arg of type -1.
42 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
43 /// the last one) which is actually 9.
45 /// For the args, the first field is the type (actually int, float or string), the second
46 /// is the default value wich is used for optional arguments
48 static mp_cmd_t mp_cmds[] = {
49 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
50 #ifdef USE_EDL
51 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
52 #endif
53 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
54 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
55 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
56 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
57 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
58 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
59 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
60 { MP_CMD_GRAB_FRAMES, "grab_frames",0, { {-1,{0}} } },
61 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
62 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
63 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
64 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
65 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
66 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
67 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
68 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
69 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
70 { MP_CMD_MUTE, "mute", 0, { {-1,{0}} } },
71 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
72 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
73 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
74 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
75 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
76 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
77 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
78 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
79 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {-1,{0}} } },
80 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
81 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
82 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
83 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
84 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
85 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
86 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
87 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
88 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
89 #ifdef USE_TV
90 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
91 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
92 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
93 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
94 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
95 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
96 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
97 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
98 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
99 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
100 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
101 #endif
102 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {-1,{0}} } },
103 #ifdef HAS_DVBIN_SUPPORT
104 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
105 #endif
106 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
107 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {-1,{0}} } },
108 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {-1,{0}} } },
109 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {-1,{0}} } },
110 { MP_CMD_SCREENSHOT, "screenshot", 0, { {-1,{0}} } },
111 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
112 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
113 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
114 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
115 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
116 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
118 #ifdef HAVE_NEW_GUI
119 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
120 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
121 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
122 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
123 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
124 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
125 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
126 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
127 #endif
129 #ifdef USE_DVDNAV
130 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
131 { MP_CMD_DVDNAV_EVENT, "dvdnav_event", 1, { { MP_CMD_ARG_VOID, {0}}, {-1, {0}} } },
132 #endif
134 #ifdef HAVE_MENU
135 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
136 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
137 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
138 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
139 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
140 #endif
142 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
143 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
145 { 0, NULL, 0, {} }
148 /// The names of the keys as used in input.conf
149 /// If you add some new keys, you also need to add them here
151 static mp_key_name_t key_names[] = {
152 { ' ', "SPACE" },
153 { KEY_ENTER, "ENTER" },
154 { KEY_TAB, "TAB" },
155 { KEY_CTRL, "CTRL" },
156 { KEY_BACKSPACE, "BS" },
157 { KEY_DELETE, "DEL" },
158 { KEY_INSERT, "INS" },
159 { KEY_HOME, "HOME" },
160 { KEY_END, "END" },
161 { KEY_PAGE_UP, "PGUP" },
162 { KEY_PAGE_DOWN, "PGDWN" },
163 { KEY_ESC, "ESC" },
164 { KEY_RIGHT, "RIGHT" },
165 { KEY_LEFT, "LEFT" },
166 { KEY_DOWN, "DOWN" },
167 { KEY_UP, "UP" },
168 { KEY_F+1, "F1" },
169 { KEY_F+2, "F2" },
170 { KEY_F+3, "F3" },
171 { KEY_F+4, "F4" },
172 { KEY_F+5, "F5" },
173 { KEY_F+6, "F6" },
174 { KEY_F+7, "F7" },
175 { KEY_F+8, "F8" },
176 { KEY_F+9, "F9" },
177 { KEY_F+10, "F10" },
178 { KEY_F+11, "F11" },
179 { KEY_F+12, "F12" },
180 { KEY_KP0, "KP0" },
181 { KEY_KP1, "KP1" },
182 { KEY_KP2, "KP2" },
183 { KEY_KP3, "KP3" },
184 { KEY_KP4, "KP4" },
185 { KEY_KP5, "KP5" },
186 { KEY_KP6, "KP6" },
187 { KEY_KP7, "KP7" },
188 { KEY_KP8, "KP8" },
189 { KEY_KP9, "KP9" },
190 { KEY_KPDEL, "KP_DEL" },
191 { KEY_KPDEC, "KP_DEC" },
192 { KEY_KPINS, "KP_INS" },
193 { KEY_KPENTER, "KP_ENTER" },
194 { MOUSE_BTN0, "MOUSE_BTN0" },
195 { MOUSE_BTN1, "MOUSE_BTN1" },
196 { MOUSE_BTN2, "MOUSE_BTN2" },
197 { MOUSE_BTN3, "MOUSE_BTN3" },
198 { MOUSE_BTN4, "MOUSE_BTN4" },
199 { MOUSE_BTN5, "MOUSE_BTN5" },
200 { MOUSE_BTN6, "MOUSE_BTN6" },
201 { MOUSE_BTN7, "MOUSE_BTN7" },
202 { MOUSE_BTN8, "MOUSE_BTN8" },
203 { MOUSE_BTN9, "MOUSE_BTN9" },
204 { JOY_AXIS1_MINUS, "JOY_UP" },
205 { JOY_AXIS1_PLUS, "JOY_DOWN" },
206 { JOY_AXIS0_MINUS, "JOY_LEFT" },
207 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
209 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
210 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
211 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
212 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
213 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
214 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
215 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
216 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
217 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
218 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
219 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
220 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
221 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
222 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
223 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
224 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
225 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
226 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
227 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
228 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
230 { JOY_BTN0, "JOY_BTN0" },
231 { JOY_BTN1, "JOY_BTN1" },
232 { JOY_BTN2, "JOY_BTN2" },
233 { JOY_BTN3, "JOY_BTN3" },
234 { JOY_BTN4, "JOY_BTN4" },
235 { JOY_BTN5, "JOY_BTN5" },
236 { JOY_BTN6, "JOY_BTN6" },
237 { JOY_BTN7, "JOY_BTN7" },
238 { JOY_BTN8, "JOY_BTN8" },
239 { JOY_BTN9, "JOY_BTN9" },
241 { KEY_XF86_PAUSE, "XF86_PAUSE" },
242 { KEY_XF86_STOP, "XF86_STOP" },
243 { KEY_XF86_PREV, "XF86_PREV" },
244 { KEY_XF86_NEXT, "XF86_NEXT" },
246 { 0, NULL }
249 // This is the default binding. The content of input.conf overrides these.
250 // The first arg is a null terminated array of key codes.
251 // The second is the command
253 static mp_cmd_bind_t def_cmd_binds[] = {
255 { { MOUSE_BTN3, 0 }, "seek 10" },
256 { { MOUSE_BTN4, 0 }, "seek -10" },
257 { { MOUSE_BTN5, 0 }, "volume 1" },
258 { { MOUSE_BTN6, 0 }, "volume -1" },
260 #ifdef USE_DVDNAV
261 { { 'K', 0 }, "dvdnav 1" }, // up
262 { { 'J', 0 }, "dvdnav 2" }, // down
263 { { 'H', 0 }, "dvdnav 3" }, // left
264 { { 'L', 0 }, "dvdnav 4" }, // right
265 { { 'M', 0 }, "dvdnav 5" }, // menu
266 { { 'S', 0 }, "dvdnav 6" }, // select
267 #endif
269 { { KEY_RIGHT, 0 }, "seek 10" },
270 { { KEY_LEFT, 0 }, "seek -10" },
271 { { KEY_UP, 0 }, "seek 60" },
272 { { KEY_DOWN, 0 }, "seek -60" },
273 { { KEY_PAGE_UP, 0 }, "seek 600" },
274 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
275 { { '-', 0 }, "audio_delay 0.100" },
276 { { '+', 0 }, "audio_delay -0.100" },
277 { { '[', 0 }, "speed_mult 0.9091" },
278 { { ']', 0 }, "speed_mult 1.1" },
279 { { '{', 0 }, "speed_mult 0.5" },
280 { { '}', 0 }, "speed_mult 2.0" },
281 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
282 { { 'q', 0 }, "quit" },
283 { { KEY_ESC, 0 }, "quit" },
284 #ifndef HAVE_NEW_GUI
285 { { 'p', 0 }, "pause" },
286 #endif
287 { { ' ', 0 }, "pause" },
288 { { '.', 0 }, "frame_step" },
289 { { KEY_HOME, 0 }, "pt_up_step 1" },
290 { { KEY_END, 0 }, "pt_up_step -1" },
291 { { '>', 0 }, "pt_step 1" },
292 #ifndef HAVE_NEW_GUI
293 { { KEY_ENTER, 0 }, "pt_step 1 1" },
294 #endif
295 { { '<', 0 }, "pt_step -1" },
296 { { KEY_INS, 0 }, "alt_src_step 1" },
297 { { KEY_DEL, 0 }, "alt_src_step -1" },
298 { { 'o', 0 }, "osd" },
299 { { 'z', 0 }, "sub_delay -0.1" },
300 { { 'x', 0 }, "sub_delay +0.1" },
301 { { 'g', 0 }, "sub_step -1" },
302 { { 'y', 0 }, "sub_step +1" },
303 { { '9', 0 }, "volume -1" },
304 { { '/', 0 }, "volume -1" },
305 { { '0', 0 }, "volume 1" },
306 { { '*', 0 }, "volume 1" },
307 { { 'm', 0 }, "mute" },
308 { { '1', 0 }, "contrast -1" },
309 { { '2', 0 }, "contrast 1" },
310 { { '3', 0 }, "brightness -1" },
311 { { '4', 0 }, "brightness 1" },
312 { { '5', 0 }, "hue -1" },
313 { { '6', 0 }, "hue 1" },
314 { { '7', 0 }, "saturation -1" },
315 { { '8', 0 }, "saturation 1" },
316 { { 'd', 0 }, "frame_drop" },
317 { { 'r', 0 }, "sub_pos -1" },
318 { { 't', 0 }, "sub_pos +1" },
319 { { 'a', 0 }, "sub_alignment" },
320 { { 'v', 0 }, "sub_visibility" },
321 { { 'b', 0 }, "sub_select" },
322 { { 'j', 0 }, "vobsub_lang" },
323 { { 'F', 0 }, "forced_subs_only" },
324 { { '#', 0 }, "switch_audio" },
325 #ifdef USE_EDL
326 { { 'i', 0 }, "edl_mark" },
327 #endif
328 #ifdef USE_TV
329 { { 'h', 0 }, "tv_step_channel 1" },
330 { { 'k', 0 }, "tv_step_channel -1" },
331 { { 'n', 0 }, "tv_step_norm" },
332 { { 'u', 0 }, "tv_step_chanlist" },
333 #endif
334 #ifdef HAVE_NEW_GUI
335 { { 'l', 0 }, "gui_loadfile" },
336 { { 't', 0 }, "gui_loadsubtitle" },
337 { { KEY_ENTER, 0 }, "gui_play" },
338 { { 's', 0 }, "gui_stop" },
339 { { 'p', 0 }, "gui_playlist" },
340 { { 'r', 0 }, "gui_preferences" },
341 { { 'c', 0 }, "gui_skinbrowser" },
342 #endif
343 #ifdef HAVE_JOYSTICK
344 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
345 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
346 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
347 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
348 { { JOY_BTN0, 0 }, "pause" },
349 { { JOY_BTN1, 0 }, "osd" },
350 { { JOY_BTN2, 0 }, "volume 1"},
351 { { JOY_BTN3, 0 }, "volume -1"},
352 #endif
353 { { 'T', 0 }, "vo_ontop" },
354 { { 'f', 0 }, "vo_fullscreen" },
355 { { 's', 0 }, "screenshot" },
356 { { 'w', 0 }, "panscan -0.1" },
357 { { 'e', 0 }, "panscan +0.1" },
359 { { KEY_XF86_PAUSE, 0 }, "pause" },
360 { { KEY_XF86_STOP, 0 }, "quit" },
361 { { KEY_XF86_PREV, 0 }, "seek -60" },
362 { { KEY_XF86_NEXT, 0 }, "seek +60" },
364 { { 0 }, NULL }
367 #ifndef MP_MAX_KEY_FD
368 #define MP_MAX_KEY_FD 10
369 #endif
371 #ifndef MP_MAX_CMD_FD
372 #define MP_MAX_CMD_FD 10
373 #endif
375 #define MP_FD_EOF (1<<0)
376 #define MP_FD_DROP (1<<1)
377 #define MP_FD_DEAD (1<<2)
378 #define MP_FD_GOT_CMD (1<<3)
379 #define MP_FD_NO_SELECT (1<<4)
381 #define CMD_QUEUE_SIZE 100
383 typedef struct mp_input_fd {
384 int fd;
385 void* read_func;
386 mp_close_func_t close_func;
387 int flags;
388 // These fields are for the cmd fds.
389 char* buffer;
390 int pos,size;
391 } mp_input_fd_t;
393 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
395 struct mp_cmd_filter_st {
396 mp_input_cmd_filter filter;
397 void* ctx;
398 mp_cmd_filter_t* next;
401 // These are the user defined binds
402 static mp_cmd_bind_t* cmd_binds = NULL;
403 static mp_cmd_filter_t* cmd_filters = NULL;
405 // Callback to allow the menu filter to grab the incoming keys
406 void (*mp_input_key_cb)(int code) = NULL;
408 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
409 static unsigned int num_key_fd = 0;
410 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
411 static unsigned int num_cmd_fd = 0;
412 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
413 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
415 // this is the key currently down
416 static int key_down[MP_MAX_KEY_DOWN];
417 static unsigned int num_key_down = 0, last_key_down = 0;
419 // Autorepeat stuff
420 static short ar_state = -1;
421 static mp_cmd_t* ar_cmd = NULL;
422 static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;
424 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
425 static char* config_file = "input.conf";
427 static char* js_dev = NULL;
429 static char* in_file = NULL;
430 static int in_file_fd = -1;
432 static int mp_input_print_key_list(m_option_t* cfg);
433 static int mp_input_print_cmd_list(m_option_t* cfg);
435 // Our command line options
436 static m_option_t input_conf[] = {
437 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
438 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
439 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
440 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
441 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
442 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
443 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
444 { NULL, NULL, 0, 0, 0, 0, NULL}
447 static m_option_t mp_input_opts[] = {
448 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
449 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
450 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
451 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
452 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
453 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
454 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
455 { NULL, NULL, 0, 0, 0, 0, NULL}
458 static int
459 mp_input_default_key_func(int fd);
461 static int
462 mp_input_default_cmd_func(int fd,char* buf, int l);
464 static char*
465 mp_input_get_key_name(int key);
469 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
470 if(num_cmd_fd == MP_MAX_CMD_FD) {
471 mp_msg(MSGT_INPUT,MSGL_ERR,"Too many command fds, unable to register fd %d.\n",fd);
472 return 0;
475 memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t));
476 cmd_fds[num_cmd_fd].fd = fd;
477 cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func;
478 cmd_fds[num_cmd_fd].close_func = close_func;
479 if(!select)
480 cmd_fds[num_cmd_fd].flags = MP_FD_NO_SELECT;
481 num_cmd_fd++;
483 return 1;
486 void
487 mp_input_rm_cmd_fd(int fd) {
488 unsigned int i;
490 for(i = 0; i < num_cmd_fd; i++) {
491 if(cmd_fds[i].fd == fd)
492 break;
494 if(i == num_cmd_fd)
495 return;
496 if(cmd_fds[i].close_func)
497 cmd_fds[i].close_func(cmd_fds[i].fd);
498 if(cmd_fds[i].buffer)
499 free(cmd_fds[i].buffer);
501 if(i + 1 < num_cmd_fd)
502 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
503 num_cmd_fd--;
506 void
507 mp_input_rm_key_fd(int fd) {
508 unsigned int i;
510 for(i = 0; i < num_key_fd; i++) {
511 if(key_fds[i].fd == fd)
512 break;
514 if(i == num_key_fd)
515 return;
516 if(key_fds[i].close_func)
517 key_fds[i].close_func(key_fds[i].fd);
519 if(i + 1 < num_key_fd)
520 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
521 num_key_fd--;
525 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) {
526 if(num_key_fd == MP_MAX_KEY_FD) {
527 mp_msg(MSGT_INPUT,MSGL_ERR,"Too many key fds, unable to register fd %d.\n",fd);
528 return 0;
531 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
532 key_fds[num_key_fd].fd = fd;
533 key_fds[num_key_fd].read_func = read_func ? read_func : mp_input_default_key_func;
534 key_fds[num_key_fd].close_func = close_func;
535 if(!select)
536 key_fds[num_key_fd].flags |= MP_FD_NO_SELECT;
537 num_key_fd++;
539 return 1;
544 mp_cmd_t*
545 mp_input_parse_cmd(char* str) {
546 int i,l;
547 int pausing = 0;
548 char *ptr,*e;
549 mp_cmd_t *cmd, *cmd_def;
551 #ifdef MP_DEBUG
552 assert(str != NULL);
553 #endif
555 if (strncmp(str, "pausing ", 8) == 0) {
556 pausing = 1;
557 str = &str[8];
560 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
561 /* NOTHING */;
562 if(ptr[0] != '\0')
563 l = ptr-str;
564 else
565 l = strlen(str);
567 if(l == 0)
568 return NULL;
570 for(i=0; mp_cmds[i].name != NULL; i++) {
571 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
572 break;
575 if(mp_cmds[i].name == NULL)
576 return NULL;
578 cmd_def = &mp_cmds[i];
580 cmd = (mp_cmd_t*)malloc(sizeof(mp_cmd_t));
581 cmd->id = cmd_def->id;
582 cmd->name = strdup(cmd_def->name);
583 cmd->pausing = pausing;
585 ptr = str;
587 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
588 ptr = strchr(ptr,' ');
589 if(!ptr) break;
590 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
591 if(ptr[0] == '\0') break;
592 cmd->args[i].type = cmd_def->args[i].type;
593 switch(cmd_def->args[i].type) {
594 case MP_CMD_ARG_INT:
595 errno = 0;
596 cmd->args[i].v.i = atoi(ptr);
597 if(errno != 0) {
598 mp_msg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
599 ptr = NULL;
601 break;
602 case MP_CMD_ARG_FLOAT:
603 errno = 0;
604 cmd->args[i].v.f = atof(ptr);
605 if(errno != 0) {
606 mp_msg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
607 ptr = NULL;
609 break;
610 case MP_CMD_ARG_STRING: {
611 char term;
612 char* ptr2 = ptr, *start;
614 if(ptr[0] == '\'' || ptr[0] == '"') {
615 term = ptr[0];
616 ptr2++;
617 } else
618 term = ' ';
619 start = ptr2;
620 while(1) {
621 e = strchr(ptr2,term);
622 if(!e) break;
623 if(e <= ptr2 || *(e - 1) != '\\') break;
624 ptr2 = e + 1;
627 if(term != ' ' && (!e || e[0] == '\0')) {
628 mp_msg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",cmd_def->name,i+1);
629 ptr = NULL;
630 break;
631 } else if(!e) e = ptr+strlen(ptr);
632 l = e-start;
633 ptr2 = start;
634 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
635 memmove(e,e+1,strlen(e));
636 ptr2 = e + 1;
637 l--;
639 cmd->args[i].v.s = (char*)malloc((l+1)*sizeof(char));
640 strncpy(cmd->args[i].v.s,start,l);
641 cmd->args[i].v.s[l] = '\0';
642 if(term != ' ') ptr += l+2;
643 } break;
644 case -1:
645 ptr = NULL;
646 break;
647 default :
648 mp_msg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
651 cmd->nargs = i;
653 if(cmd_def->nargs > cmd->nargs) {
654 mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str);
655 mp_msg(MSGT_INPUT,MSGL_ERR,"command %s requires at least %d arguments, we found only %d so far.\n",cmd_def->name,cmd_def->nargs,cmd->nargs);
656 mp_cmd_free(cmd);
657 return NULL;
660 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
661 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
662 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
663 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
666 if(i < MP_CMD_MAX_ARGS)
667 cmd->args[i].type = -1;
669 return cmd;
672 static int
673 mp_input_default_key_func(int fd) {
674 int r,code=0;
675 unsigned int l;
676 l = 0;
677 while(l < sizeof(int)) {
678 r = read(fd,((char *)&code)+l,sizeof(int)-l);
679 if(r <= 0)
680 break;
681 l +=r;
683 return code;
686 #define MP_CMD_MAX_SIZE 256
688 static int
689 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
690 char* end;
691 (*ret) = NULL;
693 // Allocate the buffer if it doesn't exist
694 if(!mp_fd->buffer) {
695 mp_fd->buffer = (char*)malloc(MP_CMD_MAX_SIZE*sizeof(char));
696 mp_fd->pos = 0;
697 mp_fd->size = MP_CMD_MAX_SIZE;
700 // Get some data if needed/possible
701 while( !(mp_fd->flags & MP_FD_GOT_CMD) && !(mp_fd->flags & MP_FD_EOF) && (mp_fd->size - mp_fd->pos > 1) ) {
702 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);
703 // Error ?
704 if(r < 0) {
705 switch(r) {
706 case MP_INPUT_ERROR:
707 case MP_INPUT_DEAD:
708 mp_msg(MSGT_INPUT,MSGL_ERR,"Error while reading cmd fd %d: %s\n",mp_fd->fd,strerror(errno));
709 case MP_INPUT_NOTHING:
710 return r;
712 // EOF ?
713 } else if(r == 0) {
714 mp_fd->flags |= MP_FD_EOF;
715 break;
717 mp_fd->pos += r;
718 break;
721 // Reset the got_cmd flag
722 mp_fd->flags &= ~MP_FD_GOT_CMD;
724 while(1) {
725 int l = 0;
726 // Find the cmd end
727 mp_fd->buffer[mp_fd->pos] = '\0';
728 end = strchr(mp_fd->buffer,'\n');
729 // No cmd end ?
730 if(!end) {
731 // If buffer is full we must drop all until the next \n
732 if(mp_fd->size - mp_fd->pos <= 1) {
733 mp_msg(MSGT_INPUT,MSGL_ERR,"Cmd buffer of fd %d is full: dropping content\n",mp_fd->fd);
734 mp_fd->pos = 0;
735 mp_fd->flags |= MP_FD_DROP;
737 break;
739 // We already have a cmd : set the got_cmd flag
740 else if((*ret)) {
741 mp_fd->flags |= MP_FD_GOT_CMD;
742 break;
745 l = end - mp_fd->buffer;
747 // Not dropping : put the cmd in ret
748 if( ! (mp_fd->flags & MP_FD_DROP)) {
749 (*ret) = (char*)malloc((l+1)*sizeof(char));
750 strncpy((*ret),mp_fd->buffer,l);
751 (*ret)[l] = '\0';
752 } else { // Remove the dropping flag
753 mp_fd->flags &= ~MP_FD_DROP;
755 if( mp_fd->pos - (l+1) > 0)
756 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
757 mp_fd->pos -= l+1;
760 if(*ret)
761 return 1;
762 else
763 return MP_INPUT_NOTHING;
766 static int
767 mp_input_default_cmd_func(int fd,char* buf, int l) {
769 while(1) {
770 int r = read(fd,buf,l);
771 // Error ?
772 if(r < 0) {
773 if(errno == EINTR)
774 continue;
775 else if(errno == EAGAIN)
776 return MP_INPUT_NOTHING;
777 return MP_INPUT_ERROR;
778 // EOF ?
780 return r;
786 void
787 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
788 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
790 filter->filter = func;
791 filter->ctx = ctx;
792 filter->next = cmd_filters;
793 cmd_filters = filter;
797 static char*
798 mp_input_find_bind_for_key(mp_cmd_bind_t* binds, int n,int* keys) {
799 int j;
801 for(j = 0; binds[j].cmd != NULL; j++) {
802 if(n > 0) {
803 int found = 1,s;
804 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
805 if(binds[j].input[s] != keys[s]) {
806 found = 0;
807 break;
810 if(found && binds[j].input[s] == 0 && s == n)
811 break;
812 else
813 continue;
814 } else if(n == 1){
815 if(binds[j].input[0] == keys[0] && binds[j].input[1] == 0)
816 break;
819 return binds[j].cmd;
822 static mp_cmd_t*
823 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
824 char* cmd = NULL;
825 mp_cmd_t* ret;
827 if(cmd_binds)
828 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
829 if(cmd == NULL)
830 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
832 if(cmd == NULL) {
833 mp_msg(MSGT_INPUT,MSGL_WARN,"No bind found for key %s",mp_input_get_key_name(keys[0]));
834 if(n > 1) {
835 int s;
836 for(s=1; s < n; s++)
837 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
839 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
840 return NULL;
842 ret = mp_input_parse_cmd(cmd);
843 if(!ret) {
844 mp_msg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s",mp_input_get_key_name(key_down[0]));
845 if( num_key_down > 1) {
846 unsigned int s;
847 for(s=1; s < num_key_down; s++)
848 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(key_down[s]));
850 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
852 return ret;
856 mp_input_read_key_code(int time) {
857 #ifndef HAVE_NO_POSIX_SELECT
858 fd_set fds;
859 struct timeval tv,*time_val;
860 #endif
861 int i,n=0,max_fd = 0;
862 static int last_loop = 0;
864 if(num_key_fd == 0)
865 return MP_INPUT_NOTHING;
867 #ifndef HAVE_NO_POSIX_SELECT
868 FD_ZERO(&fds);
869 #endif
870 // Remove fd marked as dead and build the fd_set
871 // n == number of fd's to be select() checked
872 for(i = 0; (unsigned int)i < num_key_fd; i++) {
873 if( (key_fds[i].flags & MP_FD_DEAD) ) {
874 mp_input_rm_key_fd(key_fds[i].fd);
875 i--;
876 continue;
877 } else if(key_fds[i].flags & MP_FD_NO_SELECT)
878 continue;
879 if(key_fds[i].fd > max_fd)
880 max_fd = key_fds[i].fd;
881 #ifndef HAVE_NO_POSIX_SELECT
882 FD_SET(key_fds[i].fd,&fds);
883 #endif
884 n++;
887 if(num_key_fd == 0)
888 return MP_INPUT_NOTHING;
890 #ifndef HAVE_NO_POSIX_SELECT
891 // if we have fd's without MP_FD_NO_SELECT flag, call select():
892 if(n>0){
894 if(time >= 0 ) {
895 tv.tv_sec=time/1000;
896 tv.tv_usec = (time%1000)*1000;
897 time_val = &tv;
898 } else
899 time_val = NULL;
901 while(1) {
902 if(select(max_fd+1,&fds,NULL,NULL,time_val) < 0) {
903 if(errno == EINTR)
904 continue;
905 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error: %s\n",strerror(errno));
907 break;
911 #endif
913 for(i = last_loop + 1 ; i != last_loop ; i++) {
914 int code = -1;
915 // This is to check all fds in turn
916 if((unsigned int)i >= num_key_fd) {
917 i = -1;
918 last_loop++;
919 last_loop %= (num_key_fd+1);
920 continue;
922 #ifndef HAVE_NO_POSIX_SELECT
923 // No input from this fd
924 if(! (key_fds[i].flags & MP_FD_NO_SELECT) && ! FD_ISSET(key_fds[i].fd,&fds) && key_fds[i].fd != 0)
925 continue;
926 #endif
927 if(key_fds[i].fd == 0) { // stdin is handled by getch2
928 code = getch2(time);
929 if(code < 0)
930 code = MP_INPUT_NOTHING;
932 else
933 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
935 if(code >= 0)
936 return code;
938 if(code == MP_INPUT_ERROR)
939 mp_msg(MSGT_INPUT,MSGL_ERR,"Error on key input fd %d\n",key_fds[i].fd);
940 else if(code == MP_INPUT_DEAD) {
941 mp_msg(MSGT_INPUT,MSGL_ERR,"Dead key input on fd %d\n",key_fds[i].fd);
942 key_fds[i].flags |= MP_FD_DEAD;
945 return MP_INPUT_NOTHING;
949 static mp_cmd_t*
950 mp_input_read_keys(int time,int paused) {
951 int code = mp_input_read_key_code(time);
952 unsigned int j;
953 mp_cmd_t* ret;
955 if(mp_input_key_cb) {
956 for( ; code >= 0 ; code = mp_input_read_key_code(0) ) {
957 if(code & MP_KEY_DOWN) continue;
958 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
959 mp_input_key_cb(code);
961 return NULL;
964 for( ; code >= 0 ; code = mp_input_read_key_code(0) ) {
965 // key pushed
966 if(code & MP_KEY_DOWN) {
967 if(num_key_down > MP_MAX_KEY_DOWN) {
968 mp_msg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
969 continue;
971 code &= ~MP_KEY_DOWN;
972 // Check if we don't already have this key as pushed
973 for(j = 0; j < num_key_down; j++) {
974 if(key_down[j] == code)
975 break;
977 if(j != num_key_down)
978 continue;
979 key_down[num_key_down] = code;
980 num_key_down++;
981 last_key_down = GetTimer();
982 ar_state = 0;
983 continue;
985 // key released
986 // Check if the key is in the down key, driver which can't send push event
987 // send only release event
988 for(j = 0; j < num_key_down; j++) {
989 if(key_down[j] == code)
990 break;
992 if(j == num_key_down) { // key was not in the down keys : add it
993 if(num_key_down > MP_MAX_KEY_DOWN) {
994 mp_msg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n");
995 continue;
997 key_down[num_key_down] = code;
998 num_key_down++;
999 last_key_down = 1;
1001 // We ignore key from last combination
1002 ret = last_key_down ? mp_input_get_cmd_from_keys(num_key_down,key_down,paused) : NULL;
1003 // Remove the key
1004 if(j+1 < num_key_down)
1005 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1006 num_key_down--;
1007 last_key_down = 0;
1008 ar_state = -1;
1009 if(ar_cmd) {
1010 mp_cmd_free(ar_cmd);
1011 ar_cmd = NULL;
1013 if(ret)
1014 return ret;
1017 // No input : autorepeat ?
1018 if(ar_rate > 0 && ar_state >=0 && num_key_down > 0 && ! (key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1019 unsigned int t = GetTimer();
1020 // First time : wait delay
1021 if(ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1022 ar_cmd = mp_input_get_cmd_from_keys(num_key_down,key_down,paused);
1023 if(!ar_cmd) {
1024 ar_state = -1;
1025 return NULL;
1027 ar_state = 1;
1028 last_ar = t;
1029 return mp_cmd_clone(ar_cmd);
1030 // Then send rate / sec event
1031 } else if(ar_state == 1 && (t -last_ar) >= 1000000/ar_rate) {
1032 last_ar = t;
1033 return mp_cmd_clone(ar_cmd);
1037 return NULL;
1040 static mp_cmd_t*
1041 mp_input_read_cmds(int time) {
1042 #ifndef HAVE_NO_POSIX_SELECT
1043 fd_set fds;
1044 struct timeval tv,*time_val;
1045 #endif
1046 int i,n = 0,max_fd = 0,got_cmd = 0;
1047 mp_cmd_t* ret;
1048 static int last_loop = 0;
1050 if(num_cmd_fd == 0)
1051 return NULL;
1053 #ifndef HAVE_NO_POSIX_SELECT
1054 FD_ZERO(&fds);
1055 #endif
1056 for(i = 0; (unsigned int)i < num_cmd_fd ; i++) {
1057 if( (cmd_fds[i].flags & MP_FD_DEAD) || (cmd_fds[i].flags & MP_FD_EOF) ) {
1058 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1059 i--;
1060 continue;
1061 } else if(cmd_fds[i].flags & MP_FD_NO_SELECT)
1062 continue;
1063 if(cmd_fds[i].flags & MP_FD_GOT_CMD)
1064 got_cmd = 1;
1065 if(cmd_fds[i].fd > max_fd)
1066 max_fd = cmd_fds[i].fd;
1067 #ifndef HAVE_NO_POSIX_SELECT
1068 FD_SET(cmd_fds[i].fd,&fds);
1069 #endif
1070 n++;
1073 if(num_cmd_fd == 0)
1074 return NULL;
1076 #ifndef HAVE_NO_POSIX_SELECT
1077 if(time >= 0) {
1078 tv.tv_sec=time/1000;
1079 tv.tv_usec = (time%1000)*1000;
1080 time_val = &tv;
1081 } else
1082 time_val = NULL;
1084 while(n > 0) {
1085 if((i = select(max_fd+1,&fds,NULL,NULL,time_val)) <= 0) {
1086 if(i < 0) {
1087 if(errno == EINTR)
1088 continue;
1089 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error: %s\n",strerror(errno));
1091 if(!got_cmd)
1092 return NULL;
1094 break;
1096 #endif
1098 for(i = last_loop + 1; i != last_loop ; i++) {
1099 int r = 0;
1100 char* cmd;
1101 if((unsigned int)i >= num_cmd_fd) {
1102 i = -1;
1103 last_loop++;
1104 last_loop %= (num_cmd_fd+1);
1105 continue;
1107 #ifndef HAVE_NO_POSIX_SELECT
1108 if( ! (cmd_fds[i].flags & MP_FD_NO_SELECT) && ! FD_ISSET(cmd_fds[i].fd,&fds) && ! (cmd_fds[i].flags & MP_FD_GOT_CMD) )
1109 continue;
1110 #endif
1112 r = mp_input_read_cmd(&cmd_fds[i],&cmd);
1113 if(r < 0) {
1114 if(r == MP_INPUT_ERROR)
1115 mp_msg(MSGT_INPUT,MSGL_ERR,"Error on cmd fd %d\n",cmd_fds[i].fd);
1116 else if(r == MP_INPUT_DEAD)
1117 cmd_fds[i].flags |= MP_FD_DEAD;
1118 continue;
1120 ret = mp_input_parse_cmd(cmd);
1121 free(cmd);
1122 if(!ret)
1123 continue;
1124 last_loop = i;
1125 return ret;
1128 last_loop = 0;
1129 return NULL;
1133 mp_input_queue_cmd(mp_cmd_t* cmd) {
1134 if(cmd_queue_length >= CMD_QUEUE_SIZE)
1135 return 0;
1136 cmd_queue[cmd_queue_end] = cmd;
1137 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1138 cmd_queue_length++;
1139 return 1;
1142 static mp_cmd_t*
1143 mp_input_get_queued_cmd(int peek_only) {
1144 mp_cmd_t* ret;
1146 if(cmd_queue_length == 0)
1147 return NULL;
1149 ret = cmd_queue[cmd_queue_start];
1151 if (!peek_only) {
1152 cmd_queue_length--;
1153 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1156 return ret;
1160 * \param peek_only when set, the returned command stays in the queue.
1161 * Do not free the returned cmd whe you set this!
1163 mp_cmd_t*
1164 mp_input_get_cmd(int time, int paused, int peek_only) {
1165 mp_cmd_t* ret = NULL;
1166 mp_cmd_filter_t* cf;
1167 int from_queue;
1169 while(1) {
1170 from_queue = 1;
1171 ret = mp_input_get_queued_cmd(peek_only);
1172 if(ret) break;
1173 from_queue = 0;
1174 ret = mp_input_read_keys(time,paused);
1175 if(ret) break;
1176 ret = mp_input_read_cmds(time);
1177 break;
1179 if(!ret) return NULL;
1181 for(cf = cmd_filters ; cf ; cf = cf->next) {
1182 if(cf->filter(ret,paused,cf->ctx))
1183 return NULL;
1186 if (!from_queue && peek_only)
1187 mp_input_queue_cmd(ret);
1189 return ret;
1192 void
1193 mp_cmd_free(mp_cmd_t* cmd) {
1194 int i;
1195 //#ifdef MP_DEBUG
1196 // assert(cmd != NULL);
1197 //#endif
1198 if ( !cmd ) return;
1200 if(cmd->name)
1201 free(cmd->name);
1203 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1204 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1205 free(cmd->args[i].v.s);
1207 free(cmd);
1210 mp_cmd_t*
1211 mp_cmd_clone(mp_cmd_t* cmd) {
1212 mp_cmd_t* ret;
1213 int i;
1214 #ifdef MP_DEBUG
1215 assert(cmd != NULL);
1216 #endif
1218 ret = (mp_cmd_t*)malloc(sizeof(mp_cmd_t));
1219 memcpy(ret,cmd,sizeof(mp_cmd_t));
1220 if(cmd->name)
1221 ret->name = strdup(cmd->name);
1222 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1223 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1224 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1227 return ret;
1230 static char key_str[12];
1232 static char*
1233 mp_input_get_key_name(int key) {
1234 int i;
1236 for(i = 0; key_names[i].name != NULL; i++) {
1237 if(key_names[i].key == key)
1238 return key_names[i].name;
1241 if(isascii(key)) {
1242 snprintf(key_str,12,"%c",(char)key);
1243 return key_str;
1246 // Print the hex key code
1247 snprintf(key_str,12,"%#-8x",key);
1248 return key_str;
1252 static int
1253 mp_input_get_key_from_name(char* name) {
1254 int i,ret = 0,len = strlen(name);
1255 if(len == 1) { // Direct key code
1256 ret = (unsigned char)name[0];
1257 return ret;
1258 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1259 return strtol(name,NULL,16);
1261 for(i = 0; key_names[i].name != NULL; i++) {
1262 if(strcasecmp(key_names[i].name,name) == 0)
1263 return key_names[i].key;
1266 return -1;
1269 static int
1270 mp_input_get_input_from_name(char* name,int* keys) {
1271 char *end,*ptr;
1272 int n=0;
1274 ptr = name;
1275 n = 0;
1276 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1277 if(end && end[1] != '\0') {
1278 if(end[1] == '-')
1279 end = &end[1];
1280 end[0] = '\0';
1282 keys[n] = mp_input_get_key_from_name(ptr);
1283 if(keys[n] < 0) {
1284 return 0;
1286 n++;
1287 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1288 ptr = &end[1];
1289 else
1290 break;
1292 keys[n] = 0;
1293 return 1;
1296 void
1297 mp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1298 int i = 0,j;
1299 mp_cmd_bind_t* bind = NULL;
1301 #ifdef MP_DEBUG
1302 assert(keys != NULL);
1303 assert(cmd != NULL);
1304 #endif
1306 if(cmd_binds) {
1307 for(i = 0; cmd_binds[i].cmd != NULL ; i++) {
1308 for(j = 0 ; cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1309 /* NOTHING */;
1310 if(keys[j] == 0 && cmd_binds[i].input[j] == 0 ) {
1311 bind = &cmd_binds[i];
1312 break;
1317 if(!bind) {
1318 cmd_binds = (mp_cmd_bind_t*)realloc(cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1319 memset(&cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1320 bind = &cmd_binds[i];
1322 if(bind->cmd)
1323 free(bind->cmd);
1324 bind->cmd = strdup(cmd);
1325 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1329 static void
1330 mp_input_free_binds(mp_cmd_bind_t* binds) {
1331 int i;
1333 if(!binds)
1334 return;
1336 for(i = 0; binds[i].cmd != NULL; i++)
1337 free(binds[i].cmd);
1339 free(binds);
1344 #define BS_MAX 256
1345 #define SPACE_CHAR " \n\r\t"
1347 static int
1348 mp_input_parse_config(char *file) {
1349 int fd;
1350 int bs = 0,r,eof = 0,comments = 0;
1351 char *iter,*end;
1352 char buffer[BS_MAX];
1353 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1354 mp_cmd_bind_t* binds = NULL;
1356 fd = open(file,O_RDONLY);
1358 if(fd < 0) {
1359 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1360 return 0;
1363 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1365 while(1) {
1366 if(! eof && bs < BS_MAX-1) {
1367 if(bs > 0) bs--;
1368 r = read(fd,buffer+bs,BS_MAX-1-bs);
1369 if(r < 0) {
1370 if(errno == EINTR)
1371 continue;
1372 mp_msg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno));
1373 mp_input_free_binds(binds);
1374 close(fd);
1375 return 0;
1376 } else if(r == 0) {
1377 eof = 1;
1378 } else {
1379 bs += r+1;
1380 buffer[bs-1] = '\0';
1383 // Empty buffer : return
1384 if(bs <= 1) {
1385 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1386 if(binds)
1387 cmd_binds = binds;
1388 close(fd);
1389 return 1;
1392 iter = buffer;
1394 if(comments) {
1395 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1396 /* NOTHING */;
1397 if(iter[0] == '\0') { // Buffer was full of comment
1398 bs = 0;
1399 continue;
1401 iter++;
1402 r = strlen(iter);
1403 if(r)
1404 memmove(buffer,iter,r+1);
1405 bs = r+1;
1406 if(iter[0] != '#')
1407 comments = 0;
1408 continue;
1411 // Find the wanted key
1412 if(keys[0] == 0) {
1413 // Jump beginning space
1414 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1415 /* NOTHING */;
1416 if(iter[0] == '\0') { // Buffer was full of space char
1417 bs = 0;
1418 continue;
1420 if(iter[0] == '#') { // Comments
1421 comments = 1;
1422 continue;
1424 // Find the end of the key code name
1425 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1426 /*NOTHING */;
1427 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1428 if(buffer == iter) {
1429 if(eof && (buffer-iter) == bs)
1430 mp_msg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter);
1431 else
1432 mp_msg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1433 mp_input_free_binds(binds);
1434 return 0;
1436 memmove(buffer,iter,end-iter);
1437 bs = end-iter;
1438 continue;
1441 char name[end-iter+1];
1442 strncpy(name,iter,end-iter);
1443 name[end-iter] = '\0';
1444 if(! mp_input_get_input_from_name(name,keys)) {
1445 mp_msg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name);
1446 mp_input_free_binds(binds);
1447 close(fd);
1448 return 0;
1451 if( bs > (end-buffer))
1452 memmove(buffer,end,bs - (end-buffer));
1453 bs -= end-buffer;
1454 continue;
1455 } else { // Get the command
1456 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1457 // Found new line
1458 if(iter[0] == '\n' || iter[0] == '\r') {
1459 int i;
1460 mp_msg(MSGT_INPUT,MSGL_ERR,"No command found for key %s" ,mp_input_get_key_name(keys[0]));
1461 for(i = 1; keys[i] != 0 ; i++)
1462 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1463 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1464 keys[0] = 0;
1465 if(iter > buffer) {
1466 memmove(buffer,iter,bs- (iter-buffer));
1467 bs -= (iter-buffer);
1469 continue;
1471 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1472 /* NOTHING */;
1473 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1474 if(iter == buffer) {
1475 mp_msg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer);
1476 mp_input_free_binds(binds);
1477 close(fd);
1478 return 0;
1480 memmove(buffer,iter,end - iter);
1481 bs = end - iter;
1482 continue;
1485 char cmd[end-iter+1];
1486 strncpy(cmd,iter,end-iter);
1487 cmd[end-iter] = '\0';
1488 //printf("Set bind %d => %s\n",keys[0],cmd);
1489 mp_input_bind_keys(keys,cmd);
1490 n_binds++;
1492 keys[0] = 0;
1493 end++;
1494 if(bs > (end-buffer))
1495 memmove(buffer,end,bs-(end-buffer));
1496 bs -= (end-buffer);
1497 buffer[bs-1] = '\0';
1498 continue;
1501 mp_msg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1502 close(fd);
1503 return 0;
1506 extern char *get_path(char *filename);
1508 void
1509 mp_input_init(void) {
1510 char* file;
1512 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1513 if(!file)
1514 return;
1516 if( !mp_input_parse_config(file)) {
1517 // free file if it was allocated by get_path(),
1518 // before it gets overwritten
1519 if( file != config_file)
1521 free(file);
1523 // Try global conf dir
1524 file = MPLAYER_CONFDIR "/input.conf";
1525 if(! mp_input_parse_config(file))
1526 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1528 else
1530 // free file if it was allocated by get_path()
1531 if( file != config_file)
1532 free(file);
1535 #ifdef HAVE_JOYSTICK
1536 if(use_joystick) {
1537 int fd = mp_input_joystick_init(js_dev);
1538 if(fd < 0)
1539 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1540 else
1541 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1543 #endif
1545 #ifdef HAVE_LIRC
1546 if(use_lirc) {
1547 int fd = mp_input_lirc_init();
1548 if(fd > 0)
1549 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1551 #endif
1553 #ifdef HAVE_LIRCC
1554 if(use_lircc) {
1555 int fd = lircc_init("mplayer", NULL);
1556 if(fd >= 0)
1557 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1559 #endif
1561 if(in_file) {
1562 struct stat st;
1563 if(stat(in_file,&st))
1564 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't stat %s: %s\n",in_file,strerror(errno));
1565 else {
1566 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1567 if(in_file_fd >= 0)
1568 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1569 else
1570 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't open %s: %s\n",in_file,strerror(errno));
1576 void
1577 mp_input_uninit(void) {
1578 unsigned int i;
1580 for(i=0; i < num_key_fd; i++) {
1581 if(key_fds[i].close_func)
1582 key_fds[i].close_func(key_fds[i].fd);
1585 for(i=0; i < num_cmd_fd; i++) {
1586 if(cmd_fds[i].close_func)
1587 cmd_fds[i].close_func(cmd_fds[i].fd);
1589 mp_input_free_binds(cmd_binds);
1590 cmd_binds=NULL;
1594 void
1595 mp_input_register_options(m_config_t* cfg) {
1596 m_config_register_options(cfg,mp_input_opts);
1599 static int mp_input_print_key_list(m_option_t* cfg) {
1600 int i;
1601 printf("\n");
1602 for(i= 0; key_names[i].name != NULL ; i++)
1603 printf("%s\n",key_names[i].name);
1604 exit(0);
1607 static int mp_input_print_cmd_list(m_option_t* cfg) {
1608 mp_cmd_t *cmd;
1609 int i,j;
1610 char* type;
1612 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1613 printf("%-20.20s",cmd->name);
1614 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1615 switch(cmd->args[j].type) {
1616 case MP_CMD_ARG_INT:
1617 type = "Integer";
1618 break;
1619 case MP_CMD_ARG_FLOAT:
1620 type = "Float";
1621 break;
1622 case MP_CMD_ARG_STRING:
1623 type = "String";
1624 break;
1625 default:
1626 type = "??";
1628 if(j+1 > cmd->nargs)
1629 printf(" [%s]",type);
1630 else
1631 printf(" %s",type);
1633 printf("\n");
1635 exit(0);
1639 mp_input_check_interrupt(int time) {
1640 mp_cmd_t* cmd;
1641 if((cmd = mp_input_get_cmd(time,0,1)) == NULL)
1642 return 0;
1643 switch(cmd->id) {
1644 case MP_CMD_QUIT:
1645 case MP_CMD_PLAY_TREE_STEP:
1646 case MP_CMD_PLAY_TREE_UP_STEP:
1647 case MP_CMD_PLAY_ALT_SRC_STEP:
1648 // The cmd will be executed when we are back in the main loop
1649 return 1;
1651 // remove the cmd from the queue
1652 cmd = mp_input_get_cmd(time,0,0);
1653 mp_cmd_free(cmd);
1654 return 0;