Krolyxon
Commit 26c2179

Branches

Refs →

nsxiv26c2179

Refactored thumbs, new files thumbs.[ch]

Author
Bert
Date
2011-02-16 21:40 +0100
Parent
8919204
Commit
26c2179be7676df3049035e5e65039c7fc232cb7

+191 −89

8 files changed

  1. M Makefile +1 −1
  2. M image.c +0 −26
  3. M image.h +0 −9
  4. M main.c +17 −45
  5. A thumbs.c +113 −0
  6. A thumbs.h +49 −0
  7. M window.c +9 −7
  8. M window.h +2 −1
M Makefile View
@@ -1,6 +1,6 @@
11 all: sxiv
22
3−VERSION=git-20110215
3+VERSION=git-20110216
44
55 CC?=gcc
66 PREFIX?=/usr/local
M image.c View
@@ -110,32 +110,6 @@ int img_load(img_t *img, const char *filename) {
110110 return 1;
111111 }
112112
113−int img_load_thumb(thumb_t *tn, const char *filename) {
114− int w, h;
115− float z, zw, zh;
116−
117− if (!tn)
118− return 0;
119−
120− if (!_imlib_load_image(filename))
121− return 0;
122−
123− w = imlib_image_get_width();
124− h = imlib_image_get_height();
125− zw = (float) THUMB_SIZE / (float) w;
126− zh = (float) THUMB_SIZE / (float) h;
127− z = MIN(zw, zh);
128− tn->w = z * w;
129− tn->h = z * h;
130−
131− imlib_context_set_drawable(tn->pm);
132− imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
133− 0, 0, tn->w, tn->h);
134− imlib_free_image();
135−
136− return 1;
137−}
138−
139113 void img_check_pan(img_t *img, win_t *win) {
140114 if (!img || !win)
141115 return;
M image.h View
@@ -49,20 +49,11 @@ typedef struct img_s {
4949 int h;
5050 } img_t;
5151
52−typedef struct thumb_s {
53− int x;
54− int y;
55− int w;
56− int h;
57− Pixmap pm;
58−} thumb_t;
59−
6052 void img_init(img_t*, win_t*);
6153 void img_free(img_t*);
6254
6355 int img_check(const char*);
6456 int img_load(img_t*, const char*);
65−int img_load_thumb(thumb_t*, const char*);
6657
6758 void img_render(img_t*, win_t*);
6859
M main.c View
@@ -27,9 +27,9 @@
2727 #include <X11/Xutil.h>
2828 #include <X11/keysym.h>
2929
30−#include "config.h"
3130 #include "image.h"
3231 #include "options.h"
32+#include "thumbs.h"
3333 #include "util.h"
3434 #include "window.h"
3535
@@ -39,13 +39,13 @@ typedef enum appmode_e {
3939 } appmode_t;
4040
4141 void update_title();
42−void render_thumbs();
4342 int check_append(const char*);
4443 void read_dir_rec(const char*);
4544 void run();
4645
4746 appmode_t mode;
4847 img_t img;
48+tns_t tns;
4949 win_t win;
5050
5151 #define DNAME_CNT 512
@@ -54,9 +54,6 @@ const char **filenames;
5454 int filecnt, fileidx;
5555 size_t filesize;
5656
57−thumb_t *thumbs;
58−int tvis, tcols, trows;
59−
6057 #define TITLE_LEN 256
6158 char win_title[TITLE_LEN];
6259
@@ -65,6 +62,7 @@ void cleanup() {
6562
6663 if (!in++) {
6764 img_free(&img);
65+ tns_free(&tns, &win);
6866 win_close(&win);
6967 }
7068 }
@@ -130,17 +128,13 @@ int main(int argc, char **argv) {
130128 win_open(&win);
131129 img_init(&img, &win);
132130
133− if (options->thumbnails) {
134− thumbs = (thumb_t*) s_malloc(filecnt * sizeof(thumb_t));
135− for (i = 0; i < filecnt; ++i) {
136− thumbs[i].pm = win_create_pixmap(&win);
137− img_load_thumb(&thumbs[i], filenames[i]);
138− }
139− }
131+ if (options->thumbnails)
132+ tns_load(&tns, &win, filenames, filecnt);
140133
141134 if (options->thumbnails == 2) {
142135 mode = MODE_THUMBS;
143− render_thumbs();
136+ tns.first = tns.sel = 0;
137+ tns_render(&tns, &win);
144138 } else {
145139 mode = MODE_NORMAL;
146140 load_image();
@@ -179,35 +173,6 @@ void update_title() {
179173 win_set_title(&win, win_title);
180174 }
181175
182−void render_thumbs() {
183− int i, cnt, x, y;
184−
185− tcols = win.w / (THUMB_SIZE + 10);
186− trows = win.h / (THUMB_SIZE + 10);
187−
188− x = win.w - tcols * (THUMB_SIZE + 10) + 5;
189− y = win.h - trows * (THUMB_SIZE + 10) + 5;
190− cnt = MIN(tcols * trows, filecnt);
191−
192− win_clear(&win);
193−
194− i = 0;
195− while (i < cnt) {
196− thumbs[i].x = x + (THUMB_SIZE - thumbs[i].w) / 2;
197− thumbs[i].y = y + (THUMB_SIZE - thumbs[i].h) / 2;
198− win_draw_pixmap(&win, thumbs[i].pm, thumbs[i].x, thumbs[i].y, thumbs[i].w,
199− thumbs[i].h);
200− if (++i % tcols == 0) {
201− x = win.w - tcols * (THUMB_SIZE + 10) + 5;
202− y += THUMB_SIZE + 10;
203− } else {
204− x += THUMB_SIZE + 10;
205− }
206− }
207−
208− win_draw(&win);
209−}
210−
211176 int check_append(const char *filename) {
212177 if (!filename)
213178 return 0;
@@ -287,7 +252,10 @@ unsigned char timeout;
287252 int mox, moy;
288253
289254 void redraw() {
290− img_render(&img, &win);
255+ if (mode == MODE_NORMAL)
256+ img_render(&img, &win);
257+ else
258+ tns_render(&tns, &win);
291259 update_title();
292260 timeout = 0;
293261 }
@@ -513,8 +481,11 @@ void run() {
513481 FD_SET(xfd, &fds);
514482
515483 if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
516− img_render(&img, &win);
517484 timeout = 0;
485+ if (mode == MODE_NORMAL)
486+ img_render(&img, &win);
487+ else
488+ tns_render(&tns, &win);
518489 }
519490 }
520491
@@ -535,8 +506,9 @@ void run() {
535506 break;
536507 case ConfigureNotify:
537508 if (win_configure(&win, &ev.xconfigure)) {
538− img.checkpan = 1;
539509 timeout = 1;
510+ if (mode == MODE_NORMAL)
511+ img.checkpan = 1;
540512 }
541513 break;
542514 case ClientMessage:
A thumbs.c View
@@ -0,0 +1,113 @@
1+/* sxiv: thumbs.c
2+ * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, write to the Free Software
16+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+ */
18+
19+#include <stdlib.h>
20+
21+#include <Imlib2.h>
22+
23+#include "config.h"
24+#include "thumbs.h"
25+#include "util.h"
26+
27+const int thumb_dim = THUMB_SIZE + 10;
28+
29+void tns_load(tns_t *tns, win_t *win, const char **fnames, int fcnt) {
30+ int i, w, h;
31+ float z, zw, zh;
32+ thumb_t *t;
33+ Imlib_Image *im;
34+
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;
41+
42+ for (i = 0; i < fcnt; ++i) {
43+ if (!(im = imlib_load_image(fnames[i])))
44+ continue;
45+
46+ imlib_context_set_image(im);
47+
48+ w = imlib_image_get_width();
49+ h = imlib_image_get_height();
50+ zw = (float) THUMB_SIZE / (float) w;
51+ zh = (float) THUMB_SIZE / (float) h;
52+ z = MIN(zw, zh);
53+
54+ t = &tns->thumbs[i];
55+ t->w = z * w;
56+ t->h = z * h;
57+
58+ t->pm = win_create_pixmap(win, t->w, t->h);
59+ imlib_context_set_drawable(t->pm);
60+ imlib_render_image_part_on_drawable_at_size(0, 0, w, h,
61+ 0, 0, t->w, t->h);
62+ imlib_free_image();
63+ }
64+}
65+
66+void tns_free(tns_t *tns, win_t *win) {
67+ int i;
68+
69+ if (!tns)
70+ return;
71+
72+ for (i = 0; i < tns->cnt; ++i)
73+ win_free_pixmap(win, tns->thumbs[i].pm);
74+
75+ free(tns->thumbs);
76+ tns->thumbs = NULL;
77+}
78+
79+void tns_render(tns_t *tns, win_t *win) {
80+ int i, cnt, x, y;
81+
82+ if (!tns || !win)
83+ return;
84+
85+ tns->cols = win->w / thumb_dim;
86+ tns->rows = win->h / thumb_dim;
87+
88+ cnt = tns->cols * tns->rows;
89+ if (tns->first && tns->first + cnt > tns->cnt)
90+ tns->first = MAX(0, tns->cnt - cnt);
91+ cnt = MIN(tns->first + cnt, tns->cnt);
92+
93+ win_clear(win);
94+
95+ x = y = 5;
96+ i = tns->first;
97+
98+ while (i < cnt) {
99+ tns->thumbs[i].x = x + (THUMB_SIZE - tns->thumbs[i].w) / 2;
100+ tns->thumbs[i].y = y + (THUMB_SIZE - tns->thumbs[i].h) / 2;
101+ win_draw_pixmap(win, tns->thumbs[i].pm, tns->thumbs[i].x,
102+ tns->thumbs[i].y, tns->thumbs[i].w, tns->thumbs[i].h);
103+ if (++i % tns->cols == 0) {
104+ x = 5;
105+ y += thumb_dim;
106+ } else {
107+ x += thumb_dim;
108+ }
109+ }
110+
111+ win_draw(win);
112+}
113+
A thumbs.h View
@@ -0,0 +1,49 @@
1+/* sxiv: thumbs.h
2+ * Copyright (c) 2011 Bert Muennich <muennich at informatik.hu-berlin.de>
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; either version 2 of the License, or
7+ * (at your option) any later version.
8+ *
9+ * This program is distributed in the hope that it will be useful,
10+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ * GNU General Public License for more details.
13+ *
14+ * You should have received a copy of the GNU General Public License
15+ * along with this program; if not, write to the Free Software
16+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17+ */
18+
19+#ifndef THUMBS_H
20+#define THUMBS_H
21+
22+#include "window.h"
23+
24+typedef struct thumb_s {
25+ int x;
26+ int y;
27+ int w;
28+ int h;
29+ Pixmap pm;
30+} thumb_t;
31+
32+typedef struct tns_s {
33+ thumb_t *thumbs;
34+ unsigned char loaded;
35+ int cnt;
36+ int cols;
37+ int rows;
38+ int first;
39+ int sel;
40+} tns_t;
41+
42+extern const int thumb_dim;
43+
44+void tns_load(tns_t*, win_t*, const char**, int);
45+void tns_free(tns_t*, win_t*);
46+
47+void tns_render(tns_t*, win_t*);
48+
49+#endif /* THUMBS_H */
M window.c View
@@ -211,19 +211,21 @@ void win_toggle_fullscreen(win_t *win) {
211211 SubstructureNotifyMask, &ev);
212212 }
213213
214−Pixmap win_create_pixmap(win_t *win) {
214+Pixmap win_create_pixmap(win_t *win, int w, int h) {
215215 if (!win)
216216 return 0;
217217
218− return XCreatePixmap(win->env.dpy, win->xwin, THUMB_SIZE, THUMB_SIZE,
219− win->env.depth);
218+ return XCreatePixmap(win->env.dpy, win->xwin, w, h, win->env.depth);
220219 }
221220
222−void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) {
223− if (!win)
224− return;
221+void win_free_pixmap(win_t *win, Pixmap pm) {
222+ if (win && pm)
223+ XFreePixmap(win->env.dpy, pm);
224+}
225225
226− XCopyArea(win->env.dpy, pm, win->pm, bgc, 0, 0, w, h, x, y);
226+void win_draw_pixmap(win_t *win, Pixmap pm, int x, int y, int w, int h) {
227+ if (win)
228+ XCopyArea(win->env.dpy, pm, win->pm, bgc, 0, 0, w, h, x, y);
227229 }
228230
229231 void win_clear(win_t *win) {
M window.h View
@@ -63,7 +63,8 @@ int win_moveresize(win_t*, int, int, unsigned int, unsigned int);
6363
6464 void win_toggle_fullscreen(win_t*);
6565
66−Pixmap win_create_pixmap(win_t*);
66+Pixmap win_create_pixmap(win_t*, int, int);
67+void win_free_pixmap(win_t*, Pixmap);
6768 void win_draw_pixmap(win_t*, Pixmap, int, int, int, int);
6869
6970 void win_clear(win_t*);