Skip to content

Commit

Permalink
Simplify parameter checking and use panic in order to fail fast in ca…
Browse files Browse the repository at this point in the history
…se of errors
  • Loading branch information
bendahl committed Apr 22, 2018
1 parent 9669b1d commit 3eeb385
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions uinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ device. Here are a few examples of how to use the virtual touch pad:
package uinput

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -187,12 +186,8 @@ type vTouchPad struct {
// CreateTouchPad will create a new touch pad device. note that you will need to define the x and y axis boundaries
// (min and max) within which the cursor maybe moved around.
func CreateTouchPad(path string, name []byte, minX int32, maxX int32, minY int32, maxY int32) (TouchPad, error) {
if path == "" {
return nil, errors.New("device path must not be empty")
}
if len(name) > uinputMaxNameSize {
return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
}
validateDevicePath(path)
validateUinputName(name)

fd, err := createTouchPad(path, name, minX, maxX, minY, maxY)
if err != nil {
Expand Down Expand Up @@ -303,12 +298,8 @@ func (vTouch vTouchPad) Close() error {
// CreateMouse will create a new mouse input device. A mouse is a device that allows relative input.
// Relative input means that all changes to the x and y coordinates of the mouse pointer will be
func CreateMouse(path string, name []byte) (Mouse, error) {
if path == "" {
return nil, errors.New("device path must not be empty")
}
if len(name) > uinputMaxNameSize {
return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
}
validateDevicePath(path)
validateUinputName(name)

fd, err := createMouse(path, name)
if err != nil {
Expand Down Expand Up @@ -438,12 +429,8 @@ func (vRel vMouse) Close() error {
// CreateKeyboard will create a new keyboard using the given uinput
// device path of the uinput device.
func CreateKeyboard(path string, name []byte) (Keyboard, error) {
if path == "" {
return nil, errors.New("device path must not be empty")
}
if len(name) > uinputMaxNameSize {
return nil, fmt.Errorf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize)
}
validateDevicePath(path)
validateUinputName(name)

fd, err := createVKeyboardDevice(path, name)
if err != nil {
Expand Down Expand Up @@ -509,3 +496,15 @@ func (vk vKeyboard) KeyUp(key int) error {
func (vk vKeyboard) Close() error {
return closeDevice(vk.deviceFile)
}

func validateDevicePath(path string) {
if path == "" {
panic("device path must not be empty")
}
}

func validateUinputName(name []byte) {
if len(name) > uinputMaxNameSize {
panic(fmt.Sprintf("device name %s is too long (maximum of %d characters allowed)", name, uinputMaxNameSize))
}
}

0 comments on commit 3eeb385

Please sign in to comment.