...

Source file src/edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command/xinput/xinput.go

Documentation: edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command/xinput

     1  package xinput
     2  
     3  import (
     4  	"slices"
     5  
     6  	"maps"
     7  
     8  	"edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command"
     9  	"edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command/runner"
    10  	"edge-infra.dev/pkg/sds/lib/xorg"
    11  )
    12  
    13  const (
    14  	xinputPath     = "/usr/bin/xinput"
    15  	mapToOutputCmd = "map-to-output"
    16  )
    17  
    18  // Command is able to build an xinput command to configure input devices and run it.
    19  type Command interface {
    20  	command.Command
    21  
    22  	MapToOutput(outputID xorg.OutputID, inputID xorg.InputDeviceID)
    23  }
    24  
    25  // NewXinputCommand returns a new xinput.Command implementation.
    26  func NewXinputCommand(r runner.Runner) Command {
    27  	return &xinputCmd{
    28  		cmdRunner:   r,
    29  		displayCmds: displayCmds{},
    30  	}
    31  }
    32  
    33  // Map of display IDs to the display's xinit commands
    34  type displayCmds map[xorg.OutputID][][]string
    35  
    36  type xinputCmd struct {
    37  	cmdRunner runner.Runner
    38  	displayCmds
    39  }
    40  
    41  func (x *xinputCmd) Path() string {
    42  	return xinputPath
    43  }
    44  
    45  func (x *xinputCmd) Run() error {
    46  	// iterate over the sorted keys for consistent output
    47  	outputIDs := slices.Collect(maps.Keys(x.displayCmds))
    48  	slices.Sort(outputIDs)
    49  	for _, outputID := range outputIDs {
    50  		for _, args := range x.displayCmds[outputID] {
    51  			if err := x.cmdRunner.Run(xinputPath, args...); err != nil {
    52  				return err
    53  			}
    54  		}
    55  	}
    56  	return nil
    57  }
    58  
    59  func (x *xinputCmd) MapToOutput(outputID xorg.OutputID, inputID xorg.InputDeviceID) {
    60  	// e.g. xinput map-to-output "Elo Touch Solutions" HDMI1
    61  	x.displayCmds[outputID] = append(
    62  		x.displayCmds[outputID],
    63  		[]string{mapToOutputCmd, inputID.String(), outputID.String()},
    64  	)
    65  }
    66  

View as plain text