fix pixmap leak

git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@82 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
Nishi 2025-09-30 17:47:03 +00:00
commit 8ea2d4a50a
15 changed files with 9440 additions and 1337 deletions

View file

@ -7,7 +7,7 @@ CFLAGS = -Wall -Wextra -Iinclude
LDFLAGS =
LIBS =
L_CFLAGS = $(CFLAGS) -fPIC -D_MILSKO
L_CFLAGS = $(CFLAGS) -fPIC -D_MILSKO -DHAVE_CONFIG_H
L_LDFLAGS = $(LDFLAGS)
L_LIBS = $(LIBS)
@ -15,7 +15,7 @@ E_CFLAGS = $(CFLAGS)
E_LDFLAGS = $(LDFLAGS) -Lsrc
E_LIBS = $(LIBS) -lMw
L_OBJS = src/ds.o src/core.o src/default.o src/draw.o src/lowlevel.o src/font.o
L_OBJS = src/ds.o src/core.o src/default.o src/draw.o src/lowlevel.o src/font.o src/image.o
L_OBJS += src/window.o src/button.o src/opengl.o src/frame.o
ifeq ($(TARGET),NetBSD)
@ -53,7 +53,7 @@ SO = .dll
EXEC = .exe
endif
EXAMPLES = examples/example$(EXEC) examples/rotate$(EXEC) examples/opengl$(EXEC)
EXAMPLES = examples/example$(EXEC) examples/rotate$(EXEC) examples/opengl$(EXEC) examples/image$(EXEC)
.PHONY: all format clean lib examples
@ -62,8 +62,8 @@ lib: src/libMw$(SO)
examples: $(EXAMPLES)
format:
clang-format --verbose -i $(shell find src include examples tools -name "*.c" -or -name "*.h")
perltidy -b -bext='/' --paren-tightness=2 $(shell find tools -name "*.pl")
clang-format --verbose -i `find src include examples tools "(" -name "*.c" -or -name "*.h" ")" -and -not -name "stb_*.h"`
perltidy -b -bext='/' --paren-tightness=2 `find tools -name "*.pl"`
src/libMw$(SO): $(L_OBJS)
$(CC) $(L_LDFLAGS) -shared -o $@ $^ $(L_LIBS)
@ -81,4 +81,4 @@ examples/%.o: examples/%.c
$(CC) $(E_CFLAGS) -c -o $@ $<
clean:
rm -f src/*.dll src/*.so src/*.a src/*.lib */*.o examples/*.exe $(EXAMPLES)
rm -f src/*.dll src/*.so src/*.a src/*.lib */*.o */*/*.o examples/*.exe $(EXAMPLES)

16
examples/image.c Normal file
View file

@ -0,0 +1,16 @@
/* $Id$ */
#include <Mw/Milsko.h>
int main() {
MwWidget window = MwVaCreateWidget(MwWindowClass, "window", NULL, 0, 0, 500, 500,
MwNtitle, "image button",
NULL);
MwWidget button = MwCreateWidget(MwButtonClass, "button", window, 50, 50, 400, 400);
MwLLPixmap px = MwLoadImage(button, "examples/picture.png");
MwVaApply(button,
MwNpixmap, px,
NULL);
MwLoop(window);
}

BIN
examples/picture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

View file

@ -106,6 +106,14 @@ MWDECL void MwSetInteger(MwWidget handle, const char* key, int n);
*/
MWDECL void MwSetText(MwWidget handle, const char* key, const char* value);
/*!
* %brief Sets a void pointer property
* %param handle Widget
* %param key Key
* %param value Value
*/
MWDECL void MwSetVoid(MwWidget handle, const char* key, void* value);
/*!
* %brief Gets the integer property
* %param handle Widget
@ -122,6 +130,14 @@ MWDECL int MwGetInteger(MwWidget handle, const char* key);
*/
MWDECL const char* MwGetText(MwWidget handle, const char* key);
/*!
* %brief Gets the void pointer property
* %param handle Widget
* %param key Key
* %return Value
*/
MWDECL void* MwGetVoid(MwWidget handle, const char* key);
/*!
* %brief Sets the default property
* %param handle Widget

View file

@ -60,6 +60,14 @@ MWDECL void MwDrawFrameEx(MwWidget handle, MwRect* rect, MwLLColor color, int in
*/
MWDECL void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor color);
/*!
* %brief Creates a pixmap from image
* %param handle Widget
* %param path Path
* %return Pixmap
*/
MWDECL MwLLPixmap MwLoadImage(MwWidget handle, const char* path);
#ifdef __cplusplus
}
#endif

View file

@ -16,6 +16,8 @@
#define MwNbackground "Sbackground"
#define MwNforeground "Sforeground"
#define MwNpixmap "Vpixmap"
#define MwNactivateHandler "Cactivate"
#define MwNresizeHandler "Cresize"
#define MwNtickHandler "Ctick"

View file

@ -14,6 +14,7 @@ typedef struct _MwRect MwRect;
typedef struct _MwIntegerKeyValue MwIntegerKeyValue;
typedef struct _MwTextKeyValue MwTextKeyValue;
typedef struct _MwUserHandlerKeyValue MwUserHandlerKeyValue;
typedef struct _MwVoidKeyValue MwVoidKeyValue;
typedef struct _MwFont MwFont;
#ifdef _MILSKO
typedef struct _MwWidget *MwWidget, MwWidgetRec;
@ -56,6 +57,11 @@ struct _MwUserHandlerKeyValue {
MwUserHandler value;
};
struct _MwVoidKeyValue {
char* key;
void* value;
};
#ifdef _MILSKO
struct _MwWidget {
char* name;
@ -74,6 +80,7 @@ struct _MwWidget {
MwIntegerKeyValue* integer;
MwTextKeyValue* text;
MwUserHandlerKeyValue* handler;
MwVoidKeyValue* data;
};
#endif

View file

@ -19,11 +19,15 @@
struct _MwLL {
Display* display;
Window window;
Pixmap pixmap;
GC gc;
Colormap colormap;
void* user;
Atom wm_delete;
unsigned int width;
unsigned int height;
MwLLHandler handler;
};

View file

@ -11,6 +11,7 @@ static void draw(MwWidget handle) {
MwLLColor base = MwParseColor(handle, MwGetText(handle, MwNbackground));
MwLLColor text = MwParseColor(handle, MwGetText(handle, MwNforeground));
const char* str = MwGetText(handle, MwNtext);
MwLLPixmap px = MwGetVoid(handle, MwNpixmap);
if(str == NULL) str = "";
@ -22,10 +23,33 @@ static void draw(MwWidget handle) {
MwDrawFrame(handle, &r, base, handle->pressed);
MwDrawRect(handle, &r, base);
point.x = r.x + r.width / 2;
point.y = r.x + r.height / 2;
if(px != NULL) {
int ow = r.width;
int oh = r.height;
MwDrawText(handle, &point, str, text);
double sw = (double)ow / px->width;
double sh = (double)oh / px->height;
if(sw < sh) {
r.width = px->width * sw;
r.height = px->height * sw;
} else {
r.width = px->width * sh;
r.height = px->height * sh;
}
r.width -= 10;
r.height -= 10;
r.x += (double)(ow - r.width) / 2;
r.y += (double)(oh - r.height) / 2;
MwLLDrawPixmap(handle->lowlevel, &r, px);
} else {
point.x = r.x + r.width / 2;
point.y = r.x + r.height / 2;
MwDrawText(handle, &point, str, text);
}
MwLLFreeColor(text);
MwLLFreeColor(base);

View file

@ -57,10 +57,12 @@ MwWidget MwCreateWidget(MwClass widget_class, const char* name, MwWidget parent,
sh_new_strdup(h->text);
sh_new_strdup(h->integer);
sh_new_strdup(h->handler);
sh_new_strdup(h->data);
shdefault(h->integer, -1);
shdefault(h->text, NULL);
shdefault(h->handler, NULL);
shdefault(h->data, NULL);
MwDispatch(h, create);
@ -122,6 +124,7 @@ void MwDestroyWidget(MwWidget handle) {
}
shfree(handle->text);
shfree(handle->handler);
shfree(handle->data);
free(handle);
}
@ -182,6 +185,10 @@ void MwSetText(MwWidget handle, const char* key, const char* value) {
}
}
void MwSetVoid(MwWidget handle, const char* key, void* value) {
shput(handle->data, key, value);
}
int MwGetInteger(MwWidget handle, const char* key) {
if(strcmp(key, MwNx) == 0 || strcmp(key, MwNy) == 0 || strcmp(key, MwNwidth) == 0 || strcmp(key, MwNheight) == 0) {
int x, y;
@ -203,6 +210,10 @@ const char* MwGetText(MwWidget handle, const char* key) {
return shget(handle->text, key);
}
void* MwGetVoid(MwWidget handle, const char* key) {
return shget(handle->data, key);
}
void MwVaApply(MwWidget handle, ...) {
va_list va;
@ -228,6 +239,9 @@ void MwVaListApply(MwWidget handle, va_list va) {
shput(handle->handler, key, h);
ind = shgeti(handle->handler, key);
handle->handler[ind].user_data = NULL;
} else if(key[0] == 'V') {
void* v = va_arg(va, void*);
MwSetVoid(handle, key, v);
}
}
}

View file

@ -1,6 +1,8 @@
/* $Id$ */
#include <Mw/Milsko.h>
#include <stb_image.h>
static int hex(const char* txt, int len) {
int i;
int r = 0;
@ -145,3 +147,17 @@ void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor col
sx += fw * sc;
}
}
MwLLPixmap MwLoadImage(MwWidget handle, const char* path) {
int width, height, ch;
unsigned char* rgb = stbi_load(path, &width, &height, &ch, 3);
MwLLPixmap px;
if(rgb == NULL) return NULL;
px = MwLLCreatePixmap(handle->lowlevel, rgb, width, height);
free(rgb);
return px;
}

3
src/image.c Normal file
View file

@ -0,0 +1,3 @@
/* $Id$ */
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

2443
src/stb_ds.h generated

File diff suppressed because it is too large Load diff

7988
src/stb_image.h Normal file

File diff suppressed because it is too large Load diff

View file

@ -3,6 +3,22 @@
static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask;
static void create_pixmap(MwLL handle) {
XWindowAttributes attr;
int x, y;
unsigned int w, h;
MwLLGetXYWH(handle, &x, &y, &w, &h);
XGetWindowAttributes(handle->display, handle->window, &attr);
handle->pixmap = XCreatePixmap(handle->display, handle->window, w, h, attr.depth);
}
static void destroy_pixmap(MwLL handle) {
XFreePixmap(handle->display, handle->pixmap);
}
MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
MwLL r;
Window p;
@ -18,13 +34,19 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
r->display = parent->display;
p = parent->window;
}
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
r->width = width;
r->height = height;
r->colormap = DefaultColormap(r->display, XDefaultScreen(r->display));
r->wm_delete = XInternAtom(r->display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(r->display, r->window, &r->wm_delete, 1);
r->gc = XCreateGC(r->display, r->window, 0, 0);
create_pixmap(r);
XSetGraphicsExposures(r->display, r->gc, False);
XSelectInput(r->display, r->window, mask);
@ -36,6 +58,7 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
void MwLLDestroy(MwLL handle) {
MwLLDestroyCommon(handle);
destroy_pixmap(handle);
XFreeGC(handle->display, handle->gc);
XDestroyWindow(handle->display, handle->window);
free(handle);
@ -51,7 +74,7 @@ void MwLLPolygon(MwLL handle, MwPoint* points, int points_count, MwLLColor color
p[i].x = points[i].x;
p[i].y = points[i].y;
}
XFillPolygon(handle->display, handle->window, handle->gc, p, points_count, Convex, CoordModeOrigin);
XFillPolygon(handle->display, handle->pixmap, handle->gc, p, points_count, Convex, CoordModeOrigin);
free(p);
}
@ -110,25 +133,43 @@ int MwLLPending(MwLL handle) {
void MwLLNextEvent(MwLL handle) {
XEvent ev;
while(XCheckTypedWindowEvent(handle->display, handle->window, ClientMessage, &ev) || XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
int render = 0;
if(ev.type == Expose) {
MwLLDispatch(handle, draw);
render = 1;
} else if(ev.type == ButtonPress) {
if(ev.xbutton.button == Button1) {
MwLLDispatch(handle, down);
MwLLDispatch(handle, draw);
render = 1;
}
} else if(ev.type == ButtonRelease) {
if(ev.xbutton.button == Button1) {
MwLLDispatch(handle, up);
MwLLDispatch(handle, draw);
render = 1;
}
} else if(ev.type == ConfigureNotify) {
MwLLDispatch(handle, resize);
if(handle->width != (unsigned int)ev.xconfigure.width || handle->height != (unsigned int)ev.xconfigure.height) {
destroy_pixmap(handle);
create_pixmap(handle);
render = 1;
}
handle->width = ev.xconfigure.width;
handle->height = ev.xconfigure.height;
} else if(ev.type == ClientMessage) {
if(ev.xclient.data.l[0] == (long)handle->wm_delete) {
MwLLDispatch(handle, close);
}
}
if(render) {
int x, y;
unsigned int w, h;
MwLLGetXYWH(handle, &x, &y, &w, &h);
MwLLDispatch(handle, draw);
XCopyArea(handle->display, handle->pixmap, handle->window, handle->gc, 0, 0, w, h, 0, 0);
}
}
}
@ -232,11 +273,14 @@ void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
}
src = XRenderCreatePicture(handle->display, px, format, 0, &attr);
dest = XRenderCreatePicture(handle->display, handle->window, format, 0, &attr);
dest = XRenderCreatePicture(handle->display, handle->pixmap, format, 0, &attr);
XRenderSetPictureTransform(handle->display, src, &m);
XRenderComposite(handle->display, PictOpSrc, src, 0, dest, 0, 0, 0, 0, rect->x, rect->y, rect->width, rect->height);
XRenderFreePicture(handle->display, src);
XRenderFreePicture(handle->display, dest);
XFreePixmap(handle->display, px);
}
}