1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Moved readline code to inputwin.c

This commit is contained in:
James Booth 2015-01-30 23:28:02 +00:00
parent 2cdbfc7eb7
commit 83bd207316
6 changed files with 113 additions and 101 deletions

View File

@ -43,8 +43,6 @@
#include <string.h> #include <string.h>
#include <glib.h> #include <glib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "profanity.h" #include "profanity.h"
#include "chat_session.h" #include "chat_session.h"
@ -73,20 +71,7 @@ static void _create_directories(void);
static void _connect_default(const char * const account); static void _connect_default(const char * const account);
static gboolean idle = FALSE; static gboolean idle = FALSE;
static void cb_linehandler(char *); static gboolean cont = TRUE;
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);
}
void void
prof_run(const int disable_tls, char *log_level, char *account_name) 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); _connect_default(account_name);
ui_update(); ui_update();
fd_set fds;
int r;
rl_callback_handler_install(NULL, cb_linehandler);
log_info("Starting main event loop"); log_info("Starting main event loop");
struct timeval t; while(cont) {
t.tv_sec = 0;
t.tv_usec = 10000;
while(cmd_result) {
_check_autoaway(); _check_autoaway();
FD_ZERO(&fds); cont = ui_readline();
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);
}
// line = ui_readline();
#ifdef HAVE_LIBOTR #ifdef HAVE_LIBOTR
otr_poll(); otr_poll();
#endif #endif
@ -131,8 +94,6 @@ prof_run(const int disable_tls, char *log_level, char *account_name)
jabber_process_events(); jabber_process_events();
ui_update(); ui_update();
} }
rl_callback_handler_remove();
} }
void void

View File

@ -82,7 +82,7 @@ static Display *display;
static GTimer *ui_idle_time; 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 _win_show_history(int win_index, const char * const contact);
static void _ui_draw_term_title(void); static void _ui_draw_term_title(void);
@ -174,39 +174,36 @@ ui_close(void)
{ {
notifier_uninit(); notifier_uninit();
wins_destroy(); wins_destroy();
inp_close();
endwin(); endwin();
} }
void gboolean
ui_write(char *line, int offset)
{
inp_write(line, offset);
}
char*
ui_readline(void) ui_readline(void)
{ {
int key_type; return inp_readline();
wint_t ch;
char *line = inp_read(&key_type, &ch); // int key_type;
_win_handle_switch(ch); // wint_t ch;
//
ProfWin *current = wins_get_current(); // char *line = inp_read(&key_type, &ch);
win_handle_page(current, ch, key_type); // _win_handle_switch(ch);
//
if (ch == KEY_RESIZE) { // ProfWin *current = wins_get_current();
ui_resize(); // win_handle_page(current, ch, key_type);
} //
// if (ch == KEY_RESIZE) {
if (ch != ERR && key_type != ERR) { // ui_resize();
ui_reset_idle_time(); // }
ui_input_nonblocking(TRUE); //
} else { // if (ch != ERR && key_type != ERR) {
ui_input_nonblocking(FALSE); // ui_reset_idle_time();
} // ui_input_nonblocking(TRUE);
// } else {
return line; // ui_input_nonblocking(FALSE);
// }
//
// return line;
} }
void void
@ -2945,32 +2942,32 @@ ui_hide_roster(void)
} }
} }
static void //static void
_win_handle_switch(const wint_t ch) //_win_handle_switch(const wint_t ch)
{ //{
if (ch == KEY_F(1)) { // if (ch == KEY_F(1)) {
ui_switch_win(1); // ui_switch_win(1);
} else if (ch == KEY_F(2)) { // } else if (ch == KEY_F(2)) {
ui_switch_win(2); // ui_switch_win(2);
} else if (ch == KEY_F(3)) { // } else if (ch == KEY_F(3)) {
ui_switch_win(3); // ui_switch_win(3);
} else if (ch == KEY_F(4)) { // } else if (ch == KEY_F(4)) {
ui_switch_win(4); // ui_switch_win(4);
} else if (ch == KEY_F(5)) { // } else if (ch == KEY_F(5)) {
ui_switch_win(5); // ui_switch_win(5);
} else if (ch == KEY_F(6)) { // } else if (ch == KEY_F(6)) {
ui_switch_win(6); // ui_switch_win(6);
} else if (ch == KEY_F(7)) { // } else if (ch == KEY_F(7)) {
ui_switch_win(7); // ui_switch_win(7);
} else if (ch == KEY_F(8)) { // } else if (ch == KEY_F(8)) {
ui_switch_win(8); // ui_switch_win(8);
} else if (ch == KEY_F(9)) { // } else if (ch == KEY_F(9)) {
ui_switch_win(9); // ui_switch_win(9);
} else if (ch == KEY_F(10)) { // } else if (ch == KEY_F(10)) {
ui_switch_win(0); // ui_switch_win(0);
} // }
} //}
//
static void static void
_win_show_history(int win_index, const char * const contact) _win_show_history(int win_index, const char * const contact)
{ {

View File

@ -39,6 +39,9 @@
#include <string.h> #include <string.h>
#include <wchar.h> #include <wchar.h>
#include <readline/readline.h>
#include <readline/history.h>
#ifdef HAVE_NCURSESW_NCURSES_H #ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h> #include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H #elif HAVE_NCURSES_H
@ -77,6 +80,11 @@
static WINDOW *inp_win; static WINDOW *inp_win;
static History history; static History history;
static struct timeval p_rl_timeout;
static fd_set fds;
static int r;
static gboolean cmd_result = TRUE;
// input line // input line
static char line[INP_WIN_MAX]; static char line[INP_WIN_MAX];
// current position in the utf8 string // 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 _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 void
create_input_window(void) create_input_window(void)
{ {
@ -103,6 +122,10 @@ create_input_window(void)
#else #else
ESCDELAY = 25; ESCDELAY = 25;
#endif #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); inp_win = newpad(1, INP_WIN_MAX);
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));; wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
keypad(inp_win, TRUE); keypad(inp_win, TRUE);
@ -169,9 +192,9 @@ inp_write(char *line, int offset)
} }
void void
inp_non_block(gint timeout) inp_non_block(gint block_timeout)
{ {
wtimeout(inp_win, timeout); wtimeout(inp_win, block_timeout);
} }
void void
@ -180,6 +203,35 @@ inp_block(void)
wtimeout(inp_win, -1); 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 * char *
inp_read(int *key_type, wint_t *ch) inp_read(int *key_type, wint_t *ch)
{ {

View File

@ -40,6 +40,8 @@
#define INP_WIN_MAX 1000 #define INP_WIN_MAX 1000
void create_input_window(void); void create_input_window(void);
gboolean inp_readline(void);
void inp_close(void);
char* inp_read(int *key_type, wint_t *ch); char* inp_read(int *key_type, wint_t *ch);
void inp_win_clear(void); void inp_win_clear(void);
void inp_win_resize(void); void inp_win_resize(void);

View File

@ -227,7 +227,7 @@ void ui_update_presence(const resource_presence_t resource_presence,
void ui_about(void); void ui_about(void);
void ui_statusbar_new(const int win); void ui_statusbar_new(const int win);
char * ui_readline(void); gboolean ui_readline(void);
void ui_input_clear(void); void ui_input_clear(void);
void ui_input_nonblocking(gboolean); void ui_input_nonblocking(gboolean);
void ui_write(char *line, int offset); void ui_write(char *line, int offset);

View File

@ -323,9 +323,9 @@ void ui_update_presence(const resource_presence_t resource_presence,
void ui_about(void) {} void ui_about(void) {}
void ui_statusbar_new(const int win) {} 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) {} void ui_inp_history_append(char *inp) {}