package display // #cgo CFLAGS: --std=gnu99 -pedantic -Wall // #cgo LDFLAGS: -lX11 -lXext // #include "dpms.h" // #include // #include import "C" import ( "unsafe" "errors" "os" ) // Xset monitors/controls the display using the DPMS X extension type Xset struct {} // Suspend the display func (Xset) Suspend() error { xDisplayName := C.CString(tryGetXDisplay()) defer C.free(unsafe.Pointer(xDisplayName)) err := C.turn_display_off(xDisplayName) if err != C.int(0) { return convertErrorIntToGoError(err) } return nil } func tryGetXDisplay() string { display := os.Getenv("DISPLAY") if display == "" { panic(errors.New("DISPLAY envvar not set")) } return display } func convertErrorIntToGoError(err C.int) error { errorMsg := "" switch err { case C.BADDISPLAY: errorMsg = "Failed to open X display" case C.NODPMSEXTENSION: errorMsg = "DPMS extension is not available" case C.NOTDPMSCAPABLE: errorMsg = "Display is not DPMS capable" case C.UNKNOWNDPMSMODE: errorMsg = "Reported DPMS state unknown" case C.DPMSDISABLED: errorMsg = "DPMS is disabled" default: errorMsg = "Unknown error from dpms.h" } return errors.New(errorMsg) } // IsOn determines if the display is on func (Xset) IsOn() (bool, error) { xDisplayName := C.CString(tryGetXDisplay()) defer C.free(unsafe.Pointer(xDisplayName)) var isDisplayOn C.bool err := C.is_display_on(xDisplayName, &isDisplayOn) if err != C.int(0) { return false, convertErrorIntToGoError(err) } if isDisplayOn { return true, nil } return false, nil }