package v1 import ( "errors" "fmt" ) var ( errStandByLongerThanSuspendTime = errors.New("stand-by time cannot be longer than suspend time") errSuspendLongerThanOffTime = errors.New("suspend time cannot be longer than off time") ) // Validates the DisplayConfig, checking: // - Supported resolutions are not configured in spec. // - Only one display is configured as the primary. // - DPMS configuration is valid (standby-time < suspend-time < off-time). func (displayConfig *DisplayConfig) Validate() error { if displayConfig == nil { return nil } if err := validateNonSettableFields(displayConfig); err != nil { return err } if err := validatePrimary(displayConfig); err != nil { return err } return validateDPMS(displayConfig) } // Displays cannot configure supported resolution. func validateNonSettableFields(displayConfig *DisplayConfig) error { for mpid, display := range displayConfig.Displays { if display.SupportedResolutions != nil { return fmt.Errorf("display %s specified 'supportedResolutions': resolution should be configured via the 'resolution' field", mpid) } } return nil } // Only one display can be the primary. func validatePrimary(displayConfig *DisplayConfig) error { primaries := []MPID{} for mpid, display := range displayConfig.Displays { if display.IsPrimary() { primaries = append(primaries, mpid) } } if len(primaries) > 1 { return fmt.Errorf("there can only be one primary, found %d: %v", len(primaries), primaries) } return nil } // Validates DPMS configuration. // // Where these fields are set, they must be configured so // standby-time < suspend-time < off-time. // // If a field is not set, it is assumed to be 0. func validateDPMS(displayConfig *DisplayConfig) error { if displayConfig.DPMS == nil { return nil } var standybyTime, suspendTimeTime, offTimeTime int if displayConfig.DPMS.StandbyTime != nil { standybyTime = *displayConfig.DPMS.StandbyTime } if displayConfig.DPMS.SuspendTime != nil { suspendTimeTime = *displayConfig.DPMS.SuspendTime } if displayConfig.DPMS.OffTime != nil { offTimeTime = *displayConfig.DPMS.OffTime } if standybyTime > suspendTimeTime { return errStandByLongerThanSuspendTime } else if suspendTimeTime > offTimeTime { return errSuspendLongerThanOffTime } return nil }