コード例 #1
0
ファイル: mouse-dpi-tool.c プロジェクト: chengyake/karch
int
main (int argc, char **argv) {
	int rc;
	int fd;
	const char *path;
	struct libevdev *dev;
	struct measurements measurements = {0};

	if (argc < 2)
		return usage();

	path = argv[1];
	if (path[0] == '-')
		return usage();

	fd = open(path, O_RDONLY|O_NONBLOCK);
	if (fd < 0) {
		fprintf(stderr, "Error opening the device: %s\n", strerror(errno));
		return 1;
	}

	rc = libevdev_new_from_fd(fd, &dev);
	if (rc != 0) {
		fprintf(stderr, "Error fetching the device info: %s\n", strerror(-rc));
		return 1;
	}

	if (libevdev_grab(dev, LIBEVDEV_GRAB) != 0) {
		fprintf(stderr, "Error: cannot grab the device, something else is grabbing it.\n");
		fprintf(stderr, "Use 'fuser -v %s' to find processes with an open fd\n", path);
		return 1;
	}
	libevdev_grab(dev, LIBEVDEV_UNGRAB);

	printf("Mouse %s on %s\n", libevdev_get_name(dev), path);
	printf("Move the device 250mm/10in or more along the x-axis.\n");
	printf("Pause 3 seconds before movement to reset, Ctrl+C to exit.\n");
	setbuf(stdout, NULL);

	rc = mainloop(dev, &measurements);

	printf("\n");

	print_summary(&measurements);

	printf("\n");
	printf("Entry for hwdb match (replace XXX with the resolution in DPI):\n"
	       "mouse:%s:v%04xp%04x:name:%s:\n"
	       " MOUSE_DPI=XXX@%d\n",
	       bustype(libevdev_get_id_bustype(dev)),
	       libevdev_get_id_vendor(dev),
	       libevdev_get_id_product(dev),
	       libevdev_get_name(dev),
	       (int)measurements.max_frequency);

	libevdev_free(dev);
	close(fd);

	return rc;
}
コード例 #2
0
ファイル: joystick_linux.cpp プロジェクト: Scrik/godot
void joystick_linux::open_joystick(const char *p_path) {

	int joy_num = get_free_joy_slot();
	int fd = open(p_path, O_RDONLY | O_NONBLOCK);
	if (fd != -1 && joy_num != -1) {

		int rc = libevdev_new_from_fd(fd, &joysticks[joy_num].dev);
		if (rc < 0) {

			fprintf(stderr, "Failed to init libevdev (%s)\n", strerror(-rc));
			return;
		}

		libevdev *dev = joysticks[joy_num].dev;

		//check if the device supports basic gamepad events, prevents certain keyboards from
		//being detected as joysticks
		if (libevdev_has_event_type(dev, EV_ABS) && libevdev_has_event_type(dev, EV_KEY) &&
				(libevdev_has_event_code(dev, EV_KEY, BTN_A) || libevdev_has_event_code(dev, EV_KEY, BTN_THUMBL) || libevdev_has_event_code(dev, EV_KEY, BTN_TOP))) {

			char uid[128];
			String name = libevdev_get_name(dev);
			uint16_t bus = __bswap_16(libevdev_get_id_bustype(dev));
			uint16_t vendor = __bswap_16(libevdev_get_id_vendor(dev));
			uint16_t product = __bswap_16(libevdev_get_id_product(dev));
			uint16_t version = __bswap_16(libevdev_get_id_version(dev));

			joysticks[joy_num].reset();

			Joystick &joy = joysticks[joy_num];
			joy.fd = fd;
			joy.devpath = String(p_path);
			setup_joystick_properties(joy_num);
			sprintf(uid, "%04x%04x", bus, 0);
			if (vendor && product && version) {

				sprintf(uid + String(uid).length(), "%04x%04x%04x%04x%04x%04x", vendor,0,product,0,version,0);
				input->joy_connection_changed(joy_num, true, name, uid);
			}
			else {
				String uidname = uid;
				int uidlen = MIN(name.length(), 11);
				for (int i=0; i<uidlen; i++) {

					uidname = uidname + _hex_str(name[i]);
				}
				uidname += "00";
				input->joy_connection_changed(joy_num, true, name, uidname);

			}
		}
		else {
			//device is not a gamepad, clean up
			libevdev_free(dev);
			close(fd);
		}
	}
}
コード例 #3
0
END_TEST

START_TEST(device_ids)
{
	struct litest_device *dev = litest_current_device();
	const char *name;
	unsigned int pid, vid;

	name = libevdev_get_name(dev->evdev);
	pid = libevdev_get_id_product(dev->evdev);
	vid = libevdev_get_id_vendor(dev->evdev);

	ck_assert_str_eq(name,
			 libinput_device_get_name(dev->libinput_device));
	ck_assert_int_eq(pid,
			 libinput_device_get_id_product(dev->libinput_device));
	ck_assert_int_eq(vid,
			 libinput_device_get_id_vendor(dev->libinput_device));
}
コード例 #4
0
ファイル: evmpd.c プロジェクト: hawx/evmpd
int main(int argc, char const *argv[])
{
  if (argc < 5) {
    printf("Usage: evmpd HOSTNAME PORT DEVICE HALT_COMMAND\n");
    return EXIT_FAILURE;
  }

  struct mpd_connection *client = connect(argv[1], strtol(argv[2], NULL, 10));

  struct libevdev *dev = NULL;
  int fd;
  int rc = 1;

  fd = open(argv[3], O_RDONLY | O_NONBLOCK);
  rc = libevdev_new_from_fd(fd, &dev);
  if (rc < 0) {
    fprintf(stderr, "Failed to init libevdev (%d)\n", strerror(-rc));
    return EXIT_FAILURE;
  }
  printf("Input device name: \"%s\"\n", libevdev_get_name(dev));
  printf("Input device ID: bus %#x vendor %#x product %#x\n",
         libevdev_get_id_bustype(dev),
         libevdev_get_id_vendor(dev), libevdev_get_id_product(dev));

  do {
    struct input_event ev;

    rc = libevdev_next_event(dev, LIBEVDEV_READ_FLAG_NORMAL, &ev);

    if (rc == 0 && ev.value == 0) {
      if (libevdev_event_is_code(&ev, EV_KEY, KEY_F4)) {
        system(argv[4]);
        printf("shutdown\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_PLAYPAUSE)) {
        struct mpd_status *status = mpd_run_status(client);
        enum mpd_state state = mpd_status_get_state(status);

        if (state != MPD_STATE_PLAY && state != MPD_STATE_PAUSE) {
          mpd_run_play(client);
          printf("play\n");
        } else {
          mpd_run_toggle_pause(client);
          printf("toggle-pause\n");
        }

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_STOPCD)) {
        mpd_run_stop(client);
        printf("stop\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_PREVIOUSSONG)) {
        mpd_run_previous(client);
        printf("previous\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_NEXTSONG)) {
        mpd_run_next(client);
        printf("next\n");

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_VOLUMEUP)) {
        struct mpd_status *status = mpd_run_status(client);
        int volume = mpd_status_get_volume(status);

        volume += 5;
        if (volume > 100) {
          volume = 100;
        }

        mpd_run_set_volume(client, volume);
        printf("set-volume %d\n", volume);

      } else if (libevdev_event_is_code(&ev, EV_KEY, KEY_VOLUMEDOWN)) {
        struct mpd_status *status = mpd_run_status(client);
        int volume = mpd_status_get_volume(status);

        volume -= 5;
        if (volume < 0) {
          volume = 0;
        }

        mpd_run_set_volume(client, volume);
        printf("set-volume %d\n", volume);
      } else {
        printf("Event: %s %s %d\n",
               libevdev_event_type_get_name(ev.type),
               libevdev_event_code_get_name(ev.type, ev.code),
               ev.value);
      }
    }

  }
  while (rc == LIBEVDEV_READ_STATUS_SUCCESS ||
         rc == LIBEVDEV_READ_STATUS_SYNC ||
         rc == -EAGAIN);

  printf("rc: %d\n", rc);

  if (client != NULL) {
    mpd_connection_free(client);
  }
  return EXIT_SUCCESS;
}