From 7ecc02bc80558b9cc012196b89e8faff756aed0e Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 26 Jan 2014 13:13:49 +0000 Subject: [PATCH 1/3] utf-8 fixes! Replaced wchar_t with uint16_t. -O2 doesn't increase the binary size that much so let's go for it. --- Makefile | 2 +- bar.c | 29 +++++++++++++++-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 95c9a19..567e918 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC ?= gcc STRIP ?= strip -CFLAGS = -std=c99 -fshort-wchar -Os +CFLAGS = -std=c99 -O2 LDFLAGS = -lxcb XINERAMA ?= 0 ifneq "$(XINERAMA)" "0" diff --git a/bar.c b/bar.c index 3308e4b..6c244fb 100644 --- a/bar.c +++ b/bar.c @@ -101,7 +101,7 @@ xcb_set_fontset (int i) } int -draw_char (screen_t *screen, int x, int align, wchar_t ch) +draw_char (screen_t *screen, int x, int align, uint16_t ch) { int ch_width; @@ -217,32 +217,33 @@ parse (char *text) break; } } else { /* utf-8 -> ucs-2 */ - wchar_t t; + uint8_t *utf = (uint8_t *)p; + uint16_t ucs; - if (!(p[0] & 0x80)) { - t = p[0]; - p += 1; + if (utf[0] < 0x80) { + ucs = utf[0]; + p += 1; } - else if ((p[0] & 0xe0) == 0xc0 && (p[1] & 0xc0) == 0x80) { - t = (p[0] & 0x1f) << 6 | (p[1] & 0x3f); + else if ((utf[0] & 0xe0) == 0xc0) { + ucs = (utf[0] & 0x1f) << 6 | (utf[1] & 0x3f); p += 2; } - else if ((p[0] & 0xf0) == 0xe0 && (p[1] & 0xc0) == 0x80 && (p[2] & 0xc0) == 0x80) { - t = (p[0] & 0xf) << 12 | (p[1] & 0x3f) << 6 | (p[2] & 0x3f); + else if ((utf[0] & 0xf0) == 0xe0) { + ucs = (utf[0] & 0xf) << 12 | (utf[1] & 0x3f) << 6 | (utf[2] & 0x3f); p += 3; } - else { /* ASCII chars > 127 go in the extended latin range */ - t = 0xc200 + p[0]; + else { /* Handle ascii > 0x80 */ + ucs = utf[0]; p += 1; } /* The character is outside the main font charset, use the fallback */ - if (t < fontset[FONT_MAIN].char_min || t > fontset[FONT_MAIN].char_max) + if (ucs < fontset[FONT_MAIN].char_min || ucs > fontset[FONT_MAIN].char_max) xcb_set_fontset (FONT_FALLBACK); else xcb_set_fontset (FONT_MAIN); - pos_x += draw_char (screen, pos_x, align, t); + pos_x += draw_char (screen, pos_x, align, ucs); } } } @@ -299,7 +300,7 @@ enum { }; void -set_ewmh_atoms () +set_ewmh_atoms (void) { const char *atom_names[] = { "_NET_WM_WINDOW_TYPE", From 4e1d114f4e4bba88e8a9728acc32ee097ee9091c Mon Sep 17 00:00:00 2001 From: Bill Kolokithas Date: Tue, 4 Feb 2014 22:28:01 +0200 Subject: [PATCH 2/3] check return value of fgets and exit main loop if EOF is received --- bar.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bar.c b/bar.c index 6c244fb..bea1829 100644 --- a/bar.c +++ b/bar.c @@ -568,7 +568,9 @@ main (int argc, char **argv) else break; /* ...bail out */ } if (pollin[0].revents & POLLIN) { /* New input, process it */ - fgets (input, sizeof(input), stdin); + if (fgets (input, sizeof(input), stdin) == NULL) + break; /* EOF received */ + parse (input); redraw = 1; } From 9cdaa22e0608fe41ff463dcb9d5ddc62238716c6 Mon Sep 17 00:00:00 2001 From: Bill Kolokithas Date: Tue, 4 Feb 2014 22:35:54 +0200 Subject: [PATCH 3/3] styling change: remove uneeded parentheses around dereferenced pointer --- bar.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/bar.c b/bar.c index bea1829..29676b9 100644 --- a/bar.c +++ b/bar.c @@ -155,41 +155,39 @@ parse (char *text) xcb_fill_rect (clear_gc, 0, 0, bar_width, BAR_HEIGHT); for (;;) { - if (*p == '\0') - return; - if (*p == '\n') + if (*p == '\0' || *p == '\n') return; if (*p == '\\' && p++ && *p != '\\' && strchr (control_characters, *p)) { switch (*p++) { case 'f': - xcb_set_fg (isdigit(*p) ? (*p)-'0' : 11); + xcb_set_fg (isdigit(*p) ? *p-'0' : 11); p++; break; case 'b': - xcb_set_bg (isdigit(*p) ? (*p)-'0' : 10); + xcb_set_bg (isdigit(*p) ? *p-'0' : 10); p++; break; case 'u': - xcb_set_ud (isdigit(*p) ? (*p)-'0' : 10); + xcb_set_ud (isdigit(*p) ? *p-'0' : 10); p++; break; #if XINERAMA case 's': - if ((*p) == 'r') { + if (*p == 'r') { screen = &screens[num_screens - 1]; - } else if ((*p) == 'l') { + } else if (*p == 'l') { screen = &screens[0]; - } else if ((*p) == 'n') { + } else if (*p == 'n') { if (screen == &screens[num_screens - 1]) break; screen++; - } else if ((*p) == 'p') { + } else if (*p == 'p') { if (screen == screens) break; screen--; } else if (isdigit(*p)) { - int index = (*p)-'0'; + int index = *p-'0'; if (index < num_screens) { screen = &screens[index]; } else {