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
MANPREFIX = $(PREFIX)/share/man

View File

@ -77,7 +77,7 @@ bool cg_switch_mode(arg_t a)
{
if (mode == MODE_IMAGE) {
if (tns.thumbs == NULL)
tns_init(&tns, files, filecnt, &fileidx, &win);
tns_init(&tns, files, &filecnt, &fileidx, &win);
img_close(&img, false);
reset_timeout(reset_cursor);
if (img.ss.on) {
@ -140,17 +140,12 @@ bool cg_reload_image(arg_t a)
bool cg_remove_image(arg_t a)
{
if (mode == MODE_IMAGE) {
remove_file(fileidx, true);
load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
return true;
} else if (fileidx < tns.cnt) {
remove_file(fileidx, true);
if (mode == MODE_IMAGE)
load_image(fileidx);
else
tns.dirty = true;
return true;
} else {
return false;
}
}
bool cg_first(arg_t a)
@ -245,12 +240,12 @@ bool cg_navigate_marked(arg_t a)
{
long n = (long) a;
int d, i;
int cnt = mode == MODE_IMAGE ? filecnt : tns.cnt, new = fileidx;
int new = fileidx;
if (prefix > 0)
n *= prefix;
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) {
n -= d;
new = i;
@ -471,7 +466,7 @@ bool ct_move_sel(arg_t a)
bool ct_reload_all(arg_t a)
{
tns_free(&tns);
tns_init(&tns, files, filecnt, &fileidx, &win);
tns_init(&tns, files, &filecnt, &fileidx, &win);
tns.dirty = 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].name);
if (n + 1 < filecnt)
if (n + 1 < filecnt) {
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, (tns.cnt - n - 1) *
memmove(tns.thumbs + n, tns.thumbs + n + 1, (filecnt - n - 1) *
sizeof(thumb_t));
memset(tns.thumbs + tns.cnt - 1, 0, sizeof(thumb_t));
memset(tns.thumbs + filecnt - 1, 0, sizeof(thumb_t));
}
filecnt--;
if (n < tns.cnt)
tns.cnt--;
if (mode == MODE_THUMB && tns.cnt > 0 && fileidx >= tns.cnt)
fileidx = tns.cnt - 1;
if (fileidx >= filecnt)
fileidx = filecnt - 1;
if (n < alternate)
alternate--;
}
@ -777,7 +774,7 @@ int main(int argc, char **argv)
parse_options(argc, argv);
if (options->clean_cache) {
tns_init(&tns, NULL, 0, NULL, NULL);
tns_init(&tns, NULL, NULL, NULL, NULL);
tns_clean_cache(&tns);
exit(EXIT_SUCCESS);
}
@ -869,7 +866,7 @@ int main(int argc, char **argv)
if (options->thumb_mode) {
mode = MODE_THUMB;
tns_init(&tns, files, filecnt, &fileidx, &win);
tns_init(&tns, files, &filecnt, &fileidx, &win);
while (!tns_load(&tns, 0, false))
remove_file(0, false);
} 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;
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)
return;
if (cnt > 0) {
tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, cnt * sizeof(thumb_t));
if (*cnt > 0) {
tns->thumbs = (thumb_t*) s_malloc(*cnt * sizeof(thumb_t));
memset(tns->thumbs, 0, *cnt * sizeof(thumb_t));
} else {
tns->thumbs = NULL;
}
@ -198,7 +199,7 @@ void tns_free(tns_t *tns)
return;
if (tns->thumbs != NULL) {
for (i = 0; i < tns->cnt; i++) {
for (i = 0; i < *tns->cnt; i++) {
if (tns->thumbs[i].im != NULL) {
imlib_context_set_image(tns->thumbs[i].im);
imlib_free_image();
@ -247,7 +248,7 @@ bool tns_load(tns_t *tns, int n, bool force)
if (tns == NULL || tns->thumbs == NULL)
return false;
if (n < 0 || n >= tns->cnt)
if (n < 0 || n >= *tns->cnt)
return false;
file = &tns->files[n];
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)
return;
if (n < 0 || n >= tns->cnt)
if (n < 0 || n >= *tns->cnt)
return;
t = &tns->thumbs[n];
@ -409,13 +410,13 @@ void tns_render(tns_t *tns)
tns->cols = MAX(1, win->w / 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;
cnt = tns->cnt;
cnt = *tns->cnt;
} else {
tns_check_view(tns, false);
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;
if (r > 0)
cnt -= r % tns->cols;
@ -423,7 +424,7 @@ void tns_render(tns_t *tns)
r = cnt % tns->cols ? 1 : 0;
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->loadnext = tns->cnt;
tns->loadnext = *tns->cnt;
tns->end = tns->first + cnt;
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)
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;
thumb_t *t = &tns->thumbs[n];
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)
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;
thumb_t *t = &tns->thumbs[n];
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);
break;
case DIR_DOWN:
max = tns->cols * ((tns->cnt - 1) / tns->cols) +
MIN((tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
max = tns->cols * ((*tns->cnt - 1) / tns->cols) +
MIN((*tns->cnt - 1) % tns->cols, *tns->sel % tns->cols);
*tns->sel = MIN(*tns->sel + cnt * tns->cols, max);
break;
case DIR_LEFT:
*tns->sel = MAX(*tns->sel - cnt, 0);
break;
case DIR_RIGHT:
*tns->sel = MIN(*tns->sel + cnt, tns->cnt - 1);
*tns->sel = MIN(*tns->sel + cnt, *tns->cnt - 1);
break;
}
@ -551,9 +552,9 @@ bool tns_scroll(tns_t *tns, direction_t dir, bool screen)
d = tns->cols * (screen ? tns->rows : 1);
if (dir == DIR_DOWN) {
max = tns->cnt - tns->cols * tns->rows;
if (tns->cnt % tns->cols != 0)
max += tns->cols - tns->cnt % tns->cols;
max = *tns->cnt - tns->cols * tns->rows;
if (*tns->cnt % tns->cols != 0)
max += tns->cols - *tns->cnt % tns->cols;
tns->first = MIN(tns->first + d, max);
} else if (dir == DIR_UP) {
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;
if (tns->zl != oldzl) {
for (i = 0; i < tns->cnt; i++)
for (i = 0; i < *tns->cnt; i++)
tns_unload(tns, i);
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 +
(x - tns->x) / tns->dim;
if (n >= tns->cnt)
if (n >= *tns->cnt)
n = -1;
return n;

View File

@ -36,11 +36,11 @@ typedef struct {
typedef struct {
const fileinfo_t *files;
thumb_t *thumbs;
int cnt;
const int *cnt;
int *sel;
int loadnext;
int first, end;
int r_first, r_end;
int *sel;
win_t *win;
int x;
@ -56,7 +56,7 @@ typedef struct {
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*);
bool tns_load(tns_t*, int, bool);