Text is now properly centered.
This commit is contained in:
parent
c97b0ecde6
commit
054ee4396f
80
bar.c
80
bar.c
|
@ -13,17 +13,17 @@
|
||||||
// Here be dragons
|
// Here be dragons
|
||||||
|
|
||||||
static xcb_connection_t *c;
|
static xcb_connection_t *c;
|
||||||
static int ft_height, ft_width, ft_descent;
|
static int font_height, font_width, font_descent;
|
||||||
static xcb_window_t root, win;
|
static xcb_window_t root, win;
|
||||||
static xcb_gcontext_t gc;
|
static xcb_gcontext_t gc;
|
||||||
static int bw, bh;
|
static int bar_width;
|
||||||
static const uint32_t pal[] = {COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9};
|
static const uint32_t pal[] = {COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9};
|
||||||
static xcb_drawable_t canvas;
|
static xcb_drawable_t canvas;
|
||||||
|
|
||||||
#define MIN(a,b) ((a > b ? b : a))
|
#define MIN(a,b) ((a > b ? b : a))
|
||||||
|
|
||||||
void
|
void
|
||||||
fillrect (int color, int x, int y, int width, int height)
|
fill_rect (int color, int x, int y, int width, int height)
|
||||||
{
|
{
|
||||||
xcb_change_gc (c, gc, XCB_GC_FOREGROUND, (const uint32_t []){ pal[color] });
|
xcb_change_gc (c, gc, XCB_GC_FOREGROUND, (const uint32_t []){ pal[color] });
|
||||||
xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
|
xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
|
||||||
|
@ -38,35 +38,37 @@ wcslen_ (wchar_t *s) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
draw (int x, int align, int fgcol, int bgcol, wchar_t *text)
|
draw_string (int x, int align, int fgcol, int bgcol, wchar_t *text)
|
||||||
{
|
{
|
||||||
int done = 0;
|
int done = 0;
|
||||||
int pos_x = x;
|
int pos_x = x;
|
||||||
int len = MIN(bw / ft_width, wcslen_ (text));
|
int len = MIN(bar_width / font_width, wcslen_ (text));
|
||||||
int strw = len * ft_width;
|
int strw = len * font_width;
|
||||||
|
|
||||||
if (!strw) return 0;
|
if (!strw) return 0;
|
||||||
|
|
||||||
switch (align) {
|
switch (align) {
|
||||||
case 1:
|
case 1:
|
||||||
xcb_copy_area (c, canvas, canvas, gc, bw / 2 - pos_x / 2, 0, bw / 2 - (pos_x + strw) / 2, 0, pos_x, BAR_HEIGHT);
|
xcb_copy_area (c, canvas, canvas, gc, bar_width / 2 - pos_x / 2, 0,
|
||||||
pos_x = bw / 2 - (pos_x + strw) / 2 + pos_x;
|
bar_width / 2 - (pos_x + strw) / 2, 0, pos_x, BAR_HEIGHT);
|
||||||
|
pos_x = bar_width / 2 - (pos_x + strw) / 2 + pos_x;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
xcb_copy_area (c, canvas, canvas, gc, bw - pos_x, 0, bw - pos_x - strw, 0, pos_x, BAR_HEIGHT);
|
xcb_copy_area (c, canvas, canvas, gc, bar_width - pos_x, 0,
|
||||||
pos_x = bw - strw;
|
bar_width - pos_x - strw, 0, pos_x, BAR_HEIGHT);
|
||||||
|
pos_x = bar_width - strw;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* Draw the background first */
|
/* Draw the background first */
|
||||||
fillrect (bgcol, pos_x, 0, strw, BAR_HEIGHT);
|
fill_rect (bgcol, pos_x, 0, strw, BAR_HEIGHT);
|
||||||
/* Setup the colors */
|
/* Setup the colors */
|
||||||
xcb_change_gc (c, gc, XCB_GC_FOREGROUND, (const uint32_t []){ pal[fgcol] });
|
xcb_change_gc (c, gc, XCB_GC_FOREGROUND, (const uint32_t []){ pal[fgcol] });
|
||||||
xcb_change_gc (c, gc, XCB_GC_BACKGROUND, (const uint32_t []){ pal[bgcol] });
|
xcb_change_gc (c, gc, XCB_GC_BACKGROUND, (const uint32_t []){ pal[bgcol] });
|
||||||
do {
|
do {
|
||||||
xcb_image_text_16 (c, MIN(len - done, 255), canvas, gc, pos_x, bh / 2 + ft_height / 2 - ft_descent, /* Bottom left coords */
|
xcb_image_text_16 (c, MIN(len - done, 255), canvas, gc, pos_x, /* String baseline coordinates */
|
||||||
(xcb_char2b_t *)text + done);
|
BAR_HEIGHT / 2 + font_height / 2 - font_descent, (xcb_char2b_t *)text + done);
|
||||||
done += MIN(len - done, 255);
|
done += MIN(len - done, 255);
|
||||||
pos_x = done * ft_width;
|
pos_x = done * font_width;
|
||||||
} while (done < len);
|
} while (done < len);
|
||||||
|
|
||||||
return pos_x;
|
return pos_x;
|
||||||
|
@ -86,10 +88,10 @@ parse (char *text)
|
||||||
int fgcol = 1;
|
int fgcol = 1;
|
||||||
int bgcol = 0;
|
int bgcol = 0;
|
||||||
|
|
||||||
fillrect (0, 0, 0, bw, BAR_HEIGHT);
|
fill_rect (0, 0, 0, bar_width, BAR_HEIGHT);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (*p == 0x0 || *p == 0xA || (*p == '\\' && p++ && *p != '\\' && strchr ("fblcr", *p))) {
|
if (*p == 0x0 || *p == 0xA || (*p == '\\' && p++ && *p != '\\' && strchr ("fblcr", *p))) {
|
||||||
pos_x += draw (pos_x, align, fgcol, bgcol, parsed_text);
|
pos_x += draw_string (pos_x, align, fgcol, bgcol, parsed_text);
|
||||||
switch (*p++) {
|
switch (*p++) {
|
||||||
case 0x0: return; /* EOL */
|
case 0x0: return; /* EOL */
|
||||||
case 0xA: return; /* NL */
|
case 0xA: return; /* NL */
|
||||||
|
@ -136,7 +138,7 @@ sighandle (int signal)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ewmh (void)
|
set_ewmh_props (void)
|
||||||
{
|
{
|
||||||
xcb_intern_atom_cookie_t cookies[4];
|
xcb_intern_atom_cookie_t cookies[4];
|
||||||
xcb_atom_t atoms[4];
|
xcb_atom_t atoms[4];
|
||||||
|
@ -160,15 +162,15 @@ ewmh (void)
|
||||||
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[2], XCB_ATOM_CARDINAL, 32, 1,
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[2], XCB_ATOM_CARDINAL, 32, 1,
|
||||||
(const int []){ 0xffffffff } );
|
(const int []){ 0xffffffff } );
|
||||||
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12,
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12,
|
||||||
(const int []) { 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, bw, 0, 0} );
|
(const int []) { 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, bar_width, 0, 0} );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
init (void)
|
init (void)
|
||||||
{
|
{
|
||||||
xcb_font_t xf;
|
xcb_font_t font;
|
||||||
xcb_screen_t *scr;
|
xcb_screen_t *scr;
|
||||||
xcb_query_font_reply_t *ft_info;
|
xcb_query_font_reply_t *font_info;
|
||||||
|
|
||||||
/* Connect to X */
|
/* Connect to X */
|
||||||
c = xcb_connect (NULL, NULL);
|
c = xcb_connect (NULL, NULL);
|
||||||
|
@ -178,36 +180,35 @@ init (void)
|
||||||
}
|
}
|
||||||
/* Grab infos from the first screen */
|
/* Grab infos from the first screen */
|
||||||
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
|
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
|
||||||
bw = scr->width_in_pixels;
|
bar_width = scr->width_in_pixels;
|
||||||
bh = BAR_HEIGHT;
|
|
||||||
root = scr->root;
|
root = scr->root;
|
||||||
/* Load the font */
|
/* Load the font */
|
||||||
xf = xcb_generate_id (c);
|
font = xcb_generate_id (c);
|
||||||
if (xcb_request_check (c, xcb_open_font_checked (c, xf, strlen(BAR_FONT), BAR_FONT))) {
|
if (xcb_request_check (c, xcb_open_font_checked (c, font, strlen(BAR_FONT), BAR_FONT))) {
|
||||||
fprintf (stderr, "Couldn't load the font\n");
|
fprintf (stderr, "Couldn't load the font\n");
|
||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
/* Grab infos from the font */
|
/* Grab infos from the font */
|
||||||
ft_info = xcb_query_font_reply (c, xcb_query_font (c, xf), NULL);
|
font_info = xcb_query_font_reply (c, xcb_query_font (c, font), NULL);
|
||||||
ft_height = ft_info->font_ascent + ft_info->font_descent;
|
font_height = font_info->font_ascent + font_info->font_descent;
|
||||||
ft_width = ft_info->max_bounds.character_width;
|
font_width = font_info->max_bounds.character_width;
|
||||||
ft_descent = ft_info->font_descent;
|
font_descent = font_info->font_descent;
|
||||||
/* Create the main window */
|
/* Create the main window */
|
||||||
win = xcb_generate_id (c);
|
win = xcb_generate_id (c);
|
||||||
xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, 0, bw, bh, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT,
|
xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, 0, bar_width, BAR_HEIGHT,
|
||||||
scr->root_visual, XCB_CW_BACK_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK,
|
0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, XCB_CW_BACK_PIXEL |
|
||||||
(const uint32_t []){ pal[0], 1, XCB_EVENT_MASK_EXPOSURE });
|
XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK, (const uint32_t []){ pal[0], 1, XCB_EVENT_MASK_EXPOSURE });
|
||||||
/* Set EWMH hints */
|
/* Set EWMH hints */
|
||||||
ewmh ();
|
set_ewmh_props ();
|
||||||
/* Create a temporary canvas */
|
/* Create a temporary canvas */
|
||||||
canvas = xcb_generate_id (c);
|
canvas = xcb_generate_id (c);
|
||||||
xcb_create_pixmap (c, scr->root_depth, canvas, root, bw, BAR_HEIGHT);
|
xcb_create_pixmap (c, scr->root_depth, canvas, root, bar_width, BAR_HEIGHT);
|
||||||
/* Create the gc for drawing */
|
/* Create the gc for drawing */
|
||||||
gc = xcb_generate_id (c);
|
gc = xcb_generate_id (c);
|
||||||
xcb_create_gc (c, gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT,
|
xcb_create_gc (c, gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT,
|
||||||
(const uint32_t []){ pal[1], pal[0], xf });
|
(const uint32_t []){ pal[1], pal[0], font });
|
||||||
/* Get rid of the font */
|
/* Get rid of the font */
|
||||||
xcb_close_font (c, xf);
|
xcb_close_font (c, font);
|
||||||
/* Make the bar visible */
|
/* Make the bar visible */
|
||||||
xcb_map_window (c, win);
|
xcb_map_window (c, win);
|
||||||
xcb_flush (c);
|
xcb_flush (c);
|
||||||
|
@ -216,8 +217,11 @@ init (void)
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct pollfd pollin[2] = { { .fd = STDIN_FILENO, .events = POLLIN }, { .fd = -1, .events = POLLIN } };
|
|
||||||
static char input[1024] = {0, };
|
static char input[1024] = {0, };
|
||||||
|
struct pollfd pollin[2] = {
|
||||||
|
{ .fd = STDIN_FILENO, .events = POLLIN },
|
||||||
|
{ .fd = -1 , .events = POLLIN },
|
||||||
|
};
|
||||||
|
|
||||||
xcb_generic_event_t *ev;
|
xcb_generic_event_t *ev;
|
||||||
xcb_expose_event_t *expose_ev;
|
xcb_expose_event_t *expose_ev;
|
||||||
|
@ -244,7 +248,7 @@ main (int argc, char **argv)
|
||||||
/* Get the fd to Xserver */
|
/* Get the fd to Xserver */
|
||||||
pollin[1].fd = xcb_get_file_descriptor (c);
|
pollin[1].fd = xcb_get_file_descriptor (c);
|
||||||
|
|
||||||
fillrect (0, 0, 0, bw, BAR_HEIGHT);
|
fill_rect (0, 0, 0, bar_width, BAR_HEIGHT);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int redraw = 0;
|
int redraw = 0;
|
||||||
|
@ -275,7 +279,7 @@ main (int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redraw) /* Copy our temporary pixmap onto the window */
|
if (redraw) /* Copy our temporary pixmap onto the window */
|
||||||
xcb_copy_area (c, canvas, win, gc, 0, 0, 0, 0, bw, BAR_HEIGHT);
|
xcb_copy_area (c, canvas, win, gc, 0, 0, 0, 0, bar_width, BAR_HEIGHT);
|
||||||
xcb_flush (c);
|
xcb_flush (c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user