#include "app.h"
int graphInit(Graph *g, App *app) {
LOGDBG("graph: init");
const uint16_t F = 65535; // full
const uint16_t H = 32768; // half
const uint16_t Q = 16384; // quart
const uint16_t colorsRgb[GS_COUNT*2][3] = { // background, foreground
{ 0, 0, Q }, { 0, 0, Q }, // window
{ 0, 0, H }, { 0, 0, H }, // inactive button
{ 0, 0, F }, { 0, 0, F }, // active button
{ 0, 0, H }, { F, F, 0 }, // inactive text
{ 0, 0, F }, { F, F, 0 } }; // active text
// create palette
g->app = app;
g->cm = xcb_generate_id(g->app->xcb);
xcb_create_colormap(g->app->xcb, XCB_COLORMAP_ALLOC_NONE, g->cm, g->app->win, g->app->screen->root_visual);
// encqueue colors allocarion requests
xcb_alloc_color_cookie_t cookies[GS_COUNT*2];
for(int i = 0; i < GS_COUNT*2; ++i)
cookies[i] = xcb_alloc_color(g->app->xcb, g->cm, colorsRgb[i][0], colorsRgb[i][1], colorsRgb[i][2]);
// take color allocation replies (take color indices)
uint32_t colors[GS_COUNT*2];
for(int i = 0; i < GS_COUNT*2; ++i) {
xcb_alloc_color_reply_t *reply = xcb_alloc_color_reply(g->app->xcb, cookies[i], NULL);
colors[i] = reply->pixel;
free(reply);
}
// associate palette with the window
xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->cm);
char fontname[1024] = "fixed";
{ // scan fonts
int found = 0;
int maxcnt = 1024;
const char *pattern = "*misc*medium-r-normal*-c-0-ISO10646-1*";
LOGDBG("graph: init: scan fonts by pattern [%s], maxcnt=%d", pattern, maxcnt);
xcb_list_fonts_cookie_t c = xcb_list_fonts(g->app->xcb, maxcnt, strlen(pattern), pattern);
xcb_list_fonts_reply_t *r = xcb_list_fonts_reply(g->app->xcb, c, NULL);
if (r) {
int cnt = xcb_list_fonts_names_length(r);
LOGDBG("graph: init: found %d fonts", cnt);
xcb_str_iterator_t it = xcb_list_fonts_names_iterator(r);
for(int i = 0; i < cnt; ++i) {
int l = xcb_str_name_length(it.data);
if (!found && l < sizeof(fontname)) {
memcpy(fontname, xcb_str_name(it.data), l);
fontname[l] = 0;
found = 1;
}
#ifndef NDEBUG
char buf[1024] = {};
if (l > 1023) l = 1023;
memcpy(buf, xcb_str_name(it.data), l);
buf[l] = 0;
LOGDBG("graph: init: found font: %s", buf);
#endif
xcb_str_next(&it);
}
free(r);
} else {
LOGWRN("graph: init: cannot list fonts");
}
}
// init font
LOGDBG("graph: init: open font [%s]", fontname);
g->font = xcb_generate_id(g->app->xcb);
xcb_void_cookie_t c = xcb_open_font_checked(g->app->xcb, g->font, strlen(fontname), fontname);
if (xcb_request_check(g->app->xcb, c)) {
LOGERR("graph: init: cannot open font [%s]", fontname);
xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->app->screen->default_colormap);
xcb_free_colormap(g->app->xcb, g->cm);
return 0;
}
// create contexts
uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT | XCB_GC_GRAPHICS_EXPOSURES;
uint32_t values[4] = {0, 0, g->font, 0};
for(int i = 0; i < GS_COUNT; ++i) {
values[0] = colors[i*2 + 1]; // foreground
values[1] = colors[i*2]; // background
g->gc[i] = xcb_generate_id(g->app->xcb);
xcb_create_gc(g->app->xcb, g->gc[i], g->app->win, mask, values);
}
return 1;
}
void graphDeinit(Graph *g) {
LOGDBG("graph: deinit");
for(int i = 0; i < GS_COUNT; ++i)
xcb_free_gc(g->app->xcb, g->gc[i]);
xcb_close_font(g->app->xcb, g->font);
xcb_change_window_attributes(g->app->xcb, g->app->win, XCB_CW_COLORMAP, &g->app->screen->default_colormap);
xcb_free_colormap(g->app->xcb, g->cm);
}
void graphResize(Graph *g)
{ }
void graphDrawBackgound(Graph *g, int x, int y, int w, int h) {
xcb_rectangle_t r = {x, y, w, h};
xcb_poly_fill_rectangle(g->app->xcb, g->app->win, g->gc[GS_WINDOW], 1, &r);
}
void graphDrawButton(Graph *g, int x, int y, int w, int h, int active) {
GraphStyle s = active ? GS_BUTTON_ACTIVE : GS_BUTTON_INACTIVE;
xcb_rectangle_t r = {x, y, w, h};
xcb_poly_fill_rectangle(g->app->xcb, g->app->win, g->gc[s], 1, &r);
}
void textLayoutInit(TextLayout *tl, Graph *g, const char *text) {
tl->g = g;
tl->x = tl->y = 0;
tl->len = 0;
// convert utf8 to char2b
const unsigned char *s = (const unsigned char *)text;
while(*s && tl->len < TL_MAXLEN) {
unsigned int d = 0xfffd;
if (!(s[0] & 0x80)) {
d = s[0];
} else
if ( (s[0] & 0xe0) == 0xc0
&& (s[1] & 0xc0) == 0x80 )
{
d = ((s[0] & 0x1f) << 6)
| ((s[1] & 0x3f) << 0);
++s;
} else
if ( (s[0] & 0xf0) == 0xe0
&& (s[1] & 0xc0) == 0x80
&& (s[2] & 0xc0) == 0x80 )
{
d = ((s[0] & 0x0f) << 12)
| ((s[1] & 0x3f) << 6)
| ((s[2] & 0x3f) << 0);
s += 2;
}
tl->text[tl->len].byte1 = d >> 8;
tl->text[tl->len].byte2 = d & 0xff;
++tl->len;
++s;
}
if (!tl->len)
return;
// measure text
xcb_query_text_extents_cookie_t cookie =
xcb_query_text_extents(tl->g->app->xcb, tl->g->font, tl->len, tl->text);
xcb_query_text_extents_reply_t *tx = xcb_query_text_extents_reply(tl->g->app->xcb, cookie, NULL);
if (!tx) {
LOGWRN("text layout: cannot get text extents for string [%s] len=%d", text, tl->len);
//tl->len = 0;
return;
}
tl->x = -tx->overall_left - tx->overall_width/2;
tl->y = tx->font_ascent/2;
free(tx);
}
void textLayoutDeinit(TextLayout *tl)
{ }
void textLayoutDraw(TextLayout *tl, int x, int y, int active) {
if (!tl->len) return;
GraphStyle s = active ? GS_TEXT_ACTIVE : GS_TEXT_INACTIVE;
xcb_image_text_16(tl->g->app->xcb, tl->len, tl->g->app->win, tl->g->gc[s], x + tl->x, y + tl->y, tl->text);
}