Unified file count variable for image & thumbnail mode

This commit is contained in:
Bert Münnich 2014-09-30 21:54:17 +02:00
parent b4096c59e4
commit ddd028eb3e
5 changed files with 42 additions and 49 deletions

View file

@ -1,4 +1,4 @@
VERSION = git-20140929 VERSION = git-20140930
PREFIX = /usr/local PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man MANPREFIX = $(PREFIX)/share/man

View file

@ -77,7 +77,7 @@ bool cg_switch_mode(arg_t a)
{ {
if (mode == MODE_IMAGE) { if (mode == MODE_IMAGE) {
if (tns.thumbs == NULL) if (tns.thumbs == NULL)
tns_init(&tns, files, filecnt, &fileidx, &win); tns_init(&tns, files, &filecnt, &fileidx, &win);
img_close(&img, false); img_close(&img, false);
reset_timeout(reset_cursor); reset_timeout(reset_cursor);
if (img.ss.on) { if (img.ss.on) {
@ -140,17 +140,12 @@ bool cg_reload_image(arg_t a)
bool cg_remove_image(arg_t a) bool cg_remove_image(arg_t a)
{ {
if (mode == MODE_IMAGE) { remove_file(fileidx, true);
remove_file(fileidx, true); if (mode == MODE_IMAGE)
load_image(fileidx >= filecnt ? filecnt - 1 : fileidx); load_image(fileidx);
return true; else
} else if (fileidx < tns.cnt) {
remove_file(fileidx, true);
tns.dirty = true; tns.dirty = true;
return true; return true;
} else {
return false;
}
} }
bool cg_first(arg_t a) bool cg_first(arg_t a)
@ -245,12 +240,12 @@ bool cg_navigate_marked(arg_t a)
{ {
long n = (long) a; long n = (long) a;
int d, i; int d, i;
int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx; int new = fileidx;
if (prefix > 0) if (prefix > 0)
n *= prefix; n *= prefix;
d = n > 0 ? 1 : -1; d = n > 0 ? 1 : -1;
for (i = fileidx + d; n != 0 && i >= 0 && i < cnt; i += d) { for (i = fileidx + d; n != 0 && i >= 0 && i < filecnt; i += d) {
if (files[i].marked) { if (files[i].marked) {
n -= d; n -= d;
new = i; new = i;
@ -471,7 +466,7 @@ bool ct_move_sel(arg_t a)
bool ct_reload_all(arg_t a) bool ct_reload_all(arg_t a)
{ {
tns_free(&tns); tns_free(&tns);
tns_init(&tns, files, filecnt, &fileidx, &win); tns_init(&tns, files, &filecnt, &fileidx, &win);
tns.dirty = true; tns.dirty = true;
return true; return true;
} }

17
main.c
View file

@ -176,19 +176,16 @@ void remove_file(int n, bool manual)
free((void*) files[n].path); free((void*) files[n].path);
free((void*) files[n].name); free((void*) files[n].name);
if (n + 1 < filecnt) if (n + 1 < filecnt) {
memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t)); memmove(files + n, files + n + 1, (filecnt - n - 1) * sizeof(fileinfo_t));
if (n + 1 < tns.cnt) { memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
memmove(tns.thumbs + n, tns.thumbs + n + 1, (tns.cnt - n - 1) *
sizeof(thumb_t)); sizeof(thumb_t));
memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t)); memset(tns.thumbs + filecnt - 1, 0, sizeof(thumb_t));
} }
filecnt--; filecnt--;
if (n < tns.cnt) if (fileidx >= filecnt)
tns.cnt--; fileidx = filecnt - 1;
if (mode == MODE_THUMB && tns.cnt > 0 && fileidx >= tns.cnt)
fileidx = tns.cnt - 1;
if (n < alternate) if (n < alternate)
alternate--; alternate--;
} }
@ -777,7 +774,7 @@ int main(int argc, char **argv)
parse_options(argc, argv); parse_options(argc, argv);
if (options->clean_cache) { if (options->clean_cache) {
tns_init(&tns, NULL, 0, NULL, NULL); tns_init(&tns, NULL, NULL, NULL, NULL);
tns_clean_cache(&tns); tns_clean_cache(&tns);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
@ -869,7 +866,7 @@ int main(int argc, char **argv)
if (options->thumb_mode) { if (options->thumb_mode) {
mode = MODE_THUMB; mode = MODE_THUMB;
tns_init(&tns, files, filecnt, &fileidx, &win); tns_init(&tns, files, &filecnt, &fileidx, &win);
while (!tns_load(&tns, 0, false)) while (!tns_load(&tns, 0, false))
remove_file(0, false); remove_file(0, false);
} else { } else {

View file

@ -152,7 +152,8 @@ void tns_clean_cache(tns_t *tns)
} }
void tns_init(tns_t *tns, const fileinfo_t *files, int cnt, int *sel, win_t *win) void tns_init(tns_t *tns, const fileinfo_t *files, const int *cnt, int *sel,
win_t *win)
{ {
int len; int len;
const char *homedir, *dsuffix = ""; 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
if (tns == NULL) if (tns == NULL)
return; return;
if (cnt > 0) { if (*cnt > 0) {
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t)); tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, cnt * sizeof(thumb_t)); memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
} else { } else {
tns->thumbs = NULL; tns->thumbs = NULL;
} }
@ -198,7 +199,7 @@ void tns_free(tns_t *tns)
return; return;
if (tns->thumbs != NULL) { if (tns->thumbs != NULL) {
for (i = 0; i < tns->cnt; i++) { for (i = 0; i < *tns->cnt; i++) {
if (tns->thumbs[i].im != NULL) { if (tns->thumbs[i].im != NULL) {
imlib_context_set_image(tns->thumbs[i].im); imlib_context_set_image(tns->thumbs[i].im);
imlib_free_image(); imlib_free_image();
@ -247,7 +248,7 @@ bool tns_load(tns_t *tns, int n, bool force)
if (tns == NULL || tns->thumbs == NULL) if (tns == NULL || tns->thumbs == NULL)
return false; return false;
if (n < 0 || n >= tns->cnt) if (n < 0 || n >= *tns->cnt)
return false; return false;
file = &tns->files[n]; file = &tns->files[n];
if (file->name == NULL || file->path == NULL) if (file->name == NULL || file->path == NULL)
@ -351,7 +352,7 @@ void tns_unload(tns_t *tns, int n)
if (tns == NULL || tns->thumbs == NULL) if (tns == NULL || tns->thumbs == NULL)
return; return;
if (n < 0 || n >= tns->cnt) if (n < 0 || n >= *tns->cnt)
return; return;
t = &tns->thumbs[n]; t = &tns->thumbs[n];
@ -409,13 +410,13 @@ void tns_render(tns_t *tns)
tns->cols = MAX(1, win->w / tns->dim); tns->cols = MAX(1, win->w / tns->dim);
tns->rows = MAX(1, win->h / tns->dim); tns->rows = MAX(1, win->h / tns->dim);
if (tns->cnt < tns->cols * tns->rows) { if (*tns->cnt < tns->cols * tns->rows) {
tns->first = 0; tns->first = 0;
cnt = tns->cnt; cnt = *tns->cnt;
} else { } else {
tns_check_view(tns, false); tns_check_view(tns, false);
cnt = tns->cols * tns->rows; cnt = tns->cols * tns->rows;
if ((r = tns->first + cnt - tns->cnt) >= tns->cols) if ((r = tns->first + cnt - *tns->cnt) >= tns->cols)
tns->first -= r - r % tns->cols; tns->first -= r - r % tns->cols;
if (r > 0) if (r > 0)
cnt -= r % tns->cols; cnt -= r % tns->cols;
@ -423,7 +424,7 @@ void tns_render(tns_t *tns)
r = cnt % tns->cols ? 1 : 0; r = cnt % tns->cols ? 1 : 0;
tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3; tns->x = x = (win->w - MIN(cnt, tns->cols) * tns->dim) / 2 + tns->bw + 3;
tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3; tns->y = y = (win->h - (cnt / tns->cols + r) * tns->dim) / 2 + tns->bw + 3;
tns->loadnext = tns->cnt; tns->loadnext = *tns->cnt;
tns->end = tns->first + cnt; tns->end = tns->first + cnt;
for (i = tns->r_first; i < tns->r_end; i++) { for (i = tns->r_first; i < tns->r_end; i++) {
@ -461,7 +462,7 @@ void tns_mark(tns_t *tns, int n, bool mark)
if (tns == NULL || tns->thumbs == NULL || tns->win == NULL) if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
return; return;
if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) { if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
win_t *win = tns->win; win_t *win = tns->win;
thumb_t *t = &tns->thumbs[n]; thumb_t *t = &tns->thumbs[n];
unsigned long col = win->fullscreen ? win->fscol : win->bgcol; unsigned long col = win->fullscreen ? win->fscol : win->bgcol;
@ -485,7 +486,7 @@ void tns_highlight(tns_t *tns, int n, bool hl)
if (tns == NULL || tns->thumbs == NULL || tns->win == NULL) if (tns == NULL || tns->thumbs == NULL || tns->win == NULL)
return; return;
if (n >= 0 && n < tns->cnt && tns->thumbs[n].im != NULL) { if (n >= 0 && n < *tns->cnt && tns->thumbs[n].im != NULL) {
win_t *win = tns->win; win_t *win = tns->win;
thumb_t *t = &tns->thumbs[n]; thumb_t *t = &tns->thumbs[n];
unsigned long col; unsigned long col;
@ -519,15 +520,15 @@ bool tns_move_selection(tns_t *tns, direction_t dir, int cnt)
*tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols); *tns->sel = MAX(*tns->sel - cnt * tns->cols, *tns->sel % tns->cols);
break; break;
case DIR_DOWN: case DIR_DOWN:
max = tns->cols * ((tns->cnt - 1) / tns->cols) + max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols); MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
*tns->sel = MIN(*tns->sel + cnt * tns->cols, max); *tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
break; break;
case DIR_LEFT: case DIR_LEFT:
*tns->sel = MAX(*tns->sel - cnt, 0); *tns->sel = MAX(*tns->sel - cnt, 0);
break; break;
case DIR_RIGHT: case DIR_RIGHT:
*tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1); *tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
break; break;
} }
@ -551,9 +552,9 @@ bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
d = tns->cols * (screen ? tns->rows : 1); d = tns->cols * (screen ? tns->rows : 1);
if (dir == DIR_DOWN) { if (dir == DIR_DOWN) {
max = tns->cnt - tns->cols * tns->rows; max = *tns->cnt - tns->cols * tns->rows;
if (tns->cnt % tns->cols != 0) if (*tns->cnt % tns->cols != 0)
max += tns->cols - tns->cnt % tns->cols; max += tns->cols - *tns->cnt % tns->cols;
tns->first = MIN(tns->first + d, max); tns->first = MIN(tns->first + d, max);
} else if (dir == DIR_UP) { } else if (dir == DIR_UP) {
tns->first = MAX(tns->first - d, 0); tns->first = MAX(tns->first - d, 0);
@ -582,7 +583,7 @@ bool tns_zoom(tns_t *tns, int d)
tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6; tns->dim = thumb_sizes[tns->zl] + 2 * tns->bw + 6;
if (tns->zl != oldzl) { if (tns->zl != oldzl) {
for (i = 0; i < tns->cnt; i++) for (i = 0; i < *tns->cnt; i++)
tns_unload(tns, i); tns_unload(tns, i);
tns->dirty = true; tns->dirty = true;
} }
@ -600,7 +601,7 @@ int tns_translate(tns_t *tns, int x, int y)
n = tns->first + (y - tns->y) / tns->dim * tns->cols + n = tns->first + (y - tns->y) / tns->dim * tns->cols +
(x - tns->x) / tns->dim; (x - tns->x) / tns->dim;
if (n >= tns->cnt) if (n >= *tns->cnt)
n = -1; n = -1;
return n; return n;

View file

@ -36,11 +36,11 @@ typedef struct {
typedef struct { typedef struct {
const fileinfo_t *files; const fileinfo_t *files;
thumb_t *thumbs; thumb_t *thumbs;
int cnt; const int *cnt;
int *sel;
int loadnext; int loadnext;
int first, end; int first, end;
int r_first, r_end; int r_first, r_end;
int *sel;
win_t *win; win_t *win;
int x; int x;
@ -56,7 +56,7 @@ typedef struct {
void tns_clean_cache(tns_t*); void tns_clean_cache(tns_t*);
void tns_init(tns_t*, const fileinfo_t*, int, int*, win_t*); void tns_init(tns_t*, const fileinfo_t*, const int*, int*, win_t*);
void tns_free(tns_t*); void tns_free(tns_t*);
bool tns_load(tns_t*, int, bool); bool tns_load(tns_t*, int, bool);