From 8fd6dccaf19b08542d855beb468bbf2ba4af56aa Mon Sep 17 00:00:00 2001 From: ailin-nemui Date: Tue, 17 Mar 2020 00:26:25 +0100 Subject: [PATCH] enable mirc colour processing --- src/fe-common/core/formats.c | 38 ++++++++++++++++------ src/fe-common/core/formats.h | 11 ++++++- src/fe-text/gui-printtext.c | 4 +-- src/fe-text/gui-printtext.h | 1 + src/fe-text/textbuffer-formats.c | 54 +++++++++++++++++++++++++++----- 5 files changed, 87 insertions(+), 21 deletions(-) diff --git a/src/fe-common/core/formats.c b/src/fe-common/core/formats.c index a6fde7ea..a4d3a023 100644 --- a/src/fe-common/core/formats.c +++ b/src/fe-common/core/formats.c @@ -1222,8 +1222,14 @@ char *strip_codes(const char *input) return str; } -/* send a fully parsed text string for GUI to print */ -void format_send_to_gui(TEXT_DEST_REC *dest, const char *text) +/* parse text string into GUI_PRINT_FLAG_* separated pieces and emit them to handler + handler is a SIGNAL_FUNC with the following arguments: + + WINDOW_REC *window, void *fgcolor_int, void *bgcolor_int, + void *flags_int, const char *textpiece, TEXT_DEST_REC *dest + + */ +void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC handler) { THEME_REC *theme; char *dup, *str, *ptr, type; @@ -1238,8 +1244,8 @@ void format_send_to_gui(TEXT_DEST_REC *dest, const char *text) if (*str == '\0') { /* empty line, write line info only */ - signal_emit_id(signal_gui_print_text, 6, dest->window, GINT_TO_POINTER(fgcolor), - GINT_TO_POINTER(bgcolor), GINT_TO_POINTER(flags), str, dest); + handler(dest->window, GINT_TO_POINTER(fgcolor), GINT_TO_POINTER(bgcolor), + GINT_TO_POINTER(flags), str, dest); } while (*str != '\0') { @@ -1259,16 +1265,14 @@ void format_send_to_gui(TEXT_DEST_REC *dest, const char *text) if (*str != '\0' || (flags & GUI_PRINT_FLAG_CLRTOEOL)) { /* send the text to gui handler */ - signal_emit_id(signal_gui_print_text, 6, dest->window, - GINT_TO_POINTER(fgcolor), - GINT_TO_POINTER(bgcolor), - GINT_TO_POINTER(flags), str, - dest); + handler(dest->window, GINT_TO_POINTER(fgcolor), GINT_TO_POINTER(bgcolor), + GINT_TO_POINTER(flags), str, dest); flags &= ~(GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_CLRTOEOL); } if (type == '\n') { - format_newline(dest); + handler(dest->window, GINT_TO_POINTER(-1), GINT_TO_POINTER(-1), + GINT_TO_POINTER(GUI_PRINT_FLAG_NEWLINE), "", dest); fgcolor = theme->default_color; bgcolor = -1; flags &= GUI_PRINT_FLAG_INDENT|GUI_PRINT_FLAG_MONOSPACE; @@ -1414,6 +1418,20 @@ void format_send_to_gui(TEXT_DEST_REC *dest, const char *text) g_free(dup); } +inline static void gui_print_text_emitter(WINDOW_REC *window, void *fgcolor_int, void *bgcolor_int, + void *flags_int, const char *textpiece, + TEXT_DEST_REC *dest) +{ + signal_emit_id(signal_gui_print_text, 6, window, fgcolor_int, bgcolor_int, flags_int, + textpiece, dest); +} + +/* send a fully parsed text string for GUI to print */ +void format_send_to_gui(TEXT_DEST_REC *dest, const char *text) +{ + format_send_as_gui_flags(dest, text, (SIGNAL_FUNC) gui_print_text_emitter); +} + void format_gui_flags(GString *out, int *last_fg, int *last_bg, int *last_flags, int fg, int bg, int flags) { diff --git a/src/fe-common/core/formats.h b/src/fe-common/core/formats.h index e30ac290..226348de 100644 --- a/src/fe-common/core/formats.h +++ b/src/fe-common/core/formats.h @@ -1,8 +1,9 @@ #ifndef IRSSI_FE_COMMON_CORE_FORMATS_H #define IRSSI_FE_COMMON_CORE_FORMATS_H -#include +#include #include +#include #define GUI_PRINT_FLAG_BOLD 0x0001 #define GUI_PRINT_FLAG_REVERSE 0x0002 @@ -130,6 +131,14 @@ char *strip_codes(const char *input); /* send a fully parsed text string for GUI to print */ void format_send_to_gui(TEXT_DEST_REC *dest, const char *text); +/* parse text string into GUI_PRINT_FLAG_* separated pieces and emit them to handler + handler is a SIGNAL_FUNC with the following arguments: + + WINDOW_REC *window, void *fgcolor_int, void *bgcolor_int, + void *flags_int, const char *textpiece, TEXT_DEST_REC *dest + + */ +void format_send_as_gui_flags(TEXT_DEST_REC *dest, const char *text, SIGNAL_FUNC handler); #define FORMAT_COLOR_NOCHANGE ('0'-1) /* don't change this, at least hilighting depends this value */ #define FORMAT_COLOR_EXT1 ('0'-2) diff --git a/src/fe-text/gui-printtext.c b/src/fe-text/gui-printtext.c index fe720264..872c73c4 100644 --- a/src/fe-text/gui-printtext.c +++ b/src/fe-text/gui-printtext.c @@ -238,7 +238,7 @@ static void remove_old_lines(TEXT_BUFFER_VIEW_REC *view) } } -static void get_colors(int *flags, int *fg, int *bg, int *attr) +void gui_printtext_get_colors(int *flags, int *fg, int *bg, int *attr) { *attr = 0; if (*flags & GUI_PRINT_FLAG_MIRC_COLOR) { @@ -336,7 +336,7 @@ static void sig_gui_print_text(WINDOW_REC *window, void *fgcolor, flags = GPOINTER_TO_INT(pflags); fg = GPOINTER_TO_INT(fgcolor); bg = GPOINTER_TO_INT(bgcolor); - get_colors(&flags, &fg, &bg, &attr); + gui_printtext_get_colors(&flags, &fg, &bg, &attr); if (window == NULL) { print_text_no_window(flags, fg, bg, attr, str); diff --git a/src/fe-text/gui-printtext.h b/src/fe-text/gui-printtext.h index ba5d6f8f..0acf4d31 100644 --- a/src/fe-text/gui-printtext.h +++ b/src/fe-text/gui-printtext.h @@ -21,5 +21,6 @@ void gui_printtext_internal(int xpos, int ypos, const char *str); void gui_printtext_after(TEXT_DEST_REC *dest, LINE_REC *prev, const char *str); void gui_printtext_after_time(TEXT_DEST_REC *dest, LINE_REC *prev, const char *str, time_t time); void gui_printtext_window_border(int xpos, int ypos); +void gui_printtext_get_colors(int *flags, int *fg, int *bg, int *attr); #endif diff --git a/src/fe-text/textbuffer-formats.c b/src/fe-text/textbuffer-formats.c index e0a5f193..46876e75 100644 --- a/src/fe-text/textbuffer-formats.c +++ b/src/fe-text/textbuffer-formats.c @@ -205,10 +205,47 @@ static void sig_gui_print_text_finished(WINDOW_REC *window) gui->insert_after = insert_after; } +static void parse_colors_collector(const WINDOW_REC *window, const void *fgcolor_int, + const void *bgcolor_int, const void *flags_int, + const char *textpiece, const TEXT_DEST_REC *dest) +{ + int fg, bg, flags, attr; + + flags = GPOINTER_TO_INT(flags_int); + fg = GPOINTER_TO_INT(fgcolor_int); + bg = GPOINTER_TO_INT(bgcolor_int); + gui_printtext_get_colors(&flags, &fg, &bg, &attr); + + if (flags & GUI_PRINT_FLAG_NEWLINE) { + g_string_append_c(color_buf->cur_text, '\n'); + } + format_gui_flags(color_buf->cur_text, &color_buf->last_fg, &color_buf->last_bg, + &color_buf->last_flags, fg, bg, flags); + + g_string_append(color_buf->cur_text, textpiece); +} + +static char *parse_colors(TEXT_DEST_REC *dest, const char *text) +{ + char *tmp; + + if (text == NULL) + return NULL; + + color_buf = textbuffer_create(NULL); + format_send_as_gui_flags(dest, text, (SIGNAL_FUNC) parse_colors_collector); + tmp = g_strdup(color_buf->cur_text->str); + textbuffer_destroy(color_buf); + color_buf = NULL; + + return tmp; +} + char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line) { + TEXT_DEST_REC dest; GUI_WINDOW_REC *gui; - LINE_REC *curr; + char *tmp, *text = NULL; g_return_val_if_fail(buffer != NULL, NULL); g_return_val_if_fail(buffer->window != NULL, NULL); @@ -218,11 +255,11 @@ char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line) return NULL; if (line->info.level & MSGLEVEL_FORMAT && line->info.format != NULL) { - TEXT_DEST_REC dest; + LINE_REC *curr; THEME_REC *theme; int formatnum; TEXT_BUFFER_FORMAT_REC *format_rec; - char *text, *tmp, *str; + char *str; curr = line; line = NULL; @@ -267,17 +304,18 @@ char *textbuffer_line_get_text(TEXT_BUFFER_REC *buffer, LINE_REC *line) dest.flags |= PRINT_FLAG_FORMAT; current_time = (time_t) -1; - return str; } else if (format_rec->format != NULL) { g_free(text); - return NULL; - } else { - return text; + text = NULL; } special_fill_cache(NULL); } else { - return g_strdup(line->info.text); + format_create_dest(&dest, NULL, NULL, line->info.level, buffer->window); + text = g_strdup(line->info.text); } + tmp = parse_colors(&dest, text); + g_free(text); + return tmp; } void textbuffer_formats_init(void)