static error_t parse_opt(int k, char *arg, struct argp_state *state) { char *p; long key; int rc; switch (k) { case 'v': debug++; break; case 't': test++; break; case 'c': clear++; break; case 'D': delay = atoi(arg); break; case 'P': period = atoi(arg); break; case 'd': devicename = arg; break; case 's': devclass = arg; break; case 'r': readtable++; break; case 'w': { char *name = NULL; rc = parse_keyfile(arg, &name); if (rc) goto err_inval; if (name) fprintf(stderr, "Read %s table\n", name); break; } case 'a': { rc = parse_cfgfile(arg); if (rc) goto err_inval; break; } case 'k': p = strtok(arg, ":="); do { if (!p) goto err_inval; nextkey->codes[0] = strtoul(p, NULL, 0); if (errno) goto err_inval; p = strtok(NULL, ",;"); if (!p) goto err_inval; key = parse_code(p); if (key == -1) { key = strtol(p, NULL, 0); if (errno) goto err_inval; } nextkey->codes[1] = key; if (debug) fprintf(stderr, "scancode 0x%04x=%u\n", nextkey->codes[0], nextkey->codes[1]); nextkey->next = calloc(1, sizeof(keys)); if (!nextkey->next) { perror("No memory!\n"); return ENOMEM; } nextkey = nextkey->next; p = strtok(NULL, ":="); } while (p); break; case 'p': p = strtok(arg, ",;"); do { if (!p) goto err_inval; if (!strcasecmp(p,"rc5") || !strcasecmp(p,"rc-5")) ch_proto |= RC_5; else if (!strcasecmp(p,"rc6") || !strcasecmp(p,"rc-6")) ch_proto |= RC_6; else if (!strcasecmp(p,"nec")) ch_proto |= NEC; else if (!strcasecmp(p,"jvc")) ch_proto |= JVC; else if (!strcasecmp(p,"sony")) ch_proto |= SONY; else if (!strcasecmp(p,"sanyo")) ch_proto |= SANYO; else if (!strcasecmp(p,"lirc")) ch_proto |= LIRC; else if (!strcasecmp(p,"rc-5-sz")) ch_proto |= RC_5_SZ; else goto err_inval; p = strtok(NULL, ",;"); } while (p); break; default: return ARGP_ERR_UNKNOWN; } return 0; err_inval: fprintf(stderr, "Invalid parameter(s)\n"); return ARGP_ERR_UNKNOWN; }
int main(int argc, char *argv[]) { int dev_from_class = 0, write_cnt; int fd; static struct sysfs_names *names; struct rc_device rc_dev; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); argp_parse(&argp, argc, argv, ARGP_NO_HELP | ARGP_NO_EXIT, 0, 0); /* Just list all devices */ if (!clear && !readtable && !keys.next && !ch_proto && !cfg.next && !test && !delay && !period) { if (devicename) { fd = open(devicename, O_RDONLY); if (fd < 0) { perror(_("Can't open device")); return -1; } device_info(fd, ""); close(fd); return 0; } if (show_sysfs_attribs(&rc_dev)) return -1; return 0; } if (cfg.next && (clear || keys.next || ch_proto || devicename)) { fprintf (stderr, _("Auto-mode can be used only with --read, --debug and --sysdev options\n")); return -1; } if (!devicename) { names = find_device(devclass); if (!names) return -1; rc_dev.sysfs_name = names->name; if (get_attribs(&rc_dev, names->name)) { free_names(names); return -1; } names->name = NULL; free_names(names); devicename = rc_dev.input_name; dev_from_class++; } if (cfg.next) { struct cfgfile *cur; char *fname, *name; int rc; for (cur = &cfg; cur->next; cur = cur->next) { if ((!rc_dev.drv_name || strcasecmp(cur->driver, rc_dev.drv_name)) && strcasecmp(cur->driver, "*")) continue; if ((!rc_dev.keytable_name || strcasecmp(cur->table, rc_dev.keytable_name)) && strcasecmp(cur->table, "*")) continue; break; } if (!cur->next) { if (debug) fprintf(stderr, _("Table for %s, %s not found. Keep as-is\n"), rc_dev.drv_name, rc_dev.keytable_name); return 0; } if (debug) fprintf(stderr, _("Table for %s, %s is on %s file.\n"), rc_dev.drv_name, rc_dev.keytable_name, cur->fname); if (cur->fname[0] == '/' || ((cur->fname[0] == '.') && strchr(cur->fname, '/'))) { fname = cur->fname; rc = parse_keyfile(fname, &name); if (rc < 0) { fprintf(stderr, _("Can't load %s table\n"), fname); return -1; } } else { fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_USER_DIR) + 2); strcpy(fname, IR_KEYTABLE_USER_DIR); strcat(fname, "/"); strcat(fname, cur->fname); rc = parse_keyfile(fname, &name); if (rc != 0) { fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_SYSTEM_DIR) + 2); strcpy(fname, IR_KEYTABLE_SYSTEM_DIR); strcat(fname, "/"); strcat(fname, cur->fname); rc = parse_keyfile(fname, &name); } if (rc != 0) { fprintf(stderr, _("Can't load %s table from %s or %s\n"), cur->fname, IR_KEYTABLE_USER_DIR, IR_KEYTABLE_SYSTEM_DIR); return -1; } } if (!keys.next) { fprintf(stderr, _("Empty table %s\n"), fname); return -1; } clear = 1; } if (debug) fprintf(stderr, _("Opening %s\n"), devicename); fd = open(devicename, O_RDONLY); if (fd < 0) { perror(devicename); return -1; } if (dev_from_class) free(devicename); if (get_input_protocol_version(fd)) return -1; /* * First step: clear, if --clear is specified */ if (clear) { clear_table(fd); fprintf(stderr, _("Old keytable cleared\n")); } /* * Second step: stores key tables from file or from commandline */ write_cnt = add_keys(fd); if (write_cnt) fprintf(stderr, _("Wrote %d keycode(s) to driver\n"), write_cnt); /* * Third step: change protocol */ if (ch_proto) { rc_dev.current = ch_proto; if (set_proto(&rc_dev)) fprintf(stderr, _("Couldn't change the IR protocols\n")); else { fprintf(stderr, _("Protocols changed to ")); show_proto(rc_dev.current); fprintf(stderr, "\n"); } } /* * Fourth step: display current keytable */ if (readtable) display_table(&rc_dev, fd); /* * Fiveth step: change repeat rate/delay */ if (delay || period) { unsigned int new_delay, new_period; get_rate(fd, &new_delay, &new_period); if (delay) new_delay = delay; if (period) new_period = period; set_rate(fd, new_delay, new_period); } if (test) test_event(fd); return 0; }
int main(int argc, char *argv[]) { int dev_from_class = 0, write_cnt; int fd; static struct sysfs_names *names; struct rc_device rc_dev; argp_parse(&argp, argc, argv, 0, 0, 0); /* Just list all devices */ if (!clear && !read && !keys.next && !ch_proto && !cfg.next) { static struct sysfs_names *names, *cur; names = find_device(NULL); if (!names) return -1; for (cur = names; cur->next; cur = cur->next) { if (cur->name) { if (get_attribs(&rc_dev, cur->name)) return -1; fprintf(stderr, "Found %s (%s) with:\n", rc_dev.sysfs_name, rc_dev.input_name); fprintf(stderr, "\tDriver %s, %s decoder, table %s\n", rc_dev.drv_name, (rc_dev.type == SOFTWARE_DECODER) ? "raw software" : "hardware", rc_dev.keytable_name); fprintf(stderr, "\tSupported protocols: "); show_proto(rc_dev.supported); fprintf(stderr, "\t"); display_proto(&rc_dev); } } return 0; } if (cfg.next && (clear || keys.next || ch_proto || devname)) { fprintf (stderr, "Auto-mode can be used only with --read, --debug and --sysdev options\n"); return -1; } if (!devname) { names = find_device(devclass); if (!names) return -1; rc_dev.sysfs_name = names->name; if (get_attribs(&rc_dev, names->name)) { free_names(names); return -1; } names->name = NULL; free_names(names); devname = rc_dev.input_name; dev_from_class++; } if (cfg.next) { struct cfgfile *cur; char *fname, *name; int rc; for (cur = &cfg; cur->next; cur = cur->next) { if (strcasecmp(cur->driver, rc_dev.drv_name) && strcasecmp(cur->driver, "*")) continue; if (strcasecmp(cur->table, rc_dev.keytable_name) && strcasecmp(cur->table, "*")) continue; break; } if (!cur->next) { if (debug) fprintf(stderr, "Table for %s, %s not found. Keep as-is\n", rc_dev.drv_name, rc_dev.keytable_name); return 0; } if (debug) fprintf(stderr, "Table for %s, %s is on %s file.\n", rc_dev.drv_name, rc_dev.keytable_name, cur->fname); if (cur->fname[0] == '/' || ((cur->fname[0] == '.') && strchr(cur->fname, '/'))) { fname = cur->fname; } else { fname = malloc(strlen(cur->fname) + strlen(CFGDIR) + 2); strcpy(fname, CFGDIR); strcat(fname, "/"); strcat(fname, cur->fname); } rc = parse_keyfile(fname, &name); if (rc < 0 || !keys.next) { fprintf(stderr, "Can't load %s table or empty table\n", fname); return -1; } clear = 1; } if (debug) fprintf(stderr, "Opening %s\n", devname); fd = open(devname, O_RDONLY); if (fd < 0) { perror(devname); return -1; } if (dev_from_class) free(devname); /* * First step: clear, if --clear is specified */ if (clear) { clear_table(fd); fprintf(stderr, "Old keytable cleared\n"); } /* * Second step: stores key tables from file or from commandline */ write_cnt = add_keys(fd); if (write_cnt) fprintf(stderr, "Wrote %d keycode(s) to driver\n", write_cnt); /* * Third step: change protocol */ if (ch_proto) { rc_dev.current = ch_proto; if (set_proto(&rc_dev)) fprintf(stderr, "Couldn't change the IR protocols\n"); else { fprintf(stderr, "Protocols changed to "); show_proto(rc_dev.current); fprintf(stderr, "\n"); } } /* * Fourth step: display current keytable */ if (read) display_table(&rc_dev, fd); return 0; }