lemonbar/bar.c

288 lines
9.3 KiB
C
Raw Normal View History

2012-07-16 09:41:34 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
2012-07-16 23:17:11 +00:00
#include <poll.h>
#include <getopt.h>
#include <unistd.h>
2012-07-16 09:41:34 +00:00
#include <xcb/xcb.h>
#include "config.h"
// Here be dragons
static xcb_connection_t *c;
2012-07-20 09:53:29 +00:00
static int font_height, font_width, font_descent;
2012-07-16 09:41:34 +00:00
static xcb_window_t root, win;
static xcb_gcontext_t gc;
2012-07-20 09:53:29 +00:00
static int bar_width;
2012-07-16 23:17:11 +00:00
static const uint32_t pal[] = {COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9};
2012-07-16 20:56:19 +00:00
static xcb_drawable_t canvas;
2012-07-16 09:41:34 +00:00
#define MIN(a,b) ((a > b ? b : a))
2012-07-17 09:34:12 +00:00
void
2012-07-20 09:53:29 +00:00
fill_rect (int color, int x, int y, int width, int height)
2012-07-17 09:34:12 +00:00
{
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 } });
}
2012-07-18 15:11:17 +00:00
/* libc doesn't give a shit to -fshort-wchar, so here's our wcslen */
size_t
wcslen_ (wchar_t *s) {
size_t len;
2012-07-18 15:29:37 +00:00
for (len = 0; *s; s++, len++);
2012-07-18 15:11:17 +00:00
return len;
}
2012-07-16 09:41:34 +00:00
int
2012-07-20 09:53:29 +00:00
draw_string (int x, int align, int fgcol, int bgcol, wchar_t *text)
2012-07-16 09:41:34 +00:00
{
int done = 0;
int pos_x = x;
2012-07-20 09:53:29 +00:00
int len = MIN(bar_width / font_width, wcslen_ (text));
int strw = len * font_width;
2012-07-16 09:41:34 +00:00
2012-07-16 13:54:34 +00:00
if (!strw) return 0;
2012-07-16 09:41:34 +00:00
switch (align) {
case 1:
2012-07-20 09:53:29 +00:00
xcb_copy_area (c, canvas, canvas, gc, bar_width / 2 - pos_x / 2, 0,
bar_width / 2 - (pos_x + strw) / 2, 0, pos_x, BAR_HEIGHT);
pos_x = bar_width / 2 - (pos_x + strw) / 2 + pos_x;
2012-07-16 09:41:34 +00:00
break;
case 2:
2012-07-20 09:53:29 +00:00
xcb_copy_area (c, canvas, canvas, gc, bar_width - pos_x, 0,
bar_width - pos_x - strw, 0, pos_x, BAR_HEIGHT);
pos_x = bar_width - strw;
2012-07-16 09:41:34 +00:00
break;
}
2012-07-16 20:56:19 +00:00
/* Draw the background first */
2012-07-20 09:53:29 +00:00
fill_rect (bgcol, pos_x, 0, strw, BAR_HEIGHT);
/* Setup the colors */
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] });
2012-07-16 09:41:34 +00:00
do {
2012-07-20 09:53:29 +00:00
xcb_image_text_16 (c, MIN(len - done, 255), canvas, gc, pos_x, /* String baseline coordinates */
BAR_HEIGHT / 2 + font_height / 2 - font_descent, (xcb_char2b_t *)text + done);
2012-07-16 09:41:34 +00:00
done += MIN(len - done, 255);
2012-07-20 09:53:29 +00:00
pos_x = done * font_width;
2012-07-16 09:41:34 +00:00
} while (done < len);
return pos_x;
}
void
parse (char *text)
{
2012-07-18 15:11:17 +00:00
wchar_t parsed_text[2048] = {0, };
2012-07-16 09:41:34 +00:00
2012-07-18 15:11:17 +00:00
wchar_t *q = parsed_text;
char *p = text;
2012-07-16 09:41:34 +00:00
int pos_x = 0;
int align = 0;
int fgcol = 1;
int bgcol = 0;
2012-07-20 09:53:29 +00:00
fill_rect (0, 0, 0, bar_width, BAR_HEIGHT);
2012-07-16 09:41:34 +00:00
for (;;) {
if (*p == 0x0 || *p == 0xA || (*p == '\\' && p++ && *p != '\\' && strchr ("fblcr", *p))) {
2012-07-20 09:53:29 +00:00
pos_x += draw_string (pos_x, align, fgcol, bgcol, parsed_text);
2012-07-16 09:41:34 +00:00
switch (*p++) {
case 0x0: return; /* EOL */
case 0xA: return; /* NL */
case 'f': if (*p == 'r') *p = '1'; if (isdigit (*p)) { fgcol = *p-'0'; } p++; break;
case 'b': if (*p == 'r') *p = '0'; if (isdigit (*p)) { bgcol = *p-'0'; } p++; break;
2012-07-16 09:41:34 +00:00
case 'l': align = 0; pos_x = 0; break;
case 'c': align = 1; pos_x = 0; break;
case 'r': align = 2; pos_x = 0; break;
}
q = parsed_text;
2012-07-18 15:11:17 +00:00
} else { /* utf-8 -> ucs-2 */
if (!(p[0] & 0x80)) {
*q++ = p[0] << 8;
p += 1;
}
else if ((p[0] & 0xe0) == 0xc0 && (p[1] & 0xc0) == 0x80) {
wchar_t t = (p[0] & 0x1f) << 6 | p[1] & 0x3f;
*q++ = (t >> 8) | (t << 8);
p += 2;
}
else if ((p[0] & 0xf0) == 0xe0 && (p[1] & 0xc0) == 0x80 && (p[2] & 0xc0) == 0x80) {
wchar_t t = (p[0] & 0xf) << 12 | (p[1] & 0x3f) << 6 | p[2] & 0x3f;
*q++ = (t >> 8) | (t << 8);
p += 3;
}
}
2012-07-16 09:41:34 +00:00
*q = 0;
}
}
void
cleanup (void)
{
2012-07-16 20:56:19 +00:00
xcb_free_pixmap (c, canvas);
2012-07-16 09:41:34 +00:00
xcb_destroy_window (c, win);
xcb_free_gc (c, gc);
xcb_disconnect (c);
}
void
sighandle (int signal)
{
2012-07-16 23:17:11 +00:00
if (signal == SIGINT || signal == SIGTERM) exit (0);
2012-07-16 09:41:34 +00:00
}
2012-07-20 00:44:30 +00:00
void
2012-07-20 09:53:29 +00:00
set_ewmh_props (void)
2012-07-20 00:44:30 +00:00
{
xcb_intern_atom_cookie_t cookies[4];
xcb_atom_t atoms[4];
xcb_intern_atom_reply_t *reply;
cookies[0] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE") , "_NET_WM_WINDOW_TYPE");
cookies[1] = xcb_intern_atom (c, 0, strlen ("_NET_WM_WINDOW_TYPE_DOCK"), "_NET_WM_WINDOW_TYPE_DOCK");
cookies[2] = xcb_intern_atom (c, 0, strlen ("_NET_WM_DESKTOP") , "_NET_WM_DESKTOP");
cookies[3] = xcb_intern_atom (c, 0, strlen ("_NET_WM_STRUT_PARTIAL") , "_NET_WM_STRUT_PARTIAL");
reply = xcb_intern_atom_reply (c, cookies[0], NULL);
atoms[0] = reply->atom; free (reply);
reply = xcb_intern_atom_reply (c, cookies[1], NULL);
atoms[1] = reply->atom; free (reply);
reply = xcb_intern_atom_reply (c, cookies[2], NULL);
atoms[2] = reply->atom; free (reply);
reply = xcb_intern_atom_reply (c, cookies[3], NULL);
atoms[3] = reply->atom; free (reply);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[0], XCB_ATOM_ATOM, 32, 1, &atoms[1]);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[2], XCB_ATOM_CARDINAL, 32, 1,
(const int []){ 0xffffffff } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atoms[3], XCB_ATOM_CARDINAL, 32, 12,
2012-07-20 09:53:29 +00:00
(const int []) { 0, 0, BAR_HEIGHT, 0, 0, 0, 0, 0, bar_width, 0, 0} );
2012-07-20 00:44:30 +00:00
}
2012-07-16 09:41:34 +00:00
void
init (void)
{
2012-07-20 09:53:29 +00:00
xcb_font_t font;
2012-07-16 09:41:34 +00:00
xcb_screen_t *scr;
2012-07-20 09:53:29 +00:00
xcb_query_font_reply_t *font_info;
2012-07-16 09:41:34 +00:00
/* Connect to X */
c = xcb_connect (NULL, NULL);
if (xcb_connection_has_error (c)) {
fprintf (stderr, "Couldn't connect to X\n");
exit (1);
}
/* Grab infos from the first screen */
2012-07-20 09:53:29 +00:00
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
bar_width = scr->width_in_pixels;
2012-07-16 09:41:34 +00:00
root = scr->root;
/* Load the font */
2012-07-20 09:53:29 +00:00
font = xcb_generate_id (c);
if (xcb_request_check (c, xcb_open_font_checked (c, font, strlen(BAR_FONT), BAR_FONT))) {
2012-07-16 09:41:34 +00:00
fprintf (stderr, "Couldn't load the font\n");
exit (1);
}
/* Grab infos from the font */
2012-07-20 09:53:29 +00:00
font_info = xcb_query_font_reply (c, xcb_query_font (c, font), NULL);
font_height = font_info->font_ascent + font_info->font_descent;
font_width = font_info->max_bounds.character_width;
font_descent = font_info->font_descent;
2012-07-16 09:41:34 +00:00
/* Create the main window */
win = xcb_generate_id (c);
2012-07-20 09:53:29 +00:00
xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, 0, 0, bar_width, BAR_HEIGHT,
0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual, XCB_CW_BACK_PIXEL |
XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK, (const uint32_t []){ pal[0], 1, XCB_EVENT_MASK_EXPOSURE });
2012-07-20 00:44:30 +00:00
/* Set EWMH hints */
2012-07-20 09:53:29 +00:00
set_ewmh_props ();
2012-07-16 20:56:19 +00:00
/* Create a temporary canvas */
canvas = xcb_generate_id (c);
2012-07-20 09:53:29 +00:00
xcb_create_pixmap (c, scr->root_depth, canvas, root, bar_width, BAR_HEIGHT);
2012-07-16 09:41:34 +00:00
/* Create the gc for drawing */
gc = xcb_generate_id (c);
xcb_create_gc (c, gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT,
2012-07-20 09:53:29 +00:00
(const uint32_t []){ pal[1], pal[0], font });
2012-07-16 09:41:34 +00:00
/* Get rid of the font */
2012-07-20 09:53:29 +00:00
xcb_close_font (c, font);
2012-07-16 09:41:34 +00:00
/* Make the bar visible */
xcb_map_window (c, win);
2012-07-18 15:11:17 +00:00
xcb_flush (c);
2012-07-16 09:41:34 +00:00
}
int
main (int argc, char **argv)
2012-07-16 09:41:34 +00:00
{
static char input[1024] = {0, };
2012-07-20 09:53:29 +00:00
struct pollfd pollin[2] = {
{ .fd = STDIN_FILENO, .events = POLLIN },
{ .fd = -1 , .events = POLLIN },
};
2012-07-17 09:34:12 +00:00
xcb_generic_event_t *ev;
xcb_expose_event_t *expose_ev;
int permanent = 0;
2012-07-17 09:34:12 +00:00
char ch;
while ((ch = getopt (argc, argv, "ph")) != -1) {
switch (ch) {
case 'h':
2012-07-17 09:34:12 +00:00
printf ("usage: %s [-p | -h]\n"
"\t-h Shows this help\n"
"\t-p Don't close after the data ends\n", argv[0]);
exit (0);
case 'p': permanent = 1; break;
}
}
2012-07-16 09:41:34 +00:00
atexit (cleanup);
signal (SIGINT, sighandle);
signal (SIGTERM, sighandle);
init ();
/* Get the fd to Xserver */
pollin[1].fd = xcb_get_file_descriptor (c);
2012-07-20 09:53:29 +00:00
fill_rect (0, 0, 0, bar_width, BAR_HEIGHT);
2012-07-17 09:34:12 +00:00
2012-07-16 23:17:11 +00:00
for (;;) {
int redraw = 0;
if (poll ((struct pollfd *)&pollin, 2, -1) > 0) {
2012-07-18 19:27:41 +00:00
if (pollin[0].revents & POLLHUP) { /* No more data... */
if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
else break; /* ...bail out */
}
if (pollin[0].revents & POLLIN) { /* New input, process it */
fgets (input, sizeof(input), stdin);
parse (input);
redraw = 1;
}
if (pollin[1].revents & POLLIN) { /* Xserver broadcasted an event */
while ((ev = xcb_poll_for_event (c))) {
expose_ev = (xcb_expose_event_t *)ev;
switch (ev->response_type & 0x7F) {
case XCB_EXPOSE:
if (expose_ev->count == 0) redraw = 1;
break;
}
free (ev);
}
2012-07-16 23:17:11 +00:00
}
}
2012-07-17 09:34:12 +00:00
if (redraw) /* Copy our temporary pixmap onto the window */
2012-07-20 09:53:29 +00:00
xcb_copy_area (c, canvas, win, gc, 0, 0, 0, 0, bar_width, BAR_HEIGHT);
2012-07-16 23:17:11 +00:00
xcb_flush (c);
2012-07-16 09:41:34 +00:00
}
return 0;
}