From d4a9063652ce5de126adcdea4024a6bba0af2a99 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 9 Oct 2016 11:18:02 -0400 Subject: [PATCH] General refactroing and cleanup --- edit.h | 7 ++++++- keyboard.c | 4 ---- mouse.c | 42 ++++++++++++++++++++++++++---------------- tests/tests.c | 2 +- xedit.c | 17 +++++++++-------- 5 files changed, 42 insertions(+), 30 deletions(-) diff --git a/edit.h b/edit.h index 8e1752a..4202cd1 100644 --- a/edit.h +++ b/edit.h @@ -216,8 +216,13 @@ enum ColorScheme { LIGHT = 1 }; +/* Global State + *****************************************************************************/ /* variable for holding the currently selected color scheme */ -enum ColorScheme ColorBase; +extern enum ColorScheme ColorBase; +extern Buf Buffer; +extern unsigned CursorPos; +extern unsigned TargetCol; /* Configuration *****************************************************************************/ diff --git a/keyboard.c b/keyboard.c index 3730958..8014269 100644 --- a/keyboard.c +++ b/keyboard.c @@ -1,9 +1,5 @@ #include "edit.h" -extern Buf Buffer; -extern unsigned CursorPos; -extern unsigned TargetCol; - static void special_keys(Rune key); static void control_keys(Rune key); static void vi_keys(Rune key); diff --git a/mouse.c b/mouse.c index cf7a37d..e97a2e1 100644 --- a/mouse.c +++ b/mouse.c @@ -1,14 +1,5 @@ #include "edit.h" -struct { - uint32_t time; - uint32_t count; -} Buttons[5] = { {0,0}, {0,0}, {0,0} }; - -extern Buf Buffer; -extern unsigned CursorPos; -extern unsigned TargetCol; - #ifndef __MACH__ #include #else @@ -34,12 +25,6 @@ uint32_t getmillis(void) { /*****************************************************************************/ -enum { - SINGLE_CLICK = 0, - DOUBLE_CLICK, - TRIPLE_CLICK -}; - void unused(MouseEvent* mevnt) { (void)mevnt; } @@ -67,6 +52,19 @@ void scrolldn(MouseEvent* mevnt) { CursorPos = buf_byline(&Buffer, CursorPos, ScrollLines); } +/*****************************************************************************/ + +enum { + SINGLE_CLICK = 0, + DOUBLE_CLICK, + TRIPLE_CLICK +}; + +struct { + uint32_t time; + uint32_t count; +} Buttons[5] = { {0,0}, {0,0}, {0,0} }; + void (*Actions[5][3])(MouseEvent* mevnt) = { [MOUSE_LEFT] = { [SINGLE_CLICK] = move_cursor, @@ -95,7 +93,7 @@ void (*Actions[5][3])(MouseEvent* mevnt) = { }, }; -void handle_mouse(MouseEvent* mevnt) { +static void handle_click(MouseEvent* mevnt) { if (mevnt->button >= 5) return; /* update the number of clicks */ uint32_t now = getmillis(); @@ -110,3 +108,15 @@ void handle_mouse(MouseEvent* mevnt) { nclicks = (nclicks > 3 ? 1 : nclicks); Actions[mevnt->button][nclicks-1](mevnt); } + +static void handle_drag(MouseEvent* mevnt) { + (void)mevnt; +} + +void handle_mouse(MouseEvent* mevnt) { + if (mevnt->type == MouseDown) { + handle_click(mevnt); + } else if (mevnt->type == MouseMove) { + handle_drag(mevnt); + } +} diff --git a/tests/tests.c b/tests/tests.c index 5914bea..428a740 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -5,12 +5,12 @@ Buf Buffer; unsigned CursorPos; unsigned TargetCol; +enum ColorScheme ColorBase; void die(char* m) { (void)m; } - int main(int argc, char** argv) { atf_init(argc,argv); RUN_EXTERN_TEST_SUITE(BufferTests); diff --git a/xedit.c b/xedit.c index 1f86734..4cbdbd4 100644 --- a/xedit.c +++ b/xedit.c @@ -45,7 +45,16 @@ static XftColor xftcolor(enum ColorId cid) { return xc; } +static void deinit(void) { + if (X.pixmap != None) { + XftDrawDestroy(X.xft); + XFreePixmap(X.display, X.pixmap); + } + XCloseDisplay(X.display); +} + static int init(void) { + atexit(deinit); signal(SIGPIPE, SIG_IGN); // Ignore the SIGPIPE signal /* open the X display and get basic attributes */ if (!(X.display = XOpenDisplay(0))) @@ -96,14 +105,6 @@ static int init(void) { return XConnectionNumber(X.display); } -static void deinit(void) { - if (X.pixmap != None) { - XftDrawDestroy(X.xft); - XFreePixmap(X.display, X.pixmap); - } - XCloseDisplay(X.display); -} - static Rune getkey(XEvent* e) { Rune rune = RUNE_ERR; size_t len = 0; -- 2.49.0