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'}, {"centered", no_argument, NULL, 'C'},
{"fill", no_argument, NULL, 'F'}, {"fill", no_argument, NULL, 'F'},
{"scale", no_argument, NULL, 'L'}, {"scale", no_argument, NULL, 'L'},
{"scale", no_argument, NULL, 'M'}, {"max", no_argument, NULL, 'M'},
{"ignore-empty-password", no_argument, NULL, 'e'}, {"ignore-empty-password", no_argument, NULL, 'e'},
{"inactivity-timeout", required_argument, NULL, 'I'}, {"inactivity-timeout", required_argument, NULL, 'I'},
{"show-failed-attempts", no_argument, NULL, 'f'}, {"show-failed-attempts", no_argument, NULL, 'f'},
@ -1627,30 +1627,31 @@ int main(int argc, char *argv[]) {
break; break;
case 't': case 't':
if(bg_type != NONE) { 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; bg_type = TILE;
break; break;
case 'C': case 'C':
if(bg_type != NONE) { 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; bg_type = CENTER;
break; break;
case 'F': case 'F':
if(bg_type != NONE) { 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; bg_type = FILL;
break;
case 'L': case 'L':
if(bg_type != NONE) { 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; bg_type = SCALE;
break; break;
case 'M': case 'M':
if(bg_type != NONE) { 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; bg_type = MAX;
break; 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 FILL:
case SCALE: case SCALE:
case MAX: case MAX:
cairo_save(xcb_ctx);
for (int i = 0; i < xr_screens; i++) { for (int i = 0; i < xr_screens; i++) {
// Paint around center of monitor cairo_save(xcb_ctx);
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);
// Scale image according to bg_type and aspect ratios
double scale_x = 1, scale_y = 1;
if (bg_type == SCALE) { if (bg_type == SCALE) {
cairo_scale(xcb_ctx, scale_x = xr_resolutions[i].width / image_width;
(double) xr_resolutions[i].width / image_width, scale_y = xr_resolutions[i].height / image_height;
(double) 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_set_source_surface(xcb_ctx, img, origin_x, origin_y);
cairo_paint(xcb_ctx); cairo_paint(xcb_ctx);
cairo_restore(xcb_ctx);
} }
cairo_restore(xcb_ctx);
break; break;
case TILE: case TILE: