From 1abd4c2d5a9d0dc3c9c7891ccd2fb2f4ed6064f7 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 25 Jul 2012 11:05:13 +0200 Subject: [PATCH 1/7] Don't wait for a wm to show up. This makes bar work with XMonad. --- bar.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bar.c b/bar.c index 44a9686..9dd705d 100644 --- a/bar.c +++ b/bar.c @@ -249,12 +249,12 @@ set_ewmh_atoms (xcb_window_t root) reply = xcb_intern_atom_reply (c, cookies[4], NULL); atoms[4] = reply->atom; free (reply); - do { - reply1 = xcb_get_property_reply (c, xcb_get_property (c, 0, root, atoms[4], XCB_ATOM_ATOM, 0, -1), NULL); - } while (!xcb_get_property_value_length (reply1)); - compliance_lvl = 0; + reply1 = xcb_get_property_reply (c, xcb_get_property (c, 0, root, atoms[4], XCB_ATOM_ATOM, 0, -1), NULL); + if (!reply) + return compliance_lvl; + for (xcb_atom_t *a = xcb_get_property_value (reply1); a && a != xcb_get_property_value_end (reply1).data; a++) From a4a3c805e3e6404fd539c024ac56aa769b0920e5 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 25 Jul 2012 16:55:26 +0200 Subject: [PATCH 2/7] Support for non-monospaced fonts. Changed the font definition in the config. --- README.md | 4 ++-- bar.c | 53 ++++++++++++++++++++++++++++++---------------------- config.def.h | 5 ++--- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0f34553..0bb06d4 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ b(ar) a(in't) r(ecursive) 2012 (C) The Lemon Man A lightweight bar based on XCB (yay). Provides foreground/background color -switching along with text alignment (screw you dzen!), nothing less and -nothing more. +switching along with text alignment (screw you dzen!), full utf8 support +and reduced memory footprint. Nothing less and nothing more. Options ------- diff --git a/bar.c b/bar.c index 9dd705d..281e4b1 100644 --- a/bar.c +++ b/bar.c @@ -15,12 +15,12 @@ #define MAX(a,b) ((a > b) ? a : b) typedef struct fontset_item_t { - xcb_font_t xcb_ft; - int height; - int width; - int descent; - unsigned short char_max; - unsigned short char_min; + xcb_font_t xcb_ft; + xcb_query_font_reply_t *info; + xcb_charinfo_t *table; + int avg_height; + unsigned short char_max; + unsigned short char_min; } fontset_item_t; enum { @@ -77,34 +77,43 @@ xcb_set_fontset (int i) int draw_char (int x, int align, wchar_t ch) { + int ch_width; + + ch_width = (ch > sel_font->char_min && ch < sel_font->char_max) ? + sel_font->table[ch - sel_font->char_min].character_width : + 0; + + if (ch_width == 0) + return 0; + switch (align) { case 1: xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0, - bar_width / 2 - (x + sel_font->width) / 2, 0, x, BAR_HEIGHT); - x = bar_width / 2 - (x + sel_font->width) / 2 + x; + bar_width / 2 - (x + ch_width) / 2, 0, x, BAR_HEIGHT); + x = bar_width / 2 - (x + ch_width) / 2 + x; break; case 2: xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0, - bar_width - x - sel_font->width, 0, x, BAR_HEIGHT); - x = bar_width - sel_font->width; + bar_width - x - ch_width, 0, x, BAR_HEIGHT); + x = bar_width - ch_width; break; } /* Draw the background first */ - xcb_fill_rect (clear_gc, x, 0, sel_font->width, BAR_HEIGHT); + xcb_fill_rect (clear_gc, x, 0, ch_width, BAR_HEIGHT); /* Draw the underline */ if (BAR_UNDERLINE_HEIGHT) - xcb_fill_rect (underl_gc, x, BAR_HEIGHT-BAR_UNDERLINE_HEIGHT, sel_font->width, BAR_UNDERLINE_HEIGHT); + xcb_fill_rect (underl_gc, x, BAR_HEIGHT-BAR_UNDERLINE_HEIGHT, ch_width, BAR_UNDERLINE_HEIGHT); /* xcb accepts string in UCS-2 BE, so swap */ ch = (ch >> 8) | (ch << 8); /* String baseline coordinates */ - xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->height / 2 - sel_font->descent, + xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->avg_height / 2 - sel_font->info->font_descent, (xcb_char2b_t *)&ch); - return sel_font->width; + return ch_width; } void @@ -206,19 +215,17 @@ font_load (const char **font_list) font_info = xcb_query_font_reply (c, queryreq, NULL); fontset[i].xcb_ft = font; - fontset[i].width = font_info->max_bounds.character_width; - fontset[i].descent = font_info->font_descent; + fontset[i].table = xcb_query_font_char_infos (font_info); + fontset[i].info = font_info; fontset[i].char_max= font_info->max_byte1 << 8 | font_info->max_char_or_byte2; fontset[i].char_min= font_info->min_byte1 << 8 | font_info->min_char_or_byte2; max_height = MAX(font_info->font_ascent + font_info->font_descent, max_height); - - free (font_info); } /* To have an uniform alignment */ for (int i = 0; i < FONT_MAX; i++) - fontset[i].height = max_height; + fontset[i].avg_height = max_height; return 0; } @@ -306,7 +313,7 @@ init (void) root = scr->root; /* Load the font */ - if (font_load ((const char* []){ BAR_MAIN_FONT, BAR_FALLBACK_FONT })) + if (font_load ((const char* []){ BAR_FONT })) exit (1); /* Create the main window */ @@ -348,8 +355,10 @@ init (void) void cleanup (void) { - xcb_close_font (c, fontset[FONT_MAIN].xcb_ft); - xcb_close_font (c, fontset[FONT_FALLBACK].xcb_ft); + for (int i = 0; i < FONT_MAX; i++) { + free (fontset[i].info); + xcb_close_font (c, fontset[i].xcb_ft); + } xcb_free_pixmap (c, canvas); xcb_destroy_window (c, win); xcb_free_gc (c, draw_gc); diff --git a/config.def.h b/config.def.h index 44a719b..d22089f 100644 --- a/config.def.h +++ b/config.def.h @@ -4,9 +4,8 @@ #define BAR_UNDERLINE_HEIGHT 2 /* Whether to put the bar at the screen bottom or not */ #define BAR_BOTTOM 0 -/* The font used for the bar */ -#define BAR_MAIN_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1" -#define BAR_FALLBACK_FONT "fixed" +/* The fonts used for the bar, comma separated. Only the first 2 will be used. */ +#define BAR_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1","fixed" /* Color palette */ #define COLOR0 0x1A1A1A /* background */ #define COLOR1 0xA9A9A9 /* foreground */ From 3f8bfbe514969216b0b7506467d380a3228f9514 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 25 Jul 2012 17:09:04 +0200 Subject: [PATCH 3/7] Toggleable underline/overline switch added. --- bar.c | 2 +- config.def.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bar.c b/bar.c index 281e4b1..c91c905 100644 --- a/bar.c +++ b/bar.c @@ -104,7 +104,7 @@ draw_char (int x, int align, wchar_t ch) /* Draw the underline */ if (BAR_UNDERLINE_HEIGHT) - xcb_fill_rect (underl_gc, x, BAR_HEIGHT-BAR_UNDERLINE_HEIGHT, ch_width, BAR_UNDERLINE_HEIGHT); + xcb_fill_rect (underl_gc, x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT); /* xcb accepts string in UCS-2 BE, so swap */ ch = (ch >> 8) | (ch << 8); diff --git a/config.def.h b/config.def.h index d22089f..0a01d8d 100644 --- a/config.def.h +++ b/config.def.h @@ -1,6 +1,8 @@ /* The height of the bar (in pixels) */ #define BAR_HEIGHT 18 -/* The thickness of the underline (in pixels) */ +/* Choose between an underline or an overline */ +#define BAR_UNDERLINE 1 +/* The thickness of the underline (in pixels). Set to 0 to disable. */ #define BAR_UNDERLINE_HEIGHT 2 /* Whether to put the bar at the screen bottom or not */ #define BAR_BOTTOM 0 From 6c3b81602ba53c35889833117602168faef424a9 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 25 Jul 2012 20:33:01 +0200 Subject: [PATCH 4/7] Swap order when drawing. Now it underlines properly even small fonts. --- bar.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bar.c b/bar.c index c91c905..3bf6afd 100644 --- a/bar.c +++ b/bar.c @@ -102,10 +102,6 @@ draw_char (int x, int align, wchar_t ch) /* Draw the background first */ xcb_fill_rect (clear_gc, x, 0, ch_width, BAR_HEIGHT); - /* Draw the underline */ - if (BAR_UNDERLINE_HEIGHT) - xcb_fill_rect (underl_gc, x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT); - /* xcb accepts string in UCS-2 BE, so swap */ ch = (ch >> 8) | (ch << 8); @@ -113,6 +109,10 @@ draw_char (int x, int align, wchar_t ch) xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->avg_height / 2 - sel_font->info->font_descent, (xcb_char2b_t *)&ch); + /* Draw the underline */ + if (BAR_UNDERLINE_HEIGHT) + xcb_fill_rect (underl_gc, x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT); + return ch_width; } From f089d43a46367fca4273a6f771840518a9d95e55 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Wed, 25 Jul 2012 22:57:51 +0200 Subject: [PATCH 5/7] Don't sync background color with foreground one. --- bar.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bar.c b/bar.c index 3bf6afd..2e07f5b 100644 --- a/bar.c +++ b/bar.c @@ -46,7 +46,6 @@ xcb_set_bg (int i) { xcb_change_gc (c, draw_gc , XCB_GC_BACKGROUND, (const unsigned []){ palette[i] }); xcb_change_gc (c, clear_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] }); - xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const unsigned []){ palette[i] }); } void @@ -83,6 +82,7 @@ draw_char (int x, int align, wchar_t ch) sel_font->table[ch - sel_font->char_min].character_width : 0; + if (ch_width == 0) return 0; From b74ceb323a21ef1672f591bd8f0c1ec634c0a5d1 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 26 Jul 2012 20:06:02 -0400 Subject: [PATCH 6/7] Specify how to clear underline color in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bb06d4..60570f6 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ To draw a backslash just backslash escape it (\\\\). f<0-9> Selects the text foreground color from the palette. b<0-9> Selects the text background color from the palette. u<0-9> Selects the underline color from the palette. - To reset the bg/fg color just pass 'r' as color index. + To reset the bg/fg/underline color just pass 'r' as color index. l Aligns the text to the left. c Aligns the text to the center. From ef18b389ce1684303c748dac37a68968402a63bc Mon Sep 17 00:00:00 2001 From: = Date: Thu, 26 Jul 2012 20:07:36 -0400 Subject: [PATCH 7/7] Typo, added the --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 60570f6..38f0371 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ To draw a backslash just backslash escape it (\\\\). f<0-9> Selects the text foreground color from the palette. b<0-9> Selects the text background color from the palette. u<0-9> Selects the underline color from the palette. - To reset the bg/fg/underline color just pass 'r' as color index. + To reset the bg/fg/underline color just pass 'r' as the color index. l Aligns the text to the left. c Aligns the text to the center.