Krolyxon
Commit fde9f5b

Branches

Refs →

nsxivfde9f5b

Hide cursor on redraw or after 1.5s mouse inactivity

Author
Bert
Date
2011-02-21 18:26 +0100
Parent
280fcf6
Commit
fde9f5bf859e582fb85d43831b90b7588ebfc3d3

+67 −17

3 files changed

  1. M main.c +50 −14
  2. M window.c +16 −3
  3. M window.h +1 −0
M main.c View
@@ -255,13 +255,21 @@ void read_dir_rec(const char *dirname) {
255255 /* event handling */
256256
257257 int timeout;
258+unsigned char hidecur;
259+
258260 int mox, moy;
261+unsigned char drag;
259262
260263 void redraw() {
261− if (mode == MODE_NORMAL)
264+ if (mode == MODE_NORMAL) {
265+ if (!drag && hidecur) {
266+ win_set_cursor(&win, CURSOR_NONE);
267+ hidecur = 0;
268+ }
262269 img_render(&img, &win);
263− else
270+ } else {
264271 tns_render(&tns, &win);
272+ }
265273 update_title();
266274 timeout = 0;
267275 }
@@ -377,11 +385,12 @@ void on_keypress(XKeyEvent *kev) {
377385 }
378386 break;
379387
380− /* switch to thumnail mode */
388+ /* switch to thumbnail mode */
381389 case XK_Return:
382390 if (!tns.thumbs)
383391 tns_init(&tns, filecnt);
384392 mode = MODE_THUMBS;
393+ win_set_cursor(&win, CURSOR_ARROW);
385394 tns.sel = fileidx;
386395 changed = tns.dirty = 1;
387396 break;
@@ -403,7 +412,7 @@ void on_keypress(XKeyEvent *kev) {
403412 fileidx = tns.sel;
404413 load_image();
405414 mode = MODE_NORMAL;
406− win_set_cursor(&win, CURSOR_ARROW);
415+ win_set_cursor(&win, CURSOR_NONE);
407416 changed = 1;
408417 break;
409418
@@ -479,6 +488,8 @@ void on_buttonpress(XButtonEvent *bev) {
479488 mox = bev->x;
480489 moy = bev->y;
481490 win_set_cursor(&win, CURSOR_HAND);
491+ hidecur = 0;
492+ drag = 1;
482493 break;
483494 case Button3:
484495 if (fileidx > 0) {
@@ -518,7 +529,7 @@ void on_buttonpress(XButtonEvent *bev) {
518529 fileidx = tns.sel;
519530 load_image();
520531 mode = MODE_NORMAL;
521− win_set_cursor(&win, CURSOR_ARROW);
532+ win_set_cursor(&win, CURSOR_NONE);
522533 } else {
523534 tns_highlight(&tns, &win, tns.sel, False);
524535 tns_highlight(&tns, &win, sel, True);
@@ -542,7 +553,7 @@ void on_buttonpress(XButtonEvent *bev) {
542553 }
543554
544555 void on_motionnotify(XMotionEvent *mev) {
545− if (!mev || mode != MODE_NORMAL)
556+ if (!mev)
546557 return;
547558
548559 if (mev->x >= 0 && mev->x <= win.w && mev->y >= 0 && mev->y <= win.h) {
@@ -560,7 +571,12 @@ void run() {
560571 struct timeval t, t0;
561572 XEvent ev;
562573
563− timeout = 0;
574+ drag = timeout = 0;
575+
576+ if (mode == MODE_NORMAL) {
577+ hidecur = 1;
578+ timeout = 1500000;
579+ }
564580
565581 while (1) {
566582 if (mode == MODE_THUMBS && tns.cnt < filecnt) {
@@ -582,15 +598,21 @@ void run() {
582598 timeout = 75000;
583599 }
584600 } else if (timeout) {
585− t.tv_sec = 0;
586− t.tv_usec = timeout;
601+ t.tv_sec = timeout / 1000000;
602+ t.tv_usec = timeout % 1000000;
587603 xfd = ConnectionNumber(win.env.dpy);
588604 FD_ZERO(&fds);
589605 FD_SET(xfd, &fds);
590606
591− if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t))
607+ if (!XPending(win.env.dpy) && !select(xfd + 1, &fds, 0, 0, &t)) {
592608 /* timeout fired */
593− redraw();
609+ if (hidecur) {
610+ win_set_cursor(&win, CURSOR_NONE);
611+ hidecur = 0;
612+ } else {
613+ redraw();
614+ }
615+ }
594616 }
595617
596618 if (!XNextEvent(win.env.dpy, &ev)) {
@@ -602,11 +624,25 @@ void run() {
602624 on_buttonpress(&ev.xbutton);
603625 break;
604626 case ButtonRelease:
605− if (ev.xbutton.button == Button2)
606− win_set_cursor(&win, CURSOR_ARROW);
627+ if (ev.xbutton.button == Button2) {
628+ drag = 0;
629+ if (mode == MODE_NORMAL) {
630+ win_set_cursor(&win, CURSOR_ARROW);
631+ hidecur = 1;
632+ timeout = 1500000;
633+ }
634+ }
607635 break;
608636 case MotionNotify:
609− on_motionnotify(&ev.xmotion);
637+ if (drag) {
638+ on_motionnotify(&ev.xmotion);
639+ } else if (mode == MODE_NORMAL) {
640+ if (!hidecur) {
641+ win_set_cursor(&win, CURSOR_ARROW);
642+ hidecur = 1;
643+ }
644+ timeout = 1500000;
645+ }
610646 break;
611647 case ConfigureNotify:
612648 if (win_configure(&win, &ev.xconfigure)) {
M window.c View
@@ -27,6 +27,7 @@
2727 #include "window.h"
2828
2929 static Cursor carrow;
30+static Cursor cnone;
3031 static Cursor chand;
3132 static Cursor cwatch;
3233 static GC gc;
@@ -52,6 +53,8 @@ void win_open(win_t *win) {
5253 XClassHint classhint;
5354 XColor col;
5455 XGCValues gcval;
56+ char none_data[] = {0, 0, 0, 0, 0, 0, 0, 0};
57+ Pixmap none;
5558 int gmask;
5659
5760 if (!win)
@@ -69,12 +72,12 @@ void win_open(win_t *win) {
6972 e->depth = DefaultDepth(e->dpy, e->scr);
7073
7174 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), BG_COLOR,
72− &col, &col))
75+ &col, &col))
7376 win->bgcol = col.pixel;
7477 else
7578 die("could not allocate color: %s", BG_COLOR);
7679 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), SEL_COLOR,
77− &col, &col))
80+ &col, &col))
7881 win->selcol = col.pixel;
7982 else
8083 die("could not allocate color: %s", BG_COLOR);
@@ -112,12 +115,18 @@ void win_open(win_t *win) {
112115 die("could not create window");
113116
114117 XSelectInput(e->dpy, win->xwin, StructureNotifyMask | KeyPressMask |
115− ButtonPressMask | ButtonReleaseMask | Button2MotionMask);
118+ ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
116119
117120 carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
118121 chand = XCreateFontCursor(e->dpy, XC_fleur);
119122 cwatch = XCreateFontCursor(e->dpy, XC_watch);
120123
124+ if (!XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
125+ &col, &col))
126+ die("could not allocate color: black");
127+ none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
128+ cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
129+
121130 gcval.line_width = 2;
122131 gc = XCreateGC(e->dpy, win->xwin, GCLineWidth, &gcval);
123132
@@ -145,6 +154,7 @@ void win_close(win_t *win) {
145154 return;
146155
147156 XFreeCursor(win->env.dpy, carrow);
157+ XFreeCursor(win->env.dpy, cnone);
148158 XFreeCursor(win->env.dpy, chand);
149159 XFreeCursor(win->env.dpy, cwatch);
150160
@@ -307,6 +317,9 @@ void win_set_cursor(win_t *win, win_cur_t cursor) {
307317 return;
308318
309319 switch (cursor) {
320+ case CURSOR_NONE:
321+ XDefineCursor(win->env.dpy, win->xwin, cnone);
322+ break;
310323 case CURSOR_HAND:
311324 XDefineCursor(win->env.dpy, win->xwin, chand);
312325 break;
M window.h View
@@ -25,6 +25,7 @@
2525
2626 typedef enum win_cur_e {
2727 CURSOR_ARROW = 0,
28+ CURSOR_NONE,
2829 CURSOR_HAND,
2930 CURSOR_WATCH
3031 } win_cur_t;