Krolyxon
Commit bcc70bd

Branches

Refs →

nsxivbcc70bd

Who needs threads to load thumbnails?

Author
Bert
Date
2011-02-17 11:04 +0100
Parent
e0d0892
Commit
bcc70bd7cbcdf56e84984929d9f8c6f1776f2204

+52 −82

4 files changed

  1. M Makefile +2 −2
  2. M main.c +12 −8
  3. M thumbs.c +35 −69
  4. M thumbs.h +3 −3
M Makefile View
@@ -1,12 +1,12 @@
11 all: sxiv
22
3−VERSION=git-20110216
3+VERSION=git-20110217
44
55 CC?=gcc
66 PREFIX?=/usr/local
77 CFLAGS+= -Wall -pedantic -DVERSION=\"$(VERSION)\"
88 LDFLAGS+=
9−LIBS+= -lX11 -lImlib2 -lpthread
9+LIBS+= -lX11 -lImlib2
1010
1111 SRCFILES=$(wildcard *.c)
1212 OBJFILES=$(SRCFILES:.c=.o)
M main.c View
@@ -54,6 +54,8 @@ const char **filenames;
5454 int filecnt, fileidx;
5555 size_t filesize;
5656
57+int tns_loaded;
58+
5759 #define TITLE_LEN 256
5860 char win_title[TITLE_LEN];
5961
@@ -128,13 +130,13 @@ int main(int argc, char **argv) {
128130 win_open(&win);
129131 img_init(&img, &win);
130132
131− if (options->thumbnails)
132− tns_load(&tns, &win, filenames, filecnt);
133+ if (options->thumbnails) {
134+ tns_loaded = 0;
135+ tns_init(&tns, filecnt);
136+ }
133137
134138 if (options->thumbnails == 2) {
135139 mode = MODE_THUMBS;
136− tns.first = tns.sel = 0;
137− tns_render(&tns, &win);
138140 } else {
139141 mode = MODE_NORMAL;
140142 load_image();
@@ -473,7 +475,12 @@ void run() {
473475 timeout = 0;
474476
475477 while (1) {
476− if (timeout || (mode == MODE_THUMBS && !tns.loaded)) {
478+ if (mode == MODE_THUMBS && tns_loaded < filecnt) {
479+ tns_load(&tns, &win, filenames[tns_loaded++]);
480+ tns_render(&tns, &win);
481+ if (!XPending(win.env.dpy))
482+ continue;
483+ } else if (timeout) {
477484 t.tv_sec = 0;
478485 t.tv_usec = 250;
479486 xfd = ConnectionNumber(win.env.dpy);
@@ -486,9 +493,6 @@ void run() {
486493 img_render(&img, &win);
487494 else
488495 tns_render(&tns, &win);
489−
490− if (mode == MODE_THUMBS && !tns.loaded && !XPending(win.env.dpy))
491− continue;
492496 }
493497 }
494498
M thumbs.c View
@@ -18,7 +18,6 @@
1818
1919 #include <stdlib.h>
2020 #include <string.h>
21−#include <pthread.h>
2221
2322 #include <Imlib2.h>
2423
@@ -26,75 +25,14 @@
2625 #include "thumbs.h"
2726 #include "util.h"
2827
29−typedef struct tload_s {
30− const char **filenames;
31− tns_t *tns;
32− win_t *win;
33−} tload_t;
34−
3528 const int thumb_dim = THUMB_SIZE + 10;
3629
37−pthread_t loader;
38−tload_t tinfo;
39−
40−void* thread_load(void *arg) {
41− int i, w, h;
42− float z, zw, zh;
43− tload_t *tl;
44− thumb_t *t;
45− Imlib_Image *im;
46−
47− tl = (tload_t*) arg;
48−
49− for (i = 0; i < tl->tns->cnt; ++i) {
50− if (!(im = imlib_load_image(tl->filenames[i])))
51− continue;
52−
53− imlib_context_set_image(im);
54−
55− w = imlib_image_get_width();
56− h = imlib_image_get_height();
57− zw = (float) THUMB_SIZE / (float) w;
58− zh = (float) THUMB_SIZE / (float) h;
59− z = MIN(zw, zh);
60−
61− t = &tl->tns->thumbs[i];
62− t->w = z * w;
63− t->h = z * h;
64−
65− t->pm = win_create_pixmap(tl->win, t->w, t->h);
66− imlib_context_set_drawable(t->pm);
67− imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
68− 0, 0, t->w, t->h);
69− t->loaded = 1;
70− imlib_free_image();
71− }
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)
30+void tns_init(tns_t *tns, int cnt) {
31+ if (!tns)
8232 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);
9533
96− if (pthread_create(&loader, &tattr, thread_load, (void*) &tinfo))
97− die("could not create thread");
34+ tns->cnt = tns->first = tns->sel = 0;
35+ tns->thumbs = (thumb_t*) s_malloc(cnt * sizeof(thumb_t));
9836 }
9937
10038 void tns_free(tns_t *tns, win_t *win) {
@@ -110,6 +48,37 @@ void tns_free(tns_t *tns, win_t *win) {
11048 tns->thumbs = NULL;
11149 }
11250
51+void tns_load(tns_t *tns, win_t *win, const char *filename) {
52+ int w, h;
53+ float z, zw, zh;
54+ thumb_t *t;
55+ Imlib_Image *im;
56+
57+ if (!tns || !win || !filename)
58+ return;
59+
60+ if (!(im = imlib_load_image(filename)))
61+ return;
62+
63+ imlib_context_set_image(im);
64+
65+ w = imlib_image_get_width();
66+ h = imlib_image_get_height();
67+ zw = (float) THUMB_SIZE / (float) w;
68+ zh = (float) THUMB_SIZE / (float) h;
69+ z = MIN(zw, zh);
70+
71+ t = &tns->thumbs[tns->cnt++];
72+ t->w = z * w;
73+ t->h = z * h;
74+
75+ t->pm = win_create_pixmap(win, t->w, t->h);
76+ imlib_context_set_drawable(t->pm);
77+ imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
78+ 0, 0, t->w, t->h);
79+ imlib_free_image();
80+}
81+
11382 void tns_render(tns_t *tns, win_t *win) {
11483 int i, cnt, x, y;
11584
@@ -130,9 +99,6 @@ void tns_render(tns_t *tns, win_t *win) {
13099 i = tns->first;
131100
132101 while (i < cnt) {
133− if (!tns->thumbs[i].loaded)
134− continue;
135−
136102 tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
137103 tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
138104 win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
M thumbs.h View
@@ -27,12 +27,10 @@ typedef struct thumb_s {
2727 int w;
2828 int h;
2929 Pixmap pm;
30− unsigned char loaded;
3130 } thumb_t;
3231
3332 typedef struct tns_s {
3433 thumb_t *thumbs;
35− unsigned char loaded;
3634 int cnt;
3735 int cols;
3836 int rows;
@@ -42,9 +40,11 @@ typedef struct tns_s {
4240
4341 extern const int thumb_dim;
4442
45−void tns_load(tns_t*, win_t*, const char**, int);
43+void tns_init(tns_t*, int);
4644 void tns_free(tns_t*, win_t*);
4745
46+void tns_load(tns_t*, win_t*, const char*);
47+
4848 void tns_render(tns_t*, win_t*);
4949
5050 #endif /* THUMBS_H */