1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-06-30 21:55:24 +00:00
profanity/src/ui/inputwin.c

527 lines
12 KiB
C
Raw Normal View History

/*
2013-02-02 19:57:46 +00:00
* inputwin.c
2012-02-20 20:07:38 +00:00
*
2014-03-09 01:18:19 +00:00
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
*
2012-02-20 20:07:38 +00:00
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give permission to
* link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two.
*
* You must obey the GNU General Public License in all respects for all of the
* code used other than OpenSSL. If you modify file(s) with this exception, you
* may extend this exception to your version of the file(s), but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version. If you delete this exception statement from all
* source files in the program, then also delete it here.
*
2012-02-20 20:07:38 +00:00
*/
2013-01-03 00:16:39 +00:00
#define _XOPEN_SOURCE_EXTENDED
2012-09-08 15:51:09 +00:00
#include "config.h"
#include <stdlib.h>
2012-08-26 00:50:50 +00:00
#include <string.h>
2013-01-03 00:16:39 +00:00
#include <wchar.h>
2015-02-01 21:01:00 +00:00
#include <sys/time.h>
2012-05-10 22:51:06 +00:00
2015-01-30 23:28:02 +00:00
#include <readline/readline.h>
#include <readline/history.h>
2013-01-02 20:27:37 +00:00
#ifdef HAVE_NCURSESW_NCURSES_H
#include <ncursesw/ncurses.h>
#elif HAVE_NCURSES_H
#include <ncurses.h>
2012-09-08 15:51:09 +00:00
#endif
2012-05-10 22:51:06 +00:00
#include "command/command.h"
2013-02-02 20:55:58 +00:00
#include "common.h"
#include "config/accounts.h"
2013-02-02 21:59:29 +00:00
#include "config/preferences.h"
#include "config/theme.h"
2012-10-31 01:36:52 +00:00
#include "log.h"
#include "muc.h"
2012-11-19 23:15:42 +00:00
#include "profanity.h"
#include "roster_list.h"
2013-02-02 20:55:58 +00:00
#include "ui/ui.h"
2014-04-07 20:12:30 +00:00
#include "ui/statusbar.h"
2014-04-07 20:50:28 +00:00
#include "ui/inputwin.h"
#include "ui/windows.h"
#include "xmpp/xmpp.h"
2012-02-08 23:55:11 +00:00
static WINDOW *inp_win;
2015-02-03 23:07:15 +00:00
static int pad_start = 0;
2015-01-16 23:32:57 +00:00
2015-01-30 23:28:02 +00:00
static struct timeval p_rl_timeout;
static gint inp_timeout = 0;
static gint no_input_count = 0;
2015-01-30 23:28:02 +00:00
static fd_set fds;
static int r;
static gboolean cmd_result = TRUE;
static void _inp_win_update_virtual(void);
2015-02-03 23:07:15 +00:00
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
2015-02-03 23:07:15 +00:00
_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);
}
2015-02-03 23:07:15 +00:00
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
2015-01-30 23:28:02 +00:00
static void
2015-02-03 23:07:15 +00:00
_inp_rl_linehandler(char *line)
2015-01-30 23:28:02 +00:00
{
2015-02-02 21:27:58 +00:00
if (line && *line) {
2015-01-30 23:28:02 +00:00
add_history(line);
}
cmd_result = cmd_process_input(line);
free(line);
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_getc(FILE *stream)
{
2015-02-03 23:07:15 +00:00
int ch = rl_getc(stream);
if (_inp_printable(ch)) {
cmd_reset_autocomplete();
}
return ch;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_tab_handler(int count, int key)
2015-01-31 01:58:15 +00:00
{
if (rl_point != rl_end || !rl_line_buffer) {
2015-01-31 01:58:15 +00:00
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;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win1_handler(int count, int key)
2015-01-31 18:02:42 +00:00
{
2015-01-31 22:00:22 +00:00
ui_switch_win(1);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win2_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(2);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win3_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(3);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win4_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(4);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win5_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(5);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win6_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(6);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win7_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(7);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win8_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(8);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win9_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(9);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_win0_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_switch_win(0);
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_altleft_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_previous_win();
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_altright_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_next_win();
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_pageup_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_page_up();
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_pagedown_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_page_down();
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_altpageup_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_subwin_page_up();
return 0;
}
2015-02-03 23:07:15 +00:00
static int
_inp_rl_altpagedown_handler(int count, int key)
2015-01-31 22:00:22 +00:00
{
ui_subwin_page_down();
return 0;
}
2015-02-03 23:07:15 +00:00
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);
2015-01-31 22:08:00 +00:00
2015-02-03 21:38:48 +00:00
#ifdef PLATFORM_OSX
2015-02-03 23:07:15 +00:00
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);
2015-02-03 21:38:48 +00:00
#else
2015-02-03 23:07:15 +00:00
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);
2015-02-03 21:38:48 +00:00
#endif
2015-02-03 23:07:15 +00:00
rl_bind_keyseq("\\e[5~", _inp_rl_pageup_handler);
rl_bind_keyseq("\\e[6~", _inp_rl_pagedown_handler);
2015-01-31 22:00:22 +00:00
2015-02-03 23:07:15 +00:00
rl_bind_key('\t', _inp_rl_tab_handler);
2015-01-31 22:00:22 +00:00
return 0;
2015-02-03 23:07:15 +00:00
}