mirror of
https://github.com/profanity-im/profanity.git
synced 2025-02-02 15:08:15 -05:00
Extracted keyhandler for printable characters
This commit is contained in:
parent
0e8092afef
commit
2ed78fe5af
@ -19,6 +19,7 @@ core_sources = \
|
|||||||
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
|
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
|
||||||
src/ui/console.c src/ui/notifier.c \
|
src/ui/console.c src/ui/notifier.c \
|
||||||
src/ui/windows.c src/ui/windows.h \
|
src/ui/windows.c src/ui/windows.h \
|
||||||
|
src/ui/keyhandlers.c src/ui/keyhandlers.h \
|
||||||
src/ui/rosterwin.c src/ui/occupantswin.c \
|
src/ui/rosterwin.c src/ui/occupantswin.c \
|
||||||
src/ui/buffer.c src/ui/buffer.h \
|
src/ui/buffer.c src/ui/buffer.h \
|
||||||
src/command/command.h src/command/command.c \
|
src/command/command.h src/command/command.c \
|
||||||
@ -58,6 +59,7 @@ tests_sources = \
|
|||||||
src/config/theme.c src/config/theme.h \
|
src/config/theme.c src/config/theme.h \
|
||||||
src/ui/windows.c src/ui/windows.h \
|
src/ui/windows.c src/ui/windows.h \
|
||||||
src/ui/window.c src/ui/window.h \
|
src/ui/window.c src/ui/window.h \
|
||||||
|
src/ui/keyhandlers.c src/ui/keyhandlers.h \
|
||||||
src/ui/buffer.c \
|
src/ui/buffer.c \
|
||||||
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
src/ui/titlebar.c src/ui/statusbar.c src/ui/inputwin.c \
|
||||||
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
|
src/ui/titlebar.h src/ui/statusbar.h src/ui/inputwin.h \
|
||||||
@ -92,6 +94,7 @@ tests_sources = \
|
|||||||
tests/test_server_events.c tests/test_server_events.h \
|
tests/test_server_events.c tests/test_server_events.h \
|
||||||
tests/test_autocomplete.c tests/test_autocomplete.h \
|
tests/test_autocomplete.c tests/test_autocomplete.h \
|
||||||
tests/test_chat_session.c tests/test_chat_session.h \
|
tests/test_chat_session.c tests/test_chat_session.h \
|
||||||
|
tests/test_keyhandlers.c tests/test_keyhandlers.h \
|
||||||
tests/testsuite.c
|
tests/testsuite.c
|
||||||
|
|
||||||
main_source = src/main.c
|
main_source = src/main.c
|
||||||
|
@ -212,7 +212,7 @@ ui_inp_history_append(char *inp)
|
|||||||
void
|
void
|
||||||
ui_input_clear(void)
|
ui_input_clear(void)
|
||||||
{
|
{
|
||||||
inp_win_reset();
|
inp_win_clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -60,8 +60,7 @@
|
|||||||
#include "ui/inputwin.h"
|
#include "ui/inputwin.h"
|
||||||
#include "ui/windows.h"
|
#include "ui/windows.h"
|
||||||
#include "xmpp/xmpp.h"
|
#include "xmpp/xmpp.h"
|
||||||
|
#include "ui/keyhandlers.h"
|
||||||
#define _inp_win_update_virtual() pnoutrefresh(inp_win, 0, pad_start, wrows-1, 0, wrows-1, wcols-1)
|
|
||||||
|
|
||||||
#define KEY_CTRL_A 0001
|
#define KEY_CTRL_A 0001
|
||||||
#define KEY_CTRL_B 0002
|
#define KEY_CTRL_B 0002
|
||||||
@ -74,7 +73,6 @@
|
|||||||
#define KEY_CTRL_W 0027
|
#define KEY_CTRL_W 0027
|
||||||
|
|
||||||
#define MAX_HISTORY 100
|
#define MAX_HISTORY 100
|
||||||
#define INP_WIN_MAX 1000
|
|
||||||
|
|
||||||
static WINDOW *inp_win;
|
static WINDOW *inp_win;
|
||||||
static History history;
|
static History history;
|
||||||
@ -85,7 +83,6 @@ static char line[INP_WIN_MAX];
|
|||||||
static int line_utf8_pos;
|
static int line_utf8_pos;
|
||||||
|
|
||||||
static int pad_start = 0;
|
static int pad_start = 0;
|
||||||
static int wrows, wcols;
|
|
||||||
|
|
||||||
static int _handle_edit(int key_type, const wint_t ch);
|
static int _handle_edit(int key_type, const wint_t ch);
|
||||||
static int _handle_alt_key(int key);
|
static int _handle_alt_key(int key);
|
||||||
@ -96,6 +93,8 @@ static void _handle_backspace(void);
|
|||||||
static gboolean _is_ctrl_left(int key_type, const wint_t ch);
|
static gboolean _is_ctrl_left(int key_type, const wint_t ch);
|
||||||
static gboolean _is_ctrl_right(int key_type, const wint_t ch);
|
static gboolean _is_ctrl_right(int key_type, const wint_t ch);
|
||||||
|
|
||||||
|
static void _inp_win_update_virtual(void);
|
||||||
|
|
||||||
void
|
void
|
||||||
create_input_window(void)
|
create_input_window(void)
|
||||||
{
|
{
|
||||||
@ -104,7 +103,6 @@ create_input_window(void)
|
|||||||
#else
|
#else
|
||||||
ESCDELAY = 25;
|
ESCDELAY = 25;
|
||||||
#endif
|
#endif
|
||||||
getmaxyx(stdscr, wrows, wcols);
|
|
||||||
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));;
|
||||||
keypad(inp_win, TRUE);
|
keypad(inp_win, TRUE);
|
||||||
@ -118,9 +116,8 @@ create_input_window(void)
|
|||||||
void
|
void
|
||||||
inp_win_resize(void)
|
inp_win_resize(void)
|
||||||
{
|
{
|
||||||
int col;
|
int col = getcurx(inp_win);
|
||||||
getmaxyx(stdscr, wrows, wcols);
|
int wcols = getmaxx(stdscr);
|
||||||
col = getcurx(inp_win);
|
|
||||||
|
|
||||||
// if lost cursor off screen, move contents to show it
|
// if lost cursor off screen, move contents to show it
|
||||||
if (col >= pad_start + wcols) {
|
if (col >= pad_start + wcols) {
|
||||||
@ -177,85 +174,13 @@ inp_read(int *key_type, wint_t *ch)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int col = getcurx(inp_win);
|
int col = getcurx(inp_win);
|
||||||
int utf8_len = g_utf8_strlen(line, -1);
|
int wcols = getmaxx(stdscr);
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, *ch, wcols);
|
||||||
|
|
||||||
// handle insert if not at end of input
|
werase(inp_win);
|
||||||
if (line_utf8_pos < utf8_len) {
|
waddstr(inp_win, line);
|
||||||
char bytes[MB_CUR_MAX];
|
wmove(inp_win, 0, col);
|
||||||
size_t utf8_ch_len = wcrtomb(bytes, *ch, NULL);
|
_inp_win_update_virtual();
|
||||||
bytes[utf8_ch_len] = '\0';
|
|
||||||
|
|
||||||
gchar *start = g_utf8_substring(line, 0, line_utf8_pos);
|
|
||||||
gchar *end = g_utf8_substring(line, line_utf8_pos, utf8_len);
|
|
||||||
GString *new_line = g_string_new(start);
|
|
||||||
g_string_append(new_line, bytes);
|
|
||||||
g_string_append(new_line, end);
|
|
||||||
|
|
||||||
int old_pos = line_utf8_pos;
|
|
||||||
werase(inp_win);
|
|
||||||
wmove(inp_win, 0, 0);
|
|
||||||
pad_start = 0;
|
|
||||||
line[0] = '\0';
|
|
||||||
line_utf8_pos = 0;
|
|
||||||
strncpy(line, new_line->str, INP_WIN_MAX);
|
|
||||||
waddstr(inp_win, line);
|
|
||||||
|
|
||||||
int display_len = utf8_display_len(line);
|
|
||||||
wmove(inp_win, 0, display_len);
|
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
|
||||||
|
|
||||||
if (display_len > wcols-2) {
|
|
||||||
pad_start = display_len - wcols + 1;
|
|
||||||
_inp_win_update_virtual();
|
|
||||||
}
|
|
||||||
|
|
||||||
line_utf8_pos = old_pos+1;
|
|
||||||
|
|
||||||
g_free(start);
|
|
||||||
g_free(end);
|
|
||||||
g_string_free(new_line, TRUE);
|
|
||||||
|
|
||||||
col++;
|
|
||||||
gunichar uni = g_utf8_get_char(bytes);
|
|
||||||
if (g_unichar_iswide(uni)) {
|
|
||||||
col++;
|
|
||||||
}
|
|
||||||
wmove(inp_win, 0, col);
|
|
||||||
|
|
||||||
// otherwise just append
|
|
||||||
} else {
|
|
||||||
char bytes[MB_CUR_MAX+1];
|
|
||||||
size_t utf8_ch_len = wcrtomb(bytes, *ch, NULL);
|
|
||||||
|
|
||||||
// wcrtomb can return (size_t) -1
|
|
||||||
if (utf8_ch_len < MB_CUR_MAX) {
|
|
||||||
int i;
|
|
||||||
for (i = 0 ; i < utf8_ch_len; i++) {
|
|
||||||
line[bytes_len++] = bytes[i];
|
|
||||||
}
|
|
||||||
line[bytes_len] = '\0';
|
|
||||||
|
|
||||||
bytes[utf8_ch_len] = '\0';
|
|
||||||
waddstr(inp_win, bytes);
|
|
||||||
|
|
||||||
line_utf8_pos++;
|
|
||||||
|
|
||||||
col++;
|
|
||||||
gunichar uni = g_utf8_get_char(bytes);
|
|
||||||
if (g_unichar_iswide(uni)) {
|
|
||||||
col++;
|
|
||||||
}
|
|
||||||
wmove(inp_win, 0, col);
|
|
||||||
|
|
||||||
// if gone over screen size follow input
|
|
||||||
int wrows, wcols;
|
|
||||||
getmaxyx(stdscr, wrows, wcols);
|
|
||||||
if (col - pad_start > wcols-2) {
|
|
||||||
pad_start++;
|
|
||||||
_inp_win_update_virtual();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd_reset_autocomplete();
|
cmd_reset_autocomplete();
|
||||||
}
|
}
|
||||||
@ -271,7 +196,7 @@ inp_read(int *key_type, wint_t *ch)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (*ch != ERR && *key_type != ERR) {
|
if (*ch != ERR && *key_type != ERR) {
|
||||||
cons_debug("BYTE LEN = %d", bytes_len);
|
cons_debug("BYTE LEN = %d", strlen(line));
|
||||||
cons_debug("UTF8 LEN = %d", utf8_display_len(line));
|
cons_debug("UTF8 LEN = %d", utf8_display_len(line));
|
||||||
cons_debug("CURR COL = %d", getcurx(inp_win));
|
cons_debug("CURR COL = %d", getcurx(inp_win));
|
||||||
cons_debug("CURR UNI = %d", line_utf8_pos);
|
cons_debug("CURR UNI = %d", line_utf8_pos);
|
||||||
@ -305,7 +230,7 @@ inp_put_back(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
inp_win_reset(void)
|
inp_win_clear(void)
|
||||||
{
|
{
|
||||||
werase(inp_win);
|
werase(inp_win);
|
||||||
wmove(inp_win, 0, 0);
|
wmove(inp_win, 0, 0);
|
||||||
@ -383,6 +308,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if gone off screen to left, jump left (half a screen worth)
|
// if gone off screen to left, jump left (half a screen worth)
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (col <= pad_start) {
|
if (col <= pad_start) {
|
||||||
pad_start = pad_start - (wcols / 2);
|
pad_start = pad_start - (wcols / 2);
|
||||||
if (pad_start < 0) {
|
if (pad_start < 0) {
|
||||||
@ -427,6 +353,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, col);
|
wmove(inp_win, 0, col);
|
||||||
|
|
||||||
// if gone off screen to right, jump right (half a screen worth)
|
// if gone off screen to right, jump right (half a screen worth)
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (col > pad_start + wcols) {
|
if (col > pad_start + wcols) {
|
||||||
pad_start = pad_start + (wcols / 2);
|
pad_start = pad_start + (wcols / 2);
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -458,7 +385,12 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
if (next_ch != ERR) {
|
if (next_ch != ERR) {
|
||||||
return _handle_alt_key(next_ch);
|
return _handle_alt_key(next_ch);
|
||||||
} else {
|
} else {
|
||||||
inp_win_reset();
|
werase(inp_win);
|
||||||
|
wmove(inp_win, 0, 0);
|
||||||
|
pad_start = 0;
|
||||||
|
line[0] = '\0';
|
||||||
|
line_utf8_pos = 0;
|
||||||
|
_inp_win_update_virtual();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,6 +489,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
line_utf8_pos++;
|
line_utf8_pos++;
|
||||||
|
|
||||||
// current position off screen to right
|
// current position off screen to right
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if ((col + 1 - pad_start) >= wcols) {
|
if ((col + 1 - pad_start) >= wcols) {
|
||||||
pad_start++;
|
pad_start++;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -584,6 +517,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -611,6 +545,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -630,6 +565,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -657,6 +593,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -681,6 +618,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -703,6 +641,7 @@ _handle_edit(int key_type, const wint_t ch)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -755,6 +694,7 @@ _handle_backspace(void)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -783,6 +723,7 @@ _handle_backspace(void)
|
|||||||
wmove(inp_win, 0, display_len);
|
wmove(inp_win, 0, display_len);
|
||||||
line_utf8_pos = g_utf8_strlen(line, -1);
|
line_utf8_pos = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
if (display_len > wcols-2) {
|
if (display_len > wcols-2) {
|
||||||
pad_start = display_len - wcols + 1;
|
pad_start = display_len - wcols + 1;
|
||||||
_inp_win_update_virtual();
|
_inp_win_update_virtual();
|
||||||
@ -804,6 +745,7 @@ _handle_backspace(void)
|
|||||||
|
|
||||||
// if gone off screen to left, jump left (half a screen worth)
|
// if gone off screen to left, jump left (half a screen worth)
|
||||||
if (col <= pad_start) {
|
if (col <= pad_start) {
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
pad_start = pad_start - (wcols / 2);
|
pad_start = pad_start - (wcols / 2);
|
||||||
if (pad_start < 0) {
|
if (pad_start < 0) {
|
||||||
pad_start = 0;
|
pad_start = 0;
|
||||||
@ -931,6 +873,7 @@ _handle_delete_previous_word(void)
|
|||||||
|
|
||||||
// if gone off screen to left, jump left (half a screen worth)
|
// if gone off screen to left, jump left (half a screen worth)
|
||||||
if (start_del <= pad_start) {
|
if (start_del <= pad_start) {
|
||||||
|
int wcols = getmaxx(stdscr);
|
||||||
pad_start = pad_start - (wcols / 2);
|
pad_start = pad_start - (wcols / 2);
|
||||||
if (pad_start < 0) {
|
if (pad_start < 0) {
|
||||||
pad_start = 0;
|
pad_start = 0;
|
||||||
@ -952,4 +895,12 @@ _is_ctrl_right(int key_type, const wint_t ch)
|
|||||||
{
|
{
|
||||||
return ((key_type == KEY_CODE_YES)
|
return ((key_type == KEY_CODE_YES)
|
||||||
&& (ch == 562 || ch == 560 || ch == 555 || ch == 559 || ch == 554));
|
&& (ch == 562 || ch == 560 || ch == 555 || ch == 559 || ch == 554));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
@ -35,9 +35,13 @@
|
|||||||
#ifndef UI_INPUTWIN_H
|
#ifndef UI_INPUTWIN_H
|
||||||
#define UI_INPUTWIN_H
|
#define UI_INPUTWIN_H
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#define INP_WIN_MAX 1000
|
||||||
|
|
||||||
void create_input_window(void);
|
void create_input_window(void);
|
||||||
char* inp_read(int *key_type, wint_t *ch);
|
char* inp_read(int *key_type, wint_t *ch);
|
||||||
void inp_win_reset(void);
|
void inp_win_clear(void);
|
||||||
void inp_win_resize(void);
|
void inp_win_resize(void);
|
||||||
void inp_put_back(void);
|
void inp_put_back(void);
|
||||||
void inp_non_block(gint);
|
void inp_non_block(gint);
|
||||||
|
123
src/ui/keyhandlers.c
Normal file
123
src/ui/keyhandlers.c
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
/*
|
||||||
|
* keyhandlers.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
|
#include <glib.h>
|
||||||
|
|
||||||
|
#include "ui/inputwin.h"
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
void
|
||||||
|
key_printable(char * const line, int * const line_utf8_pos, int * const col, int * const pad_start, const wint_t ch, const int wcols)
|
||||||
|
{
|
||||||
|
int utf8_len = g_utf8_strlen(line, -1);
|
||||||
|
|
||||||
|
// handle insert if not at end of input
|
||||||
|
if (*line_utf8_pos < utf8_len) {
|
||||||
|
// create new line
|
||||||
|
char bytes[MB_CUR_MAX];
|
||||||
|
size_t utf8_ch_len = wcrtomb(bytes, ch, NULL);
|
||||||
|
bytes[utf8_ch_len] = '\0';
|
||||||
|
gchar *start = g_utf8_substring(line, 0, *line_utf8_pos);
|
||||||
|
gchar *end = g_utf8_substring(line, *line_utf8_pos, utf8_len);
|
||||||
|
GString *new_line_str = g_string_new(start);
|
||||||
|
g_string_append(new_line_str, bytes);
|
||||||
|
g_string_append(new_line_str, end);
|
||||||
|
char *new_line = new_line_str->str;
|
||||||
|
g_free(start);
|
||||||
|
g_free(end);
|
||||||
|
g_string_free(new_line_str, FALSE);
|
||||||
|
|
||||||
|
// replace old line
|
||||||
|
strncpy(line, new_line, INP_WIN_MAX);
|
||||||
|
free(new_line);
|
||||||
|
|
||||||
|
// set utf8 position
|
||||||
|
(*line_utf8_pos)++;
|
||||||
|
|
||||||
|
// set col position
|
||||||
|
(*col)++;
|
||||||
|
gunichar uni = g_utf8_get_char(bytes);
|
||||||
|
if (g_unichar_iswide(uni)) {
|
||||||
|
(*col)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set pad_start
|
||||||
|
int display_len = utf8_display_len(line);
|
||||||
|
(*pad_start) = 0;
|
||||||
|
if (display_len > wcols-2) {
|
||||||
|
(*pad_start) = display_len - wcols + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise just append
|
||||||
|
} else {
|
||||||
|
char bytes[MB_CUR_MAX+1];
|
||||||
|
size_t utf8_ch_len = wcrtomb(bytes, ch, NULL);
|
||||||
|
|
||||||
|
// wcrtomb can return (size_t) -1
|
||||||
|
if (utf8_ch_len < MB_CUR_MAX) {
|
||||||
|
// update old line
|
||||||
|
int i;
|
||||||
|
int bytes_len = strlen(line);
|
||||||
|
|
||||||
|
for (i = 0 ; i < utf8_ch_len; i++) {
|
||||||
|
line[bytes_len++] = bytes[i];
|
||||||
|
}
|
||||||
|
line[bytes_len] = '\0';
|
||||||
|
|
||||||
|
// set utf8 position
|
||||||
|
(*line_utf8_pos)++;
|
||||||
|
|
||||||
|
// set col position
|
||||||
|
(*col)++;
|
||||||
|
bytes[utf8_ch_len] = '\0';
|
||||||
|
gunichar uni = g_utf8_get_char(bytes);
|
||||||
|
if (g_unichar_iswide(uni)) {
|
||||||
|
(*col)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set pad_start
|
||||||
|
// if gone over screen size follow input
|
||||||
|
if (*col - *pad_start > wcols-2) {
|
||||||
|
(*pad_start)++;
|
||||||
|
if (g_unichar_iswide(uni)) {
|
||||||
|
(*pad_start)++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
42
src/ui/keyhandlers.h
Normal file
42
src/ui/keyhandlers.h
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* keyhandlers.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2012 - 2014 James Booth <boothj5@gmail.com>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef UI_KEYHANDLERS_H
|
||||||
|
#define UI_KEYHANDLERS_H
|
||||||
|
|
||||||
|
#include <wchar.h>
|
||||||
|
|
||||||
|
void key_printable(char * const line, int * const line_utf8_pos, int * const col, int * const pad_start, const wint_t ch, const int wcols);
|
||||||
|
|
||||||
|
#endif
|
150
tests/test_keyhandlers.c
Normal file
150
tests/test_keyhandlers.c
Normal file
@ -0,0 +1,150 @@
|
|||||||
|
#include "ui/keyhandlers.h"
|
||||||
|
#include "ui/inputwin.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
|
static char line[INP_WIN_MAX];
|
||||||
|
|
||||||
|
void append_non_wide_to_empty(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
line[0] = '\0';
|
||||||
|
int line_utf8_pos = 0;
|
||||||
|
int col = 0;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'a', 80);
|
||||||
|
|
||||||
|
assert_string_equal("a", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 1);
|
||||||
|
assert_int_equal(col, 1);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_wide_to_empty(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
line[0] = '\0';
|
||||||
|
int line_utf8_pos = 0;
|
||||||
|
int col = 0;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);
|
||||||
|
|
||||||
|
assert_string_equal("四", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 1);
|
||||||
|
assert_int_equal(col, 2);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_non_wide_to_non_wide(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
strncpy(line, "a", 1);
|
||||||
|
line[1] = '\0';
|
||||||
|
int line_utf8_pos = 1;
|
||||||
|
int col = 1;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);
|
||||||
|
|
||||||
|
assert_string_equal("ab", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 2);
|
||||||
|
assert_int_equal(col, 2);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_wide_to_non_wide(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
strncpy(line, "a", 1);
|
||||||
|
line[1] = '\0';
|
||||||
|
int line_utf8_pos = 1;
|
||||||
|
int col = 1;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x56DB, 80);
|
||||||
|
|
||||||
|
assert_string_equal("a四", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 2);
|
||||||
|
assert_int_equal(col, 3);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_non_wide_to_wide(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
g_utf8_strncpy(line, "四", 1);
|
||||||
|
line[strlen(line)] = '\0';
|
||||||
|
int line_utf8_pos = 1;
|
||||||
|
int col = 2;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'b', 80);
|
||||||
|
|
||||||
|
assert_string_equal("四b", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 2);
|
||||||
|
assert_int_equal(col, 3);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_wide_to_wide(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
g_utf8_strncpy(line, "四", 1);
|
||||||
|
line[strlen(line)] = '\0';
|
||||||
|
int line_utf8_pos = 1;
|
||||||
|
int col = 2;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 80);
|
||||||
|
|
||||||
|
assert_string_equal("四三", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 2);
|
||||||
|
assert_int_equal(col, 4);
|
||||||
|
assert_int_equal(pad_start, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_no_wide_when_overrun(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
g_utf8_strncpy(line, "0123456789四1234567", 18);
|
||||||
|
line[strlen(line)] = '\0';
|
||||||
|
int line_utf8_pos = 18;
|
||||||
|
int col = 19;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 'z', 20);
|
||||||
|
|
||||||
|
assert_string_equal("0123456789四1234567zzz", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 21);
|
||||||
|
assert_int_equal(col, 22);
|
||||||
|
assert_int_equal(pad_start, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void append_wide_when_overrun(void **state)
|
||||||
|
{
|
||||||
|
setlocale(LC_ALL, "");
|
||||||
|
g_utf8_strncpy(line, "0123456789四1234567", 18);
|
||||||
|
line[strlen(line)] = '\0';
|
||||||
|
int line_utf8_pos = 18;
|
||||||
|
int col = 19;
|
||||||
|
int pad_start = 0;
|
||||||
|
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
|
||||||
|
key_printable(line, &line_utf8_pos, &col, &pad_start, 0x4E09, 20);
|
||||||
|
|
||||||
|
assert_string_equal("0123456789四1234567三三三", line);
|
||||||
|
assert_int_equal(line_utf8_pos, 21);
|
||||||
|
assert_int_equal(col, 25);
|
||||||
|
assert_int_equal(pad_start, 6);
|
||||||
|
}
|
11
tests/test_keyhandlers.h
Normal file
11
tests/test_keyhandlers.h
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
void append_non_wide_to_empty(void **state);
|
||||||
|
void append_wide_to_empty(void **state);
|
||||||
|
|
||||||
|
void append_non_wide_to_non_wide(void **state);
|
||||||
|
void append_wide_to_non_wide(void **state);
|
||||||
|
|
||||||
|
void append_non_wide_to_wide(void **state);
|
||||||
|
void append_wide_to_wide(void **state);
|
||||||
|
|
||||||
|
void append_no_wide_when_overrun(void **state);
|
||||||
|
void append_wide_when_overrun(void **state);
|
@ -35,6 +35,7 @@
|
|||||||
#include "test_cmd_win.h"
|
#include "test_cmd_win.h"
|
||||||
#include "test_cmd_disconnect.h"
|
#include "test_cmd_disconnect.h"
|
||||||
#include "test_form.h"
|
#include "test_form.h"
|
||||||
|
#include "test_keyhandlers.h"
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
const UnitTest all_tests[] = {
|
const UnitTest all_tests[] = {
|
||||||
@ -622,6 +623,15 @@ int main(int argc, char* argv[]) {
|
|||||||
unit_test(remove_text_multi_value_removes_when_many),
|
unit_test(remove_text_multi_value_removes_when_many),
|
||||||
|
|
||||||
unit_test(clears_chat_sessions),
|
unit_test(clears_chat_sessions),
|
||||||
|
|
||||||
|
unit_test(append_non_wide_to_empty),
|
||||||
|
unit_test(append_wide_to_empty),
|
||||||
|
unit_test(append_non_wide_to_non_wide),
|
||||||
|
unit_test(append_wide_to_non_wide),
|
||||||
|
unit_test(append_non_wide_to_wide),
|
||||||
|
unit_test(append_wide_to_wide),
|
||||||
|
unit_test(append_no_wide_when_overrun),
|
||||||
|
unit_test(append_wide_when_overrun),
|
||||||
};
|
};
|
||||||
|
|
||||||
return run_tests(all_tests);
|
return run_tests(all_tests);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user