Krolyxon
Commit 5b01c15

Branches

Refs →

nsxiv5b01c15

Unified file index variable for image & thumbnail mode

Author
Bert Münnich
Date
2014-08-16 18:36 +0200
Committer
Bert Münnich · 2014-08-16 21:49 +0200
Parent
61f61ca
Commit
5b01c15176298b0335432e089d70e55c84379756

+65 −78

4 files changed

  1. M commands.c +18 −28
  2. M main.c +22 −26
  3. M thumbs.c +23 −22
  4. M thumbs.h +2 −2
M commands.c View
@@ -76,18 +76,17 @@ bool cg_switch_mode(arg_t a)
7676 {
7777 if (mode == MODE_IMAGE) {
7878 if (tns.thumbs == NULL)
79− tns_init(&tns, filecnt, &win);
79+ tns_init(&tns, filecnt, &win, &fileidx);
8080 img_close(&img, false);
8181 reset_timeout(reset_cursor);
8282 if (img.ss.on) {
8383 img.ss.on = false;
8484 reset_timeout(slideshow);
8585 }
86− tns.sel = fileidx;
8786 tns.dirty = true;
8887 mode = MODE_THUMB;
8988 } else {
90− load_image(tns.sel);
89+ load_image(fileidx);
9190 mode = MODE_IMAGE;
9291 }
9392 return true;
@@ -130,11 +129,9 @@ bool cg_reload_image(arg_t a)
130129 load_image(fileidx);
131130 } else {
132131 win_set_cursor(&win, CURSOR_WATCH);
133− if (!tns_load(&tns, tns.sel, &files[tns.sel], true, false)) {
134− remove_file(tns.sel, false);
132+ if (!tns_load(&tns, fileidx, &files[fileidx], true, false)) {
133+ remove_file(fileidx, false);
135134 tns.dirty = true;
136− if (tns.sel >= tns.cnt)
137− tns.sel = tns.cnt - 1;
138135 }
139136 }
140137 return true;
@@ -146,11 +143,9 @@ bool cg_remove_image(arg_t a)
146143 remove_file(fileidx, true);
147144 load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
148145 return true;
149− } else if (tns.sel < tns.cnt) {
150− remove_file(tns.sel, true);
146+ } else if (fileidx < tns.cnt) {
147+ remove_file(fileidx, true);
151148 tns.dirty = true;
152− if (tns.sel >= tns.cnt)
153− tns.sel = tns.cnt - 1;
154149 return true;
155150 } else {
156151 return false;
@@ -162,8 +157,8 @@ bool cg_first(arg_t a)
162157 if (mode == MODE_IMAGE && fileidx != 0) {
163158 load_image(0);
164159 return true;
165− } else if (mode == MODE_THUMB && tns.sel != 0) {
166− tns.sel = 0;
160+ } else if (mode == MODE_THUMB && fileidx != 0) {
161+ fileidx = 0;
167162 tns.dirty = true;
168163 return true;
169164 } else {
@@ -178,8 +173,8 @@ bool cg_n_or_last(arg_t a)
178173 if (mode == MODE_IMAGE && fileidx != n) {
179174 load_image(n);
180175 return true;
181− } else if (mode == MODE_THUMB && tns.sel != n) {
182− tns.sel = n;
176+ } else if (mode == MODE_THUMB && fileidx != n) {
177+ fileidx = n;
183178 tns.dirty = true;
184179 return true;
185180 } else {
@@ -199,11 +194,9 @@ bool cg_scroll_screen(arg_t a)
199194
200195 bool cg_toggle_image_mark(arg_t a)
201196 {
202− int sel = mode == MODE_IMAGE ? fileidx : tns.sel;
203−
204− files[sel].marked = !files[sel].marked;
197+ files[fileidx].marked = !files[fileidx].marked;
205198 if (mode == MODE_THUMB)
206− tns_mark(&tns, sel, files[sel].marked);
199+ tns_mark(&tns, fileidx, files[fileidx].marked);
207200 return true;
208201 }
209202
@@ -221,26 +214,23 @@ bool cg_reverse_marks(arg_t a)
221214 bool cg_navigate_marked(arg_t a)
222215 {
223216 long n = (long) a;
224− int d, i, cnt, sel, new;
217+ int d, i;
218+ int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
225219
226− if (mode == MODE_IMAGE)
227− cnt = filecnt, sel = new = fileidx;
228− else
229− cnt = tns.cnt, sel = new = tns.sel;
230220 if (prefix > 0)
231221 n *= prefix;
232222 d = n > 0 ? 1 : -1;
233− for (i = sel + d; n != 0 && i >= 0 && i < cnt; i += d) {
223+ for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
234224 if (files[i].marked) {
235225 n -= d;
236226 new = i;
237227 }
238228 }
239− if (new != sel) {
229+ if (new != fileidx) {
240230 if (mode == MODE_IMAGE) {
241231 load_image(new);
242232 } else {
243− tns.sel = new;
233+ fileidx = new;
244234 tns.dirty = true;
245235 }
246236 return true;
@@ -462,7 +452,7 @@ bool ct_move_sel(arg_t a)
462452 bool ct_reload_all(arg_t a)
463453 {
464454 tns_free(&tns);
465− tns_init(&tns, filecnt, &win);
455+ tns_init(&tns, filecnt, &win, &fileidx);
466456 return false;
467457 }
468458
M main.c View
@@ -184,6 +184,8 @@ void remove_file(int n, bool manual)
184184 filecnt--;
185185 if (n < tns.cnt)
186186 tns.cnt--;
187+ if (mode == MODE_THUMB && fileidx >= tns.cnt)
188+ fileidx = tns.cnt - 1;
187189 if (n < alternate)
188190 alternate--;
189191 }
@@ -340,7 +342,6 @@ void load_image(int new)
340342
341343 void update_info(void)
342344 {
343− int sel;
344345 unsigned int i, fn, fw, n;
345346 unsigned int llen = sizeof(win.bar.l), rlen = sizeof(win.bar.r);
346347 char *lt = win.bar.l, *rt = win.bar.r, title[TITLE_LEN];
@@ -348,23 +349,22 @@ void update_info(void)
348349 bool ow_info;
349350
350351 for (fw = 0, i = filecnt; i > 0; fw++, i /= 10);
351− sel = mode == MODE_IMAGE ? fileidx : tns.sel;
352352
353353 /* update window title */
354354 if (mode == MODE_THUMB) {
355355 win_set_title(&win, "sxiv");
356356 } else {
357− snprintf(title, sizeof(title), "sxiv - %s", files[sel].name);
357+ snprintf(title, sizeof(title), "sxiv - %s", files[fileidx].name);
358358 win_set_title(&win, title);
359359 }
360360
361361 /* update bar contents */
362362 if (win.bar.h == 0)
363363 return;
364− mark = files[sel].marked ? "* " : "";
364+ mark = files[fileidx].marked ? "* " : "";
365365 if (mode == MODE_THUMB) {
366366 if (tns.loadnext >= filecnt) {
367− n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, sel + 1, filecnt);
367+ n = snprintf(rt, rlen, "%s%0*d/%d", mark, fw, fileidx + 1, filecnt);
368368 ow_info = true;
369369 } else {
370370 snprintf(lt, llen, "Loading... %0*d/%d", fw, tns.loadnext, filecnt);
@@ -383,18 +383,18 @@ void update_info(void)
383383 n += snprintf(rt + n, rlen - n, "%0*d/%d | ",
384384 fn, img.multi.sel + 1, img.multi.cnt);
385385 }
386− n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, sel + 1, filecnt);
386+ n += snprintf(rt + n, rlen - n, "%0*d/%d", fw, fileidx + 1, filecnt);
387387 ow_info = info.cmd == NULL;
388388 }
389389 if (ow_info) {
390− fn = strlen(files[sel].name);
390+ fn = strlen(files[fileidx].name);
391391 if (fn < llen &&
392− win_textwidth(files[sel].name, fn, true) +
392+ win_textwidth(files[fileidx].name, fn, true) +
393393 win_textwidth(rt, n, true) < win.w)
394394 {
395− strncpy(lt, files[sel].name, llen);
395+ strncpy(lt, files[fileidx].name, llen);
396396 } else {
397− strncpy(lt, files[sel].base, llen);
397+ strncpy(lt, files[fileidx].base, llen);
398398 }
399399 }
400400 }
@@ -464,7 +464,7 @@ void clear_resize(void)
464464 void run_key_handler(const char *key, unsigned int mask)
465465 {
466466 pid_t pid;
467− int retval, status, n = mode == MODE_IMAGE ? fileidx : tns.sel;
467+ int retval, status;
468468 char kstr[32], oldbar[sizeof(win.bar.l)];
469469 bool restore_bar = mode == MODE_IMAGE && info.cmd != NULL;
470470 struct stat oldst, newst;
@@ -489,10 +489,10 @@ void run_key_handler(const char *key, unsigned int mask)
489489 strncpy(win.bar.l, "Running key handler...", sizeof(win.bar.l));
490490 win_draw(&win);
491491 win_set_cursor(&win, CURSOR_WATCH);
492− stat(files[n].path, &oldst);
492+ stat(files[fileidx].path, &oldst);
493493
494494 if ((pid = fork()) == 0) {
495− execl(keyhandler.cmd, keyhandler.cmd, kstr, files[n].path, NULL);
495+ execl(keyhandler.cmd, keyhandler.cmd, kstr, files[fileidx].path, NULL);
496496 warn("could not exec key handler");
497497 exit(EXIT_FAILURE);
498498 } else if (pid < 0) {
@@ -504,7 +504,7 @@ void run_key_handler(const char *key, unsigned int mask)
504504 if (WIFEXITED(status) == 0 || retval != 0)
505505 warn("key handler exited with non-zero return value: %d", retval);
506506
507− if (stat(files[n].path, &newst) == 0 &&
507+ if (stat(files[fileidx].path, &newst) == 0 &&
508508 memcmp(&oldst.st_mtime, &newst.st_mtime, sizeof(oldst.st_mtime)) == 0)
509509 {
510510 /* file has not changed */
@@ -518,13 +518,11 @@ void run_key_handler(const char *key, unsigned int mask)
518518 img_close(&img, true);
519519 load_image(fileidx);
520520 }
521− if (!tns_load(&tns, n, &files[n], true, mode == MODE_IMAGE) &&
521+ if (!tns_load(&tns, fileidx, &files[fileidx], true, mode == MODE_IMAGE) &&
522522 mode == MODE_THUMB)
523523 {
524− remove_file(tns.sel, false);
524+ remove_file(fileidx, false);
525525 tns.dirty = true;
526− if (tns.sel >= tns.cnt)
527− tns.sel = tns.cnt - 1;
528526 }
529527 end:
530528 if (restore_bar)
@@ -610,16 +608,16 @@ void on_buttonpress(XButtonEvent *bev)
610608 switch (bev->button) {
611609 case Button1:
612610 if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
613− if (sel != tns.sel) {
614− tns_highlight(&tns, tns.sel, false);
611+ if (sel != fileidx) {
612+ tns_highlight(&tns, fileidx, false);
615613 tns_highlight(&tns, sel, true);
616− tns.sel = sel;
614+ fileidx = sel;
617615 firstclick = bev->time;
618616 redraw();
619617 } else if (bev->time - firstclick <= TO_DOUBLE_CLICK) {
620618 mode = MODE_IMAGE;
621619 set_timeout(reset_cursor, TO_CURSOR_HIDE, true);
622− load_image(tns.sel);
620+ load_image(fileidx);
623621 redraw();
624622 } else {
625623 firstclick = bev->time;
@@ -665,8 +663,6 @@ void run(void)
665663 tns.cnt++;
666664 } else {
667665 remove_file(tns.loadnext, false);
668− if (tns.sel > 0 && tns.sel >= tns.cnt)
669− tns.sel--;
670666 }
671667 while (tns.loadnext < filecnt && tns.thumbs[tns.loadnext].loaded)
672668 tns.loadnext++;
@@ -766,7 +762,7 @@ int main(int argc, char **argv)
766762 parse_options(argc, argv);
767763
768764 if (options->clean_cache) {
769− tns_init(&tns, 0, NULL);
765+ tns_init(&tns, 0, NULL, NULL);
770766 tns_clean_cache(&tns);
771767 exit(EXIT_SUCCESS);
772768 }
@@ -859,7 +855,7 @@ int main(int argc, char **argv)
859855
860856 if (options->thumb_mode) {
861857 mode = MODE_THUMB;
862− tns_init(&tns, filecnt, &win);
858+ tns_init(&tns, filecnt, &win, &fileidx);
863859 while (!tns_load(&tns, 0, &files[0], false, false))
864860 remove_file(0, false);
865861 tns.cnt = 1;
M thumbs.c View
@@ -150,7 +150,7 @@ void tns_clean_cache(tns_t *tns)
150150 }
151151
152152
153−void tns_init(tns_t *tns, int cnt, win_t *win)
153+void tns_init(tns_t *tns, int cnt, win_t *win, int *sel)
154154 {
155155 int len;
156156 const char *homedir, *dsuffix = "";
@@ -165,7 +165,8 @@ void tns_init(tns_t *tns, int cnt, win_t *win)
165165 tns->thumbs = NULL;
166166 }
167167 tns->cap = cnt;
168− tns->cnt = tns->loadnext = tns->first = tns->sel = 0;
168+ tns->cnt = tns->loadnext = tns->first = 0;
169+ tns->sel = sel;
169170 tns->win = win;
170171 tns->dirty = false;
171172
@@ -338,21 +339,21 @@ void tns_check_view(tns_t *tns, bool scrolled)
338339 return;
339340
340341 tns->first -= tns->first % tns->cols;
341− r = tns->sel % tns->cols;
342+ r = *tns->sel % tns->cols;
342343
343344 if (scrolled) {
344345 /* move selection into visible area */
345− if (tns->sel >= tns->first + tns->cols * tns->rows)
346− tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
347− else if (tns->sel < tns->first)
348− tns->sel = tns->first + r;
346+ if (*tns->sel >= tns->first + tns->cols * tns->rows)
347+ *tns->sel = tns->first + r + tns->cols * (tns->rows - 1);
348+ else if (*tns->sel < tns->first)
349+ *tns->sel = tns->first + r;
349350 } else {
350351 /* scroll to selection */
351− if (tns->first + tns->cols * tns->rows <= tns->sel) {
352− tns->first = tns->sel - r - tns->cols * (tns->rows - 1);
352+ if (tns->first + tns->cols * tns->rows <= *tns->sel) {
353+ tns->first = *tns->sel - r - tns->cols * (tns->rows - 1);
353354 tns->dirty = true;
354− } else if (tns->first > tns->sel) {
355− tns->first = tns->sel - r;
355+ } else if (tns->first > *tns->sel) {
356+ tns->first = *tns->sel - r;
356357 tns->dirty = true;
357358 }
358359 }
@@ -409,7 +410,7 @@ void tns_render(tns_t *tns)
409410 }
410411 }
411412 tns->dirty = false;
412− tns_highlight(tns, tns->sel, true);
413+ tns_highlight(tns, *tns->sel, true);
413414 }
414415
415416 void tns_mark(tns_t *tns, int n, bool mark)
@@ -423,7 +424,7 @@ void tns_mark(tns_t *tns, int n, bool mark)
423424 win_t *win = tns->win;
424425 int x = t->x, y = t->y, w = t->w, h = t->h;
425426
426− if (mark || n == tns->sel)
427+ if (mark || n == *tns->sel)
427428 col = win->selcol;
428429 else if (win->fullscreen)
429430 col = win->fscol;
@@ -473,33 +474,33 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
473474 if (tns == NULL || tns->thumbs == NULL)
474475 return false;
475476
476− old = tns->sel;
477+ old = *tns->sel;
477478 cnt = cnt > 1 ? cnt : 1;
478479
479480 switch (dir) {
480481 case DIR_UP:
481− tns->sel = MAX(tns->sel - cnt * tns->cols, tns->sel % tns->cols);
482+ *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
482483 break;
483484 case DIR_DOWN:
484485 max = tns->cols * ((tns->cnt - 1) / tns->cols) +
485− MIN((tns->cnt - 1) % tns->cols, tns->sel % tns->cols);
486− tns->sel = MIN(tns->sel + cnt * tns->cols, max);
486+ MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
487+ *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
487488 break;
488489 case DIR_LEFT:
489− tns->sel = MAX(tns->sel - cnt, 0);
490+ *tns->sel = MAX(*tns->sel - cnt, 0);
490491 break;
491492 case DIR_RIGHT:
492− tns->sel = MIN(tns->sel + cnt, tns->cnt - 1);
493+ *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
493494 break;
494495 }
495496
496− if (tns->sel != old) {
497+ if (*tns->sel != old) {
497498 tns_highlight(tns, old, false);
498499 tns_check_view(tns, false);
499500 if (!tns->dirty)
500− tns_highlight(tns, tns->sel, true);
501+ tns_highlight(tns, *tns->sel, true);
501502 }
502− return tns->sel != old;
503+ return *tns->sel != old;
503504 }
504505
505506 bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
M thumbs.h View
@@ -41,7 +41,7 @@ typedef struct {
4141 int cnt;
4242 int loadnext;
4343 int first;
44− int sel;
44+ int *sel;
4545
4646 win_t *win;
4747 int x;
@@ -54,7 +54,7 @@ typedef struct {
5454
5555 void tns_clean_cache(tns_t*);
5656
57−void tns_init(tns_t*, int, win_t*);
57+void tns_init(tns_t*, int, win_t*, int*);
5858 void tns_free(tns_t*);
5959
6060 bool tns_load(tns_t*, int, const fileinfo_t*, bool, bool);