From 5201fc705bb5b7a96ff47144e20bf48e790bc7ab Mon Sep 17 00:00:00 2001 From: Wilhelmina Drengwitz Date: Thu, 25 Aug 2016 16:29:15 -0400 Subject: [PATCH] add a user-defined name for VKeyBoard --- uinput.go | 30 +++++++++++++++++++++--------- uinputwrapper.c | 4 ++-- uinputwrapper.h | 2 +- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/uinput.go b/uinput.go index c1684f5..d9f33ec 100644 --- a/uinput.go +++ b/uinput.go @@ -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 } @@ -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 } @@ -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.") } diff --git a/uinputwrapper.c b/uinputwrapper.c index 18b28db..38f6ea1 100755 --- a/uinputwrapper.c +++ b/uinputwrapper.c @@ -14,7 +14,7 @@ #include "uinputwrapper.h" -int initVKeyboardDevice(char* uinputPath) { +int initVKeyboardDevice(char* uinputPath, char* virtDeviceName) { int i; int deviceHandle = -1; struct uinput_user_dev uidev; @@ -39,7 +39,7 @@ int initVKeyboardDevice(char* uinputPath) { } memset(&uidev, 0, sizeof (uidev)); - snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput_vkeyboard"); + snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s", virtDeviceName); uidev.id.bustype = BUS_USB; uidev.id.vendor = 0x4711; uidev.id.product = 0x0815; diff --git a/uinputwrapper.h b/uinputwrapper.h index df64ab1..9305f9c 100755 --- a/uinputwrapper.h +++ b/uinputwrapper.h @@ -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