forked from tmackay/g13
-
Notifications
You must be signed in to change notification settings - Fork 0
/
g13_device.hpp
159 lines (106 loc) · 3.45 KB
/
g13_device.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// Created by khampf on 07-05-2020.
//
#ifndef G13_G13_DEVICE_HPP
#define G13_G13_DEVICE_HPP
#include "g13_lcd.hpp"
#include "g13_manager.hpp"
#include "g13_profile.hpp"
#include "g13_stick.hpp"
#include <functional>
#include <libusb-1.0/libusb.h>
#include <linux/uinput.h>
#include <map>
#include <memory>
namespace G13 {
// *************************************************************************
class G13_Profile;
class G13_Action;
class G13_Manager;
class G13_Font;
typedef std::shared_ptr<G13_Profile> ProfilePtr;
typedef std::shared_ptr<G13_Action> G13_ActionPtr;
typedef std::shared_ptr<G13_Font> FontPtr;
const size_t G13_NUM_KEYS = 40;
class G13_Device {
public:
G13_Device(libusb_device *dev, libusb_context *ctx,
libusb_device_handle *handle, int m_id);
~G13_Device();
G13_LCD &lcd() { return m_lcd; }
// [[nodiscard]] const G13_LCD &lcd() const { return m_lcd; }
G13_Stick &stick() { return m_stick; }
// [[nodiscard]] const G13_Stick &stick() const { return m_stick; }
FontPtr SwitchToFont(const std::string &name);
void SwitchToProfile(const std::string &name);
ProfilePtr Profile(const std::string &name);
void Dump(std::ostream &o, int detail = 0);
void Command(char const *str);
void ReadCommandsFromPipe();
void ReadConfigFile(const std::string &filename);
int ReadKeypresses();
void parse_joystick(unsigned char *buf);
G13_ActionPtr MakeAction(const std::string &action);
void SetKeyColor(int red, int green, int blue);
void SetModeLeds(int leds);
void SendEvent(int type, int code, int val);
void OutputPipeWrite(const std::string &out) const;
void LcdWrite(unsigned char *data, size_t size);
// bool is_set(int key);
bool update(int key, bool v);
// used by G13_Manager
void Cleanup();
void RegisterContext(libusb_context *libusbContext);
void LcdWriteFile(const std::string &filename);
G13_Font ¤t_font() { return *m_currentFont; }
// G13_Profile ¤t_profile() { return *m_currentProfile; }
[[nodiscard]] int id_within_manager() const { return m_id_within_manager; }
static std::string DescribeLibusbErrorCode(int code);
// typedef boost::function<void(const char*)> COMMAND_FUNCTION;
typedef std::function<void(const char *)> COMMAND_FUNCTION;
typedef std::map<std::string, COMMAND_FUNCTION> CommandFunctionTable;
/*
void setManager(G13_Manager manager) {
_manager = manager;
}
*/
// libusb_device_handle *Handle() const;
libusb_device *Device() const;
protected:
void InitFonts();
void LcdInit();
void InitCommands();
// typedef void (COMMAND_FUNCTION)( G13_Device*, const char *, const char * );
CommandFunctionTable _command_table;
// struct timeval _event_time;
struct input_event m_event {};
int m_id_within_manager;
libusb_context *m_ctx;
int m_uinput_fid;
int m_input_pipe_fid{};
std::string m_input_pipe_name;
int m_output_pipe_fid{};
std::string m_output_pipe_name;
std::map<std::string, FontPtr> pFonts;
FontPtr m_currentFont;
std::map<std::string, ProfilePtr> m_profiles;
ProfilePtr m_currentProfile;
G13_LCD m_lcd;
G13_Stick m_stick;
bool keys[G13_NUM_KEYS]{};
private:
libusb_device_handle *handle;
libusb_device *device;
};
/*
inline bool G13_Device::is_set(int key) {
return keys[key];
}
*/
inline bool G13_Device::update(int key, bool v) {
bool old = keys[key];
keys[key] = v;
return old != v;
}
} // namespace G13
#endif // G13_G13_DEVICE_HPP