lemonbar/bar.c

818 lines
24 KiB
C
Raw Normal View History

2012-07-24 10:08:02 +00:00
#include <stdbool.h>
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 <xcb/xinerama.h>
2012-07-16 09:41:34 +00:00
#include "config.h"
// Here be dragons
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
2014-02-08 15:45:38 +00:00
typedef struct font_t {
xcb_font_t ptr;
uint32_t descent;
uint32_t height;
uint16_t char_max;
uint16_t char_min;
xcb_charinfo_t *width_lut;
} font_t;
typedef struct monitor_t {
uint32_t x;
uint32_t width;
xcb_window_t window;
struct monitor_t *prev, *next;
} monitor_t;
2014-02-08 15:45:38 +00:00
struct config_t {
int place_bottom;
int force_docking;
int permanent;
int width;
int height;
float alpha;
char main_font[64];
char alt_font[64];
} cfg = {
0, 0, 0, -1, 18, 1.0f, "fixed", "fixed",
};
2012-07-24 10:25:45 +00:00
enum {
ALIGN_L = 0,
ALIGN_C,
ALIGN_R
};
xcb_connection_t *c;
xcb_screen_t *scr;
2014-02-08 15:45:38 +00:00
xcb_drawable_t canvas;
xcb_gcontext_t draw_gc;
xcb_gcontext_t clear_gc;
xcb_gcontext_t underl_gc;
2014-02-08 15:45:38 +00:00
monitor_t *monhead, *montail;
font_t *main_font, *alt_font;
2014-02-08 15:45:38 +00:00
const uint32_t palette[] = {
2014-02-08 15:45:38 +00:00
COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9,BACKGROUND,FOREGROUND
};
const char *control_characters = "_-fbulcrs";
2014-02-08 15:45:38 +00:00
void
set_bg (int i)
2012-07-17 09:34:12 +00:00
{
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] });
2012-07-17 09:34:12 +00:00
}
2014-02-08 15:45:38 +00:00
void
set_fg (int i)
{
2012-07-24 10:25:45 +00:00
xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
2012-07-18 15:11:17 +00:00
}
2014-02-08 15:45:38 +00:00
void
set_ud (int i)
2012-07-16 09:41:34 +00:00
{
2012-07-24 10:25:45 +00:00
xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
}
2014-02-08 15:45:38 +00:00
void
fill_rect (xcb_gcontext_t gc, int x, int y, int width, int height)
{
xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
}
2012-07-16 09:41:34 +00:00
int
2014-02-08 15:45:38 +00:00
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, int underline, uint16_t ch)
{
int ch_width;
2014-02-08 15:45:38 +00:00
ch_width = (ch > cur_font->char_min && ch < cur_font->char_max) ?
cur_font->width_lut[ch - cur_font->char_min].character_width :
0;
/* Some fonts (such as anorexia) have the space char with the width set to 0 */
if (ch_width == 0)
ch_width = BAR_FONT_FALLBACK_WIDTH;
2012-07-16 09:41:34 +00:00
switch (align) {
2012-07-24 10:25:45 +00:00
case ALIGN_C:
2014-02-08 15:45:38 +00:00
xcb_copy_area (c, canvas, canvas, draw_gc, mon->width / 2 - x / 2 + mon->x, 0,
mon->width / 2 - (x + ch_width) / 2 + mon->x, 0, x, cfg.height);
x = mon->width / 2 - (x + ch_width) / 2 + x;
2012-07-16 09:41:34 +00:00
break;
2012-07-24 10:25:45 +00:00
case ALIGN_R:
2014-02-08 15:45:38 +00:00
xcb_copy_area (c, canvas, canvas, draw_gc, mon->width - x + mon->x, 0,
mon->width - x - ch_width + mon->x, 0, x, cfg.height);
x = mon->width - ch_width;
2012-07-16 09:41:34 +00:00
break;
}
2012-07-16 20:56:19 +00:00
/* Draw the background first */
fill_rect (clear_gc, x + mon->x, 0, ch_width, cfg.height);
/* xcb accepts string in UCS-2 BE, so swap */
ch = (ch >> 8) | (ch << 8);
/* String baseline coordinates */
2014-02-08 15:45:38 +00:00
xcb_image_text_16 (c, 1, canvas, draw_gc, x + mon->x, cfg.height / 2 + cur_font->height / 2 - cur_font->descent,
(xcb_char2b_t *)&ch);
2014-02-08 15:45:38 +00:00
/* Draw the underline if -1, an overline if 1 */
if (BAR_UNDERLINE_HEIGHT && underline)
fill_rect (underl_gc, x + mon->x, (underline < 0)*(cfg.height-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT);
return ch_width;
2012-07-16 09:41:34 +00:00
}
void
parse (char *text)
{
char *p = text;
2012-07-16 09:41:34 +00:00
int pos_x = 0;
int align = 0;
2014-02-08 15:45:38 +00:00
int underline_flag = 0;
2012-07-16 09:41:34 +00:00
2014-02-08 15:45:38 +00:00
font_t *cur_font;
monitor_t *cur_mon;
cur_font = main_font;
cur_mon = monhead;
2014-02-08 15:45:38 +00:00
fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
2012-07-16 09:41:34 +00:00
for (;;) {
if (*p == '\0' || *p == '\n')
return;
2014-02-08 15:45:38 +00:00
if (*p == '^' && p++ && *p != '^' && strchr (control_characters, *p)) {
switch (*p++) {
case 'f':
set_fg (isdigit(*p) ? *p-'0' : 11);
2013-08-31 16:54:02 +00:00
p++;
break;
case 'b':
set_bg (isdigit(*p) ? *p-'0' : 10);
2013-08-31 16:54:02 +00:00
p++;
break;
case 'u':
set_ud (isdigit(*p) ? *p-'0' : 10);
2013-08-31 16:54:02 +00:00
p++;
break;
2014-02-08 15:45:38 +00:00
case '_':
underline_flag = (underline_flag == -1) ? 0 : -1;
break;
case '-':
underline_flag = (underline_flag == 1) ? 0 : 1;
break;
case 's':
/* montail only gets set with multiple monitors */
if (montail) {
if (*p == 'r') {
cur_mon = montail;
} else if (*p == 'l') {
cur_mon = monhead;
} else if (*p == 'n') {
if (cur_mon->next)
cur_mon = cur_mon->next;
} else if (*p == 'p') {
if (cur_mon->prev)
cur_mon = cur_mon->prev;
} else if (isdigit(*p)) {
cur_mon = monhead;
/* Start at 0 */
for (int i = 0; i != *p-'0' && cur_mon->next; i++)
cur_mon = cur_mon->next;
}
} else {
p++;
break;
}
2014-01-24 11:30:20 +00:00
/* Consume the argument */
p++;
align = ALIGN_L;
pos_x = 0;
break;
case 'l':
2012-07-24 10:25:45 +00:00
align = ALIGN_L;
pos_x = 0;
break;
case 'c':
2012-07-24 10:25:45 +00:00
align = ALIGN_C;
pos_x = 0;
break;
case 'r':
2012-07-24 10:25:45 +00:00
align = ALIGN_R;
pos_x = 0;
break;
}
2012-07-18 15:11:17 +00:00
} else { /* utf-8 -> ucs-2 */
uint8_t *utf = (uint8_t *)p;
uint16_t ucs;
if (utf[0] < 0x80) {
ucs = utf[0];
p += 1;
2012-07-18 15:11:17 +00:00
}
else if ((utf[0] & 0xe0) == 0xc0) {
ucs = (utf[0] & 0x1f) << 6 | (utf[1] & 0x3f);
p += 2;
2012-07-18 15:11:17 +00:00
}
else if ((utf[0] & 0xf0) == 0xe0) {
ucs = (utf[0] & 0xf) << 12 | (utf[1] & 0x3f) << 6 | (utf[2] & 0x3f);
p += 3;
}
else { /* Handle ascii > 0x80 */
ucs = utf[0];
p += 1;
2012-07-18 15:11:17 +00:00
}
2014-02-08 15:45:38 +00:00
/* If the character is outside the main font charset use the alternate font */
cur_font = (ucs < main_font->char_min || ucs > main_font->char_max) ? alt_font : main_font;
xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr });
2014-02-08 15:45:38 +00:00
pos_x += draw_char (cur_mon, cur_font, pos_x, align, underline_flag, ucs);
2012-07-18 15:11:17 +00:00
}
2012-07-16 09:41:34 +00:00
}
}
2014-02-08 15:45:38 +00:00
font_t *
font_load (char *font_string)
2012-07-16 09:41:34 +00:00
{
xcb_query_font_cookie_t queryreq;
xcb_query_font_reply_t *font_info;
xcb_void_cookie_t cookie;
xcb_font_t font;
2014-02-08 15:45:38 +00:00
font = xcb_generate_id (c);
2014-02-08 15:45:38 +00:00
cookie = xcb_open_font_checked (c, font, strlen (font_string), font_string);
if (xcb_request_check (c, cookie)) {
fprintf (stderr, "Could not load font %s\n", font_string);
return NULL;
}
2014-02-08 15:45:38 +00:00
font_t *ret = calloc(1, sizeof(font_t));
2014-02-08 15:45:38 +00:00
if (!ret)
return NULL;
2014-02-08 15:45:38 +00:00
queryreq = xcb_query_font (c, font);
font_info = xcb_query_font_reply (c, queryreq, NULL);
2014-02-08 15:45:38 +00:00
ret->ptr = font;
ret->width_lut = xcb_query_font_char_infos (font_info);
ret->descent = font_info->font_descent;
ret->char_max = font_info->max_byte1 << 8 | font_info->max_char_or_byte2;
ret->char_min = font_info->min_byte1 << 8 | font_info->min_char_or_byte2;
ret->height = font_info->font_ascent + font_info->font_descent;
2014-02-08 15:45:38 +00:00
return ret;
2012-07-16 09:41:34 +00:00
}
enum {
NET_WM_WINDOW_TYPE,
NET_WM_WINDOW_TYPE_DOCK,
NET_WM_DESKTOP,
NET_WM_STRUT_PARTIAL,
NET_WM_STRUT,
NET_WM_STATE,
NET_WM_WINDOW_OPACITY,
NET_WM_STATE_STICKY,
NET_WM_STATE_ABOVE,
};
void
set_ewmh_atoms (void)
2012-07-20 00:44:30 +00:00
{
const char *atom_names[] = {
"_NET_WM_WINDOW_TYPE",
"_NET_WM_WINDOW_TYPE_DOCK",
"_NET_WM_DESKTOP",
"_NET_WM_STRUT_PARTIAL",
"_NET_WM_STRUT",
"_NET_WM_STATE",
"_NET_WM_WINDOW_OPACITY",
/* Leave those at the end since are batch-set */
"_NET_WM_STATE_STICKY",
"_NET_WM_STATE_ABOVE",
};
const int atoms = sizeof(atom_names)/sizeof(char *);
xcb_intern_atom_cookie_t atom_cookie[atoms];
xcb_atom_t atom_list[atoms];
xcb_intern_atom_reply_t *atom_reply;
/* As suggested fetch all the cookies first (yum!) and then retrieve the
* atoms to exploit the async'ness */
for (int i = 0; i < atoms; i++)
atom_cookie[i] = xcb_intern_atom(c, 0, strlen(atom_names[i]), atom_names[i]);
for (int i = 0; i < atoms; i++) {
atom_reply = xcb_intern_atom_reply(c, atom_cookie[i], NULL);
if (!atom_reply)
return;
atom_list[i] = atom_reply->atom;
free(atom_reply);
}
/* Prepare the strut array */
for (monitor_t *mon = monhead; mon; mon = mon->next) {
2013-11-16 14:07:53 +00:00
int strut[12] = {0};
2014-02-08 15:45:38 +00:00
if (cfg.place_bottom) {
strut[3] = cfg.height;
strut[10] = mon->x;
strut[11] = mon->x + mon->width;
2013-11-16 14:07:53 +00:00
} else {
2014-02-08 15:45:38 +00:00
strut[2] = cfg.height;
strut[8] = mon->x;
strut[9] = mon->x + mon->width;
2013-11-16 14:07:53 +00:00
}
2014-02-08 15:45:38 +00:00
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_WINDOW_OPACITY], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ (uint32_t)(cfg.alpha * 0xffffffff) } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_WINDOW_TYPE], XCB_ATOM_ATOM, 32, 1, &atom_list[NET_WM_WINDOW_TYPE_DOCK]);
xcb_change_property (c, XCB_PROP_MODE_APPEND, mon->window, atom_list[NET_WM_STATE], XCB_ATOM_ATOM, 32, 2, &atom_list[NET_WM_STATE_STICKY]);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ -1 } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
2012-07-22 10:44:19 +00:00
}
2013-11-16 14:07:53 +00:00
}
2014-02-08 15:45:38 +00:00
monitor_t *
monitor_new (int x, int y, int width, int height)
2014-02-08 15:45:38 +00:00
{
monitor_t *ret;
ret = malloc(sizeof(monitor_t));
2014-02-08 15:45:38 +00:00
if (!ret)
return NULL;
ret->x = x;
ret->width = width;
ret->next = ret->prev = NULL;
2012-07-22 10:44:19 +00:00
int win_y = (cfg.place_bottom ? (height - cfg.height) : 0) + y;
ret->window = xcb_generate_id(c);
2013-11-16 14:07:53 +00:00
xcb_create_window(c, XCB_COPY_FROM_PARENT, ret->window, scr->root,
x, win_y, width, cfg.height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual,
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
(const uint32_t []){ palette[10], XCB_EVENT_MASK_EXPOSURE });
2014-02-08 15:45:38 +00:00
xcb_change_window_attributes (c, ret->window, XCB_CW_OVERRIDE_REDIRECT,
(const uint32_t []){ cfg.force_docking });
2014-02-08 15:45:38 +00:00
return ret;
}
void
get_randr_outputs(void)
{
int i, j, num, cnt = 0;
xcb_generic_error_t *err;
xcb_randr_get_screen_resources_current_cookie_t rres_query;
xcb_randr_get_screen_resources_current_reply_t *rres_reply;
xcb_randr_output_t *outputs;
xcb_timestamp_t config_timestamp;
rres_query = xcb_randr_get_screen_resources_current(c, scr->root);
rres_reply = xcb_randr_get_screen_resources_current_reply(c, rres_query, &err);
if (rres_reply == NULL || err != NULL) {
fprintf(stderr, "Failed to get current randr screen resources\n");
free(rres_reply);
return;
2014-02-08 15:45:38 +00:00
}
num = xcb_randr_get_screen_resources_current_outputs_length(rres_reply);
outputs = xcb_randr_get_screen_resources_current_outputs(rres_reply);
config_timestamp = rres_reply->config_timestamp;
free(rres_reply);
if (num < 1) {
fprintf(stderr, "Failed to get current randr outputs\n");
return;
}
xcb_rectangle_t rects[num];
/* get all outputs */
for (i = 0; i < num; i++) {
xcb_randr_get_output_info_cookie_t output_query;
xcb_randr_get_output_info_reply_t *output_reply;
xcb_randr_get_crtc_info_cookie_t crtc_query;
xcb_randr_get_crtc_info_reply_t *crtc_reply;
output_query = xcb_randr_get_output_info(c, outputs[i], config_timestamp);
output_reply = xcb_randr_get_output_info_reply(c, output_query, &err);
if (err != NULL || output_reply == NULL || output_reply->crtc == XCB_NONE) {
rects[i].width = 0;
continue;
}
crtc_query = xcb_randr_get_crtc_info(c, output_reply->crtc, config_timestamp);
crtc_reply = xcb_randr_get_crtc_info_reply(c, crtc_query, &err);
if (err != NULL | crtc_reply == NULL) {
fprintf(stderr, "Failed to get randr ctrc info\n");
rects[i].width = 0;
free(output_reply);
continue;
}
rects[i].x = crtc_reply->x;
rects[i].y = crtc_reply->y;
rects[i].width = crtc_reply->width;
rects[i].height = crtc_reply->height;
free(crtc_reply);
free(output_reply);
cnt++;
}
if (cnt < 1) {
fprintf(stderr, "No usable randr outputs\n");
return;
}
/* check for clones and inactive outputs */
for (i = 0; i < num; i++) {
if (rects[i].width == 0)
continue;
for (j = 0; j < num; j++) {
if (i == j || rects[j].width == 0 || rects[i].x != rects[j].x || rects[i].y != rects[j].y)
continue;
/* clone found, only keep one */
rects[i].width = (rects[i].width < rects[j].width) ? rects[i].width : rects[j].width;
rects[i].height = (rects[i].height < rects[j].height) ? rects[i].height : rects[j].height;
rects[j].width = 0;
cnt--;
}
}
if (cnt < 1) {
fprintf(stderr, "No usable randr outputs\n");
return;
}
int width = cfg.width;
for (i = j = 0; i < num && j < cnt; i++) {
if (rects[i].width) {
monitor_t *mon = monitor_new (
rects[i].x,
rects[i].y,
MIN(width, rects[i].width),
rects[i].height);
if (!mon) {
fprintf(stderr, "Failed to allocate new monitor\n");
exit(EXIT_FAILURE);
}
if (!monhead) {
monhead = mon;
}
else if (!montail) {
montail = mon;
monhead->next = mon;
mon->prev = monhead;
}
else {
mon->prev = montail;
montail->next = mon;
montail = montail->next;
}
width -= rects[i].width;
/* No need to check for other monitors */
if (width <= 0)
break;
j++;
}
}
2012-07-20 00:44:30 +00:00
}
2012-07-16 09:41:34 +00:00
void
init (void)
{
/* Connect to X */
c = xcb_connect (NULL, NULL);
if (xcb_connection_has_error (c)) {
fprintf (stderr, "Couldn't connect to X\n");
2014-02-08 15:45:38 +00:00
exit (EXIT_FAILURE);
2012-07-16 09:41:34 +00:00
}
2012-07-16 09:41:34 +00:00
/* Grab infos from the first screen */
2012-07-20 09:53:29 +00:00
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
2014-02-08 15:45:38 +00:00
/* If I fits I sits */
if (cfg.width < 0 || cfg.width > scr->width_in_pixels)
2014-02-08 15:45:38 +00:00
cfg.width = scr->width_in_pixels;
2014-02-08 15:45:38 +00:00
/* Load the fonts */
main_font = font_load((char *)cfg.main_font);
if (!main_font)
exit(EXIT_FAILURE);
alt_font = font_load((char *)cfg.alt_font);
if (!alt_font)
exit(EXIT_FAILURE);
/* To make the alignment uniform */
main_font->height = alt_font->height = MAX(main_font->height, alt_font->height);
/* Generate a list of screens */
const xcb_query_extension_reply_t *qe_reply;
int width = cfg.width;
/* Initialiaze monitor list head and tail */
monhead = montail = NULL;
/* Check if RandR is present */
qe_reply = xcb_get_extension_data (c, &xcb_randr_id);
if (qe_reply && qe_reply->present) {
get_randr_outputs();
} else {
int xia = 0;
qe_reply = xcb_get_extension_data (c, &xcb_xinerama_id);
2013-11-16 14:07:53 +00:00
/* Check if Xinerama extension is present and active */
if (qe_reply && qe_reply->present) {
xcb_xinerama_is_active_reply_t *xia_reply;
xia_reply = xcb_xinerama_is_active_reply (c, xcb_xinerama_is_active (c), NULL);
if (xia_reply && xia_reply->state)
xia = 1;
free (xia_reply);
}
2014-02-08 15:45:38 +00:00
if (xia) {
xcb_xinerama_query_screens_reply_t *xqs_reply;
xcb_xinerama_screen_info_iterator_t iter;
xqs_reply = xcb_xinerama_query_screens_reply (c,
xcb_xinerama_query_screens_unchecked (c), NULL);
iter = xcb_xinerama_query_screens_screen_info_iterator (xqs_reply);
2014-02-08 15:45:38 +00:00
/* The width is consumed across all the screens */
while (iter.rem) {
monitor_t *mon = monitor_new (
iter.data->x_org,
iter.data->y_org,
MIN(width, iter.data->width),
iter.data->height);
if (!monhead) {
monhead = mon;
}
else if (!montail) {
montail = mon;
monhead->next = mon;
mon->prev = monhead;
}
else {
mon->prev = montail;
montail->next = mon;
montail = montail->next;
}
width -= iter.data->width;
/* No need to check for other monitors */
if (width <= 0)
break;
xcb_xinerama_screen_info_next (&iter);
}
free (xqs_reply);
2014-02-08 15:45:38 +00:00
}
}
if (!monhead) {
fprintf(stderr, "Falling back to using whole screen mode\n");
monhead = monitor_new (0, 0, width, scr->height_in_pixels);
}
if (!monhead) {
fprintf(stderr, "Failed to retrieve any usable monitors!\n");
exit(EXIT_FAILURE);
2014-01-24 11:30:20 +00:00
}
2014-02-08 15:45:38 +00:00
if (!mons)
exit(EXIT_FAILURE);
/* For WM that support EWMH atoms */
set_ewmh_atoms();
2012-07-16 20:56:19 +00:00
/* Create a temporary canvas */
canvas = xcb_generate_id (c);
xcb_create_pixmap (c, scr->root_depth, canvas, scr->root, cfg.width, cfg.height);
2012-07-16 09:41:34 +00:00
/* Create the gc for drawing */
draw_gc = xcb_generate_id (c);
xcb_create_gc (c, draw_gc, scr->root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t []){ palette[11], palette[10] });
clear_gc = xcb_generate_id (c);
xcb_create_gc (c, clear_gc, scr->root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
underl_gc = xcb_generate_id (c);
xcb_create_gc (c, underl_gc, scr->root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
/* Make the bar visible */
for (monitor_t *mon = monhead; mon; mon = mon->next)
xcb_map_window(c, mon->window);
2012-07-18 15:11:17 +00:00
xcb_flush (c);
2012-07-16 09:41:34 +00:00
}
void
cleanup (void)
{
2014-02-08 15:45:38 +00:00
if (main_font) {
xcb_close_font (c, main_font->ptr);
free(main_font);
}
2014-02-08 15:45:38 +00:00
if (alt_font) {
xcb_close_font (c, alt_font->ptr);
free(alt_font);
}
while (monhead) {
monitor_t *next = monhead->next;
free(monhead);
monhead = next;
2013-11-16 14:07:53 +00:00
}
2014-02-08 15:45:38 +00:00
2012-07-24 12:12:04 +00:00
if (canvas)
xcb_free_pixmap (c, canvas);
if (draw_gc)
xcb_free_gc (c, draw_gc);
if (clear_gc)
xcb_free_gc (c, clear_gc);
if (underl_gc)
xcb_free_gc (c, underl_gc);
if (c)
xcb_disconnect (c);
}
void
sighandle (int signal)
{
if (signal == SIGINT || signal == SIGTERM)
2014-02-08 15:45:38 +00:00
exit (EXIT_SUCCESS);
}
/* Parse an urxvt-like geometry string {width}x{height}, both the fields are
* optional. A width of -1 means that the bar spawns the whole screen. */
void
parse_geometry_string (char *str)
{
char *p, *q;
int tmp;
if (!str)
return;
p = str;
tmp = strtoul(p, &q, 10);
if (p != q)
cfg.width = tmp;
/* P now might point to a NULL char, strtoul takes care of that */
p = q + 1;
tmp = strtoul(p, &q, 10);
if (p != q)
cfg.height = tmp;
}
void
parse_font_list (char *str)
{
char *tok;
if (!str)
return;
tok = strtok(str, ",");
if (tok)
strncpy(cfg.main_font, tok, sizeof(cfg.main_font));
tok = strtok(NULL, ",");
if (tok)
strncpy(cfg.alt_font, tok, sizeof(cfg.alt_font));
return;
}
2012-07-16 09:41:34 +00:00
int
main (int argc, char **argv)
2012-07-16 09:41:34 +00:00
{
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;
char ch;
2014-02-08 15:45:38 +00:00
while ((ch = getopt (argc, argv, "hg:bdf:a:o:p")) != -1) {
switch (ch) {
case 'h':
2014-02-08 15:45:38 +00:00
printf ("usage: %s [-h | -g | -b | -d | -f | -a | -o | -p ]\n"
2012-07-24 10:08:02 +00:00
"\t-h Show this help\n"
2014-02-08 15:45:38 +00:00
"\t-g Set the bar geometry {width}x{height})\n"
2012-07-24 10:08:02 +00:00
"\t-b Put bar at the bottom of the screen\n"
2014-02-08 15:45:38 +00:00
"\t-d Force docking (use this if your WM isn't EWMH compliant)\n"
"\t-f Bar font list, comma separated\n"
"\t-a Set the bar alpha ranging from 0.0 to 1.0 (requires a compositor)\n"
2012-07-17 09:34:12 +00:00
"\t-p Don't close after the data ends\n", argv[0]);
2014-02-08 15:45:38 +00:00
exit (EXIT_SUCCESS);
case 'a': cfg.alpha = strtof(optarg, NULL); break;
case 'g': parse_geometry_string(optarg); break;
case 'p': cfg.permanent = 1; break;
case 'b': cfg.place_bottom = 1; break;
case 'd': cfg.force_docking = 1; break;
case 'f': parse_font_list(optarg); break;
}
}
2012-07-16 09:41:34 +00:00
2014-02-08 15:45:38 +00:00
/* Sanitize the arguments */
if (cfg.alpha > 1.0f)
cfg.alpha = 1.0f;
if (cfg.alpha < 0.0f)
cfg.alpha = 0.0f;
2012-07-24 12:12:04 +00:00
atexit (cleanup);
2012-07-16 09:41:34 +00:00
signal (SIGINT, sighandle);
signal (SIGTERM, sighandle);
init ();
/* Get the fd to Xserver */
pollin[1].fd = xcb_get_file_descriptor (c);
fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
2012-07-17 09:34:12 +00:00
2012-07-16 23:17:11 +00:00
for (;;) {
int redraw = 0;
2012-07-24 10:25:45 +00:00
if (poll (pollin, 2, -1) > 0) {
2014-02-08 15:45:38 +00:00
if (pollin[0].revents & POLLHUP) { /* No more data... */
if (cfg.permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
else break; /* ...bail out */
2012-07-18 19:27:41 +00:00
}
if (pollin[0].revents & POLLIN) { /* New input, process it */
if (fgets (input, sizeof(input), stdin) == NULL)
break; /* EOF received */
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
2014-01-24 11:30:20 +00:00
if (redraw) { /* Copy our temporary pixmap onto the window */
for (monitor_t *mon = monhead; mon; mon = mon->next) {
// if (mon->width)
2014-02-08 15:45:38 +00:00
xcb_copy_area (c, canvas, mon->window, draw_gc, mon->x, 0, 0, 0, mon->width, cfg.height);
2014-01-24 11:30:20 +00:00
}
}
2012-07-16 23:17:11 +00:00
xcb_flush (c);
2012-07-16 09:41:34 +00:00
}
return 0;
}