24 #include "../osdep/getch2.h"
25 #include "../osdep/keycodes.h"
26 #include "../osdep/timer.h"
27 #include "../mp_msg.h"
28 #include "../m_config.h"
29 #include "../m_option.h"
38 #include <lirc/lircc.h>
41 /// This array defines all known commands.
42 /// The first field is an id used to recognize the command without too many strcmp.
43 /// The second is obviously the command name.
44 /// The third is the minimum number of arguments this command needs.
45 /// Then comes the definition of each argument, terminated with an arg of type -1.
46 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
47 /// the last one) which is actually 9.
49 /// For the args, the first field is the type (actually int, float or string), the second
50 /// is the default value wich is used for optional arguments
52 static mp_cmd_t mp_cmds
[] = {
53 { MP_CMD_SEEK
, "seek", 1, { {MP_CMD_ARG_INT
,{0}}, {MP_CMD_ARG_INT
,{0}}, {-1,{0}} } },
55 { MP_CMD_EDL_MARK
, "edl_mark", 0, { {-1,{0}} } },
57 { MP_CMD_AUDIO_DELAY
, "audio_delay", 1, { {MP_CMD_ARG_FLOAT
,{0}}, {-1,{0}} } },
58 { MP_CMD_QUIT
, "quit", 0, { {-1,{0}} } },
59 { MP_CMD_PAUSE
, "pause", 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_VOBSUB_LANG
, "vobsub_lang", 0, { {-1,{0}} } },
81 { MP_CMD_GET_PERCENT_POS
, "get_percent_pos", 0, { {-1,{0}} } },
82 { MP_CMD_GET_TIME_LENGTH
, "get_time_length", 0, { {-1,{0}} } },
84 { MP_CMD_TV_STEP_CHANNEL
, "tv_step_channel", 1, { { MP_CMD_ARG_INT
,{0}}, {-1,{0}} }},
85 { MP_CMD_TV_STEP_NORM
, "tv_step_norm",0, { {-1,{0}} } },
86 { MP_CMD_TV_STEP_CHANNEL_LIST
, "tv_step_chanlist", 0, { {-1,{0}} } },
87 { MP_CMD_TV_SET_CHANNEL
, "tv_set_channel", 1, { { MP_CMD_ARG_STRING
, {0}}, {-1,{0}} }},
88 { MP_CMD_TV_LAST_CHANNEL
, "tv_last_channel", 0, { {-1,{0}} } },
89 { MP_CMD_TV_SET_FREQ
, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT
,{0}}, {-1,{0}} } },
90 { MP_CMD_TV_SET_NORM
, "tv_set_norm", 1, { {MP_CMD_ARG_STRING
,{0}}, {-1,{0}} } },
91 { MP_CMD_TV_SET_BRIGHTNESS
, "tv_set_brightness", 1, { { MP_CMD_ARG_INT
,{0}}, {-1,{0}} }},
92 { MP_CMD_TV_SET_CONTRAST
, "tv_set_contrast", 1, { { MP_CMD_ARG_INT
,{0}}, {-1,{0}} }},
93 { MP_CMD_TV_SET_HUE
, "tv_set_hue", 1, { { MP_CMD_ARG_INT
,{0}}, {-1,{0}} }},
94 { MP_CMD_TV_SET_SATURATION
, "tv_set_saturation", 1, { { MP_CMD_ARG_INT
,{0}}, {-1,{0}} }},
96 #ifdef HAS_DVBIN_SUPPORT
97 { MP_CMD_DVB_SET_CHANNEL
, "dvb_set_channel", 1, { { MP_CMD_ARG_INT
, {0}}, {-1,{0}} }},
99 { MP_CMD_VO_FULLSCREEN
, "vo_fullscreen", 0, { {-1,{0}} } },
100 { MP_CMD_SCREENSHOT
, "screenshot", 0, { {-1,{0}} } },
101 { MP_CMD_PANSCAN
, "panscan",1, { {MP_CMD_ARG_FLOAT
,{0}}, {MP_CMD_ARG_INT
,{0}}, {-1,{0}} } },
102 { MP_CMD_LOADFILE
, "loadfile", 1, { {MP_CMD_ARG_STRING
, {0}}, {-1,{0}} } },
103 { MP_CMD_LOADLIST
, "loadlist", 1, { {MP_CMD_ARG_STRING
, {0}}, {-1,{0}} } },
104 { MP_CMD_VF_CHANGE_RECTANGLE
, "change_rectangle", 2, { {MP_CMD_ARG_INT
,{0}}, {MP_CMD_ARG_INT
,{0}}, {-1,{0}}}},
107 { MP_CMD_GUI_LOADFILE
, "gui_loadfile", 0, { {-1,{0}} } },
108 { MP_CMD_GUI_LOADSUBTITLE
, "gui_loadsubtitle", 0, { {-1,{0}} } },
109 { MP_CMD_GUI_ABOUT
, "gui_about", 0, { {-1,{0}} } },
110 { MP_CMD_GUI_PLAY
, "gui_play", 0, { {-1,{0}} } },
111 { MP_CMD_GUI_STOP
, "gui_stop", 0, { {-1,{0}} } },
112 { MP_CMD_GUI_PLAYLIST
, "gui_playlist", 0, { {-1,{0}} } },
113 { MP_CMD_GUI_PREFERENCES
, "gui_preferences", 0, { {-1,{0}} } },
114 { MP_CMD_GUI_SKINBROWSER
, "gui_skinbrowser", 0, { {-1,{0}} } },
118 { MP_CMD_DVDNAV
, "dvdnav", 1, { {MP_CMD_ARG_INT
,{0}}, {-1,{0}} } },
119 { MP_CMD_DVDNAV_EVENT
, "dvdnav_event", 1, { { MP_CMD_ARG_VOID
, {0}}, {-1, {0}} } },
123 { MP_CMD_MENU
, "menu",1, { {MP_CMD_ARG_STRING
, {0}}, {-1,{0}} } },
124 { MP_CMD_SET_MENU
, "set_menu",1, { {MP_CMD_ARG_STRING
, {0}}, {MP_CMD_ARG_STRING
, {0}}, {-1,{0}} } },
125 { MP_CMD_CHELP
, "help", 0, { {-1,{0}} } },
126 { MP_CMD_CEXIT
, "exit", 0, { {-1,{0}} } },
127 { MP_CMD_CHIDE
, "hide", 0, { {MP_CMD_ARG_INT
,{3000}}, {-1,{0}} } },
128 { MP_CMD_CRUN
, "run", 1, { {MP_CMD_ARG_STRING
,{0}}, {-1,{0}} } },
134 /// The names of the keys as used in input.conf
135 /// If you add some new keys, you also need to add them here
137 static mp_key_name_t key_names
[] = {
139 { KEY_ENTER
, "ENTER" },
141 { KEY_CTRL
, "CTRL" },
142 { KEY_BACKSPACE
, "BS" },
143 { KEY_DELETE
, "DEL" },
144 { KEY_INSERT
, "INS" },
145 { KEY_HOME
, "HOME" },
147 { KEY_PAGE_UP
, "PGUP" },
148 { KEY_PAGE_DOWN
, "PGDWN" },
150 { KEY_RIGHT
, "RIGHT" },
151 { KEY_LEFT
, "LEFT" },
152 { KEY_DOWN
, "DOWN" },
174 { KEY_KPDEL
, "KP_DEL" },
175 { KEY_KPDEC
, "KP_DEL" },
176 { KEY_KPINS
, "KP0" },
177 { KEY_KPENTER
, "KP_ENTER" },
178 { MOUSE_BTN0
, "MOUSE_BTN0" },
179 { MOUSE_BTN1
, "MOUSE_BTN1" },
180 { MOUSE_BTN2
, "MOUSE_BTN2" },
181 { MOUSE_BTN3
, "MOUSE_BTN3" },
182 { MOUSE_BTN4
, "MOUSE_BTN4" },
183 { MOUSE_BTN5
, "MOUSE_BTN5" },
184 { MOUSE_BTN6
, "MOUSE_BTN6" },
185 { MOUSE_BTN7
, "MOUSE_BTN7" },
186 { MOUSE_BTN8
, "MOUSE_BTN8" },
187 { MOUSE_BTN9
, "MOUSE_BTN9" },
188 { JOY_AXIS1_MINUS
, "JOY_UP" },
189 { JOY_AXIS1_PLUS
, "JOY_DOWN" },
190 { JOY_AXIS0_MINUS
, "JOY_LEFT" },
191 { JOY_AXIS0_PLUS
, "JOY_RIGHT" },
193 { JOY_AXIS0_PLUS
, "JOY_AXIS0_PLUS" },
194 { JOY_AXIS0_MINUS
, "JOY_AXIS0_MINUS" },
195 { JOY_AXIS1_PLUS
, "JOY_AXIS1_PLUS" },
196 { JOY_AXIS1_MINUS
, "JOY_AXIS1_MINUS" },
197 { JOY_AXIS2_PLUS
, "JOY_AXIS2_PLUS" },
198 { JOY_AXIS2_MINUS
, "JOY_AXIS2_MINUS" },
199 { JOY_AXIS3_PLUS
, "JOY_AXIS3_PLUS" },
200 { JOY_AXIS3_MINUS
, "JOY_AXIS3_MINUS" },
201 { JOY_AXIS4_PLUS
, "JOY_AXIS4_PLUS" },
202 { JOY_AXIS4_MINUS
, "JOY_AXIS4_MINUS" },
203 { JOY_AXIS5_PLUS
, "JOY_AXIS5_PLUS" },
204 { JOY_AXIS5_MINUS
, "JOY_AXIS5_MINUS" },
205 { JOY_AXIS6_PLUS
, "JOY_AXIS6_PLUS" },
206 { JOY_AXIS6_MINUS
, "JOY_AXIS6_MINUS" },
207 { JOY_AXIS7_PLUS
, "JOY_AXIS7_PLUS" },
208 { JOY_AXIS7_MINUS
, "JOY_AXIS7_MINUS" },
209 { JOY_AXIS8_PLUS
, "JOY_AXIS8_PLUS" },
210 { JOY_AXIS8_MINUS
, "JOY_AXIS8_MINUS" },
211 { JOY_AXIS9_PLUS
, "JOY_AXIS9_PLUS" },
212 { JOY_AXIS9_MINUS
, "JOY_AXIS9_MINUS" },
214 { JOY_BTN0
, "JOY_BTN0" },
215 { JOY_BTN1
, "JOY_BTN1" },
216 { JOY_BTN2
, "JOY_BTN2" },
217 { JOY_BTN3
, "JOY_BTN3" },
218 { JOY_BTN4
, "JOY_BTN4" },
219 { JOY_BTN5
, "JOY_BTN5" },
220 { JOY_BTN6
, "JOY_BTN6" },
221 { JOY_BTN7
, "JOY_BTN7" },
222 { JOY_BTN8
, "JOY_BTN8" },
223 { JOY_BTN9
, "JOY_BTN9" },
225 { KEY_XF86_PAUSE
, "XF86_PAUSE" },
226 { KEY_XF86_STOP
, "XF86_STOP" },
227 { KEY_XF86_PREV
, "XF86_PREV" },
228 { KEY_XF86_NEXT
, "XF86_NEXT" },
233 // This is the default binding. The content of input.conf overrides these.
234 // The first arg is a null terminated array of key codes.
235 // The second is the command
237 static mp_cmd_bind_t def_cmd_binds
[] = {
239 { { MOUSE_BTN3
, 0 }, "seek 10" },
240 { { MOUSE_BTN4
, 0 }, "seek -10" },
241 { { MOUSE_BTN5
, 0 }, "volume 1" },
242 { { MOUSE_BTN6
, 0 }, "volume -1" },
245 { { 'K', 0 }, "dvdnav 1" }, // up
246 { { 'J', 0 }, "dvdnav 2" }, // down
247 { { 'H', 0 }, "dvdnav 3" }, // left
248 { { 'L', 0 }, "dvdnav 4" }, // right
249 { { 'M', 0 }, "dvdnav 5" }, // menu
250 { { 'S', 0 }, "dvdnav 6" }, // select
253 { { KEY_RIGHT
, 0 }, "seek 10" },
254 { { KEY_LEFT
, 0 }, "seek -10" },
255 { { KEY_UP
, 0 }, "seek 60" },
256 { { KEY_DOWN
, 0 }, "seek -60" },
257 { { KEY_PAGE_UP
, 0 }, "seek 600" },
258 { { KEY_PAGE_DOWN
, 0 }, "seek -600" },
259 { { '-', 0 }, "audio_delay 0.100" },
260 { { '+', 0 }, "audio_delay -0.100" },
261 { { 'q', 0 }, "quit" },
262 { { KEY_ESC
, 0 }, "quit" },
264 { { 'p', 0 }, "pause" },
266 { { ' ', 0 }, "pause" },
267 { { KEY_HOME
, 0 }, "pt_up_step 1" },
268 { { KEY_END
, 0 }, "pt_up_step -1" },
269 { { '>', 0 }, "pt_step 1" },
271 { { KEY_ENTER
, 0 }, "pt_step 1 1" },
273 { { '<', 0 }, "pt_step -1" },
274 { { KEY_INS
, 0 }, "alt_src_step 1" },
275 { { KEY_DEL
, 0 }, "alt_src_step -1" },
276 { { 'o', 0 }, "osd" },
277 { { 'z', 0 }, "sub_delay -0.1" },
278 { { 'x', 0 }, "sub_delay +0.1" },
279 { { 'g', 0 }, "sub_step -1" },
280 { { 'y', 0 }, "sub_step +1" },
281 { { '9', 0 }, "volume -1" },
282 { { '/', 0 }, "volume -1" },
283 { { '0', 0 }, "volume 1" },
284 { { '*', 0 }, "volume 1" },
285 { { 'm', 0 }, "mute" },
286 { { '1', 0 }, "contrast -1" },
287 { { '2', 0 }, "contrast 1" },
288 { { '3', 0 }, "brightness -1" },
289 { { '4', 0 }, "brightness 1" },
290 { { '5', 0 }, "hue -1" },
291 { { '6', 0 }, "hue 1" },
292 { { '7', 0 }, "saturation -1" },
293 { { '8', 0 }, "saturation 1" },
294 { { 'd', 0 }, "frame_drop" },
295 { { 'r', 0 }, "sub_pos -1" },
296 { { 't', 0 }, "sub_pos +1" },
297 { { 'a', 0 }, "sub_alignment" },
298 { { 'v', 0 }, "sub_visibility" },
299 { { 'j', 0 }, "vobsub_lang" },
301 { { 'i', 0 }, "edl_mark" },
304 { { 'h', 0 }, "tv_step_channel 1" },
305 { { 'k', 0 }, "tv_step_channel -1" },
306 { { 'n', 0 }, "tv_step_norm" },
307 { { 'u', 0 }, "tv_step_chanlist" },
310 { { 'l', 0 }, "gui_loadfile" },
311 { { 't', 0 }, "gui_loadsubtitle" },
312 { { KEY_ENTER
, 0 }, "gui_play" },
313 { { 's', 0 }, "gui_stop" },
314 { { 'p', 0 }, "gui_playlist" },
315 { { 'r', 0 }, "gui_preferences" },
316 { { 'c', 0 }, "gui_skinbrowser" },
319 { { JOY_AXIS0_PLUS
, 0 }, "seek 10" },
320 { { JOY_AXIS0_MINUS
, 0 }, "seek -10" },
321 { { JOY_AXIS1_MINUS
, 0 }, "seek 60" },
322 { { JOY_AXIS1_PLUS
, 0 }, "seek -60" },
323 { { JOY_BTN0
, 0 }, "pause" },
324 { { JOY_BTN1
, 0 }, "osd" },
325 { { JOY_BTN2
, 0 }, "volume 1"},
326 { { JOY_BTN3
, 0 }, "volume -1"},
328 { { 'f', 0 }, "vo_fullscreen" },
329 { { 's', 0 }, "screenshot" },
330 { { 'w', 0 }, "panscan -0.1" },
331 { { 'e', 0 }, "panscan +0.1" },
333 { { KEY_XF86_PAUSE
, 0 }, "pause" },
334 { { KEY_XF86_STOP
, 0 }, "quit" },
335 { { KEY_XF86_PREV
, 0 }, "seek -60" },
336 { { KEY_XF86_NEXT
, 0 }, "seek +60" },
341 #ifndef MP_MAX_KEY_FD
342 #define MP_MAX_KEY_FD 10
345 #ifndef MP_MAX_CMD_FD
346 #define MP_MAX_CMD_FD 10
349 #define MP_FD_EOF (1<<0)
350 #define MP_FD_DROP (1<<1)
351 #define MP_FD_DEAD (1<<2)
352 #define MP_FD_GOT_CMD (1<<3)
353 #define MP_FD_NO_SELECT (1<<4)
355 #define CMD_QUEUE_SIZE 100
357 typedef struct mp_input_fd
{
360 mp_close_func_t close_func
;
362 // These fields are for the cmd fds.
367 typedef struct mp_cmd_filter_st mp_cmd_filter_t
;
369 struct mp_cmd_filter_st
{
370 mp_input_cmd_filter filter
;
372 mp_cmd_filter_t
* next
;
375 // These are the user defined binds
376 static mp_cmd_bind_t
* cmd_binds
= NULL
;
377 static mp_cmd_filter_t
* cmd_filters
= NULL
;
379 // Callback to allow the menu filter to grab the incoming keys
380 void (*mp_input_key_cb
)(int code
) = NULL
;
382 static mp_input_fd_t key_fds
[MP_MAX_KEY_FD
];
383 static unsigned int num_key_fd
= 0;
384 static mp_input_fd_t cmd_fds
[MP_MAX_CMD_FD
];
385 static unsigned int num_cmd_fd
= 0;
386 static mp_cmd_t
* cmd_queue
[CMD_QUEUE_SIZE
];
387 static unsigned int cmd_queue_length
= 0,cmd_queue_start
= 0, cmd_queue_end
= 0;
389 // this is the key currently down
390 static int key_down
[MP_MAX_KEY_DOWN
];
391 static unsigned int num_key_down
= 0, last_key_down
= 0;
394 static short ar_state
= -1;
395 static mp_cmd_t
* ar_cmd
= NULL
;
396 static unsigned int ar_delay
= 100, ar_rate
= 8, last_ar
= 0;
398 static int use_joystick
= 1, use_lirc
= 1, use_lircc
= 1;
399 static char* config_file
= "input.conf";
401 static char* js_dev
= NULL
;
403 static char* in_file
= NULL
;
404 static int in_file_fd
= -1;
406 static int mp_input_print_key_list(m_option_t
* cfg
);
407 static int mp_input_print_cmd_list(m_option_t
* cfg
);
409 // Our command line options
410 static m_option_t input_conf
[] = {
411 { "conf", &config_file
, CONF_TYPE_STRING
, CONF_GLOBAL
, 0, 0, NULL
},
412 { "ar-delay", &ar_delay
, CONF_TYPE_INT
, CONF_GLOBAL
, 0, 0, NULL
},
413 { "ar-rate", &ar_rate
, CONF_TYPE_INT
, CONF_GLOBAL
, 0, 0, NULL
},
414 { "keylist", mp_input_print_key_list
, CONF_TYPE_FUNC
, CONF_GLOBAL
, 0, 0, NULL
},
415 { "cmdlist", mp_input_print_cmd_list
, CONF_TYPE_FUNC
, CONF_GLOBAL
, 0, 0, NULL
},
416 { "js-dev", &js_dev
, CONF_TYPE_STRING
, CONF_GLOBAL
, 0, 0, NULL
},
417 { "file", &in_file
, CONF_TYPE_STRING
, CONF_GLOBAL
, 0, 0, NULL
},
418 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
421 static m_option_t mp_input_opts
[] = {
422 { "input", &input_conf
, CONF_TYPE_SUBCONFIG
, 0, 0, 0, NULL
},
423 { "nojoystick", &use_joystick
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 1, 0, NULL
},
424 { "joystick", &use_joystick
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 0, 1, NULL
},
425 { "nolirc", &use_lirc
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 1, 0, NULL
},
426 { "lirc", &use_lirc
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 0, 1, NULL
},
427 { "nolircc", &use_lircc
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 1, 0, NULL
},
428 { "lircc", &use_lircc
, CONF_TYPE_FLAG
, CONF_GLOBAL
, 0, 1, NULL
},
429 { NULL
, NULL
, 0, 0, 0, 0, NULL
}
433 mp_input_default_key_func(int fd
);
436 mp_input_default_cmd_func(int fd
,char* buf
, int l
);
439 mp_input_get_key_name(int key
);
443 mp_input_add_cmd_fd(int fd
, int select
, mp_cmd_func_t read_func
, mp_close_func_t close_func
) {
444 if(num_cmd_fd
== MP_MAX_CMD_FD
) {
445 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Too many command fds, unable to register fd %d.\n",fd
);
449 memset(&cmd_fds
[num_cmd_fd
],0,sizeof(mp_input_fd_t
));
450 cmd_fds
[num_cmd_fd
].fd
= fd
;
451 cmd_fds
[num_cmd_fd
].read_func
= read_func
? read_func
: mp_input_default_cmd_func
;
452 cmd_fds
[num_cmd_fd
].close_func
= close_func
;
454 cmd_fds
[num_cmd_fd
].flags
= MP_FD_NO_SELECT
;
461 mp_input_rm_cmd_fd(int fd
) {
464 for(i
= 0; i
< num_cmd_fd
; i
++) {
465 if(cmd_fds
[i
].fd
== fd
)
470 if(cmd_fds
[i
].close_func
)
471 cmd_fds
[i
].close_func(cmd_fds
[i
].fd
);
472 if(cmd_fds
[i
].buffer
)
473 free(cmd_fds
[i
].buffer
);
475 if(i
+ 1 < num_cmd_fd
)
476 memmove(&cmd_fds
[i
],&cmd_fds
[i
+1],(num_cmd_fd
- i
- 1)*sizeof(mp_input_fd_t
));
481 mp_input_rm_key_fd(int fd
) {
484 for(i
= 0; i
< num_key_fd
; i
++) {
485 if(key_fds
[i
].fd
== fd
)
490 if(key_fds
[i
].close_func
)
491 key_fds
[i
].close_func(key_fds
[i
].fd
);
493 if(i
+ 1 < num_key_fd
)
494 memmove(&key_fds
[i
],&key_fds
[i
+1],(num_key_fd
- i
- 1)*sizeof(mp_input_fd_t
));
499 mp_input_add_key_fd(int fd
, int select
, mp_key_func_t read_func
, mp_close_func_t close_func
) {
500 if(num_key_fd
== MP_MAX_KEY_FD
) {
501 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Too many key fds, unable to register fd %d.\n",fd
);
505 memset(&key_fds
[num_key_fd
],0,sizeof(mp_input_fd_t
));
506 key_fds
[num_key_fd
].fd
= fd
;
507 key_fds
[num_key_fd
].read_func
= read_func
? read_func
: mp_input_default_key_func
;
508 key_fds
[num_key_fd
].close_func
= close_func
;
510 key_fds
[num_key_fd
].flags
|= MP_FD_NO_SELECT
;
519 mp_input_parse_cmd(char* str
) {
522 mp_cmd_t
*cmd
, *cmd_def
;
528 for(ptr
= str
; ptr
[0] != '\0' && ptr
[0] != '\t' && ptr
[0] != ' ' ; ptr
++)
538 for(i
=0; mp_cmds
[i
].name
!= NULL
; i
++) {
539 if(strncasecmp(mp_cmds
[i
].name
,str
,l
) == 0)
543 if(mp_cmds
[i
].name
== NULL
)
546 cmd_def
= &mp_cmds
[i
];
548 cmd
= (mp_cmd_t
*)malloc(sizeof(mp_cmd_t
));
549 cmd
->id
= cmd_def
->id
;
550 cmd
->name
= strdup(cmd_def
->name
);
554 for(i
=0; ptr
&& i
< MP_CMD_MAX_ARGS
; i
++) {
555 ptr
= strchr(ptr
,' ');
557 while(ptr
[0] == ' ' || ptr
[0] == '\t') ptr
++;
558 if(ptr
[0] == '\0') break;
559 cmd
->args
[i
].type
= cmd_def
->args
[i
].type
;
560 switch(cmd_def
->args
[i
].type
) {
563 cmd
->args
[i
].v
.i
= atoi(ptr
);
565 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Command %s: argument %d isn't an integer.\n",cmd_def
->name
,i
+1);
569 case MP_CMD_ARG_FLOAT
:
571 /* <olo@altkom.com.pl> Use portable C locale for parsing floats: */
573 setlocale(LC_NUMERIC
, "C");
575 cmd
->args
[i
].v
.f
= atof(ptr
);
577 setlocale(LC_NUMERIC
, "");
580 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Command %s: argument %d isn't a float.\n",cmd_def
->name
,i
+1);
584 case MP_CMD_ARG_STRING
: {
586 char* ptr2
= ptr
, *start
;
588 if(ptr
[0] == '\'' || ptr
[0] == '"') {
595 e
= strchr(ptr2
,term
);
597 if(e
<= ptr2
|| *(e
- 1) != '\\') break;
601 if(term
!= ' ' && (!e
|| e
[0] == '\0')) {
602 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Command %s: argument %d is unterminated.\n",cmd_def
->name
,i
+1);
605 } else if(!e
) e
= ptr
+strlen(ptr
);
608 for(e
= strchr(ptr2
,'\\') ; e
; e
= strchr(ptr2
,'\\')) {
609 memmove(e
,e
+1,strlen(e
));
613 cmd
->args
[i
].v
.s
= (char*)malloc((l
+1)*sizeof(char));
614 strncpy(cmd
->args
[i
].v
.s
,start
,l
);
615 cmd
->args
[i
].v
.s
[l
] = '\0';
620 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Unknown argument %d\n",i
);
625 if(cmd_def
->nargs
> cmd
->nargs
) {
626 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Got command '%s' but\n",str
);
627 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
);
632 for( ; i
< MP_CMD_MAX_ARGS
&& cmd_def
->args
[i
].type
!= -1 ; i
++) {
633 memcpy(&cmd
->args
[i
],&cmd_def
->args
[i
],sizeof(mp_cmd_arg_t
));
634 if(cmd_def
->args
[i
].type
== MP_CMD_ARG_STRING
&& cmd_def
->args
[i
].v
.s
!= NULL
)
635 cmd
->args
[i
].v
.s
= strdup(cmd_def
->args
[i
].v
.s
);
638 if(i
< MP_CMD_MAX_ARGS
)
639 cmd
->args
[i
].type
= -1;
645 mp_input_default_key_func(int fd
) {
649 while(l
< sizeof(int)) {
650 r
= read(fd
,(&code
)+l
,sizeof(int)-l
);
658 #define MP_CMD_MAX_SIZE 256
661 mp_input_read_cmd(mp_input_fd_t
* mp_fd
, char** ret
) {
665 // Allocate the buffer if it doesn't exist
667 mp_fd
->buffer
= (char*)malloc(MP_CMD_MAX_SIZE
*sizeof(char));
669 mp_fd
->size
= MP_CMD_MAX_SIZE
;
672 // Get some data if needed/possible
673 while( !(mp_fd
->flags
& MP_FD_GOT_CMD
) && !(mp_fd
->flags
& MP_FD_EOF
) && (mp_fd
->size
- mp_fd
->pos
> 1) ) {
674 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
);
680 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Error while reading cmd fd %d: %s\n",mp_fd
->fd
,strerror(errno
));
681 case MP_INPUT_NOTHING
:
686 mp_fd
->flags
|= MP_FD_EOF
;
693 // Reset the got_cmd flag
694 mp_fd
->flags
&= ~MP_FD_GOT_CMD
;
699 mp_fd
->buffer
[mp_fd
->pos
] = '\0';
700 end
= strchr(mp_fd
->buffer
,'\n');
703 // If buffer is full we must drop all until the next \n
704 if(mp_fd
->size
- mp_fd
->pos
<= 1) {
705 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Cmd buffer of fd %d is full: dropping content\n",mp_fd
->fd
);
707 mp_fd
->flags
|= MP_FD_DROP
;
711 // We already have a cmd : set the got_cmd flag
713 mp_fd
->flags
|= MP_FD_GOT_CMD
;
717 l
= end
- mp_fd
->buffer
;
719 // Not dropping : put the cmd in ret
720 if( ! (mp_fd
->flags
& MP_FD_DROP
)) {
721 (*ret
) = (char*)malloc((l
+1)*sizeof(char));
722 strncpy((*ret
),mp_fd
->buffer
,l
);
724 } else { // Remove the dropping flag
725 mp_fd
->flags
&= ~MP_FD_DROP
;
727 if( mp_fd
->pos
- (l
+1) > 0)
728 memmove(mp_fd
->buffer
,end
+1,mp_fd
->pos
-(l
+1));
735 return MP_INPUT_NOTHING
;
739 mp_input_default_cmd_func(int fd
,char* buf
, int l
) {
742 int r
= read(fd
,buf
,l
);
747 else if(errno
== EAGAIN
)
748 return MP_INPUT_NOTHING
;
749 return MP_INPUT_ERROR
;
759 mp_input_add_cmd_filter(mp_input_cmd_filter func
, void* ctx
) {
760 mp_cmd_filter_t
* filter
= malloc(sizeof(mp_cmd_filter_t
))/*, *prev*/;
762 filter
->filter
= func
;
764 filter
->next
= cmd_filters
;
765 cmd_filters
= filter
;
770 mp_input_find_bind_for_key(mp_cmd_bind_t
* binds
, int n
,int* keys
) {
773 for(j
= 0; binds
[j
].cmd
!= NULL
; j
++) {
776 for(s
= 0; s
< n
&& binds
[j
].input
[s
] != 0; s
++) {
777 if(binds
[j
].input
[s
] != keys
[s
]) {
782 if(found
&& binds
[j
].input
[s
] == 0 && s
== n
)
787 if(binds
[j
].input
[0] == keys
[0] && binds
[j
].input
[1] == 0)
795 mp_input_get_cmd_from_keys(int n
,int* keys
, int paused
) {
800 cmd
= mp_input_find_bind_for_key(cmd_binds
,n
,keys
);
802 cmd
= mp_input_find_bind_for_key(def_cmd_binds
,n
,keys
);
805 mp_msg(MSGT_INPUT
,MSGL_WARN
,"No bind found for key %s",mp_input_get_key_name(keys
[0]));
809 mp_msg(MSGT_INPUT
,MSGL_WARN
,"-%s",mp_input_get_key_name(keys
[s
]));
811 mp_msg(MSGT_INPUT
,MSGL_WARN
," \n");
814 ret
= mp_input_parse_cmd(cmd
);
816 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Invalid command for bound key %s",mp_input_get_key_name(key_down
[0]));
817 if( num_key_down
> 1) {
819 for(s
=1; s
< num_key_down
; s
++)
820 mp_msg(MSGT_INPUT
,MSGL_ERR
,"-%s",mp_input_get_key_name(key_down
[s
]));
822 mp_msg(MSGT_INPUT
,MSGL_ERR
," : %s \n",cmd
);
828 mp_input_read_key_code(int time
) {
829 #ifndef HAVE_NO_POSIX_SELECT
831 struct timeval tv
,*time_val
;
833 int i
,n
=0,max_fd
= 0;
834 static int last_loop
= 0;
837 return MP_INPUT_NOTHING
;
839 #ifndef HAVE_NO_POSIX_SELECT
842 // Remove fd marked as dead and build the fd_set
843 // n == number of fd's to be select() checked
844 for(i
= 0; (unsigned int)i
< num_key_fd
; i
++) {
845 if( (key_fds
[i
].flags
& MP_FD_DEAD
) ) {
846 mp_input_rm_key_fd(key_fds
[i
].fd
);
849 } else if(key_fds
[i
].flags
& MP_FD_NO_SELECT
)
851 if(key_fds
[i
].fd
> max_fd
)
852 max_fd
= key_fds
[i
].fd
;
853 #ifndef HAVE_NO_POSIX_SELECT
854 FD_SET(key_fds
[i
].fd
,&fds
);
860 return MP_INPUT_NOTHING
;
862 #ifndef HAVE_NO_POSIX_SELECT
863 // if we have fd's without MP_FD_NO_SELECT flag, call select():
868 tv
.tv_usec
= (time
%1000)*1000;
874 if(select(max_fd
+1,&fds
,NULL
,NULL
,time_val
) < 0) {
877 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Select error: %s\n",strerror(errno
));
885 for(i
= last_loop
+ 1 ; i
!= last_loop
; i
++) {
887 // This is to check all fds in turn
888 if((unsigned int)i
>= num_key_fd
) {
891 last_loop
%= (num_key_fd
+1);
894 #ifndef HAVE_NO_POSIX_SELECT
895 // No input from this fd
896 if(! (key_fds
[i
].flags
& MP_FD_NO_SELECT
) && ! FD_ISSET(key_fds
[i
].fd
,&fds
))
899 if(key_fds
[i
].fd
== 0) { // stdin is handled by getch2
902 code
= MP_INPUT_NOTHING
;
905 code
= ((mp_key_func_t
)key_fds
[i
].read_func
)(key_fds
[i
].fd
);
910 if(code
== MP_INPUT_ERROR
)
911 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Error on key input fd %d\n",key_fds
[i
].fd
);
912 else if(code
== MP_INPUT_DEAD
) {
913 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Dead key input on fd %d\n",key_fds
[i
].fd
);
914 key_fds
[i
].flags
|= MP_FD_DEAD
;
917 return MP_INPUT_NOTHING
;
922 mp_input_read_keys(int time
,int paused
) {
923 int code
= mp_input_read_key_code(time
);
927 if(mp_input_key_cb
) {
928 for( ; code
>= 0 ; code
= mp_input_read_key_code(0) ) {
929 if(code
& MP_KEY_DOWN
) continue;
930 code
&= ~(MP_KEY_DOWN
|MP_NO_REPEAT_KEY
);
931 mp_input_key_cb(code
);
936 for( ; code
>= 0 ; code
= mp_input_read_key_code(0) ) {
938 if(code
& MP_KEY_DOWN
) {
939 if(num_key_down
> MP_MAX_KEY_DOWN
) {
940 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Too many key down events at the same time\n");
943 code
&= ~MP_KEY_DOWN
;
944 // Check if we don't already have this key as pushed
945 for(j
= 0; j
< num_key_down
; j
++) {
946 if(key_down
[j
] == code
)
949 if(j
!= num_key_down
)
951 key_down
[num_key_down
] = code
;
953 last_key_down
= GetTimer();
958 // Check if the key is in the down key, driver which can't send push event
959 // send only release event
960 for(j
= 0; j
< num_key_down
; j
++) {
961 if(key_down
[j
] == code
)
964 if(j
== num_key_down
) { // key was not in the down keys : add it
965 if(num_key_down
> MP_MAX_KEY_DOWN
) {
966 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Too many key down events at the same time\n");
969 key_down
[num_key_down
] = code
;
973 // We ignore key from last combination
974 ret
= last_key_down
? mp_input_get_cmd_from_keys(num_key_down
,key_down
,paused
) : NULL
;
976 if(j
+1 < num_key_down
)
977 memmove(&key_down
[j
],&key_down
[j
+1],(num_key_down
-(j
+1))*sizeof(int));
989 // No input : autorepeat ?
990 if(ar_rate
> 0 && ar_state
>=0 && num_key_down
> 0 && ! (key_down
[num_key_down
-1] & MP_NO_REPEAT_KEY
)) {
991 unsigned int t
= GetTimer();
992 // First time : wait delay
993 if(ar_state
== 0 && (t
- last_key_down
) >= ar_delay
*1000) {
994 ar_cmd
= mp_input_get_cmd_from_keys(num_key_down
,key_down
,paused
);
1001 return mp_cmd_clone(ar_cmd
);
1002 // Then send rate / sec event
1003 } else if(ar_state
== 1 && (t
-last_ar
) >= 1000000/ar_rate
) {
1005 return mp_cmd_clone(ar_cmd
);
1013 mp_input_read_cmds(int time
) {
1014 #ifndef HAVE_NO_POSIX_SELECT
1016 struct timeval tv
,*time_val
;
1018 int i
,n
= 0,max_fd
= 0,got_cmd
= 0;
1020 static int last_loop
= 0;
1025 #ifndef HAVE_NO_POSIX_SELECT
1028 for(i
= 0; (unsigned int)i
< num_cmd_fd
; i
++) {
1029 if( (cmd_fds
[i
].flags
& MP_FD_DEAD
) || (cmd_fds
[i
].flags
& MP_FD_EOF
) ) {
1030 mp_input_rm_cmd_fd(cmd_fds
[i
].fd
);
1033 } else if(cmd_fds
[i
].flags
& MP_FD_NO_SELECT
)
1035 if(cmd_fds
[i
].flags
& MP_FD_GOT_CMD
)
1037 if(cmd_fds
[i
].fd
> max_fd
)
1038 max_fd
= cmd_fds
[i
].fd
;
1039 #ifndef HAVE_NO_POSIX_SELECT
1040 FD_SET(cmd_fds
[i
].fd
,&fds
);
1048 #ifndef HAVE_NO_POSIX_SELECT
1050 tv
.tv_sec
=time
/1000;
1051 tv
.tv_usec
= (time
%1000)*1000;
1057 if((i
= select(max_fd
+1,&fds
,NULL
,NULL
,time_val
)) <= 0) {
1061 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Select error: %s\n",strerror(errno
));
1070 for(i
= last_loop
+ 1; i
!= last_loop
; i
++) {
1073 if((unsigned int)i
>= num_cmd_fd
) {
1076 last_loop
%= (num_cmd_fd
+1);
1079 #ifndef HAVE_NO_POSIX_SELECT
1080 if( ! (cmd_fds
[i
].flags
& MP_FD_NO_SELECT
) && ! FD_ISSET(cmd_fds
[i
].fd
,&fds
) && ! (cmd_fds
[i
].flags
& MP_FD_GOT_CMD
) )
1084 r
= mp_input_read_cmd(&cmd_fds
[i
],&cmd
);
1086 if(r
== MP_INPUT_ERROR
)
1087 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Error on cmd fd %d\n",cmd_fds
[i
].fd
);
1088 else if(r
== MP_INPUT_DEAD
)
1089 cmd_fds
[i
].flags
|= MP_FD_DEAD
;
1092 ret
= mp_input_parse_cmd(cmd
);
1105 mp_input_queue_cmd(mp_cmd_t
* cmd
) {
1106 if(cmd_queue_length
>= CMD_QUEUE_SIZE
)
1108 cmd_queue
[cmd_queue_end
] = cmd
;
1109 cmd_queue_end
= (cmd_queue_end
+ 1) % CMD_QUEUE_SIZE
;
1115 mp_input_get_queued_cmd(void) {
1118 if(cmd_queue_length
== 0)
1121 ret
= cmd_queue
[cmd_queue_start
];
1124 cmd_queue_start
= (cmd_queue_start
+ 1) % CMD_QUEUE_SIZE
;
1130 mp_input_get_cmd(int time
, int paused
) {
1131 mp_cmd_t
* ret
= NULL
;
1132 mp_cmd_filter_t
* cf
;
1135 ret
= mp_input_get_queued_cmd();
1137 ret
= mp_input_read_keys(time
,paused
);
1139 ret
= mp_input_read_cmds(time
);
1142 if(!ret
) return NULL
;
1144 for(cf
= cmd_filters
; cf
; cf
= cf
->next
) {
1145 if(cf
->filter(ret
,paused
,cf
->ctx
))
1153 mp_cmd_free(mp_cmd_t
* cmd
) {
1156 // assert(cmd != NULL);
1163 for(i
=0; i
< MP_CMD_MAX_ARGS
&& cmd
->args
[i
].type
!= -1; i
++) {
1164 if(cmd
->args
[i
].type
== MP_CMD_ARG_STRING
&& cmd
->args
[i
].v
.s
!= NULL
)
1165 free(cmd
->args
[i
].v
.s
);
1171 mp_cmd_clone(mp_cmd_t
* cmd
) {
1175 assert(cmd
!= NULL
);
1178 ret
= (mp_cmd_t
*)malloc(sizeof(mp_cmd_t
));
1179 memcpy(ret
,cmd
,sizeof(mp_cmd_t
));
1181 ret
->name
= strdup(cmd
->name
);
1182 for(i
= 0; i
< MP_CMD_MAX_ARGS
&& cmd
->args
[i
].type
!= -1; i
++) {
1183 if(cmd
->args
[i
].type
== MP_CMD_ARG_STRING
&& cmd
->args
[i
].v
.s
!= NULL
)
1184 ret
->args
[i
].v
.s
= strdup(cmd
->args
[i
].v
.s
);
1190 static char key_str
[12];
1193 mp_input_get_key_name(int key
) {
1196 for(i
= 0; key_names
[i
].name
!= NULL
; i
++) {
1197 if(key_names
[i
].key
== key
)
1198 return key_names
[i
].name
;
1202 snprintf(key_str
,12,"%c",(char)key
);
1206 // Print the hex key code
1207 snprintf(key_str
,12,"%#-8x",key
);
1213 mp_input_get_key_from_name(char* name
) {
1214 int i
,ret
= 0,len
= strlen(name
);
1215 if(len
== 1) { // Direct key code
1216 ret
= (unsigned char)name
[0];
1218 } else if(len
> 2 && strncasecmp("0x",name
,2) == 0)
1219 return strtol(name
,NULL
,16);
1221 for(i
= 0; key_names
[i
].name
!= NULL
; i
++) {
1222 if(strcasecmp(key_names
[i
].name
,name
) == 0)
1223 return key_names
[i
].key
;
1230 mp_input_get_input_from_name(char* name
,int* keys
) {
1236 for(end
= strchr(ptr
,'-') ; ptr
!= NULL
; end
= strchr(ptr
,'-')) {
1237 if(end
&& end
[1] != '\0') {
1242 keys
[n
] = mp_input_get_key_from_name(ptr
);
1247 if(end
&& end
[1] != '\0' && n
< MP_MAX_KEY_DOWN
)
1257 mp_input_bind_keys(int keys
[MP_MAX_KEY_DOWN
+1], char* cmd
) {
1259 mp_cmd_bind_t
* bind
= NULL
;
1262 assert(keys
!= NULL
);
1263 assert(cmd
!= NULL
);
1267 for(i
= 0; cmd_binds
[i
].cmd
!= NULL
; i
++) {
1268 for(j
= 0 ; cmd_binds
[i
].input
[j
] == keys
[j
] && keys
[j
] != 0 ; j
++)
1270 if(keys
[j
] == 0 && cmd_binds
[i
].input
[j
] == 0 ) {
1271 bind
= &cmd_binds
[i
];
1278 cmd_binds
= (mp_cmd_bind_t
*)realloc(cmd_binds
,(i
+2)*sizeof(mp_cmd_bind_t
));
1279 memset(&cmd_binds
[i
],0,2*sizeof(mp_cmd_bind_t
));
1280 bind
= &cmd_binds
[i
];
1284 bind
->cmd
= strdup(cmd
);
1285 memcpy(bind
->input
,keys
,(MP_MAX_KEY_DOWN
+1)*sizeof(int));
1290 mp_input_free_binds(mp_cmd_bind_t
* binds
) {
1296 for(i
= 0; binds
[i
].cmd
!= NULL
; i
++)
1305 #define SPACE_CHAR " \n\r\t"
1308 mp_input_parse_config(char *file
) {
1310 int bs
= 0,r
,eof
= 0,comments
= 0;
1312 char buffer
[BS_MAX
];
1313 int n_binds
= 0, keys
[MP_MAX_KEY_DOWN
+1] = { 0 };
1314 mp_cmd_bind_t
* binds
= NULL
;
1316 fd
= open(file
,O_RDONLY
);
1319 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Can't open input config file %s: %s\n",file
,strerror(errno
));
1323 mp_msg(MSGT_INPUT
,MSGL_V
,"Parsing input config file %s\n",file
);
1326 if(! eof
&& bs
< BS_MAX
-1) {
1328 r
= read(fd
,buffer
+bs
,BS_MAX
-1-bs
);
1332 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Error while reading input config file %s: %s\n",file
,strerror(errno
));
1333 mp_input_free_binds(binds
);
1340 buffer
[bs
-1] = '\0';
1343 // Empty buffer : return
1345 mp_msg(MSGT_INPUT
,MSGL_INFO
,"Input config file %s parsed: %d binds\n",file
,n_binds
);
1355 for( ; iter
[0] != '\0' && iter
[0] != '\n' ; iter
++)
1357 if(iter
[0] == '\0') { // Buffer was full of comment
1364 memmove(buffer
,iter
,r
+1);
1371 // Find the wanted key
1373 // Jump beginning space
1374 for( ; iter
[0] != '\0' && strchr(SPACE_CHAR
,iter
[0]) != NULL
; iter
++)
1376 if(iter
[0] == '\0') { // Buffer was full of space char
1380 if(iter
[0] == '#') { // Comments
1384 // Find the end of the key code name
1385 for(end
= iter
; end
[0] != '\0' && strchr(SPACE_CHAR
,end
[0]) == NULL
; end
++)
1387 if(end
[0] == '\0') { // Key name doesn't fit in the buffer
1388 if(buffer
== iter
) {
1389 if(eof
&& (buffer
-iter
) == bs
)
1390 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Unfinished binding %s\n",iter
);
1392 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Buffer is too small for this key name: %s\n",iter
);
1393 mp_input_free_binds(binds
);
1396 memmove(buffer
,iter
,end
-iter
);
1401 char name
[end
-iter
+1];
1402 strncpy(name
,iter
,end
-iter
);
1403 name
[end
-iter
] = '\0';
1404 if(! mp_input_get_input_from_name(name
,keys
)) {
1405 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Unknown key '%s'\n",name
);
1406 mp_input_free_binds(binds
);
1411 if( bs
> (end
-buffer
))
1412 memmove(buffer
,end
,bs
- (end
-buffer
));
1415 } else { // Get the command
1416 while(iter
[0] == ' ' || iter
[0] == '\t') iter
++;
1418 if(iter
[0] == '\n' || iter
[0] == '\r') {
1420 mp_msg(MSGT_INPUT
,MSGL_ERR
,"No command found for key %s" ,mp_input_get_key_name(keys
[0]));
1421 for(i
= 1; keys
[i
] != 0 ; i
++)
1422 mp_msg(MSGT_INPUT
,MSGL_ERR
,"-%s",mp_input_get_key_name(keys
[i
]));
1423 mp_msg(MSGT_INPUT
,MSGL_ERR
,"\n");
1426 memmove(buffer
,iter
,bs
- (iter
-buffer
));
1427 bs
-= (iter
-buffer
);
1431 for(end
= iter
; end
[0] != '\n' && end
[0] != '\r' && end
[0] != '\0' ; end
++)
1433 if(end
[0] == '\0' && ! (eof
&& ((end
+1) - buffer
) == bs
)) {
1434 if(iter
== buffer
) {
1435 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Buffer is too small for command %s\n",buffer
);
1436 mp_input_free_binds(binds
);
1440 memmove(buffer
,iter
,end
- iter
);
1445 char cmd
[end
-iter
+1];
1446 strncpy(cmd
,iter
,end
-iter
);
1447 cmd
[end
-iter
] = '\0';
1448 //printf("Set bind %d => %s\n",keys[0],cmd);
1449 mp_input_bind_keys(keys
,cmd
);
1454 if(bs
> (end
-buffer
))
1455 memmove(buffer
,end
,bs
-(end
-buffer
));
1457 buffer
[bs
-1] = '\0';
1461 mp_msg(MSGT_INPUT
,MSGL_ERR
,"What are we doing here?\n");
1466 extern char *get_path(char *filename
);
1469 mp_input_init(void) {
1472 file
= config_file
[0] != '/' ? get_path(config_file
) : config_file
;
1476 if(! mp_input_parse_config(file
)) {
1477 // Try global conf dir
1478 file
= MPLAYER_CONFDIR
"/input.conf";
1479 if(! mp_input_parse_config(file
))
1480 mp_msg(MSGT_INPUT
,MSGL_WARN
,"Falling back on default (hardcoded) input config\n");
1483 #ifdef HAVE_JOYSTICK
1485 int fd
= mp_input_joystick_init(js_dev
);
1487 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Can't init input joystick\n");
1489 mp_input_add_key_fd(fd
,1,mp_input_joystick_read
,(mp_close_func_t
)close
);
1495 int fd
= mp_input_lirc_init();
1497 mp_input_add_cmd_fd(fd
,0,mp_input_lirc_read
,mp_input_lirc_close
);
1503 int fd
= lircc_init("mplayer", NULL
);
1505 mp_input_add_cmd_fd(fd
,1,NULL
,(mp_close_func_t
)lircc_cleanup
);
1511 if(stat(in_file
,&st
))
1512 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Can't stat %s: %s\n",in_file
,strerror(errno
));
1514 in_file_fd
= open(in_file
,S_ISFIFO(st
.st_mode
) ? O_RDWR
: O_RDONLY
);
1516 mp_input_add_cmd_fd(in_file_fd
,1,NULL
,(mp_close_func_t
)close
);
1518 mp_msg(MSGT_INPUT
,MSGL_ERR
,"Can't open %s: %s\n",in_file
,strerror(errno
));
1525 mp_input_uninit(void) {
1528 for(i
=0; i
< num_key_fd
; i
++) {
1529 if(key_fds
[i
].close_func
)
1530 key_fds
[i
].close_func(key_fds
[i
].fd
);
1533 for(i
=0; i
< num_cmd_fd
; i
++) {
1534 if(cmd_fds
[i
].close_func
)
1535 cmd_fds
[i
].close_func(cmd_fds
[i
].fd
);
1541 mp_input_register_options(m_config_t
* cfg
) {
1542 m_config_register_options(cfg
,mp_input_opts
);
1545 static int mp_input_print_key_list(m_option_t
* cfg
) {
1548 for(i
= 0; key_names
[i
].name
!= NULL
; i
++)
1549 printf("%s\n",key_names
[i
].name
);
1553 static int mp_input_print_cmd_list(m_option_t
* cfg
) {
1558 for(i
= 0; (cmd
= &mp_cmds
[i
])->name
!= NULL
; i
++) {
1559 printf("%-20.20s",cmd
->name
);
1560 for(j
= 0 ; j
< MP_CMD_MAX_ARGS
&& cmd
->args
[j
].type
!= -1 ; j
++) {
1561 switch(cmd
->args
[j
].type
) {
1562 case MP_CMD_ARG_INT
:
1565 case MP_CMD_ARG_FLOAT
:
1568 case MP_CMD_ARG_STRING
:
1574 if(j
+1 > cmd
->nargs
)
1575 printf(" [%s]",type
);
1585 mp_input_check_interrupt(int time
) {
1587 if((cmd
= mp_input_get_cmd(time
,0)) == NULL
)
1591 case MP_CMD_PLAY_TREE_STEP
:
1592 case MP_CMD_PLAY_TREE_UP_STEP
:
1593 case MP_CMD_PLAY_ALT_SRC_STEP
:
1594 // The cmd will be executed when we are back in the main loop
1595 if(! mp_input_queue_cmd(cmd
)) {
1596 mp_msg(MSGT_INPUT
,MSGL_ERR
,"mpdemux_check_interrupt: can't queue cmd %s\n",cmd
->name
);