10l: comparison of char* ptrs with string literals
[mplayer.git] / input / input.c
blob1f04273e31120bde3517b2857589ff75d1bcd7ea
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 "help_mp.h"
25 #include "m_config.h"
26 #include "m_option.h"
28 #include "joystick.h"
30 #ifdef HAVE_LIRC
31 #include "lirc.h"
32 #endif
34 #ifdef HAVE_LIRCC
35 #include <lirc/lircc.h>
36 #endif
38 /// This array defines all known commands.
39 /// The first field is an id used to recognize the command without too many strcmp.
40 /// The second is obviously the command name.
41 /// The third is the minimum number of arguments this command needs.
42 /// Then comes the definition of each argument, terminated with an arg of type -1.
43 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
44 /// the last one) which is actually 9.
46 /// For the args, the first field is the type (actually int, float or string), the second
47 /// is the default value wich is used for optional arguments
49 static mp_cmd_t mp_cmds[] = {
50 #ifdef USE_RADIO
51 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
52 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
53 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
54 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
55 #endif
56 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
57 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
58 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
59 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
60 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
61 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
62 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
63 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
64 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
65 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
66 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
67 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
68 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
69 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
70 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
71 { MP_CMD_OSD_SHOW_TEXT, "osd_show_text", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
72 { MP_CMD_OSD_SHOW_PROPERTY_TEXT, "osd_show_property_text",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{-1}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
73 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
74 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
75 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
76 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
77 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
78 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
79 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
80 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
81 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
82 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
83 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
84 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
85 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
86 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
87 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
88 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
89 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
90 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
91 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
92 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
93 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
94 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
95 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
96 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
97 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
98 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
99 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
100 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
101 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
102 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
103 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
104 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
105 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
106 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
107 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
108 #ifdef USE_TV
109 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
110 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
111 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
112 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
113 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
114 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
115 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
116 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
117 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
118 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
119 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
120 #endif
121 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 #ifdef HAS_DVBIN_SUPPORT
123 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
124 #endif
125 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
126 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
127 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
128 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
129 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
130 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
131 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
132 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
133 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
134 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
135 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
136 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
137 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
139 #ifdef HAVE_NEW_GUI
140 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
141 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
142 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
143 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
144 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
145 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
146 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
147 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
148 #endif
150 #ifdef USE_DVDNAV
151 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
152 #endif
154 #ifdef HAVE_MENU
155 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
156 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
157 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
158 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
159 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
160 #endif
162 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
163 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
164 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
165 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
166 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
167 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
169 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
170 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
172 { 0, NULL, 0, {} }
175 /// The names of the keys as used in input.conf
176 /// If you add some new keys, you also need to add them here
178 static mp_key_name_t key_names[] = {
179 { ' ', "SPACE" },
180 { KEY_ENTER, "ENTER" },
181 { KEY_TAB, "TAB" },
182 { KEY_CTRL, "CTRL" },
183 { KEY_BACKSPACE, "BS" },
184 { KEY_DELETE, "DEL" },
185 { KEY_INSERT, "INS" },
186 { KEY_HOME, "HOME" },
187 { KEY_END, "END" },
188 { KEY_PAGE_UP, "PGUP" },
189 { KEY_PAGE_DOWN, "PGDWN" },
190 { KEY_ESC, "ESC" },
191 { KEY_RIGHT, "RIGHT" },
192 { KEY_LEFT, "LEFT" },
193 { KEY_DOWN, "DOWN" },
194 { KEY_UP, "UP" },
195 { KEY_F+1, "F1" },
196 { KEY_F+2, "F2" },
197 { KEY_F+3, "F3" },
198 { KEY_F+4, "F4" },
199 { KEY_F+5, "F5" },
200 { KEY_F+6, "F6" },
201 { KEY_F+7, "F7" },
202 { KEY_F+8, "F8" },
203 { KEY_F+9, "F9" },
204 { KEY_F+10, "F10" },
205 { KEY_F+11, "F11" },
206 { KEY_F+12, "F12" },
207 { KEY_KP0, "KP0" },
208 { KEY_KP1, "KP1" },
209 { KEY_KP2, "KP2" },
210 { KEY_KP3, "KP3" },
211 { KEY_KP4, "KP4" },
212 { KEY_KP5, "KP5" },
213 { KEY_KP6, "KP6" },
214 { KEY_KP7, "KP7" },
215 { KEY_KP8, "KP8" },
216 { KEY_KP9, "KP9" },
217 { KEY_KPDEL, "KP_DEL" },
218 { KEY_KPDEC, "KP_DEC" },
219 { KEY_KPINS, "KP_INS" },
220 { KEY_KPENTER, "KP_ENTER" },
221 { MOUSE_BTN0, "MOUSE_BTN0" },
222 { MOUSE_BTN1, "MOUSE_BTN1" },
223 { MOUSE_BTN2, "MOUSE_BTN2" },
224 { MOUSE_BTN3, "MOUSE_BTN3" },
225 { MOUSE_BTN4, "MOUSE_BTN4" },
226 { MOUSE_BTN5, "MOUSE_BTN5" },
227 { MOUSE_BTN6, "MOUSE_BTN6" },
228 { MOUSE_BTN7, "MOUSE_BTN7" },
229 { MOUSE_BTN8, "MOUSE_BTN8" },
230 { MOUSE_BTN9, "MOUSE_BTN9" },
231 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
232 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
233 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
234 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
235 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
236 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
237 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
238 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
239 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
240 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
241 { JOY_AXIS1_MINUS, "JOY_UP" },
242 { JOY_AXIS1_PLUS, "JOY_DOWN" },
243 { JOY_AXIS0_MINUS, "JOY_LEFT" },
244 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
246 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
247 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
248 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
249 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
250 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
251 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
252 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
253 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
254 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
255 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
256 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
257 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
258 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
259 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
260 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
261 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
262 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
263 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
264 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
265 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
267 { JOY_BTN0, "JOY_BTN0" },
268 { JOY_BTN1, "JOY_BTN1" },
269 { JOY_BTN2, "JOY_BTN2" },
270 { JOY_BTN3, "JOY_BTN3" },
271 { JOY_BTN4, "JOY_BTN4" },
272 { JOY_BTN5, "JOY_BTN5" },
273 { JOY_BTN6, "JOY_BTN6" },
274 { JOY_BTN7, "JOY_BTN7" },
275 { JOY_BTN8, "JOY_BTN8" },
276 { JOY_BTN9, "JOY_BTN9" },
278 { KEY_POWER, "POWER" },
279 { KEY_MENU, "MENU" },
280 { KEY_PLAY, "PLAY" },
281 { KEY_PAUSE, "PAUSE" },
282 { KEY_PLAYPAUSE, "PLAYPAUSE" },
283 { KEY_STOP, "STOP" },
284 { KEY_FORWARD, "FORWARD" },
285 { KEY_REWIND, "REWIND" },
286 { KEY_NEXT, "NEXT" },
287 { KEY_PREV, "PREV" },
288 { KEY_VOLUME_UP, "VOLUME_UP" },
289 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
290 { KEY_MUTE, "MUTE" },
292 // These are kept for backward compatibility
293 { KEY_PAUSE, "XF86_PAUSE" },
294 { KEY_STOP, "XF86_STOP" },
295 { KEY_PREV, "XF86_PREV" },
296 { KEY_NEXT, "XF86_NEXT" },
298 { KEY_CLOSE_WIN, "CLOSE_WIN" },
300 { 0, NULL }
303 // This is the default binding. The content of input.conf overrides these.
304 // The first arg is a null terminated array of key codes.
305 // The second is the command
307 static mp_cmd_bind_t def_cmd_binds[] = {
309 { { MOUSE_BTN3, 0 }, "seek 10" },
310 { { MOUSE_BTN4, 0 }, "seek -10" },
311 { { MOUSE_BTN5, 0 }, "volume 1" },
312 { { MOUSE_BTN6, 0 }, "volume -1" },
314 #ifdef USE_DVDNAV
315 { { KEY_KP8, 0 }, "dvdnav 1" }, // up
316 { { KEY_KP2, 0 }, "dvdnav 2" }, // down
317 { { KEY_KP4, 0 }, "dvdnav 3" }, // left
318 { { KEY_KP6, 0 }, "dvdnav 4" }, // right
319 { { KEY_KP5, 0 }, "dvdnav 5" }, // menu
320 { { KEY_KPENTER, 0 }, "dvdnav 6" }, // select
321 { { MOUSE_BTN0, 0 }, "dvdnav 8" }, //select
322 { { KEY_KP7, 0 }, "dvdnav 7" }, // previous menu
323 #endif
325 { { KEY_RIGHT, 0 }, "seek 10" },
326 { { KEY_LEFT, 0 }, "seek -10" },
327 { { KEY_UP, 0 }, "seek 60" },
328 { { KEY_DOWN, 0 }, "seek -60" },
329 { { KEY_PAGE_UP, 0 }, "seek 600" },
330 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
331 { { '+', 0 }, "audio_delay 0.100" },
332 { { '-', 0 }, "audio_delay -0.100" },
333 { { '[', 0 }, "speed_mult 0.9091" },
334 { { ']', 0 }, "speed_mult 1.1" },
335 { { '{', 0 }, "speed_mult 0.5" },
336 { { '}', 0 }, "speed_mult 2.0" },
337 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
338 { { 'q', 0 }, "quit" },
339 { { KEY_ESC, 0 }, "quit" },
340 { { 'p', 0 }, "pause" },
341 { { ' ', 0 }, "pause" },
342 { { '.', 0 }, "frame_step" },
343 { { KEY_HOME, 0 }, "pt_up_step 1" },
344 { { KEY_END, 0 }, "pt_up_step -1" },
345 { { '>', 0 }, "pt_step 1" },
346 { { KEY_ENTER, 0 }, "pt_step 1 1" },
347 { { '<', 0 }, "pt_step -1" },
348 { { KEY_INS, 0 }, "alt_src_step 1" },
349 { { KEY_DEL, 0 }, "alt_src_step -1" },
350 { { 'o', 0 }, "osd" },
351 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
352 { { 'z', 0 }, "sub_delay -0.1" },
353 { { 'x', 0 }, "sub_delay +0.1" },
354 { { 'g', 0 }, "sub_step -1" },
355 { { 'y', 0 }, "sub_step +1" },
356 { { '9', 0 }, "volume -1" },
357 { { '/', 0 }, "volume -1" },
358 { { '0', 0 }, "volume 1" },
359 { { '*', 0 }, "volume 1" },
360 { { 'm', 0 }, "mute" },
361 { { '1', 0 }, "contrast -1" },
362 { { '2', 0 }, "contrast 1" },
363 { { '3', 0 }, "brightness -1" },
364 { { '4', 0 }, "brightness 1" },
365 { { '5', 0 }, "hue -1" },
366 { { '6', 0 }, "hue 1" },
367 { { '7', 0 }, "saturation -1" },
368 { { '8', 0 }, "saturation 1" },
369 { { 'd', 0 }, "frame_drop" },
370 { { 'D', 0 }, "step_property deinterlace" },
371 { { 'r', 0 }, "sub_pos -1" },
372 { { 't', 0 }, "sub_pos +1" },
373 { { 'a', 0 }, "sub_alignment" },
374 { { 'v', 0 }, "sub_visibility" },
375 { { 'j', 0 }, "sub_select" },
376 { { 'F', 0 }, "forced_subs_only" },
377 { { '#', 0 }, "switch_audio" },
378 { { '_', 0 }, "step_property switch_video" },
379 { { KEY_TAB, 0 }, "step_property switch_program" },
380 { { 'i', 0 }, "edl_mark" },
381 #ifdef USE_TV
382 { { 'h', 0 }, "tv_step_channel 1" },
383 { { 'k', 0 }, "tv_step_channel -1" },
384 { { 'n', 0 }, "tv_step_norm" },
385 { { 'u', 0 }, "tv_step_chanlist" },
386 #endif
387 #ifdef HAVE_JOYSTICK
388 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
389 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
390 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
391 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
392 { { JOY_BTN0, 0 }, "pause" },
393 { { JOY_BTN1, 0 }, "osd" },
394 { { JOY_BTN2, 0 }, "volume 1"},
395 { { JOY_BTN3, 0 }, "volume -1"},
396 #endif
397 { { 'T', 0 }, "vo_ontop" },
398 { { 'f', 0 }, "vo_fullscreen" },
399 { { 's', 0 }, "screenshot 0" },
400 { { 'S', 0 }, "screenshot 1" },
401 { { 'w', 0 }, "panscan -0.1" },
402 { { 'e', 0 }, "panscan +0.1" },
404 { { KEY_POWER, 0 }, "quit" },
405 { { KEY_MENU, 0 }, "osd" },
406 { { KEY_PLAY, 0 }, "pause" },
407 { { KEY_PAUSE, 0 }, "pause" },
408 { { KEY_PLAYPAUSE, 0 }, "pause" },
409 { { KEY_STOP, 0 }, "quit" },
410 { { KEY_FORWARD, 0 }, "seek 60" },
411 { { KEY_REWIND, 0 }, "seek -60" },
412 { { KEY_NEXT, 0 }, "pt_step 1" },
413 { { KEY_PREV, 0 }, "pt_step -1" },
414 { { KEY_VOLUME_UP, 0 }, "volume 1" },
415 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
416 { { KEY_MUTE, 0 }, "mute" },
418 { { KEY_CLOSE_WIN, 0 }, "quit" },
420 { { '!', 0 }, "seek_chapter -1" },
421 { { '@', 0 }, "seek_chapter 1" },
423 { { 0 }, NULL }
427 #ifdef HAVE_NEW_GUI
428 static mp_cmd_bind_t gui_def_cmd_binds[] = {
430 { { 'l', 0 }, "gui_loadfile" },
431 { { 't', 0 }, "gui_loadsubtitle" },
432 { { KEY_ENTER, 0 }, "gui_play" },
433 { { KEY_ESC, 0 }, "gui_stop" },
434 { { 'p', 0 }, "gui_playlist" },
435 { { 'r', 0 }, "gui_preferences" },
436 { { 'c', 0 }, "gui_skinbrowser" },
438 { { 0 }, NULL }
440 #endif
442 #ifndef MP_MAX_KEY_FD
443 #define MP_MAX_KEY_FD 10
444 #endif
446 #ifndef MP_MAX_CMD_FD
447 #define MP_MAX_CMD_FD 10
448 #endif
450 #define MP_FD_EOF (1<<0)
451 #define MP_FD_DROP (1<<1)
452 #define MP_FD_DEAD (1<<2)
453 #define MP_FD_GOT_CMD (1<<3)
454 #define MP_FD_NO_SELECT (1<<4)
456 #define CMD_QUEUE_SIZE 100
458 typedef struct mp_input_fd {
459 int fd;
460 void* read_func;
461 mp_close_func_t close_func;
462 int flags;
463 // These fields are for the cmd fds.
464 char* buffer;
465 int pos,size;
466 } mp_input_fd_t;
468 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
470 struct mp_cmd_filter_st {
471 mp_input_cmd_filter filter;
472 void* ctx;
473 mp_cmd_filter_t* next;
476 // These are the user defined binds
477 static mp_cmd_bind_t* cmd_binds = NULL;
478 static mp_cmd_filter_t* cmd_filters = NULL;
480 // Callback to allow the menu filter to grab the incoming keys
481 void (*mp_input_key_cb)(int code) = NULL;
483 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
484 static unsigned int num_key_fd = 0;
485 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
486 static unsigned int num_cmd_fd = 0;
487 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
488 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
490 // this is the key currently down
491 static int key_down[MP_MAX_KEY_DOWN];
492 static unsigned int num_key_down = 0, last_key_down = 0;
494 // Autorepeat stuff
495 static short ar_state = -1;
496 static mp_cmd_t* ar_cmd = NULL;
497 static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;
499 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
500 static char* config_file = "input.conf";
502 static char* js_dev = NULL;
504 static char* in_file = NULL;
505 static int in_file_fd = -1;
507 static int mp_input_print_key_list(m_option_t* cfg);
508 static int mp_input_print_cmd_list(m_option_t* cfg);
510 // Our command line options
511 static m_option_t input_conf[] = {
512 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
513 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
514 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
515 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
516 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
517 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
518 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
519 { NULL, NULL, 0, 0, 0, 0, NULL}
522 static m_option_t mp_input_opts[] = {
523 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
524 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
525 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
526 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
527 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
528 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
529 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
530 { NULL, NULL, 0, 0, 0, 0, NULL}
533 static int
534 mp_input_default_key_func(int fd);
536 static int
537 mp_input_default_cmd_func(int fd,char* buf, int l);
539 static char*
540 mp_input_get_key_name(int key);
544 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
545 if(num_cmd_fd == MP_MAX_CMD_FD) {
546 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
547 return 0;
550 memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t));
551 cmd_fds[num_cmd_fd].fd = fd;
552 cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func;
553 cmd_fds[num_cmd_fd].close_func = close_func;
554 if(!select)
555 cmd_fds[num_cmd_fd].flags = MP_FD_NO_SELECT;
556 num_cmd_fd++;
558 return 1;
561 void
562 mp_input_rm_cmd_fd(int fd) {
563 unsigned int i;
565 for(i = 0; i < num_cmd_fd; i++) {
566 if(cmd_fds[i].fd == fd)
567 break;
569 if(i == num_cmd_fd)
570 return;
571 if(cmd_fds[i].close_func)
572 cmd_fds[i].close_func(cmd_fds[i].fd);
573 if(cmd_fds[i].buffer)
574 free(cmd_fds[i].buffer);
576 if(i + 1 < num_cmd_fd)
577 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
578 num_cmd_fd--;
581 void
582 mp_input_rm_key_fd(int fd) {
583 unsigned int i;
585 for(i = 0; i < num_key_fd; i++) {
586 if(key_fds[i].fd == fd)
587 break;
589 if(i == num_key_fd)
590 return;
591 if(key_fds[i].close_func)
592 key_fds[i].close_func(key_fds[i].fd);
594 if(i + 1 < num_key_fd)
595 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
596 num_key_fd--;
600 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) {
601 if(num_key_fd == MP_MAX_KEY_FD) {
602 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
603 return 0;
606 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
607 key_fds[num_key_fd].fd = fd;
608 key_fds[num_key_fd].read_func = read_func ? read_func : mp_input_default_key_func;
609 key_fds[num_key_fd].close_func = close_func;
610 if(!select)
611 key_fds[num_key_fd].flags |= MP_FD_NO_SELECT;
612 num_key_fd++;
614 return 1;
619 mp_cmd_t*
620 mp_input_parse_cmd(char* str) {
621 int i,l;
622 int pausing = 0;
623 char *ptr,*e;
624 mp_cmd_t *cmd, *cmd_def;
626 #ifdef MP_DEBUG
627 assert(str != NULL);
628 #endif
630 if (strncmp(str, "pausing ", 8) == 0) {
631 pausing = 1;
632 str = &str[8];
633 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
634 pausing = 2;
635 str = &str[13];
636 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
637 pausing = 3;
638 str = &str[15];
641 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
642 /* NOTHING */;
643 if(ptr[0] != '\0')
644 l = ptr-str;
645 else
646 l = strlen(str);
648 if(l == 0)
649 return NULL;
651 for(i=0; mp_cmds[i].name != NULL; i++) {
652 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
653 break;
656 if(mp_cmds[i].name == NULL)
657 return NULL;
659 cmd_def = &mp_cmds[i];
661 cmd = calloc(1, sizeof(mp_cmd_t));
662 cmd->id = cmd_def->id;
663 cmd->name = strdup(cmd_def->name);
664 cmd->pausing = pausing;
666 ptr = str;
668 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
669 ptr = strchr(ptr,' ');
670 if(!ptr) break;
671 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
672 if(ptr[0] == '\0') break;
673 cmd->args[i].type = cmd_def->args[i].type;
674 switch(cmd_def->args[i].type) {
675 case MP_CMD_ARG_INT:
676 errno = 0;
677 cmd->args[i].v.i = atoi(ptr);
678 if(errno != 0) {
679 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
680 ptr = NULL;
682 break;
683 case MP_CMD_ARG_FLOAT:
684 errno = 0;
685 cmd->args[i].v.f = atof(ptr);
686 if(errno != 0) {
687 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
688 ptr = NULL;
690 break;
691 case MP_CMD_ARG_STRING: {
692 char term;
693 char* ptr2 = ptr, *start;
695 if(ptr[0] == '\'' || ptr[0] == '"') {
696 term = ptr[0];
697 ptr2++;
698 } else
699 term = ' ';
700 start = ptr2;
701 while(1) {
702 e = strchr(ptr2,term);
703 if(!e) break;
704 if(e <= ptr2 || *(e - 1) != '\\') break;
705 ptr2 = e + 1;
708 if(term != ' ' && (!e || e[0] == '\0')) {
709 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
710 ptr = NULL;
711 break;
712 } else if(!e) e = ptr+strlen(ptr);
713 l = e-start;
714 ptr2 = start;
715 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
716 memmove(e,e+1,strlen(e));
717 ptr2 = e + 1;
718 l--;
720 cmd->args[i].v.s = malloc(l+1);
721 strncpy(cmd->args[i].v.s,start,l);
722 cmd->args[i].v.s[l] = '\0';
723 if(term != ' ') ptr += l+2;
724 } break;
725 case -1:
726 ptr = NULL;
727 break;
728 default :
729 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
732 cmd->nargs = i;
734 if(cmd_def->nargs > cmd->nargs) {
735 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
736 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
737 mp_cmd_free(cmd);
738 return NULL;
741 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
742 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
743 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
744 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
747 if(i < MP_CMD_MAX_ARGS)
748 cmd->args[i].type = -1;
750 return cmd;
753 static int
754 mp_input_default_key_func(int fd) {
755 int r,code=0;
756 unsigned int l;
757 l = 0;
758 while(l < sizeof(int)) {
759 r = read(fd,((char *)&code)+l,sizeof(int)-l);
760 if(r <= 0)
761 break;
762 l +=r;
764 return code;
767 #define MP_CMD_MAX_SIZE 256
769 static int
770 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
771 char* end;
772 (*ret) = NULL;
774 // Allocate the buffer if it doesn't exist
775 if(!mp_fd->buffer) {
776 mp_fd->buffer = malloc(MP_CMD_MAX_SIZE);
777 mp_fd->pos = 0;
778 mp_fd->size = MP_CMD_MAX_SIZE;
781 // Get some data if needed/possible
782 while( !(mp_fd->flags & MP_FD_GOT_CMD) && !(mp_fd->flags & MP_FD_EOF) && (mp_fd->size - mp_fd->pos > 1) ) {
783 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);
784 // Error ?
785 if(r < 0) {
786 switch(r) {
787 case MP_INPUT_ERROR:
788 case MP_INPUT_DEAD:
789 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
790 case MP_INPUT_NOTHING:
791 return r;
792 case MP_INPUT_RETRY:
793 continue;
795 // EOF ?
796 } else if(r == 0) {
797 mp_fd->flags |= MP_FD_EOF;
798 break;
800 mp_fd->pos += r;
801 break;
804 // Reset the got_cmd flag
805 mp_fd->flags &= ~MP_FD_GOT_CMD;
807 while(1) {
808 int l = 0;
809 // Find the cmd end
810 mp_fd->buffer[mp_fd->pos] = '\0';
811 end = strchr(mp_fd->buffer,'\n');
812 // No cmd end ?
813 if(!end) {
814 // If buffer is full we must drop all until the next \n
815 if(mp_fd->size - mp_fd->pos <= 1) {
816 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
817 mp_fd->pos = 0;
818 mp_fd->flags |= MP_FD_DROP;
820 break;
822 // We already have a cmd : set the got_cmd flag
823 else if((*ret)) {
824 mp_fd->flags |= MP_FD_GOT_CMD;
825 break;
828 l = end - mp_fd->buffer;
830 // Not dropping : put the cmd in ret
831 if( ! (mp_fd->flags & MP_FD_DROP)) {
832 (*ret) = malloc(l+1);
833 strncpy((*ret),mp_fd->buffer,l);
834 (*ret)[l] = '\0';
835 } else { // Remove the dropping flag
836 mp_fd->flags &= ~MP_FD_DROP;
838 if( mp_fd->pos - (l+1) > 0)
839 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
840 mp_fd->pos -= l+1;
843 if(*ret)
844 return 1;
845 else
846 return MP_INPUT_NOTHING;
849 static int
850 mp_input_default_cmd_func(int fd,char* buf, int l) {
852 while(1) {
853 int r = read(fd,buf,l);
854 // Error ?
855 if(r < 0) {
856 if(errno == EINTR)
857 continue;
858 else if(errno == EAGAIN)
859 return MP_INPUT_NOTHING;
860 return MP_INPUT_ERROR;
861 // EOF ?
863 return r;
869 void
870 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
871 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
873 filter->filter = func;
874 filter->ctx = ctx;
875 filter->next = cmd_filters;
876 cmd_filters = filter;
880 static char*
881 mp_input_find_bind_for_key(mp_cmd_bind_t* binds, int n,int* keys) {
882 int j;
884 if (n <= 0) return NULL;
885 for(j = 0; binds[j].cmd != NULL; j++) {
886 int found = 1,s;
887 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
888 if(binds[j].input[s] != keys[s]) {
889 found = 0;
890 break;
893 if(found && binds[j].input[s] == 0 && s == n)
894 break;
896 return binds[j].cmd;
899 static mp_cmd_t*
900 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
901 char* cmd = NULL;
902 mp_cmd_t* ret;
904 if(cmd_binds)
905 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
906 if(cmd == NULL)
907 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
909 if(cmd == NULL) {
910 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound,mp_input_get_key_name(keys[0]));
911 if(n > 1) {
912 int s;
913 for(s=1; s < n; s++)
914 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
916 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
917 return NULL;
919 if (strcmp(cmd, "ignore") == 0) return NULL;
920 ret = mp_input_parse_cmd(cmd);
921 if(!ret) {
922 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,mp_input_get_key_name(key_down[0]));
923 if( num_key_down > 1) {
924 unsigned int s;
925 for(s=1; s < num_key_down; s++)
926 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(key_down[s]));
928 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
930 return ret;
934 mp_input_read_key_code(int time) {
935 #ifndef HAVE_NO_POSIX_SELECT
936 fd_set fds;
937 struct timeval tv,*time_val;
938 #endif
939 int i,n=0,max_fd = 0, did_sleep = 0;
940 static int last_loop = 0;
942 if(num_key_fd == 0)
944 if (time)
945 usec_sleep(time * 1000);
946 return MP_INPUT_NOTHING;
949 #ifndef HAVE_NO_POSIX_SELECT
950 FD_ZERO(&fds);
951 #endif
952 // Remove fd marked as dead and build the fd_set
953 // n == number of fd's to be select() checked
954 for(i = 0; (unsigned int)i < num_key_fd; i++) {
955 if( (key_fds[i].flags & MP_FD_DEAD) ) {
956 mp_input_rm_key_fd(key_fds[i].fd);
957 i--;
958 continue;
959 } else if(key_fds[i].flags & MP_FD_NO_SELECT)
960 continue;
961 if(key_fds[i].fd > max_fd)
962 max_fd = key_fds[i].fd;
963 #ifndef HAVE_NO_POSIX_SELECT
964 FD_SET(key_fds[i].fd,&fds);
965 #endif
966 n++;
969 #ifndef HAVE_NO_POSIX_SELECT
970 // if we have fd's without MP_FD_NO_SELECT flag, call select():
971 if(n>0){
973 if(time >= 0 ) {
974 tv.tv_sec=time/1000;
975 tv.tv_usec = (time%1000)*1000;
976 time_val = &tv;
977 } else
978 time_val = NULL;
980 while(1) {
981 if(select(max_fd+1,&fds,NULL,NULL,time_val) < 0) {
982 if(errno == EINTR)
983 continue;
984 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrSelect,strerror(errno));
986 break;
988 did_sleep = 1;
991 #endif
993 for(i = last_loop + 1 ; i != last_loop ; i++) {
994 int code = -1;
995 // This is to check all fds in turn
996 if((unsigned int)i >= num_key_fd) {
997 i = -1;
998 last_loop++;
999 last_loop %= (num_key_fd+1);
1000 continue;
1002 #ifndef HAVE_NO_POSIX_SELECT
1003 // No input from this fd
1004 if(! (key_fds[i].flags & MP_FD_NO_SELECT) && ! FD_ISSET(key_fds[i].fd,&fds) && key_fds[i].fd != 0)
1005 continue;
1006 #endif
1007 if(key_fds[i].fd == 0) { // stdin is handled by getch2
1008 code = getch2(time);
1009 if(code < 0)
1010 code = MP_INPUT_NOTHING;
1011 did_sleep = 1;
1013 else
1014 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
1016 if(code >= 0)
1017 return code;
1019 if(code == MP_INPUT_ERROR)
1020 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrOnKeyInFd,key_fds[i].fd);
1021 else if(code == MP_INPUT_DEAD) {
1022 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,key_fds[i].fd);
1023 key_fds[i].flags |= MP_FD_DEAD;
1026 if (time && !did_sleep)
1027 usec_sleep(time * 1000);
1028 return MP_INPUT_NOTHING;
1032 static mp_cmd_t*
1033 mp_input_read_keys(int time,int paused) {
1034 int code = mp_input_read_key_code(time);
1035 unsigned int j;
1036 mp_cmd_t* ret;
1038 if(mp_input_key_cb) {
1039 for( ; code >= 0 ; code = mp_input_read_key_code(0) ) {
1040 if(code & MP_KEY_DOWN) continue;
1041 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1042 mp_input_key_cb(code);
1044 return NULL;
1047 for( ; code >= 0 ; code = mp_input_read_key_code(0) ) {
1048 // key pushed
1049 if(code & MP_KEY_DOWN) {
1050 if(num_key_down > MP_MAX_KEY_DOWN) {
1051 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1052 continue;
1054 code &= ~MP_KEY_DOWN;
1055 // Check if we don't already have this key as pushed
1056 for(j = 0; j < num_key_down; j++) {
1057 if(key_down[j] == code)
1058 break;
1060 if(j != num_key_down)
1061 continue;
1062 key_down[num_key_down] = code;
1063 num_key_down++;
1064 last_key_down = GetTimer();
1065 ar_state = 0;
1066 continue;
1068 // key released
1069 // Check if the key is in the down key, driver which can't send push event
1070 // send only release event
1071 for(j = 0; j < num_key_down; j++) {
1072 if(key_down[j] == code)
1073 break;
1075 if(j == num_key_down) { // key was not in the down keys : add it
1076 if(num_key_down > MP_MAX_KEY_DOWN) {
1077 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1078 continue;
1080 key_down[num_key_down] = code;
1081 num_key_down++;
1082 last_key_down = 1;
1084 // We ignore key from last combination
1085 ret = last_key_down ? mp_input_get_cmd_from_keys(num_key_down,key_down,paused) : NULL;
1086 // Remove the key
1087 if(j+1 < num_key_down)
1088 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1089 num_key_down--;
1090 last_key_down = 0;
1091 ar_state = -1;
1092 if(ar_cmd) {
1093 mp_cmd_free(ar_cmd);
1094 ar_cmd = NULL;
1096 if(ret)
1097 return ret;
1100 // No input : autorepeat ?
1101 if(ar_rate > 0 && ar_state >=0 && num_key_down > 0 && ! (key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1102 unsigned int t = GetTimer();
1103 // First time : wait delay
1104 if(ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1105 ar_cmd = mp_input_get_cmd_from_keys(num_key_down,key_down,paused);
1106 if(!ar_cmd) {
1107 ar_state = -1;
1108 return NULL;
1110 ar_state = 1;
1111 last_ar = t;
1112 return mp_cmd_clone(ar_cmd);
1113 // Then send rate / sec event
1114 } else if(ar_state == 1 && (t -last_ar) >= 1000000/ar_rate) {
1115 last_ar = t;
1116 return mp_cmd_clone(ar_cmd);
1120 return NULL;
1123 static mp_cmd_t*
1124 mp_input_read_cmds(int time) {
1125 #ifndef HAVE_NO_POSIX_SELECT
1126 fd_set fds;
1127 struct timeval tv,*time_val;
1128 #endif
1129 int i,n = 0,max_fd = 0,got_cmd = 0;
1130 mp_cmd_t* ret;
1131 static int last_loop = 0;
1133 if(num_cmd_fd == 0)
1134 return NULL;
1136 #ifndef HAVE_NO_POSIX_SELECT
1137 FD_ZERO(&fds);
1138 #endif
1139 for(i = 0; (unsigned int)i < num_cmd_fd ; i++) {
1140 if( (cmd_fds[i].flags & MP_FD_DEAD) || (cmd_fds[i].flags & MP_FD_EOF) ) {
1141 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1142 i--;
1143 continue;
1144 } else if(cmd_fds[i].flags & MP_FD_NO_SELECT)
1145 continue;
1146 if(cmd_fds[i].flags & MP_FD_GOT_CMD)
1147 got_cmd = 1;
1148 if(cmd_fds[i].fd > max_fd)
1149 max_fd = cmd_fds[i].fd;
1150 #ifndef HAVE_NO_POSIX_SELECT
1151 FD_SET(cmd_fds[i].fd,&fds);
1152 #endif
1153 n++;
1156 if(num_cmd_fd == 0)
1157 return NULL;
1159 #ifndef HAVE_NO_POSIX_SELECT
1160 if(time >= 0) {
1161 tv.tv_sec=time/1000;
1162 tv.tv_usec = (time%1000)*1000;
1163 time_val = &tv;
1164 } else
1165 time_val = NULL;
1167 while(n > 0) {
1168 if((i = select(max_fd+1,&fds,NULL,NULL,time_val)) <= 0) {
1169 if(i < 0) {
1170 if(errno == EINTR)
1171 continue;
1172 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrSelect,strerror(errno));
1174 if(!got_cmd)
1175 return NULL;
1177 break;
1179 #endif
1181 for(i = last_loop + 1; i != last_loop ; i++) {
1182 int r = 0;
1183 char* cmd;
1184 if((unsigned int)i >= num_cmd_fd) {
1185 i = -1;
1186 last_loop++;
1187 last_loop %= (num_cmd_fd+1);
1188 continue;
1190 #ifndef HAVE_NO_POSIX_SELECT
1191 if( ! (cmd_fds[i].flags & MP_FD_NO_SELECT) && ! FD_ISSET(cmd_fds[i].fd,&fds) && ! (cmd_fds[i].flags & MP_FD_GOT_CMD) )
1192 continue;
1193 #endif
1195 r = mp_input_read_cmd(&cmd_fds[i],&cmd);
1196 if(r < 0) {
1197 if(r == MP_INPUT_ERROR)
1198 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrOnCmdFd,cmd_fds[i].fd);
1199 else if(r == MP_INPUT_DEAD)
1200 cmd_fds[i].flags |= MP_FD_DEAD;
1201 continue;
1203 ret = mp_input_parse_cmd(cmd);
1204 free(cmd);
1205 if(!ret)
1206 continue;
1207 last_loop = i;
1208 return ret;
1211 last_loop = 0;
1212 return NULL;
1216 mp_input_queue_cmd(mp_cmd_t* cmd) {
1217 if(cmd_queue_length >= CMD_QUEUE_SIZE)
1218 return 0;
1219 cmd_queue[cmd_queue_end] = cmd;
1220 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1221 cmd_queue_length++;
1222 return 1;
1225 static mp_cmd_t*
1226 mp_input_get_queued_cmd(int peek_only) {
1227 mp_cmd_t* ret;
1229 if(cmd_queue_length == 0)
1230 return NULL;
1232 ret = cmd_queue[cmd_queue_start];
1234 if (!peek_only) {
1235 cmd_queue_length--;
1236 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1239 return ret;
1243 * \param peek_only when set, the returned command stays in the queue.
1244 * Do not free the returned cmd whe you set this!
1246 mp_cmd_t*
1247 mp_input_get_cmd(int time, int paused, int peek_only) {
1248 mp_cmd_t* ret = NULL;
1249 mp_cmd_filter_t* cf;
1250 int from_queue;
1252 while(1) {
1253 from_queue = 1;
1254 ret = mp_input_get_queued_cmd(peek_only);
1255 if(ret) break;
1256 from_queue = 0;
1257 ret = mp_input_read_keys(time,paused);
1258 if(ret) break;
1259 ret = mp_input_read_cmds(time);
1260 break;
1262 if(!ret) return NULL;
1264 for(cf = cmd_filters ; cf ; cf = cf->next) {
1265 if(cf->filter(ret,paused,cf->ctx))
1266 return NULL;
1269 if (!from_queue && peek_only)
1270 mp_input_queue_cmd(ret);
1272 return ret;
1275 void
1276 mp_cmd_free(mp_cmd_t* cmd) {
1277 int i;
1278 //#ifdef MP_DEBUG
1279 // assert(cmd != NULL);
1280 //#endif
1281 if ( !cmd ) return;
1283 if(cmd->name)
1284 free(cmd->name);
1286 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1287 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1288 free(cmd->args[i].v.s);
1290 free(cmd);
1293 mp_cmd_t*
1294 mp_cmd_clone(mp_cmd_t* cmd) {
1295 mp_cmd_t* ret;
1296 int i;
1297 #ifdef MP_DEBUG
1298 assert(cmd != NULL);
1299 #endif
1301 ret = malloc(sizeof(mp_cmd_t));
1302 memcpy(ret,cmd,sizeof(mp_cmd_t));
1303 if(cmd->name)
1304 ret->name = strdup(cmd->name);
1305 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1306 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1307 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1310 return ret;
1313 static char key_str[12];
1315 static char*
1316 mp_input_get_key_name(int key) {
1317 int i;
1319 for(i = 0; key_names[i].name != NULL; i++) {
1320 if(key_names[i].key == key)
1321 return key_names[i].name;
1324 if(isascii(key)) {
1325 snprintf(key_str,12,"%c",(char)key);
1326 return key_str;
1329 // Print the hex key code
1330 snprintf(key_str,12,"%#-8x",key);
1331 return key_str;
1335 static int
1336 mp_input_get_key_from_name(char* name) {
1337 int i,ret = 0,len = strlen(name);
1338 if(len == 1) { // Direct key code
1339 ret = (unsigned char)name[0];
1340 return ret;
1341 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1342 return strtol(name,NULL,16);
1344 for(i = 0; key_names[i].name != NULL; i++) {
1345 if(strcasecmp(key_names[i].name,name) == 0)
1346 return key_names[i].key;
1349 return -1;
1352 static int
1353 mp_input_get_input_from_name(char* name,int* keys) {
1354 char *end,*ptr;
1355 int n=0;
1357 ptr = name;
1358 n = 0;
1359 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1360 if(end && end[1] != '\0') {
1361 if(end[1] == '-')
1362 end = &end[1];
1363 end[0] = '\0';
1365 keys[n] = mp_input_get_key_from_name(ptr);
1366 if(keys[n] < 0) {
1367 return 0;
1369 n++;
1370 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1371 ptr = &end[1];
1372 else
1373 break;
1375 keys[n] = 0;
1376 return 1;
1379 void
1380 mp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1381 int i = 0,j;
1382 mp_cmd_bind_t* bind = NULL;
1384 #ifdef MP_DEBUG
1385 assert(keys != NULL);
1386 assert(cmd != NULL);
1387 #endif
1389 if(cmd_binds) {
1390 for(i = 0; cmd_binds[i].cmd != NULL ; i++) {
1391 for(j = 0 ; cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1392 /* NOTHING */;
1393 if(keys[j] == 0 && cmd_binds[i].input[j] == 0 ) {
1394 bind = &cmd_binds[i];
1395 break;
1400 if(!bind) {
1401 cmd_binds = (mp_cmd_bind_t*)realloc(cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1402 memset(&cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1403 bind = &cmd_binds[i];
1405 if(bind->cmd)
1406 free(bind->cmd);
1407 bind->cmd = strdup(cmd);
1408 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1411 void
1412 mp_input_add_binds(mp_cmd_bind_t* list) {
1413 int i;
1414 for(i = 0 ; list[i].cmd ; i++)
1415 mp_input_bind_keys(list[i].input,list[i].cmd);
1418 static void
1419 mp_input_free_binds(mp_cmd_bind_t* binds) {
1420 int i;
1422 if(!binds)
1423 return;
1425 for(i = 0; binds[i].cmd != NULL; i++)
1426 free(binds[i].cmd);
1428 free(binds);
1433 #define BS_MAX 256
1434 #define SPACE_CHAR " \n\r\t"
1436 static int
1437 mp_input_parse_config(char *file) {
1438 int fd;
1439 int bs = 0,r,eof = 0,comments = 0;
1440 char *iter,*end;
1441 char buffer[BS_MAX];
1442 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1444 fd = open(file,O_RDONLY);
1446 if(fd < 0) {
1447 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1448 return 0;
1451 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1453 while(1) {
1454 if(! eof && bs < BS_MAX-1) {
1455 if(bs > 0) bs--;
1456 r = read(fd,buffer+bs,BS_MAX-1-bs);
1457 if(r < 0) {
1458 if(errno == EINTR)
1459 continue;
1460 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1461 close(fd);
1462 return 0;
1463 } else if(r == 0) {
1464 eof = 1;
1465 } else {
1466 bs += r+1;
1467 buffer[bs-1] = '\0';
1470 // Empty buffer : return
1471 if(bs <= 1) {
1472 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1473 close(fd);
1474 return 1;
1477 iter = buffer;
1479 if(comments) {
1480 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1481 /* NOTHING */;
1482 if(iter[0] == '\0') { // Buffer was full of comment
1483 bs = 0;
1484 continue;
1486 iter++;
1487 r = strlen(iter);
1488 if(r)
1489 memmove(buffer,iter,r+1);
1490 bs = r+1;
1491 if(iter[0] != '#')
1492 comments = 0;
1493 continue;
1496 // Find the wanted key
1497 if(keys[0] == 0) {
1498 // Jump beginning space
1499 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1500 /* NOTHING */;
1501 if(iter[0] == '\0') { // Buffer was full of space char
1502 bs = 0;
1503 continue;
1505 if(iter[0] == '#') { // Comments
1506 comments = 1;
1507 continue;
1509 // Find the end of the key code name
1510 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1511 /*NOTHING */;
1512 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1513 if(buffer == iter) {
1514 if(eof && (buffer-iter) == bs)
1515 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1516 else
1517 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1518 return 0;
1520 memmove(buffer,iter,end-iter);
1521 bs = end-iter;
1522 continue;
1525 char name[end-iter+1];
1526 strncpy(name,iter,end-iter);
1527 name[end-iter] = '\0';
1528 if(! mp_input_get_input_from_name(name,keys)) {
1529 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1530 close(fd);
1531 return 0;
1534 if( bs > (end-buffer))
1535 memmove(buffer,end,bs - (end-buffer));
1536 bs -= end-buffer;
1537 continue;
1538 } else { // Get the command
1539 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1540 // Found new line
1541 if(iter[0] == '\n' || iter[0] == '\r') {
1542 int i;
1543 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey,mp_input_get_key_name(keys[0]));
1544 for(i = 1; keys[i] != 0 ; i++)
1545 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1546 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1547 keys[0] = 0;
1548 if(iter > buffer) {
1549 memmove(buffer,iter,bs- (iter-buffer));
1550 bs -= (iter-buffer);
1552 continue;
1554 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1555 /* NOTHING */;
1556 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1557 if(iter == buffer) {
1558 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1559 close(fd);
1560 return 0;
1562 memmove(buffer,iter,end - iter);
1563 bs = end - iter;
1564 continue;
1567 char cmd[end-iter+1];
1568 strncpy(cmd,iter,end-iter);
1569 cmd[end-iter] = '\0';
1570 //printf("Set bind %d => %s\n",keys[0],cmd);
1571 mp_input_bind_keys(keys,cmd);
1572 n_binds++;
1574 keys[0] = 0;
1575 end++;
1576 if(bs > (end-buffer))
1577 memmove(buffer,end,bs-(end-buffer));
1578 bs -= (end-buffer);
1579 buffer[bs-1] = '\0';
1580 continue;
1583 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1584 close(fd);
1585 return 0;
1588 extern char *get_path(const char *filename);
1590 void
1591 mp_input_init(int use_gui) {
1592 char* file;
1594 #ifdef HAVE_NEW_GUI
1595 if(use_gui)
1596 mp_input_add_binds(gui_def_cmd_binds);
1597 #endif
1599 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1600 if(!file)
1601 return;
1603 if( !mp_input_parse_config(file)) {
1604 // free file if it was allocated by get_path(),
1605 // before it gets overwritten
1606 if( file != config_file)
1608 free(file);
1610 // Try global conf dir
1611 file = MPLAYER_CONFDIR "/input.conf";
1612 if(! mp_input_parse_config(file))
1613 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1615 else
1617 // free file if it was allocated by get_path()
1618 if( file != config_file)
1619 free(file);
1622 #ifdef HAVE_JOYSTICK
1623 if(use_joystick) {
1624 int fd = mp_input_joystick_init(js_dev);
1625 if(fd < 0)
1626 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1627 else
1628 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1630 #endif
1632 #ifdef HAVE_LIRC
1633 if(use_lirc) {
1634 int fd = mp_input_lirc_init();
1635 if(fd > 0)
1636 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1638 #endif
1640 #ifdef HAVE_LIRCC
1641 if(use_lircc) {
1642 int fd = lircc_init("mplayer", NULL);
1643 if(fd >= 0)
1644 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1646 #endif
1648 if(in_file) {
1649 struct stat st;
1650 if(stat(in_file,&st))
1651 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,in_file,strerror(errno));
1652 else {
1653 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1654 if(in_file_fd >= 0)
1655 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1656 else
1657 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));
1663 void
1664 mp_input_uninit(void) {
1665 unsigned int i;
1667 for(i=0; i < num_key_fd; i++) {
1668 if(key_fds[i].close_func)
1669 key_fds[i].close_func(key_fds[i].fd);
1672 for(i=0; i < num_cmd_fd; i++) {
1673 if(cmd_fds[i].close_func)
1674 cmd_fds[i].close_func(cmd_fds[i].fd);
1676 mp_input_free_binds(cmd_binds);
1677 cmd_binds=NULL;
1681 void
1682 mp_input_register_options(m_config_t* cfg) {
1683 m_config_register_options(cfg,mp_input_opts);
1686 static int mp_input_print_key_list(m_option_t* cfg) {
1687 int i;
1688 printf("\n");
1689 for(i= 0; key_names[i].name != NULL ; i++)
1690 printf("%s\n",key_names[i].name);
1691 exit(0);
1694 static int mp_input_print_cmd_list(m_option_t* cfg) {
1695 mp_cmd_t *cmd;
1696 int i,j;
1697 const char* type;
1699 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1700 printf("%-20.20s",cmd->name);
1701 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1702 switch(cmd->args[j].type) {
1703 case MP_CMD_ARG_INT:
1704 type = "Integer";
1705 break;
1706 case MP_CMD_ARG_FLOAT:
1707 type = "Float";
1708 break;
1709 case MP_CMD_ARG_STRING:
1710 type = "String";
1711 break;
1712 default:
1713 type = "??";
1715 if(j+1 > cmd->nargs)
1716 printf(" [%s]",type);
1717 else
1718 printf(" %s",type);
1720 printf("\n");
1722 exit(0);
1726 mp_input_check_interrupt(int time) {
1727 mp_cmd_t* cmd;
1728 if((cmd = mp_input_get_cmd(time,0,1)) == NULL)
1729 return 0;
1730 switch(cmd->id) {
1731 case MP_CMD_QUIT:
1732 case MP_CMD_PLAY_TREE_STEP:
1733 case MP_CMD_PLAY_TREE_UP_STEP:
1734 case MP_CMD_PLAY_ALT_SRC_STEP:
1735 // The cmd will be executed when we are back in the main loop
1736 return 1;
1738 // remove the cmd from the queue
1739 cmd = mp_input_get_cmd(time,0,0);
1740 mp_cmd_free(cmd);
1741 return 0;