Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build the X11 and XFT demos from the same sources #776

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,5 @@ jobs:
run: make -C demo/x11_opengl3
- name: build x11_rawfb
run: make -C demo/rawfb/x11
- name: build x11_xft
run: make -C demo/x11_xft
- name: build example
run: make -C example
17 changes: 15 additions & 2 deletions demo/x11/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@
BIN = demo

# Flags
CFLAGS += -std=c89 -Wall -Wextra -pedantic -Wno-unused-function
CFLAGS += -std=c89 -Wall -Wextra -pedantic -Wno-unused-function -D_POSIX_C_SOURCE=200809L

SRC = main.c
OBJ = $(SRC:.c=.o)

X11_CFLAGS += ${shell pkg-config --cflags x11}
X11_LDFLAGS += ${shell pkg-config --libs x11} -lm

XFT_CFLAGS += ${shell pkg-config --cflags xft x11} -DNK_XLIB_USE_XFT
XFT_LDFLAGS += ${shell pkg-config --libs xft x11} -lm

all: $(BIN) $(BIN)-xft

$(BIN):
@mkdir -p bin
rm -f bin/$(BIN) $(OBJS)
$(CC) $(SRC) $(CFLAGS) -D_POSIX_C_SOURCE=200809L -o bin/$(BIN) -lX11 -lm
$(CC) $(SRC) $(CFLAGS) $(X11_CFLAGS) -o bin/$(BIN) ${LDFLAGS} ${X11_LDFLAGS}

$(BIN)-xft:
@mkdir -p bin
rm -f bin/$(BIN)-xft $(OBJS)
$(CC) $(SRC) $(CFLAGS) $(XFT_CFLAGS) -o bin/$(BIN)-xft ${LDFLAGS} ${LDFLAGS} ${XFT_LDFLAGS}
6 changes: 6 additions & 0 deletions demo/x11/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,14 @@ main(void)
xw.height = (unsigned int)xw.attr.height;

/* GUI */
#ifdef NK_XLIB_USE_XFT
xw.font = nk_xfont_create(xw.dpy, "Arial");
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win,
xw.vis, xw.cmap, xw.width, xw.height);
#else
xw.font = nk_xfont_create(xw.dpy, "fixed");
ctx = nk_xlib_init(xw.font, xw.dpy, xw.screen, xw.win, xw.width, xw.height);
#endif

while (running)
{
Expand Down
84 changes: 84 additions & 0 deletions demo/x11/nuklear_xlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#include <X11/Xlib.h>

typedef struct XFont XFont;
#ifdef NK_XLIB_USE_XFT
NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, Visual *vis, Colormap cmap, unsigned w, unsigned h);
#else
NK_API struct nk_context* nk_xlib_init(XFont*, Display*, int scrn, Window root, unsigned w, unsigned h);
#endif
NK_API int nk_xlib_handle_event(Display*, int scrn, Window, XEvent*);
NK_API void nk_xlib_render(Drawable screen, struct nk_color clear);
NK_API void nk_xlib_shutdown(void);
Expand Down Expand Up @@ -53,6 +57,10 @@ NK_API void nk_xfont_del(Display *dpy, XFont *font);
#include <X11/Xlocale.h>
#include <X11/Xatom.h>

#ifdef NK_XLIB_USE_XFT
#include <X11/Xft/Xft.h>
#endif

#include <sys/time.h>
#include <unistd.h>
#include <time.h>
Expand Down Expand Up @@ -80,8 +88,12 @@ struct XFont {
int ascent;
int descent;
int height;
#ifdef NK_XLIB_USE_XFT
XftFont * ft;
#else
XFontSet set;
XFontStruct *xfont;
#endif
struct nk_user_font handle;
};
struct XSurface {
Expand All @@ -91,6 +103,9 @@ struct XSurface {
Window root;
Drawable drawable;
unsigned int w, h;
#ifdef NK_XLIB_USE_XFT
XftDraw * ftdraw;
#endif
};
struct XImageWithAlpha {
XImage* ximage;
Expand All @@ -112,6 +127,10 @@ static struct {
Cursor cursor;
Display *dpy;
Window root;
#ifdef NK_XLIB_USE_XFT
Visual *vis;
Colormap cmap;
#endif
double last_button_click;
double time_of_last_frame;
} xlib;
Expand Down Expand Up @@ -147,6 +166,10 @@ nk_xsurf_create(int screen, unsigned int w, unsigned int h)
XSetLineAttributes(xlib.dpy, surface->gc, 1, LineSolid, CapButt, JoinMiter);
surface->drawable = XCreatePixmap(xlib.dpy, xlib.root, w, h,
(unsigned int)DefaultDepth(xlib.dpy, screen));
#ifdef NK_XLIB_USE_XFT
surface->ftdraw = XftDrawCreate(xlib.dpy, surface->drawable,
xlib.vis, xlib.cmap);
#endif
return surface;
}

Expand All @@ -159,6 +182,9 @@ nk_xsurf_resize(XSurface *surf, unsigned int w, unsigned int h)
if(surf->drawable) XFreePixmap(surf->dpy, surf->drawable);
surf->drawable = XCreatePixmap(surf->dpy, surf->root, w, h,
(unsigned int)DefaultDepth(surf->dpy, surf->screen));
#ifdef NK_XLIB_USE_XFT
XftDrawChange(surf->ftdraw, surf->drawable);
#endif
}

NK_INTERN void
Expand All @@ -170,6 +196,10 @@ nk_xsurf_scissor(XSurface *surf, float x, float y, float w, float h)
clip_rect.width = (unsigned short)(w+2);
clip_rect.height = (unsigned short)(h+2);
XSetClipRectangles(surf->dpy, surf->gc, 0, 0, &clip_rect, 1, Unsorted);

#ifdef NK_XLIB_USE_XFT
XftDrawSetClipRectangles(surf->ftdraw, 0, 0, &clip_rect, 1);
#endif
}

NK_INTERN void
Expand Down Expand Up @@ -416,16 +446,31 @@ nk_xsurf_draw_text(XSurface *surf, short x, short y, const char *text, int len,
XFont *font, struct nk_color cfg)
{
int tx, ty;
#ifdef NK_XLIB_USE_XFT
XRenderColor xrc;
XftColor color;
#else
unsigned long fg = nk_color_from_byte(&cfg.r);
#endif

if(!text || !font || !len) return;

tx = (int)x;
ty = (int)y + font->ascent;
#ifdef NK_XLIB_USE_XFT
xrc.red = cfg.r * 257;
xrc.green = cfg.g * 257;
xrc.blue = cfg.b * 257;
xrc.alpha = cfg.a * 257;
XftColorAllocValue(surf->dpy, xlib.vis, xlib.cmap, &xrc, &color);
XftDrawStringUtf8(surf->ftdraw, &color, font->ft, tx, ty, (FcChar8*)text, len);
XftColorFree(surf->dpy, xlib.vis, xlib.cmap, &color);
#else
XSetForeground(surf->dpy, surf->gc, fg);
if(font->set)
XmbDrawString(surf->dpy,surf->drawable,font->set,surf->gc,tx,ty,(const char*)text,(int)len);
else XDrawString(surf->dpy, surf->drawable, surf->gc, tx, ty, (const char*)text, (int)len);
#endif
}


Expand Down Expand Up @@ -563,6 +608,9 @@ nk_xsurf_blit(Drawable target, XSurface *surf, unsigned int w, unsigned int h)
NK_INTERN void
nk_xsurf_del(XSurface *surf)
{
#ifdef NK_XLIB_USE_XFT
XftDrawDestroy(surf->ftdraw);
#endif
XFreePixmap(surf->dpy, surf->drawable);
XFreeGC(surf->dpy, surf->gc);
free(surf);
Expand All @@ -571,6 +619,17 @@ nk_xsurf_del(XSurface *surf)
NK_API XFont*
nk_xfont_create(Display *dpy, const char *name)
{
#ifdef NK_XLIB_USE_XFT
XFont *font = (XFont*)calloc(1, sizeof(XFont));
font->ft = XftFontOpenName(dpy, XDefaultScreen(dpy), name);
if (!font->ft) {
fprintf(stderr, "missing font: %s\n", name);
return font;
}
font->ascent = font->ft->ascent;
font->descent = font->ft->descent;
font->height = font->ft->height;
#else
int n;
char *def, **missing;
XFont *font = (XFont*)calloc(1, sizeof(XFont));
Expand Down Expand Up @@ -600,13 +659,26 @@ nk_xfont_create(Display *dpy, const char *name)
font->descent = font->xfont->descent;
}
font->height = font->ascent + font->descent;
#endif
return font;
}

NK_INTERN float
nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int len)
{
XFont *font = (XFont*)handle.ptr;

#ifdef NK_XLIB_USE_XFT
XGlyphInfo g;

NK_UNUSED(height);

if(!font || !text)
return 0;

XftTextExtentsUtf8(xlib.dpy, font->ft, (FcChar8*)text, len, &g);
return g.xOff;
#else
XRectangle r;

NK_UNUSED(height);
Expand All @@ -621,21 +693,29 @@ nk_xfont_get_text_width(nk_handle handle, float height, const char *text, int le
int w = XTextWidth(font->xfont, (const char*)text, len);
return (float)w;
}
#endif
}

NK_API void
nk_xfont_del(Display *dpy, XFont *font)
{
if(!font) return;
#ifdef NK_XLIB_USE_XFT
XftFontClose(dpy, font->ft);
#else
if(font->set)
XFreeFontSet(dpy, font->set);
else
XFreeFont(dpy, font->xfont);
#endif
free(font);
}

NK_API struct nk_context*
nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
#ifdef NK_XLIB_USE_XFT
Visual *vis, Colormap cmap,
#endif
unsigned int w, unsigned int h)
{
struct nk_user_font *font = &xfont->handle;
Expand All @@ -644,6 +724,10 @@ nk_xlib_init(XFont *xfont, Display *dpy, int screen, Window root,
font->width = nk_xfont_get_text_width;
xlib.dpy = dpy;
xlib.root = root;
#ifdef NK_XLIB_USE_XFT
xlib.vis = vis;
xlib.cmap = cmap;
#endif

if (!setlocale(LC_ALL,"")) return 0;
if (!XSupportsLocale()) return 0;
Expand Down
21 changes: 0 additions & 21 deletions demo/x11_xft/Makefile

This file was deleted.

Loading