2012-07-24 10:08:02 +00:00
|
|
|
#include <stdbool.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
2012-07-16 23:17:11 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2014-02-21 11:52:50 +00:00
|
|
|
#include <errno.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
#include <xcb/xcb.h>
|
2013-09-25 03:14:43 +00:00
|
|
|
#include <xcb/xinerama.h>
|
2014-02-12 04:29:40 +00:00
|
|
|
#include <xcb/randr.h>
|
2014-07-23 19:15:40 +00:00
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <cairo/cairo-xcb.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
|
|
|
|
// Here be dragons
|
|
|
|
|
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 monitor_t {
|
2014-02-26 12:02:33 +00:00
|
|
|
int x, width;
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_window_t window;
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
cairo_t *cr;
|
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 {
|
2014-06-11 18:05:23 +00:00
|
|
|
int begin, end, align, button;
|
2014-02-26 10:07:49 +00:00
|
|
|
xcb_window_t window;
|
2014-02-24 12:46:46 +00:00
|
|
|
char *cmd;
|
|
|
|
} area_t;
|
|
|
|
|
2014-06-11 18:05:23 +00:00
|
|
|
#define N 20
|
2014-02-26 10:07:49 +00:00
|
|
|
|
|
|
|
typedef struct area_stack_t {
|
|
|
|
int pos;
|
|
|
|
area_t slot[N];
|
|
|
|
} area_stack_t;
|
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
typedef struct color_t {
|
|
|
|
double r, g, b, a;
|
|
|
|
} color_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
|
|
|
};
|
|
|
|
|
2012-07-24 10:25:45 +00:00
|
|
|
enum {
|
|
|
|
ALIGN_L = 0,
|
|
|
|
ALIGN_C,
|
|
|
|
ALIGN_R
|
|
|
|
};
|
|
|
|
|
2014-02-23 21:46:15 +00:00
|
|
|
enum {
|
|
|
|
GC_DRAW = 0,
|
|
|
|
GC_CLEAR,
|
|
|
|
GC_ATTR,
|
|
|
|
GC_MAX
|
|
|
|
};
|
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static xcb_connection_t *c;
|
2014-02-19 17:53:32 +00:00
|
|
|
static xcb_screen_t *scr;
|
2014-07-23 19:15:40 +00:00
|
|
|
static xcb_visualtype_t *vt;
|
2014-02-24 12:35:56 +00:00
|
|
|
static xcb_colormap_t colormap;
|
2014-02-12 15:01:42 +00:00
|
|
|
static monitor_t *monhead, *montail;
|
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;
|
2014-02-21 11:52:50 +00:00
|
|
|
static int bu = 1; /* Underline height */
|
2014-07-23 19:15:40 +00:00
|
|
|
static char *mfont = NULL;
|
|
|
|
static uint32_t fgc, bgc;
|
2014-02-26 10:07:49 +00:00
|
|
|
static area_stack_t astack;
|
2015-01-07 02:55:34 +00:00
|
|
|
static char *img_file = NULL;
|
2012-07-18 15:11:17 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
enum {
|
|
|
|
PAL_BG,
|
|
|
|
PAL_FG,
|
|
|
|
PAL_ATTR,
|
|
|
|
PAL_MAX,
|
|
|
|
};
|
|
|
|
|
|
|
|
static color_t palette[PAL_MAX];
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_set_color (cairo_t *cr, const int i)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_set_source_rgba(cr, palette[i].r, palette[i].g, palette[i].b, palette[i].a);
|
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
|
2014-07-23 19:15:40 +00:00
|
|
|
fill_rect (cairo_t *cr, const int i, int x, int y, int width, int height)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2014-07-23 19:15:40 +00:00
|
|
|
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);
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
int
|
2015-01-07 02:55:34 +00:00
|
|
|
draw_char (monitor_t *mon, int x, int align, char *ch, int draw_image)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_font_extents_t ext;
|
|
|
|
cairo_text_extents_t te;
|
|
|
|
cairo_font_extents(mon->cr, &ext);
|
|
|
|
cairo_text_extents(mon->cr, ch, &te);
|
2012-07-25 14:55:26 +00:00
|
|
|
|
2015-01-07 03:19:31 +00:00
|
|
|
int ch_width;
|
|
|
|
|
|
|
|
/* Calculate the width of the character or image. */
|
|
|
|
cairo_surface_t *img;
|
|
|
|
if (draw_image && img_file != NULL) {
|
|
|
|
img = cairo_image_surface_create_from_png(img_file);
|
|
|
|
int w = cairo_image_surface_get_width(img);
|
|
|
|
int h = cairo_image_surface_get_height(img);
|
|
|
|
ch_width = w;
|
|
|
|
} else {
|
|
|
|
ch_width = (int)te.x_advance + 1;
|
|
|
|
}
|
2012-07-25 14:55:26 +00:00
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
switch (align) {
|
2012-07-24 10:25:45 +00:00
|
|
|
case ALIGN_C:
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_copy(mon->cr, mon->surface, 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:
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_copy(mon->cr, mon->surface, 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;
|
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2012-07-16 20:56:19 +00:00
|
|
|
/* Draw the background first */
|
2014-07-23 19:15:40 +00:00
|
|
|
fill_rect(mon->cr, PAL_BG, x, by, ch_width, bh);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2015-01-07 03:19:31 +00:00
|
|
|
if (draw_image && img != NULL) {
|
2015-01-07 02:55:34 +00:00
|
|
|
cairo_set_source_surface(mon->cr, img, x, 0);
|
|
|
|
cairo_mask_surface(mon->cr, img, x, 0);
|
|
|
|
cairo_surface_destroy(img);
|
|
|
|
} else {
|
|
|
|
/* String baseline coordinates */
|
|
|
|
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);
|
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
/* We can render both at the same time */
|
|
|
|
if (attrs & ATTR_OVERL)
|
2014-07-23 19:15:40 +00:00
|
|
|
fill_rect(mon->cr, PAL_ATTR, x, 0, ch_width, bu);
|
2014-02-19 17:53:32 +00:00
|
|
|
if (attrs & ATTR_UNDERL)
|
2014-07-23 19:15:40 +00:00
|
|
|
fill_rect(mon->cr, PAL_ATTR, x, bh - bu, ch_width, bu);
|
2012-07-25 18:33:01 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
return ch_width;
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
uint32_t
|
|
|
|
parse_color (const char *str, char **end, const uint32_t def)
|
|
|
|
{
|
|
|
|
xcb_alloc_named_color_reply_t *nc_reply;
|
|
|
|
int str_len;
|
|
|
|
uint32_t ret;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return def;
|
|
|
|
|
|
|
|
/* Reset */
|
|
|
|
if (str[0] == '-') {
|
|
|
|
if (end)
|
|
|
|
*end = (char *)str + 1;
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
|
2014-02-24 12:35:56 +00:00
|
|
|
/* Hex representation */
|
|
|
|
if (str[0] == '#') {
|
|
|
|
errno = 0;
|
|
|
|
uint32_t tmp = strtoul(str + 1, end, 16);
|
2014-05-07 18:53:10 +00:00
|
|
|
/* Some error checking is definitely good */
|
2014-02-24 12:35:56 +00:00
|
|
|
if (errno)
|
2014-05-07 18:53:10 +00:00
|
|
|
tmp = def;
|
2014-02-24 12:35:56 +00:00
|
|
|
return tmp;
|
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
/* Actual color name, resolve it */
|
|
|
|
str_len = 0;
|
|
|
|
while (isalpha(str[str_len]))
|
|
|
|
str_len++;
|
|
|
|
|
2014-02-24 12:35:56 +00:00
|
|
|
nc_reply = xcb_alloc_named_color_reply(c, xcb_alloc_named_color(c, colormap, str_len, str), NULL);
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
if (!nc_reply)
|
|
|
|
fprintf(stderr, "Could not alloc color \"%.*s\"\n", str_len, str);
|
|
|
|
ret = (nc_reply) ? nc_reply->pixel : def;
|
|
|
|
free(nc_reply);
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
*end = (char *)str + str_len;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
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;
|
|
|
|
}
|
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) {
|
|
|
|
case '+': attrs |= (1<<pos); break;
|
|
|
|
case '-': attrs &=~(1<<pos); break;
|
|
|
|
case '!': attrs ^= (1<<pos); break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-24 12:46:46 +00:00
|
|
|
|
|
|
|
area_t *
|
2014-02-26 10:07:49 +00:00
|
|
|
area_get (xcb_window_t win, const int x)
|
2014-02-24 12:46:46 +00:00
|
|
|
{
|
2014-02-26 10:07:49 +00:00
|
|
|
for (int i = 0; i < astack.pos; i++)
|
|
|
|
if (astack.slot[i].window == win && x > astack.slot[i].begin && x < astack.slot[i].end)
|
|
|
|
return &astack.slot[i];
|
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;
|
|
|
|
|
|
|
|
for (int i = 0; i < astack.pos; i++) {
|
|
|
|
if (astack.slot[i].window == win && astack.slot[i].align == align) {
|
|
|
|
astack.slot[i].begin -= delta;
|
|
|
|
astack.slot[i].end -= delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 02:55:34 +00:00
|
|
|
bool get_image_file(char *str, char *optend, char **end)
|
|
|
|
{
|
|
|
|
char *p = str;
|
|
|
|
|
|
|
|
/* Closing tag. */
|
|
|
|
if (*p != ':') {
|
|
|
|
*end = p;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *trail = strchr(++p, ':');
|
|
|
|
|
|
|
|
/* Find the trailing : and make sure it's whitin the formatting block, also reject empty files */
|
|
|
|
if (!trail || p == trail || trail > optend) {
|
|
|
|
*end = p;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*trail = '\0';
|
|
|
|
img_file = p;
|
|
|
|
*end = trail + 1;
|
|
|
|
|
2015-01-07 03:07:06 +00:00
|
|
|
/* Check if the image exists. */
|
|
|
|
if (access(img_file, F_OK) == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-07 02:55:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
char *p = str;
|
2014-02-26 10:07:49 +00:00
|
|
|
area_t *a = &astack.slot[astack.pos];
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
if (astack.pos == N) {
|
|
|
|
fprintf(stderr, "astack overflow!\n");
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
/* A wild close area tag appeared! */
|
|
|
|
if (*p != ':') {
|
2014-02-24 12:46:46 +00:00
|
|
|
*end = p;
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
/* Basic safety checks */
|
|
|
|
if (!a->cmd || a->align != align || a->window != mon->window)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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:
|
|
|
|
/* The newest is the rightmost one */
|
|
|
|
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
|
|
|
|
|
|
|
astack.pos++;
|
|
|
|
|
|
|
|
return true;
|
2014-02-24 12:46:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *trail = strchr(++p, ':');
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
/* Find the trailing : and make sure it's whitin the formatting block, also reject empty commands */
|
|
|
|
if (!trail || p == trail || trail > optend) {
|
2014-02-24 12:46:46 +00:00
|
|
|
*end = p;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*trail = '\0';
|
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
/* This is a pointer to the string buffer allocated in the main */
|
2014-02-24 12:46:46 +00:00
|
|
|
a->cmd = p;
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
void
|
|
|
|
parse (char *text)
|
|
|
|
{
|
2014-02-08 15:45:38 +00:00
|
|
|
monitor_t *cur_mon;
|
2014-06-11 18:05:23 +00:00
|
|
|
int pos_x, align, button;
|
2014-02-19 17:53:32 +00:00
|
|
|
char *p = text, *end;
|
|
|
|
uint32_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
|
|
|
|
2014-02-26 10:07:49 +00:00
|
|
|
memset(&astack, 0, sizeof(area_stack_t));
|
2014-02-24 12:46:46 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
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);
|
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2015-01-07 02:55:34 +00:00
|
|
|
bool char_is_image = false;
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
for (;;) {
|
2014-02-04 20:35:54 +00:00
|
|
|
if (*p == '\0' || *p == '\n')
|
2012-07-23 14:49:39 +00:00
|
|
|
return;
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (*p == '%' && p++ && *p == '{' && (end = strchr(p++, '}'))) {
|
|
|
|
while (p < end) {
|
|
|
|
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':
|
2014-07-23 19:15:40 +00:00
|
|
|
/*tmp = fgc;*/
|
|
|
|
/*fgc = bgc;*/
|
|
|
|
/*bgc = tmp;*/
|
2014-02-19 17:53:32 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-07 02:55:34 +00:00
|
|
|
case 'I':
|
|
|
|
char_is_image = get_image_file(p, end, &p);
|
|
|
|
break;
|
2014-02-24 12:46:46 +00:00
|
|
|
case 'A':
|
2014-06-11 18:05:23 +00:00
|
|
|
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);
|
2014-02-24 12:46:46 +00:00
|
|
|
break;
|
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
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;
|
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; }
|
|
|
|
|
2014-02-23 21:55:20 +00:00
|
|
|
p++;
|
2014-02-23 21:46:15 +00:00
|
|
|
pos_x = 0;
|
2014-02-19 17:53:32 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* In case of error keep parsing after the closing } */
|
|
|
|
default:
|
|
|
|
p = end;
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2014-02-19 17:53:32 +00:00
|
|
|
}
|
|
|
|
/* Eat the trailing } */
|
|
|
|
p++;
|
2014-07-23 19:15:40 +00:00
|
|
|
} 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';
|
|
|
|
|
2015-01-07 02:55:34 +00:00
|
|
|
int w = draw_char(cur_mon, pos_x, align, tmp, char_is_image);
|
2014-02-26 10:07:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
/* Leave those at the end since are batch-set */
|
|
|
|
"_NET_WM_STATE_STICKY",
|
|
|
|
"_NET_WM_STATE_ABOVE",
|
|
|
|
};
|
|
|
|
const int atoms = sizeof(atom_names)/sizeof(char *);
|
|
|
|
xcb_intern_atom_cookie_t atom_cookie[atoms];
|
|
|
|
xcb_atom_t atom_list[atoms];
|
|
|
|
xcb_intern_atom_reply_t *atom_reply;
|
|
|
|
|
|
|
|
/* As suggested fetch all the cookies first (yum!) and then retrieve the
|
|
|
|
* atoms to exploit the async'ness */
|
|
|
|
for (int i = 0; i < atoms; i++)
|
|
|
|
atom_cookie[i] = xcb_intern_atom(c, 0, strlen(atom_names[i]), atom_names[i]);
|
|
|
|
|
|
|
|
for (int i = 0; i < atoms; i++) {
|
|
|
|
atom_reply = xcb_intern_atom_reply(c, atom_cookie[i], NULL);
|
|
|
|
if (!atom_reply)
|
|
|
|
return;
|
|
|
|
atom_list[i] = atom_reply->atom;
|
|
|
|
free(atom_reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare the strut array */
|
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;
|
|
|
|
strut[9] = mon->x + mon->width;
|
2014-02-19 17:53:32 +00:00
|
|
|
} else {
|
|
|
|
strut[3] = bh;
|
|
|
|
strut[10] = mon->x;
|
|
|
|
strut[11] = mon->x + mon->width;
|
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]);
|
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ -1 } );
|
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
|
|
|
|
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
|
2012-07-22 10:44:19 +00:00
|
|
|
}
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
monitor_t *
|
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;
|
|
|
|
ret->width = width;
|
2014-02-09 23:54:33 +00:00
|
|
|
ret->next = ret->prev = NULL;
|
2012-07-22 10:44:19 +00:00
|
|
|
|
2014-04-24 21:46:00 +00:00
|
|
|
int win_y = (topbar ? by : height - bh - by) + y;
|
2014-02-09 23:54:33 +00:00
|
|
|
ret->window = xcb_generate_id(c);
|
2013-11-16 14:07:53 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
int depth = (vt->visual_id == scr->root_visual) ? XCB_COPY_FROM_PARENT : 32;
|
2014-02-24 12:35:56 +00:00
|
|
|
xcb_create_window(c, depth, ret->window, scr->root,
|
2014-02-19 17:53:32 +00:00
|
|
|
x, win_y, width, bh, 0,
|
2014-07-23 19:15:40 +00:00
|
|
|
XCB_WINDOW_CLASS_INPUT_OUTPUT, vt->visual_id,
|
2014-02-24 12:35:56 +00:00
|
|
|
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
|
2014-07-23 19:15:40 +00:00
|
|
|
(const uint32_t []){ scr->black_pixel, scr->black_pixel, dock, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS, colormap });
|
|
|
|
|
|
|
|
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);
|
2014-02-23 21:46:15 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_select_font_face (ret->cr, mfont? mfont: "fixed", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
|
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;
|
|
|
|
|
|
|
|
if (r1->x < r2->x || r1->y < r2->y)
|
|
|
|
return -1;
|
|
|
|
if (r1->x > r2->x || r1->y > r2->y)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Sort before use */
|
|
|
|
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;
|
|
|
|
/* 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)
|
2014-07-23 19:15:40 +00:00
|
|
|
bh = 18;
|
2014-05-29 01:00:58 +00:00
|
|
|
|
2014-06-01 01:56:50 +00:00
|
|
|
/* Check the geometry */
|
|
|
|
if (bx + bw > width || by + bh > height) {
|
|
|
|
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-02-21 11:52:50 +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(
|
|
|
|
rects[i].x + left,
|
|
|
|
rects[i].y,
|
|
|
|
min(width, rects[i].width - left),
|
|
|
|
rects[i].height);
|
|
|
|
|
|
|
|
monitor_add(mon);
|
|
|
|
|
|
|
|
width -= rects[i].width - left;
|
|
|
|
|
|
|
|
/* No need to check for other monitors */
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
|
|
|
/* 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];
|
|
|
|
|
2014-02-19 17:53:32 +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);
|
|
|
|
|
|
|
|
/* 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) {
|
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,
|
|
|
|
xcb_randr_get_crtc_info(c, oi_reply->crtc, XCB_CURRENT_TIME), NULL);
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-07-24 08:50:31 +00:00
|
|
|
/* 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 };
|
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);
|
|
|
|
|
2014-02-19 17:53:32 +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++) {
|
2014-02-18 00:23:35 +00:00
|
|
|
/* Does I countain 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 &&
|
|
|
|
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-07-23 19:15:40 +00:00
|
|
|
xcb_rectangle_t r[valid];
|
2014-06-01 02:01:14 +00:00
|
|
|
|
2014-07-23 19:15:40 +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
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
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
|
|
|
|
2014-02-17 14:42:43 +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
|
|
|
}
|
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
xcb_visualtype_t *
|
|
|
|
get_visual_type (void)
|
2014-02-24 12:35:56 +00:00
|
|
|
{
|
|
|
|
xcb_depth_iterator_t 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 == 32)
|
2014-07-23 19:15:40 +00:00
|
|
|
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;
|
2014-02-24 12:35:56 +00:00
|
|
|
|
|
|
|
xcb_depth_next(&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fallback to the default one */
|
2014-07-23 19:15:40 +00:00
|
|
|
return NULL;
|
2014-02-24 12:35:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
/* Connect to X */
|
|
|
|
c = xcb_connect (NULL, NULL);
|
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-07-23 19:15:40 +00:00
|
|
|
vt = get_visual_type();
|
2014-02-24 12:35:56 +00:00
|
|
|
|
|
|
|
colormap = xcb_generate_id(c);
|
2014-07-23 19:15:40 +00:00
|
|
|
xcb_create_colormap(c, XCB_COLORMAP_ALLOC_NONE, colormap, scr->root, vt->visual_id);
|
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
|
|
|
|
init (void)
|
|
|
|
{
|
2014-02-08 15:45:38 +00:00
|
|
|
/* To make the alignment uniform */
|
2014-07-23 19:15:40 +00:00
|
|
|
/*main_font->height = alt_font->height = max(main_font->height, alt_font->height);*/
|
2014-02-19 17:53:32 +00:00
|
|
|
|
2013-09-25 03:14:43 +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
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
/* Initialiaze monitor list head and tail */
|
|
|
|
monhead = montail = NULL;
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
/* Check if RandR is present */
|
2014-02-19 17:53:32 +00:00
|
|
|
qe_reply = xcb_get_extension_data(c, &xcb_randr_id);
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
if (qe_reply && qe_reply->present) {
|
2014-02-19 17:53:32 +00:00
|
|
|
get_randr_monitors();
|
2014-02-09 23:54:33 +00:00
|
|
|
} 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
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
/* Check if Xinerama extension is present and active */
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-05-29 01:00:58 +00:00
|
|
|
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)
|
2014-07-23 19:15:40 +00:00
|
|
|
bh = 18;
|
2014-05-29 01:00:58 +00:00
|
|
|
|
2014-06-01 13:21:06 +00:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2014-02-11 02:42:14 +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
|
|
|
|
2013-09-02 13:35:32 +00:00
|
|
|
/* For WM that support EWMH atoms */
|
|
|
|
set_ewmh_atoms();
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-24 12:35:56 +00:00
|
|
|
/* Make the bar visible and clear the pixmap */
|
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next) {
|
2014-07-23 19:15:40 +00:00
|
|
|
fill_rect(mon->cr, PAL_BG, 0, 0, mon->width, bh);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_map_window(c, mon->window);
|
2014-02-24 12:35:56 +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
|
|
|
}
|
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
void
|
|
|
|
cleanup (void)
|
|
|
|
{
|
2014-02-09 23:54:33 +00:00
|
|
|
while (monhead) {
|
|
|
|
monitor_t *next = monhead->next;
|
2014-07-23 19:15:40 +00:00
|
|
|
cairo_destroy(monhead->cr);
|
|
|
|
cairo_surface_destroy(monhead->surface);
|
2014-02-23 21:46:15 +00:00
|
|
|
xcb_destroy_window(c, monhead->window);
|
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-02-24 12:35:56 +00:00
|
|
|
xcb_free_colormap(c, colormap);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-21 11:52:50 +00:00
|
|
|
/* Parse an X-styled geometry string, we don't support signed offsets tho. */
|
|
|
|
bool
|
|
|
|
parse_geometry_string (char *str, int *tmp)
|
2014-02-08 15:45:38 +00:00
|
|
|
{
|
2014-02-21 11:52:50 +00:00
|
|
|
char *p = str;
|
|
|
|
int i = 0, j;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
if (!str || !str[0])
|
|
|
|
return false;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
/* The leading = is optional */
|
|
|
|
if (*p == '=')
|
|
|
|
p++;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
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;
|
|
|
|
}
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
return true;
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parse_font_list (char *str)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tok = strtok(str, ",");
|
2014-02-19 17:53:32 +00:00
|
|
|
if (tok)
|
|
|
|
mfont = tok;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
|
|
|
return;
|
2012-07-23 14:49:39 +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 };
|
2014-02-19 17:53:32 +00:00
|
|
|
|
|
|
|
/* Install the parachute! */
|
|
|
|
atexit(cleanup);
|
|
|
|
signal(SIGINT, sighandle);
|
|
|
|
signal(SIGTERM, sighandle);
|
|
|
|
|
|
|
|
/* Connect to the Xserver and initialize scr */
|
|
|
|
xconn();
|
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
bgc = scr->black_pixel;
|
|
|
|
fgc = scr->white_pixel;
|
2012-07-17 09:34:12 +00:00
|
|
|
|
|
|
|
char ch;
|
2014-02-21 11:52:50 +00:00
|
|
|
while ((ch = getopt(argc, argv, "hg:bdf:a:pu:B:F:")) != -1) {
|
2012-07-16 10:30:49 +00:00
|
|
|
switch (ch) {
|
2014-02-19 17:53:32 +00:00
|
|
|
case 'h':
|
2014-02-21 11:52:50 +00:00
|
|
|
printf ("usage: %s [-h | -g | -b | -d | -f | -a | -p | -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"
|
2012-07-24 10:08:02 +00:00
|
|
|
"\t-b Put bar at the bottom of the screen\n"
|
2014-02-08 15:45:38 +00:00
|
|
|
"\t-d Force docking (use this if your WM isn't EWMH compliant)\n"
|
|
|
|
"\t-f Bar font list, comma separated\n"
|
2014-02-19 17:53:32 +00:00
|
|
|
"\t-p Don't close after the data ends\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"
|
|
|
|
"\t-F Set foreground color in #AARRGGBB\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;
|
|
|
|
case 'b': topbar = false; break;
|
|
|
|
case 'd': dock = true; break;
|
2014-02-08 15:45:38 +00:00
|
|
|
case 'f': parse_font_list(optarg); break;
|
2014-02-21 11:52:50 +00:00
|
|
|
case 'u': bu = strtoul(optarg, NULL, 10); break;
|
2014-07-23 19:15:40 +00:00
|
|
|
case 'B': bgc = parse_color(optarg, NULL, scr->black_pixel); break;
|
|
|
|
case 'F': fgc = parse_color(optarg, NULL, scr->white_pixel); break;
|
2012-07-16 10:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-07-23 19:15:40 +00:00
|
|
|
convert_color(bgc, &palette[PAL_BG]);
|
|
|
|
convert_color(fgc, &palette[PAL_FG]);
|
|
|
|
convert_color(fgc, &palette[PAL_ATTR]);
|
|
|
|
|
2014-02-21 11:52:50 +00:00
|
|
|
/* Copy the geometry values in place */
|
|
|
|
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
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
/* Do the heavy lifting */
|
|
|
|
init();
|
2012-07-17 14:56:03 +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-17 09:34:12 +00:00
|
|
|
|
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
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
if (poll(pollin, 2, -1) > 0) {
|
|
|
|
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
|
|
|
}
|
2012-07-17 14:56:03 +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)
|
2014-02-04 20:28:01 +00:00
|
|
|
break; /* EOF received */
|
|
|
|
|
2014-02-19 17:53:32 +00:00
|
|
|
parse(input);
|
|
|
|
redraw = true;
|
2012-07-17 14:56:03 +00:00
|
|
|
}
|
|
|
|
if (pollin[1].revents & POLLIN) { /* Xserver broadcasted an event */
|
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:
|
2014-02-26 12:02:33 +00:00
|
|
|
if (expose_ev->count == 0)
|
|
|
|
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
|
|
|
{
|
2014-03-13 23:33:52 +00:00
|
|
|
area_t *area = area_get(press_ev->event, press_ev->event_x);
|
2014-06-11 18:05:23 +00:00
|
|
|
/* Respond to the click */
|
|
|
|
if (area && area->button == press_ev->detail) {
|
|
|
|
write(STDOUT_FILENO, area->cmd, strlen(area->cmd));
|
|
|
|
write(STDOUT_FILENO, "\n", 1);
|
|
|
|
}
|
2014-02-24 12:46:46 +00:00
|
|
|
}
|
2014-02-26 12:02:33 +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
|
|
|
|
2014-01-24 11:30:20 +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-07-23 19:15:40 +00:00
|
|
|
cairo_surface_flush(mon->surface);
|
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
|
|
|
}
|