about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-02-03 23:07:15 +0000
committerJames Booth <boothj5@gmail.com>2015-02-03 23:07:15 +0000
commit8b58eb68d302b6b7c2c6e6319daf8cb064c38d54 (patch)
tree207f278da9b3728759035abb7c8d942ba22a9b3f /src
parentaf345b6700f37ae190985c4e27f5ba01931da63e (diff)
downloadprofani-tty-8b58eb68d302b6b7c2c6e6319daf8cb064c38d54.tar.gz
Tidied inputwin.c
Diffstat (limited to 'src')
-rw-r--r--src/ui/inputwin.c602
1 files changed, 315 insertions, 287 deletions
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index d0d0dfe6..f9e123c5 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -65,6 +65,7 @@
 #include "xmpp/xmpp.h"
 
 static WINDOW *inp_win;
+static int pad_start = 0;
 
 static struct timeval p_rl_timeout;
 static gint inp_timeout = 0;
@@ -74,12 +75,201 @@ static fd_set fds;
 static int r;
 static gboolean cmd_result = TRUE;
 
-static int pad_start = 0;
-
 static void _inp_win_update_virtual(void);
+static int _inp_printable(const wint_t ch);
+static void _inp_win_handle_scroll(void);
+static void _inp_resize_signal_handler(int signal);
+static int _inp_offset_to_col(char *str, int offset);
+static void _inp_write(char *line, int offset);
+
+static int _inp_rl_getc(FILE *stream);
+static void _inp_rl_linehandler(char *line);
+static int _inp_rl_tab_handler(int count, int key);
+static int _inp_rl_win1_handler(int count, int key);
+static int _inp_rl_win2_handler(int count, int key);
+static int _inp_rl_win3_handler(int count, int key);
+static int _inp_rl_win4_handler(int count, int key);
+static int _inp_rl_win5_handler(int count, int key);
+static int _inp_rl_win6_handler(int count, int key);
+static int _inp_rl_win7_handler(int count, int key);
+static int _inp_rl_win8_handler(int count, int key);
+static int _inp_rl_win9_handler(int count, int key);
+static int _inp_rl_win0_handler(int count, int key);
+static int _inp_rl_altleft_handler(int count, int key);
+static int _inp_rl_altright_handler(int count, int key);
+static int _inp_rl_pageup_handler(int count, int key);
+static int _inp_rl_pagedown_handler(int count, int key);
+static int _inp_rl_altpageup_handler(int count, int key);
+static int _inp_rl_altpagedown_handler(int count, int key);
+static int _inp_rl_startup_hook(void);
+
+void
+create_input_window(void)
+{
+#ifdef NCURSES_REENTRANT
+    set_escdelay(25);
+#else
+    ESCDELAY = 25;
+#endif
+	p_rl_timeout.tv_sec = 0;
+    p_rl_timeout.tv_usec = inp_timeout * 1000;
+
+    rl_readline_name = "profanity";
+    rl_getc_function = _inp_rl_getc;
+    rl_startup_hook = _inp_rl_startup_hook;
+    rl_callback_handler_install(NULL, _inp_rl_linehandler);
+
+    signal(SIGWINCH, _inp_resize_signal_handler);
+
+    inp_win = newpad(1, INP_WIN_MAX);
+    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
+    keypad(inp_win, TRUE);
+    wmove(inp_win, 0, 0);
+
+    _inp_win_update_virtual();
+}
+
+gboolean
+inp_readline(void)
+{
+    FD_ZERO(&fds);
+    FD_SET(fileno(rl_instream), &fds);
+    r = select(FD_SETSIZE, &fds, NULL, NULL, &p_rl_timeout);
+    if (r < 0) {
+        log_error("Readline failed.");
+        return TRUE;
+    }
+
+    if (FD_ISSET(fileno(rl_instream), &fds)) {
+        rl_callback_read_char();
+
+        if (rl_line_buffer && rl_line_buffer[0] != '/' && rl_line_buffer[0] != '\0' && rl_line_buffer[0] != '\n') {
+            prof_handle_activity();
+        }
+
+        ui_reset_idle_time();
+        _inp_write(rl_line_buffer, rl_point);
+        inp_nonblocking(TRUE);
+    } else {
+        inp_nonblocking(FALSE);
+        prof_handle_idle();
+    }
+
+    p_rl_timeout.tv_sec = 0;
+    p_rl_timeout.tv_usec = inp_timeout * 1000;
+
+    return cmd_result;
+}
+
+void
+inp_win_resize(void)
+{
+    int col = getcurx(inp_win);
+    int wcols = getmaxx(stdscr);
+
+    // if lost cursor off screen, move contents to show it
+    if (col >= pad_start + wcols) {
+        pad_start = col - (wcols / 2);
+        if (pad_start < 0) {
+            pad_start = 0;
+        }
+    }
+
+    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
+    _inp_win_update_virtual();
+}
+
+void
+inp_nonblocking(gboolean reset)
+{
+    if (! prefs_get_boolean(PREF_INPBLOCK_DYNAMIC)) {
+        inp_timeout = prefs_get_inpblock();
+        return;
+    }
+
+    if (reset) {
+        inp_timeout = 0;
+        no_input_count = 0;
+    }
+
+    if (inp_timeout < prefs_get_inpblock()) {
+        no_input_count++;
+
+        if (no_input_count % 10 == 0) {
+            inp_timeout += no_input_count;
+
+            if (inp_timeout > prefs_get_inpblock()) {
+                inp_timeout = prefs_get_inpblock();
+            }
+        }
+    }
+}
+
+void
+inp_block(void)
+{
+    wtimeout(inp_win, -1);
+}
+
+
+void
+inp_close(void)
+{
+    rl_callback_handler_remove();
+}
+
+void
+inp_get_password(char *passwd)
+{
+    werase(inp_win);
+    wmove(inp_win, 0, 0);
+    pad_start = 0;
+    _inp_win_update_virtual();
+    doupdate();
+    noecho();
+    mvwgetnstr(inp_win, 0, 1, passwd, MAX_PASSWORD_SIZE);
+    wmove(inp_win, 0, 0);
+    echo();
+    status_bar_clear();
+}
+
+void
+inp_put_back(void)
+{
+    _inp_win_update_virtual();
+}
+
+void
+inp_win_clear(void)
+{
+    werase(inp_win);
+    wmove(inp_win, 0, 0);
+    pad_start = 0;
+    _inp_win_update_virtual();
+}
+
+static void
+_inp_win_update_virtual(void)
+{
+    int wrows, wcols;
+    getmaxyx(stdscr, wrows, wcols);
+    pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-2);
+}
+
+static void
+_inp_write(char *line, int offset)
+{
+    int col = _inp_offset_to_col(line, offset);
+    werase(inp_win);
+    waddstr(inp_win, line);
+    wmove(inp_win, 0, col);
+    _inp_win_handle_scroll();
+
+    _inp_win_update_virtual();
+}
 
 static int
-_printable(const wint_t ch)
+_inp_printable(const wint_t ch)
 {
     char bytes[MB_CUR_MAX+1];
     size_t utf_len = wcrtomb(bytes, ch, NULL);
@@ -89,8 +279,55 @@ _printable(const wint_t ch)
     return g_unichar_isprint(unichar) && (ch != KEY_MOUSE);
 }
 
+static int
+_inp_offset_to_col(char *str, int offset)
+{
+    int i = 0;
+    int col = 0;
+
+    while (i < offset && str[i] != '\0') {
+        gunichar uni = g_utf8_get_char(&str[i]);
+        size_t ch_len = mbrlen(&str[i], 4, NULL);
+        i += ch_len;
+        col++;
+        if (g_unichar_iswide(uni)) {
+            col++;
+        }
+    }
+
+    return col;
+}
+
+static void
+_inp_resize_signal_handler(int signal)
+{
+    ui_resize();
+}
+
+static void
+_inp_win_handle_scroll(void)
+{
+    int col = getcurx(inp_win);
+    int wcols = getmaxx(stdscr);
+
+    // if lost cursor off screen, move contents to show it
+    if (col >= pad_start + (wcols -2)) {
+        pad_start = col - (wcols / 2);
+        if (pad_start < 0) {
+            pad_start = 0;
+        }
+    } else if (col <= pad_start) {
+        pad_start = pad_start - (wcols / 2);
+        if (pad_start < 0) {
+            pad_start = 0;
+        }
+    }
+}
+
+// Readline callbacks
+
 static void
-cb_linehandler(char *line)
+_inp_rl_linehandler(char *line)
 {
     if (line && *line) {
         add_history(line);
@@ -99,24 +336,18 @@ cb_linehandler(char *line)
     free(line);
 }
 
-int
-prof_rl_getc(FILE *filein)
+static int
+_inp_rl_getc(FILE *stream)
 {
-    int ch = rl_getc(filein);
-    if (_printable(ch)) {
+    int ch = rl_getc(stream);
+    if (_inp_printable(ch)) {
         cmd_reset_autocomplete();
     }
     return ch;
 }
 
-void
-resize_signal_handler(int signal)
-{
-    ui_resize();
-}
-
-int
-tab_handler(int count, int key)
+static int
+_inp_rl_tab_handler(int count, int key)
 {
     if (rl_point != rl_end || !rl_line_buffer) {
         return 0;
@@ -139,361 +370,158 @@ tab_handler(int count, int key)
     return 0;
 }
 
-int
-win1_handler(int count, int key)
+static int
+_inp_rl_win1_handler(int count, int key)
 {
     ui_switch_win(1);
     return 0;
 }
 
-int
-win2_handler(int count, int key)
+static int
+_inp_rl_win2_handler(int count, int key)
 {
     ui_switch_win(2);
     return 0;
 }
 
-int
-win3_handler(int count, int key)
+static int
+_inp_rl_win3_handler(int count, int key)
 {
     ui_switch_win(3);
     return 0;
 }
 
-int
-win4_handler(int count, int key)
+static int
+_inp_rl_win4_handler(int count, int key)
 {
     ui_switch_win(4);
     return 0;
 }
 
-int
-win5_handler(int count, int key)
+static int
+_inp_rl_win5_handler(int count, int key)
 {
     ui_switch_win(5);
     return 0;
 }
 
-int
-win6_handler(int count, int key)
+static int
+_inp_rl_win6_handler(int count, int key)
 {
     ui_switch_win(6);
     return 0;
 }
 
-int
-win7_handler(int count, int key)
+static int
+_inp_rl_win7_handler(int count, int key)
 {
     ui_switch_win(7);
     return 0;
 }
 
-int
-win8_handler(int count, int key)
+static int
+_inp_rl_win8_handler(int count, int key)
 {
     ui_switch_win(8);
     return 0;
 }
 
-int
-win9_handler(int count, int key)
+static int
+_inp_rl_win9_handler(int count, int key)
 {
     ui_switch_win(9);
     return 0;
 }
 
-int
-win0_handler(int count, int key)
+static int
+_inp_rl_win0_handler(int count, int key)
 {
     ui_switch_win(0);
     return 0;
 }
 
-int
-altleft_handler(int count, int key)
+static int
+_inp_rl_altleft_handler(int count, int key)
 {
     ui_previous_win();
     return 0;
 }
 
-int
-altright_handler(int count, int key)
+static int
+_inp_rl_altright_handler(int count, int key)
 {
     ui_next_win();
     return 0;
 }
 
-int
-pageup_handler(int count, int key)
+static int
+_inp_rl_pageup_handler(int count, int key)
 {
     ui_page_up();
     return 0;
 }
 
-int
-pagedown_handler(int count, int key)
+static int
+_inp_rl_pagedown_handler(int count, int key)
 {
     ui_page_down();
     return 0;
 }
 
-int
-altpageup_handler(int count, int key)
+static int
+_inp_rl_altpageup_handler(int count, int key)
 {
     ui_subwin_page_up();
     return 0;
 }
 
-int
-altpagedown_handler(int count, int key)
+static int
+_inp_rl_altpagedown_handler(int count, int key)
 {
     ui_subwin_page_down();
     return 0;
 }
 
-int
-startup_hook(void)
-{
-    rl_bind_keyseq("\\e1", win1_handler);
-    rl_bind_keyseq("\\e2", win2_handler);
-    rl_bind_keyseq("\\e3", win3_handler);
-    rl_bind_keyseq("\\e4", win4_handler);
-    rl_bind_keyseq("\\e5", win5_handler);
-    rl_bind_keyseq("\\e6", win6_handler);
-    rl_bind_keyseq("\\e7", win7_handler);
-    rl_bind_keyseq("\\e8", win8_handler);
-    rl_bind_keyseq("\\e9", win9_handler);
-    rl_bind_keyseq("\\e0", win0_handler);
-
-    rl_bind_keyseq("\\eOP", win1_handler);
-    rl_bind_keyseq("\\eOQ", win2_handler);
-    rl_bind_keyseq("\\eOR", win3_handler);
-    rl_bind_keyseq("\\eOS", win4_handler);
-    rl_bind_keyseq("\\e[15~", win5_handler);
-    rl_bind_keyseq("\\e[17~", win6_handler);
-    rl_bind_keyseq("\\e[18~", win7_handler);
-    rl_bind_keyseq("\\e[19~", win8_handler);
-    rl_bind_keyseq("\\e[20~", win9_handler);
-    rl_bind_keyseq("\\e[21~", win0_handler);
+static int
+_inp_rl_startup_hook(void)
+{
+    rl_bind_keyseq("\\e1", _inp_rl_win1_handler);
+    rl_bind_keyseq("\\e2", _inp_rl_win2_handler);
+    rl_bind_keyseq("\\e3", _inp_rl_win3_handler);
+    rl_bind_keyseq("\\e4", _inp_rl_win4_handler);
+    rl_bind_keyseq("\\e5", _inp_rl_win5_handler);
+    rl_bind_keyseq("\\e6", _inp_rl_win6_handler);
+    rl_bind_keyseq("\\e7", _inp_rl_win7_handler);
+    rl_bind_keyseq("\\e8", _inp_rl_win8_handler);
+    rl_bind_keyseq("\\e9", _inp_rl_win9_handler);
+    rl_bind_keyseq("\\e0", _inp_rl_win0_handler);
+
+    rl_bind_keyseq("\\eOP", _inp_rl_win1_handler);
+    rl_bind_keyseq("\\eOQ", _inp_rl_win2_handler);
+    rl_bind_keyseq("\\eOR", _inp_rl_win3_handler);
+    rl_bind_keyseq("\\eOS", _inp_rl_win4_handler);
+    rl_bind_keyseq("\\e[15~", _inp_rl_win5_handler);
+    rl_bind_keyseq("\\e[17~", _inp_rl_win6_handler);
+    rl_bind_keyseq("\\e[18~", _inp_rl_win7_handler);
+    rl_bind_keyseq("\\e[19~", _inp_rl_win8_handler);
+    rl_bind_keyseq("\\e[20~", _inp_rl_win9_handler);
+    rl_bind_keyseq("\\e[21~", _inp_rl_win0_handler);
 
 #ifdef PLATFORM_OSX
-    rl_bind_keyseq("\\e[1;9D", altleft_handler);
-    rl_bind_keyseq("\\e[1;9C", altright_handler);
-    rl_bind_keyseq("\\e\\e[5~", altpageup_handler);
-    rl_bind_keyseq("\\e\\e[6~", altpagedown_handler);
+    rl_bind_keyseq("\\e[1;9D", _inp_rl_altleft_handler);
+    rl_bind_keyseq("\\e[1;9C", _inp_rl_altright_handler);
+    rl_bind_keyseq("\\e\\e[5~", _inp_rl_altpageup_handler);
+    rl_bind_keyseq("\\e\\e[6~", _inp_rl_altpagedown_handler);
 #else
-    rl_bind_keyseq("\\e[1;3D", altleft_handler);
-    rl_bind_keyseq("\\e[1;3C", altright_handler);
-    rl_bind_keyseq("\\e[5;3~", altpageup_handler);
-    rl_bind_keyseq("\\e[6;3~", altpagedown_handler);
+    rl_bind_keyseq("\\e[1;3D", _inp_rl_altleft_handler);
+    rl_bind_keyseq("\\e[1;3C", _inp_rl_altright_handler);
+    rl_bind_keyseq("\\e[5;3~", _inp_rl_altpageup_handler);
+    rl_bind_keyseq("\\e[6;3~", _inp_rl_altpagedown_handler);
 #endif
-    rl_bind_keyseq("\\e[5~", pageup_handler);
-    rl_bind_keyseq("\\e[6~", pagedown_handler);
+    rl_bind_keyseq("\\e[5~", _inp_rl_pageup_handler);
+    rl_bind_keyseq("\\e[6~", _inp_rl_pagedown_handler);
 
-    rl_bind_key('\t', tab_handler);
+    rl_bind_key('\t', _inp_rl_tab_handler);
 
     return 0;
-}
-
-void
-create_input_window(void)
-{
-#ifdef NCURSES_REENTRANT
-    set_escdelay(25);
-#else
-    ESCDELAY = 25;
-#endif
-	p_rl_timeout.tv_sec = 0;
-    p_rl_timeout.tv_usec = inp_timeout * 1000;
-
-    rl_readline_name = "profanity";
-    rl_getc_function = prof_rl_getc;
-    rl_startup_hook = startup_hook;
-    rl_callback_handler_install(NULL, cb_linehandler);
-
-    signal(SIGWINCH, resize_signal_handler);
-
-    inp_win = newpad(1, INP_WIN_MAX);
-    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
-    keypad(inp_win, TRUE);
-    wmove(inp_win, 0, 0);
-
-    _inp_win_update_virtual();
-}
-
-void
-inp_win_resize(void)
-{
-    int col = getcurx(inp_win);
-    int wcols = getmaxx(stdscr);
-
-    // if lost cursor off screen, move contents to show it
-    if (col >= pad_start + wcols) {
-        pad_start = col - (wcols / 2);
-        if (pad_start < 0) {
-            pad_start = 0;
-        }
-    }
-
-    wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
-    _inp_win_update_virtual();
-}
-
-void
-inp_nonblocking(gboolean reset)
-{
-    if (! prefs_get_boolean(PREF_INPBLOCK_DYNAMIC)) {
-        inp_timeout = prefs_get_inpblock();
-        return;
-    }
-
-    if (reset) {
-        inp_timeout = 0;
-        no_input_count = 0;
-    }
-
-    if (inp_timeout < prefs_get_inpblock()) {
-        no_input_count++;
-
-        if (no_input_count % 10 == 0) {
-            inp_timeout += no_input_count;
-
-            if (inp_timeout > prefs_get_inpblock()) {
-                inp_timeout = prefs_get_inpblock();
-            }
-        }
-    }
-}
-
-void
-inp_block(void)
-{
-    wtimeout(inp_win, -1);
-}
-
-void
-inp_win_handle_scroll(void)
-{
-    int col = getcurx(inp_win);
-    int wcols = getmaxx(stdscr);
-
-    // if lost cursor off screen, move contents to show it
-    if (col >= pad_start + (wcols -2)) {
-        pad_start = col - (wcols / 2);
-        if (pad_start < 0) {
-            pad_start = 0;
-        }
-    } else if (col <= pad_start) {
-        pad_start = pad_start - (wcols / 2);
-        if (pad_start < 0) {
-            pad_start = 0;
-        }
-    }
-}
-
-int
-offset_to_col(char *str, int offset)
-{
-    int i = 0;
-    int col = 0;
-
-    while (i < offset && str[i] != '\0') {
-        gunichar uni = g_utf8_get_char(&str[i]);
-        size_t ch_len = mbrlen(&str[i], 4, NULL);
-        i += ch_len;
-        col++;
-        if (g_unichar_iswide(uni)) {
-            col++;
-        }
-    }
-
-    return col;
-}
-
-void
-inp_write(char *line, int offset)
-{
-    int col = offset_to_col(line, offset);
-    werase(inp_win);
-    waddstr(inp_win, line);
-    wmove(inp_win, 0, col);
-    inp_win_handle_scroll();
-
-    _inp_win_update_virtual();
-}
-
-gboolean
-inp_readline(void)
-{
-    FD_ZERO(&fds);
-    FD_SET(fileno(rl_instream), &fds);
-    r = select(FD_SETSIZE, &fds, NULL, NULL, &p_rl_timeout);
-    if (r < 0) {
-        log_error("Readline failed.");
-        return TRUE;
-    }
-
-    if (FD_ISSET(fileno(rl_instream), &fds)) {
-        rl_callback_read_char();
-
-        if (rl_line_buffer && rl_line_buffer[0] != '/' && rl_line_buffer[0] != '\0' && rl_line_buffer[0] != '\n') {
-            prof_handle_activity();
-        }
-
-        ui_reset_idle_time();
-        inp_write(rl_line_buffer, rl_point);
-        inp_nonblocking(TRUE);
-    } else {
-        inp_nonblocking(FALSE);
-        prof_handle_idle();
-    }
-
-    p_rl_timeout.tv_sec = 0;
-    p_rl_timeout.tv_usec = inp_timeout * 1000;
-
-    return cmd_result;
-}
-
-void
-inp_close(void)
-{
-    rl_callback_handler_remove();
-}
-
-void
-inp_get_password(char *passwd)
-{
-    werase(inp_win);
-    wmove(inp_win, 0, 0);
-    pad_start = 0;
-    _inp_win_update_virtual();
-    doupdate();
-    noecho();
-    mvwgetnstr(inp_win, 0, 1, passwd, MAX_PASSWORD_SIZE);
-    wmove(inp_win, 0, 0);
-    echo();
-    status_bar_clear();
-}
-
-void
-inp_put_back(void)
-{
-    _inp_win_update_virtual();
-}
-
-void
-inp_win_clear(void)
-{
-    werase(inp_win);
-    wmove(inp_win, 0, 0);
-    pad_start = 0;
-    _inp_win_update_virtual();
-}
-
-static void
-_inp_win_update_virtual(void)
-{
-    int wrows, wcols;
-    getmaxyx(stdscr, wrows, wcols);
-    pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-2);
-}
+}
\ No newline at end of file