File: evdevPlus.hpp

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 (113 lines) | stat: -rw-r--r-- 2,458 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
    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.
*/

#ifndef LIBEVDEVPLUS_HPP
#define LIBEVDEVPLUS_HPP

#include "CommonIncludes.hpp"
#include "InputEvent.hpp"

namespace evdevPlus {

	extern const std::unordered_map<std::string, int> Table_FunctionKeys;
	extern const std::unordered_map<std::string, int> Table_ModifierKeys;
	extern const std::unordered_map<char, int> Table_LowerKeys;
	extern const std::unordered_map<char, int> Table_UpperKeys;
	extern const std::unordered_map<std::string, int> Table_KeyCodes;


	class EventDeviceID {
	public:
		input_id inputId{};

		uint16_t &BusType = inputId.bustype;
		uint16_t &Vendor = inputId.vendor;
		uint16_t &Product = inputId.product;
		uint16_t &Version = inputId.version;

		EventDeviceID() = default;
		EventDeviceID(uint16_t bus_type, uint16_t vid, uint16_t pid, uint16_t version) {
			BusType = bus_type;
			Vendor = vid;
			Product = pid;
			Version = version;
		}

		friend void swap(EventDeviceID &first, EventDeviceID &second) {
			using std::swap;

			swap(first.inputId, second.inputId);
		}

		EventDeviceID(EventDeviceID &&other) noexcept : EventDeviceID() {
			swap(*this, other);
		}

		EventDeviceID& operator= (EventDeviceID other) {
			swap(*this, other);
			return *this;
		}

		EventDeviceID(const EventDeviceID &other) {
			memcpy(&inputId, &(other.inputId), sizeof(uinput_setup));
		}

	};


	class EventDevice {
	public:
		int FD = -1;
		std::string Path;

		int DriverVersion = -1;
		EventDeviceID DeviceID;
		std::string DeviceName;
		std::set<int> EventTypes;

		EventDevice() = default;
		EventDevice(const std::string &path, int open_flags = O_RDONLY) {
			Open(path, open_flags);
		}
		EventDevice(int fd) {
			Open(fd);
		}
		~EventDevice() {
			Close();
		}

		void Open(const std::string &path, int open_flags = O_RDONLY);
		void Open(int fd);
		void Close();

		void Init();

		void Grab();
		void Ungrab();

		InputEvent Read();

		static bool IsValidDevice(int fd);
		bool IsValidDevice();


		bool const operator== (const EventDevice &o) const {
			return (Path == o.Path) && (FD == o.FD);
		}

	};
}


#endif //LIBEVDEVPLUS_HPP