weston/compositor/text-backend.c

1121 lines
30 KiB
C
Raw Normal View History

/*
* Copyright © 2012 Openismus GmbH
* Copyright © 2012 Intel Corporation
*
* 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.
*/
#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <libweston/libweston.h>
#include "weston.h"
#include "text-input-unstable-v1-server-protocol.h"
#include "input-method-unstable-v1-server-protocol.h"
#include "shared/helpers.h"
#include "shared/timespec-util.h"
struct text_input_manager;
struct input_method;
struct input_method_context;
struct text_backend;
struct text_input {
struct wl_resource *resource;
struct weston_compositor *ec;
struct wl_list input_methods;
struct weston_surface *surface;
pixman_box32_t cursor_rectangle;
bool input_panel_visible;
struct text_input_manager *manager;
};
struct text_input_manager {
struct wl_global *text_input_manager_global;
struct wl_listener destroy_listener;
struct text_input *current_text_input;
struct weston_compositor *ec;
};
struct input_method {
struct wl_resource *input_method_binding;
struct wl_global *input_method_global;
struct wl_listener destroy_listener;
struct weston_seat *seat;
struct text_input *input;
struct wl_list link;
struct wl_listener keyboard_focus_listener;
bool focus_listener_initialized;
struct input_method_context *context;
struct text_backend *text_backend;
};
struct input_method_context {
struct wl_resource *resource;
struct text_input *input;
struct input_method *input_method;
struct wl_resource *keyboard;
};
struct text_backend {
struct weston_compositor *compositor;
struct {
char *path;
bool overlay_keyboard;
struct wl_client *client;
unsigned deathcount;
struct timespec deathstamp;
} input_method;
struct wl_listener client_listener;
struct wl_listener seat_created_listener;
};
static void
input_method_context_create(struct text_input *input,
struct input_method *input_method);
static void
input_method_context_end_keyboard_grab(struct input_method_context *context);
static void
input_method_init_seat(struct weston_seat *seat);
static void
deactivate_input_method(struct input_method *input_method)
{
struct text_input *text_input = input_method->input;
struct weston_compositor *ec = text_input->ec;
if (input_method->context && input_method->input_method_binding) {
input_method_context_end_keyboard_grab(input_method->context);
zwp_input_method_v1_send_deactivate(
input_method->input_method_binding,
input_method->context->resource);
input_method->context->input = NULL;
}
wl_list_remove(&input_method->link);
input_method->input = NULL;
input_method->context = NULL;
if (wl_list_empty(&text_input->input_methods) &&
text_input->input_panel_visible &&
text_input->manager->current_text_input == text_input) {
wl_signal_emit(&ec->hide_input_panel_signal, ec);
text_input->input_panel_visible = false;
}
if (text_input->manager->current_text_input == text_input)
text_input->manager->current_text_input = NULL;
zwp_text_input_v1_send_leave(text_input->resource);
}
static void
destroy_text_input(struct wl_resource *resource)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link)
deactivate_input_method(input_method);
free(text_input);
}
static void
text_input_set_surrounding_text(struct wl_client *client,
struct wl_resource *resource,
const char *text,
uint32_t cursor,
uint32_t anchor)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_surrounding_text(
input_method->context->resource, text, cursor, anchor);
}
}
static void
text_input_activate(struct wl_client *client,
struct wl_resource *resource,
struct wl_resource *seat,
struct wl_resource *surface)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct weston_seat *weston_seat = wl_resource_get_user_data(seat);
libweston: Make weston_seat release safe Ensure the server can safely handle client requests for wl_seat resource that have become inert due to weston_seat object release and subsequent destruction. The clean-up involves, among other things, unsetting the destroyed weston_seat object from the user data of wl_seat resources, and handling this NULL user data case where required. The list of sites extracting and using weston_seat object from wl_seat resources which were audited for this patch are: Legend: N/A = Not Applicable (not implemented by weston) FIXED = Fixed in the commit OK = Already works correctly == keyboard_shortcuts_inhibit_unstable_v1 == [N/A] zwp_keyboard_shortcuts_inhibit_manager_v1.inhibit_shortcuts == tablet_input_unstable_v{1,2} == [N/A] zwp_tablet_manager_v{1,2}.get_tablet_seat == text_input_unstable_v1 == [FIXED] zwp_text_input_v1.activate [FIXED] zwp_text_input_v1.deactivate == wl_data_device == [FIXED] wl_data_device_manager.get_data_device [OK] wl_data_device.start_drag [FIXED] wl_data_device.set_selection [OK] wl_data_device.release == wl_shell == [FIXED] wl_shell_surface.move [FIXED] wl_shell_surface.resize [FIXED] wl_shell_surface.set_popup == xdg_shell and xdg_shell_unstable_v6 == [FIXED] xdg_toplevel.show_window_menu [FIXED] xdg_toplevel.move [FIXED] xdg_toplevel.resize [FIXED] xdg_popup.grab == xdg_shell_unstable_v5 == [FIXED] xdg_shell.get_xdg_popup [FIXED] xdg_surface.show_window_menu [FIXED] xdg_surface.move [FIXED] xdg_surface.resize Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com> Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
2018-02-15 14:07:09 +03:00
struct input_method *input_method;
struct weston_compositor *ec = text_input->ec;
struct text_input *current;
libweston: Make weston_seat release safe Ensure the server can safely handle client requests for wl_seat resource that have become inert due to weston_seat object release and subsequent destruction. The clean-up involves, among other things, unsetting the destroyed weston_seat object from the user data of wl_seat resources, and handling this NULL user data case where required. The list of sites extracting and using weston_seat object from wl_seat resources which were audited for this patch are: Legend: N/A = Not Applicable (not implemented by weston) FIXED = Fixed in the commit OK = Already works correctly == keyboard_shortcuts_inhibit_unstable_v1 == [N/A] zwp_keyboard_shortcuts_inhibit_manager_v1.inhibit_shortcuts == tablet_input_unstable_v{1,2} == [N/A] zwp_tablet_manager_v{1,2}.get_tablet_seat == text_input_unstable_v1 == [FIXED] zwp_text_input_v1.activate [FIXED] zwp_text_input_v1.deactivate == wl_data_device == [FIXED] wl_data_device_manager.get_data_device [OK] wl_data_device.start_drag [FIXED] wl_data_device.set_selection [OK] wl_data_device.release == wl_shell == [FIXED] wl_shell_surface.move [FIXED] wl_shell_surface.resize [FIXED] wl_shell_surface.set_popup == xdg_shell and xdg_shell_unstable_v6 == [FIXED] xdg_toplevel.show_window_menu [FIXED] xdg_toplevel.move [FIXED] xdg_toplevel.resize [FIXED] xdg_popup.grab == xdg_shell_unstable_v5 == [FIXED] xdg_shell.get_xdg_popup [FIXED] xdg_surface.show_window_menu [FIXED] xdg_surface.move [FIXED] xdg_surface.resize Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com> Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
2018-02-15 14:07:09 +03:00
if (!weston_seat)
return;
input_method = weston_seat->input_method;
if (input_method->input == text_input)
return;
if (input_method->input)
deactivate_input_method(input_method);
input_method->input = text_input;
wl_list_insert(&text_input->input_methods, &input_method->link);
input_method_init_seat(weston_seat);
text_input->surface = wl_resource_get_user_data(surface);
input_method_context_create(text_input, input_method);
current = text_input->manager->current_text_input;
if (current && current != text_input) {
current->input_panel_visible = false;
wl_signal_emit(&ec->hide_input_panel_signal, ec);
}
if (text_input->input_panel_visible) {
wl_signal_emit(&ec->show_input_panel_signal,
text_input->surface);
wl_signal_emit(&ec->update_input_panel_signal,
&text_input->cursor_rectangle);
}
text_input->manager->current_text_input = text_input;
zwp_text_input_v1_send_enter(text_input->resource,
text_input->surface->resource);
}
static void
text_input_deactivate(struct wl_client *client,
struct wl_resource *resource,
struct wl_resource *seat)
{
struct weston_seat *weston_seat = wl_resource_get_user_data(seat);
libweston: Make weston_seat release safe Ensure the server can safely handle client requests for wl_seat resource that have become inert due to weston_seat object release and subsequent destruction. The clean-up involves, among other things, unsetting the destroyed weston_seat object from the user data of wl_seat resources, and handling this NULL user data case where required. The list of sites extracting and using weston_seat object from wl_seat resources which were audited for this patch are: Legend: N/A = Not Applicable (not implemented by weston) FIXED = Fixed in the commit OK = Already works correctly == keyboard_shortcuts_inhibit_unstable_v1 == [N/A] zwp_keyboard_shortcuts_inhibit_manager_v1.inhibit_shortcuts == tablet_input_unstable_v{1,2} == [N/A] zwp_tablet_manager_v{1,2}.get_tablet_seat == text_input_unstable_v1 == [FIXED] zwp_text_input_v1.activate [FIXED] zwp_text_input_v1.deactivate == wl_data_device == [FIXED] wl_data_device_manager.get_data_device [OK] wl_data_device.start_drag [FIXED] wl_data_device.set_selection [OK] wl_data_device.release == wl_shell == [FIXED] wl_shell_surface.move [FIXED] wl_shell_surface.resize [FIXED] wl_shell_surface.set_popup == xdg_shell and xdg_shell_unstable_v6 == [FIXED] xdg_toplevel.show_window_menu [FIXED] xdg_toplevel.move [FIXED] xdg_toplevel.resize [FIXED] xdg_popup.grab == xdg_shell_unstable_v5 == [FIXED] xdg_shell.get_xdg_popup [FIXED] xdg_surface.show_window_menu [FIXED] xdg_surface.move [FIXED] xdg_surface.resize Signed-off-by: Alexandros Frantzis <alexandros.frantzis@collabora.com> Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-by: Quentin Glidic <sardemff7+git@sardemff7.net>
2018-02-15 14:07:09 +03:00
if (weston_seat && weston_seat->input_method->input)
deactivate_input_method(weston_seat->input_method);
}
static void
text_input_reset(struct wl_client *client,
struct wl_resource *resource)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_reset(
input_method->context->resource);
}
}
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 text_input *text_input = wl_resource_get_user_data(resource);
struct weston_compositor *ec = text_input->ec;
text_input->cursor_rectangle.x1 = x;
text_input->cursor_rectangle.y1 = y;
text_input->cursor_rectangle.x2 = x + width;
text_input->cursor_rectangle.y2 = y + height;
wl_signal_emit(&ec->update_input_panel_signal,
&text_input->cursor_rectangle);
}
static void
text_input_set_content_type(struct wl_client *client,
struct wl_resource *resource,
uint32_t hint,
uint32_t purpose)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_content_type(
input_method->context->resource, hint, purpose);
}
}
static void
text_input_invoke_action(struct wl_client *client,
struct wl_resource *resource,
uint32_t button,
uint32_t index)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_invoke_action(
input_method->context->resource, button, index);
}
}
static void
text_input_commit_state(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_commit_state(
input_method->context->resource, serial);
}
}
static void
text_input_show_input_panel(struct wl_client *client,
struct wl_resource *resource)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct weston_compositor *ec = text_input->ec;
text_input->input_panel_visible = true;
if (!wl_list_empty(&text_input->input_methods) &&
text_input == text_input->manager->current_text_input) {
wl_signal_emit(&ec->show_input_panel_signal,
text_input->surface);
wl_signal_emit(&ec->update_input_panel_signal,
&text_input->cursor_rectangle);
}
}
static void
text_input_hide_input_panel(struct wl_client *client,
struct wl_resource *resource)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct weston_compositor *ec = text_input->ec;
text_input->input_panel_visible = false;
if (!wl_list_empty(&text_input->input_methods) &&
text_input == text_input->manager->current_text_input)
wl_signal_emit(&ec->hide_input_panel_signal, ec);
}
static void
text_input_set_preferred_language(struct wl_client *client,
struct wl_resource *resource,
const char *language)
{
struct text_input *text_input = wl_resource_get_user_data(resource);
struct input_method *input_method, *next;
wl_list_for_each_safe(input_method, next,
&text_input->input_methods, link) {
if (!input_method->context)
continue;
zwp_input_method_context_v1_send_preferred_language(
input_method->context->resource, language);
}
}
static const struct zwp_text_input_v1_interface text_input_implementation = {
text_input_activate,
text_input_deactivate,
text_input_show_input_panel,
text_input_hide_input_panel,
text_input_reset,
text_input_set_surrounding_text,
text_input_set_content_type,
text_input_set_cursor_rectangle,
text_input_set_preferred_language,
text_input_commit_state,
text_input_invoke_action
};
static void text_input_manager_create_text_input(struct wl_client *client,
struct wl_resource *resource,
uint32_t id)
{
struct text_input_manager *text_input_manager =
wl_resource_get_user_data(resource);
struct text_input *text_input;
text_input = zalloc(sizeof *text_input);
if (text_input == NULL)
return;
text_input->resource =
wl_resource_create(client, &zwp_text_input_v1_interface, 1, id);
wl_resource_set_implementation(text_input->resource,
&text_input_implementation,
text_input, destroy_text_input);
text_input->ec = text_input_manager->ec;
text_input->manager = text_input_manager;
wl_list_init(&text_input->input_methods);
};
static const struct zwp_text_input_manager_v1_interface manager_implementation = {
text_input_manager_create_text_input
};
static void
bind_text_input_manager(struct wl_client *client,
void *data,
uint32_t version,
uint32_t id)
{
struct text_input_manager *text_input_manager = data;
struct wl_resource *resource;
/* No checking for duplicate binding necessary. */
resource =
wl_resource_create(client,
&zwp_text_input_manager_v1_interface, 1, id);
if (resource)
wl_resource_set_implementation(resource,
&manager_implementation,
text_input_manager, NULL);
}
static void
text_input_manager_notifier_destroy(struct wl_listener *listener, void *data)
{
struct text_input_manager *text_input_manager =
container_of(listener,
struct text_input_manager,
destroy_listener);
wl_list_remove(&text_input_manager->destroy_listener.link);
2013-07-09 03:03:57 +04:00
wl_global_destroy(text_input_manager->text_input_manager_global);
free(text_input_manager);
}
static void
text_input_manager_create(struct weston_compositor *ec)
{
struct text_input_manager *text_input_manager;
text_input_manager = zalloc(sizeof *text_input_manager);
if (text_input_manager == NULL)
return;
text_input_manager->ec = ec;
text_input_manager->text_input_manager_global =
2013-07-09 03:03:57 +04:00
wl_global_create(ec->wl_display,
&zwp_text_input_manager_v1_interface, 1,
2013-07-09 03:03:57 +04:00
text_input_manager, bind_text_input_manager);
text_input_manager->destroy_listener.notify =
text_input_manager_notifier_destroy;
wl_signal_add(&ec->destroy_signal,
&text_input_manager->destroy_listener);
}
static void
input_method_context_destroy(struct wl_client *client,
struct wl_resource *resource)
{
wl_resource_destroy(resource);
}
static void
input_method_context_commit_string(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
const char *text)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_commit_string(context->input->resource,
serial, text);
}
static void
input_method_context_preedit_string(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
const char *text,
const char *commit)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_preedit_string(context->input->resource,
serial, text, commit);
}
static void
input_method_context_preedit_styling(struct wl_client *client,
struct wl_resource *resource,
uint32_t index,
uint32_t length,
uint32_t style)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_preedit_styling(context->input->resource,
index, length, style);
}
static void
input_method_context_preedit_cursor(struct wl_client *client,
struct wl_resource *resource,
int32_t cursor)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_preedit_cursor(context->input->resource,
cursor);
}
static void
input_method_context_delete_surrounding_text(struct wl_client *client,
struct wl_resource *resource,
int32_t index,
uint32_t length)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_delete_surrounding_text(
context->input->resource, index, length);
}
static void
input_method_context_cursor_position(struct wl_client *client,
struct wl_resource *resource,
int32_t index,
int32_t anchor)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_cursor_position(context->input->resource,
index, anchor);
}
static void
input_method_context_modifiers_map(struct wl_client *client,
struct wl_resource *resource,
struct wl_array *map)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_modifiers_map(context->input->resource,
map);
}
static void
input_method_context_keysym(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
uint32_t time,
uint32_t sym,
uint32_t state,
uint32_t modifiers)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_keysym(context->input->resource,
serial, time,
sym, state, modifiers);
}
static void
unbind_keyboard(struct wl_resource *resource)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
input_method_context_end_keyboard_grab(context);
context->keyboard = NULL;
}
static void
2013-04-18 23:25:39 +04:00
input_method_context_grab_key(struct weston_keyboard_grab *grab,
const struct timespec *time, uint32_t key,
uint32_t state_w)
{
2013-04-18 23:25:39 +04:00
struct weston_keyboard *keyboard = grab->keyboard;
struct wl_display *display;
uint32_t serial;
uint32_t msecs;
if (!keyboard->input_method_resource)
return;
display = wl_client_get_display(
wl_resource_get_client(keyboard->input_method_resource));
serial = wl_display_next_serial(display);
msecs = timespec_to_msec(time);
wl_keyboard_send_key(keyboard->input_method_resource,
serial, msecs, key, state_w);
}
static void
input_method_context_grab_modifier(struct weston_keyboard_grab *grab,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group)
{
2013-04-18 23:25:39 +04:00
struct weston_keyboard *keyboard = grab->keyboard;
if (!keyboard->input_method_resource)
return;
wl_keyboard_send_modifiers(keyboard->input_method_resource,
serial, mods_depressed, mods_latched,
mods_locked, group);
}
static void
input_method_context_grab_cancel(struct weston_keyboard_grab *grab)
{
weston_keyboard_end_grab(grab->keyboard);
}
2013-04-18 23:25:39 +04:00
static const struct weston_keyboard_grab_interface input_method_context_grab = {
input_method_context_grab_key,
input_method_context_grab_modifier,
input_method_context_grab_cancel,
};
static void
input_method_context_grab_keyboard(struct wl_client *client,
struct wl_resource *resource,
uint32_t id)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
struct wl_resource *cr;
struct weston_seat *seat = context->input_method->seat;
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
if (!keyboard)
return;
cr = wl_resource_create(client, &wl_keyboard_interface, 1, id);
wl_resource_set_implementation(cr, NULL, context, unbind_keyboard);
context->keyboard = cr;
weston_keyboard_send_keymap(keyboard, cr);
2013-04-18 23:25:39 +04:00
if (keyboard->grab != &keyboard->default_grab) {
weston_keyboard_end_grab(keyboard);
}
2013-04-18 23:25:39 +04:00
weston_keyboard_start_grab(keyboard, &keyboard->input_method_grab);
keyboard->input_method_resource = cr;
}
static void
input_method_context_key(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state_w)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
struct weston_seat *seat = context->input_method->seat;
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2013-04-18 23:25:39 +04:00
struct weston_keyboard_grab *default_grab = &keyboard->default_grab;
struct timespec ts;
timespec_from_msec(&ts, time);
default_grab->interface->key(default_grab, &ts, key, state_w);
}
static void
input_method_context_modifiers(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
struct weston_seat *seat = context->input_method->seat;
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
2013-04-18 23:25:39 +04:00
struct weston_keyboard_grab *default_grab = &keyboard->default_grab;
default_grab->interface->modifiers(default_grab,
serial, mods_depressed,
mods_latched, mods_locked,
group);
}
static void
input_method_context_language(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
const char *language)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_language(context->input->resource,
serial, language);
}
static void
input_method_context_text_direction(struct wl_client *client,
struct wl_resource *resource,
uint32_t serial,
uint32_t direction)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->input)
zwp_text_input_v1_send_text_direction(context->input->resource,
serial, direction);
}
static const struct zwp_input_method_context_v1_interface context_implementation = {
input_method_context_destroy,
input_method_context_commit_string,
input_method_context_preedit_string,
input_method_context_preedit_styling,
input_method_context_preedit_cursor,
input_method_context_delete_surrounding_text,
input_method_context_cursor_position,
input_method_context_modifiers_map,
input_method_context_keysym,
input_method_context_grab_keyboard,
input_method_context_key,
input_method_context_modifiers,
input_method_context_language,
input_method_context_text_direction
};
static void
destroy_input_method_context(struct wl_resource *resource)
{
struct input_method_context *context =
wl_resource_get_user_data(resource);
if (context->keyboard)
wl_resource_destroy(context->keyboard);
if (context->input_method && context->input_method->context == context)
context->input_method->context = NULL;
free(context);
}
static void
input_method_context_create(struct text_input *input,
struct input_method *input_method)
{
struct input_method_context *context;
struct wl_resource *binding;
if (!input_method->input_method_binding)
return;
context = zalloc(sizeof *context);
if (context == NULL)
return;
binding = input_method->input_method_binding;
context->resource =
wl_resource_create(wl_resource_get_client(binding),
&zwp_input_method_context_v1_interface,
1, 0);
wl_resource_set_implementation(context->resource,
&context_implementation,
context, destroy_input_method_context);
context->input = input;
context->input_method = input_method;
input_method->context = context;
zwp_input_method_v1_send_activate(binding, context->resource);
}
static void
input_method_context_end_keyboard_grab(struct input_method_context *context)
{
struct weston_keyboard_grab *grab;
struct weston_keyboard *keyboard;
keyboard = weston_seat_get_keyboard(context->input_method->seat);
if (!keyboard)
return;
grab = &keyboard->input_method_grab;
keyboard = grab->keyboard;
if (!keyboard)
return;
if (keyboard->grab == grab)
weston_keyboard_end_grab(keyboard);
keyboard->input_method_resource = NULL;
}
static void
unbind_input_method(struct wl_resource *resource)
{
struct input_method *input_method = wl_resource_get_user_data(resource);
compositor: fix UAF on text-backend tear-down Found by Address sanitizer on test-devices: ==10640==ERROR: AddressSanitizer: heap-use-after-free on address 0x60c000000580 at pc 0x7fa0f050dcd1 bp 0x7fff41c908e0 sp 0x7fff41c908d8 WRITE of size 8 at 0x60c000000580 thread T0 #0 0x7fa0f050dcd0 in unbind_input_method ../../git/weston/compositor/text-backend.c:852 #1 0x7fa0efd1b20d in destroy_resource ../../git/wayland/src/wayland-server.c:724 #2 0x7fa0efd1f7f1 in for_each_helper ../../git/wayland/src/wayland-util.c:372 #3 0x7fa0efd1fcde in wl_map_for_each ../../git/wayland/src/wayland-util.c:385 #4 0x7fa0efd1b35c in wl_client_destroy ../../git/wayland/src/wayland-server.c:883 #5 0x7fa0f050ea82 in text_backend_destroy ../../git/weston/compositor/text-backend.c:1067 #6 0x7fa0ebb69f2f in shell_destroy ../../git/weston/desktop-shell/shell.c:5012 #7 0x7fa0efd55933 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #8 0x7fa0efd7d061 in weston_compositor_destroy ../../git/weston/libweston/compositor.c:7896 #9 0x7fa0f050a349 in wet_main ../../git/weston/compositor/main.c:3493 #10 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #11 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #12 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #13 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #14 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #15 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 #16 0x559c1e786769 in _start (/home/pq/build/weston-meson/tests/test-devices+0xc769) 0x60c000000580 is located 0 bytes inside of 120-byte region [0x60c000000580,0x60c0000005f8) freed by thread T0 here: #0 0x7fa0f0618fb0 in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.5+0xe8fb0) #1 0x7fa0f050df1d in input_method_notifier_destroy ../../git/weston/compositor/text-backend.c:902 #2 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #3 0x7fa0efd98086 in weston_seat_release ../../git/weston/libweston/input.c:3475 #4 0x7fa0ebb0d002 in test_seat_release ../../git/weston/tests/weston-test.c:132 #5 0x7fa0ebb0e197 in device_release ../../git/weston/tests/weston-test.c:314 #6 0x7fa0efca88ed in ffi_call_unix64 (/lib/x86_64-linux-gnu/libffi.so.6+0x68ed) previously allocated by thread T0 here: #0 0x7fa0f0619518 in calloc (/lib/x86_64-linux-gnu/libasan.so.5+0xe9518) #1 0x7fa0f050a8bf in zalloc ../../git/weston/include/libweston/zalloc.h:38 #2 0x7fa0f050e6f1 in text_backend_seat_created ../../git/weston/compositor/text-backend.c:1011 #3 0x7fa0f050e947 in handle_seat_created ../../git/weston/compositor/text-backend.c:1040 #4 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #5 0x7fa0efd97d57 in weston_seat_init ../../git/weston/libweston/input.c:3440 #6 0x7fa0ebb0ce4b in test_seat_init ../../git/weston/tests/weston-test.c:110 #7 0x7fa0ebb0f699 in wet_module_init ../../git/weston/tests/weston-test.c:592 #8 0x7fa0f04f8d69 in wet_load_module ../../git/weston/compositor/main.c:941 #9 0x7fa0f04f914d in load_modules ../../git/weston/compositor/main.c:1012 #10 0x7fa0f0509ec1 in wet_main ../../git/weston/compositor/main.c:3441 #11 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #12 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #13 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #14 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #15 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #16 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 Fix UAF by resetting wl_resource user data, and ensuring it is valid before used. Setting seat->input_method to NULL may not be necessary since it is being called from seat destroy listener, but added just in case. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
2021-05-07 15:58:10 +03:00
if (!input_method)
return;
input_method->input_method_binding = NULL;
input_method->context = NULL;
}
static void
bind_input_method(struct wl_client *client,
void *data,
uint32_t version,
uint32_t id)
{
struct input_method *input_method = data;
struct text_backend *text_backend = input_method->text_backend;
struct wl_resource *resource;
resource =
wl_resource_create(client,
&zwp_input_method_v1_interface, 1, id);
if (input_method->input_method_binding != NULL) {
wl_resource_post_error(resource,
WL_DISPLAY_ERROR_INVALID_OBJECT,
"interface object already bound");
return;
}
if (text_backend->input_method.client != client) {
wl_resource_post_error(resource,
WL_DISPLAY_ERROR_INVALID_OBJECT,
"permission to bind "
"input_method denied");
return;
}
wl_resource_set_implementation(resource, NULL, input_method,
unbind_input_method);
input_method->input_method_binding = resource;
}
static void
input_method_notifier_destroy(struct wl_listener *listener, void *data)
{
struct input_method *input_method =
container_of(listener, struct input_method, destroy_listener);
if (input_method->input)
deactivate_input_method(input_method);
compositor: fix UAF on text-backend tear-down Found by Address sanitizer on test-devices: ==10640==ERROR: AddressSanitizer: heap-use-after-free on address 0x60c000000580 at pc 0x7fa0f050dcd1 bp 0x7fff41c908e0 sp 0x7fff41c908d8 WRITE of size 8 at 0x60c000000580 thread T0 #0 0x7fa0f050dcd0 in unbind_input_method ../../git/weston/compositor/text-backend.c:852 #1 0x7fa0efd1b20d in destroy_resource ../../git/wayland/src/wayland-server.c:724 #2 0x7fa0efd1f7f1 in for_each_helper ../../git/wayland/src/wayland-util.c:372 #3 0x7fa0efd1fcde in wl_map_for_each ../../git/wayland/src/wayland-util.c:385 #4 0x7fa0efd1b35c in wl_client_destroy ../../git/wayland/src/wayland-server.c:883 #5 0x7fa0f050ea82 in text_backend_destroy ../../git/weston/compositor/text-backend.c:1067 #6 0x7fa0ebb69f2f in shell_destroy ../../git/weston/desktop-shell/shell.c:5012 #7 0x7fa0efd55933 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #8 0x7fa0efd7d061 in weston_compositor_destroy ../../git/weston/libweston/compositor.c:7896 #9 0x7fa0f050a349 in wet_main ../../git/weston/compositor/main.c:3493 #10 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #11 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #12 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #13 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #14 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #15 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 #16 0x559c1e786769 in _start (/home/pq/build/weston-meson/tests/test-devices+0xc769) 0x60c000000580 is located 0 bytes inside of 120-byte region [0x60c000000580,0x60c0000005f8) freed by thread T0 here: #0 0x7fa0f0618fb0 in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.5+0xe8fb0) #1 0x7fa0f050df1d in input_method_notifier_destroy ../../git/weston/compositor/text-backend.c:902 #2 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #3 0x7fa0efd98086 in weston_seat_release ../../git/weston/libweston/input.c:3475 #4 0x7fa0ebb0d002 in test_seat_release ../../git/weston/tests/weston-test.c:132 #5 0x7fa0ebb0e197 in device_release ../../git/weston/tests/weston-test.c:314 #6 0x7fa0efca88ed in ffi_call_unix64 (/lib/x86_64-linux-gnu/libffi.so.6+0x68ed) previously allocated by thread T0 here: #0 0x7fa0f0619518 in calloc (/lib/x86_64-linux-gnu/libasan.so.5+0xe9518) #1 0x7fa0f050a8bf in zalloc ../../git/weston/include/libweston/zalloc.h:38 #2 0x7fa0f050e6f1 in text_backend_seat_created ../../git/weston/compositor/text-backend.c:1011 #3 0x7fa0f050e947 in handle_seat_created ../../git/weston/compositor/text-backend.c:1040 #4 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #5 0x7fa0efd97d57 in weston_seat_init ../../git/weston/libweston/input.c:3440 #6 0x7fa0ebb0ce4b in test_seat_init ../../git/weston/tests/weston-test.c:110 #7 0x7fa0ebb0f699 in wet_module_init ../../git/weston/tests/weston-test.c:592 #8 0x7fa0f04f8d69 in wet_load_module ../../git/weston/compositor/main.c:941 #9 0x7fa0f04f914d in load_modules ../../git/weston/compositor/main.c:1012 #10 0x7fa0f0509ec1 in wet_main ../../git/weston/compositor/main.c:3441 #11 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #12 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #13 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #14 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #15 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #16 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 Fix UAF by resetting wl_resource user data, and ensuring it is valid before used. Setting seat->input_method to NULL may not be necessary since it is being called from seat destroy listener, but added just in case. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
2021-05-07 15:58:10 +03:00
if (input_method->input_method_binding)
wl_resource_set_user_data(input_method->input_method_binding, NULL);
2013-07-09 03:03:57 +04:00
wl_global_destroy(input_method->input_method_global);
wl_list_remove(&input_method->destroy_listener.link);
compositor: fix UAF on text-backend tear-down Found by Address sanitizer on test-devices: ==10640==ERROR: AddressSanitizer: heap-use-after-free on address 0x60c000000580 at pc 0x7fa0f050dcd1 bp 0x7fff41c908e0 sp 0x7fff41c908d8 WRITE of size 8 at 0x60c000000580 thread T0 #0 0x7fa0f050dcd0 in unbind_input_method ../../git/weston/compositor/text-backend.c:852 #1 0x7fa0efd1b20d in destroy_resource ../../git/wayland/src/wayland-server.c:724 #2 0x7fa0efd1f7f1 in for_each_helper ../../git/wayland/src/wayland-util.c:372 #3 0x7fa0efd1fcde in wl_map_for_each ../../git/wayland/src/wayland-util.c:385 #4 0x7fa0efd1b35c in wl_client_destroy ../../git/wayland/src/wayland-server.c:883 #5 0x7fa0f050ea82 in text_backend_destroy ../../git/weston/compositor/text-backend.c:1067 #6 0x7fa0ebb69f2f in shell_destroy ../../git/weston/desktop-shell/shell.c:5012 #7 0x7fa0efd55933 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #8 0x7fa0efd7d061 in weston_compositor_destroy ../../git/weston/libweston/compositor.c:7896 #9 0x7fa0f050a349 in wet_main ../../git/weston/compositor/main.c:3493 #10 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #11 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #12 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #13 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #14 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #15 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 #16 0x559c1e786769 in _start (/home/pq/build/weston-meson/tests/test-devices+0xc769) 0x60c000000580 is located 0 bytes inside of 120-byte region [0x60c000000580,0x60c0000005f8) freed by thread T0 here: #0 0x7fa0f0618fb0 in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.5+0xe8fb0) #1 0x7fa0f050df1d in input_method_notifier_destroy ../../git/weston/compositor/text-backend.c:902 #2 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #3 0x7fa0efd98086 in weston_seat_release ../../git/weston/libweston/input.c:3475 #4 0x7fa0ebb0d002 in test_seat_release ../../git/weston/tests/weston-test.c:132 #5 0x7fa0ebb0e197 in device_release ../../git/weston/tests/weston-test.c:314 #6 0x7fa0efca88ed in ffi_call_unix64 (/lib/x86_64-linux-gnu/libffi.so.6+0x68ed) previously allocated by thread T0 here: #0 0x7fa0f0619518 in calloc (/lib/x86_64-linux-gnu/libasan.so.5+0xe9518) #1 0x7fa0f050a8bf in zalloc ../../git/weston/include/libweston/zalloc.h:38 #2 0x7fa0f050e6f1 in text_backend_seat_created ../../git/weston/compositor/text-backend.c:1011 #3 0x7fa0f050e947 in handle_seat_created ../../git/weston/compositor/text-backend.c:1040 #4 0x7fa0efd86d77 in wl_signal_emit /home/pq/local/include/wayland-server-core.h:478 #5 0x7fa0efd97d57 in weston_seat_init ../../git/weston/libweston/input.c:3440 #6 0x7fa0ebb0ce4b in test_seat_init ../../git/weston/tests/weston-test.c:110 #7 0x7fa0ebb0f699 in wet_module_init ../../git/weston/tests/weston-test.c:592 #8 0x7fa0f04f8d69 in wet_load_module ../../git/weston/compositor/main.c:941 #9 0x7fa0f04f914d in load_modules ../../git/weston/compositor/main.c:1012 #10 0x7fa0f0509ec1 in wet_main ../../git/weston/compositor/main.c:3441 #11 0x559c1e794354 in execute_compositor ../../git/weston/tests/weston-test-fixture-compositor.c:432 #12 0x559c1e797dc0 in weston_test_harness_execute_as_client ../../git/weston/tests/weston-test-runner.c:528 #13 0x559c1e786ab8 in fixture_setup ../../git/weston/tests/devices-test.c:39 #14 0x559c1e786b3a in fixture_setup_run_ ../../git/weston/tests/devices-test.c:41 #15 0x559c1e798375 in main ../../git/weston/tests/weston-test-runner.c:661 #16 0x7fa0f016e09a in __libc_start_main ../csu/libc-start.c:308 Fix UAF by resetting wl_resource user data, and ensuring it is valid before used. Setting seat->input_method to NULL may not be necessary since it is being called from seat destroy listener, but added just in case. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.com>
2021-05-07 15:58:10 +03:00
input_method->seat->input_method = NULL;
free(input_method);
}
static void
handle_keyboard_focus(struct wl_listener *listener, void *data)
{
2013-04-18 23:25:39 +04:00
struct weston_keyboard *keyboard = data;
struct input_method *input_method =
container_of(listener, struct input_method,
keyboard_focus_listener);
struct weston_surface *surface = keyboard->focus;
if (!input_method->input)
return;
if (!surface || input_method->input->surface != surface)
deactivate_input_method(input_method);
}
static void
input_method_init_seat(struct weston_seat *seat)
{
struct weston_keyboard *keyboard = weston_seat_get_keyboard(seat);
if (seat->input_method->focus_listener_initialized)
return;
if (keyboard) {
seat->input_method->keyboard_focus_listener.notify =
handle_keyboard_focus;
wl_signal_add(&keyboard->focus_signal,
&seat->input_method->keyboard_focus_listener);
keyboard->input_method_grab.interface =
&input_method_context_grab;
}
seat->input_method->focus_listener_initialized = true;
}
static void launch_input_method(void *data);
static void
respawn_input_method_process(struct text_backend *text_backend)
{
struct timespec time;
int64_t tdiff;
/* if input_method dies more than 5 times in 10 seconds, give up */
weston_compositor_get_time(&time);
tdiff = timespec_sub_to_msec(&time,
&text_backend->input_method.deathstamp);
if (tdiff > 10000) {
text_backend->input_method.deathstamp = time;
text_backend->input_method.deathcount = 0;
}
text_backend->input_method.deathcount++;
if (text_backend->input_method.deathcount > 5) {
weston_log("input_method disconnected, giving up.\n");
return;
}
weston_log("input_method disconnected, respawning...\n");
launch_input_method(text_backend);
}
static void
input_method_client_notifier(struct wl_listener *listener, void *data)
{
struct text_backend *text_backend;
text_backend = container_of(listener, struct text_backend,
client_listener);
text_backend->input_method.client = NULL;
respawn_input_method_process(text_backend);
}
static void
launch_input_method(void *data)
{
struct text_backend *text_backend = data;
if (!text_backend->input_method.path)
return;
if (strcmp(text_backend->input_method.path, "") == 0)
return;
if (text_backend->input_method.overlay_keyboard)
setenv("WESTON_KEYBOARD_SURFACE_TYPE", "overlay", 1);
text_backend->input_method.client =
weston_client_start(text_backend->compositor,
text_backend->input_method.path);
if (!text_backend->input_method.client) {
weston_log("not able to start %s\n",
text_backend->input_method.path);
return;
}
text_backend->client_listener.notify = input_method_client_notifier;
wl_client_add_destroy_listener(text_backend->input_method.client,
&text_backend->client_listener);
}
static void
text_backend_seat_created(struct text_backend *text_backend,
struct weston_seat *seat)
{
struct input_method *input_method;
struct weston_compositor *ec = seat->compositor;
input_method = zalloc(sizeof *input_method);
if (input_method == NULL)
return;
input_method->seat = seat;
input_method->input = NULL;
input_method->focus_listener_initialized = false;
input_method->context = NULL;
input_method->text_backend = text_backend;
input_method->input_method_global =
wl_global_create(ec->wl_display,
&zwp_input_method_v1_interface, 1,
2013-07-09 03:03:57 +04:00
input_method, bind_input_method);
input_method->destroy_listener.notify = input_method_notifier_destroy;
wl_signal_add(&seat->destroy_signal, &input_method->destroy_listener);
seat->input_method = input_method;
}
static void
handle_seat_created(struct wl_listener *listener, void *data)
{
struct weston_seat *seat = data;
struct text_backend *text_backend =
container_of(listener, struct text_backend,
seat_created_listener);
text_backend_seat_created(text_backend, seat);
}
static void
text_backend_configuration(struct text_backend *text_backend)
{
struct weston_config *config = wet_get_config(text_backend->compositor);
2013-05-24 05:45:51 +04:00
struct weston_config_section *section;
char *client;
section = weston_config_get_section(config,
2013-05-24 05:45:51 +04:00
"input-method", NULL, NULL);
client = wet_get_libexec_path("weston-keyboard");
2013-05-24 05:45:51 +04:00
weston_config_section_get_string(section, "path",
&text_backend->input_method.path,
client);
weston_config_section_get_bool(section, "overlay-keyboard",
&text_backend->input_method.overlay_keyboard,
false);
free(client);
}
text_backend: make destructor call explicit We used to rely on the order in which the weston_compositor::destroy_signal callbacks happened, to not access freed memory. Don't know when, but this broke at least with ivi-shell, which caused crashes in random places on compositor shutdown. Valgrind found the following: Invalid write of size 8 at 0xC2EDC69: unbind_input_panel (input-panel-ivi.c:340) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea360 is 208 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0D7: wl_list_remove (wayland-util.c:57) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea370 is 224 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0E7: wl_list_remove (wayland-util.c:58) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea368 is 216 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Looking at the first of these, unbind_input_panel() gets called when the text-backend destroys its helper client which has bound to input_panel interface. This happens after the shell's destroy_signal callback has been called, so the shell has already been freed. The other two errors come from wl_list_remove(&input_panel_surface->link); which has gone stale when the shell was destroyed (shell->input_panel.surfaces list). Rather than creating even more destroy listeners and hooking them up in spaghetti, modify text-backend to not hook up to the compositor destroy signal. Instead, make it the text_backend_init() callers' responsibility to also call text_backend_destroy() appropriately, before the shell goes away. This fixed all the above Valgrind errors, and avoid a crash with ivi-shell when exiting Weston. Also using desktop-shell exhibited similar Valgrind errors which are fixed by this patch, but those didn't happen to cause any crashes AFAIK. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-By: Derek Foreman <derekf@osg.samsung.com>
2015-06-24 16:09:17 +03:00
WL_EXPORT void
text_backend_destroy(struct text_backend *text_backend)
{
wl_list_remove(&text_backend->seat_created_listener.link);
if (text_backend->input_method.client) {
/* disable respawn */
wl_list_remove(&text_backend->client_listener.link);
wl_client_destroy(text_backend->input_method.client);
}
free(text_backend->input_method.path);
free(text_backend);
}
text_backend: make destructor call explicit We used to rely on the order in which the weston_compositor::destroy_signal callbacks happened, to not access freed memory. Don't know when, but this broke at least with ivi-shell, which caused crashes in random places on compositor shutdown. Valgrind found the following: Invalid write of size 8 at 0xC2EDC69: unbind_input_panel (input-panel-ivi.c:340) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea360 is 208 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0D7: wl_list_remove (wayland-util.c:57) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea370 is 224 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0E7: wl_list_remove (wayland-util.c:58) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea368 is 216 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Looking at the first of these, unbind_input_panel() gets called when the text-backend destroys its helper client which has bound to input_panel interface. This happens after the shell's destroy_signal callback has been called, so the shell has already been freed. The other two errors come from wl_list_remove(&input_panel_surface->link); which has gone stale when the shell was destroyed (shell->input_panel.surfaces list). Rather than creating even more destroy listeners and hooking them up in spaghetti, modify text-backend to not hook up to the compositor destroy signal. Instead, make it the text_backend_init() callers' responsibility to also call text_backend_destroy() appropriately, before the shell goes away. This fixed all the above Valgrind errors, and avoid a crash with ivi-shell when exiting Weston. Also using desktop-shell exhibited similar Valgrind errors which are fixed by this patch, but those didn't happen to cause any crashes AFAIK. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-By: Derek Foreman <derekf@osg.samsung.com>
2015-06-24 16:09:17 +03:00
WL_EXPORT struct text_backend *
text_backend_init(struct weston_compositor *ec)
{
struct text_backend *text_backend;
struct weston_seat *seat;
struct wl_event_loop *loop;
text_backend = zalloc(sizeof(*text_backend));
if (text_backend == NULL)
text_backend: make destructor call explicit We used to rely on the order in which the weston_compositor::destroy_signal callbacks happened, to not access freed memory. Don't know when, but this broke at least with ivi-shell, which caused crashes in random places on compositor shutdown. Valgrind found the following: Invalid write of size 8 at 0xC2EDC69: unbind_input_panel (input-panel-ivi.c:340) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea360 is 208 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0D7: wl_list_remove (wayland-util.c:57) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea370 is 224 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0E7: wl_list_remove (wayland-util.c:58) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea368 is 216 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Looking at the first of these, unbind_input_panel() gets called when the text-backend destroys its helper client which has bound to input_panel interface. This happens after the shell's destroy_signal callback has been called, so the shell has already been freed. The other two errors come from wl_list_remove(&input_panel_surface->link); which has gone stale when the shell was destroyed (shell->input_panel.surfaces list). Rather than creating even more destroy listeners and hooking them up in spaghetti, modify text-backend to not hook up to the compositor destroy signal. Instead, make it the text_backend_init() callers' responsibility to also call text_backend_destroy() appropriately, before the shell goes away. This fixed all the above Valgrind errors, and avoid a crash with ivi-shell when exiting Weston. Also using desktop-shell exhibited similar Valgrind errors which are fixed by this patch, but those didn't happen to cause any crashes AFAIK. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-By: Derek Foreman <derekf@osg.samsung.com>
2015-06-24 16:09:17 +03:00
return NULL;
text_backend->compositor = ec;
text_backend_configuration(text_backend);
wl_list_for_each(seat, &ec->seat_list, link)
text_backend_seat_created(text_backend, seat);
text_backend->seat_created_listener.notify = handle_seat_created;
wl_signal_add(&ec->seat_created_signal,
&text_backend->seat_created_listener);
text_input_manager_create(ec);
loop = wl_display_get_event_loop(ec->wl_display);
wl_event_loop_add_idle(loop, launch_input_method, text_backend);
text_backend: make destructor call explicit We used to rely on the order in which the weston_compositor::destroy_signal callbacks happened, to not access freed memory. Don't know when, but this broke at least with ivi-shell, which caused crashes in random places on compositor shutdown. Valgrind found the following: Invalid write of size 8 at 0xC2EDC69: unbind_input_panel (input-panel-ivi.c:340) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea360 is 208 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0D7: wl_list_remove (wayland-util.c:57) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea370 is 224 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Invalid write of size 8 at 0x4E3E0E7: wl_list_remove (wayland-util.c:58) by 0xC2EDEE9: destroy_input_panel_surface (input-panel-ivi.c:191) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3BC7B: wl_resource_destroy (wayland-server.c:550) by 0x40DB8B: wl_signal_emit (wayland-server-core.h:264) by 0x40DB8B: weston_surface_destroy (compositor.c:1883) by 0x40DB8B: weston_surface_destroy (compositor.c:1873) by 0x4E3B6BB: destroy_resource (wayland-server.c:537) by 0x4E3E085: for_each_helper.isra.0 (wayland-util.c:359) by 0x4E3E60D: wl_map_for_each (wayland-util.c:365) by 0x4E3BEC7: wl_client_destroy (wayland-server.c:675) by 0x4182F2: text_backend_notifier_destroy (text-backend.c:1047) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Address 0x67ea368 is 216 bytes inside a block of size 232 free'd at 0x4C2A6BC: free (vg_replace_malloc.c:473) by 0x4084FB: wl_signal_emit (wayland-server-core.h:264) by 0x4084FB: main (compositor.c:5465) Looking at the first of these, unbind_input_panel() gets called when the text-backend destroys its helper client which has bound to input_panel interface. This happens after the shell's destroy_signal callback has been called, so the shell has already been freed. The other two errors come from wl_list_remove(&input_panel_surface->link); which has gone stale when the shell was destroyed (shell->input_panel.surfaces list). Rather than creating even more destroy listeners and hooking them up in spaghetti, modify text-backend to not hook up to the compositor destroy signal. Instead, make it the text_backend_init() callers' responsibility to also call text_backend_destroy() appropriately, before the shell goes away. This fixed all the above Valgrind errors, and avoid a crash with ivi-shell when exiting Weston. Also using desktop-shell exhibited similar Valgrind errors which are fixed by this patch, but those didn't happen to cause any crashes AFAIK. Signed-off-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk> Reviewed-By: Derek Foreman <derekf@osg.samsung.com>
2015-06-24 16:09:17 +03:00
return text_backend;
}