From e0d08920657e8e5132a732c2f429243efa899ac1 Mon Sep 17 00:00:00 2001 From: Bert Date: Wed, 16 Feb 2011 23:03:42 +0100 Subject: [PATCH] Use pthread to load thumbnails --- Makefile | 2 +- main.c | 7 +++++-- thumbs.c | 59 +++++++++++++++++++++++++++++++++++++++++++++----------- thumbs.h | 1 + 4 files changed, 55 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index de784ab..dd4427f 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ CC?=gcc PREFIX?=/usr/local CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\" LDFLAGS+= -LIBS+= -lX11 -lImlib2 +LIBS+= -lX11 -lImlib2 -lpthread SRCFILES=$(wildcard *.c) OBJFILES=$(SRCFILES:.c=.o) diff --git a/main.c b/main.c index 4d66595..2fccf7a 100644 --- a/main.c +++ b/main.c @@ -473,19 +473,22 @@ void run() { timeout = 0; while (1) { - if (timeout) { + if (timeout || (mode == MODE_THUMBS && !tns.loaded)) { t.tv_sec = 0; t.tv_usec = 250; xfd = ConnectionNumber(win.env.dpy); FD_ZERO(&fds); FD_SET(xfd, &fds); - + if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) { timeout = 0; if (mode == MODE_NORMAL) img_render(&img, &win); else tns_render(&tns, &win); + + if (mode == MODE_THUMBS && !tns.loaded && !XPending(win.env.dpy)) + continue; } } diff --git a/thumbs.c b/thumbs.c index a1e0fdf..d8de3e8 100644 --- a/thumbs.c +++ b/thumbs.c @@ -17,6 +17,8 @@ */ #include +#include +#include #include @@ -24,23 +26,28 @@ #include "thumbs.h" #include "util.h" +typedef struct tload_s { + const char **filenames; + tns_t *tns; + win_t *win; +} tload_t; + const int thumb_dim = THUMB_SIZE + 10; -void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { +pthread_t loader; +tload_t tinfo; + +void* thread_load(void *arg) { int i, w, h; float z, zw, zh; + tload_t *tl; thumb_t *t; Imlib_Image *im; - if (!tns || !win || !fnames || !fcnt) - return; - - tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t)); - tns->cnt = fcnt; - tns->loaded = 0; + tl = (tload_t*) arg; - for (i = 0; i < fcnt; ++i) { - if (!(im = imlib_load_image(fnames[i]))) + for (i = 0; i < tl->tns->cnt; ++i) { + if (!(im = imlib_load_image(tl->filenames[i]))) continue; imlib_context_set_image(im); @@ -51,16 +58,43 @@ void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { zh = (float) THUMB_SIZE / (float) h; z = MIN(zw, zh); - t = &tns->thumbs[i]; + t = &tl->tns->thumbs[i]; t->w = z * w; t->h = z * h; - t->pm = win_create_pixmap(win, t->w, t->h); + t->pm = win_create_pixmap(tl->win, t->w, t->h); imlib_context_set_drawable(t->pm); imlib_render_image_part_on_drawable_at_size(0, 0, w, h, 0, 0, t->w, t->h); + t->loaded = 1; imlib_free_image(); } + + tl->tns->loaded = 1; + + return 0; +} + +void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { + pthread_attr_t tattr; + + if (!tns || !win || !fnames || !fcnt) + return; + + tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t)); + memset(tns->thumbs, 0, fcnt * sizeof(thumb_t)); + tns->cnt = fcnt; + tns->loaded = 0; + + tinfo.filenames = fnames; + tinfo.tns = tns; + tinfo.win = win; + + pthread_attr_init(&tattr); + pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE); + + if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo)) + die("could not create thread"); } void tns_free(tns_t *tns, win_t *win) { @@ -96,6 +130,9 @@ void tns_render(tns_t *tns, win_t *win) { i = tns->first; while (i < cnt) { + if (!tns->thumbs[i].loaded) + continue; + tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2; tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2; win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x, diff --git a/thumbs.h b/thumbs.h index ae6faa7..bfc6573 100644 --- a/thumbs.h +++ b/thumbs.h @@ -27,6 +27,7 @@ typedef struct thumb_s { int w; int h; Pixmap pm; + unsigned char loaded; } thumb_t; typedef struct tns_s {