From 9e67b6585ec42d16fd73a4f2df372f54f1188a32 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Thu, 16 Jul 2015 13:05:48 +0200 Subject: [PATCH 1/3] make arrays one dimensional With more dimensional arrays we have to define the array size. Use one dimensional arrays to be more flexible. Additionally this allows to shrink array size for ASCII. --- Meter.c | 61 ++++++++++++++++++++++++++++++--------------------------- Meter.h | 3 +++ 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/Meter.c b/Meter.c index 6e56e36a..af863ae5 100644 --- a/Meter.c +++ b/Meter.c @@ -308,23 +308,24 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { /* ---------- GraphMeterMode ---------- */ -static const char* GraphMeterMode_dotsUtf8[5][5] = { - { /*00*/"⠀", /*01*/"⢀", /*02*/"⢠", /*03*/"⢰", /*04*/ "⢸" }, - { /*10*/"⡀", /*11*/"⣀", /*12*/"⣠", /*13*/"⣰", /*14*/ "⣸" }, - { /*20*/"⡄", /*21*/"⣄", /*22*/"⣤", /*23*/"⣴", /*24*/ "⣼" }, - { /*30*/"⡆", /*31*/"⣆", /*32*/"⣦", /*33*/"⣶", /*34*/ "⣾" }, - { /*40*/"⡇", /*41*/"⣇", /*42*/"⣧", /*43*/"⣷", /*44*/ "⣿" }, +#define PIXPERROW_UTF8 4 +static const char* GraphMeterMode_dotsUtf8[] = { + /*00*/"⠀", /*01*/"⢀", /*02*/"⢠", /*03*/"⢰", /*04*/ "⢸", + /*10*/"⡀", /*11*/"⣀", /*12*/"⣠", /*13*/"⣰", /*14*/ "⣸", + /*20*/"⡄", /*21*/"⣄", /*22*/"⣤", /*23*/"⣴", /*24*/ "⣼", + /*30*/"⡆", /*31*/"⣆", /*32*/"⣦", /*33*/"⣶", /*34*/ "⣾", + /*40*/"⡇", /*41*/"⣇", /*42*/"⣧", /*43*/"⣷", /*44*/ "⣿" }; -static const char* GraphMeterMode_dotsAscii[5][5] = { - { /*00*/" ", /*01*/".", /*02*/".", /*03*/":", /*04*/ ":" }, - { /*10*/".", /*11*/".", /*12*/".", /*13*/":", /*14*/ ":" }, - { /*20*/".", /*21*/".", /*22*/".", /*23*/":", /*24*/ ":" }, - { /*30*/":", /*31*/":", /*32*/":", /*33*/":", /*34*/ ":" }, - { /*40*/":", /*41*/":", /*42*/":", /*43*/":", /*44*/ ":" }, +#define PIXPERROW_ASCII 2 +static const char* GraphMeterMode_dotsAscii[] = { + /*00*/" ", /*01*/".", /*02*/":", + /*10*/".", /*11*/".", /*12*/":", + /*20*/":", /*21*/":", /*22*/":" }; -static const char* (*GraphMeterMode_dots)[5]; +static const char** GraphMeterMode_dots; +static int pixperrow; static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { @@ -334,8 +335,10 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { if (CRT_utf8) { GraphMeterMode_dots = GraphMeterMode_dotsUtf8; + pixperrow = PIXPERROW_UTF8; } else { GraphMeterMode_dots = GraphMeterMode_dotsAscii; + pixperrow = PIXPERROW_ASCII; } attrset(CRT_colors[METER_TEXT]); @@ -365,24 +368,24 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { } for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) { - const double dot = (1.0 / 16); + const double dot = (1.0 / (pixperrow * 4)); int v1 = data->values[i] / dot; int v2 = data->values[i+1] / dot; if (v1 == 0) v1 = 1; if (v2 == 0) v2 = 1; - int level = 12; + int level = pixperrow * 3; int colorIdx = GRAPH_1; for (int line = 0; line < 4; line++) { - int line1 = MIN(4, MAX(0, v1 - level)); - int line2 = MIN(4, MAX(0, v2 - level)); + int line1 = MIN(pixperrow, MAX(0, v1 - level)); + int line2 = MIN(pixperrow, MAX(0, v2 - level)); attrset(CRT_colors[colorIdx]); - mvaddstr(y+line, x+k, GraphMeterMode_dots[line1][line2]); + mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (pixperrow + 1) + line2]); colorIdx = GRAPH_2; - level -= 4; + level -= pixperrow; } } attrset(CRT_colors[RESET_COLOR]); @@ -390,23 +393,23 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { /* ---------- LEDMeterMode ---------- */ -static const char* LEDMeterMode_digitsAscii[3][10] = { - { " __ "," "," __ "," __ "," "," __ "," __ "," __ "," __ "," __ "}, - { "| |"," |"," __|"," __|","|__|","|__ ","|__ "," |","|__|","|__|"}, - { "|__|"," |","|__ "," __|"," |"," __|","|__|"," |","|__|"," __|"}, +static const char* LEDMeterMode_digitsAscii[] = { + " __ "," "," __ "," __ "," "," __ "," __ "," __ "," __ "," __ ", + "| |"," |"," __|"," __|","|__|","|__ ","|__ "," |","|__|","|__|", + "|__|"," |","|__ "," __|"," |"," __|","|__|"," |","|__|"," __|" }; -static const char* LEDMeterMode_digitsUtf8[3][10] = { - { "┌──┐"," ┐ ","╶──┐","╶──┐","╷ ╷","┌──╴","┌──╴","╶──┐","┌──┐","┌──┐"}, - { "│ │"," │ ","┌──┘"," ──┤","└──┤","└──┐","├──┐"," │","├──┤","└──┤"}, - { "└──┘"," ╵ ","└──╴","╶──┘"," ╵","╶──┘","└──┘"," ╵","└──┘"," ──┘"}, +static const char* LEDMeterMode_digitsUtf8[] = { + "┌──┐"," ┐ ","╶──┐","╶──┐","╷ ╷","┌──╴","┌──╴","╶──┐","┌──┐","┌──┐", + "│ │"," │ ","┌──┘"," ──┤","└──┤","└──┐","├──┐"," │","├──┤","└──┤", + "└──┘"," ╵ ","└──╴","╶──┘"," ╵","╶──┘","└──┘"," ╵","└──┘"," ──┘" }; -static const char* (*LEDMeterMode_digits)[10]; +static const char** LEDMeterMode_digits; static void LEDMeterMode_drawDigit(int x, int y, int n) { for (int i = 0; i < 3; i++) - mvaddstr(y+i, x, LEDMeterMode_digits[i][n]); + mvaddstr(y+i, x, LEDMeterMode_digits[i * 10 + n]); } static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { diff --git a/Meter.h b/Meter.h index 3c9bd880..63e32955 100644 --- a/Meter.h +++ b/Meter.h @@ -119,6 +119,9 @@ ListItem* Meter_toListItem(Meter* this, bool moving); /* ---------- GraphMeterMode ---------- */ +#define PIXPERROW_UTF8 4 +#define PIXPERROW_ASCII 2 + /* ---------- LEDMeterMode ---------- */ extern MeterMode* Meter_modes[]; From d8e23bb0847d766d7f3fdf8bdd11f7d70829fad8 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 17 Jul 2015 14:33:34 +0200 Subject: [PATCH 2/3] remove UTF-8 code when compiling with --disable-unicode --- CRT.c | 14 +++++++++++--- CRT.h | 8 ++++++-- ListItem.c | 6 +++++- Meter.c | 26 +++++++++++++++++++++----- Meter.h | 8 ++++++++ 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/CRT.c b/CRT.c index b44a86b6..3c0829a1 100644 --- a/CRT.c +++ b/CRT.c @@ -133,6 +133,8 @@ const char *CRT_treeStrAscii[TREE_STR_COUNT] = { "-", // TREE_STR_SHUT }; +#ifdef HAVE_LIBNCURSESW + const char *CRT_treeStrUtf8[TREE_STR_COUNT] = { "\xe2\x94\x80", // TREE_STR_HORZ ─ "\xe2\x94\x82", // TREE_STR_VERT │ @@ -143,14 +145,16 @@ const char *CRT_treeStrUtf8[TREE_STR_COUNT] = { "\xe2\x94\x80", // TREE_STR_SHUT ─ }; +bool CRT_utf8 = false; + +#endif + const char **CRT_treeStr = CRT_treeStrAscii; static bool CRT_hasColors; int CRT_delay = 0; -bool CRT_utf8 = false; - int* CRT_colors; int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT] = { @@ -596,7 +600,11 @@ void CRT_init(int delay, int colorScheme) { CRT_utf8 = false; #endif - CRT_treeStr = CRT_utf8 ? CRT_treeStrUtf8 : CRT_treeStrAscii; + CRT_treeStr = +#ifdef HAVE_LIBNCURSESW + CRT_utf8 ? CRT_treeStrUtf8 : +#endif + CRT_treeStrAscii; mousemask(BUTTON1_CLICKED, NULL); } diff --git a/CRT.h b/CRT.h index 3e8babe7..074cff36 100644 --- a/CRT.h +++ b/CRT.h @@ -114,14 +114,18 @@ void CRT_handleSIGSEGV(int sgn); extern const char *CRT_treeStrAscii[TREE_STR_COUNT]; +#ifdef HAVE_LIBNCURSESW + extern const char *CRT_treeStrUtf8[TREE_STR_COUNT]; +extern bool CRT_utf8; + +#endif + extern const char **CRT_treeStr; extern int CRT_delay; -extern bool CRT_utf8; - int* CRT_colors; extern int CRT_colorSchemes[LAST_COLORSCHEME][LAST_COLORELEMENT]; diff --git a/ListItem.c b/ListItem.c index 68fd9ffe..07788e73 100644 --- a/ListItem.c +++ b/ListItem.c @@ -42,7 +42,11 @@ static void ListItem_display(Object* cast, RichString* out) { snprintf(buffer, len, "%s", this->value); */ if (this->moving) { - RichString_write(out, CRT_colors[DEFAULT_COLOR], CRT_utf8 ? "↕ " : "+ "); + RichString_write(out, CRT_colors[DEFAULT_COLOR], +#ifdef HAVE_LIBNCURSESW + CRT_utf8 ? "↕ " : +#endif + "+ "); } else { RichString_prune(out); } diff --git a/Meter.c b/Meter.c index af863ae5..f7ad61aa 100644 --- a/Meter.c +++ b/Meter.c @@ -308,6 +308,8 @@ static void BarMeterMode_draw(Meter* this, int x, int y, int w) { /* ---------- GraphMeterMode ---------- */ +#ifdef HAVE_LIBNCURSESW + #define PIXPERROW_UTF8 4 static const char* GraphMeterMode_dotsUtf8[] = { /*00*/"⠀", /*01*/"⢀", /*02*/"⢠", /*03*/"⢰", /*04*/ "⢸", @@ -317,6 +319,8 @@ static const char* GraphMeterMode_dotsUtf8[] = { /*40*/"⡇", /*41*/"⣇", /*42*/"⣧", /*43*/"⣷", /*44*/ "⣿" }; +#endif + #define PIXPERROW_ASCII 2 static const char* GraphMeterMode_dotsAscii[] = { /*00*/" ", /*01*/".", /*02*/":", @@ -333,10 +337,13 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { GraphData* data = (GraphData*) this->drawData; const int nValues = METER_BUFFER_LEN; +#ifdef HAVE_LIBNCURSESW if (CRT_utf8) { GraphMeterMode_dots = GraphMeterMode_dotsUtf8; pixperrow = PIXPERROW_UTF8; - } else { + } else +#endif + { GraphMeterMode_dots = GraphMeterMode_dotsAscii; pixperrow = PIXPERROW_ASCII; } @@ -399,12 +406,16 @@ static const char* LEDMeterMode_digitsAscii[] = { "|__|"," |","|__ "," __|"," |"," __|","|__|"," |","|__|"," __|" }; +#ifdef HAVE_LIBNCURSESW + static const char* LEDMeterMode_digitsUtf8[] = { "┌──┐"," ┐ ","╶──┐","╶──┐","╷ ╷","┌──╴","┌──╴","╶──┐","┌──┐","┌──┐", "│ │"," │ ","┌──┘"," ──┤","└──┤","└──┐","├──┐"," │","├──┤","└──┤", "└──┘"," ╵ ","└──╴","╶──┘"," ╵","╶──┘","└──┘"," ╵","└──┘"," ──┘" }; +#endif + static const char** LEDMeterMode_digits; static void LEDMeterMode_drawDigit(int x, int y, int n) { @@ -415,11 +426,12 @@ static void LEDMeterMode_drawDigit(int x, int y, int n) { static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { (void) w; - if (CRT_utf8) { +#ifdef HAVE_LIBNCURSESW + if (CRT_utf8) LEDMeterMode_digits = LEDMeterMode_digitsUtf8; - } else { + else +#endif LEDMeterMode_digits = LEDMeterMode_digitsAscii; - } char buffer[METER_BUFFER_LEN]; Meter_setValues(this, buffer, METER_BUFFER_LEN - 1); @@ -427,7 +439,11 @@ static void LEDMeterMode_draw(Meter* this, int x, int y, int w) { RichString_begin(out); Meter_displayBuffer(this, buffer, &out); - int yText = CRT_utf8 ? y+1 : y+2; + int yText = +#ifdef HAVE_LIBNCURSESW + CRT_utf8 ? y+1 : +#endif + y+2; attrset(CRT_colors[LED_COLOR]); mvaddstr(yText, x, this->caption); int xx = x + strlen(this->caption); diff --git a/Meter.h b/Meter.h index 63e32955..6b268c3f 100644 --- a/Meter.h +++ b/Meter.h @@ -119,11 +119,19 @@ ListItem* Meter_toListItem(Meter* this, bool moving); /* ---------- GraphMeterMode ---------- */ +#ifdef HAVE_LIBNCURSESW + #define PIXPERROW_UTF8 4 +#endif + #define PIXPERROW_ASCII 2 /* ---------- LEDMeterMode ---------- */ +#ifdef HAVE_LIBNCURSESW + +#endif + extern MeterMode* Meter_modes[]; /* Blank meter */ From a2a34e89a1f56996ba2a58d72726d259766ad7b4 Mon Sep 17 00:00:00 2001 From: Christian Hesse Date: Fri, 14 Aug 2015 16:59:14 +0200 Subject: [PATCH 3/3] simplify Meter loop * Use MIN() and MAX() to make sure values are inside bounds. This should fix an issue where Meters were missing dots at the bottom. * Remove variable 'level' and calculate on the fly. --- Meter.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/Meter.c b/Meter.c index f7ad61aa..ff0f7000 100644 --- a/Meter.c +++ b/Meter.c @@ -376,23 +376,17 @@ static void GraphMeterMode_draw(Meter* this, int x, int y, int w) { for (int i = nValues - (w*2) + 2, k = 0; i < nValues; i+=2, k++) { const double dot = (1.0 / (pixperrow * 4)); - int v1 = data->values[i] / dot; - int v2 = data->values[i+1] / dot; - - if (v1 == 0) v1 = 1; - if (v2 == 0) v2 = 1; - - int level = pixperrow * 3; + int v1 = MIN(pixperrow * 4, MAX(1, data->values[i] / dot)); + int v2 = MIN(pixperrow * 4, MAX(1, data->values[i+1] / dot)); + int colorIdx = GRAPH_1; for (int line = 0; line < 4; line++) { - - int line1 = MIN(pixperrow, MAX(0, v1 - level)); - int line2 = MIN(pixperrow, MAX(0, v2 - level)); - + int line1 = MIN(pixperrow, MAX(0, v1 - (pixperrow * (3 - line)))); + int line2 = MIN(pixperrow, MAX(0, v2 - (pixperrow * (3 - line)))); + attrset(CRT_colors[colorIdx]); mvaddstr(y+line, x+k, GraphMeterMode_dots[line1 * (pixperrow + 1) + line2]); colorIdx = GRAPH_2; - level -= pixperrow; } } attrset(CRT_colors[RESET_COLOR]);