input.c
来自「君正早期ucos系统(只有早期的才不没有打包成库),MPLAYER,文件系统,图」· C语言 代码 · 共 1,860 行 · 第 1/4 页
C
1,860 行
{ { 0 }, NULL }};#ifdef HAVE_NEW_GUIstatic mp_cmd_bind_t gui_def_cmd_binds[] = { { { 'l', 0 }, "gui_loadfile" }, { { 't', 0 }, "gui_loadsubtitle" }, { { KEY_ENTER, 0 }, "gui_play" }, { { KEY_ESC, 0 }, "gui_stop" }, { { 'p', 0 }, "gui_playlist" }, { { 'r', 0 }, "gui_preferences" }, { { 'c', 0 }, "gui_skinbrowser" }, { { 0 }, NULL }};#endif#ifndef MP_MAX_KEY_FD#define MP_MAX_KEY_FD 10#endif#ifndef MP_MAX_CMD_FD#define MP_MAX_CMD_FD 10#endif#define CMD_QUEUE_SIZE 100typedef struct mp_input_fd { int fd; void* read_func; mp_close_func_t close_func; unsigned eof : 1; unsigned drop : 1; unsigned dead : 1; unsigned got_cmd : 1; unsigned no_select : 1; unsigned no_readfunc_retval : 1; // These fields are for the cmd fds. char* buffer; int pos,size;} mp_input_fd_t;typedef struct mp_cmd_filter_st mp_cmd_filter_t;struct mp_cmd_filter_st { mp_input_cmd_filter filter; void* ctx; mp_cmd_filter_t* next;};typedef struct mp_cmd_bind_section_st mp_cmd_bind_section_t;struct mp_cmd_bind_section_st { mp_cmd_bind_t* cmd_binds; char* section; mp_cmd_bind_section_t* next;};// These are the user defined bindsstatic mp_cmd_bind_section_t* cmd_binds_section = NULL;static char* section = NULL;static mp_cmd_bind_t* cmd_binds = NULL;static mp_cmd_bind_t* cmd_binds_default = NULL;static mp_cmd_filter_t* cmd_filters = NULL;// Callback to allow the menu filter to grab the incoming keysvoid (*mp_input_key_cb)(int code) = NULL;static mp_input_fd_t key_fds[MP_MAX_KEY_FD];static unsigned int num_key_fd = 0;static mp_input_fd_t cmd_fds[MP_MAX_CMD_FD];static unsigned int num_cmd_fd = 0;static mp_cmd_t* cmd_queue[CMD_QUEUE_SIZE];static unsigned int cmd_queue_length = 0,cmd_queue_start = 0, cmd_queue_end = 0;// this is the key currently downstatic int key_down[MP_MAX_KEY_DOWN];static unsigned int num_key_down = 0, last_key_down = 0;// Autorepeat stuffstatic short ar_state = -1;static mp_cmd_t* ar_cmd = NULL;static unsigned int ar_delay = 100, ar_rate = 8, last_ar = 0;static int use_joystick = 1, use_lirc = 1, use_lircc = 1;static char* config_file = "input.conf";/* Apple Remote */static int use_ar = 1;static char* js_dev = NULL;static char* in_file = NULL;static int in_file_fd = -1;static int mp_input_print_key_list(m_option_t* cfg);static int mp_input_print_cmd_list(m_option_t* cfg);// Our command line optionsstatic m_option_t input_conf[] = { { "conf", &config_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL }, { "ar-delay", &ar_delay, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL }, { "ar-rate", &ar_rate, CONF_TYPE_INT, CONF_GLOBAL, 0, 0, NULL }, { "keylist", mp_input_print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, { "cmdlist", mp_input_print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, { "js-dev", &js_dev, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL }, { "file", &in_file, CONF_TYPE_STRING, CONF_GLOBAL, 0, 0, NULL }, { NULL, NULL, 0, 0, 0, 0, NULL}};static m_option_t mp_input_opts[] = { { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, { "nojoystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL }, { "joystick", &use_joystick, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL }, { "nolirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL }, { "lirc", &use_lirc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL }, { "nolircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL }, { "lircc", &use_lircc, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL }, { "noar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL }, { "ar", &use_ar, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL }, { NULL, NULL, 0, 0, 0, 0, NULL}};static intmp_input_default_cmd_func(int fd,char* buf, int l);static char*mp_input_get_key_name(int key);intmp_input_add_cmd_fd(int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) { #if 0 if(num_cmd_fd == MP_MAX_CMD_FD) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyCmdFds,fd); return 0; } if (select && fd < 0) { mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_cmd_fd", fd); return 0; } memset(&cmd_fds[num_cmd_fd],0,sizeof(mp_input_fd_t)); cmd_fds[num_cmd_fd].fd = fd; cmd_fds[num_cmd_fd].read_func = read_func ? read_func : mp_input_default_cmd_func; cmd_fds[num_cmd_fd].close_func = close_func; cmd_fds[num_cmd_fd].no_select = !select; num_cmd_fd++; #endif return 1;}voidmp_input_rm_cmd_fd(int fd) { #if 0 unsigned int i; for(i = 0; i < num_cmd_fd; i++) { if(cmd_fds[i].fd == fd) break; } if(i == num_cmd_fd) return; if(cmd_fds[i].close_func) cmd_fds[i].close_func(cmd_fds[i].fd); if(cmd_fds[i].buffer) free(cmd_fds[i].buffer); if(i + 1 < num_cmd_fd) memmove(&cmd_fds[i],&cmd_fds[i+1],(num_cmd_fd - i - 1)*sizeof(mp_input_fd_t)); num_cmd_fd--; #endif}voidmp_input_rm_key_fd(int fd) { #if 0 unsigned int i; for(i = 0; i < num_key_fd; i++) { if(key_fds[i].fd == fd) break; } if(i == num_key_fd) return; if(key_fds[i].close_func) key_fds[i].close_func(key_fds[i].fd); if(i + 1 < num_key_fd) memmove(&key_fds[i],&key_fds[i+1],(num_key_fd - i - 1)*sizeof(mp_input_fd_t)); num_key_fd--; #endif}intmp_input_add_key_fd(int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func) { #if 0 if(num_key_fd == MP_MAX_KEY_FD) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd); return 0; } if (select && fd < 0) { mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_key_fd", fd); return 0; } memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t)); key_fds[num_key_fd].fd = fd; key_fds[num_key_fd].read_func = read_func; key_fds[num_key_fd].close_func = close_func; key_fds[num_key_fd].no_select = !select; num_key_fd++; #endif return 1;}intmp_input_add_event_fd(int fd, void (*read_func)(void)){ #if 0 if(num_key_fd == MP_MAX_KEY_FD) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCantRegister2ManyKeyFds,fd); return 0; } if (fd < 0) { mp_msg(MSGT_INPUT, MSGL_ERR, "Invalid fd %i in mp_input_add_event_fd", fd); return 0; } memset(&key_fds[num_key_fd],0,sizeof(mp_input_fd_t)); key_fds[num_key_fd].fd = fd; key_fds[num_key_fd].read_func = read_func; key_fds[num_key_fd].close_func = NULL; key_fds[num_key_fd].no_readfunc_retval = 1; num_key_fd++; #endif return 1;}void mp_input_rm_event_fd(int fd){ //mp_input_rm_key_fd(fd);}mp_cmd_t*mp_input_parse_cmd(char* str) { #if 0 int i,l; int pausing = 0; char *ptr,*e; mp_cmd_t *cmd, *cmd_def;#ifdef MP_DEBUG assert(str != NULL);#endif if (strncmp(str, "pausing ", 8) == 0) { pausing = 1; str = &str[8]; } else if (strncmp(str, "pausing_keep ", 13) == 0) { pausing = 2; str = &str[13]; } else if (strncmp(str, "pausing_toggle ", 15) == 0) { pausing = 3; str = &str[15]; } for(ptr = str ; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' ' ; ptr++) /* NOTHING */; if(ptr[0] != '\0') l = ptr-str; else l = strlen(str); if(l == 0) return NULL; for(i=0; mp_cmds[i].name != NULL; i++) { if(strncasecmp(mp_cmds[i].name,str,l) == 0) break; } if(mp_cmds[i].name == NULL) return NULL; cmd_def = &mp_cmds[i]; cmd = calloc(1, sizeof(mp_cmd_t)); cmd->id = cmd_def->id; cmd->name = strdup(cmd_def->name); cmd->pausing = pausing; ptr = str; for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) { ptr = strchr(ptr,' '); if(!ptr) break; while(ptr[0] == ' ' || ptr[0] == '\t') ptr++; if(ptr[0] == '\0') break; cmd->args[i].type = cmd_def->args[i].type; switch(cmd_def->args[i].type) { case MP_CMD_ARG_INT: errno = 0; cmd->args[i].v.i = atoi(ptr); if(errno != 0) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeInt,cmd_def->name,i+1); ptr = NULL; } break; case MP_CMD_ARG_FLOAT: errno = 0; cmd->args[i].v.f = atof(ptr); if(errno != 0) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrArgMustBeFloat,cmd_def->name,i+1); ptr = NULL; } break; case MP_CMD_ARG_STRING: { char term; char* ptr2 = ptr, *start; if(ptr[0] == '\'' || ptr[0] == '"') { term = ptr[0]; ptr2++; } else term = ' '; start = ptr2; while(1) { e = strchr(ptr2,term); if(!e) break; if(e <= ptr2 || *(e - 1) != '\\') break; ptr2 = e + 1; } if(term != ' ' && (!e || e[0] == '\0')) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnterminatedArg,cmd_def->name,i+1); ptr = NULL; break; } else if(!e) e = ptr+strlen(ptr); l = e-start; ptr2 = start; for(e = strchr(ptr2,'\\') ; e && e<start+l ; e = strchr(ptr2,'\\')) { memmove(e,e+1,strlen(e)); ptr2 = e + 1; l--; } cmd->args[i].v.s = malloc(l+1); strncpy(cmd->args[i].v.s,start,l); cmd->args[i].v.s[l] = '\0'; if(term != ' ') ptr += l+2; } break; case -1: ptr = NULL; break; default : mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrUnknownArg,i); } } cmd->nargs = i; if(cmd_def->nargs > cmd->nargs) {/* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */ mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_Err2FewArgs,cmd_def->name,cmd_def->nargs,cmd->nargs); mp_cmd_free(cmd); return NULL; } for( ; i < MP_CMD_MAX_ARGS && cmd_def->args[i].type != -1 ; i++) { memcpy(&cmd->args[i],&cmd_def->args[i],sizeof(mp_cmd_arg_t)); if(cmd_def->args[i].type == MP_CMD_ARG_STRING && cmd_def->args[i].v.s != NULL) cmd->args[i].v.s = strdup(cmd_def->args[i].v.s); } if(i < MP_CMD_MAX_ARGS) cmd->args[i].type = -1; return cmd; #endif return 0;}#define MP_CMD_MAX_SIZE 256static intmp_input_read_cmd(mp_input_fd_t* mp_fd, char** ret) { char* end; (*ret) = NULL; // Allocate the buffer if it doesn't exist if(!mp_fd->buffer) { mp_fd->buffer = malloc(MP_CMD_MAX_SIZE); mp_fd->pos = 0; mp_fd->size = MP_CMD_MAX_SIZE; } // Get some data if needed/possible while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1) ) { 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); // Error ? if(r < 0) { switch(r) { case MP_INPUT_ERROR: case MP_INPUT_DEAD: mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrReadingCmdFd,mp_fd->fd,strerror(errno)); case MP_INPUT_NOTHING: return r; case MP_INPUT_RETRY: continue; } // EOF ? } else if(r == 0) { mp_fd->eof = 1; break; } mp_fd->pos += r; break; } mp_fd->got_cmd = 0; while(1) { int l = 0; // Find the cmd end mp_fd->buffer[mp_fd->pos] = '\0'; end = strchr(mp_fd->buffer,'\n'); // No cmd end ? if(!end) { // If buffer is full we must drop all until the next \n if(mp_fd->size - mp_fd->pos <= 1) { mp_msg(MSGT_INPUT,MSGL_ERR,MSGTR_INPUT_INPUT_ErrCmdBufferFullDroppingContent,mp_fd->fd); mp_fd->pos = 0; mp_fd->drop = 1; } break; } // We already have a cmd : set the got_cmd flag else if((*ret)) { mp_fd->got_cmd = 1; break; } l = end - mp_fd->buffer; // Not dropping : put the cmd in ret if (!mp_fd->drop) { (*ret) = malloc(l+1); strncpy((*ret),mp_fd->buffer,l); (*ret)[l] = '\0'; } else { // Remove the dropping flag mp_fd->drop = 0; } if( mp_fd->pos - (l+1) > 0) memmove(mp_fd->buffer,end+1,mp_fd->pos-(l+1)); mp_fd->pos -= l+1; } if(*ret) return 1; else return MP_INPUT_NOTHING;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?