Пример #1
0
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;
	}
}
Пример #2
0
int cmd_mousemove_relative(context_t *context) {
  int x, y;
  int ret = 0;
  char *cmd = *context->argv;
  int polar_coordinates = 0;
  int clear_modifiers = 0;
  int opsync = 0;
  int origin_x = -1, origin_y = -1;

  charcodemap_t *active_mods = NULL;
  int active_mods_n;
  int c;
  typedef enum {
    opt_unused, opt_help, opt_sync, opt_clearmodifiers, opt_polar
  } optlist_t;
  static struct option longopts[] = {
    { "help", no_argument, NULL, opt_help },
    { "sync", no_argument, NULL, opt_sync },
    { "polar", no_argument, NULL, opt_polar },
    { "clearmodifiers", no_argument, NULL, opt_clearmodifiers },
    { 0, 0, 0, 0 },
  };
  static const char *usage =
      "Usage: %s [options] <x> <y>\n"
      "-c, --clearmodifiers      - reset active modifiers (alt, etc) while typing\n"
      "-p, --polar               - Use polar coordinates. X as an angle, Y as distance\n"
      "--sync                    - only exit once the mouse has moved\n"
      "\n"
      "Using polar coordinate mode makes 'x' the angle (in degrees) and\n"
      "'y' the distance.\n"
      "\n"
      "If you want to use negative numbers for a coordinate, you'll need to\n"
      "invoke it this way (with the '--'):\n"
      "   %s -- -20 -15\n"
      "otherwise, normal usage looks like this:\n"
      "   %s 100 140\n";
  int option_index;

  while ((c = getopt_long_only(context->argc, context->argv, "+cph",
                               longopts, &option_index)) != -1) {
    switch (c) {
      case 'h':
      case opt_help:
        printf(usage, cmd, cmd, cmd);
        consume_args(context, context->argc);
        return EXIT_SUCCESS;
        break;
      case 'p':
      case opt_polar:
        polar_coordinates = 1;
        break;
      case opt_sync:
        opsync = 1;
        break;
      case 'c':
      case opt_clearmodifiers:
        clear_modifiers = 1;
        break;
      default:
        fprintf(stderr, usage, cmd, cmd, cmd);
        return EXIT_FAILURE;
    }
  }

  consume_args(context, optind);

  if (context->argc < 2) {
    fprintf(stderr, usage, cmd, cmd, cmd);
    fprintf(stderr, "You specified the wrong number of args (expected 2).\n");
    return EXIT_FAILURE;
  }

  x = atoi(context->argv[0]);
  y = atoi(context->argv[1]);
  consume_args(context, 2);

  /* Quit early if we don't have to move. */
  if (x == 0 && y == 0) {
    return EXIT_SUCCESS;
  }

  if (polar_coordinates) {
    /* The original request for polar support was that '0' degrees is up
     * and that rotation was clockwise, so 0 is up, 90 right, 180 down, 270
     * left. This conversion can be done with (360 - degrees) + 90 */
    double radians = ((360 - x) + 90) * (M_PI / 180);
    double distance = y;
    x = (cos(radians) * distance);

    /* Negative sin, since screen Y coordinates are descending, where cartesian
     * is ascending */
    y = (-sin(radians) * distance);
  }
 
  if (clear_modifiers) {
    xdo_get_active_modifiers(context->xdo, &active_mods, &active_mods_n);
    xdo_clear_active_modifiers(context->xdo, CURRENTWINDOW, active_mods, active_mods_n);
  }

  if (opsync) {
    xdo_get_mouse_location(context->xdo, &origin_x, &origin_y, NULL);
  }

  ret = xdo_move_mouse_relative(context->xdo, x, y);

  if (ret) {
    fprintf(stderr, "xdo_move_mouse_relative reported an error\n");
  } else {
    if (opsync) {
      /* Wait until the mouse moves away from its current position */
      xdo_wait_for_mouse_move_from(context->xdo, origin_x, origin_y);
    }
  }

  if (clear_modifiers) {
    xdo_set_active_modifiers(context->xdo, CURRENTWINDOW, active_mods, active_mods_n);
    free(active_mods);
  }

  return ret;
}