mirror of
https://gitea.nishi.boats/pyrite-dev/milsko
synced 2025-12-31 06:30:52 +00:00
xshm
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@69 b9cfdab3-6d41-4d17-bbe4-086880011989
This commit is contained in:
@@ -33,7 +33,7 @@ endif
|
||||
ifeq ($(UNIX),1)
|
||||
L_CFLAGS += -DUSE_X11
|
||||
L_OBJS += src/x11.o
|
||||
L_LIBS += -lX11 -lGL
|
||||
L_LIBS += -lX11 -lXrender -lXext -lGL
|
||||
|
||||
GL = -lGL
|
||||
|
||||
|
||||
@@ -64,6 +64,8 @@ int main() {
|
||||
MwNbackground, "#66f",
|
||||
NULL);
|
||||
|
||||
MwVaCreateWidget(MwFrameClass, "frame", window, 50, 50, 300, 300, NULL);
|
||||
|
||||
MwAddUserHandler(window, MwNresizeHandler, resize, NULL);
|
||||
MwAddUserHandler(button, MwNactivateHandler, handler, NULL);
|
||||
MwAddUserHandler(button2, MwNactivateHandler, handler, NULL);
|
||||
|
||||
@@ -3,10 +3,6 @@
|
||||
#define __MW_GDI_H__
|
||||
|
||||
#include <Mw/MachDep.h>
|
||||
|
||||
typedef struct _MwLL * MwLL, MwLLRec;
|
||||
typedef struct _MwLLColor *MwLLColor, MwLLColorRec;
|
||||
|
||||
#include <Mw/TypeDefs.h>
|
||||
#include <Mw/LowLevel.h>
|
||||
|
||||
@@ -28,4 +24,11 @@ struct _MwLLColor {
|
||||
int blue;
|
||||
};
|
||||
|
||||
struct _MwLLPixmap {
|
||||
int width;
|
||||
int height;
|
||||
|
||||
HBITMAP hBitmap;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -7,10 +7,12 @@
|
||||
typedef struct _MwLLHandler *MwLLHandler, MwLLHandlerRec;
|
||||
#ifdef _MILSKO
|
||||
typedef struct _MwLL * MwLL, MwLLRec;
|
||||
typedef struct _MwLLColor *MwLLColor, MwLLColorRec;
|
||||
typedef struct _MwLLColor * MwLLColor, MwLLColorRec;
|
||||
typedef struct _MwLLPixmap *MwLLPixmap, MwLLPixmapRec;
|
||||
#else
|
||||
typedef void* MwLL;
|
||||
typedef void* MwLLColor;
|
||||
typedef void* MwLLPixmap;
|
||||
#endif
|
||||
|
||||
#ifdef _MILSKO
|
||||
@@ -61,6 +63,10 @@ MWDECL int MwLLPending(MwLL handle);
|
||||
MWDECL void MwLLNextEvent(MwLL handle);
|
||||
MWDECL void MwLLSleep(int ms);
|
||||
|
||||
MWDECL MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int height);
|
||||
MWDECL void MwLLDestroyPixmap(MwLLPixmap pixmap);
|
||||
MWDECL void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/ipc.h>
|
||||
#include <sys/shm.h>
|
||||
#endif
|
||||
|
||||
#if defined(_MILSKO) && defined(_WIN32)
|
||||
|
||||
@@ -3,15 +3,13 @@
|
||||
#define __MW_X11_H__
|
||||
|
||||
#include <Mw/MachDep.h>
|
||||
|
||||
typedef struct _MwLL * MwLL, MwLLRec;
|
||||
typedef struct _MwLLColor *MwLLColor, MwLLColorRec;
|
||||
|
||||
#include <Mw/TypeDefs.h>
|
||||
#include <Mw/LowLevel.h>
|
||||
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xutil.h>
|
||||
#include <X11/extensions/XShm.h>
|
||||
#include <X11/extensions/Xrender.h>
|
||||
|
||||
struct _MwLL {
|
||||
Display* display;
|
||||
@@ -31,4 +29,14 @@ struct _MwLLColor {
|
||||
int blue;
|
||||
};
|
||||
|
||||
struct _MwLLPixmap {
|
||||
int width;
|
||||
int height;
|
||||
|
||||
int use_shm;
|
||||
XShmSegmentInfo shm;
|
||||
Display* display;
|
||||
XImage* image;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
20
src/frame.c
20
src/frame.c
@@ -1,8 +1,25 @@
|
||||
/* $Id$ */
|
||||
#include <Mw/Milsko.h>
|
||||
|
||||
MwLLPixmap pixmap;
|
||||
|
||||
static void create(MwWidget handle) {
|
||||
MwSetDefault(handle);
|
||||
|
||||
int y, x;
|
||||
unsigned char* dat = malloc(640 * 480 * 3);
|
||||
for(y = 0; y < 480; y++) {
|
||||
for(x = 0; x < 640; x++) {
|
||||
unsigned char* px = &dat[(y * 640 + x) * 3];
|
||||
double c = (double)x / 640 * 255 / 2 + (double)y / 480 * 255 / 2;
|
||||
|
||||
px[0] = c;
|
||||
px[1] = 0;
|
||||
px[2] = 0;
|
||||
}
|
||||
}
|
||||
pixmap = MwLLCreatePixmap(handle->lowlevel, dat, 640, 480);
|
||||
free(dat);
|
||||
}
|
||||
|
||||
static void draw(MwWidget handle) {
|
||||
@@ -16,7 +33,8 @@ static void draw(MwWidget handle) {
|
||||
|
||||
MwDrawFrameEx(handle, &r, base, 1, 1);
|
||||
MwDrawFrameEx(handle, &r, base, 0, 1);
|
||||
MwDrawRect(handle, &r, base);
|
||||
MwLLDrawPixmap(handle->lowlevel, &r, pixmap);
|
||||
// MwDrawRect(handle, &r, base);
|
||||
|
||||
MwLLFreeColor(base);
|
||||
}
|
||||
|
||||
57
src/gdi.c
57
src/gdi.c
@@ -45,6 +45,7 @@ static LRESULT CALLBACK wndproc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) {
|
||||
} else if(msg == WM_SIZE) {
|
||||
MwLLDispatch(u->ll, resize);
|
||||
} else if(msg == WM_ERASEBKGND) {
|
||||
return 1;
|
||||
} else if(msg == WM_NCHITTEST) {
|
||||
return HTCLIENT;
|
||||
} else if(msg == WM_DESTROY) {
|
||||
@@ -210,3 +211,59 @@ void MwLLNextEvent(MwLL handle) {
|
||||
void MwLLSleep(int ms) {
|
||||
Sleep(ms);
|
||||
}
|
||||
|
||||
MwLLPixmap MwLLCreatePixmap(MwLL handle, unsigned char* data, int width, int height) {
|
||||
MwLLPixmap r = malloc(sizeof(*r));
|
||||
HDC dc = GetDC(handle->hWnd);
|
||||
BITMAPINFOHEADER bmih;
|
||||
RGBQUAD* quad = NULL;
|
||||
int y, x;
|
||||
|
||||
r->width = width;
|
||||
r->height = height;
|
||||
|
||||
bmih.biSize = sizeof(bmih);
|
||||
bmih.biWidth = width;
|
||||
bmih.biHeight = -(LONG)height;
|
||||
bmih.biPlanes = 1;
|
||||
bmih.biBitCount = 32;
|
||||
bmih.biCompression = BI_RGB;
|
||||
bmih.biSizeImage = 0;
|
||||
bmih.biXPelsPerMeter = 0;
|
||||
bmih.biYPelsPerMeter = 0;
|
||||
bmih.biClrUsed = 0;
|
||||
bmih.biClrImportant = 0;
|
||||
|
||||
r->hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, (void**)&quad, NULL, (DWORD)0);
|
||||
|
||||
for(y = 0; y < height; y++) {
|
||||
for(x = 0; x < width; x++) {
|
||||
RGBQUAD* q = &quad[y * width + x];
|
||||
unsigned char* px = &data[(y * width + x) * 3];
|
||||
q->rgbRed = px[0];
|
||||
q->rgbGreen = px[1];
|
||||
q->rgbBlue = px[2];
|
||||
}
|
||||
}
|
||||
|
||||
ReleaseDC(handle->hWnd, dc);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void MwLLDestroyPixmap(MwLLPixmap pixmap) {
|
||||
DeleteObject(pixmap->hBitmap);
|
||||
|
||||
free(pixmap);
|
||||
}
|
||||
|
||||
void MwLLDrawPixmap(MwLL handle, MwRect* rect, MwLLPixmap pixmap) {
|
||||
HDC hmdc = CreateCompatibleDC(handle->hDC);
|
||||
|
||||
SelectObject(hmdc, pixmap->hBitmap);
|
||||
|
||||
SetStretchBltMode(handle->hDC, HALFTONE);
|
||||
StretchBlt(handle->hDC, rect->x, rect->y, rect->width, rect->height, hmdc, 0, 0, pixmap->width, pixmap->height, SRCCOPY);
|
||||
|
||||
DeleteDC(hmdc);
|
||||
}
|
||||
|
||||
64
src/x11.c
64
src/x11.c
@@ -25,6 +25,8 @@ MwLL MwLLCreate(MwLL parent, int x, int y, int width, int height) {
|
||||
|
||||
r->gc = XCreateGC(r->display, r->window, 0, 0);
|
||||
|
||||
XSetGraphicsExposures(r->display, r->gc, False);
|
||||
|
||||
XSelectInput(r->display, r->window, mask);
|
||||
XMapWindow(r->display, r->window);
|
||||
|
||||
@@ -137,3 +139,65 @@ void MwLLSleep(int ms) {
|
||||
void MwLLSetTitle(MwLL handle, const char* title) {
|
||||
XSetStandardProperties(handle->display, handle->window, title, "Mw Widget Toolkit", None, (char**)NULL, 0, NULL);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user