Krolyxon
Commit ddd028e

Branches

Refs →

nsxivddd028e

Unified file count variable for image & thumbnail mode

Author
Bert Münnich
Date
2014-09-30 21:54 +0200
Parent
b4096c5
Commit
ddd028eb3ea628855b4bb35ca7fb40562bd87861

+42 −49

5 files changed

  1. M Makefile +1 −1
  2. M commands.c +9 −14
  3. M main.c +7 −10
  4. M thumbs.c +22 −21
  5. M thumbs.h +3 −3
M Makefile View
@@ -1,4 +1,4 @@
1−VERSION = git-20140929
1+VERSION = git-20140930
22
33 PREFIX = /usr/local
44 MANPREFIX = $(PREFIX)/share/man
M commands.c View
@@ -77,7 +77,7 @@ bool cg_switch_mode(arg_t a)
7777 {
7878 if (mode == MODE_IMAGE) {
7979 if (tns.thumbs == NULL)
80− tns_init(&tns, files, filecnt, &fileidx, &win);
80+ tns_init(&tns, files, &filecnt, &fileidx, &win);
8181 img_close(&img, false);
8282 reset_timeout(reset_cursor);
8383 if (img.ss.on) {
@@ -140,17 +140,12 @@ bool cg_reload_image(arg_t a)
140140
141141 bool cg_remove_image(arg_t a)
142142 {
143− if (mode == MODE_IMAGE) {
144− remove_file(fileidx, true);
145− load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
146− return true;
147− } else if (fileidx < tns.cnt) {
148− remove_file(fileidx, true);
143+ remove_file(fileidx, true);
144+ if (mode == MODE_IMAGE)
145+ load_image(fileidx);
146+ else
149147 tns.dirty = true;
150− return true;
151− } else {
152− return false;
153− }
148+ return true;
154149 }
155150
156151 bool cg_first(arg_t a)
@@ -245,12 +240,12 @@ bool cg_navigate_marked(arg_t a)
245240 {
246241 long n = (long) a;
247242 int d, i;
248− int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
243+ int new = fileidx;
249244
250245 if (prefix > 0)
251246 n *= prefix;
252247 d = n > 0 ? 1 : -1;
253− for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) {
248+ for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
254249 if (files[i].marked) {
255250 n -= d;
256251 new = i;
@@ -471,7 +466,7 @@ bool ct_move_sel(arg_t a)
471466 bool ct_reload_all(arg_t a)
472467 {
473468 tns_free(&tns);
474− tns_init(&tns, files, filecnt, &fileidx, &win);
469+ tns_init(&tns, files, &filecnt, &fileidx, &win);
475470 tns.dirty = true;
476471 return true;
477472 }
M main.c View
@@ -176,19 +176,16 @@ void remove_file(int n, bool manual)
176176 free((void*) files[n].path);
177177 free((void*) files[n].name);
178178
179− if (n + 1 < filecnt)
179+ if (n + 1 < filecnt) {
180180 memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
181− if (n + 1 < tns.cnt) {
182− memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
181+ memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
183182 sizeof(thumb_t));
184− memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
183+ memset(tns.thumbs + filecnt - 1, 0, sizeof(thumb_t));
185184 }
186185
187186 filecnt--;
188− if (n < tns.cnt)
189− tns.cnt--;
190− if (mode == MODE_THUMB && tns.cnt > 0 && fileidx >= tns.cnt)
191− fileidx = tns.cnt - 1;
187+ if (fileidx >= filecnt)
188+ fileidx = filecnt - 1;
192189 if (n < alternate)
193190 alternate--;
194191 }
@@ -777,7 +774,7 @@ int main(int argc, char **argv)
777774 parse_options(argc, argv);
778775
779776 if (options->clean_cache) {
780− tns_init(&tns, NULL, 0, NULL, NULL);
777+ tns_init(&tns, NULL, NULL, NULL, NULL);
781778 tns_clean_cache(&tns);
782779 exit(EXIT_SUCCESS);
783780 }
@@ -869,7 +866,7 @@ int main(int argc, char **argv)
869866
870867 if (options->thumb_mode) {
871868 mode = MODE_THUMB;
872− tns_init(&tns, files, filecnt, &fileidx, &win);
869+ tns_init(&tns, files, &filecnt, &fileidx, &win);
873870 while (!tns_load(&tns, 0, false))
874871 remove_file(0, false);
875872 } else {
M thumbs.c View
@@ -152,7 +152,8 @@ void tns_clean_cache(tns_t *tns)
152152 }
153153
154154
155−void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win)
155+void tns_init(tns_t *tns, const fileinfo_t *files, const int *cnt, int *sel,
156+ win_t *win)
156157 {
157158 int len;
158159 const char *homedir, *dsuffix = "";
@@ -160,9 +161,9 @@ void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win
160161 if (tns == NULL)
161162 return;
162163
163− if (cnt > 0) {
164− tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
165− memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
164+ if (*cnt > 0) {
165+ tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
166+ memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
166167 } else {
167168 tns->thumbs = NULL;
168169 }
@@ -198,7 +199,7 @@ void tns_free(tns_t *tns)
198199 return;
199200
200201 if (tns->thumbs != NULL) {
201− for (i = 0; i < tns->cnt; i++) {
202+ for (i = 0; i < *tns->cnt; i++) {
202203 if (tns->thumbs[i].im != NULL) {
203204 imlib_context_set_image(tns->thumbs[i].im);
204205 imlib_free_image();
@@ -247,7 +248,7 @@ bool tns_load(tns_t *tns, int n, bool force)
247248
248249 if (tns == NULL || tns->thumbs == NULL)
249250 return false;
250− if (n < 0 || n >= tns->cnt)
251+ if (n < 0 || n >= *tns->cnt)
251252 return false;
252253 file = &tns->files[n];
253254 if (file->name == NULL || file->path == NULL)
@@ -351,7 +352,7 @@ void tns_unload(tns_t *tns, int n)
351352
352353 if (tns == NULL || tns->thumbs == NULL)
353354 return;
354− if (n < 0 || n >= tns->cnt)
355+ if (n < 0 || n >= *tns->cnt)
355356 return;
356357
357358 t = &tns->thumbs[n];
@@ -409,13 +410,13 @@ void tns_render(tns_t *tns)
409410 tns->cols = MAX(1, win->w / tns->dim);
410411 tns->rows = MAX(1, win->h / tns->dim);
411412
412− if (tns->cnt < tns->cols * tns->rows) {
413+ if (*tns->cnt < tns->cols * tns->rows) {
413414 tns->first = 0;
414− cnt = tns->cnt;
415+ cnt = *tns->cnt;
415416 } else {
416417 tns_check_view(tns, false);
417418 cnt = tns->cols * tns->rows;
418− if ((r = tns->first + cnt - tns->cnt) >= tns->cols)
419+ if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
419420 tns->first -= r - r % tns->cols;
420421 if (r > 0)
421422 cnt -= r % tns->cols;
@@ -423,7 +424,7 @@ void tns_render(tns_t *tns)
423424 r = cnt % tns->cols ? 1 : 0;
424425 tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
425426 tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
426− tns->loadnext = tns->cnt;
427+ tns->loadnext = *tns->cnt;
427428 tns->end = tns->first + cnt;
428429
429430 for (i = tns->r_first; i < tns->r_end; i++) {
@@ -461,7 +462,7 @@ void tns_mark(tns_t *tns, int n, bool mark)
461462 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
462463 return;
463464
464− if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
465+ if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
465466 win_t *win = tns->win;
466467 thumb_t *t = &tns->thumbs[n];
467468 unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
@@ -485,7 +486,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
485486 if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
486487 return;
487488
488− if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) {
489+ if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
489490 win_t *win = tns->win;
490491 thumb_t *t = &tns->thumbs[n];
491492 unsigned long col;
@@ -519,15 +520,15 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
519520 *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
520521 break;
521522 case DIR_DOWN:
522− max = tns->cols * ((tns->cnt - 1) / tns->cols) +
523− MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
523+ max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
524+ MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
524525 *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
525526 break;
526527 case DIR_LEFT:
527528 *tns->sel = MAX(*tns->sel - cnt, 0);
528529 break;
529530 case DIR_RIGHT:
530− *tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
531+ *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
531532 break;
532533 }
533534
@@ -551,9 +552,9 @@ bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
551552 d = tns->cols * (screen ? tns->rows : 1);
552553
553554 if (dir == DIR_DOWN) {
554− max = tns->cnt - tns->cols * tns->rows;
555− if (tns->cnt % tns->cols != 0)
556− max += tns->cols - tns->cnt % tns->cols;
555+ max = *tns->cnt - tns->cols * tns->rows;
556+ if (*tns->cnt % tns->cols != 0)
557+ max += tns->cols - *tns->cnt % tns->cols;
557558 tns->first = MIN(tns->first + d, max);
558559 } else if (dir == DIR_UP) {
559560 tns->first = MAX(tns->first - d, 0);
@@ -582,7 +583,7 @@ bool tns_zoom(tns_t *tns, int d)
582583 tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
583584
584585 if (tns->zl != oldzl) {
585− for (i = 0; i < tns->cnt; i++)
586+ for (i = 0; i < *tns->cnt; i++)
586587 tns_unload(tns, i);
587588 tns->dirty = true;
588589 }
@@ -600,7 +601,7 @@ int tns_translate(tns_t *tns, int x, int y)
600601
601602 n = tns->first + (y - tns->y) / tns->dim * tns->cols +
602603 (x - tns->x) / tns->dim;
603− if (n >= tns->cnt)
604+ if (n >= *tns->cnt)
604605 n = -1;
605606
606607 return n;
M thumbs.h View
@@ -36,11 +36,11 @@ typedef struct {
3636 typedef struct {
3737 const fileinfo_t *files;
3838 thumb_t *thumbs;
39− int cnt;
39+ const int *cnt;
40+ int *sel;
4041 int loadnext;
4142 int first, end;
4243 int r_first, r_end;
43− int *sel;
4444
4545 win_t *win;
4646 int x;
@@ -56,7 +56,7 @@ typedef struct {
5656
5757 void tns_clean_cache(tns_t*);
5858
59−void tns_init(tns_t*, const fileinfo_t*, int, int*, win_t*);
59+void tns_init(tns_t*, const fileinfo_t*, const int*, int*, win_t*);
6060 void tns_free(tns_t*);
6161
6262 bool tns_load(tns_t*, int, bool);