setxbg

X11 background image
git clone https://porkepik.fr/setxbg
Log | Files | Refs | README | LICENSE

commit 480aef1d3bd62e13579a80b06cb50a9c3c65da0b
parent a2d2fc848702bc97f521f40a4fad6d0d27ffe191
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Sat, 17 Aug 2019 21:46:24 +0200

reorganize functions

Diffstat:
Msetxbg.c | 96++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 49 insertions(+), 47 deletions(-)

diff --git a/setxbg.c b/setxbg.c @@ -17,10 +17,10 @@ #define RGBA 4 -enum { +enum modes { BG_CENTER, BG_FILL, - BG_MAX, + BG_MAX }; struct image { @@ -31,7 +31,7 @@ struct image { int dest_x; int dest_y; unsigned char *data; - int bg_mode; + enum modes bg_mode; char *bg_color; }; @@ -41,7 +41,7 @@ usage(void) { printf("Usage: setxbg [--bg-center | --bg-fill | --bg-max] " "[--bg-color \"#ffffff\"] [FILE]\n"); - exit(EXIT_SUCCESS); + exit(0); } static void @@ -66,7 +66,7 @@ rgb_to_bgr(struct image img) { unsigned char red, blue; - for (int i = 0; i < img.width * img.height * RGBA; i += RGBA) { + for (long i = 0; i < img.width * img.height * RGBA; i += RGBA) { red = img.data[i+2]; blue = img.data[i]; img.data[i] = red; @@ -92,6 +92,38 @@ resize(struct image img, int new_w, int new_h) } static void +set_dimensions(struct image *img, int screen_w, int screen_h) +{ + int scaled_w, scaled_h, new_w, new_h; + + scaled_w = (float)screen_h * ((float)img->width / (float)img->height); + scaled_h = (float)screen_w / ((float)img->width / (float)img->height); + + if ((img->bg_mode == BG_FILL && scaled_w < screen_w) || + (img->bg_mode == BG_MAX && scaled_h <= screen_h)) { + new_w = screen_w; + new_h = scaled_h; + } else { + new_w = scaled_w; + new_h = screen_h; + } + + if (img->bg_mode != BG_CENTER) { + img->data = resize(*img, new_w, new_h); + img->width = new_w; + img->height = new_h; + } + + if (img->bg_mode == BG_FILL) { + img->src_x = (img->width - screen_w)/2; + img->src_y = (img->height - screen_h)/2; + } else if (img->bg_mode == BG_MAX || img->bg_mode == BG_CENTER) { + img->dest_x = (screen_w - img->width)/2; + img->dest_y = (screen_h - img->height)/2; + } +} + +static void forget_old_background(Display *disp, Window root) { Atom prop, prop_esetroot, type; @@ -168,50 +200,17 @@ apply_background(Display *disp, struct image img, int screen_w, int screen_h) XDestroyImage(ximg); } -static void -set_background(Display *disp, struct image *img) -{ - int scaled_w, scaled_h, screen_w, screen_h, new_w, new_h; - - screen_w = WidthOfScreen(DefaultScreenOfDisplay(disp)); - screen_h = HeightOfScreen(DefaultScreenOfDisplay(disp)); - - scaled_w = (float)screen_h * ((float)img->width / (float)img->height); - scaled_h = (float)screen_w / ((float)img->width / (float)img->height); - - if ((img->bg_mode == BG_FILL && scaled_w < screen_w) || - (img->bg_mode == BG_MAX && scaled_h <= screen_h)) { - new_w = screen_w; - new_h = scaled_h; - } else { - new_w = scaled_w; - new_h = screen_h; - } - - if (img->bg_mode != BG_CENTER) { - img->data = resize(*img, new_w, new_h); - img->width = new_w; - img->height = new_h; - } - - if (img->bg_mode == BG_FILL) { - img->src_x = (img->width - screen_w)/2; - img->src_y = (img->height - screen_h)/2; - } else if (img->bg_mode == BG_MAX || img->bg_mode == BG_CENTER) { - img->dest_x = (screen_w - img->width)/2; - img->dest_y = (screen_h - img->height)/2; - } - - apply_background(disp, *img, screen_w, screen_h); -} - int main(int argc, char **argv) { Display *disp; + int screen_w, screen_h; struct image img; - img.src_x = 0, img.src_y = 0; - img.dest_x = 0, img.dest_y = 0; + + img.src_x = 0; + img.src_y = 0; + img.dest_x = 0; + img.dest_y = 0; img.bg_mode = BG_MAX; img.bg_color = NULL; @@ -226,11 +225,14 @@ main(int argc, char **argv) img.data = stbi_load(argv[argc-1], &img.width, &img.height, NULL, RGBA); if (img.data == NULL) errx(1, "Error during image loading."); - rgb_to_bgr(img); - set_background(disp, &img); + + screen_w = WidthOfScreen(DefaultScreenOfDisplay(disp)); + screen_h = HeightOfScreen(DefaultScreenOfDisplay(disp)); + set_dimensions(&img, screen_w, screen_h); + apply_background(disp, img, screen_w, screen_h); XCloseDisplay(disp); - return EXIT_SUCCESS; + return 0; }