commit fe2a90b53fd9ecc20a7747a38f6b04489017227c
parent 6bc931975d224e235882acb70a5564008f76cb82
Author: pancake <pancake@nopcode.org>
Date: Wed, 1 Sep 2010 00:11:22 +0200
use imlib2 to load images for x11 backend
set x11 window title
add support for loading and manipulating images in x11
added image.c api to wrap Imlib and cache images
Diffstat:
Makefile | | | 13 | +++++++------ |
gi_x11.c | | | 51 | ++++++++++++++++++++++++++++++++++++--------------- |
image.c | | | 29 | +++++++++++++++++++++++++++++ |
swk.c | | | 2 | +- |
swk.h | | | 13 | +++++++++++++ |
5 files changed, 86 insertions(+), 22 deletions(-)
diff --git a/Makefile b/Makefile
@@ -3,6 +3,7 @@
-include config.mk
VERSION=0.1
+OBJS=swk.o text.o image.o
# graphic backend
GI?=sdl
ifeq (${GI},sdl)
@@ -10,7 +11,7 @@ GI_LIBS=-lSDL -lSDL_ttf -lSDL_image
CFLAGS+=-DUSE_SDL
else
ifeq (${GI},x11)
-GI_LIBS=-lX11 -ldraw
+GI_LIBS=-lX11 -ldraw -lImlib2
CFLAGS+=-DUSE_X11
endif
endif
@@ -38,7 +39,7 @@ config.mk: config.h
clean:
echo >swk.mk
cd t && ${MAKE} clean
- rm -f swk.pc swk.mk libswk.a libswk.so swk.o text.o ${GI_OBJS}
+ rm -f swk.pc swk.mk libswk.a libswk.so ${OBJS} ${GI_OBJS}
install:
mkdir -p ${DESTDIR}/${INCDIR}
@@ -62,14 +63,14 @@ static: libswk.a
shared: libswk.so
-libswk.so: config.mk swk.o text.o ${GI_OBJS}
- ${CC} ${CFLAGS} -fPIC -shared swk.c ${GI_SRCS} -o libswk.so
+libswk.so: config.mk ${OBJS} ${GI_OBJS}
+ ${CC} ${CFLAGS} -fPIC -shared ${OBJS} ${GI_SRCS} -o libswk.so
swk.o: config.mk
-libswk.a: config.mk swk.o text.o ${GI_OBJS}
+libswk.a: config.mk ${OBJS} ${GI_OBJS}
rm -f libswk.a
- ar qcvf libswk.a text.o swk.o ${GI_OBJS}
+ ar qcvf libswk.a ${OBJS} ${GI_OBJS}
echo SWKINCS+=-I${PREFIX}/include > swk.mk
echo SWKLIB+=${PREFIX}/lib/libswk.a >> swk.mk
echo SWKLIBS+=${GI_LIBS} >> swk.mk
diff --git a/gi_x11.c b/gi_x11.c
@@ -26,13 +26,15 @@ static int colors[ColorLast] = { FGCOLOR, BGCOLOR, HICOLOR, TFCOLOR, CCCOLOR };
static Window /* TODO: push into libdraw */
dc_window(DC *dc, int x, int y, int w, int h) {
Window window;
- int screen = DefaultScreen(dc->dpy);
- window = XCreateSimpleWindow(dc->dpy, RootWindow(dc->dpy, screen),
+ window = XCreateSimpleWindow(dc->dpy, RootWindow(dc->dpy, DefaultScreen(dc->dpy)),
x, y, w, h, 1, col[ColorBG], col[ColorFG]);
XSelectInput(dc->dpy, window, EVENTMASK);
XMapWindow(dc->dpy, window);
return window;
}
+static void dc_window_title(Window w, const char *title) {
+ XSetStandardProperties(dc->dpy, window, title, NULL, None, NULL, 0, NULL);
+}
int
swk_gi_fontsize(int sz) {
@@ -54,6 +56,7 @@ swk_gi_init(SwkWindow *w) {
dc_font(dc, FONTNAME);
// TODO: must be dc_window(dc, x, y, w, h, bg, fg)
window = dc_window(dc, 10, 10, w->r.w, w->r.h);
+ dc_window_title(window, w->title);
return swk_gi_fontsize(0);
}
@@ -270,35 +273,53 @@ swk_gi_text(Rect r, const char *text) {
}
void
-swk_gi_img(Rect r, void *img) {
- /* TODO */
+swk_gi_img(Rect r, void *_img) {
+ SwkImage *img = _img;
+ if(img)
+ XPutImage(dc->dpy, dc->canvas, DefaultGC(dc->dpy, 0), img->pub,
+ 0, 0, r.x*fs, r.y*fs, img->w, img->h);
}
-/* image api */
void*
swk_gi_img_new(int w, int h, int color) {
- /* TODO */
- return NULL;
+ SwkImage *img = img_open(NULL);
+ img->w = w*fs;
+ img->h = h*fs;
+ img->bpp = 24;
+ img->priv = NULL;
+ img->name = NULL;
+ img->data = malloc(img->w*img->h*4);
+ memset(img->data, colors[color]&0xff, img->w*img->h*4);
+ img->pub = XCreateImage(dc->dpy, DefaultVisual(dc->dpy, 0), 24, ZPixmap,
+ 0, img->data, img->w, img->h, 32, 0);
+ return img;
}
void*
swk_gi_img_load(const char *str) {
- /* TODO */
- return (void*)1;
+ SwkImage *img = img_open(str);
+ if (img == NULL)
+ return NULL;
+ img->pub = XCreateImage(dc->dpy, DefaultVisual(dc->dpy, 0), 24, ZPixmap,
+ 0, img->data, img->w, img->h, 32, 0);
+ return img;
}
void
swk_gi_img_free(void *s) {
- /* TODO */
+ img_free(s);
}
void
-swk_gi_img_set(void *img, int x, int y, int color) {
- /* TODO */
+swk_gi_img_set(void *_img, int x, int y, int color) {
+ SwkImage *img = _img;
+ int *ptr = img->data;
+ if(ptr) ptr[(y*img->w)+x] = color;
}
int
-swk_gi_img_get(void *img, int x, int y) {
- /* TODO */
- return 0;
+swk_gi_img_get(void *_img, int x, int y) {
+ SwkImage *img = _img;
+ int *ptr = img->data;
+ return ptr?ptr[(y*img->w)+x]:0;
}
diff --git a/image.c b/image.c
@@ -0,0 +1,29 @@
+/* See LICENSE file for copyright and license details. */
+#include "swk.h"
+#include <Imlib2.h>
+
+#define MAXIMGS 32
+//static SwkImage images[MAXIMGS];
+
+SwkImage *
+img_open(const char *str) {
+ int x,y;
+ SwkImage *img = malloc(sizeof(SwkImage));
+ if(str) {
+ img->name = strdup (str);
+ img->priv = imlib_load_image (str);
+ imlib_context_set_image ((Imlib_Image*)img->priv);
+ img->w = imlib_image_get_width();
+ img->h = imlib_image_get_height();
+ img->data = imlib_image_get_data();
+ img->bpp = 24;
+ } else memset(img, 0, sizeof(SwkImage));
+ return img;
+}
+
+void
+img_free(SwkImage *img) {
+ img->ref--; // XXX
+ imlib_context_set_image ((Imlib_Image*)img->priv);
+ imlib_free_image ();
+}
diff --git a/swk.c b/swk.c
@@ -585,7 +585,7 @@ swk_image(SwkEvent *e) {
switch(e->type) {
case EExpose:
swk_gi_img(e->box->r, e->box->data);
-// swk_gi_rect(e->box->r, ColorFG);
+ //swk_gi_rect(e->box->r, ColorFG);
if(e->win->box == e->box) {
Rect r = e->box->r;
swk_gi_line(r.x, r.y+1, r.w, 0, ColorHI);
diff --git a/swk.h b/swk.h
@@ -78,6 +78,17 @@ struct SwkWindow {
SwkEvent _e;
};
+typedef struct {
+ char* name;
+ int ref;
+ void *data;
+ int bpp;
+ int w;
+ int h;
+ void *priv;
+ void *pub;
+} SwkImage;
+
int swk_use(SwkWindow *w);
void swk_update();
void swk_exit();
@@ -166,3 +177,5 @@ void text_sel_mode(Text *t, int enable);
/* text.c widgets */
void swk_text(SwkEvent *e);
+SwkImage *img_open(const char *str);
+void img_close(SwkImage *img);