Adjust all code to use new kmscon_symbol_t

This is a big performance boost and reduces the code size quite a bit.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
This commit is contained in:
David Herrmann 2012-01-01 17:43:57 +01:00
parent 4eca3ef714
commit a7128cc683
11 changed files with 112 additions and 207 deletions

View File

@ -48,9 +48,11 @@
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "unicode.h"
struct kmscon_console { struct kmscon_console {
size_t ref; size_t ref;
struct kmscon_symbol_table *st;
/* GL texture and font */ /* GL texture and font */
GLuint tex; GLuint tex;
@ -146,7 +148,8 @@ err_free:
return ret; return ret;
} }
int kmscon_console_new(struct kmscon_console **out) int kmscon_console_new(struct kmscon_console **out,
struct kmscon_symbol_table *st)
{ {
struct kmscon_console *con; struct kmscon_console *con;
int ret; int ret;
@ -160,6 +163,7 @@ int kmscon_console_new(struct kmscon_console **out)
memset(con, 0, sizeof(*con)); memset(con, 0, sizeof(*con));
con->ref = 1; con->ref = 1;
con->st = st;
log_debug("console: new console\n"); log_debug("console: new console\n");
ret = kmscon_buffer_new(&con->cells, 0, 0); ret = kmscon_buffer_new(&con->cells, 0, 0);
@ -169,7 +173,9 @@ int kmscon_console_new(struct kmscon_console **out)
con->cells_x = kmscon_buffer_get_width(con->cells); con->cells_x = kmscon_buffer_get_width(con->cells);
con->cells_y = kmscon_buffer_get_height(con->cells); con->cells_y = kmscon_buffer_get_height(con->cells);
kmscon_symbol_table_ref(con->st);
*out = con; *out = con;
return 0; return 0;
err_free: err_free:
@ -200,6 +206,7 @@ void kmscon_console_unref(struct kmscon_console *con)
kmscon_console_free_res(con); kmscon_console_free_res(con);
kmscon_font_unref(con->font); kmscon_font_unref(con->font);
kmscon_buffer_unref(con->cells); kmscon_buffer_unref(con->cells);
kmscon_symbol_table_unref(con->st);
free(con); free(con);
log_debug("console: destroing console\n"); log_debug("console: destroing console\n");
} }
@ -272,7 +279,7 @@ int kmscon_console_resize(struct kmscon_console *con, unsigned int x,
if (con->cursor_y > con->cells_y) if (con->cursor_y > con->cells_y)
con->cursor_y = con->cells_y; con->cursor_y = con->cells_y;
ret = kmscon_font_new(&font, height / con->cells_y); ret = kmscon_font_new(&font, height / con->cells_y, con->st);
if (ret) { if (ret) {
log_err("console: cannot create new font: %d\n", ret); log_err("console: cannot create new font: %d\n", ret);
return ret; return ret;
@ -362,8 +369,7 @@ void kmscon_console_map(struct kmscon_console *con)
glEnd(); glEnd();
} }
void kmscon_console_write(struct kmscon_console *con, void kmscon_console_write(struct kmscon_console *con, kmscon_symbol_t ch)
const struct kmscon_char *ch)
{ {
if (!con) if (!con)
return; return;

View File

@ -37,6 +37,7 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdlib.h> #include <stdlib.h>
#include "unicode.h"
struct kmscon_char; struct kmscon_char;
struct kmscon_font; struct kmscon_font;
@ -63,13 +64,14 @@ int kmscon_char_append_u8(struct kmscon_char *ch, const char *str, size_t len);
/* font objects with cached glyphs */ /* font objects with cached glyphs */
int kmscon_font_new(struct kmscon_font **out, unsigned int height); int kmscon_font_new(struct kmscon_font **out, unsigned int height,
struct kmscon_symbol_table *st);
void kmscon_font_ref(struct kmscon_font *font); void kmscon_font_ref(struct kmscon_font *font);
void kmscon_font_unref(struct kmscon_font *font); void kmscon_font_unref(struct kmscon_font *font);
unsigned int kmscon_font_get_height(struct kmscon_font *font); unsigned int kmscon_font_get_height(struct kmscon_font *font);
unsigned int kmscon_font_get_width(struct kmscon_font *font); unsigned int kmscon_font_get_width(struct kmscon_font *font);
int kmscon_font_draw(struct kmscon_font *font, const struct kmscon_char *ch, int kmscon_font_draw(struct kmscon_font *font, kmscon_symbol_t ch,
void *dcr, uint32_t x, uint32_t y); void *dcr, uint32_t x, uint32_t y);
/* console buffer with cell objects */ /* console buffer with cell objects */
@ -87,14 +89,15 @@ void kmscon_buffer_draw(struct kmscon_buffer *buf, struct kmscon_font *font,
unsigned int kmscon_buffer_get_width(struct kmscon_buffer *buf); unsigned int kmscon_buffer_get_width(struct kmscon_buffer *buf);
unsigned int kmscon_buffer_get_height(struct kmscon_buffer *buf); unsigned int kmscon_buffer_get_height(struct kmscon_buffer *buf);
void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x, void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x,
unsigned int y, const struct kmscon_char *ch); unsigned int y, kmscon_symbol_t ch);
void kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x, kmscon_symbol_t kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x,
unsigned int y, struct kmscon_char *ch); unsigned int y);
void kmscon_buffer_rotate(struct kmscon_buffer *buf); void kmscon_buffer_rotate(struct kmscon_buffer *buf);
/* console objects */ /* console objects */
int kmscon_console_new(struct kmscon_console **out); int kmscon_console_new(struct kmscon_console **out,
struct kmscon_symbol_table *st);
void kmscon_console_ref(struct kmscon_console *con); void kmscon_console_ref(struct kmscon_console *con);
void kmscon_console_unref(struct kmscon_console *con); void kmscon_console_unref(struct kmscon_console *con);
@ -106,6 +109,5 @@ int kmscon_console_resize(struct kmscon_console *con, unsigned int x,
void kmscon_console_draw(struct kmscon_console *con); void kmscon_console_draw(struct kmscon_console *con);
void kmscon_console_map(struct kmscon_console *con); void kmscon_console_map(struct kmscon_console *con);
void kmscon_console_write(struct kmscon_console *con, void kmscon_console_write(struct kmscon_console *con, kmscon_symbol_t ch);
const struct kmscon_char *ch);
void kmscon_console_newline(struct kmscon_console *con); void kmscon_console_newline(struct kmscon_console *con);

View File

@ -84,13 +84,14 @@
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "unicode.h"
#define DEFAULT_WIDTH 80 #define DEFAULT_WIDTH 80
#define DEFAULT_HEIGHT 24 #define DEFAULT_HEIGHT 24
#define DEFAULT_SCROLLBACK 128 #define DEFAULT_SCROLLBACK 128
struct cell { struct cell {
struct kmscon_char *ch; kmscon_symbol_t ch;
}; };
struct line { struct line {
@ -121,8 +122,6 @@ static void destroy_cell(struct cell *cell)
{ {
if (!cell) if (!cell)
return; return;
kmscon_char_free(cell->ch);
} }
static int init_cell(struct cell *cell) static int init_cell(struct cell *cell)
@ -131,8 +130,7 @@ static int init_cell(struct cell *cell)
return -EINVAL; return -EINVAL;
memset(cell, 0, sizeof(*cell)); memset(cell, 0, sizeof(*cell));
return 0;
return kmscon_char_new(&cell->ch);
} }
static void free_line(struct line *line) static void free_line(struct line *line)
@ -487,12 +485,12 @@ unsigned int kmscon_buffer_get_height(struct kmscon_buffer *buf)
} }
void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x, void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x,
unsigned int y, const struct kmscon_char *ch) unsigned int y, kmscon_symbol_t ch)
{ {
struct line *line; struct line *line;
int ret; int ret;
if (!buf || !ch) if (!buf)
return; return;
if (x >= buf->size_x || y >= buf->size_y) { if (x >= buf->size_x || y >= buf->size_y) {
@ -523,42 +521,30 @@ void kmscon_buffer_write(struct kmscon_buffer *buf, unsigned int x,
} }
} }
ret = kmscon_char_set(line->cells[x].ch, ch); line->cells[x].ch = ch;
if (ret) {
log_warning("console: cannot copy character (%d); "
"dropping input\n", ret);
return;
}
} }
void kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x, kmscon_symbol_t kmscon_buffer_read(struct kmscon_buffer *buf, unsigned int x,
unsigned int y, struct kmscon_char *ch) unsigned int y)
{ {
struct line *line; struct line *line;
if (!ch)
return;
if (!buf) if (!buf)
goto err_out; return kmscon_symbol_default;
if (x >= buf->size_x || y >= buf->size_y) { if (x >= buf->size_x || y >= buf->size_y) {
log_warning("console: reading out of buffer bounds\n"); log_warning("console: reading out of buffer bounds\n");
goto err_out; return kmscon_symbol_default;
} }
line = buf->current[y]; line = buf->current[y];
if (!line) if (!line)
goto err_out; return kmscon_symbol_default;
if (x >= line->size) if (x >= line->size)
goto err_out; return kmscon_symbol_default;
kmscon_char_set(ch, line->cells[x].ch); return line->cells[x].ch;
return;
err_out:
kmscon_char_reset(ch);
} }
void kmscon_buffer_rotate(struct kmscon_buffer *buf) void kmscon_buffer_rotate(struct kmscon_buffer *buf)

View File

@ -51,6 +51,7 @@
#include <pango/pangocairo.h> #include <pango/pangocairo.h>
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "unicode.h"
/* maximum size of a single character */ /* maximum size of a single character */
#define KMSCON_CHAR_SIZE 6 #define KMSCON_CHAR_SIZE 6
@ -69,7 +70,7 @@ enum glyph_type {
struct kmscon_glyph { struct kmscon_glyph {
size_t ref; size_t ref;
struct kmscon_char *ch; kmscon_symbol_t ch;
unsigned int width; unsigned int width;
int type; int type;
@ -88,6 +89,7 @@ struct kmscon_glyph {
struct kmscon_font { struct kmscon_font {
size_t ref; size_t ref;
struct kmscon_symbol_table *st;
unsigned int width; unsigned int width;
unsigned int height; unsigned int height;
@ -95,8 +97,8 @@ struct kmscon_font {
PangoContext *ctx; PangoContext *ctx;
}; };
static int kmscon_font_lookup(struct kmscon_font *font, static int kmscon_font_lookup(struct kmscon_font *font, kmscon_symbol_t key,
const struct kmscon_char *key, struct kmscon_glyph **out); struct kmscon_glyph **out);
static int new_char(struct kmscon_char **out, size_t size) static int new_char(struct kmscon_char **out, size_t size)
{ {
@ -322,34 +324,6 @@ int kmscon_char_append_u8(struct kmscon_char *ch, const char *str, size_t len)
return 0; return 0;
} }
/*
* Create a hash for a kmscon_char. This uses a simple hash technique described
* by Daniel J. Bernstein.
*/
static guint kmscon_char_hash(gconstpointer key)
{
guint val = 5381;
size_t i;
const struct kmscon_char *ch = (void*)key;
for (i = 0; i < ch->len; ++i)
val = val * 33 + ch->buf[i];
return val;
}
/* compare two kmscon_char for equality */
static gboolean kmscon_char_equal(gconstpointer a, gconstpointer b)
{
const struct kmscon_char *ch1 = (void*)a;
const struct kmscon_char *ch2 = (void*)b;
if (ch1->len != ch2->len)
return FALSE;
return (memcmp(ch1->buf, ch2->buf, ch1->len) == 0);
}
/* /*
* Glyphs * Glyphs
* Glyphs are for internal use only! The outside world uses kmscon_char * Glyphs are for internal use only! The outside world uses kmscon_char
@ -369,31 +343,24 @@ static gboolean kmscon_char_equal(gconstpointer a, gconstpointer b)
* any text you want. It uses a PangoLayout internally and recalculates the * any text you want. It uses a PangoLayout internally and recalculates the
* character sizes each time we draw them. * character sizes each time we draw them.
*/ */
static int kmscon_glyph_new(struct kmscon_glyph **out, static int kmscon_glyph_new(struct kmscon_glyph **out, kmscon_symbol_t ch)
const struct kmscon_char *ch)
{ {
struct kmscon_glyph *glyph; struct kmscon_glyph *glyph;
int ret;
if (!out || !ch || !ch->len) if (!out)
return -EINVAL; return -EINVAL;
glyph = malloc(sizeof(*glyph)); glyph = malloc(sizeof(*glyph));
if (!glyph) if (!glyph)
return -ENOMEM; return -ENOMEM;
memset(glyph, 0, sizeof(*glyph));
glyph->ref = 1; glyph->ref = 1;
glyph->type = GLYPH_NONE; glyph->type = GLYPH_NONE;
glyph->ch = ch;
ret = kmscon_char_dup(&glyph->ch, ch);
if (ret)
goto err_free;
*out = glyph; *out = glyph;
return 0; return 0;
err_free:
free(glyph);
return ret;
} }
/* /*
@ -436,7 +403,6 @@ static void kmscon_glyph_unref(struct kmscon_glyph *glyph)
return; return;
kmscon_glyph_reset(glyph); kmscon_glyph_reset(glyph);
kmscon_char_free(glyph->ch);
free(glyph); free(glyph);
} }
@ -454,6 +420,8 @@ static int kmscon_glyph_set(struct kmscon_glyph *glyph,
PangoGlyphItem *tmp; PangoGlyphItem *tmp;
PangoGlyphString *str; PangoGlyphString *str;
PangoRectangle rec; PangoRectangle rec;
size_t len;
const char *val;
if (!glyph || !font) if (!glyph || !font)
return -EINVAL; return -EINVAL;
@ -462,7 +430,10 @@ static int kmscon_glyph_set(struct kmscon_glyph *glyph,
if (!layout) if (!layout)
return -EINVAL; return -EINVAL;
pango_layout_set_text(layout, glyph->ch->buf, glyph->ch->len); val = kmscon_symbol_get_u8(font->st, glyph->ch, &len);
pango_layout_set_text(layout, val, len);
kmscon_symbol_free_u8(val);
pango_layout_get_extents(layout, NULL, &rec); pango_layout_get_extents(layout, NULL, &rec);
line = pango_layout_get_line_readonly(layout, 0); line = pango_layout_get_line_readonly(layout, 0);
@ -505,23 +476,15 @@ static int measure_width(struct kmscon_font *font)
{ {
unsigned int i, num, width; unsigned int i, num, width;
int ret; int ret;
struct kmscon_char *ch; kmscon_symbol_t ch;
char buf;
struct kmscon_glyph *glyph; struct kmscon_glyph *glyph;
if (!font) if (!font)
return -EINVAL; return -EINVAL;
ret = kmscon_char_new(&ch);
if (ret)
return ret;
num = 0; num = 0;
for (i = 0; i < 127; ++i) { for (i = 0; i < 127; ++i) {
buf = i; ch = kmscon_symbol_make(i);
ret = kmscon_char_set_u8(ch, &buf, 1);
if (ret)
continue;
ret = kmscon_font_lookup(font, ch, &glyph); ret = kmscon_font_lookup(font, ch, &glyph);
if (ret) if (ret)
@ -535,8 +498,6 @@ static int measure_width(struct kmscon_font *font)
kmscon_glyph_unref(glyph); kmscon_glyph_unref(glyph);
} }
kmscon_char_free(ch);
if (!num) if (!num)
return -EFAULT; return -EFAULT;
@ -551,7 +512,8 @@ static int measure_width(struct kmscon_font *font)
* \height is the height in pixel that we have for each character. * \height is the height in pixel that we have for each character.
* Returns 0 on success and stores the new font in \out. * Returns 0 on success and stores the new font in \out.
*/ */
int kmscon_font_new(struct kmscon_font **out, unsigned int height) int kmscon_font_new(struct kmscon_font **out, unsigned int height,
struct kmscon_symbol_table *st)
{ {
struct kmscon_font *font; struct kmscon_font *font;
int ret; int ret;
@ -570,6 +532,7 @@ int kmscon_font_new(struct kmscon_font **out, unsigned int height)
return -ENOMEM; return -ENOMEM;
font->ref = 1; font->ref = 1;
font->height = height; font->height = height;
font->st = st;
map = pango_cairo_font_map_get_default(); map = pango_cairo_font_map_get_default();
if (!map) { if (!map) {
@ -614,9 +577,8 @@ int kmscon_font_new(struct kmscon_font **out, unsigned int height)
cairo_font_options_destroy(opt); cairo_font_options_destroy(opt);
} }
font->glyphs = g_hash_table_new_full(kmscon_char_hash, font->glyphs = g_hash_table_new_full(g_direct_hash, g_direct_equal,
kmscon_char_equal, (GDestroyNotify) kmscon_char_free, NULL, (GDestroyNotify) kmscon_glyph_unref);
(GDestroyNotify) kmscon_glyph_unref);
if (!font->glyphs) { if (!font->glyphs) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_ctx; goto err_ctx;
@ -626,7 +588,9 @@ int kmscon_font_new(struct kmscon_font **out, unsigned int height)
if (ret) if (ret)
goto err_hash; goto err_hash;
kmscon_symbol_table_ref(font->st);
*out = font; *out = font;
return 0; return 0;
err_hash: err_hash:
@ -656,6 +620,7 @@ void kmscon_font_unref(struct kmscon_font *font)
g_hash_table_unref(font->glyphs); g_hash_table_unref(font->glyphs);
g_object_unref(font->ctx); g_object_unref(font->ctx);
kmscon_symbol_table_unref(font->st);
free(font); free(font);
log_debug("font: destroying font\n"); log_debug("font: destroying font\n");
} }
@ -682,30 +647,25 @@ unsigned int kmscon_font_get_height(struct kmscon_font *font)
* Returns 0 on success and stores the glyph with a new reference in \out. * Returns 0 on success and stores the glyph with a new reference in \out.
*/ */
static int kmscon_font_lookup(struct kmscon_font *font, static int kmscon_font_lookup(struct kmscon_font *font,
const struct kmscon_char *key, struct kmscon_glyph **out) kmscon_symbol_t key, struct kmscon_glyph **out)
{ {
struct kmscon_glyph *glyph; struct kmscon_glyph *glyph;
struct kmscon_char *ch;
int ret; int ret;
if (!font || !key || !out) if (!font || !out)
return -EINVAL; return -EINVAL;
glyph = g_hash_table_lookup(font->glyphs, key); glyph = g_hash_table_lookup(font->glyphs, GUINT_TO_POINTER(key));
if (!glyph) { if (!glyph) {
ret = kmscon_char_dup(&ch, key);
if (ret)
return ret;
ret = kmscon_glyph_new(&glyph, key); ret = kmscon_glyph_new(&glyph, key);
if (ret) if (ret)
goto err_char; return ret;
ret = kmscon_glyph_set(glyph, font); ret = kmscon_glyph_set(glyph, font);
if (ret) if (ret)
goto err_glyph; goto err_glyph;
g_hash_table_insert(font->glyphs, ch, glyph); g_hash_table_insert(font->glyphs, GUINT_TO_POINTER(key), glyph);
} }
kmscon_glyph_ref(glyph); kmscon_glyph_ref(glyph);
@ -714,8 +674,6 @@ static int kmscon_font_lookup(struct kmscon_font *font,
err_glyph: err_glyph:
kmscon_glyph_unref(glyph); kmscon_glyph_unref(glyph);
err_char:
kmscon_char_free(ch);
return ret; return ret;
} }
@ -724,8 +682,8 @@ err_char:
* The glyph will be drawn with the upper-left corner at x/y. * The glyph will be drawn with the upper-left corner at x/y.
* Returns 0 on success. * Returns 0 on success.
*/ */
int kmscon_font_draw(struct kmscon_font *font, const struct kmscon_char *ch, int kmscon_font_draw(struct kmscon_font *font, kmscon_symbol_t ch, void *dcr,
void *dcr, uint32_t x, uint32_t y) uint32_t x, uint32_t y)
{ {
struct kmscon_glyph *glyph; struct kmscon_glyph *glyph;
int ret; int ret;

View File

@ -40,6 +40,7 @@
#include "eloop.h" #include "eloop.h"
#include "log.h" #include "log.h"
#include "terminal.h" #include "terminal.h"
#include "unicode.h"
#include "vte.h" #include "vte.h"
struct term_out { struct term_out {
@ -105,24 +106,18 @@ static const char help_text[] =
static void print_help(struct kmscon_terminal *term) static void print_help(struct kmscon_terminal *term)
{ {
int ret;
unsigned int i, len; unsigned int i, len;
struct kmscon_char *ch; kmscon_symbol_t ch;
ret = kmscon_char_new_u8(&ch, NULL, 0);
if (ret)
return;
len = sizeof(help_text) - 1; len = sizeof(help_text) - 1;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
kmscon_char_set_u8(ch, &help_text[i], 1); ch = kmscon_symbol_make(help_text[i]);
kmscon_terminal_input(term, ch); kmscon_terminal_input(term, ch);
} }
kmscon_char_free(ch);
} }
int kmscon_terminal_new(struct kmscon_terminal **out) int kmscon_terminal_new(struct kmscon_terminal **out,
struct kmscon_symbol_table *st)
{ {
struct kmscon_terminal *term; struct kmscon_terminal *term;
int ret; int ret;
@ -143,7 +138,7 @@ int kmscon_terminal_new(struct kmscon_terminal **out)
if (ret) if (ret)
goto err_free; goto err_free;
ret = kmscon_console_new(&term->console); ret = kmscon_console_new(&term->console, st);
if (ret) if (ret)
goto err_idle; goto err_idle;
@ -265,8 +260,7 @@ void kmscon_terminal_rm_all_outputs(struct kmscon_terminal *term)
} }
} }
void kmscon_terminal_input(struct kmscon_terminal *term, void kmscon_terminal_input(struct kmscon_terminal *term, kmscon_symbol_t ch)
const struct kmscon_char *ch)
{ {
kmscon_vte_input(term->vte, ch); kmscon_vte_input(term->vte, ch);
schedule_redraw(term); schedule_redraw(term);

View File

@ -36,10 +36,12 @@
#include <stdlib.h> #include <stdlib.h>
#include "console.h" #include "console.h"
#include "output.h" #include "output.h"
#include "unicode.h"
struct kmscon_terminal; struct kmscon_terminal;
int kmscon_terminal_new(struct kmscon_terminal **out); int kmscon_terminal_new(struct kmscon_terminal **out,
struct kmscon_symbol_table *st);
void kmscon_terminal_ref(struct kmscon_terminal *term); void kmscon_terminal_ref(struct kmscon_terminal *term);
void kmscon_terminal_unref(struct kmscon_terminal *term); void kmscon_terminal_unref(struct kmscon_terminal *term);
@ -51,7 +53,6 @@ int kmscon_terminal_add_output(struct kmscon_terminal *term,
struct kmscon_output *output); struct kmscon_output *output);
void kmscon_terminal_rm_all_outputs(struct kmscon_terminal *term); void kmscon_terminal_rm_all_outputs(struct kmscon_terminal *term);
void kmscon_terminal_input(struct kmscon_terminal *term, void kmscon_terminal_input(struct kmscon_terminal *term, kmscon_symbol_t ch);
const struct kmscon_char *ch);
#endif /* KMSCON_TERMINAL_H */ #endif /* KMSCON_TERMINAL_H */

View File

@ -36,6 +36,7 @@
#include "console.h" #include "console.h"
#include "log.h" #include "log.h"
#include "unicode.h"
#include "vte.h" #include "vte.h"
struct kmscon_vte { struct kmscon_vte {
@ -94,28 +95,13 @@ void kmscon_vte_bind(struct kmscon_vte *vte, struct kmscon_console *con)
kmscon_console_ref(vte->con); kmscon_console_ref(vte->con);
} }
void kmscon_vte_input(struct kmscon_vte *vte, const struct kmscon_char *ch) void kmscon_vte_input(struct kmscon_vte *vte, kmscon_symbol_t ch)
{ {
size_t len;
const char *val;
if (!vte || !vte->con) if (!vte || !vte->con)
return; return;
len = kmscon_char_get_len(ch); if (ch == '\n')
val = kmscon_char_get_u8(ch);
if (len == 1) {
if (*val == '\n')
kmscon_console_newline(vte->con); kmscon_console_newline(vte->con);
else else
goto write_default;
} else {
goto write_default;
}
return;
write_default:
kmscon_console_write(vte->con, ch); kmscon_console_write(vte->con, ch);
} }

View File

@ -35,6 +35,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "console.h" #include "console.h"
#include "unicode.h"
struct kmscon_vte; struct kmscon_vte;
@ -43,6 +44,6 @@ void kmscon_vte_ref(struct kmscon_vte *vte);
void kmscon_vte_unref(struct kmscon_vte *vte); void kmscon_vte_unref(struct kmscon_vte *vte);
void kmscon_vte_bind(struct kmscon_vte *vte, struct kmscon_console *con); void kmscon_vte_bind(struct kmscon_vte *vte, struct kmscon_console *con);
void kmscon_vte_input(struct kmscon_vte *vte, const struct kmscon_char *ch); void kmscon_vte_input(struct kmscon_vte *vte, kmscon_symbol_t ch);
#endif /* KMSCON_VTE_H */ #endif /* KMSCON_VTE_H */

View File

@ -45,15 +45,7 @@
static void print_buf(struct kmscon_buffer *buf) static void print_buf(struct kmscon_buffer *buf)
{ {
unsigned int width, height, x, y; unsigned int width, height, x, y;
struct kmscon_char *ch; kmscon_symbol_t ch;
int ret;
const char *c;
ret = kmscon_char_new(&ch);
if (ret) {
log_warning("Cannot allocate character\n");
return;
}
width = kmscon_buffer_get_width(buf); width = kmscon_buffer_get_width(buf);
height = kmscon_buffer_get_height(buf); height = kmscon_buffer_get_height(buf);
@ -68,9 +60,8 @@ static void print_buf(struct kmscon_buffer *buf)
for (y = 0; y < height; ++y) { for (y = 0; y < height; ++y) {
printf("x"); printf("x");
for (x = 0; x < width; ++x) { for (x = 0; x < width; ++x) {
kmscon_buffer_read(buf, x, y, ch); ch = kmscon_buffer_read(buf, x, y);
c = kmscon_char_get_u8(ch); printf("%c", ch ? ch : ' ');
printf("%c", c ? *c : ' ');
} }
printf("x\n"); printf("x\n");
} }
@ -79,22 +70,15 @@ static void print_buf(struct kmscon_buffer *buf)
for (x = 0; x < width; ++x) for (x = 0; x < width; ++x)
printf("x"); printf("x");
printf("x\n"); printf("x\n");
kmscon_char_free(ch);
} }
static void test1(struct kmscon_buffer *buf) static void test1(struct kmscon_buffer *buf)
{ {
int ret; kmscon_symbol_t ch;
struct kmscon_char *ch;
log_info("Test1:\n"); log_info("Test1:\n");
ret = kmscon_char_new_u8(&ch, "?", 1); ch = kmscon_symbol_make('?');
if (ret) {
log_err("Cannot create character\n");
return;
}
kmscon_buffer_write(buf, 0, 0, ch); kmscon_buffer_write(buf, 0, 0, ch);
kmscon_buffer_write(buf, 9, 2, ch); kmscon_buffer_write(buf, 9, 2, ch);
@ -109,8 +93,6 @@ static void test1(struct kmscon_buffer *buf)
print_buf(buf); print_buf(buf);
kmscon_buffer_rotate(buf); kmscon_buffer_rotate(buf);
print_buf(buf); print_buf(buf);
kmscon_char_free(ch);
} }
static void test2() static void test2()

View File

@ -57,6 +57,7 @@
#include "eloop.h" #include "eloop.h"
#include "log.h" #include "log.h"
#include "output.h" #include "output.h"
#include "unicode.h"
#include "vt.h" #include "vt.h"
static volatile sig_atomic_t terminate; static volatile sig_atomic_t terminate;
@ -66,6 +67,7 @@ struct console {
struct kmscon_signal *sig_term; struct kmscon_signal *sig_term;
struct kmscon_signal *sig_int; struct kmscon_signal *sig_int;
struct kmscon_fd *stdin_fd; struct kmscon_fd *stdin_fd;
struct kmscon_symbol_table *st;
struct kmscon_compositor *comp; struct kmscon_compositor *comp;
struct kmscon_vt *vt; struct kmscon_vt *vt;
struct kmscon_console *con; struct kmscon_console *con;
@ -81,7 +83,7 @@ static void stdin_cb(struct kmscon_fd *fd, int mask, void *data)
char buf[512]; char buf[512];
int ret; int ret;
unsigned int i, len; unsigned int i, len;
struct kmscon_char *ch; kmscon_symbol_t ch;
if (!con || !fd) if (!con || !fd)
return; return;
@ -96,20 +98,14 @@ static void stdin_cb(struct kmscon_fd *fd, int mask, void *data)
len = ret; len = ret;
log_debug("stdin input read (len: %d)\n", len); log_debug("stdin input read (len: %d)\n", len);
ret = kmscon_char_new_u8(&ch, NULL, 0);
if (ret)
return;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (buf[i] == '\n') { if (buf[i] == '\n') {
kmscon_console_newline(con->con); kmscon_console_newline(con->con);
} else { } else {
kmscon_char_set_u8(ch, &buf[i], 1); ch = buf[i];
kmscon_console_write(con->con, ch); kmscon_console_write(con->con, ch);
} }
} }
kmscon_char_free(ch);
} }
} }
@ -222,25 +218,18 @@ static const char help_text[] =
static void print_help(struct console *con) static void print_help(struct console *con)
{ {
int ret;
unsigned int i, len; unsigned int i, len;
struct kmscon_char *ch; kmscon_symbol_t ch;
ret = kmscon_char_new_u8(&ch, NULL, 0);
if (ret)
return;
len = sizeof(help_text) - 1; len = sizeof(help_text) - 1;
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
if (help_text[i] == '\n') { if (help_text[i] == '\n') {
kmscon_console_newline(con->con); kmscon_console_newline(con->con);
} else { } else {
kmscon_char_set_u8(ch, &help_text[i], 1); ch = help_text[i];
kmscon_console_write(con->con, ch); kmscon_console_write(con->con, ch);
} }
} }
kmscon_char_free(ch);
} }
static void destroy_eloop(struct console *con) static void destroy_eloop(struct console *con)
@ -250,6 +239,7 @@ static void destroy_eloop(struct console *con)
kmscon_console_unref(con->con); kmscon_console_unref(con->con);
kmscon_compositor_unref(con->comp); kmscon_compositor_unref(con->comp);
kmscon_vt_unref(con->vt); kmscon_vt_unref(con->vt);
kmscon_symbol_table_unref(con->st);
kmscon_eloop_rm_fd(con->stdin_fd); kmscon_eloop_rm_fd(con->stdin_fd);
kmscon_eloop_rm_signal(con->sig_int); kmscon_eloop_rm_signal(con->sig_int);
kmscon_eloop_rm_signal(con->sig_term); kmscon_eloop_rm_signal(con->sig_term);
@ -279,6 +269,10 @@ static int setup_eloop(struct console *con)
if (ret) if (ret)
goto err_loop; goto err_loop;
ret = kmscon_symbol_table_new(&con->st);
if (ret)
goto err_loop;
ret = kmscon_compositor_new(&con->comp); ret = kmscon_compositor_new(&con->comp);
if (ret) if (ret)
goto err_loop; goto err_loop;
@ -295,7 +289,7 @@ static int setup_eloop(struct console *con)
if (ret) if (ret)
goto err_loop; goto err_loop;
ret = kmscon_console_new(&con->con); ret = kmscon_console_new(&con->con, con->st);
if (ret) if (ret)
goto err_loop; goto err_loop;

View File

@ -45,10 +45,10 @@
#include "vt.h" #include "vt.h"
struct app { struct app {
struct kmscon_char *ch;
struct kmscon_eloop *eloop; struct kmscon_eloop *eloop;
struct kmscon_signal *sig_term; struct kmscon_signal *sig_term;
struct kmscon_signal *sig_int; struct kmscon_signal *sig_int;
struct kmscon_symbol_table *st;
struct kmscon_compositor *comp; struct kmscon_compositor *comp;
struct kmscon_input *input; struct kmscon_input *input;
struct kmscon_vt *vt; struct kmscon_vt *vt;
@ -66,18 +66,13 @@ static void read_input(struct kmscon_input *input,
struct kmscon_input_event *ev, void *data) struct kmscon_input_event *ev, void *data)
{ {
struct app *app = data; struct app *app = data;
int ret; kmscon_symbol_t ch;
if (ev->unicode == KMSCON_INPUT_INVALID) if (ev->unicode == KMSCON_INPUT_INVALID)
return; return;
ret = kmscon_char_set_ucs4(app->ch, &ev->unicode, 1); ch = kmscon_symbol_make(ev->unicode);
if (ret) { kmscon_terminal_input(app->term, ch);
log_warning("Cannot convert UCS4 to UTF8\n");
return;
}
kmscon_terminal_input(app->term, app->ch);
} }
static void activate_outputs(struct app *app) static void activate_outputs(struct app *app)
@ -133,20 +128,16 @@ static void destroy_app(struct app *app)
kmscon_vt_unref(app->vt); kmscon_vt_unref(app->vt);
kmscon_input_unref(app->input); kmscon_input_unref(app->input);
kmscon_compositor_unref(app->comp); kmscon_compositor_unref(app->comp);
kmscon_symbol_table_unref(app->st);
kmscon_eloop_rm_signal(app->sig_int); kmscon_eloop_rm_signal(app->sig_int);
kmscon_eloop_rm_signal(app->sig_term); kmscon_eloop_rm_signal(app->sig_term);
kmscon_eloop_unref(app->eloop); kmscon_eloop_unref(app->eloop);
kmscon_char_free(app->ch);
} }
static int setup_app(struct app *app) static int setup_app(struct app *app)
{ {
int ret; int ret;
ret = kmscon_char_new(&app->ch);
if (ret)
goto err_loop;
ret = kmscon_eloop_new(&app->eloop); ret = kmscon_eloop_new(&app->eloop);
if (ret) if (ret)
goto err_loop; goto err_loop;
@ -161,6 +152,10 @@ static int setup_app(struct app *app)
if (ret) if (ret)
goto err_loop; goto err_loop;
ret = kmscon_symbol_table_new(&app->st);
if (ret)
goto err_loop;
ret = kmscon_compositor_new(&app->comp); ret = kmscon_compositor_new(&app->comp);
if (ret) if (ret)
goto err_loop; goto err_loop;
@ -181,7 +176,7 @@ static int setup_app(struct app *app)
if (ret) if (ret)
goto err_loop; goto err_loop;
ret = kmscon_terminal_new(&app->term); ret = kmscon_terminal_new(&app->term, app->st);
if (ret) if (ret)
goto err_loop; goto err_loop;