input.c

来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 1,860 行 · 第 1/4 页

C
1,860
字号
  } 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;}#define BS_MAX 256#define SPACE_CHAR " \n\r\t"voidmp_input_bind_keys(int keys[MP_MAX_KEY_DOWN+1], char* cmd) {  #if 0  int i = 0,j;  mp_cmd_bind_t* bind = NULL;  mp_cmd_bind_section_t* bind_section = NULL;  char *section=NULL, *p;#ifdef MP_DEBUG  assert(keys != NULL);  assert(cmd != NULL);#endif  if(*cmd=='{' && (p=strchr(cmd,'}'))) {    *p=0;    section=++cmd;    cmd=++p;    // Jump beginning space    for(  ; cmd[0] != '\0' && strchr(SPACE_CHAR,cmd[0]) != NULL ; cmd++)      /* NOTHING */;  }  bind_section=mp_input_get_bind_section(section);  if(bind_section->cmd_binds) {    for(i = 0; bind_section->cmd_binds[i].cmd != NULL ; i++) {      for(j = 0 ; bind_section->cmd_binds[i].input[j] == keys[j]  && keys[j] != 0 ; j++)	/* NOTHING */;      if(keys[j] == 0 && bind_section->cmd_binds[i].input[j] == 0 ) {	bind = &bind_section->cmd_binds[i];	break;      }    }  }    if(!bind) {    bind_section->cmd_binds = (mp_cmd_bind_t*)realloc(bind_section->cmd_binds,(i+2)*sizeof(mp_cmd_bind_t));    memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t));    bind = &bind_section->cmd_binds[i];  }  if(bind->cmd)    free(bind->cmd);  bind->cmd = strdup(cmd);  memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int));  #endif}voidmp_input_add_binds(mp_cmd_bind_t* list) {  #if 0  int i;  for(i = 0 ; list[i].cmd ; i++)    mp_input_bind_keys(list[i].input,list[i].cmd);  #endif }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);}  static intmp_input_parse_config(char *file) {#if 0  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 };  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,MSGTR_INPUT_INPUT_ErrReadingInputConfig,file,strerror(errno));	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);      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,MSGTR_INPUT_INPUT_ErrUnfinishedBinding,iter);	  else	    mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrBuffer2SmallForKeyName,iter);	  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,MSGTR_INPUT_INPUT_ErrUnknownKey,name);	  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,MSGTR_INPUT_INPUT_ErrNoCmdForKey,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,MSGTR_INPUT_INPUT_ErrBuffer2SmallForCmd,buffer);	  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,MSGTR_INPUT_INPUT_ErrWhyHere);  close(fd);  mp_input_set_section(NULL);#endif  return 0;}voidmp_input_set_section(char *name) {  #if 0  mp_cmd_bind_section_t* bind_section = NULL;  cmd_binds=NULL;  cmd_binds_default=NULL;  if(section) free(section);  if(name) section=strdup(name); else section=strdup("default");  if((bind_section=mp_input_get_bind_section(section)))    cmd_binds=bind_section->cmd_binds;  if(strcmp(section,"default")==0) return;  if((bind_section=mp_input_get_bind_section(NULL)))    cmd_binds_default=bind_section->cmd_binds;#endif}char*mp_input_get_section(void) {  return section;}voidmp_input_init(int use_gui) {	#if 0  char* file;#ifdef HAVE_NEW_GUI    if(use_gui)    mp_input_add_binds(gui_def_cmd_binds);#endif    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,MSGTR_INPUT_INPUT_ErrCantInitJoystick);    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#ifdef HAVE_APPLE_REMOTE  if(use_ar) {    if(mp_input_ar_init() < 0)      mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantInitAppleRemote);    else      mp_input_add_key_fd(-1,0,mp_input_ar_read,mp_input_ar_close);  }#endif#if 0  if(in_file) {    struct stat st;    if(stat(in_file,&st))      mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantStatFile,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,MSGTR_INPUT_INPUT_ErrCantOpenFile,in_file,strerror(errno));    }  }#endif#endif}voidmp_input_uninit(void) {  #if 0  unsigned int i;  mp_cmd_bind_section_t* bind_section;  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);  }  while (cmd_binds_section) {    mp_input_free_binds(cmd_binds_section->cmd_binds);    free(cmd_binds_section->section);    bind_section=cmd_binds_section->next;    free(cmd_binds_section);    cmd_binds_section=bind_section;  }  cmd_binds_section=NULL;  #endif}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) {  #if 0  mp_cmd_t *cmd;  int i,j;  const 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);  #endif}intmp_input_check_interrupt(int time) { #if 0  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); #endif    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?