From 0f714bccda28cd2faa5095fa1ace5d8a113beaec Mon Sep 17 00:00:00 2001 From: Bert Date: Sat, 22 Jan 2011 23:27:29 +0100 Subject: Handle window resize events properly --- image.c | 42 +++++++++++++++++++++++------------------- image.h | 3 ++- main.c | 33 +++++++++++++++++++++++++++++---- window.c | 17 +++++++++-------- 4 files changed, 63 insertions(+), 32 deletions(-) diff --git a/image.c b/image.c index 6c51761..5187d51 100644 --- a/image.c +++ b/image.c @@ -65,6 +65,9 @@ int img_load(img_t *img, const char *filename) { imlib_context_set_image(im); img->re = 0; + img->checkpan = 0; + img->zoomed = 0; + img->w = imlib_image_get_width(); img->h = imlib_image_get_height(); @@ -101,32 +104,32 @@ void img_render(img_t *img, win_t *win) { if (!img || !win || !imlib_context_get_image()) return; - if (!img->re) { - /* rendered for the first time */ - img->re = 1; - + if ((!img->re || !img->zoomed) && SCALE_MODE != SCALE_ZOOM) { /* set zoom level to fit image into window */ - if (SCALE_MODE != SCALE_ZOOM) { - zw = (float) win->w / (float) img->w; - zh = (float) win->h / (float) img->h; - img->zoom = MIN(zw, zh); + zw = (float) win->w / (float) img->w; + zh = (float) win->h / (float) img->h; + img->zoom = MIN(zw, zh); - if (img->zoom < zoom_min) - img->zoom = zoom_min; - else if (img->zoom > zoom_max) - img->zoom = zoom_max; + if (img->zoom < zoom_min) + img->zoom = zoom_min; + else if (img->zoom > zoom_max) + img->zoom = zoom_max; - if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0) - img->zoom = 1.0; - } + if (SCALE_MODE == SCALE_DOWN && img->zoom > 1.0) + img->zoom = 1.0; + } + if (!img->re) { + /* rendered for the first time */ + img->re = 1; /* center image in window */ img->x = (win->w - img->w * img->zoom) / 2; img->y = (win->h - img->h * img->zoom) / 2; - } else if (img->cp) { - /* only useful after zooming */ + } + + if (img->checkpan) { img_check_pan(img, win); - img->cp = 0; + img->checkpan = 0; } /* calculate source and destination offsets */ @@ -174,7 +177,8 @@ int img_zoom(img_t *img, float z) { img->x -= (img->w * z - img->w * img->zoom) / 2; img->y -= (img->h * z - img->h * img->zoom) / 2; img->zoom = z; - img->cp = 1; + img->checkpan = 1; + img->zoomed = 1; return 1; } else { return 0; diff --git a/image.h b/image.h index 645b706..4e4d816 100644 --- a/image.h +++ b/image.h @@ -37,7 +37,8 @@ typedef enum pandir_e { typedef struct img_s { float zoom; unsigned char re; - unsigned char cp; + unsigned char checkpan; + unsigned char zoomed; int x; int y; int w; diff --git a/main.c b/main.c index 497cc38..5c29e8e 100644 --- a/main.c +++ b/main.c @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -45,14 +46,34 @@ const char **filenames; unsigned int filecnt; unsigned int fileidx; +unsigned char timeout; + #define TITLE_LEN 256 char win_title[TITLE_LEN]; void run() { + int xfd; + fd_set fds; + struct timeval t; XEvent ev; - while (!XNextEvent(win.env.dpy, &ev)) { - if (handler[ev.type]) + timeout = 0; + + while (1) { + if (timeout) { + 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)) { + img_render(&img, &win); + timeout = 0; + } + } + + if (!XNextEvent(win.env.dpy, &ev) && handler[ev.type]) handler[ev.type](&ev); } } @@ -175,14 +196,18 @@ void on_keypress(XEvent *ev) { if (changed) { img_render(&img, &win); update_title(); + timeout = 0; } } void on_configurenotify(XEvent *ev) { if (!ev) return; - - win_configure(&win, &ev->xconfigure); + + if (win_configure(&win, &ev->xconfigure)) { + img.checkpan = 1; + timeout = 1; + } } void update_title() { diff --git a/window.c b/window.c index fc6a264..752f045 100644 --- a/window.c +++ b/window.c @@ -102,19 +102,20 @@ void win_set_title(win_t *win, const char *title) { XSetIconName(win->env.dpy, win->xwin, title); } -int win_configure(win_t *win, XConfigureEvent *cev) { +int win_configure(win_t *win, XConfigureEvent *c) { int changed; if (!win) return 0; - changed = win->x != cev->x || win->y != cev->y || - win->w != cev->width || win->h != cev->height; - win->x = cev->x; - win->y = cev->y; - win->w = cev->width; - win->h = cev->height; - win->bw = cev->border_width; + changed = win->w != c->width || win->h != c->height; + + win->x = c->x; + win->y = c->y; + win->w = c->width; + win->h = c->height; + win->bw = c->border_width; + return changed; } -- cgit v1.2.3