...

Source file src/edge-infra.dev/pkg/sds/display/displaymanager/applier/example/example_applier.go

Documentation: edge-infra.dev/pkg/sds/display/displaymanager/applier/example

     1  package example
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"slices"
     7  
     8  	ctrl "sigs.k8s.io/controller-runtime"
     9  	"sigs.k8s.io/yaml"
    10  
    11  	"edge-infra.dev/pkg/sds/display/displaymanager/applier"
    12  	examplereader "edge-infra.dev/pkg/sds/display/displaymanager/reader/example"
    13  	v2 "edge-infra.dev/pkg/sds/display/k8s/apis/v2"
    14  )
    15  
    16  // Returns a new DisplayApplier which simply logs the display configuration to be applied.
    17  func NewExampleDisplayApplier(withLogging bool) applier.DisplayApplier {
    18  	return &exampleApplier{
    19  		withLogging: withLogging,
    20  	}
    21  }
    22  
    23  type exampleApplier struct {
    24  	withLogging bool
    25  }
    26  
    27  func (a *exampleApplier) Apply(ctx context.Context, displayConfig *v2.DisplayConfig) (*v2.DisplayConfig, error) {
    28  	log := ctrl.LoggerFrom(ctx)
    29  
    30  	filterInputDeviceMappings(displayConfig, examplereader.HostInputDevices)
    31  
    32  	bytes, err := yaml.Marshal(displayConfig)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	if a.withLogging {
    38  		log.Info("applying display configuration", "DisplayConfig", displayConfig)
    39  		fmt.Printf("\nApplying display configuration:\n\n%s\n", string(bytes))
    40  	}
    41  
    42  	return displayConfig, nil
    43  }
    44  
    45  // Removes mappings from the display config where a matching input device does not exist,
    46  // or it was already mapped to another display.
    47  func filterInputDeviceMappings(displayConfig *v2.DisplayConfig, inputDevices []v2.InputDeviceName) {
    48  	for _, display := range displayConfig.Displays {
    49  		displayInputDevices := []v2.InputDeviceName{}
    50  
    51  		for _, inputDevice := range display.InputDeviceMappings {
    52  			if slices.Contains(inputDevices, inputDevice) {
    53  				displayInputDevices = append(displayInputDevices, inputDevice)
    54  			}
    55  		}
    56  
    57  		display.InputDeviceMappings = displayInputDevices
    58  		displayConfig.Displays.UpdateDisplay(display)
    59  	}
    60  }
    61  

View as plain text