diff --git a/examples/basic/rotate.c b/examples/basic/rotate.c index c3b85d7..835993d 100644 --- a/examples/basic/rotate.c +++ b/examples/basic/rotate.c @@ -56,13 +56,13 @@ void resize(MwWidget w, void* user, void* client) { int main() { MwWidget window; - int i; + int i; MwLibraryInit(); window = MwVaCreateWidget(MwWindowClass, "window", NULL, MwDEFAULT, MwDEFAULT, (ww = 500), (wh = 500), - MwNtitle, "rotate", - NULL); + MwNtitle, "rotate", + NULL); for(i = 0; i < (int)(sizeof(buttons) / sizeof(buttons[0])); i++) { const char* color = ""; diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index c3f7fe9..49c1cc7 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -198,8 +198,6 @@ MWDECL void (*MwLLMakePopup)(MwLL handle, MwLL parent); MWDECL void (*MwLLBeginStateChange)(MwLL handle); MWDECL void (*MwLLEndStateChange)(MwLL handle); -MWDECL void (*MwLLSetBackground)(MwLL handle, MwLLColor color); - MWDECL void (*MwLLFocus)(MwLL handle); MWDECL void (*MwLLGrabPointer)(MwLL handle, int toggle); diff --git a/include/Mw/LowLevel/Wayland.h b/include/Mw/LowLevel/Wayland.h index 843c604..c502a53 100644 --- a/include/Mw/LowLevel/Wayland.h +++ b/include/Mw/LowLevel/Wayland.h @@ -7,6 +7,7 @@ #ifndef __MW_LOWLEVEL_WAYLAND_H__ #define __MW_LOWLEVEL_WAYLAND_H__ +#include "Mw/BaseTypes.h" #include #include #include @@ -63,6 +64,9 @@ struct _MwLLWayland { struct wl_shm* shm; struct wl_registry_listener registry_listener; + MwU8* mapped_buffer; + int mapped_buffer_size; + struct { const char* key; wl_setup_func* value; @@ -74,6 +78,7 @@ struct _MwLLWayland { }* wl_protocol_map; MwBool configured; + MwBool force_redraw; MwU32 x, y, ww, wh; }; diff --git a/src/backend/call.c b/src/backend/call.c index bace30c..de4b7f0 100644 --- a/src/backend/call.c +++ b/src/backend/call.c @@ -44,8 +44,6 @@ \ MwLLBeginStateChange = MwLLBeginStateChangeImpl; \ MwLLEndStateChange = MwLLEndStateChangeImpl; \ -\ - MwLLSetBackground = MwLLSetBackgroundImpl; \ \ MwLLFocus = MwLLFocusImpl; \ MwLLGrabPointer = MwLLGrabPointerImpl; \ diff --git a/src/backend/gdi.c b/src/backend/gdi.c index f466dfd..d366d6b 100644 --- a/src/backend/gdi.c +++ b/src/backend/gdi.c @@ -342,11 +342,6 @@ static void MwLLFreeColorImpl(MwLLColor color) { free(color); } -static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) { - (void)handle; - (void)color; -} - static void MwLLGetXYWHImpl(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) { RECT rc; diff --git a/src/backend/wayland.c b/src/backend/wayland.c index 18d4a18..d14cada 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -13,6 +13,8 @@ #include #include "../../external/stb_ds.h" +#include "Mw/BaseTypes.h" +#include "Mw/LowLevel.h" #include @@ -62,7 +64,6 @@ static void buffer_setup(struct _MwLLWayland* wayland) { int fd; MwU32 size = wayland->ww * wayland->wh * 4; char temp_name[] = "/tmp/milsko-wl-shm-XXXXXX"; - void* map; fd = mkstemp(temp_name); @@ -80,8 +81,9 @@ static void buffer_setup(struct _MwLLWayland* wayland) { } lseek(fd, 0, SEEK_SET); - map = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); - memset(map, 0xFF, size); + wayland->mapped_buffer = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); + memset(wayland->mapped_buffer, 0xFF, size); + wayland->mapped_buffer_size = size; fsync(fd); @@ -176,10 +178,11 @@ static void xdg_toplevel_configure(void* data, self->wayland.wh = height; xdg_surface_set_window_geometry(self->wayland.toplevel->xdg_surface, self->wayland.x, self->wayland.y, self->wayland.ww, self->wayland.wh); + MwLLSetWH(self, width, height); MwLLDispatch(self, resize, NULL); }; - wl_surface_commit(self->wayland.surface); + MwLLForceRender(self); return; }; @@ -253,48 +256,6 @@ static MwBool flush_display(struct wl_display* display) { return MwTRUE; } -static void event_loop(MwLL handle) { - enum { - DISPLAY_FD, - KEYREPEAT_FD, - CURSOR_FD, - LIBDECOR_FD - }; - if(handle->wayland.display == NULL) { - return; - } - struct pollfd fd = {wl_display_get_fd(handle->wayland.display), POLLIN}; - int timeout = 500; - - while(wl_display_prepare_read(handle->wayland.display) != 0) { - if(wl_display_dispatch_pending(handle->wayland.display) > 0) { - return; - } - } - - // If an error other than EAGAIN happens, we have likely been disconnected - // from the Wayland session - if(!flush_display(handle->wayland.display)) { - wl_display_cancel_read(handle->wayland.display); - - handle->wayland.toplevel->running = MwFALSE; - return; - } - - if(!poll(&fd, 1, timeout)) { - wl_display_cancel_read(handle->wayland.display); - return; - } - - if(fd.revents & POLLIN) { - wl_display_read_events(handle->wayland.display); - if(wl_display_dispatch_pending(handle->wayland.display) > 0) { - } - } else { - wl_display_cancel_read(handle->wayland.display); - } -} - static void setup_toplevel(MwLL r, int x, int y, int width, int height) { r->wayland.type = MWLL_WAYLAND_TOPLEVEL; r->wayland.toplevel = malloc(sizeof(struct _MwLLWaylandTopLevel)); @@ -412,8 +373,8 @@ static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { r = malloc(sizeof(*r)); MwLLCreateCommon(r); - r->common.copy_buffer = 1; - r->common.type = MwLLBackendWayland; + // r->common.copy_buffer = 1; + r->common.type = MwLLBackendWayland; if(width == 0) width = parent->wayland.ww; if(height == 0) height = parent->wayland.wh; @@ -467,10 +428,76 @@ static void MwLLSetWHImpl(MwLL handle, int w, int h) { } } +// fractional part of x +static float fpart(float x) { + return x - floor(x); +} +static float rfpart(float x) { + return x - fpart(x); +} + +#define IMAGE_POS_GET(x, y, o) handle->wayland.mapped_buffer[(((y * handle->wayland.ww) + x) * 4) + o] + +#define IMAGE_POS_SET(x, y, o, v) IMAGE_POS_GET(x, y, o) = v; +#define IMAGE_PLOT(x, y, color) \ + IMAGE_POS_SET(x, y, 0, color->common.blue); \ + IMAGE_POS_SET(x, y, 1, color->common.green); \ + IMAGE_POS_SET(x, y, 2, color->common.red); \ + IMAGE_POS_SET(x, y, 3, 255); + +static void +line_draw(MwLL handle, int x0, int y0, int x1, int y1, MwLLColor color) { + int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; + int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1; + int err = dx + dy, e2; + + for(;;) { + IMAGE_PLOT(x0, y0, color); + if(x0 == x1 && y0 == y1) break; + e2 = 2 * err; + if(e2 >= dy) { + err += dy; + x0 += sx; + } + if(e2 <= dx) { + err += dx; + y0 += sy; + } + } +} + static void MwLLPolygonImpl(MwLL handle, MwPoint* points, int points_count, MwLLColor color) { + int i; + int lx = 65536, ly = 65536; + int hx = 0, hy = 0; + int x, y; + + for(i = 0; i < points_count - 1; i++) { + MwPoint p1 = points[i]; + MwPoint p2 = points[i + 1]; + if(p1.x < lx) lx = p1.x; + if(p1.y < ly) ly = p1.y; + if(p1.x > hx) hx = p1.x; + if(p1.y > hy) hy = p1.y; + } + // for(y = ly; y < hy; y++) { + // for(x = lx; x < hx; x++) { + // IMAGE_PLOT(x, y, color); + // } + // } + for(i = 0; i < points_count - 1; i++) { + MwPoint p1 = points[i]; + MwPoint p2 = points[i + 1]; + + line_draw(handle, p1.x, p1.y, p2.x, p2.y, color); + } } static void MwLLLineImpl(MwLL handle, MwPoint* points, MwLLColor color) { + MwPoint p1 = points[0]; + MwPoint p2 = points[1]; + + line_draw(handle, p1.x, p1.y, p2.x, p2.y, color); } static MwLLColor MwLLAllocColorImpl(MwLL handle, int r, int g, int b) { @@ -493,16 +520,73 @@ static void MwLLFreeColorImpl(MwLLColor color) { free(color); } -static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) { -} - static int MwLLPendingImpl(MwLL handle) { - event_loop(handle); - return 0; + enum { + DISPLAY_FD, + KEYREPEAT_FD, + CURSOR_FD + }; + struct pollfd fd; + int timeout = 1; + + if(handle->wayland.force_redraw) { + return 1; + } + + if(handle->wayland.display == NULL) { + return 0; + } + fd.fd = wl_display_get_fd(handle->wayland.display); + fd.events = POLLIN; + + while(wl_display_prepare_read(handle->wayland.display) != 0) { + if(wl_display_dispatch_pending(handle->wayland.display) > 0) { + return 0; + } + } + + // If an error other than EAGAIN happens, we have likely been disconnected + // from the Wayland session + if(!flush_display(handle->wayland.display)) { + wl_display_cancel_read(handle->wayland.display); + + handle->wayland.toplevel->running = MwFALSE; + return 0; + } + + // Condition where no events are being sent. + if(!poll(&fd, 1, timeout)) { + wl_display_cancel_read(handle->wayland.display); + // In this case, we need to commit the surface for any animations, etc. + wl_surface_commit(handle->wayland.surface); + return 0; + } + + if(fd.revents & POLLIN) { + wl_display_read_events(handle->wayland.display); + if(wl_display_dispatch_pending(handle->wayland.display) > 0) { + } + } else { + wl_display_cancel_read(handle->wayland.display); + } + + return 1; } static void MwLLNextEventImpl(MwLL handle) { - event_loop(handle); + MwBool render; + + render = handle->wayland.force_redraw; + + if(render) { + int x, y; + unsigned int w, h; + + MwLLGetXYWH(handle, &x, &y, &w, &h); + + MwLLDispatch(handle, draw, NULL); + handle->wayland.force_redraw = MwFALSE; + } } static void MwLLSetTitleImpl(MwLL handle, const char* title) { @@ -537,6 +621,7 @@ static void MwLLForceRenderImpl(MwLL handle) { // event_loop(handle); wl_surface_damage(handle->wayland.surface, 0, 0, handle->wayland.ww, handle->wayland.wh); wl_surface_commit(handle->wayland.surface); + handle->wayland.force_redraw = MwTRUE; } static void MwLLSetCursorImpl(MwLL handle, MwCursor* image, MwCursor* mask) { diff --git a/src/backend/x11.c b/src/backend/x11.c index a7bea2f..ec50ad2 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -395,10 +395,6 @@ static void MwLLFreeColorImpl(MwLLColor color) { free(color); } -static void MwLLSetBackgroundImpl(MwLL handle, MwLLColor color) { - XSetWindowBackground(handle->x11.display, handle->x11.window, color->x11.pixel); -} - static int MwLLPendingImpl(MwLL handle) { XEvent ev; if(XCheckTypedWindowEvent(handle->x11.display, handle->x11.window, ClientMessage, &ev) || XCheckWindowEvent(handle->x11.display, handle->x11.window, mask, &ev)) { diff --git a/src/core.c b/src/core.c index 6bb4979..19eaaa8 100644 --- a/src/core.c +++ b/src/core.c @@ -295,7 +295,9 @@ int MwStep(MwWidget handle) { arrfree(widgets); handle->prop_event = 0; - if(handle->lowlevel != NULL && MwLLPending(handle->lowlevel)) MwLLNextEvent(handle->lowlevel); + if(handle->lowlevel != NULL && MwLLPending(handle->lowlevel)) + MwLLNextEvent(handle->lowlevel); + handle->prop_event = 1; clean_destroy_queue(handle); @@ -378,11 +380,7 @@ void MwSetText(MwWidget handle, const char* key, const char* value) { shput(handle->text, key, v); } if(handle->prop_event) MwDispatch3(handle, prop_change, key); - if(strcmp(key, MwNbackground) == 0) { - MwLLColor c = MwParseColor(handle, value); - MwLLSetBackground(handle->lowlevel, c); - MwLLFreeColor(c); - } + if(strcmp(key, MwNbackground) == 0 || strcmp(key, MwNforeground) == 0 || strcmp(key, MwNsubBackground) == 0 || strcmp(key, MwNsubForeground) == 0) { MwForceRender(handle); } diff --git a/src/draw.c b/src/draw.c index fd3541f..deaa7e8 100644 --- a/src/draw.c +++ b/src/draw.c @@ -1,5 +1,6 @@ /* $Id$ */ #include +#include #ifdef USE_STB_IMAGE #include "../external/stb_image.h" diff --git a/src/lowlevel.c b/src/lowlevel.c index 70dd50f..f8a4cca 100644 --- a/src/lowlevel.c +++ b/src/lowlevel.c @@ -40,8 +40,6 @@ void (*MwLLMakePopup)(MwLL handle, MwLL parent); void (*MwLLBeginStateChange)(MwLL handle); void (*MwLLEndStateChange)(MwLL handle); -void (*MwLLSetBackground)(MwLL handle, MwLLColor color); - void (*MwLLFocus)(MwLL handle); void (*MwLLGrabPointer)(MwLL handle, int toggle);