...
1 package v2
2
3 import (
4 "fmt"
5
6 "edge-infra.dev/pkg/lib/kernel/drm"
7 v1 "edge-infra.dev/pkg/sds/display/k8s/apis/v1"
8 "edge-infra.dev/pkg/sds/lib/set"
9 )
10
11 const (
12
13 UnknownCard = "unknown"
14
15 DisconnectedPort = "disconnected"
16 )
17
18
19 type DisplayConfig struct {
20
21 Displays Displays `json:"displays,omitempty"`
22
23 DPMS *DPMS `json:"dpms,omitempty"`
24
25 Layout Layout `json:"layout,omitempty"`
26
27
28
29
30
31
32
33 V1 *v1.DisplayConfig `json:"v1-displayConfig,omitempty"`
34 }
35
36
37 func (displayConfig *DisplayConfig) DisplayPorts() []DisplayPort {
38 if displayConfig == nil {
39 return []DisplayPort{}
40 }
41 dps := set.OrderedFromSlice(displayConfig.Displays.DisplayPorts())
42 dps.Add(displayConfig.Layout...)
43 return dps.ToSlice()
44 }
45
46
47 func (displayConfig *DisplayConfig) HasV1() bool {
48 return displayConfig != nil && displayConfig.V1 != nil
49 }
50
51
52
53 func (displayConfig *DisplayConfig) HasDisconnected() bool {
54 if displayConfig == nil {
55 return false
56 }
57 for _, display := range displayConfig.Displays {
58 if !display.IsConnected() {
59 return true
60 }
61 }
62 for _, dp := range displayConfig.Layout {
63 if !dp.IsConnected() {
64 return true
65 }
66 }
67 return false
68 }
69
70 type Displays []Display
71
72
73
74 func (displays *Displays) UpdateDisplay(display Display) {
75 if display.DisplayPort == "" {
76 return
77 }
78 if currentDisplay := displays.FindByDisplayPort(display.DisplayPort); currentDisplay != nil {
79 *currentDisplay = display
80 } else {
81 *displays = append(*displays, display)
82 }
83 }
84
85
86 func (displays Displays) FindByDisplayPort(dp DisplayPort) *Display {
87 for idx, display := range displays {
88 if display.DisplayPort == dp {
89 return &displays[idx]
90 }
91 }
92 return nil
93 }
94
95
96 func (displays Displays) FindByMPID(mpid MPID) *Display {
97 for idx, display := range displays {
98 if display.MPID != nil && *display.MPID == mpid {
99 return &displays[idx]
100 }
101 }
102 return nil
103 }
104
105
106 func (displays Displays) FindPrimary() *Display {
107 for _, display := range displays {
108 if display.IsPrimary() {
109 return &display
110 }
111 }
112 return nil
113 }
114
115
116 func (displays Displays) DisplayPorts() []DisplayPort {
117 dps := []DisplayPort{}
118 for _, display := range displays {
119 dps = append(dps, display.DisplayPort)
120 }
121 return dps
122 }
123
124
125 type Display struct {
126
127
128
129
130 DisplayPort `json:"displayPort"`
131
132
133
134
135
136 *MPID `json:"mpid,omitempty"`
137
138 *Primary `json:"primary,omitempty"`
139
140 Orientation *Orientation `json:"orientation,omitempty"`
141
142 *Resolution `json:"resolution,omitempty"`
143
144
145
146
147
148
149 SupportedResolutions []Resolution `json:"supportedResolutions,omitempty"`
150
151 InputDeviceMappings []InputDeviceName `json:"inputDeviceMappings,omitempty"`
152 }
153
154
155
156
157
158
159
160
161
162
163
164 type DisplayPort string
165
166 func NewDisplayPort(card, port string) DisplayPort {
167 return DisplayPort(fmt.Sprintf("%s-%s", card, port))
168 }
169
170 func (dp *DisplayPort) String() string {
171 if dp == nil {
172 return ""
173 }
174 return string(*dp)
175 }
176
177
178
179
180
181 func (dp DisplayPort) IsConnected() bool {
182
183 err := drm.ValidateConnectorName(dp.String())
184 return err == nil
185 }
186
187
188
189
190
191 type MPID string
192
193 func NewMPID(manufacturer string, productCode uint) MPID {
194 return MPID(fmt.Sprintf("%s-%d", manufacturer, productCode))
195 }
196
197 func (mpid *MPID) String() string {
198 if mpid == nil {
199 return ""
200 }
201 return string(*mpid)
202 }
203
204
205 type Primary bool
206
207 func (display *Display) IsPrimary() bool {
208 if display.Primary == nil {
209 return false
210 }
211 return bool(*display.Primary)
212 }
213
214 func (display *Display) SetPrimary(primary bool) {
215 display.Primary = (*Primary)(&primary)
216 }
217
218
219
220
221
222 type Orientation string
223
224 var (
225 NormalOrientation Orientation = "normal"
226 LeftOrientation Orientation = "left"
227 RightOrientation Orientation = "right"
228 InvertedOrientation Orientation = "inverted"
229 )
230
231 func (o *Orientation) String() string {
232 if o == nil {
233 return ""
234 }
235 return string(*o)
236 }
237
238
239 type Resolution struct {
240
241
242
243 Width int `json:"width"`
244
245
246
247 Height int `json:"height"`
248 }
249
250 func (res *Resolution) String() string {
251 if res == nil {
252 return ""
253 }
254 return fmt.Sprintf("%dx%d", res.Width, res.Height)
255 }
256
257
258 type InputDeviceName string
259
260 func (name *InputDeviceName) String() string {
261 if name == nil {
262 return ""
263 }
264 return string(*name)
265 }
266
267
268 type DPMS struct {
269
270 Enabled *bool `json:"enabled,omitempty"`
271
272
273
274
275 BlankTime *int `json:"blankTime,omitempty"`
276
277
278
279
280 StandbyTime *int `json:"standybyTime,omitempty"`
281
282
283
284
285 SuspendTime *int `json:"suspendTime,omitempty"`
286
287
288
289
290 OffTime *int `json:"offTime,omitempty"`
291 }
292
293
294
295 type Layout []DisplayPort
296
View as plain text