2012-10-21 15:02:20 -04:00
|
|
|
/*
|
2013-02-02 14:57:46 -05:00
|
|
|
* inputwin.c
|
2012-02-20 15:07:38 -05:00
|
|
|
*
|
2014-03-08 20:18:19 -05:00
|
|
|
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
|
2012-10-21 15:02:20 -04:00
|
|
|
*
|
2012-02-20 15:07:38 -05: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/>.
|
|
|
|
*
|
2014-08-24 15:57:39 -04:00
|
|
|
* 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 15:07:38 -05:00
|
|
|
*/
|
|
|
|
|
2013-01-02 19:16:39 -05:00
|
|
|
#define _XOPEN_SOURCE_EXTENDED
|
2012-09-08 11:51:09 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-03-10 17:32:07 -05:00
|
|
|
#include <stdlib.h>
|
2012-08-25 20:50:50 -04:00
|
|
|
#include <string.h>
|
2013-01-02 19:16:39 -05:00
|
|
|
#include <wchar.h>
|
2012-05-10 18:51:06 -04:00
|
|
|
|
2015-01-30 18:28:02 -05:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
2013-01-02 15:27:37 -05:00
|
|
|
#ifdef HAVE_NCURSESW_NCURSES_H
|
|
|
|
#include <ncursesw/ncurses.h>
|
2013-01-03 19:35:54 -05:00
|
|
|
#elif HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
2012-09-08 11:51:09 -04:00
|
|
|
#endif
|
2012-05-10 18:51:06 -04:00
|
|
|
|
2013-02-02 16:43:59 -05:00
|
|
|
#include "command/command.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "common.h"
|
2013-11-07 17:15:43 -05:00
|
|
|
#include "config/accounts.h"
|
2013-02-02 16:59:29 -05:00
|
|
|
#include "config/preferences.h"
|
|
|
|
#include "config/theme.h"
|
2012-10-30 21:36:52 -04:00
|
|
|
#include "log.h"
|
2014-07-08 19:35:43 -04:00
|
|
|
#include "muc.h"
|
2012-11-19 18:15:42 -05:00
|
|
|
#include "profanity.h"
|
2013-12-14 10:34:17 -05:00
|
|
|
#include "roster_list.h"
|
2013-02-02 15:55:58 -05:00
|
|
|
#include "ui/ui.h"
|
2014-04-07 16:12:30 -04:00
|
|
|
#include "ui/statusbar.h"
|
2014-04-07 16:50:28 -04:00
|
|
|
#include "ui/inputwin.h"
|
2013-09-25 15:51:54 -04:00
|
|
|
#include "ui/windows.h"
|
2013-05-06 18:04:46 -04:00
|
|
|
#include "xmpp/xmpp.h"
|
2013-01-05 20:06:12 -05:00
|
|
|
|
2012-02-08 18:55:11 -05:00
|
|
|
static WINDOW *inp_win;
|
2015-01-16 18:32:57 -05:00
|
|
|
|
2015-01-30 18:28:02 -05:00
|
|
|
static struct timeval p_rl_timeout;
|
2015-01-30 20:11:41 -05:00
|
|
|
static gint inp_timeout = 0;
|
|
|
|
static gint no_input_count = 0;
|
|
|
|
|
2015-01-30 18:28:02 -05:00
|
|
|
static fd_set fds;
|
|
|
|
static int r;
|
|
|
|
static gboolean cmd_result = TRUE;
|
|
|
|
|
2012-07-07 16:51:43 -04:00
|
|
|
static int pad_start = 0;
|
2012-02-08 18:55:11 -05:00
|
|
|
|
2015-01-19 19:09:47 -05:00
|
|
|
static void _inp_win_update_virtual(void);
|
|
|
|
|
2015-01-30 18:28:02 -05:00
|
|
|
static void
|
|
|
|
cb_linehandler(char *line)
|
|
|
|
{
|
|
|
|
if (*line) {
|
|
|
|
add_history(line);
|
|
|
|
}
|
|
|
|
rl_redisplay();
|
|
|
|
cmd_result = cmd_process_input(line);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
|
2015-01-30 20:58:15 -05:00
|
|
|
int
|
|
|
|
tab_handler(int count, int key)
|
|
|
|
{
|
2015-01-31 11:51:09 -05:00
|
|
|
if (rl_point != rl_end || !rl_line_buffer) {
|
2015-01-30 20:58:15 -05: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;
|
|
|
|
inp_write(result, rl_point);
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
inp_write(result, rl_point);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
|
|
|
create_input_window(void)
|
2012-02-08 18:55:11 -05:00
|
|
|
{
|
2012-10-22 20:31:19 -04:00
|
|
|
#ifdef NCURSES_REENTRANT
|
|
|
|
set_escdelay(25);
|
|
|
|
#else
|
|
|
|
ESCDELAY = 25;
|
|
|
|
#endif
|
2015-01-30 18:28:02 -05:00
|
|
|
p_rl_timeout.tv_sec = 0;
|
2015-01-30 20:11:41 -05:00
|
|
|
p_rl_timeout.tv_usec = inp_timeout * 1000;
|
2015-01-30 18:28:02 -05:00
|
|
|
rl_callback_handler_install(NULL, cb_linehandler);
|
2015-01-30 20:58:15 -05:00
|
|
|
rl_bind_key('\t', tab_handler);
|
2015-01-31 11:51:09 -05:00
|
|
|
|
2012-07-09 18:48:53 -04:00
|
|
|
inp_win = newpad(1, INP_WIN_MAX);
|
2014-11-16 15:40:19 -05:00
|
|
|
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
|
2012-02-08 18:55:11 -05:00
|
|
|
keypad(inp_win, TRUE);
|
2012-07-07 21:43:28 -04:00
|
|
|
wmove(inp_win, 0, 0);
|
2014-04-01 16:52:04 -04:00
|
|
|
_inp_win_update_virtual();
|
2012-02-08 18:55:11 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
2014-11-19 11:32:26 -05:00
|
|
|
inp_win_resize(void)
|
2012-04-22 15:59:36 -04:00
|
|
|
{
|
2015-01-19 19:09:47 -05:00
|
|
|
int col = getcurx(inp_win);
|
|
|
|
int wcols = getmaxx(stdscr);
|
2012-10-21 15:02:20 -04:00
|
|
|
|
2012-07-07 22:31:54 -04:00
|
|
|
// if lost cursor off screen, move contents to show it
|
2015-01-18 17:55:51 -05:00
|
|
|
if (col >= pad_start + wcols) {
|
|
|
|
pad_start = col - (wcols / 2);
|
2012-07-09 18:48:53 -04:00
|
|
|
if (pad_start < 0) {
|
|
|
|
pad_start = 0;
|
|
|
|
}
|
2012-07-07 22:31:54 -04:00
|
|
|
}
|
|
|
|
|
2014-11-19 11:32:26 -05:00
|
|
|
wbkgd(inp_win, theme_attrs(THEME_INPUT_TEXT));;
|
2014-04-01 16:52:04 -04:00
|
|
|
_inp_win_update_virtual();
|
2012-02-08 18:55:11 -05:00
|
|
|
}
|
|
|
|
|
2015-01-27 17:13:09 -05:00
|
|
|
static int
|
|
|
|
offset_to_col(char *str, int offset)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
int col = 0;
|
|
|
|
mbstate_t internal;
|
|
|
|
|
|
|
|
while (i != offset && str[i] != '\n') {
|
|
|
|
gunichar uni = g_utf8_get_char(&str[i]);
|
|
|
|
size_t ch_len = mbrlen(&str[i], 4, &internal);
|
|
|
|
i += ch_len;
|
|
|
|
col++;
|
|
|
|
if (g_unichar_iswide(uni)) {
|
|
|
|
col++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return col;
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
2015-01-30 20:11:41 -05:00
|
|
|
inp_nonblocking(gboolean reset)
|
2012-02-08 18:55:11 -05:00
|
|
|
{
|
2015-01-30 20:11:41 -05:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-08 18:55:11 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
|
|
|
inp_block(void)
|
2012-02-16 19:42:41 -05:00
|
|
|
{
|
|
|
|
wtimeout(inp_win, -1);
|
|
|
|
}
|
|
|
|
|
2015-01-30 19:20:25 -05:00
|
|
|
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_update_virtual();
|
|
|
|
}
|
|
|
|
|
2015-01-31 11:51:09 -05:00
|
|
|
static int
|
|
|
|
_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-01-30 18:28:02 -05:00
|
|
|
gboolean
|
|
|
|
inp_readline(void)
|
|
|
|
{
|
|
|
|
FD_ZERO(&fds);
|
2015-01-30 18:42:51 -05:00
|
|
|
FD_SET(fileno(rl_instream), &fds);
|
2015-01-30 18:28:02 -05:00
|
|
|
r = select(FD_SETSIZE, &fds, NULL, NULL, &p_rl_timeout);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Readline failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:42:51 -05:00
|
|
|
if (FD_ISSET(fileno(rl_instream), &fds)) {
|
2015-01-31 11:51:09 -05:00
|
|
|
if (_printable(rl_executing_key)) {
|
|
|
|
cmd_reset_autocomplete();
|
|
|
|
}
|
|
|
|
|
2015-01-30 18:28:02 -05:00
|
|
|
rl_callback_read_char();
|
2015-01-31 11:51:09 -05:00
|
|
|
|
2015-01-30 19:20:25 -05:00
|
|
|
if (rl_line_buffer && rl_line_buffer[0] != '/') {
|
|
|
|
prof_handle_activity();
|
|
|
|
}
|
2015-01-31 11:51:09 -05:00
|
|
|
|
2015-01-30 20:11:41 -05:00
|
|
|
ui_reset_idle_time();
|
|
|
|
inp_nonblocking(TRUE);
|
2015-01-30 18:28:02 -05:00
|
|
|
inp_write(rl_line_buffer, rl_point);
|
2015-01-30 19:20:25 -05:00
|
|
|
} else {
|
2015-01-30 20:11:41 -05:00
|
|
|
inp_nonblocking(FALSE);
|
2015-01-30 19:20:25 -05:00
|
|
|
prof_handle_idle();
|
2015-01-30 18:28:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
p_rl_timeout.tv_sec = 0;
|
2015-01-30 20:11:41 -05:00
|
|
|
p_rl_timeout.tv_usec = inp_timeout * 1000;
|
2015-01-30 18:28:02 -05:00
|
|
|
|
|
|
|
return cmd_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
inp_close(void)
|
|
|
|
{
|
|
|
|
rl_callback_handler_remove();
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
|
|
|
inp_get_password(char *passwd)
|
2012-02-29 18:15:27 -05:00
|
|
|
{
|
2015-01-19 15:51:41 -05:00
|
|
|
werase(inp_win);
|
|
|
|
wmove(inp_win, 0, 0);
|
|
|
|
pad_start = 0;
|
2014-04-01 16:52:04 -04:00
|
|
|
_inp_win_update_virtual();
|
2014-02-02 18:56:50 -05:00
|
|
|
doupdate();
|
2012-02-29 18:15:27 -05:00
|
|
|
noecho();
|
2013-11-07 17:15:43 -05:00
|
|
|
mvwgetnstr(inp_win, 0, 1, passwd, MAX_PASSWORD_SIZE);
|
2012-07-07 21:43:28 -04:00
|
|
|
wmove(inp_win, 0, 0);
|
2012-02-29 18:15:27 -05:00
|
|
|
echo();
|
|
|
|
status_bar_clear();
|
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
|
|
|
inp_put_back(void)
|
2012-02-29 18:15:27 -05:00
|
|
|
{
|
2014-04-01 16:52:04 -04:00
|
|
|
_inp_win_update_virtual();
|
2012-02-29 18:15:27 -05:00
|
|
|
}
|
|
|
|
|
2014-04-07 16:50:28 -04:00
|
|
|
void
|
2015-01-19 19:09:47 -05:00
|
|
|
inp_win_clear(void)
|
2013-01-05 20:06:12 -05:00
|
|
|
{
|
2015-01-19 15:51:41 -05:00
|
|
|
werase(inp_win);
|
|
|
|
wmove(inp_win, 0, 0);
|
|
|
|
pad_start = 0;
|
2014-04-01 16:52:04 -04:00
|
|
|
_inp_win_update_virtual();
|
2013-01-05 20:06:12 -05:00
|
|
|
}
|
|
|
|
|
2015-01-19 19:09:47 -05:00
|
|
|
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-1);
|
2015-01-18 17:55:51 -05:00
|
|
|
}
|