Krolyxon
Commit b8fd923

Branches

Refs →

nsxivb8fd923

Simplify cursor handling

Author
Bert Münnich
Date
2017-10-05 12:30 +0200
Parent
e310136
Commit
b8fd923e740bcbaebe523c48ca67c3725c1b3863

+25 −32

3 files changed

  1. M Makefile +1 −1
  2. M types.h +4 −2
  3. M window.c +20 −29
M Makefile View
@@ -1,4 +1,4 @@
1−VERSION := git-20171004
1+VERSION := git-20171005
22
33 all: sxiv
44
M types.h View
@@ -65,9 +65,11 @@ typedef enum {
6565
6666 typedef enum {
6767 CURSOR_ARROW,
68− CURSOR_NONE,
6968 CURSOR_DRAG,
70− CURSOR_WATCH
69+ CURSOR_WATCH,
70+ CURSOR_NONE,
71+
72+ CURSOR_COUNT
7173 } cursor_t;
7274
7375 typedef enum {
M window.c View
@@ -35,10 +35,13 @@ enum {
3535 V_TEXT_PAD = 1
3636 };
3737
38−static Cursor carrow;
39−static Cursor cnone;
40−static Cursor cdrag;
41−static Cursor cwatch;
38+static struct {
39+ int name;
40+ Cursor icon;
41+} cursors[CURSOR_COUNT] = {
42+ { XC_left_ptr }, { XC_dotbox }, { XC_watch }
43+};
44+
4245 static GC gc;
4346
4447 static XftFont *font;
@@ -153,6 +156,7 @@ void win_open(win_t *win)
153156 XClassHint classhint;
154157 unsigned long *icon_data;
155158 XColor col;
159+ Cursor *cnone = &cursors[CURSOR_NONE].icon;
156160 char none_data[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
157161 Pixmap none;
158162 int gmask;
@@ -209,17 +213,17 @@ void win_open(win_t *win)
209213 ButtonReleaseMask | ButtonPressMask | KeyPressMask |
210214 PointerMotionMask | StructureNotifyMask);
211215
212− carrow = XCreateFontCursor(e->dpy, XC_left_ptr);
213− cdrag = XCreateFontCursor(e->dpy, XC_dotbox);
214− cwatch = XCreateFontCursor(e->dpy, XC_watch);
215−
216+ for (i = 0; i < ARRLEN(cursors); i++) {
217+ if (i != CURSOR_NONE)
218+ cursors[i].icon = XCreateFontCursor(e->dpy, cursors[i].name);
219+ }
216220 if (XAllocNamedColor(e->dpy, DefaultColormap(e->dpy, e->scr), "black",
217221 &col, &col) == 0)
218222 {
219223 error(EXIT_FAILURE, 0, "Error allocating color 'black'");
220224 }
221225 none = XCreateBitmapFromData(e->dpy, win->xwin, none_data, 8, 8);
222− cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
226+ *cnone = XCreatePixmapCursor(e->dpy, none, none, &col, &col, 0, 0);
223227
224228 gc = XCreateGC(e->dpy, win->xwin, 0, None);
225229
@@ -275,10 +279,10 @@ void win_open(win_t *win)
275279
276280 CLEANUP void win_close(win_t *win)
277281 {
278− XFreeCursor(win->env.dpy, carrow);
279− XFreeCursor(win->env.dpy, cnone);
280− XFreeCursor(win->env.dpy, cdrag);
281− XFreeCursor(win->env.dpy, cwatch);
282+ int i;
283+
284+ for (i = 0; i < ARRLEN(cursors); i++)
285+ XFreeCursor(win->env.dpy, cursors[i].icon);
282286
283287 XFreeGC(win->env.dpy, gc);
284288
@@ -461,21 +465,8 @@ void win_set_title(win_t *win, const char *title)
461465
462466 void win_set_cursor(win_t *win, cursor_t cursor)
463467 {
464− switch (cursor) {
465− case CURSOR_NONE:
466− XDefineCursor(win->env.dpy, win->xwin, cnone);
467− break;
468− case CURSOR_DRAG:
469− XDefineCursor(win->env.dpy, win->xwin, cdrag);
470− break;
471− case CURSOR_WATCH:
472− XDefineCursor(win->env.dpy, win->xwin, cwatch);
473− break;
474− case CURSOR_ARROW:
475− default:
476− XDefineCursor(win->env.dpy, win->xwin, carrow);
477− break;
468+ if (cursor >= 0 && cursor < ARRLEN(cursors)) {
469+ XDefineCursor(win->env.dpy, win->xwin, cursors[cursor].icon);
470+ XFlush(win->env.dpy);
478471 }
479−
480− XFlush(win->env.dpy);
481472 }