From 5f435b1ffc271b113f433823c28e4ec4d7a65d25 Mon Sep 17 00:00:00 2001 From: Mike Lowis Date: Thu, 6 Oct 2016 12:49:26 -0400 Subject: [PATCH] Added new mouse event data structure and moved mouse handling to it's own module --- Makefile | 2 +- edit.h | 25 ++++++++++++++++++------- mouse.c | 20 ++++++++++++++++++++ tests/buf.c | 3 +-- xedit.c | 40 ++++++++++++++++++++++------------------ 5 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 mouse.c diff --git a/Makefile b/Makefile index 3245475..b6d9998 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LDFLAGS = -L/opt/X11/lib -lX11 -lXft CFLAGS = --std=c99 -Wall -Wextra -I. -I/opt/X11/include -I/opt/local/include/freetype2 -I/usr/include/freetype2 -SRCS = xedit.c buf.c screen.c utf8.c keyboard.c +SRCS = xedit.c buf.c screen.c utf8.c keyboard.c mouse.c OBJS = $(SRCS:.c=.o) TESTSRCS = tests/tests.c tests/buf.c tests/utf8.c TESTOBJS = $(TESTSRCS:.c=.o) diff --git a/edit.h b/edit.h index 1319072..bd6e009 100644 --- a/edit.h +++ b/edit.h @@ -95,15 +95,26 @@ enum Keys { }; /* Define the mouse buttons used for input */ -enum MouseBtn { - MOUSE_LEFT = 0, - MOUSE_MIDDLE, - MOUSE_RIGHT, - MOUSE_WHEELUP, - MOUSE_WHEELDOWN -}; +typedef struct { + enum { + MouseUp, + MouseDown, + MouseMove + } type; + enum { + MOUSE_LEFT = 0, + MOUSE_MIDDLE, + MOUSE_RIGHT, + MOUSE_WHEELUP, + MOUSE_WHEELDOWN, + MOUSE_NONE + } button; + int x; + int y; +} MouseEvent; void handle_key(Rune key); +void handle_mouse(MouseEvent* mevnt); /* Buffer management functions *****************************************************************************/ diff --git a/mouse.c b/mouse.c new file mode 100644 index 0000000..556bdef --- /dev/null +++ b/mouse.c @@ -0,0 +1,20 @@ +#include "edit.h" + +extern Buf Buffer; +extern unsigned CursorPos; + +void handle_mouse(MouseEvent* mevnt) { + switch (mevnt->button) { + case MOUSE_LEFT: + CursorPos = screen_getoff(&Buffer, CursorPos, mevnt->y, mevnt->x); + break; + case MOUSE_WHEELUP: + CursorPos = buf_byline(&Buffer, CursorPos, -ScrollLines); + break; + case MOUSE_WHEELDOWN: + CursorPos = buf_byline(&Buffer, CursorPos, ScrollLines); + break; + default: + break; + } +} diff --git a/tests/buf.c b/tests/buf.c index 46c5dfa..a77c750 100644 --- a/tests/buf.c +++ b/tests/buf.c @@ -8,17 +8,16 @@ static void set_buffer_text(char* str) { buf_clr(&TestBuf); for (Rune* curr = TestBuf.bufstart; curr < TestBuf.bufend; curr++) *curr = '-'; + TestBuf.insert_mode = true; while (*str) buf_ins(&TestBuf, i++, (Rune)*str++); } static bool buf_text_eq(char* str) { for (unsigned i = 0; i < buf_end(&TestBuf); i++) { -// printf("%c", (char)buf_get(&TestBuf, i)); if ((Rune)*(str++) != buf_get(&TestBuf, i)) return false; } -// puts(""); return true; } diff --git a/xedit.c b/xedit.c index eb642f8..afd2d27 100644 --- a/xedit.c +++ b/xedit.c @@ -148,32 +148,36 @@ static Rune getkey(XEvent* e) { } } -static void handle_mousebtn(XEvent* e) { - switch (e->xbutton.button) { - case Button1: /* Left Button */ - CursorPos = screen_getoff(&Buffer, CursorPos, - e->xbutton.y / (X.font->ascent + X.font->descent), - e->xbutton.x / X.font->max_advance_width); - break; - case Button2: /* Middle Button */ - break; - case Button3: /* Right Button */ - break; - case Button4: /* Wheel Up */ - CursorPos = buf_byline(&Buffer, CursorPos, -ScrollLines); - break; - case Button5: /* Wheel Down */ - CursorPos = buf_byline(&Buffer, CursorPos, ScrollLines); - break; +static MouseEvent* getmouse(XEvent* e) { + static MouseEvent event; + if (e->type == MotionNotify) { + event.type = MouseMove; + event.button = MOUSE_NONE; + event.x = e->xmotion.x / X.font->max_advance_width; + event.y = e->xmotion.y / (X.font->ascent + X.font->descent); + } else { + event.type = (e->type = ButtonPress ? MouseDown : MouseUp); + /* set the button id */ + switch (e->xbutton.button) { + case Button1: event.button = MOUSE_LEFT; break; + case Button2: event.button = MOUSE_MIDDLE; break; + case Button3: event.button = MOUSE_RIGHT; break; + case Button4: event.button = MOUSE_WHEELUP; break; + case Button5: event.button = MOUSE_WHEELDOWN; break; + default: event.button = MOUSE_NONE; break; + } + event.x = e->xbutton.x / X.font->max_advance_width; + event.y = e->xbutton.y / (X.font->ascent + X.font->descent); } + return &event; } static void handle_event(XEvent* e) { switch (e->type) { case FocusIn: if (X.xic) XSetICFocus(X.xic); break; case FocusOut: if (X.xic) XUnsetICFocus(X.xic); break; - case ButtonPress: handle_mousebtn(e); break; case KeyPress: handle_key(getkey(e)); break; + case ButtonPress: handle_mouse(getmouse(e)); break; case ConfigureNotify: // Resize the window if (e->xconfigure.width != X.width || e->xconfigure.height != X.height) { X.width = e->xconfigure.width; -- 2.49.0