From 3832d67afd7ce1feefd86a345af2af6d708efc38 Mon Sep 17 00:00:00 2001 From: Uoti Urpala Date: Mon, 25 Apr 2011 11:38:46 +0300 Subject: cleanup: reformat input.c --- input/input.c | 1878 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 957 insertions(+), 921 deletions(-) (limited to 'input') diff --git a/input/input.c b/input/input.c index f5e2d3824d..225c4eee0d 100644 --- a/input/input.c +++ b/input/input.c @@ -59,13 +59,13 @@ #include "ar.h" typedef struct mp_cmd_bind { - int input[MP_MAX_KEY_DOWN+1]; - char* cmd; + int input[MP_MAX_KEY_DOWN + 1]; + char *cmd; } mp_cmd_bind_t; typedef struct mp_key_name { - int key; - char* name; + int key; + char *name; } mp_key_name_t; /// This array defines all known commands. @@ -532,37 +532,37 @@ static const mp_cmd_bind_t def_cmd_binds[] = { #define CMD_QUEUE_SIZE 100 typedef struct mp_input_fd { - int fd; - union { - mp_key_func_t key; - mp_cmd_func_t cmd; - } read_func; - mp_close_func_t close_func; - void *ctx; - unsigned eof : 1; - unsigned drop : 1; - unsigned dead : 1; - unsigned got_cmd : 1; - unsigned no_select : 1; - // These fields are for the cmd fds. - char* buffer; - int pos,size; + int fd; + union { + mp_key_func_t key; + mp_cmd_func_t cmd; + } read_func; + mp_close_func_t close_func; + void *ctx; + unsigned eof : 1; + unsigned drop : 1; + unsigned dead : 1; + unsigned got_cmd : 1; + unsigned no_select : 1; + // These fields are for the cmd fds. + char *buffer; + int pos, size; } mp_input_fd_t; typedef struct mp_cmd_filter mp_cmd_filter_t; struct mp_cmd_filter { - mp_input_cmd_filter filter; - void* ctx; - mp_cmd_filter_t* next; + mp_input_cmd_filter filter; + void *ctx; + mp_cmd_filter_t *next; }; typedef struct mp_cmd_bind_section mp_cmd_bind_section_t; struct mp_cmd_bind_section { - mp_cmd_bind_t* cmd_binds; - char* section; - mp_cmd_bind_section_t* next; + mp_cmd_bind_t *cmd_binds; + char *section; + mp_cmd_bind_section_t *next; }; struct input_ctx { @@ -599,40 +599,40 @@ struct input_ctx { }; -static mp_cmd_filter_t* cmd_filters = NULL; +static mp_cmd_filter_t *cmd_filters = NULL; // Callback to allow the menu filter to grab the incoming keys int (*mp_input_key_cb)(int code) = NULL; int async_quit_request; -static int print_key_list(m_option_t* cfg); -static int print_cmd_list(m_option_t* cfg); +static int print_key_list(m_option_t *cfg); +static int print_cmd_list(m_option_t *cfg); // Our command line options static const m_option_t input_conf[] = { OPT_STRING("conf", input.config_file, CONF_GLOBAL), - OPT_INT("ar-delay",input.ar_delay, CONF_GLOBAL), + OPT_INT("ar-delay", input.ar_delay, CONF_GLOBAL), OPT_INT("ar-rate", input.ar_rate, CONF_GLOBAL), - { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, - { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, + { "keylist", print_key_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, + { "cmdlist", print_cmd_list, CONF_TYPE_FUNC, CONF_GLOBAL, 0, 0, NULL }, OPT_STRING("js-dev", input.js_dev, CONF_GLOBAL), OPT_STRING("ar-dev", input.ar_dev, CONF_GLOBAL), OPT_STRING("file", input.in_file, CONF_GLOBAL), OPT_MAKE_FLAGS("default-bindings", input.default_bindings, CONF_GLOBAL), - { NULL, NULL, 0, 0, 0, 0, NULL} + { NULL, NULL, 0, 0, 0, 0, NULL} }; static const m_option_t mp_input_opts[] = { - { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, + { "input", &input_conf, CONF_TYPE_SUBCONFIG, 0, 0, 0, NULL}, OPT_MAKE_FLAGS("joystick", input.use_joystick, CONF_GLOBAL), OPT_MAKE_FLAGS("lirc", input.use_lirc, CONF_GLOBAL), OPT_MAKE_FLAGS("lircc", input.use_lircc, CONF_GLOBAL), OPT_MAKE_FLAGS("ar", input.use_ar, CONF_GLOBAL), - { NULL, NULL, 0, 0, 0, 0, NULL} + { NULL, NULL, 0, 0, 0, 0, NULL} }; -static int default_cmd_func(int fd,char* buf, int l); +static int default_cmd_func(int fd, char *buf, int l); static char *get_key_name(int key) { @@ -659,90 +659,94 @@ static char *get_key_name(int key) int mp_input_add_cmd_fd(struct input_ctx *ictx, int fd, int select, mp_cmd_func_t read_func, mp_close_func_t close_func) { - if (ictx->num_cmd_fd == MP_MAX_CMD_FD) { - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many command file descriptors, cannot register file descriptor %d.\n",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; - } + if (ictx->num_cmd_fd == MP_MAX_CMD_FD) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many command file descriptors, " + "cannot register file descriptor %d.\n", fd); + return 0; + } + if (select && fd < 0) { + mp_msg(MSGT_INPUT, MSGL_ERR, + "Invalid fd %d in mp_input_add_cmd_fd", fd); + return 0; + } - ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){ - .fd = fd, - .read_func.cmd = read_func ? read_func : default_cmd_func, - .close_func = close_func, - .no_select = !select - }; - ictx->num_cmd_fd++; + ictx->cmd_fds[ictx->num_cmd_fd] = (struct mp_input_fd){ + .fd = fd, + .read_func.cmd = read_func ? read_func : default_cmd_func, + .close_func = close_func, + .no_select = !select + }; + ictx->num_cmd_fd++; - return 1; + return 1; } void mp_input_rm_cmd_fd(struct input_ctx *ictx, int fd) { struct mp_input_fd *cmd_fds = ictx->cmd_fds; - unsigned int i; - - for (i = 0; i < ictx->num_cmd_fd; i++) { - if(cmd_fds[i].fd == fd) - break; - } - if (i == ictx->num_cmd_fd) - return; - if(cmd_fds[i].close_func) - cmd_fds[i].close_func(cmd_fds[i].fd); - talloc_free(cmd_fds[i].buffer); - - if (i + 1 < ictx->num_cmd_fd) - memmove(&cmd_fds[i], &cmd_fds[i+1], - (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t)); - ictx->num_cmd_fd--; + unsigned int i; + + for (i = 0; i < ictx->num_cmd_fd; i++) { + if (cmd_fds[i].fd == fd) + break; + } + if (i == ictx->num_cmd_fd) + return; + if (cmd_fds[i].close_func) + cmd_fds[i].close_func(cmd_fds[i].fd); + talloc_free(cmd_fds[i].buffer); + + if (i + 1 < ictx->num_cmd_fd) + memmove(&cmd_fds[i], &cmd_fds[i + 1], + (ictx->num_cmd_fd - i - 1) * sizeof(mp_input_fd_t)); + ictx->num_cmd_fd--; } void mp_input_rm_key_fd(struct input_ctx *ictx, int fd) { struct mp_input_fd *key_fds = ictx->key_fds; - unsigned int i; - - for (i = 0; i < ictx->num_key_fd; i++) { - if(key_fds[i].fd == fd) - break; - } - if (i == ictx->num_key_fd) - return; - if(key_fds[i].close_func) - key_fds[i].close_func(key_fds[i].fd); - - if(i + 1 < ictx->num_key_fd) - memmove(&key_fds[i], &key_fds[i+1], - (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t)); - ictx->num_key_fd--; + unsigned int i; + + for (i = 0; i < ictx->num_key_fd; i++) { + if (key_fds[i].fd == fd) + break; + } + if (i == ictx->num_key_fd) + return; + if (key_fds[i].close_func) + key_fds[i].close_func(key_fds[i].fd); + + if (i + 1 < ictx->num_key_fd) + memmove(&key_fds[i], &key_fds[i + 1], + (ictx->num_key_fd - i - 1) * sizeof(mp_input_fd_t)); + ictx->num_key_fd--; } int mp_input_add_key_fd(struct input_ctx *ictx, int fd, int select, mp_key_func_t read_func, mp_close_func_t close_func, void *ctx) { - if (ictx->num_key_fd == MP_MAX_KEY_FD) { - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key file descriptors, cannot register file descriptor %d.\n",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; - } - - ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){ - .fd = fd, - .read_func.key = read_func, - .close_func = close_func, - .no_select = !select, - .ctx = ctx, - }; - ictx->num_key_fd++; - - return 1; + if (ictx->num_key_fd == MP_MAX_KEY_FD) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key file descriptors, " + "cannot register file descriptor %d.\n", fd); + return 0; + } + if (select && fd < 0) { + mp_msg(MSGT_INPUT, MSGL_ERR, + "Invalid fd %d in mp_input_add_key_fd", fd); + return 0; + } + + ictx->key_fds[ictx->num_key_fd] = (struct mp_input_fd){ + .fd = fd, + .read_func.key = read_func, + .close_func = close_func, + .no_select = !select, + .ctx = ctx, + }; + ictx->num_key_fd++; + + return 1; } int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str) @@ -754,8 +758,8 @@ int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str) while (*str) { mp_cmd_t *cmd; size_t len = strcspn(str, "\r\n"); - char *cmdbuf = talloc_size(NULL, len+1); - av_strlcpy(cmdbuf, str, len+1); + char *cmdbuf = talloc_size(NULL, len + 1); + av_strlcpy(cmdbuf, str, len + 1); cmd = mp_input_parse_cmd(cmdbuf); if (cmd) { mp_input_queue_cmd(ictx, cmd); @@ -769,351 +773,375 @@ int mp_input_parse_and_queue_cmds(struct input_ctx *ictx, const char *str) return cmd_num; } -mp_cmd_t* -mp_input_parse_cmd(char* str) { - int i,l; - int pausing = 0; - char *ptr,*e; - const mp_cmd_t *cmd_def; +mp_cmd_t *mp_input_parse_cmd(char *str) +{ + int i, l; + int pausing = 0; + char *ptr, *e; + const mp_cmd_t *cmd_def; #ifdef MP_DEBUG - assert(str != NULL); + assert(str != NULL); #endif - // Ignore heading spaces. - while (str[0] == ' ' || str[0] == '\t') - ++str; - - 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]; - } else if (strncmp(str, "pausing_keep_force ", 19) == 0) { - pausing = 4; - str = &str[19]; - } - - 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; + // Ignore heading spaces. + while (str[0] == ' ' || str[0] == '\t') + ++str; - for(i=0; mp_cmds[i].name != NULL; i++) { - if(strncasecmp(mp_cmds[i].name,str,l) == 0) - break; - } + 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]; + } else if (strncmp(str, "pausing_keep_force ", 19) == 0) { + pausing = 4; + str = &str[19]; + } - if(mp_cmds[i].name == NULL) - return NULL; + for (ptr = str; ptr[0] != '\0' && ptr[0] != '\t' && ptr[0] != ' '; ptr++) + /* NOTHING */; + if (ptr[0] != '\0') + l = ptr - str; + else + l = strlen(str); - cmd_def = &mp_cmds[i]; - - mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd); - *cmd = (mp_cmd_t){ - .id = cmd_def->id, - .name = talloc_strdup(cmd, cmd_def->name), - .pausing = pausing, - }; - - ptr = str; - - for(i=0; ptr && i < MP_CMD_MAX_ARGS; i++) { - while(ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') ptr++; - if(ptr[0] == '\0') break; - while(ptr[0] == ' ' || ptr[0] == '\t') ptr++; - if(ptr[0] == '\0' || ptr[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_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't an integer.\n",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_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d isn't a float.\n",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_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s: argument %d is unterminated.\n",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 && eargs[i].v.s = talloc_strndup(cmd, start, l); - if(term != ' ') ptr += l+2; - } break; - case -1: - ptr = NULL; - break; - default : - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown argument %d\n",i); + if (l == 0) + return NULL; + + for (i = 0; mp_cmds[i].name != NULL; i++) { + if (strncasecmp(mp_cmds[i].name, str, l) == 0) + break; } - } - cmd->nargs = i; - if(cmd_def->nargs > cmd->nargs) { -/* mp_msg(MSGT_INPUT,MSGL_ERR,"Got command '%s' but\n",str); */ - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Command %s requires at least %d arguments, we found only %d so far.\n",cmd_def->name,cmd_def->nargs,cmd->nargs); - mp_cmd_free(cmd); - return NULL; - } + if (mp_cmds[i].name == NULL) + 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 = talloc_strdup(cmd, cmd_def->args[i].v.s); - } + cmd_def = &mp_cmds[i]; + + mp_cmd_t *cmd = talloc_ptrtype(NULL, cmd); + *cmd = (mp_cmd_t){ + .id = cmd_def->id, + .name = talloc_strdup(cmd, cmd_def->name), + .pausing = pausing, + }; - if(i < MP_CMD_MAX_ARGS) - cmd->args[i].type = -1; + ptr = str; + + for (i = 0; ptr && i < MP_CMD_MAX_ARGS; i++) { + while (ptr[0] != ' ' && ptr[0] != '\t' && ptr[0] != '\0') + ptr++; + if (ptr[0] == '\0') + break; + while (ptr[0] == ' ' || ptr[0] == '\t') + ptr++; + if (ptr[0] == '\0' || ptr[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_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d " + "isn't an integer.\n", 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_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d " + "isn't a float.\n", 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; + } - return cmd; + if (term != ' ' && (!e || e[0] == '\0')) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s: argument %d is " + "unterminated.\n", 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 = talloc_strndup(cmd, start, l); + if (term != ' ') + ptr += l + 2; + break; + } + case -1: + ptr = NULL; + break; + default: + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown argument %d\n", i); + } + } + cmd->nargs = i; + + if (cmd_def->nargs > cmd->nargs) { +/* mp_msg(MSGT_INPUT, MSGL_ERR, "Got command '%s' but\n", str); */ + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Command %s requires at least %d " + "arguments, we found only %d so far.\n", 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 = talloc_strdup(cmd, cmd_def->args[i].v.s); + } + + if (i < MP_CMD_MAX_ARGS) + cmd->args[i].type = -1; + + return cmd; } #define MP_CMD_MAX_SIZE 4096 -static int read_cmd(mp_input_fd_t* mp_fd, char** ret) +static int 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 = talloc_size(NULL, 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_fd->read_func.cmd(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_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading command file descriptor %d: %s\n",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,'\r'); - if (end) *end = '\n'; - 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_tmsg(MSGT_INPUT,MSGL_ERR,"Command buffer of file descriptor %d is full: dropping content.\n",mp_fd->fd); - mp_fd->pos = 0; - mp_fd->drop = 1; - } - break; + char *end; + *ret = NULL; + + // Allocate the buffer if it doesn't exist + if (!mp_fd->buffer) { + mp_fd->buffer = talloc_size(NULL, MP_CMD_MAX_SIZE); + mp_fd->pos = 0; + mp_fd->size = MP_CMD_MAX_SIZE; } - // We already have a cmd : set the got_cmd flag - else if((*ret)) { - mp_fd->got_cmd = 1; - break; + + // Get some data if needed/possible + while (!mp_fd->got_cmd && !mp_fd->eof && (mp_fd->size - mp_fd->pos > 1)) { + int r = mp_fd->read_func.cmd(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_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading " + "command file descriptor %d: %s\n", + 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; } - l = end - mp_fd->buffer; + 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, '\r'); + if (end) + *end = '\n'; + 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_tmsg(MSGT_INPUT, MSGL_ERR, "Command buffer of file " + "descriptor %d is full: dropping content.\n", + 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; + } - // Not dropping : put the cmd in ret - if (!mp_fd->drop) - *ret = talloc_strndup(NULL, mp_fd->buffer, l); - else - mp_fd->drop = 0; - mp_fd->pos -= l+1; - memmove(mp_fd->buffer, end+1, mp_fd->pos); - } + l = end - mp_fd->buffer; - if(*ret) - return 1; - else - return MP_INPUT_NOTHING; + // Not dropping : put the cmd in ret + if (!mp_fd->drop) + *ret = talloc_strndup(NULL, mp_fd->buffer, l); + else + mp_fd->drop = 0; + mp_fd->pos -= l + 1; + memmove(mp_fd->buffer, end + 1, mp_fd->pos); + } + + if (*ret) + return 1; + else + return MP_INPUT_NOTHING; } -static int default_cmd_func(int fd,char* buf, int l) +static int default_cmd_func(int fd, char *buf, int l) { - while(1) { - int r = read(fd,buf,l); - // Error ? - if(r < 0) { - if(errno == EINTR) - continue; - else if(errno == EAGAIN) - return MP_INPUT_NOTHING; - return MP_INPUT_ERROR; - // EOF ? + while (1) { + int r = read(fd, buf, l); + // Error ? + if (r < 0) { + if (errno == EINTR) + continue; + else if (errno == EAGAIN) + return MP_INPUT_NOTHING; + return MP_INPUT_ERROR; + // EOF ? + } + return r; } - return r; - } - } -void -mp_input_add_cmd_filter(mp_input_cmd_filter func, void* ctx) { +void mp_input_add_cmd_filter(mp_input_cmd_filter func, void *ctx) +{ mp_cmd_filter_t *filter = talloc_ptrtype(NULL, filter); - filter->filter = func; - filter->ctx = ctx; - filter->next = cmd_filters; - cmd_filters = filter; + filter->filter = func; + filter->ctx = ctx; + filter->next = cmd_filters; + cmd_filters = filter; } -static char *find_bind_for_key(const mp_cmd_bind_t* binds, int n,int* keys) +static char *find_bind_for_key(const mp_cmd_bind_t *binds, int n, int *keys) { - int j; - - if (n <= 0) return NULL; - for(j = 0; binds[j].cmd != NULL; j++) { - int found = 1,s; - for(s = 0; s < n && binds[j].input[s] != 0; s++) { - if(binds[j].input[s] != keys[s]) { - found = 0; - break; - } - } - if(found && binds[j].input[s] == 0 && s == n) - break; - } - return binds[j].cmd; + int j; + + if (n <= 0) + return NULL; + for (j = 0; binds[j].cmd != NULL; j++) { + int found = 1, s; + for (s = 0; s < n && binds[j].input[s] != 0; s++) { + if (binds[j].input[s] != keys[s]) { + found = 0; + break; + } + } + if (found && binds[j].input[s] == 0 && s == n) + break; + } + return binds[j].cmd; } static mp_cmd_bind_section_t *get_bind_section(struct input_ctx *ictx, char *section) { - mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections; - - if (section==NULL) section="default"; - while (bind_section) { - if(strcmp(section,bind_section->section)==0) return bind_section; - if(bind_section->next==NULL) break; - bind_section=bind_section->next; - } - if(bind_section) { - bind_section->next = talloc_ptrtype(ictx, bind_section->next); - bind_section=bind_section->next; - } else { - ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections); - bind_section = ictx->cmd_bind_sections; - } - bind_section->cmd_binds=NULL; - bind_section->section = talloc_strdup(bind_section, section); - bind_section->next=NULL; - return bind_section; + mp_cmd_bind_section_t *bind_section = ictx->cmd_bind_sections; + + if (section == NULL) + section = "default"; + while (bind_section) { + if (strcmp(section, bind_section->section) == 0) + return bind_section; + if (bind_section->next == NULL) + break; + bind_section = bind_section->next; + } + if (bind_section) { + bind_section->next = talloc_ptrtype(ictx, bind_section->next); + bind_section = bind_section->next; + } else { + ictx->cmd_bind_sections = talloc_ptrtype(ictx, ictx->cmd_bind_sections); + bind_section = ictx->cmd_bind_sections; + } + bind_section->cmd_binds = NULL; + bind_section->section = talloc_strdup(bind_section, section); + bind_section->next = NULL; + return bind_section; } static mp_cmd_t *get_cmd_from_keys(struct input_ctx *ictx, int n, int *keys) { - char* cmd = NULL; - mp_cmd_t* ret; - - if (ictx->cmd_binds) - cmd = find_bind_for_key(ictx->cmd_binds, n, keys); - if (ictx->cmd_binds_default && cmd == NULL) - cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys); - if (ictx->default_bindings && cmd == NULL) - cmd = find_bind_for_key(def_cmd_binds,n,keys); - - if(cmd == NULL) { - char *key_buf = get_key_name(keys[0]); - mp_tmsg(MSGT_INPUT,MSGL_WARN,"No bind found for key '%s'.", key_buf); - talloc_free(key_buf); - if(n > 1) { - int s; - for(s=1; s < n; s++) { - key_buf = get_key_name(keys[s]); - mp_msg(MSGT_INPUT,MSGL_WARN,"-%s", key_buf); - talloc_free(key_buf); - } + char *cmd = NULL; + mp_cmd_t *ret; + + if (ictx->cmd_binds) + cmd = find_bind_for_key(ictx->cmd_binds, n, keys); + if (ictx->cmd_binds_default && cmd == NULL) + cmd = find_bind_for_key(ictx->cmd_binds_default, n, keys); + if (ictx->default_bindings && cmd == NULL) + cmd = find_bind_for_key(def_cmd_binds, n, keys); + + if (cmd == NULL) { + char *key_buf = get_key_name(keys[0]); + mp_tmsg(MSGT_INPUT, MSGL_WARN, "No bind found for key '%s'.", key_buf); + talloc_free(key_buf); + if (n > 1) { + for (int s = 1; s < n; s++) { + key_buf = get_key_name(keys[s]); + mp_msg(MSGT_INPUT, MSGL_WARN, "-%s", key_buf); + talloc_free(key_buf); + } + } + mp_msg(MSGT_INPUT, MSGL_WARN, " \n"); + return NULL; } - mp_msg(MSGT_INPUT,MSGL_WARN," \n"); - return NULL; - } - if (strcmp(cmd, "ignore") == 0) return NULL; - ret = mp_input_parse_cmd(cmd); - if(!ret) { - char *key_buf = get_key_name(ictx->key_down[0]); - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Invalid command for bound key %s", key_buf); - talloc_free(key_buf); - if (ictx->num_key_down > 1) { - unsigned int s; - for(s=1; s < ictx->num_key_down; s++) { - char *key_buf = get_key_name(ictx->key_down[s]); - mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf); - talloc_free(key_buf); - } + if (strcmp(cmd, "ignore") == 0) + return NULL; + ret = mp_input_parse_cmd(cmd); + if (!ret) { + char *key_buf = get_key_name(ictx->key_down[0]); + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Invalid command for bound key %s", + key_buf); + talloc_free(key_buf); + if (ictx->num_key_down > 1) { + unsigned int s; + for (s = 1; s < ictx->num_key_down; s++) { + char *key_buf = get_key_name(ictx->key_down[s]); + mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf); + talloc_free(key_buf); + } + } + mp_msg(MSGT_INPUT, MSGL_ERR, " : %s \n", cmd); } - mp_msg(MSGT_INPUT,MSGL_ERR," : %s \n",cmd); - } - return ret; + return ret; } -static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code) +static mp_cmd_t *interpret_key(struct input_ctx *ictx, int code) { - unsigned int j; - mp_cmd_t* ret; + unsigned int j; + mp_cmd_t *ret; /* On normal keyboards shift changes the character code of non-special * keys, so don't count the modifier separately for those. In other words @@ -1124,57 +1152,57 @@ static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code) if (unmod < 256 && unmod != KEY_ENTER && unmod != KEY_TAB) code &= ~KEY_MODIFIER_SHIFT; - if(mp_input_key_cb) { - if (code & MP_KEY_DOWN) - return NULL; - code &= ~(MP_KEY_DOWN|MP_NO_REPEAT_KEY); - if (mp_input_key_cb(code)) - return NULL; - } - - if(code & MP_KEY_DOWN) { - if (ictx->num_key_down > MP_MAX_KEY_DOWN) { - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n"); - return NULL; - } - code &= ~MP_KEY_DOWN; - // Check if we don't already have this key as pushed - for (j = 0; j < ictx->num_key_down; j++) { - if (ictx->key_down[j] == code) - break; - } - if (j != ictx->num_key_down) - return NULL; - ictx->key_down[ictx->num_key_down] = code; - ictx->num_key_down++; - ictx->last_key_down = GetTimer(); - ictx->ar_state = 0; - return NULL; + if (mp_input_key_cb) { + if (code & MP_KEY_DOWN) + return NULL; + code &= ~(MP_KEY_DOWN | MP_NO_REPEAT_KEY); + if (mp_input_key_cb(code)) + return NULL; } - // key released - // Check if the key is in the down key, driver which can't send push event - // send only release event + + if (code & MP_KEY_DOWN) { + if (ictx->num_key_down > MP_MAX_KEY_DOWN) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events " + "at the same time\n"); + return NULL; + } + code &= ~MP_KEY_DOWN; + // Check if we don't already have this key as pushed + for (j = 0; j < ictx->num_key_down; j++) { + if (ictx->key_down[j] == code) + break; + } + if (j != ictx->num_key_down) + return NULL; + ictx->key_down[ictx->num_key_down] = code; + ictx->num_key_down++; + ictx->last_key_down = GetTimer(); + ictx->ar_state = 0; + return NULL; + } + // button released or press of key with no separate down/up events for (j = 0; j < ictx->num_key_down; j++) { - if (ictx->key_down[j] == code) - break; + if (ictx->key_down[j] == code) + break; } - if (j == ictx->num_key_down) { // key was not in the down keys : add it - if (ictx->num_key_down > MP_MAX_KEY_DOWN) { - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Too many key down events at the same time\n"); - return NULL; - } - ictx->key_down[ictx->num_key_down] = code; - ictx->num_key_down++; - ictx->last_key_down = 1; + if (j == ictx->num_key_down) { // was not already down; add temporarily + if (ictx->num_key_down > MP_MAX_KEY_DOWN) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Too many key down events " + "at the same time\n"); + return NULL; + } + ictx->key_down[ictx->num_key_down] = code; + ictx->num_key_down++; + ictx->last_key_down = 1; } - // We ignore key from last combination + // Interpret only maximal point of multibutton event ret = ictx->last_key_down ? - get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down) - : NULL; + get_cmd_from_keys(ictx, ictx->num_key_down, ictx->key_down) + : NULL; // Remove the key - if (j+1 < ictx->num_key_down) - memmove(&ictx->key_down[j], &ictx->key_down[j+1], - (ictx->num_key_down-(j+1))*sizeof(int)); + if (j + 1 < ictx->num_key_down) + memmove(&ictx->key_down[j], &ictx->key_down[j + 1], + (ictx->num_key_down - (j + 1)) * sizeof(int)); ictx->num_key_down--; ictx->last_key_down = 0; ictx->ar_state = -1; @@ -1185,30 +1213,30 @@ static mp_cmd_t* interpret_key(struct input_ctx *ictx, int code) static mp_cmd_t *check_autorepeat(struct input_ctx *ictx) { - // No input : autorepeat ? - if (ictx->ar_rate > 0 && ictx->ar_state >=0 && ictx->num_key_down > 0 - && !(ictx->key_down[ictx->num_key_down-1] & MP_NO_REPEAT_KEY)) { - unsigned int t = GetTimer(); - // First time : wait delay - if (ictx->ar_state == 0 - && (t - ictx->last_key_down) >= ictx->ar_delay*1000) { - ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down, - ictx->key_down); - if (!ictx->ar_cmd) { - ictx->ar_state = -1; - return NULL; - } - ictx->ar_state = 1; - ictx->last_ar = t; - return mp_cmd_clone(ictx->ar_cmd); - // Then send rate / sec event - } else if (ictx->ar_state == 1 - && (t -ictx->last_ar) >= 1000000 / ictx->ar_rate) { - ictx->last_ar = t; - return mp_cmd_clone(ictx->ar_cmd); + // No input : autorepeat ? + if (ictx->ar_rate > 0 && ictx->ar_state >= 0 && ictx->num_key_down > 0 + && !(ictx->key_down[ictx->num_key_down - 1] & MP_NO_REPEAT_KEY)) { + unsigned int t = GetTimer(); + // First time : wait delay + if (ictx->ar_state == 0 + && (t - ictx->last_key_down) >= ictx->ar_delay * 1000) { + ictx->ar_cmd = get_cmd_from_keys(ictx, ictx->num_key_down, + ictx->key_down); + if (!ictx->ar_cmd) { + ictx->ar_state = -1; + return NULL; + } + ictx->ar_state = 1; + ictx->last_ar = t; + return mp_cmd_clone(ictx->ar_cmd); + // Then send rate / sec event + } else if (ictx->ar_state == 1 + && (t - ictx->last_ar) >= 1000000 / ictx->ar_rate) { + ictx->last_ar = t; + return mp_cmd_clone(ictx->ar_cmd); + } } - } - return NULL; + return NULL; } @@ -1222,36 +1250,35 @@ static mp_cmd_t *read_events(struct input_ctx *ictx, int time) struct mp_input_fd *key_fds = ictx->key_fds; struct mp_input_fd *cmd_fds = ictx->cmd_fds; for (i = 0; i < ictx->num_key_fd; i++) - if (key_fds[i].dead) { - mp_input_rm_key_fd(ictx, key_fds[i].fd); - i--; - } + if (key_fds[i].dead) { + mp_input_rm_key_fd(ictx, key_fds[i].fd); + i--; + } for (i = 0; i < ictx->num_cmd_fd; i++) - if (cmd_fds[i].dead || cmd_fds[i].eof) { - mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd); - i--; - } - else if (cmd_fds[i].got_cmd) - got_cmd = 1; + if (cmd_fds[i].dead || cmd_fds[i].eof) { + mp_input_rm_cmd_fd(ictx, cmd_fds[i].fd); + i--; + } else if (cmd_fds[i].got_cmd) + got_cmd = 1; #ifdef HAVE_POSIX_SELECT fd_set fds; FD_ZERO(&fds); if (!got_cmd) { - int max_fd = 0; - for (i = 0; i < ictx->num_key_fd; i++) { - if (key_fds[i].no_select) - continue; - if (key_fds[i].fd > max_fd) - max_fd = key_fds[i].fd; - FD_SET(key_fds[i].fd, &fds); - } - for (i = 0; i < ictx->num_cmd_fd; i++) { - if (cmd_fds[i].no_select) - continue; - if (cmd_fds[i].fd > max_fd) - max_fd = cmd_fds[i].fd; - FD_SET(cmd_fds[i].fd, &fds); - } + int max_fd = 0; + for (i = 0; i < ictx->num_key_fd; i++) { + if (key_fds[i].no_select) + continue; + if (key_fds[i].fd > max_fd) + max_fd = key_fds[i].fd; + FD_SET(key_fds[i].fd, &fds); + } + for (i = 0; i < ictx->num_cmd_fd; i++) { + if (cmd_fds[i].no_select) + continue; + if (cmd_fds[i].fd > max_fd) + max_fd = cmd_fds[i].fd; + FD_SET(cmd_fds[i].fd, &fds); + } struct timeval tv, *time_val; if (time >= 0) { tv.tv_sec = time / 1000; @@ -1268,86 +1295,86 @@ static mp_cmd_t *read_events(struct input_ctx *ictx, int time) } #else if (!got_cmd && time) - usec_sleep(time * 1000); + usec_sleep(time * 1000); #endif for (i = 0; i < ictx->num_key_fd; i++) { #ifdef HAVE_POSIX_SELECT - if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds)) - continue; + if (!key_fds[i].no_select && !FD_ISSET(key_fds[i].fd, &fds)) + continue; #endif int code; while ((code = key_fds[i].read_func.key(key_fds[i].ctx, key_fds[i].fd)) >= 0) { - mp_cmd_t *ret = interpret_key(ictx, code); - if (ret) - return ret; - } + mp_cmd_t *ret = interpret_key(ictx, code); + if (ret) + return ret; + } if (code == MP_INPUT_ERROR) - mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input file descriptor %d\n", - key_fds[i].fd); - else if (code == MP_INPUT_DEAD) { - mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on file descriptor %d\n", - key_fds[i].fd); - key_fds[i].dead = 1; - } + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on key input " + "file descriptor %d\n", key_fds[i].fd); + else if (code == MP_INPUT_DEAD) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Dead key input on " + "file descriptor %d\n", key_fds[i].fd); + key_fds[i].dead = 1; + } } mp_cmd_t *autorepeat_cmd = check_autorepeat(ictx); if (autorepeat_cmd) - return autorepeat_cmd; + return autorepeat_cmd; for (i = 0; i < ictx->num_cmd_fd; i++) { #ifdef HAVE_POSIX_SELECT - if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) && - !cmd_fds[i].got_cmd) - continue; + if (!cmd_fds[i].no_select && !FD_ISSET(cmd_fds[i].fd, &fds) && + !cmd_fds[i].got_cmd) + continue; #endif - char *cmd; + char *cmd; int r; while ((r = read_cmd(&cmd_fds[i], &cmd)) >= 0) { - mp_cmd_t *ret = mp_input_parse_cmd(cmd); - talloc_free(cmd); - if (ret) - return ret; - } + mp_cmd_t *ret = mp_input_parse_cmd(cmd); + talloc_free(cmd); + if (ret) + return ret; + } if (r == MP_INPUT_ERROR) - mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command file descriptor %d\n", - cmd_fds[i].fd); - else if (r == MP_INPUT_DEAD) - cmd_fds[i].dead = 1; + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Error on command " + "file descriptor %d\n", cmd_fds[i].fd); + else if (r == MP_INPUT_DEAD) + cmd_fds[i].dead = 1; } return NULL; } -int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t* cmd) +int mp_input_queue_cmd(struct input_ctx *ictx, mp_cmd_t *cmd) { - if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE) - return 0; - ictx->cmd_queue[ictx->cmd_queue_end] = cmd; - ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE; - ictx->cmd_queue_length++; - return 1; + if (!cmd || ictx->cmd_queue_length >= CMD_QUEUE_SIZE) + return 0; + ictx->cmd_queue[ictx->cmd_queue_end] = cmd; + ictx->cmd_queue_end = (ictx->cmd_queue_end + 1) % CMD_QUEUE_SIZE; + ictx->cmd_queue_length++; + return 1; } static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only) { - mp_cmd_t* ret; + mp_cmd_t *ret; - if (ictx->cmd_queue_length == 0) - return NULL; + if (ictx->cmd_queue_length == 0) + return NULL; - ret = ictx->cmd_queue[ictx->cmd_queue_start]; + ret = ictx->cmd_queue[ictx->cmd_queue_start]; - if (!peek_only) { - ictx->cmd_queue_length--; - ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE; - } + if (!peek_only) { + ictx->cmd_queue_length--; + ictx->cmd_queue_start = (ictx->cmd_queue_start + 1) % CMD_QUEUE_SIZE; + } - return ret; + return ret; } /** @@ -1356,63 +1383,65 @@ static mp_cmd_t *get_queued_cmd(struct input_ctx *ictx, int peek_only) */ mp_cmd_t *mp_input_get_cmd(struct input_ctx *ictx, int time, int peek_only) { - mp_cmd_t* ret = NULL; - mp_cmd_filter_t* cf; - int from_queue; - - if (async_quit_request) - return mp_input_parse_cmd("quit 1"); - while(1) { - from_queue = 1; - ret = get_queued_cmd(ictx, peek_only); - if(ret) break; - from_queue = 0; - ret = read_events(ictx, time); - if (!ret) { - from_queue = 1; - ret = get_queued_cmd(ictx, peek_only); + mp_cmd_t *ret = NULL; + mp_cmd_filter_t *cf; + int from_queue; + + if (async_quit_request) + return mp_input_parse_cmd("quit 1"); + while (1) { + from_queue = 1; + ret = get_queued_cmd(ictx, peek_only); + if (ret) + break; + from_queue = 0; + ret = read_events(ictx, time); + if (!ret) { + from_queue = 1; + ret = get_queued_cmd(ictx, peek_only); + } + break; } - break; - } - if(!ret) return NULL; - - for(cf = cmd_filters ; cf ; cf = cf->next) { - if (cf->filter(ret, cf->ctx)) { - if (peek_only && from_queue) - // The filter ate the cmd, so we remove it from queue - ret = get_queued_cmd(ictx, 0); - mp_cmd_free(ret); - return NULL; + if (!ret) + return NULL; + + for (cf = cmd_filters; cf; cf = cf->next) { + if (cf->filter(ret, cf->ctx)) { + if (peek_only && from_queue) + // The filter ate the cmd, so we remove it from queue + ret = get_queued_cmd(ictx, 0); + mp_cmd_free(ret); + return NULL; + } } - } - if (!from_queue && peek_only) - mp_input_queue_cmd(ictx, ret); + if (!from_queue && peek_only) + mp_input_queue_cmd(ictx, ret); - return ret; + return ret; } void -mp_cmd_free(mp_cmd_t* cmd) { +mp_cmd_free(mp_cmd_t *cmd) { talloc_free(cmd); } -mp_cmd_t* -mp_cmd_clone(mp_cmd_t* cmd) { - mp_cmd_t* ret; - int i; +mp_cmd_t * +mp_cmd_clone(mp_cmd_t *cmd) { + mp_cmd_t *ret; + int i; #ifdef MP_DEBUG - assert(cmd != NULL); + assert(cmd != NULL); #endif - ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t)); - ret->name = talloc_strdup(ret, 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 = talloc_strdup(ret, cmd->args[i].v.s); - } + ret = talloc_memdup(NULL, cmd, sizeof(mp_cmd_t)); + ret->name = talloc_strdup(ret, 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 = talloc_strdup(ret, cmd->args[i].v.s); + } - return ret; + return ret; } int mp_input_get_key_from_name(const char *name) @@ -1428,7 +1457,7 @@ int mp_input_get_key_from_name(const char *name) if (!strcmp(name, "+")) return '+' + modifiers; return -1; - found: +found: name = p + 1; } int len = strlen(name); @@ -1445,240 +1474,249 @@ int mp_input_get_key_from_name(const char *name) return -1; } -static int 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; +static int 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; } - n++; - if(end && end[1] != '\0' && n < MP_MAX_KEY_DOWN) - ptr = &end[1]; - else - break; - } - keys[n] = 0; - return 1; + keys[n] = 0; + return 1; } #define BS_MAX 256 #define SPACE_CHAR " \n\r\t" static void bind_keys(struct input_ctx *ictx, - const int keys[MP_MAX_KEY_DOWN+1], char *cmd) + const int keys[MP_MAX_KEY_DOWN + 1], char *cmd) { - int i = 0,j; - mp_cmd_bind_t* bind = NULL; - mp_cmd_bind_section_t* bind_section = NULL; - char *section=NULL, *p; + 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); + 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 = get_bind_section(ictx, 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 (*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 = get_bind_section(ictx, 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 = talloc_realloc(bind_section, + bind_section->cmd_binds, + mp_cmd_bind_t, i + 2); + memset(&bind_section->cmd_binds[i], 0, 2 * sizeof(mp_cmd_bind_t)); + bind = &bind_section->cmd_binds[i]; } - } - - if(!bind) { - bind_section->cmd_binds = talloc_realloc(bind_section, - bind_section->cmd_binds, - mp_cmd_bind_t, i + 2); - memset(&bind_section->cmd_binds[i],0,2*sizeof(mp_cmd_bind_t)); - bind = &bind_section->cmd_binds[i]; - } - talloc_free(bind->cmd); - bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd); - memcpy(bind->input,keys,(MP_MAX_KEY_DOWN+1)*sizeof(int)); + talloc_free(bind->cmd); + bind->cmd = talloc_strdup(bind_section->cmd_binds, cmd); + memcpy(bind->input, keys, (MP_MAX_KEY_DOWN + 1) * sizeof(int)); } static int parse_config(struct input_ctx *ictx, 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 }; + 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; + } - fd = open(file,O_RDONLY); + 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_tmsg(MSGT_INPUT, MSGL_ERR, "Error while reading " + "input config file %s: %s\n", 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; + } - 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_tmsg(MSGT_INPUT,MSGL_ERR,"Error while reading input config file %s: %s\n",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; - 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); - memmove(buffer,iter,r+1); - bs = r+1; - comments = 0; - continue; - } + 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); + memmove(buffer, iter, r + 1); + bs = r + 1; + 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_tmsg(MSGT_INPUT,MSGL_ERR,"Unfinished binding %s\n",iter); - else - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for this key name: %s\n",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 (!get_input_from_name(name,keys)) { - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Unknown key '%s'\n",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; - char *key_buf = get_key_name(keys[0]); - mp_tmsg(MSGT_INPUT,MSGL_ERR,"No command found for key %s", key_buf); - talloc_free(key_buf); - for(i = 1; keys[i] != 0 ; i++) { - char *key_buf = get_key_name(keys[i]); - mp_msg(MSGT_INPUT,MSGL_ERR,"-%s", key_buf); - talloc_free(key_buf); + // 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_tmsg(MSGT_INPUT, MSGL_ERR, + "Unfinished binding %s\n", iter); + else + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small " + "for this key name: %s\n", 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 (!get_input_from_name(name, keys)) { + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Unknown key '%s'\n", 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; + char *key_buf = get_key_name(keys[0]); + mp_tmsg(MSGT_INPUT, MSGL_ERR, "No command found for key %s", + key_buf); + talloc_free(key_buf); + for (i = 1; keys[i] != 0; i++) { + char *key_buf = get_key_name(keys[i]); + mp_msg(MSGT_INPUT, MSGL_ERR, "-%s", key_buf); + talloc_free(key_buf); + } + 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_tmsg(MSGT_INPUT, MSGL_ERR, "Buffer is too small " + "for command %s\n", 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); + bind_keys(ictx, 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,"\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_tmsg(MSGT_INPUT,MSGL_ERR,"Buffer is too small for command %s\n",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); - bind_keys(ictx, 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_tmsg(MSGT_INPUT,MSGL_ERR,"What are we doing here?\n"); - close(fd); - mp_input_set_section(ictx, NULL); - return 0; + mp_tmsg(MSGT_INPUT, MSGL_ERR, "What are we doing here?\n"); + close(fd); + mp_input_set_section(ictx, NULL); + return 0; } void mp_input_set_section(struct input_ctx *ictx, char *name) { - mp_cmd_bind_section_t* bind_section = NULL; + mp_cmd_bind_section_t *bind_section = NULL; ictx->cmd_binds = NULL; ictx->cmd_binds_default = NULL; @@ -1710,97 +1748,95 @@ struct input_ctx *mp_input_init(struct input_conf *input_conf) .default_bindings = input_conf->default_bindings, }; - char* file; - char *config_file = input_conf->config_file; - file = config_file[0] != '/' ? get_path(config_file) : config_file; - if(!file) - return ictx; - - if (!parse_config(ictx, file)) { - // free file if it was allocated by get_path(), - // before it gets overwritten - if( file != config_file) - { - free(file); + char *file; + char *config_file = input_conf->config_file; + file = config_file[0] != '/' ? get_path(config_file) : config_file; + if (!file) + return ictx; + + if (!parse_config(ictx, 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 (!parse_config(ictx, 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); } - // Try global conf dir - file = MPLAYER_CONFDIR "/input.conf"; - if (!parse_config(ictx, 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 CONFIG_JOYSTICK - if (input_conf->use_joystick) { - int fd = mp_input_joystick_init(input_conf->js_dev); - if(fd < 0) - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init input joystick\n"); - else - mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read, - (mp_close_func_t)close,NULL); - } + if (input_conf->use_joystick) { + int fd = mp_input_joystick_init(input_conf->js_dev); + if (fd < 0) + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init input joystick\n"); + else + mp_input_add_key_fd(ictx, fd, 1, mp_input_joystick_read, + (mp_close_func_t)close, NULL); + } #endif #ifdef CONFIG_LIRC - if (input_conf->use_lirc) { - int fd = mp_input_lirc_init(); - if(fd > 0) - mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read, - mp_input_lirc_close); - } + if (input_conf->use_lirc) { + int fd = mp_input_lirc_init(); + if (fd > 0) + mp_input_add_cmd_fd(ictx, fd, 0, mp_input_lirc_read, + mp_input_lirc_close); + } #endif #ifdef CONFIG_LIRCC - if (input_conf->use_lircc) { - int fd = lircc_init("mplayer", NULL); - if(fd >= 0) - mp_input_add_cmd_fd(ictx, fd, 1, NULL, (mp_close_func_t)lircc_cleanup); - } + if (input_conf->use_lircc) { + int fd = lircc_init("mplayer", NULL); + if (fd >= 0) + mp_input_add_cmd_fd(ictx, fd, 1, NULL, + (mp_close_func_t)lircc_cleanup); + } #endif #ifdef CONFIG_APPLE_REMOTE - if (input_conf->use_ar) { - if(mp_input_ar_init() < 0) - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n"); - else - mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, mp_input_ar_close, - NULL); - } + if (input_conf->use_ar) { + if (mp_input_ar_init() < 0) + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n"); + else + mp_input_add_key_fd(ictx, -1, 0, mp_input_ar_read, + mp_input_ar_close, NULL); + } #endif #ifdef CONFIG_APPLE_IR - if (input_conf->use_ar) { - int fd = mp_input_appleir_init(input_conf->ar_dev); - if(fd < 0) - mp_tmsg(MSGT_INPUT,MSGL_ERR,"Can't init Apple Remote.\n"); - else - mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read, - (mp_close_func_t)close, NULL); - } + if (input_conf->use_ar) { + int fd = mp_input_appleir_init(input_conf->ar_dev); + if (fd < 0) + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't init Apple Remote.\n"); + else + mp_input_add_key_fd(ictx, fd, 1, mp_input_appleir_read, + (mp_close_func_t)close, NULL); + } #endif - if (input_conf->in_file) { - struct stat st; - int mode = O_RDONLY; - // Use RDWR for FIFOs to ensure they stay open over multiple accesses. - // Note that on Windows stat may fail for named pipes, but due to how the - // API works, using RDONLY should be ok. - if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode)) - mode = O_RDWR; - int in_file_fd = open(input_conf->in_file, mode); - if(in_file_fd >= 0) - mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, - (mp_close_func_t)close); - else - mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n", - input_conf->in_file, strerror(errno)); - } - return ictx; + if (input_conf->in_file) { + struct stat st; + int mode = O_RDONLY; + // Use RDWR for FIFOs to ensure they stay open over multiple accesses. + // Note that on Windows stat may fail for named pipes, + // but due to how the API works, using RDONLY should be ok. + if (stat(input_conf->in_file, &st) == 0 && S_ISFIFO(st.st_mode)) + mode = O_RDWR; + int in_file_fd = open(input_conf->in_file, mode); + if (in_file_fd >= 0) + mp_input_add_cmd_fd(ictx, in_file_fd, 1, NULL, + (mp_close_func_t)close); + else + mp_tmsg(MSGT_INPUT, MSGL_ERR, "Can't open %s: %s\n", + input_conf->in_file, strerror(errno)); + } + return ictx; } void mp_input_uninit(struct input_ctx *ictx) @@ -1808,64 +1844,64 @@ void mp_input_uninit(struct input_ctx *ictx) if (!ictx) return; - unsigned int i; + unsigned int i; - for (i=0; i < ictx->num_key_fd; i++) { - if (ictx->key_fds[i].close_func) - ictx->key_fds[i].close_func(ictx->key_fds[i].fd); - } + for (i = 0; i < ictx->num_key_fd; i++) { + if (ictx->key_fds[i].close_func) + ictx->key_fds[i].close_func(ictx->key_fds[i].fd); + } - for (i = 0; i < ictx->num_cmd_fd; i++) { - if (ictx->cmd_fds[i].close_func) - i