xscreen

X11 screenshot utility
git clone https://porkepik.fr/xscreen
Log | Files | Refs | README | LICENSE

commit 90367daa29919d6389e8752f1577dc3002943101
parent 58165a7b65f47bfce6a1d02909f20ecf40549c1b
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Tue, 20 Aug 2019 14:34:50 +0200

var name img->shot

Diffstat:
Mxscreen.c | 130++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 65 insertions(+), 65 deletions(-)

diff --git a/xscreen.c b/xscreen.c @@ -62,41 +62,41 @@ display_help(void) } static void -check_args(int argc, char **argv, struct screenshot *img) +check_args(int argc, char **argv, struct screenshot *shot) { int opt; while ((opt = getopt(argc, argv, "f:d:j:husz")) != -1) { switch (opt) { case 'f': - img->filename = malloc(strlen(optarg) + 1); - if (img->filename == NULL) + shot->filename = malloc(strlen(optarg) + 1); + if (shot->filename == NULL) errx(1, "malloc failure"); - strcpy(img->filename, optarg); + strcpy(shot->filename, optarg); break; case 'd': - img->delay = strtol(optarg, NULL, 0); - if (img->delay < 0) - img->delay = 0; + shot->delay = strtol(optarg, NULL, 0); + if (shot->delay < 0) + shot->delay = 0; break; case 'h': display_help(); break; case 'u': - img->window_type = FOCUSED; + shot->window_type = FOCUSED; break; case 's': - img->window_type = RECTANGLE; + shot->window_type = RECTANGLE; break; case 'z': - img->freeze = true; + shot->freeze = true; break; case 'j': - img->format = JPEG; - img->jpg_quality = strtol(optarg, NULL, 0); - if (img->jpg_quality < 1) - img->jpg_quality = 1; - else if (img->jpg_quality > 100) - img->jpg_quality = 100; + shot->format = JPEG; + shot->jpg_quality = strtol(optarg, NULL, 0); + if (shot->jpg_quality < 1) + shot->jpg_quality = 1; + else if (shot->jpg_quality > 100) + shot->jpg_quality = 100; break; } } @@ -266,7 +266,7 @@ delay(int delay_sec) } static void -capture(Window win, struct screenshot *img) +capture(Window win, struct screenshot *shot) { int sr, sg; XWindowAttributes attr; @@ -274,19 +274,19 @@ capture(Window win, struct screenshot *img) XGetWindowAttributes(disp, win, &attr); - img->width = attr.width; - img->height = attr.height; + shot->width = attr.width; + shot->height = attr.height; - if (img->window_type == RECTANGLE) { + if (shot->window_type == RECTANGLE) { ximg = XGetImage(disp, DefaultRootWindow(disp), attr.x, attr.y, - img->width, img->height, AllPlanes, ZPixmap); + shot->width, shot->height, AllPlanes, ZPixmap); } else { - ximg = XGetImage(disp, win, 0, 0, img->width, img->height, + ximg = XGetImage(disp, win, 0, 0, shot->width, shot->height, AllPlanes, ZPixmap); } - img->data = malloc(img->width * img->height * 3); - if (img->data == NULL) + shot->data = malloc(shot->width * shot->height * 3); + if (shot->data == NULL) errx(1, "malloc failure"); switch (ximg->bits_per_pixel) { @@ -303,14 +303,14 @@ capture(Window win, struct screenshot *img) errx(1, "unsupported bpp: %d", ximg->bits_per_pixel); } - for (int x = 0; x < img->width; x++) { - for (int y = 0; y < img->height ; y++) { + for (int x = 0; x < shot->width; x++) { + for (int y = 0; y < shot->height ; y++) { unsigned long xpix = XGetPixel(ximg, x, y); - size_t pix_pos = (x + img->width * y) * 3; + size_t pix_pos = (x + shot->width * y) * 3; - img->data[pix_pos] = (xpix & ximg->red_mask) >> sr; - img->data[pix_pos+1] = (xpix & ximg->green_mask) >> sg; - img->data[pix_pos+2] = xpix & ximg->blue_mask; + shot->data[pix_pos] = (xpix & ximg->red_mask) >> sr; + shot->data[pix_pos+1] = (xpix & ximg->green_mask) >> sg; + shot->data[pix_pos+2] = xpix & ximg->blue_mask; } } @@ -318,18 +318,18 @@ capture(Window win, struct screenshot *img) } static void -save_image(struct screenshot img) +save_image(struct screenshot shot) { - if (access(img.filename, F_OK) == -1) { - if (img.format == PNG) { - stbi_write_png(img.filename, img.width, img.height, 3, - img.data, img.width * 3); - } else if (img.format == JPEG) { - stbi_write_jpg(img.filename, img.width, img.height, 3, - img.data, img.jpg_quality); + if (access(shot.filename, F_OK) == -1) { + if (shot.format == PNG) { + stbi_write_png(shot.filename, shot.width, + shot.height, 3, shot.data, shot.width * 3); + } else if (shot.format == JPEG) { + stbi_write_jpg(shot.filename, shot.width, + shot.height, 3, shot.data, shot.jpg_quality); } } else { - printf("\"%s\" already exists\n", img.filename); + printf("\"%s\" already exists\n", shot.filename); } } @@ -341,41 +341,41 @@ main(int argc, char **argv) errx(1, "Failed opening DISPLAY."); Window win; - struct screenshot img; - img.format = PNG; - img.jpg_quality = 100; - img.window_type = ROOT; - img.delay = 0; - img.freeze = false; - img.filename = NULL; - - check_args(argc, argv, &img); - if (img.filename == NULL) { - img.filename = make_default_filename(img.format); - } else if (strend(img.filename, "/")) { - char *temp = make_default_filename(img.format); - img.filename = realloc(img.filename, - strlen(img.filename) + strlen(temp) + 1); - strcat(img.filename, temp); + struct screenshot shot; + shot.format = PNG; + shot.jpg_quality = 100; + shot.window_type = ROOT; + shot.delay = 0; + shot.freeze = false; + shot.filename = NULL; + + check_args(argc, argv, &shot); + if (shot.filename == NULL) { + shot.filename = make_default_filename(shot.format); + } else if (strend(shot.filename, "/")) { + char *temp = make_default_filename(shot.format); + shot.filename = realloc(shot.filename, + strlen(shot.filename) + strlen(temp) + 1); + strcat(shot.filename, temp); free(temp); } - if (img.window_type == RECTANGLE) { - win = get_selected_rectangle(img.freeze); - delay(img.delay); - } else if (img.window_type == FOCUSED) { - delay(img.delay); + if (shot.window_type == RECTANGLE) { + win = get_selected_rectangle(shot.freeze); + delay(shot.delay); + } else if (shot.window_type == FOCUSED) { + delay(shot.delay); win = get_focused_window(); } else { - delay(img.delay); + delay(shot.delay); win = get_root_window(); } - capture(win, &img); - save_image(img); + capture(win, &shot); + save_image(shot); - free(img.filename); - free(img.data); + free(shot.filename); + free(shot.data); XCloseDisplay(disp); return EXIT_SUCCESS;