]> git.mdlowis.com Git - projs/tide.git/commitdiff
Added new mouse event data structure and moved mouse handling to it's own module
authorMike Lowis <mike.lowis@gentex.com>
Thu, 6 Oct 2016 16:49:26 +0000 (12:49 -0400)
committerMike Lowis <mike.lowis@gentex.com>
Thu, 6 Oct 2016 16:49:26 +0000 (12:49 -0400)
Makefile
edit.h
mouse.c [new file with mode: 0644]
tests/buf.c
xedit.c

index 324547573c1be515b616d5120f8df0630fe3cdf5..b6d9998f185b83ca3f4c22b7f6721aeb12fd917f 100644 (file)
--- 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 13190723ef41b7b1f500a6a935e94100f30ccc45..bd6e009955c15e46bd8f676cbc5a05a7ba66bfcf 100644 (file)
--- 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 (file)
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;
+    }
+}
index 46c5dfafc100b2bf17f46f51228c7c44aa189974..a77c75016d673a18531527c49ad842bdfc592a95 100644 (file)
@@ -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 eb642f835d3038904b15e57960eeb9fbb6b78033..afd2d27247eff0ac378b1fb967e8bd726eadda6e 100644 (file)
--- 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;