Skip to content

Commit 76f5ea0

Browse files
author
Benjamin Dahlmanns
authored
Merge pull request bendahl#3 from ghthor/enable-device-naming
Enable device naming
2 parents 87dc87b + 5201fc7 commit 76f5ea0

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

uinput.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ import (
2828
"unsafe"
2929
)
3030

31-
// VKeyboard represents a virtual keyboard device. There are several
31+
// VKeyboard represents a virtual keyboard device. There are several
3232
// methods available to work with this virtual device. Devices can be
3333
// created, receive events, and closed.
3434
type VKeyboard struct {
35+
// The Name of the uinput device. Will be trimmed to a Max Length of 80 bytes.
36+
// If left blank the device will have a default name.
37+
Name string
38+
3539
id int
3640
}
3741

@@ -41,7 +45,8 @@ type VKeyboard struct {
4145
func (vk *VKeyboard) Create(path string) (err error) {
4246
vk.id = -1
4347
var ret error
44-
vk.id, ret = createVKeyboardDevice(path)
48+
49+
vk.id, ret = createVKeyboardDevice(path, vk.Name)
4550
return ret
4651
}
4752

@@ -65,22 +70,29 @@ func (vk *VKeyboard) SendKeyRelease(key int) (err error) {
6570
return sendBtnEvent(vk.id, key, 0)
6671
}
6772

68-
// Close will close the device and free resources.
73+
// Close will close the device and free resources.
6974
// It's usually a good idea to use defer to call this function.
70-
func (vk * VKeyboard) Close() (err error) {
75+
func (vk *VKeyboard) Close() (err error) {
7176
if vk.id < 0 {
7277
return errors.New("Keyboard not initialized. Closing device failed.")
7378
}
7479
return closeDevice(vk.id)
7580
}
7681

77-
func createVKeyboardDevice(path string) (deviceId int, err error) {
78-
var fd C.int
79-
var deviceName = C.CString(path)
80-
defer C.free(unsafe.Pointer(deviceName))
82+
func createVKeyboardDevice(path, name string) (deviceId int, err error) {
83+
uinputDevice := C.CString(path)
84+
defer C.free(unsafe.Pointer(uinputDevice))
8185

82-
fd = C.initVKeyboardDevice(deviceName)
86+
if name == "" {
87+
name = "uinput_default_vkeyboard"
88+
}
89+
virtDeviceName := C.CString(name)
90+
defer C.free(unsafe.Pointer(virtDeviceName))
91+
92+
var fd C.int
93+
fd = C.initVKeyboardDevice(uinputDevice, virtDeviceName)
8394
if fd < 0 {
95+
// TODO: Map ErrValues into more specific Errors
8496
return 0, errors.New("Could not initialize device.")
8597
}
8698

uinputwrapper.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,47 @@
1414

1515
#include "uinputwrapper.h"
1616

17-
int initVKeyboardDevice(char* uinputPath) {
17+
int initVKeyboardDevice(char* uinputPath, char* virtDeviceName) {
1818
int i;
1919
int deviceHandle = -1;
2020
struct uinput_user_dev uidev;
2121

2222
deviceHandle = open(uinputPath, O_WRONLY | O_NONBLOCK | O_NDELAY);
23+
if(deviceHandle <= 0) {
24+
return -1;
25+
}
2326

2427
// if a valid handle could be determined, try to enable key events
25-
if(deviceHandle > 0) {
26-
if(ioctl(deviceHandle, UI_SET_EVBIT, EV_KEY) < 0) {
27-
if(releaseDevice(deviceHandle) < 0) {
28-
exit(EXIT_FAILURE);
29-
} else {
30-
deviceHandle = -1;
31-
}
28+
if(ioctl(deviceHandle, UI_SET_EVBIT, EV_KEY) < 0) {
29+
if(releaseDevice(deviceHandle) < 0) {
30+
exit(EXIT_FAILURE);
3231
} else {
33-
// register key events - only values from 1 to 255 are valid
34-
for(i=1; i<256; i++) {
35-
ioctl(deviceHandle, UI_SET_KEYBIT, i);
36-
}
37-
38-
memset(&uidev, 0, sizeof (uidev));
39-
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput_vkeyboard");
40-
uidev.id.bustype = BUS_USB;
41-
uidev.id.vendor = 0x4711;
42-
uidev.id.product = 0x0815;
43-
uidev.id.version = 1;
32+
return -1;
33+
}
34+
}
4435

45-
if (write(deviceHandle, &uidev, sizeof (uidev)) < 0) {
46-
exit(EXIT_FAILURE);
47-
}
36+
// register key events - only values from 1 to 255 are valid
37+
for(i=1; i<256; i++) {
38+
ioctl(deviceHandle, UI_SET_KEYBIT, i);
39+
}
4840

49-
if (ioctl(deviceHandle, UI_DEV_CREATE) < 0) {
50-
exit(EXIT_FAILURE);
51-
}
41+
memset(&uidev, 0, sizeof (uidev));
42+
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s", virtDeviceName);
43+
uidev.id.bustype = BUS_USB;
44+
uidev.id.vendor = 0x4711;
45+
uidev.id.product = 0x0815;
46+
uidev.id.version = 1;
5247

53-
sleep(2);
48+
if (write(deviceHandle, &uidev, sizeof (uidev)) < 0) {
49+
exit(EXIT_FAILURE);
50+
}
5451

55-
}
52+
if (ioctl(deviceHandle, UI_DEV_CREATE) < 0) {
53+
exit(EXIT_FAILURE);
5654
}
5755

56+
sleep(2);
57+
5858
return deviceHandle;
5959
}
6060

uinputwrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* virtual keyboard. It will return 0 if successful, otherwise
2222
* -1 will be returned.
2323
*/
24-
int initVKeyboardDevice(char* uinputPath);
24+
int initVKeyboardDevice(char* uinputPath, char* virtDeviceName);
2525

2626
/*
2727
* Send a button event to the virutal keyboard. Possible values for the key

0 commit comments

Comments
 (0)