2012-07-24 10:08:02 +00:00
|
|
|
#include <stdbool.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <signal.h>
|
2012-07-16 23:17:11 +00:00
|
|
|
#include <poll.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
#include <xcb/xcb.h>
|
2013-09-25 03:14:43 +00:00
|
|
|
#include <xcb/xinerama.h>
|
2014-02-12 04:29:40 +00:00
|
|
|
#include <xcb/randr.h>
|
2012-07-16 09:41:34 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
// Here be dragons
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
#define MAX(a,b) ((a) > (b) ? (a) : (b))
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
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;
|
|
|
|
uint32_t descent;
|
|
|
|
uint32_t height;
|
|
|
|
uint16_t char_max;
|
|
|
|
uint16_t char_min;
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_charinfo_t *width_lut;
|
|
|
|
} font_t;
|
|
|
|
|
|
|
|
typedef struct monitor_t {
|
2014-02-12 15:01:42 +00:00
|
|
|
uint32_t x;
|
|
|
|
uint32_t width;
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_window_t window;
|
|
|
|
struct monitor_t *prev, *next;
|
|
|
|
} monitor_t;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
typedef struct cmd_area_t {
|
|
|
|
struct cmd_area_t *next;
|
|
|
|
struct cmd_area_t *prev;
|
|
|
|
char *cmd;
|
|
|
|
int begin;
|
|
|
|
int end;
|
|
|
|
int begin_x;
|
|
|
|
int end_x;
|
|
|
|
int align;
|
|
|
|
} cmd_area_t;
|
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static struct config_t {
|
|
|
|
int place_bottom;
|
2014-02-08 15:45:38 +00:00
|
|
|
int force_docking;
|
|
|
|
int permanent;
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
float alpha;
|
|
|
|
char main_font[64];
|
|
|
|
char alt_font[64];
|
|
|
|
} cfg = {
|
|
|
|
0, 0, 0, -1, 18, 1.0f, "fixed", "fixed",
|
2012-07-23 14:49:39 +00:00
|
|
|
};
|
|
|
|
|
2012-07-24 10:25:45 +00:00
|
|
|
enum {
|
|
|
|
ALIGN_L = 0,
|
|
|
|
ALIGN_C,
|
|
|
|
ALIGN_R
|
|
|
|
};
|
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static xcb_connection_t *c;
|
|
|
|
static xcb_screen_t *scr;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static xcb_drawable_t canvas;
|
|
|
|
static xcb_gcontext_t draw_gc, clear_gc, underl_gc;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static monitor_t *monhead, *montail;
|
|
|
|
static font_t *main_font, *alt_font;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
static const uint32_t palette[] = {
|
2014-02-08 15:45:38 +00:00
|
|
|
COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9,BACKGROUND,FOREGROUND
|
|
|
|
};
|
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
static cmd_area_t *cmd_area_list_head;
|
|
|
|
static cmd_area_t *cmd_area_list_tail;
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2014-02-09 23:54:33 +00:00
|
|
|
set_bg (int i)
|
2012-07-17 09:34:12 +00:00
|
|
|
{
|
2012-07-23 14:49:39 +00:00
|
|
|
xcb_change_gc (c, draw_gc , XCB_GC_BACKGROUND, (const unsigned []){ palette[i] });
|
|
|
|
xcb_change_gc (c, clear_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] });
|
2012-07-17 09:34:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2014-02-09 23:54:33 +00:00
|
|
|
set_fg (int i)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2012-07-24 10:25:45 +00:00
|
|
|
xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
void
|
2014-02-09 23:54:33 +00:00
|
|
|
set_ud (int i)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
2012-07-24 10:25:45 +00:00
|
|
|
xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
|
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-02-09 23:54:33 +00:00
|
|
|
fill_rect (xcb_gcontext_t gc, int x, int y, int width, int height)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
|
|
|
xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
int
|
2014-02-08 15:45:38 +00:00
|
|
|
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, int underline, uint16_t ch)
|
2012-07-23 14:49:39 +00:00
|
|
|
{
|
2014-02-12 15:01:42 +00:00
|
|
|
/* In the unlikely case that the font doesn't have the glyph wanted just do nothing */
|
|
|
|
if (ch < cur_font->char_min || ch > cur_font->char_max)
|
|
|
|
return 0;
|
2012-07-25 14:55:26 +00:00
|
|
|
|
2014-02-12 15:01:42 +00:00
|
|
|
int ch_width = cur_font->width_lut[ch - cur_font->char_min].character_width;
|
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-02-08 15:45:38 +00:00
|
|
|
xcb_copy_area (c, canvas, canvas, draw_gc, mon->width / 2 - x / 2 + mon->x, 0,
|
|
|
|
mon->width / 2 - (x + ch_width) / 2 + mon->x, 0, x, cfg.height);
|
|
|
|
x = mon->width / 2 - (x + ch_width) / 2 + x;
|
2012-07-16 09:41:34 +00:00
|
|
|
break;
|
2012-07-24 10:25:45 +00:00
|
|
|
case ALIGN_R:
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_copy_area (c, canvas, canvas, draw_gc, mon->width - x + mon->x, 0,
|
|
|
|
mon->width - x - ch_width + mon->x, 0, x, cfg.height);
|
|
|
|
x = mon->width - ch_width;
|
2012-07-16 09:41:34 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2012-07-16 20:56:19 +00:00
|
|
|
/* Draw the background first */
|
2014-02-09 23:54:33 +00:00
|
|
|
fill_rect (clear_gc, x + mon->x, 0, ch_width, cfg.height);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
/* xcb accepts string in UCS-2 BE, so swap */
|
|
|
|
ch = (ch >> 8) | (ch << 8);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
/* String baseline coordinates */
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_image_text_16 (c, 1, canvas, draw_gc, x + mon->x, cfg.height / 2 + cur_font->height / 2 - cur_font->descent,
|
2012-07-23 14:49:39 +00:00
|
|
|
(xcb_char2b_t *)&ch);
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
/* Draw the underline if -1, an overline if 1 */
|
|
|
|
if (BAR_UNDERLINE_HEIGHT && underline)
|
2014-02-09 23:54:33 +00:00
|
|
|
fill_rect (underl_gc, x + mon->x, (underline < 0)*(cfg.height-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT);
|
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
|
|
|
}
|
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
void
|
|
|
|
xcb_handle_mouse_event (int16_t x)
|
|
|
|
{
|
|
|
|
cmd_area_t *area = cmd_area_list_head;
|
|
|
|
while (area) {
|
|
|
|
if (area->begin <=x && area->end >= x) {
|
|
|
|
system (area->cmd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
area = area->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_area_begin (monitor_t *mon, int x, int align)
|
|
|
|
{
|
|
|
|
cmd_area_t *area = calloc (1, sizeof (*area));
|
|
|
|
|
|
|
|
area->align = align;
|
|
|
|
area->begin_x = x;
|
|
|
|
|
|
|
|
switch (align) {
|
|
|
|
case ALIGN_L:
|
|
|
|
area->begin = x + mon->x;
|
|
|
|
break;
|
|
|
|
case ALIGN_C:
|
|
|
|
area->begin = mon->width / 2 + x / 2 + mon->x;
|
|
|
|
break;
|
|
|
|
case ALIGN_R:
|
|
|
|
area->begin = mon->width + mon->x;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmd_area_list_head) {
|
|
|
|
cmd_area_list_head = area;
|
|
|
|
cmd_area_list_tail = area;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cmd_area_list_tail->next = area;
|
|
|
|
area->prev = cmd_area_list_tail;
|
|
|
|
cmd_area_list_tail = area;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_area_end (monitor_t *mon, int x, int align)
|
|
|
|
{
|
|
|
|
cmd_area_t *area = cmd_area_list_tail;
|
|
|
|
area->end_x = x;
|
|
|
|
|
|
|
|
switch (align) {
|
|
|
|
case ALIGN_L:
|
|
|
|
area->end = x + mon->x;
|
|
|
|
break;
|
|
|
|
case ALIGN_C:
|
|
|
|
area->begin -= (x - area->begin_x) / 2;
|
|
|
|
area->end = mon->width / 2 + x / 2 + mon->x;
|
|
|
|
/*
|
|
|
|
* if there were any other center aligned areas
|
|
|
|
* before this one, adjust their position
|
|
|
|
*/
|
|
|
|
cmd_area_t *a = area->prev;
|
|
|
|
if (a && a->align == ALIGN_C) {
|
|
|
|
int diff = (area->begin_x - a->end_x + area->end - area->begin) / 2;
|
|
|
|
while (a && a->align == ALIGN_C) {
|
|
|
|
a->begin -= diff;
|
|
|
|
a->end -= diff;
|
|
|
|
a = a->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALIGN_R:
|
|
|
|
area->begin -= (x - area->begin_x);
|
|
|
|
area->end = mon->width + mon->x;
|
|
|
|
/*
|
|
|
|
* if there were any other right aligned areas
|
|
|
|
* before this one, adjust their position
|
|
|
|
*/
|
|
|
|
a = area->prev;
|
|
|
|
if (a && a->align == ALIGN_R) {
|
|
|
|
int diff = area->begin_x - a->end_x + area->end - area->begin;
|
|
|
|
while (a && a->align == ALIGN_R) {
|
|
|
|
a->begin -= diff;
|
|
|
|
a->end -= diff;
|
|
|
|
a = a->prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
void
|
|
|
|
parse (char *text)
|
|
|
|
{
|
2013-02-16 10:57:51 +00:00
|
|
|
char *p = text;
|
2014-02-14 11:51:07 +00:00
|
|
|
char *cmd_start = 0;
|
2012-07-16 09:41:34 +00:00
|
|
|
|
|
|
|
int pos_x = 0;
|
|
|
|
int align = 0;
|
2014-02-08 15:45:38 +00:00
|
|
|
int underline_flag = 0;
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
font_t *cur_font;
|
|
|
|
monitor_t *cur_mon;
|
|
|
|
|
|
|
|
cur_font = main_font;
|
2014-02-09 23:54:33 +00:00
|
|
|
cur_mon = monhead;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
|
2012-07-21 15:41:57 +00:00
|
|
|
|
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-14 11:51:07 +00:00
|
|
|
if (*p == '^' && p++ && *p != '^' && strchr ("_-afbulcrs", *p)) {
|
2012-07-23 14:49:39 +00:00
|
|
|
switch (*p++) {
|
|
|
|
case 'f':
|
2014-02-09 23:54:33 +00:00
|
|
|
set_fg (isdigit(*p) ? *p-'0' : 11);
|
2013-08-31 16:54:02 +00:00
|
|
|
p++;
|
2012-07-23 14:49:39 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
2014-02-09 23:54:33 +00:00
|
|
|
set_bg (isdigit(*p) ? *p-'0' : 10);
|
2013-08-31 16:54:02 +00:00
|
|
|
p++;
|
2012-07-23 14:49:39 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2014-02-09 23:54:33 +00:00
|
|
|
set_ud (isdigit(*p) ? *p-'0' : 10);
|
2013-08-31 16:54:02 +00:00
|
|
|
p++;
|
2012-07-23 14:49:39 +00:00
|
|
|
break;
|
2014-02-08 15:45:38 +00:00
|
|
|
|
|
|
|
case '_':
|
|
|
|
underline_flag = (underline_flag == -1) ? 0 : -1;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
underline_flag = (underline_flag == 1) ? 0 : 1;
|
|
|
|
break;
|
2013-09-25 03:14:43 +00:00
|
|
|
case 's':
|
2014-02-09 23:54:33 +00:00
|
|
|
/* montail only gets set with multiple monitors */
|
|
|
|
if (montail) {
|
|
|
|
if (*p == 'r') {
|
|
|
|
cur_mon = montail;
|
|
|
|
} else if (*p == 'l') {
|
|
|
|
cur_mon = monhead;
|
|
|
|
} else if (*p == 'n') {
|
|
|
|
if (cur_mon->next)
|
|
|
|
cur_mon = cur_mon->next;
|
|
|
|
} else if (*p == 'p') {
|
|
|
|
if (cur_mon->prev)
|
|
|
|
cur_mon = cur_mon->prev;
|
|
|
|
} else if (isdigit(*p)) {
|
|
|
|
cur_mon = monhead;
|
|
|
|
/* Start at 0 */
|
|
|
|
for (int i = 0; i != *p-'0' && cur_mon->next; i++)
|
|
|
|
cur_mon = cur_mon->next;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
p++;
|
2013-09-25 03:14:43 +00:00
|
|
|
break;
|
2014-02-09 23:54:33 +00:00
|
|
|
}
|
2014-01-24 11:30:20 +00:00
|
|
|
|
|
|
|
/* Consume the argument */
|
|
|
|
p++;
|
|
|
|
|
2013-09-25 03:14:43 +00:00
|
|
|
align = ALIGN_L;
|
|
|
|
pos_x = 0;
|
|
|
|
break;
|
2012-07-23 14:49:39 +00:00
|
|
|
case 'l':
|
2012-07-24 10:25:45 +00:00
|
|
|
align = ALIGN_L;
|
2012-07-23 14:49:39 +00:00
|
|
|
pos_x = 0;
|
|
|
|
break;
|
|
|
|
case 'c':
|
2012-07-24 10:25:45 +00:00
|
|
|
align = ALIGN_C;
|
2012-07-23 14:49:39 +00:00
|
|
|
pos_x = 0;
|
|
|
|
break;
|
|
|
|
case 'r':
|
2012-07-24 10:25:45 +00:00
|
|
|
align = ALIGN_R;
|
2012-07-23 14:49:39 +00:00
|
|
|
pos_x = 0;
|
|
|
|
break;
|
2014-02-14 11:51:07 +00:00
|
|
|
case 'a':
|
|
|
|
switch (*p) {
|
|
|
|
case 'b':
|
|
|
|
cmd_area_begin (cur_mon, pos_x, align);
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cmd_start = ++p;
|
|
|
|
while (*p != '\0' && *p != '\n' && *p != '^')
|
|
|
|
p++;
|
|
|
|
continue;
|
|
|
|
case 'e':
|
|
|
|
cmd_area_end (cur_mon, pos_x, align);
|
|
|
|
size_t cmd_len = (size_t)(p - cmd_start) - 2;
|
|
|
|
cmd_area_list_tail->cmd = calloc (cmd_len + 1, sizeof(char));
|
|
|
|
strncpy (cmd_area_list_tail->cmd, cmd_start, cmd_len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
break;
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
2012-07-18 15:11:17 +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
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
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;
|
|
|
|
}
|
2014-01-26 13:13:49 +00:00
|
|
|
else { /* Handle ascii > 0x80 */
|
|
|
|
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-02-08 15:45:38 +00:00
|
|
|
/* If the character is outside the main font charset use the alternate font */
|
|
|
|
cur_font = (ucs < main_font->char_min || ucs > main_font->char_max) ? alt_font : main_font;
|
|
|
|
|
|
|
|
xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ cur_font->ptr });
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
pos_x += draw_char (cur_mon, cur_font, pos_x, align, underline_flag, ucs);
|
2012-07-18 15:11:17 +00:00
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
font_t *
|
|
|
|
font_load (char *font_string)
|
2012-07-16 09:41:34 +00:00
|
|
|
{
|
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-08 15:45:38 +00:00
|
|
|
font = xcb_generate_id (c);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
cookie = xcb_open_font_checked (c, font, strlen (font_string), font_string);
|
|
|
|
if (xcb_request_check (c, cookie)) {
|
|
|
|
fprintf (stderr, "Could not load font %s\n", font_string);
|
|
|
|
return NULL;
|
|
|
|
}
|
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)
|
|
|
|
return NULL;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
queryreq = xcb_query_font (c, font);
|
|
|
|
font_info = xcb_query_font_reply (c, queryreq, NULL);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
ret->ptr = font;
|
|
|
|
ret->width_lut = xcb_query_font_char_infos (font_info);
|
|
|
|
ret->descent = font_info->font_descent;
|
|
|
|
ret->char_max = font_info->max_byte1 << 8 | font_info->max_char_or_byte2;
|
|
|
|
ret->char_min = font_info->min_byte1 << 8 | font_info->min_char_or_byte2;
|
|
|
|
ret->height = font_info->font_ascent + font_info->font_descent;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
return ret;
|
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_WINDOW_OPACITY,
|
|
|
|
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",
|
|
|
|
"_NET_WM_WINDOW_OPACITY",
|
|
|
|
/* Leave those at the end since are batch-set */
|
|
|
|
"_NET_WM_STATE_STICKY",
|
|
|
|
"_NET_WM_STATE_ABOVE",
|
|
|
|
};
|
|
|
|
const int atoms = sizeof(atom_names)/sizeof(char *);
|
|
|
|
xcb_intern_atom_cookie_t atom_cookie[atoms];
|
|
|
|
xcb_atom_t atom_list[atoms];
|
|
|
|
xcb_intern_atom_reply_t *atom_reply;
|
|
|
|
|
|
|
|
/* As suggested fetch all the cookies first (yum!) and then retrieve the
|
|
|
|
* atoms to exploit the async'ness */
|
|
|
|
for (int i = 0; i < atoms; i++)
|
|
|
|
atom_cookie[i] = xcb_intern_atom(c, 0, strlen(atom_names[i]), atom_names[i]);
|
|
|
|
|
|
|
|
for (int i = 0; i < atoms; i++) {
|
|
|
|
atom_reply = xcb_intern_atom_reply(c, atom_cookie[i], NULL);
|
|
|
|
if (!atom_reply)
|
|
|
|
return;
|
|
|
|
atom_list[i] = atom_reply->atom;
|
|
|
|
free(atom_reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare the strut array */
|
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-08 15:45:38 +00:00
|
|
|
if (cfg.place_bottom) {
|
|
|
|
strut[3] = cfg.height;
|
|
|
|
strut[10] = mon->x;
|
|
|
|
strut[11] = mon->x + mon->width;
|
2013-11-16 14:07:53 +00:00
|
|
|
} else {
|
2014-02-08 15:45:38 +00:00
|
|
|
strut[2] = cfg.height;
|
|
|
|
strut[8] = mon->x;
|
|
|
|
strut[9] = mon->x + mon->width;
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_WINDOW_OPACITY], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ (uint32_t)(cfg.alpha * 0xffffffff) } );
|
|
|
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_WINDOW_TYPE], XCB_ATOM_ATOM, 32, 1, &atom_list[NET_WM_WINDOW_TYPE_DOCK]);
|
|
|
|
xcb_change_property (c, XCB_PROP_MODE_APPEND, mon->window, atom_list[NET_WM_STATE], XCB_ATOM_ATOM, 32, 2, &atom_list[NET_WM_STATE_STICKY]);
|
|
|
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ -1 } );
|
|
|
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
|
|
|
|
xcb_change_property (c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
|
2012-07-22 10:44:19 +00:00
|
|
|
}
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
monitor_t *
|
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-09 23:54:33 +00:00
|
|
|
ret = malloc(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-02-09 23:54:33 +00:00
|
|
|
int win_y = (cfg.place_bottom ? (height - cfg.height) : 0) + y;
|
|
|
|
ret->window = xcb_generate_id(c);
|
2013-11-16 14:07:53 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_create_window(c, XCB_COPY_FROM_PARENT, ret->window, scr->root,
|
|
|
|
x, win_y, width, cfg.height, 0,
|
|
|
|
XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual,
|
|
|
|
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK,
|
2014-02-14 11:51:07 +00:00
|
|
|
(const uint32_t []){ palette[10], XCB_EVENT_MASK_EXPOSURE
|
|
|
|
| XCB_EVENT_MASK_BUTTON_RELEASE });
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_change_window_attributes (c, ret->window, XCB_CW_OVERRIDE_REDIRECT,
|
|
|
|
(const uint32_t []){ cfg.force_docking });
|
2014-02-08 15:45:38 +00:00
|
|
|
|
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-09 23:54:33 +00:00
|
|
|
void
|
|
|
|
get_randr_outputs(void)
|
|
|
|
{
|
|
|
|
int i, j, num, cnt = 0;
|
|
|
|
xcb_generic_error_t *err;
|
|
|
|
xcb_randr_get_screen_resources_current_cookie_t rres_query;
|
|
|
|
xcb_randr_get_screen_resources_current_reply_t *rres_reply;
|
|
|
|
xcb_randr_output_t *outputs;
|
|
|
|
xcb_timestamp_t config_timestamp;
|
|
|
|
|
|
|
|
rres_query = xcb_randr_get_screen_resources_current(c, scr->root);
|
|
|
|
rres_reply = xcb_randr_get_screen_resources_current_reply(c, rres_query, &err);
|
|
|
|
if (rres_reply == NULL || err != NULL) {
|
|
|
|
fprintf(stderr, "Failed to get current randr screen resources\n");
|
|
|
|
free(rres_reply);
|
|
|
|
return;
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
config_timestamp = rres_reply->config_timestamp;
|
|
|
|
free(rres_reply);
|
|
|
|
if (num < 1) {
|
|
|
|
fprintf(stderr, "Failed to get current randr outputs\n");
|
|
|
|
return;
|
|
|
|
}
|
2014-02-11 02:42:14 +00:00
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_rectangle_t rects[num];
|
|
|
|
|
|
|
|
/* get all outputs */
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
xcb_randr_get_output_info_cookie_t output_query;
|
|
|
|
xcb_randr_get_output_info_reply_t *output_reply;
|
|
|
|
xcb_randr_get_crtc_info_cookie_t crtc_query;
|
|
|
|
xcb_randr_get_crtc_info_reply_t *crtc_reply;
|
|
|
|
|
|
|
|
output_query = xcb_randr_get_output_info(c, outputs[i], config_timestamp);
|
|
|
|
output_reply = xcb_randr_get_output_info_reply(c, output_query, &err);
|
|
|
|
if (err != NULL || output_reply == NULL || output_reply->crtc == XCB_NONE) {
|
|
|
|
rects[i].width = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
crtc_query = xcb_randr_get_crtc_info(c, output_reply->crtc, config_timestamp);
|
|
|
|
crtc_reply = xcb_randr_get_crtc_info_reply(c, crtc_query, &err);
|
|
|
|
if (err != NULL | crtc_reply == NULL) {
|
|
|
|
fprintf(stderr, "Failed to get randr ctrc info\n");
|
|
|
|
rects[i].width = 0;
|
|
|
|
free(output_reply);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rects[i].x = crtc_reply->x;
|
|
|
|
rects[i].y = crtc_reply->y;
|
|
|
|
rects[i].width = crtc_reply->width;
|
|
|
|
rects[i].height = crtc_reply->height;
|
|
|
|
free(crtc_reply);
|
|
|
|
free(output_reply);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt < 1) {
|
|
|
|
fprintf(stderr, "No usable randr outputs\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for clones and inactive outputs */
|
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
if (rects[i].width == 0)
|
|
|
|
continue;
|
|
|
|
for (j = 0; j < num; j++) {
|
|
|
|
if (i == j || rects[j].width == 0 || rects[i].x != rects[j].x || rects[i].y != rects[j].y)
|
|
|
|
continue;
|
|
|
|
/* clone found, only keep one */
|
|
|
|
rects[i].width = (rects[i].width < rects[j].width) ? rects[i].width : rects[j].width;
|
|
|
|
rects[i].height = (rects[i].height < rects[j].height) ? rects[i].height : rects[j].height;
|
|
|
|
rects[j].width = 0;
|
|
|
|
cnt--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt < 1) {
|
|
|
|
fprintf(stderr, "No usable randr outputs\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width = cfg.width;
|
|
|
|
|
|
|
|
for (i = j = 0; i < num && j < cnt; i++) {
|
|
|
|
if (rects[i].width) {
|
|
|
|
monitor_t *mon = monitor_new (
|
|
|
|
rects[i].x,
|
|
|
|
rects[i].y,
|
|
|
|
MIN(width, rects[i].width),
|
|
|
|
rects[i].height);
|
|
|
|
|
2014-02-11 02:42:14 +00:00
|
|
|
monitor_add (mon);
|
2014-02-09 23:54:33 +00:00
|
|
|
|
|
|
|
width -= rects[i].width;
|
|
|
|
|
|
|
|
/* No need to check for other monitors */
|
|
|
|
if (width <= 0)
|
|
|
|
break;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 00:44:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-11 02:42:14 +00:00
|
|
|
void
|
|
|
|
get_xinerama_screens (void)
|
|
|
|
{
|
|
|
|
xcb_xinerama_query_screens_reply_t *xqs_reply;
|
|
|
|
xcb_xinerama_screen_info_iterator_t iter;
|
|
|
|
int width = cfg.width;
|
|
|
|
|
|
|
|
xqs_reply = xcb_xinerama_query_screens_reply (c,
|
|
|
|
xcb_xinerama_query_screens_unchecked (c), NULL);
|
|
|
|
|
|
|
|
iter = xcb_xinerama_query_screens_screen_info_iterator (xqs_reply);
|
|
|
|
|
|
|
|
/* The width is consumed across all the screens */
|
|
|
|
|
|
|
|
while (iter.rem) {
|
|
|
|
monitor_t *mon = monitor_new (
|
|
|
|
iter.data->x_org,
|
|
|
|
iter.data->y_org,
|
|
|
|
MIN(width, iter.data->width),
|
|
|
|
iter.data->height);
|
|
|
|
|
|
|
|
monitor_add (mon);
|
|
|
|
|
|
|
|
width -= iter.data->width;
|
|
|
|
|
|
|
|
/* No need to check for other monitors */
|
|
|
|
if (width <= 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
xcb_xinerama_screen_info_next (&iter);
|
|
|
|
}
|
|
|
|
|
|
|
|
free (xqs_reply);
|
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
void
|
|
|
|
init (void)
|
|
|
|
{
|
|
|
|
/* Connect to X */
|
|
|
|
c = xcb_connect (NULL, NULL);
|
|
|
|
if (xcb_connection_has_error (c)) {
|
|
|
|
fprintf (stderr, "Couldn't connect to X\n");
|
2014-02-08 15:45:38 +00:00
|
|
|
exit (EXIT_FAILURE);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
/* Grab infos from the first screen */
|
2012-07-20 09:53:29 +00:00
|
|
|
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
/* If I fits I sits */
|
2014-02-09 23:54:33 +00:00
|
|
|
if (cfg.width < 0 || cfg.width > scr->width_in_pixels)
|
2014-02-08 15:45:38 +00:00
|
|
|
cfg.width = scr->width_in_pixels;
|
2012-08-17 00:29:18 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
/* Load the fonts */
|
|
|
|
main_font = font_load((char *)cfg.main_font);
|
|
|
|
if (!main_font)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
alt_font = font_load((char *)cfg.alt_font);
|
|
|
|
if (!alt_font)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
|
|
|
/* To make the alignment uniform */
|
|
|
|
main_font->height = alt_font->height = MAX(main_font->height, alt_font->height);
|
2012-07-23 14:49:39 +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;
|
|
|
|
int width = cfg.width;
|
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 */
|
|
|
|
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-11 02:42:14 +00:00
|
|
|
get_randr_outputs ();
|
2014-02-09 23:54:33 +00:00
|
|
|
} else {
|
|
|
|
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;
|
|
|
|
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-11 02:42:14 +00:00
|
|
|
get_xinerama_screens ();
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-11 02:42:14 +00:00
|
|
|
free (xia_reply);
|
2014-02-08 15:45:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-09 23:54:33 +00:00
|
|
|
|
2014-02-11 02:42:14 +00:00
|
|
|
if (!monhead)
|
|
|
|
/* If no RandR outputs or Xinerama screens, fall back to using whole screen */
|
2014-02-09 23:54:33 +00:00
|
|
|
monhead = monitor_new (0, 0, width, scr->height_in_pixels);
|
2013-09-25 03:14:43 +00:00
|
|
|
|
2014-02-12 04:29:40 +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
|
|
|
|
2012-07-16 20:56:19 +00:00
|
|
|
/* Create a temporary canvas */
|
|
|
|
canvas = xcb_generate_id (c);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_create_pixmap (c, scr->root_depth, canvas, scr->root, cfg.width, cfg.height);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2012-07-16 09:41:34 +00:00
|
|
|
/* Create the gc for drawing */
|
2012-07-23 14:49:39 +00:00
|
|
|
draw_gc = xcb_generate_id (c);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_create_gc (c, draw_gc, scr->root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t []){ palette[11], palette[10] });
|
2012-07-23 14:49:39 +00:00
|
|
|
|
|
|
|
clear_gc = xcb_generate_id (c);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_create_gc (c, clear_gc, scr->root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
|
2012-07-23 14:49:39 +00:00
|
|
|
|
|
|
|
underl_gc = xcb_generate_id (c);
|
2014-02-09 23:54:33 +00:00
|
|
|
xcb_create_gc (c, underl_gc, scr->root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
|
|
|
|
|
|
|
|
/* Make the bar visible */
|
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next)
|
|
|
|
xcb_map_window(c, mon->window);
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2012-07-18 15:11:17 +00:00
|
|
|
xcb_flush (c);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
static void
|
|
|
|
clear_cmd_area_list (void)
|
|
|
|
{
|
|
|
|
while (cmd_area_list_head) {
|
|
|
|
cmd_area_t *area = cmd_area_list_head;
|
|
|
|
cmd_area_list_head = cmd_area_list_head->next;
|
|
|
|
free (area->cmd);
|
|
|
|
free (area);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-23 14:49:39 +00:00
|
|
|
void
|
|
|
|
cleanup (void)
|
|
|
|
{
|
2014-02-08 15:45:38 +00:00
|
|
|
if (main_font) {
|
|
|
|
xcb_close_font (c, main_font->ptr);
|
|
|
|
free(main_font);
|
2012-07-25 14:55:26 +00:00
|
|
|
}
|
2014-02-08 15:45:38 +00:00
|
|
|
|
|
|
|
if (alt_font) {
|
|
|
|
xcb_close_font (c, alt_font->ptr);
|
|
|
|
free(alt_font);
|
|
|
|
}
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
while (monhead) {
|
|
|
|
monitor_t *next = monhead->next;
|
|
|
|
free(monhead);
|
|
|
|
monhead = next;
|
2013-11-16 14:07:53 +00:00
|
|
|
}
|
2014-02-08 15:45:38 +00:00
|
|
|
|
2012-07-24 12:12:04 +00:00
|
|
|
if (canvas)
|
|
|
|
xcb_free_pixmap (c, canvas);
|
|
|
|
if (draw_gc)
|
|
|
|
xcb_free_gc (c, draw_gc);
|
|
|
|
if (clear_gc)
|
|
|
|
xcb_free_gc (c, clear_gc);
|
|
|
|
if (underl_gc)
|
|
|
|
xcb_free_gc (c, underl_gc);
|
|
|
|
if (c)
|
|
|
|
xcb_disconnect (c);
|
2014-02-14 11:51:07 +00:00
|
|
|
|
|
|
|
clear_cmd_area_list();
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sighandle (int signal)
|
|
|
|
{
|
|
|
|
if (signal == SIGINT || signal == SIGTERM)
|
2014-02-08 15:45:38 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse an urxvt-like geometry string {width}x{height}, both the fields are
|
|
|
|
* optional. A width of -1 means that the bar spawns the whole screen. */
|
|
|
|
void
|
|
|
|
parse_geometry_string (char *str)
|
|
|
|
{
|
|
|
|
char *p, *q;
|
|
|
|
int tmp;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
p = str;
|
|
|
|
|
|
|
|
tmp = strtoul(p, &q, 10);
|
|
|
|
if (p != q)
|
|
|
|
cfg.width = tmp;
|
|
|
|
|
|
|
|
/* P now might point to a NULL char, strtoul takes care of that */
|
|
|
|
p = q + 1;
|
|
|
|
|
|
|
|
tmp = strtoul(p, &q, 10);
|
|
|
|
if (p != q)
|
|
|
|
cfg.height = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
parse_font_list (char *str)
|
|
|
|
{
|
|
|
|
char *tok;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
tok = strtok(str, ",");
|
|
|
|
if (tok)
|
|
|
|
strncpy(cfg.main_font, tok, sizeof(cfg.main_font));
|
|
|
|
tok = strtok(NULL, ",");
|
|
|
|
if (tok)
|
|
|
|
strncpy(cfg.alt_font, tok, sizeof(cfg.alt_font));
|
|
|
|
|
|
|
|
return;
|
2012-07-23 14:49:39 +00:00
|
|
|
}
|
|
|
|
|
2012-07-16 09:41:34 +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-14 11:51:07 +00:00
|
|
|
char input[2048] = {0, };
|
2012-07-20 09:53:29 +00:00
|
|
|
struct pollfd pollin[2] = {
|
|
|
|
{ .fd = STDIN_FILENO, .events = POLLIN },
|
|
|
|
{ .fd = -1 , .events = POLLIN },
|
|
|
|
};
|
2012-07-17 09:34:12 +00:00
|
|
|
|
|
|
|
xcb_generic_event_t *ev;
|
|
|
|
xcb_expose_event_t *expose_ev;
|
2014-02-14 11:51:07 +00:00
|
|
|
xcb_button_release_event_t *button_ev;
|
2012-07-17 09:34:12 +00:00
|
|
|
|
|
|
|
char ch;
|
2014-02-08 15:45:38 +00:00
|
|
|
while ((ch = getopt (argc, argv, "hg:bdf:a:o:p")) != -1) {
|
2012-07-16 10:30:49 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'h':
|
2014-02-08 15:45:38 +00:00
|
|
|
printf ("usage: %s [-h | -g | -b | -d | -f | -a | -o | -p ]\n"
|
2012-07-24 10:08:02 +00:00
|
|
|
"\t-h Show this help\n"
|
2014-02-08 15:45:38 +00:00
|
|
|
"\t-g Set the bar geometry {width}x{height})\n"
|
2012-07-24 10:08:02 +00:00
|
|
|
"\t-b Put bar at the bottom of the screen\n"
|
2014-02-08 15:45:38 +00:00
|
|
|
"\t-d Force docking (use this if your WM isn't EWMH compliant)\n"
|
|
|
|
"\t-f Bar font list, comma separated\n"
|
|
|
|
"\t-a Set the bar alpha ranging from 0.0 to 1.0 (requires a compositor)\n"
|
2012-07-17 09:34:12 +00:00
|
|
|
"\t-p Don't close after the data ends\n", argv[0]);
|
2014-02-08 15:45:38 +00:00
|
|
|
exit (EXIT_SUCCESS);
|
|
|
|
case 'a': cfg.alpha = strtof(optarg, NULL); break;
|
|
|
|
case 'g': parse_geometry_string(optarg); break;
|
|
|
|
case 'p': cfg.permanent = 1; break;
|
|
|
|
case 'b': cfg.place_bottom = 1; break;
|
|
|
|
case 'd': cfg.force_docking = 1; break;
|
|
|
|
case 'f': parse_font_list(optarg); break;
|
2012-07-16 10:30:49 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-16 09:41:34 +00:00
|
|
|
|
2014-02-08 15:45:38 +00:00
|
|
|
/* Sanitize the arguments */
|
|
|
|
if (cfg.alpha > 1.0f)
|
|
|
|
cfg.alpha = 1.0f;
|
|
|
|
if (cfg.alpha < 0.0f)
|
|
|
|
cfg.alpha = 0.0f;
|
|
|
|
|
2012-07-24 12:12:04 +00:00
|
|
|
atexit (cleanup);
|
2012-07-16 09:41:34 +00:00
|
|
|
signal (SIGINT, sighandle);
|
|
|
|
signal (SIGTERM, sighandle);
|
|
|
|
init ();
|
|
|
|
|
2012-07-17 14:56:03 +00:00
|
|
|
/* Get the fd to Xserver */
|
|
|
|
pollin[1].fd = xcb_get_file_descriptor (c);
|
|
|
|
|
2014-02-09 23:54:33 +00:00
|
|
|
fill_rect (clear_gc, 0, 0, cfg.width, cfg.height);
|
2012-07-17 09:34:12 +00:00
|
|
|
|
2012-07-16 23:17:11 +00:00
|
|
|
for (;;) {
|
2012-07-17 14:56:03 +00:00
|
|
|
int redraw = 0;
|
|
|
|
|
2012-07-24 10:25:45 +00:00
|
|
|
if (poll (pollin, 2, -1) > 0) {
|
2014-02-08 15:45:38 +00:00
|
|
|
if (pollin[0].revents & POLLHUP) { /* No more data... */
|
|
|
|
if (cfg.permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
|
|
|
|
else break; /* ...bail out */
|
2012-07-18 19:27:41 +00:00
|
|
|
}
|
2012-07-17 14:56:03 +00:00
|
|
|
if (pollin[0].revents & POLLIN) { /* New input, process it */
|
2014-02-04 20:28:01 +00:00
|
|
|
if (fgets (input, sizeof(input), stdin) == NULL)
|
|
|
|
break; /* EOF received */
|
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
clear_cmd_area_list();
|
2012-07-17 14:56:03 +00:00
|
|
|
parse (input);
|
|
|
|
redraw = 1;
|
|
|
|
}
|
|
|
|
if (pollin[1].revents & POLLIN) { /* Xserver broadcasted an event */
|
|
|
|
while ((ev = xcb_poll_for_event (c))) {
|
|
|
|
|
2014-02-14 11:51:07 +00:00
|
|
|
switch (ev->response_type & ~0x80) {
|
2012-07-17 14:56:03 +00:00
|
|
|
case XCB_EXPOSE:
|
2014-02-14 11:51:07 +00:00
|
|
|
expose_ev = (xcb_expose_event_t *)ev;
|
2012-07-17 14:56:03 +00:00
|
|
|
if (expose_ev->count == 0) redraw = 1;
|
|
|
|
break;
|
2014-02-14 11:51:07 +00:00
|
|
|
case XCB_BUTTON_RELEASE:
|
|
|
|
button_ev = (xcb_button_release_event_t *)ev;
|
|
|
|
if (button_ev->detail == MOUSE_BUTTON)
|
|
|
|
xcb_handle_mouse_event (button_ev->root_x);
|
2012-07-17 14:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free (ev);
|
|
|
|
}
|
2012-07-16 23:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-17 09:34:12 +00:00
|
|
|
|
2014-01-24 11:30:20 +00:00
|
|
|
if (redraw) { /* Copy our temporary pixmap onto the window */
|
2014-02-09 23:54:33 +00:00
|
|
|
for (monitor_t *mon = monhead; mon; mon = mon->next) {
|
|
|
|
// if (mon->width)
|
2014-02-08 15:45:38 +00:00
|
|
|
xcb_copy_area (c, canvas, mon->window, draw_gc, mon->x, 0, 0, 0, mon->width, cfg.height);
|
2014-01-24 11:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 14:49:39 +00:00
|
|
|
|
2012-07-16 23:17:11 +00:00
|
|
|
xcb_flush (c);
|
2012-07-16 09:41:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|