2025-09-27 14:29:44 +00:00
|
|
|
/* $Id$ */
|
2025-09-29 00:10:20 +00:00
|
|
|
#include <Mw/Milsko.h>
|
2025-09-27 14:29:44 +00:00
|
|
|
|
2025-09-28 04:04:30 +00:00
|
|
|
static unsigned long mask = ExposureMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask;
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
|
2025-09-29 00:21:38 +00:00
|
|
|
MwLL r;
|
|
|
|
|
Window p;
|
2025-09-28 02:51:15 +00:00
|
|
|
|
2025-09-28 22:00:17 +00:00
|
|
|
r = malloc(sizeof(*r));
|
2025-09-28 21:55:50 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLCreateCommon(r);
|
2025-09-28 04:06:26 +00:00
|
|
|
|
2025-09-28 04:07:41 +00:00
|
|
|
if(parent == NULL) {
|
2025-09-28 02:51:15 +00:00
|
|
|
r->display = XOpenDisplay(NULL);
|
2025-09-28 04:07:41 +00:00
|
|
|
p = XRootWindow(r->display, XDefaultScreen(r->display));
|
|
|
|
|
} else {
|
2025-09-28 02:51:15 +00:00
|
|
|
r->display = parent->display;
|
2025-09-28 04:07:41 +00:00
|
|
|
p = parent->window;
|
2025-09-28 02:51:15 +00:00
|
|
|
}
|
2025-09-29 07:05:09 +00:00
|
|
|
r->window = XCreateSimpleWindow(r->display, p, x, y, width, height, 0, 0, WhitePixel(r->display, XDefaultScreen(r->display)));
|
|
|
|
|
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);
|
2025-09-28 09:04:35 +00:00
|
|
|
|
|
|
|
|
r->gc = XCreateGC(r->display, r->window, 0, 0);
|
2025-09-28 02:51:15 +00:00
|
|
|
|
2025-09-30 00:52:43 +00:00
|
|
|
XSetGraphicsExposures(r->display, r->gc, False);
|
|
|
|
|
|
2025-09-28 04:04:30 +00:00
|
|
|
XSelectInput(r->display, r->window, mask);
|
2025-09-28 02:51:15 +00:00
|
|
|
XMapWindow(r->display, r->window);
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLDestroy(MwLL handle) {
|
|
|
|
|
MwLLDestroyCommon(handle);
|
2025-09-28 22:00:17 +00:00
|
|
|
|
2025-09-28 04:04:30 +00:00
|
|
|
XFreeGC(handle->display, handle->gc);
|
|
|
|
|
XDestroyWindow(handle->display, handle->window);
|
|
|
|
|
free(handle);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLPolygon(MwLL handle, MwPoint* points, int points_count, MwLLColor color) {
|
2025-09-28 04:07:41 +00:00
|
|
|
int i;
|
2025-09-28 04:04:30 +00:00
|
|
|
XPoint* p = malloc(sizeof(*p) * points_count);
|
|
|
|
|
|
|
|
|
|
XSetForeground(handle->display, handle->gc, color->pixel);
|
|
|
|
|
|
2025-09-28 04:07:41 +00:00
|
|
|
for(i = 0; i < points_count; i++) {
|
2025-09-28 04:04:30 +00:00
|
|
|
p[i].x = points[i].x;
|
|
|
|
|
p[i].y = points[i].y;
|
|
|
|
|
}
|
|
|
|
|
XFillPolygon(handle->display, handle->window, handle->gc, p, points_count, Convex, CoordModeOrigin);
|
|
|
|
|
|
|
|
|
|
free(p);
|
2025-09-28 02:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLColor MwLLAllocColor(MwLL handle, int r, int g, int b) {
|
|
|
|
|
MwLLColor c = malloc(sizeof(*c));
|
2025-09-29 00:10:20 +00:00
|
|
|
XColor xc;
|
2025-09-28 14:36:24 +00:00
|
|
|
|
|
|
|
|
if(r > 255) r = 255;
|
|
|
|
|
if(g > 255) g = 255;
|
|
|
|
|
if(b > 255) b = 255;
|
|
|
|
|
if(r < 0) r = 0;
|
|
|
|
|
if(g < 0) g = 0;
|
|
|
|
|
if(b < 0) b = 0;
|
|
|
|
|
|
2025-09-28 04:07:41 +00:00
|
|
|
xc.red = 256 * r;
|
2025-09-28 02:51:15 +00:00
|
|
|
xc.green = 256 * g;
|
2025-09-28 04:07:41 +00:00
|
|
|
xc.blue = 256 * b;
|
2025-09-28 02:51:15 +00:00
|
|
|
XAllocColor(handle->display, handle->colormap, &xc);
|
|
|
|
|
|
|
|
|
|
c->pixel = xc.pixel;
|
2025-09-28 13:53:35 +00:00
|
|
|
c->red = r;
|
|
|
|
|
c->green = g;
|
|
|
|
|
c->blue = b;
|
2025-09-28 02:51:15 +00:00
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLGetXYWH(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) {
|
2025-09-28 09:04:35 +00:00
|
|
|
Window root;
|
|
|
|
|
unsigned int border, depth;
|
|
|
|
|
|
|
|
|
|
XGetGeometry(handle->display, handle->window, &root, x, y, w, h, &border, &depth);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLSetXY(MwLL handle, int x, int y) {
|
2025-09-28 09:04:35 +00:00
|
|
|
XMoveWindow(handle->display, handle->window, x, y);
|
2025-09-28 04:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLSetWH(MwLL handle, int w, int h) {
|
2025-09-28 09:04:35 +00:00
|
|
|
XResizeWindow(handle->display, handle->window, w, h);
|
2025-09-28 08:45:58 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLFreeColor(MwLLColor color) {
|
2025-09-28 02:51:15 +00:00
|
|
|
free(color);
|
|
|
|
|
}
|
2025-09-28 04:04:30 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
int MwLLPending(MwLL handle) {
|
2025-09-28 04:04:30 +00:00
|
|
|
XEvent ev;
|
2025-09-29 07:05:09 +00:00
|
|
|
if(XCheckTypedWindowEvent(handle->display, handle->window, ClientMessage, &ev) || XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
|
2025-09-28 04:04:30 +00:00
|
|
|
XPutBackEvent(handle->display, &ev);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-09-28 04:09:25 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLNextEvent(MwLL handle) {
|
2025-09-28 04:09:25 +00:00
|
|
|
XEvent ev;
|
2025-09-29 07:05:09 +00:00
|
|
|
while(XCheckTypedWindowEvent(handle->display, handle->window, ClientMessage, &ev) || XCheckWindowEvent(handle->display, handle->window, mask, &ev)) {
|
2025-09-28 09:55:42 +00:00
|
|
|
if(ev.type == Expose) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLDispatch(handle, draw);
|
2025-09-28 22:10:42 +00:00
|
|
|
} else if(ev.type == ButtonPress) {
|
|
|
|
|
if(ev.xbutton.button == Button1) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLDispatch(handle, down);
|
|
|
|
|
MwLLDispatch(handle, draw);
|
2025-09-28 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
} else if(ev.type == ButtonRelease) {
|
|
|
|
|
if(ev.xbutton.button == Button1) {
|
2025-09-29 00:04:04 +00:00
|
|
|
MwLLDispatch(handle, up);
|
|
|
|
|
MwLLDispatch(handle, draw);
|
2025-09-28 22:10:42 +00:00
|
|
|
}
|
2025-09-29 05:58:28 +00:00
|
|
|
} else if(ev.type == ConfigureNotify) {
|
|
|
|
|
MwLLDispatch(handle, resize);
|
2025-09-29 07:05:09 +00:00
|
|
|
} else if(ev.type == ClientMessage) {
|
|
|
|
|
if(ev.xclient.data.l[0] == (long)handle->wm_delete) {
|
|
|
|
|
MwLLDispatch(handle, close);
|
|
|
|
|
}
|
2025-09-28 09:55:42 +00:00
|
|
|
}
|
2025-09-28 04:09:25 +00:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-28 08:37:12 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLSleep(int ms) {
|
2025-09-28 08:37:12 +00:00
|
|
|
usleep(ms * 1000);
|
|
|
|
|
}
|
2025-09-28 09:08:53 +00:00
|
|
|
|
2025-09-29 00:04:04 +00:00
|
|
|
void MwLLSetTitle(MwLL handle, const char* title) {
|
|
|
|
|
XSetStandardProperties(handle->display, handle->window, title, "Mw Widget Toolkit", None, (char**)NULL, 0, NULL);
|
2025-09-28 09:08:53 +00:00
|
|
|
}
|
2025-09-30 00:52:43 +00:00
|
|
|
|
|
|
|
|
MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int height) {
|
|
|
|
|
MwLLPixmap r = malloc(sizeof(*r));
|
|
|
|
|
char* d = malloc(4 * width * height);
|
|
|
|
|
int y, x;
|
|
|
|
|
|
|
|
|
|
r->width = width;
|
|
|
|
|
r->height = height;
|
|
|
|
|
r->display = handle->display;
|
|
|
|
|
r->use_shm = XShmQueryExtension(handle->display) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
if(r->use_shm) {
|
|
|
|
|
r->image = XShmCreateImage(handle->display, DefaultVisual(handle->display, DefaultScreen(handle->display)), 24, ZPixmap, NULL, &r->shm, width, height);
|
|
|
|
|
free(d);
|
|
|
|
|
|
|
|
|
|
r->shm.shmid = shmget(IPC_PRIVATE, r->image->bytes_per_line * height, IPC_CREAT | 0777);
|
|
|
|
|
r->shm.shmaddr = d = r->image->data = shmat(r->shm.shmid, 0, 0);
|
|
|
|
|
r->shm.readOnly = False;
|
|
|
|
|
XShmAttach(handle->display, &r->shm);
|
|
|
|
|
} else {
|
|
|
|
|
r->image = XCreateImage(handle->display, DefaultVisual(handle->display, DefaultScreen(handle->display)), 24, ZPixmap, 0, d, width, height, 32, width * 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(y = 0; y < height; y++) {
|
|
|
|
|
for(x = 0; x < height; x++) {
|
|
|
|
|
unsigned char* px = &data[(y * width + x) * 3];
|
|
|
|
|
unsigned long p = 0;
|
|
|
|
|
p <<= 8;
|
|
|
|
|
p |= px[0];
|
|
|
|
|
p <<= 8;
|
|
|
|
|
p |= px[1];
|
|
|
|
|
p <<= 8;
|
|
|
|
|
p |= px[2];
|
|
|
|
|
XPutPixel(r->image, x, y, p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MwLLDestroyPixmap(MwLLPixmap pixmap) {
|
|
|
|
|
if(pixmap->use_shm) {
|
|
|
|
|
XShmDetach(pixmap->display, &pixmap->shm);
|
|
|
|
|
}
|
|
|
|
|
XDestroyImage(pixmap->image);
|
|
|
|
|
if(pixmap->use_shm) {
|
|
|
|
|
shmdt(pixmap->shm.shmaddr);
|
|
|
|
|
shmctl(pixmap->shm.shmid, IPC_RMID, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(pixmap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
|
|
|
|
|
if(pixmap->image != NULL) {
|
|
|
|
|
if(pixmap->use_shm) {
|
|
|
|
|
XShmPutImage(handle->display, handle->window, handle->gc, pixmap->image, 0, 0, rect->x, rect->y, rect->width, rect->height, False);
|
|
|
|
|
} else {
|
|
|
|
|
XPutImage(handle->display, handle->window, handle->gc, pixmap->image, 0, 0, rect->x, rect->y, rect->width, rect->height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|