nsxiv9b9294b
Use bit-field for boolean flags in fileinfo struct
+34 −27
6 files changed
- M Makefile +1 −1
- M commands.c +8 −8
- M image.c +2 −2
- M main.c +14 −11
- M thumbs.c +3 −3
- M types.h +6 −2
| @@ -1,4 +1,4 @@ | |||
| 1 | − | VERSION := git-20141222 | |
| 1 | + | VERSION := git-20150104 | |
| 2 | 2 | ||
| 3 | 3 | PREFIX := /usr/local | |
| 4 | 4 | MANPREFIX := $(PREFIX)/share/man | |
| @@ -61,7 +61,7 @@ bool cg_quit(arg_t a) | |||
| 61 | 61 | ||
| 62 | 62 | if (options->to_stdout && markcnt > 0) { | |
| 63 | 63 | for (i = 0; i < filecnt; i++) { | |
| 64 | − | if (files[i].marked) | |
| 64 | + | if (files[i].flags & FF_MARK) | |
| 65 | 65 | printf("%s\n", files[i].name); | |
| 66 | 66 | } | |
| 67 | 67 | } | |
| @@ -200,10 +200,10 @@ bool cg_zoom(arg_t a) | |||
| 200 | 200 | ||
| 201 | 201 | bool cg_toggle_image_mark(arg_t a) | |
| 202 | 202 | { | |
| 203 | − | files[fileidx].marked = !files[fileidx].marked; | |
| 204 | − | markcnt += files[fileidx].marked ? 1 : -1; | |
| 203 | + | files[fileidx].flags ^= FF_MARK; | |
| 204 | + | markcnt += files[fileidx].flags & FF_MARK ? 1 : -1; | |
| 205 | 205 | if (mode == MODE_THUMB) | |
| 206 | − | tns_mark(&tns, fileidx, files[fileidx].marked); | |
| 206 | + | tns_mark(&tns, fileidx, !!(files[fileidx].flags & FF_MARK)); | |
| 207 | 207 | return true; | |
| 208 | 208 | } | |
| 209 | 209 | ||
| @@ -212,8 +212,8 @@ bool cg_reverse_marks(arg_t a) | |||
| 212 | 212 | int i; | |
| 213 | 213 | ||
| 214 | 214 | for (i = 0; i < filecnt; i++) { | |
| 215 | − | files[i].marked = !files[i].marked; | |
| 216 | − | markcnt += files[i].marked ? 1 : -1; | |
| 215 | + | files[i].flags ^= FF_MARK; | |
| 216 | + | markcnt += files[i].flags & FF_MARK ? 1 : -1; | |
| 217 | 217 | } | |
| 218 | 218 | if (mode == MODE_THUMB) | |
| 219 | 219 | tns.dirty = true; | |
| @@ -225,7 +225,7 @@ bool cg_unmark_all(arg_t a) | |||
| 225 | 225 | int i; | |
| 226 | 226 | ||
| 227 | 227 | for (i = 0; i < filecnt; i++) | |
| 228 | − | files[i].marked = false; | |
| 228 | + | files[i].flags &= ~FF_MARK; | |
| 229 | 229 | markcnt = 0; | |
| 230 | 230 | if (mode == MODE_THUMB) | |
| 231 | 231 | tns.dirty = true; | |
| @@ -242,7 +242,7 @@ bool cg_navigate_marked(arg_t a) | |||
| 242 | 242 | n *= prefix; | |
| 243 | 243 | d = n > 0 ? 1 : -1; | |
| 244 | 244 | for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) { | |
| 245 | − | if (files[i].marked) { | |
| 245 | + | if (files[i].flags & FF_MARK) { | |
| 246 | 246 | n -= d; | |
| 247 | 247 | new = i; | |
| 248 | 248 | } | |
| @@ -292,7 +292,7 @@ bool img_load_gif(img_t *img, const fileinfo_t *file) | |||
| 292 | 292 | DGifCloseFile(gif); | |
| 293 | 293 | #endif | |
| 294 | 294 | ||
| 295 | − | if (err && file->warn) | |
| 295 | + | if (err && (file->flags & FF_WARN)) | |
| 296 | 296 | warn("corrupted gif file: %s", file->name); | |
| 297 | 297 | ||
| 298 | 298 | if (img->multi.cnt > 1) { | |
| @@ -321,7 +321,7 @@ bool img_load(img_t *img, const fileinfo_t *file) | |||
| 321 | 321 | if (access(file->path, R_OK) < 0 || | |
| 322 | 322 | (img->im = imlib_load_image(file->path)) == NULL) | |
| 323 | 323 | { | |
| 324 | − | if (file->warn) | |
| 324 | + | if (file->flags & FF_WARN) | |
| 325 | 325 | warn("could not open image: %s", file->name); | |
| 326 | 326 | return false; | |
| 327 | 327 | } | |
| @@ -128,7 +128,8 @@ void check_add_file(char *filename, bool given) | |||
| 128 | 128 | ||
| 129 | 129 | if (fileidx == filecnt) { | |
| 130 | 130 | filecnt *= 2; | |
| 131 | − | files = (fileinfo_t*) s_realloc(files, filecnt * sizeof(fileinfo_t)); | |
| 131 | + | files = s_realloc(files, filecnt * sizeof(*files)); | |
| 132 | + | memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files)); | |
| 132 | 133 | } | |
| 133 | 134 | ||
| 134 | 135 | #if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \ | |
| @@ -149,7 +150,6 @@ void check_add_file(char *filename, bool given) | |||
| 149 | 150 | } | |
| 150 | 151 | #endif | |
| 151 | 152 | ||
| 152 | − | files[fileidx].warn = given; | |
| 153 | 153 | files[fileidx].name = s_strdup(filename); | |
| 154 | 154 | if (files[fileidx].path == NULL) | |
| 155 | 155 | files[fileidx].path = files[fileidx].name; | |
| @@ -157,6 +157,8 @@ void check_add_file(char *filename, bool given) | |||
| 157 | 157 | files[fileidx].base = ++bn; | |
| 158 | 158 | else | |
| 159 | 159 | files[fileidx].base = files[fileidx].name; | |
| 160 | + | if (given) | |
| 161 | + | files[fileidx].flags |= FF_WARN; | |
| 160 | 162 | fileidx++; | |
| 161 | 163 | } | |
| 162 | 164 | ||
| @@ -171,7 +173,7 @@ void remove_file(int n, bool manual) | |||
| 171 | 173 | cleanup(); | |
| 172 | 174 | exit(manual ? EXIT_SUCCESS : EXIT_FAILURE); | |
| 173 | 175 | } | |
| 174 | − | if (files[n].marked) | |
| 176 | + | if (files[n].flags & FF_MARK) | |
| 175 | 177 | markcnt--; | |
| 176 | 178 | ||
| 177 | 179 | if (files[n].path != files[n].name) | |
| @@ -335,7 +337,7 @@ void load_image(int new) | |||
| 335 | 337 | else if (new > 0 && new < fileidx) | |
| 336 | 338 | new--; | |
| 337 | 339 | } | |
| 338 | − | files[new].warn = false; | |
| 340 | + | files[new].flags &= ~FF_WARN; | |
| 339 | 341 | fileidx = current = new; | |
| 340 | 342 | ||
| 341 | 343 | info.open = false; | |
| @@ -378,7 +380,7 @@ void update_info(void) | |||
| 378 | 380 | if (win.bar.h == 0) | |
| 379 | 381 | return; | |
| 380 | 382 | for (fw = 0, i = filecnt; i > 0; fw++, i /= 10); | |
| 381 | − | mark = files[fileidx].marked ? "* " : ""; | |
| 383 | + | mark = files[fileidx].flags & FF_MARK ? "* " : ""; | |
| 382 | 384 | l->p = l->buf; | |
| 383 | 385 | r->p = r->buf; | |
| 384 | 386 | if (mode == MODE_THUMB) { | |
| @@ -535,7 +537,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 535 | 537 | } | |
| 536 | 538 | ||
| 537 | 539 | for (f = i = 0; f < fcnt; i++) { | |
| 538 | − | if ((marked && files[i].marked) || (!marked && i == fileidx)) { | |
| 540 | + | if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { | |
| 539 | 541 | stat(files[i].path, &oldst[f]); | |
| 540 | 542 | fprintf(pfs, "%s\n", files[i].name); | |
| 541 | 543 | f++; | |
| @@ -548,7 +550,7 @@ void run_key_handler(const char *key, unsigned int mask) | |||
| 548 | 550 | warn("key handler exited with non-zero return value: %d", retval); | |
| 549 | 551 | ||
| 550 | 552 | for (f = i = 0; f < fcnt; i++) { | |
| 551 | − | if ((marked && files[i].marked) || (!marked && i == fileidx)) { | |
| 553 | + | if ((marked && (files[i].flags & FF_MARK)) || (!marked && i == fileidx)) { | |
| 552 | 554 | if (stat(files[i].path, &st) != 0 || | |
| 553 | 555 | memcmp(&oldst[f].st_mtime, &st.st_mtime, sizeof(st.st_mtime)) != 0) | |
| 554 | 556 | { | |
| @@ -670,9 +672,9 @@ void on_buttonpress(XButtonEvent *bev) | |||
| 670 | 672 | break; | |
| 671 | 673 | case Button3: | |
| 672 | 674 | if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { | |
| 673 | − | files[sel].marked = !files[sel].marked; | |
| 674 | − | markcnt += files[sel].marked ? 1 : -1; | |
| 675 | − | tns_mark(&tns, sel, files[sel].marked); | |
| 675 | + | files[sel].flags ^= FF_MARK; | |
| 676 | + | markcnt += files[sel].flags & FF_MARK ? 1 : -1; | |
| 677 | + | tns_mark(&tns, sel, !!(files[sel].flags & FF_MARK)); | |
| 676 | 678 | redraw(); | |
| 677 | 679 | } | |
| 678 | 680 | break; | |
| @@ -818,7 +820,8 @@ int main(int argc, char **argv) | |||
| 818 | 820 | else | |
| 819 | 821 | filecnt = options->filecnt; | |
| 820 | 822 | ||
| 821 | − | files = (fileinfo_t*) s_malloc(filecnt * sizeof(fileinfo_t)); | |
| 823 | + | files = s_malloc(filecnt * sizeof(*files)); | |
| 824 | + | memset(files, 0, filecnt * sizeof(*files)); | |
| 822 | 825 | fileidx = 0; | |
| 823 | 826 | ||
| 824 | 827 | if (options->from_stdin) { | |
| @@ -341,7 +341,7 @@ bool tns_load(tns_t *tns, int n, bool force) | |||
| 341 | 341 | if (im == NULL && (access(file->path, R_OK) < 0 || | |
| 342 | 342 | (im = imlib_load_image(file->path)) == NULL)) | |
| 343 | 343 | { | |
| 344 | − | if (file->warn) | |
| 344 | + | if (file->flags & FF_WARN) | |
| 345 | 345 | warn("could not open image: %s", file->name); | |
| 346 | 346 | return false; | |
| 347 | 347 | } | |
| @@ -461,7 +461,7 @@ void tns_render(tns_t *tns) | |||
| 461 | 461 | t->y = y + (thumb_sizes[tns->zl] - t->h) / 2; | |
| 462 | 462 | imlib_context_set_image(t->im); | |
| 463 | 463 | imlib_render_image_on_drawable_at_size(t->x, t->y, t->w, t->h); | |
| 464 | − | if (tns->files[i].marked) | |
| 464 | + | if (tns->files[i].flags & FF_MARK) | |
| 465 | 465 | tns_mark(tns, i, true); | |
| 466 | 466 | } else { | |
| 467 | 467 | tns->loadnext = MIN(tns->loadnext, i); | |
| @@ -520,7 +520,7 @@ void tns_highlight(tns_t *tns, int n, bool hl) | |||
| 520 | 520 | win_draw_rect(win, t->x - oxy, t->y - oxy, t->w + owh, t->h + owh, | |
| 521 | 521 | false, tns->bw, col); | |
| 522 | 522 | ||
| 523 | − | if (tns->files[n].marked) | |
| 523 | + | if (tns->files[n].flags & FF_MARK) | |
| 524 | 524 | tns_mark(tns, n, true); | |
| 525 | 525 | } | |
| 526 | 526 | } | |
| @@ -64,12 +64,16 @@ typedef enum { | |||
| 64 | 64 | CURSOR_WATCH | |
| 65 | 65 | } cursor_t; | |
| 66 | 66 | ||
| 67 | + | typedef enum { | |
| 68 | + | FF_WARN = 1, | |
| 69 | + | FF_MARK = 2 | |
| 70 | + | } fileflags_t; | |
| 71 | + | ||
| 67 | 72 | typedef struct { | |
| 68 | 73 | const char *name; /* as given by user */ | |
| 69 | 74 | const char *path; /* always absolute */ | |
| 70 | 75 | const char *base; | |
| 71 | − | bool warn; | |
| 72 | − | bool marked; | |
| 76 | + | fileflags_t flags; | |
| 73 | 77 | } fileinfo_t; | |
| 74 | 78 | ||
| 75 | 79 | /* timeouts in milliseconds: */ | |