diff --git a/examples/basic/clipboard.c b/examples/basic/clipboard.c index 38084a18..e6a75023 100644 --- a/examples/basic/clipboard.c +++ b/examples/basic/clipboard.c @@ -1,6 +1,6 @@ #include -MwWidget window, instructions, text1, text2, cur_text; +MwWidget window, instructions, text; static void resize(MwWidget handle, void* user_data, void* call_data) { unsigned int w, h; @@ -17,14 +17,9 @@ static void resize(MwWidget handle, void* user_data, void* call_data) { MwNheight, h - 125 - 50 * 3, NULL); - MwVaApply(text1, - MwNy, 150, - MwNwidth, w - 25 * 2, - MwNheight, h - 125 - 50 * 3, - NULL); - MwVaApply(text2, - MwNy, 250, - MwNwidth, w - 25 * 2, + MwVaApply(text, + MwNy, 200, + MwNwidth, w - 50 * 2, MwNheight, h - 125 - 50 * 3, NULL); } @@ -35,17 +30,14 @@ static void clipboard(MwWidget handle, void* user_data, void* call_data) { (void)user_data; if(clipboard != NULL) { - MwVaApply(cur_text, MwNtext, clipboard, NULL); - MwForceRender(cur_text); + MwVaApply(text, MwNtext, clipboard, NULL); + MwForceRender(text); } + MwForceRender(window); } static void tick(MwWidget handle, void* user_data, void* call_data) { - cur_text = text1; - MwGetClipboard(handle, MwClipboardMain); - cur_text = text2; - MwGetClipboard(handle, MwClipboardPrimary); - MwForceRender(window); + MwGetClipboard(handle); } int main() { @@ -57,19 +49,14 @@ int main() { instructions = MwVaCreateWidget(MwLabelClass, "button", window, 50, 50, 300, 125, MwNtext, "clipboard contents will show up below.", NULL); - text1 = MwVaCreateWidget(MwLabelClass, "label", window, 25, 150, 300, 75, + text = MwVaCreateWidget(MwLabelClass, "label", window, 50, 200, 300, 125, MwNtext, "", NULL); - text2 = MwVaCreateWidget(MwLabelClass, "label2", window, 25, 250, 300, 75, - MwNtext, "", - NULL); - - cur_text = text1; MwAddUserHandler(window, MwNresizeHandler, resize, NULL); MwAddUserHandler(window, MwNclipboardHandler, clipboard, NULL); MwAddUserHandler(instructions, MwNclipboardHandler, clipboard, NULL); - MwAddUserHandler(cur_text, MwNclipboardHandler, clipboard, NULL); + MwAddUserHandler(text, MwNclipboardHandler, clipboard, NULL); MwAddUserHandler(window, MwNtickHandler, tick, NULL); resize(window, NULL, NULL); diff --git a/include/Mw/Core.h b/include/Mw/Core.h index ad4cd19d..4d3e4b99 100644 --- a/include/Mw/Core.h +++ b/include/Mw/Core.h @@ -362,11 +362,10 @@ MWDECL void MwGetScreenSize(MwWidget handle, MwRect* rect); MWDECL int MwGetCoordinateType(MwWidget handle); /*! - * @brief Queue to get clipboard content. + * @brief Queue to get clipboard content * @param handle Widget - * @param clipboard_type The Clipboard Type to get. See MwClipboardType. This is ignored on backends that aren't X11/Wayland, so if you're unsure, just use MwClipboardMain */ -MWDECL void MwGetClipboard(MwWidget handle, int clipboard_type); +MWDECL void MwGetClipboard(MwWidget handle); #ifdef __cplusplus } diff --git a/include/Mw/LowLevel.h b/include/Mw/LowLevel.h index 5ba698e9..2a293b02 100644 --- a/include/Mw/LowLevel.h +++ b/include/Mw/LowLevel.h @@ -277,8 +277,8 @@ MWDECL void (*MwLLEndStateChange)(MwLL handle); MWDECL void (*MwLLFocus)(MwLL handle); MWDECL void (*MwLLGrabPointer)(MwLL handle, int toggle); -MWDECL void (*MwLLSetClipboard)(MwLL handle, const char* text, int clipboard_type); -MWDECL void (*MwLLGetClipboard)(MwLL handle, int clipboard_type); +MWDECL void (*MwLLSetClipboard)(MwLL handle, const char* text); +MWDECL void (*MwLLGetClipboard)(MwLL handle); MWDECL void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point); MWDECL void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect); diff --git a/include/Mw/LowLevel/Wayland.h b/include/Mw/LowLevel/Wayland.h index 7debc887..77e43b3b 100644 --- a/include/Mw/LowLevel/Wayland.h +++ b/include/Mw/LowLevel/Wayland.h @@ -452,7 +452,7 @@ struct _MwLLWayland { /* clipboard related stuff. * Note that unlike most interfaces, we don't keep zwp_primary_selection stuff in a wayland_protocol_t because we use wl_data_device as a fallback and want to have it share memory space.*/ - struct { + union { struct wl_data_device_manager* wl; struct zwp_primary_selection_device_manager_v1* zwp; } clipboard_manager; @@ -464,8 +464,7 @@ struct _MwLLWayland { MwBool supports_zwp; uint32_t clipboard_serial; - wl_clipboard_device_context_t** clipboard_devices_wl; - wl_clipboard_device_context_t** clipboard_devices_zwp; + wl_clipboard_device_context_t** clipboard_devices; struct wl_pointer* pointer; MwU32 pointer_serial; diff --git a/include/Mw/TypeDefs.h b/include/Mw/TypeDefs.h index bd0af3d7..62380f00 100644 --- a/include/Mw/TypeDefs.h +++ b/include/Mw/TypeDefs.h @@ -239,12 +239,4 @@ enum MwCoordinateType { MwCoordinatesLocal, }; -/* The clipboard type that MwGetClipboard should return */ -enum MwClipboardType { - /* the clipboard that is present on every platform that supports it */ - MwClipboardMain = 0, - /* the "primary" clipboard that Wayland stores to emulate X11's behavior. */ - MwClipboardPrimary, -}; - #endif diff --git a/milsko.xml b/milsko.xml index d42f7e96..9fb45a60 100644 --- a/milsko.xml +++ b/milsko.xml @@ -1,4 +1,4 @@ - + + @@ -149,10 +146,6 @@ - - 0 - - 0x0fffffff @@ -406,19 +399,6 @@ - - - - - - - - - - - - -
diff --git a/src/backend/cocoa.m b/src/backend/cocoa.m index 3177d14c..95fd56db 100644 --- a/src/backend/cocoa.m +++ b/src/backend/cocoa.m @@ -1042,15 +1042,13 @@ static void MwLLGrabPointerImpl(MwLL handle, int toggle) { handle->cocoa.real->pointerLocked = toggle; } -static void MwLLSetClipboardImpl(MwLL handle, const char* text, int clipboard_type) { +static void MwLLSetClipboardImpl(MwLL handle, const char* text) { (void)handle; (void)text; - (void)clipboard_type; } -static void MwLLGetClipboardImpl(MwLL handle, int clipboard_type) { +static void MwLLGetClipboardImpl(MwLL handle) { (void)handle; - (void)clipboard_type; } static void MwLLMakeToolWindowImpl(MwLL handle) { diff --git a/src/backend/gdi.c b/src/backend/gdi.c index 8813ba24..51e33df1 100644 --- a/src/backend/gdi.c +++ b/src/backend/gdi.c @@ -748,9 +748,8 @@ static void MwLLGrabPointerImpl(MwLL handle, int toggle) { } } -static void MwLLSetClipboardImpl(MwLL handle, const char* text, int clipboard_type) { +static void MwLLSetClipboardImpl(MwLL handle, const char* text) { HGLOBAL hg; - (void)clipboard_type; if(OpenClipboard(handle->gdi.hWnd) != 0) { char* lock; @@ -767,8 +766,7 @@ static void MwLLSetClipboardImpl(MwLL handle, const char* text, int clipboard_ty } } -static void MwLLGetClipboardImpl(MwLL handle, int clipboard_type) { - (void)clipboard_type; +static void MwLLGetClipboardImpl(MwLL handle) { handle->gdi.get_clipboard = 1; /* nishi: we do this to make clipboard api work similar to other backends */ } diff --git a/src/backend/wayland.c b/src/backend/wayland.c index 425308d7..bd468608 100644 --- a/src/backend/wayland.c +++ b/src/backend/wayland.c @@ -266,7 +266,7 @@ struct zwp_primary_selection_device_v1_listener zwp_primary_selection_device_v1_ .selection = zwp_primary_selection_device_v1_selection, }; -static void wl_clipboard_read(wl_clipboard_device_context_t* ctx, int clipboard_type) { +static void wl_clipboard_read(wl_clipboard_device_context_t* ctx) { int fds[2]; fd_set set; char* buf = NULL; @@ -282,7 +282,7 @@ static void wl_clipboard_read(wl_clipboard_device_context_t* ctx, int clipboard_ timeout.tv_sec = 0; timeout.tv_usec = 100; - if(clipboard_type == MwClipboardPrimary) { + if(ctx->ll->wayland.supports_zwp) { zwp_primary_selection_offer_v1_receive(ctx->offer.zwp, "text/plain", fds[1]); } else if(ctx->offer.wl) { wl_data_offer_receive(ctx->offer.wl, "text/plain", fds[1]); @@ -427,7 +427,7 @@ static wayland_protocol_t* wl_data_device_manager_setup(MwU32 name, struct _MwLL static void wl_data_device_manager_interface_destroy(struct _MwLLWayland* wayland, wayland_protocol_t* data) { (void)data; - if(wayland->clipboard_manager.wl != NULL) { + if(wayland->clipboard_manager.wl != NULL && !wayland->supports_zwp) { wl_data_device_manager_destroy(wayland->clipboard_manager.wl); wl_data_source_destroy(wayland->clipboard_source.wl); wayland->clipboard_manager.wl = NULL; @@ -436,6 +436,10 @@ static void wl_data_device_manager_interface_destroy(struct _MwLLWayland* waylan /* zwp_primary_selection_device_manager_v1 setup function */ static wayland_protocol_t* zwp_primary_selection_device_manager_v1_setup(MwU32 name, struct _MwLLWayland* wayland) { + if(wayland->clipboard_manager.wl != NULL) { + wl_data_device_manager_interface_destroy(wayland, NULL); + } + wayland->clipboard_manager.zwp = wl_registry_bind(wayland->registry, name, &zwp_primary_selection_device_manager_v1_interface, 1); wayland->clipboard_source.zwp = zwp_primary_selection_device_manager_v1_create_source(wayland->clipboard_manager.zwp); @@ -617,6 +621,10 @@ static void pointer_button(void* data, struct wl_pointer* wl_pointer, MwU32 seri break; case BTN_MIDDLE: p.button = MwLLMouseMiddle; + for(i = 0; i < arrlen(self->wayland.clipboard_devices); i++) { + wl_clipboard_read( + self->wayland.clipboard_devices[i]); + } break; case BTN_RIGHT: p.button = MwLLMouseRight; @@ -832,6 +840,14 @@ static void keyboard_key(void* data, } if((self->wayland.mod_state & 4) == 4) { + /* clipboard paste */ + if(key == 'V') { + int i; + for(i = 0; i < arrlen(self->wayland.clipboard_devices); i++) { + wl_clipboard_read( + self->wayland.clipboard_devices[i]); + } + } key |= MwLLControlMask; } if((self->wayland.mod_state & 8) == 8) { @@ -897,9 +913,8 @@ wl_seat_name(void* data, struct wl_seat* wl_seat, const char* name) { /* `wl_seat.capabilities` callback */ static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat, MwU32 capabilities) { - MwLL self = data; - wl_clipboard_device_context_t* device_ctx_zwp = NULL; - wl_clipboard_device_context_t* device_ctx_wl = malloc(sizeof(wl_clipboard_device_context_t)); + MwLL self = data; + wl_clipboard_device_context_t* device_ctx = malloc(sizeof(wl_clipboard_device_context_t)); WAYLAND_EVENT_OP_START(self); @@ -915,23 +930,21 @@ static void wl_seat_capabilities(void* data, struct wl_seat* wl_seat, if(self->wayland.clipboard_manager.wl != NULL) { if(self->wayland.supports_zwp) { - device_ctx_zwp = malloc(sizeof(wl_clipboard_device_context_t)); - device_ctx_zwp->device.zwp = zwp_primary_selection_device_manager_v1_get_device(self->wayland.clipboard_manager.zwp, wl_seat); - device_ctx_zwp->ll = self; - device_ctx_zwp->seat = wl_seat; - device_ctx_zwp->capabilities = capabilities; + device_ctx->device.zwp = zwp_primary_selection_device_manager_v1_get_device(self->wayland.clipboard_manager.zwp, wl_seat); + } else { + device_ctx->device.wl = wl_data_device_manager_get_data_device(self->wayland.clipboard_manager.wl, wl_seat); } - device_ctx_wl->device.wl = wl_data_device_manager_get_data_device(self->wayland.clipboard_manager.wl, wl_seat); - device_ctx_wl->ll = self; - device_ctx_wl->seat = wl_seat; - device_ctx_wl->capabilities = capabilities; + device_ctx->ll = self; + device_ctx->seat = wl_seat; + device_ctx->capabilities = capabilities; if(self->wayland.supports_zwp) { - zwp_primary_selection_device_v1_add_listener(device_ctx_zwp->device.zwp, &zwp_primary_selection_device_v1_listener, device_ctx_zwp); - arrpush(self->wayland.clipboard_devices_zwp, device_ctx_zwp); + zwp_primary_selection_device_v1_add_listener(device_ctx->device.zwp, &zwp_primary_selection_device_v1_listener, device_ctx); + } else { + wl_data_device_add_listener(device_ctx->device.wl, &wl_data_device_listener, device_ctx); } - wl_data_device_add_listener(device_ctx_wl->device.wl, &wl_data_device_listener, device_ctx_wl); - arrpush(self->wayland.clipboard_devices_wl, device_ctx_wl); + + arrpush(self->wayland.clipboard_devices, device_ctx); } WAYLAND_EVENT_OP_END(self); @@ -2495,48 +2508,33 @@ static void MwLLGrabPointerImpl(MwLL handle, int toggle) { } } -static void MwLLSetClipboardImpl(MwLL handle, const char* text, int clipboard_type) { - int i; - +static void MwLLSetClipboardImpl(MwLL handle, const char* text) { if(handle->wayland.clipboard_buffer != NULL) { free(handle->wayland.clipboard_buffer); } handle->wayland.clipboard_buffer = malloc(strlen(text)); strcpy(handle->wayland.clipboard_buffer, text); - if(clipboard_type == MwClipboardPrimary) { - if(handle->wayland.supports_zwp) { - for(i = 0; i < arrlen(handle->wayland.clipboard_devices_zwp); i++) { - wl_clipboard_device_context_t* device = handle->wayland.clipboard_devices_zwp[i]; - zwp_primary_selection_device_v1_set_selection(device->device.zwp, handle->wayland.clipboard_source.zwp, handle->wayland.keyboard_serial); - } - } else { - printf("[WARNING] Primary clipboard requested for MwLLSetClipboard, but this Wayland compositor doesn't support it.\n"); + if(handle->wayland.supports_zwp) { + int i; + for(i = 0; i < arrlen(handle->wayland.clipboard_devices); i++) { + wl_clipboard_device_context_t* device = handle->wayland.clipboard_devices[i]; + zwp_primary_selection_device_v1_set_selection(device->device.zwp, handle->wayland.clipboard_source.zwp, handle->wayland.keyboard_serial); } } else { - for(i = 0; i < arrlen(handle->wayland.clipboard_devices_wl); i++) { - wl_clipboard_device_context_t* device = handle->wayland.clipboard_devices_wl[i]; + int i; + for(i = 0; i < arrlen(handle->wayland.clipboard_devices); i++) { + wl_clipboard_device_context_t* device = handle->wayland.clipboard_devices[i]; wl_data_device_set_selection(device->device.wl, handle->wayland.clipboard_source.wl, handle->wayland.keyboard_serial); } } } -static void MwLLGetClipboardImpl(MwLL handle, int clipboard_type) { +static void MwLLGetClipboardImpl(MwLL handle) { int i; - if(clipboard_type == MwClipboardPrimary) { - if(handle->wayland.supports_zwp) { - for(i = 0; i < arrlen(handle->wayland.clipboard_devices_zwp); i++) { - wl_clipboard_read( - handle->wayland.clipboard_devices_zwp[i], clipboard_type); - } - } else { - printf("[WARNING] Primary clipboard requested for MwLLSetClipboard, but this Wayland compositor doesn't support it.\n"); - } - } else { - for(i = 0; i < arrlen(handle->wayland.clipboard_devices_wl); i++) { - wl_clipboard_read( - handle->wayland.clipboard_devices_wl[i], clipboard_type); - } + for(i = 0; i < arrlen(handle->wayland.clipboard_devices); i++) { + wl_clipboard_read( + handle->wayland.clipboard_devices[i]); } return; } diff --git a/src/backend/x11.c b/src/backend/x11.c index a556c1c6..dcc41d7a 100644 --- a/src/backend/x11.c +++ b/src/backend/x11.c @@ -1253,14 +1253,14 @@ static void MwLLGrabPointerImpl(MwLL handle, int toggle) { } } -static void MwLLSetClipboardImpl(MwLL handle, const char* text, int clipboard_type) { +static void MwLLSetClipboardImpl(MwLL handle, const char* text) { /* TODO */ (void)handle; (void)text; } -static void MwLLGetClipboardImpl(MwLL handle, int clipboard_type) { +static void MwLLGetClipboardImpl(MwLL handle) { if(handle->x11.clipboard_pending) return; XConvertSelection(handle->x11.display, handle->x11.clipboard, handle->x11.utf8_string, handle->x11.selection, handle->x11.window, CurrentTime); diff --git a/src/core.c b/src/core.c index 6c08970a..1b00615c 100644 --- a/src/core.c +++ b/src/core.c @@ -966,6 +966,6 @@ int MwGetCoordinateType(MwWidget handle) { } } -void MwGetClipboard(MwWidget handle, int clipboard_type) { - MwLLGetClipboard(handle->lowlevel, clipboard_type); +void MwGetClipboard(MwWidget handle) { + MwLLGetClipboard(handle->lowlevel); } diff --git a/src/lowlevel.c b/src/lowlevel.c index 2a874e6c..e80b95b9 100644 --- a/src/lowlevel.c +++ b/src/lowlevel.c @@ -45,8 +45,8 @@ void (*MwLLEndStateChange)(MwLL handle) = NULL; void (*MwLLFocus)(MwLL handle) = NULL; void (*MwLLGrabPointer)(MwLL handle, int toggle) = NULL; -void (*MwLLSetClipboard)(MwLL handle, const char* text, int clipboard_type) = NULL; -void (*MwLLGetClipboard)(MwLL, int clipboard_type) = NULL; +void (*MwLLSetClipboard)(MwLL handle, const char* text) = NULL; +void (*MwLLGetClipboard)(MwLL handle) = NULL; void (*MwLLGetCursorCoord)(MwLL handle, MwPoint* point) = NULL; void (*MwLLGetScreenSize)(MwLL handle, MwRect* rect) = NULL; diff --git a/src/widget/entry.c b/src/widget/entry.c index 846dc197..4d1cc736 100644 --- a/src/widget/entry.c +++ b/src/widget/entry.c @@ -113,7 +113,7 @@ static void key(MwWidget handle, int code) { } else if(code == MwLLKeyEnter) { MwDispatchUserHandler(handle, MwNactivateHandler, NULL); } else if(code == (MwLLControlMask | 'v')) { - MwLLGetClipboard(handle->lowlevel, MwClipboardMain); + MwLLGetClipboard(handle->lowlevel); } else if(!(code & MwLLKeyMask)) { int incr = 0; out = malloc(strlen(str) + 5 + 1); @@ -133,20 +133,6 @@ static void key(MwWidget handle, int code) { MwForceRender(handle); } -#if defined(USE_X11) || defined(USE_WAYLAND) -static void mouse_up(MwWidget handle, void* ptr) { - if(handle->lowlevel->common.type == MwLLBackendWayland || handle->lowlevel->common.type == MwLLBackendX11) { - MwLLMouse* mouse = ptr; - if(mouse->button == MwLLMouseMiddle) { - MwLLGetClipboard(handle->lowlevel, MwClipboardPrimary); - } - } - MwForceRender2(handle, NULL); -} -#else -#define mouse_up MwForceRender2 -#endif - static void prop_change(MwWidget handle, const char* prop) { if(strcmp(prop, MwNtext) == 0 || strcmp(prop, MwNhideInput) == 0) MwForceRender(handle); @@ -193,7 +179,7 @@ MwClassRec MwEntryClassRec = { NULL, /* parent_resize */ prop_change, /* prop_change */ NULL, /* mouse_move */ - mouse_up, /* mouse_up */ + MwForceRender2, /* mouse_up */ MwForceRender2, /* mouse_down */ key, /* key */ NULL, /* execute */