File: evdevPlus.cpp

package info (click to toggle)
libevdevplus 0.1.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 132 kB
  • sloc: cpp: 464; makefile: 13
file content (98 lines) | stat: -rw-r--r-- 2,115 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
    This file is part of libevdevPlus.
    Copyright (C) 2018 YukiWorkshop

    This program is free software: you can redistribute it and/or modify
    it under the terms of the MIT License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

#include "evdevPlus.hpp"

using namespace evdevPlus;


void EventDevice::Open(const std::string &path, int open_flags) {
	int fd = open(path.c_str(), open_flags);

	if (fd < 0)
		throw std::runtime_error("failed to open device: " + std::string(strerror(errno)));

	Open(fd);
}

void EventDevice::Open(int fd) {
	if (!IsValidDevice(fd))
		throw std::runtime_error("not a valid event device");

	FD = fd;

	Init();
}

void EventDevice::Close() {
	if (FD != -1)
		close(FD);
}

bool EventDevice::IsValidDevice(int fd) {
	if (fcntl(fd, F_GETFD) == -1)
		return false;

	int _;

	return ioctl(fd, EVIOCGVERSION, &_) == 0;
}

bool EventDevice::IsValidDevice() {
	return IsValidDevice(FD);
}

void EventDevice::Init() {
	if (ioctl(FD, EVIOCGVERSION, &DriverVersion))
		throw std::runtime_error(std::string("EVIOCGVERSION") + " failed");

	if (ioctl(FD, EVIOCGID, &(DeviceID.inputId)))
		throw std::runtime_error(std::string("EVIOCGID") + " failed");

	char name[256] = "Unknown";
	ioctl(FD, EVIOCGNAME(sizeof(name)), name);
	DeviceName = name;


	long evtypes;
	if (ioctl(FD, EVIOCGBIT(0, sizeof(long)), &evtypes) == -1)
		throw std::runtime_error(std::string("EVIOCGBIT") + " failed");

	// TODO
}

void EventDevice::Grab() {
	if (ioctl(FD, EVIOCGRAB, 1))
		throw std::runtime_error(std::string("EVIOCGRAB") + "failed");
}

void EventDevice::Ungrab() {
	if (ioctl(FD, EVIOCGRAB, 0))
		throw std::runtime_error(std::string("EVIOCGRAB") + "failed");
}

InputEvent EventDevice::Read() {
	InputEvent ret;

	ssize_t rc = read(FD, &ret.event, sizeof(input_event));

	if (rc != sizeof(input_event)) {
		if (rc < 0)
			throw std::runtime_error(strerror(errno));
		else
			throw std::runtime_error("truncated read");
	}

	return ret;
}