]> Pileus Git - lackey/commitdiff
Add some mouse support
authorAndy Spencer <andy753421@gmail.com>
Sat, 29 Sep 2012 06:59:15 +0000 (06:59 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sat, 29 Sep 2012 06:59:15 +0000 (06:59 +0000)
13 files changed:
src/main.c
src/main.h
src/screen.c
src/screen.h
src/view/day.c
src/view/help.c
src/view/mkview.sh
src/view/month.c
src/view/notes.c
src/view/settings.c
src/view/todo.c
src/view/week.c
src/view/year.c

index 41e1c81c180cf62c047d7bf87a93df0a6856f3b4..5de195da34a006558770a10cc66a8d9be5e22b41 100644 (file)
@@ -7,12 +7,7 @@
 #include "main.h"
 #include "screen.h"
 
-/* Global data */
-int win_rows = 0;
-int win_cols = 0;
-
 /* Static data */
-static WINDOW *win = NULL;
 static FILE *debug_fd = NULL;
 
 /* Control-C handler, so we don't hose the therminal */
@@ -23,21 +18,12 @@ static void on_sigint(int signum)
        exit(0);
 }
 
-/* Window change */
-static void update(void)
-{
-       getmaxyx(win, win_rows, win_cols);
-       win_rows++;
-       win_cols++;
-       screen_draw();
-}
-
 /* Window change */
 static void on_sigwinch(int signum)
 {
        endwin();
        refresh();
-       update();
+       screen_draw();
 }
 
 /* Debugging functions */
@@ -45,9 +31,21 @@ int debug(char *fmt, ...)
 {
        int rval;
        va_list ap;
+
+       /* Log to debug file */
        va_start(ap, fmt);
        vfprintf(debug_fd, "debug: ", ap);
        rval = vfprintf(debug_fd, fmt, ap);
+
+       /* Log to status bar */
+       va_start(ap, fmt);
+       mvhline(LINES-2, 0, ACS_HLINE, COLS);
+       move(LINES-1, 0);
+       attron(COLOR_PAIR(COLOR_ERROR));
+       vwprintw(stdscr, fmt, ap);
+       attroff(COLOR_PAIR(COLOR_ERROR));
+       clrtoeol();
+
        va_end(ap);
        return rval;
 }
@@ -68,26 +66,37 @@ int main(int argc, char **argv)
                debug("sigwinch error\n");
 
        /* Curses setup */
-       win = initscr();
+       initscr();
        cbreak();
        noecho();
+       keypad(stdscr, TRUE);
        start_color();
        curs_set(false);
+       mousemask(ALL_MOUSE_EVENTS, NULL);
+       init_pair(COLOR_TITLE, COLOR_GREEN, COLOR_BLACK);
+       init_pair(COLOR_ERROR, COLOR_RED,   COLOR_BLACK);
        screen_init();
 
        /* Run */
        while (1) {
+               MEVENT btn;
                int chr = getch();
+               if (chr == KEY_MOUSE)
+                       if (getmouse(&btn) != OK)
+                               continue;
                if (chr == 'q')
                        break;
+               if (KEY_MOUSE)
+                       //debug("mouse xyz=%d,%d,%d id=%hd state=%lx\n",
+                       //      btn.x, btn.y, btn.z, btn.id, btn.bstate);
                switch (chr) {
                        case 'L':
                                clear();
                        case 'l':
-                               update();
+                               screen_draw();
                                break;
                        default:
-                               screen_run(chr);
+                               screen_run(chr, btn.bstate, btn.y, btn.x);
                                break;
                }
        }
index ea66f9dec9b7e92fca0d9413623d3956e7d4e8f0..bed6fb1aa5065c74a0c3f740cd90ba93f66c31f2 100644 (file)
@@ -1,6 +1,6 @@
+#define COLOR_TITLE 1
+#define COLOR_ERROR 2
+
 #define N_ELEMENTS(x) (sizeof(x)/sizeof((x)[0]))
 
 int debug(char *fmt, ...);
-
-extern int win_rows;
-extern int win_cols;
index a841f1e9fc68f836fada6f9cb0be2de151d13679..926ed505c0ddb34f7d98db81afd05269d64a85e9 100644 (file)
@@ -1,3 +1,4 @@
+#include <string.h>
 #include <ncurses.h>
 #include "main.h"
 #include "screen.h"
@@ -7,7 +8,7 @@ typedef struct {
        char  *name;
        void (*init)(void);
        void (*draw)(void);
-       int  (*run)(int);
+       int  (*run)(int,mmask_t,int,int);
        int    keys[8];
 } view_t;
 
@@ -25,52 +26,81 @@ view_t views[] = {
        { "Help",     help_init,     help_draw,     help_run,     {KEY_F(8), '8', 'h', '?'} },
 };
 
-view_t *active = &views[0];
+int active = 0;
 
 /* Local functions */
 void draw_header(void)
 {
        move(0, 0);
-       attron(COLOR_PAIR(1));
+       attron(COLOR_PAIR(COLOR_TITLE));
        for (int i = 0; i < N_ELEMENTS(views); i++) {
-               if (active == &views[i])
+               if (i == active)
                        attron(A_BOLD);
                printw(" %s", views[i].name);
-               if (active == &views[i])
+               if (i == active)
                        attroff(A_BOLD);
        }
-       attroff(COLOR_PAIR(1));
-       mvhline(1, 0, ACS_HLINE, win_cols);
+       attroff(COLOR_PAIR(COLOR_TITLE));
+       mvhline(1, 0, ACS_HLINE, COLS);
 }
 
 /* Screen init */
 void screen_init(void)
 {
-       init_pair(1, COLOR_GREEN, COLOR_BLACK);
 }
 
-/* Scren draw */
+/* Screen draw */
 void screen_draw(void)
 {
        draw_header();
-       active->draw();
+       views[active].draw();
+}
+
+/* Screen set */
+int screen_set(int num)
+{
+       if (active != num) {
+               active = num;
+               screen_draw();
+       }
+       return 1;
 }
 
 /* Screen run */
-int screen_run(int chr)
+int screen_run(int key, mmask_t btn, int row, int col)
 {
+       /* Check for mouse events */
+       if (key == KEY_MOUSE && row == 0) {
+               int start = 1;
+               for (int i = 0; i < N_ELEMENTS(views); i++) {
+                       int end = start + strlen(views[i].name) - 1;
+                       if (start <= col && col <= end)
+                               return screen_set(i);
+                       start = end + 2;
+               }
+       }
+
        /* Check for view change */
        for (int i = 0; i < N_ELEMENTS(views); i++) {
-               view_t *view = &views[i];
-               if (view == active)
+               if (i == active)
                        continue;
-               for (int j = 0; j < N_ELEMENTS(view->keys); j++)
-                       if (view->keys[j] == chr) {
-                               active = view;
-                               screen_draw();
-                       }
+               for (int j = 0; j < N_ELEMENTS(views[i].keys); j++)
+                       if (views[i].keys[j] == key)
+                               return screen_set(i);
+       }
+
+       /* Shift windows */
+       int num   = active;
+       int shift = key == KEY_RIGHT ? +1 :
+                   key == KEY_LEFT  ? -1 : 0;
+       while (shift) {
+               num += shift;
+               num += N_ELEMENTS(views);
+               num %= N_ELEMENTS(views);
+               if (views[num].run)
+                       return screen_set(num);
        }
 
        /* Pass key to active view */
-       return active->run(chr);
+       return views[active].run(key, btn, row, col);
 }
index 9df33aba5624cef481abe259b54e02f14f41cb12..0054dbfebe02bb78eab448c32e4a6f2dac3b5b67 100644 (file)
@@ -1,7 +1,7 @@
 /* Screen functions */
 void screen_init(void);
 void screen_draw(void);
-int  screen_run(int);
+int  screen_run(int key, mmask_t btn, int row, int col);
 
 /* View init functions */
 void day_init(void);
@@ -24,11 +24,11 @@ void settings_draw(void);
 void help_draw(void);
 
 /* View run functions */
-int day_run(int);
-int week_run(int);
-int month_run(int);
-int year_run(int);
-int todo_run(int);
-int notes_run(int);
-int settings_run(int);
-int help_run(int);
+int day_run(int,mmask_t,int,int);
+int week_run(int,mmask_t,int,int);
+int month_run(int,mmask_t,int,int);
+int year_run(int,mmask_t,int,int);
+int todo_run(int,mmask_t,int,int);
+int notes_run(int,mmask_t,int,int);
+int settings_run(int,mmask_t,int,int);
+int help_run(int,mmask_t,int,int);
index 4944861377e1dfd7ddcceaccf543a33f196ffd0d..7d25d5b665b6636bf00d47c84c2b3b366b1ff9f3 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* day init */
 void day_init(void)
 {
@@ -9,7 +11,7 @@ void day_draw(void)
 }
 
 /* day run */
-int day_run(int chr)
+int day_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index 63b2ebcc767b5bbf06ac927e26094fa0f8003c6d..3db46472e66b7b720e1a91c0ea4bf2c0cc5a8d07 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* help init */
 void help_init(void)
 {
@@ -9,7 +11,7 @@ void help_draw(void)
 }
 
 /* help run */
-int help_run(int chr)
+int help_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index cda2b2d2e83e9a8ad425ce1bd2c48af93ac6c985..6f5773cf8f5c201b28561ce5fa822815ea06da8a 100755 (executable)
@@ -4,6 +4,8 @@ for file; do
        name=${file%.c}
        tab="   "
        cat > $file <<-EOF
+               #include <ncurses.h>
+
                /* $name init */
                void ${name}_init(void)
                {
@@ -15,7 +17,7 @@ for file; do
                }
 
                /* $name run */
-               int ${name}_run(int chr)
+               int ${name}_run(int key, mmask_t btn, int row, int col)
                {
                ${tab}return 0;
                }
index 435faf558e49e705a1bac6acd1b209c90f485675..1644ed9aa6463796127d632d1e3f6c0bd2580a86 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* month init */
 void month_init(void)
 {
@@ -9,7 +11,7 @@ void month_draw(void)
 }
 
 /* month run */
-int month_run(int chr)
+int month_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index 7a4307a9954ac018696bfc2dc711df4ed9a98b65..6bb2984047a25ab9b33dcf1d74cbac7b3406d23b 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* notes init */
 void notes_init(void)
 {
@@ -9,7 +11,7 @@ void notes_draw(void)
 }
 
 /* notes run */
-int notes_run(int chr)
+int notes_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index d013fa11cabd602da18cb4d908b643402f97249a..baa84cf6b0799c208a21b6f838bc01ea98fe5e95 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* settings init */
 void settings_init(void)
 {
@@ -9,7 +11,7 @@ void settings_draw(void)
 }
 
 /* settings run */
-int settings_run(int chr)
+int settings_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index 430cbf8d2bad540d7519354b85bd95338ea560d0..acc068224eefcf0c1de26f4415f340c500898c0b 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* todo init */
 void todo_init(void)
 {
@@ -9,7 +11,7 @@ void todo_draw(void)
 }
 
 /* todo run */
-int todo_run(int chr)
+int todo_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index cc6e5d69582a81320785d9e280fcbea48282675e..ddaec477bdaf8b48a041f89ff330dfb90206982e 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* week init */
 void week_init(void)
 {
@@ -9,7 +11,7 @@ void week_draw(void)
 }
 
 /* week run */
-int week_run(int chr)
+int week_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }
index 9819228f29486d605f1ab83135f7211291a873fa..156a4da5938cd471a29c2ba83eb9fd66e793b753 100644 (file)
@@ -1,3 +1,5 @@
+#include <ncurses.h>
+
 /* year init */
 void year_init(void)
 {
@@ -9,7 +11,7 @@ void year_draw(void)
 }
 
 /* year run */
-int year_run(int chr)
+int year_run(int key, mmask_t btn, int row, int col)
 {
        return 0;
 }