#include <util.h>
#include <X11/cursorfont.h>
#include <X11/extensions/Xrandr.h>
-#include <xcb/xcb.h>
+#include <X11/Xlib-xcb.h>
#include <xcb/xcb_atom.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_event.h>
int xrandr_support;
int xrandr_eventbase;
unsigned int numlockmask = 0;
+
+Display *display;
xcb_connection_t *conn;
xcb_key_symbols_t *syms;
int verbose_layout = 0;
time_t time_started;
pid_t bar_pid;
-#if 0
-XFontSet bar_fs;
-XFontSetExtents *bar_fs_extents;
-#endif
+xcb_font_t bar_fs;
+int32_t bar_fs_height;
char *bar_fonts;
struct passwd *pwd;
};
/* function prototypes */
+xcb_char2b_t *char2b(const char *);
int conf_load(char *, int);
void constrain_window(struct ws_win *, struct swm_region *, int);
void do_sync(void);
void update_window(struct ws_win *);
/* function definitions */
+xcb_char2b_t *
+char2b(const char *str)
+{
+ xcb_char2b_t *s;
+ size_t i, len;
+
+ len = strlen(str);
+ s = malloc(len * sizeof(xcb_char2b_t));
+ if (!s)
+ return (NULL);
+
+ for (i = 0; i < len; i++) {
+ s[i].byte1 = '\0';
+ s[i].byte2 = str[i];
+ }
+
+ return (s);
+}
+
int
parse_rgb(const char *rgb, uint16_t *rr, uint16_t *gg, uint16_t *bb)
{
num_screens = xcb_setup_roots_length(xcb_get_setup(conn));
for (i = 0; i < num_screens; i++) {
/* Support check window will be created by workaround(). */
-
+
/* Report supported atoms */
xcb_delete_property(conn, screens[i].root, sup_list);
for (j = 0; j < LENGTH(ewmh); j++)
void
bar_print(struct swm_region *r, const char *s)
{
- size_t len;
- xcb_rectangle_t rect;
- uint32_t gcv[1];
+ size_t len;
+ xcb_rectangle_t rect;
+ uint32_t gcv[1];
+ xcb_char2b_t *c2b;
+ xcb_query_text_extents_reply_t *ter;
+ int32_t x, width;
len = strlen(s);
- /* FIXME fix bar font position calculations */
-#if 0
- int x = 0;
- XRectangle ibox, lbox;
- XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
+ c2b = char2b(s);
+ if (!c2b)
+ return;
+ ter = xcb_query_text_extents_reply(conn,
+ xcb_query_text_extents(conn, bar_fs, len, c2b),
+ NULL);
+ if (!ter) {
+ free(c2b);
+ return;
+ }
+ width = ter->overall_width;
+
+ free(ter);
+ free(c2b);
switch (bar_justify) {
case SWM_BAR_JUSTIFY_LEFT:
x = SWM_BAR_OFFSET;
break;
case SWM_BAR_JUSTIFY_CENTER:
- x = (WIDTH(r) - lbox.width) / 2;
+ x = (WIDTH(r) - width) / 2;
break;
case SWM_BAR_JUSTIFY_RIGHT:
- x = WIDTH(r) - lbox.width - SWM_BAR_OFFSET;
+ x = WIDTH(r) - width - SWM_BAR_OFFSET;
break;
}
if (x < SWM_BAR_OFFSET)
x = SWM_BAR_OFFSET;
-#endif
rect.x = 0;
rect.y = 0;
xcb_change_gc(conn, r->s->bar_gc, XCB_GC_BACKGROUND, gcv);
gcv[0] = r->s->c[SWM_S_COLOR_BAR_FONT].color;
xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FOREGROUND, gcv);
-#if 0
+ gcv[0] = bar_fs;
+ xcb_change_gc(conn, r->s->bar_gc, XCB_GC_FONT, gcv);
+
xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, x,
- (bar_fs_extents->max_logical_extent.height - lbox.height) / 2 -
- lbox.y, s);
-#else
- /* workaround */
- xcb_image_text_8(conn, len, r->bar->buffer, r->s->bar_gc, 4, 14, s);
-#endif
+ bar_fs_height, s);
/* blt */
xcb_copy_area(conn, r->bar->buffer, r->bar->id, r->s->bar_gc, 0, 0,
void
bar_setup(struct swm_region *r)
{
- char *default_string;
- char **missing_charsets;
- int num_missing_charsets = 0;
- int i;
+ char *bar_font;
xcb_screen_t *screen = get_screen(r->s->idx);
uint32_t wa[3];
+ xcb_generic_error_t *error;
+ xcb_void_cookie_t voc;
+ xcb_query_font_reply_t *bar_fs_info;
-#if 0
if (bar_fs) {
- XFreeFontSet(display, bar_fs);
- bar_fs = NULL;
+ xcb_close_font(conn, bar_fs);
+ bar_fs = 0;
}
-#endif
if ((r->bar = calloc(1, sizeof(struct swm_bar))) == NULL)
err(1, "bar_setup: calloc: failed to allocate memory.");
-#if 0
- DNPRINTF(SWM_D_BAR, "bar_setup: loading bar_fonts: %s\n", bar_fonts);
-
- bar_fs = XCreateFontSet(display, bar_fonts, &missing_charsets,
- &num_missing_charsets, &default_string);*/
- if (num_missing_charsets > 0) {
- warnx("Unable to load charset(s):");
-
- for (i = 0; i < num_missing_charsets; ++i)
- warnx("%s", missing_charsets[i]);
+ bar_fs = xcb_generate_id(conn);
+ while ((bar_font = strsep(&bar_fonts, " ,")) != NULL) {
+ if (*bar_font == '\0')
+ continue;
- XFreeStringList(missing_charsets);
+ DNPRINTF(SWM_D_INIT, "bar_setup: try font %s\n", bar_font);
+ voc = xcb_open_font_checked(conn, bar_fs, strlen(bar_font),
+ bar_font);
- if (strcmp(default_string, ""))
- warnx("Glyphs from those sets will be replaced "
- "by '%s'.", default_string);
- else
- warnx("Glyphs from those sets won't be drawn.");
+ if ((error = xcb_request_check(conn, voc))) {
+ DNPRINTF(SWM_D_INIT,
+ "bar_setup: unable to open font: %s\n",
+ bar_font);
+ free(error);
+ warnx("unable to load font %s", bar_font);
+ } else {
+ DNPRINTF(SWM_D_INIT,
+ "bar_setup: successfully opened font: %s\n",
+ bar_font);
+ break;
+ }
}
- if (bar_fs == NULL)
- errx(1, "Error creating font set structure.");
-
- bar_fs_extents = XExtentsOfFontSet(bar_fs);
+ bar_fs_info = xcb_query_font_reply(conn, xcb_query_font(conn, bar_fs),
+ NULL);
+ if (!bar_fs_info) {
+ warnx(1, "unable to get font information for font %s\n",
+ bar_font);
+ return;
+ }
+ bar_fs_height = bar_fs_info->font_ascent + bar_fs_info->font_descent;
+ free(bar_fs_info);
- bar_height = bar_fs_extents->max_logical_extent.height +
- 2 * bar_border_width;
+ bar_height = bar_fs_height + 4 * bar_border_width;
if (bar_height < 1)
bar_height = 1;
-#else
- /* workaround */
- bar_height = 24;
-#endif
+
X(r->bar) = X(r);
Y(r->bar) = bar_at_bottom ? (Y(r) + HEIGHT(r) - bar_height) : Y(r);
WIDTH(r->bar) = WIDTH(r) - 2 * bar_border_width;
struct ws_win *win = NULL;
struct search_window *sw = NULL;
xcb_window_t w;
- uint32_t gcv[3], wa[2];
- int i;
+ uint32_t gcv[4], wa[2];
+ int i, width;
char s[8];
FILE *lfile;
size_t len;
- XRectangle ibox, lbox;
+ xcb_char2b_t *c2b;
+ xcb_query_text_extents_reply_t *ter;
DNPRINTF(SWM_D_MISC, "search_win\n");
snprintf(s, sizeof s, "%d", i);
len = strlen(s);
- /* FIXME fix calculations */
-#if 0
- XmbTextExtents(bar_fs, s, len, &ibox, &lbox);
-#endif
+ c2b = char2b(s);
+ if (!c2b) {
+ warn("search_win: char2b malloc");
+ free(sw);
+ fclose(lfile);
+ search_win_cleanup();
+ return;
+ }
+ ter = xcb_query_text_extents_reply(conn,
+ xcb_query_text_extents(conn, bar_fs,
+ len, c2b),
+ NULL);
+ if (!ter) {
+ warn("search_win: query text failed");
+ free(c2b);
+ free(sw);
+ fclose(lfile);
+ search_win_cleanup();
+ return;
+ }
+ width = ter->overall_width;
+ free(ter);
+ free(c2b);
w = xcb_generate_id(conn);
wa[0] = r->s->c[SWM_S_COLOR_FOCUS].color;
wa[1] = r->s->c[SWM_S_COLOR_UNFOCUS].color;
-#if 0
xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
- lbox.width + 4, bar_fs_extents->max_logical_extent.height,
+ width + 4, bar_fs_height + 4,
1, XCB_WINDOW_CLASS_INPUT_OUTPUT, XCB_COPY_FROM_PARENT,
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL, wa);
-#else
- /* workaround */
- xcb_create_window(conn, XCB_COPY_FROM_PARENT, w, win->id, 0, 0,
- 22, 20, 1, XCB_WINDOW_CLASS_INPUT_OUTPUT,
- XCB_COPY_FROM_PARENT, XCB_CW_BACK_PIXEL |
- XCB_CW_BORDER_PIXEL, wa);
-#endif
sw->indicator = w;
TAILQ_INSERT_TAIL(&search_wl, sw, entry);
sw->gc = xcb_generate_id(conn);
gcv[0] = r->s->c[SWM_S_COLOR_BAR].color;
gcv[1] = r->s->c[SWM_S_COLOR_FOCUS].color;
- gcv[2] = 0;
+ gcv[2] = bar_fs;
+ gcv[3] = 0;
xcb_create_gc(conn, sw->gc, w, XCB_GC_FOREGROUND |
- XCB_GC_BACKGROUND | XCB_GC_GRAPHICS_EXPOSURES, gcv);
+ XCB_GC_BACKGROUND | XCB_GC_FONT | XCB_GC_GRAPHICS_EXPOSURES,
+ gcv);
map_window_raised(w);
-#if 0
- xcb_image_text_8(conn, len, w, sw->gc, 2,
- (bar_fs_extents->max_logical_extent.height -
- lbox.height) / 2 - lbox.y, s);
-#else
- /* workaround */
- xcb_image_text_8(conn, len, w, sw->gc, 6, 14, s);
-#endif
+ xcb_image_text_8(conn, len, w, sw->gc, 2, bar_fs_height, s);
DNPRINTF(SWM_D_MISC, "search_win: mapped window: 0x%x\n", w);
if (asprintf(&bar_fonts, "%s,%s", value, bar_fonts) == -1)
err(1, "setconfvalue: asprintf: failed to allocate "
"memory for bar_fonts.");
-
free(b);
break;
case SWM_S_BAR_FORMAT:
sact.sa_flags = SA_NOCLDSTOP;
sigaction(SIGCHLD, &sact, NULL);
- conn = xcb_connect(NULL, NULL);
+ if (!(display = XOpenDisplay(0)))
+ errx(1, "can not open display");
+
+ conn = XGetXCBConnection(display);
if (xcb_connection_has_error(conn))
errx(1, "can not get XCB connection");