beh

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

commit 37b5300adbd1a925a67c2c3b4e05284ea4a94f97
parent e11ecf89d36e1b99a3d2e46b26942c98037de300
Author: Thomas Philippe <dev@porkepik.fr>
Date:   Fri, 26 Jun 2020 21:35:58 +0200

add multiple files/folders as argument

Diffstat:
Mbeh.c | 122+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 97 insertions(+), 25 deletions(-)

diff --git a/beh.c b/beh.c @@ -2,15 +2,18 @@ Not finished. Much refactoring needed. Get out of here while you can. TODO - - handle folders and archives - - next/previous/go to.../first/last + - handle archives + - go to page + - set window title - display filename, res, file size */ +#include <dirent.h> #include <err.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/stat.h> #include <X11/Xlib.h> #include <X11/Xutil.h> @@ -39,6 +42,7 @@ struct app { }; struct image { + int filepath; int width; int height; int dpy_width; @@ -55,17 +59,52 @@ struct image { static Display *dpy; static struct app app; +static char **filepaths; +static int nimgs; static struct image img; static void usage(void) { - printf("Usage: beh [--bg-color \"#ffffff\"] [FILE]\n"); + printf("Usage: beh [--bg-color \"#ffffff\"] [FILES]\n"); exit(0); } static void +check_file(char *path) +{ + struct stat path_stat; + stat(path, &path_stat); + + if (S_ISREG(path_stat.st_mode)) { + filepaths = realloc(filepaths, sizeof(char *) * nimgs+1); + filepaths[nimgs++] = path; + } else if (S_ISDIR(path_stat.st_mode)) { + struct dirent *dir; + DIR *d; + if (!(d = opendir(path))) { + printf("Could not open directory: %s\n", path); + return; + } + while ((dir = readdir(d))) { + char *filepath = malloc(strlen(path) + + strlen(dir->d_name) + 2); + strcpy(filepath, path); + strcat(filepath, "/"); + strcat(filepath, dir->d_name); + stat(filepath, &path_stat); + if (!S_ISREG(path_stat.st_mode)) + continue; + filepaths = realloc(filepaths, sizeof(char *)*nimgs+1); + filepaths[nimgs++] = filepath; + } + closedir(d); + } +} + + +static void check_args(int argc, char **argv) { app.bg_color = NULL; @@ -75,6 +114,8 @@ check_args(int argc, char **argv) app.bg_color = argv[++i]; else if (strcmp(argv[i], "-h") == 0) usage(); + else + check_file(argv[i]); } } @@ -91,6 +132,24 @@ rgb_to_bgr(void) } } +static int +init_img(char *filepath) +{ + img.data = stbi_load(filepath, &img.width, &img.height, NULL, RGBA); + if (img.data == NULL) { + printf("Could not open %s\n", filepath); + return 1; + } + printf("Opened: %s\n", filepath); + rgb_to_bgr(); + + img.dpy_data = img.data; + img.dpy_width = img.width; + img.dpy_height = img.height; + + return 0; +} + static unsigned char * resize(int new_w, int new_h) { @@ -198,8 +257,9 @@ event_loop() KeySym keysym; XConfigureEvent xce; int tmp_x, tmp_y, left_mouse_hold, hold_x, hold_y; + int imgid, status; - left_mouse_hold = 0; + left_mouse_hold = imgid = 0; while (1) { XNextEvent(dpy, &ev); @@ -224,17 +284,35 @@ event_loop() goto quit; else if (keysym == XK_m) { set_zoom(MAX); - pixmap = create_pixmap(); - apply_image(pixmap); } else if (keysym == XK_l) { set_zoom(FILL); - pixmap = create_pixmap(); - apply_image(pixmap); } else if (keysym == XK_o) { set_zoom(NO_ZOOM); - pixmap = create_pixmap(); - apply_image(pixmap); + } else if (keysym == XK_p) { + if (imgid == 0) + break; + do { + imgid--; + status = init_img(filepaths[imgid]); + set_zoom(NO_ZOOM); + } while (status != 0 && imgid > 0); + if (status != 0) + goto quit; + } else if (keysym == XK_n) { + if (imgid == nimgs - 1) + break; + do { + imgid++; + status = init_img(filepaths[imgid]); + set_zoom(NO_ZOOM); + } while (status != 0 && imgid < nimgs - 1); + if (status != 0) + goto quit; + } else { + break; } + pixmap = create_pixmap(); + apply_image(pixmap); break; case ButtonPress: if (ev.xbutton.button == 1) { @@ -282,8 +360,7 @@ quit: int main(int argc, char **argv) { - int screen; - char *title; + int screen, status, i; dpy = XOpenDisplay(NULL); if (dpy == NULL) @@ -293,24 +370,19 @@ main(int argc, char **argv) usage(); check_args(argc, 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.dpy_data = img.data; - img.dpy_width = img.width; - img.dpy_height = img.height; + for (i = 0; i < nimgs - 1; i++) { + status = init_img(filepaths[i]); + if (status == 0) + break; + } + if (status != 0) + return 0; app.width = img.width; app.height = img.height; screen = DefaultScreen(dpy); app.win = XCreateSimpleWindow(dpy, RootWindow(dpy, screen), 0, 0, app.width, app.height, 0, 0, WhitePixel(dpy, screen)); - title = malloc(strlen(argv[argc-1]) + 50); - strcpy(title, "beh - "); - strcat(title, argv[argc-1]); - XSetStandardProperties(dpy, app.win, title, NULL, None, NULL, 0, NULL); XSelectInput(dpy, app.win, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask | PointerMotionMask | ButtonReleaseMask); @@ -323,7 +395,7 @@ main(int argc, char **argv) if (img.dpy_data != img.data) free(img.dpy_data); stbi_image_free(img.data); - free(title); + free(filepaths); return 0; }