18 Commits

Author SHA1 Message Date
LemonBoy
4cb41ace87 Fix #69. Thanks @matchew ! 2014-08-06 11:41:09 +02:00
LemonBoy
f9deacf0bc Initial cairo migration 2014-07-23 21:15:40 +02:00
LemonBoy
1199bcd4cb Travis integration 2014-07-22 22:55:30 +02:00
Giuseppe
5e7b44dce9 Merge pull request #67 from LemonBoy/revert-65-no-alpha-fix
Revert "Add support for #RRGGBB sytle colors"
2014-07-02 15:58:28 +02:00
Giuseppe
b66bd0a172 Revert "Add support for #RRGGBB sytle colors" 2014-07-02 15:58:19 +02:00
Giuseppe
524593d871 Merge pull request #65 from Sprocklem/no-alpha-fix
Add support for #RRGGBB sytle colors
2014-07-02 03:37:44 +02:00
Sprocklem
939aead84d Small change to fix #64 2014-06-18 17:50:26 -06:00
LemonBoy
970332ac76 RGBA colors now should render properly. 2014-06-11 20:34:40 +02:00
LemonBoy
1ab492730b Area activation button is now configurable, aka right-clickable areas. 2014-06-11 20:05:23 +02:00
Giuseppe
eb90ab7675 Merge pull request #61 from jvvv/master
Multi-monitor geometry fix.
2014-06-09 16:23:01 +02:00
John Vogel
99e927ee70 Put geom checks after adjust for non-multi also 2014-06-01 09:21:06 -04:00
John Vogel
8a7015fce2 Add omitted variable declaration 2014-05-31 22:01:14 -04:00
John Vogel
e81c1ff8cb Put geometry checks after width/height adjust 2014-05-31 21:56:50 -04:00
John Vogel
caf14a0ec4 Don't send zero width rects to monitor_create_chain 2014-05-31 21:52:48 -04:00
John Vogel
c233646012 Consider all monitors for geometry settings/checks. 2014-05-28 21:00:58 -04:00
John Vogel
3546890578 Clear all pixmaps at start of parse, but not at each ${S} command. 2014-05-28 20:38:36 -04:00
LemonBoy
26044f210b Feed Xorg colors with the alpha channel premultiplied in 2014-05-07 20:53:10 +02:00
LemonBoy
a447500477 Y offset support thanks to z3bra 2014-04-24 23:46:00 +02:00
4 changed files with 226 additions and 214 deletions

7
.travis.yml Normal file
View File

@@ -0,0 +1,7 @@
language: c
compiler:
- clang
- gcc
before_install:
- sudo apt-get install -qq libx11-xcb-dev libxcb-randr0-dev libxcb-xinerama0-dev
script: make

View File

@@ -1,7 +1,7 @@
CC ?= gcc
STRIP ?= strip
CFLAGS = -std=c99 -Os
LDFLAGS = -lxcb -lxcb-xinerama -lxcb-randr
LDFLAGS = -lxcb -lcairo -lxcb-xinerama -lxcb-randr
CFDEBUG = -g3 -pedantic -Wall -Wunused-parameter -Wlong-long\
-Wsign-conversion -Wconversion -Wimplicit-function-declaration

View File

@@ -2,9 +2,11 @@
bar - bar ain't recursive
=for HTML <a href="https://travis-ci.org/LemonBoy/bar"><img src="https://travis-ci.org/LemonBoy/bar.svg?branch=master"></a>
=head1 SYNOPSIS
I<bar> [-h | -g I<width>B<x>I<height>B<+>I<x> | -b | -d | -f I<font> | -p | -u I<pixel> | -B I<color> | -F I<color>]
I<bar> [-h | -g I<width>B<x>I<height>B<+>I<x>B<+>I<y> | -b | -d | -f I<font> | -p | -u I<pixel> | -B I<color> | -F I<color>]
=head1 DESCRIPTION
@@ -18,9 +20,9 @@ B<bar> is a lightweight bar entirely based on XCB. Provides full UTF-8 support,
Display the help and exit.
=item B<-g> I<width>B<x>I<height>B<+>I<x>
=item B<-g> I<width>B<x>I<height>B<+>I<x>B<+>I<y>
Set the window geometry. If a parameter is omitted it's filled with the default value.
Set the window geometry. If a parameter is omitted it's filled with the default value. If the I<y> parameter is specified along with the B<-b> switch then the position is relative to the bottom of the screen.
=item B<-b>
@@ -86,12 +88,14 @@ Set the text foreground color. The parameter I<color> can be I<-> or a color in
Set the text underline color. The parameter I<color> can be I<-> or a color in one of the formats mentioned before. The special value I<-> resets the color to the default one.
=item B<A>:I<command>:
=item B<A>I<button>:I<command>:
Create a clickable area starting from the current position, when the area is clicked I<command> is executed. The area is closed when a B<A> token, not followed by : is encountered.
Eg. I<%{A:reboot:} Click here to reboot %{A}>
The I<button> field is optional, it defaults to the left button, and it's a number ranging from 1 to 5 which maps to the left, middle, right, scroll down and scroll up movements. Your mileage may vary.
=item B<S>I<dir>
Change the monitor bar is rendering to. I<dir> can be either

419
bar.c
View File

@@ -11,6 +11,8 @@
#include <xcb/xcb.h>
#include <xcb/xinerama.h>
#include <xcb/randr.h>
#include <cairo/cairo.h>
#include <cairo/cairo-xcb.h>
// Here be dragons
@@ -18,34 +20,31 @@
#define min(a,b) ((a) < (b) ? (a) : (b))
#define indexof(c,s) (strchr((s),(c))-(s))
typedef struct font_t {
xcb_font_t ptr;
int descent, height;
uint16_t char_max;
uint16_t char_min;
xcb_charinfo_t *width_lut;
} font_t;
typedef struct monitor_t {
int x, width;
xcb_window_t window;
xcb_pixmap_t pixmap;
cairo_surface_t *surface;
cairo_t *cr;
struct monitor_t *prev, *next;
} monitor_t;
typedef struct area_t {
int begin, end, align;
int begin, end, align, button;
xcb_window_t window;
char *cmd;
} area_t;
#define N 10
#define N 20
typedef struct area_stack_t {
int pos;
area_t slot[N];
} area_stack_t;
typedef struct color_t {
double r, g, b, a;
} color_t;
enum {
ATTR_OVERL = (1<<0),
ATTR_UNDERL = (1<<1),
@@ -66,73 +65,87 @@ enum {
static xcb_connection_t *c;
static xcb_screen_t *scr;
static xcb_gcontext_t gc[GC_MAX];
static xcb_visualid_t visual;
static xcb_visualtype_t *vt;
static xcb_colormap_t colormap;
static monitor_t *monhead, *montail;
static font_t *main_font, *alt_font;
static uint32_t attrs = 0;
static bool dock = false;
static bool topbar = true;
static int bw = -1, bh = -1, bx = 0;
static int bw = -1, bh = -1, bx = 0, by = 0;
static int bu = 1; /* Underline height */
static char *mfont, *afont;
static uint32_t fgc, bgc, ugc;
static uint32_t dfgc, dbgc;
static char *mfont = NULL;
static uint32_t fgc, bgc;
static area_stack_t astack;
enum {
PAL_BG,
PAL_FG,
PAL_ATTR,
PAL_MAX,
};
static color_t palette[PAL_MAX];
void
update_gc (void)
cairo_set_color (cairo_t *cr, const int i)
{
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_BACKGROUND | XCB_GC_FOREGROUND, (const uint32_t []){ fgc, bgc });
xcb_change_gc(c, gc[GC_CLEAR], XCB_GC_FOREGROUND, (const uint32_t []){ bgc });
xcb_change_gc(c, gc[GC_ATTR], XCB_GC_FOREGROUND, (const uint32_t []){ ugc });
cairo_set_source_rgba(cr, palette[i].r, palette[i].g, palette[i].b, palette[i].a);
}
void
fill_rect (xcb_drawable_t d, xcb_gcontext_t gc, int x, int y, int width, int height)
fill_rect (cairo_t *cr, const int i, int x, int y, int width, int height)
{
xcb_poly_fill_rectangle(c, d, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
cairo_set_color(cr, i);
cairo_rectangle(cr, x, y, width, height);
cairo_stroke_preserve(cr);
cairo_fill(cr);
}
void
cairo_copy (cairo_t *cr, cairo_surface_t *s, int sx, int sy, int dx, int dy, int w, int h)
{
cairo_set_source_surface(cr, s, dx - sx, dy - sy);
cairo_rectangle (cr, dx, dy, w, h);
cairo_fill (cr);
}
int
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
draw_char (monitor_t *mon, int x, int align, char *ch)
{
/* In the unlikely case that the font doesn't have the glyph wanted just do nothing */
if (ch < cur_font->char_min || ch > cur_font->char_max)
return 0;
cairo_font_extents_t ext;
cairo_text_extents_t te;
cairo_font_extents(mon->cr, &ext);
cairo_text_extents(mon->cr, ch, &te);
int ch_width = cur_font->width_lut[ch - cur_font->char_min].character_width;
int ch_width = (int)te.x_advance + 1;
/*printf("%f %i %i\n", te.x_advance, (int)te.x_advance, ch_width);*/
switch (align) {
case ALIGN_C:
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW], mon->width / 2 - x / 2, 0,
mon->width / 2 - (x + ch_width) / 2, 0, x, bh);
cairo_copy(mon->cr, mon->surface, mon->width / 2 - x / 2, 0, mon->width / 2 - (x + ch_width) / 2, 0, x, bh);
x = mon->width / 2 - (x + ch_width) / 2 + x;
break;
case ALIGN_R:
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW], mon->width - x, 0,
mon->width - x - ch_width, 0, x, bh);
cairo_copy(mon->cr, mon->surface, mon->width - x, 0, mon->width - x - ch_width, 0, x, bh);
x = mon->width - ch_width;
break;
}
/* Draw the background first */
fill_rect(mon->pixmap, gc[GC_CLEAR], x, 0, ch_width, bh);
/* xcb accepts string in UCS-2 BE, so swap */
ch = (ch >> 8) | (ch << 8);
fill_rect(mon->cr, PAL_BG, x, by, ch_width, bh);
/* String baseline coordinates */
xcb_image_text_16(c, 1, mon->pixmap, gc[GC_DRAW], x, bh / 2 + cur_font->height / 2 - cur_font->descent, (xcb_char2b_t *)&ch);
cairo_move_to(mon->cr, x, bh / 2 + ext.height / 2 - ext.descent);
cairo_set_color(mon->cr, PAL_FG);
cairo_show_text(mon->cr, ch);
/* We can render both at the same time */
if (attrs & ATTR_OVERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, 0, ch_width, bu);
fill_rect(mon->cr, PAL_ATTR, x, 0, ch_width, bu);
if (attrs & ATTR_UNDERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, bh - bu, ch_width, bu);
fill_rect(mon->cr, PAL_ATTR, x, bh - bu, ch_width, bu);
return ch_width;
return ch_width;
}
uint32_t
@@ -156,9 +169,9 @@ parse_color (const char *str, char **end, const uint32_t def)
if (str[0] == '#') {
errno = 0;
uint32_t tmp = strtoul(str + 1, end, 16);
/* Some error checking it's good */
/* Some error checking is definitely good */
if (errno)
return def;
tmp = def;
return tmp;
}
@@ -180,6 +193,15 @@ parse_color (const char *str, char **end, const uint32_t def)
return ret;
}
void
convert_color (const uint32_t col, color_t *out)
{
out->b = ((col >> 0)&0xff) / 255.0;
out->g = ((col >> 8)&0xff) / 255.0;
out->r = ((col >>16)&0xff) / 255.0;
/*out->a = ((col >>24)&0xff) / 255.0;*/
out->a = 1.0;
}
void
set_attribute (const char modifier, const char attribute)
@@ -225,7 +247,7 @@ area_shift (xcb_window_t win, const int align, int delta)
}
bool
area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x, const int align)
area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x, const int align, const int button)
{
char *p = str;
area_t *a = &astack.slot[astack.pos];
@@ -280,6 +302,7 @@ area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x
a->align = align;
a->begin = x;
a->window = mon->window;
a->button = button;
*end = trail + 1;
@@ -289,10 +312,8 @@ area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x
void
parse (char *text)
{
font_t *cur_font;
monitor_t *cur_mon;
int pos_x;
int align;
int pos_x, align, button;
char *p = text, *end;
uint32_t tmp;
@@ -302,7 +323,10 @@ parse (char *text)
memset(&astack, 0, sizeof(area_stack_t));
fill_rect(cur_mon->pixmap, gc[GC_CLEAR], 0, 0, bw, bh);
for (monitor_t *m = monhead; m != NULL; m = m->next) {
cairo_set_operator(m->cr, CAIRO_OPERATOR_SOURCE);
fill_rect(m->cr, PAL_BG, 0, 0, m->width, bh);
}
for (;;) {
if (*p == '\0' || *p == '\n')
@@ -319,10 +343,9 @@ parse (char *text)
case '!': set_attribute('!', *p++); break;
case 'R':
tmp = fgc;
fgc = bgc;
bgc = tmp;
update_gc();
/*tmp = fgc;*/
/*fgc = bgc;*/
/*bgc = tmp;*/
break;
case 'l': pos_x = 0; align = ALIGN_L; break;
@@ -330,12 +353,19 @@ parse (char *text)
case 'r': pos_x = 0; align = ALIGN_R; break;
case 'A':
area_add(p, end, &p, cur_mon, pos_x, align);
button = XCB_BUTTON_INDEX_1;
/* The range is 1-5 */
if (isdigit(*p) && (*p > '0' && *p < '6'))
button = *p++ - '0';
area_add(p, end, &p, cur_mon, pos_x, align, button);
break;
case 'B': bgc = parse_color(p, &p, dbgc); update_gc(); break;
case 'F': fgc = parse_color(p, &p, dfgc); update_gc(); break;
case 'U': ugc = parse_color(p, &p, dbgc); update_gc(); break;
case 'B': convert_color(parse_color(p, &p, bgc), &palette[PAL_BG]);
break;
case 'F': convert_color(parse_color(p, &p, fgc), &palette[PAL_FG]);
break;
case 'U': convert_color(parse_color(p, &p, fgc), &palette[PAL_ATTR]);
break;
case 'S':
if (*p == '+' && cur_mon->next)
@@ -356,7 +386,6 @@ parse (char *text)
p++;
pos_x = 0;
fill_rect(cur_mon->pixmap, gc[GC_CLEAR], 0, 0, cur_mon->width, bh);
break;
/* In case of error keep parsing after the closing } */
@@ -366,33 +395,25 @@ parse (char *text)
}
/* Eat the trailing } */
p++;
} else { /* utf-8 -> ucs-2 */
uint8_t *utf = (uint8_t *)p;
uint16_t ucs;
} else {
const int utf8_size[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xC0-0xCF
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0xD0-0xDF
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE0-0xEF
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 0xF0-0xFF
};
int size = ((uint8_t)p[0]&0x80) ? utf8_size[(uint8_t)p[0]^0x80] : 1;
char tmp[size];
for (int i = 0; i < size; i++)
tmp[i] = *p++;
tmp[size] = '\0';
if (utf[0] < 0x80) {
ucs = utf[0];
p += 1;
}
else if ((utf[0] & 0xe0) == 0xc0) {
ucs = (utf[0] & 0x1f) << 6 | (utf[1] & 0x3f);
p += 2;
}
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;
}
/* 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, gc[GC_DRAW] , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr });
int w = draw_char(cur_mon, cur_font, pos_x, align, ucs);
int w = draw_char(cur_mon, pos_x, align, tmp);
pos_x += w;
area_shift(cur_mon->window, align, w);
@@ -400,40 +421,6 @@ parse (char *text)
}
}
font_t *
font_load (const char *str)
{
xcb_query_font_cookie_t queryreq;
xcb_query_font_reply_t *font_info;
xcb_void_cookie_t cookie;
xcb_font_t font;
font = xcb_generate_id(c);
cookie = xcb_open_font_checked(c, font, strlen(str), str);
if (xcb_request_check (c, cookie)) {
fprintf(stderr, "Could not load font %s\n", str);
return NULL;
}
font_t *ret = calloc(1, sizeof(font_t));
if (!ret)
return NULL;
queryreq = xcb_query_font(c, font);
font_info = xcb_query_font_reply(c, queryreq, NULL);
ret->ptr = font;
ret->descent = font_info->font_descent;
ret->height = font_info->font_ascent + 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->width_lut = xcb_query_font_char_infos(font_info);
return ret;
}
enum {
NET_WM_WINDOW_TYPE,
NET_WM_WINDOW_TYPE_DOCK,
@@ -513,18 +500,23 @@ monitor_new (int x, int y, int width, int height)
ret->width = width;
ret->next = ret->prev = NULL;
int win_y = (topbar ? 0 : height - bh) + y;
int win_y = (topbar ? by : height - bh - by) + y;
ret->window = xcb_generate_id(c);
int depth = (visual == scr->root_visual) ? XCB_COPY_FROM_PARENT : 32;
int depth = (vt->visual_id == scr->root_visual) ? XCB_COPY_FROM_PARENT : 32;
xcb_create_window(c, depth, ret->window, scr->root,
x, win_y, width, bh, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, visual,
XCB_WINDOW_CLASS_INPUT_OUTPUT, vt->visual_id,
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
(const uint32_t []){ bgc, bgc, dock, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS, colormap });
(const uint32_t []){ scr->black_pixel, scr->black_pixel, dock, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS, colormap });
ret->pixmap = xcb_generate_id(c);
xcb_create_pixmap(c, depth, ret->pixmap, ret->window, width, bh);
ret->surface = cairo_xcb_surface_create(c, ret->window, vt, width, height);
if (!cairo_surface_status != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "awwwwww\n");
}
ret->cr = cairo_create(ret->surface);
cairo_select_font_face (ret->cr, mfont? mfont: "fixed", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
return ret;
}
@@ -562,14 +554,39 @@ rect_sort_cb (const void *p1, const void *p2)
void
monitor_create_chain (xcb_rectangle_t *rects, const int num)
{
int width = bw;
int i;
int width = 0, height = 0;
int left = bx;
/* Sort before use */
qsort(rects, num, sizeof(xcb_rectangle_t), rect_sort_cb);
for (i = 0; i < num; i++) {
int h = rects[i].y + rects[i].height;
/* Accumulated width of all monitors */
width += rects[i].width;
/* Get height of screen from y_offset + height of lowest monitor */
if (h >= height)
height = h;
}
if (bw < 0)
bw = width - bx;
if (bh < 0 || bh > height)
bh = 18;
/* Check the geometry */
if (bx + bw > width || by + bh > height) {
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
exit(EXIT_FAILURE);
}
/* Left is a positive number or zero therefore monitors with zero width are excluded */
for (int i = 0; i < num; i++) {
width = bw;
for (i = 0; i < num; i++) {
if (rects[i].y + rects[i].height < by)
continue;
if (rects[i].width > left) {
monitor_t *mon = monitor_new(
rects[i].x + left,
@@ -598,7 +615,7 @@ get_randr_monitors (void)
{
xcb_randr_get_screen_resources_current_reply_t *rres_reply;
xcb_randr_output_t *outputs;
int num, valid = 0;
int i, j, num, valid = 0;
rres_reply = xcb_randr_get_screen_resources_current_reply(c,
xcb_randr_get_screen_resources_current(c, scr->root), NULL);
@@ -621,7 +638,7 @@ get_randr_monitors (void)
xcb_rectangle_t rects[num];
/* Get all outputs */
for (int i = 0; i < num; i++) {
for (i = 0; i < num; i++) {
xcb_randr_get_output_info_reply_t *oi_reply;
xcb_randr_get_crtc_info_reply_t *ci_reply;
@@ -644,10 +661,8 @@ get_randr_monitors (void)
return;
}
if (ci_reply->rotation & (XCB_RANDR_ROTATION_ROTATE_90|XCB_RANDR_ROTATION_ROTATE_270))
rects[i] = (xcb_rectangle_t){ ci_reply->x, ci_reply->y, ci_reply->height, ci_reply->width };
else
rects[i] = (xcb_rectangle_t){ ci_reply->x, ci_reply->y, ci_reply->width, ci_reply->height };
/* There's no need to handle rotated screens here (see #69) */
rects[i] = (xcb_rectangle_t){ ci_reply->x, ci_reply->y, ci_reply->width, ci_reply->height };
free(ci_reply);
@@ -657,11 +672,11 @@ get_randr_monitors (void)
free(rres_reply);
/* Check for clones and inactive outputs */
for (int i = 0; i < num; i++) {
for (i = 0; i < num; i++) {
if (rects[i].width == 0)
continue;
for (int j = 0; j < num; j++) {
for (j = 0; j < num; j++) {
/* Does I countain J ? */
if (i != j && rects[j].width) {
@@ -679,7 +694,13 @@ get_randr_monitors (void)
return;
}
monitor_create_chain(rects, num);
xcb_rectangle_t r[valid];
for (i = j = 0; i < num && j < valid; i++)
if (rects[i].width != 0)
r[j++] = rects[i];
monitor_create_chain(r, valid);
}
void
@@ -711,8 +732,8 @@ get_xinerama_monitors (void)
monitor_create_chain(rects, screens);
}
xcb_visualid_t
get_visual (void)
xcb_visualtype_t *
get_visual_type (void)
{
xcb_depth_iterator_t iter;
@@ -723,13 +744,24 @@ get_visual (void)
xcb_visualtype_t *vis = xcb_depth_visuals(iter.data);
if (iter.data->depth == 32)
return vis->visual_id;
return vis;
xcb_depth_next(&iter);
}
iter = xcb_screen_allowed_depths_iterator(scr);
/* Try to find a RGBA visual */
while (iter.rem) {
xcb_visualtype_t *vis = xcb_depth_visuals(iter.data);
if (iter.data->depth == 24)
return vis;
xcb_depth_next(&iter);
}
/* Fallback to the default one */
return scr->root_visual;
return NULL;
}
void
@@ -746,34 +778,17 @@ xconn (void)
scr = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
/* Try to get a RGBA visual and build the colormap for that */
visual = get_visual();
vt = get_visual_type();
colormap = xcb_generate_id(c);
xcb_create_colormap(c, XCB_COLORMAP_ALLOC_NONE, colormap, scr->root, visual);
xcb_create_colormap(c, XCB_COLORMAP_ALLOC_NONE, colormap, scr->root, vt->visual_id);
}
void
init (void)
{
/* If I fits I sits */
if (bw < 0)
bw = scr->width_in_pixels - bx;
/* Load the fonts */
main_font = font_load(mfont ? mfont : "fixed");
if (!main_font)
exit(EXIT_FAILURE);
alt_font = font_load(afont ? afont : "fixed");
if (!alt_font)
exit(EXIT_FAILURE);
/* To make the alignment uniform */
main_font->height = alt_font->height = max(main_font->height, alt_font->height);
/* Adjust the height */
if (bh < 0 || bh > scr->height_in_pixels)
bh = main_font->height + bu + 2;
/*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;
@@ -801,9 +816,24 @@ init (void)
}
}
if (!monhead)
if (!monhead) {
/* If I fits I sits */
if (bw < 0)
bw = scr->width_in_pixels - bx;
/* Adjust the height */
if (bh < 0 || bh > scr->height_in_pixels)
bh = 18;
/* Check the geometry */
if (bx + bw > scr->width_in_pixels || by + bh > scr->height_in_pixels) {
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
exit(EXIT_FAILURE);
}
/* If no RandR outputs or Xinerama screens, fall back to using whole screen */
monhead = monitor_new(0, 0, bw, scr->height_in_pixels);
}
if (!monhead)
exit(EXIT_FAILURE);
@@ -811,19 +841,9 @@ init (void)
/* For WM that support EWMH atoms */
set_ewmh_atoms();
/* Create the gc for drawing */
gc[GC_DRAW] = xcb_generate_id(c);
xcb_create_gc(c, gc[GC_DRAW], monhead->pixmap, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t []){ fgc, bgc });
gc[GC_CLEAR] = xcb_generate_id(c);
xcb_create_gc(c, gc[GC_CLEAR], monhead->pixmap, XCB_GC_FOREGROUND, (const uint32_t []){ bgc });
gc[GC_ATTR] = xcb_generate_id(c);
xcb_create_gc(c, gc[GC_ATTR], monhead->pixmap, XCB_GC_FOREGROUND, (const uint32_t []){ ugc });
/* Make the bar visible and clear the pixmap */
for (monitor_t *mon = monhead; mon; mon = mon->next) {
fill_rect(mon->pixmap, gc[GC_CLEAR], 0, 0, mon->width, bh);
fill_rect(mon->cr, PAL_BG, 0, 0, mon->width, bh);
xcb_map_window(c, mon->window);
}
@@ -833,32 +853,17 @@ init (void)
void
cleanup (void)
{
if (main_font) {
xcb_close_font(c, main_font->ptr);
free(main_font);
}
if (alt_font) {
xcb_close_font(c, alt_font->ptr);
free(alt_font);
}
while (monhead) {
monitor_t *next = monhead->next;
cairo_destroy(monhead->cr);
cairo_surface_destroy(monhead->surface);
xcb_destroy_window(c, monhead->window);
xcb_free_pixmap(c, monhead->pixmap);
free(monhead);
monhead = next;
}
xcb_free_colormap(c, colormap);
if (gc[GC_DRAW])
xcb_free_gc(c, gc[GC_DRAW]);
if (gc[GC_CLEAR])
xcb_free_gc(c, gc[GC_CLEAR]);
if (gc[GC_ATTR])
xcb_free_gc(c, gc[GC_ATTR]);
if (c)
xcb_disconnect(c);
}
@@ -932,9 +937,6 @@ parse_font_list (char *str)
tok = strtok(str, ",");
if (tok)
mfont = tok;
tok = strtok(NULL, ",");
if (tok)
afont = tok;
return;
}
@@ -949,7 +951,7 @@ main (int argc, char **argv)
xcb_generic_event_t *ev;
xcb_expose_event_t *expose_ev;
xcb_button_press_event_t *press_ev;
char input[2048] = {0, };
char input[4096] = {0, };
bool permanent = false;
int geom_v[4] = { -1, -1, 0, 0 };
@@ -961,11 +963,8 @@ main (int argc, char **argv)
/* Connect to the Xserver and initialize scr */
xconn();
/* B/W combo */
dbgc = bgc = parse_color("black", NULL, scr->black_pixel);
dfgc = fgc = parse_color("white", NULL, scr->white_pixel);
ugc = fgc;
bgc = scr->black_pixel;
fgc = scr->white_pixel;
char ch;
while ((ch = getopt(argc, argv, "hg:bdf:a:pu:B:F:")) != -1) {
@@ -973,7 +972,7 @@ main (int argc, char **argv)
case 'h':
printf ("usage: %s [-h | -g | -b | -d | -f | -a | -p | -u | -B | -F]\n"
"\t-h Show this help\n"
"\t-g Set the bar geometry {width}x{height})\n"
"\t-g Set the bar geometry {width}x{height}+{xoffset}+{yoffset}\n"
"\t-b Put bar at the bottom of the screen\n"
"\t-d Force docking (use this if your WM isn't EWMH compliant)\n"
"\t-f Bar font list, comma separated\n"
@@ -988,21 +987,20 @@ main (int argc, char **argv)
case 'd': dock = true; break;
case 'f': parse_font_list(optarg); break;
case 'u': bu = strtoul(optarg, NULL, 10); break;
case 'B': dbgc = bgc = parse_color(optarg, NULL, scr->black_pixel); break;
case 'F': dfgc = fgc = parse_color(optarg, NULL, scr->white_pixel); break;
case 'B': bgc = parse_color(optarg, NULL, scr->black_pixel); break;
case 'F': fgc = parse_color(optarg, NULL, scr->white_pixel); break;
}
}
convert_color(bgc, &palette[PAL_BG]);
convert_color(fgc, &palette[PAL_FG]);
convert_color(fgc, &palette[PAL_ATTR]);
/* Copy the geometry values in place */
bw = geom_v[0];
bh = geom_v[1];
bx = geom_v[2];
/* Check the geometry */
if (bx >= scr->width_in_pixels || bx + bw > scr->width_in_pixels) {
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
return EXIT_FAILURE;
}
by = geom_v[3];
/* Do the heavy lifting */
init();
@@ -1035,10 +1033,13 @@ main (int argc, char **argv)
break;
case XCB_BUTTON_PRESS:
press_ev = (xcb_button_press_event_t *)ev;
/* Respond to left click */
if (press_ev->detail == XCB_BUTTON_INDEX_1) {
{
area_t *area = area_get(press_ev->event, press_ev->event_x);
if (area) { write(STDOUT_FILENO, area->cmd, strlen(area->cmd)); write(STDOUT_FILENO, "\n", 1); }
/* Respond to the click */
if (area && area->button == press_ev->detail) {
write(STDOUT_FILENO, area->cmd, strlen(area->cmd));
write(STDOUT_FILENO, "\n", 1);
}
}
break;
}
@@ -1050,7 +1051,7 @@ main (int argc, char **argv)
if (redraw) { /* Copy our temporary pixmap onto the window */
for (monitor_t *mon = monhead; mon; mon = mon->next) {
xcb_copy_area(c, mon->pixmap, mon->window, gc[GC_DRAW], 0, 0, 0, 0, mon->width, bh);
cairo_surface_flush(mon->surface);
}
}