1
2 package dpms
3
4
5
6 import (
7 "github.com/jezek/xgb"
8
9 "github.com/jezek/xgb/xproto"
10 )
11
12
13 func Init(c *xgb.Conn) error {
14 reply, err := xproto.QueryExtension(c, 4, "DPMS").Reply()
15 switch {
16 case err != nil:
17 return err
18 case !reply.Present:
19 return xgb.Errorf("No extension named DPMS could be found on on the server.")
20 }
21
22 c.ExtLock.Lock()
23 c.Extensions["DPMS"] = reply.MajorOpcode
24 c.ExtLock.Unlock()
25 for evNum, fun := range xgb.NewExtEventFuncs["DPMS"] {
26 xgb.NewEventFuncs[int(reply.FirstEvent)+evNum] = fun
27 }
28 for errNum, fun := range xgb.NewExtErrorFuncs["DPMS"] {
29 xgb.NewErrorFuncs[int(reply.FirstError)+errNum] = fun
30 }
31 return nil
32 }
33
34 func init() {
35 xgb.NewExtEventFuncs["DPMS"] = make(map[int]xgb.NewEventFun)
36 xgb.NewExtErrorFuncs["DPMS"] = make(map[int]xgb.NewErrorFun)
37 }
38
39 const (
40 DPMSModeOn = 0
41 DPMSModeStandby = 1
42 DPMSModeSuspend = 2
43 DPMSModeOff = 3
44 )
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 type CapableCookie struct {
72 *xgb.Cookie
73 }
74
75
76
77 func Capable(c *xgb.Conn) CapableCookie {
78 c.ExtLock.RLock()
79 defer c.ExtLock.RUnlock()
80 if _, ok := c.Extensions["DPMS"]; !ok {
81 panic("Cannot issue request 'Capable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
82 }
83 cookie := c.NewCookie(true, true)
84 c.NewRequest(capableRequest(c), cookie)
85 return CapableCookie{cookie}
86 }
87
88
89
90 func CapableUnchecked(c *xgb.Conn) CapableCookie {
91 c.ExtLock.RLock()
92 defer c.ExtLock.RUnlock()
93 if _, ok := c.Extensions["DPMS"]; !ok {
94 panic("Cannot issue request 'Capable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
95 }
96 cookie := c.NewCookie(false, true)
97 c.NewRequest(capableRequest(c), cookie)
98 return CapableCookie{cookie}
99 }
100
101
102 type CapableReply struct {
103 Sequence uint16
104 Length uint32
105
106 Capable bool
107
108 }
109
110
111 func (cook CapableCookie) Reply() (*CapableReply, error) {
112 buf, err := cook.Cookie.Reply()
113 if err != nil {
114 return nil, err
115 }
116 if buf == nil {
117 return nil, nil
118 }
119 return capableReply(buf), nil
120 }
121
122
123 func capableReply(buf []byte) *CapableReply {
124 v := new(CapableReply)
125 b := 1
126
127 b += 1
128
129 v.Sequence = xgb.Get16(buf[b:])
130 b += 2
131
132 v.Length = xgb.Get32(buf[b:])
133 b += 4
134
135 if buf[b] == 1 {
136 v.Capable = true
137 } else {
138 v.Capable = false
139 }
140 b += 1
141
142 b += 23
143
144 return v
145 }
146
147
148
149 func capableRequest(c *xgb.Conn) []byte {
150 size := 4
151 b := 0
152 buf := make([]byte, size)
153
154 c.ExtLock.RLock()
155 buf[b] = c.Extensions["DPMS"]
156 c.ExtLock.RUnlock()
157 b += 1
158
159 buf[b] = 1
160 b += 1
161
162 xgb.Put16(buf[b:], uint16(size/4))
163 b += 2
164
165 return buf
166 }
167
168
169 type DisableCookie struct {
170 *xgb.Cookie
171 }
172
173
174
175 func Disable(c *xgb.Conn) DisableCookie {
176 c.ExtLock.RLock()
177 defer c.ExtLock.RUnlock()
178 if _, ok := c.Extensions["DPMS"]; !ok {
179 panic("Cannot issue request 'Disable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
180 }
181 cookie := c.NewCookie(false, false)
182 c.NewRequest(disableRequest(c), cookie)
183 return DisableCookie{cookie}
184 }
185
186
187
188 func DisableChecked(c *xgb.Conn) DisableCookie {
189 c.ExtLock.RLock()
190 defer c.ExtLock.RUnlock()
191 if _, ok := c.Extensions["DPMS"]; !ok {
192 panic("Cannot issue request 'Disable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
193 }
194 cookie := c.NewCookie(true, false)
195 c.NewRequest(disableRequest(c), cookie)
196 return DisableCookie{cookie}
197 }
198
199
200
201 func (cook DisableCookie) Check() error {
202 return cook.Cookie.Check()
203 }
204
205
206
207 func disableRequest(c *xgb.Conn) []byte {
208 size := 4
209 b := 0
210 buf := make([]byte, size)
211
212 c.ExtLock.RLock()
213 buf[b] = c.Extensions["DPMS"]
214 c.ExtLock.RUnlock()
215 b += 1
216
217 buf[b] = 5
218 b += 1
219
220 xgb.Put16(buf[b:], uint16(size/4))
221 b += 2
222
223 return buf
224 }
225
226
227 type EnableCookie struct {
228 *xgb.Cookie
229 }
230
231
232
233 func Enable(c *xgb.Conn) EnableCookie {
234 c.ExtLock.RLock()
235 defer c.ExtLock.RUnlock()
236 if _, ok := c.Extensions["DPMS"]; !ok {
237 panic("Cannot issue request 'Enable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
238 }
239 cookie := c.NewCookie(false, false)
240 c.NewRequest(enableRequest(c), cookie)
241 return EnableCookie{cookie}
242 }
243
244
245
246 func EnableChecked(c *xgb.Conn) EnableCookie {
247 c.ExtLock.RLock()
248 defer c.ExtLock.RUnlock()
249 if _, ok := c.Extensions["DPMS"]; !ok {
250 panic("Cannot issue request 'Enable' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
251 }
252 cookie := c.NewCookie(true, false)
253 c.NewRequest(enableRequest(c), cookie)
254 return EnableCookie{cookie}
255 }
256
257
258
259 func (cook EnableCookie) Check() error {
260 return cook.Cookie.Check()
261 }
262
263
264
265 func enableRequest(c *xgb.Conn) []byte {
266 size := 4
267 b := 0
268 buf := make([]byte, size)
269
270 c.ExtLock.RLock()
271 buf[b] = c.Extensions["DPMS"]
272 c.ExtLock.RUnlock()
273 b += 1
274
275 buf[b] = 4
276 b += 1
277
278 xgb.Put16(buf[b:], uint16(size/4))
279 b += 2
280
281 return buf
282 }
283
284
285 type ForceLevelCookie struct {
286 *xgb.Cookie
287 }
288
289
290
291 func ForceLevel(c *xgb.Conn, PowerLevel uint16) ForceLevelCookie {
292 c.ExtLock.RLock()
293 defer c.ExtLock.RUnlock()
294 if _, ok := c.Extensions["DPMS"]; !ok {
295 panic("Cannot issue request 'ForceLevel' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
296 }
297 cookie := c.NewCookie(false, false)
298 c.NewRequest(forceLevelRequest(c, PowerLevel), cookie)
299 return ForceLevelCookie{cookie}
300 }
301
302
303
304 func ForceLevelChecked(c *xgb.Conn, PowerLevel uint16) ForceLevelCookie {
305 c.ExtLock.RLock()
306 defer c.ExtLock.RUnlock()
307 if _, ok := c.Extensions["DPMS"]; !ok {
308 panic("Cannot issue request 'ForceLevel' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
309 }
310 cookie := c.NewCookie(true, false)
311 c.NewRequest(forceLevelRequest(c, PowerLevel), cookie)
312 return ForceLevelCookie{cookie}
313 }
314
315
316
317 func (cook ForceLevelCookie) Check() error {
318 return cook.Cookie.Check()
319 }
320
321
322
323 func forceLevelRequest(c *xgb.Conn, PowerLevel uint16) []byte {
324 size := 8
325 b := 0
326 buf := make([]byte, size)
327
328 c.ExtLock.RLock()
329 buf[b] = c.Extensions["DPMS"]
330 c.ExtLock.RUnlock()
331 b += 1
332
333 buf[b] = 6
334 b += 1
335
336 xgb.Put16(buf[b:], uint16(size/4))
337 b += 2
338
339 xgb.Put16(buf[b:], PowerLevel)
340 b += 2
341
342 return buf
343 }
344
345
346 type GetTimeoutsCookie struct {
347 *xgb.Cookie
348 }
349
350
351
352 func GetTimeouts(c *xgb.Conn) GetTimeoutsCookie {
353 c.ExtLock.RLock()
354 defer c.ExtLock.RUnlock()
355 if _, ok := c.Extensions["DPMS"]; !ok {
356 panic("Cannot issue request 'GetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
357 }
358 cookie := c.NewCookie(true, true)
359 c.NewRequest(getTimeoutsRequest(c), cookie)
360 return GetTimeoutsCookie{cookie}
361 }
362
363
364
365 func GetTimeoutsUnchecked(c *xgb.Conn) GetTimeoutsCookie {
366 c.ExtLock.RLock()
367 defer c.ExtLock.RUnlock()
368 if _, ok := c.Extensions["DPMS"]; !ok {
369 panic("Cannot issue request 'GetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
370 }
371 cookie := c.NewCookie(false, true)
372 c.NewRequest(getTimeoutsRequest(c), cookie)
373 return GetTimeoutsCookie{cookie}
374 }
375
376
377 type GetTimeoutsReply struct {
378 Sequence uint16
379 Length uint32
380
381 StandbyTimeout uint16
382 SuspendTimeout uint16
383 OffTimeout uint16
384
385 }
386
387
388 func (cook GetTimeoutsCookie) Reply() (*GetTimeoutsReply, error) {
389 buf, err := cook.Cookie.Reply()
390 if err != nil {
391 return nil, err
392 }
393 if buf == nil {
394 return nil, nil
395 }
396 return getTimeoutsReply(buf), nil
397 }
398
399
400 func getTimeoutsReply(buf []byte) *GetTimeoutsReply {
401 v := new(GetTimeoutsReply)
402 b := 1
403
404 b += 1
405
406 v.Sequence = xgb.Get16(buf[b:])
407 b += 2
408
409 v.Length = xgb.Get32(buf[b:])
410 b += 4
411
412 v.StandbyTimeout = xgb.Get16(buf[b:])
413 b += 2
414
415 v.SuspendTimeout = xgb.Get16(buf[b:])
416 b += 2
417
418 v.OffTimeout = xgb.Get16(buf[b:])
419 b += 2
420
421 b += 18
422
423 return v
424 }
425
426
427
428 func getTimeoutsRequest(c *xgb.Conn) []byte {
429 size := 4
430 b := 0
431 buf := make([]byte, size)
432
433 c.ExtLock.RLock()
434 buf[b] = c.Extensions["DPMS"]
435 c.ExtLock.RUnlock()
436 b += 1
437
438 buf[b] = 2
439 b += 1
440
441 xgb.Put16(buf[b:], uint16(size/4))
442 b += 2
443
444 return buf
445 }
446
447
448 type GetVersionCookie struct {
449 *xgb.Cookie
450 }
451
452
453
454 func GetVersion(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
455 c.ExtLock.RLock()
456 defer c.ExtLock.RUnlock()
457 if _, ok := c.Extensions["DPMS"]; !ok {
458 panic("Cannot issue request 'GetVersion' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
459 }
460 cookie := c.NewCookie(true, true)
461 c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
462 return GetVersionCookie{cookie}
463 }
464
465
466
467 func GetVersionUnchecked(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) GetVersionCookie {
468 c.ExtLock.RLock()
469 defer c.ExtLock.RUnlock()
470 if _, ok := c.Extensions["DPMS"]; !ok {
471 panic("Cannot issue request 'GetVersion' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
472 }
473 cookie := c.NewCookie(false, true)
474 c.NewRequest(getVersionRequest(c, ClientMajorVersion, ClientMinorVersion), cookie)
475 return GetVersionCookie{cookie}
476 }
477
478
479 type GetVersionReply struct {
480 Sequence uint16
481 Length uint32
482
483 ServerMajorVersion uint16
484 ServerMinorVersion uint16
485 }
486
487
488 func (cook GetVersionCookie) Reply() (*GetVersionReply, error) {
489 buf, err := cook.Cookie.Reply()
490 if err != nil {
491 return nil, err
492 }
493 if buf == nil {
494 return nil, nil
495 }
496 return getVersionReply(buf), nil
497 }
498
499
500 func getVersionReply(buf []byte) *GetVersionReply {
501 v := new(GetVersionReply)
502 b := 1
503
504 b += 1
505
506 v.Sequence = xgb.Get16(buf[b:])
507 b += 2
508
509 v.Length = xgb.Get32(buf[b:])
510 b += 4
511
512 v.ServerMajorVersion = xgb.Get16(buf[b:])
513 b += 2
514
515 v.ServerMinorVersion = xgb.Get16(buf[b:])
516 b += 2
517
518 return v
519 }
520
521
522
523 func getVersionRequest(c *xgb.Conn, ClientMajorVersion uint16, ClientMinorVersion uint16) []byte {
524 size := 8
525 b := 0
526 buf := make([]byte, size)
527
528 c.ExtLock.RLock()
529 buf[b] = c.Extensions["DPMS"]
530 c.ExtLock.RUnlock()
531 b += 1
532
533 buf[b] = 0
534 b += 1
535
536 xgb.Put16(buf[b:], uint16(size/4))
537 b += 2
538
539 xgb.Put16(buf[b:], ClientMajorVersion)
540 b += 2
541
542 xgb.Put16(buf[b:], ClientMinorVersion)
543 b += 2
544
545 return buf
546 }
547
548
549 type InfoCookie struct {
550 *xgb.Cookie
551 }
552
553
554
555 func Info(c *xgb.Conn) InfoCookie {
556 c.ExtLock.RLock()
557 defer c.ExtLock.RUnlock()
558 if _, ok := c.Extensions["DPMS"]; !ok {
559 panic("Cannot issue request 'Info' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
560 }
561 cookie := c.NewCookie(true, true)
562 c.NewRequest(infoRequest(c), cookie)
563 return InfoCookie{cookie}
564 }
565
566
567
568 func InfoUnchecked(c *xgb.Conn) InfoCookie {
569 c.ExtLock.RLock()
570 defer c.ExtLock.RUnlock()
571 if _, ok := c.Extensions["DPMS"]; !ok {
572 panic("Cannot issue request 'Info' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
573 }
574 cookie := c.NewCookie(false, true)
575 c.NewRequest(infoRequest(c), cookie)
576 return InfoCookie{cookie}
577 }
578
579
580 type InfoReply struct {
581 Sequence uint16
582 Length uint32
583
584 PowerLevel uint16
585 State bool
586
587 }
588
589
590 func (cook InfoCookie) Reply() (*InfoReply, error) {
591 buf, err := cook.Cookie.Reply()
592 if err != nil {
593 return nil, err
594 }
595 if buf == nil {
596 return nil, nil
597 }
598 return infoReply(buf), nil
599 }
600
601
602 func infoReply(buf []byte) *InfoReply {
603 v := new(InfoReply)
604 b := 1
605
606 b += 1
607
608 v.Sequence = xgb.Get16(buf[b:])
609 b += 2
610
611 v.Length = xgb.Get32(buf[b:])
612 b += 4
613
614 v.PowerLevel = xgb.Get16(buf[b:])
615 b += 2
616
617 if buf[b] == 1 {
618 v.State = true
619 } else {
620 v.State = false
621 }
622 b += 1
623
624 b += 21
625
626 return v
627 }
628
629
630
631 func infoRequest(c *xgb.Conn) []byte {
632 size := 4
633 b := 0
634 buf := make([]byte, size)
635
636 c.ExtLock.RLock()
637 buf[b] = c.Extensions["DPMS"]
638 c.ExtLock.RUnlock()
639 b += 1
640
641 buf[b] = 7
642 b += 1
643
644 xgb.Put16(buf[b:], uint16(size/4))
645 b += 2
646
647 return buf
648 }
649
650
651 type SetTimeoutsCookie struct {
652 *xgb.Cookie
653 }
654
655
656
657 func SetTimeouts(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) SetTimeoutsCookie {
658 c.ExtLock.RLock()
659 defer c.ExtLock.RUnlock()
660 if _, ok := c.Extensions["DPMS"]; !ok {
661 panic("Cannot issue request 'SetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
662 }
663 cookie := c.NewCookie(false, false)
664 c.NewRequest(setTimeoutsRequest(c, StandbyTimeout, SuspendTimeout, OffTimeout), cookie)
665 return SetTimeoutsCookie{cookie}
666 }
667
668
669
670 func SetTimeoutsChecked(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) SetTimeoutsCookie {
671 c.ExtLock.RLock()
672 defer c.ExtLock.RUnlock()
673 if _, ok := c.Extensions["DPMS"]; !ok {
674 panic("Cannot issue request 'SetTimeouts' using the uninitialized extension 'DPMS'. dpms.Init(connObj) must be called first.")
675 }
676 cookie := c.NewCookie(true, false)
677 c.NewRequest(setTimeoutsRequest(c, StandbyTimeout, SuspendTimeout, OffTimeout), cookie)
678 return SetTimeoutsCookie{cookie}
679 }
680
681
682
683 func (cook SetTimeoutsCookie) Check() error {
684 return cook.Cookie.Check()
685 }
686
687
688
689 func setTimeoutsRequest(c *xgb.Conn, StandbyTimeout uint16, SuspendTimeout uint16, OffTimeout uint16) []byte {
690 size := 12
691 b := 0
692 buf := make([]byte, size)
693
694 c.ExtLock.RLock()
695 buf[b] = c.Extensions["DPMS"]
696 c.ExtLock.RUnlock()
697 b += 1
698
699 buf[b] = 3
700 b += 1
701
702 xgb.Put16(buf[b:], uint16(size/4))
703 b += 2
704
705 xgb.Put16(buf[b:], StandbyTimeout)
706 b += 2
707
708 xgb.Put16(buf[b:], SuspendTimeout)
709 b += 2
710
711 xgb.Put16(buf[b:], OffTimeout)
712 b += 2
713
714 return buf
715 }
716
View as plain text