Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use linux input Bus enum with udev data #172

Merged
merged 1 commit into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions pkg/hwinfo/input.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package hwinfo

import (
"errors"
"fmt"
"log/slog"

"github.com/numtide/nixos-facter/pkg/udev"

"github.com/numtide/nixos-facter/pkg/linux/input"
)
Expand All @@ -10,15 +14,26 @@ import (
// error.
// It accepts a deviceIdx to ensure it continues on from the last device index generated by hwinfo.
func captureTouchpads(deviceIdx uint) ([]HardwareDevice, error) {
inputDevices, err := input.ReadDevices(nil, true)
inputDevices, err := input.ReadDevices(nil)
if err != nil {
return nil, fmt.Errorf("failed to read input devices: %w", err)
}

var result []HardwareDevice //nolint:prealloc

for _, inputDevice := range inputDevices {
if !inputDevice.Udev.Input.IsTouchpad {

path := "/sys" + inputDevice.Sysfs

udevData, err := udev.Read(path)
if errors.Is(err, udev.ErrNotFound) {
slog.Warn("udev data not found", "name", inputDevice.Name, "sysfs", inputDevice.Sysfs)
continue
} else if err != nil {
return nil, fmt.Errorf("failed to fetch udev data for device %q with udev data: %w", path, err)
}

if !udevData.Input.IsTouchpad {
// currently, we are only interested in touchpads, as hwinfo does not capture them
// eventually, we may take over more of the input processing that hwinfo performs
continue
Expand All @@ -35,11 +50,11 @@ func captureTouchpads(deviceIdx uint) ([]HardwareDevice, error) {
Class: HardwareClassMouse,
BaseClass: NewBaseClassID(BaseClassTouchpad),
Vendor: &ID{
Name: inputDevice.Udev.Vendor,
Name: udevData.Vendor,
Value: inputDevice.Vendor,
},
Device: &ID{
Name: inputDevice.Udev.Model,
Name: udevData.Model,
Value: inputDevice.Product,
},
SysfsID: inputDevice.Sysfs,
Expand Down
18 changes: 1 addition & 17 deletions pkg/linux/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package input

import (
"bufio"
"errors"
"fmt"
"io"
"log/slog"
"os"
"regexp"
"strconv"
"strings"

"github.com/numtide/nixos-facter/pkg/udev"
)

//go:generate enumer -type=Bus -json -text -trimprefix Bus -output=./input_bus.go
Expand Down Expand Up @@ -74,7 +70,6 @@ type Device struct {
Sysfs string
Phys string
Capabilities map[string]string
Udev *udev.Udev
}

func (d *Device) Path() string {
Expand All @@ -101,7 +96,7 @@ func (d *Device) EventHandler() string {
return ""
}

func ReadDevices(r io.ReadCloser, udevAnnotate bool) ([]*Device, error) {
func ReadDevices(r io.ReadCloser) ([]*Device, error) {
var err error

if r == nil {
Expand All @@ -123,17 +118,6 @@ func ReadDevices(r io.ReadCloser, udevAnnotate bool) ([]*Device, error) {
line := scanner.Text()

if line == "" {
// try to append udev data
if udevAnnotate {
device.Udev, err = udev.Read("/sys" + device.Sysfs)

if errors.Is(err, udev.ErrNotFound) {
slog.Warn("udev data not found", "name", device.Name, "sysfs", device.Sysfs)
} else if err != nil {
return nil, fmt.Errorf("failed to annotate with udev data: %w", err)
}
}

devices = append(devices, device)
device = nil

Expand Down
2 changes: 1 addition & 1 deletion pkg/linux/input/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestReadDevices(t *testing.T) {
as := require.New(t)
r := io.NopCloser(bytes.NewReader([]byte(devices)))

devices, err := input.ReadDevices(r, false)
devices, err := input.ReadDevices(r)
as.NoError(err)

expected := []*input.Device{
Expand Down
27 changes: 4 additions & 23 deletions pkg/udev/udev.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,8 @@ import (
"fmt"
"strconv"
"unsafe"
)

//go:generate enumer -type=Bus -json -text -transform=snake -trimprefix Bus -output=./udev_bus.go
type Bus int //nolint:recvcheck

const (
BusAta Bus = iota // ATA (IDE) devices
BusBluetooth
BusI8042
BusI2c // Inter-Integrated Circuit
BusIeee1394 // Firewire
BusPci
BusPciExpress
BusPcmcia // Personal Computer Memory Card International Association
BusPlatform // Devices on a system's motherboard
BusScsi
BusSerial
BusSerio // Serial interface controller devices
BusSpi // Serial Peripheral Interface connected devices
BusUsb
BusVirtio
"github.com/numtide/nixos-facter/pkg/linux/input"
)

//go:generate enumer -type=Type -json -text -trimprefix Type -output=./udev_type.go
Expand Down Expand Up @@ -210,7 +191,7 @@ func NewUdevPci(env map[string]string) (*Pci, error) {
}

type Udev struct {
Bus Bus
Bus input.Bus
Type Type
Model string
ModelID uint16
Expand Down Expand Up @@ -270,11 +251,11 @@ func NewUdev(env map[string]string) (*Udev, error) {
var err error

switch result.Bus {
case BusUsb:
case input.BusUsb:
if result.Usb, err = NewUdevUsb(env); err != nil {
return nil, fmt.Errorf("failed to parse usb: %w", err)
}
case BusPci:
case input.BusPci:
if result.Pci, err = NewUdevPci(env); err != nil {
return nil, fmt.Errorf("failed to parse pci: %w", err)
}
Expand Down
160 changes: 0 additions & 160 deletions pkg/udev/udev_bus.go

This file was deleted.