1
0
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 <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;
}
cont = ui_readline();
if (FD_ISSET (fileno (rl_instream), &fds)) {
rl_callback_read_char();
ui_write(rl_line_buffer, rl_point);
}
// 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

View File

@ -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;
return inp_readline();
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;
// 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)
{

View File

@ -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)
{

View File

@ -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);

View File

@ -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);

View File

@ -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) {}