Much better drag()

No more need to pass XEvent* to handler functions
This commit is contained in:
Bert 2011-08-12 12:27:07 +02:00
parent 2b7192b1ac
commit 75e36939a1
3 changed files with 106 additions and 101 deletions

View file

@ -1,6 +1,6 @@
all: sxiv all: sxiv
VERSION = git-20110805 VERSION = git-20110812
CC = gcc CC = gcc
DESTDIR = DESTDIR =

151
events.c
View file

@ -38,7 +38,6 @@
/* timeouts in milliseconds: */ /* timeouts in milliseconds: */
enum { enum {
TO_WIN_RESIZE = 75, TO_WIN_RESIZE = 75,
TO_IMAGE_DRAG = 1,
TO_CURSOR_HIDE = 1500, TO_CURSOR_HIDE = 1500,
TO_THUMBS_LOAD = 200 TO_THUMBS_LOAD = 200
}; };
@ -58,8 +57,6 @@ extern int filecnt, fileidx;
int timo_cursor; int timo_cursor;
int timo_redraw; int timo_redraw;
unsigned char dragging;
int mox, moy;
int run_command(const char *cline, Bool reload) { int run_command(const char *cline, Bool reload) {
int fncnt, fnlen; int fncnt, fnlen;
@ -122,7 +119,7 @@ void redraw() {
img_render(&img, &win); img_render(&img, &win);
if (timo_cursor) if (timo_cursor)
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
else if (!dragging) else
win_set_cursor(&win, CURSOR_NONE); win_set_cursor(&win, CURSOR_NONE);
} else { } else {
tns_render(&tns, &win); tns_render(&tns, &win);
@ -131,16 +128,14 @@ void redraw() {
timo_redraw = 0; timo_redraw = 0;
} }
void on_keypress(XEvent *ev) { void on_keypress(XKeyEvent *kev) {
int i; int i;
XKeyEvent *kev;
KeySym ksym; KeySym ksym;
char key; char key;
if (!ev || ev->type != KeyPress) if (!kev)
return; return;
kev = &ev->xkey;
XLookupString(kev, &key, 1, &ksym, NULL); XLookupString(kev, &key, 1, &ksym, NULL);
if (EXT_COMMANDS && (CLEANMASK(kev->state) & ControlMask)) { if (EXT_COMMANDS && (CLEANMASK(kev->state) & ControlMask)) {
@ -174,39 +169,34 @@ void on_keypress(XEvent *ev) {
for (i = 0; i < LEN(keys); i++) { for (i = 0; i < LEN(keys); i++) {
if (ksym == keys[i].ksym && keys[i].handler) { if (ksym == keys[i].ksym && keys[i].handler) {
if (keys[i].handler(ev, keys[i].arg)) if (keys[i].handler(keys[i].arg))
redraw(); redraw();
return; return;
} }
} }
} }
void on_buttonpress(XEvent *ev) { void on_buttonpress(XButtonEvent *bev) {
int i, sel; int i, sel;
XButtonEvent *bev;
if (!ev || ev->type != ButtonPress) if (!bev)
return; return;
bev = &ev->xbutton;
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
if (!dragging) {
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
timo_cursor = TO_CURSOR_HIDE; timo_cursor = TO_CURSOR_HIDE;
}
for (i = 0; i < LEN(buttons); i++) { for (i = 0; i < LEN(buttons); i++) {
if (CLEANMASK(bev->state) == CLEANMASK(buttons[i].mod) && if (CLEANMASK(bev->state) == CLEANMASK(buttons[i].mod) &&
bev->button == buttons[i].button && buttons[i].handler) bev->button == buttons[i].button && buttons[i].handler)
{ {
if (buttons[i].handler(ev, buttons[i].arg)) if (buttons[i].handler(buttons[i].arg))
redraw(); redraw();
return; return;
} }
} }
} else { } else {
/* thumbnail mode */ /* thumbnail mode (hard-coded) */
switch (bev->button) { switch (bev->button) {
case Button1: case Button1:
if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) { if ((sel = tns_translate(&tns, bev->x, bev->y)) >= 0) {
@ -232,29 +222,12 @@ void on_buttonpress(XEvent *ev) {
} }
} }
void on_motionnotify(XEvent *ev) {
XMotionEvent *mev;
if (!ev || ev->type != MotionNotify)
return;
mev = &ev->xmotion;
if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
if (img_move(&img, &win, mev->x - mox, mev->y - moy))
timo_redraw = TO_IMAGE_DRAG;
mox = mev->x;
moy = mev->y;
}
}
void run() { void run() {
int xfd, timeout; int xfd, timeout;
fd_set fds; fd_set fds;
struct timeval tt, t0, t1; struct timeval tt, t0, t1;
XEvent ev; XEvent ev;
dragging = 0;
timo_cursor = mode == MODE_NORMAL ? TO_CURSOR_HIDE : 0; timo_cursor = mode == MODE_NORMAL ? TO_CURSOR_HIDE : 0;
redraw(); redraw();
@ -312,18 +285,10 @@ void run() {
} }
if (!XNextEvent(win.env.dpy, &ev)) { if (!XNextEvent(win.env.dpy, &ev)) {
/* handle events */
switch (ev.type) { switch (ev.type) {
case ButtonPress: case ButtonPress:
on_buttonpress(&ev); on_buttonpress(&ev.xbutton);
break;
case ButtonRelease:
if (dragging) {
dragging = 0;
if (mode == MODE_NORMAL) {
win_set_cursor(&win, CURSOR_ARROW);
timo_cursor = TO_CURSOR_HIDE;
}
}
break; break;
case ClientMessage: case ClientMessage:
if ((Atom) ev.xclient.data.l[0] == wm_delete_win) if ((Atom) ev.xclient.data.l[0] == wm_delete_win)
@ -339,16 +304,12 @@ void run() {
} }
break; break;
case KeyPress: case KeyPress:
on_keypress(&ev); on_keypress(&ev.xkey);
break; break;
case MotionNotify: case MotionNotify:
if (dragging) {
on_motionnotify(&ev);
} else if (mode == MODE_NORMAL) {
if (!timo_cursor) if (!timo_cursor)
win_set_cursor(&win, CURSOR_ARROW); win_set_cursor(&win, CURSOR_ARROW);
timo_cursor = TO_CURSOR_HIDE; timo_cursor = TO_CURSOR_HIDE;
}
break; break;
} }
} }
@ -358,12 +319,12 @@ void run() {
/* handler functions for key and button mappings: */ /* handler functions for key and button mappings: */
int quit(XEvent *e, arg_t a) { int quit(arg_t a) {
cleanup(); cleanup();
exit(0); exit(0);
} }
int reload(XEvent *e, arg_t a) { int reload(arg_t a) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
load_image(fileidx); load_image(fileidx);
return 1; return 1;
@ -372,7 +333,7 @@ int reload(XEvent *e, arg_t a) {
} }
} }
int toggle_fullscreen(XEvent *e, arg_t a) { int toggle_fullscreen(arg_t a) {
win_toggle_fullscreen(&win); win_toggle_fullscreen(&win);
if (mode == MODE_NORMAL) if (mode == MODE_NORMAL)
img.checkpan = 1; img.checkpan = 1;
@ -382,7 +343,7 @@ int toggle_fullscreen(XEvent *e, arg_t a) {
return 0; return 0;
} }
int toggle_antialias(XEvent *e, arg_t a) { int toggle_antialias(arg_t a) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
img_toggle_antialias(&img); img_toggle_antialias(&img);
return 1; return 1;
@ -391,7 +352,7 @@ int toggle_antialias(XEvent *e, arg_t a) {
} }
} }
int toggle_alpha(XEvent *e, arg_t a) { int toggle_alpha(arg_t a) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
img.alpha ^= 1; img.alpha ^= 1;
return 1; return 1;
@ -400,7 +361,7 @@ int toggle_alpha(XEvent *e, arg_t a) {
} }
} }
int switch_mode(XEvent *e, arg_t a) { int switch_mode(arg_t a) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
if (!tns.thumbs) if (!tns.thumbs)
tns_init(&tns, filecnt); tns_init(&tns, filecnt);
@ -418,7 +379,7 @@ int switch_mode(XEvent *e, arg_t a) {
return 1; return 1;
} }
int navigate(XEvent *e, arg_t n) { int navigate(arg_t n) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
n += fileidx; n += fileidx;
if (n < 0) if (n < 0)
@ -434,7 +395,7 @@ int navigate(XEvent *e, arg_t n) {
return 0; return 0;
} }
int first(XEvent *e, arg_t a) { int first(arg_t a) {
if (mode == MODE_NORMAL && fileidx != 0) { if (mode == MODE_NORMAL && fileidx != 0) {
load_image(0); load_image(0);
return 1; return 1;
@ -447,7 +408,7 @@ int first(XEvent *e, arg_t a) {
} }
} }
int last(XEvent *e, arg_t a) { int last(arg_t a) {
if (mode == MODE_NORMAL && fileidx != filecnt - 1) { if (mode == MODE_NORMAL && fileidx != filecnt - 1) {
load_image(filecnt - 1); load_image(filecnt - 1);
return 1; return 1;
@ -460,7 +421,7 @@ int last(XEvent *e, arg_t a) {
} }
} }
int remove_image(XEvent *e, arg_t a) { int remove_image(arg_t a) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
remove_file(fileidx, 1); remove_file(fileidx, 1);
load_image(fileidx >= filecnt ? filecnt - 1 : fileidx); load_image(fileidx >= filecnt ? filecnt - 1 : fileidx);
@ -476,39 +437,83 @@ int remove_image(XEvent *e, arg_t a) {
} }
} }
int move(XEvent *e, arg_t dir) { int move(arg_t dir) {
if (mode == MODE_NORMAL) if (mode == MODE_NORMAL)
return img_pan(&img, &win, dir, 0); return img_pan(&img, &win, dir, 0);
else else
return tns_move_selection(&tns, &win, dir); return tns_move_selection(&tns, &win, dir);
} }
int pan_screen(XEvent *e, arg_t dir) { int pan_screen(arg_t dir) {
if (mode == MODE_NORMAL) if (mode == MODE_NORMAL)
return img_pan(&img, &win, dir, 1); return img_pan(&img, &win, dir, 1);
else else
return 0; return 0;
} }
int pan_edge(XEvent *e, arg_t dir) { int pan_edge(arg_t dir) {
if (mode == MODE_NORMAL) if (mode == MODE_NORMAL)
return img_pan_edge(&img, &win, dir); return img_pan_edge(&img, &win, dir);
else else
return 0; return 0;
} }
int drag(XEvent *e, arg_t a) { /* Xlib helper function for drag() */
if (mode == MODE_NORMAL) { Bool ismnotify(Display *d, XEvent *e, XPointer a) {
mox = e->xbutton.x; return e != NULL && e->type == MotionNotify;
moy = e->xbutton.y; }
int drag(arg_t a) {
int dx = 0, dy = 0, i, ox, oy, x, y;
unsigned int ui;
Bool dragging = True, next = False;
XEvent e;
Window w;
if (mode != MODE_NORMAL)
return 0;
if (!XQueryPointer(win.env.dpy, win.xwin, &w, &w, &i, &i, &ox, &oy, &ui))
return 0;
win_set_cursor(&win, CURSOR_HAND); win_set_cursor(&win, CURSOR_HAND);
timo_cursor = 0;
dragging = 1; while (dragging) {
if (!next)
XMaskEvent(win.env.dpy,
ButtonPressMask | ButtonReleaseMask | PointerMotionMask, &e);
switch (e.type) {
case ButtonPress:
case ButtonRelease:
dragging = False;
break;
case MotionNotify:
x = e.xmotion.x;
y = e.xmotion.y;
if (x >= 0 && x <= win.w && y >= 0 && y <= win.h) {
dx += x - ox;
dy += y - oy;
} }
ox = x;
oy = y;
break;
}
if (dragging)
next = XCheckIfEvent(win.env.dpy, &e, ismnotify, None);
if ((!dragging || !next) && (dx != 0 || dy != 0)) {
if (img_move(&img, &win, dx, dy))
img_render(&img, &win);
dx = dy = 0;
}
}
win_set_cursor(&win, CURSOR_ARROW);
timo_cursor = TO_CURSOR_HIDE;
timo_redraw = 0;
return 0; return 0;
} }
int rotate(XEvent *e, arg_t dir) { int rotate(arg_t dir) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
if (dir == DIR_LEFT) { if (dir == DIR_LEFT) {
img_rotate_left(&img, &win); img_rotate_left(&img, &win);
@ -521,7 +526,7 @@ int rotate(XEvent *e, arg_t dir) {
return 0; return 0;
} }
int zoom(XEvent *e, arg_t scale) { int zoom(arg_t scale) {
if (mode != MODE_NORMAL) if (mode != MODE_NORMAL)
return 0; return 0;
if (scale > 0) if (scale > 0)
@ -532,7 +537,7 @@ int zoom(XEvent *e, arg_t scale) {
return img_zoom(&img, &win, 1.0); return img_zoom(&img, &win, 1.0);
} }
int fit_to_win(XEvent *e, arg_t ret) { int fit_to_win(arg_t ret) {
if (mode == MODE_NORMAL) { if (mode == MODE_NORMAL) {
if ((ret = img_fit_win(&img, &win))) if ((ret = img_fit_win(&img, &win)))
img_center(&img, &win); img_center(&img, &win);
@ -542,7 +547,7 @@ int fit_to_win(XEvent *e, arg_t ret) {
} }
} }
int fit_to_img(XEvent *e, arg_t ret) { int fit_to_img(arg_t ret) {
int x, y; int x, y;
unsigned int w, h; unsigned int w, h;

View file

@ -31,37 +31,37 @@ typedef int arg_t;
typedef struct { typedef struct {
KeySym ksym; KeySym ksym;
int (*handler)(XEvent*, arg_t); int (*handler)(arg_t);
arg_t arg; arg_t arg;
} keymap_t; } keymap_t;
typedef struct { typedef struct {
unsigned int mod; unsigned int mod;
unsigned int button; unsigned int button;
int (*handler)(XEvent*, arg_t); int (*handler)(arg_t);
arg_t arg; arg_t arg;
} button_t; } button_t;
void run(); void run();
/* handler functions for key and button mappings: */ /* handler functions for key and button mappings: */
int quit(XEvent*, arg_t); int quit(arg_t);
int reload(XEvent*, arg_t); int reload(arg_t);
int toggle_fullscreen(XEvent*, arg_t); int toggle_fullscreen(arg_t);
int toggle_antialias(XEvent*, arg_t); int toggle_antialias(arg_t);
int toggle_alpha(XEvent*, arg_t); int toggle_alpha(arg_t);
int switch_mode(XEvent*, arg_t); int switch_mode(arg_t);
int navigate(XEvent*, arg_t); int navigate(arg_t);
int first(XEvent*, arg_t); int first(arg_t);
int last(XEvent*, arg_t); int last(arg_t);
int remove_image(XEvent*, arg_t); int remove_image(arg_t);
int move(XEvent*, arg_t); int move(arg_t);
int pan_screen(XEvent*, arg_t); int pan_screen(arg_t);
int pan_edge(XEvent*, arg_t); int pan_edge(arg_t);
int drag(XEvent*, arg_t); int drag(arg_t);
int rotate(XEvent*, arg_t); int rotate(arg_t);
int zoom(XEvent*, arg_t); int zoom(arg_t);
int fit_to_win(XEvent*, arg_t); int fit_to_win(arg_t);
int fit_to_img(XEvent*, arg_t); int fit_to_img(arg_t);
#endif /* EVENTS_H */ #endif /* EVENTS_H */