Merge pull request #227 from Rio6/refactor-background-drawing

Refactor background drawing
This commit is contained in:
Raymond Li 2021-06-04 18:43:50 -04:00 committed by GitHub
commit 3435465d60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 55 deletions

View File

@ -1105,74 +1105,62 @@ void render_lock(uint32_t *resolution, xcb_drawable_t drawable) {
/** /**
* Draws the configured image on the provided context. The image is drawn centered on all monitors, tiled, or just * Draws the configured image on the provided context. The image is drawn centered on all monitors, tiled, or just
* painted starting from 0,0. * painted starting from 0,0. It is also scaled if bg_type is FILL, MAX, or SCALE.
*/ */
void draw_image(uint32_t* root_resolution, cairo_surface_t *img, cairo_t* xcb_ctx) { void draw_image(uint32_t* root_resolution, cairo_surface_t *img, cairo_t* xcb_ctx) {
if (bg_type == NONE) {
// Don't do any image manipulation
cairo_set_source_surface(xcb_ctx, img, 0, 0);
cairo_paint(xcb_ctx);
return;
}
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img);
cairo_pattern_set_extend(pattern, bg_type == TILE ? CAIRO_EXTEND_REPEAT : CAIRO_EXTEND_NONE);
cairo_set_source(xcb_ctx, pattern);
double image_width = cairo_image_surface_get_width(img); double image_width = cairo_image_surface_get_width(img);
double image_height = cairo_image_surface_get_height(img); double image_height = cairo_image_surface_get_height(img);
switch (bg_type) { for (int i = 0; i < xr_screens; i++) {
case CENTER: // Find out scaling factors using bg_type and aspect ratios
case FILL: double scale_x = 1, scale_y = 1;
case SCALE: if (bg_type == SCALE) {
case MAX: scale_x = xr_resolutions[i].width / image_width;
for (int i = 0; i < xr_screens; i++) { scale_y = xr_resolutions[i].height / image_height;
cairo_save(xcb_ctx);
// Scale image according to bg_type and aspect ratios } else if (bg_type == MAX || bg_type == FILL) {
double scale_x = 1, scale_y = 1; double aspect_diff = (double) xr_resolutions[i].height / xr_resolutions[i].width - image_height / image_width;
if (bg_type == SCALE) { if((bg_type == MAX && aspect_diff > 0) || (bg_type == FILL && aspect_diff < 0)) {
scale_x = xr_resolutions[i].width / image_width; scale_x = scale_y = xr_resolutions[i].width / image_width;
scale_y = xr_resolutions[i].height / image_height; } else if ((bg_type == MAX && aspect_diff < 0) || (bg_type == FILL && aspect_diff > 0)) {
} else { scale_x = scale_y = xr_resolutions[i].height / image_height;
double aspect_diff = (double) xr_resolutions[i].height / xr_resolutions[i].width - image_height / image_width;
if((bg_type == MAX && aspect_diff > 0) || (bg_type == FILL && aspect_diff < 0)) {
scale_x = scale_y = xr_resolutions[i].width / image_width;
} else if ((bg_type == MAX && aspect_diff < 0) || (bg_type == FILL && aspect_diff > 0)) {
scale_x = scale_y = xr_resolutions[i].height / image_height;
}
}
if (scale_x != 0 || scale_y != 0) {
cairo_scale(xcb_ctx, scale_x, scale_y);
}
// Place image in the middle
double origin_x = (xr_resolutions[i].x + xr_resolutions[i].width / 2) / scale_x - image_width / 2;
double origin_y = (xr_resolutions[i].y + xr_resolutions[i].height / 2) / scale_y - image_height / 2;
cairo_set_source_surface(xcb_ctx, img, origin_x, origin_y);
cairo_paint(xcb_ctx);
cairo_restore(xcb_ctx);
} }
break; }
case TILE: // Scale and translate the pattern
{ cairo_matrix_t matrix;
cairo_pattern_t *pattern = cairo_pattern_create_for_surface(img); cairo_matrix_init_scale(&matrix, 1/scale_x, 1/scale_y);
cairo_set_source(xcb_ctx, pattern);
cairo_pattern_set_extend(pattern, CAIRO_EXTEND_REPEAT);
for (int i = 0; i < xr_screens; i++) { if (bg_type == TILE) {
cairo_matrix_t matrix; // Start image from top-left corner
cairo_matrix_init_translate(&matrix, -xr_resolutions[i].x, -xr_resolutions[i].y); cairo_matrix_translate(&matrix, -xr_resolutions[i].x, -xr_resolutions[i].y);
cairo_pattern_set_matrix(pattern, &matrix); } else {
// Draw image in the center of the screen
cairo_matrix_translate(&matrix,
(image_width * scale_x - xr_resolutions[i].width ) / 2 - xr_resolutions[i].x,
(image_height * scale_y - xr_resolutions[i].height) / 2 - xr_resolutions[i].y);
}
cairo_rectangle(xcb_ctx, xr_resolutions[i].x, xr_resolutions[i].y, xr_resolutions[i].width, xr_resolutions[i].height); cairo_pattern_set_matrix(pattern, &matrix);
cairo_fill(xcb_ctx);
}
cairo_pattern_destroy(pattern); // Draw to screen
break; cairo_rectangle(xcb_ctx, xr_resolutions[i].x, xr_resolutions[i].y, xr_resolutions[i].width, xr_resolutions[i].height);
} cairo_fill(xcb_ctx);
default:
cairo_set_source_surface(xcb_ctx, img, 0, 0);
cairo_paint(xcb_ctx);
break;
} }
cairo_pattern_destroy(pattern);
} }
/* /*