From cb875724da7233ab407f63ddbb98b083147d9fe4 Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Wed, 20 Jun 2012 18:21:42 +0300 Subject: vdagent-x11-randr: factor out set_screen_to_best_size --- src/vdagent-x11-randr.c | 70 +++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 29 deletions(-) diff --git a/src/vdagent-x11-randr.c b/src/vdagent-x11-randr.c index b88bee3..b323095 100644 --- a/src/vdagent-x11-randr.c +++ b/src/vdagent-x11-randr.c @@ -47,18 +47,8 @@ void vdagent_x11_randr_handle_root_size_change(struct vdagent_x11 *x11, vdagent_x11_send_daemon_guest_xorg_res(x11); } -/* - * Set monitor configuration according to client request. - * - * On exit send current configuration to client, regardless of error. - * - * Errors: - * screen size too large for driver to handle. (we set the largest/smallest possible) - * no randr support in X server. - * invalid configuration request from client. - */ -void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, - VDAgentMonitorsConfig *mon_config) +static int set_screen_to_best_size(struct vdagent_x11 *x11, int width, int height, + int *out_width, int *out_height) { int i, num_sizes = 0; int best = -1; @@ -67,28 +57,20 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, XRRScreenConfiguration *config; Rotation rotation; - if (!x11->has_xrandr) - return; - - if (mon_config->num_of_monitors != 1) { - fprintf(x11->errfile, - "Only 1 monitor supported, ignoring additional monitors\n"); - } - sizes = XRRSizes(x11->display, x11->screen, &num_sizes); if (!sizes || !num_sizes) { fprintf(x11->errfile, "XRRSizes failed\n"); - return; + return 0; } /* Find the closest size which will fit within the monitor */ for (i = 0; i < num_sizes; i++) { - if (sizes[i].width > mon_config->monitors[0].width || - sizes[i].height > mon_config->monitors[0].height) + if (sizes[i].width > width || + sizes[i].height > height) continue; /* Too large for the monitor */ - unsigned int wdiff = mon_config->monitors[0].width - sizes[i].width; - unsigned int hdiff = mon_config->monitors[0].height - sizes[i].height; + unsigned int wdiff = width - sizes[i].width; + unsigned int hdiff = height - sizes[i].height; unsigned int diff = wdiff * wdiff + hdiff * hdiff; if (diff < closest_diff) { closest_diff = diff; @@ -98,20 +80,50 @@ void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, if (best == -1) { fprintf(x11->errfile, "no suitable resolution found for monitor\n"); - return; + return 0; } config = XRRGetScreenInfo(x11->display, x11->root_window); if(!config) { fprintf(x11->errfile, "get screen info failed\n"); - return; + return 0; } XRRConfigCurrentConfiguration(config, &rotation); XRRSetScreenConfig(x11->display, config, x11->root_window, best, rotation, CurrentTime); XRRFreeScreenConfigInfo(config); - x11->width = sizes[best].width; - x11->height = sizes[best].height; + + *out_width = sizes[best].width; + *out_height = sizes[best].height; + return 1; +} + +/* + * Set monitor configuration according to client request. + * + * On exit send current configuration to client, regardless of error. + * + * Errors: + * screen size too large for driver to handle. (we set the largest/smallest possible) + * no randr support in X server. + * invalid configuration request from client. + */ +void vdagent_x11_set_monitor_config(struct vdagent_x11 *x11, + VDAgentMonitorsConfig *mon_config) +{ + if (!x11->has_xrandr) + return; + + if (mon_config->num_of_monitors != 1) { + fprintf(x11->errfile, + "Only 1 monitor supported, ignoring additional monitors\n"); + } + + if (!set_screen_to_best_size(x11, mon_config->monitors[0].width, + mon_config->monitors[0].height, + &x11->width, &x11->height)) { + return; + } vdagent_x11_send_daemon_guest_xorg_res(x11); /* Flush output buffers and consume any pending events (ConfigureNotify) */ -- cgit v1.2.3