📄 input.c
字号:
}#ifndef HAVE_NO_POSIX_SELECT if( ! (cmd_fds[i].flags & MP_FD_NO_SELECT) && ! FD_ISSET(cmd_fds[i].fd,&fds) && ! (cmd_fds[i].flags & MP_FD_GOT_CMD) ) continue;#endif r = mp_input_read_cmd(&cmd_fds[i],&cmd); if(r < 0) { if(r == MP_INPUT_ERROR) mp_msg(MSGT_INPUT,MSGL_ERR,"Error on cmd fd %d\n",cmd_fds[i].fd); else if(r == MP_INPUT_DEAD) cmd_fds[i].flags |= MP_FD_DEAD; continue; } ret = mp_input_parse_cmd(cmd); free(cmd); if(!ret) continue; last_loop = i; return ret; } last_loop = 0; return NULL; }intmp_input_queue_cmd(mp_cmd_t* cmd) { if(cmd_queue_length >= CMD_QUEUE_SIZE) return 0; cmd_queue[cmd_queue_end] = cmd; cmd_queue_end = (cmd_queue_end + 1) % CMD_QUEUE_SIZE; cmd_queue_length++; return 1;}static mp_cmd_t*mp_input_get_queued_cmd(int peek_only) { mp_cmd_t* ret; if(cmd_queue_length == 0) return NULL; ret = cmd_queue[cmd_queue_start]; if (!peek_only) { cmd_queue_length--; cmd_queue_start = (cmd_queue_start + 1) % CMD_QUEUE_SIZE; } return ret;} /** * \param peek_only when set, the returned command stays in the queue. * Do not free the returned cmd whe you set this! */mp_cmd_t*mp_input_get_cmd(int time, int paused, int peek_only) { mp_cmd_t* ret = NULL; mp_cmd_filter_t* cf; int from_queue; while(1) { from_queue = 1; ret = mp_input_get_queued_cmd(peek_only); if(ret) break; from_queue = 0; ret = mp_input_read_keys(time,paused); if(ret) break; ret = mp_input_read_cmds(time); break; } if(!ret) return NULL; for(cf = cmd_filters ; cf ; cf = cf->next) { if(cf->filter(ret,paused,cf->ctx)) return NULL; } if (!from_queue && peek_only) mp_input_queue_cmd(ret); return ret;}voidmp_cmd_free(mp_cmd_t* cmd) { int i;//#ifdef MP_DEBUG// assert(cmd != NULL);//#endif if ( !cmd ) return; if(cmd->name) free(cmd->name); for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) { if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL) free(cmd->args[i].v.s); } free(cmd);}mp_cmd_t*mp_cmd_clone(mp_cmd_t* cmd) { mp_cmd_t* ret; int i;#ifdef MP_DEBUG assert(cmd != NULL);#endif ret = (mp_cmd_t*)malloc(sizeof(mp_cmd_t)); memcpy(ret,cmd,sizeof(mp_cmd_t)); if(cmd->name) ret->name = strdup(cmd->name); for(i = 0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) { if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL) ret->args[i].v.s = strdup(cmd->args[i].v.s); } return ret;}static char key_str[12];static char*mp_input_get_key_name(int key) { int i; for(i = 0; key_names[i].name != NULL; i++) { if(key_names[i].key == key) return key_names[i].name; } if(isascii(key)) { snprintf(key_str,12,"%c",(char)key); return key_str; } // Print the hex key code snprintf(key_str,12,"%#-8x",key); return key_str;}static intmp_input_get_key_from_name(char* name) { int i,ret = 0,len = strlen(name); if(len == 1) { // Direct key code ret = (unsigned char)name[0]; return ret; } else if(len > 2 && strncasecmp("0x",name,2) == 0) return strtol(name,NULL,16); for(i = 0; key_names[i].name != NULL; i++) { if(strcasecmp(key_names[i].name,name) == 0) return key_names[i].key; } return -1;}static intmp_input_get_input_from_name(char* name,int* keys) { char *end,*ptr; int n=0; ptr = name; n = 0; for(end = strchr(ptr,'-') ; ptr != NULL ; end = strchr(ptr,'-')) { if(end && end[1] != '\0') { if(end[1] == '-') end = &end[1]; end[0] = '\0'; } keys[n] = mp_input_get_key_from_name(ptr); if(keys[n] < 0) { return 0; } n++; if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN) ptr = &end[1]; else break; } keys[n] = 0; return 1;}voidmp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) { int i = 0,j; mp_cmd_bind_t* bind = NULL;#ifdef MP_DEBUG assert(keys != NULL); assert(cmd != NULL);#endif if(cmd_binds) { for(i = 0; cmd_binds[i].cmd != NULL ; i++) { for(j = 0 ; cmd_binds[i].input[j] == keys[j] && keys[j] != 0 ; j++) /* NOTHING */; if(keys[j] == 0 && cmd_binds[i].input[j] == 0 ) { bind = &cmd_binds[i]; break; } } } if(!bind) { cmd_binds = (mp_cmd_bind_t*)realloc(cmd_binds,(i+2)*sizeof(mp_cmd_bind_t)); memset(&cmd_binds[i],0,2*sizeof(mp_cmd_bind_t)); bind = &cmd_binds[i]; } if(bind->cmd) free(bind->cmd); bind->cmd = strdup(cmd); memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));}static voidmp_input_free_binds(mp_cmd_bind_t* binds) { int i; if(!binds) return; for(i = 0; binds[i].cmd != NULL; i++) free(binds[i].cmd); free(binds);} #define BS_MAX 256#define SPACE_CHAR " \n\r\t"static intmp_input_parse_config(char *file) { int fd; int bs = 0,r,eof = 0,comments = 0; char *iter,*end; char buffer[BS_MAX]; int n_binds = 0, keys[MP_MAX_KEY_DOWN+1] = { 0 }; mp_cmd_bind_t* binds = NULL; fd = open(file,O_RDONLY); if(fd < 0) { mp_msg(MSGT_INPUT,MSGL_V,"Can't open input config file %s: %s\n",file,strerror(errno)); return 0; } mp_msg(MSGT_INPUT,MSGL_V,"Parsing input config file %s\n",file); while(1) { if(! eof && bs < BS_MAX-1) { if(bs > 0) bs--; r = read(fd,buffer+bs,BS_MAX-1-bs); if(r < 0) { if(errno == EINTR) continue; mp_msg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",file,strerror(errno)); mp_input_free_binds(binds); close(fd); return 0; } else if(r == 0) { eof = 1; } else { bs += r+1; buffer[bs-1] = '\0'; } } // Empty buffer : return if(bs <= 1) { mp_msg(MSGT_INPUT,MSGL_V,"Input config file %s parsed: %d binds\n",file,n_binds); if(binds) cmd_binds = binds; close(fd); return 1; } iter = buffer; if(comments) { for( ; iter[0] != '\0' && iter[0] != '\n' ; iter++) /* NOTHING */; if(iter[0] == '\0') { // Buffer was full of comment bs = 0; continue; } iter++; r = strlen(iter); if(r) memmove(buffer,iter,r+1); bs = r+1; if(iter[0] != '#') comments = 0; continue; } // Find the wanted key if(keys[0] == 0) { // Jump beginning space for( ; iter[0] != '\0' && strchr(SPACE_CHAR,iter[0]) != NULL ; iter++) /* NOTHING */; if(iter[0] == '\0') { // Buffer was full of space char bs = 0; continue; } if(iter[0] == '#') { // Comments comments = 1; continue; } // Find the end of the key code name for(end = iter; end[0] != '\0' && strchr(SPACE_CHAR,end[0]) == NULL ; end++) /*NOTHING */; if(end[0] == '\0') { // Key name doesn't fit in the buffer if(buffer == iter) { if(eof && (buffer-iter) == bs) mp_msg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter); else mp_msg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",iter); mp_input_free_binds(binds); return 0; } memmove(buffer,iter,end-iter); bs = end-iter; continue; } { char name[end-iter+1]; strncpy(name,iter,end-iter); name[end-iter] = '\0'; if(! mp_input_get_input_from_name(name,keys)) { mp_msg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",name); mp_input_free_binds(binds); close(fd); return 0; } } if( bs > (end-buffer)) memmove(buffer,end,bs - (end-buffer)); bs -= end-buffer; continue; } else { // Get the command while(iter[0] == ' ' || iter[0] == '\t') iter++; // Found new line if(iter[0] == '\n' || iter[0] == '\r') { int i; mp_msg(MSGT_INPUT,MSGL_ERR,"No command found for key %s" ,mp_input_get_key_name(keys[0])); for(i = 1; keys[i] != 0 ; i++) mp_msg(MSGT_INPUT,MSGL_ERR,"-%s",mp_input_get_key_name(keys[i])); mp_msg(MSGT_INPUT,MSGL_ERR,"\n"); keys[0] = 0; if(iter > buffer) { memmove(buffer,iter,bs- (iter-buffer)); bs -= (iter-buffer); } continue; } for(end = iter ; end[0] != '\n' && end[0] != '\r' && end[0] != '\0' ; end++) /* NOTHING */; if(end[0] == '\0' && ! (eof && ((end+1) - buffer) == bs)) { if(iter == buffer) { mp_msg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",buffer); mp_input_free_binds(binds); close(fd); return 0; } memmove(buffer,iter,end - iter); bs = end - iter; continue; } { char cmd[end-iter+1]; strncpy(cmd,iter,end-iter); cmd[end-iter] = '\0'; //printf("Set bind %d => %s\n",keys[0],cmd); mp_input_bind_keys(keys,cmd); n_binds++; } keys[0] = 0; end++; if(bs > (end-buffer)) memmove(buffer,end,bs-(end-buffer)); bs -= (end-buffer); buffer[bs-1] = '\0'; continue; } } mp_msg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n"); close(fd); return 0;}extern char *get_path(char *filename);voidmp_input_init(void) { char* file; file = config_file[0] != '/' ? get_path(config_file) : config_file; if(!file) return; if( !mp_input_parse_config(file)) { // free file if it was allocated by get_path(), // before it gets overwritten if( file != config_file) { free(file); } // Try global conf dir file = MPLAYER_CONFDIR "/input.conf"; if(! mp_input_parse_config(file)) mp_msg(MSGT_INPUT,MSGL_V,"Falling back on default (hardcoded) input config\n"); } else { // free file if it was allocated by get_path() if( file != config_file) free(file); }#ifdef HAVE_JOYSTICK if(use_joystick) { int fd = mp_input_joystick_init(js_dev); if(fd < 0) mp_msg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n"); else mp_input_add_key_fd(fd,1,mp_input_joystick_read,(mp_close_func_t)close); }#endif#ifdef HAVE_LIRC if(use_lirc) { int fd = mp_input_lirc_init(); if(fd > 0) mp_input_add_cmd_fd(fd,0,mp_input_lirc_read,mp_input_lirc_close); }#endif#ifdef HAVE_LIRCC if(use_lircc) { int fd = lircc_init("mplayer", NULL); if(fd >= 0) mp_input_add_cmd_fd(fd,1,NULL,(mp_close_func_t)lircc_cleanup); }#endif if(in_file) { struct stat st; if(stat(in_file,&st)) mp_msg(MSGT_INPUT,MSGL_ERR,"Can't stat %s: %s\n",in_file,strerror(errno)); else { in_file_fd = open(in_file,S_ISFIFO(st.st_mode) ? O_RDWR : O_RDONLY); if(in_file_fd >= 0) mp_input_add_cmd_fd(in_file_fd,1,NULL,(mp_close_func_t)close); else mp_msg(MSGT_INPUT,MSGL_ERR,"Can't open %s: %s\n",in_file,strerror(errno)); } }}voidmp_input_uninit(void) { unsigned int i; for(i=0; i < num_key_fd; i++) { if(key_fds[i].close_func) key_fds[i].close_func(key_fds[i].fd); } for(i=0; i < num_cmd_fd; i++) { if(cmd_fds[i].close_func) cmd_fds[i].close_func(cmd_fds[i].fd); } mp_input_free_binds(cmd_binds); cmd_binds=NULL; }voidmp_input_register_options(m_config_t* cfg) { m_config_register_options(cfg,mp_input_opts);}static int mp_input_print_key_list(m_option_t* cfg) { int i; printf("\n"); for(i= 0; key_names[i].name != NULL ; i++) printf("%s\n",key_names[i].name); exit(0);}static int mp_input_print_cmd_list(m_option_t* cfg) { mp_cmd_t *cmd; int i,j; char* type; for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) { printf("%-20.20s",cmd->name); for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) { switch(cmd->args[j].type) { case MP_CMD_ARG_INT: type = "Integer"; break; case MP_CMD_ARG_FLOAT: type = "Float"; break; case MP_CMD_ARG_STRING: type = "String"; break; default: type = "??"; } if(j+1 > cmd->nargs) printf(" [%s]",type); else printf(" %s",type); } printf("\n"); } exit(0);}intmp_input_check_interrupt(int time) { mp_cmd_t* cmd; if((cmd = mp_input_get_cmd(time,0,1)) == NULL) return 0; switch(cmd->id) { case MP_CMD_QUIT: case MP_CMD_PLAY_TREE_STEP: case MP_CMD_PLAY_TREE_UP_STEP: case MP_CMD_PLAY_ALT_SRC_STEP: // The cmd will be executed when we are back in the main loop return 1; } // remove the cmd from the queue cmd = mp_input_get_cmd(time,0,0); mp_cmd_free(cmd); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -