...

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

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

     1  package xset
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command"
     7  	"edge-infra.dev/pkg/sds/display/displaymanager/applier/xorg/command/runner"
     8  )
     9  
    10  const xsetPath = "/usr/bin/xset"
    11  
    12  const (
    13  	dpmsCmd        = "dpms"
    14  	dpmsEnableCmd  = "+dpms"
    15  	dpmsDisableCmd = "-dpms"
    16  	screenSaverCmd = "s"
    17  	blankCmd       = "blank"
    18  )
    19  
    20  // Command is able to build an Xset command to configure DPMS and run it.
    21  type Command interface {
    22  	command.Command
    23  
    24  	SetDPMS(enabled bool)
    25  	SetBlankTimeSeconds(seconds int)
    26  	SetDPMSSettings(standBySeconds, suspendTimeSeconds, offTimeSeconds int)
    27  }
    28  
    29  // NewXsetCommand returns a new xset.Command implementation.
    30  func NewXsetCommand(r runner.Runner) Command {
    31  	return &xsetCmd{
    32  		cmdRunner: r,
    33  		cmds:      cmds{},
    34  	}
    35  }
    36  
    37  // Slice of args for each xset command
    38  type cmds [][]string
    39  
    40  type xsetCmd struct {
    41  	cmdRunner runner.Runner
    42  	cmds
    43  }
    44  
    45  func (x *xsetCmd) Path() string {
    46  	return xsetPath
    47  }
    48  
    49  func (x *xsetCmd) Run() error {
    50  	for _, args := range x.cmds {
    51  		if err := x.cmdRunner.Run(xsetPath, args...); err != nil {
    52  			return err
    53  		}
    54  	}
    55  	return nil
    56  }
    57  
    58  func (x *xsetCmd) SetDPMS(enabled bool) {
    59  	if enabled {
    60  		// e.g. xset +dpms
    61  		args := []string{dpmsEnableCmd}
    62  		x.cmds = append(x.cmds, args)
    63  	} else {
    64  		// e.g. xset -dpms
    65  		args := []string{dpmsDisableCmd}
    66  		x.cmds = append(x.cmds, args)
    67  	}
    68  }
    69  
    70  func (x *xsetCmd) SetDPMSSettings(standBySeconds, suspendTimeSeconds, offTimeSeconds int) {
    71  	// e.g. xset dpms 30 60 90
    72  	args := []string{dpmsCmd, fmt.Sprint(standBySeconds), fmt.Sprint(suspendTimeSeconds), fmt.Sprint(offTimeSeconds)}
    73  	x.cmds = append(x.cmds, args)
    74  }
    75  
    76  func (x *xsetCmd) SetBlankTimeSeconds(seconds int) {
    77  	// e.g. xset s blank && xset s 30
    78  	blankArgs := []string{screenSaverCmd, blankCmd}
    79  	x.cmds = append(x.cmds, blankArgs)
    80  	delayArgs := []string{screenSaverCmd, fmt.Sprint(seconds)}
    81  	x.cmds = append(x.cmds, delayArgs)
    82  }
    83  

View as plain text