Files should be opened in binary mode on OS/2.
[mplayer/glamo.git] / input / input.c
blob5fbc26b309a8bfd37bc20221467f656ccda60f82
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "config.h"
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <fcntl.h>
30 #include <ctype.h>
32 #include "input.h"
33 #include "mouse.h"
34 #ifdef MP_DEBUG
35 #include <assert.h>
36 #endif
37 #include "mp_fifo.h"
38 #include "osdep/getch2.h"
39 #include "osdep/keycodes.h"
40 #include "osdep/timer.h"
41 #include "libavutil/avstring.h"
42 #include "mp_msg.h"
43 #include "help_mp.h"
44 #include "m_config.h"
45 #include "m_option.h"
46 #include "get_path.h"
48 #include "joystick.h"
50 #ifdef CONFIG_LIRC
51 #include "lirc.h"
52 #endif
54 #ifdef CONFIG_LIRCC
55 #include <lirc/lircc.h>
56 #endif
58 #include "ar.h"
60 /// This array defines all known commands.
61 /// The first field is an id used to recognize the command without too many strcmp.
62 /// The second is obviously the command name.
63 /// The third is the minimum number of arguments this command needs.
64 /// Then comes the definition of each argument, terminated with an arg of type -1.
65 /// A command can take a maximum of MP_CMD_MAX_ARGS-1 arguments (-1 because of
66 /// the last one) which is actually 9.
68 /// For the args, the first field is the type (actually int, float or string), the second
69 /// is the default value wich is used for optional arguments
71 static const mp_cmd_t mp_cmds[] = {
72 #ifdef CONFIG_RADIO
73 { MP_CMD_RADIO_STEP_CHANNEL, "radio_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
74 { MP_CMD_RADIO_SET_CHANNEL, "radio_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
75 { MP_CMD_RADIO_SET_FREQ, "radio_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
76 { MP_CMD_RADIO_STEP_FREQ, "radio_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
77 #endif
78 { MP_CMD_SEEK, "seek", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
79 { MP_CMD_EDL_MARK, "edl_mark", 0, { {-1,{0}} } },
80 { MP_CMD_AUDIO_DELAY, "audio_delay", 1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
81 { MP_CMD_SPEED_INCR, "speed_incr", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
82 { MP_CMD_SPEED_MULT, "speed_mult", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
83 { MP_CMD_SPEED_SET, "speed_set", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
84 { MP_CMD_QUIT, "quit", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
85 { MP_CMD_STOP, "stop", 0, { {-1,{0}} } },
86 { MP_CMD_PAUSE, "pause", 0, { {-1,{0}} } },
87 { MP_CMD_FRAME_STEP, "frame_step", 0, { {-1,{0}} } },
88 { MP_CMD_PLAY_TREE_STEP, "pt_step",1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
89 { MP_CMD_PLAY_TREE_UP_STEP, "pt_up_step",1, { { MP_CMD_ARG_INT,{0} }, { MP_CMD_ARG_INT ,{0}}, {-1,{0}} } },
90 { MP_CMD_PLAY_ALT_SRC_STEP, "alt_src_step",1, { { MP_CMD_ARG_INT,{0} }, {-1,{0}} } },
91 { MP_CMD_LOOP, "loop", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
92 { MP_CMD_SUB_DELAY, "sub_delay",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
93 { MP_CMD_SUB_STEP, "sub_step",1, { { MP_CMD_ARG_INT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
94 { MP_CMD_OSD, "osd",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
95 { 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}} } },
96 { 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}} } },
97 { MP_CMD_VOLUME, "volume", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
98 { MP_CMD_BALANCE, "balance", 1, { { MP_CMD_ARG_FLOAT,{0} }, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
99 { MP_CMD_MIXER_USEMASTER, "use_master", 0, { {-1,{0}} } },
100 { MP_CMD_MUTE, "mute", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
101 { MP_CMD_CONTRAST, "contrast",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
102 { MP_CMD_GAMMA, "gamma", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
103 { MP_CMD_BRIGHTNESS, "brightness",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
104 { MP_CMD_HUE, "hue",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
105 { MP_CMD_SATURATION, "saturation",1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
106 { MP_CMD_FRAMEDROPPING, "frame_drop",0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
107 { MP_CMD_SUB_POS, "sub_pos", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
108 { MP_CMD_SUB_ALIGNMENT, "sub_alignment",0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
109 { MP_CMD_SUB_VISIBILITY, "sub_visibility", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
110 { MP_CMD_SUB_LOAD, "sub_load", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
111 { MP_CMD_SUB_REMOVE, "sub_remove", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
112 { MP_CMD_SUB_SELECT, "vobsub_lang", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } }, // for compatibility
113 { MP_CMD_SUB_SELECT, "sub_select", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
114 { MP_CMD_SUB_SOURCE, "sub_source", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
115 { MP_CMD_SUB_VOB, "sub_vob", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
116 { MP_CMD_SUB_DEMUX, "sub_demux", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
117 { MP_CMD_SUB_FILE, "sub_file", 0, { { MP_CMD_ARG_INT,{-2} }, {-1,{0}} } },
118 { MP_CMD_SUB_LOG, "sub_log", 0, { {-1,{0}} } },
119 { MP_CMD_SUB_SCALE, "sub_scale",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
120 #ifdef CONFIG_ASS
121 { MP_CMD_ASS_USE_MARGINS, "ass_use_margins", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
122 #endif
123 { MP_CMD_GET_PERCENT_POS, "get_percent_pos", 0, { {-1,{0}} } },
124 { MP_CMD_GET_TIME_POS, "get_time_pos", 0, { {-1,{0}} } },
125 { MP_CMD_GET_TIME_LENGTH, "get_time_length", 0, { {-1,{0}} } },
126 { MP_CMD_GET_FILENAME, "get_file_name", 0, { {-1,{0}} } },
127 { MP_CMD_GET_VIDEO_CODEC, "get_video_codec", 0, { {-1,{0}} } },
128 { MP_CMD_GET_VIDEO_BITRATE, "get_video_bitrate", 0, { {-1,{0}} } },
129 { MP_CMD_GET_VIDEO_RESOLUTION, "get_video_resolution", 0, { {-1,{0}} } },
130 { MP_CMD_GET_AUDIO_CODEC, "get_audio_codec", 0, { {-1,{0}} } },
131 { MP_CMD_GET_AUDIO_BITRATE, "get_audio_bitrate", 0, { {-1,{0}} } },
132 { MP_CMD_GET_AUDIO_SAMPLES, "get_audio_samples", 0, { {-1,{0}} } },
133 { MP_CMD_GET_META_TITLE, "get_meta_title", 0, { {-1,{0}} } },
134 { MP_CMD_GET_META_ARTIST, "get_meta_artist", 0, { {-1,{0}} } },
135 { MP_CMD_GET_META_ALBUM, "get_meta_album", 0, { {-1,{0}} } },
136 { MP_CMD_GET_META_YEAR, "get_meta_year", 0, { {-1,{0}} } },
137 { MP_CMD_GET_META_COMMENT, "get_meta_comment", 0, { {-1,{0}} } },
138 { MP_CMD_GET_META_TRACK, "get_meta_track", 0, { {-1,{0}} } },
139 { MP_CMD_GET_META_GENRE, "get_meta_genre", 0, { {-1,{0}} } },
140 { MP_CMD_SWITCH_AUDIO, "switch_audio", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
141 { MP_CMD_SWITCH_ANGLE, "switch_angle", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
142 { MP_CMD_SWITCH_TITLE, "switch_title", 0, { { MP_CMD_ARG_INT,{-1} }, {-1,{0}} } },
143 #ifdef CONFIG_TV
144 { MP_CMD_TV_START_SCAN, "tv_start_scan", 0, { {-1,{0}} }},
145 { MP_CMD_TV_STEP_CHANNEL, "tv_step_channel", 1, { { MP_CMD_ARG_INT ,{0}}, {-1,{0}} }},
146 { MP_CMD_TV_STEP_NORM, "tv_step_norm",0, { {-1,{0}} } },
147 { MP_CMD_TV_STEP_CHANNEL_LIST, "tv_step_chanlist", 0, { {-1,{0}} } },
148 { MP_CMD_TV_SET_CHANNEL, "tv_set_channel", 1, { { MP_CMD_ARG_STRING, {0}}, {-1,{0}} }},
149 { MP_CMD_TV_LAST_CHANNEL, "tv_last_channel", 0, { {-1,{0}} } },
150 { MP_CMD_TV_SET_FREQ, "tv_set_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
151 { MP_CMD_TV_STEP_FREQ, "tv_step_freq", 1, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
152 { MP_CMD_TV_SET_NORM, "tv_set_norm", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
153 { MP_CMD_TV_SET_BRIGHTNESS, "tv_set_brightness", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
154 { MP_CMD_TV_SET_CONTRAST, "tv_set_contrast", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
155 { MP_CMD_TV_SET_HUE, "tv_set_hue", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
156 { MP_CMD_TV_SET_SATURATION, "tv_set_saturation", 1, { { MP_CMD_ARG_INT ,{0}}, { MP_CMD_ARG_INT,{1} }, {-1,{0}} }},
157 #endif
158 { MP_CMD_SUB_FORCED_ONLY, "forced_subs_only", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
159 #ifdef CONFIG_DVBIN
160 { MP_CMD_DVB_SET_CHANNEL, "dvb_set_channel", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
161 #endif
162 { MP_CMD_SWITCH_RATIO, "switch_ratio", 0, { {MP_CMD_ARG_FLOAT,{0}}, {-1,{0}} } },
163 { MP_CMD_VO_FULLSCREEN, "vo_fullscreen", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
164 { MP_CMD_VO_ONTOP, "vo_ontop", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
165 { MP_CMD_FILE_FILTER, "file_filter", 1, { { MP_CMD_ARG_INT, {0}}, {-1,{0}}}},
166 { MP_CMD_VO_ROOTWIN, "vo_rootwin", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
167 { MP_CMD_VO_BORDER, "vo_border", 0, { {MP_CMD_ARG_INT,{-1}}, {-1,{0}} } },
168 { MP_CMD_SCREENSHOT, "screenshot", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
169 { MP_CMD_PANSCAN, "panscan",1, { {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
170 { MP_CMD_SWITCH_VSYNC, "switch_vsync", 0, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
171 { MP_CMD_LOADFILE, "loadfile", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
172 { MP_CMD_LOADLIST, "loadlist", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
173 { MP_CMD_RUN, "run", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
174 { MP_CMD_VF_CHANGE_RECTANGLE, "change_rectangle", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}}}},
175 #ifdef CONFIG_TV_TELETEXT
176 { MP_CMD_TV_TELETEXT_ADD_DEC, "teletext_add_dec", 1, { {MP_CMD_ARG_STRING,{0}}, {-1,{0}} } },
177 { MP_CMD_TV_TELETEXT_GO_LINK, "teletext_go_link", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
178 #endif
180 #ifdef CONFIG_GUI
181 { MP_CMD_GUI_LOADFILE, "gui_loadfile", 0, { {-1,{0}} } },
182 { MP_CMD_GUI_LOADSUBTITLE, "gui_loadsubtitle", 0, { {-1,{0}} } },
183 { MP_CMD_GUI_ABOUT, "gui_about", 0, { {-1,{0}} } },
184 { MP_CMD_GUI_PLAY, "gui_play", 0, { {-1,{0}} } },
185 { MP_CMD_GUI_STOP, "gui_stop", 0, { {-1,{0}} } },
186 { MP_CMD_GUI_PLAYLIST, "gui_playlist", 0, { {-1,{0}} } },
187 { MP_CMD_GUI_PREFERENCES, "gui_preferences", 0, { {-1,{0}} } },
188 { MP_CMD_GUI_SKINBROWSER, "gui_skinbrowser", 0, { {-1,{0}} } },
189 #endif
191 #ifdef CONFIG_DVDNAV
192 { MP_CMD_DVDNAV, "dvdnav", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
193 #endif
195 #ifdef CONFIG_MENU
196 { MP_CMD_MENU, "menu",1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
197 { MP_CMD_SET_MENU, "set_menu",1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
198 { MP_CMD_CHELP, "help", 0, { {-1,{0}} } },
199 { MP_CMD_CEXIT, "exit", 0, { {-1,{0}} } },
200 { MP_CMD_CHIDE, "hide", 0, { {MP_CMD_ARG_INT,{3000}}, {-1,{0}} } },
201 #endif
203 { MP_CMD_GET_VO_FULLSCREEN, "get_vo_fullscreen", 0, { {-1,{0}} } },
204 { MP_CMD_GET_SUB_VISIBILITY, "get_sub_visibility", 0, { {-1,{0}} } },
205 { MP_CMD_KEYDOWN_EVENTS, "key_down_event", 1, { {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
206 { MP_CMD_SET_PROPERTY, "set_property", 2, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
207 { MP_CMD_GET_PROPERTY, "get_property", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
208 { MP_CMD_STEP_PROPERTY, "step_property", 1, { {MP_CMD_ARG_STRING, {0}}, {MP_CMD_ARG_FLOAT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
210 { MP_CMD_SEEK_CHAPTER, "seek_chapter", 1, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
211 { MP_CMD_SET_MOUSE_POS, "set_mouse_pos", 2, { {MP_CMD_ARG_INT,{0}}, {MP_CMD_ARG_INT,{0}}, {-1,{0}} } },
213 { 0, NULL, 0, {} }
216 /// The names of the keys as used in input.conf
217 /// If you add some new keys, you also need to add them here
219 static const mp_key_name_t key_names[] = {
220 { ' ', "SPACE" },
221 { '#', "SHARP" },
222 { KEY_ENTER, "ENTER" },
223 { KEY_TAB, "TAB" },
224 { KEY_CTRL, "CTRL" },
225 { KEY_BACKSPACE, "BS" },
226 { KEY_DELETE, "DEL" },
227 { KEY_INSERT, "INS" },
228 { KEY_HOME, "HOME" },
229 { KEY_END, "END" },
230 { KEY_PAGE_UP, "PGUP" },
231 { KEY_PAGE_DOWN, "PGDWN" },
232 { KEY_ESC, "ESC" },
233 { KEY_RIGHT, "RIGHT" },
234 { KEY_LEFT, "LEFT" },
235 { KEY_DOWN, "DOWN" },
236 { KEY_UP, "UP" },
237 { KEY_F+1, "F1" },
238 { KEY_F+2, "F2" },
239 { KEY_F+3, "F3" },
240 { KEY_F+4, "F4" },
241 { KEY_F+5, "F5" },
242 { KEY_F+6, "F6" },
243 { KEY_F+7, "F7" },
244 { KEY_F+8, "F8" },
245 { KEY_F+9, "F9" },
246 { KEY_F+10, "F10" },
247 { KEY_F+11, "F11" },
248 { KEY_F+12, "F12" },
249 { KEY_KP0, "KP0" },
250 { KEY_KP1, "KP1" },
251 { KEY_KP2, "KP2" },
252 { KEY_KP3, "KP3" },
253 { KEY_KP4, "KP4" },
254 { KEY_KP5, "KP5" },
255 { KEY_KP6, "KP6" },
256 { KEY_KP7, "KP7" },
257 { KEY_KP8, "KP8" },
258 { KEY_KP9, "KP9" },
259 { KEY_KPDEL, "KP_DEL" },
260 { KEY_KPDEC, "KP_DEC" },
261 { KEY_KPINS, "KP_INS" },
262 { KEY_KPENTER, "KP_ENTER" },
263 { MOUSE_BTN0, "MOUSE_BTN0" },
264 { MOUSE_BTN1, "MOUSE_BTN1" },
265 { MOUSE_BTN2, "MOUSE_BTN2" },
266 { MOUSE_BTN3, "MOUSE_BTN3" },
267 { MOUSE_BTN4, "MOUSE_BTN4" },
268 { MOUSE_BTN5, "MOUSE_BTN5" },
269 { MOUSE_BTN6, "MOUSE_BTN6" },
270 { MOUSE_BTN7, "MOUSE_BTN7" },
271 { MOUSE_BTN8, "MOUSE_BTN8" },
272 { MOUSE_BTN9, "MOUSE_BTN9" },
273 { MOUSE_BTN0_DBL, "MOUSE_BTN0_DBL" },
274 { MOUSE_BTN1_DBL, "MOUSE_BTN1_DBL" },
275 { MOUSE_BTN2_DBL, "MOUSE_BTN2_DBL" },
276 { MOUSE_BTN3_DBL, "MOUSE_BTN3_DBL" },
277 { MOUSE_BTN4_DBL, "MOUSE_BTN4_DBL" },
278 { MOUSE_BTN5_DBL, "MOUSE_BTN5_DBL" },
279 { MOUSE_BTN6_DBL, "MOUSE_BTN6_DBL" },
280 { MOUSE_BTN7_DBL, "MOUSE_BTN7_DBL" },
281 { MOUSE_BTN8_DBL, "MOUSE_BTN8_DBL" },
282 { MOUSE_BTN9_DBL, "MOUSE_BTN9_DBL" },
283 { JOY_AXIS1_MINUS, "JOY_UP" },
284 { JOY_AXIS1_PLUS, "JOY_DOWN" },
285 { JOY_AXIS0_MINUS, "JOY_LEFT" },
286 { JOY_AXIS0_PLUS, "JOY_RIGHT" },
288 { JOY_AXIS0_PLUS, "JOY_AXIS0_PLUS" },
289 { JOY_AXIS0_MINUS, "JOY_AXIS0_MINUS" },
290 { JOY_AXIS1_PLUS, "JOY_AXIS1_PLUS" },
291 { JOY_AXIS1_MINUS, "JOY_AXIS1_MINUS" },
292 { JOY_AXIS2_PLUS, "JOY_AXIS2_PLUS" },
293 { JOY_AXIS2_MINUS, "JOY_AXIS2_MINUS" },
294 { JOY_AXIS3_PLUS, "JOY_AXIS3_PLUS" },
295 { JOY_AXIS3_MINUS, "JOY_AXIS3_MINUS" },
296 { JOY_AXIS4_PLUS, "JOY_AXIS4_PLUS" },
297 { JOY_AXIS4_MINUS, "JOY_AXIS4_MINUS" },
298 { JOY_AXIS5_PLUS, "JOY_AXIS5_PLUS" },
299 { JOY_AXIS5_MINUS, "JOY_AXIS5_MINUS" },
300 { JOY_AXIS6_PLUS, "JOY_AXIS6_PLUS" },
301 { JOY_AXIS6_MINUS, "JOY_AXIS6_MINUS" },
302 { JOY_AXIS7_PLUS, "JOY_AXIS7_PLUS" },
303 { JOY_AXIS7_MINUS, "JOY_AXIS7_MINUS" },
304 { JOY_AXIS8_PLUS, "JOY_AXIS8_PLUS" },
305 { JOY_AXIS8_MINUS, "JOY_AXIS8_MINUS" },
306 { JOY_AXIS9_PLUS, "JOY_AXIS9_PLUS" },
307 { JOY_AXIS9_MINUS, "JOY_AXIS9_MINUS" },
309 { JOY_BTN0, "JOY_BTN0" },
310 { JOY_BTN1, "JOY_BTN1" },
311 { JOY_BTN2, "JOY_BTN2" },
312 { JOY_BTN3, "JOY_BTN3" },
313 { JOY_BTN4, "JOY_BTN4" },
314 { JOY_BTN5, "JOY_BTN5" },
315 { JOY_BTN6, "JOY_BTN6" },
316 { JOY_BTN7, "JOY_BTN7" },
317 { JOY_BTN8, "JOY_BTN8" },
318 { JOY_BTN9, "JOY_BTN9" },
320 { AR_PLAY, "AR_PLAY" },
321 { AR_PLAY_HOLD, "AR_PLAY_HOLD" },
322 { AR_NEXT, "AR_NEXT" },
323 { AR_NEXT_HOLD, "AR_NEXT_HOLD" },
324 { AR_PREV, "AR_PREV" },
325 { AR_PREV_HOLD, "AR_PREV_HOLD" },
326 { AR_MENU, "AR_MENU" },
327 { AR_MENU_HOLD, "AR_MENU_HOLD" },
328 { AR_VUP, "AR_VUP" },
329 { AR_VDOWN, "AR_VDOWN" },
331 { KEY_POWER, "POWER" },
332 { KEY_MENU, "MENU" },
333 { KEY_PLAY, "PLAY" },
334 { KEY_PAUSE, "PAUSE" },
335 { KEY_PLAYPAUSE, "PLAYPAUSE" },
336 { KEY_STOP, "STOP" },
337 { KEY_FORWARD, "FORWARD" },
338 { KEY_REWIND, "REWIND" },
339 { KEY_NEXT, "NEXT" },
340 { KEY_PREV, "PREV" },
341 { KEY_VOLUME_UP, "VOLUME_UP" },
342 { KEY_VOLUME_DOWN, "VOLUME_DOWN" },
343 { KEY_MUTE, "MUTE" },
345 // These are kept for backward compatibility
346 { KEY_PAUSE, "XF86_PAUSE" },
347 { KEY_STOP, "XF86_STOP" },
348 { KEY_PREV, "XF86_PREV" },
349 { KEY_NEXT, "XF86_NEXT" },
351 { KEY_CLOSE_WIN, "CLOSE_WIN" },
353 { 0, NULL }
356 // This is the default binding. The content of input.conf overrides these.
357 // The first arg is a null terminated array of key codes.
358 // The second is the command
360 static const mp_cmd_bind_t def_cmd_binds[] = {
362 { { MOUSE_BTN3, 0 }, "seek 10" },
363 { { MOUSE_BTN4, 0 }, "seek -10" },
364 { { MOUSE_BTN5, 0 }, "volume 1" },
365 { { MOUSE_BTN6, 0 }, "volume -1" },
367 #ifdef CONFIG_DVDNAV
368 { { KEY_KP8, 0 }, "dvdnav up" }, // up
369 { { KEY_KP2, 0 }, "dvdnav down" }, // down
370 { { KEY_KP4, 0 }, "dvdnav left" }, // left
371 { { KEY_KP6, 0 }, "dvdnav right" }, // right
372 { { KEY_KP5, 0 }, "dvdnav menu" }, // menu
373 { { KEY_KPENTER, 0 }, "dvdnav select" }, // select
374 { { MOUSE_BTN0, 0 }, "dvdnav mouse" }, //select
375 { { KEY_KP7, 0 }, "dvdnav prev" }, // previous menu
376 #endif
378 { { KEY_RIGHT, 0 }, "seek 10" },
379 { { KEY_LEFT, 0 }, "seek -10" },
380 { { KEY_UP, 0 }, "seek 60" },
381 { { KEY_DOWN, 0 }, "seek -60" },
382 { { KEY_PAGE_UP, 0 }, "seek 600" },
383 { { KEY_PAGE_DOWN, 0 }, "seek -600" },
384 { { '+', 0 }, "audio_delay 0.100" },
385 { { '-', 0 }, "audio_delay -0.100" },
386 { { '[', 0 }, "speed_mult 0.9091" },
387 { { ']', 0 }, "speed_mult 1.1" },
388 { { '{', 0 }, "speed_mult 0.5" },
389 { { '}', 0 }, "speed_mult 2.0" },
390 { { KEY_BACKSPACE, 0 }, "speed_set 1.0" },
391 { { 'q', 0 }, "quit" },
392 { { KEY_ESC, 0 }, "quit" },
393 { { 'p', 0 }, "pause" },
394 { { ' ', 0 }, "pause" },
395 { { '.', 0 }, "frame_step" },
396 { { KEY_HOME, 0 }, "pt_up_step 1" },
397 { { KEY_END, 0 }, "pt_up_step -1" },
398 { { '>', 0 }, "pt_step 1" },
399 { { KEY_ENTER, 0 }, "pt_step 1 1" },
400 { { '<', 0 }, "pt_step -1" },
401 { { KEY_INS, 0 }, "alt_src_step 1" },
402 { { KEY_DEL, 0 }, "alt_src_step -1" },
403 { { 'o', 0 }, "osd" },
404 { { 'I', 0 }, "osd_show_property_text \"${filename}\"" },
405 { { 'z', 0 }, "sub_delay -0.1" },
406 { { 'x', 0 }, "sub_delay +0.1" },
407 { { 'g', 0 }, "sub_step -1" },
408 { { 'y', 0 }, "sub_step +1" },
409 { { '9', 0 }, "volume -1" },
410 { { '/', 0 }, "volume -1" },
411 { { '0', 0 }, "volume 1" },
412 { { '*', 0 }, "volume 1" },
413 { { '(', 0 }, "balance -0.1" },
414 { { ')', 0 }, "balance 0.1" },
415 { { 'm', 0 }, "mute" },
416 { { '1', 0 }, "contrast -1" },
417 { { '2', 0 }, "contrast 1" },
418 { { '3', 0 }, "brightness -1" },
419 { { '4', 0 }, "brightness 1" },
420 { { '5', 0 }, "hue -1" },
421 { { '6', 0 }, "hue 1" },
422 { { '7', 0 }, "saturation -1" },
423 { { '8', 0 }, "saturation 1" },
424 { { 'd', 0 }, "frame_drop" },
425 { { 'D', 0 }, "step_property deinterlace" },
426 { { 'r', 0 }, "sub_pos -1" },
427 { { 't', 0 }, "sub_pos +1" },
428 { { 'a', 0 }, "sub_alignment" },
429 { { 'v', 0 }, "sub_visibility" },
430 { { 'j', 0 }, "sub_select" },
431 { { 'F', 0 }, "forced_subs_only" },
432 { { '#', 0 }, "switch_audio" },
433 { { '_', 0 }, "step_property switch_video" },
434 { { KEY_TAB, 0 }, "step_property switch_program" },
435 { { 'i', 0 }, "edl_mark" },
436 #ifdef CONFIG_TV
437 { { 'h', 0 }, "tv_step_channel 1" },
438 { { 'k', 0 }, "tv_step_channel -1" },
439 { { 'n', 0 }, "tv_step_norm" },
440 { { 'u', 0 }, "tv_step_chanlist" },
441 #endif
442 #ifdef CONFIG_TV_TELETEXT
443 { { 'X', 0 }, "step_property teletext_mode 1" },
444 { { 'W', 0 }, "step_property teletext_page 1" },
445 { { 'Q', 0 }, "step_property teletext_page -1" },
446 #endif
447 #ifdef CONFIG_JOYSTICK
448 { { JOY_AXIS0_PLUS, 0 }, "seek 10" },
449 { { JOY_AXIS0_MINUS, 0 }, "seek -10" },
450 { { JOY_AXIS1_MINUS, 0 }, "seek 60" },
451 { { JOY_AXIS1_PLUS, 0 }, "seek -60" },
452 { { JOY_BTN0, 0 }, "pause" },
453 { { JOY_BTN1, 0 }, "osd" },
454 { { JOY_BTN2, 0 }, "volume 1"},
455 { { JOY_BTN3, 0 }, "volume -1"},
456 #endif
457 #ifdef CONFIG_APPLE_REMOTE
458 { { AR_PLAY, 0}, "pause" },
459 { { AR_PLAY_HOLD, 0}, "quit" },
460 { { AR_NEXT, 0 }, "seek 30" },
461 { { AR_NEXT_HOLD, 0 }, "seek 120" },
462 { { AR_PREV, 0 }, "seek -10" },
463 { { AR_PREV_HOLD, 0 }, "seek -120" },
464 { { AR_MENU, 0 }, "osd" },
465 { { AR_MENU_HOLD, 0 }, "mute" },
466 { { AR_VUP, 0 }, "volume 1"},
467 { { AR_VDOWN, 0 }, "volume -1"},
468 #endif
469 { { 'T', 0 }, "vo_ontop" },
470 { { 'f', 0 }, "vo_fullscreen" },
471 { { 's', 0 }, "screenshot 0" },
472 { { 'S', 0 }, "screenshot 1" },
473 { { 'w', 0 }, "panscan -0.1" },
474 { { 'e', 0 }, "panscan +0.1" },
476 { { KEY_POWER, 0 }, "quit" },
477 { { KEY_MENU, 0 }, "osd" },
478 { { KEY_PLAY, 0 }, "pause" },
479 { { KEY_PAUSE, 0 }, "pause" },
480 { { KEY_PLAYPAUSE, 0 }, "pause" },
481 { { KEY_STOP, 0 }, "quit" },
482 { { KEY_FORWARD, 0 }, "seek 60" },
483 { { KEY_REWIND, 0 }, "seek -60" },
484 { { KEY_NEXT, 0 }, "pt_step 1" },
485 { { KEY_PREV, 0 }, "pt_step -1" },
486 { { KEY_VOLUME_UP, 0 }, "volume 1" },
487 { { KEY_VOLUME_DOWN, 0 }, "volume -1" },
488 { { KEY_MUTE, 0 }, "mute" },
490 { { KEY_CLOSE_WIN, 0 }, "quit" },
492 { { '!', 0 }, "seek_chapter -1" },
493 { { '@', 0 }, "seek_chapter 1" },
494 { { 'A', 0 }, "switch_angle 1" },
495 { { 'U', 0 }, "stop" },
497 { { 0 }, NULL }
501 #ifdef CONFIG_GUI
502 static const mp_cmd_bind_t gui_def_cmd_binds[] = {
504 { { 'l', 0 }, "gui_loadfile" },
505 { { 't', 0 }, "gui_loadsubtitle" },
506 { { KEY_ENTER, 0 }, "gui_play" },
507 { { KEY_ESC, 0 }, "gui_stop" },
508 { { 'p', 0 }, "gui_playlist" },
509 { { 'r', 0 }, "gui_preferences" },
510 { { 'c', 0 }, "gui_skinbrowser" },
512 { { 0 }, NULL }
514 #endif
516 #ifndef MP_MAX_KEY_FD
517 #define MP_MAX_KEY_FD 10
518 #endif
520 #ifndef MP_MAX_CMD_FD
521 #define MP_MAX_CMD_FD 10
522 #endif
524 #define CMD_QUEUE_SIZE 100
526 typedef struct mp_input_fd {
527 int fd;
528 void* read_func;
529 mp_close_func_t close_func;
530 unsigned eof : 1;
531 unsigned drop : 1;
532 unsigned dead : 1;
533 unsigned got_cmd : 1;
534 unsigned no_select : 1;
535 unsigned no_readfunc_retval : 1;
536 // These fields are for the cmd fds.
537 char* buffer;
538 int pos,size;
539 } mp_input_fd_t;
541 typedef struct mp_cmd_filter_st mp_cmd_filter_t;
543 struct mp_cmd_filter_st {
544 mp_input_cmd_filter filter;
545 void* ctx;
546 mp_cmd_filter_t* next;
549 typedef struct mp_cmd_bind_section_st mp_cmd_bind_section_t;
551 struct mp_cmd_bind_section_st {
552 mp_cmd_bind_t* cmd_binds;
553 char* section;
554 mp_cmd_bind_section_t* next;
557 // These are the user defined binds
558 static mp_cmd_bind_section_t* cmd_binds_section = NULL;
559 static char* section = NULL;
560 static mp_cmd_bind_t* cmd_binds = NULL;
561 static mp_cmd_bind_t* cmd_binds_default = NULL;
562 static mp_cmd_filter_t* cmd_filters = NULL;
564 // Callback to allow the menu filter to grab the incoming keys
565 int (*mp_input_key_cb)(int code) = NULL;
567 int async_quit_request;
569 static mp_input_fd_t key_fds[MP_MAX_KEY_FD];
570 static unsigned int num_key_fd = 0;
571 static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];
572 static unsigned int num_cmd_fd = 0;
573 static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];
574 static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;
576 // this is the key currently down
577 static int key_down[MP_MAX_KEY_DOWN];
578 static unsigned int num_key_down = 0, last_key_down = 0;
580 // Autorepeat stuff
581 static short ar_state = -1;
582 static mp_cmd_t* ar_cmd = NULL;
583 static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;
585 static int use_joystick = 1, use_lirc = 1, use_lircc = 1;
586 static int default_bindings = 1;
587 static char* config_file = "input.conf";
589 /* Apple Remote */
590 #ifdef CONFIG_APPLE_REMOTE
591 static int use_ar = 1;
592 #else
593 static int use_ar = 0;
594 #endif
596 static char* js_dev = NULL;
597 static char* ar_dev = NULL;
599 static char* in_file = NULL;
600 static int in_file_fd = -1;
602 static int mp_input_print_key_list(m_option_t* cfg);
603 static int mp_input_print_cmd_list(m_option_t* cfg);
605 // Our command line options
606 static m_option_t input_conf[] = {
607 { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
608 { "ar-dev", &ar_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
609 { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
610 { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL },
611 { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
612 { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL },
613 { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
614 { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL },
615 { "default-bindings", &default_bindings, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
616 { "nodefault-bindings", &default_bindings, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
617 { NULL, NULL, 0, 0, 0, 0, NULL}
620 static m_option_t mp_input_opts[] = {
621 { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL},
622 { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
623 { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
624 { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
625 { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
626 { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
627 { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
628 { "noar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL },
629 { "ar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL },
630 { NULL, NULL, 0, 0, 0, 0, NULL}
633 static int
634 mp_input_default_cmd_func(int fd,char* buf, int l);
636 static char*
637 mp_input_get_key_name(int key);
641 mp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) {
642 if(num_cmd_fd == MP_MAX_CMD_FD) {
643 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd);
644 return 0;
646 if (select && fd < 0) {
647 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd);
648 return 0;
651 memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t));
652 cmd_fds[num_cmd_fd].fd = fd;
653 cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func;
654 cmd_fds[num_cmd_fd].close_func = close_func;
655 cmd_fds[num_cmd_fd].no_select = !select;
656 num_cmd_fd++;
658 return 1;
661 void
662 mp_input_rm_cmd_fd(int fd) {
663 unsigned int i;
665 for(i = 0; i < num_cmd_fd; i++) {
666 if(cmd_fds[i].fd == fd)
667 break;
669 if(i == num_cmd_fd)
670 return;
671 if(cmd_fds[i].close_func)
672 cmd_fds[i].close_func(cmd_fds[i].fd);
673 if(cmd_fds[i].buffer)
674 free(cmd_fds[i].buffer);
676 if(i + 1 < num_cmd_fd)
677 memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t));
678 num_cmd_fd--;
681 void
682 mp_input_rm_key_fd(int fd) {
683 unsigned int i;
685 for(i = 0; i < num_key_fd; i++) {
686 if(key_fds[i].fd == fd)
687 break;
689 if(i == num_key_fd)
690 return;
691 if(key_fds[i].close_func)
692 key_fds[i].close_func(key_fds[i].fd);
694 if(i + 1 < num_key_fd)
695 memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t));
696 num_key_fd--;
700 mp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) {
701 if(num_key_fd == MP_MAX_KEY_FD) {
702 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
703 return 0;
705 if (select && fd < 0) {
706 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd);
707 return 0;
710 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
711 key_fds[num_key_fd].fd = fd;
712 key_fds[num_key_fd].read_func = read_func;
713 key_fds[num_key_fd].close_func = close_func;
714 key_fds[num_key_fd].no_select = !select;
715 num_key_fd++;
717 return 1;
721 mp_input_add_event_fd(int fd, void (*read_func)(void))
723 if(num_key_fd == MP_MAX_KEY_FD) {
724 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd);
725 return 0;
727 if (fd < 0) {
728 mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_event_fd", fd);
729 return 0;
732 memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t));
733 key_fds[num_key_fd].fd = fd;
734 key_fds[num_key_fd].read_func = read_func;
735 key_fds[num_key_fd].close_func = NULL;
736 key_fds[num_key_fd].no_readfunc_retval = 1;
737 num_key_fd++;
739 return 1;
742 void mp_input_rm_event_fd(int fd)
744 mp_input_rm_key_fd(fd);
747 int mp_input_parse_and_queue_cmds(const char *str) {
748 int cmd_num = 0;
750 while (*str == '\n' || *str == '\r' || *str == ' ')
751 ++str;
752 while (*str) {
753 mp_cmd_t *cmd;
754 size_t len = strcspn(str, "\r\n");
755 char *cmdbuf = malloc(len+1);
756 av_strlcpy(cmdbuf, str, len+1);
757 cmd = mp_input_parse_cmd(cmdbuf);
758 if (cmd) {
759 mp_input_queue_cmd(cmd);
760 ++cmd_num;
762 str += len;
763 while (*str == '\n' || *str == '\r' || *str == ' ')
764 ++str;
765 free(cmdbuf);
767 return cmd_num;
770 mp_cmd_t*
771 mp_input_parse_cmd(char* str) {
772 int i,l;
773 int pausing = -1;
774 char *ptr,*e;
775 mp_cmd_t *cmd;
776 const mp_cmd_t *cmd_def;
778 #ifdef MP_DEBUG
779 assert(str != NULL);
780 #endif
782 // Ignore heading spaces.
783 while (str[0] == ' ' || str[0] == '\t')
784 ++str;
786 if (strncmp(str, "pausing ", 8) == 0) {
787 pausing = 1;
788 str = &str[8];
789 } else if (strncmp(str, "pausing_keep ", 13) == 0) {
790 pausing = 2;
791 str = &str[13];
792 } else if (strncmp(str, "pausing_toggle ", 15) == 0) {
793 pausing = 3;
794 str = &str[15];
795 } else if (strncmp(str, "pausing_keep_force ", 19) == 0) {
796 pausing = 4;
797 str = &str[19];
800 for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++)
801 /* NOTHING */;
802 if(ptr[0] != '\0')
803 l = ptr-str;
804 else
805 l = strlen(str);
807 if(l == 0)
808 return NULL;
810 for(i=0; mp_cmds[i].name != NULL; i++) {
811 if(strncasecmp(mp_cmds[i].name,str,l) == 0)
812 break;
815 if(mp_cmds[i].name == NULL)
816 return NULL;
818 cmd_def = &mp_cmds[i];
820 cmd = calloc(1, sizeof(mp_cmd_t));
821 cmd->id = cmd_def->id;
822 cmd->name = strdup(cmd_def->name);
823 if (pausing == -1) {
824 switch (cmd->id) {
825 case MP_CMD_KEYDOWN_EVENTS:
826 case MP_CMD_SET_MOUSE_POS:
827 pausing = 4; break;
828 default:
829 pausing = 0; break;
832 cmd->pausing = pausing;
834 ptr = str;
836 for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) {
837 while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++;
838 if(ptr[0] == '\0') break;
839 while(ptr[0] == ' ' || ptr[0] == '\t') ptr++;
840 if(ptr[0] == '\0' || ptr[0] == '#') break;
841 cmd->args[i].type = cmd_def->args[i].type;
842 switch(cmd_def->args[i].type) {
843 case MP_CMD_ARG_INT:
844 errno = 0;
845 cmd->args[i].v.i = atoi(ptr);
846 if(errno != 0) {
847 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1);
848 ptr = NULL;
850 break;
851 case MP_CMD_ARG_FLOAT:
852 errno = 0;
853 cmd->args[i].v.f = atof(ptr);
854 if(errno != 0) {
855 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1);
856 ptr = NULL;
858 break;
859 case MP_CMD_ARG_STRING: {
860 char term;
861 char* ptr2 = ptr, *start;
863 if(ptr[0] == '\'' || ptr[0] == '"') {
864 term = ptr[0];
865 ptr2++;
866 } else
867 term = ' ';
868 start = ptr2;
869 while(1) {
870 e = strchr(ptr2,term);
871 if(!e) break;
872 if(e <= ptr2 || *(e - 1) != '\\') break;
873 ptr2 = e + 1;
876 if(term != ' ' && (!e || e[0] == '\0')) {
877 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1);
878 ptr = NULL;
879 break;
880 } else if(!e) e = ptr+strlen(ptr);
881 l = e-start;
882 ptr2 = start;
883 for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) {
884 memmove(e,e+1,strlen(e));
885 ptr2 = e + 1;
886 l--;
888 cmd->args[i].v.s = malloc(l+1);
889 strncpy(cmd->args[i].v.s,start,l);
890 cmd->args[i].v.s[l] = '\0';
891 if(term != ' ') ptr += l+2;
892 } break;
893 case -1:
894 ptr = NULL;
895 break;
896 default :
897 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i);
900 cmd->nargs = i;
902 if(cmd_def->nargs > cmd->nargs) {
903 /* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */
904 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs);
905 mp_cmd_free(cmd);
906 return NULL;
909 for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) {
910 memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t));
911 if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL)
912 cmd->args[i].v.s = strdup(cmd_def->args[i].v.s);
915 if(i < MP_CMD_MAX_ARGS)
916 cmd->args[i].type = -1;
918 return cmd;
921 #define MP_CMD_MAX_SIZE 256
923 static int
924 mp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) {
925 char* end;
926 (*ret) = NULL;
928 // Allocate the buffer if it doesn't exist
929 if(!mp_fd->buffer) {
930 mp_fd->buffer = malloc(MP_CMD_MAX_SIZE);
931 mp_fd->pos = 0;
932 mp_fd->size = MP_CMD_MAX_SIZE;
935 // Get some data if needed/possible
936 while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) {
937 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);
938 // Error ?
939 if(r < 0) {
940 switch(r) {
941 case MP_INPUT_ERROR:
942 case MP_INPUT_DEAD:
943 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno));
944 case MP_INPUT_NOTHING:
945 return r;
946 case MP_INPUT_RETRY:
947 continue;
949 // EOF ?
950 } else if(r == 0) {
951 mp_fd->eof = 1;
952 break;
954 mp_fd->pos += r;
955 break;
958 mp_fd->got_cmd = 0;
960 while(1) {
961 int l = 0;
962 // Find the cmd end
963 mp_fd->buffer[mp_fd->pos] = '\0';
964 end = strchr(mp_fd->buffer,'\n');
965 // No cmd end ?
966 if(!end) {
967 // If buffer is full we must drop all until the next \n
968 if(mp_fd->size - mp_fd->pos <= 1) {
969 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd);
970 mp_fd->pos = 0;
971 mp_fd->drop = 1;
973 break;
975 // We already have a cmd : set the got_cmd flag
976 else if((*ret)) {
977 mp_fd->got_cmd = 1;
978 break;
981 l = end - mp_fd->buffer;
983 // Not dropping : put the cmd in ret
984 if (!mp_fd->drop) {
985 (*ret) = malloc(l+1);
986 strncpy((*ret),mp_fd->buffer,l);
987 (*ret)[l] = '\0';
988 } else { // Remove the dropping flag
989 mp_fd->drop = 0;
991 if( mp_fd->pos - (l+1) > 0)
992 memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1));
993 mp_fd->pos -= l+1;
996 if(*ret)
997 return 1;
998 else
999 return MP_INPUT_NOTHING;
1002 static int
1003 mp_input_default_cmd_func(int fd,char* buf, int l) {
1005 while(1) {
1006 int r = read(fd,buf,l);
1007 // Error ?
1008 if(r < 0) {
1009 if(errno == EINTR)
1010 continue;
1011 else if(errno == EAGAIN)
1012 return MP_INPUT_NOTHING;
1013 return MP_INPUT_ERROR;
1014 // EOF ?
1016 return r;
1022 void
1023 mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) {
1024 mp_cmd_filter_t* filter = malloc(sizeof(mp_cmd_filter_t))/*, *prev*/;
1026 filter->filter = func;
1027 filter->ctx = ctx;
1028 filter->next = cmd_filters;
1029 cmd_filters = filter;
1033 static char*
1034 mp_input_find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys) {
1035 int j;
1037 if (n <= 0) return NULL;
1038 for(j = 0; binds[j].cmd != NULL; j++) {
1039 int found = 1,s;
1040 for(s = 0; s < n && binds[j].input[s] != 0; s++) {
1041 if(binds[j].input[s] != keys[s]) {
1042 found = 0;
1043 break;
1046 if(found && binds[j].input[s] == 0 && s == n)
1047 break;
1049 return binds[j].cmd;
1052 static mp_cmd_bind_section_t*
1053 mp_input_get_bind_section(char *section) {
1054 mp_cmd_bind_section_t* bind_section = cmd_binds_section;
1056 if (section==NULL) section="default";
1057 while (bind_section) {
1058 if(strcmp(section,bind_section->section)==0) return bind_section;
1059 if(bind_section->next==NULL) break;
1060 bind_section=bind_section->next;
1062 if(bind_section) {
1063 bind_section->next=malloc(sizeof(mp_cmd_bind_section_t));
1064 bind_section=bind_section->next;
1065 } else {
1066 cmd_binds_section=malloc(sizeof(mp_cmd_bind_section_t));
1067 bind_section=cmd_binds_section;
1069 bind_section->cmd_binds=NULL;
1070 bind_section->section=strdup(section);
1071 bind_section->next=NULL;
1072 return bind_section;
1075 static mp_cmd_t*
1076 mp_input_get_cmd_from_keys(int n,int* keys, int paused) {
1077 char* cmd = NULL;
1078 mp_cmd_t* ret;
1080 if(cmd_binds)
1081 cmd = mp_input_find_bind_for_key(cmd_binds,n,keys);
1082 if(cmd_binds_default && cmd == NULL)
1083 cmd = mp_input_find_bind_for_key(cmd_binds_default,n,keys);
1084 if(default_bindings && cmd == NULL)
1085 cmd = mp_input_find_bind_for_key(def_cmd_binds,n,keys);
1087 if(cmd == NULL) {
1088 mp_msg(MSGT_INPUT,MSGL_WARN,MSGTR_NoBindFound,mp_input_get_key_name(keys[0]));
1089 if(n > 1) {
1090 int s;
1091 for(s=1; s < n; s++)
1092 mp_msg(MSGT_INPUT,MSGL_WARN,"-%s",mp_input_get_key_name(keys[s]));
1094 mp_msg(MSGT_INPUT,MSGL_WARN," \n");
1095 return NULL;
1097 if (strcmp(cmd, "ignore") == 0) return NULL;
1098 ret = mp_input_parse_cmd(cmd);
1099 if(!ret) {
1100 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrInvalidCommandForKey,mp_input_get_key_name(key_down[0]));
1101 if( num_key_down > 1) {
1102 unsigned int s;
1103 for(s=1; s < num_key_down; s++)
1104 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(key_down[s]));
1106 mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd);
1108 return ret;
1112 static mp_cmd_t*
1113 interpret_key(int code, int paused)
1115 unsigned int j;
1116 mp_cmd_t* ret;
1118 if(mp_input_key_cb) {
1119 if (code & MP_KEY_DOWN)
1120 return NULL;
1121 code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY);
1122 if (mp_input_key_cb(code))
1123 return NULL;
1126 if(code & MP_KEY_DOWN) {
1127 if(num_key_down > MP_MAX_KEY_DOWN) {
1128 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1129 return NULL;
1131 code &= ~MP_KEY_DOWN;
1132 // Check if we don't already have this key as pushed
1133 for(j = 0; j < num_key_down; j++) {
1134 if(key_down[j] == code)
1135 break;
1137 if(j != num_key_down)
1138 return NULL;
1139 key_down[num_key_down] = code;
1140 num_key_down++;
1141 last_key_down = GetTimer();
1142 ar_state = 0;
1143 return NULL;
1145 // key released
1146 // Check if the key is in the down key, driver which can't send push event
1147 // send only release event
1148 for(j = 0; j < num_key_down; j++) {
1149 if(key_down[j] == code)
1150 break;
1152 if(j == num_key_down) { // key was not in the down keys : add it
1153 if(num_key_down > MP_MAX_KEY_DOWN) {
1154 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2ManyKeyDowns);
1155 return NULL;
1157 key_down[num_key_down] = code;
1158 num_key_down++;
1159 last_key_down = 1;
1161 // We ignore key from last combination
1162 ret = last_key_down ? mp_input_get_cmd_from_keys(num_key_down,key_down,paused) : NULL;
1163 // Remove the key
1164 if(j+1 < num_key_down)
1165 memmove(&key_down[j],&key_down[j+1],(num_key_down-(j+1))*sizeof(int));
1166 num_key_down--;
1167 last_key_down = 0;
1168 ar_state = -1;
1169 if(ar_cmd) {
1170 mp_cmd_free(ar_cmd);
1171 ar_cmd = NULL;
1173 return ret;
1176 static mp_cmd_t *check_autorepeat(int paused)
1178 // No input : autorepeat ?
1179 if(ar_rate > 0 && ar_state >=0 && num_key_down > 0 && ! (key_down[num_key_down-1] & MP_NO_REPEAT_KEY)) {
1180 unsigned int t = GetTimer();
1181 // First time : wait delay
1182 if(ar_state == 0 && (t - last_key_down) >= ar_delay*1000) {
1183 ar_cmd = mp_input_get_cmd_from_keys(num_key_down,key_down,paused);
1184 if(!ar_cmd) {
1185 ar_state = -1;
1186 return NULL;
1188 ar_state = 1;
1189 last_ar = t;
1190 return mp_cmd_clone(ar_cmd);
1191 // Then send rate / sec event
1192 } else if(ar_state == 1 && (t -last_ar) >= 1000000/ar_rate) {
1193 last_ar = t;
1194 return mp_cmd_clone(ar_cmd);
1197 return NULL;
1201 static mp_cmd_t *read_events(int time, int paused)
1203 int i;
1204 int got_cmd = 0;
1205 mp_cmd_t *autorepeat_cmd;
1206 #ifdef HAVE_POSIX_SELECT
1207 fd_set fds;
1208 #endif
1209 for (i = 0; i < num_key_fd; i++)
1210 if (key_fds[i].dead) {
1211 mp_input_rm_key_fd(key_fds[i].fd);
1212 i--;
1214 for (i = 0; i < num_cmd_fd; i++)
1215 if (cmd_fds[i].dead || cmd_fds[i].eof) {
1216 mp_input_rm_cmd_fd(cmd_fds[i].fd);
1217 i--;
1219 else if (cmd_fds[i].got_cmd)
1220 got_cmd = 1;
1221 #ifdef HAVE_POSIX_SELECT
1222 FD_ZERO(&fds);
1223 if (!got_cmd) {
1224 int max_fd = 0, num_fd = 0;
1225 for (i = 0; i < num_key_fd; i++) {
1226 if (key_fds[i].no_select)
1227 continue;
1228 if (key_fds[i].fd > max_fd)
1229 max_fd = key_fds[i].fd;
1230 FD_SET(key_fds[i].fd, &fds);
1231 num_fd++;
1233 for (i = 0; i < num_cmd_fd; i++) {
1234 if (cmd_fds[i].no_select)
1235 continue;
1236 if (cmd_fds[i].fd > max_fd)
1237 max_fd = cmd_fds[i].fd;
1238 FD_SET(cmd_fds[i].fd, &fds);
1239 num_fd++;
1241 if (num_fd > 0) {
1242 struct timeval tv, *time_val;
1243 if (time >= 0) {
1244 tv.tv_sec = time / 1000;
1245 tv.tv_usec = (time % 1000) * 1000;
1246 time_val = &tv;
1248 else
1249 time_val = NULL;
1250 if (select(max_fd + 1, &fds, NULL, NULL, time_val) < 0) {
1251 if (errno != EINTR)
1252 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrSelect,
1253 strerror(errno));
1254 FD_ZERO(&fds);
1258 #else
1259 if (!got_cmd && time)
1260 usec_sleep(time * 1000);
1261 #endif
1264 for (i = 0; i < num_key_fd; i++) {
1265 int code;
1266 #ifdef HAVE_POSIX_SELECT
1267 if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds))
1268 continue;
1269 #endif
1271 if (key_fds[i].no_readfunc_retval) { // getch2 handler special-cased for now
1272 ((void (*)(void))key_fds[i].read_func)();
1273 if (cmd_queue_length)
1274 return NULL;
1275 code = mplayer_get_key(0);
1276 if (code < 0)
1277 code = MP_INPUT_NOTHING;
1279 else
1280 code = ((mp_key_func_t)key_fds[i].read_func)(key_fds[i].fd);
1281 if (code >= 0) {
1282 mp_cmd_t *ret = interpret_key(code, paused);
1283 if (ret)
1284 return ret;
1286 else if (code == MP_INPUT_ERROR)
1287 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnKeyInFd,
1288 key_fds[i].fd);
1289 else if (code == MP_INPUT_DEAD) {
1290 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrDeadKeyOnFd,
1291 key_fds[i].fd);
1292 key_fds[i].dead = 1;
1295 autorepeat_cmd = check_autorepeat(paused);
1296 if (autorepeat_cmd)
1297 return autorepeat_cmd;
1299 for (i = 0; i < num_cmd_fd; i++) {
1300 char *cmd;
1301 int r;
1302 #ifdef HAVE_POSIX_SELECT
1303 if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) &&
1304 !cmd_fds[i].got_cmd)
1305 continue;
1306 #endif
1307 r = mp_input_read_cmd(&cmd_fds[i], &cmd);
1308 if (r >= 0) {
1309 mp_cmd_t *ret = mp_input_parse_cmd(cmd);
1310 free(cmd);
1311 if (ret)
1312 return ret;
1314 else if (r == MP_INPUT_ERROR)
1315 mp_msg(MSGT_INPUT, MSGL_ERR, MSGTR_INPUT_INPUT_ErrOnCmdFd,
1316 cmd_fds[i].fd);
1317 else if (r == MP_INPUT_DEAD)
1318 cmd_fds[i].dead = 1;
1321 return NULL;
1326 mp_input_queue_cmd(mp_cmd_t* cmd) {
1327 if(!cmd || cmd_queue_length >= CMD_QUEUE_SIZE)
1328 return 0;
1329 cmd_queue[cmd_queue_end] = cmd;
1330 cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE;
1331 cmd_queue_length++;
1332 return 1;
1335 static mp_cmd_t*
1336 mp_input_get_queued_cmd(int peek_only) {
1337 mp_cmd_t* ret;
1339 if(cmd_queue_length == 0)
1340 return NULL;
1342 ret = cmd_queue[cmd_queue_start];
1344 if (!peek_only) {
1345 cmd_queue_length--;
1346 cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE;
1349 return ret;
1353 * \param peek_only when set, the returned command stays in the queue.
1354 * Do not free the returned cmd whe you set this!
1356 mp_cmd_t*
1357 mp_input_get_cmd(int time, int paused, int peek_only) {
1358 mp_cmd_t* ret = NULL;
1359 mp_cmd_filter_t* cf;
1360 int from_queue;
1362 if (async_quit_request)
1363 return mp_input_parse_cmd("quit 1");
1364 while(1) {
1365 from_queue = 1;
1366 ret = mp_input_get_queued_cmd(peek_only);
1367 if(ret) break;
1368 from_queue = 0;
1369 ret = read_events(time, paused);
1370 if (!ret) {
1371 from_queue = 1;
1372 ret = mp_input_get_queued_cmd(peek_only);
1374 break;
1376 if(!ret) return NULL;
1378 for(cf = cmd_filters ; cf ; cf = cf->next) {
1379 if(cf->filter(ret,paused,cf->ctx)) {
1380 if (peek_only && from_queue)
1381 // The filter ate the cmd, so we remove it from queue
1382 ret = mp_input_get_queued_cmd(0);
1383 mp_cmd_free(ret);
1384 return NULL;
1388 if (!from_queue && peek_only)
1389 mp_input_queue_cmd(ret);
1391 return ret;
1394 void
1395 mp_cmd_free(mp_cmd_t* cmd) {
1396 int i;
1397 //#ifdef MP_DEBUG
1398 // assert(cmd != NULL);
1399 //#endif
1400 if ( !cmd ) return;
1402 if(cmd->name)
1403 free(cmd->name);
1405 for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1406 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1407 free(cmd->args[i].v.s);
1409 free(cmd);
1412 mp_cmd_t*
1413 mp_cmd_clone(mp_cmd_t* cmd) {
1414 mp_cmd_t* ret;
1415 int i;
1416 #ifdef MP_DEBUG
1417 assert(cmd != NULL);
1418 #endif
1420 ret = malloc(sizeof(mp_cmd_t));
1421 memcpy(ret,cmd,sizeof(mp_cmd_t));
1422 if(cmd->name)
1423 ret->name = strdup(cmd->name);
1424 for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
1425 if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
1426 ret->args[i].v.s = strdup(cmd->args[i].v.s);
1429 return ret;
1432 static char key_str[12];
1434 static char*
1435 mp_input_get_key_name(int key) {
1436 int i;
1438 for(i = 0; key_names[i].name != NULL; i++) {
1439 if(key_names[i].key == key)
1440 return key_names[i].name;
1443 if(isascii(key)) {
1444 snprintf(key_str,12,"%c",(char)key);
1445 return key_str;
1448 // Print the hex key code
1449 snprintf(key_str,12,"%#-8x",key);
1450 return key_str;
1455 mp_input_get_key_from_name(const char *name) {
1456 int i,ret = 0,len = strlen(name);
1457 if(len == 1) { // Direct key code
1458 ret = (unsigned char)name[0];
1459 return ret;
1460 } else if(len > 2 && strncasecmp("0x",name,2) == 0)
1461 return strtol(name,NULL,16);
1463 for(i = 0; key_names[i].name != NULL; i++) {
1464 if(strcasecmp(key_names[i].name,name) == 0)
1465 return key_names[i].key;
1468 return -1;
1471 static int
1472 mp_input_get_input_from_name(char* name,int* keys) {
1473 char *end,*ptr;
1474 int n=0;
1476 ptr = name;
1477 n = 0;
1478 for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) {
1479 if(end && end[1] != '\0') {
1480 if(end[1] == '-')
1481 end = &end[1];
1482 end[0] = '\0';
1484 keys[n] = mp_input_get_key_from_name(ptr);
1485 if(keys[n] < 0) {
1486 return 0;
1488 n++;
1489 if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN)
1490 ptr = &end[1];
1491 else
1492 break;
1494 keys[n] = 0;
1495 return 1;
1498 #define BS_MAX 256
1499 #define SPACE_CHAR " \n\r\t"
1501 void
1502 mp_input_bind_keys(const int keys[MP_MAX_KEY_DOWN+1], char* cmd) {
1503 int i = 0,j;
1504 mp_cmd_bind_t* bind = NULL;
1505 mp_cmd_bind_section_t* bind_section = NULL;
1506 char *section=NULL, *p;
1508 #ifdef MP_DEBUG
1509 assert(keys != NULL);
1510 assert(cmd != NULL);
1511 #endif
1513 if(*cmd=='{' && (p=strchr(cmd,'}'))) {
1514 *p=0;
1515 section=++cmd;
1516 cmd=++p;
1517 // Jump beginning space
1518 for( ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)
1519 /* NOTHING */;
1521 bind_section=mp_input_get_bind_section(section);
1523 if(bind_section->cmd_binds) {
1524 for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {
1525 for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++)
1526 /* NOTHING */;
1527 if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {
1528 bind = &bind_section->cmd_binds[i];
1529 break;
1534 if(!bind) {
1535 bind_section->cmd_binds = realloc(bind_section->cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));
1536 memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));
1537 bind = &bind_section->cmd_binds[i];
1539 if(bind->cmd)
1540 free(bind->cmd);
1541 bind->cmd = strdup(cmd);
1542 memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));
1545 void
1546 mp_input_add_binds(const mp_cmd_bind_t* list) {
1547 int i;
1548 for(i = 0 ; list[i].cmd ; i++)
1549 mp_input_bind_keys(list[i].input,list[i].cmd);
1552 static void
1553 mp_input_free_binds(mp_cmd_bind_t* binds) {
1554 int i;
1556 if(!binds)
1557 return;
1559 for(i = 0; binds[i].cmd != NULL; i++)
1560 free(binds[i].cmd);
1562 free(binds);
1566 static int
1567 mp_input_parse_config(char *file) {
1568 int fd;
1569 int bs = 0,r,eof = 0,comments = 0;
1570 char *iter,*end;
1571 char buffer[BS_MAX];
1572 int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 };
1574 fd = open(file,O_RDONLY);
1576 if(fd < 0) {
1577 mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno));
1578 return 0;
1581 mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file);
1583 while(1) {
1584 if(! eof && bs < BS_MAX-1) {
1585 if(bs > 0) bs--;
1586 r = read(fd,buffer+bs,BS_MAX-1-bs);
1587 if(r < 0) {
1588 if(errno == EINTR)
1589 continue;
1590 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));
1591 close(fd);
1592 return 0;
1593 } else if(r == 0) {
1594 eof = 1;
1595 } else {
1596 bs += r+1;
1597 buffer[bs-1] = '\0';
1600 // Empty buffer : return
1601 if(bs <= 1) {
1602 mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds);
1603 close(fd);
1604 return 1;
1607 iter = buffer;
1609 if(comments) {
1610 for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++)
1611 /* NOTHING */;
1612 if(iter[0] == '\0') { // Buffer was full of comment
1613 bs = 0;
1614 continue;
1616 iter++;
1617 r = strlen(iter);
1618 memmove(buffer,iter,r+1);
1619 bs = r+1;
1620 comments = 0;
1621 continue;
1624 // Find the wanted key
1625 if(keys[0] == 0) {
1626 // Jump beginning space
1627 for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++)
1628 /* NOTHING */;
1629 if(iter[0] == '\0') { // Buffer was full of space char
1630 bs = 0;
1631 continue;
1633 if(iter[0] == '#') { // Comments
1634 comments = 1;
1635 continue;
1637 // Find the end of the key code name
1638 for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++)
1639 /*NOTHING */;
1640 if(end[0] == '\0') { // Key name doesn't fit in the buffer
1641 if(buffer == iter) {
1642 if(eof && (buffer-iter) == bs)
1643 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);
1644 else
1645 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);
1646 return 0;
1648 memmove(buffer,iter,end-iter);
1649 bs = end-iter;
1650 continue;
1653 char name[end-iter+1];
1654 strncpy(name,iter,end-iter);
1655 name[end-iter] = '\0';
1656 if(! mp_input_get_input_from_name(name,keys)) {
1657 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownKey,name);
1658 close(fd);
1659 return 0;
1662 if( bs > (end-buffer))
1663 memmove(buffer,end,bs - (end-buffer));
1664 bs -= end-buffer;
1665 continue;
1666 } else { // Get the command
1667 while(iter[0] == ' ' || iter[0] == '\t') iter++;
1668 // Found new line
1669 if(iter[0] == '\n' || iter[0] == '\r') {
1670 int i;
1671 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrNoCmdForKey,mp_input_get_key_name(keys[0]));
1672 for(i = 1; keys[i] != 0 ; i++)
1673 mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i]));
1674 mp_msg(MSGT_INPUT,MSGL_ERR,"\n");
1675 keys[0] = 0;
1676 if(iter > buffer) {
1677 memmove(buffer,iter,bs- (iter-buffer));
1678 bs -= (iter-buffer);
1680 continue;
1682 for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++)
1683 /* NOTHING */;
1684 if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) {
1685 if(iter == buffer) {
1686 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);
1687 close(fd);
1688 return 0;
1690 memmove(buffer,iter,end - iter);
1691 bs = end - iter;
1692 continue;
1695 char cmd[end-iter+1];
1696 strncpy(cmd,iter,end-iter);
1697 cmd[end-iter] = '\0';
1698 //printf("Set bind %d => %s\n",keys[0],cmd);
1699 mp_input_bind_keys(keys,cmd);
1700 n_binds++;
1702 keys[0] = 0;
1703 end++;
1704 if(bs > (end-buffer))
1705 memmove(buffer,end,bs-(end-buffer));
1706 bs -= (end-buffer);
1707 buffer[bs-1] = '\0';
1708 continue;
1711 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrWhyHere);
1712 close(fd);
1713 mp_input_set_section(NULL);
1714 return 0;
1717 void
1718 mp_input_set_section(char *name) {
1719 mp_cmd_bind_section_t* bind_section = NULL;
1721 cmd_binds=NULL;
1722 cmd_binds_default=NULL;
1723 if(section) free(section);
1724 if(name) section=strdup(name); else section=strdup("default");
1725 if((bind_section=mp_input_get_bind_section(section)))
1726 cmd_binds=bind_section->cmd_binds;
1727 if(strcmp(section,"default")==0) return;
1728 if((bind_section=mp_input_get_bind_section(NULL)))
1729 cmd_binds_default=bind_section->cmd_binds;
1732 char*
1733 mp_input_get_section(void) {
1734 return section;
1737 void
1738 mp_input_init(int use_gui) {
1739 char* file;
1741 #ifdef CONFIG_GUI
1742 if(use_gui)
1743 mp_input_add_binds(gui_def_cmd_binds);
1744 #endif
1746 file = config_file[0] != '/' ? get_path(config_file) : config_file;
1747 if(!file)
1748 return;
1750 if( !mp_input_parse_config(file)) {
1751 // free file if it was allocated by get_path(),
1752 // before it gets overwritten
1753 if( file != config_file)
1755 free(file);
1757 // Try global conf dir
1758 file = MPLAYER_CONFDIR "/input.conf";
1759 if(! mp_input_parse_config(file))
1760 mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n");
1762 else
1764 // free file if it was allocated by get_path()
1765 if( file != config_file)
1766 free(file);
1769 #ifdef CONFIG_JOYSTICK
1770 if(use_joystick) {
1771 int fd = mp_input_joystick_init(js_dev);
1772 if(fd < 0)
1773 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitJoystick);
1774 else
1775 mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close);
1777 #endif
1779 #ifdef CONFIG_LIRC
1780 if(use_lirc) {
1781 int fd = mp_input_lirc_init();
1782 if(fd > 0)
1783 mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close);
1785 #endif
1787 #ifdef CONFIG_LIRCC
1788 if(use_lircc) {
1789 int fd = lircc_init("mplayer", NULL);
1790 if(fd >= 0)
1791 mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup);
1793 #endif
1795 #ifdef CONFIG_APPLE_REMOTE
1796 if(use_ar) {
1797 if(mp_input_ar_init() < 0)
1798 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1799 else
1800 mp_input_add_key_fd(-1,0,mp_input_ar_read,mp_input_ar_close);
1802 #endif
1804 #ifdef CONFIG_APPLE_IR
1805 if(use_ar) {
1806 int fd = mp_input_appleir_init(ar_dev);
1807 if(fd < 0)
1808 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);
1809 else
1810 mp_input_add_key_fd(fd,1,mp_input_appleir_read,(mp_close_func_t)close);
1812 #endif
1814 if(in_file) {
1815 struct stat st;
1816 if(stat(in_file,&st))
1817 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,in_file,strerror(errno));
1818 else {
1819 in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY);
1820 if(in_file_fd >= 0)
1821 mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close);
1822 else
1823 mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));
1829 void
1830 mp_input_uninit(void) {
1831 unsigned int i;
1832 mp_cmd_bind_section_t* bind_section;
1834 for(i=0; i < num_key_fd; i++) {
1835 if(key_fds[i].close_func)
1836 key_fds[i].close_func(key_fds[i].fd);
1839 for(i=0; i < num_cmd_fd; i++) {
1840 if(cmd_fds[i].close_func)
1841 cmd_fds[i].close_func(cmd_fds[i].fd);
1843 while (cmd_binds_section) {
1844 mp_input_free_binds(cmd_binds_section->cmd_binds);
1845 free(cmd_binds_section->section);
1846 bind_section=cmd_binds_section->next;
1847 free(cmd_binds_section);
1848 cmd_binds_section=bind_section;
1850 cmd_binds_section=NULL;
1853 void
1854 mp_input_register_options(m_config_t* cfg) {
1855 m_config_register_options(cfg,mp_input_opts);
1858 static int mp_input_print_key_list(m_option_t* cfg) {
1859 int i;
1860 printf("\n");
1861 for(i= 0; key_names[i].name != NULL ; i++)
1862 printf("%s\n",key_names[i].name);
1863 exit(0);
1866 static int mp_input_print_cmd_list(m_option_t* cfg) {
1867 const mp_cmd_t *cmd;
1868 int i,j;
1869 const char* type;
1871 for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
1872 printf("%-20.20s",cmd->name);
1873 for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
1874 switch(cmd->args[j].type) {
1875 case MP_CMD_ARG_INT:
1876 type = "Integer";
1877 break;
1878 case MP_CMD_ARG_FLOAT:
1879 type = "Float";
1880 break;
1881 case MP_CMD_ARG_STRING:
1882 type = "String";
1883 break;
1884 default:
1885 type = "??";
1887 if(j+1 > cmd->nargs)
1888 printf(" [%s]",type);
1889 else
1890 printf(" %s",type);
1892 printf("\n");
1894 exit(0);
1898 mp_input_check_interrupt(int time) {
1899 mp_cmd_t* cmd;
1900 if((cmd = mp_input_get_cmd(time,0,1)) == NULL)
1901 return 0;
1902 switch(cmd->id) {
1903 case MP_CMD_QUIT:
1904 case MP_CMD_PLAY_TREE_STEP:
1905 case MP_CMD_PLAY_TREE_UP_STEP:
1906 case MP_CMD_PLAY_ALT_SRC_STEP:
1907 // The cmd will be executed when we are back in the main loop
1908 return 1;
1910 // remove the cmd from the queue
1911 cmd = mp_input_get_cmd(time,0,0);
1912 mp_cmd_free(cmd);
1913 return 0;