xscreen

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

commit 8c9965a683dcd617136ed9e4d998ab6765a913f1
parent ca1d295434e00a5e86abd25a080d9c230c54d198
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Thu,  1 Aug 2019 04:23:40 +0200

improve style

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

diff --git a/xscreen.c b/xscreen.c @@ -51,13 +51,13 @@ static void display_help(void) { printf("Usage: By default, takes a screenshot of the root window\n " - " and saves it in a PNG file in the current directory.\n\n" - " -u Focused window.\n" - " -s Rectangle selection.\n" - " -z Freeze display during rectangle selection.\n" - " -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"); + " and saves it in a PNG file in the current directory.\n\n" + " -u Focused window.\n" + " -s Rectangle selection.\n" + " -z Freeze display during rectangle selection.\n" + " -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"); exit(EXIT_SUCCESS); } @@ -69,7 +69,7 @@ check_args(int argc, char **argv, struct screenshot *img) switch (opt) { case 'f': img->filename = malloc(strlen(optarg) + 1); - if (!img->filename) + if (img->filename == NULL) errx(1, "malloc failure"); strcpy(img->filename, optarg); break; @@ -105,21 +105,22 @@ check_args(int argc, char **argv, struct screenshot *img) static char * make_default_filename(int format) { + size_t len; time_t cur_time = time(NULL); struct tm *date = localtime(&cur_time); /* get length of formatted string before malloc */ - 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); + 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(len + 5); - if (!filename) + if (filename == NULL) errx(1, "malloc failure"); sprintf(filename, "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); + date->tm_year + 1900, date->tm_mon + 1, date->tm_mday, + date->tm_hour, date->tm_min, date->tm_sec); if (format == PNG) strcat(filename, ".png"); @@ -145,7 +146,7 @@ get_toplevel_parent(Window win) while (1) { status = XQueryTree(disp, win, &root, &parent, &children, &num_children); - if (!status) + if (status == 0) errx(1, "XQueryTree error"); if (children) XFree(children); @@ -163,7 +164,7 @@ get_focused_window(void) int revert_to; XGetInputFocus(disp, &focused_win, &revert_to); - if (!focused_win) + if (focused_win == None) errx(1, "No window is being focused."); focused_win = get_toplevel_parent(focused_win); @@ -176,13 +177,12 @@ get_selected_rectangle(bool freeze) 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; + Window root = DefaultRootWindow(disp); XGrabPointer(disp, root, False, - ButtonMotionMask | ButtonPressMask | ButtonReleaseMask, - GrabModeAsync, GrabModeAsync, None, None, CurrentTime); - + ButtonMotionMask | ButtonPressMask | ButtonReleaseMask, + GrabModeAsync, GrabModeAsync, None, None, CurrentTime); gcval.foreground = XWhitePixel(disp, 0); gcval.function = GXxor; @@ -191,8 +191,7 @@ get_selected_rectangle(bool freeze) gcval.subwindow_mode = IncludeInferiors; GC gc = XCreateGC(disp, root, - GCFunction | GCForeground | GCBackground | GCSubwindowMode, - &gcval); + GCFunction | GCForeground | GCBackground | GCSubwindowMode, &gcval); while (1) { XNextEvent(disp, &e); @@ -280,14 +279,14 @@ capture(Window win, struct screenshot *img) if (img->window_type == RECTANGLE) { ximg = XGetImage(disp, DefaultRootWindow(disp), attr.x, attr.y, - img->width, img->height, AllPlanes, ZPixmap); + img->width, img->height, AllPlanes, ZPixmap); } else { ximg = XGetImage(disp, win, 0, 0, img->width, img->height, - AllPlanes, ZPixmap); + AllPlanes, ZPixmap); } img->data = malloc(img->width * img->height * 3); - if (!img->data) + if (img->data == NULL) errx(1, "malloc failure"); switch (ximg->bits_per_pixel) { @@ -324,10 +323,10 @@ save_image(struct screenshot img) 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); + 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); + img.data, img.jpg_quality); } } else { printf("\"%s\" already exists\n", img.filename); @@ -338,7 +337,7 @@ int main(int argc, char **argv) { disp = XOpenDisplay(NULL); - if (!disp) + if (disp == NULL) errx(1, "Failed opening DISPLAY."); Window win; @@ -351,7 +350,7 @@ main(int argc, char **argv) img.filename = NULL; check_args(argc, argv, &img); - if (!img.filename) { + if (img.filename == NULL) { img.filename = make_default_filename(img.format); } else if (strend(img.filename, "/")) { char *temp = make_default_filename(img.format);