about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2015-01-30 23:28:02 +0000
committerJames Booth <boothj5@gmail.com>2015-01-30 23:28:02 +0000
commit83bd207316037a8086f15f37c0e187a2ffba2e14 (patch)
treeb4788d1a1b55f7bb5c6b83f0e76bc7aecdb34c2d
parent2cdbfc7eb7d90ed286c6c5f432990166cdec0b93 (diff)
downloadprofani-tty-83bd207316037a8086f15f37c0e187a2ffba2e14.tar.gz
Moved readline code to inputwin.c
-rw-r--r--src/profanity.c45
-rw-r--r--src/ui/core.c107
-rw-r--r--src/ui/inputwin.c56
-rw-r--r--src/ui/inputwin.h2
-rw-r--r--src/ui/ui.h2
-rw-r--r--tests/ui/stub_ui.c4
6 files changed, 114 insertions, 102 deletions
diff --git a/src/profanity.c b/src/profanity.c
index c105d33f..f3c53003 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -43,8 +43,6 @@
 #include <string.h>
 
 #include <glib.h>
-#include <readline/readline.h>
-#include <readline/history.h>
 
 #include "profanity.h"
 #include "chat_session.h"
@@ -73,20 +71,7 @@ static void _create_directories(void);
 static void _connect_default(const char * const account);
 
 static gboolean idle = FALSE;
-static void cb_linehandler(char *);
-static gboolean cmd_result = TRUE;
-
-static void
-cb_linehandler(char *line)
-{
-    /* Can use ^D (stty eof) or `exit' to exit. */
-    if (*line) {
-        add_history(line);
-    }
-    rl_redisplay();
-    cmd_result = cmd_process_input(line);
-    free(line);
-}
+static gboolean cont = TRUE;
 
 void
 prof_run(const int disable_tls, char *log_level, char *account_name)
@@ -95,35 +80,13 @@ prof_run(const int disable_tls, char *log_level, char *account_name)
     _connect_default(account_name);
     ui_update();
 
-    fd_set fds;
-    int r;
-    rl_callback_handler_install(NULL, cb_linehandler);
-
     log_info("Starting main event loop");
 
-    struct timeval t;
-	t.tv_sec = 0;
-    t.tv_usec = 10000;
-
-    while(cmd_result) {
+    while(cont) {
         _check_autoaway();
 
-        FD_ZERO(&fds);
-        FD_SET(fileno (rl_instream), &fds);
-        r = select(FD_SETSIZE, &fds, NULL, NULL, &t);
-        if (r < 0) {
-            perror ("rltest: select");
-            rl_callback_handler_remove();
-            break;
-        }
-
-        if (FD_ISSET (fileno (rl_instream), &fds)) {
-            rl_callback_read_char();
-            ui_write(rl_line_buffer, rl_point);
-        }
-
+        cont = ui_readline();
 
-//            line = ui_readline();
 #ifdef HAVE_LIBOTR
         otr_poll();
 #endif
@@ -131,8 +94,6 @@ prof_run(const int disable_tls, char *log_level, char *account_name)
         jabber_process_events();
         ui_update();
     }
-
-    rl_callback_handler_remove();
 }
 
 void
diff --git a/src/ui/core.c b/src/ui/core.c
index efebdb5e..3520f81d 100644
--- a/src/ui/core.c
+++ b/src/ui/core.c
@@ -82,7 +82,7 @@ static Display *display;
 
 static GTimer *ui_idle_time;
 
-static void _win_handle_switch(const wint_t ch);
+//static void _win_handle_switch(const wint_t ch);
 static void _win_show_history(int win_index, const char * const contact);
 static void _ui_draw_term_title(void);
 
@@ -174,39 +174,36 @@ ui_close(void)
 {
     notifier_uninit();
     wins_destroy();
+    inp_close();
     endwin();
 }
 
-void
-ui_write(char *line, int offset)
-{
-    inp_write(line, offset);
-}
-
-char*
+gboolean
 ui_readline(void)
 {
-    int key_type;
-    wint_t ch;
-
-    char *line = inp_read(&key_type, &ch);
-    _win_handle_switch(ch);
-
-    ProfWin *current = wins_get_current();
-    win_handle_page(current, ch, key_type);
-
-    if (ch == KEY_RESIZE) {
-        ui_resize();
-    }
-
-    if (ch != ERR && key_type != ERR) {
-        ui_reset_idle_time();
-        ui_input_nonblocking(TRUE);
-    } else {
-        ui_input_nonblocking(FALSE);
-    }
-
-    return line;
+    return inp_readline();
+
+//    int key_type;
+//    wint_t ch;
+//
+//    char *line = inp_read(&key_type, &ch);
+//    _win_handle_switch(ch);
+//
+//    ProfWin *current = wins_get_current();
+//    win_handle_page(current, ch, key_type);
+//
+//    if (ch == KEY_RESIZE) {
+//        ui_resize();
+//    }
+//
+//    if (ch != ERR && key_type != ERR) {
+//        ui_reset_idle_time();
+//        ui_input_nonblocking(TRUE);
+//    } else {
+//        ui_input_nonblocking(FALSE);
+//    }
+//
+//    return line;
 }
 
 void
@@ -2945,32 +2942,32 @@ ui_hide_roster(void)
     }
 }
 
-static void
-_win_handle_switch(const wint_t ch)
-{
-    if (ch == KEY_F(1)) {
-        ui_switch_win(1);
-    } else if (ch == KEY_F(2)) {
-        ui_switch_win(2);
-    } else if (ch == KEY_F(3)) {
-        ui_switch_win(3);
-    } else if (ch == KEY_F(4)) {
-        ui_switch_win(4);
-    } else if (ch == KEY_F(5)) {
-        ui_switch_win(5);
-    } else if (ch == KEY_F(6)) {
-        ui_switch_win(6);
-    } else if (ch == KEY_F(7)) {
-        ui_switch_win(7);
-    } else if (ch == KEY_F(8)) {
-        ui_switch_win(8);
-    } else if (ch == KEY_F(9)) {
-        ui_switch_win(9);
-    } else if (ch == KEY_F(10)) {
-        ui_switch_win(0);
-    }
-}
-
+//static void
+//_win_handle_switch(const wint_t ch)
+//{
+//    if (ch == KEY_F(1)) {
+//        ui_switch_win(1);
+//    } else if (ch == KEY_F(2)) {
+//        ui_switch_win(2);
+//    } else if (ch == KEY_F(3)) {
+//        ui_switch_win(3);
+//    } else if (ch == KEY_F(4)) {
+//        ui_switch_win(4);
+//    } else if (ch == KEY_F(5)) {
+//        ui_switch_win(5);
+//    } else if (ch == KEY_F(6)) {
+//        ui_switch_win(6);
+//    } else if (ch == KEY_F(7)) {
+//        ui_switch_win(7);
+//    } else if (ch == KEY_F(8)) {
+//        ui_switch_win(8);
+//    } else if (ch == KEY_F(9)) {
+//        ui_switch_win(9);
+//    } else if (ch == KEY_F(10)) {
+//        ui_switch_win(0);
+//    }
+//}
+//
 static void
 _win_show_history(int win_index, const char * const contact)
 {
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index 64c65a96..4b3ec296 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -39,6 +39,9 @@
 #include <string.h>
 #include <wchar.h>
 
+#include <readline/readline.h>
+#include <readline/history.h>
+
 #ifdef HAVE_NCURSESW_NCURSES_H
 #include <ncursesw/ncurses.h>
 #elif HAVE_NCURSES_H
@@ -77,6 +80,11 @@
 static WINDOW *inp_win;
 static History history;
 
+static struct timeval p_rl_timeout;
+static fd_set fds;
+static int r;
+static gboolean cmd_result = TRUE;
+
 // input line
 static char line[INP_WIN_MAX];
 // current position in the utf8 string
@@ -95,6 +103,17 @@ static gboolean _is_ctrl_right(int key_type, const wint_t ch);
 
 static void _inp_win_update_virtual(void);
 
+static void
+cb_linehandler(char *line)
+{
+    if (*line) {
+        add_history(line);
+    }
+    rl_redisplay();
+    cmd_result = cmd_process_input(line);
+    free(line);
+}
+
 void
 create_input_window(void)
 {
@@ -103,6 +122,10 @@ create_input_window(void)
 #else
     ESCDELAY = 25;
 #endif
+	p_rl_timeout.tv_sec = 0;
+    p_rl_timeout.tv_usec = 500000;
+    rl_callback_handler_install(NULL, cb_linehandler);
+
     inp_win = newpad(1, INP_WIN_MAX);
     wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
     keypad(inp_win, TRUE);
@@ -169,9 +192,9 @@ inp_write(char *line, int offset)
 }
 
 void
-inp_non_block(gint timeout)
+inp_non_block(gint block_timeout)
 {
-    wtimeout(inp_win, timeout);
+    wtimeout(inp_win, block_timeout);
 }
 
 void
@@ -180,6 +203,35 @@ inp_block(void)
     wtimeout(inp_win, -1);
 }
 
+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.");
+        rl_callback_handler_remove();
+        return false;
+    }
+
+    if (FD_ISSET (fileno (rl_instream), &fds)) {
+        rl_callback_read_char();
+        inp_write(rl_line_buffer, rl_point);
+    }
+
+    p_rl_timeout.tv_sec = 0;
+    p_rl_timeout.tv_usec = 500000;
+
+    return cmd_result;
+}
+
+void
+inp_close(void)
+{
+    rl_callback_handler_remove();
+}
+
 char *
 inp_read(int *key_type, wint_t *ch)
 {
diff --git a/src/ui/inputwin.h b/src/ui/inputwin.h
index d27bfef7..7193e049 100644
--- a/src/ui/inputwin.h
+++ b/src/ui/inputwin.h
@@ -40,6 +40,8 @@
 #define INP_WIN_MAX 1000
 
 void create_input_window(void);
+gboolean inp_readline(void);
+void inp_close(void);
 char* inp_read(int *key_type, wint_t *ch);
 void inp_win_clear(void);
 void inp_win_resize(void);
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 23315af9..2cfb6e6e 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -227,7 +227,7 @@ void ui_update_presence(const resource_presence_t resource_presence,
 void ui_about(void);
 void ui_statusbar_new(const int win);
 
-char * ui_readline(void);
+gboolean ui_readline(void);
 void ui_input_clear(void);
 void ui_input_nonblocking(gboolean);
 void ui_write(char *line, int offset);
diff --git a/tests/ui/stub_ui.c b/tests/ui/stub_ui.c
index 6b9050c3..6127e4f0 100644
--- a/tests/ui/stub_ui.c
+++ b/tests/ui/stub_ui.c
@@ -323,9 +323,9 @@ void ui_update_presence(const resource_presence_t resource_presence,
 void ui_about(void) {}
 void ui_statusbar_new(const int win) {}
 
-char * ui_readline(void)
+gboolean ui_readline(void)
 {
-    return NULL;
+    return TRUE;
 }
 
 void ui_inp_history_append(char *inp) {}