From 0de8ffc773c709bf41055f6139524aef7246f944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 13 Mar 2023 17:55:41 +0100 Subject: [PATCH] update screen rate set. (#34) --- pkg/xorg/xorg.c | 27 ++------------------------- pkg/xorg/xorg.go | 30 ++++++++++++++++++++++-------- pkg/xorg/xorg.h | 2 +- 3 files changed, 25 insertions(+), 34 deletions(-) diff --git a/pkg/xorg/xorg.c b/pkg/xorg/xorg.c index 6ef6f8e9..d70e5aaa 100644 --- a/pkg/xorg/xorg.c +++ b/pkg/xorg/xorg.c @@ -229,7 +229,7 @@ void XKey(KeySym keysym, int down) { XSync(display, 0); } -Status XSetScreenConfiguration(int width, int height, short *rate) { +Status XSetScreenConfiguration(int width, int height, short rate) { Display *display = getXDisplay(); Window root = RootWindow(display, 0); XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root); @@ -251,31 +251,8 @@ Status XSetScreenConfiguration(int width, int height, short *rate) { return RRSetConfigFailed; } - short current_rate = 0; - if (rate != NULL) { - short *rates; - int num_rates; - rates = XRRConfigRates(conf, size_index, &num_rates); - - // try to find the nearest rate - short nearest_rate = 0; - float diff = 0; - for (int i = 0; i < num_rates; i++) { - if (nearest_rate == 0 || abs(rates[i] - *rate) < diff) { - nearest_rate = rates[i]; - diff = abs(rates[i] - *rate); - } - } - - if (nearest_rate != 0 && diff < 10) { - current_rate = nearest_rate; - } - - *rate = current_rate; - } - Status status; - status = XRRSetScreenConfigAndRate(display, conf, root, size_index, RR_Rotate_0, current_rate, CurrentTime); + status = XRRSetScreenConfigAndRate(display, conf, root, size_index, RR_Rotate_0, rate, CurrentTime); XRRFreeScreenConfigInfo(conf); return status; diff --git a/pkg/xorg/xorg.go b/pkg/xorg/xorg.go index 703aa49d..2be47a66 100644 --- a/pkg/xorg/xorg.go +++ b/pkg/xorg/xorg.go @@ -192,23 +192,37 @@ func ChangeScreenSize(width int, height int, rate int16) (int, int, int16, error // round width to 8, because of Xorg width = width - (width % 8) + // if rate is 0, set it to 60 + if rate == 0 { + rate = 60 + } + // convert variables to C types c_width, c_height, c_rate := C.int(width), C.int(height), C.short(rate) // if screen configuration already exists, just set it - if status := C.XSetScreenConfiguration(c_width, c_height, &c_rate); status == C.RRSetConfigSuccess { - return width, height, int16(c_rate), nil + status := C.XSetScreenConfiguration(c_width, c_height, c_rate) + if status != C.RRSetConfigSuccess { + // create new screen configuration + C.XCreateScreenMode(c_width, c_height, c_rate) + + // screen configuration should exist now, set it + status = C.XSetScreenConfiguration(c_width, c_height, c_rate) } - // create new screen configuration - C.XCreateScreenMode(c_width, c_height, c_rate) + var err error - // screen configuration should exist now, set it - if status := C.XSetScreenConfiguration(c_width, c_height, &c_rate); status == C.RRSetConfigSuccess { - return width, height, int16(c_rate), nil + // if screen configuration was not set successfully, return error + if status != C.RRSetConfigSuccess { + err = fmt.Errorf("unknown screen configuration %dx%d@%d", width, height, rate) } - return 0, 0, 0, fmt.Errorf("unknown screen configuration %dx%d@%d", width, height, rate) + // if specified rate is not supported a BadValue error is returned + if status == C.BadValue { + err = fmt.Errorf("unsupported screen rate %d", rate) + } + + return width, height, rate, err } func GetScreenSize() types.ScreenSize { diff --git a/pkg/xorg/xorg.h b/pkg/xorg/xorg.h index a6e750d7..4bca692c 100644 --- a/pkg/xorg/xorg.h +++ b/pkg/xorg/xorg.h @@ -36,7 +36,7 @@ static KeyCode XKeyEntryGet(KeySym keysym); static KeyCode XkbKeysymToKeycode(Display *dpy, KeySym keysym); void XKey(KeySym keysym, int down); -Status XSetScreenConfiguration(int width, int height, short *rate); +Status XSetScreenConfiguration(int width, int height, short rate); void XGetScreenConfiguration(int *width, int *height, short *rate); void XGetScreenConfigurations(); void XCreateScreenMode(int width, int height, short rate);