Krolyxon
Commit e0d0892

Branches

Refs →

nsxive0d0892

Use pthread to load thumbnails

Author
Bert
Date
2011-02-16 23:03 +0100
Parent
26c2179
Commit
e0d08920657e8e5132a732c2f429243efa899ac1

+55 −14

4 files changed

  1. M Makefile +1 −1
  2. M main.c +5 −2
  3. M thumbs.c +48 −11
  4. M thumbs.h +1 −0
M Makefile View
@@ -6,7 +6,7 @@ CC?=gcc
66 PREFIX?=/usr/local
77 CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\"
88 LDFLAGS+=
9−LIBS+= -lX11 -lImlib2
9+LIBS+= -lX11 -lImlib2 -lpthread
1010
1111 SRCFILES=$(wildcard *.c)
1212 OBJFILES=$(SRCFILES:.c=.o)
M main.c View
@@ -473,19 +473,22 @@ void run() {
473473 timeout = 0;
474474
475475 while (1) {
476− if (timeout) {
476+ if (timeout || (mode == MODE_THUMBS && !tns.loaded)) {
477477 t.tv_sec = 0;
478478 t.tv_usec = 250;
479479 xfd = ConnectionNumber(win.env.dpy);
480480 FD_ZERO(&fds);
481481 FD_SET(xfd, &fds);
482−
482+
483483 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
484484 timeout = 0;
485485 if (mode == MODE_NORMAL)
486486 img_render(&img, &win);
487487 else
488488 tns_render(&tns, &win);
489+
490+ if (mode == MODE_THUMBS && !tns.loaded && !XPending(win.env.dpy))
491+ continue;
489492 }
490493 }
491494
M thumbs.c View
@@ -17,6 +17,8 @@
1717 */
1818
1919 #include <stdlib.h>
20+#include <string.h>
21+#include <pthread.h>
2022
2123 #include <Imlib2.h>
2224
@@ -24,23 +26,28 @@
2426 #include "thumbs.h"
2527 #include "util.h"
2628
29+typedef struct tload_s {
30+ const char **filenames;
31+ tns_t *tns;
32+ win_t *win;
33+} tload_t;
34+
2735 const int thumb_dim = THUMB_SIZE + 10;
2836
29−void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
37+pthread_t loader;
38+tload_t tinfo;
39+
40+void* thread_load(void *arg) {
3041 int i, w, h;
3142 float z, zw, zh;
43+ tload_t *tl;
3244 thumb_t *t;
3345 Imlib_Image *im;
3446
35− if (!tns || !win || !fnames || !fcnt)
36− return;
37−
38− tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
39− tns->cnt = fcnt;
40− tns->loaded = 0;
47+ tl = (tload_t*) arg;
4148
42− for (i = 0; i < fcnt; ++i) {
43− if (!(im = imlib_load_image(fnames[i])))
49+ for (i = 0; i < tl->tns->cnt; ++i) {
50+ if (!(im = imlib_load_image(tl->filenames[i])))
4451 continue;
4552
4653 imlib_context_set_image(im);
@@ -51,16 +58,43 @@ void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
5158 zh = (float) THUMB_SIZE / (float) h;
5259 z = MIN(zw, zh);
5360
54− t = &tns->thumbs[i];
61+ t = &tl->tns->thumbs[i];
5562 t->w = z * w;
5663 t->h = z * h;
5764
58− t->pm = win_create_pixmap(win, t->w, t->h);
65+ t->pm = win_create_pixmap(tl->win, t->w, t->h);
5966 imlib_context_set_drawable(t->pm);
6067 imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
6168 0, 0, t->w, t->h);
69+ t->loaded = 1;
6270 imlib_free_image();
6371 }
72+
73+ tl->tns->loaded = 1;
74+
75+ return 0;
76+}
77+
78+void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
79+ pthread_attr_t tattr;
80+
81+ if (!tns || !win || !fnames || !fcnt)
82+ return;
83+
84+ tns->thumbs = (thumb_t*) s_malloc(fcnt * sizeof(thumb_t));
85+ memset(tns->thumbs, 0, fcnt * sizeof(thumb_t));
86+ tns->cnt = fcnt;
87+ tns->loaded = 0;
88+
89+ tinfo.filenames = fnames;
90+ tinfo.tns = tns;
91+ tinfo.win = win;
92+
93+ pthread_attr_init(&tattr);
94+ pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
95+
96+ if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo))
97+ die("could not create thread");
6498 }
6599
66100 void tns_free(tns_t *tns, win_t *win) {
@@ -96,6 +130,9 @@ void tns_render(tns_t *tns, win_t *win) {
96130 i = tns->first;
97131
98132 while (i < cnt) {
133+ if (!tns->thumbs[i].loaded)
134+ continue;
135+
99136 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
100137 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
101138 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
M thumbs.h View
@@ -27,6 +27,7 @@ typedef struct thumb_s {
2727 int w;
2828 int h;
2929 Pixmap pm;
30+ unsigned char loaded;
3031 } thumb_t;
3132
3233 typedef struct tns_s {