void run_command (player * p, char *str) { command *com; char *param; for (; *str == ' '; str++); trim_end (str); if (!*str) return; if (!isalpha (*str)) com = root_commands[0]; else com = root_commands[((tolower (*str) - 'a') + 1)]; for (; com->key; com++) if ((param = match_command_key (com, str)) && com->level <= p->residency) { if (com->sublist) run_subcommand (p, param, com); else actually_do_command (p, com, param); return; } param = next_space (str); if (*param) *param++ = '\0'; vwritep (p, "No command %s available to you.\n", str); return; }
static int ratbag_cmd_resolution_rate(const struct ratbag_cmd *cmd, struct ratbag *ratbag, struct ratbag_cmd_options *options, int argc, char **argv) { if (argc < 1) return ERR_USAGE; return run_subcommand(argv[0], cmd, ratbag, options, argc, argv); }
static int ratbag_cmd_resolution(const struct ratbag_cmd *cmd, struct ratbag *ratbag, struct ratbag_cmd_options *options, int argc, char **argv) { struct ratbag_profile *profile; struct ratbag_resolution *resolution; const char *command; int resolution_idx = 0; char *endp; if (argc < 1) return ERR_USAGE; command = argv[0]; profile = options->profile; resolution_idx = strtol(command, &endp, 10); if (command != endp && *endp == '\0') { resolution = ratbag_profile_get_resolution(profile, resolution_idx); if (!resolution) { error("Unable to retrieve resolution %d\n", resolution_idx); return ERR_UNSUPPORTED; } argc--; argv++; command = argv[0]; } else { resolution = ratbag_cmd_get_active_resolution(profile); if (!resolution) return ERR_DEVICE; } options->resolution = resolution; return run_subcommand(command, cmd, ratbag, options, argc, argv); }
static int ratbag_cmd_button_action(const struct ratbag_cmd *cmd, struct ratbag *ratbag, struct ratbag_cmd_options *options, int argc, char **argv) { const char *command; if (argc < 1) return ERR_USAGE; command = argv[0]; return run_subcommand(command, cmd, ratbag, options, argc, argv); }
static int ratbag_cmd_profile(const struct ratbag_cmd *cmd, struct ratbag *ratbag, struct ratbag_cmd_options *options, int argc, char **argv) { struct ratbag_profile *profile; struct ratbag_device *device; const char *command; int profile_idx = 0; char *endp; device = options->device; if (argc < 1) return ERR_USAGE; command = argv[0]; profile_idx = strtol(command, &endp, 10); if (command != endp && *endp == '\0') { profile = ratbag_device_get_profile(device, profile_idx); if (!profile) { error("Unable to find profile %d\n", profile_idx); return ERR_UNSUPPORTED; } argc--; argv++; command = argv[0]; } else { profile = ratbag_cmd_get_active_profile(device); if (!profile) return ERR_DEVICE; } options->profile = profile; return run_subcommand(command, cmd, ratbag, options, argc, argv); }
static int ratbag_cmd_button(const struct ratbag_cmd *cmd, struct ratbag *ratbag, struct ratbag_cmd_options *options, int argc, char **argv) { struct ratbag_profile *profile; struct ratbag_button *button; const char *command; int button_idx = 0; char *endp; if (argc < 1) return ERR_USAGE; profile = options->profile; command = argv[0]; button_idx = strtol(command, &endp, 10); if (command != endp && *endp == '\0') { button = ratbag_profile_get_button(profile, button_idx); if (!button) { error("Invalid button %d\n", button_idx); return ERR_UNSUPPORTED; } options->button = button; argc--; argv++; command = argv[0]; } return run_subcommand(command, cmd, ratbag, options, argc, argv); }
int main(int argc, char **argv) { struct ratbag *ratbag; const char *command; int rc = SUCCESS; struct ratbag_cmd_options options = {0}; ratbag = ratbag_create_context(&interface, NULL); if (!ratbag) { rc = ERR_DEVICE; error("Failed to initialize ratbag\n"); goto out; } options.flags = 0; while (1) { int c; int option_index = 0; static struct option opts[] = { { "verbose", 2, 0, OPT_VERBOSE }, { "help", 0, 0, OPT_HELP }, }; c = getopt_long(argc, argv, "+h", opts, &option_index); if (c == -1) break; switch(c) { case 'h': case OPT_HELP: usage(); goto out; case OPT_VERBOSE: if (optarg && streq(optarg, "raw")) options.flags |= FLAG_VERBOSE_RAW; else options.flags |= FLAG_VERBOSE; break; default: goto out; } } if (optind >= argc) { rc = ERR_USAGE; goto out; } if (options.flags & FLAG_VERBOSE_RAW) ratbag_log_set_priority(ratbag, RATBAG_LOG_PRIORITY_RAW); else if (options.flags & FLAG_VERBOSE) ratbag_log_set_priority(ratbag, RATBAG_LOG_PRIORITY_DEBUG); argc -= optind; argv += optind; command = argv[0]; rc = run_subcommand(command, ratbag_commands, ratbag, &options, argc, argv); if (options.device) rc = ratbag_device_commit(options.device); out: ratbag_resolution_unref(options.resolution); ratbag_button_unref(options.button); ratbag_profile_unref(options.profile); ratbag_device_unref(options.device); ratbag_unref(ratbag); if (rc == ERR_USAGE) usage(); return rc; }