diff options
41 files changed, 3157 insertions, 81 deletions
@@ -30,8 +30,8 @@ development tools - or any subset of these features you like, because all of them work independently of one another and freely compose with anything you want to implement yourself. -**Status**: prior to 1.0 the API is not stable, but we've done most of the work -and various projects are using wlroots to build Wayland compositors with. +Check out our [wiki](https://github.com/swaywm/wlroots/wiki/Getting-started) to +get started with wlroots. wlroots is developed under the direction of the [sway](https://github.com/swaywm/sway) project. A variety of wrapper libraries @@ -83,7 +83,8 @@ Install like so: wlroots comes with a test compositor called rootston, which demonstrates the features of the library and is used as a testbed for the development of the library. It may also be useful as a reference for understanding how to use -various wlroots features. +various wlroots features, but it's not considered a production-quality codebase +and is not designed for daily use. If you followed the build instructions above the rootston executable can be found at `./build/rootston/rootston`. To use it, refer to the example config at diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c index 5c064ac7..ad80f2fd 100644 --- a/backend/drm/atomic.c +++ b/backend/drm/atomic.c @@ -104,7 +104,8 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *drm, drmModeDestroyPropertyBlob(drm->fd, crtc->mode_id); } - if (drmModeCreatePropertyBlob(drm->fd, mode, sizeof(*mode), &crtc->mode_id)) { + if (drmModeCreatePropertyBlob(drm->fd, mode, sizeof(*mode), + &crtc->mode_id)) { wlr_log_errno(WLR_ERROR, "Unable to create property blob"); return false; } @@ -120,6 +121,10 @@ static bool atomic_crtc_pageflip(struct wlr_drm_backend *drm, struct atomic atom; atomic_begin(crtc, &atom); atomic_add(&atom, conn->id, conn->props.crtc_id, crtc->id); + if (mode != NULL && conn->props.link_status != 0) { + atomic_add(&atom, conn->id, conn->props.link_status, + DRM_MODE_LINK_STATUS_GOOD); + } atomic_add(&atom, crtc->id, crtc->props.mode_id, crtc->mode_id); atomic_add(&atom, crtc->id, crtc->props.active, 1); set_plane_props(&atom, crtc->primary, crtc->id, fb_id, true); diff --git a/backend/drm/drm.c b/backend/drm/drm.c index b9ab7f82..0b624717 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include <assert.h> #include <drm_mode.h> #include <EGL/egl.h> @@ -156,6 +156,11 @@ bool init_drm_resources(struct wlr_drm_backend *drm) { wlr_log(WLR_INFO, "Found %d DRM CRTCs", res->count_crtcs); drm->num_crtcs = res->count_crtcs; + if (drm->num_crtcs == 0) { + drmModeFreeResources(res); + return true; + } + drm->crtcs = calloc(drm->num_crtcs, sizeof(drm->crtcs[0])); if (!drm->crtcs) { wlr_log_errno(WLR_ERROR, "Allocation failed"); @@ -433,9 +438,9 @@ static void realloc_planes(struct wlr_drm_backend *drm, const uint32_t *crtc_in, continue; } - uint32_t possible[drm->num_type_planes[type]]; - uint32_t crtc[drm->num_crtcs]; - uint32_t crtc_res[drm->num_crtcs]; + uint32_t possible[drm->num_type_planes[type] + 1]; + uint32_t crtc[drm->num_crtcs + 1]; + uint32_t crtc_res[drm->num_crtcs + 1]; for (size_t i = 0; i < drm->num_type_planes[type]; ++i) { possible[i] = drm->type_planes[type][i].possible_crtcs; @@ -1078,6 +1083,24 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { wlr_conn->crtc = NULL; } + // This can only happen *after* hotplug, since we haven't read the + // connector properties yet + if (wlr_conn->props.link_status != 0) { + uint64_t link_status; + if (!get_drm_prop(drm->fd, wlr_conn->id, + wlr_conn->props.link_status, &link_status)) { + wlr_log(WLR_ERROR, "Failed to get link status for '%s'", + wlr_conn->output.name); + continue; + } + + if (link_status == DRM_MODE_LINK_STATUS_BAD) { + // We need to reload our list of modes and force a modeset + wlr_log(WLR_INFO, "Bad link for '%s'", wlr_conn->output.name); + drm_connector_cleanup(wlr_conn); + } + } + if (wlr_conn->state == WLR_DRM_CONN_DISCONNECTED && drm_conn->connection == DRM_MODE_CONNECTED) { wlr_log(WLR_INFO, "'%s' connected", wlr_conn->output.name); @@ -1160,7 +1183,7 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { } } - bool changed_outputs[wl_list_length(&drm->outputs)]; + bool changed_outputs[wl_list_length(&drm->outputs) + 1]; memset(changed_outputs, false, sizeof(changed_outputs)); for (size_t i = 0; i < new_outputs_len; ++i) { struct wlr_drm_connector *conn = new_outputs[i]; @@ -1238,7 +1261,7 @@ static void page_flip_handler(int fd, unsigned seq, int handle_drm_event(int fd, uint32_t mask, void *data) { drmEventContext event = { - .version = DRM_EVENT_CONTEXT_VERSION, + .version = 2, .page_flip_handler = page_flip_handler, }; @@ -1324,7 +1347,13 @@ static void drm_connector_cleanup(struct wlr_drm_connector *conn) { memset(&conn->output.model, 0, sizeof(conn->output.model)); memset(&conn->output.serial, 0, sizeof(conn->output.serial)); - conn->pageflip_pending = false; + if (conn->output.idle_frame != NULL) { + wl_event_source_remove(conn->output.idle_frame); + conn->output.idle_frame = NULL; + } + conn->output.needs_swap = false; + conn->output.frame_pending = false; + /* Fallthrough */ case WLR_DRM_CONN_NEEDS_MODESET: wlr_log(WLR_INFO, "Emitting destruction signal for '%s'", diff --git a/backend/drm/properties.c b/backend/drm/properties.c index 372de232..5ca4c8ac 100644 --- a/backend/drm/properties.c +++ b/backend/drm/properties.c @@ -19,9 +19,10 @@ struct prop_info { static const struct prop_info connector_info[] = { #define INDEX(name) (offsetof(union wlr_drm_connector_props, name) / sizeof(uint32_t)) - { "CRTC_ID", INDEX(crtc_id) }, - { "DPMS", INDEX(dpms) }, - { "EDID", INDEX(edid) }, + { "CRTC_ID", INDEX(crtc_id) }, + { "DPMS", INDEX(dpms) }, + { "EDID", INDEX(edid) }, + { "link-status", INDEX(link_status) }, #undef INDEX }; @@ -87,7 +88,8 @@ static bool scan_properties(int fd, uint32_t id, uint32_t type, uint32_t *result return true; } -bool get_drm_connector_props(int fd, uint32_t id, union wlr_drm_connector_props *out) { +bool get_drm_connector_props(int fd, uint32_t id, + union wlr_drm_connector_props *out) { return scan_properties(fd, id, DRM_MODE_OBJECT_CONNECTOR, out->props, connector_info, sizeof(connector_info) / sizeof(connector_info[0])); } @@ -103,7 +105,8 @@ bool get_drm_plane_props(int fd, uint32_t id, union wlr_drm_plane_props *out) { } bool get_drm_prop(int fd, uint32_t obj, uint32_t prop, uint64_t *ret) { - drmModeObjectProperties *props = drmModeObjectGetProperties(fd, obj, DRM_MODE_OBJECT_ANY); + drmModeObjectProperties *props = + drmModeObjectGetProperties(fd, obj, DRM_MODE_OBJECT_ANY); if (!props) { return false; } diff --git a/backend/multi/backend.c b/backend/multi/backend.c index cefaa361..50851109 100644 --- a/backend/multi/backend.c +++ b/backend/multi/backend.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include <assert.h> #include <stdbool.h> #include <stdlib.h> diff --git a/examples/dmabuf-capture.c b/examples/dmabuf-capture.c index 2c510b5a..abdb146a 100644 --- a/examples/dmabuf-capture.c +++ b/examples/dmabuf-capture.c @@ -12,7 +12,7 @@ #include <unistd.h> #include <pthread.h> #include <stdbool.h> -#include <libdrm/drm_fourcc.h> +#include <drm_fourcc.h> #include "wlr-export-dmabuf-unstable-v1-client-protocol.h" struct wayland_output { diff --git a/examples/input-method.c b/examples/input-method.c new file mode 100644 index 00000000..3e485d1c --- /dev/null +++ b/examples/input-method.c @@ -0,0 +1,402 @@ +#define _POSIX_C_SOURCE 200809L +#include <assert.h> +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/epoll.h> +#include <sys/timerfd.h> +#include <unistd.h> +#include <wayland-client.h> +#include <wayland-egl.h> +#include <wlr/render/egl.h> +#include "input-method-unstable-v2-client-protocol.h" +#include "text-input-unstable-v3-client-protocol.h" +#include "xdg-shell-client-protocol.h" + +const char usage[] = "Usage: input-method [seconds]\n\ +\n\ +Creates an input method using the input-method protocol.\n\ +\n\ +Whenever a text input is activated, this program sends a few sequences of\n\ +commands and checks the validity of the responses, relying on returned\n\ +surrounding text.\n\ +\n\ +The \"seconds\" argument is optional and defines the maximum delay between\n\ +stages."; + +struct input_method_state { + enum zwp_text_input_v3_change_cause change_cause; + struct { + enum zwp_text_input_v3_content_hint hint; + enum zwp_text_input_v3_content_purpose purpose; + } content_type; + struct { + char *text; + uint32_t cursor; + uint32_t anchor; + } surrounding; +}; + +static int sleeptime = 0; + +static struct wl_display *display = NULL; +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct zwp_input_method_manager_v2 *input_method_manager = NULL; +static struct zwp_input_method_v2 *input_method = NULL; + +struct input_method_state pending; +struct input_method_state current; + +static uint32_t serial = 0; +bool active = false; +bool pending_active = false; +bool unavailable = false; +bool running = false; + +uint32_t update_stage = 0; + +int timer_fd = 0; + +static void print_state_diff(struct input_method_state previous, + struct input_method_state future) { + if (previous.content_type.hint != future.content_type.hint) { + char *strs[] = { "COMPLETION", "SPELLCHECK", "AUTO_CAPITALIZATION", + "LOWERCASE", "UPPERCASE", "TITLECASE", "HIDDEN_TEXT", + "SENSITIVE_DATA", "LATIN", "MULTILINE"}; + printf("content_type.hint:"); + uint32_t hint = future.content_type.hint; + if (!hint) { + printf(" NONE"); + } + for (unsigned i = 0; i < sizeof(strs) / sizeof(*strs); i++) { + if (hint & 1 << i) { + printf(" %s", strs[i]); + } + } + printf("\n"); + } + if (previous.content_type.purpose != future.content_type.purpose) { + char *strs[] = { "NORMAL", "ALPHA", "DIGITS", "NUMBER", "PHONE", "URL", + "EMAIL", "NAME", "PASSWORD", "PIN", "DATE", "TIME", "DATETIME", + "TERMINAL" }; + printf("content_type.purpose: %s\n", strs[future.content_type.purpose]); + } + if (!!previous.surrounding.text != !!future.surrounding.text + || (previous.surrounding.text && future.surrounding.text + && strcmp(previous.surrounding.text, future.surrounding.text) != 0) + || previous.surrounding.anchor != future.surrounding.anchor + || previous.surrounding.cursor != future.surrounding.cursor) { + char *text = future.surrounding.text; + if (!text) { + printf("Removed surrounding text\n"); + } else { + printf("Surrounding text: %s\n", text); + uint32_t anchor = future.surrounding.anchor; + uint32_t cursor = future.surrounding.cursor; + if (cursor == anchor) { + char *temp = strndup(text, cursor); + printf("Cursor after %d: %s\n", cursor, temp); + free(temp); + } else { + if (cursor > anchor) { + uint32_t tmp = anchor; + anchor = cursor; + cursor = tmp; + } + char *temp = strndup(&text[cursor], anchor - cursor); + printf("Selection: %s\n", temp); + free(temp); + } + } + } + if (previous.change_cause != future.change_cause) { + char *strs[] = { "INPUT_METHOD", "OTHER" }; + printf("Change cause: %s\n", strs[future.change_cause]); + } +} + +static void handle_content_type(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + uint32_t hint, uint32_t purpose) { + pending.content_type.hint = hint; + pending.content_type.purpose = purpose; +} + +static void handle_surrounding_text(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + const char *text, uint32_t cursor, uint32_t anchor) { + free(pending.surrounding.text); + pending.surrounding.text = strdup(text); + pending.surrounding.cursor = cursor; + pending.surrounding.anchor = anchor; +} + +static void handle_text_change_cause(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2, + uint32_t cause) { + pending.change_cause = cause; +} + +static void handle_activate(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + pending_active = true; +} + +static void handle_deactivate(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + pending_active = false; +} + +static void handle_unavailable(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + printf("IM disappeared\n"); + zwp_input_method_v2_destroy(zwp_input_method_v2); + input_method = NULL; + running = false; +} + +static void im_activate(void *data, + struct zwp_input_method_v2 *id) { + update_stage = 0; +} + +static void timer_arm(unsigned seconds) { + printf("Timer armed\n"); + struct itimerspec spec = { + .it_interval = {0}, + .it_value = { + .tv_sec = seconds, + .tv_nsec = 0 + } + }; + if (timerfd_settime(timer_fd, 0, &spec, NULL)) { + fprintf(stderr, "Failed to arm timer: %s\n", strerror(errno)); + } +} + +static void do_updates() { + printf("Update %d\n", update_stage); + switch (update_stage) { + case 0: + // TODO: remember initial surrounding text + zwp_input_method_v2_set_preedit_string(input_method, "Preedit", 2, 4); + zwp_input_method_v2_commit(input_method, serial); + // don't expect an answer, preedit doesn't change anything visible + timer_arm(sleeptime); + update_stage++; + return; + case 1: + zwp_input_method_v2_set_preedit_string(input_method, "Præedit2", strlen("Pr"), strlen("Præed")); + zwp_input_method_v2_commit_string(input_method, "_Commit_"); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 2: + if (strcmp(current.surrounding.text, "_Commit_") != 0) { + return; + } + zwp_input_method_v2_commit_string(input_method, "_CommitNoPreed_"); + zwp_input_method_v2_commit(input_method, serial); + timer_arm(sleeptime); + update_stage++; + break; + case 3: + if (strcmp(current.surrounding.text, "_Commit__CommitNoPreed_") != 0) { + return; + } + zwp_input_method_v2_commit_string(input_method, "_WaitNo_"); + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("_CommitNoPreed_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 4: + if (strcmp(current.surrounding.text, "_Commit__WaitNo_") != 0) { + return; + } + zwp_input_method_v2_set_preedit_string(input_method, "PreedWithDel", strlen("Preed"), strlen("Preed")); + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("_WaitNo_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 5: + if (strcmp(current.surrounding.text, "_Commit_") != 0) { + return; + } + zwp_input_method_v2_delete_surrounding_text(input_method, strlen("mit_"), 0); + zwp_input_method_v2_commit(input_method, serial); + update_stage++; + break; + case 6: + if (strcmp(current.surrounding.text, "_Com") != 0) { + printf("Failed\n"); + } + update_stage++; + break; + default: + printf("Submitted everything\n"); + return; + }; +} + +static void handle_timer() { + printf("Timer dispatched at %d\n", update_stage); + do_updates(); +} + +static void im_deactivate(void *data, + struct zwp_input_method_v2 *context) { + // No special action needed +} + +static void handle_done(void *data, + struct zwp_input_method_v2 *zwp_input_method_v2) { + bool prev_active = active; + serial++; + printf("Handle serial %d\n", serial); + if (active != pending_active) { + printf("Now %s\n", pending_active ? "active" : "inactive"); + } + if (pending_active) { + print_state_diff(current, pending); + } + active = pending_active; + free(current.surrounding.text); + struct input_method_state default_state = {0}; + current = pending; + pending = default_state; + if (active && !prev_active) { + im_activate(data, zwp_input_method_v2); + } else if (!active && prev_active) { + im_deactivate(data, zwp_input_method_v2); + } + + do_updates(); +} + +static const struct zwp_input_method_v2_listener im_listener = { + .activate = handle_activate, + .deactivate = handle_deactivate, + .surrounding_text = handle_surrounding_text, + .text_change_cause = handle_text_change_cause, + .content_type = handle_content_type, + .done = handle_done, + .unavailable = handle_unavailable, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, "wl_compositor") == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, zwp_input_method_manager_v2_interface.name) == 0) { + input_method_manager = wl_registry_bind(registry, name, + &zwp_input_method_manager_v2_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, version); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // who cares +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +int main(int argc, char **argv) { + if (argc > 1) { + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { + printf(usage); + return 0; + } + sleeptime = atoi(argv[1]); + } + display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "Failed to create display\n"); + return EXIT_FAILURE; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (compositor == NULL) { + fprintf(stderr, "wl-compositor not available\n"); + return EXIT_FAILURE; + } + if (input_method_manager == NULL) { + fprintf(stderr, "input-method not available\n"); + return EXIT_FAILURE; + } + if (seat == NULL) { + fprintf(stderr, "seat not available\n"); + return EXIT_FAILURE; + } + + input_method = zwp_input_method_manager_v2_get_input_method( + input_method_manager, seat); + running = true; + zwp_input_method_v2_add_listener(input_method, &im_listener, NULL); + + int display_fd = wl_display_get_fd(display); + timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); + if (timer_fd < 0) { + fprintf(stderr, "Failed to start timer\n"); + return EXIT_FAILURE; + } + int epoll = epoll_create1(EPOLL_CLOEXEC); + if (epoll < 0) { + fprintf(stderr, "Failed to start epoll\n"); + return EXIT_FAILURE; + } + + struct epoll_event epoll_display = { + .events = EPOLLIN | EPOLLOUT, + .data = {.fd = display_fd}, + }; + if (epoll_ctl(epoll, EPOLL_CTL_ADD, display_fd, &epoll_display)) { + fprintf(stderr, "Failed to epoll display\n"); + return EXIT_FAILURE; + } + + wl_display_roundtrip(display); // timer may be armed here + + struct epoll_event epoll_timer = { + .events = EPOLLIN, + .data = {.fd = timer_fd}, + }; + if (epoll_ctl(epoll, EPOLL_CTL_ADD, timer_fd, &epoll_timer)) { + fprintf(stderr, "Failed to epoll timer\n"); + return EXIT_FAILURE; + } + + timer_arm(2); + + struct epoll_event caught; + while (epoll_wait(epoll, &caught, 1, -1)) { + if (!running) { + printf("Exiting\n"); + return EXIT_SUCCESS; + } + if (caught.data.fd == display_fd) { + if (wl_display_dispatch(display) == -1) { + break; + } + } else if (caught.data.fd == timer_fd) { + uint64_t expirations; + ssize_t n = read(timer_fd, &expirations, sizeof(expirations)); + assert(n >= 0); + handle_timer(); + } else { + printf("Unknown source\n"); + } + } + return EXIT_SUCCESS; +} diff --git a/examples/meson.build b/examples/meson.build index 86c0ddbb..369c7049 100644 --- a/examples/meson.build +++ b/examples/meson.build @@ -6,6 +6,13 @@ libavutil = dependency('libavutil', version: '>=56.14.100', required: false) libavcodec = dependency('libavcodec', version: '>=58.18.100', required: false) libavformat = dependency('libavformat', version: '>=58.12.100', required: false) +# epoll is a separate library in FreeBSD +if host_machine.system() == 'freebsd' + libepoll = [dependency('epoll-shim')] +else + libepoll = [] +endif + # Small hack until https://github.com/mesonbuild/meson/pull/3386/ is merged foreach dep : ['libpng', 'libavutil', 'libavcodec', 'libavformat'] if not get_variable(dep).found() @@ -80,6 +87,7 @@ examples = { libavcodec, libavformat, libavutil, + drm.partial_dependency(compile_args: true), # <drm_fourcc.h> threads, wayland_client, wlr_protos, @@ -94,6 +102,14 @@ examples = { 'src': 'toplevel-decoration.c', 'dep': [wayland_client, wlr_protos, wlroots], }, + 'input-method': { + 'src': 'input-method.c', + 'dep': [wayland_client, wlr_protos, wlroots] + libepoll, + }, + 'text-input': { + 'src': 'text-input.c', + 'dep': [wayland_cursor, wayland_client, wlr_protos, wlroots], + }, } foreach name, info : examples diff --git a/examples/text-input.c b/examples/text-input.c new file mode 100644 index 00000000..3ccd99a0 --- /dev/null +++ b/examples/text-input.c @@ -0,0 +1,394 @@ +#define _POSIX_C_SOURCE 200809L +#include <GLES2/gl2.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <wayland-client.h> +#include <wayland-egl.h> +#include <wlr/render/egl.h> +#include "text-input-unstable-v3-client-protocol.h" +#include "xdg-shell-client-protocol.h" + +const char usage[] = "Usage: text-input [seconds [width height]]\n\ +\n\ +Creates a xdg-toplevel using the text-input protocol.\n\ +It will be solid black when it has no text input focus, yellow when it\n\ +has focus, and red when it was notified that the focus moved away\n\ +but still didn't give up the text input ability.\n\ +\n\ +The \"seconds\" argument is optional and defines the delay between getting\n\ +notified of lost focus and releasing text input.\n\ +\n\ +The \"width\" and \"height\" arguments define the window shape.\n\ +\n\ +The console will print the internal state of the text field:\n\ +- the text in the 1st line\n\ +- \".\" under each preedit character\n\ +- \"_\" under each selected preedit character\n\ +- \"|\" at the cursor position if there are no selected characters in the\n\ +preedit.\n\ +\n\ +The cursor positions may be inaccurate, especially in presence of zero-width\n\ +characters or non-monospaced fonts.\n"; + +struct text_input_state { + char *commit; + struct { + char *text; + int32_t cursor_begin; + int32_t cursor_end; + } preedit; + struct { + uint32_t after_length; + uint32_t before_length; + } delete_surrounding; +}; + +static struct text_input_state pending = {0}; +static struct text_input_state current = {0}; +static bool entered = false; +static uint32_t serial; +static char *buffer; // text buffer +// cursor is not present, there's no way to move it outside of preedit + +static int sleeptime = 0; +static int width = 100, height = 200; +static int enabled = 0; + +static struct wl_display *display = NULL; +static struct wl_compositor *compositor = NULL; +static struct wl_seat *seat = NULL; +static struct xdg_wm_base *wm_base = NULL; +static struct zwp_text_input_manager_v3 *text_input_manager = NULL; +static struct zwp_text_input_v3 *text_input = NULL; + +struct wlr_egl egl; +struct wl_egl_window *egl_window; +struct wlr_egl_surface *egl_surface; + +static void draw(void) { + eglMakeCurrent(egl.display, egl_surface, egl_surface, egl.context); + + float color[] = {1.0, 1.0, 0.0, 1.0}; + color[0] = enabled * 1.0; + color[1] = entered * 1.0; + + glViewport(0, 0, width, height); + glClearColor(color[0], color[1], color[2], 1.0); + glClear(GL_COLOR_BUFFER_BIT); + + eglSwapBuffers(egl.display, egl_surface); +} + +static size_t utf8_strlen(char *str) { + size_t cp_count = 0; + for (; *str != '\0'; str++) { + if ((*str & 0xc0) != 0x80) { + cp_count++; + } + } + return cp_count; +} + +static size_t utf8_offset(char *utf8_str, size_t byte_offset) { + size_t cp_count = 0; + for (char *c = utf8_str; c < utf8_str + byte_offset; c++) { + if ((*c & 0xc0) != 0x80) { + cp_count++; + } + } + return cp_count; +} + +// TODO: would be nicer to have this text display inside the window +static void show_status() { + printf("State %d:", serial); + if (!enabled) { + printf(" disabled"); + } + + char *preedit_text = current.preedit.text; + if (!preedit_text) { + preedit_text = ""; + } + + printf("\n"); + printf("%s", buffer); + printf("%s\n", preedit_text); + + // Positioning of the cursor requires UTF8 offsets to match monospaced + // glyphs + for (unsigned i = 0; i < utf8_strlen(buffer); i++) { + printf(" "); + } + char *cursor_mark = calloc(utf8_strlen(preedit_text) + 2, sizeof(char)); + for (unsigned i = 0; i < utf8_strlen(preedit_text); i++) { + cursor_mark[i] = '.'; + } + if (current.preedit.cursor_begin == -1 + && current.preedit.cursor_end == -1) { + goto end; + } + if (current.preedit.cursor_begin == -1 + || current.preedit.cursor_end == -1) { + printf("Only one cursor side is defined: %d to %d\n", + current.preedit.cursor_begin, current.preedit.cursor_end); + goto end; + } + + if ((unsigned)current.preedit.cursor_begin > strlen(preedit_text) + || (unsigned)current.preedit.cursor_begin > strlen(preedit_text)) { + printf("Cursor out of bounds\n"); + goto end; + } + + if (current.preedit.cursor_begin == current.preedit.cursor_end) { + cursor_mark[utf8_offset(preedit_text, current.preedit.cursor_begin)] + = '|'; + goto print; + } + + if (current.preedit.cursor_begin > current.preedit.cursor_end) { + printf("End cursor is before start cursor\n"); + goto end; + } + + // negative offsets already checked before + for (unsigned i = utf8_offset(preedit_text, current.preedit.cursor_begin); + i < utf8_offset(preedit_text, current.preedit.cursor_end); i++) { + cursor_mark[i] = '_'; + } +print: + printf("%s\n", cursor_mark); +end: + free(cursor_mark); +} + +static void commit(struct zwp_text_input_v3 *text_input) { + zwp_text_input_v3_commit(text_input); + serial++; +} + +static void send_status_update(struct zwp_text_input_v3 *text_input) { + zwp_text_input_v3_set_surrounding_text(text_input, buffer, strlen(buffer), strlen(buffer)); + zwp_text_input_v3_set_text_change_cause(text_input, ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD); + commit(text_input); +} + +static void text_input_handle_enter(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + struct wl_surface *surface) { + entered = true; + zwp_text_input_v3_enable(zwp_text_input_v3); + commit(zwp_text_input_v3); + enabled = true; + draw(); + show_status(); +} + +static void text_input_handle_leave(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + struct wl_surface *surface) { + entered = false; + draw(); + wl_display_roundtrip(display); + sleep(sleeptime); + zwp_text_input_v3_disable(zwp_text_input_v3); + commit(zwp_text_input_v3); + enabled = false; + draw(); + show_status(); +} + +static void text_input_commit_string(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + const char *text) { + free(pending.commit); + pending.commit = strdup(text); +} + +static void text_input_delete_surrounding_text(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + uint32_t before_length, uint32_t after_length) { + pending.delete_surrounding.before_length = before_length; + pending.delete_surrounding.after_length = after_length; +} + +static void text_input_preedit_string(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + const char *text, int32_t cursor_begin, int32_t cursor_end) { + free(pending.preedit.text); + pending.preedit.text = strdup(text); + pending.preedit.cursor_begin = cursor_begin; + pending.preedit.cursor_end = cursor_end; +} + +static void text_input_handle_done(void *data, + struct zwp_text_input_v3 *zwp_text_input_v3, + uint32_t incoming_serial) { + if (serial != incoming_serial) { + fprintf(stderr, "Received serial %d while expecting %d\n", incoming_serial, serial); + return; + } + free(current.preedit.text); + free(current.commit); + current = pending; + struct text_input_state empty = {0}; + pending = empty; + + if (current.delete_surrounding.after_length + current.delete_surrounding.before_length > 0) { + // cursor is always after committed text, after_length != 0 will never happen + unsigned delete_before = current.delete_surrounding.before_length; + if (delete_before > strlen(buffer)) { + delete_before = strlen(buffer); + } + buffer[strlen(buffer) - delete_before] = '\0'; + } + + char *commit_string = current.commit; + if (!commit_string) { + commit_string = ""; + } + char *old_buffer = buffer; + buffer = calloc(strlen(buffer) + strlen(commit_string) + 1, sizeof(char)); // realloc may fail anyway + strcpy(buffer, old_buffer); + free(old_buffer); + strcat(buffer, commit_string); + + send_status_update(zwp_text_input_v3); + show_status(); +} + +static const struct zwp_text_input_v3_listener text_input_listener = { + .enter = text_input_handle_enter, + .leave = text_input_handle_leave, + .commit_string = text_input_commit_string, + .delete_surrounding_text = text_input_delete_surrounding_text, + .preedit_string = text_input_preedit_string, + .done = text_input_handle_done, +}; + +static void xdg_surface_handle_configure(void *data, + struct xdg_surface *xdg_surface, uint32_t serial) { + xdg_surface_ack_configure(xdg_surface, serial); + wl_egl_window_resize(egl_window, width, height, 0, 0); + draw(); +} + +static const struct xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_handle_configure, +}; + +static void xdg_toplevel_handle_configure(void *data, + struct xdg_toplevel *xdg_toplevel, int32_t w, int32_t h, + struct wl_array *states) { + width = w; + height = h; +} + +static void xdg_toplevel_handle_close(void *data, + struct xdg_toplevel *xdg_toplevel) { + exit(EXIT_SUCCESS); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + .configure = xdg_toplevel_handle_configure, + .close = xdg_toplevel_handle_close, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + if (strcmp(interface, "wl_compositor") == 0) { + compositor = wl_registry_bind(registry, name, + &wl_compositor_interface, 1); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + wm_base = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + } else if (strcmp(interface, zwp_text_input_manager_v3_interface.name) == 0) { + text_input_manager = wl_registry_bind(registry, name, + &zwp_text_input_manager_v3_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + seat = wl_registry_bind(registry, name, &wl_seat_interface, version); + } +} + +static void handle_global_remove(void *data, struct wl_registry *registry, + uint32_t name) { + // who cares +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, +}; + +int main(int argc, char **argv) { + if (argc > 1) { + if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { + printf(usage); + return 0; + } + sleeptime = atoi(argv[1]); + if (argc > 3) { + width = atoi(argv[2]); + height = atoi(argv[3]); + } + } + + buffer = calloc(1, sizeof(char)); + + display = wl_display_connect(NULL); + if (display == NULL) { + fprintf(stderr, "Failed to create display\n"); + return EXIT_FAILURE; + } + + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + wl_display_dispatch(display); + wl_display_roundtrip(display); + + if (compositor == NULL) { + fprintf(stderr, "wl-compositor not available\n"); + return EXIT_FAILURE; + } + if (wm_base == NULL) { + fprintf(stderr, "xdg-shell not available\n"); + return EXIT_FAILURE; + } + if (text_input_manager == NULL) { + fprintf(stderr, "text-input not available\n"); + return EXIT_FAILURE; + } + + text_input = zwp_text_input_manager_v3_get_text_input(text_input_manager, seat); + + zwp_text_input_v3_add_listener(text_input, &text_input_listener, NULL); + + + wlr_egl_init(&egl, EGL_PLATFORM_WAYLAND_EXT, display, NULL, + WL_SHM_FORMAT_ARGB8888); + + struct wl_surface *surface = wl_compositor_create_surface(compositor); + struct xdg_surface *xdg_surface = + xdg_wm_base_get_xdg_surface(wm_base, surface); + struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface); + + xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, NULL); + xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, NULL); + + wl_surface_commit(surface); + + egl_window = wl_egl_window_create(surface, width, height); + egl_surface = wlr_egl_create_surface(&egl, egl_window); + + wl_display_roundtrip(display); + + draw(); + + while (wl_display_dispatch(display) != -1) { + // This space intentionally left blank + } + + return EXIT_SUCCESS; +} diff --git a/include/backend/drm/properties.h b/include/backend/drm/properties.h index 5b17e77e..321b4492 100644 --- a/include/backend/drm/properties.h +++ b/include/backend/drm/properties.h @@ -14,12 +14,13 @@ union wlr_drm_connector_props { struct { uint32_t edid; uint32_t dpms; + uint32_t link_status; // not guaranteed to exist // atomic-modesetting only uint32_t crtc_id; }; - uint32_t props[3]; + uint32_t props[4]; }; union wlr_drm_crtc_props { diff --git a/include/backend/x11.h b/include/backend/x11.h index 38aaad50..37e3e4b6 100644 --- a/include/backend/x11.h +++ b/include/backend/x11.h @@ -3,6 +3,7 @@ #include <stdbool.h> #include <wayland-server.h> +#include <wlr/config.h> #include <wlr/backend/x11.h> #include <wlr/interfaces/wlr_input_device.h> #include <wlr/interfaces/wlr_output.h> diff --git a/include/render/gles2.h b/include/render/gles2.h index f649f3e2..7ff2f174 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -19,7 +19,7 @@ extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; struct wlr_gles2_pixel_format { - uint32_t wl_format; + enum wl_shm_format wl_format; GLint gl_format, gl_type; int depth, bpp; bool has_alpha; @@ -72,6 +72,7 @@ struct wlr_gles2_texture { enum wlr_gles2_texture_type type; int width, height; bool has_alpha; + enum wl_shm_format wl_format; // used to interpret upload data bool inverted_y; // Not set if WLR_GLES2_TEXTURE_GLTEX diff --git a/include/rootston/desktop.h b/include/rootston/desktop.h index 8cee170f..345c9c09 100644 --- a/include/rootston/desktop.h +++ b/include/rootston/desktop.h @@ -9,6 +9,7 @@ #include <wlr/types/wlr_idle_inhibit_v1.h> #include <wlr/types/wlr_idle.h> #include <wlr/types/wlr_input_inhibitor.h> +#include <wlr/types/wlr_input_method_v2.h> #include <wlr/types/wlr_layer_shell_v1.h> #include <wlr/types/wlr_list.h> #include <wlr/types/wlr_output_layout.h> @@ -17,6 +18,7 @@ #include <wlr/types/wlr_primary_selection.h> #include <wlr/types/wlr_screencopy_v1.h> #include <wlr/types/wlr_screenshooter.h> +#include <wlr/types/wlr_text_input_v3.h> #include <wlr/types/wlr_virtual_keyboard_v1.h> #include <wlr/types/wlr_wl_shell.h> #include <wlr/types/wlr_xcursor_manager.h> @@ -54,6 +56,8 @@ struct roots_desktop { struct wlr_idle_inhibit_manager_v1 *idle_inhibit; struct wlr_input_inhibit_manager *input_inhibit; struct wlr_layer_shell_v1 *layer_shell; + struct wlr_input_method_manager_v2 *input_method; + struct wlr_text_input_manager_v3 *text_input; struct wlr_virtual_keyboard_manager_v1 *virtual_keyboard; struct wlr_screencopy_manager_v1 *screencopy; struct wlr_tablet_manager_v2 *tablet_v2; diff --git a/include/rootston/seat.h b/include/rootston/seat.h index 7e7ee3e9..0187c6cc 100644 --- a/include/rootston/seat.h +++ b/include/rootston/seat.h @@ -5,6 +5,7 @@ #include "rootston/input.h" #include "rootston/keyboard.h" #include "rootston/layers.h" +#include "rootston/text_input.h" struct roots_seat { struct roots_input *input; @@ -19,6 +20,8 @@ struct roots_seat { // If the focused layer is set, views cannot receive keyboard focus struct wlr_layer_surface_v1 *focused_layer; + struct roots_input_method_relay im_relay; + // If non-null, only this client can receive input events struct wl_client *exclusive_client; diff --git a/include/rootston/text_input.h b/include/rootston/text_input.h new file mode 100644 index 00000000..82e45e3e --- /dev/null +++ b/include/rootston/text_input.h @@ -0,0 +1,63 @@ +#ifndef ROOTSTON_TEXT_INPUT_H +#define ROOTSTON_TEXT_INPUT_H + +#include <wlr/types/wlr_text_input_v3.h> +#include <wlr/types/wlr_input_method_v2.h> +#include <wlr/types/wlr_surface.h> +#include "rootston/seat.h" + +/** + * The relay structure manages the relationship between text-input and + * input_method interfaces on a given seat. Multiple text-input interfaces may + * be bound to a relay, but at most one will be focused (reveiving events) at + * a time. At most one input-method interface may be bound to the seat. The + * relay manages life cycle of both sides. When both sides are present and + * focused, the relay passes messages between them. + * + * Text input focus is a subset of keyboard focus - if the text-input is + * in the focused state, wl_keyboard sent an enter as well. However, having + * wl_keyboard focused doesn't mean that text-input will be focused. + */ +struct roots_input_method_relay { + struct roots_seat *seat; + + struct wl_list text_inputs; // roots_text_input::link + struct wlr_input_method_v2 *input_method; // doesn't have to be present + + struct wl_listener text_input_new; + struct wl_listener text_input_enable; + struct wl_listener text_input_commit; + struct wl_listener text_input_disable; + struct wl_listener text_input_destroy; + + struct wl_listener input_method_new; + struct wl_listener input_method_commit; + struct wl_listener input_method_destroy; +}; + +struct roots_text_input { + struct roots_input_method_relay *relay; + + struct wlr_text_input_v3 *input; + // The surface getting seat's focus. Stored for when text-input cannot + // be sent an enter event immediately after getting focus, e.g. when + // there's no input method available. Cleared once text-input is entered. + struct wlr_surface *pending_focused_surface; + + struct wl_list link; + + struct wl_listener pending_focused_surface_destroy; +}; + +void roots_input_method_relay_init(struct roots_seat *seat, + struct roots_input_method_relay *relay); + +// Updates currently focused surface. Surface must belong to the same seat. +void roots_input_method_relay_set_focus(struct roots_input_method_relay *relay, + struct wlr_surface *surface); + +struct roots_text_input *roots_text_input_create( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input); + +#endif diff --git a/include/wlr/render/dmabuf.h b/include/wlr/render/dmabuf.h index 33c3a129..32cfe874 100644 --- a/include/wlr/render/dmabuf.h +++ b/include/wlr/render/dmabuf.h @@ -16,6 +16,10 @@ #define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1) #endif +#ifndef DRM_FORMAT_MOD_LINEAR +#define DRM_FORMAT_MOD_LINEAR 0 +#endif + #define WLR_DMABUF_MAX_PLANES 4 enum wlr_dmabuf_attributes_flags { diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 63f4265c..905d419f 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -66,9 +66,9 @@ struct wlr_texture_impl { void (*get_size)(struct wlr_texture *texture, int *width, int *height); bool (*is_opaque)(struct wlr_texture *texture); bool (*write_pixels)(struct wlr_texture *texture, - enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width, - uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, - uint32_t dst_y, const void *data); + uint32_t stride, uint32_t width, uint32_t height, + uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, + const void *data); bool (*to_dmabuf)(struct wlr_texture *texture, struct wlr_dmabuf_attributes *attribs); void (*destroy)(struct wlr_texture *texture); diff --git a/include/wlr/render/wlr_texture.h b/include/wlr/render/wlr_texture.h index dbfabfee..f210717a 100644 --- a/include/wlr/render/wlr_texture.h +++ b/include/wlr/render/wlr_texture.h @@ -54,10 +54,11 @@ void wlr_texture_get_size(struct wlr_texture *texture, int *width, int *height); bool wlr_texture_is_opaque(struct wlr_texture *texture); /** - * Update a texture with raw pixels. The texture must be mutable. - */ + * Update a texture with raw pixels. The texture must be mutable, and the input + * data must have the same pixel format that the texture was created with. + */ bool wlr_texture_write_pixels(struct wlr_texture *texture, - enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width, uint32_t height, + uint32_t stride, uint32_t width, uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, const void *data); diff --git a/include/wlr/types/meson.build b/include/wlr/types/meson.build index 7a02c3da..3f61ae20 100644 --- a/include/wlr/types/meson.build +++ b/include/wlr/types/meson.build @@ -11,6 +11,7 @@ install_headers( 'wlr_idle.h', 'wlr_input_device.h', 'wlr_input_inhibitor.h', + 'wlr_input_method_v2.h', 'wlr_keyboard.h', 'wlr_layer_shell_v1.h', 'wlr_linux_dmabuf_v1.h', @@ -31,6 +32,7 @@ install_headers( 'wlr_tablet_pad.h', 'wlr_tablet_tool.h', 'wlr_tablet_v2.h', + 'wlr_text_input_v3.h', 'wlr_touch.h', 'wlr_virtual_keyboard_v1.h', 'wlr_wl_shell.h', diff --git a/include/wlr/types/wlr_input_method_v2.h b/include/wlr/types/wlr_input_method_v2.h new file mode 100644 index 00000000..d22d54d1 --- /dev/null +++ b/include/wlr/types/wlr_input_method_v2.h @@ -0,0 +1,87 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_INPUT_METHOD_V2_H +#define WLR_TYPES_WLR_INPUT_METHOD_V2_H +#include <stdint.h> +#include <stdlib.h> +#include <wayland-server.h> +#include <wlr/types/wlr_seat.h> + +struct wlr_input_method_v2_preedit_string { + char *text; + int32_t cursor_begin; + int32_t cursor_end; +}; + +struct wlr_input_method_v2_delete_surrounding_text { + uint32_t before_length; + uint32_t after_length; +}; + +struct wlr_input_method_v2_state { + struct wlr_input_method_v2_preedit_string preedit; + char *commit_text; + struct wlr_input_method_v2_delete_surrounding_text delete; +}; + +struct wlr_input_method_v2 { + struct wl_resource *resource; + + struct wlr_seat *seat; + + struct wlr_input_method_v2_state pending; + struct wlr_input_method_v2_state current; + bool active; // pending compositor-side state + bool client_active; // state known to the client + uint32_t current_serial; // received in last commit call + + struct wl_list link; + + struct wl_listener seat_destroy; + + struct { + struct wl_signal commit; // (struct wlr_input_method_v2*) + struct wl_signal destroy; // (struct wlr_input_method_v2*) + } events; +}; + +struct wlr_input_method_manager_v2 { + struct wl_global *global; + struct wl_list bound_resources; // struct wl_resource*::link + struct wl_list input_methods; // struct wlr_input_method_v2*::link + + struct wl_listener display_destroy; + + struct { + struct wl_signal input_method; // (struct wlr_input_method_v2*) + struct wl_signal destroy; // (struct wlr_input_method_manager_v2*) + } events; +}; + +struct wlr_input_method_manager_v2 *wlr_input_method_manager_v2_create( + struct wl_display *display); +void wlr_input_method_manager_v2_destroy( + struct wlr_input_method_manager_v2 *manager); + +void wlr_input_method_v2_send_activate( + struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_deactivate( + struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_surrounding_text( + struct wlr_input_method_v2 *input_method, const char *text, + uint32_t cursor, uint32_t anchor); +void wlr_input_method_v2_send_content_type( + struct wlr_input_method_v2 *input_method, uint32_t hint, + uint32_t purpose); +void wlr_input_method_v2_send_text_change_cause( + struct wlr_input_method_v2 *input_method, uint32_t cause); +void wlr_input_method_v2_send_done(struct wlr_input_method_v2 *input_method); +void wlr_input_method_v2_send_unavailable( + struct wlr_input_method_v2 *input_method); +#endif diff --git a/include/wlr/types/wlr_text_input_v3.h b/include/wlr/types/wlr_text_input_v3.h new file mode 100644 index 00000000..0db0cf47 --- /dev/null +++ b/include/wlr/types/wlr_text_input_v3.h @@ -0,0 +1,93 @@ +/* + * This an unstable interface of wlroots. No guarantees are made regarding the + * future consistency of this API. + */ +#ifndef WLR_USE_UNSTABLE +#error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features" +#endif + +#ifndef WLR_TYPES_WLR_TEXT_INPUT_V3_H +#define WLR_TYPES_WLR_TEXT_INPUT_V3_H + +#include <wayland-server.h> +#include <wlr/types/wlr_seat.h> +#include <wlr/types/wlr_surface.h> + +struct wlr_text_input_v3_state { + struct { + char *text; // NULL is allowed and equivalent to empty string + uint32_t cursor; + uint32_t anchor; + } surrounding; + + uint32_t text_change_cause; + + struct { + uint32_t hint; + uint32_t purpose; + } content_type; + + struct { + int32_t x; + int32_t y; + int32_t width; + int32_t height; + } cursor_rectangle; +}; + +struct wlr_text_input_v3 { + struct wlr_seat *seat; // becomes null when seat destroyed + struct wl_resource *resource; + struct wlr_surface *focused_surface; + struct wlr_text_input_v3_state pending; + struct wlr_text_input_v3_state current; + uint32_t current_serial; // next in line to send + bool pending_enabled; + bool current_enabled; + + struct wl_list link; + + struct wl_listener surface_destroy; + struct wl_listener seat_destroy; + + struct { + struct wl_signal enable; // (struct wlr_text_input_v3*) + struct wl_signal commit; // (struct wlr_text_input_v3*) + struct wl_signal disable; // (struct wlr_text_input_v3*) + struct wl_signal destroy; // (struct wlr_text_input_v3*) + } events; +}; + +struct wlr_text_input_manager_v3 { + struct wl_global *global; + + struct wl_list bound_resources; // struct wl_resource*::link + struct wl_list text_inputs; // struct wlr_text_input_v3::resource::link + + struct wl_listener display_destroy; + + struct { + struct wl_signal text_input; // (struct wlr_text_input_v3*) + struct wl_signal destroy; // (struct wlr_input_method_manager_v3*) + } events; +}; + +struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create( + struct wl_display *wl_display); +void wlr_text_input_manager_v3_destroy( + struct wlr_text_input_manager_v3 *manager); + +// Sends enter to the surface and saves it +void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input, + struct wlr_surface *wlr_surface); +// Sends leave to the currently focused surface and clears it +void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input); +void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input, + const char *text, uint32_t cursor_begin, uint32_t cursor_end); +void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input, + const char *text); +void wlr_text_input_v3_send_delete_surrounding_text( + struct wlr_text_input_v3 *text_input, uint32_t before_length, + uint32_t after_length); +void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input); +#endif diff --git a/meson.build b/meson.build index 9bd71191..9d40dbfc 100644 --- a/meson.build +++ b/meson.build @@ -42,7 +42,7 @@ endif # Avoid wl_buffer deprecation warnings add_project_arguments('-DWL_HIDE_DEPRECATED', language: 'c') -wayland_server = dependency('wayland-server', version: '>=1.15') +wayland_server = dependency('wayland-server', version: '>=1.16') wayland_client = dependency('wayland-client') wayland_egl = dependency('wayland-egl') wayland_protos = dependency('wayland-protocols', version: '>=1.15') @@ -55,7 +55,7 @@ xkbcommon = dependency('xkbcommon') udev = dependency('libudev') pixman = dependency('pixman-1') libcap = dependency('libcap', required: get_option('libcap')) -logind = dependency('lib' + get_option('logind-provider'), required: get_option('logind')) +logind = dependency('lib' + get_option('logind-provider'), required: get_option('logind'), version: '>=237') math = cc.find_library('m', required: false) wlr_parts = [] diff --git a/protocol/input-method-unstable-v2.xml b/protocol/input-method-unstable-v2.xml new file mode 100644 index 00000000..62be9d94 --- /dev/null +++ b/protocol/input-method-unstable-v2.xml @@ -0,0 +1,490 @@ +<?xml version="1.0" encoding="UTF-8"?> +<protocol name="input_method_unstable_v2"> + + <copyright> + Copyright © 2008-2011 Kristian Høgsberg + Copyright © 2010-2011 Intel Corporation + Copyright © 2012-2013 Collabora, Ltd. + Copyright © 2012, 2013 Intel Corporation + Copyright © 2015, 2016 Jan Arne Petersen + Copyright © 2017, 2018 Red Hat, Inc. + Copyright © 2018 Purism SPC + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + </copyright> + + <description summary="Protocol for creating input methods"> + This protocol allows applications to act as input methods for compositors. + + An input method context is used to manage the state of the input method. + + Text strings are UTF-8 encoded, their indices and lengths are in bytes. + + This document adheres to the RFC 2119 when using words like "must", + "should", "may", etc. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + </description> + + <interface name="zwp_input_method_v2" version="1"> + <description summary="input method"> + An input method object allows for clients to compose text. + + The objects connects the client to a text input in an application, and + lets the client to serve as an input method for a seat. + + The zwp_input_method_v2 object can occupy two distinct states: active and + inactive. In the active state, the object is associated to and + communicates with a text input. In the inactive state, there is no + associated text input, and the only communication is with the compositor. + Initially, the input method is in the inactive state. + + Requests issued in the inactive state must be accepted by the compositor. + Because of the serial mechanism, and the state reset on activate event, + they will not have any effect on the state of the next text input. + + There must be no more than one input method object per seat. + </description> + + <event name="activate"> + <description summary="input method has been requested"> + Notification that a text input focused on this seat requested the input + method to be activated. + + This event serves the purpose of providing the compositor with an + active input method. + + This event resets all state associated with previous enable, disable, + surrounding_text, text_change_cause, and content_type events, as well + as the state associated with set_preedit_string, commit_string, and + delete_surrounding_text requests. In addition, it marks the + zwp_input_method_v2 object as active, and makes any existing + zwp_input_popup_surface_v2 objects visible. + + The surrounding_text, and content_type events must follow before the + next done event if the text input supports the respective + functionality. + + State set with this event is double-buffered. It will get applied on + the next zwp_input_method_v2.done event, and stay valid until changed. + </description> + </event> + + <event name="deactivate"> + <description summary="deactivate event"> + Notification that no focused text input currently needs an active + input method on this seat. + + This event marks the zwp_input_method_v2 object as inactive. The + compositor must make all existing zwp_input_popup_surface_v2 objects + invisible until the next activate event. + + State set with this event is double-buffered. It will get applied on + the next zwp_input_method_v2.done event, and stay valid until changed. + </description> + </event> + + <event name="surrounding_text"> + <description summary="surrounding text event"> + Updates the surrounding plain text around the cursor, excluding the + preedit text. + + If any preedit text is present, it is replaced with the cursor for the + purpose of this event. + + The argument text is a buffer containing the preedit string, and must + include the cursor position, and the complete selection. It should + contain additional characters before and after these. There is a + maximum length of wayland messages, so text can not be longer than 4000 + bytes. + + cursor is the byte offset of the cursor within the text buffer. + + anchor is the byte offset of the selection anchor within the text + buffer. If there is no selected text, anchor must be the same as + cursor. + + If this event does not arrive before the first done event, the input + method may assume that the text input does not support this + functionality and ignore following surrounding_text events. + + Values set with this event are double-buffered. They will get applied + and set to initial values on the next zwp_input_method_v2.done + event. + + The initial state for affected fields is empty, meaning that the text + input does not support sending surrounding text. If the empty values + get applied, subsequent attempts to change them may have no effect. + </description> + <arg name="text" type="string"/> + <arg name="cursor" type="uint"/> + <arg name="anchor" type="uint"/> + </event> + + <event name="text_change_cause"> + <description summary="indicates the cause of surrounding text change"> + Tells the input method why the text surrounding the cursor changed. + + Whenever the client detects an external change in text, cursor, or + anchor position, it must issue this request to the compositor. This + request is intended to give the input method a chance to update the + preedit text in an appropriate way, e.g. by removing it when the user + starts typing with a keyboard. + + cause describes the source of the change. + + The value set with this event is double-buffered. It will get applied + and set to its initial value on the next zwp_input_method_v2.done + event. + + The initial value of cause is input_method. + </description> + <arg name="cause" type="uint" enum="zwp_text_input_v3.change_cause"/> + </event> + + <event name="content_type"> + <description summary="content purpose and hint"> + Indicates the content type and hint for the current + zwp_input_method_v2 instance. + + Values set with this event are double-buffered. They will get applied + on the next zwp_input_method_v2.done event. + + The initial value for hint is none, and the initial value for purpose + is normal. + </description> + <arg name="hint" type="uint" enum="zwp_text_input_v3.content_hint"/> + <arg name="purpose" type="uint" enum="zwp_text_input_v3.content_purpose"/> + </event> + + <event name="done"> + <description summary="apply state"> + Atomically applies state changes recently sent to the client. + + The done event establishes and updates the state of the client, and + must be issued after any changes to apply them. + + Text input state (content purpose, content hint, surrounding text, and + change cause) is conceptually double-buffered within an input method + context. + + Events modify the pending state, as opposed to the current state in use + by the input method. A done event atomically applies all pending state, + replacing the current state. After done, the new pending state is as + documented for each related request. + + Events must be applied in the order of arrival. + + Neither current nor pending state are modified unless noted otherwise. + </description> + </event> + + <request name="commit_string"> + <description summary="commit string"> + Send the commit string text for insertion to the application. + + Inserts a string at current cursor position (see commit event + sequence). The string to commit could be either just a single character + after a key press or the result of some composing. + + The argument text is a buffer containing the string to insert. There is + a maximum length of wayland messages, so text can not be longer than + 4000 bytes. + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.commit request. + + The initial value of text is an empty string. + </description> + <arg name="text" type="string"/> + </request> + + <request name="set_preedit_string"> + <description summary="pre-edit string"> + Send the pre-edit string text to the application text input. + + Place a new composing text (pre-edit) at the current cursor position. + Any previously set composing text must be removed. Any previously + existing selected text must be removed. The cursor is moved to a new + position within the preedit string. + + The argument text is a buffer containing the preedit string. There is + a maximum length of wayland messages, so text can not be longer than + 4000 bytes. + + The arguments cursor_begin and cursor_end are counted in bytes relative + to the beginning of the submitted string buffer. Cursor should be + hidden by the text input when both are equal to -1. + + cursor_begin indicates the beginning of the cursor. cursor_end + indicates the end of the cursor. It may be equal or different than + cursor_begin. + + Values set with this event are double-buffered. They must be applied on + the next zwp_input_method_v2.commit event. + + The initial value of text is an empty string. The initial value of + cursor_begin, and cursor_end are both 0. + </description> + <arg name="text" type="string"/> + <arg name="cursor_begin" type="int"/> + <arg name="cursor_end" type="int"/> + </request> + + <request name="delete_surrounding_text"> + <description summary="delete text"> + Remove the surrounding text. + + before_length and after_length are the number of bytes before and after + the current cursor index (excluding the preedit text) to delete. + + If any preedit text is present, it is replaced with the cursor for the + purpose of this event. In effect before_length is counted from the + beginning of preedit text, and after_length from its end (see commit + event sequence). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_input_method_v2.commit request. + + The initial values of both before_length and after_length are 0. + </description> + <arg name="before_length" type="uint"/> + <arg name="after_length" type="uint"/> + </request> + + <request name="commit"> + <description summary="apply state"> + Apply state changes from commit_string, set_preedit_string and + delete_surrounding_text requests. + + The state relating to these events is double-buffered, and each one + modifies the pending state. This request replaces the current state + with the pending state. + + The connected text input is expected to proceed by evaluating the + changes in the following order: + + 1. Replace existing preedit string with the cursor. + 2. Delete requested surrounding text. + 3. Insert commit string with the cursor at its end. + 4. Calculate surrounding text to send. + 5. Insert new preedit text in cursor position. + 6. Place cursor inside preedit text. + + The serial number reflects the last state of the zwp_input_method_v2 + object known to the client. The value of the serial argument must be + equal to the number of done events already issued by that object. When + the compositor receives a commit request with a serial different than + the number of past done events, it must proceed as normal, except it + should not change the current state of the zwp_input_method_v2 object. + </description> + <arg name="serial" type="uint"/> + </request> + + <request name="get_input_popup_surface"> + <description summary="create popup surface"> + Creates a new zwp_input_popup_surface_v2 object wrapping a given + surface. + + The surface gets assigned the "input_popup" role. If the surface + already has an assigned role, the compositor must issue a protocol + error. + </description> + <arg name="id" type="new_id" interface="zwp_input_popup_surface_v2"/> + <arg name="surface" type="object" interface="wl_surface"/> + </request> + + <request name="grab_keyboard"> + <description summary="grab hardware keyboard"> + Allow an input method to receive hardware keyboard input and process + key events to generate text events (with pre-edit) over the wire. This + allows input methods which compose multiple key events for inputting + text like it is done for CJK languages. + + The compositor should send all keyboard events on the seat to the grab + holder via the returned wl_keyboard object. Nevertheless, the + compositor may decide not to forward any particular event. The + compositor must not further process any event after it has been + forwarded to the grab holder. + + Releasing the resulting wl_keyboard object releases the grab. + </description> + <arg name="keyboard" type="new_id" + interface="zwp_input_method_keyboard_grab_v2"/> + </request> + + <event name="unavailable"> + <description summary="input method unavailable"> + The input method ceased to be available. + + The compositor must issue this event as the only event on the object if + there was another input_method object associated with the same seat at + the time of its creation. + + The compositor must issue this request when the object is no longer + useable, e.g. due to seat removal. + + The input method context becomes inert and should be destroyed after + deactivation is handled. Any further requests and events except for the + destroy request must be ignored. + </description> + </event> + + <request name="destroy" type="destructor"> + <description summary="destroy the text input"> + Destroys the zwp_text_input_v2 object and any associated child + objects, i.e. zwp_input_popup_surface_v2 and + zwp_input_method_keyboard_grab_v2. + </description> + </request> + </interface> + + <interface name="zwp_input_popup_surface_v2" version="1"> + <description summary="popup surface"> + This interface marks a surface as a popup for interacting with an input + method. + + The compositor should place it near the active text input area. It must + be visible if and only if the input method is in the active state. + + The client must not destroy the underlying wl_surface while the + zwp_input_popup_surface_v2 object exists. + </description> + + <event name="text_input_rectangle"> + <description summary="set text input area position"> + Notify about the position of the area of the text input expressed as a + rectangle in surface local coordinates. + + This is a hint to the input method telling it the relative position of + the text being entered. + </description> + <arg name="x" type="int"/> + <arg name="y" type="int"/> + <arg name="width" type="int"/> + <arg name="height" type="int"/> + </event> + + <request name="destroy" type="destructor"/> + </interface> + + <interface name="zwp_input_method_keyboard_grab_v2" version="1"> + <!-- Closely follows wl_keyboard version 6 --> + <description summary="keyboard grab"> + The zwp_input_method_keyboard_grab_v2 interface represents an exclusive + grab of the wl_keyboard interface associated with the seat. + </description> + + <event name="keymap"> + <description summary="keyboard mapping"> + This event provides a file descriptor to the client which can be + memory-mapped to provide a keyboard mapping description. + </description> + <arg name="format" type="uint" enum="wl_keyboard.keymap_format" + summary="keymap format"/> + <arg name="fd" type="fd" summary="keymap file descriptor"/> + <arg name="size" type="uint" summary="keymap size, in bytes"/> + </event> + + <event name="key"> + <description summary="key event"> + A key was pressed or released. + The time argument is a timestamp with millisecond granularity, with an + undefined base. + </description> + <arg name="serial" type="uint" summary="serial number of the key event"/> + <arg name="time" type="uint" summary="timestamp with millisecond granularity"/> + <arg name="key" type="uint" summary="key that produced the event"/> + <arg name="state" type="uint" enum="wl_keyboard.key_state" + summary="physical state of the key"/> + </event> + + <event name="modifiers"> + <description summary="modifier and group state"> + Notifies clients that the modifier and/or group state has changed, and + it should update its local state. + </description> + <arg name="serial" type="uint" summary="serial number of the modifiers event"/> + <arg name="mods_depressed" type="uint" summary="depressed modifiers"/> + <arg name="mods_latched" type="uint" summary="latched modifiers"/> + <arg name="mods_locked" type="uint" summary="locked modifiers"/> + <arg name="group" type="uint" summary="keyboard layout"/> + </event> + + <request name="release" type="destructor"> + <description summary="release the grab object"/> + </request> + + <event name="repeat_info"> + <description summary="repeat rate and delay"> + Informs the client about the keyboard's repeat rate and delay. + + This event is sent as soon as the zwp_input_method_keyboard_grab_v2 + object has been created, and is guaranteed to be received by the + client before any key press event. + + Negative values for either rate or delay are illegal. A rate of zero + will disable any repeating (regardless of the value of delay). + + This event can be sent later on as well with a new value if necessary, + so clients should continue listening for the event past the creation + of zwp_input_method_keyboard_grab_v2. + </description> + <arg name="rate" type="int" + summary="the rate of repeating keys in characters per second"/> + <arg name="delay" type="int" + summary="delay in milliseconds since key down until repeating starts"/> + </event> + </interface> + + <interface name="zwp_input_method_manager_v2" version="1"> + <description summary="input method manager"> + The input method manager allows the client to become the input method on + a chosen seat. + + No more than one input method must be associated with any seat at any + given time. + </description> + + <request name="get_input_method"> + <description summary="request an input method object"> + Request a new input zwp_input_method_v2 object associated with a given + seat. + </description> + <arg name="seat" type="object" interface="wl_seat"/> + <arg name="input_method" type="new_id" interface="zwp_input_method_v2"/> + </request> + + <request name="destroy" type="destructor"> + <description summary="destroy the input method manager"> + Destroys the zwp_input_method_manager_v2 object. + + The zwp_input_method_v2 objects originating from it remain valid. + </description> + </request> + </interface> +</protocol> diff --git a/protocol/meson.build b/protocol/meson.build index 82131ff6..dfe2a5ec 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -23,8 +23,10 @@ protocols = [ 'gamma-control.xml', 'gtk-primary-selection.xml', 'idle.xml', + 'input-method-unstable-v2.xml', 'screenshooter.xml', 'server-decoration.xml', + 'text-input-unstable-v3.xml', 'virtual-keyboard-unstable-v1.xml', 'wlr-export-dmabuf-unstable-v1.xml', 'wlr-gamma-control-unstable-v1.xml', @@ -40,7 +42,9 @@ client_protocols = [ [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], [wl_protocol_dir, 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml'], 'idle.xml', + 'input-method-unstable-v2.xml', 'screenshooter.xml', + 'text-input-unstable-v3.xml', 'wlr-export-dmabuf-unstable-v1.xml', 'wlr-gamma-control-unstable-v1.xml', 'wlr-input-inhibitor-unstable-v1.xml', diff --git a/protocol/text-input-unstable-v3.xml b/protocol/text-input-unstable-v3.xml new file mode 100644 index 00000000..8b710fd6 --- /dev/null +++ b/protocol/text-input-unstable-v3.xml @@ -0,0 +1,441 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<protocol name="text_input_unstable_v3"> + <copyright> + Copyright © 2012, 2013 Intel Corporation + Copyright © 2015, 2016 Jan Arne Petersen + Copyright © 2017, 2018 Red Hat, Inc. + Copyright © 2018 Purism SPC + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that copyright notice and this permission + notice appear in supporting documentation, and that the name of + the copyright holders not be used in advertising or publicity + pertaining to distribution of the software without specific, + written prior permission. The copyright holders make no + representations about the suitability of this software for any + purpose. It is provided "as is" without express or implied + warranty. + + THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS + SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND + FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY + SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN + AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, + ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF + THIS SOFTWARE. + </copyright> + + <description summary="Protocol for composing text"> + This protocol allows compositors to act as input methods and to send text + to applications. A text input object is used to manage state of what are + typically text entry fields in the application. + + This document adheres to the RFC 2119 when using words like "must", + "should", "may", etc. + + Warning! The protocol described in this file is experimental and + backward incompatible changes may be made. Backward compatible changes + may be added together with the corresponding interface version bump. + Backward incompatible changes are done by bumping the version number in + the protocol and interface names and resetting the interface version. + Once the protocol is to be declared stable, the 'z' prefix and the + version number in the protocol and interface names are removed and the + interface version number is reset. + </description> + + <interface name="zwp_text_input_v3" version="1"> + <description summary="text input"> + The zwp_text_input_v3 interface represents text input and input methods + associated with a seat. It provides enter/leave events to follow the + text input focus for a seat. + + Requests are used to enable/disable the text-input object and set + state information like surrounding and selected text or the content type. + The information about the entered text is sent to the text-input object + via the preedit_string and commit_string events. + + Text is valid UTF-8 encoded, indices and lengths are in bytes. Indices + must not point to middle bytes inside a code point: they must either + point to the first byte of a code point or to the end of the buffer. + Lengths must be measured between two valid indices. + + Focus moving throughout surfaces will result in the emission of + zwp_text_input_v3.enter and zwp_text_input_v3.leave events. The focused + surface must commit zwp_text_input_v3.enable and + zwp_text_input_v3.disable requests as the keyboard focus moves across + editable and non-editable elements of the UI. Those two requests are not + expected to be paired with each other, the compositor must be able to + handle consecutive series of the same request. + + State is sent by the state requests (set_surrounding_text, + set_content_type and set_cursor_rectangle) and a commit request. After an + enter event or disable request all state information is invalidated and + needs to be resent by the client. + </description> + + <request name="destroy" type="destructor"> + <description summary="Destroy the wp_text_input"> + Destroy the wp_text_input object. Also disables all surfaces enabled + through this wp_text_input object. + </description> + </request> + + <request name="enable"> + <description summary="Request text input to be enabled"> + Requests text input on the surface previously obtained from the enter + event. + + This request must be issued every time the active text input changes + to a new one, including within the current surface. Use + zwp_text_input_v3.disable when there is no longer any input focus on + the current surface. + + This request resets all state associated with previous enable, disable, + set_surrounding_text, set_text_change_cause, set_content_type, and + set_cursor_rectangle requests, as well as the state associated with + preedit_string, commit_string, and delete_surrounding_text events. + + The set_surrounding_text, set_content_type and set_cursor_rectangle + requests must follow if the text input supports the necessary + functionality. + + State set with this request is double-buffered. It will get applied on + the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The changes must be applied by the compositor after issuing a + zwp_text_input_v3.commit request. + </description> + </request> + + <request name="disable"> + <description summary="Disable text input on a surface"> + Explicitly disable text input on the current surface (typically when + there is no focus on any text entry inside the surface). + + State set with this request is double-buffered. It will get applied on + the next zwp_text_input_v3.commit request. + </description> + </request> + + <request name="set_surrounding_text"> + <description summary="sets the surrounding text"> + Sets the surrounding plain text around the input, excluding the preedit + text. + + The client should notify the compositor of any changes in any of the + values carried with this request, including changes caused by handling + incoming text-input events as well as changes caused by other + mechanisms like keyboard typing. + + If the client is unaware of the text around the cursor, it should not + issue this request, to signify lack of support to the compositor. + + Text is UTF-8 encoded, and should include the cursor position, the + complete selection and additional characters before and after them. + There is a maximum length of wayland messages, so text can not be + longer than 4000 bytes. + + Cursor is the byte offset of the cursor within text buffer. + + Anchor is the byte offset of the selection anchor within text buffer. + If there is no selected text, anchor is the same as cursor. + + If any preedit text is present, it is replaced with a cursor for the + purpose of this event. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The initial state for affected fields is empty, meaning that the text + input does not support sending surrounding text. If the empty values + get applied, subsequent attempts to change them may have no effect. + </description> + <arg name="text" type="string"/> + <arg name="cursor" type="int"/> + <arg name="anchor" type="int"/> + </request> + + <enum name="change_cause"> + <description summary="text change reason"> + Reason for the change of surrounding text or cursor posision. + </description> + <entry name="input_method" value="0" summary="input method caused the change"/> + <entry name="other" value="1" summary="something else than the input method caused the change"/> + </enum> + + <request name="set_text_change_cause"> + <description summary="indicates the cause of surrounding text change"> + Tells the compositor why the text surrounding the cursor changed. + + Whenever the client detects an external change in text, cursor, or + anchor posision, it must issue this request to the compositor. This + request is intended to give the input method a chance to update the + preedit text in an appropriate way, e.g. by removing it when the user + starts typing with a keyboard. + + cause describes the source of the change. + + The value set with this request is double-buffered. It must be applied + and reset to initial at the next zwp_text_input_v3.commit request. + + The initial value of cause is input_method. + </description> + <arg name="cause" type="uint" enum="change_cause"/> + </request> + + <enum name="content_hint" bitfield="true"> + <description summary="content hint"> + Content hint is a bitmask to allow to modify the behavior of the text + input. + </description> + <entry name="none" value="0x0" summary="no special behavior"/> + <entry name="completion" value="0x1" summary="suggest word completions"/> + <entry name="spellcheck" value="0x2" summary="suggest word corrections"/> + <entry name="auto_capitalization" value="0x4" summary="switch to uppercase letters at the start of a sentence"/> + <entry name="lowercase" value="0x8" summary="prefer lowercase letters"/> + <entry name="uppercase" value="0x10" summary="prefer uppercase letters"/> + <entry name="titlecase" value="0x20" summary="prefer casing for titles and headings (can be language dependent)"/> + <entry name="hidden_text" value="0x40" summary="characters should be hidden"/> + <entry name="sensitive_data" value="0x80" summary="typed text should not be stored"/> + <entry name="latin" value="0x100" summary="just Latin characters should be entered"/> + <entry name="multiline" value="0x200" summary="the text input is multiline"/> + </enum> + + <enum name="content_purpose"> + <description summary="content purpose"> + The content purpose allows to specify the primary purpose of a text + input. + + This allows an input method to show special purpose input panels with + extra characters or to disallow some characters. + </description> + <entry name="normal" value="0" summary="default input, allowing all characters"/> + <entry name="alpha" value="1" summary="allow only alphabetic characters"/> + <entry name="digits" value="2" summary="allow only digits"/> + <entry name="number" value="3" summary="input a number (including decimal separator and sign)"/> + <entry name="phone" value="4" summary="input a phone number"/> + <entry name="url" value="5" summary="input an URL"/> + <entry name="email" value="6" summary="input an email address"/> + <entry name="name" value="7" summary="input a name of a person"/> + <entry name="password" value="8" summary="input a password (combine with sensitive_data hint)"/> + <entry name="pin" value="9" summary="input is a numeric password (combine with sensitive_data hint)"/> + <entry name="date" value="10" summary="input a date"/> + <entry name="time" value="11" summary="input a time"/> + <entry name="datetime" value="12" summary="input a date and time"/> + <entry name="terminal" value="13" summary="input for a terminal"/> + </enum> + + <request name="set_content_type"> + <description summary="set content purpose and hint"> + Sets the content purpose and content hint. While the purpose is the + basic purpose of an input field, the hint flags allow to modify some of + the behavior. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request. + Subsequent attempts to update them may have no effect. The values + remain valid until the next committed enable or disable request. + + The initial value for hint is none, and the initial value for purpose + is normal. + </description> + <arg name="hint" type="uint" enum="content_hint"/> + <arg name="purpose" type="uint" enum="content_purpose"/> + </request> + + <request name="set_cursor_rectangle"> + <description summary="set cursor position"> + Marks an area around the cursor as a x, y, width, height rectangle in + surface local coordinates. + + Allows the compositor to put a window with word suggestions near the + cursor, without obstructing the text being input. + + If the client is unaware of the position of edited text, it should not + issue this request, to signify lack of support to the compositor. + + Values set with this request are double-buffered. They will get applied + on the next zwp_text_input_v3.commit request, and stay valid until the + next committed enable or disable request. + + The initial values describing a cursor rectangle are empty. That means + the text input does not support describing the cursor area. If the + empty values get applied, subsequent attempts to change them may have + no effect. + </description> + <arg name="x" type="int"/> + <arg name="y" type="int"/> + <arg name="width" type="int"/> + <arg name="height" type="int"/> + </request> + + <request name="commit"> + <description summary="commit state"> + Atomically applies state changes recently sent to the compositor. + + The commit request establishes and updates the state of the client, and + must be issued after any changes to apply them. + + Text input state (enabled status, content purpose, content hint, + surrounding text and change cause, cursor rectangle) is conceptually + double-buffered within the context of a text input, i.e. between a + committed enable request and the following committed enable or disable + request. + + Protocol requests modify the pending state, as opposed to the current + state in use by the input method. A commit request atomically applies + all pending state, replacing the current state. After commit, the new + pending state is as documented for each related request. + + Requests are applied in the order of arrival. + + Neither current nor pending state are modified unless noted otherwise. + + The compositor must count the number of commit requests coming from + each zwp_text_input_v3 object and use the count as the serial in done + events. + </description> + </request> + + <event name="enter"> + <description summary="enter event"> + Notification that this seat's text-input focus is on a certain surface. + + When the seat has the keyboard capability the text-input focus follows + the keyboard focus. This event sets the current surface for the + text-input object. + </description> + <arg name="surface" type="object" interface="wl_surface"/> + </event> + + <event name="leave"> + <description summary="leave event"> + Notification that this seat's text-input focus is no longer on a + certain surface. The client should reset any preedit string previously + set. + + The leave notification clears the current surface. It is sent before + the enter notification for the new focus. + + When the seat has the keyboard capability the text-input focus follows + the keyboard focus. + </description> + <arg name="surface" type="object" interface="wl_surface"/> + </event> + + <event name="preedit_string"> + <description summary="pre-edit"> + Notify when a new composing text (pre-edit) should be set at the + current cursor position. Any previously set composing text must be + removed. Any previously existing selected text must be removed. + + The argument text contains the pre-edit string buffer. + + The parameters cursor_begin and cursor_end are counted in bytes + relative to the beginning of the submitted text buffer. Cursor should + be hidden when both are equal to -1. + + They could be represented by the client as a line if both values are + the same, or as a text highlight otherwise. + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial value of text is an empty string, and cursor_begin, + cursor_end and cursor_hidden are all 0. + </description> + <arg name="text" type="string" allow-null="true"/> + <arg name="cursor_begin" type="int"/> + <arg name="cursor_end" type="int"/> + </event> + + <event name="commit_string"> + <description summary="text commit"> + Notify when text should be inserted into the editor widget. The text to + commit could be either just a single character after a key press or the + result of some composing (pre-edit). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial value of text is an empty string. + </description> + <arg name="text" type="string" allow-null="true"/> + </event> + + <event name="delete_surrounding_text"> + <description summary="delete surrounding text"> + Notify when the text around the current cursor position should be + deleted. + + Before_length and after_length are the number of bytes before and after + the current cursor index (excluding the selection) to delete. + + If a preedit text is present, in effect before_length is counted from + the beginning of it, and after_length from its end (see done event + sequence). + + Values set with this event are double-buffered. They must be applied + and reset to initial on the next zwp_text_input_v3.done event. + + The initial values of both before_length and after_length are 0. + </description> + <arg name="before_length" type="uint" summary="length of text before current cursor position"/> + <arg name="after_length" type="uint" summary="length of text after current cursor position"/> + </event> + + <event name="done"> + <description summary="apply changes"> + Instruct the application to apply changes to state requested by the + preedit_string, commit_string and delete_surrounding_text events. The + state relating to these events is double-buffered, and each one + modifies the pending state. This event replaces the current state with + the pending state. + + The application must proceed by evaluating the changes in the following + order: + + 1. Replace existing preedit string with the cursor. + 2. Delete requested surrounding text. + 3. Insert commit string with the cursor at its end. + 4. Calculate surrounding text to send. + 5. Insert new preedit text in cursor position. + 6. Place cursor inside preedit text. + + The serial number reflects the last state of the zwp_text_input_v3 + object known to the compositor. The value of the serial argument must + be equal to the number of commit requests already issued on that object. + When the client receives a done event with a serial different than the + number of past commit requests, it must proceed as normal, except it + should not change the current state of the zwp_text_input_v3 object. + </description> + <arg name="serial" type="uint"/> + </event> + </interface> + + <interface name="zwp_text_input_manager_v3" version="1"> + <description summary="text input manager"> + A factory for text-input objects. This object is a global singleton. + </description> + + <request name="destroy" type="destructor"> + <description summary="Destroy the wp_text_input_manager"> + Destroy the wp_text_input_manager object. + </description> + </request> + + <request name="get_text_input"> + <description summary="create a new text input object"> + Creates a new text-input object for a given seat. + </description> + <arg name="id" type="new_id" interface="zwp_text_input_v3"/> + <arg name="seat" type="object" interface="wl_seat"/> + </request> + </interface> +</protocol> diff --git a/render/egl.c b/render/egl.c index 96e48c06..cfa37f20 100644 --- a/render/egl.c +++ b/render/egl.c @@ -3,6 +3,7 @@ #include <EGL/egl.h> #include <EGL/eglext.h> #include <stdlib.h> +#include <drm_fourcc.h> #include <wlr/render/egl.h> #include <wlr/util/log.h> #include "glapi.h" @@ -120,7 +121,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, if (platform == EGL_PLATFORM_SURFACELESS_MESA) { assert(remote_display == NULL); - egl->display = eglGetDisplay(EGL_DEFAULT_DISPLAY); + egl->display = eglGetPlatformDisplayEXT(platform, EGL_DEFAULT_DISPLAY, NULL); } else { egl->display = eglGetPlatformDisplayEXT(platform, remote_display, NULL); } @@ -382,13 +383,21 @@ EGLImageKHR wlr_egl_create_image_from_wl_drm(struct wlr_egl *egl, EGLImageKHR wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, struct wlr_dmabuf_attributes *attributes) { - if (!egl->exts.image_base_khr) { + if (!egl->exts.image_base_khr || !egl->exts.image_dmabuf_import_ext) { + wlr_log(WLR_ERROR, "dmabuf import extension not present"); return NULL; } bool has_modifier = false; - if (attributes->modifier != DRM_FORMAT_MOD_INVALID) { + + // we assume the same way we assumed formats without the import_modifiers + // extension that mod_linear is supported. The special mod mod_invalid + // is sometimes used to signal modifier unawareness which is what we + // have here + if (attributes->modifier != DRM_FORMAT_MOD_INVALID && + attributes->modifier != DRM_FORMAT_MOD_LINEAR) { if (!egl->exts.image_dmabuf_import_modifiers_ext) { + wlr_log(WLR_ERROR, "dmabuf modifiers extension not present"); return NULL; } has_modifier = true; @@ -460,12 +469,34 @@ EGLImageKHR wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, int **formats) { - if (!egl->exts.image_dmabuf_import_ext || - !egl->exts.image_dmabuf_import_modifiers_ext) { - wlr_log(WLR_DEBUG, "dmabuf extension not present"); + if (!egl->exts.image_dmabuf_import_ext) { + wlr_log(WLR_DEBUG, "dmabuf import extension not present"); return -1; } + // when we only have the image_dmabuf_import extension we can't query + // which formats are supported. These two are on almost always + // supported; it's the intended way to just try to create buffers. + // Just a guess but better than not supporting dmabufs at all, + // given that the modifiers extension isn't supported everywhere. + if (!egl->exts.image_dmabuf_import_modifiers_ext) { + static const int fallback_formats[] = { + DRM_FORMAT_ARGB8888, + DRM_FORMAT_XRGB8888, + }; + static unsigned num = sizeof(fallback_formats) / + sizeof(fallback_formats[0]); + + *formats = calloc(num, sizeof(int)); + if (!*formats) { + wlr_log_errno(WLR_ERROR, "Allocation failed"); + return -1; + } + + memcpy(*formats, fallback_formats, num * sizeof(**formats)); + return num; + } + EGLint num; if (!eglQueryDmaBufFormatsEXT(egl->display, 0, NULL, &num)) { wlr_log(WLR_ERROR, "failed to query number of dmabuf formats"); @@ -488,12 +519,16 @@ int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, int format, uint64_t **modifiers) { - if (!egl->exts.image_dmabuf_import_ext || - !egl->exts.image_dmabuf_import_modifiers_ext) { + if (!egl->exts.image_dmabuf_import_ext) { wlr_log(WLR_DEBUG, "dmabuf extension not present"); return -1; } + if(!egl->exts.image_dmabuf_import_modifiers_ext) { + *modifiers = NULL; + return 0; + } + EGLint num; if (!eglQueryDmaBufModifiersEXT(egl->display, format, 0, NULL, NULL, &num)) { diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 22d02cde..d035841e 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -44,9 +44,9 @@ static bool gles2_texture_is_opaque(struct wlr_texture *wlr_texture) { } static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture, - enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width, - uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, - uint32_t dst_y, const void *data) { + uint32_t stride, uint32_t width, uint32_t height, + uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, + const void *data) { struct wlr_gles2_texture *texture = get_gles2_texture_in_context(wlr_texture); @@ -55,11 +55,9 @@ static bool gles2_texture_write_pixels(struct wlr_texture *wlr_texture, return false; } - const struct wlr_gles2_pixel_format *fmt = get_gles2_format_from_wl(wl_fmt); - if (fmt == NULL) { - wlr_log(WLR_ERROR, "Unsupported pixel format %"PRIu32, wl_fmt); - return false; - } + const struct wlr_gles2_pixel_format *fmt = + get_gles2_format_from_wl(texture->wl_format); + assert(fmt); // TODO: what if the unpack subimage extension isn't supported? PUSH_GLES2_DEBUG; @@ -167,6 +165,7 @@ struct wlr_texture *wlr_gles2_texture_from_pixels(struct wlr_egl *egl, texture->height = height; texture->type = WLR_GLES2_TEXTURE_GLTEX; texture->has_alpha = fmt->has_alpha; + texture->wl_format = fmt->wl_format; PUSH_GLES2_DEBUG; @@ -203,6 +202,7 @@ struct wlr_texture *wlr_gles2_texture_from_wl_drm(struct wlr_egl *egl, texture->wl_drm = data; EGLint fmt; + texture->wl_format = 0xFFFFFFFF; // texture can't be written anyways texture->image = wlr_egl_create_image_from_wl_drm(egl, data, &fmt, &texture->width, &texture->height, &texture->inverted_y); if (texture->image == NULL) { @@ -283,6 +283,7 @@ struct wlr_texture *wlr_gles2_texture_from_dmabuf(struct wlr_egl *egl, texture->height = attribs->height; texture->type = WLR_GLES2_TEXTURE_DMABUF; texture->has_alpha = true; + texture->wl_format = 0xFFFFFFFF; // texture can't be written anyways texture->inverted_y = (attribs->flags & WLR_DMABUF_ATTRIBUTES_FLAGS_Y_INVERT) != 0; diff --git a/render/meson.build b/render/meson.build index ab66eab0..e45ea90b 100644 --- a/render/meson.build +++ b/render/meson.build @@ -22,7 +22,13 @@ lib_wlr_render = static_library( ), glapi, include_directories: wlr_inc, - dependencies: [egl, glesv2, pixman, wayland_server], + dependencies: [ + egl, + drm.partial_dependency(compile_args: true), # <drm_fourcc.h> + glesv2, + pixman, + wayland_server + ], ) wlr_render = declare_dependency( diff --git a/render/wlr_texture.c b/render/wlr_texture.c index 06872f1e..833032c9 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -55,10 +55,10 @@ bool wlr_texture_is_opaque(struct wlr_texture *texture) { } bool wlr_texture_write_pixels(struct wlr_texture *texture, - enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width, - uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, - uint32_t dst_y, const void *data) { - return texture->impl->write_pixels(texture, wl_fmt, stride, width, height, + uint32_t stride, uint32_t width, uint32_t height, + uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, + const void *data) { + return texture->impl->write_pixels(texture, stride, width, height, src_x, src_y, dst_x, dst_y, data); } diff --git a/rootston/desktop.c b/rootston/desktop.c index 7f749050..06b785ad 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -1,4 +1,4 @@ -#define _POSIX_C_SOURCE 199309L +#define _POSIX_C_SOURCE 200112L #include <assert.h> #include <math.h> #include <stdlib.h> @@ -881,7 +881,6 @@ struct roots_desktop *desktop_create(struct roots_server *server, desktop->tablet_v2 = wlr_tablet_v2_create(server->wl_display); -#ifdef WLR_HAS_XWAYLAND const char *cursor_theme = NULL; const char *cursor_default = ROOTS_XCURSOR_DEFAULT; struct roots_cursor_config *cc = @@ -893,6 +892,15 @@ struct roots_desktop *desktop_create(struct roots_server *server, } } + char cursor_size_fmt[16]; + snprintf(cursor_size_fmt, sizeof(cursor_size_fmt), + "%d", ROOTS_XCURSOR_SIZE); + setenv("XCURSOR_SIZE", cursor_size_fmt, 1); + if (cursor_theme != NULL) { + setenv("XCURSOR_THEME", cursor_theme, 1); + } + +#ifdef WLR_HAS_XWAYLAND desktop->xcursor_manager = wlr_xcursor_manager_create(cursor_theme, ROOTS_XCURSOR_SIZE); if (desktop->xcursor_manager == NULL) { @@ -949,6 +957,11 @@ struct roots_desktop *desktop_create(struct roots_server *server, wl_signal_add(&desktop->input_inhibit->events.deactivate, &desktop->input_inhibit_deactivate); + desktop->input_method = + wlr_input_method_manager_v2_create(server->wl_display); + + desktop->text_input = wlr_text_input_manager_v3_create(server->wl_display); + desktop->virtual_keyboard = wlr_virtual_keyboard_manager_v1_create( server->wl_display); wl_signal_add(&desktop->virtual_keyboard->events.new_virtual_keyboard, diff --git a/rootston/meson.build b/rootston/meson.build index 9d1decce..d650dc51 100644 --- a/rootston/meson.build +++ b/rootston/meson.build @@ -9,6 +9,7 @@ sources = [ 'main.c', 'output.c', 'seat.c', + 'text_input.c', 'virtual_keyboard.c', 'wl_shell.c', 'xdg_shell.c', diff --git a/rootston/output.c b/rootston/output.c index 9d376f8e..bd38f3ab 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -459,7 +459,8 @@ static void render_output(struct roots_output *output) { output_box->y; view_move(view, view_x, view_y); - if (has_standalone_surface(view)) { + if (has_standalone_surface(view) && + wl_list_empty(&output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY])) { wlr_output_set_fullscreen_surface(wlr_output, view->wlr_surface); } else { wlr_output_set_fullscreen_surface(wlr_output, NULL); diff --git a/rootston/seat.c b/rootston/seat.c index c41c32ed..82444dcb 100644 --- a/rootston/seat.c +++ b/rootston/seat.c @@ -16,6 +16,7 @@ #include "rootston/input.h" #include "rootston/keyboard.h" #include "rootston/seat.h" +#include "rootston/text_input.h" #include "rootston/xcursor.h" @@ -605,6 +606,8 @@ struct roots_seat *roots_seat_create(struct roots_input *input, char *name) { return NULL; } + roots_input_method_relay_init(seat, &seat->im_relay); + wl_list_insert(&input->seats, &seat->link); seat->new_drag_icon.notify = roots_seat_handle_new_drag_icon; @@ -1182,6 +1185,7 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { if (view == NULL) { seat->cursor->mode = ROOTS_CURSOR_PASSTHROUGH; wlr_seat_keyboard_clear_focus(seat->seat); + roots_input_method_relay_set_focus(&seat->im_relay, NULL); return; } @@ -1220,6 +1224,8 @@ void roots_seat_set_focus(struct roots_seat *seat, struct roots_view *view) { if (seat->cursor) { roots_cursor_update_focus(seat->cursor); } + + roots_input_method_relay_set_focus(&seat->im_relay, view->wlr_surface); } /** diff --git a/rootston/text_input.c b/rootston/text_input.c new file mode 100644 index 00000000..70c92761 --- /dev/null +++ b/rootston/text_input.c @@ -0,0 +1,310 @@ +#include <assert.h> +#include <stdlib.h> +#include <wlr/util/log.h> +#include "rootston/seat.h" +#include "rootston/text_input.h" + +static struct roots_text_input *relay_get_focusable_text_input( + struct roots_input_method_relay *relay) { + struct roots_text_input *text_input = NULL; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->pending_focused_surface) { + return text_input; + } + } + return NULL; +} + +static struct roots_text_input *relay_get_focused_text_input( + struct roots_input_method_relay *relay) { + struct roots_text_input *text_input = NULL; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->input->focused_surface) { + return text_input; + } + } + return NULL; +} + +static void handle_im_commit(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_commit); + + struct roots_text_input *text_input = relay_get_focused_text_input(relay); + if (!text_input) { + return; + } + struct wlr_input_method_v2 *context = data; + assert(context == relay->input_method); + if (context->current.preedit.text) { + wlr_text_input_v3_send_preedit_string(text_input->input, + context->current.preedit.text, + context->current.preedit.cursor_begin, + context->current.preedit.cursor_end); + } + if (context->current.commit_text) { + wlr_text_input_v3_send_commit_string(text_input->input, + context->current.commit_text); + } + if (context->current.delete.before_length + || context->current.delete.after_length) { + wlr_text_input_v3_send_delete_surrounding_text(text_input->input, + context->current.delete.before_length, + context->current.delete.after_length); + } + wlr_text_input_v3_send_done(text_input->input); +} + +static void text_input_set_pending_focused_surface( + struct roots_text_input *text_input, struct wlr_surface *surface) { + text_input->pending_focused_surface = surface; + wl_signal_add(&surface->events.destroy, + &text_input->pending_focused_surface_destroy); +} + +static void text_input_clear_pending_focused_surface( + struct roots_text_input *text_input) { + wl_list_remove(&text_input->pending_focused_surface_destroy.link); + wl_list_init(&text_input->pending_focused_surface_destroy.link); + text_input->pending_focused_surface = NULL; +} + +static void handle_im_destroy(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_destroy); + struct wlr_input_method_v2 *context = data; + assert(context == relay->input_method); + relay->input_method = NULL; + struct roots_text_input *text_input = relay_get_focused_text_input(relay); + if (text_input) { + // keyboard focus is still there, so keep the surface at hand in case + // the input method returns + text_input_set_pending_focused_surface(text_input, + text_input->input->focused_surface); + wlr_text_input_v3_send_leave(text_input->input); + } +} + +static void relay_send_im_done(struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *input) { + struct wlr_input_method_v2 *input_method = relay->input_method; + if (!input_method) { + wlr_log(WLR_INFO, "Sending IM_DONE but im is gone"); + return; + } + // TODO: only send each of those if they were modified + wlr_input_method_v2_send_surrounding_text(input_method, + input->current.surrounding.text, input->current.surrounding.cursor, + input->current.surrounding.anchor); + wlr_input_method_v2_send_text_change_cause(input_method, + input->current.text_change_cause); + wlr_input_method_v2_send_content_type(input_method, + input->current.content_type.hint, input->current.content_type.purpose); + wlr_input_method_v2_send_done(input_method); + // TODO: pass intent, display popup size +} + +static struct roots_text_input *text_input_to_roots( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input) { + struct roots_text_input *roots_text_input = NULL; + wl_list_for_each(roots_text_input, &relay->text_inputs, link) { + if (roots_text_input->input == text_input) { + return roots_text_input; + } + } + return NULL; +} + +static void handle_text_input_enable(struct wl_listener *listener, void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_enable); + if (relay->input_method == NULL) { + wlr_log(WLR_INFO, "Enabling text input when input method is gone"); + return; + } + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + wlr_input_method_v2_send_activate(relay->input_method); + relay_send_im_done(relay, text_input->input); +} + +static void handle_text_input_commit(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_commit); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + if (!text_input->input->current_enabled) { + wlr_log(WLR_INFO, "Inactive text input tried to commit an update"); + return; + } + wlr_log(WLR_DEBUG, "Text input committed update"); + if (relay->input_method == NULL) { + wlr_log(WLR_INFO, "Text input committed, but input method is gone"); + return; + } + relay_send_im_done(relay, text_input->input); +} + +static void relay_disable_text_input(struct roots_input_method_relay *relay, + struct roots_text_input *text_input) { + if (relay->input_method == NULL) { + wlr_log(WLR_DEBUG, "Disabling text input, but input method is gone"); + return; + } + wlr_input_method_v2_send_deactivate(relay->input_method); + relay_send_im_done(relay, text_input->input); +} + +static void handle_text_input_disable(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_disable); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + relay_disable_text_input(relay, text_input); +} + +static void handle_text_input_destroy(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_destroy); + struct roots_text_input *text_input = text_input_to_roots(relay, + (struct wlr_text_input_v3*)data); + + if (text_input->input->current_enabled) { + relay_disable_text_input(relay, text_input); + } + text_input_clear_pending_focused_surface(text_input); + wl_list_remove(&text_input->link); + text_input->input = NULL; + free(text_input); +} + +static void handle_pending_focused_surface_destroy(struct wl_listener *listener, + void *data) { + struct roots_text_input *text_input = wl_container_of(listener, text_input, + pending_focused_surface_destroy); + struct wlr_surface *surface = data; + assert(text_input->pending_focused_surface == surface); + text_input->pending_focused_surface = NULL; +} + +struct roots_text_input *roots_text_input_create( + struct roots_input_method_relay *relay, + struct wlr_text_input_v3 *text_input) { + struct roots_text_input *input = calloc(1, sizeof(struct roots_text_input)); + if (!input) { + return NULL; + } + input->input = text_input; + input->relay = relay; + + wl_signal_add(&text_input->events.enable, &relay->text_input_enable); + relay->text_input_enable.notify = handle_text_input_enable; + + wl_signal_add(&text_input->events.commit, &relay->text_input_commit); + relay->text_input_commit.notify = handle_text_input_commit; + + wl_signal_add(&text_input->events.disable, &relay->text_input_disable); + relay->text_input_disable.notify = handle_text_input_disable; + + wl_signal_add(&text_input->events.destroy, &relay->text_input_destroy); + relay->text_input_destroy.notify = handle_text_input_destroy; + + input->pending_focused_surface_destroy.notify = + handle_pending_focused_surface_destroy; + wl_list_init(&input->pending_focused_surface_destroy.link); + return input; +} + +static void relay_handle_text_input(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + text_input_new); + struct wlr_text_input_v3 *wlr_text_input = data; + if (relay->seat->seat != wlr_text_input->seat) { + return; + } + + struct roots_text_input *text_input = roots_text_input_create(relay, + wlr_text_input); + if (!text_input) { + return; + } + wl_list_insert(&relay->text_inputs, &text_input->link); +} + +static void relay_handle_input_method(struct wl_listener *listener, + void *data) { + struct roots_input_method_relay *relay = wl_container_of(listener, relay, + input_method_new); + struct wlr_input_method_v2 *input_method = data; + if (relay->seat->seat != input_method->seat) { + return; + } + + if (relay->input_method != NULL) { + wlr_log(WLR_INFO, "Attempted to connect second input method to a seat"); + wlr_input_method_v2_send_unavailable(input_method); + return; + } + + relay->input_method = input_method; + wl_signal_add(&relay->input_method->events.commit, + &relay->input_method_commit); + relay->input_method_commit.notify = handle_im_commit; + wl_signal_add(&relay->input_method->events.destroy, + &relay->input_method_destroy); + relay->input_method_destroy.notify = handle_im_destroy; + + struct roots_text_input *text_input = relay_get_focusable_text_input(relay); + if (text_input) { + wlr_text_input_v3_send_enter(text_input->input, + text_input->pending_focused_surface); + text_input_clear_pending_focused_surface(text_input); + } +} + +void roots_input_method_relay_init(struct roots_seat *seat, + struct roots_input_method_relay *relay) { + relay->seat = seat; + wl_list_init(&relay->text_inputs); + + relay->text_input_new.notify = relay_handle_text_input; + wl_signal_add(&seat->input->server->desktop->text_input->events.text_input, + &relay->text_input_new); + + relay->input_method_new.notify = relay_handle_input_method; + wl_signal_add( + &seat->input->server->desktop->input_method->events.input_method, + &relay->input_method_new); +} + +void roots_input_method_relay_set_focus(struct roots_input_method_relay *relay, + struct wlr_surface *surface) { + struct roots_text_input *text_input; + wl_list_for_each(text_input, &relay->text_inputs, link) { + if (text_input->pending_focused_surface) { + assert(text_input->input->focused_surface == NULL); + if (surface != text_input->pending_focused_surface) { + text_input_clear_pending_focused_surface(text_input); + } + } else if (text_input->input->focused_surface) { + assert(text_input->pending_focused_surface == NULL); + if (surface != text_input->input->focused_surface) { + relay_disable_text_input(relay, text_input); + wlr_text_input_v3_send_leave(text_input->input); + } + } else if (surface + && wl_resource_get_client(text_input->input->resource) + == wl_resource_get_client(surface->resource)) { + if (relay->input_method) { + wlr_text_input_v3_send_enter(text_input->input, surface); + } else { + text_input_set_pending_focused_surface(text_input, surface); + } + } + } +} diff --git a/types/meson.build b/types/meson.build index 9e2e74a8..bd7fd1a0 100644 --- a/types/meson.build +++ b/types/meson.build @@ -34,6 +34,7 @@ lib_wlr_types = static_library( 'wlr_idle.c', 'wlr_input_device.c', 'wlr_input_inhibitor.c', + 'wlr_input_method_v2.c', 'wlr_keyboard.c', 'wlr_layer_shell_v1.c', 'wlr_linux_dmabuf_v1.c', @@ -53,6 +54,7 @@ lib_wlr_types = static_library( 'wlr_surface.c', 'wlr_tablet_pad.c', 'wlr_tablet_tool.c', + 'wlr_text_input_v3.c', 'wlr_touch.c', 'wlr_virtual_keyboard_v1.c', 'wlr_wl_shell.c', diff --git a/types/wlr_buffer.c b/types/wlr_buffer.c index ce733f40..cec3475c 100644 --- a/types/wlr_buffer.c +++ b/types/wlr_buffer.c @@ -150,12 +150,20 @@ struct wlr_buffer *wlr_buffer_apply_damage(struct wlr_buffer *buffer, } struct wl_shm_buffer *shm_buf = wl_shm_buffer_get(resource); - if (shm_buf == NULL) { - // Uploading only damaged regions only works for wl_shm buffers + struct wl_shm_buffer *old_shm_buf = wl_shm_buffer_get(buffer->resource); + if (shm_buf == NULL || old_shm_buf == NULL) { + // Uploading only damaged regions only works for wl_shm buffers and + // mutable textures (created from wl_shm buffer) + return NULL; + } + + enum wl_shm_format new_fmt = wl_shm_buffer_get_format(shm_buf); + enum wl_shm_format old_fmt = wl_shm_buffer_get_format(old_shm_buf); + if (new_fmt != old_fmt) { + // Uploading to textures can't change the format return NULL; } - enum wl_shm_format fmt = wl_shm_buffer_get_format(shm_buf); int32_t stride = wl_shm_buffer_get_stride(shm_buf); int32_t width = wl_shm_buffer_get_width(shm_buf); int32_t height = wl_shm_buffer_get_height(shm_buf); @@ -173,7 +181,7 @@ struct wlr_buffer *wlr_buffer_apply_damage(struct wlr_buffer *buffer, pixman_box32_t *rects = pixman_region32_rectangles(damage, &n); for (int i = 0; i < n; ++i) { pixman_box32_t *r = &rects[i]; - if (!wlr_texture_write_pixels(buffer->texture, fmt, stride, + if (!wlr_texture_write_pixels(buffer->texture, stride, r->x2 - r->x1, r->y2 - r->y1, r->x1, r->y1, r->x1, r->y1, data)) { wl_shm_buffer_end_access(shm_buf); diff --git a/types/wlr_input_method_v2.c b/types/wlr_input_method_v2.c new file mode 100644 index 00000000..fc9fa62b --- /dev/null +++ b/types/wlr_input_method_v2.c @@ -0,0 +1,297 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include <assert.h> +#include <string.h> +#include <wayland-util.h> +#include <wlr/types/wlr_input_method_v2.h> +#include <wlr/types/wlr_surface.h> +#include <wlr/util/log.h> +#include <xkbcommon/xkbcommon.h> +#include "input-method-unstable-v2-protocol.h" +#include "util/signal.h" + +static const struct zwp_input_method_v2_interface input_method_impl; + +static struct wlr_input_method_v2 *input_method_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_input_method_v2_interface, &input_method_impl)); + return wl_resource_get_user_data(resource); +} + +static void input_method_destroy(struct wlr_input_method_v2 *input_method) { + wlr_signal_emit_safe(&input_method->events.destroy, input_method); + wl_list_remove(&input_method->seat_destroy.link); + free(input_method->pending.commit_text); + free(input_method->pending.preedit.text); + free(input_method->current.commit_text); + free(input_method->current.preedit.text); + free(input_method); +} + +static void input_method_resource_destroy(struct wl_resource *resource) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method_destroy(input_method); +} + +static void im_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void im_commit(struct wl_client *client, struct wl_resource *resource, + uint32_t serial) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->current = input_method->pending; + input_method->current_serial = serial; + struct wlr_input_method_v2_state default_state = {0}; + input_method->pending = default_state; + wlr_signal_emit_safe(&input_method->events.commit, (void*)input_method); +} + +static void im_commit_string(struct wl_client *client, + struct wl_resource *resource, const char *text) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + free(input_method->pending.commit_text); + input_method->pending.commit_text = strdup(text); +} + +static void im_set_preedit_string(struct wl_client *client, + struct wl_resource *resource, const char *text, int32_t cursor_begin, + int32_t cursor_end) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->pending.preedit.cursor_begin = cursor_begin; + input_method->pending.preedit.cursor_end = cursor_end; + free(input_method->pending.preedit.text); + input_method->pending.preedit.text = strdup(text); +} + +static void im_delete_surrounding_text(struct wl_client *client, + struct wl_resource *resource, + uint32_t before_length, uint32_t after_length) { + struct wlr_input_method_v2 *input_method = + input_method_from_resource(resource); + if (!input_method) { + return; + } + input_method->pending.delete.before_length = before_length; + input_method->pending.delete.after_length = after_length; +} + +static void im_get_input_popup_surface(struct wl_client *client, + struct wl_resource *resource, uint32_t id, + struct wl_resource *surface) { + wlr_log(WLR_INFO, "Stub: zwp_input_method_v2::get_input_popup_surface"); +} + + +static void im_grab_keyboard(struct wl_client *client, + struct wl_resource *resource, uint32_t keyboard) { + wlr_log(WLR_INFO, "Stub: zwp_input_method_v2::grab_keyboard"); +} + +static const struct zwp_input_method_v2_interface input_method_impl = { + .destroy = im_destroy, + .commit = im_commit, + .commit_string = im_commit_string, + .set_preedit_string = im_set_preedit_string, + .delete_surrounding_text = im_delete_surrounding_text, + .get_input_popup_surface = im_get_input_popup_surface, + .grab_keyboard = im_grab_keyboard, +}; + +void wlr_input_method_v2_send_activate( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_activate(input_method->resource); + input_method->active = true; +} + +void wlr_input_method_v2_send_deactivate( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_deactivate(input_method->resource); + input_method->active = false; +} + +void wlr_input_method_v2_send_surrounding_text( + struct wlr_input_method_v2 *input_method, const char *text, + uint32_t cursor, uint32_t anchor) { + const char *send_text = text; + if (!send_text) { + send_text = ""; + } + zwp_input_method_v2_send_surrounding_text(input_method->resource, send_text, + cursor, anchor); +} + +void wlr_input_method_v2_send_text_change_cause( + struct wlr_input_method_v2 *input_method, uint32_t cause) { + zwp_input_method_v2_send_text_change_cause(input_method->resource, cause); +} + +void wlr_input_method_v2_send_content_type( + struct wlr_input_method_v2 *input_method, + uint32_t hint, uint32_t purpose) { + zwp_input_method_v2_send_content_type(input_method->resource, hint, + purpose); +} + +void wlr_input_method_v2_send_done(struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_done(input_method->resource); + input_method->client_active = input_method->active; + input_method->current_serial++; +} + +void wlr_input_method_v2_send_unavailable( + struct wlr_input_method_v2 *input_method) { + zwp_input_method_v2_send_unavailable(input_method->resource); + struct wl_resource *resource = input_method->resource; + input_method_destroy(input_method); + wl_resource_set_user_data(resource, NULL); +} + +static const struct zwp_input_method_manager_v2_interface + input_method_manager_impl; + +static struct wlr_input_method_manager_v2 *input_method_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_input_method_manager_v2_interface, &input_method_manager_impl)); + return wl_resource_get_user_data(resource); +} + +static void input_method_handle_seat_destroy(struct wl_listener *listener, + void *data) { + struct wlr_input_method_v2 *input_method = wl_container_of(listener, + input_method, seat_destroy); + wlr_input_method_v2_send_unavailable(input_method); +} + +static void manager_get_input_method(struct wl_client *client, + struct wl_resource *resource, struct wl_resource *seat, + uint32_t input_method_id) { + struct wlr_input_method_manager_v2 *im_manager = + input_method_manager_from_resource(resource); + + struct wlr_input_method_v2 *input_method = calloc(1, + sizeof(struct wlr_input_method_v2)); + if (!input_method) { + wl_client_post_no_memory(client); + return; + } + wl_signal_init(&input_method->events.commit); + wl_signal_init(&input_method->events.destroy); + int version = wl_resource_get_version(resource); + struct wl_resource *im_resource = wl_resource_create(client, + &zwp_input_method_v2_interface, version, input_method_id); + if (im_resource == NULL) { + free(input_method); + wl_client_post_no_memory(client); + return; + } + wl_resource_set_implementation(im_resource, &input_method_impl, + input_method, input_method_resource_destroy); + + struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat); + wl_signal_add(&seat_client->events.destroy, + &input_method->seat_destroy); + input_method->seat_destroy.notify = + input_method_handle_seat_destroy; + + input_method->resource = im_resource; + input_method->seat = seat_client->seat; + wl_list_insert(&im_manager->input_methods, + wl_resource_get_link(input_method->resource)); + wlr_signal_emit_safe(&im_manager->events.input_method, input_method); +} + +static void manager_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct zwp_input_method_manager_v2_interface + input_method_manager_impl = { + .get_input_method = manager_get_input_method, + .destroy = manager_destroy, +}; + +static void input_method_manager_unbind(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + +static void input_method_manager_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + assert(wl_client); + struct wlr_input_method_manager_v2 *im_manager = data; + + struct wl_resource *bound_resource = wl_resource_create(wl_client, + &zwp_input_method_manager_v2_interface, version, id); + if (bound_resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + wl_resource_set_implementation(bound_resource, &input_method_manager_impl, + im_manager, input_method_manager_unbind); + wl_list_insert(&im_manager->bound_resources, + wl_resource_get_link(bound_resource)); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_input_method_manager_v2 *manager = + wl_container_of(listener, manager, display_destroy); + wlr_input_method_manager_v2_destroy(manager); +} + +struct wlr_input_method_manager_v2 *wlr_input_method_manager_v2_create( + struct wl_display *display) { + struct wlr_input_method_manager_v2 *im_manager = calloc(1, + sizeof(struct wlr_input_method_manager_v2)); + if (!im_manager) { + return NULL; + } + wl_signal_init(&im_manager->events.input_method); + wl_list_init(&im_manager->bound_resources); + wl_list_init(&im_manager->input_methods); + + im_manager->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &im_manager->display_destroy); + + im_manager->global = wl_global_create(display, + &zwp_input_method_manager_v2_interface, 1, im_manager, + input_method_manager_bind); + return im_manager; +} + +void wlr_input_method_manager_v2_destroy( + struct wlr_input_method_manager_v2 *manager) { + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy.link); + + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, + &manager->bound_resources) { + wl_resource_destroy(resource); + } + struct wlr_input_method_v2 *im, *im_tmp; + wl_list_for_each_safe(im, im_tmp, &manager->input_methods, link) { + wl_resource_destroy(im->resource); + } + wl_global_destroy(manager->global); + free(manager); +} diff --git a/types/wlr_linux_dmabuf_v1.c b/types/wlr_linux_dmabuf_v1.c index f72e7c07..eb7b2f9e 100644 --- a/types/wlr_linux_dmabuf_v1.c +++ b/types/wlr_linux_dmabuf_v1.c @@ -103,6 +103,7 @@ static void params_add(struct wl_client *client, close(fd); return; } + buffer->attributes.modifier = modifier; buffer->has_modifier = true; @@ -382,13 +383,9 @@ struct wlr_linux_dmabuf_v1 *wlr_linux_dmabuf_v1_from_resource( return dmabuf; } -static void linux_dmabuf_send_modifiers(struct wlr_linux_dmabuf_v1 *linux_dmabuf, - struct wl_resource *resource) { +static void linux_dmabuf_send_formats(struct wlr_linux_dmabuf_v1 *linux_dmabuf, + struct wl_resource *resource, uint32_t version) { struct wlr_renderer *renderer = linux_dmabuf->renderer; - /* - * Use EGL_EXT_image_dma_buf_import_modifiers to query and advertise - * format/modifier codes. - */ uint64_t modifier_invalid = DRM_FORMAT_MOD_INVALID; int *formats = NULL; int num_formats = wlr_renderer_get_dmabuf_formats(renderer, &formats); @@ -410,10 +407,17 @@ static void linux_dmabuf_send_modifiers(struct wlr_linux_dmabuf_v1 *linux_dmabuf modifiers = &modifier_invalid; } for (int j = 0; j < num_modifiers; j++) { - uint32_t modifier_lo = modifiers[j] & 0xFFFFFFFF; - uint32_t modifier_hi = modifiers[j] >> 32; - zwp_linux_dmabuf_v1_send_modifier(resource, formats[i], - modifier_hi, modifier_lo); + if (version >= ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) { + uint32_t modifier_lo = modifiers[j] & 0xFFFFFFFF; + uint32_t modifier_hi = modifiers[j] >> 32; + zwp_linux_dmabuf_v1_send_modifier(resource, + formats[i], + modifier_hi, + modifier_lo); + } else if (modifiers[j] == DRM_FORMAT_MOD_LINEAR || + modifiers == &modifier_invalid) { + zwp_linux_dmabuf_v1_send_format(resource, formats[i]); + } } if (modifiers != &modifier_invalid) { free(modifiers); @@ -439,10 +443,7 @@ static void linux_dmabuf_bind(struct wl_client *client, void *data, wl_resource_set_implementation(resource, &linux_dmabuf_impl, linux_dmabuf, linux_dmabuf_resource_destroy); wl_list_insert(&linux_dmabuf->resources, wl_resource_get_link(resource)); - - if (version >= ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) { - linux_dmabuf_send_modifiers(linux_dmabuf, resource); - } + linux_dmabuf_send_formats(linux_dmabuf, resource, version); } void wlr_linux_dmabuf_v1_destroy(struct wlr_linux_dmabuf_v1 *linux_dmabuf) { diff --git a/types/wlr_output.c b/types/wlr_output.c index 35a3ab14..7bcf2556 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -298,17 +298,17 @@ void wlr_output_destroy(struct wlr_output *output) { wlr_signal_emit_safe(&output->events.destroy, output); - struct wlr_output_mode *mode, *tmp_mode; - wl_list_for_each_safe(mode, tmp_mode, &output->modes, link) { - wl_list_remove(&mode->link); - free(mode); - } + // The backend is responsible for free-ing the list of modes struct wlr_output_cursor *cursor, *tmp_cursor; wl_list_for_each_safe(cursor, tmp_cursor, &output->cursors, link) { wlr_output_cursor_destroy(cursor); } + if (output->idle_frame != NULL) { + wl_event_source_remove(output->idle_frame); + } + pixman_region32_fini(&output->damage); if (output->impl && output->impl->destroy) { diff --git a/types/wlr_text_input_v3.c b/types/wlr_text_input_v3.c new file mode 100644 index 00000000..6ec0762a --- /dev/null +++ b/types/wlr_text_input_v3.c @@ -0,0 +1,337 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include <assert.h> +#include <stdlib.h> +#include <stddef.h> +#include <wlr/types/wlr_text_input_v3.h> +#include <wlr/util/log.h> +#include "text-input-unstable-v3-protocol.h" +#include "util/signal.h" + +static void text_input_clear_focused_surface(struct wlr_text_input_v3 *text_input) { + wl_list_remove(&text_input->surface_destroy.link); + wl_list_init(&text_input->surface_destroy.link); + text_input->focused_surface = NULL; +} + +static const struct zwp_text_input_v3_interface text_input_impl; + +static struct wlr_text_input_v3 *text_input_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_text_input_v3_interface, + &text_input_impl)); + return wl_resource_get_user_data(resource); +} + +void wlr_text_input_v3_send_enter(struct wlr_text_input_v3 *text_input, + struct wlr_surface *surface) { + assert(wl_resource_get_client(text_input->resource) + == wl_resource_get_client(surface->resource)); + text_input->focused_surface = surface; + wl_signal_add(&text_input->focused_surface->events.destroy, + &text_input->surface_destroy); + zwp_text_input_v3_send_enter(text_input->resource, + text_input->focused_surface->resource); +} + +void wlr_text_input_v3_send_leave(struct wlr_text_input_v3 *text_input) { + zwp_text_input_v3_send_leave(text_input->resource, + text_input->focused_surface->resource); + text_input_clear_focused_surface(text_input); +} + +void wlr_text_input_v3_send_preedit_string(struct wlr_text_input_v3 *text_input, + const char *text, uint32_t cursor_begin, uint32_t cursor_end) { + zwp_text_input_v3_send_preedit_string(text_input->resource, text, + cursor_begin, cursor_end); +} + +void wlr_text_input_v3_send_commit_string(struct wlr_text_input_v3 *text_input, + const char *text) { + zwp_text_input_v3_send_commit_string(text_input->resource, text); +} + +void wlr_text_input_v3_send_delete_surrounding_text( + struct wlr_text_input_v3 *text_input, uint32_t before_length, + uint32_t after_length) { + zwp_text_input_v3_send_delete_surrounding_text(text_input->resource, + before_length, after_length); +} + +void wlr_text_input_v3_send_done(struct wlr_text_input_v3 *text_input) { + zwp_text_input_v3_send_done(text_input->resource, + text_input->current_serial); +} + +static void wlr_text_input_destroy(struct wlr_text_input_v3 *text_input) { + wlr_signal_emit_safe(&text_input->events.destroy, text_input); + text_input_clear_focused_surface(text_input); + wl_list_remove(&text_input->seat_destroy.link); + // remove from manager::text_inputs + wl_list_remove(&text_input->link); + free(text_input->current.surrounding.text); + free(text_input->pending.surrounding.text); + free(text_input); +} + +static void text_input_resource_destroy(struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + wlr_text_input_destroy(text_input); +} + +static void text_input_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void text_input_enable(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + struct wlr_text_input_v3_state defaults = {0}; + free(text_input->pending.surrounding.text); + text_input->pending = defaults; + text_input->pending_enabled = true; +} + +static void text_input_disable(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending_enabled = false; +} + +static void text_input_set_surrounding_text(struct wl_client *client, + struct wl_resource *resource, const char *text, int32_t cursor, + int32_t anchor) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + free(text_input->pending.surrounding.text); + text_input->pending.surrounding.text = strdup(text); + if (!text_input->pending.surrounding.text) { + wl_client_post_no_memory(client); + } + + text_input->pending.surrounding.cursor = cursor; + text_input->pending.surrounding.anchor = anchor; +} + +static void text_input_set_text_change_cause(struct wl_client *client, + struct wl_resource *resource, uint32_t cause) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.text_change_cause = cause; +} + +static void text_input_set_content_type(struct wl_client *client, + struct wl_resource *resource, uint32_t hint, uint32_t purpose) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.content_type.hint = hint; + text_input->pending.content_type.purpose = purpose; +} + +static void text_input_set_cursor_rectangle(struct wl_client *client, + struct wl_resource *resource, int32_t x, int32_t y, int32_t width, + int32_t height) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + text_input->pending.cursor_rectangle.x = x; + text_input->pending.cursor_rectangle.y = y; + text_input->pending.cursor_rectangle.width = width; + text_input->pending.cursor_rectangle.height = height; +} + +static void text_input_commit(struct wl_client *client, + struct wl_resource *resource) { + struct wlr_text_input_v3 *text_input = text_input_from_resource(resource); + if (!text_input) { + return; + } + free(text_input->current.surrounding.text); + text_input->current = text_input->pending; + if (text_input->pending.surrounding.text) { + text_input->current.surrounding.text = + strdup(text_input->pending.surrounding.text); + } + + bool old_enabled = text_input->current_enabled; + text_input->current_enabled = text_input->pending_enabled; + text_input->current_serial++; + + if (text_input->focused_surface == NULL) { + wlr_log(WLR_DEBUG, "Text input commit received without focus\n"); + } + + if (!old_enabled && text_input->current_enabled) { + wlr_signal_emit_safe(&text_input->events.enable, text_input); + } else if (old_enabled && !text_input->current_enabled) { + wlr_signal_emit_safe(&text_input->events.disable, text_input); + } else { // including never enabled + wlr_signal_emit_safe(&text_input->events.commit, text_input); + } +} + +static const struct zwp_text_input_v3_interface text_input_impl = { + .destroy = text_input_destroy, + .enable = text_input_enable, + .disable = text_input_disable, + .set_surrounding_text = text_input_set_surrounding_text, + .set_text_change_cause = text_input_set_text_change_cause, + .set_content_type = text_input_set_content_type, + .set_cursor_rectangle = text_input_set_cursor_rectangle, + .commit = text_input_commit, +}; + +static const struct zwp_text_input_manager_v3_interface text_input_manager_impl; + +static struct wlr_text_input_manager_v3 *text_input_manager_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, + &zwp_text_input_manager_v3_interface, &text_input_manager_impl)); + return wl_resource_get_user_data(resource); +} + +static void text_input_manager_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void text_input_handle_seat_destroy(struct wl_listener *listener, + void *data) { + struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input, + seat_destroy); + struct wl_resource *resource = text_input->resource; + wlr_text_input_destroy(text_input); + wl_resource_set_user_data(resource, NULL); +} + +static void text_input_handle_focused_surface_destroy( + struct wl_listener *listener, void *data) { + struct wlr_text_input_v3 *text_input = wl_container_of(listener, text_input, + surface_destroy); + text_input_clear_focused_surface(text_input); +} + +static void text_input_manager_get_text_input(struct wl_client *client, + struct wl_resource *resource, uint32_t id, struct wl_resource *seat) { + struct wlr_text_input_v3 *text_input = + calloc(1, sizeof(struct wlr_text_input_v3)); + if (text_input == NULL) { + wl_client_post_no_memory(client); + return; + } + + wl_signal_init(&text_input->events.enable); + wl_signal_init(&text_input->events.commit); + wl_signal_init(&text_input->events.disable); + wl_signal_init(&text_input->events.destroy); + + int version = wl_resource_get_version(resource); + struct wl_resource *text_input_resource = wl_resource_create(client, + &zwp_text_input_v3_interface, version, id); + if (text_input_resource == NULL) { + free(text_input); + wl_client_post_no_memory(client); + return; + } + text_input->resource = text_input_resource; + + wl_resource_set_implementation(text_input->resource, &text_input_impl, + text_input, text_input_resource_destroy); + + struct wlr_seat_client *seat_client = wlr_seat_client_from_resource(seat); + struct wlr_seat *wlr_seat = seat_client->seat; + text_input->seat = wlr_seat; + wl_signal_add(&seat_client->events.destroy, + &text_input->seat_destroy); + text_input->seat_destroy.notify = + text_input_handle_seat_destroy; + text_input->surface_destroy.notify = + text_input_handle_focused_surface_destroy; + wl_list_init(&text_input->surface_destroy.link); + + struct wlr_text_input_manager_v3 *manager = + text_input_manager_from_resource(resource); + wl_list_insert(&manager->text_inputs, &text_input->link); + + wlr_signal_emit_safe(&manager->events.text_input, text_input); +} + +static const struct zwp_text_input_manager_v3_interface + text_input_manager_impl = { + .destroy = text_input_manager_destroy, + .get_text_input = text_input_manager_get_text_input, +}; + +static void text_input_manager_unbind(struct wl_resource *resource) { + wl_list_remove(wl_resource_get_link(resource)); +} + +static void text_input_manager_bind(struct wl_client *wl_client, void *data, + uint32_t version, uint32_t id) { + struct wlr_text_input_manager_v3 *manager = data; + assert(wl_client && manager); + + struct wl_resource *resource = wl_resource_create(wl_client, + &zwp_text_input_manager_v3_interface, version, id); + if (resource == NULL) { + wl_client_post_no_memory(wl_client); + return; + } + wl_list_insert(&manager->bound_resources, wl_resource_get_link(resource)); + wl_resource_set_implementation(resource, &text_input_manager_impl, + manager, text_input_manager_unbind); +} + +struct wlr_text_input_manager_v3 *wlr_text_input_manager_v3_create( + struct wl_display *wl_display) { + struct wlr_text_input_manager_v3 *manager = + calloc(1, sizeof(struct wlr_text_input_manager_v3)); + wl_list_init(&manager->bound_resources); + wl_list_init(&manager->text_inputs); + wl_signal_init(&manager->events.text_input); + manager->global = wl_global_create(wl_display, + &zwp_text_input_manager_v3_interface, 1, manager, + text_input_manager_bind); + if (!manager->global) { + free(manager); + return NULL; + } + return manager; +} + +void wlr_text_input_manager_v3_destroy( + struct wlr_text_input_manager_v3 *manager) { + wlr_signal_emit_safe(&manager->events.destroy, manager); + wl_list_remove(&manager->display_destroy.link); + + struct wl_resource *resource, *resource_tmp; + wl_resource_for_each_safe(resource, resource_tmp, + &manager->bound_resources) { + wl_resource_destroy(resource); + } + struct wlr_text_input_v3 *text_input, *text_input_tmp; + wl_list_for_each_safe(text_input, text_input_tmp, &manager->text_inputs, + link) { + wl_resource_destroy(text_input->resource); + } + wl_global_destroy(manager->global); + free(manager); +} diff --git a/xwayland/xwm.c b/xwayland/xwm.c index 1fb6f331..474b53d0 100644 --- a/xwayland/xwm.c +++ b/xwayland/xwm.c @@ -522,6 +522,12 @@ static void read_surface_hints(struct wlr_xwm *xwm, memcpy(xsurface->hints, &hints, sizeof(struct wlr_xwayland_surface_hints)); xsurface->hints_urgency = xcb_icccm_wm_hints_get_urgency(&hints); + if (!(xsurface->hints->flags & XCB_ICCCM_WM_HINT_INPUT)) { + // The client didn't specify whether it wants input. + // Assume it does. + xsurface->hints->input = true; + } + wlr_log(WLR_DEBUG, "WM_HINTS (%d)", reply->value_len); wlr_signal_emit_safe(&xsurface->events.set_hints, xsurface); } @@ -797,8 +803,6 @@ static void xwm_handle_destroy_notify(struct wlr_xwm *xwm, static void xwm_handle_configure_request(struct wlr_xwm *xwm, xcb_configure_request_event_t *ev) { - wlr_log(WLR_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window, - ev->width, ev->height, ev->x, ev->y); struct wlr_xwayland_surface *surface = lookup_surface(xwm, ev->window); if (surface == NULL) { return; @@ -806,13 +810,22 @@ static void xwm_handle_configure_request(struct wlr_xwm *xwm, // TODO: handle ev->{parent,sibling}? + uint16_t mask = ev->value_mask; + uint16_t geo_mask = XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | + XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT; + if ((mask & geo_mask) == 0) { + return; + } + struct wlr_xwayland_surface_configure_event wlr_event = { .surface = surface, - .x = ev->x, - .y = ev->y, - .width = ev->width, - .height = ev->height, + .x = mask & XCB_CONFIG_WINDOW_X ? ev->x : surface->x, + .y = mask & XCB_CONFIG_WINDOW_Y ? ev->y : surface->y, + .width = mask & XCB_CONFIG_WINDOW_WIDTH ? ev->width : surface->width, + .height = mask & XCB_CONFIG_WINDOW_HEIGHT ? ev->height : surface->height, }; + wlr_log(WLR_DEBUG, "XCB_CONFIGURE_REQUEST (%u) [%ux%u+%d,%d]", ev->window, + wlr_event.width, wlr_event.height, wlr_event.x, wlr_event.y); wlr_signal_emit_safe(&surface->events.request_configure, &wlr_event); } |