void ejecutarComando(std::string &comando) { xdo_t * x = xdo_new(NULL); if (comando.substr(0, 4) == "type") { comando = comando.substr(5, comando.length()); xdo_enter_text_window(x, CURRENTWINDOW, comando.data(), 0); } else if (comando.substr(0, 5) == "click") { comando = comando.substr(6, comando.length()); int click = std::stoi(comando); xdo_click_window(x, CURRENTWINDOW, click); } else if (comando.substr(0, 9) == "mousemove") { comando = comando.substr(10, comando.length()); std::vector<std::string> coordenadas = split(comando); int x_mouse = std::stoi(coordenadas.at(0)); int y_mouse = std::stoi(coordenadas.at(1)); xdo_move_mouse(x, x_mouse, y_mouse, 0); } else { xdo_send_keysequence_window(x, CURRENTWINDOW, comando.c_str(), 0); } xdo_free(x); }
void mainloop(xdo_t* xdo, int joyfd, struct js_event* joystick, char jsbuttons, char jsaxes, char* buttons, char* axes, char* reversed, command* button_commands, command* axis_commands) { int last_value[jsaxes]; char x; for (x = 0; x < jsaxes; ++x) last_value[x] = 0; char temp_axes[jsaxes]; for (x = 0; x < jsaxes; ++x) temp_axes[axes[x]] = x; long delta[jsaxes]; int sensitivity; while (1) { if (read(joyfd, joystick, sizeof(joystick)) < 0) syslog(LOG_ERR, "Failed to read from the joystick."); if (joystick->type == JS_EVENT_AXIS) { delta[joystick->number] = reversed[temp_axes[joystick->number]] * joystick->value - last_value[joystick->number]; if (axis_commands[axes[joystick->number]].type == CMD_KEYPRESS) { if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "/HORIZONTAL") == 0) { if (joystick->value == 0) { if (delta[joystick->number] < 0) xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, "Right", 0); else xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, "Left", 0); } else if (joystick->value * reversed[temp_axes[joystick->number]] > 0) xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, "Right", 0); else xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, "Left", 0); if (reversed[temp_axes[joystick->number]] * joystick->value > 0 && delta[joystick->number] < 0 || reversed[temp_axes[joystick->number]] * joystick->value < 0 && delta[joystick->number] > 0) continue; else if (joystick->value == 0) { last_value[joystick->number] = 0; continue; } else { last_value[joystick->number] = reversed[temp_axes[joystick->number]] * joystick->value; continue; } } else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "/VERTICAL") == 0) { if (joystick->value == 0) { if (delta[joystick->number] < 0) xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, "Up", 0); else xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, "Down", 0); } else if (joystick->value * reversed[temp_axes[joystick->number]] > 0) xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, "Up", 0); else xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, "Down", 0); if (reversed[temp_axes[joystick->number]] * joystick->value > 0 && delta[joystick->number] < 0 || reversed[temp_axes[joystick->number]] * joystick->value < 0 && delta[joystick->number] > 0) continue; else if (joystick->value == 0) { last_value[joystick->number] = 0; continue; } else { last_value[joystick->number] = reversed[temp_axes[joystick->number]] * joystick->value; continue; } } } if (reversed[temp_axes[joystick->number]] * joystick->value > 0 && delta[joystick->number] < 0 || reversed[temp_axes[joystick->number]] * joystick->value < 0 && delta[joystick->number] > 0) continue; else if (joystick->value == 0) { last_value[joystick->number] = 0; continue; } switch (axis_commands[axes[joystick->number]].type) { case CMD_NONE: break; case CMD_MOUSEMOVE: sensitivity = atoi(axis_commands[joystick->number].arguments[1]); delta[joystick->number] = reversed[temp_axes[joystick->number]] * delta[joystick->number] * (delta[joystick->number] > 0 ? delta[joystick->number] : delta[joystick->number] * (-1)) * sensitivity / 1800000; if (strcmp(axis_commands[joystick->number].arguments[0], "horizontal") == 0) xdo_move_mouse_relative(xdo, delta[joystick->number], 0); else xdo_move_mouse_relative(xdo, 0, delta[joystick->number]); break; case CMD_MOUSECLICK: if (strcmp(axis_commands[axes[joystick->number]].arguments[1], "current") == 0) { if (delta[joystick->number] > 0) { if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "left") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 1); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 2); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "right") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 3); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 4); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 5); } else { if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "left") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 1); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 2); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "right") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 3); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 4); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 5); } } else { int x = atoi(axis_commands[axes[joystick->number]].arguments[1]); int y = atoi(axis_commands[axes[joystick->number]].arguments[2]); int screen = atoi(axis_commands[axes[joystick->number]].arguments[3]); xdo_move_mouse(xdo, x, y, screen); if (delta[joystick->number] > 0) { if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "left") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 1); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 2); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "right") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 3); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 4); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 5); } else { if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "left") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 1); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 2); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "right") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 3); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 4); else if (strcmp(axis_commands[axes[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 5); } } break; case CMD_MOUSETELEPORT: if (delta[joystick->number] > 0) { int x = atoi(axis_commands[axes[joystick->number]].arguments[0]); int y = atoi(axis_commands[axes[joystick->number]].arguments[1]); int screen = atoi(axis_commands[axes[joystick->number]].arguments[2]); xdo_move_mouse(xdo, x, y, screen); } break; case CMD_KEYPRESS: if (delta[joystick->number] > 0) xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, axis_commands[axes[joystick->number]].arguments[0], 0); else xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, axis_commands[axes[joystick->number]].arguments[0], 0); break; case CMD_KEYSTROKE: if (delta[joystick->number] > 0) xdo_enter_text_window(xdo, CURRENTWINDOW, axis_commands[axes[joystick->number]].arguments[0], 0); break; case CMD_COMMAND: if (delta[joystick->number] > 0) system(axis_commands[axes[joystick->number]].arguments[0]); break; default: break; } } else if (joystick->type == JS_EVENT_BUTTON) { switch (button_commands[buttons[joystick->number]].type) { case CMD_NONE: break; case CMD_MOUSECLICK: if (strcmp(button_commands[buttons[joystick->number]].arguments[1], "current") == 0) { if (joystick->value == 1) { if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "left") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 1); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 2); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "right") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 3); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 4); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 5); } else { if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "left") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 1); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 2); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "right") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 3); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 4); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 5); } } else { int x = atoi(button_commands[buttons[joystick->number]].arguments[1]); int y = atoi(button_commands[buttons[joystick->number]].arguments[2]); int screen = atoi(button_commands[buttons[joystick->number]].arguments[3]); xdo_move_mouse(xdo, x, y, screen); if (joystick->value == 1) { if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "left") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 1); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 2); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "right") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 3); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 4); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_down(xdo, CURRENTWINDOW, 5); } else { if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "left") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 1); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "middle") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 2); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "right") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 3); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheelup") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 4); else if (strcmp(button_commands[buttons[joystick->number]].arguments[0], "wheeldown") == 0) xdo_mouse_up(xdo, CURRENTWINDOW, 5); } } break; case CMD_MOUSETELEPORT: if (joystick->value == 1) { int x = atoi(button_commands[buttons[joystick->number]].arguments[0]); int y = atoi(button_commands[buttons[joystick->number]].arguments[1]); int screen = atoi(button_commands[buttons[joystick->number]].arguments[2]); xdo_move_mouse(xdo, x, y, screen); } break; case CMD_KEYPRESS: if (joystick->value == 1) xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, button_commands[buttons[joystick->number]].arguments[0], 0); else xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, button_commands[buttons[joystick->number]].arguments[0], 0); break; case CMD_KEYSTROKE: if (joystick->value == 1) xdo_enter_text_window(xdo, CURRENTWINDOW, button_commands[buttons[joystick->number]].arguments[0], 0); break; case CMD_COMMAND: if (joystick->value == 1) system(button_commands[buttons[joystick->number]].arguments[0]); break; default: break; } } last_value[joystick->number] = reversed[temp_axes[joystick->number]] * joystick->value; } }
/** * @param *str is the not constant char pointer to the string that will be printed using the kb simulation * */ int kb_print_str(char * str) { xdo_t * xdo_instance = xdo_new(NULL); ///< Create new xdo instance and null param indicates that uses env var DISPLAY, also could be used ":0.0" xdo_enter_text_window(xdo_instance, CURRENTWINDOW, str, 0); ///< Send a string to the current window, check xdotool ref return 1;///< As windows sendinput return 0 means error, we need to return the number of correct succesful insertions }
int cmd_type(context_t *context) { int ret = 0; int i; int c; char *cmd = *context->argv; char *window_arg = NULL; int arity = -1; char *terminator = NULL; char *file = NULL; FILE *input = NULL; char *buffer = NULL; char *marker = NULL; size_t bytes_read = 0; char **data = NULL; /* stuff to type */ int data_count = 0; int args_count = 0; charcodemap_t *active_mods = NULL; int active_mods_n; /* Options */ int clear_modifiers = 0; useconds_t delay = 12000; /* 12ms between keystrokes default */ typedef enum { opt_unused, opt_clearmodifiers, opt_delay, opt_help, opt_window, opt_args, opt_terminator, opt_file } optlist_t; struct option longopts[] = { { "clearmodifiers", no_argument, NULL, opt_clearmodifiers }, { "delay", required_argument, NULL, opt_delay }, { "help", no_argument, NULL, opt_help }, { "window", required_argument, NULL, opt_window }, { "args", required_argument, NULL, opt_args }, { "terminator", required_argument, NULL, opt_terminator }, { "file", required_argument, NULL, opt_file }, { 0, 0, 0, 0 }, }; static const char *usage = "Usage: %s [--window windowid] [--delay milliseconds] " "<things to type>\n" "--window <windowid> - specify a window to send keys to\n" "--delay <milliseconds> - delay between keystrokes\n" "--clearmodifiers - reset active modifiers (alt, etc) while typing\n" "--args N - how many arguments to expect in the exec command. This is\n" " useful for ending an exec and continuing with more xdotool\n" " commands\n" "--terminator TERM - similar to --args, specifies a terminator that\n" " marks the end of 'exec' arguments. This is useful\n" " for continuing with more xdotool commands.\n" "--file <filepath> - specify a file, the contents of which will be\n" " be typed as if passed as an argument. The filepath\n" " may also be '-' to read from stdin.\n" "-h, --help - show this help output\n"; int option_index; while ((c = getopt_long_only(context->argc, context->argv, "+w:d:ch", longopts, &option_index)) != -1) { switch (c) { case opt_window: window_arg = strdup(optarg); break; case opt_delay: /* --delay is in milliseconds, convert to microseconds */ delay = strtoul(optarg, NULL, 0) * 1000; break; case opt_clearmodifiers: clear_modifiers = 1; break; case opt_help: printf(usage, cmd); consume_args(context, context->argc); return EXIT_SUCCESS; break; case opt_args: arity = atoi(optarg); break; case opt_terminator: terminator = strdup(optarg); break; case opt_file: file = strdup(optarg); break; default: fprintf(stderr, usage, cmd); return EXIT_FAILURE; } } consume_args(context, optind); if (context->argc == 0 && file == NULL) { fprintf(stderr, "You specified the wrong number of args.\n"); fprintf(stderr, usage, cmd); return 1; } if (arity > 0 && terminator != NULL) { fprintf(stderr, "Don't use both --terminator and --args.\n"); return EXIT_FAILURE; } if (context->argc < arity) { fprintf(stderr, "You said '--args %d' but only gave %d arguments.\n", arity, context->argc); return EXIT_FAILURE; } if (file != NULL) { data = calloc(1 + context->argc, sizeof(char *)); /* determine whether reading from a file or from stdin */ if (!strcmp(file, "-")) { input = fdopen(0, "r"); } else { input = fopen(file, "r"); if (input == NULL) { fprintf(stderr, "Failure opening '%s': %s\n", file, strerror(errno)); return EXIT_FAILURE; } } while (feof(input) == 0) { marker = realloc(buffer, bytes_read + 4096); if (marker == NULL) { fprintf(stderr, "Failure allocating for '%s': %s\n", file, strerror(errno)); return EXIT_FAILURE; } buffer = marker; marker = buffer + bytes_read; if (fgets(marker, 4096, input) != NULL) { bytes_read = (marker - buffer) + strlen(marker); } if (ferror(input) != 0) { fprintf(stderr, "Failure reading '%s': %s\n", file, strerror(errno)); return EXIT_FAILURE; } } data[0] = buffer; data_count++; fclose(input); } else { data = calloc(context->argc, sizeof(char *)); } /* Apply any --arity or --terminator */ for (i=0; i < context->argc; i++) { if (arity > 0 && i == arity) { data[data_count] = NULL; break; } /* if we have a terminator and the current argument matches it... */ if (terminator != NULL && strcmp(terminator, context->argv[i]) == 0) { data[data_count] = NULL; args_count++; /* Consume the terminator, too */ break; } data[data_count] = strdup(context->argv[i]); xdotool_debug(context, "Exec arg[%d]: %s", i, data[data_count]); data_count++; args_count++; } window_each(context, window_arg, { if (clear_modifiers) { xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n); xdo_clear_active_modifiers(context->xdo, window, active_mods, active_mods_n); } for (i = 0; i < data_count; i++) { //printf("Typing: '%s'\n", context->argv[i]); int tmp = xdo_enter_text_window(context->xdo, window, data[i], delay); if (tmp) { fprintf(stderr, "xdo_enter_text_window reported an error\n"); } ret += tmp; } if (clear_modifiers) { xdo_set_active_modifiers(context->xdo, window, active_mods, active_mods_n); free(active_mods); } }); /* window_each(...) */