Exemplo n.º 1
0
int main(int argc, char *argv[])
{
    int i;
    int fd;
    int ret;
    int version;

    int num_args = 0;
    int args[MAX_COMMAND_ARGS];
    char *line, *cmd, *arg;
    char device_name[80];
    unsigned int device_flags = 0;

    if(argc != 3) {
        fprintf(stderr, "Usage: %s <device> <script file>\n", argv[0]);
        return 1;
    }

    fd = open(argv[1], O_RDWR);
    if(fd < 0) {
        fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
        return 1;
    }
    if (ioctl(fd, EVIOCGVERSION, &version)) {
        fprintf(stderr, "could not get driver version for %s, %s\n",
                argv[optind], strerror(errno));
        return 1;
    }
    if(ioctl(fd, EVIOCGNAME(sizeof(device_name) - 1), &device_name) < 1) {
        //fprintf(stderr, "could not get device name for %s, %s\n", device, strerror(errno));
        device_name[0] = '\0';
    }

    // the atmel touchscreen has a weird protocol which requires MT_SYN events
    // to be sent after every touch
    if(strcmp(device_name, "atmel-touchscreen") == 0) {
      device_flags &= NEED_MT_SYN;
    }

    FILE *f = fopen(argv[2], "r");
    if (!f) {
      printf("Unable to read file %s", argv[1]);
      return 1;
    }

    line = malloc(sizeof(char)*MAX_COMMAND_LEN);
    while (fgets(line, MAX_COMMAND_LEN, f) != NULL) {
      cmd = strtok(line, " ");

      num_args = 0;
      while ((arg = strtok(NULL, " \n")) != NULL) {
        assert(num_args < MAX_COMMAND_ARGS);

        args[num_args] = atoi(arg);
        num_args++;
      }

      if (strcmp(cmd, "tap") == 0) {
        assert(num_args == 3);
        execute_tap(fd, version, device_flags, args[0], args[1], args[2]);
      } else if (strcmp(cmd, "drag") == 0) {
        assert(num_args == 6);
        execute_drag(fd, version, device_flags, args[0], args[1], args[2],
                     args[3], args[4], args[5]);
      } else if (strcmp(cmd, "sleep") == 0) {
        assert(num_args == 1);
        execute_sleep(args[0]);
      } else if (strcmp(cmd, "pinch") == 0) {
        assert(num_args == 10);
        execute_pinch(fd, version, device_flags, args[0], args[1], args[2],
                      args[3], args[4], args[5], args[6], args[7], args[8],
                      args[9]);
      } else {
        printf("Unrecognized command: '%s'", cmd);
        return 1;
      }
    }
    free(line);

    return 0;
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
  int i;
  int fd;
  int ret;
  int c;
  const char *device;
  const char *script_file;

  int num_args = 0;
  int args[MAX_COMMAND_ARGS];
  char *line, *cmd, *arg;

  while ((c = getopt (argc, argv, "t")) != -1) {
    if (c=='t') {
      print_actions = 1;
    } else {
      fprintf(stderr, "Unknown option: -%c\n", c);
    }
  }

  if((argc - optind) != 2) {
    fprintf(stderr, "Usage: %s [options] <device> <script file>\n\n"
            "Options:\n"
            "  -t                  print event timings\n", argv[0]);
    return 1;
  }
  device = argv[optind];
  script_file = argv[optind + 1];

  fd = open(device, O_RDWR);
  if(fd < 0) {
    fprintf(stderr, "could not open %s, %s\n", device, strerror(errno));
    return 1;
  }

  uint32_t device_flags = figure_out_events_device_reports(fd);

  FILE *f = fopen(script_file, "r");
  if (!f) {
    printf("Unable to read file %s", script_file);
    return 1;
  }

  line = malloc(sizeof(char)*MAX_COMMAND_LEN);
  int lineCount = 0;
  while (fgets(line, MAX_COMMAND_LEN, f) != NULL) {
    // Remove end-of-line comments.
    char *comment = strstr(line, "#");
    if (comment != NULL)
      *comment = '\0';

    lineCount += 1;
    int hasNextCmd = 1;
    char *tempLine = line;
  commandLoop:
    while (hasNextCmd) {
      num_args = 0;
      hasNextCmd = 0;
      int errCode = 0;

      // Parse {-} comments before command names.
      do {
        if ((cmd = strtok(tempLine, " \n")) == NULL)
          goto commandLoop;
        tempLine = NULL;
      } while ((errCode = parseComment(cmd, lineCount)) == 1);
      if (errCode < 0)
        return 1;

      while ((arg = strtok(NULL, " \n")) != NULL) {
        // Parse comment {-} within arguments.
        if ((errCode = parseComment(arg, lineCount)) != 0) {
          if (errCode < 0)
            return 1;
          continue;
        }

        // If we enter a new command, we remember the position for the next iteration.
        if (*arg == ';') {
          hasNextCmd = 1;
          break;
        }

        assert(num_args < MAX_COMMAND_ARGS);

        args[num_args] = atoi(arg);
        num_args++;
      }

      if (strcmp(cmd, "tap") == 0) {
        checkArguments(cmd, num_args, 4, lineCount);
        execute_tap(fd, device_flags, args[0], args[1], args[2], args[3]);
      } else if (strcmp(cmd, "drag") == 0) {
        checkArguments(cmd, num_args, 6, lineCount);
        execute_drag(fd, device_flags, args[0], args[1], args[2],
                     args[3], args[4], args[5]);
      } else if (strcmp(cmd, "sleep") == 0) {
        checkArguments(cmd, num_args, 1, lineCount);
        execute_sleep(args[0]);
      } else if (strcmp(cmd, "pinch") == 0) {
        checkArguments(cmd, num_args, 10, lineCount);
        execute_pinch(fd, device_flags, args[0], args[1], args[2],
                      args[3], args[4], args[5], args[6], args[7], args[8],
                      args[9]);
      } else if (strcmp(cmd, "keyup") == 0) {
        checkArguments(cmd, num_args, 1, lineCount);
        execute_keyup(fd, args[0]);
      } else if (strcmp(cmd, "keydown") == 0) {
        checkArguments(cmd, num_args, 1, lineCount);
        execute_keydown(fd, args[0]);
      } else {
        printf("Unrecognized command at line %d: '%s'\n", lineCount, cmd);
        return 1;
      }
    }
  }
  free(line);

  return 0;
}
Exemplo n.º 3
0
Arquivo: orng.c Projeto: tdz/orangutan
int main(int argc, char *argv[])
{
    int i;
    int fd;
    int ret;

    int num_args = 0;
    int args[MAX_COMMAND_ARGS];
    char *line, *cmd, *arg;

    if(argc != 3) {
        fprintf(stderr, "Usage: %s <device> <script file>\n", argv[0]);
        return 1;
    }

    fd = open(argv[1], O_RDWR);
    if(fd < 0) {
        fprintf(stderr, "could not open %s, %s\n", argv[optind], strerror(errno));
        return 1;
    }

    uint32_t device_flags = figure_out_events_device_reports(fd);

    FILE *f = fopen(argv[2], "r");
    if (!f) {
      printf("Unable to read file %s", argv[1]);
      return 1;
    }

    line = malloc(sizeof(char)*MAX_COMMAND_LEN);
    while (fgets(line, MAX_COMMAND_LEN, f) != NULL) {
      cmd = strtok(line, " ");

      num_args = 0;
      while ((arg = strtok(NULL, " \n")) != NULL) {
        assert(num_args < MAX_COMMAND_ARGS);

        args[num_args] = atoi(arg);
        num_args++;
      }

      if (strcmp(cmd, "tap") == 0) {
        assert(num_args == 4);
        execute_tap(fd, device_flags, args[0], args[1], args[2], args[3]);
      } else if (strcmp(cmd, "drag") == 0) {
        assert(num_args == 6);
        execute_drag(fd, device_flags, args[0], args[1], args[2],
                     args[3], args[4], args[5]);
      } else if (strcmp(cmd, "sleep") == 0) {
        assert(num_args == 1);
        execute_sleep(args[0]);
      } else if (strcmp(cmd, "pinch") == 0) {
        assert(num_args == 10);
        execute_pinch(fd, device_flags, args[0], args[1], args[2],
                      args[3], args[4], args[5], args[6], args[7], args[8],
                      args[9]);
      } else {
        printf("Unrecognized command: '%s'", cmd);
        return 1;
      }
    }
    free(line);

    return 0;
}