...

Source file src/edge-infra.dev/pkg/sds/display/k8s/apis/v2/displayconfig_convert.go

Documentation: edge-infra.dev/pkg/sds/display/k8s/apis/v2

     1  package v2
     2  
     3  import (
     4  	"fmt"
     5  
     6  	v1 "edge-infra.dev/pkg/sds/display/k8s/apis/v1"
     7  )
     8  
     9  // Converts a V1 DisplayConfig to V2.
    10  //
    11  // As the identifier for displays moves from MPID to DisplayPort, the conversion
    12  // requires the current display configuration read from the node. Using this
    13  // we can tell which port displays identified by MPID are connected to.
    14  //
    15  // For displays which have been specified in the V1 display config, but are not
    16  // connected at the time of the conversion, their DisplayPort will be marked as
    17  // disconnected. A map of disconnected DisplayPorts to MPIDs will also be
    18  // returned, so they can later be converted later on if the display is reconnected.
    19  func DisplayConfigFromV1(src *v1.DisplayConfig, displayConfig *DisplayConfig) (result *DisplayConfig, disconnectedIDs map[DisplayPort]MPID) {
    20  	if src == nil {
    21  		return nil, nil
    22  	}
    23  
    24  	ids, disconnectedIDs := convertMPIDs(src, displayConfig)
    25  
    26  	return &DisplayConfig{
    27  		Displays: convertDisplaysFromV1(src, ids),
    28  		Layout:   convertLayoutFromV1(src, ids),
    29  		DPMS:     convertDPMSFromV1(src),
    30  	}, disconnectedIDs
    31  }
    32  
    33  func convertMPIDs(src *v1.DisplayConfig, displayConfig *DisplayConfig) (ids map[v1.MPID]DisplayPort, disconnectedIDs map[DisplayPort]MPID) {
    34  	// keep track of disconnected displays so we can retain them after conversion
    35  	disconnectedCount := 0
    36  
    37  	ids = map[v1.MPID]DisplayPort{}
    38  	disconnectedIDs = map[DisplayPort]MPID{}
    39  
    40  	for _, mpid := range src.MPIDs() {
    41  		if display := displayConfig.Displays.FindByMPID(MPID(mpid)); display != nil {
    42  			ids[mpid] = display.DisplayPort
    43  		} else {
    44  			disconnectedCount++
    45  			port := fmt.Sprintf("%s-%d", DisconnectedPort, disconnectedCount)
    46  			dp := NewDisplayPort(UnknownCard, port)
    47  			ids[mpid] = dp
    48  			disconnectedIDs[dp] = MPID(mpid)
    49  		}
    50  	}
    51  
    52  	return ids, disconnectedIDs
    53  }
    54  
    55  func convertDisplaysFromV1(src *v1.DisplayConfig, ids map[v1.MPID]DisplayPort) Displays {
    56  	if src.Displays == nil {
    57  		return nil
    58  	}
    59  
    60  	displays := Displays{}
    61  	for _, mpid := range src.Displays.MPIDs() {
    62  		display := src.Displays[mpid]
    63  		displays.UpdateDisplay(convertDisplayFromV1(display, ids[mpid]))
    64  	}
    65  
    66  	return displays
    67  }
    68  
    69  func convertDisplayFromV1(src v1.Display, dp DisplayPort) Display {
    70  	return Display{
    71  		DisplayPort:         dp,
    72  		Primary:             (*Primary)(src.Primary),
    73  		Orientation:         (*Orientation)(src.Orientation),
    74  		Resolution:          (*Resolution)(src.Resolution),
    75  		InputDeviceMappings: convertInputDeviceMappingsFromV1(src.InputDeviceMappings),
    76  	}
    77  }
    78  
    79  func convertInputDeviceMappingsFromV1(src []v1.InputDeviceName) []InputDeviceName {
    80  	var inputDeviceMappings []InputDeviceName
    81  	for _, inputDeviceName := range src {
    82  		inputDeviceMappings = append(inputDeviceMappings, InputDeviceName(inputDeviceName))
    83  	}
    84  	return inputDeviceMappings
    85  }
    86  
    87  func convertLayoutFromV1(src *v1.DisplayConfig, ids map[v1.MPID]DisplayPort) Layout {
    88  	if src.Layout == nil {
    89  		return nil
    90  	}
    91  
    92  	layout := Layout{}
    93  	for _, mpid := range src.Layout {
    94  		layout = append(layout, ids[mpid])
    95  	}
    96  
    97  	return layout
    98  }
    99  
   100  func convertDPMSFromV1(src *v1.DisplayConfig) *DPMS {
   101  	if src.DPMS == nil {
   102  		return nil
   103  	}
   104  
   105  	return &DPMS{
   106  		Enabled:     src.DPMS.Enabled,
   107  		BlankTime:   src.DPMS.BlankTime,
   108  		StandbyTime: src.DPMS.StandbyTime,
   109  		SuspendTime: src.DPMS.SuspendTime,
   110  		OffTime:     src.DPMS.OffTime,
   111  	}
   112  }
   113  

View as plain text