2014-06-26 08:33:09 +02:00
|
|
|
/* input.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic 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.
|
|
|
|
*
|
|
|
|
* Toxic 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 Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE /* needed for wcwidth() */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
|
|
|
#include "misc_tools.h"
|
|
|
|
#include "toxic_strings.h"
|
|
|
|
#include "line_info.h"
|
2014-07-27 01:49:59 +02:00
|
|
|
#include "notify.h"
|
2014-06-26 08:33:09 +02:00
|
|
|
|
|
|
|
/* add a char to input field and buffer */
|
|
|
|
void input_new_char(ToxWindow *self, wint_t key, int x, int y, int mx_x, int mx_y)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
|
|
|
int cur_len = wcwidth(key);
|
|
|
|
|
|
|
|
/* this is the only place we need to do this check */
|
2014-07-14 20:32:38 +02:00
|
|
|
if (cur_len == -1) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
2014-07-14 20:32:38 +02:00
|
|
|
}
|
2014-06-26 08:33:09 +02:00
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (add_char_to_buf(ctx, key) == -1) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-07-14 20:32:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-06-26 08:33:09 +02:00
|
|
|
|
|
|
|
if (x + cur_len >= mx_x) {
|
|
|
|
int s_len = wcwidth(ctx->line[ctx->start]);
|
2014-07-14 20:32:38 +02:00
|
|
|
ctx->start += 1 + MAX(0, cur_len - s_len);
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a char via backspace key from input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_backspace(ToxWindow *self, int x, int mx_x)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (del_char_buf_bck(ctx) == -1) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 21:43:51 +02:00
|
|
|
int cur_len = ctx->pos > 0 ? wcwidth(ctx->line[ctx->pos - 1]) : 0;
|
|
|
|
int s_len = ctx->start > 0 ? wcwidth(ctx->line[ctx->start - 1]) : 0;
|
2014-06-26 08:33:09 +02:00
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (ctx->start && (x >= mx_x - cur_len))
|
|
|
|
ctx->start = MAX(0, ctx->start - 1 + (s_len - cur_len));
|
|
|
|
else if (ctx->start)
|
2014-06-26 08:33:09 +02:00
|
|
|
ctx->start = MAX(0, ctx->start - cur_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* delete a char via delete key from input field and buffer */
|
|
|
|
static void input_delete(ToxWindow *self)
|
|
|
|
{
|
2014-07-14 20:32:38 +02:00
|
|
|
if (del_char_buf_frnt(self->chatwin) == -1)
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* deletes entire line before cursor from input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_discard(ToxWindow *self)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
2014-07-14 20:32:38 +02:00
|
|
|
if (discard_buf(self->chatwin) == -1)
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* deletes entire line after cursor from input field and buffer */
|
|
|
|
static void input_kill(ChatContext *ctx)
|
|
|
|
{
|
2014-07-14 20:32:38 +02:00
|
|
|
if (kill_buf(ctx) == -1)
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(NULL, error, NT_ALWAYS, NULL);
|
2014-07-14 20:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void input_yank(ToxWindow *self, int x, int mx_x)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
|
|
|
if (yank_buf(ctx) == -1) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, error, 0, NULL);
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
int yank_cols = MAX(0, wcswidth(ctx->yank, ctx->yank_len));
|
|
|
|
|
|
|
|
if (x + yank_cols >= mx_x) {
|
|
|
|
int rmdr = MAX(0, (x + yank_cols) - mx_x);
|
|
|
|
int s_len = wcswidth(&ctx->line[ctx->start], rmdr);
|
|
|
|
ctx->start += s_len + 1;
|
|
|
|
}
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* moves cursor/line position to end of line in input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_mv_end(ToxWindow *self, int y, int mx_x)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
|
|
|
ctx->pos = ctx->len;
|
|
|
|
|
|
|
|
int wlen = wcswidth(ctx->line, sizeof(ctx->line));
|
|
|
|
ctx->start = MAX(0, 1 + (mx_x * (wlen / mx_x) - mx_x) + (wlen % mx_x));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* moves cursor/line position to start of line in input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_mv_home(ToxWindow *self)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (ctx->pos <= 0)
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
ctx->pos = 0;
|
|
|
|
ctx->start = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* moves cursor/line position left in input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_mv_left(ToxWindow *self, int x, int mx_x)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (ctx->pos <= 0)
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
int cur_len = ctx->pos > 0 ? wcwidth(ctx->line[ctx->pos - 1]) : 0;
|
|
|
|
int s_len = ctx->start > 0 ? wcwidth(ctx->line[ctx->start - 1]) : 0;
|
2014-06-26 08:33:09 +02:00
|
|
|
|
|
|
|
--ctx->pos;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (ctx->start && (x >= mx_x - cur_len))
|
|
|
|
ctx->start = MAX(0, ctx->start - 1 + (s_len - cur_len));
|
|
|
|
else if (ctx->start)
|
2014-06-26 08:33:09 +02:00
|
|
|
ctx->start = MAX(0, ctx->start - cur_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* moves cursor/line position right in input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_mv_right(ToxWindow *self, int x, int mx_x)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
if (ctx->pos >= ctx->len)
|
2014-06-26 08:33:09 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
++ctx->pos;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
int cur_len = wcwidth(ctx->line[ctx->pos - 1]);
|
2014-06-26 08:33:09 +02:00
|
|
|
|
|
|
|
if (x + cur_len >= mx_x) {
|
|
|
|
int s_len = wcwidth(ctx->line[ctx->start]);
|
2014-07-14 20:32:38 +02:00
|
|
|
ctx->start += 1 + MAX(0, cur_len - s_len);
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* puts a line history item in input field and buffer */
|
2014-07-14 20:32:38 +02:00
|
|
|
static void input_history(ToxWindow *self, wint_t key, int mx_x)
|
2014-06-26 08:33:09 +02:00
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
|
|
|
fetch_hist_item(ctx, key);
|
2014-07-16 18:47:14 +02:00
|
|
|
int wlen = wcswidth(ctx->line, sizeof(ctx->line));
|
|
|
|
ctx->start = wlen < mx_x ? 0 : wlen - mx_x + 1;
|
2014-06-26 08:33:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handles non-printable input keys that behave the same for all types of chat windows.
|
|
|
|
return true if key matches a function, false otherwise */
|
|
|
|
bool input_handle(ToxWindow *self, wint_t key, int x, int y, int mx_x, int mx_y)
|
|
|
|
{
|
|
|
|
bool match = true;
|
|
|
|
|
|
|
|
switch (key) {
|
2014-06-26 17:19:51 +02:00
|
|
|
case 0x7f:
|
2014-06-26 08:33:09 +02:00
|
|
|
case KEY_BACKSPACE:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_backspace(self, x, mx_x);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_DC:
|
|
|
|
input_delete(self);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_KEY_DISCARD:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_discard(self);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_KEY_KILL:
|
|
|
|
input_kill(self->chatwin);
|
|
|
|
break;
|
|
|
|
|
2014-07-14 20:32:38 +02:00
|
|
|
case T_KEY_C_Y:
|
|
|
|
input_yank(self, x, mx_x);
|
|
|
|
break;
|
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
case KEY_HOME:
|
|
|
|
case T_KEY_C_A:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_mv_home(self);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_END:
|
|
|
|
case T_KEY_C_E:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_mv_end(self, y, mx_x);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_LEFT:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_mv_left(self, x, mx_x);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_RIGHT:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_mv_right(self, x, mx_x);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case KEY_UP:
|
|
|
|
case KEY_DOWN:
|
2014-07-14 20:32:38 +02:00
|
|
|
input_history(self, key, mx_x);
|
2014-06-26 08:33:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|