Add P4 Northwood. (Roman Shiryaev <mih_val@mail.ru>; See <200308250434.38516.mih_val...
[mplayer/greg.git] / input / input.c
blobb1af8abf842832bdaa064d03b3bbf0e2b744af0c
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 #ifdef USE_SETLOCALE
16 #include <locale.h>
17 #endif
19 #include "input.h"
20 #include "mouse.h"
21 #ifdef MP_DEBUG
22 #include <assert.h>
23 #endif
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"
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 /// 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}} } },
54 #ifdef USE_EDL
55 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
56 #endif
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}} } },
83 #ifdef USE_TV
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}} }},
95 #endif
96 #ifdef HAS_DVBIN_SUPPORT
97 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}} }},
98 #endif
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}}}},
106 #ifdef HAVE_NEW_GUI
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}} } },
115 #endif
117 #ifdef USE_DVDNAV
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}} } },
120 #endif
122 #ifdef HAVE_MENU
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}} } },
129 #endif
131 { 0, NULL, 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[] = {
138 { ' ', "SPACE" },
139 { KEY_ENTER, "ENTER" },
140 { KEY_TAB, "TAB" },
141 { KEY_CTRL, "CTRL" },
142 { KEY_BACKSPACE, "BS" },
143 { KEY_DELETE, "DEL" },
144 { KEY_INSERT, "INS" },
145 { KEY_HOME, "HOME" },
146 { KEY_END, "END" },
147 { KEY_PAGE_UP, "PGUP" },
148 { KEY_PAGE_DOWN, "PGDWN" },
149 { KEY_ESC, "ESC" },
150 { KEY_RIGHT, "RIGHT" },
151 { KEY_LEFT, "LEFT" },
152 { KEY_DOWN, "DOWN" },
153 { KEY_UP, "UP" },
154 { KEY_F+1, "F1" },
155 { KEY_F+2, "F2" },
156 { KEY_F+3, "F3" },
157 { KEY_F+4, "F4" },
158 { KEY_F+5, "F5" },
159 { KEY_F+6, "F6" },
160 { KEY_F+7, "F7" },
161 { KEY_F+8, "F8" },
162 { KEY_F+9, "F9" },
163 { KEY_F+10, "F10" },
164 { KEY_KP0, "KP0" },
165 { KEY_KP1, "KP1" },
166 { KEY_KP2, "KP2" },
167 { KEY_KP3, "KP3" },
168 { KEY_KP4, "KP4" },
169 { KEY_KP5, "KP5" },
170 { KEY_KP6, "KP6" },
171 { KEY_KP7, "KP7" },
172 { KEY_KP8, "KP8" },
173 { KEY_KP9, "KP9" },
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" },
230 { 0, NULL }
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" },
244 #ifdef USE_DVDNAV
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
251 #endif
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" },
263 #ifndef HAVE_NEW_GUI
264 { { 'p', 0 }, "pause" },
265 #endif
266 { { ' ', 0 }, "pause" },
267 { { KEY_HOME, 0 }, "pt_up_step 1" },
268 { { KEY_END, 0 }, "pt_up_step -1" },
269 { { '>', 0 }, "pt_step 1" },
270 #ifndef HAVE_NEW_GUI
271 { { KEY_ENTER, 0 }, "pt_step 1 1" },
272 #endif
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" },
300 #ifdef USE_EDL
301 { { 'i', 0 }, "edl_mark" },
302 #endif
303 #ifdef USE_TV
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" },
308 #endif
309 #ifdef HAVE_NEW_GUI
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" },
317 #endif
318 #ifdef HAVE_JOYSTICK
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"},
327 #endif
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" },
338 { { 0 }, NULL }
341 #ifndef MP_MAX_KEY_FD
342 #define MP_MAX_KEY_FD 10
343 #endif
345 #ifndef MP_MAX_CMD_FD
346 #define MP_MAX_CMD_FD 10
347 #endif
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 {
358 int fd;
359 void* read_func;
360 mp_close_func_t close_func;
361 int flags;
362 // These fields are for the cmd fds.
363 char* buffer;
364 int pos,size;
365 } mp_input_fd_t;
367 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
369 struct mp_cmd_filter_st {
370 mp_input_cmd_filter filter;
371 void* ctx;
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;
393 // Autorepeat stuff
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}
432 static int
433 mp_input_default_key_func(int fd);
435 static int
436 mp_input_default_cmd_func(int fd,char* buf, int l);
438 static char*
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);
446 return 0;
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;
453 if(!select)
454 cmd_fds[num_cmd_fd].flags = MP_FD_NO_SELECT;
455 num_cmd_fd++;
457 return 1;
460 void
461 mp_input_rm_cmd_fd(int fd) {
462 unsigned int i;
464 for(i = 0; i < num_cmd_fd; i++) {
465 if(cmd_fds[i].fd == fd)
466 break;
468 if(i == num_cmd_fd)
469 return;
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));
477 num_cmd_fd--;
480 void
481 mp_input_rm_key_fd(int fd) {
482 unsigned int i;
484 for(i = 0; i < num_key_fd; i++) {
485 if(key_fds[i].fd == fd)
486 break;
488 if(i == num_key_fd)
489 return;
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));
495 num_key_fd--;
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);
502 return 0;
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;
509 if(!select)
510 key_fds[num_key_fd].flags |= MP_FD_NO_SELECT;
511 num_key_fd++;
513 return 1;
518 mp_cmd_t*
519 mp_input_parse_cmd(char* str) {
520 int i,l;
521 char *ptr,*e;
522 mp_cmd_t *cmd, *cmd_def;
524 #ifdef MP_DEBUG
525 assert(str != NULL);
526 #endif
528 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
529 /* NOTHING */;
530 if(ptr[0] != '\0')
531 l = ptr-str;
532 else
533 l = strlen(str);
535 if(l == 0)
536 return NULL;
538 for(i=0; mp_cmds[i].name != NULL; i++) {
539 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
540 break;
543 if(mp_cmds[i].name == NULL)
544 return 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);
552 ptr = str;
554 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
555 ptr = strchr(ptr,' ');
556 if(!ptr) break;
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) {
561 case MP_CMD_ARG_INT:
562 errno = 0;
563 cmd->args[i].v.i = atoi(ptr);
564 if(errno != 0) {
565 mp_msg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",cmd_def->name,i+1);
566 ptr = NULL;
568 break;
569 case MP_CMD_ARG_FLOAT:
570 errno = 0;
571 /* <olo@altkom.com.pl> Use portable C locale for parsing floats: */
572 #ifdef USE_SETLOCALE
573 setlocale(LC_NUMERIC, "C");
574 #endif
575 cmd->args[i].v.f = atof(ptr);
576 #ifdef USE_SETLOCALE
577 setlocale(LC_NUMERIC, "");
578 #endif
579 if(errno != 0) {
580 mp_msg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",cmd_def->name,i+1);
581 ptr = NULL;
583 break;
584 case MP_CMD_ARG_STRING: {
585 char term;
586 char* ptr2 = ptr, *start;
588 if(ptr[0] == '\'' || ptr[0] == '"') {
589 term = ptr[0];
590 ptr2++;
591 } else
592 term = ' ';
593 start = ptr2;
594 while(1) {
595 e = strchr(ptr2,term);
596 if(!e) break;
597 if(e <= ptr2 || *(e - 1) != '\\') break;
598 ptr2 = e + 1;
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);
603 ptr = NULL;
604 break;
605 } else if(!e) e = ptr+strlen(ptr);
606 l = e-start;
607 ptr2 = start;
608 for(e = strchr(ptr2,'\\') ; e ; e = strchr(ptr2,'\\')) {
609 memmove(e,e+1,strlen(e));
610 ptr2 = e + 1;
611 l--;
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';
616 } break;
617 case -1:
618 ptr = NULL;
619 default :
620 mp_msg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i);
623 cmd->nargs = 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);
628 mp_cmd_free(cmd);
629 return NULL;
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;
641 return cmd;
644 static int
645 mp_input_default_key_func(int fd) {
646 int r,code=0;
647 unsigned int l;
648 l = 0;
649 while(l < sizeof(int)) {
650 r = read(fd,(&code)+l,sizeof(int)-l);
651 if(r <= 0)
652 break;
653 l +=r;
655 return code;
658 #define MP_CMD_MAX_SIZE 256
660 static int
661 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
662 char* end;
663 (*ret) = NULL;
665 // Allocate the buffer if it doesn't exist
666 if(!mp_fd->buffer) {
667 mp_fd->buffer = (char*)malloc(MP_CMD_MAX_SIZE*sizeof(char));
668 mp_fd->pos = 0;
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);
675 // Error ?
676 if(r < 0) {
677 switch(r) {
678 case MP_INPUT_ERROR:
679 case MP_INPUT_DEAD:
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:
682 return r;
684 // EOF ?
685 } else if(r == 0) {
686 mp_fd->flags |= MP_FD_EOF;
687 break;
689 mp_fd->pos += r;
690 break;
693 // Reset the got_cmd flag
694 mp_fd->flags &= ~MP_FD_GOT_CMD;
696 while(1) {
697 int l = 0;
698 // Find the cmd end
699 mp_fd->buffer[mp_fd->pos] = '\0';
700 end = strchr(mp_fd->buffer,'\n');
701 // No cmd end ?
702 if(!end) {
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);
706 mp_fd->pos = 0;
707 mp_fd->flags |= MP_FD_DROP;
709 break;
711 // We already have a cmd : set the got_cmd flag
712 else if((*ret)) {
713 mp_fd->flags |= MP_FD_GOT_CMD;
714 break;
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);
723 (*ret)[l] = '\0';
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));
729 mp_fd->pos -= l+1;
732 if(*ret)
733 return 1;
734 else
735 return MP_INPUT_NOTHING;
738 static int
739 mp_input_default_cmd_func(int fd,char* buf, int l) {
741 while(1) {
742 int r = read(fd,buf,l);
743 // Error ?
744 if(r < 0) {
745 if(errno == EINTR)
746 continue;
747 else if(errno == EAGAIN)
748 return MP_INPUT_NOTHING;
749 return MP_INPUT_ERROR;
750 // EOF ?
752 return r;
758 void
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;
763 filter->ctx = ctx;
764 filter->next = cmd_filters;
765 cmd_filters = filter;
769 static char*
770 mp_input_find_bind_for_key(mp_cmd_bind_t* binds, int n,int* keys) {
771 int j;
773 for(j = 0; binds[j].cmd != NULL; j++) {
774 if(n > 0) {
775 int found = 1,s;
776 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
777 if(binds[j].input[s] != keys[s]) {
778 found = 0;
779 break;
782 if(found && binds[j].input[s] == 0 && s == n)
783 break;
784 else
785 continue;
786 } else if(n == 1){
787 if(binds[j].input[0] == keys[0] && binds[j].input[1] == 0)
788 break;
791 return binds[j].cmd;
794 static mp_cmd_t*
795 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
796 char* cmd = NULL;
797 mp_cmd_t* ret;
799 if(cmd_binds)
800 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
801 if(cmd == NULL)
802 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
804 if(cmd == NULL) {
805 mp_msg(MSGT_INPUT,MSGL_WARN,"No bind found for key %s",mp_input_get_key_name(keys[0]));
806 if(n > 1) {
807 int s;
808 for(s=1; s < n; s++)
809 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
811 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
812 return NULL;
814 ret = mp_input_parse_cmd(cmd);
815 if(!ret) {
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) {
818 unsigned int s;
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);
824 return ret;
828 mp_input_read_key_code(int time) {
829 #ifndef HAVE_NO_POSIX_SELECT
830 fd_set fds;
831 struct timeval tv,*time_val;
832 #endif
833 int i,n=0,max_fd = 0;
834 static int last_loop = 0;
836 if(num_key_fd == 0)
837 return MP_INPUT_NOTHING;
839 #ifndef HAVE_NO_POSIX_SELECT
840 FD_ZERO(&fds);
841 #endif
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);
847 i--;
848 continue;
849 } else if(key_fds[i].flags & MP_FD_NO_SELECT)
850 continue;
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);
855 #endif
856 n++;
859 if(num_key_fd == 0)
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():
864 if(n>0){
866 if(time >= 0 ) {
867 tv.tv_sec=time/1000;
868 tv.tv_usec = (time%1000)*1000;
869 time_val = &tv;
870 } else
871 time_val = NULL;
873 while(1) {
874 if(select(max_fd+1,&fds,NULL,NULL,time_val) < 0) {
875 if(errno == EINTR)
876 continue;
877 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error: %s\n",strerror(errno));
879 break;
883 #endif
885 for(i = last_loop + 1 ; i != last_loop ; i++) {
886 int code = -1;
887 // This is to check all fds in turn
888 if((unsigned int)i >= num_key_fd) {
889 i = -1;
890 last_loop++;
891 last_loop %= (num_key_fd+1);
892 continue;
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))
897 continue;
898 #endif
899 if(key_fds[i].fd == 0) { // stdin is handled by getch2
900 code = getch2(time);
901 if(code < 0)
902 code = MP_INPUT_NOTHING;
904 else
905 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
907 if(code >= 0)
908 return code;
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;
921 static mp_cmd_t*
922 mp_input_read_keys(int time,int paused) {
923 int code = mp_input_read_key_code(time);
924 unsigned int j;
925 mp_cmd_t* ret;
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);
933 return NULL;
936 for( ; code >= 0 ; code = mp_input_read_key_code(0) ) {
937 // key pushed
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");
941 continue;
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)
947 break;
949 if(j != num_key_down)
950 continue;
951 key_down[num_key_down] = code;
952 num_key_down++;
953 last_key_down = GetTimer();
954 ar_state = 0;
955 continue;
957 // key released
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)
962 break;
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");
967 continue;
969 key_down[num_key_down] = code;
970 num_key_down++;
971 last_key_down = 1;
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;
975 // Remove the key
976 if(j+1 < num_key_down)
977 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
978 num_key_down--;
979 last_key_down = 0;
980 ar_state = -1;
981 if(ar_cmd) {
982 mp_cmd_free(ar_cmd);
983 ar_cmd = NULL;
985 if(ret)
986 return ret;
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);
995 if(!ar_cmd) {
996 ar_state = -1;
997 return NULL;
999 ar_state = 1;
1000 last_ar = t;
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) {
1004 last_ar = t;
1005 return mp_cmd_clone(ar_cmd);
1009 return NULL;
1012 static mp_cmd_t*
1013 mp_input_read_cmds(int time) {
1014 #ifndef HAVE_NO_POSIX_SELECT
1015 fd_set fds;
1016 struct timeval tv,*time_val;
1017 #endif
1018 int i,n = 0,max_fd = 0,got_cmd = 0;
1019 mp_cmd_t* ret;
1020 static int last_loop = 0;
1022 if(num_cmd_fd == 0)
1023 return NULL;
1025 #ifndef HAVE_NO_POSIX_SELECT
1026 FD_ZERO(&fds);
1027 #endif
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);
1031 i--;
1032 continue;
1033 } else if(cmd_fds[i].flags & MP_FD_NO_SELECT)
1034 continue;
1035 if(cmd_fds[i].flags & MP_FD_GOT_CMD)
1036 got_cmd = 1;
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);
1041 #endif
1042 n++;
1045 if(num_cmd_fd == 0)
1046 return NULL;
1048 #ifndef HAVE_NO_POSIX_SELECT
1049 if(time >= 0) {
1050 tv.tv_sec=time/1000;
1051 tv.tv_usec = (time%1000)*1000;
1052 time_val = &tv;
1053 } else
1054 time_val = NULL;
1056 while(n > 0) {
1057 if((i = select(max_fd+1,&fds,NULL,NULL,time_val)) <= 0) {
1058 if(i < 0) {
1059 if(errno == EINTR)
1060 continue;
1061 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error: %s\n",strerror(errno));
1063 if(!got_cmd)
1064 return NULL;
1066 break;
1068 #endif
1070 for(i = last_loop + 1; i != last_loop ; i++) {
1071 int r = 0;
1072 char* cmd;
1073 if((unsigned int)i >= num_cmd_fd) {
1074 i = -1;
1075 last_loop++;
1076 last_loop %= (num_cmd_fd+1);
1077 continue;
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) )
1081 continue;
1082 #endif
1084 r = mp_input_read_cmd(&cmd_fds[i],&cmd);
1085 if(r < 0) {
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;
1090 continue;
1092 ret = mp_input_parse_cmd(cmd);
1093 free(cmd);
1094 if(!ret)
1095 continue;
1096 last_loop = i;
1097 return ret;
1100 last_loop = 0;
1101 return NULL;
1105 mp_input_queue_cmd(mp_cmd_t* cmd) {
1106 if(cmd_queue_length >= CMD_QUEUE_SIZE)
1107 return 0;
1108 cmd_queue[cmd_queue_end] = cmd;
1109 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1110 cmd_queue_length++;
1111 return 1;
1114 static mp_cmd_t*
1115 mp_input_get_queued_cmd(void) {
1116 mp_cmd_t* ret;
1118 if(cmd_queue_length == 0)
1119 return NULL;
1121 ret = cmd_queue[cmd_queue_start];
1123 cmd_queue_length--;
1124 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1126 return ret;
1129 mp_cmd_t*
1130 mp_input_get_cmd(int time, int paused) {
1131 mp_cmd_t* ret = NULL;
1132 mp_cmd_filter_t* cf;
1134 while(1) {
1135 ret = mp_input_get_queued_cmd();
1136 if(ret) break;
1137 ret = mp_input_read_keys(time,paused);
1138 if(ret) break;
1139 ret = mp_input_read_cmds(time);
1140 break;
1142 if(!ret) return NULL;
1144 for(cf = cmd_filters ; cf ; cf = cf->next) {
1145 if(cf->filter(ret,paused,cf->ctx))
1146 return NULL;
1149 return ret;
1152 void
1153 mp_cmd_free(mp_cmd_t* cmd) {
1154 int i;
1155 //#ifdef MP_DEBUG
1156 // assert(cmd != NULL);
1157 //#endif
1158 if ( !cmd ) return;
1160 if(cmd->name)
1161 free(cmd->name);
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);
1167 free(cmd);
1170 mp_cmd_t*
1171 mp_cmd_clone(mp_cmd_t* cmd) {
1172 mp_cmd_t* ret;
1173 int i;
1174 #ifdef MP_DEBUG
1175 assert(cmd != NULL);
1176 #endif
1178 ret = (mp_cmd_t*)malloc(sizeof(mp_cmd_t));
1179 memcpy(ret,cmd,sizeof(mp_cmd_t));
1180 if(cmd->name)
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);
1187 return ret;
1190 static char key_str[12];
1192 static char*
1193 mp_input_get_key_name(int key) {
1194 int i;
1196 for(i = 0; key_names[i].name != NULL; i++) {
1197 if(key_names[i].key == key)
1198 return key_names[i].name;
1201 if(isascii(key)) {
1202 snprintf(key_str,12,"%c",(char)key);
1203 return key_str;
1206 // Print the hex key code
1207 snprintf(key_str,12,"%#-8x",key);
1208 return key_str;
1212 static int
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];
1217 return ret;
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;
1226 return -1;
1229 static int
1230 mp_input_get_input_from_name(char* name,int* keys) {
1231 char *end,*ptr;
1232 int n=0;
1234 ptr = name;
1235 n = 0;
1236 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1237 if(end && end[1] != '\0') {
1238 if(end[1] == '-')
1239 end = &end[1];
1240 end[0] = '\0';
1242 keys[n] = mp_input_get_key_from_name(ptr);
1243 if(keys[n] < 0) {
1244 return 0;
1246 n++;
1247 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1248 ptr = &end[1];
1249 else
1250 break;
1252 keys[n] = 0;
1253 return 1;
1256 void
1257 mp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1258 int i = 0,j;
1259 mp_cmd_bind_t* bind = NULL;
1261 #ifdef MP_DEBUG
1262 assert(keys != NULL);
1263 assert(cmd != NULL);
1264 #endif
1266 if(cmd_binds) {
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++)
1269 /* NOTHING */;
1270 if(keys[j] == 0 && cmd_binds[i].input[j] == 0 ) {
1271 bind = &cmd_binds[i];
1272 break;
1277 if(!bind) {
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];
1282 if(bind->cmd)
1283 free(bind->cmd);
1284 bind->cmd = strdup(cmd);
1285 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1289 static void
1290 mp_input_free_binds(mp_cmd_bind_t* binds) {
1291 int i;
1293 if(!binds)
1294 return;
1296 for(i = 0; binds[i].cmd != NULL; i++)
1297 free(binds[i].cmd);
1299 free(binds);
1304 #define BS_MAX 256
1305 #define SPACE_CHAR " \n\r\t"
1307 static int
1308 mp_input_parse_config(char *file) {
1309 int fd;
1310 int bs = 0,r,eof = 0,comments = 0;
1311 char *iter,*end;
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);
1318 if(fd < 0) {
1319 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't open input config file %s: %s\n",file,strerror(errno));
1320 return 0;
1323 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1325 while(1) {
1326 if(! eof && bs < BS_MAX-1) {
1327 if(bs > 0) bs--;
1328 r = read(fd,buffer+bs,BS_MAX-1-bs);
1329 if(r < 0) {
1330 if(errno == EINTR)
1331 continue;
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);
1334 close(fd);
1335 return 0;
1336 } else if(r == 0) {
1337 eof = 1;
1338 } else {
1339 bs += r+1;
1340 buffer[bs-1] = '\0';
1343 // Empty buffer : return
1344 if(bs <= 1) {
1345 mp_msg(MSGT_INPUT,MSGL_INFO,"Input config file %s parsed: %d binds\n",file,n_binds);
1346 if(binds)
1347 cmd_binds = binds;
1348 close(fd);
1349 return 1;
1352 iter = buffer;
1354 if(comments) {
1355 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1356 /* NOTHING */;
1357 if(iter[0] == '\0') { // Buffer was full of comment
1358 bs = 0;
1359 continue;
1361 iter++;
1362 r = strlen(iter);
1363 if(r)
1364 memmove(buffer,iter,r+1);
1365 bs = r+1;
1366 if(iter[0] != '#')
1367 comments = 0;
1368 continue;
1371 // Find the wanted key
1372 if(keys[0] == 0) {
1373 // Jump beginning space
1374 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1375 /* NOTHING */;
1376 if(iter[0] == '\0') { // Buffer was full of space char
1377 bs = 0;
1378 continue;
1380 if(iter[0] == '#') { // Comments
1381 comments = 1;
1382 continue;
1384 // Find the end of the key code name
1385 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1386 /*NOTHING */;
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);
1391 else
1392 mp_msg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter);
1393 mp_input_free_binds(binds);
1394 return 0;
1396 memmove(buffer,iter,end-iter);
1397 bs = end-iter;
1398 continue;
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);
1407 close(fd);
1408 return 0;
1411 if( bs > (end-buffer))
1412 memmove(buffer,end,bs - (end-buffer));
1413 bs -= end-buffer;
1414 continue;
1415 } else { // Get the command
1416 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1417 // Found new line
1418 if(iter[0] == '\n' || iter[0] == '\r') {
1419 int i;
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");
1424 keys[0] = 0;
1425 if(iter > buffer) {
1426 memmove(buffer,iter,bs- (iter-buffer));
1427 bs -= (iter-buffer);
1429 continue;
1431 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1432 /* NOTHING */;
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);
1437 close(fd);
1438 return 0;
1440 memmove(buffer,iter,end - iter);
1441 bs = end - iter;
1442 continue;
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);
1450 n_binds++;
1452 keys[0] = 0;
1453 end++;
1454 if(bs > (end-buffer))
1455 memmove(buffer,end,bs-(end-buffer));
1456 bs -= (end-buffer);
1457 buffer[bs-1] = '\0';
1458 continue;
1461 mp_msg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n");
1462 close(fd);
1463 return 0;
1466 extern char *get_path(char *filename);
1468 void
1469 mp_input_init(void) {
1470 char* file;
1472 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1473 if(!file)
1474 return;
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
1484 if(use_joystick) {
1485 int fd = mp_input_joystick_init(js_dev);
1486 if(fd < 0)
1487 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n");
1488 else
1489 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1491 #endif
1493 #ifdef HAVE_LIRC
1494 if(use_lirc) {
1495 int fd = mp_input_lirc_init();
1496 if(fd > 0)
1497 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1499 #endif
1501 #ifdef HAVE_LIRCC
1502 if(use_lircc) {
1503 int fd = lircc_init("mplayer", NULL);
1504 if(fd >= 0)
1505 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1507 #endif
1509 if(in_file) {
1510 struct stat st;
1511 if(stat(in_file,&st))
1512 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't stat %s: %s\n",in_file,strerror(errno));
1513 else {
1514 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1515 if(in_file_fd >= 0)
1516 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1517 else
1518 mp_msg(MSGT_INPUT,MSGL_ERR,"Can't open %s: %s\n",in_file,strerror(errno));
1524 void
1525 mp_input_uninit(void) {
1526 unsigned int i;
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);
1540 void
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) {
1546 int i;
1547 printf("\n");
1548 for(i= 0; key_names[i].name != NULL ; i++)
1549 printf("%s\n",key_names[i].name);
1550 exit(0);
1553 static int mp_input_print_cmd_list(m_option_t* cfg) {
1554 mp_cmd_t *cmd;
1555 int i,j;
1556 char* type;
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:
1563 type = "Integer";
1564 break;
1565 case MP_CMD_ARG_FLOAT:
1566 type = "Float";
1567 break;
1568 case MP_CMD_ARG_STRING:
1569 type = "String";
1570 break;
1571 default:
1572 type = "??";
1574 if(j+1 > cmd->nargs)
1575 printf(" [%s]",type);
1576 else
1577 printf(" %s",type);
1579 printf("\n");
1581 exit(0);
1585 mp_input_check_interrupt(int time) {
1586 mp_cmd_t* cmd;
1587 if((cmd = mp_input_get_cmd(time,0)) == NULL)
1588 return 0;
1589 switch(cmd->id) {
1590 case MP_CMD_QUIT:
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);
1597 mp_cmd_free(cmd);
1599 return 1;
1601 mp_cmd_free(cmd);
1602 return 0;