update screen rate set. (#34)

This commit is contained in:
Miroslav Šedivý 2023-03-13 17:55:41 +01:00 committed by GitHub
parent 17bfd2d58f
commit 0de8ffc773
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 34 deletions

View File

@ -229,7 +229,7 @@ void XKey(KeySym keysym, int down) {
XSync(display, 0); XSync(display, 0);
} }
Status XSetScreenConfiguration(int width, int height, short *rate) { Status XSetScreenConfiguration(int width, int height, short rate) {
Display *display = getXDisplay(); Display *display = getXDisplay();
Window root = RootWindow(display, 0); Window root = RootWindow(display, 0);
XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root); XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root);
@ -251,31 +251,8 @@ Status XSetScreenConfiguration(int width, int height, short *rate) {
return RRSetConfigFailed; 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 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); XRRFreeScreenConfigInfo(conf);
return status; return status;

View File

@ -192,23 +192,37 @@ func ChangeScreenSize(width int, height int, rate int16) (int, int, int16, error
// round width to 8, because of Xorg // round width to 8, because of Xorg
width = width - (width % 8) width = width - (width % 8)
// if rate is 0, set it to 60
if rate == 0 {
rate = 60
}
// convert variables to C types // convert variables to C types
c_width, c_height, c_rate := C.int(width), C.int(height), C.short(rate) c_width, c_height, c_rate := C.int(width), C.int(height), C.short(rate)
// if screen configuration already exists, just set it // if screen configuration already exists, just set it
if status := C.XSetScreenConfiguration(c_width, c_height, &c_rate); status == C.RRSetConfigSuccess { status := C.XSetScreenConfiguration(c_width, c_height, c_rate)
return width, height, int16(c_rate), nil if status != C.RRSetConfigSuccess {
}
// create new screen configuration // create new screen configuration
C.XCreateScreenMode(c_width, c_height, c_rate) C.XCreateScreenMode(c_width, c_height, c_rate)
// screen configuration should exist now, set it // screen configuration should exist now, set it
if status := C.XSetScreenConfiguration(c_width, c_height, &c_rate); status == C.RRSetConfigSuccess { status = C.XSetScreenConfiguration(c_width, c_height, c_rate)
return width, height, int16(c_rate), nil
} }
return 0, 0, 0, fmt.Errorf("unknown screen configuration %dx%d@%d", width, height, rate) var err error
// 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)
}
// 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 { func GetScreenSize() types.ScreenSize {

View File

@ -36,7 +36,7 @@ static KeyCode XKeyEntryGet(KeySym keysym);
static KeyCode XkbKeysymToKeycode(Display *dpy, KeySym keysym); static KeyCode XkbKeysymToKeycode(Display *dpy, KeySym keysym);
void XKey(KeySym keysym, int down); 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 XGetScreenConfiguration(int *width, int *height, short *rate);
void XGetScreenConfigurations(); void XGetScreenConfigurations();
void XCreateScreenMode(int width, int height, short rate); void XCreateScreenMode(int width, int height, short rate);