/
676767ap
/
AstroCatGame
Обзор
Документация
Войти
/
676767ap
/
AstroCatGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
resolution/linux_set.go
65 строк
2 KB
Anton Polikapov
Swithcing to 1920x1080 resolution
01 мар 2024, 16:27
01 мар 2024, 16:27
ad9a4d7
Код
Авторство
О чём код?
package resolution // #cgo LDFLAGS: -lX11 -lXrandr -lstdc++ // #include <X11/Xlib.h> // #include <X11/extensions/Xrandr.h> import "C" import ( "fmt" "unsafe" ) func (m *MonitorLinux) SetResolution(width, height int) error { // Get the main display of X server. display := C.XOpenDisplay(nil) // Get the ID of the root window. window := C.XRootWindow(display, 0) // Get the information about the display. conf := C.XRRGetScreenInfo(display, window) // Get the rotation of the display. var originalRotation C.Rotation C.XRRConfigCurrentConfiguration(conf, &originalRotation) // Get the array of resolutions. var numSizes C.int resolutions := C.XRRSizes(display, 0, &numSizes) // Turn it into a Go slice. goResolutions := (*[1 << 30]C.XRRScreenSize)( unsafe.Pointer(resolutions))[:numSizes:numSizes] // Get all the frequency rates for each resolution. resRates := make([][]C.short, numSizes) for i := 0; i < int(numSizes); i++ { var numRates C.int rates := C.XRRRates(display, 0, C.int(i), &numRates) goRates := (*[1 << 30]C.short)( unsafe.Pointer(rates))[:numSizes:numSizes] // Add the array of rates to the table // of rates for resolutions. resRates[i] = goRates } // Check if the requested resolution // is supported by the display. resInd := -1 for i := 0; i < int(numSizes); i++ { if int(goResolutions[i].width) == width && int(goResolutions[i].height) == height { resInd = i break } } if resInd < 0 { return fmt.Errorf("resolution %dx%d is not supported by the display", width, height) } m.Res.Height = height m.Res.Width = width m.ResIdx = resInd C.XRRSetScreenConfigAndRate(display, conf, window, C.int(resInd), originalRotation, resRates[resInd][0], C.CurrentTime) return nil }