Skip to content

Commit

Permalink
Merge pull request #23 from viccie30/backspace
Browse files Browse the repository at this point in the history
Make code sent by backspace key configurable
  • Loading branch information
Aetf authored Jul 21, 2022
2 parents 4faef1e + b26a06a commit 9637480
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/tsm/libtsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,19 @@ void tsm_vte_get_def_attr(struct tsm_vte *vte, struct tsm_screen_attr *out);
void tsm_vte_reset(struct tsm_vte *vte);
void tsm_vte_hard_reset(struct tsm_vte *vte);
void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len);

/**
* @brief Set backspace key to send either backspace or delete.
*
* Some terminals send ASCII backspace (010, 8, 0x08), some send ASCII delete
* (0177, 127, 0x7f).
*
* The default for vte is to send ASCII backspace.
*
* @param vte The vte object to set on
* @param enable Send ASCII delete if \c true, send ASCII backspace if \c false.
*/
void tsm_vte_set_backspace_sends_delete(struct tsm_vte *vte, bool enable);
bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
uint32_t ascii, unsigned int mods,
uint32_t unicode);
Expand Down
7 changes: 6 additions & 1 deletion src/tsm/libtsm.sym
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,10 @@ LIBTSM_4 {
global:
tsm_screen_draw;

tsm_vte_set_custom_palette;
tsm_vte_set_custom_palette;
} LIBTSM_3;

LIBTSM_4_1 {
global:
tsm_vte_set_backspace_sends_delete;
} LIBTSM_4;
13 changes: 12 additions & 1 deletion src/tsm/tsm-vte.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ struct tsm_vte {
tsm_vte_write_cb write_cb;
void *data;
char *palette_name;
bool backspace_sends_delete;

struct tsm_utf8_mach *mach;
unsigned long parse_cnt;
Expand Down Expand Up @@ -442,6 +443,7 @@ int tsm_vte_new(struct tsm_vte **out, struct tsm_screen *con,
vte->con = con;
vte->write_cb = write_cb;
vte->data = data;
vte->backspace_sends_delete = false;
vte->osc_cb = NULL;
vte->osc_data = NULL;
vte->custom_palette_storage = NULL;
Expand Down Expand Up @@ -2389,6 +2391,12 @@ void tsm_vte_input(struct tsm_vte *vte, const char *u8, size_t len)
--vte->parse_cnt;
}

SHL_EXPORT
void tsm_vte_set_backspace_sends_delete(struct tsm_vte *vte, bool enable)
{
vte->backspace_sends_delete = enable;
}

SHL_EXPORT
bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,
uint32_t ascii, unsigned int mods,
Expand Down Expand Up @@ -2569,7 +2577,10 @@ bool tsm_vte_handle_keyboard(struct tsm_vte *vte, uint32_t keysym,

switch (keysym) {
case XKB_KEY_BackSpace:
vte_write(vte, "\x08", 1);
if (vte->backspace_sends_delete)
vte_write(vte, "\x7f", 1);
else
vte_write(vte, "\x08", 1);
return true;
case XKB_KEY_Tab:
case XKB_KEY_KP_Tab:
Expand Down
16 changes: 16 additions & 0 deletions test/test_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,20 @@ static inline int test_run_suite(Suite *s)
return test_run_suite(_suite); \
}

#ifndef ck_assert_mem_eq
#include <string.h>
#define ck_assert_mem_eq(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) == 0)
#define ck_assert_mem_ne(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) != 0)
#define ck_assert_mem_lt(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) < 0)
#define ck_assert_mem_le(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) <= 0)
#define ck_assert_mem_gt(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) > 0)
#define ck_assert_mem_ge(_x, _y, _len) \
ck_assert(memcmp((_x), (_y), (_len)) >= 0)
#endif

#endif /* TEST_COMMON_H */
53 changes: 51 additions & 2 deletions test/test_vte.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "libtsm.h"
#include "test_common.h"

#include <xkbcommon/xkbcommon-keysyms.h>

static void log_cb(void *data, const char *file, int line, const char *func, const char *subs,
unsigned int sev, const char *format, va_list args)
{
Expand Down Expand Up @@ -157,10 +159,57 @@ START_TEST(test_vte_custom_palette)
}
END_TEST

static void checking_write_cb(struct tsm_vte *vte, const char *u8, size_t len, void *data)
{
ck_assert_ptr_ne(vte, NULL);
ck_assert_ptr_ne(u8, NULL);
ck_assert_uint_gt(len, 0);

ck_assert_mem_eq(u8, data, len);
}

START_TEST(test_vte_backspace_key)
{
struct tsm_screen *screen;
struct tsm_vte *vte;
char expected_output;
int r;

r = tsm_screen_new(&screen, log_cb, NULL);
ck_assert_int_eq(r, 0);

r = tsm_vte_new(&vte, screen, checking_write_cb, &expected_output, log_cb, NULL);
ck_assert_int_eq(r, 0);

expected_output = '\010';
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 010, 0, 010);
ck_assert(r);
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 0177, 0, 0177);
ck_assert(r);

tsm_vte_set_backspace_sends_delete(vte, true);

expected_output = '\177';
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 010, 0, 010);
ck_assert(r);
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 0177, 0, 0177);
ck_assert(r);

tsm_vte_set_backspace_sends_delete(vte, false);

expected_output = '\010';
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 010, 0, 010);
ck_assert(r);
r = tsm_vte_handle_keyboard(vte, XKB_KEY_BackSpace, 0177, 0, 0177);
ck_assert(r);
}
END_TEST

TEST_DEFINE_CASE(misc)
TEST(test_vte_init)
TEST(test_vte_null)
TEST(test_vte_custom_palette)
TEST(test_vte_null)
TEST(test_vte_custom_palette)
TEST(test_vte_backspace_key)
TEST_END_CASE

// clang-format off
Expand Down

0 comments on commit 9637480

Please sign in to comment.