| @@ -17,6 +17,7 @@ |
| 17 | 17 | | */ |
| 18 | 18 | | |
| 19 | 19 | | #include <stdlib.h> |
| 20 | + | #include <stdio.h> |
| 20 | 21 | | |
| 21 | 22 | | #include <X11/Xlib.h> |
| 22 | 23 | | #include <X11/keysym.h> |
| @@ -38,6 +39,9 @@ static void (*handler[LASTEvent])(XEvent*) = { |
| 38 | 39 | | |
| 39 | 40 | | img_t img; |
| 40 | 41 | | win_t win; |
| 42 | + | |
| 43 | + | const char **filenames; |
| 44 | + | unsigned int filecnt; |
| 41 | 45 | | unsigned int fileidx; |
| 42 | 46 | | |
| 43 | 47 | | void run() { |
| @@ -50,6 +54,8 @@ void run() { |
| 50 | 54 | | } |
| 51 | 55 | | |
| 52 | 56 | | int main(int argc, char **argv) { |
| 57 | + | int i; |
| 58 | + | |
| 53 | 59 | | parse_options(argc, argv); |
| 54 | 60 | | |
| 55 | 61 | | if (!options->filecnt) { |
| @@ -57,7 +63,21 @@ int main(int argc, char **argv) { |
| 57 | 63 | | exit(1); |
| 58 | 64 | | } |
| 59 | 65 | | |
| 66 | + | if (!(filenames = (const char**) malloc(options->filecnt * sizeof(char*)))) |
| 67 | + | DIE("could not allocate memory"); |
| 68 | + | |
| 60 | 69 | | fileidx = 0; |
| 70 | + | filecnt = 0; |
| 71 | + | |
| 72 | + | for (i = 0; i < options->filecnt; ++i) { |
| 73 | + | if (!(img_load(&img, options->filenames[i]) < 0)) |
| 74 | + | filenames[filecnt++] = options->filenames[i]; |
| 75 | + | } |
| 76 | + | |
| 77 | + | if (!filecnt) { |
| 78 | + | fprintf(stderr, "sxiv: no valid image filename given, aborting\n"); |
| 79 | + | exit(1); |
| 80 | + | } |
| 61 | 81 | | |
| 62 | 82 | | img.zoom = 1.0; |
| 63 | 83 | | img.scalemode = SCALE_MODE; |
| @@ -68,7 +88,7 @@ int main(int argc, char **argv) { |
| 68 | 88 | | win_open(&win); |
| 69 | 89 | | imlib_init(&win); |
| 70 | 90 | | |
| 71 | | − | img_load(&img, options->filenames[fileidx]); |
| 91 | + | img_load(&img, filenames[fileidx]); |
| 72 | 92 | | img_display(&img, &win); |
| 73 | 93 | | |
| 74 | 94 | | run(); |
| @@ -104,15 +124,15 @@ void on_keypress(XEvent *ev) { |
| 104 | 124 | | exit(0); |
| 105 | 125 | | case XK_n: |
| 106 | 126 | | case XK_space: |
| 107 | | − | if (fileidx + 1 < options->filecnt) { |
| 108 | | − | img_load(&img, options->filenames[++fileidx]); |
| 127 | + | if (fileidx + 1 < filecnt) { |
| 128 | + | img_load(&img, filenames[++fileidx]); |
| 109 | 129 | | img_display(&img, &win); |
| 110 | 130 | | } |
| 111 | 131 | | break; |
| 112 | 132 | | case XK_p: |
| 113 | 133 | | case XK_BackSpace: |
| 114 | 134 | | if (fileidx > 0) { |
| 115 | | − | img_load(&img, options->filenames[--fileidx]); |
| 135 | + | img_load(&img, filenames[--fileidx]); |
| 116 | 136 | | img_display(&img, &win); |
| 117 | 137 | | } |
| 118 | 138 | | break; |