diff --git a/.gitignore b/.gitignore index 7b0e56d33..06c431e48 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,8 @@ *.o *.so -examples/* -examples/*.exe examples/*/* -examples/*/*.exe -!examples/*/ -!examples/*.* -!examples/*/*.* +!examples/*/*.c +!examples/*/*.spv compile_flags.txt +*.a +Makefile diff --git a/examples/basic/rotate.c b/examples/basic/rotate.c index c3b85d739..835993dc5 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 cb9c1e66b..49c1cc7fe 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -26,13 +26,15 @@ typedef void* MwLLPixmap; enum MwLLBackends { MwLLBackendX11 = 0, - MwLLBackendGDI + MwLLBackendGDI, + MwLLBackendWayland, }; struct _MwLLCommon { void* user; int copy_buffer; int type; + int success; MwLLHandler handler; }; @@ -56,6 +58,9 @@ struct _MwLLCommonPixmap { #ifdef USE_GDI #include #endif +#ifdef USE_WAYLAND +#include +#endif union _MwLL { struct _MwLLCommon common; @@ -65,6 +70,9 @@ union _MwLL { #ifdef USE_GDI struct _MwLLGDI gdi; #endif +#ifdef USE_WAYLAND + struct _MwLLWayland wayland; +#endif }; union _MwLLColor { @@ -75,6 +83,9 @@ union _MwLLColor { #ifdef USE_GDI struct _MwLLGDIColor gdi; #endif +#ifdef USE_WAYLAND + struct _MwLLWaylandColor wayland; +#endif }; union _MwLLPixmap { @@ -85,6 +96,9 @@ union _MwLLPixmap { #ifdef USE_GDI struct _MwLLGDIPixmap gdi; #endif +#ifdef USE_WAYLAND + struct _MwLLWaylandPixmap wayland; +#endif }; #endif #include @@ -184,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 new file mode 100644 index 000000000..c502a533a --- /dev/null +++ b/include/Mw/LowLevel/Wayland.h @@ -0,0 +1,93 @@ +/* $Id$ */ +/*! + * @file Mw/LowLevel/Wayland.h + * @brief Wayland Backend + * @warning This is used internally + */ +#ifndef __MW_LOWLEVEL_WAYLAND_H__ +#define __MW_LOWLEVEL_WAYLAND_H__ + +#include "Mw/BaseTypes.h" +#include +#include +#include + +#include +#include + +MWDECL int MwLLWaylandCallInit(void); + +#ifndef WL_PROTOCOLS_DEFINED +#define WL_PROTOCOLS_DEFINED +#include "Wayland/xdg-shell-client-protocol.h" +#include "Wayland/xdg-decoration-client-protocol.h" +#include "Wayland/cursor-shape-client-protocol.h" +#endif + +typedef struct wayland_protocol { + void* listener; + void* context; +} wayland_protocol_t; + +typedef wayland_protocol_t*(wl_setup_func)(MwU32, struct _MwLLWayland*); + +struct _MwLLWaylandTopLevel { + struct xdg_surface* xdg_surface; + struct xdg_toplevel* xdg_top_level; + struct xdg_toplevel_listener xdg_toplevel_listener; + struct xdg_surface_listener xdg_surface_listener; + + struct xkb_context* xkb_context; + + MwBool running; + MwBool compositor_created; + MwBool xdg_wm_base_created; + MwBool xdg_surface_created; +}; + +struct _MwLLWayland { + struct _MwLLCommon common; + union { + struct _MwLLWaylandTopLevel* toplevel; + struct wl_subsurface* subsurface; + }; + enum { + MWLL_WAYLAND_TOPLEVEL = 0, + MWLL_WAYLAND_SUBSURFACE = 1, + } type; + + struct wl_display* display; + struct wl_registry* registry; + struct wl_compositor* compositor; + struct wl_subcompositor* subcompositor; + struct wl_surface* surface; + 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; + }* wl_protocol_setup_map; + + struct { + const char* key; + wayland_protocol_t* value; + }* wl_protocol_map; + + MwBool configured; + MwBool force_redraw; + MwU32 x, y, ww, wh; +}; + +struct _MwLLWaylandColor { + struct _MwLLCommonColor common; +}; + +struct _MwLLWaylandPixmap { + struct _MwLLCommonPixmap common; +}; + +#endif diff --git a/include/Mw/LowLevel/Wayland/.gitignore b/include/Mw/LowLevel/Wayland/.gitignore new file mode 100644 index 000000000..25a4663fa --- /dev/null +++ b/include/Mw/LowLevel/Wayland/.gitignore @@ -0,0 +1 @@ +*protocol.h diff --git a/include/Mw/LowLevel/Wayland/README.md b/include/Mw/LowLevel/Wayland/README.md new file mode 100644 index 000000000..e0c17e81f --- /dev/null +++ b/include/Mw/LowLevel/Wayland/README.md @@ -0,0 +1 @@ +Folder for wayland-scanner to place any of its files. diff --git a/pl/ostype/Linux.pl b/pl/ostype/Linux.pl index 980b11975..f3befce02 100644 --- a/pl/ostype/Linux.pl +++ b/pl/ostype/Linux.pl @@ -1,4 +1,9 @@ # $Id$ -use_backend("x11"); + +if(system("wayland-scanner -v >/dev/null 2>&1") == 0){ + use_backend("wayland", "x11"); +} else { + use_backend("x11"); +} 1; diff --git a/pl/rules.pl b/pl/rules.pl index 90778b949..e1bd49997 100644 --- a/pl/rules.pl +++ b/pl/rules.pl @@ -27,6 +27,19 @@ if (grep(/^gdi$/, @backends)) { $gl_libs = "-lopengl32 -lglu32"; } +if (grep(/^wayland$/, @backends)) { + add_cflags("-DUSE_WAYLAND"); + new_object("src/backend/wayland.c"); + add_libs("-lwayland-client -lxkbcommon"); + + scan_wayland_protocol("stable", "xdg-shell",""); + scan_wayland_protocol("stable", "tablet","-v2"); + scan_wayland_protocol("staging", "cursor-shape","-v1"); + scan_wayland_protocol("unstable", "xdg-decoration","-unstable-v1"); + + $gl_libs = "-lGL -lGLU"; +} + if (param_get("stb-image")) { add_cflags("-DUSE_STB_IMAGE"); } diff --git a/pl/utils.pl b/pl/utils.pl index 218f50504..5887e74c8 100644 --- a/pl/utils.pl +++ b/pl/utils.pl @@ -55,6 +55,23 @@ sub use_backend { } } +sub scan_wayland_protocol { + my $tier = $_[0]; + my $proto = $_[1]; + my $ver = $_[2]; + + my $proto_c = "src/backend/wayland-${proto}-protocol.c"; + + if(system("wayland-scanner private-code /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml ${proto_c}") != 0) { + print("^ Error on getting private code for /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml\n"); + } else { + new_object($proto_c); + }; + if(system("wayland-scanner client-header /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml include/Mw/LowLevel/Wayland/${proto}-client-protocol.h")) { + print("^ Error on getting client header for /usr/share/wayland-protocols/${tier}/${proto}/${proto}${ver}.xml\n"); + }; +} + our %params = (); sub param_set { diff --git a/src/backend/.gitignore b/src/backend/.gitignore new file mode 100644 index 000000000..137387c58 --- /dev/null +++ b/src/backend/.gitignore @@ -0,0 +1 @@ +wayland*protocol.c diff --git a/src/backend/call.c b/src/backend/call.c index bace30c1b..de4b7f085 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 f466dfd17..d366d6b18 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 new file mode 100644 index 000000000..d14cada54 --- /dev/null +++ b/src/backend/wayland.c @@ -0,0 +1,704 @@ +/* $Id$ */ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../external/stb_ds.h" +#include "Mw/BaseTypes.h" +#include "Mw/LowLevel.h" + +#include + +#define WAYLAND_GET_INTERFACE(r, inter) shget(r.wl_protocol_map, inter##_interface.name) + +static void new_protocol(void* data, struct wl_registry* registry, + MwU32 name, const char* interface, + MwU32 version) { + struct _MwLLWayland* wayland = data; + + wl_setup_func* func = shget(wayland->wl_protocol_setup_map, interface); + if(func != NULL) { + // printf("registering interface %s\n", interface); + shput(wayland->wl_protocol_map, interface, func(name, data)); + } else { + // printf("unknown interface %s\n", interface); + } +}; + +static void protocol_removed(void* data, + struct wl_registry* registry, + MwU32 name) { + +}; + +static void wl_seat_name(void* data, struct wl_seat* wl_seat, const char* name) {}; +static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat, + MwU32 capabilities) {}; + +void xdg_wm_base_ping(void* data, + struct xdg_wm_base* xdg_wm_base, + MwU32 serial) { + // The compositor will send us a ping event to check that we're responsive. + // We need to send back a pong request immediately. + xdg_wm_base_pong(xdg_wm_base, serial); +}; + +static wayland_protocol_t* wl_shm_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland->shm = wl_registry_bind(wayland->registry, name, &wl_shm_interface, 1); + return NULL; +} +static void buffer_setup(struct _MwLLWayland* wayland) { + struct wl_shm_pool* pool; + struct wl_buffer* buffer; + int x, y; + + int fd; + MwU32 size = wayland->ww * wayland->wh * 4; + char temp_name[] = "/tmp/milsko-wl-shm-XXXXXX"; + + fd = mkstemp(temp_name); + + unlink(temp_name); + + if(posix_fallocate(fd, 0, size) != 0) { + printf("failure setting up wl_shm: could not fallocate. %s.\n", strerror(errno)); + close(fd); + return; + } + if(ftruncate(fd, size) != 0) { + printf("failure setting up wl_shm: could not truncate. %s.\n", strerror(errno)); + close(fd); + return; + } + + lseek(fd, 0, SEEK_SET); + 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); + + if(!(pool = wl_shm_create_pool(wayland->shm, fd, size))) { + printf("failure setting up wl_shm: could not create pool.\n"); + } + + buffer = wl_shm_pool_create_buffer(pool, 0, wayland->ww, wayland->wh, wayland->ww * 4, WL_SHM_FORMAT_ARGB8888); + + if(wayland->configured) { + wl_surface_attach(wayland->surface, buffer, 0, 0); + wl_surface_commit(wayland->surface); + } + wl_buffer_destroy(buffer); + wl_shm_pool_destroy(pool); + close(fd); +} + +static wayland_protocol_t* wl_seat_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); + proto->listener = malloc(sizeof(struct wl_seat_listener)); + + ((struct wl_seat_listener*)proto->listener)->name = wl_seat_name; + ((struct wl_seat_listener*)proto->listener)->capabilities = wl_seat_capabilities; + + proto->context = wl_registry_bind(wayland->registry, name, &wl_seat_interface, 1); + wl_seat_add_listener(proto->context, proto->listener, wayland); + + return proto; +} + +static wayland_protocol_t* wl_compositor_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland->compositor = wl_registry_bind(wayland->registry, name, &wl_compositor_interface, 1); + + return NULL; +} + +static wayland_protocol_t* wl_subcompositor_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland->subcompositor = wl_registry_bind(wayland->registry, name, &wl_subcompositor_interface, 1); + + return NULL; +} + +static wayland_protocol_t* xdg_wm_base_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); + proto->listener = malloc(sizeof(struct xdg_wm_base_listener)); + + ((struct xdg_wm_base_listener*)proto->listener)->ping = xdg_wm_base_ping; + + proto->context = wl_registry_bind(wayland->registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(proto->context, proto->listener, wayland); + + return proto; +} + +static wayland_protocol_t* wp_cursor_shape_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); + proto->listener = NULL; + + proto->context = wl_registry_bind(wayland->registry, name, &wp_cursor_shape_manager_v1_interface, 1); + + return proto; +} + +typedef struct zxdg_decoration_manager_v1_context { + struct zxdg_decoration_manager_v1* manager; + // struct zxdg_decoration_toplevel_v1* toplevel; + struct zxdg_toplevel_decoration_v1* decoration; +} zxdg_decoration_manager_v1_context_t; + +static wayland_protocol_t* zxdg_decoration_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) { + wayland_protocol_t* proto = malloc(sizeof(wayland_protocol_t)); + zxdg_decoration_manager_v1_context_t* ctx = malloc(sizeof(zxdg_decoration_manager_v1_context_t)); + proto->listener = NULL; + + ctx->manager = wl_registry_bind(wayland->registry, name, &zxdg_decoration_manager_v1_interface, 1); + ; + + proto->context = ctx; + + return proto; +} + +static void xdg_toplevel_configure(void* data, + struct xdg_toplevel* xdg_toplevel, + MwI32 width, MwI32 height, + struct wl_array* states) { + MwLL self = data; + + if(width != 0 && height != 0) { + self->wayland.ww = width; + 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); + }; + + MwLLForceRender(self); + + return; +}; + +static void decoration_configure( + void* data, struct zxdg_toplevel_decoration_v1* zxdg_toplevel_decoration_v1, + MwU32 mode) { +} + +static void xdg_toplevel_close( + void* data, struct xdg_toplevel* xdg_toplevel) { + MwLL self = data; + + // Stop running if the user requests to close the toplevel + self->wayland.toplevel->running = MwFALSE; +}; + +static void xdg_surface_configure( + void* data, struct xdg_surface* xdg_surface, MwU32 serial) { + MwLL self = data; + + // The compositor configures our surface, acknowledge the configure event + xdg_surface_ack_configure(xdg_surface, serial); + + if(self->wayland.configured) { + // If this isn't the first configure event we've received, we already + // have a buffer attached, so no need to do anything. Commit the + // surface to apply the configure acknowledgement. + // wl_surface_attach(self->surface, self->buffer, 0, 0); + wl_surface_commit(self->wayland.surface); + } + + self->wayland.configured = MwTRUE; +} + +#define WL_INTERFACE(interface) \ + shput(wayland->wl_protocol_setup_map, interface##_interface.name, (wl_setup_func*)interface##_setup); + +static void setup_callbacks(struct _MwLLWayland* wayland) { + wayland->registry_listener.global = new_protocol; + wayland->registry_listener.global_remove = protocol_removed; + wayland->wl_protocol_setup_map = NULL; + wayland->wl_protocol_map = NULL; + + WL_INTERFACE(wl_shm); + WL_INTERFACE(wl_compositor); + WL_INTERFACE(wl_subcompositor); + if(wayland->type == MWLL_WAYLAND_TOPLEVEL) { + WL_INTERFACE(wl_seat); + WL_INTERFACE(xdg_wm_base); + WL_INTERFACE(zxdg_decoration_manager_v1); + WL_INTERFACE(wp_cursor_shape_manager_v1); + } +} + +#undef WL_INTERFACE + +static MwBool flush_display(struct wl_display* display) { + while(wl_display_flush(display) == -1) { + if(errno != EAGAIN) { + return MwFALSE; + } + + struct pollfd fd = {wl_display_get_fd(display), POLL_OUT}; + + while(poll(&fd, 1, -1) == -1) { + if(errno != EINTR && errno != EAGAIN) + return MwFALSE; + } + } + return MwTRUE; +} + +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)); + + setup_callbacks(&r->wayland); + + // Connect to the Wayland compositor + r->wayland.display = wl_display_connect(NULL); + if(r->wayland.display == NULL) { + printf("wayland: failed to create display\n"); + r->common.success = MwFALSE; + return; + } + + // Do a roundtrip to ensure all interfaces are setup. + r->wayland.registry = wl_display_get_registry(r->wayland.display); + wl_registry_add_listener(r->wayland.registry, &r->wayland.registry_listener, r); + if(wl_display_roundtrip(r->wayland.display) == -1) { + printf("roundtrip failed\n"); + r->common.success = MwFALSE; + return; + } + + // Check that all globals we require are available + if( + r->wayland.compositor == NULL || + WAYLAND_GET_INTERFACE(r->wayland, xdg_wm_base)->context == NULL || + WAYLAND_GET_INTERFACE(r->wayland, wl_seat)->context == NULL) { + + printf("no wl_shm, wl_compositor, wl_seat, or xdg_wm_base support\n"); + r->common.success = MwFALSE; + return; + } + + r->wayland.toplevel->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + + // Create a wl_surface, a xdg_surface and a xdg_toplevel + r->wayland.surface = wl_compositor_create_surface(r->wayland.compositor); + r->wayland.toplevel->xdg_surface = + xdg_wm_base_get_xdg_surface(WAYLAND_GET_INTERFACE(r->wayland, xdg_wm_base)->context, r->wayland.surface); + r->wayland.toplevel->xdg_top_level = xdg_surface_get_toplevel(r->wayland.toplevel->xdg_surface); + + // setup mandatory listeners + r->wayland.toplevel->xdg_surface_listener.configure = xdg_surface_configure; + r->wayland.toplevel->xdg_toplevel_listener.configure = xdg_toplevel_configure; + r->wayland.toplevel->xdg_toplevel_listener.close = xdg_toplevel_close; + + xdg_surface_add_listener(r->wayland.toplevel->xdg_surface, &r->wayland.toplevel->xdg_surface_listener, r); + xdg_toplevel_add_listener(r->wayland.toplevel->xdg_top_level, &r->wayland.toplevel->xdg_toplevel_listener, + r); + + xdg_toplevel_set_title(r->wayland.toplevel->xdg_top_level, "Milsko Wayland App"); + xdg_toplevel_set_app_id(r->wayland.toplevel->xdg_top_level, "MilskoWaylandApp"); + + // Perform the initial commit and wait for the first configure event + wl_surface_commit(r->wayland.surface); + while(wl_display_dispatch(r->wayland.display) != -1 && !r->wayland.configured) { + } + + // setup decorations if we can + if(WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context != NULL) { + zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(r->wayland, zxdg_decoration_manager_v1)->context; + + dec->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration( + dec->manager, r->wayland.toplevel->xdg_top_level); + + zxdg_toplevel_decoration_v1_set_mode( + dec->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + } else { + printf("zxdg null\n"); + } + + buffer_setup(&r->wayland); + + wl_surface_commit(r->wayland.surface); +} + +static void setup_subsurface(MwLL parent, MwLL r, int x, int y, int width, int height) { + struct wl_compositor* compositor = parent->wayland.compositor; + struct wl_subcompositor* subcompositor = parent->wayland.subcompositor; + struct wl_surface* parent_surface = parent->wayland.surface; + + r->wayland.type = MWLL_WAYLAND_SUBSURFACE; + + r->wayland.display = parent->wayland.display; + + r->wayland.surface = wl_compositor_create_surface(compositor); + + r->wayland.subsurface = wl_subcompositor_get_subsurface(subcompositor, r->wayland.surface, parent_surface); + + wl_subsurface_set_position(r->wayland.subsurface, x, y); + + // printf("position %d %d\n", x, y); + + setup_callbacks(&r->wayland); + + r->wayland.registry = wl_display_get_registry(parent->wayland.display); + r->wayland.display = parent->wayland.display; + + wl_registry_add_listener(r->wayland.registry, &r->wayland.registry_listener, r); + if(wl_display_roundtrip(r->wayland.display) == -1) { + printf("roundtrip failed\n"); + raise(SIGTRAP); + return; + } + + r->wayland.configured = MwTRUE; + + buffer_setup(&r->wayland); + + wl_surface_commit(parent->wayland.surface); +} +static MwLL MwLLCreateImpl(MwLL parent, int x, int y, int width, int height) { + MwLL r; + r = malloc(sizeof(*r)); + MwLLCreateCommon(r); + + // r->common.copy_buffer = 1; + r->common.type = MwLLBackendWayland; + + if(width == 0) width = parent->wayland.ww; + if(height == 0) height = parent->wayland.wh; + + r->wayland.ww = width; + r->wayland.wh = height; + + r->common.success = MwTRUE; + + if(parent == NULL) { + setup_toplevel(r, x, y, width, height); + } else { + setup_subsurface(parent, r, x, y, width, height); + } + + return r; +} + +static void MwLLDestroyImpl(MwLL handle) { + MwLLDestroyCommon(handle); + + free(handle); +} + +static void MwLLGetXYWHImpl(MwLL handle, int* x, int* y, unsigned int* w, unsigned int* h) { + *x = handle->wayland.x; + *y = handle->wayland.y; + *w = handle->wayland.ww; + *h = handle->wayland.wh; +} + +static void MwLLSetXYImpl(MwLL handle, int x, int y) { + handle->wayland.x = x; + handle->wayland.y = y; + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + xdg_surface_set_window_geometry(handle->wayland.toplevel->xdg_surface, handle->wayland.x, handle->wayland.y, handle->wayland.ww, handle->wayland.wh); + } else { + wl_subsurface_set_position(handle->wayland.subsurface, x, y); + } +} + +static void MwLLSetWHImpl(MwLL handle, int w, int h) { + handle->wayland.ww = w; + handle->wayland.wh = h; + + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + xdg_surface_set_window_geometry(handle->wayland.toplevel->xdg_surface, handle->wayland.x, handle->wayland.y, handle->wayland.ww, handle->wayland.wh); + } else { + // setup the buffer again with the new size. + buffer_setup(&handle->wayland); + } +} + +// 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) { + MwLLColor c = malloc(sizeof(*c)); + + c->common.red = r; + c->common.blue = b; + c->common.green = g; + + return c; +} + +static void MwLLColorUpdateImpl(MwLL handle, MwLLColor c, int r, int g, int b) { + c->common.red = r; + c->common.blue = b; + c->common.green = g; +} + +static void MwLLFreeColorImpl(MwLLColor color) { + free(color); +} + +static int MwLLPendingImpl(MwLL handle) { + 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) { + 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) { + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + xdg_toplevel_set_title(handle->wayland.toplevel->xdg_top_level, title); + } +} + +static MwLLPixmap MwLLCreatePixmapImpl(MwLL handle, unsigned char* data, int width, int height) { + MwLLPixmap r = malloc(sizeof(*r)); + + r->common.width = width; + r->common.height = height; + r->common.raw = data; + return r; +} + +static void MwLLPixmapUpdateImpl(MwLLPixmap r) { +} + +static void MwLLDestroyPixmapImpl(MwLLPixmap pixmap) { + free(pixmap); +} + +static void MwLLDrawPixmapImpl(MwLL handle, MwRect* rect, MwLLPixmap pixmap) { +} + +static void MwLLSetIconImpl(MwLL handle, MwLLPixmap pixmap) { +} + +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) { +} + +static void MwLLDetachImpl(MwLL handle, MwPoint* point) { +} + +static void MwLLShowImpl(MwLL handle, int show) { +} + +static void MwLLMakePopupImpl(MwLL handle, MwLL parent) { +} + +static void MwLLSetSizeHintsImpl(MwLL handle, int minx, int miny, int maxx, int maxy) { + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + xdg_toplevel_set_min_size(handle->wayland.toplevel->xdg_top_level, minx, miny); + xdg_toplevel_set_max_size(handle->wayland.toplevel->xdg_top_level, maxx, maxy); + } +} + +static void MwLLMakeBorderlessImpl(MwLL handle, int toggle) { + if(handle->wayland.type == MWLL_WAYLAND_TOPLEVEL) { + if(WAYLAND_GET_INTERFACE(handle->wayland, zxdg_decoration_manager_v1)->context != NULL) { + zxdg_decoration_manager_v1_context_t* dec = WAYLAND_GET_INTERFACE(handle->wayland, zxdg_decoration_manager_v1)->context; + + dec->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration( + dec->manager, handle->wayland.toplevel->xdg_top_level); + + zxdg_toplevel_decoration_v1_set_mode( + dec->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE); + } else { + // TODO: hide our custom window decorations when we have them. + printf("zxdg null\n"); + } + } +} + +static void MwLLFocusImpl(MwLL handle) { +} + +static void MwLLGrabPointerImpl(MwLL handle, int toggle) { +} + +static void MwLLSetClipboardImpl(MwLL handle, const char* text) { +} + +static char* MwLLGetClipboardImpl(MwLL handle) { + char* r = NULL; + return r; +} + +static void MwLLMakeToolWindowImpl(MwLL handle) { +} + +static void MwLLGetCursorCoordImpl(MwLL handle, MwPoint* point) { +} + +static void MwLLGetScreenSizeImpl(MwLL handle, MwRect* rect) { +} + +static void MwLLBeginStateChangeImpl(MwLL handle) { + MwLLShow(handle, 0); +} + +static void MwLLEndStateChangeImpl(MwLL handle) { + MwLLShow(handle, 1); +} + +static int MwLLWaylandCallInitImpl(void) { +#ifdef __linux__ + if(strcmp(getenv("XDG_SESSION_TYPE"), "wayland")) { + return 1; + } +#endif + return 0; +} + +#include "call.c" +CALL(Wayland); diff --git a/src/backend/x11.c b/src/backend/x11.c index f75adfb95..ec50ad21b 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)) { @@ -1019,7 +1015,6 @@ static void MwLLEndStateChangeImpl(MwLL handle) { } static int MwLLX11CallInitImpl(void) { - /* TODO: check properly */ return 0; } diff --git a/src/core.c b/src/core.c index 77c6d9a40..19eaaa8db 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); } @@ -687,6 +685,9 @@ MwWidget MwGetParent(MwWidget handle) { typedef int (*call_t)(void); int MwLibraryInit(void) { call_t calls[] = { +#ifdef USE_WAYLAND + MwLLWaylandCallInit, +#endif #ifdef USE_X11 MwLLX11CallInit, #endif diff --git a/src/draw.c b/src/draw.c index fd3541f24..deaa7e846 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 70dd50f2a..f8a4ccac1 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);