implement bg scaling

This commit is contained in:
Rio6 2021-05-30 01:37:26 -04:00
parent 91092a1bf1
commit 10546fcd72
2 changed files with 29 additions and 14 deletions

View File

@ -1436,7 +1436,7 @@ int main(int argc, char *argv[]) {
{"centered", no_argument, NULL, 'C'},
{"fill", no_argument, NULL, 'F'},
{"scale", no_argument, NULL, 'L'},
{"scale", no_argument, NULL, 'M'},
{"max", no_argument, NULL, 'M'},
{"ignore-empty-password", no_argument, NULL, 'e'},
{"inactivity-timeout", required_argument, NULL, 'I'},
{"show-failed-attempts", no_argument, NULL, 'f'},
@ -1627,30 +1627,31 @@ int main(int argc, char *argv[]) {
break;
case 't':
if(bg_type != NONE) {
errx(EXIT_FAILURE, "i3lock-color: Options tiling, centered, and fill conflict.");
errx(EXIT_FAILURE, "i3lock-color: Only one background type can be used.");
}
bg_type = TILE;
break;
case 'C':
if(bg_type != NONE) {
errx(EXIT_FAILURE, "i3lock-color: Options tiling, centered, and fill conflict.");
errx(EXIT_FAILURE, "i3lock-color: Only one background type can be used.");
}
bg_type = CENTER;
break;
case 'F':
if(bg_type != NONE) {
errx(EXIT_FAILURE, "i3lock-color: Options tiling, centered, and fill conflict.");
errx(EXIT_FAILURE, "i3lock-color: Only one background type can be used.");
}
bg_type = FILL;
break;
case 'L':
if(bg_type != NONE) {
errx(EXIT_FAILURE, "i3lock-color: Options tiling, centered, and fill conflict.");
errx(EXIT_FAILURE, "i3lock-color: Only one background type can be used.");
}
bg_type = SCALE;
break;
case 'M':
if(bg_type != NONE) {
errx(EXIT_FAILURE, "i3lock-color: Options tiling, centered, and fill conflict.");
errx(EXIT_FAILURE, "i3lock-color: Only one background type can be used.");
}
bg_type = MAX;
break;

View File

@ -1110,22 +1110,36 @@ void draw_image(uint32_t* root_resolution, cairo_surface_t *img, cairo_t* xcb_ct
case FILL:
case SCALE:
case MAX:
cairo_save(xcb_ctx);
for (int i = 0; i < xr_screens; i++) {
// Paint around center of monitor
double origin_x = xr_resolutions[i].x + (xr_resolutions[i].width / 2.0 - image_width / 2.0);
double origin_y = xr_resolutions[i].y + (xr_resolutions[i].height / 2.0 - image_height / 2.0);
cairo_save(xcb_ctx);
// Scale image according to bg_type and aspect ratios
double scale_x = 1, scale_y = 1;
if (bg_type == SCALE) {
cairo_scale(xcb_ctx,
(double) xr_resolutions[i].width / image_width,
(double) xr_resolutions[i].height / image_height);
scale_x = xr_resolutions[i].width / image_width;
scale_y = xr_resolutions[i].height / image_height;
} else {
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);
}
cairo_restore(xcb_ctx);
break;
case TILE: