1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Tidied inputwin.c

This commit is contained in:
James Booth 2015-02-03 23:07:15 +00:00
parent af345b6700
commit 8b58eb68d3

View File

@ -65,6 +65,7 @@
#include "xmpp/xmpp.h" #include "xmpp/xmpp.h"
static WINDOW *inp_win; static WINDOW *inp_win;
static int pad_start = 0;
static struct timeval p_rl_timeout; static struct timeval p_rl_timeout;
static gint inp_timeout = 0; static gint inp_timeout = 0;
@ -74,226 +75,33 @@ static fd_set fds;
static int r; static int r;
static gboolean cmd_result = TRUE; static gboolean cmd_result = TRUE;
static int pad_start = 0;
static void _inp_win_update_virtual(void); 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 static int _inp_rl_getc(FILE *stream);
_printable(const wint_t ch) static void _inp_rl_linehandler(char *line);
{ static int _inp_rl_tab_handler(int count, int key);
char bytes[MB_CUR_MAX+1]; static int _inp_rl_win1_handler(int count, int key);
size_t utf_len = wcrtomb(bytes, ch, NULL); static int _inp_rl_win2_handler(int count, int key);
bytes[utf_len] = '\0'; static int _inp_rl_win3_handler(int count, int key);
gunichar unichar = g_utf8_get_char(bytes); static int _inp_rl_win4_handler(int count, int key);
static int _inp_rl_win5_handler(int count, int key);
return g_unichar_isprint(unichar) && (ch != KEY_MOUSE); 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 void static int _inp_rl_win9_handler(int count, int key);
cb_linehandler(char *line) static int _inp_rl_win0_handler(int count, int key);
{ static int _inp_rl_altleft_handler(int count, int key);
if (line && *line) { static int _inp_rl_altright_handler(int count, int key);
add_history(line); static int _inp_rl_pageup_handler(int count, int key);
} static int _inp_rl_pagedown_handler(int count, int key);
cmd_result = cmd_process_input(line); static int _inp_rl_altpageup_handler(int count, int key);
free(line); static int _inp_rl_altpagedown_handler(int count, int key);
} static int _inp_rl_startup_hook(void);
int
prof_rl_getc(FILE *filein)
{
int ch = rl_getc(filein);
if (_printable(ch)) {
cmd_reset_autocomplete();
}
return ch;
}
void
resize_signal_handler(int signal)
{
ui_resize();
}
int
tab_handler(int count, int key)
{
if (rl_point != rl_end || !rl_line_buffer) {
return 0;
}
if ((strncmp(rl_line_buffer, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) {
char *result = muc_autocomplete(rl_line_buffer);
if (result) {
rl_replace_line(result, 0);
rl_point = rl_end;
}
} else if (strncmp(rl_line_buffer, "/", 1) == 0) {
char *result = cmd_autocomplete(rl_line_buffer);
if (result) {
rl_replace_line(result, 0);
rl_point = rl_end;
}
}
return 0;
}
int
win1_handler(int count, int key)
{
ui_switch_win(1);
return 0;
}
int
win2_handler(int count, int key)
{
ui_switch_win(2);
return 0;
}
int
win3_handler(int count, int key)
{
ui_switch_win(3);
return 0;
}
int
win4_handler(int count, int key)
{
ui_switch_win(4);
return 0;
}
int
win5_handler(int count, int key)
{
ui_switch_win(5);
return 0;
}
int
win6_handler(int count, int key)
{
ui_switch_win(6);
return 0;
}
int
win7_handler(int count, int key)
{
ui_switch_win(7);
return 0;
}
int
win8_handler(int count, int key)
{
ui_switch_win(8);
return 0;
}
int
win9_handler(int count, int key)
{
ui_switch_win(9);
return 0;
}
int
win0_handler(int count, int key)
{
ui_switch_win(0);
return 0;
}
int
altleft_handler(int count, int key)
{
ui_previous_win();
return 0;
}
int
altright_handler(int count, int key)
{
ui_next_win();
return 0;
}
int
pageup_handler(int count, int key)
{
ui_page_up();
return 0;
}
int
pagedown_handler(int count, int key)
{
ui_page_down();
return 0;
}
int
altpageup_handler(int count, int key)
{
ui_subwin_page_up();
return 0;
}
int
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);
#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);
#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);
#endif
rl_bind_keyseq("\\e[5~", pageup_handler);
rl_bind_keyseq("\\e[6~", pagedown_handler);
rl_bind_key('\t', tab_handler);
return 0;
}
void void
create_input_window(void) create_input_window(void)
@ -307,11 +115,11 @@ create_input_window(void)
p_rl_timeout.tv_usec = inp_timeout * 1000; p_rl_timeout.tv_usec = inp_timeout * 1000;
rl_readline_name = "profanity"; rl_readline_name = "profanity";
rl_getc_function = prof_rl_getc; rl_getc_function = _inp_rl_getc;
rl_startup_hook = startup_hook; rl_startup_hook = _inp_rl_startup_hook;
rl_callback_handler_install(NULL, cb_linehandler); rl_callback_handler_install(NULL, _inp_rl_linehandler);
signal(SIGWINCH, resize_signal_handler); signal(SIGWINCH, _inp_resize_signal_handler);
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));;
@ -321,6 +129,38 @@ create_input_window(void)
_inp_win_update_virtual(); _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 void
inp_win_resize(void) inp_win_resize(void)
{ {
@ -371,88 +211,6 @@ inp_block(void)
wtimeout(inp_win, -1); 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 void
inp_close(void) inp_close(void)
@ -497,3 +255,273 @@ _inp_win_update_virtual(void)
getmaxyx(stdscr, wrows, wcols); getmaxyx(stdscr, wrows, wcols);
pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-2); 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
_inp_printable(const wint_t ch)
{
char bytes[MB_CUR_MAX+1];
size_t utf_len = wcrtomb(bytes, ch, NULL);
bytes[utf_len] = '\0';
gunichar unichar = g_utf8_get_char(bytes);
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
_inp_rl_linehandler(char *line)
{
if (line && *line) {
add_history(line);
}
cmd_result = cmd_process_input(line);
free(line);
}
static int
_inp_rl_getc(FILE *stream)
{
int ch = rl_getc(stream);
if (_inp_printable(ch)) {
cmd_reset_autocomplete();
}
return ch;
}
static int
_inp_rl_tab_handler(int count, int key)
{
if (rl_point != rl_end || !rl_line_buffer) {
return 0;
}
if ((strncmp(rl_line_buffer, "/", 1) != 0) && (ui_current_win_type() == WIN_MUC)) {
char *result = muc_autocomplete(rl_line_buffer);
if (result) {
rl_replace_line(result, 0);
rl_point = rl_end;
}
} else if (strncmp(rl_line_buffer, "/", 1) == 0) {
char *result = cmd_autocomplete(rl_line_buffer);
if (result) {
rl_replace_line(result, 0);
rl_point = rl_end;
}
}
return 0;
}
static int
_inp_rl_win1_handler(int count, int key)
{
ui_switch_win(1);
return 0;
}
static int
_inp_rl_win2_handler(int count, int key)
{
ui_switch_win(2);
return 0;
}
static int
_inp_rl_win3_handler(int count, int key)
{
ui_switch_win(3);
return 0;
}
static int
_inp_rl_win4_handler(int count, int key)
{
ui_switch_win(4);
return 0;
}
static int
_inp_rl_win5_handler(int count, int key)
{
ui_switch_win(5);
return 0;
}
static int
_inp_rl_win6_handler(int count, int key)
{
ui_switch_win(6);
return 0;
}
static int
_inp_rl_win7_handler(int count, int key)
{
ui_switch_win(7);
return 0;
}
static int
_inp_rl_win8_handler(int count, int key)
{
ui_switch_win(8);
return 0;
}
static int
_inp_rl_win9_handler(int count, int key)
{
ui_switch_win(9);
return 0;
}
static int
_inp_rl_win0_handler(int count, int key)
{
ui_switch_win(0);
return 0;
}
static int
_inp_rl_altleft_handler(int count, int key)
{
ui_previous_win();
return 0;
}
static int
_inp_rl_altright_handler(int count, int key)
{
ui_next_win();
return 0;
}
static int
_inp_rl_pageup_handler(int count, int key)
{
ui_page_up();
return 0;
}
static int
_inp_rl_pagedown_handler(int count, int key)
{
ui_page_down();
return 0;
}
static int
_inp_rl_altpageup_handler(int count, int key)
{
ui_subwin_page_up();
return 0;
}
static int
_inp_rl_altpagedown_handler(int count, int key)
{
ui_subwin_page_down();
return 0;
}
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", _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", _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~", _inp_rl_pageup_handler);
rl_bind_keyseq("\\e[6~", _inp_rl_pagedown_handler);
rl_bind_key('\t', _inp_rl_tab_handler);
return 0;
}