aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2019-09-02 15:03:33 +0100
committerHarry Jeffery <harry@exec64.co.uk>2019-09-02 15:54:50 +0100
commit42a15b157827dbd9c981416c8919ce5f5c907331 (patch)
tree31786a1a4bf6e67beced6533b9eb276f8a8ce3d8
parentb8395af887ec1c3a3adfd8d56baa12caed740172 (diff)
downloadimv-42a15b157827dbd9c981416c8919ce5f5c907331.tar.gz
console: Use icu to provide proper UTF-8 editing
-rw-r--r--.builds/archlinux.yml1
-rw-r--r--.builds/debian.yml1
-rw-r--r--.builds/fedora.yml1
-rw-r--r--.builds/freebsd.yml1
-rw-r--r--.builds/ubuntu.yml1
-rw-r--r--Makefile2
-rw-r--r--README.md1
-rw-r--r--src/canvas.c7
-rw-r--r--src/canvas.h4
-rw-r--r--src/console.c102
-rw-r--r--src/console.h4
-rw-r--r--src/imv.c18
12 files changed, 131 insertions, 12 deletions
diff --git a/.builds/archlinux.yml b/.builds/archlinux.yml
index 27db21f..c60bbf7 100644
--- a/.builds/archlinux.yml
+++ b/.builds/archlinux.yml
@@ -3,6 +3,7 @@ packages:
- clang
- cmocka
- freeimage
+ - icu
- libjpeg-turbo
- libpng
- librsvg
diff --git a/.builds/debian.yml b/.builds/debian.yml
index d11ceef..81e2959 100644
--- a/.builds/debian.yml
+++ b/.builds/debian.yml
@@ -5,6 +5,7 @@ packages:
- libegl1-mesa-dev
- libfreeimage-dev
- libglu-dev
+ - libicu-dev
- libpango1.0-dev
- libpng-dev
- librsvg2-dev
diff --git a/.builds/fedora.yml b/.builds/fedora.yml
index 4bcec48..e641dde 100644
--- a/.builds/fedora.yml
+++ b/.builds/fedora.yml
@@ -4,6 +4,7 @@ packages:
- freeimage-devel
- libX11-devel
- libcmocka-devel
+ - libicu-devel
- libpng-devel
- librsvg2-devel
- libtiff-devel
diff --git a/.builds/freebsd.yml b/.builds/freebsd.yml
index 449b84d..98b62e7 100644
--- a/.builds/freebsd.yml
+++ b/.builds/freebsd.yml
@@ -1,6 +1,7 @@
image: freebsd/latest
packages:
- devel/gmake
+ - devel/icu
- devel/pkgconf
- graphics/freeimage
- graphics/libGLU
diff --git a/.builds/ubuntu.yml b/.builds/ubuntu.yml
index 3e8b080..0477de9 100644
--- a/.builds/ubuntu.yml
+++ b/.builds/ubuntu.yml
@@ -5,6 +5,7 @@ packages:
- libegl1-mesa-dev
- libfreeimage-dev
- libglu-dev
+ - libicu-dev
- libpango1.0-dev
- libpng-dev
- librsvg2-dev
diff --git a/Makefile b/Makefile
index d2a8b5a..2f0ff1d 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,7 @@ INSTALL_SCRIPT ?= install -m 0755
override CFLAGS += -std=c99 -W -Wall -Wpedantic -Wextra $(shell pkg-config --cflags pangocairo)
override CPPFLAGS += -D_XOPEN_SOURCE=700
-override LIBS := -lGL -lpthread -lxkbcommon $(shell pkg-config --libs pangocairo)
+override LIBS := -lGL -lpthread -lxkbcommon $(shell pkg-config --libs pangocairo) $(shell pkg-config --libs icu-io)
BUILDDIR ?= build
TARGET_WAYLAND = $(BUILDDIR)/imv-wayland
diff --git a/README.md b/README.md
index bb4e5d3..9e8008e 100644
--- a/README.md
+++ b/README.md
@@ -120,6 +120,7 @@ Installation
| pthreads | | Required. |
| xkbcommon | | Required. |
| pangocairo | | Required. |
+| icu | | Required. |
| X11 | | Optional. Required for X11 support. |
| GLU | | Optional. Required for X11 support. |
| xcb | | Optional. Required for X11 support. |
diff --git a/src/canvas.c b/src/canvas.c
index 4a00dbc..eaec538 100644
--- a/src/canvas.c
+++ b/src/canvas.c
@@ -127,7 +127,7 @@ void imv_canvas_font(struct imv_canvas *canvas, const char *name, int size)
pango_font_description_set_absolute_size(canvas->font, size * PANGO_SCALE);
}
-void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...)
+int imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...)
{
char line[1024];
va_list args;
@@ -140,9 +140,14 @@ void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt,
cairo_move_to(canvas->cairo, x, y);
pango_cairo_show_layout(canvas->cairo, layout);
+
+ PangoRectangle extents;
+ pango_layout_get_pixel_extents(layout, NULL, &extents);
+
g_object_unref(layout);
va_end(args);
+ return extents.width;
}
void imv_canvas_draw(struct imv_canvas *canvas)
diff --git a/src/canvas.h b/src/canvas.h
index 4f520fe..03425d9 100644
--- a/src/canvas.h
+++ b/src/canvas.h
@@ -37,8 +37,8 @@ void imv_canvas_fill_checkers(struct imv_canvas *canvas, int size);
/* Select the font to draw text with */
void imv_canvas_font(struct imv_canvas *canvas, const char *name, int size);
-/* Draw some text on the canvas */
-void imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...);
+/* Draw some text on the canvas, returns the width used in pixels */
+int imv_canvas_printf(struct imv_canvas *canvas, int x, int y, const char *fmt, ...);
/* Blit the canvas to the current OpenGL framebuffer */
void imv_canvas_draw(struct imv_canvas *canvas);
diff --git a/src/console.c b/src/console.c
index 92e578d..01b7925 100644
--- a/src/console.c
+++ b/src/console.c
@@ -1,15 +1,65 @@
#include "console.h"
+#include <assert.h>
#include <stdlib.h>
#include <string.h>
+#include <unicode/utext.h>
+#include <unicode/ubrk.h>
struct imv_console {
char *buffer;
size_t buffer_len;
+ size_t cursor;
+
imv_console_callback callback;
void *callback_data;
};
+/* Iterates forwards over characters in a UTF-8 string */
+static size_t next_char(char *buffer, size_t position)
+{
+ size_t result = position;
+ UErrorCode status = U_ZERO_ERROR;
+ UText *ut = utext_openUTF8(NULL, buffer, -1, &status);
+
+ UBreakIterator *it = ubrk_open(UBRK_CHARACTER, NULL, NULL, 0, &status);
+ ubrk_setUText(it, ut, &status);
+
+ int boundary = ubrk_following(it, (int)position);
+ if (boundary != UBRK_DONE) {
+ result = (size_t)boundary;
+ }
+
+ ubrk_close(it);
+
+ utext_close(ut);
+ assert(U_SUCCESS(status));
+ return result;
+}
+
+/* Iterates backwards over characters in a UTF-8 string */
+static size_t prev_char(char *buffer, size_t position)
+{
+ size_t result = position;
+ UErrorCode status = U_ZERO_ERROR;
+ UText *ut = utext_openUTF8(NULL, buffer, -1, &status);
+
+ UBreakIterator *it = ubrk_open(UBRK_CHARACTER, NULL, NULL, 0, &status);
+ ubrk_setUText(it, ut, &status);
+
+ int boundary = ubrk_preceding(it, (int)position);
+ if (boundary != UBRK_DONE) {
+ result = (size_t)boundary;
+ }
+
+ ubrk_close(it);
+
+ utext_close(ut);
+ assert(U_SUCCESS(status));
+ return result;
+}
+
+
struct imv_console *imv_console_create(void)
{
struct imv_console *console = calloc(1, sizeof *console);
@@ -45,6 +95,7 @@ void imv_console_activate(struct imv_console *console)
}
console->buffer = calloc(1, console->buffer_len);
+ console->cursor = 0;
}
void imv_console_input(struct imv_console *console, const char *text)
@@ -53,7 +104,21 @@ void imv_console_input(struct imv_console *console, const char *text)
return;
}
- strncat(console->buffer, text, console->buffer_len - 1);
+ /* Increase buffer size if needed */
+ if (strlen(text) + strlen(console->buffer) + 1 > console->buffer_len) {
+ console->buffer_len *= 2;
+ console->buffer = realloc(console->buffer, console->buffer_len);
+ assert(console->buffer);
+ }
+
+ /* memmove over everything after the cursor right then copy in the new bytes */
+ size_t to_insert = strlen(text);
+ size_t old_cursor = console->cursor;
+ size_t new_cursor = console->cursor + to_insert;
+ size_t to_shift = strlen(console->buffer) - old_cursor;
+ memmove(&console->buffer[new_cursor], &console->buffer[old_cursor], to_shift);
+ memcpy(&console->buffer[old_cursor], text, to_insert);
+ console->cursor = new_cursor;
}
bool imv_console_key(struct imv_console *console, const char *key)
@@ -77,11 +142,33 @@ bool imv_console_key(struct imv_console *console, const char *key)
return true;
}
+ if (!strcmp("Left", key) || !strcmp("Ctrl+b", key)) {
+ console->cursor = prev_char(console->buffer, console->cursor);
+ return true;
+ }
+
+ if (!strcmp("Right", key) || !strcmp("Ctrl+f", key)) {
+ console->cursor = next_char(console->buffer, console->cursor);
+ return true;
+ }
+
+ if (!strcmp("Ctrl+a", key)) {
+ console->cursor = 0;
+ return true;
+ }
+
+ if (!strcmp("Ctrl+e", key)) {
+ console->cursor = strlen(console->buffer);
+ return true;
+ }
+
if (!strcmp("BackSpace", key)) {
- const size_t len = strlen(console->buffer);
- if (len > 0) {
- console->buffer[len - 1] = '\0';
- }
+ /* memmove everything after the cursor left */
+ size_t new_cursor = prev_char(console->buffer, console->cursor);
+ size_t old_cursor = console->cursor;
+ size_t to_shift = strlen(console->buffer) - new_cursor;
+ memmove(&console->buffer[new_cursor], &console->buffer[old_cursor], to_shift);
+ console->cursor = new_cursor;
return true;
}
@@ -93,6 +180,11 @@ const char *imv_console_prompt(struct imv_console *console)
return console->buffer;
}
+size_t imv_console_prompt_cursor(struct imv_console *console)
+{
+ return console->cursor;
+}
+
const char *imv_console_backlog(struct imv_console *console)
{
(void)console;
diff --git a/src/console.h b/src/console.h
index 22cf9b6..d8d270d 100644
--- a/src/console.h
+++ b/src/console.h
@@ -2,6 +2,7 @@
#define IMV_CONSOLE
#include <stdbool.h>
+#include <unistd.h>
struct imv_console;
@@ -33,6 +34,9 @@ bool imv_console_key(struct imv_console *console, const char *key);
/* What is the console prompt's current text? */
const char *imv_console_prompt(struct imv_console *console);
+/* What is the console prompt's current cursor position? (bytes into UTF-8 string) */
+size_t imv_console_prompt_cursor(struct imv_console *console);
+
/* What is the output history of the console? */
const char *imv_console_backlog(struct imv_console *console);
diff --git a/src/imv.c b/src/imv.c
index a612bb2..4231fd4 100644
--- a/src/imv.c
+++ b/src/imv.c
@@ -377,7 +377,7 @@ static void key_handler(struct imv *imv, const struct imv_event *event)
{
if (imv_console_is_active(imv->console)) {
- if (imv_console_key(imv->console, event->data.keyboard.keyname)) {
+ if (imv_console_key(imv->console, event->data.keyboard.description)) {
imv->need_redraw = true;
return;
}
@@ -1209,8 +1209,20 @@ static void render_window(struct imv *imv)
imv_canvas_fill_rectangle(imv->canvas, 0, wh - height - bottom_offset,
ww, height + bottom_offset);
imv_canvas_color(imv->canvas, 1, 1, 1, 1);
- imv_canvas_printf(imv->canvas, 0, wh - height - bottom_offset,
- ":%s\u2588", imv_console_prompt(imv->console));
+
+ int x = 0;
+ /* draw pre-cursor text */
+ x += imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset,
+ ":%.*s",
+ imv_console_prompt_cursor(imv->console),
+ imv_console_prompt(imv->console));
+ /* draw the cursor */
+ imv_canvas_color(imv->canvas, 1, 1, 1, 0.5);
+ imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset, "\u2588");
+ /* any any remaining text on top of the cursor */
+ imv_canvas_color(imv->canvas, 1, 1, 1, 1);
+ imv_canvas_printf(imv->canvas, x, wh - height - bottom_offset, "%s",
+ imv_console_prompt(imv->console) + imv_console_prompt_cursor(imv->console));
}
imv_canvas_draw(imv->canvas);