Skip to content

Commit e1cd134

Browse files
author
Georg Reinke
committed
Merge remote-tracking branch 'upstream/master'
Conflicts: bar.c
2 parents 06d2db7 + c36808d commit e1cd134

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ b(ar) a(in't) r(ecursive)
33
2012 (C) The Lemon Man
44

55
A lightweight bar based on XCB (yay). Provides foreground/background color
6-
switching along with text alignment (screw you dzen!), nothing less and
7-
nothing more.
6+
switching along with text alignment (screw you dzen!), full utf8 support
7+
and reduced memory footprint. Nothing less and nothing more.
88

99
Options
1010
-------
@@ -43,7 +43,7 @@ To draw a backslash just backslash escape it (\\\\).
4343
f<0-9> Selects the text foreground color from the palette.
4444
b<0-9> Selects the text background color from the palette.
4545
u<0-9> Selects the underline color from the palette.
46-
To reset the bg/fg color just pass 'r' as color index.
46+
To reset the bg/fg/underline color just pass 'r' as the color index.
4747
4848
l Aligns the text to the left.
4949
c Aligns the text to the center.

bar.c

+42-32
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
#define MAX(a,b) ((a > b) ? a : b)
1717

1818
typedef struct fontset_item_t {
19-
xcb_font_t xcb_ft;
20-
int height;
21-
int width;
22-
int descent;
23-
unsigned short char_max;
24-
unsigned short char_min;
19+
xcb_font_t xcb_ft;
20+
xcb_query_font_reply_t *info;
21+
xcb_charinfo_t *table;
22+
int avg_height;
23+
unsigned short char_max;
24+
unsigned short char_min;
2525
} fontset_item_t;
2626

2727
enum {
@@ -54,7 +54,6 @@ xcb_set_bg (int i)
5454
{
5555
xcb_change_gc (c, draw_gc , XCB_GC_BACKGROUND, (const unsigned []){ palette[i] });
5656
xcb_change_gc (c, clear_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] });
57-
xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const unsigned []){ palette[i] });
5857
}
5958

6059
void
@@ -85,34 +84,44 @@ xcb_set_fontset (int i)
8584
int
8685
draw_char (int x, int align, wchar_t ch)
8786
{
87+
int ch_width;
88+
89+
ch_width = (ch > sel_font->char_min && ch < sel_font->char_max) ?
90+
sel_font->table[ch - sel_font->char_min].character_width :
91+
0;
92+
93+
94+
if (ch_width == 0)
95+
return 0;
96+
8897
switch (align) {
8998
case ALIGN_C:
9099
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0,
91-
bar_width / 2 - (x + sel_font->width) / 2, 0, x, BAR_HEIGHT);
92-
x = bar_width / 2 - (x + sel_font->width) / 2 + x;
100+
bar_width / 2 - (x + ch_width) / 2, 0, x, BAR_HEIGHT);
101+
x = bar_width / 2 - (x + ch_width) / 2 + x;
93102
break;
94103
case ALIGN_R:
95104
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0,
96-
bar_width - x - sel_font->width, 0, x, BAR_HEIGHT);
97-
x = bar_width - sel_font->width;
105+
bar_width - x - ch_width, 0, x, BAR_HEIGHT);
106+
x = bar_width - ch_width;
98107
break;
99108
}
100109

101110
/* Draw the background first */
102-
xcb_fill_rect (clear_gc, x, 0, sel_font->width, BAR_HEIGHT);
103-
104-
/* Draw the underline */
105-
if (BAR_UNDERLINE_HEIGHT)
106-
xcb_fill_rect (underl_gc, x, BAR_HEIGHT-BAR_UNDERLINE_HEIGHT, sel_font->width, BAR_UNDERLINE_HEIGHT);
111+
xcb_fill_rect (clear_gc, x, 0, ch_width, BAR_HEIGHT);
107112

108113
/* xcb accepts string in UCS-2 BE, so swap */
109114
ch = (ch >> 8) | (ch << 8);
110115

111116
/* String baseline coordinates */
112-
xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->height / 2 - sel_font->descent,
117+
xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->avg_height / 2 - sel_font->info->font_descent,
113118
(xcb_char2b_t *)&ch);
114119

115-
return sel_font->width;
120+
/* Draw the underline */
121+
if (BAR_UNDERLINE_HEIGHT)
122+
xcb_fill_rect (underl_gc, x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT);
123+
124+
return ch_width;
116125
}
117126

118127
void
@@ -214,19 +223,17 @@ font_load (const char **font_list)
214223
font_info = xcb_query_font_reply (c, queryreq, NULL);
215224

216225
fontset[i].xcb_ft = font;
217-
fontset[i].width = font_info->max_bounds.character_width;
218-
fontset[i].descent = font_info->font_descent;
226+
fontset[i].table = xcb_query_font_char_infos (font_info);
227+
fontset[i].info = font_info;
219228
fontset[i].char_max= font_info->max_byte1 << 8 | font_info->max_char_or_byte2;
220229
fontset[i].char_min= font_info->min_byte1 << 8 | font_info->min_char_or_byte2;
221230

222231
max_height = MAX(font_info->font_ascent + font_info->font_descent, max_height);
223-
224-
free (font_info);
225232
}
226233

227234
/* To have an uniform alignment */
228235
for (int i = 0; i < FONT_MAX; i++)
229-
fontset[i].height = max_height;
236+
fontset[i].avg_height = max_height;
230237

231238
return 0;
232239
}
@@ -258,12 +265,12 @@ set_ewmh_atoms (xcb_window_t root)
258265
reply = xcb_intern_atom_reply (c, cookies[4], NULL);
259266
atoms[4] = reply->atom; free (reply);
260267

261-
do {
262-
reply1 = xcb_get_property_reply (c, xcb_get_property (c, 0, root, atoms[4], XCB_ATOM_ATOM, 0, -1), NULL);
263-
} while (!xcb_get_property_value_length (reply1));
264-
265268
compliance_lvl = 0;
266269

270+
reply1 = xcb_get_property_reply (c, xcb_get_property (c, 0, root, atoms[4], XCB_ATOM_ATOM, 0, -1), NULL);
271+
if (!reply)
272+
return compliance_lvl;
273+
267274
for (xcb_atom_t *a = xcb_get_property_value (reply1);
268275
a && a != xcb_get_property_value_end (reply1).data;
269276
a++)
@@ -319,7 +326,7 @@ init (void)
319326
root = scr->root;
320327

321328
/* Load the font */
322-
if (font_load ((const char* []){ BAR_MAIN_FONT, BAR_FALLBACK_FONT }))
329+
if (font_load ((const char* []){ BAR_FONT }))
323330
exit (1);
324331

325332
/* Create the main window */
@@ -360,10 +367,13 @@ init (void)
360367
void
361368
cleanup (void)
362369
{
363-
if (fontset[FONT_MAIN].xcb_ft)
364-
xcb_close_font (c, fontset[FONT_MAIN].xcb_ft);
365-
if (fontset[FONT_FALLBACK].xcb_ft)
366-
xcb_close_font (c, fontset[FONT_FALLBACK].xcb_ft);
370+
int i;
371+
for (i = 0; i < FONT_MAX; i++) {
372+
if (fontset[i].info)
373+
free (fontset[i].info);
374+
if (fontset[i].xcb_ft)
375+
xcb_close_font (c, fontset[i].xcb_ft);
376+
}
367377
if (canvas)
368378
xcb_free_pixmap (c, canvas);
369379
if (win)

config.def.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
/* The height of the bar (in pixels) */
22
#define BAR_HEIGHT 18
3-
/* The thickness of the underline (in pixels) */
3+
/* Choose between an underline or an overline */
4+
#define BAR_UNDERLINE 1
5+
/* The thickness of the underline (in pixels). Set to 0 to disable. */
46
#define BAR_UNDERLINE_HEIGHT 2
57
/* Default bar position, overwritten by '-b' switch */
68
#define BAR_BOTTOM 0
7-
/* The font used for the bar */
8-
#define BAR_MAIN_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1"
9-
#define BAR_FALLBACK_FONT "fixed"
9+
/* The fonts used for the bar, comma separated. Only the first 2 will be used. */
10+
#define BAR_FONT "-*-terminus-medium-r-normal-*-12-*-*-*-c-*-*-1","fixed"
1011
/* Color palette */
1112
#define COLOR0 0x1A1A1A /* background */
1213
#define COLOR1 0xA9A9A9 /* foreground */

0 commit comments

Comments
 (0)