| @@ -17,6 +17,8 @@ |
| 17 | 17 | | */ |
| 18 | 18 | | |
| 19 | 19 | | #include <stdlib.h> |
| 20 | + | #include <string.h> |
| 21 | + | #include <pthread.h> |
| 20 | 22 | | |
| 21 | 23 | | #include <Imlib2.h> |
| 22 | 24 | | |
| @@ -24,23 +26,28 @@ |
| 24 | 26 | | #include "thumbs.h" |
| 25 | 27 | | #include "util.h" |
| 26 | 28 | | |
| 29 | + | typedef struct tload_s { |
| 30 | + | const char **filenames; |
| 31 | + | tns_t *tns; |
| 32 | + | win_t *win; |
| 33 | + | } tload_t; |
| 34 | + | |
| 27 | 35 | | const int thumb_dim = THUMB_SIZE + 10; |
| 28 | 36 | | |
| 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) { |
| 30 | 41 | | int i, w, h; |
| 31 | 42 | | float z, zw, zh; |
| 43 | + | tload_t *tl; |
| 32 | 44 | | thumb_t *t; |
| 33 | 45 | | Imlib_Image *im; |
| 34 | 46 | | |
| 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; |
| 41 | 48 | | |
| 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]))) |
| 44 | 51 | | continue; |
| 45 | 52 | | |
| 46 | 53 | | imlib_context_set_image(im); |
| @@ -51,16 +58,43 @@ void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) { |
| 51 | 58 | | zh = (float) THUMB_SIZE / (float) h; |
| 52 | 59 | | z = MIN(zw, zh); |
| 53 | 60 | | |
| 54 | | − | t = &tns->thumbs[i]; |
| 61 | + | t = &tl->tns->thumbs[i]; |
| 55 | 62 | | t->w = z * w; |
| 56 | 63 | | t->h = z * h; |
| 57 | 64 | | |
| 58 | | − | t->pm = win_create_pixmap(win, t->w, t->h); |
| 65 | + | t->pm = win_create_pixmap(tl->win, t->w, t->h); |
| 59 | 66 | | imlib_context_set_drawable(t->pm); |
| 60 | 67 | | imlib_render_image_part_on_drawable_at_size(0, 0, w, h, |
| 61 | 68 | | 0, 0, t->w, t->h); |
| 69 | + | t->loaded = 1; |
| 62 | 70 | | imlib_free_image(); |
| 63 | 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) |
| 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"); |
| 64 | 98 | | } |
| 65 | 99 | | |
| 66 | 100 | | void tns_free(tns_t *tns, win_t *win) { |
| @@ -96,6 +130,9 @@ void tns_render(tns_t *tns, win_t *win) { |
| 96 | 130 | | i = tns->first; |
| 97 | 131 | | |
| 98 | 132 | | while (i < cnt) { |
| 133 | + | if (!tns->thumbs[i].loaded) |
| 134 | + | continue; |
| 135 | + | |
| 99 | 136 | | tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2; |
| 100 | 137 | | tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2; |
| 101 | 138 | | win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x, |