Skip to content

Commit 2e095a3

Browse files
author
StefanoBelli
committed
PhysicalKeyboard and VirtualKeyboard types for linux input subsystem
1 parent 2c43cb4 commit 2e095a3

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/input.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#define COPY_INPUT_EVENT(from_stream,dest) \
2323
for(unsigned i=0;i<sizeof(struct input_event);i++) \
2424
((char*)&dest)[i] = fgetc(from_stream)
25+
26+
using ERTypePhysicalKeyboard = erebos::input::PhysicalKeyboard;
27+
using ERTypeVirtualKeyboard = erebos::input::VirtualKeyboard;
2528
#endif
2629

2730
using eikey = erebos::input::Key;
@@ -70,14 +73,14 @@ static int get_keyboard_evpath_s(char* target) {
7073
return 0;
7174
}
7275

73-
FILE* erebos::input::getev_physical_keyboard() {
76+
ERTypePhysicalKeyboard erebos::input::getev_physical_keyboard() {
7477
char path[PATH_MAX];
7578
int ret = get_keyboard_evpath_s(path);
7679

7780
return ret ? nullptr : fopen(path,"r");
7881
}
7982

80-
int erebos::input::new_virtual_kb_device(const char* name, const bool& after_wait,
83+
ERTypeVirtualKeyboard erebos::input::new_virtual_kb_device(const char* name, const bool& after_wait,
8184
const u16& bus_type, const u16& vendor,
8285
const u16& product, const u16& version) {
8386
int fd=open("/dev/uinput",O_WRONLY | O_NONBLOCK);
@@ -113,7 +116,7 @@ int erebos::input::new_virtual_kb_device(const char* name, const bool& after_wai
113116
return fd;
114117
}
115118

116-
bool erebos::input::destroy_virtual_kb_device(const int& devfd) {
119+
bool erebos::input::destroy_virtual_kb_device(const VirtualKeyboard& devfd) {
117120
if(ioctl(devfd,UI_DEV_DESTROY) < 0)
118121
return false;
119122

@@ -145,7 +148,7 @@ eikey erebos::input::get_key(const unsigned& delta) {
145148
}
146149
}
147150
#elif defined(LINUX)
148-
eikey erebos::input::get_key(FILE *physkb) {
151+
eikey erebos::input::get_key(PhysicalKeyboard physkb) {
149152
struct input_event current;
150153

151154
while(true) {
@@ -174,7 +177,7 @@ bool erebos::input::fake_put(const Key& key) {
174177
return res != 0;
175178
}
176179
#elif defined(LINUX)
177-
bool erebos::input::fake_put(const int& devfd, const Key& key,
180+
bool erebos::input::fake_put(const VirtualKeyboard& devfd, const Key& key,
178181
const KeyPressType& type) {
179182
static struct input_event event;
180183

src/input.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ namespace erebos {
583583
ERAPI bool fake_put(const Key& key);
584584

585585
#elif defined(LINUX)
586+
using PhysicalKeyboard = FILE*;
587+
using VirtualKeyboard = int;
586588

587589
/*!
588590
* @brief determine which kind of event, see fake_put
@@ -595,14 +597,14 @@ namespace erebos {
595597
/*!
596598
* @return retrieve physical connected keyboard event path (see also get_key)
597599
*/
598-
FILE* getev_physical_keyboard();
600+
PhysicalKeyboard getev_physical_keyboard();
599601

600602
/*!
601603
*
602604
* @param physkb : the opened stream to keyboard event file
603605
* @return pressed key
604606
*/
605-
Key get_key(FILE* physkb);
607+
Key get_key(PhysicalKeyboard physkb);
606608

607609
/*!
608610
*
@@ -614,7 +616,7 @@ namespace erebos {
614616
* @param version (default 0x03)
615617
* @return a new file descriptor, representing the virtual keyboard, -1 otherwise
616618
*/
617-
int new_virtual_kb_device(const char* name,
619+
VirtualKeyboard new_virtual_kb_device(const char* name,
618620
const bool& after_wait = true,
619621
const u16& bus_type = 0x03, //USB
620622
const u16& vendor = 0x01,
@@ -627,21 +629,21 @@ namespace erebos {
627629
* @param type : press or release? (see KeyPressType)
628630
* @return true if key pressed, false otherwise
629631
*/
630-
bool fake_put(const int& devfd, const Key& key, const KeyPressType& type);
632+
bool fake_put(const VirtualKeyboard& devfd, const Key& key, const KeyPressType& type);
631633

632634
/*!
633635
* @param devfd : the virtual keyboard fd
634636
* @return true if correctly destroyed, false otherwise
635637
*/
636-
bool destroy_virtual_kb_device(const int& devfd);
638+
bool destroy_virtual_kb_device(const VirtualKeyboard& devfd);
637639

638640
/*!
639641
*
640642
* @param devfd : the virtual keyboard fd
641643
* @param key : key you wanna put
642644
* @return true if key event emitted, false otherwise
643645
*/
644-
inline bool fake_put(const int& devfd, const Key& key) {
646+
inline bool fake_put(const VirtualKeyboard& devfd, const Key& key) {
645647
if(!fake_put(devfd, key, KeyPressType::KEY_PRESS))
646648
return false;
647649

0 commit comments

Comments
 (0)