2015-02-13 13:18:48 +00:00
|
|
|
// vim:sw=4:ts=4:et:
|
2016-05-22 13:31:42 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L
|
2012-07-24 10:08:02 +00:00
|
|
|
#include <stdbool.h>
|
2014-10-31 01:58:19 +00:00
|
|
|
#include <stddef.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>
|
2014-02-21 11:52:50 +00:00
|
|
|
#include <errno.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
#include <xcb/xcb.h>
|
2015-02-13 23:06:43 +00:00
|
|
|
#include <xcb/xcbext.h>
|
2016-04-23 06:59:38 +00:00
|
|
|
#if WITH_XINERAMA
|
2013-09-25 03:14:43 +00:00
|
|
|
#include <xcb/xinerama.h>
|
2016-04-23 06:59:38 +00:00
|
|
|
#endif
|
2014-02-12 04:29:40 +00:00
|
|
|
#include <xcb/randr.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
#include <X11/Xft/Xft.h>
|
|
|
|
#include <X11/Xlib-xcb.h>
|
|
|
|
|
|
|
|
// Here bet dragons
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
#define max(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
#define min(a,b) ((a) < (b) ? (a) : (b))
|
2014-02-23 21:46:15 +00:00
|
|
|
#define indexof(c,s) (strchr((s),(c))-(s))
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
typedef struct font_t {
|
2014-02-12 15:01:42 +00:00
|
|
|
xcb_font_t ptr;
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_charinfo_t *width_lut;
|
|
|
|
|
|
|
|
XftFont *xft_ft;
|
|
|
|
|
|
|
|
int ascent;
|
|
|
|
|
2015-04-15 08:21:52 +00:00
|
|
|
int descent, height, width;
|
2014-02-12 15:01:42 +00:00
|
|
|
uint16_t char_max;
|
|
|
|
uint16_t char_min;
|
2014-02-08 15:45:38 +00:00
|
|
|
} font_t;
|
|
|
|
|
|
|
|
typedef struct monitor_t {
|
2015-02-10 19:35:11 +00:00
|
|
|
int x, y, width;
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_window_t window;
|
2014-02-23 21:46:15 +00:00
|
|
|
xcb_pixmap_t pixmap;
|
2014-02-08 15:45:38 +00:00
|
|
|
struct monitor_t *prev, *next;
|
|
|
|
} monitor_t;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-24 12:46:46 +00:00
|
|
|
typedef struct area_t {
|
2015-10-24 12:28:51 +00:00
|
|
|
unsigned int begin:16;
|
|
|
|
unsigned int end:16;
|
|
|
|
bool active:1;
|
|
|
|
int align:3;
|
2015-11-15 22:06:19 +00:00
|
|
|
unsigned int button:3;
|
2014-02-26 10:07:49 +00:00
|
|
|
xcb_window_t window;
|
2014-02-24 12:46:46 +00:00
|
|
|
char *cmd;
|
|
|
|
} area_t;
|
|
|
|
|
2015-02-13 23:06:43 +00:00
|
|
|
typedef union rgba_t {
|
|
|
|
struct {
|
|
|
|
uint8_t b;
|
2015-02-15 22:54:04 +00:00
|
|
|
uint8_t g;
|
|
|
|
uint8_t r;
|
2015-02-13 23:06:43 +00:00
|
|
|
uint8_t a;
|
|
|
|
};
|
|
|
|
uint32_t v;
|
|
|
|
} rgba_t;
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
typedef struct area_stack_t {
|
2015-10-24 12:28:51 +00:00
|
|
|
int at, max;
|
|
|
|
area_t *area;
|
2014-02-26 10:07:49 +00:00
|
|
|
} area_stack_t;
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
enum {
|
|
|
|
ATTR_OVERL = (1<<0),
|
|
|
|
ATTR_UNDERL = (1<<1),
|
2012-07-23 14:49:39 +00:00
|
|
|
};
|
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
enum { ALIGN_L = 0,
|
|
|
|
ALIGN_C,
|
|
|
|
ALIGN_R
|
|
|
|
};
|
2012-07-24 10:25:45 +00:00
|
|
|
|
2014-02-23 21:46:15 +00:00
|
|
|
enum {
|
|
|
|
GC_DRAW = 0,
|
|
|
|
GC_CLEAR,
|
|
|
|
GC_ATTR,
|
|
|
|
GC_MAX
|
|
|
|
};
|
|
|
|
|
2014-07-22 20:14:48 +00:00
|
|
|
#define MAX_FONT_COUNT 5
|
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
static Display *dpy;
|
2014-02-12 15:01:42 +00:00
|
|
|
static xcb_connection_t *c;
|
2014-10-08 21:00:19 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
static xcb_screen_t *scr;
|
2014-10-08 21:00:19 +00:00
|
|
|
static int scr_nbr = 0;
|
|
|
|
|
2014-02-23 21:46:15 +00:00
|
|
|
static xcb_gcontext_t gc[GC_MAX];
|
2014-02-24 12:35:56 +00:00
|
|
|
static xcb_visualid_t visual;
|
2014-10-08 21:12:50 +00:00
|
|
|
static Visual *visual_ptr;
|
2014-02-24 12:35:56 +00:00
|
|
|
static xcb_colormap_t colormap;
|
2014-10-08 19:47:26 +00:00
|
|
|
|
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static monitor_t *monhead, *montail;
|
2014-07-22 20:14:48 +00:00
|
|
|
static font_t *font_list[MAX_FONT_COUNT];
|
2015-01-21 18:43:22 +00:00
|
|
|
static int font_count = 0;
|
|
|
|
static int font_index = -1;
|
2015-06-02 07:03:55 +00:00
|
|
|
static int offsets_y[MAX_FONT_COUNT];
|
|
|
|
static int offset_y_count = 0;
|
|
|
|
static int offset_y_index = 0;
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
static uint32_t attrs = 0;
|
|
|
|
static bool dock = false;
|
|
|
|
static bool topbar = true;
|
2014-04-24 21:46:00 +00:00
|
|
|
static int bw = -1, bh = -1, bx = 0, by = 0;
|
2015-01-25 20:14:06 +00:00
|
|
|
static int bu = 1; // Underline height
|
2015-02-13 23:06:43 +00:00
|
|
|
static rgba_t fgc, bgc, ugc;
|
2016-01-19 13:18:16 +00:00
|
|
|
static rgba_t dfgc, dbgc, dugc;
|
2015-10-24 12:28:51 +00:00
|
|
|
static area_stack_t area_stack;
|
2012-07-18 15:11:17 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
static XftColor sel_fg;
|
|
|
|
static XftDraw *xft_draw;
|
|
|
|
|
2014-10-09 10:58:12 +00:00
|
|
|
//char width lookuptable
|
2014-10-08 19:47:26 +00:00
|
|
|
#define MAX_WIDTHS (1 << 16)
|
|
|
|
static wchar_t xft_char[MAX_WIDTHS];
|
|
|
|
static char xft_width[MAX_WIDTHS];
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2014-02-19 17:53:32 +00:00
|
|
|
update_gc (void)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ fgc.v });
|
|
|
|
xcb_change_gc(c, gc[GC_CLEAR], XCB_GC_FOREGROUND, (const uint32_t []){ bgc.v });
|
|
|
|
xcb_change_gc(c, gc[GC_ATTR], XCB_GC_FOREGROUND, (const uint32_t []){ ugc.v });
|
2014-10-08 21:00:19 +00:00
|
|
|
XftColorFree(dpy, visual_ptr, colormap , &sel_fg);
|
2014-11-25 12:16:01 +00:00
|
|
|
char color[] = "#ffffff";
|
2015-02-24 13:00:49 +00:00
|
|
|
uint32_t nfgc = fgc.v & 0x00ffffff;
|
2014-11-25 12:16:01 +00:00
|
|
|
snprintf(color, sizeof(color), "#%06X", nfgc);
|
2014-10-08 21:00:19 +00:00
|
|
|
if (!XftColorAllocName (dpy, visual_ptr, colormap, color, &sel_fg)) {
|
2014-10-08 19:47:26 +00:00
|
|
|
fprintf(stderr, "Couldn't allocate xft font color '%s'\n", color);
|
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2015-02-13 23:06:43 +00:00
|
|
|
fill_gradient (xcb_drawable_t d, int x, int y, int width, int height, rgba_t start, rgba_t stop)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2015-02-13 23:06:43 +00:00
|
|
|
float i;
|
|
|
|
const int K = 25; // The number of steps
|
|
|
|
|
|
|
|
for (i = 0.; i < 1.; i += (1. / K)) {
|
|
|
|
// Perform the linear interpolation magic
|
|
|
|
unsigned int rr = i * stop.r + (1. - i) * start.r;
|
|
|
|
unsigned int gg = i * stop.g + (1. - i) * start.g;
|
|
|
|
unsigned int bb = i * stop.b + (1. - i) * start.b;
|
|
|
|
|
|
|
|
// The alpha is ignored here
|
2015-02-13 23:20:46 +00:00
|
|
|
rgba_t step = {
|
|
|
|
.r = rr,
|
|
|
|
.g = gg,
|
|
|
|
.b = bb,
|
2015-02-13 23:32:16 +00:00
|
|
|
.a = 255,
|
2015-02-13 23:20:46 +00:00
|
|
|
};
|
2015-02-13 23:06:43 +00:00
|
|
|
|
|
|
|
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ step.v });
|
|
|
|
xcb_poly_fill_rectangle(c, d, gc[GC_DRAW], 1,
|
|
|
|
(const xcb_rectangle_t []){ { x, i * bh, width, bh / K + 1 } });
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ fgc.v });
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2015-02-13 23:06:43 +00:00
|
|
|
fill_rect (xcb_drawable_t d, xcb_gcontext_t _gc, int x, int y, int width, int height)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_poly_fill_rectangle(c, d, _gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apparently xcb cannot seem to compose the right request for this call, hence we have to do it by
|
|
|
|
// ourselves.
|
|
|
|
// The funcion is taken from 'wmdia' (http://wmdia.sourceforge.net/)
|
|
|
|
xcb_void_cookie_t xcb_poly_text_16_simple(xcb_connection_t * c,
|
|
|
|
xcb_drawable_t drawable, xcb_gcontext_t gc, int16_t x, int16_t y,
|
|
|
|
uint32_t len, const uint16_t *str)
|
|
|
|
{
|
|
|
|
static const xcb_protocol_request_t xcb_req = {
|
2015-03-03 10:29:35 +00:00
|
|
|
5, // count
|
|
|
|
0, // ext
|
|
|
|
XCB_POLY_TEXT_16, // opcode
|
|
|
|
1 // isvoid
|
2015-02-13 23:06:43 +00:00
|
|
|
};
|
|
|
|
struct iovec xcb_parts[7];
|
|
|
|
uint8_t xcb_lendelta[2];
|
|
|
|
xcb_void_cookie_t xcb_ret;
|
|
|
|
xcb_poly_text_8_request_t xcb_out;
|
|
|
|
|
|
|
|
xcb_out.pad0 = 0;
|
|
|
|
xcb_out.drawable = drawable;
|
|
|
|
xcb_out.gc = gc;
|
|
|
|
xcb_out.x = x;
|
|
|
|
xcb_out.y = y;
|
|
|
|
|
|
|
|
xcb_lendelta[0] = len;
|
|
|
|
xcb_lendelta[1] = 0;
|
|
|
|
|
|
|
|
xcb_parts[2].iov_base = (char *)&xcb_out;
|
|
|
|
xcb_parts[2].iov_len = sizeof(xcb_out);
|
|
|
|
xcb_parts[3].iov_base = 0;
|
|
|
|
xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;
|
|
|
|
|
|
|
|
xcb_parts[4].iov_base = xcb_lendelta;
|
|
|
|
xcb_parts[4].iov_len = sizeof(xcb_lendelta);
|
|
|
|
xcb_parts[5].iov_base = (char *)str;
|
|
|
|
xcb_parts[5].iov_len = len * sizeof(int16_t);
|
|
|
|
|
|
|
|
xcb_parts[6].iov_base = 0;
|
|
|
|
xcb_parts[6].iov_len = -(xcb_parts[4].iov_len + xcb_parts[5].iov_len) & 3;
|
|
|
|
|
|
|
|
xcb_ret.sequence = xcb_send_request(c, 0, xcb_parts + 2, &xcb_req);
|
2015-04-15 08:21:52 +00:00
|
|
|
|
2015-02-13 23:06:43 +00:00
|
|
|
return xcb_ret;
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
int
|
2015-02-24 13:09:23 +00:00
|
|
|
xft_char_width_slot (uint16_t ch)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2014-10-08 19:47:26 +00:00
|
|
|
int slot = ch % MAX_WIDTHS;
|
|
|
|
while (xft_char[slot] != 0 && xft_char[slot] != ch)
|
|
|
|
{
|
|
|
|
slot = (slot + 1) % MAX_WIDTHS;
|
|
|
|
}
|
|
|
|
return slot;
|
|
|
|
}
|
|
|
|
|
2015-02-24 13:09:23 +00:00
|
|
|
int xft_char_width (uint16_t ch, font_t *cur_font)
|
2014-10-08 19:47:26 +00:00
|
|
|
{
|
|
|
|
int slot = xft_char_width_slot(ch);
|
|
|
|
if (!xft_char[slot]) {
|
|
|
|
XGlyphInfo gi;
|
2014-11-05 20:04:50 +00:00
|
|
|
FT_UInt glyph = XftCharIndex (dpy, cur_font->xft_ft, (FcChar32) ch);
|
2014-10-08 19:47:26 +00:00
|
|
|
XftFontLoadGlyphs (dpy, cur_font->xft_ft, FcFalse, &glyph, 1);
|
|
|
|
XftGlyphExtents (dpy, cur_font->xft_ft, &glyph, 1, &gi);
|
|
|
|
XftFontUnloadGlyphs (dpy, cur_font->xft_ft, &glyph, 1);
|
|
|
|
xft_char[slot] = ch;
|
|
|
|
xft_width[slot] = gi.xOff;
|
|
|
|
return gi.xOff;
|
|
|
|
} else if (xft_char[slot] == ch)
|
|
|
|
return xft_width[slot];
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-03-01 18:31:27 +00:00
|
|
|
shift (monitor_t *mon, int x, int align, int ch_width)
|
2014-10-08 19:47:26 +00:00
|
|
|
{
|
2012-07-16 09:41:34 +00:00
|
|
|
switch (align) {
|
2012-07-24 10:25:45 +00:00
|
|
|
case ALIGN_C:
|
2015-02-13 23:06:43 +00:00
|
|
|
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);
|
2014-03-15 15:07:10 +00:00
|
|
|
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:
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW],
|
|
|
|
mon->width - x, 0,
|
|
|
|
mon->width - x - ch_width, 0,
|
|
|
|
x, bh);
|
2014-02-08 15:45:38 +00:00
|
|
|
x = mon->width - ch_width;
|
2012-07-16 09:41:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-02-24 13:00:49 +00:00
|
|
|
|
|
|
|
/* Draw the background first */
|
2014-10-09 20:22:28 +00:00
|
|
|
fill_rect(mon->pixmap, gc[GC_CLEAR], x, 0, ch_width, bh);
|
2014-03-01 18:31:27 +00:00
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_lines (monitor_t *mon, int x, int w)
|
|
|
|
{
|
|
|
|
/* We can render both at the same time */
|
|
|
|
if (attrs & ATTR_OVERL)
|
|
|
|
fill_rect(mon->pixmap, gc[GC_ATTR], x, 0, w, bu);
|
|
|
|
if (attrs & ATTR_UNDERL)
|
|
|
|
fill_rect(mon->pixmap, gc[GC_ATTR], x, bh - bu, w, bu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_shift (monitor_t *mon, int x, int align, int w)
|
|
|
|
{
|
|
|
|
x = shift(mon, x, align, w);
|
|
|
|
draw_lines(mon, x, w);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
|
|
|
|
{
|
2016-02-17 21:18:23 +00:00
|
|
|
int ch_width;
|
2014-03-01 18:31:27 +00:00
|
|
|
|
2016-02-17 21:18:23 +00:00
|
|
|
if (cur_font->xft_ft) {
|
|
|
|
ch_width = xft_char_width(ch, cur_font);
|
|
|
|
} else {
|
|
|
|
ch_width = (cur_font->width_lut) ?
|
|
|
|
cur_font->width_lut[ch - cur_font->char_min].character_width:
|
|
|
|
cur_font->width;
|
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2016-02-17 21:18:23 +00:00
|
|
|
x = shift(mon, x, align, ch_width);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2015-06-02 07:03:55 +00:00
|
|
|
int y = bh / 2 + cur_font->height / 2- cur_font->descent + offsets_y[offset_y_index];
|
2014-10-08 19:47:26 +00:00
|
|
|
if (cur_font->xft_ft) {
|
2015-02-24 13:00:49 +00:00
|
|
|
XftDrawString16 (xft_draw, &sel_fg, cur_font->xft_ft, x,y, &ch, 1);
|
2014-10-08 19:47:26 +00:00
|
|
|
} else {
|
|
|
|
/* xcb accepts string in UCS-2 BE, so swap */
|
|
|
|
ch = (ch >> 8) | (ch << 8);
|
2015-03-03 14:34:02 +00:00
|
|
|
|
2015-02-24 13:00:49 +00:00
|
|
|
// The coordinates here are those of the baseline
|
|
|
|
xcb_poly_text_16_simple(c, mon->pixmap, gc[GC_DRAW],
|
2015-03-30 16:53:53 +00:00
|
|
|
x, y,
|
2015-02-13 23:06:43 +00:00
|
|
|
1, &ch);
|
2014-10-08 19:47:26 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-03-01 18:31:27 +00:00
|
|
|
draw_lines(mon, x, ch_width);
|
2012-07-25 18:33:01 +00:00
|
|
|
|
2012-07-25 14:55:26 +00:00
|
|
|
return ch_width;
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2015-02-13 23:06:43 +00:00
|
|
|
rgba_t
|
|
|
|
parse_color (const char *str, char **end, const rgba_t def)
|
2014-02-19 17:53:32 +00:00
|
|
|
{
|
2015-02-13 23:20:46 +00:00
|
|
|
int string_len;
|
2015-02-13 23:32:16 +00:00
|
|
|
char *ep;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return def;
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Reset
|
2014-02-19 17:53:32 +00:00
|
|
|
if (str[0] == '-') {
|
|
|
|
if (end)
|
|
|
|
*end = (char *)str + 1;
|
2015-02-13 23:32:16 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Hex representation
|
2015-10-24 12:04:37 +00:00
|
|
|
if (str[0] != '#') {
|
2015-02-13 23:32:16 +00:00
|
|
|
if (end)
|
2015-10-24 12:04:37 +00:00
|
|
|
*end = (char *)str;
|
2015-02-13 23:32:16 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
fprintf(stderr, "Invalid color specified\n");
|
|
|
|
return def;
|
2014-02-24 12:35:56 +00:00
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
errno = 0;
|
|
|
|
rgba_t tmp = (rgba_t)(uint32_t)strtoul(str + 1, &ep, 16);
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
if (end)
|
|
|
|
*end = ep;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
// Some error checking is definitely good
|
|
|
|
if (errno) {
|
|
|
|
fprintf(stderr, "Invalid color specified\n");
|
|
|
|
return def;
|
|
|
|
}
|
2015-02-13 23:06:43 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
string_len = ep - (str + 1);
|
2015-02-13 23:06:43 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
switch (string_len) {
|
|
|
|
case 3:
|
|
|
|
// Expand the #rgb format into #rrggbb (aa is set to 0xff)
|
|
|
|
tmp.v = (tmp.v & 0xf00) * 0x1100
|
|
|
|
| (tmp.v & 0x0f0) * 0x0110
|
|
|
|
| (tmp.v & 0x00f) * 0x0011;
|
|
|
|
case 6:
|
|
|
|
// If the code is in #rrggbb form then assume it's opaque
|
|
|
|
tmp.a = 255;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
// Colors in #aarrggbb format, those need no adjustments
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Invalid color specified\n");
|
|
|
|
return def;
|
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
// Premultiply the alpha in
|
|
|
|
if (tmp.a) {
|
|
|
|
// The components are clamped automagically as the rgba_t is made of uint8_t
|
|
|
|
return (rgba_t){
|
|
|
|
.r = (tmp.r * tmp.a) / 255,
|
|
|
|
.g = (tmp.g * tmp.a) / 255,
|
|
|
|
.b = (tmp.b * tmp.a) / 255,
|
|
|
|
.a = tmp.a,
|
|
|
|
};
|
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-10-24 12:04:37 +00:00
|
|
|
return (rgba_t)0U;
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
void
|
2014-02-19 17:53:32 +00:00
|
|
|
set_attribute (const char modifier, const char attribute)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2014-02-19 17:53:32 +00:00
|
|
|
int pos = indexof(attribute, "ou");
|
|
|
|
|
|
|
|
if (pos < 0) {
|
|
|
|
fprintf(stderr, "Invalid attribute \"%c\" found\n", attribute);
|
|
|
|
return;
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
switch (modifier) {
|
2014-10-08 19:47:26 +00:00
|
|
|
case '+':
|
2014-11-05 20:04:50 +00:00
|
|
|
attrs |= (1u<<pos);
|
2014-10-08 19:47:26 +00:00
|
|
|
break;
|
|
|
|
case '-':
|
2014-11-05 20:04:50 +00:00
|
|
|
attrs &=~(1u<<pos);
|
2014-10-08 19:47:26 +00:00
|
|
|
break;
|
|
|
|
case '!':
|
2014-11-05 20:04:50 +00:00
|
|
|
attrs ^= (1u<<pos);
|
2014-10-08 19:47:26 +00:00
|
|
|
break;
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-24 12:46:46 +00:00
|
|
|
|
|
|
|
area_t *
|
2015-01-15 23:37:45 +00:00
|
|
|
area_get (xcb_window_t win, const int btn, const int x)
|
2014-02-24 12:46:46 +00:00
|
|
|
{
|
2015-02-11 12:54:47 +00:00
|
|
|
// Looping backwards ensures that we get the innermost area first
|
2015-12-15 18:05:39 +00:00
|
|
|
for (int i = area_stack.at - 1; i >= 0; i--) {
|
2015-10-24 12:28:51 +00:00
|
|
|
area_t *a = &area_stack.area[i];
|
2015-12-15 18:05:39 +00:00
|
|
|
if (a->window == win && a->button == btn && x >= a->begin && x < a->end)
|
2015-01-15 23:37:45 +00:00
|
|
|
return a;
|
|
|
|
}
|
2014-02-24 12:46:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
void
|
|
|
|
area_shift (xcb_window_t win, const int align, int delta)
|
|
|
|
{
|
|
|
|
if (align == ALIGN_L)
|
|
|
|
return;
|
|
|
|
if (align == ALIGN_C)
|
|
|
|
delta /= 2;
|
|
|
|
|
2015-10-24 12:28:51 +00:00
|
|
|
for (int i = 0; i < area_stack.at; i++) {
|
|
|
|
area_t *a = &area_stack.area[i];
|
2015-02-27 13:57:19 +00:00
|
|
|
if (a->window == win && a->align == align && !a->active) {
|
|
|
|
a->begin -= delta;
|
|
|
|
a->end -= delta;
|
2014-02-26 10:07:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 12:46:46 +00:00
|
|
|
bool
|
2014-06-11 18:05:23 +00:00
|
|
|
area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x, const int align, const int button)
|
2014-02-24 12:46:46 +00:00
|
|
|
{
|
2015-01-16 21:20:14 +00:00
|
|
|
int i;
|
2014-12-13 11:12:34 +00:00
|
|
|
char *trail;
|
2015-01-15 23:37:45 +00:00
|
|
|
area_t *a;
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// A wild close area tag appeared!
|
2015-01-15 23:41:56 +00:00
|
|
|
if (*str != ':') {
|
|
|
|
*end = str;
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2015-02-11 12:54:47 +00:00
|
|
|
// Find most recent unclosed area.
|
2015-10-24 12:28:51 +00:00
|
|
|
for (i = area_stack.at - 1; i >= 0 && !area_stack.area[i].active; i--)
|
2015-01-15 23:37:45 +00:00
|
|
|
;
|
2015-10-24 12:28:51 +00:00
|
|
|
a = &area_stack.area[i];
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Basic safety checks
|
2015-10-24 12:35:36 +00:00
|
|
|
if (!a->cmd || a->align != align || a->window != mon->window) {
|
|
|
|
fprintf(stderr, "Invalid geometry for the clickable area\n");
|
2014-02-26 10:07:49 +00:00
|
|
|
return false;
|
2015-10-24 12:35:36 +00:00
|
|
|
}
|
2014-02-26 10:07:49 +00:00
|
|
|
|
|
|
|
const int size = x - a->begin;
|
|
|
|
|
|
|
|
switch (align) {
|
2014-02-26 16:42:31 +00:00
|
|
|
case ALIGN_L:
|
|
|
|
a->end = x;
|
|
|
|
break;
|
2014-02-26 10:07:49 +00:00
|
|
|
case ALIGN_C:
|
|
|
|
a->begin = mon->width / 2 - size / 2 + a->begin / 2;
|
|
|
|
a->end = a->begin + size;
|
|
|
|
break;
|
|
|
|
case ALIGN_R:
|
2015-01-25 20:14:06 +00:00
|
|
|
// The newest is the rightmost one
|
2014-02-26 10:07:49 +00:00
|
|
|
a->begin = mon->width - size;
|
|
|
|
a->end = mon->width;
|
|
|
|
break;
|
2014-02-24 12:46:46 +00:00
|
|
|
}
|
2014-02-26 10:07:49 +00:00
|
|
|
|
2015-01-15 23:37:45 +00:00
|
|
|
a->active = false;
|
2014-02-26 10:07:49 +00:00
|
|
|
return true;
|
2014-02-24 12:46:46 +00:00
|
|
|
}
|
|
|
|
|
2015-10-24 12:28:51 +00:00
|
|
|
if (area_stack.at + 1 > area_stack.max) {
|
|
|
|
fprintf(stderr, "Cannot add any more clickable areas (used %d/%d)\n",
|
|
|
|
area_stack.at, area_stack.max);
|
2015-01-15 23:37:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-24 12:28:51 +00:00
|
|
|
a = &area_stack.area[area_stack.at++];
|
2015-01-15 23:37:45 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Found the closing : and check if it's just an escaped one
|
2015-01-15 23:41:56 +00:00
|
|
|
for (trail = strchr(++str, ':'); trail && trail[-1] == '\\'; trail = strchr(trail + 1, ':'))
|
2014-12-13 11:12:34 +00:00
|
|
|
;
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2015-01-15 23:40:04 +00:00
|
|
|
// Find the trailing : and make sure it's within the formatting block, also reject empty commands
|
2015-01-15 23:41:56 +00:00
|
|
|
if (!trail || str == trail || trail > optend) {
|
|
|
|
*end = str;
|
2014-02-24 12:46:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*trail = '\0';
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Sanitize the user command by unescaping all the :
|
2015-01-15 23:41:56 +00:00
|
|
|
for (char *needle = str; *needle; needle++) {
|
2014-12-13 11:12:34 +00:00
|
|
|
int delta = trail - &needle[1];
|
|
|
|
if (needle[0] == '\\' && needle[1] == ':') {
|
|
|
|
memmove(&needle[0], &needle[1], delta);
|
|
|
|
needle[delta] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// This is a pointer to the string buffer allocated in the main
|
2015-01-15 23:41:56 +00:00
|
|
|
a->cmd = str;
|
2015-01-15 23:37:45 +00:00
|
|
|
a->active = true;
|
2014-02-26 10:07:49 +00:00
|
|
|
a->align = align;
|
|
|
|
a->begin = x;
|
|
|
|
a->window = mon->window;
|
2014-06-11 18:05:23 +00:00
|
|
|
a->button = button;
|
2014-02-24 12:46:46 +00:00
|
|
|
|
|
|
|
*end = trail + 1;
|
2014-02-26 10:07:49 +00:00
|
|
|
|
2014-02-24 12:46:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-21 18:43:22 +00:00
|
|
|
bool
|
|
|
|
font_has_glyph (font_t *font, const uint16_t c)
|
2014-07-22 18:47:41 +00:00
|
|
|
{
|
2015-06-02 06:44:28 +00:00
|
|
|
if (font->xft_ft) {
|
|
|
|
if (XftCharExists(dpy, font->xft_ft, (FcChar32) c)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-15 08:21:52 +00:00
|
|
|
if (c < font->char_min || c > font->char_max)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (font->width_lut && font->width_lut[c - font->char_min].character_width == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
2014-07-22 18:47:41 +00:00
|
|
|
}
|
|
|
|
|
2014-07-22 21:23:56 +00:00
|
|
|
font_t *
|
2014-07-22 22:35:21 +00:00
|
|
|
select_drawable_font (const uint16_t c)
|
2014-07-22 21:23:56 +00:00
|
|
|
{
|
2015-01-25 20:14:06 +00:00
|
|
|
// If the user has specified a font to use, try that first.
|
2015-06-02 07:03:55 +00:00
|
|
|
if (font_index != -1 && font_has_glyph(font_list[font_index - 1], c)) {
|
|
|
|
offset_y_index = font_index - 1;
|
2015-01-21 18:43:22 +00:00
|
|
|
return font_list[font_index - 1];
|
2015-06-02 07:03:55 +00:00
|
|
|
}
|
2015-01-21 18:43:22 +00:00
|
|
|
|
2015-03-15 20:35:54 +00:00
|
|
|
// If the end is reached without finding an appropriate font, return NULL.
|
2015-01-25 20:14:06 +00:00
|
|
|
// If the font can draw the character, return it.
|
|
|
|
for (int i = 0; i < font_count; i++) {
|
2015-06-02 07:03:55 +00:00
|
|
|
if (font_has_glyph(font_list[i], c)) {
|
|
|
|
offset_y_index = i;
|
2015-01-25 20:14:06 +00:00
|
|
|
return font_list[i];
|
2015-06-02 07:03:55 +00:00
|
|
|
}
|
2014-07-22 20:14:48 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
2014-07-22 21:23:56 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 13:00:49 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
void
|
|
|
|
parse (char *text)
|
|
|
|
{
|
2014-02-08 15:45:38 +00:00
|
|
|
font_t *cur_font;
|
|
|
|
monitor_t *cur_mon;
|
2014-06-11 18:05:23 +00:00
|
|
|
int pos_x, align, button;
|
2015-02-16 13:22:34 +00:00
|
|
|
char *p = text, *block_end, *ep;
|
2015-02-13 23:06:43 +00:00
|
|
|
rgba_t tmp;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
pos_x = 0;
|
|
|
|
align = ALIGN_L;
|
2014-02-09 23:54:33 +00:00
|
|
|
cur_mon = monhead;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2015-10-24 12:28:51 +00:00
|
|
|
// Reset the stack position
|
|
|
|
area_stack.at = 0;
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2014-05-29 00:38:36 +00:00
|
|
|
for (monitor_t *m = monhead; m != NULL; m = m->next)
|
|
|
|
fill_rect(m->pixmap, gc[GC_CLEAR], 0, 0, m->width, bh);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
/* Create xft drawable */
|
2015-03-04 19:06:09 +00:00
|
|
|
if (!(xft_draw = XftDrawCreate (dpy, cur_mon->pixmap, visual_ptr , colormap))) {
|
2014-10-08 19:47:26 +00:00
|
|
|
fprintf(stderr, "Couldn't create xft drawable\n");
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
for (;;) {
|
2014-02-04 20:35:54 +00:00
|
|
|
if (*p == '\0' || *p == '\n')
|
2014-10-09 10:58:12 +00:00
|
|
|
break;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2015-09-27 20:21:49 +00:00
|
|
|
if (p[0] == '%' && p[1] == '{' && (block_end = strchr(p++, '}'))) {
|
2015-07-14 17:47:49 +00:00
|
|
|
p++;
|
2015-02-16 13:22:34 +00:00
|
|
|
while (p < block_end) {
|
2014-03-01 18:31:27 +00:00
|
|
|
int w;
|
2014-02-19 17:53:32 +00:00
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
switch (*p++) {
|
|
|
|
case '+': set_attribute('+', *p++); break;
|
|
|
|
case '-': set_attribute('-', *p++); break;
|
|
|
|
case '!': set_attribute('!', *p++); break;
|
|
|
|
|
|
|
|
case 'R':
|
|
|
|
tmp = fgc;
|
|
|
|
fgc = bgc;
|
|
|
|
bgc = tmp;
|
|
|
|
update_gc();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': pos_x = 0; align = ALIGN_L; break;
|
|
|
|
case 'c': pos_x = 0; align = ALIGN_C; break;
|
|
|
|
case 'r': pos_x = 0; align = ALIGN_R; break;
|
|
|
|
|
2014-07-22 20:14:48 +00:00
|
|
|
case 'A':
|
2014-06-11 18:05:23 +00:00
|
|
|
button = XCB_BUTTON_INDEX_1;
|
2015-01-25 20:14:06 +00:00
|
|
|
// The range is 1-5
|
2014-06-11 18:05:23 +00:00
|
|
|
if (isdigit(*p) && (*p > '0' && *p < '6'))
|
|
|
|
button = *p++ - '0';
|
2015-10-24 12:28:51 +00:00
|
|
|
if (!area_add(p, block_end, &p, cur_mon, pos_x, align, button))
|
|
|
|
return;
|
2014-02-24 12:46:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
case 'B': bgc = parse_color(p, &p, dbgc); update_gc(); break;
|
|
|
|
case 'F': fgc = parse_color(p, &p, dfgc); update_gc(); break;
|
2016-01-19 13:18:16 +00:00
|
|
|
case 'U': ugc = parse_color(p, &p, dugc); update_gc(); break;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
case 'S':
|
|
|
|
if (*p == '+' && cur_mon->next)
|
2014-02-23 21:46:15 +00:00
|
|
|
{ cur_mon = cur_mon->next; }
|
|
|
|
else if (*p == '-' && cur_mon->prev)
|
|
|
|
{ cur_mon = cur_mon->prev; }
|
|
|
|
else if (*p == 'f')
|
|
|
|
{ cur_mon = monhead; }
|
|
|
|
else if (*p == 'l')
|
|
|
|
{ cur_mon = montail ? montail : monhead; }
|
|
|
|
else if (isdigit(*p))
|
2014-02-19 17:53:32 +00:00
|
|
|
{ cur_mon = monhead;
|
2014-02-09 23:54:33 +00:00
|
|
|
for (int i = 0; i != *p-'0' && cur_mon->next; i++)
|
|
|
|
cur_mon = cur_mon->next;
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
2014-02-23 21:46:15 +00:00
|
|
|
else
|
|
|
|
{ p++; continue; }
|
2015-02-24 13:00:49 +00:00
|
|
|
XftDrawDestroy (xft_draw);
|
|
|
|
if (!(xft_draw = XftDrawCreate (dpy, cur_mon->pixmap, visual_ptr , colormap ))) {
|
|
|
|
fprintf(stderr, "Couldn't create xft drawable\n");
|
|
|
|
}
|
2014-02-23 21:46:15 +00:00
|
|
|
|
2014-02-23 21:55:20 +00:00
|
|
|
p++;
|
2014-02-23 21:46:15 +00:00
|
|
|
pos_x = 0;
|
2015-07-26 15:04:03 +00:00
|
|
|
fill_rect(cur_mon->pixmap, gc[GC_CLEAR], 0, 0, cur_mon->width, bh);
|
2014-02-19 17:53:32 +00:00
|
|
|
break;
|
2014-03-01 18:31:27 +00:00
|
|
|
case 'O':
|
|
|
|
errno = 0;
|
|
|
|
w = (int) strtoul(p, &p, 10);
|
|
|
|
if (errno)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
draw_shift(cur_mon, pos_x, align, w);
|
|
|
|
|
|
|
|
pos_x += w;
|
|
|
|
area_shift(cur_mon->window, align, w);
|
|
|
|
break;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-01-21 18:43:22 +00:00
|
|
|
case 'T':
|
2015-10-21 05:22:52 +00:00
|
|
|
if (*p == '-') { //Reset to automatic font selection
|
2015-01-21 18:43:22 +00:00
|
|
|
font_index = -1;
|
2015-07-22 15:39:31 +00:00
|
|
|
p++;
|
|
|
|
break;
|
2015-07-22 20:46:30 +00:00
|
|
|
} else if (isdigit(*p)) {
|
|
|
|
font_index = (int)strtoul(p, &ep, 10);
|
|
|
|
// User-specified 'font_index' ∊ (0,font_count]
|
|
|
|
// Otherwise just fallback to the automatic font selection
|
|
|
|
if (!font_index || font_index > font_count)
|
2015-01-21 18:43:22 +00:00
|
|
|
font_index = -1;
|
2015-07-22 20:46:30 +00:00
|
|
|
p = ep;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Invalid font slot \"%c\"\n", *p++); //Swallow the token
|
|
|
|
break;
|
|
|
|
}
|
2015-01-21 18:43:22 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// In case of error keep parsing after the closing }
|
2014-02-19 17:53:32 +00:00
|
|
|
default:
|
2015-02-16 13:22:34 +00:00
|
|
|
p = block_end;
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
// Eat the trailing }
|
2014-02-19 17:53:32 +00:00
|
|
|
p++;
|
2015-01-25 20:14:06 +00:00
|
|
|
} else { // utf-8 -> ucs-2
|
2014-01-26 13:13:49 +00:00
|
|
|
uint8_t *utf = (uint8_t *)p;
|
|
|
|
uint16_t ucs;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// ASCII
|
2014-01-26 13:13:49 +00:00
|
|
|
if (utf[0] < 0x80) {
|
|
|
|
ucs = utf[0];
|
|
|
|
p += 1;
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
// Two byte utf8 sequence
|
2014-01-26 13:13:49 +00:00
|
|
|
else if ((utf[0] & 0xe0) == 0xc0) {
|
|
|
|
ucs = (utf[0] & 0x1f) << 6 | (utf[1] & 0x3f);
|
2012-07-23 14:49:39 +00:00
|
|
|
p += 2;
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
// Three byte utf8 sequence
|
2014-01-26 13:13:49 +00:00
|
|
|
else if ((utf[0] & 0xf0) == 0xe0) {
|
|
|
|
ucs = (utf[0] & 0xf) << 12 | (utf[1] & 0x3f) << 6 | (utf[2] & 0x3f);
|
2012-07-23 14:49:39 +00:00
|
|
|
p += 3;
|
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
// Four byte utf8 sequence
|
|
|
|
else if ((utf[0] & 0xf8) == 0xf0) {
|
|
|
|
ucs = 0xfffd;
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
// Five byte utf8 sequence
|
|
|
|
else if ((utf[0] & 0xfc) == 0xf8) {
|
|
|
|
ucs = 0xfffd;
|
|
|
|
p += 5;
|
|
|
|
}
|
2015-12-10 22:59:58 +00:00
|
|
|
// Six byte utf8 sequence
|
2015-01-25 20:14:06 +00:00
|
|
|
else if ((utf[0] & 0xfe) == 0xfc) {
|
|
|
|
ucs = 0xfffd;
|
|
|
|
p += 6;
|
|
|
|
}
|
|
|
|
// Not a valid utf-8 sequence
|
|
|
|
else {
|
2014-01-26 13:13:49 +00:00
|
|
|
ucs = utf[0];
|
2012-07-23 14:49:39 +00:00
|
|
|
p += 1;
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-07-22 18:47:41 +00:00
|
|
|
cur_font = select_drawable_font(ucs);
|
2014-07-22 20:14:48 +00:00
|
|
|
if (!cur_font)
|
2014-07-22 18:47:41 +00:00
|
|
|
continue;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
if(cur_font->ptr)
|
|
|
|
xcb_change_gc(c, gc[GC_DRAW] , XCB_GC_FONT, (const uint32_t []) {
|
|
|
|
cur_font->ptr
|
|
|
|
});
|
2014-02-26 10:07:49 +00:00
|
|
|
int w = draw_char(cur_mon, cur_font, pos_x, align, ucs);
|
|
|
|
|
|
|
|
pos_x += w;
|
|
|
|
area_shift(cur_mon->window, align, w);
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
2014-10-08 19:47:26 +00:00
|
|
|
XftDrawDestroy (xft_draw);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 21:07:42 +00:00
|
|
|
void
|
|
|
|
font_load (const char *pattern)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2015-06-02 07:27:53 +00:00
|
|
|
if (font_count >= MAX_FONT_COUNT) {
|
|
|
|
fprintf(stderr, "Max font count reached. Could not load font \"%s\"\n", pattern);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-07-23 14:49:39 +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-19 17:53:32 +00:00
|
|
|
font = xcb_generate_id(c);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
font_t *ret = calloc(1, sizeof(font_t));
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
if (!ret)
|
2015-04-23 21:07:42 +00:00
|
|
|
return;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2015-06-02 06:44:28 +00:00
|
|
|
cookie = xcb_open_font_checked(c, font, strlen(pattern), pattern);
|
2014-10-08 19:47:26 +00:00
|
|
|
if (!xcb_request_check (c, cookie)) {
|
|
|
|
queryreq = xcb_query_font(c, font);
|
|
|
|
font_info = xcb_query_font_reply(c, queryreq, NULL);
|
|
|
|
|
2014-11-20 21:00:11 +00:00
|
|
|
ret->xft_ft = NULL;
|
2014-10-08 19:47:26 +00:00
|
|
|
ret->ptr = font;
|
|
|
|
ret->descent = font_info->font_descent;
|
|
|
|
ret->height = font_info->font_ascent + font_info->font_descent;
|
2015-06-02 06:44:28 +00:00
|
|
|
ret->width = font_info->max_bounds.character_width;
|
2014-10-08 19:47:26 +00:00
|
|
|
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;
|
2015-02-24 13:00:49 +00:00
|
|
|
// Copy over the width lut as it's part of font_info
|
|
|
|
int lut_size = sizeof(xcb_charinfo_t) * xcb_query_font_char_infos_length(font_info);
|
|
|
|
if (lut_size) {
|
|
|
|
ret->width_lut = malloc(lut_size);
|
|
|
|
memcpy(ret->width_lut, xcb_query_font_char_infos(font_info), lut_size);
|
|
|
|
}
|
|
|
|
free(font_info);
|
2015-06-02 06:44:28 +00:00
|
|
|
} else if ((ret->xft_ft = XftFontOpenName (dpy, scr_nbr, pattern))) {
|
2014-11-20 21:00:11 +00:00
|
|
|
ret->ptr = 0;
|
2014-10-08 19:47:26 +00:00
|
|
|
ret->ascent = ret->xft_ft->ascent;
|
|
|
|
ret->descent = ret->xft_ft->descent;
|
|
|
|
ret->height = ret->ascent + ret->descent;
|
|
|
|
} else {
|
2015-06-02 06:44:28 +00:00
|
|
|
fprintf(stderr, "Could not load font %s\n", pattern);
|
2015-11-15 21:41:53 +00:00
|
|
|
free(ret);
|
2015-11-15 21:31:05 +00:00
|
|
|
return;
|
2014-10-08 19:47:26 +00:00
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
|
2015-04-23 21:07:42 +00:00
|
|
|
font_list[font_count++] = ret;
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2015-06-02 07:38:31 +00:00
|
|
|
void add_y_offset(int offset) {
|
|
|
|
if (offset_y_count >= MAX_FONT_COUNT) {
|
|
|
|
fprintf(stderr, "Max offset count reached. Could not set offset \"%d\"\n", offset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
offsets_y[offset_y_count] = strtol(optarg, NULL, 10);
|
|
|
|
if (offset_y_count == 0) {
|
|
|
|
for (int i = 1; i < MAX_FONT_COUNT; ++i) {
|
|
|
|
offsets_y[i] = offsets_y[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++offset_y_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-02 13:35:32 +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_STATE_STICKY,
|
|
|
|
NET_WM_STATE_ABOVE,
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-01-26 13:13:49 +00:00
|
|
|
set_ewmh_atoms (void)
|
2012-07-20 00:44:30 +00:00
|
|
|
{
|
2013-09-02 13:35:32 +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",
|
2015-01-25 20:14:06 +00:00
|
|
|
// Leave those at the end since are batch-set
|
2013-09-02 13:35:32 +00:00
|
|
|
"_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;
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// As suggested fetch all the cookies first (yum!) and then retrieve the
|
|
|
|
// atoms to exploit the async'ness
|
2013-09-02 13:35:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Prepare the strut array
|
2014-02-09 23:54:33 +00:00
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next) {
|
2013-11-16 14:07:53 +00:00
|
|
|
int strut[12] = {0};
|
2014-02-19 17:53:32 +00:00
|
|
|
if (topbar) {
|
|
|
|
strut[2] = bh;
|
2014-02-08 15:45:38 +00:00
|
|
|
strut[8] = mon->x;
|
2016-11-16 09:14:27 +00:00
|
|
|
strut[9] = mon->x + mon->width - 1;
|
2014-02-19 17:53:32 +00:00
|
|
|
} else {
|
|
|
|
strut[3] = bh;
|
|
|
|
strut[10] = mon->x;
|
2016-11-16 09:14:27 +00:00
|
|
|
strut[11] = mon->x + mon->width - 1;
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
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]);
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []) {
|
2014-11-05 20:04:50 +00:00
|
|
|
0u - 1u
|
2014-10-08 19:47:26 +00:00
|
|
|
} );
|
2014-02-19 17:53:32 +00:00
|
|
|
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);
|
2015-02-10 19:35:11 +00:00
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, 3, "bar");
|
2016-10-16 19:39:17 +00:00
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, 12, "lemonbar\0Bar");
|
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 *
|
2014-02-09 23:54:33 +00:00
|
|
|
monitor_new (int x, int y, int width, int height)
|
2014-02-08 15:45:38 +00:00
|
|
|
{
|
|
|
|
monitor_t *ret;
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
ret = calloc(1, sizeof(monitor_t));
|
2014-02-11 02:42:14 +00:00
|
|
|
if (!ret) {
|
|
|
|
fprintf(stderr, "Failed to allocate new monitor\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-02-08 15:45:38 +00:00
|
|
|
|
|
|
|
ret->x = x;
|
2015-02-10 19:35:11 +00:00
|
|
|
ret->y = (topbar ? by : height - bh - by) + y;
|
2014-02-08 15:45:38 +00:00
|
|
|
ret->width = width;
|
2014-02-09 23:54:33 +00:00
|
|
|
ret->next = ret->prev = NULL;
|
|
|
|
ret->window = xcb_generate_id(c);
|
2015-03-04 19:06:09 +00:00
|
|
|
int depth = (visual == scr->root_visual) ? XCB_COPY_FROM_PARENT : 32;
|
|
|
|
xcb_create_window(c, depth, ret->window, scr->root,
|
2015-02-24 13:00:49 +00:00
|
|
|
ret->x, ret->y, width, bh, 0,
|
2014-10-08 21:45:25 +00:00
|
|
|
XCB_WINDOW_CLASS_INPUT_OUTPUT, visual,
|
|
|
|
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
|
2014-10-08 19:47:26 +00:00
|
|
|
(const uint32_t []) {
|
2015-02-24 13:00:49 +00:00
|
|
|
bgc.v, bgc.v, dock, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS, colormap
|
2014-10-08 19:47:26 +00:00
|
|
|
});
|
2014-02-23 21:46:15 +00:00
|
|
|
|
|
|
|
ret->pixmap = xcb_generate_id(c);
|
2015-03-04 19:06:09 +00:00
|
|
|
xcb_create_pixmap(c, depth, ret->pixmap, ret->window, width, bh);
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-11 02:42:14 +00:00
|
|
|
void
|
|
|
|
monitor_add (monitor_t *mon)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-17 14:42:43 +00:00
|
|
|
int
|
|
|
|
rect_sort_cb (const void *p1, const void *p2)
|
|
|
|
{
|
2014-02-18 00:23:35 +00:00
|
|
|
const xcb_rectangle_t *r1 = (xcb_rectangle_t *)p1;
|
|
|
|
const xcb_rectangle_t *r2 = (xcb_rectangle_t *)p2;
|
|
|
|
|
2015-10-21 05:22:52 +00:00
|
|
|
if (r1->x < r2->x || r1->y + r1->height <= r2->y)
|
|
|
|
{
|
2014-02-18 00:23:35 +00:00
|
|
|
return -1;
|
2015-10-21 05:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r1->x > r2->x || r1->y + r1->height > r2->y)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2014-02-18 00:23:35 +00:00
|
|
|
|
|
|
|
return 0;
|
2014-02-17 14:42:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
void
|
|
|
|
monitor_create_chain (xcb_rectangle_t *rects, const int num)
|
|
|
|
{
|
2014-05-29 01:00:58 +00:00
|
|
|
int i;
|
|
|
|
int width = 0, height = 0;
|
2014-02-21 11:52:50 +00:00
|
|
|
int left = bx;
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Sort before use
|
2014-02-21 11:52:50 +00:00
|
|
|
qsort(rects, num, sizeof(xcb_rectangle_t), rect_sort_cb);
|
|
|
|
|
2014-05-29 01:00:58 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
int h = rects[i].y + rects[i].height;
|
2015-01-25 20:14:06 +00:00
|
|
|
// Accumulated width of all monitors
|
2014-05-29 01:00:58 +00:00
|
|
|
width += rects[i].width;
|
2015-01-25 20:14:06 +00:00
|
|
|
// Get height of screen from y_offset + height of lowest monitor
|
2014-05-29 01:00:58 +00:00
|
|
|
if (h >= height)
|
2014-10-08 19:47:26 +00:00
|
|
|
height = h;
|
2014-05-29 01:00:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bw < 0)
|
|
|
|
bw = width - bx;
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Use the first font height as all the font heights have been set to the biggest of the set
|
2014-05-29 01:00:58 +00:00
|
|
|
if (bh < 0 || bh > height)
|
2014-07-22 20:14:48 +00:00
|
|
|
bh = font_list[0]->height + bu + 2;
|
2014-05-29 01:00:58 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Check the geometry
|
2014-06-01 01:56:50 +00:00
|
|
|
if (bx + bw > width || by + bh > height) {
|
|
|
|
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Left is a positive number or zero therefore monitors with zero width are excluded
|
2014-05-29 01:00:58 +00:00
|
|
|
width = bw;
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (rects[i].y + rects[i].height < by)
|
|
|
|
continue;
|
2014-02-21 11:52:50 +00:00
|
|
|
if (rects[i].width > left) {
|
|
|
|
monitor_t *mon = monitor_new(
|
2014-10-08 19:47:26 +00:00
|
|
|
rects[i].x + left,
|
|
|
|
rects[i].y,
|
|
|
|
min(width, rects[i].width - left),
|
|
|
|
rects[i].height);
|
2014-02-21 11:52:50 +00:00
|
|
|
|
2015-02-18 11:52:24 +00:00
|
|
|
if (!mon)
|
|
|
|
break;
|
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
monitor_add(mon);
|
|
|
|
|
|
|
|
width -= rects[i].width - left;
|
2015-01-25 20:14:06 +00:00
|
|
|
// No need to check for other monitors
|
2014-02-21 11:52:50 +00:00
|
|
|
if (width <= 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
left -= rects[i].width;
|
|
|
|
|
|
|
|
if (left < 0)
|
|
|
|
left = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
void
|
2014-02-19 17:53:32 +00:00
|
|
|
get_randr_monitors (void)
|
2014-02-09 23:54:33 +00:00
|
|
|
{
|
|
|
|
xcb_randr_get_screen_resources_current_reply_t *rres_reply;
|
|
|
|
xcb_randr_output_t *outputs;
|
2014-06-01 01:52:48 +00:00
|
|
|
int i, j, num, valid = 0;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
rres_reply = xcb_randr_get_screen_resources_current_reply(c,
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_randr_get_screen_resources_current(c, scr->root), NULL);
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (!rres_reply) {
|
2014-02-09 23:54:33 +00:00
|
|
|
fprintf(stderr, "Failed to get current randr screen resources\n");
|
|
|
|
return;
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
num = xcb_randr_get_screen_resources_current_outputs_length(rres_reply);
|
|
|
|
outputs = xcb_randr_get_screen_resources_current_outputs(rres_reply);
|
2014-02-18 00:23:35 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// There should be at least one output
|
2014-02-20 20:21:41 +00:00
|
|
|
if (num < 1) {
|
|
|
|
free(rres_reply);
|
2014-02-09 23:54:33 +00:00
|
|
|
return;
|
2014-02-20 20:21:41 +00:00
|
|
|
}
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_rectangle_t rects[num];
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Get all outputs
|
2014-06-01 01:52:48 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_randr_get_output_info_reply_t *oi_reply;
|
|
|
|
xcb_randr_get_crtc_info_reply_t *ci_reply;
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
oi_reply = xcb_randr_get_output_info_reply(c, xcb_randr_get_output_info(c, outputs[i], XCB_CURRENT_TIME), NULL);
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Output disconnected or not attached to any CRTC ?
|
2014-02-20 20:38:50 +00:00
|
|
|
if (!oi_reply || oi_reply->crtc == XCB_NONE || oi_reply->connection != XCB_RANDR_CONNECTION_CONNECTED) {
|
2015-01-25 20:14:06 +00:00
|
|
|
free(oi_reply);
|
2014-02-09 23:54:33 +00:00
|
|
|
rects[i].width = 0;
|
|
|
|
continue;
|
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
ci_reply = xcb_randr_get_crtc_info_reply(c,
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_randr_get_crtc_info(c, oi_reply->crtc, XCB_CURRENT_TIME), NULL);
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
free(oi_reply);
|
|
|
|
|
|
|
|
if (!ci_reply) {
|
|
|
|
fprintf(stderr, "Failed to get RandR ctrc info\n");
|
2014-02-20 20:21:41 +00:00
|
|
|
free(rres_reply);
|
2014-02-19 17:53:32 +00:00
|
|
|
return;
|
2014-02-09 23:54:33 +00:00
|
|
|
}
|
2014-02-18 00:23:35 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// There's no need to handle rotated screens here (see #69)
|
2014-07-24 08:50:31 +00:00
|
|
|
rects[i] = (xcb_rectangle_t){ ci_reply->x, ci_reply->y, ci_reply->width, ci_reply->height };
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
free(ci_reply);
|
2014-02-18 00:23:35 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
valid++;
|
2014-02-09 23:54:33 +00:00
|
|
|
}
|
|
|
|
|
2014-02-20 20:21:41 +00:00
|
|
|
free(rres_reply);
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Check for clones and inactive outputs
|
2014-06-01 01:52:48 +00:00
|
|
|
for (i = 0; i < num; i++) {
|
2014-02-09 23:54:33 +00:00
|
|
|
if (rects[i].width == 0)
|
|
|
|
continue;
|
2014-02-18 00:23:35 +00:00
|
|
|
|
2014-06-01 01:52:48 +00:00
|
|
|
for (j = 0; j < num; j++) {
|
2015-03-15 20:35:54 +00:00
|
|
|
// Does I contain J ?
|
2014-02-20 20:21:41 +00:00
|
|
|
|
2014-02-18 00:23:35 +00:00
|
|
|
if (i != j && rects[j].width) {
|
2014-02-20 20:21:41 +00:00
|
|
|
if (rects[j].x >= rects[i].x && rects[j].x + rects[j].width <= rects[i].x + rects[i].width &&
|
2014-10-08 19:47:26 +00:00
|
|
|
rects[j].y >= rects[i].y && rects[j].y + rects[j].height <= rects[i].y + rects[i].height) {
|
2014-02-18 00:23:35 +00:00
|
|
|
rects[j].width = 0;
|
2014-02-19 17:53:32 +00:00
|
|
|
valid--;
|
2014-02-18 00:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 23:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (valid < 1) {
|
|
|
|
fprintf(stderr, "No usable RandR output found\n");
|
2014-02-09 23:54:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_rectangle_t r[valid];
|
2014-06-01 02:01:14 +00:00
|
|
|
|
2014-10-08 19:47:26 +00:00
|
|
|
for (i = j = 0; i < num && j < valid; i++)
|
|
|
|
if (rects[i].width != 0)
|
|
|
|
r[j++] = rects[i];
|
2014-06-01 01:52:48 +00:00
|
|
|
|
|
|
|
monitor_create_chain(r, valid);
|
2012-07-20 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 06:59:38 +00:00
|
|
|
#ifdef WITH_XINERAMA
|
2014-02-11 02:42:14 +00:00
|
|
|
void
|
2014-02-19 17:53:32 +00:00
|
|
|
get_xinerama_monitors (void)
|
2014-02-11 02:42:14 +00:00
|
|
|
{
|
|
|
|
xcb_xinerama_query_screens_reply_t *xqs_reply;
|
|
|
|
xcb_xinerama_screen_info_iterator_t iter;
|
2014-02-26 12:02:33 +00:00
|
|
|
int screens;
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
xqs_reply = xcb_xinerama_query_screens_reply(c,
|
2014-10-08 19:47:26 +00:00
|
|
|
xcb_xinerama_query_screens_unchecked(c), NULL);
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
iter = xcb_xinerama_query_screens_screen_info_iterator(xqs_reply);
|
|
|
|
screens = iter.rem;
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_rectangle_t rects[screens];
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Fetch all the screens first
|
2014-02-19 17:53:32 +00:00
|
|
|
for (int i = 0; iter.rem; i++) {
|
2014-02-17 14:42:43 +00:00
|
|
|
rects[i].x = iter.data->x_org;
|
|
|
|
rects[i].y = iter.data->y_org;
|
|
|
|
rects[i].width = iter.data->width;
|
|
|
|
rects[i].height = iter.data->height;
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_xinerama_screen_info_next(&iter);
|
2014-02-17 14:42:43 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
free(xqs_reply);
|
2014-02-21 11:52:50 +00:00
|
|
|
|
|
|
|
monitor_create_chain(rects, screens);
|
2014-02-11 02:42:14 +00:00
|
|
|
}
|
2016-04-23 06:59:38 +00:00
|
|
|
#endif
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-24 12:35:56 +00:00
|
|
|
xcb_visualid_t
|
|
|
|
get_visual (void)
|
|
|
|
{
|
2015-03-04 19:06:09 +00:00
|
|
|
|
|
|
|
XVisualInfo xv;
|
|
|
|
xv.depth = 32;
|
|
|
|
int result = 0;
|
|
|
|
XVisualInfo* result_ptr = NULL;
|
|
|
|
result_ptr = XGetVisualInfo(dpy, VisualDepthMask, &xv, &result);
|
|
|
|
|
|
|
|
if (result > 0) {
|
|
|
|
visual_ptr = result_ptr->visual;
|
|
|
|
return result_ptr->visualid;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Fallback
|
|
|
|
visual_ptr = DefaultVisual(dpy, scr_nbr);
|
|
|
|
return scr->root_visual;
|
2014-02-24 12:35:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-15 20:35:54 +00:00
|
|
|
// Parse an X-styled geometry string, we don't support signed offsets though.
|
2015-01-25 20:14:06 +00:00
|
|
|
bool
|
|
|
|
parse_geometry_string (char *str, int *tmp)
|
|
|
|
{
|
|
|
|
char *p = str;
|
|
|
|
int i = 0, j;
|
|
|
|
|
|
|
|
if (!str || !str[0])
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The leading = is optional
|
|
|
|
if (*p == '=')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
// A geometry string has only 4 fields
|
|
|
|
if (i >= 4) {
|
|
|
|
fprintf(stderr, "Invalid geometry specified\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Move on if we encounter a 'x' or '+'
|
|
|
|
if (*p == 'x') {
|
|
|
|
if (i > 0) // The 'x' must precede '+'
|
|
|
|
break;
|
|
|
|
i++; p++; continue;
|
|
|
|
}
|
|
|
|
if (*p == '+') {
|
|
|
|
if (i < 1) // Stray '+', skip the first two fields
|
|
|
|
i = 2;
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
p++; continue;
|
|
|
|
}
|
|
|
|
// A digit must follow
|
|
|
|
if (!isdigit(*p)) {
|
|
|
|
fprintf(stderr, "Invalid geometry specified\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Try to parse the number
|
|
|
|
errno = 0;
|
|
|
|
j = strtoul(p, &p, 10);
|
|
|
|
if (errno) {
|
|
|
|
fprintf(stderr, "Invalid geometry specified\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tmp[i] = j;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
void
|
2014-02-19 17:53:32 +00:00
|
|
|
xconn (void)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2014-10-08 19:47:26 +00:00
|
|
|
if ((dpy = XOpenDisplay(0)) == NULL) {
|
|
|
|
fprintf (stderr, "Couldnt open display\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((c = XGetXCBConnection(dpy)) == NULL) {
|
|
|
|
fprintf (stderr, "Couldnt connect to X\n");
|
|
|
|
exit (EXIT_FAILURE);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-10-08 21:16:48 +00:00
|
|
|
XSetEventQueueOwner(dpy, XCBOwnsEventQueue);
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (xcb_connection_has_error(c)) {
|
|
|
|
fprintf(stderr, "Couldn't connect to X\n");
|
|
|
|
exit(EXIT_FAILURE);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
/* Grab infos from the first screen */
|
2014-02-19 17:53:32 +00:00
|
|
|
scr = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
|
2014-02-24 12:35:56 +00:00
|
|
|
|
|
|
|
/* Try to get a RGBA visual and build the colormap for that */
|
2014-10-08 21:00:19 +00:00
|
|
|
visual = get_visual();
|
2015-03-04 19:06:09 +00:00
|
|
|
colormap = xcb_generate_id(c);
|
|
|
|
xcb_create_colormap(c, XCB_COLORMAP_ALLOC_NONE, colormap, scr->root, visual);
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
void
|
2016-06-08 21:02:10 +00:00
|
|
|
init (char *wm_name, char *wm_instance)
|
2014-02-19 17:53:32 +00:00
|
|
|
{
|
2015-01-25 20:14:06 +00:00
|
|
|
// Try to load a default font
|
|
|
|
if (!font_count)
|
2015-04-23 21:07:42 +00:00
|
|
|
font_load("fixed");
|
2014-07-22 18:47:41 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// We tried and failed hard, there's something wrong
|
|
|
|
if (!font_count)
|
2014-02-08 15:45:38 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// To make the alignment uniform, find maximum height
|
2014-07-22 20:14:48 +00:00
|
|
|
int maxh = font_list[0]->height;
|
2015-01-25 20:14:06 +00:00
|
|
|
for (int i = 1; i < font_count; i++)
|
2014-07-22 20:14:48 +00:00
|
|
|
maxh = max(maxh, font_list[i]->height);
|
2014-07-22 18:47:41 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Set maximum height to all fonts
|
|
|
|
for (int i = 0; i < font_count; i++)
|
2014-07-22 20:14:48 +00:00
|
|
|
font_list[i]->height = maxh;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Generate a list of screens
|
2014-02-09 23:54:33 +00:00
|
|
|
const xcb_query_extension_reply_t *qe_reply;
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2015-03-15 20:35:54 +00:00
|
|
|
// Initialize monitor list head and tail
|
2014-02-09 23:54:33 +00:00
|
|
|
monhead = montail = NULL;
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Check if RandR is present
|
2014-10-08 21:00:19 +00:00
|
|
|
qe_reply = xcb_get_extension_data(c, &xcb_randr_id);
|
|
|
|
|
|
|
|
if (qe_reply && qe_reply->present) {
|
2014-02-19 17:53:32 +00:00
|
|
|
get_randr_monitors();
|
2016-04-23 06:59:38 +00:00
|
|
|
}
|
|
|
|
#if WITH_XINERAMA
|
|
|
|
else {
|
2014-02-19 17:53:32 +00:00
|
|
|
qe_reply = xcb_get_extension_data(c, &xcb_xinerama_id);
|
2013-11-16 14:07:53 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Check if Xinerama extension is present and active
|
2014-02-09 23:54:33 +00:00
|
|
|
if (qe_reply && qe_reply->present) {
|
|
|
|
xcb_xinerama_is_active_reply_t *xia_reply;
|
2014-02-19 17:53:32 +00:00
|
|
|
xia_reply = xcb_xinerama_is_active_reply(c, xcb_xinerama_is_active(c), NULL);
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
if (xia_reply && xia_reply->state)
|
2014-02-19 17:53:32 +00:00
|
|
|
get_xinerama_monitors();
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
free(xia_reply);
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-23 06:59:38 +00:00
|
|
|
#endif
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-05-29 01:00:58 +00:00
|
|
|
if (!monhead) {
|
2015-01-25 20:14:06 +00:00
|
|
|
// If I fits I sits
|
2014-05-29 01:00:58 +00:00
|
|
|
if (bw < 0)
|
|
|
|
bw = scr->width_in_pixels - bx;
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Adjust the height
|
2014-05-29 01:00:58 +00:00
|
|
|
if (bh < 0 || bh > scr->height_in_pixels)
|
2014-07-22 18:47:41 +00:00
|
|
|
bh = maxh + bu + 2;
|
2014-05-29 01:00:58 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Check the geometry
|
2014-06-01 13:21:06 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// If no RandR outputs or Xinerama screens, fall back to using whole screen
|
2014-02-19 17:53:32 +00:00
|
|
|
monhead = monitor_new(0, 0, bw, scr->height_in_pixels);
|
2014-05-29 01:00:58 +00:00
|
|
|
}
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (!monhead)
|
2014-02-08 15:45:38 +00:00
|
|
|
exit(EXIT_FAILURE);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// For WM that support EWMH atoms
|
2013-09-02 13:35:32 +00:00
|
|
|
set_ewmh_atoms();
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Create the gc for drawing
|
2014-02-24 12:35:56 +00:00
|
|
|
gc[GC_DRAW] = xcb_generate_id(c);
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_create_gc(c, gc[GC_DRAW], monhead->pixmap, XCB_GC_FOREGROUND, (const uint32_t []){ fgc.v });
|
2014-02-24 12:35:56 +00:00
|
|
|
|
|
|
|
gc[GC_CLEAR] = xcb_generate_id(c);
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_create_gc(c, gc[GC_CLEAR], monhead->pixmap, XCB_GC_FOREGROUND, (const uint32_t []){ bgc.v });
|
2014-02-24 12:35:56 +00:00
|
|
|
|
|
|
|
gc[GC_ATTR] = xcb_generate_id(c);
|
2015-02-13 23:06:43 +00:00
|
|
|
xcb_create_gc(c, gc[GC_ATTR], monhead->pixmap, XCB_GC_FOREGROUND, (const uint32_t []){ ugc.v });
|
2014-02-24 12:35:56 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Make the bar visible and clear the pixmap
|
2014-02-24 12:35:56 +00:00
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next) {
|
|
|
|
fill_rect(mon->pixmap, gc[GC_CLEAR], 0, 0, mon->width, bh);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_map_window(c, mon->window);
|
2015-02-10 19:35:11 +00:00
|
|
|
|
|
|
|
// Make sure that the window really gets in the place it's supposed to be
|
|
|
|
// Some WM such as Openbox need this
|
|
|
|
xcb_configure_window(c, mon->window, XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y, (const uint32_t []){ mon->x, mon->y });
|
2015-10-24 12:44:41 +00:00
|
|
|
|
|
|
|
// Set the WM_NAME atom to the user specified value
|
|
|
|
if (wm_name)
|
2016-05-22 18:09:26 +00:00
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8 ,strlen(wm_name), wm_name);
|
2016-06-08 21:02:10 +00:00
|
|
|
|
|
|
|
// set the WM_CLASS atom instance to the executable name
|
|
|
|
if (wm_instance) {
|
|
|
|
char *wm_class;
|
|
|
|
int wm_class_offset, wm_class_len;
|
|
|
|
|
2016-10-16 19:39:17 +00:00
|
|
|
// WM_CLASS is nullbyte seperated: wm_instance + "\0Bar\0"
|
2016-06-08 21:02:10 +00:00
|
|
|
wm_class_offset = strlen(wm_instance) + 1;
|
|
|
|
wm_class_len = wm_class_offset + 4;
|
|
|
|
|
|
|
|
wm_class = calloc(1, wm_class_len + 1);
|
|
|
|
strcpy(wm_class, wm_instance);
|
2016-10-16 19:39:17 +00:00
|
|
|
strcpy(wm_class+wm_class_offset, "Bar");
|
2016-06-08 21:02:10 +00:00
|
|
|
|
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, wm_class_len, wm_class);
|
|
|
|
|
|
|
|
free(wm_class);
|
|
|
|
}
|
2014-02-24 12:35:56 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-11-25 12:16:01 +00:00
|
|
|
char color[] = "#ffffff";
|
2015-02-24 13:00:49 +00:00
|
|
|
uint32_t nfgc = fgc.v & 0x00ffffff;
|
2014-11-25 12:16:01 +00:00
|
|
|
snprintf(color, sizeof(color), "#%06X", nfgc);
|
2014-10-08 19:47:26 +00:00
|
|
|
|
2014-10-08 21:00:19 +00:00
|
|
|
if (!XftColorAllocName (dpy, visual_ptr, colormap, color, &sel_fg)) {
|
2014-10-08 19:47:26 +00:00
|
|
|
fprintf(stderr, "Couldn't allocate xft font color '%s'\n", color);
|
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_flush(c);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
void
|
|
|
|
cleanup (void)
|
|
|
|
{
|
2015-10-24 12:28:51 +00:00
|
|
|
free(area_stack.area);
|
2014-07-22 20:14:48 +00:00
|
|
|
for (int i = 0; font_list[i]; i++) {
|
2014-10-08 19:47:26 +00:00
|
|
|
if (font_list[i]->xft_ft) {
|
|
|
|
XftFontClose (dpy, font_list[i]->xft_ft);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xcb_close_font(c, font_list[i]->ptr);
|
2015-02-24 13:00:49 +00:00
|
|
|
free(font_list[i]->width_lut);
|
2014-10-08 19:47:26 +00:00
|
|
|
}
|
2014-07-22 20:14:48 +00:00
|
|
|
free(font_list[i]);
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
while (monhead) {
|
|
|
|
monitor_t *next = monhead->next;
|
2014-02-23 21:46:15 +00:00
|
|
|
xcb_destroy_window(c, monhead->window);
|
|
|
|
xcb_free_pixmap(c, monhead->pixmap);
|
2014-02-09 23:54:33 +00:00
|
|
|
free(monhead);
|
|
|
|
monhead = next;
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-10-08 21:00:19 +00:00
|
|
|
XftColorFree(dpy, visual_ptr, colormap, &sel_fg);
|
2014-02-24 12:35:56 +00:00
|
|
|
|
2014-02-23 21:46:15 +00:00
|
|
|
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]);
|
2012-07-24 12:12:04 +00:00
|
|
|
if (c)
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_disconnect(c);
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
|
|
|
|
2016-06-08 21:02:10 +00:00
|
|
|
char*
|
|
|
|
strip_path(char *path)
|
|
|
|
{
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
if (path == NULL || *path == '\0')
|
|
|
|
return strdup("lemonbar");
|
|
|
|
|
|
|
|
slash = strrchr(path, '/');
|
|
|
|
if (slash != NULL)
|
|
|
|
return strndup(slash + 1, 31);
|
|
|
|
|
|
|
|
return strndup(path, 31);
|
|
|
|
}
|
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
void
|
|
|
|
sighandle (int signal)
|
|
|
|
{
|
2014-02-19 17:53:32 +00:00
|
|
|
if (signal == SIGINT || signal == SIGTERM)
|
|
|
|
exit(EXIT_SUCCESS);
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
int
|
2012-07-16 10:30:49 +00:00
|
|
|
main (int argc, char **argv)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2014-02-19 17:53:32 +00:00
|
|
|
struct pollfd pollin[2] = {
|
|
|
|
{ .fd = STDIN_FILENO, .events = POLLIN },
|
|
|
|
{ .fd = -1 , .events = POLLIN },
|
2012-07-20 09:53:29 +00:00
|
|
|
};
|
2012-07-17 09:34:12 +00:00
|
|
|
xcb_generic_event_t *ev;
|
|
|
|
xcb_expose_event_t *expose_ev;
|
2014-02-24 12:46:46 +00:00
|
|
|
xcb_button_press_event_t *press_ev;
|
2014-06-11 18:05:23 +00:00
|
|
|
char input[4096] = {0, };
|
2014-02-19 17:53:32 +00:00
|
|
|
bool permanent = false;
|
2014-02-21 11:52:50 +00:00
|
|
|
int geom_v[4] = { -1, -1, 0, 0 };
|
2015-10-24 12:28:51 +00:00
|
|
|
int ch, areas;
|
2015-10-24 12:44:41 +00:00
|
|
|
char *wm_name;
|
2016-06-08 21:02:10 +00:00
|
|
|
char *instance_name;
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Install the parachute!
|
2014-02-19 17:53:32 +00:00
|
|
|
atexit(cleanup);
|
|
|
|
signal(SIGINT, sighandle);
|
|
|
|
signal(SIGTERM, sighandle);
|
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// B/W combo
|
2015-10-24 12:28:51 +00:00
|
|
|
dbgc = bgc = (rgba_t)0x00000000U;
|
|
|
|
dfgc = fgc = (rgba_t)0xffffffffU;
|
2014-02-24 12:35:56 +00:00
|
|
|
|
2016-01-19 13:18:16 +00:00
|
|
|
dugc = ugc = fgc;
|
2012-07-17 09:34:12 +00:00
|
|
|
|
2015-10-24 12:28:51 +00:00
|
|
|
// A safe default
|
|
|
|
areas = 10;
|
2015-10-24 12:44:41 +00:00
|
|
|
wm_name = NULL;
|
2012-07-17 09:34:12 +00:00
|
|
|
|
2016-06-08 21:02:10 +00:00
|
|
|
instance_name = strip_path(argv[0]);
|
|
|
|
|
2015-10-28 21:41:15 +00:00
|
|
|
// Connect to the Xserver and initialize scr
|
|
|
|
xconn();
|
|
|
|
|
2016-02-17 21:18:23 +00:00
|
|
|
while ((ch = getopt(argc, argv, "hg:bdf:a:pu:B:F:U:n:o:")) != -1) {
|
2012-07-16 10:30:49 +00:00
|
|
|
switch (ch) {
|
2014-02-19 17:53:32 +00:00
|
|
|
case 'h':
|
2016-01-15 11:44:15 +00:00
|
|
|
printf ("lemonbar version %s patched with XFT support\n", VERSION);
|
2015-10-24 12:44:41 +00:00
|
|
|
printf ("usage: %s [-h | -g | -b | -d | -f | -a | -p | -n | -u | -B | -F]\n"
|
2012-07-24 10:08:02 +00:00
|
|
|
"\t-h Show this help\n"
|
2014-04-24 21:46:00 +00:00
|
|
|
"\t-g Set the bar geometry {width}x{height}+{xoffset}+{yoffset}\n"
|
2015-03-14 12:51:29 +00:00
|
|
|
"\t-b Put the 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"
|
2015-05-03 15:06:36 +00:00
|
|
|
"\t-f Set the font name to use\n"
|
2015-10-24 12:28:51 +00:00
|
|
|
"\t-a Number of clickable areas available (default is 10)\n"
|
2014-02-19 17:53:32 +00:00
|
|
|
"\t-p Don't close after the data ends\n"
|
2015-10-24 12:44:41 +00:00
|
|
|
"\t-n Set the WM_NAME atom to the specified value for this bar\n"
|
2014-02-21 11:52:50 +00:00
|
|
|
"\t-u Set the underline/overline height in pixels\n"
|
2014-02-24 12:35:56 +00:00
|
|
|
"\t-B Set background color in #AARRGGBB\n"
|
2015-03-30 13:38:04 +00:00
|
|
|
"\t-F Set foreground color in #AARRGGBB\n"
|
|
|
|
"\t-o Add a vertical offset to the text, it can be negative\n", argv[0]);
|
2014-02-08 15:45:38 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
2014-02-21 11:52:50 +00:00
|
|
|
case 'g': (void)parse_geometry_string(optarg, geom_v); break;
|
2014-02-19 17:53:32 +00:00
|
|
|
case 'p': permanent = true; break;
|
2016-05-22 13:31:42 +00:00
|
|
|
case 'n': wm_name = strdup(optarg); break;
|
2014-02-19 17:53:32 +00:00
|
|
|
case 'b': topbar = false; break;
|
|
|
|
case 'd': dock = true; break;
|
2015-04-23 21:07:42 +00:00
|
|
|
case 'f': font_load(optarg); break;
|
2014-02-21 11:52:50 +00:00
|
|
|
case 'u': bu = strtoul(optarg, NULL, 10); break;
|
2015-06-02 07:38:31 +00:00
|
|
|
case 'o': add_y_offset(strtol(optarg, NULL, 10)); break;
|
2015-10-24 12:28:51 +00:00
|
|
|
case 'B': dbgc = bgc = parse_color(optarg, NULL, (rgba_t)0x00000000U); break;
|
|
|
|
case 'F': dfgc = fgc = parse_color(optarg, NULL, (rgba_t)0xffffffffU); break;
|
2016-01-19 13:18:16 +00:00
|
|
|
case 'U': dugc = ugc = parse_color(optarg, NULL, fgc); break;
|
2015-10-24 12:28:51 +00:00
|
|
|
case 'a': areas = strtoul(optarg, NULL, 10); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the stack holding the clickable areas
|
|
|
|
area_stack.at = 0;
|
|
|
|
area_stack.max = areas;
|
|
|
|
if (areas) {
|
|
|
|
area_stack.area = calloc(areas, sizeof(area_t));
|
|
|
|
|
|
|
|
if (!area_stack.area) {
|
|
|
|
fprintf(stderr, "Could not allocate enough memory for %d clickable areas, try lowering the number\n", areas);
|
|
|
|
return EXIT_FAILURE;
|
2012-07-16 10:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
2015-10-24 12:28:51 +00:00
|
|
|
else
|
|
|
|
area_stack.area = NULL;
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Copy the geometry values in place
|
2014-02-21 11:52:50 +00:00
|
|
|
bw = geom_v[0];
|
|
|
|
bh = geom_v[1];
|
|
|
|
bx = geom_v[2];
|
2014-04-24 21:46:00 +00:00
|
|
|
by = geom_v[3];
|
2014-02-21 11:52:50 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// Do the heavy lifting
|
2016-06-08 21:02:10 +00:00
|
|
|
init(wm_name, instance_name);
|
2016-05-22 13:31:42 +00:00
|
|
|
// The string is strdup'd when the command line arguments are parsed
|
|
|
|
free(wm_name);
|
2016-06-08 21:02:10 +00:00
|
|
|
// The string is strdup'd when stripping argv[0]
|
|
|
|
free(instance_name);
|
2015-01-25 20:14:06 +00:00
|
|
|
// Get the fd to Xserver
|
2014-02-19 17:53:32 +00:00
|
|
|
pollin[1].fd = xcb_get_file_descriptor(c);
|
2012-07-16 23:17:11 +00:00
|
|
|
for (;;) {
|
2014-02-19 17:53:32 +00:00
|
|
|
bool redraw = false;
|
2012-07-17 14:56:03 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
// If connection is in error state, then it has been shut down.
|
2015-01-06 23:37:08 +00:00
|
|
|
if (xcb_connection_has_error(c))
|
|
|
|
break;
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (poll(pollin, 2, -1) > 0) {
|
2015-01-25 20:14:06 +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
|
2012-07-18 19:27:41 +00:00
|
|
|
}
|
2015-01-25 20:14:06 +00:00
|
|
|
if (pollin[0].revents & POLLIN) { // New input, process it
|
2014-02-19 17:53:32 +00:00
|
|
|
if (fgets(input, sizeof(input), stdin) == NULL)
|
2015-01-25 20:14:06 +00:00
|
|
|
break; // EOF received
|
2014-02-04 20:28:01 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
parse(input);
|
|
|
|
redraw = true;
|
2012-07-17 14:56:03 +00:00
|
|
|
}
|
2015-03-15 20:35:54 +00:00
|
|
|
if (pollin[1].revents & POLLIN) { // The event comes from the Xorg server
|
2014-02-19 17:53:32 +00:00
|
|
|
while ((ev = xcb_poll_for_event(c))) {
|
2012-07-17 14:56:03 +00:00
|
|
|
expose_ev = (xcb_expose_event_t *)ev;
|
|
|
|
|
|
|
|
switch (ev->response_type & 0x7F) {
|
2014-02-19 17:53:32 +00:00
|
|
|
case XCB_EXPOSE:
|
2015-01-21 18:43:22 +00:00
|
|
|
if (expose_ev->count == 0)
|
2014-02-26 12:02:33 +00:00
|
|
|
redraw = true;
|
|
|
|
break;
|
2014-02-24 12:46:46 +00:00
|
|
|
case XCB_BUTTON_PRESS:
|
|
|
|
press_ev = (xcb_button_press_event_t *)ev;
|
2014-06-11 18:05:23 +00:00
|
|
|
{
|
2015-01-15 23:37:45 +00:00
|
|
|
area_t *area = area_get(press_ev->event, press_ev->detail, press_ev->event_x);
|
2015-01-25 20:14:06 +00:00
|
|
|
// Respond to the click
|
2015-01-16 21:15:00 +00:00
|
|
|
if (area) {
|
2016-04-23 07:06:00 +00:00
|
|
|
(void)write(STDOUT_FILENO, area->cmd, strlen(area->cmd));
|
|
|
|
(void)write(STDOUT_FILENO, "\n", 1);
|
2014-06-11 18:05:23 +00:00
|
|
|
}
|
2014-02-24 12:46:46 +00:00
|
|
|
}
|
2014-10-08 19:47:26 +00:00
|
|
|
break;
|
2012-07-17 14:56:03 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
free(ev);
|
2012-07-17 14:56:03 +00:00
|
|
|
}
|
2012-07-16 23:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-17 09:34:12 +00:00
|
|
|
|
2015-01-25 20:14:06 +00:00
|
|
|
if (redraw) { // Copy our temporary pixmap onto the window
|
2014-02-09 23:54:33 +00:00
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next) {
|
2014-02-23 21:46:15 +00:00
|
|
|
xcb_copy_area(c, mon->pixmap, mon->window, gc[GC_DRAW], 0, 0, 0, 0, mon->width, bh);
|
2014-01-24 11:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
xcb_flush(c);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
return EXIT_SUCCESS;
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|