Skip to content

Commit

Permalink
Merge pull request bendahl#3 from ghthor/enable-device-naming
Browse files Browse the repository at this point in the history
Enable device naming
  • Loading branch information
Benjamin Dahlmanns authored Aug 29, 2016
2 parents 87dc87b + 5201fc7 commit 76f5ea0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
30 changes: 21 additions & 9 deletions uinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ import (
"unsafe"
)

// VKeyboard represents a virtual keyboard device. There are several
// VKeyboard represents a virtual keyboard device. There are several
// methods available to work with this virtual device. Devices can be
// created, receive events, and closed.
type VKeyboard struct {
// The Name of the uinput device. Will be trimmed to a Max Length of 80 bytes.
// If left blank the device will have a default name.
Name string

id int
}

Expand All @@ -41,7 +45,8 @@ type VKeyboard struct {
func (vk *VKeyboard) Create(path string) (err error) {
vk.id = -1
var ret error
vk.id, ret = createVKeyboardDevice(path)

vk.id, ret = createVKeyboardDevice(path, vk.Name)
return ret
}

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

// Close will close the device and free resources.
// Close will close the device and free resources.
// It's usually a good idea to use defer to call this function.
func (vk * VKeyboard) Close() (err error) {
func (vk *VKeyboard) Close() (err error) {
if vk.id < 0 {
return errors.New("Keyboard not initialized. Closing device failed.")
}
return closeDevice(vk.id)
}

func createVKeyboardDevice(path string) (deviceId int, err error) {
var fd C.int
var deviceName = C.CString(path)
defer C.free(unsafe.Pointer(deviceName))
func createVKeyboardDevice(path, name string) (deviceId int, err error) {
uinputDevice := C.CString(path)
defer C.free(unsafe.Pointer(uinputDevice))

fd = C.initVKeyboardDevice(deviceName)
if name == "" {
name = "uinput_default_vkeyboard"
}
virtDeviceName := C.CString(name)
defer C.free(unsafe.Pointer(virtDeviceName))

var fd C.int
fd = C.initVKeyboardDevice(uinputDevice, virtDeviceName)
if fd < 0 {
// TODO: Map ErrValues into more specific Errors
return 0, errors.New("Could not initialize device.")
}

Expand Down
54 changes: 27 additions & 27 deletions uinputwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,47 @@

#include "uinputwrapper.h"

int initVKeyboardDevice(char* uinputPath) {
int initVKeyboardDevice(char* uinputPath, char* virtDeviceName) {
int i;
int deviceHandle = -1;
struct uinput_user_dev uidev;

deviceHandle = open(uinputPath, O_WRONLY | O_NONBLOCK | O_NDELAY);
if(deviceHandle <= 0) {
return -1;
}

// if a valid handle could be determined, try to enable key events
if(deviceHandle > 0) {
if(ioctl(deviceHandle, UI_SET_EVBIT, EV_KEY) < 0) {
if(releaseDevice(deviceHandle) < 0) {
exit(EXIT_FAILURE);
} else {
deviceHandle = -1;
}
if(ioctl(deviceHandle, UI_SET_EVBIT, EV_KEY) < 0) {
if(releaseDevice(deviceHandle) < 0) {
exit(EXIT_FAILURE);
} else {
// register key events - only values from 1 to 255 are valid
for(i=1; i<256; i++) {
ioctl(deviceHandle, UI_SET_KEYBIT, i);
}

memset(&uidev, 0, sizeof (uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput_vkeyboard");
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x4711;
uidev.id.product = 0x0815;
uidev.id.version = 1;
return -1;
}
}

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

if (ioctl(deviceHandle, UI_DEV_CREATE) < 0) {
exit(EXIT_FAILURE);
}
memset(&uidev, 0, sizeof (uidev));
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s", virtDeviceName);
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x4711;
uidev.id.product = 0x0815;
uidev.id.version = 1;

sleep(2);
if (write(deviceHandle, &uidev, sizeof (uidev)) < 0) {
exit(EXIT_FAILURE);
}

}
if (ioctl(deviceHandle, UI_DEV_CREATE) < 0) {
exit(EXIT_FAILURE);
}

sleep(2);

return deviceHandle;
}

Expand Down
2 changes: 1 addition & 1 deletion uinputwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* virtual keyboard. It will return 0 if successful, otherwise
* -1 will be returned.
*/
int initVKeyboardDevice(char* uinputPath);
int initVKeyboardDevice(char* uinputPath, char* virtDeviceName);

/*
* Send a button event to the virutal keyboard. Possible values for the key
Expand Down

0 comments on commit 76f5ea0

Please sign in to comment.