xscreen

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

commit 36f8038dc8194a870bef4357ed109ec2e00a3102
parent 2cf7fff08629547e40d347c06f30e8bde342a2e4
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Tue, 30 Jul 2019 01:28:41 +0200

clean up code

Diffstat:
Mxscreen.c | 53+++++++++++++++++++++++++----------------------------
1 file changed, 25 insertions(+), 28 deletions(-)

diff --git a/xscreen.c b/xscreen.c @@ -58,7 +58,6 @@ display_help(void) " -j [quality] Save in a JPG, quality from 1 to 100.\n" " -d [delay] Delay in seconds.\n" " -f [path] Path + optional filename location.\n"); - XCloseDisplay(disp); exit(EXIT_SUCCESS); } @@ -110,11 +109,11 @@ make_default_filename(int format) struct tm *date = localtime(&cur_time); /* get length of formatted string before malloc */ - size_t str_len = snprintf(NULL, 0, "screenshot_%d-%02d-%02d-%02d-%02d-%02d", + size_t len = snprintf(NULL, 0, "screenshot_%d-%02d-%02d-%02d-%02d-%02d", date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, date->tm_hour, date->tm_min, date->tm_sec); - char *filename = malloc(str_len + 5); + char *filename = malloc(len + 5); if (!filename) errx(1, "malloc failure"); @@ -152,8 +151,8 @@ get_focused_window(void) static Window get_selected_rectangle(bool freeze) { - int x = 0, y = 0, rec_x = 0, rec_y = 0; - int width = 0, height = 0, rec_w = 0, rec_h = 0; + int x = 0, y = 0, rx = 0, ry = 0; + int width = 0, height = 0, rw = 0, rh = 0; XEvent e; Window root = DefaultRootWindow(disp); XGCValues gcval; @@ -197,23 +196,23 @@ get_selected_rectangle(bool freeze) } break; case MotionNotify: - XDrawRectangle(disp, root, gc, rec_x, rec_y, rec_w, rec_h); + XDrawRectangle(disp, root, gc, rx, ry, rw, rh); - rec_x = x - 1; - rec_y = y - 1; - rec_w = e.xmotion.x - rec_x + 1; - rec_h = e.xmotion.y - rec_y + 1; + rx = x - 1; + ry = y - 1; + rw = e.xmotion.x - rx + 1; + rh = e.xmotion.y - ry + 1; - if (rec_w < 0) { - rec_x += rec_w; - rec_w = 0 - rec_w; + if (rw < 0) { + rx += rw; + rw = 0 - rw; } - if (rec_h < 0) { - rec_y += rec_h; - rec_h = 0 - rec_h; + if (rh < 0) { + ry += rh; + rh = 0 - rh; } - XDrawRectangle(disp, root, gc, rec_x, rec_y, rec_w, rec_h); + XDrawRectangle(disp, root, gc, rx, ry, rw, rh); XFlush(disp); break; } @@ -222,12 +221,13 @@ get_selected_rectangle(bool freeze) break; } - XDrawRectangle(disp, root, gc, rec_x, rec_y, rec_w, rec_h); + XDrawRectangle(disp, root, gc, rx, ry, rw, rh); if (freeze) XUngrabServer(disp); XUngrabPointer(disp, CurrentTime); XFreeGC(disp, gc); XFlush(disp); + return XCreateSimpleWindow(disp, root, x, y, width, height, 0, 0, 0); } @@ -284,12 +284,12 @@ capture(Window win, struct screenshot *img) for (int x = 0; x < img->width; x++) { for (int y = 0; y < img->height ; y++) { - unsigned long xpixel = XGetPixel(ximg, x, y); + unsigned long xpix = XGetPixel(ximg, x, y); size_t pix_pos = (x + img->width * y) * 3; - img->data[pix_pos] = (xpixel & ximg->red_mask) >> sr; - img->data[pix_pos + 1] = (xpixel & ximg->green_mask) >> sg; - img->data[pix_pos + 2] = xpixel & ximg->blue_mask; + 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; } } @@ -316,20 +316,17 @@ int main(int argc, char **argv) { disp = XOpenDisplay(NULL); - if (!disp) { - fprintf(stderr, "Failed opening DISPLAY.\n"); - return EXIT_FAILURE; - } - + if (!disp) + errx(1, "Failed opening DISPLAY."); Window win; struct screenshot img; img.format = PNG; img.jpg_quality = 100; - img.filename = NULL; img.window_type = ROOT; img.delay = 0; img.freeze = false; + img.filename = NULL; check_args(argc, argv, &img); if (!img.filename) {