So revamp. Much flexible. Such wow.

This commit is contained in:
LemonBoy 2014-02-08 15:45:38 +00:00
parent e8ec2fcfe5
commit edc2e2c61c
3 changed files with 286 additions and 240 deletions

View File

@ -1,12 +1,7 @@
CC ?= gcc CC ?= gcc
STRIP ?= strip STRIP ?= strip
CFLAGS = -std=c99 -O2 CFLAGS = -std=c99 -O2
LDFLAGS = -lxcb LDFLAGS = -lxcb -lxcb-xinerama
XINERAMA ?= 0
ifneq "$(XINERAMA)" "0"
LDFLAGS += -lxcb-xinerama
CFLAGS += -DXINERAMA=${XINERAMA}
endif
CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\ CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\
-Wsign-conversion -Wconversion -Wimplicit-function-declaration -Wsign-conversion -Wconversion -Wimplicit-function-declaration

503
bar.c
View File

@ -8,9 +8,7 @@
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
#if XINERAMA
#include <xcb/xinerama.h> #include <xcb/xinerama.h>
#endif
#include "config.h" #include "config.h"
@ -19,19 +17,33 @@
#define MAX(a,b) ((a > b) ? a : b) #define MAX(a,b) ((a > b) ? a : b)
#define MIN(a,b) ((a < b) ? a : b) #define MIN(a,b) ((a < b) ? a : b)
typedef struct fontset_item_t { typedef struct font_t {
xcb_font_t xcb_ft; xcb_font_t ptr;
xcb_charinfo_t *table; uint32_t descent;
int descent; uint32_t height;
int avg_height; uint16_t char_max;
unsigned short char_max; uint16_t char_min;
unsigned short char_min; xcb_charinfo_t *width_lut;
} fontset_item_t; } font_t;
enum { typedef struct monitor_t {
FONT_MAIN, uint32_t x;
FONT_FALLBACK, uint32_t width;
FONT_MAX xcb_window_t window;
struct monitor_t *prev, *next;
} monitor_t;
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",
}; };
enum { enum {
@ -40,73 +52,54 @@ enum {
ALIGN_R ALIGN_R
}; };
typedef struct screen_t {
int x;
int width;
xcb_window_t window;
} screen_t;
static xcb_connection_t *c; static xcb_connection_t *c;
static xcb_drawable_t canvas; static xcb_drawable_t canvas;
static xcb_gcontext_t draw_gc; static xcb_gcontext_t draw_gc;
static xcb_gcontext_t clear_gc; static xcb_gcontext_t clear_gc;
static xcb_gcontext_t underl_gc; static xcb_gcontext_t underl_gc;
static int bar_width;
static int bar_bottom = BAR_BOTTOM;
static int force_docking = 0;
static fontset_item_t fontset[FONT_MAX];
static fontset_item_t *sel_font = NULL;
static screen_t *screens;
static int num_screens;
static const unsigned palette[] = {COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9,BACKGROUND,FOREGROUND};
#if XINERAMA static monitor_t *mons;
static const char *control_characters = "fbulcsr"; static font_t *main_font, *alt_font;
#else
static const char *control_characters = "fbulcr";
#endif
static inline void static const uint32_t palette[] = {
COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9,BACKGROUND,FOREGROUND
};
static const char *control_characters = "_-fbulcrs";
void
xcb_set_bg (int i) xcb_set_bg (int i)
{ {
xcb_change_gc (c, draw_gc , XCB_GC_BACKGROUND, (const unsigned []){ palette[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, clear_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] });
} }
static inline void void
xcb_set_fg (int i) xcb_set_fg (int i)
{ {
xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] }); xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
} }
static inline void void
xcb_set_ud (int i) xcb_set_ud (int i)
{ {
xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] }); xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
} }
static inline void void
xcb_fill_rect (xcb_gcontext_t gc, int x, int y, int width, int height) xcb_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 } }); xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
} }
static inline void
xcb_set_fontset (int i)
{
if (sel_font != &fontset[i]) {
sel_font = &fontset[i];
xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ sel_font->xcb_ft });
}
}
int int
draw_char (screen_t *screen, int x, int align, uint16_t ch) draw_char (monitor_t *mon, font_t *cur_font, int x, int align, int underline, uint16_t ch)
{ {
int ch_width; int ch_width;
ch_width = (ch > sel_font->char_min && ch < sel_font->char_max) ? ch_width = (ch > cur_font->char_min && ch < cur_font->char_max) ?
sel_font->table[ch - sel_font->char_min].character_width : cur_font->width_lut[ch - cur_font->char_min].character_width :
0; 0;
/* Some fonts (such as anorexia) have the space char with the width set to 0 */ /* Some fonts (such as anorexia) have the space char with the width set to 0 */
@ -115,30 +108,30 @@ draw_char (screen_t *screen, int x, int align, uint16_t ch)
switch (align) { switch (align) {
case ALIGN_C: case ALIGN_C:
xcb_copy_area (c, canvas, canvas, draw_gc, screen->width / 2 - x / 2 + screen->x, 0, xcb_copy_area (c, canvas, canvas, draw_gc, mon->width / 2 - x / 2 + mon->x, 0,
screen->width / 2 - (x + ch_width) / 2 + screen->x, 0, x, BAR_HEIGHT); mon->width / 2 - (x + ch_width) / 2 + mon->x, 0, x, cfg.height);
x = screen->width / 2 - (x + ch_width) / 2 + x; x = mon->width / 2 - (x + ch_width) / 2 + x;
break; break;
case ALIGN_R: case ALIGN_R:
xcb_copy_area (c, canvas, canvas, draw_gc, screen->width - x + screen->x, 0, xcb_copy_area (c, canvas, canvas, draw_gc, mon->width - x + mon->x, 0,
screen->width - x - ch_width + screen->x, 0, x, BAR_HEIGHT); mon->width - x - ch_width + mon->x, 0, x, cfg.height);
x = screen->width - ch_width; x = mon->width - ch_width;
break; break;
} }
/* Draw the background first */ /* Draw the background first */
xcb_fill_rect (clear_gc, x + screen->x, 0, ch_width, BAR_HEIGHT); xcb_fill_rect (clear_gc, x + mon->x, 0, ch_width, cfg.height);
/* xcb accepts string in UCS-2 BE, so swap */ /* xcb accepts string in UCS-2 BE, so swap */
ch = (ch >> 8) | (ch << 8); ch = (ch >> 8) | (ch << 8);
/* String baseline coordinates */ /* String baseline coordinates */
xcb_image_text_16 (c, 1, canvas, draw_gc, x + screen->x, BAR_HEIGHT / 2 + sel_font->avg_height / 2 - sel_font->descent, 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); (xcb_char2b_t *)&ch);
/* Draw the underline */ /* Draw the underline if -1, an overline if 1 */
if (BAR_UNDERLINE_HEIGHT) if (BAR_UNDERLINE_HEIGHT && underline)
xcb_fill_rect (underl_gc, x + screen->x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT); xcb_fill_rect (underl_gc, x + mon->x, (underline < 0)*(cfg.height-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT);
return ch_width; return ch_width;
} }
@ -150,16 +143,21 @@ parse (char *text)
int pos_x = 0; int pos_x = 0;
int align = 0; int align = 0;
int screen_idx = 0; int underline_flag = 0;
screen_t *screen_ptr = &screens[screen_idx];
xcb_fill_rect (clear_gc, 0, 0, bar_width, BAR_HEIGHT); font_t *cur_font;
monitor_t *cur_mon;
cur_font = main_font;
cur_mon = mons;
xcb_fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
for (;;) { for (;;) {
if (*p == '\0' || *p == '\n') if (*p == '\0' || *p == '\n')
return; return;
if (*p == '\\' && p++ && *p != '\\' && strchr (control_characters, *p)) { if (*p == '^' && p++ && *p != '^' && strchr (control_characters, *p)) {
switch (*p++) { switch (*p++) {
case 'f': case 'f':
xcb_set_fg (isdigit(*p) ? *p-'0' : 11); xcb_set_fg (isdigit(*p) ? *p-'0' : 11);
@ -173,36 +171,37 @@ parse (char *text)
xcb_set_ud (isdigit(*p) ? *p-'0' : 10); xcb_set_ud (isdigit(*p) ? *p-'0' : 10);
p++; p++;
break; break;
#if XINERAMA
case '_':
underline_flag = (underline_flag == -1) ? 0 : -1;
break;
case '-':
underline_flag = (underline_flag == 1) ? 0 : 1;
break;
case 's': case 's':
if (*p == 'r') if (*p == 'r')
screen_idx = num_screens - 1; cur_mon = mons;
else if (*p == 'l') else if (*p == 'l')
screen_idx = 0; { while (cur_mon->next) cur_mon = cur_mon->next; }
else if (*p == 'n') else if (*p == 'n')
screen_idx++; { if (cur_mon->next) cur_mon = cur_mon->next; }
else if (*p == 'p') else if (*p == 'p')
screen_idx--; { if (cur_mon->prev) cur_mon = cur_mon->prev; }
else if (isdigit(*p)) else if (isdigit(*p)) {
screen_idx = *p-'0'; cur_mon = mons;
/* Start at 1 */
for (int i = 1; i <= *p-'0' && cur_mon->next; i++)
cur_mon = cur_mon->next;
}
else else
break; break;
/* Consume the argument */ /* Consume the argument */
p++; p++;
/* Sanity checks */
if (screen_idx < 0)
screen_idx = 0;
if (screen_idx > num_screens - 1)
screen_idx = num_screens - 1;
screen_ptr = &screens[screen_idx];
align = ALIGN_L; align = ALIGN_L;
pos_x = 0; pos_x = 0;
break; break;
#endif
case 'l': case 'l':
align = ALIGN_L; align = ALIGN_L;
pos_x = 0; pos_x = 0;
@ -217,10 +216,9 @@ parse (char *text)
break; break;
} }
} else { /* utf-8 -> ucs-2 */ } else { /* utf-8 -> ucs-2 */
#if XINERAMA if (!cur_mon->window)
if (!screen_ptr->window)
continue; continue;
#endif
uint8_t *utf = (uint8_t *)p; uint8_t *utf = (uint8_t *)p;
uint16_t ucs; uint16_t ucs;
@ -241,54 +239,48 @@ parse (char *text)
p += 1; p += 1;
} }
/* The character is outside the main font charset, use the fallback */ /* If the character is outside the main font charset use the alternate font */
if (ucs < fontset[FONT_MAIN].char_min || ucs > fontset[FONT_MAIN].char_max) cur_font = (ucs < main_font->char_min || ucs > main_font->char_max) ? alt_font : main_font;
xcb_set_fontset (FONT_FALLBACK);
else
xcb_set_fontset (FONT_MAIN);
pos_x += draw_char (screen_ptr, pos_x, align, ucs); xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr });
pos_x += draw_char (cur_mon, cur_font, pos_x, align, underline_flag, ucs);
} }
} }
} }
int font_t *
font_load (const char **font_list) font_load (char *font_string)
{ {
xcb_query_font_cookie_t queryreq; xcb_query_font_cookie_t queryreq;
xcb_query_font_reply_t *font_info; xcb_query_font_reply_t *font_info;
xcb_void_cookie_t cookie; xcb_void_cookie_t cookie;
xcb_font_t font; xcb_font_t font;
int max_height;
max_height = -1; font = xcb_generate_id (c);
for (int i = 0; i < FONT_MAX; i++) { cookie = xcb_open_font_checked (c, font, strlen (font_string), font_string);
font = xcb_generate_id (c); if (xcb_request_check (c, cookie)) {
fprintf (stderr, "Could not load font %s\n", font_string);
cookie = xcb_open_font_checked (c, font, strlen (font_list[i]), font_list[i]); return NULL;
if (xcb_request_check (c, cookie)) {
fprintf (stderr, "Could not load font %s\n", font_list[i]);
return 1;
}
queryreq = xcb_query_font (c, font);
font_info = xcb_query_font_reply (c, queryreq, NULL);
fontset[i].xcb_ft = font;
fontset[i].table = xcb_query_font_char_infos (font_info);
fontset[i].descent = font_info->font_descent;
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);
} }
/* To have an uniform alignment */ font_t *ret = calloc(1, sizeof(font_t));
for (int i = 0; i < FONT_MAX; i++)
fontset[i].avg_height = max_height;
return 0; if (!ret)
return NULL;
queryreq = xcb_query_font (c, font);
font_info = xcb_query_font_reply (c, queryreq, NULL);
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;
return ret;
} }
enum { enum {
@ -337,35 +329,53 @@ set_ewmh_atoms (void)
} }
/* Prepare the strut array */ /* Prepare the strut array */
for (int i = 0; i < num_screens; i++) { for (monitor_t *mon = mons; mon; mon = mon->next) {
int strut[12] = {0}; int strut[12] = {0};
if (bar_bottom) { if (cfg.place_bottom) {
strut[3] = BAR_HEIGHT; strut[3] = cfg.height;
strut[10] = screens[i].x; strut[10] = mon->x;
strut[11] = screens[i].x + screens[i].width; strut[11] = mon->x + mon->width;
} else { } else {
strut[2] = BAR_HEIGHT; strut[2] = cfg.height;
strut[8] = screens[i].x; strut[8] = mon->x;
strut[9] = screens[i].x + screens[i].width; strut[9] = mon->x + mon->width;
} }
xcb_change_property (c, XCB_PROP_MODE_REPLACE, screens[i].window, atom_list[NET_WM_WINDOW_OPACITY], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ (uint32_t)(BAR_OPACITY * 0xffffffff) } ); 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, screens[i].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_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, screens[i].window, atom_list[NET_WM_STATE], XCB_ATOM_ATOM, 32, 2, &atom_list[NET_WM_STATE_STICKY]); 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, screens[i].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_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ -1 } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, screens[i].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_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, screens[i].window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut); xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
} }
} }
xcb_window_t monitor_t *
create_window(xcb_window_t root, int x, int y, int width, int height, xcb_visualid_t visual) { monitor_new (int x, int y, int width, int height, xcb_window_t root, xcb_visualid_t visual)
xcb_window_t window = xcb_generate_id(c); {
xcb_create_window(c, XCB_COPY_FROM_PARENT, window, root, x, y, width, height, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, visual, XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const uint32_t []){ palette[10], XCB_EVENT_MASK_EXPOSURE }); monitor_t *ret;
xcb_change_window_attributes (c, window, XCB_CW_OVERRIDE_REDIRECT, (const uint32_t []){ force_docking }); ret = calloc(1, sizeof(monitor_t));
if (!ret)
return NULL;
return window; ret->x = x;
ret->width = width;
if (width) {
int win_y = (cfg.place_bottom ? (height - cfg.height) : 0) + y;
ret->window = xcb_generate_id(c);
xcb_create_window(c, XCB_COPY_FROM_PARENT, ret->window, root, x, win_y, width, cfg.height, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, visual, XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
(const uint32_t []){ palette[10], XCB_EVENT_MASK_EXPOSURE });
xcb_change_window_attributes (c, ret->window, XCB_CW_OVERRIDE_REDIRECT, (const uint32_t []){ cfg.force_docking });
xcb_map_window(c, ret->window);
}
return ret;
} }
void void
@ -374,84 +384,88 @@ init (void)
xcb_screen_t *scr; xcb_screen_t *scr;
xcb_window_t root; xcb_window_t root;
#if XINERAMA
xcb_generic_error_t* err = NULL;
xcb_xinerama_query_screens_reply_t *xinerama_reply;
xcb_xinerama_query_screens_cookie_t xinerama_query;
xcb_xinerama_screen_info_iterator_t xinerama_iter;
#endif
/* Connect to X */ /* Connect to X */
c = xcb_connect (NULL, NULL); c = xcb_connect (NULL, NULL);
if (xcb_connection_has_error (c)) { if (xcb_connection_has_error (c)) {
fprintf (stderr, "Couldn't connect to X\n"); fprintf (stderr, "Couldn't connect to X\n");
exit (1); exit (EXIT_FAILURE);
} }
/* 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;
root = scr->root; root = scr->root;
/* where to place the window */ /* If I fits I sits */
bar_width = (BAR_WIDTH < 0) ? (scr->width_in_pixels - BAR_OFFSET) : BAR_WIDTH; if (cfg.width < 0)
cfg.width = scr->width_in_pixels;
/* Load the font */ /* Load the fonts */
if (font_load ((const char* []){ BAR_FONT })) main_font = font_load((char *)cfg.main_font);
exit (1); 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 */ /* Generate a list of screens */
#if XINERAMA xcb_xinerama_is_active_reply_t *xia_reply;
xinerama_query = xcb_xinerama_query_screens_unchecked (c);
xinerama_reply = xcb_xinerama_query_screens_reply (c, xinerama_query, &err);
if (err != NULL || xinerama_reply == NULL)
exit (1);
xinerama_iter = xcb_xinerama_query_screens_screen_info_iterator (xinerama_reply); xia_reply = xcb_xinerama_is_active_reply (c, xcb_xinerama_is_active (c), NULL);
num_screens = xinerama_iter.rem; if (xia_reply && xia_reply->state) {
screens = calloc (num_screens, sizeof(screen_t)); xcb_xinerama_query_screens_reply_t *xqs_reply;
xcb_xinerama_screen_info_iterator_t iter;
if (screens == NULL) xqs_reply = xcb_xinerama_query_screens_reply (c, xcb_xinerama_query_screens_unchecked (c), NULL);
exit (1);
/* Consume the offset across all the monitors */ iter = xcb_xinerama_query_screens_screen_info_iterator (xqs_reply);
int left_offset = BAR_OFFSET;
for (int i = 0; i < num_screens; i++) {
screens[i].width = xinerama_iter.data->width;
screens[i].x = xinerama_iter.data->x_org;
if (screens[i].width >= left_offset) { /* The width is consumed across all the screens */
screens[i].width -= left_offset; int width_to_consume = (cfg.width == scr->width_in_pixels) ? 0 : cfg.width;
screens[i].x += left_offset;
int y = (bar_bottom ? (xinerama_iter.data->height - BAR_HEIGHT) : 0) + xinerama_iter.data->y_org; while (iter.rem) {
screens[i].window = create_window(root, screens[i].x, y, screens[i].width, BAR_HEIGHT, scr->root_visual); monitor_t *mon = monitor_new (
iter.data->x_org + ((iter.data->width > width_to_consume) ? width_to_consume : 0),
iter.data->y_org + cfg.place_bottom ? (iter.data->height - cfg.height) : 0,
iter.data->width - MIN(iter.data->width, width_to_consume),
iter.data->height,
root, scr->root_visual);
mon->prev = mons;
if (!mons)
mons = mon;
else
mons->next = mon;
if (width_to_consume)
width_to_consume -= MIN(iter.data->width, width_to_consume);
xcb_xinerama_screen_info_next (&iter);
} }
}
if (left_offset) else {
left_offset -= MIN(screens[i].width, left_offset); mons = monitor_new (
0,
xcb_xinerama_screen_info_next (&xinerama_iter); cfg.place_bottom ? (scr->height_in_pixels - cfg.height) : 0,
cfg.width,
scr->height_in_pixels,
root, scr->root_visual);
} }
free(xinerama_reply); if (!mons)
#else exit(EXIT_FAILURE);
num_screens = 1;
screens = calloc(1, sizeof(screen_t));
/* Take into account the offset */
screens[0].x = BAR_OFFSET;
screens[0].width = bar_width - BAR_OFFSET;
/* Create the main window */
int y = bar_bottom ? (scr->height_in_pixels - BAR_HEIGHT) : 0;
screens->window = create_window(root, screens->x, y, screens->width, BAR_HEIGHT, scr->root_visual);
#endif
/* For WM that support EWMH atoms */ /* For WM that support EWMH atoms */
set_ewmh_atoms(); set_ewmh_atoms();
/* 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, bar_width, BAR_HEIGHT); xcb_create_pixmap (c, scr->root_depth, canvas, root, cfg.width, cfg.height);
/* Create the gc for drawing */ /* Create the gc for drawing */
draw_gc = xcb_generate_id (c); draw_gc = xcb_generate_id (c);
@ -463,30 +477,28 @@ init (void)
underl_gc = xcb_generate_id (c); underl_gc = xcb_generate_id (c);
xcb_create_gc (c, underl_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] }); xcb_create_gc (c, underl_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
/* Make the bar visible */
for (int i = 0; i < num_screens; i++) {
if (screens[i].window)
xcb_map_window (c, screens[i].window);
}
xcb_flush (c); xcb_flush (c);
} }
void void
cleanup (void) cleanup (void)
{ {
int i; if (main_font) {
for (i = 0; i < FONT_MAX; i++) { xcb_close_font (c, main_font->ptr);
if (fontset[i].xcb_ft) free(main_font);
xcb_close_font (c, fontset[i].xcb_ft);
} }
if (screens) {
for (int i = 0; i < num_screens; i++) { if (alt_font) {
if (screens[i].window) xcb_close_font (c, alt_font->ptr);
xcb_destroy_window (c, screens[i].window); free(alt_font);
}
free (screens);
} }
while (mons) {
monitor_t *next = mons->next;
free(mons);
mons = next;
}
if (canvas) if (canvas)
xcb_free_pixmap (c, canvas); xcb_free_pixmap (c, canvas);
if (draw_gc) if (draw_gc)
@ -503,7 +515,50 @@ void
sighandle (int signal) sighandle (int signal)
{ {
if (signal == SIGINT || signal == SIGTERM) if (signal == SIGINT || signal == SIGTERM)
exit (0); 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;
} }
int int
@ -518,24 +573,34 @@ main (int argc, char **argv)
xcb_generic_event_t *ev; xcb_generic_event_t *ev;
xcb_expose_event_t *expose_ev; xcb_expose_event_t *expose_ev;
int permanent = 0;
char ch; char ch;
while ((ch = getopt (argc, argv, "phbf")) != -1) { while ((ch = getopt (argc, argv, "hg:bdf:a:o:p")) != -1) {
switch (ch) { switch (ch) {
case 'h': case 'h':
printf ("usage: %s [-p | -h] [-b]\n" printf ("usage: %s [-h | -g | -b | -d | -f | -a | -o | -p ]\n"
"\t-h Show this help\n" "\t-h Show this help\n"
"\t-g Set the bar geometry {width}x{height})\n"
"\t-b Put bar at the bottom of the screen\n" "\t-b Put bar at the bottom of the screen\n"
"\t-f Force docking (use this if your WM isn't EWMH compliant)\n" "\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"
"\t-p Don't close after the data ends\n", argv[0]); "\t-p Don't close after the data ends\n", argv[0]);
exit (0); exit (EXIT_SUCCESS);
case 'p': permanent = 1; break; case 'a': cfg.alpha = strtof(optarg, NULL); break;
case 'b': bar_bottom = 1; break; case 'g': parse_geometry_string(optarg); break;
case 'f': force_docking = 1; 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;
} }
} }
/* Sanitize the arguments */
if (cfg.alpha > 1.0f)
cfg.alpha = 1.0f;
if (cfg.alpha < 0.0f)
cfg.alpha = 0.0f;
atexit (cleanup); atexit (cleanup);
signal (SIGINT, sighandle); signal (SIGINT, sighandle);
signal (SIGTERM, sighandle); signal (SIGTERM, sighandle);
@ -544,15 +609,15 @@ 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);
xcb_fill_rect (clear_gc, 0, 0, bar_width, BAR_HEIGHT); xcb_fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
for (;;) { for (;;) {
int redraw = 0; int redraw = 0;
if (poll (pollin, 2, -1) > 0) { if (poll (pollin, 2, -1) > 0) {
if (pollin[0].revents & POLLHUP) { /* No more data... */ if (pollin[0].revents & POLLHUP) { /* No more data... */
if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */ if (cfg.permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
else break; /* ...bail out */ else break; /* ...bail out */
} }
if (pollin[0].revents & POLLIN) { /* New input, process it */ if (pollin[0].revents & POLLIN) { /* New input, process it */
if (fgets (input, sizeof(input), stdin) == NULL) if (fgets (input, sizeof(input), stdin) == NULL)
@ -577,9 +642,9 @@ main (int argc, char **argv)
} }
if (redraw) { /* Copy our temporary pixmap onto the window */ if (redraw) { /* Copy our temporary pixmap onto the window */
for (int i = 0; i < num_screens; i++) { for (monitor_t *mon = mons; mon; mon = mon->next) {
if (screens[i].window) if (mon->window)
xcb_copy_area (c, canvas, screens[i].window, draw_gc, screens[i].x, 0, 0, 0, screens[i].width, BAR_HEIGHT); xcb_copy_area (c, canvas, mon->window, draw_gc, mon->x, 0, 0, 0, mon->width, cfg.height);
} }
} }

View File

@ -1,21 +1,7 @@
/* The height of the bar (in pixels) */
#define BAR_HEIGHT 18
/* The width of the bar. Set to -1 to fit screen */
#define BAR_WIDTH -1
/* Offset from the left. Set to 0 to have no effect */
#define BAR_OFFSET 0
/* Choose between an underline or an overline */
#define BAR_UNDERLINE 1
/* The thickness of the underline (in pixels). Set to 0 to disable. */ /* The thickness of the underline (in pixels). Set to 0 to disable. */
#define BAR_UNDERLINE_HEIGHT 2 #define BAR_UNDERLINE_HEIGHT 2
/* Default bar position, overwritten by '-b' switch */
#define BAR_BOTTOM 0
/* 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"
/* Some fonts don't set the right width for some chars, pheex it */ /* Some fonts don't set the right width for some chars, pheex it */
#define BAR_FONT_FALLBACK_WIDTH 6 #define BAR_FONT_FALLBACK_WIDTH 6
/* Define the opacity of the bar (requires a compositor such as compton) */
#define BAR_OPACITY 1.0 /* 0 is invisible, 1 is opaque */
/* Color palette */ /* Color palette */
#define BACKGROUND 0x232c31 #define BACKGROUND 0x232c31
#define COLOR0 0x2d3c46 #define COLOR0 0x2d3c46