changeset: 72:ecce56149925
tag: tip
user: pancake <pancake_AT_nopcode.org>
date: Wed Sep 01 00:11:22 2010 +0200
files: Makefile gi_x11.c image.c swk.c swk.h
description:
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
diff -r be5c51a4f016 -r ecce56149925 Makefile
--- a/Makefile Mon Aug 30 22:53:00 2010 +0200
+++ b/Makefile Wed Sep 01 00:11:22 2010 +0200
@@ -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 @@
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 @@
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 @@
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 -r be5c51a4f016 -r ecce56149925 gi_x11.c
--- a/gi_x11.c Mon Aug 30 22:53:00 2010 +0200
+++ b/gi_x11.c Wed Sep 01 00:11:22 2010 +0200
@@ -26,13 +26,15 @@
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 @@
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 @@
}
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 -r be5c51a4f016 -r ecce56149925 image.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/image.c Wed Sep 01 00:11:22 2010 +0200
@@ -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 -r be5c51a4f016 -r ecce56149925 swk.c
--- a/swk.c Mon Aug 30 22:53:00 2010 +0200
+++ b/swk.c Wed Sep 01 00:11:22 2010 +0200
@@ -585,7 +585,7 @@
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 -r be5c51a4f016 -r ecce56149925 swk.h
--- a/swk.h Mon Aug 30 22:53:00 2010 +0200
+++ b/swk.h Wed Sep 01 00:11:22 2010 +0200
@@ -78,6 +78,17 @@
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 @@
/* text.c widgets */
void swk_text(SwkEvent *e);
+SwkImage *img_open(const char *str);
+void img_close(SwkImage *img);
Received on Wed Sep 01 2010 - 00:14:35 CEST
This archive was generated by hypermail 2.2.0 : Wed Sep 01 2010 - 00:24:04 CEST