2017-11-30 17:53:26 +00:00
|
|
|
/* GTK - The GIMP Toolkit
|
|
|
|
* Copyright (C) 2017 Red Hat, Inc.
|
2018-03-29 17:49:10 +00:00
|
|
|
* Copyright (C) 2018 Purism SPC
|
2017-11-30 17:53:26 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <wayland-client-protocol.h>
|
|
|
|
|
2020-12-25 17:57:48 +00:00
|
|
|
#include "gtk/gtkdragsourceprivate.h"
|
2018-02-23 19:59:49 +00:00
|
|
|
#include "gtk/gtkimcontextwayland.h"
|
2017-11-30 17:53:26 +00:00
|
|
|
#include "gtk/gtkintl.h"
|
2018-03-09 07:13:13 +00:00
|
|
|
#include "gtk/gtkimmoduleprivate.h"
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
#include "gdk/wayland/gdkwayland.h"
|
2018-03-29 17:49:10 +00:00
|
|
|
#include "text-input-unstable-v3-client-protocol.h"
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
typedef struct _GtkIMContextWaylandGlobal GtkIMContextWaylandGlobal;
|
|
|
|
typedef struct _GtkIMContextWayland GtkIMContextWayland;
|
|
|
|
typedef struct _GtkIMContextWaylandClass GtkIMContextWaylandClass;
|
|
|
|
|
|
|
|
struct _GtkIMContextWaylandGlobal
|
|
|
|
{
|
|
|
|
struct wl_display *display;
|
|
|
|
struct wl_registry *registry;
|
2018-03-27 18:42:43 +00:00
|
|
|
uint32_t text_input_manager_wl_id;
|
2018-03-29 17:49:10 +00:00
|
|
|
struct zwp_text_input_manager_v3 *text_input_manager;
|
|
|
|
struct zwp_text_input_v3 *text_input;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
GtkIMContext *current;
|
2018-09-14 20:31:55 +00:00
|
|
|
|
2018-10-16 16:29:08 +00:00
|
|
|
/* The input-method.enter event may happen before or after GTK focus-in,
|
|
|
|
* so the context may not exist at the time. Same for leave and focus-out. */
|
|
|
|
gboolean focused;
|
|
|
|
|
2018-09-14 20:31:55 +00:00
|
|
|
guint serial;
|
2017-11-30 17:53:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct _GtkIMContextWaylandClass
|
|
|
|
{
|
|
|
|
GtkIMContextSimpleClass parent_class;
|
|
|
|
};
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
struct preedit {
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2020-07-24 13:54:49 +00:00
|
|
|
int cursor_begin;
|
|
|
|
int cursor_end;
|
2018-03-29 17:49:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct surrounding_delete {
|
|
|
|
guint before_length;
|
|
|
|
guint after_length;
|
|
|
|
};
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
struct _GtkIMContextWayland
|
|
|
|
{
|
|
|
|
GtkIMContextSimple parent_instance;
|
|
|
|
GtkWidget *widget;
|
|
|
|
|
|
|
|
GtkGesture *gesture;
|
2020-07-24 20:32:16 +00:00
|
|
|
double press_x;
|
|
|
|
double press_y;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
struct {
|
2020-07-24 18:40:36 +00:00
|
|
|
char *text;
|
2020-07-24 13:54:49 +00:00
|
|
|
int cursor_idx;
|
|
|
|
int anchor_idx;
|
2017-11-30 17:53:26 +00:00
|
|
|
} surrounding;
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
enum zwp_text_input_v3_change_cause surrounding_change;
|
|
|
|
|
|
|
|
struct surrounding_delete pending_surrounding_delete;
|
|
|
|
|
|
|
|
struct preedit current_preedit;
|
|
|
|
struct preedit pending_preedit;
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char *pending_commit;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
cairo_rectangle_int_t cursor_rect;
|
|
|
|
guint use_preedit : 1;
|
|
|
|
};
|
|
|
|
|
2020-06-29 20:12:53 +00:00
|
|
|
static void gtk_im_context_wayland_focus_out (GtkIMContext *context);
|
|
|
|
|
2018-03-05 17:51:00 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkIMContextWayland, gtk_im_context_wayland, GTK_TYPE_IM_CONTEXT_SIMPLE,
|
2018-03-09 07:13:13 +00:00
|
|
|
gtk_im_module_ensure_extension_point ();
|
2018-02-23 19:59:49 +00:00
|
|
|
g_io_extension_point_implement (GTK_IM_MODULE_EXTENSION_POINT_NAME,
|
|
|
|
g_define_type_id,
|
|
|
|
"wayland",
|
2021-12-03 23:15:54 +00:00
|
|
|
100));
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-02-25 02:44:03 +00:00
|
|
|
#define GTK_IM_CONTEXT_WAYLAND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), gtk_im_context_wayland_get_type (), GtkIMContextWayland))
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
static GtkIMContextWaylandGlobal *
|
|
|
|
gtk_im_context_wayland_global_get (GdkDisplay *display);
|
|
|
|
|
|
|
|
static GtkIMContextWaylandGlobal *
|
|
|
|
gtk_im_context_wayland_get_global (GtkIMContextWayland *self)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global;
|
|
|
|
|
|
|
|
if (self->widget == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
global = gtk_im_context_wayland_global_get (gtk_widget_get_display (self->widget));
|
|
|
|
if (global->current != GTK_IM_CONTEXT (self))
|
|
|
|
return NULL;
|
|
|
|
if (global->text_input == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return global;
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
static void
|
2018-03-29 17:49:10 +00:00
|
|
|
notify_external_change (GtkIMContextWayland *context)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
2018-03-29 17:49:10 +00:00
|
|
|
gboolean result;
|
2018-03-27 01:38:28 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
2018-09-07 14:54:13 +00:00
|
|
|
return;
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
context->surrounding_change = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_OTHER;
|
2019-01-22 03:28:14 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_preedit (void *data,
|
2018-03-29 17:49:10 +00:00
|
|
|
struct zwp_text_input_v3 *text_input,
|
2017-11-30 17:53:26 +00:00
|
|
|
const char *text,
|
2020-07-24 13:54:49 +00:00
|
|
|
int cursor_begin,
|
|
|
|
int cursor_end)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
GtkIMContextWayland *context;
|
2018-03-29 17:49:10 +00:00
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
if (!global->current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
2018-03-29 17:49:10 +00:00
|
|
|
|
|
|
|
g_free (context->pending_preedit.text);
|
|
|
|
context->pending_preedit.text = g_strdup (text);
|
|
|
|
context->pending_preedit.cursor_begin = cursor_begin;
|
|
|
|
context->pending_preedit.cursor_end = cursor_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_preedit_apply (GtkIMContextWaylandGlobal *global)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context;
|
|
|
|
gboolean state_change;
|
|
|
|
struct preedit defaults = {0};
|
|
|
|
|
|
|
|
if (!global->current)
|
2017-11-30 17:53:26 +00:00
|
|
|
return;
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
|
|
|
|
|
|
|
state_change = ((context->pending_preedit.text == NULL)
|
|
|
|
!= (context->current_preedit.text == NULL));
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
if (state_change && !context->current_preedit.text)
|
2017-11-30 17:53:26 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-start");
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
g_free (context->current_preedit.text);
|
|
|
|
context->current_preedit = context->pending_preedit;
|
|
|
|
context->pending_preedit = defaults;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
g_signal_emit_by_name (context, "preedit-changed");
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
if (state_change && !context->current_preedit.text)
|
2017-11-30 17:53:26 +00:00
|
|
|
g_signal_emit_by_name (context, "preedit-end");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_commit (void *data,
|
2018-03-29 17:49:10 +00:00
|
|
|
struct zwp_text_input_v3 *text_input,
|
2017-11-30 17:53:26 +00:00
|
|
|
const char *text)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
2018-03-29 17:49:10 +00:00
|
|
|
GtkIMContextWayland *context;
|
|
|
|
|
|
|
|
if (!global->current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
|
|
|
|
|
|
|
g_free (context->pending_commit);
|
|
|
|
context->pending_commit = g_strdup (text);
|
|
|
|
}
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
static void
|
|
|
|
text_input_commit_apply (GtkIMContextWaylandGlobal *global, gboolean valid)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context;
|
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
|
|
|
if (context->pending_commit && valid)
|
|
|
|
g_signal_emit_by_name (global->current, "commit", context->pending_commit);
|
|
|
|
g_free (context->pending_commit);
|
|
|
|
context->pending_commit = NULL;
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_delete_surrounding_text (void *data,
|
2018-03-29 17:49:10 +00:00
|
|
|
struct zwp_text_input_v3 *text_input,
|
|
|
|
uint32_t before_length,
|
|
|
|
uint32_t after_length)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
2018-03-29 17:49:10 +00:00
|
|
|
GtkIMContextWayland *context;
|
|
|
|
|
|
|
|
if (!global->current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
context->pending_surrounding_delete.before_length = before_length;
|
|
|
|
context->pending_surrounding_delete.after_length = after_length;
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
static void
|
|
|
|
text_input_delete_surrounding_text_apply (GtkIMContextWaylandGlobal *global,
|
|
|
|
gboolean valid)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context;
|
2018-09-28 15:47:56 +00:00
|
|
|
gboolean retval;
|
2020-07-24 13:54:49 +00:00
|
|
|
int len;
|
2018-03-29 17:49:10 +00:00
|
|
|
struct surrounding_delete defaults = {0};
|
2018-09-28 15:47:56 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
context = GTK_IM_CONTEXT_WAYLAND (global->current);
|
2018-09-28 15:47:56 +00:00
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
len = context->pending_surrounding_delete.after_length
|
|
|
|
+ context->pending_surrounding_delete.before_length;
|
|
|
|
if (len > 0 && valid)
|
|
|
|
g_signal_emit_by_name (global->current, "delete-surrounding",
|
|
|
|
-context->pending_surrounding_delete.before_length,
|
2018-09-28 15:47:56 +00:00
|
|
|
len, &retval);
|
2018-03-29 17:49:10 +00:00
|
|
|
context->pending_surrounding_delete = defaults;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_done (void *data,
|
|
|
|
struct zwp_text_input_v3 *text_input,
|
|
|
|
uint32_t serial)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
gboolean result;
|
|
|
|
gboolean valid;
|
|
|
|
|
|
|
|
if (!global->current)
|
|
|
|
return;
|
|
|
|
|
2018-09-14 20:31:55 +00:00
|
|
|
valid = serial == global->serial;
|
2018-03-29 17:49:10 +00:00
|
|
|
text_input_delete_surrounding_text_apply(global, valid);
|
|
|
|
text_input_commit_apply(global, valid);
|
|
|
|
g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
|
|
|
|
text_input_preedit_apply(global);
|
|
|
|
}
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
static void
|
|
|
|
notify_surrounding_text (GtkIMContextWayland *context)
|
|
|
|
{
|
2018-12-06 16:05:43 +00:00
|
|
|
#define MAX_LEN 4000
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *start, *end;
|
2018-12-06 16:05:43 +00:00
|
|
|
int len, cursor, anchor;
|
|
|
|
char *str = NULL;
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
if (!context->surrounding.text)
|
|
|
|
return;
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
|
|
|
return;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-12-06 16:05:43 +00:00
|
|
|
len = strlen (context->surrounding.text);
|
|
|
|
cursor = context->surrounding.cursor_idx;
|
|
|
|
anchor = context->surrounding.anchor_idx;
|
|
|
|
|
|
|
|
/* The protocol specifies a maximum length of 4KiB on transfers,
|
|
|
|
* mangle the surrounding text if it's bigger than that, and relocate
|
|
|
|
* cursor/anchor locations as per the string being sent.
|
|
|
|
*/
|
|
|
|
if (len > MAX_LEN)
|
|
|
|
{
|
|
|
|
if (context->surrounding.cursor_idx < MAX_LEN &&
|
|
|
|
context->surrounding.anchor_idx < MAX_LEN)
|
|
|
|
{
|
|
|
|
start = context->surrounding.text;
|
|
|
|
end = &context->surrounding.text[MAX_LEN];
|
|
|
|
}
|
|
|
|
else if (context->surrounding.cursor_idx > len - MAX_LEN &&
|
2019-01-04 04:55:26 +00:00
|
|
|
context->surrounding.anchor_idx > len - MAX_LEN)
|
2018-12-06 16:05:43 +00:00
|
|
|
{
|
|
|
|
start = &context->surrounding.text[len - MAX_LEN];
|
|
|
|
end = &context->surrounding.text[len];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int mid, a, b;
|
|
|
|
int cursor_len = ABS (context->surrounding.cursor_idx -
|
|
|
|
context->surrounding.anchor_idx);
|
|
|
|
|
|
|
|
if (cursor_len > MAX_LEN)
|
|
|
|
{
|
|
|
|
g_warn_if_reached ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mid = MIN (context->surrounding.cursor_idx,
|
2020-03-05 02:57:53 +00:00
|
|
|
context->surrounding.anchor_idx) + (cursor_len / 2);
|
2018-12-06 16:05:43 +00:00
|
|
|
a = MAX (0, mid - (MAX_LEN / 2));
|
2020-04-03 10:44:37 +00:00
|
|
|
b = MIN (len, mid + (MAX_LEN / 2));
|
2018-12-06 16:05:43 +00:00
|
|
|
|
|
|
|
start = &context->surrounding.text[a];
|
|
|
|
end = &context->surrounding.text[b];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start != context->surrounding.text)
|
|
|
|
start = g_utf8_next_char (start);
|
|
|
|
if (end != &context->surrounding.text[len])
|
|
|
|
end = g_utf8_find_prev_char (context->surrounding.text, end);
|
|
|
|
|
|
|
|
cursor -= start - context->surrounding.text;
|
|
|
|
anchor -= start - context->surrounding.text;
|
|
|
|
|
|
|
|
str = g_strndup (start, end - start);
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_set_surrounding_text (global->text_input,
|
2018-12-06 16:05:43 +00:00
|
|
|
str ? str : context->surrounding.text,
|
|
|
|
cursor, anchor);
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_set_text_change_cause (global->text_input,
|
|
|
|
context->surrounding_change);
|
2018-12-06 16:05:43 +00:00
|
|
|
g_free (str);
|
|
|
|
#undef MAX_LEN
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notify_cursor_location (GtkIMContextWayland *context)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
2017-11-30 17:53:26 +00:00
|
|
|
cairo_rectangle_int_t rect;
|
2020-05-17 21:08:01 +00:00
|
|
|
double x, y;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
2017-11-30 17:53:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
rect = context->cursor_rect;
|
|
|
|
gtk_widget_translate_coordinates (context->widget,
|
2019-05-20 04:47:50 +00:00
|
|
|
GTK_WIDGET (gtk_widget_get_root (context->widget)),
|
2017-11-30 17:53:26 +00:00
|
|
|
rect.x, rect.y,
|
2020-05-17 21:08:01 +00:00
|
|
|
&x, &y);
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2020-05-17 21:08:01 +00:00
|
|
|
rect.x = x;
|
|
|
|
rect.y = y;
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_set_cursor_rectangle (global->text_input,
|
|
|
|
rect.x, rect.y,
|
|
|
|
rect.width, rect.height);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
translate_hints (GtkInputHints input_hints,
|
|
|
|
GtkInputPurpose purpose)
|
|
|
|
{
|
|
|
|
uint32_t hints = 0;
|
|
|
|
|
|
|
|
if (input_hints & GTK_INPUT_HINT_SPELLCHECK)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_SPELLCHECK;
|
2017-11-30 17:53:26 +00:00
|
|
|
if (input_hints & GTK_INPUT_HINT_WORD_COMPLETION)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_COMPLETION;
|
2017-11-30 17:53:26 +00:00
|
|
|
if (input_hints & GTK_INPUT_HINT_LOWERCASE)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_LOWERCASE;
|
2017-11-30 17:53:26 +00:00
|
|
|
if (input_hints & GTK_INPUT_HINT_UPPERCASE_CHARS)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_UPPERCASE;
|
2017-11-30 17:53:26 +00:00
|
|
|
if (input_hints & GTK_INPUT_HINT_UPPERCASE_WORDS)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_TITLECASE;
|
2017-11-30 17:53:26 +00:00
|
|
|
if (input_hints & GTK_INPUT_HINT_UPPERCASE_SENTENCES)
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= ZWP_TEXT_INPUT_V3_CONTENT_HINT_AUTO_CAPITALIZATION;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
if (purpose == GTK_INPUT_PURPOSE_PIN ||
|
|
|
|
purpose == GTK_INPUT_PURPOSE_PASSWORD)
|
|
|
|
{
|
2018-03-29 17:49:10 +00:00
|
|
|
hints |= (ZWP_TEXT_INPUT_V3_CONTENT_HINT_HIDDEN_TEXT |
|
|
|
|
ZWP_TEXT_INPUT_V3_CONTENT_HINT_SENSITIVE_DATA);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hints;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
translate_purpose (GtkInputPurpose purpose)
|
|
|
|
{
|
|
|
|
switch (purpose)
|
|
|
|
{
|
|
|
|
case GTK_INPUT_PURPOSE_FREE_FORM:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_ALPHA:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_ALPHA;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_DIGITS:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_DIGITS;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_NUMBER:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NUMBER;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_PHONE:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PHONE;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_URL:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_URL;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_EMAIL:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_EMAIL;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_NAME:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NAME;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_PASSWORD:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PASSWORD;
|
2017-11-30 17:53:26 +00:00
|
|
|
case GTK_INPUT_PURPOSE_PIN:
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_PIN;
|
2019-12-12 14:37:51 +00:00
|
|
|
case GTK_INPUT_PURPOSE_TERMINAL:
|
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_TERMINAL;
|
2018-02-23 19:59:49 +00:00
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
return ZWP_TEXT_INPUT_V3_CONTENT_PURPOSE_NORMAL;
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
notify_content_type (GtkIMContextWayland *context)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
2017-11-30 17:53:26 +00:00
|
|
|
GtkInputHints hints;
|
|
|
|
GtkInputPurpose purpose;
|
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
2017-11-30 17:53:26 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
g_object_get (context,
|
|
|
|
"input-hints", &hints,
|
|
|
|
"input-purpose", &purpose,
|
|
|
|
NULL);
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_set_content_type (global->text_input,
|
|
|
|
translate_hints (hints, purpose),
|
|
|
|
translate_purpose (purpose));
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
commit_state (GtkIMContextWayland *context)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
|
|
|
|
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
2017-11-30 17:53:26 +00:00
|
|
|
return;
|
2019-01-22 03:28:14 +00:00
|
|
|
|
2018-09-14 20:31:55 +00:00
|
|
|
global->serial++;
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_commit (global->text_input);
|
|
|
|
context->surrounding_change = ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD;
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context = GTK_IM_CONTEXT_WAYLAND (object);
|
|
|
|
|
2020-06-29 20:12:53 +00:00
|
|
|
gtk_im_context_wayland_focus_out (GTK_IM_CONTEXT (context));
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
g_clear_object (&context->widget);
|
|
|
|
g_clear_object (&context->gesture);
|
2018-10-30 20:57:09 +00:00
|
|
|
g_free (context->surrounding.text);
|
|
|
|
g_free (context->current_preedit.text);
|
|
|
|
g_free (context->pending_preedit.text);
|
|
|
|
g_free (context->pending_commit);
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-02-19 23:29:00 +00:00
|
|
|
G_OBJECT_CLASS (gtk_im_context_wayland_parent_class)->finalize (object);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-29 17:10:46 +00:00
|
|
|
pressed_cb (GtkGestureClick *gesture,
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_press,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-05-29 17:10:46 +00:00
|
|
|
GtkIMContextWayland *context)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
if (n_press == 1)
|
|
|
|
{
|
|
|
|
context->press_x = x;
|
|
|
|
context->press_y = y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-05-29 17:10:46 +00:00
|
|
|
released_cb (GtkGestureClick *gesture,
|
2020-07-24 13:54:49 +00:00
|
|
|
int n_press,
|
2020-07-24 20:32:16 +00:00
|
|
|
double x,
|
|
|
|
double y,
|
2019-05-29 17:10:46 +00:00
|
|
|
GtkIMContextWayland *context)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
2017-11-30 17:53:26 +00:00
|
|
|
GtkInputHints hints;
|
2018-03-29 17:49:10 +00:00
|
|
|
gboolean result;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (context);
|
|
|
|
if (global == NULL)
|
|
|
|
return;
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
g_object_get (context, "input-hints", &hints, NULL);
|
|
|
|
|
2018-10-16 16:29:08 +00:00
|
|
|
if (global->focused &&
|
|
|
|
n_press == 1 &&
|
2017-11-30 17:53:26 +00:00
|
|
|
(hints & GTK_INPUT_HINT_INHIBIT_OSK) == 0 &&
|
2020-12-25 17:57:48 +00:00
|
|
|
!gtk_drag_check_threshold_double (context->widget,
|
|
|
|
context->press_x,
|
|
|
|
context->press_y,
|
|
|
|
x, y))
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
2018-10-16 16:29:08 +00:00
|
|
|
zwp_text_input_v3_enable (global->text_input);
|
2018-03-29 17:49:10 +00:00
|
|
|
g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
|
|
|
|
commit_state (context);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_set_client_widget (GtkIMContext *context,
|
|
|
|
GtkWidget *widget)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
|
|
|
|
if (widget == context_wayland->widget)
|
|
|
|
return;
|
|
|
|
|
2018-03-09 05:14:59 +00:00
|
|
|
if (context_wayland->widget)
|
|
|
|
{
|
2020-11-05 15:00:29 +00:00
|
|
|
gtk_im_context_wayland_focus_out (context);
|
2018-03-09 05:14:59 +00:00
|
|
|
gtk_widget_remove_controller (context_wayland->widget, GTK_EVENT_CONTROLLER (context_wayland->gesture));
|
|
|
|
context_wayland->gesture = NULL;
|
|
|
|
}
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
g_set_object (&context_wayland->widget, widget);
|
|
|
|
|
|
|
|
if (widget)
|
|
|
|
{
|
|
|
|
GtkGesture *gesture;
|
|
|
|
|
2019-05-29 17:10:46 +00:00
|
|
|
gesture = gtk_gesture_click_new ();
|
2021-04-03 02:24:00 +00:00
|
|
|
gtk_event_controller_set_name (GTK_EVENT_CONTROLLER (gesture), "wayland-im-context-click");
|
2017-11-30 17:53:26 +00:00
|
|
|
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture),
|
|
|
|
GTK_PHASE_CAPTURE);
|
|
|
|
g_signal_connect (gesture, "pressed",
|
|
|
|
G_CALLBACK (pressed_cb), context);
|
|
|
|
g_signal_connect (gesture, "released",
|
|
|
|
G_CALLBACK (released_cb), context);
|
2018-03-09 05:14:59 +00:00
|
|
|
gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (gesture));
|
2017-11-30 17:53:26 +00:00
|
|
|
context_wayland->gesture = gesture;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_get_preedit_string (GtkIMContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
char **str,
|
2017-11-30 17:53:26 +00:00
|
|
|
PangoAttrList **attrs,
|
2020-07-24 13:54:49 +00:00
|
|
|
int *cursor_pos)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
2021-08-24 16:02:31 +00:00
|
|
|
const char *preedit_str;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-07-28 17:37:21 +00:00
|
|
|
if (attrs)
|
|
|
|
*attrs = NULL;
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
GTK_IM_CONTEXT_CLASS (gtk_im_context_wayland_parent_class)->get_preedit_string (context,
|
|
|
|
str, attrs,
|
|
|
|
cursor_pos);
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
/* If the parent implementation returns a len>0 string, go with it */
|
2018-06-24 20:49:46 +00:00
|
|
|
if (str && *str)
|
|
|
|
{
|
|
|
|
if (**str)
|
|
|
|
return;
|
|
|
|
|
|
|
|
g_free (*str);
|
|
|
|
}
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
preedit_str =
|
2021-08-24 16:02:31 +00:00
|
|
|
context_wayland->current_preedit.text ? context_wayland->current_preedit.text : "";
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
if (cursor_pos)
|
2020-01-17 08:01:01 +00:00
|
|
|
*cursor_pos = g_utf8_strlen (preedit_str,
|
|
|
|
context_wayland->current_preedit.cursor_begin);
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2021-08-24 16:02:31 +00:00
|
|
|
if (str)
|
|
|
|
*str = g_strdup (preedit_str);
|
2017-11-30 17:53:26 +00:00
|
|
|
if (attrs)
|
|
|
|
{
|
2021-02-18 03:57:41 +00:00
|
|
|
PangoAttribute *attr;
|
|
|
|
guint len = strlen (preedit_str);
|
|
|
|
|
2018-07-28 17:37:21 +00:00
|
|
|
if (!*attrs)
|
|
|
|
*attrs = pango_attr_list_new ();
|
2021-02-18 03:57:41 +00:00
|
|
|
|
|
|
|
attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
|
|
|
|
attr->start_index = 0;
|
|
|
|
attr->end_index = len;
|
|
|
|
pango_attr_list_insert (*attrs, attr);
|
|
|
|
|
2021-02-18 03:08:43 +00:00
|
|
|
/* enable fallback, since IBus will send us things like ⎄ */
|
2021-02-18 03:57:41 +00:00
|
|
|
attr = pango_attr_fallback_new (TRUE);
|
|
|
|
attr->start_index = 0;
|
|
|
|
attr->end_index = len;
|
|
|
|
pango_attr_list_insert (*attrs, attr);
|
|
|
|
|
2018-03-29 17:49:10 +00:00
|
|
|
if (context_wayland->current_preedit.cursor_begin
|
|
|
|
!= context_wayland->current_preedit.cursor_end)
|
|
|
|
{
|
|
|
|
/* FIXME: Oh noes, how to highlight while taking into account user preferences? */
|
|
|
|
PangoAttribute *cursor = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
|
|
|
|
cursor->start_index = context_wayland->current_preedit.cursor_begin;
|
|
|
|
cursor->end_index = context_wayland->current_preedit.cursor_end;
|
|
|
|
pango_attr_list_insert (*attrs, cursor);
|
|
|
|
}
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_im_context_wayland_filter_keypress (GtkIMContext *context,
|
2020-02-21 02:21:08 +00:00
|
|
|
GdkEvent *key)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
/* This is done by the compositor */
|
2018-02-19 23:29:00 +00:00
|
|
|
return GTK_IM_CONTEXT_CLASS (gtk_im_context_wayland_parent_class)->filter_keypress (context, key);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-22 03:28:14 +00:00
|
|
|
enable (GtkIMContextWayland *context_wayland,
|
|
|
|
GtkIMContextWaylandGlobal *global)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
2018-03-29 17:49:10 +00:00
|
|
|
gboolean result;
|
2018-10-16 16:29:08 +00:00
|
|
|
zwp_text_input_v3_enable (global->text_input);
|
2018-03-29 17:49:10 +00:00
|
|
|
g_signal_emit_by_name (global->current, "retrieve-surrounding", &result);
|
2017-11-30 17:53:26 +00:00
|
|
|
notify_content_type (context_wayland);
|
|
|
|
notify_cursor_location (context_wayland);
|
|
|
|
commit_state (context_wayland);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2019-01-22 03:28:14 +00:00
|
|
|
disable (GtkIMContextWayland *context_wayland,
|
|
|
|
GtkIMContextWaylandGlobal *global)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
2018-03-29 17:49:10 +00:00
|
|
|
zwp_text_input_v3_disable (global->text_input);
|
|
|
|
commit_state (context_wayland);
|
2018-08-23 10:32:11 +00:00
|
|
|
|
|
|
|
/* after disable, incoming state changes won't take effect anyway */
|
2018-09-11 12:12:28 +00:00
|
|
|
if (context_wayland->current_preedit.text)
|
|
|
|
{
|
|
|
|
text_input_preedit (global, global->text_input, NULL, 0, 0);
|
|
|
|
text_input_preedit_apply (global);
|
|
|
|
}
|
2018-10-16 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:38:24 +00:00
|
|
|
static void
|
|
|
|
text_input_enter (void *data,
|
|
|
|
struct zwp_text_input_v3 *text_input,
|
|
|
|
struct wl_surface *surface)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
|
2018-10-16 16:38:24 +00:00
|
|
|
global->focused = TRUE;
|
|
|
|
|
|
|
|
if (global->current)
|
2019-01-22 03:28:14 +00:00
|
|
|
enable (GTK_IM_CONTEXT_WAYLAND (global->current), global);
|
2018-10-16 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
text_input_leave (void *data,
|
|
|
|
struct zwp_text_input_v3 *text_input,
|
|
|
|
struct wl_surface *surface)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
|
2018-10-16 16:38:24 +00:00
|
|
|
global->focused = FALSE;
|
|
|
|
|
|
|
|
if (global->current)
|
2019-01-22 03:28:14 +00:00
|
|
|
disable (GTK_IM_CONTEXT_WAYLAND (global->current), global);
|
2018-10-16 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const struct zwp_text_input_v3_listener text_input_listener = {
|
|
|
|
text_input_enter,
|
|
|
|
text_input_leave,
|
|
|
|
text_input_preedit,
|
|
|
|
text_input_commit,
|
|
|
|
text_input_delete_surrounding_text,
|
|
|
|
text_input_done,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
registry_handle_global (void *data,
|
|
|
|
struct wl_registry *registry,
|
|
|
|
uint32_t id,
|
|
|
|
const char *interface,
|
|
|
|
uint32_t version)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
GdkSeat *seat = gdk_display_get_default_seat (gdk_display_get_default ());
|
|
|
|
|
|
|
|
if (strcmp (interface, "zwp_text_input_manager_v3") == 0)
|
|
|
|
{
|
|
|
|
global->text_input_manager_wl_id = id;
|
|
|
|
global->text_input_manager =
|
|
|
|
wl_registry_bind (global->registry, global->text_input_manager_wl_id,
|
|
|
|
&zwp_text_input_manager_v3_interface, 1);
|
|
|
|
global->text_input =
|
|
|
|
zwp_text_input_manager_v3_get_text_input (global->text_input_manager,
|
2018-10-17 12:51:07 +00:00
|
|
|
gdk_wayland_seat_get_wl_seat (seat));
|
2018-10-16 16:38:24 +00:00
|
|
|
global->serial = 0;
|
|
|
|
zwp_text_input_v3_add_listener (global->text_input,
|
|
|
|
&text_input_listener, global);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
registry_handle_global_remove (void *data,
|
|
|
|
struct wl_registry *registry,
|
|
|
|
uint32_t id)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
|
|
|
|
if (id != global->text_input_manager_wl_id)
|
|
|
|
return;
|
|
|
|
|
2018-10-17 12:51:07 +00:00
|
|
|
g_clear_pointer (&global->text_input, zwp_text_input_v3_destroy);
|
|
|
|
g_clear_pointer (&global->text_input_manager, zwp_text_input_manager_v3_destroy);
|
2018-10-16 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_registry_listener registry_listener = {
|
|
|
|
registry_handle_global,
|
|
|
|
registry_handle_global_remove
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2019-01-22 03:28:14 +00:00
|
|
|
gtk_im_context_wayland_global_free (gpointer data)
|
|
|
|
{
|
|
|
|
GtkIMContextWaylandGlobal *global = data;
|
|
|
|
|
|
|
|
g_free (global);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkIMContextWaylandGlobal *
|
|
|
|
gtk_im_context_wayland_global_get (GdkDisplay *display)
|
2018-10-16 16:38:24 +00:00
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWaylandGlobal *global;
|
|
|
|
|
|
|
|
global = g_object_get_data (G_OBJECT (display), "gtk-im-context-wayland-global");
|
2018-10-16 16:38:24 +00:00
|
|
|
if (global != NULL)
|
2019-01-22 03:28:14 +00:00
|
|
|
return global;
|
2018-10-16 16:38:24 +00:00
|
|
|
|
|
|
|
global = g_new0 (GtkIMContextWaylandGlobal, 1);
|
|
|
|
global->display = gdk_wayland_display_get_wl_display (display);
|
|
|
|
global->registry = wl_display_get_registry (global->display);
|
|
|
|
|
|
|
|
wl_registry_add_listener (global->registry, ®istry_listener, global);
|
2019-01-22 03:28:14 +00:00
|
|
|
|
|
|
|
g_object_set_data_full (G_OBJECT (display),
|
|
|
|
"gtk-im-context-wayland-global",
|
|
|
|
global,
|
|
|
|
gtk_im_context_wayland_global_free);
|
|
|
|
|
|
|
|
return global;
|
2018-10-16 16:38:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:29:08 +00:00
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_focus_in (GtkIMContext *context)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWayland *self = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
GtkIMContextWaylandGlobal *global;
|
2018-10-16 16:29:08 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
if (self->widget == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
global = gtk_im_context_wayland_global_get (gtk_widget_get_display (self->widget));
|
2018-10-16 16:29:08 +00:00
|
|
|
if (global->current == context)
|
|
|
|
return;
|
2022-01-18 13:22:24 +00:00
|
|
|
|
|
|
|
global->current = context;
|
2018-10-16 16:29:08 +00:00
|
|
|
if (!global->text_input)
|
|
|
|
return;
|
|
|
|
|
2019-02-08 22:11:48 +00:00
|
|
|
if (self->gesture)
|
|
|
|
gtk_event_controller_reset (GTK_EVENT_CONTROLLER (self->gesture));
|
2018-10-16 16:29:08 +00:00
|
|
|
|
|
|
|
if (global->focused)
|
2019-01-22 03:28:14 +00:00
|
|
|
enable (self, global);
|
2018-10-16 16:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_focus_out (GtkIMContext *context)
|
|
|
|
{
|
2019-01-22 03:28:14 +00:00
|
|
|
GtkIMContextWayland *self = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
GtkIMContextWaylandGlobal *global;
|
2018-10-16 16:29:08 +00:00
|
|
|
|
2019-01-22 03:28:14 +00:00
|
|
|
global = gtk_im_context_wayland_get_global (self);
|
|
|
|
if (global == NULL)
|
2018-10-16 16:29:08 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (global->focused)
|
2019-01-22 03:28:14 +00:00
|
|
|
disable (self, global);
|
2018-08-23 10:32:11 +00:00
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
global->current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_reset (GtkIMContext *context)
|
|
|
|
{
|
2018-03-29 17:49:10 +00:00
|
|
|
notify_external_change (GTK_IM_CONTEXT_WAYLAND (context));
|
2017-11-30 17:53:26 +00:00
|
|
|
|
2018-02-19 23:29:00 +00:00
|
|
|
GTK_IM_CONTEXT_CLASS (gtk_im_context_wayland_parent_class)->reset (context);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_set_cursor_location (GtkIMContext *context,
|
|
|
|
GdkRectangle *rect)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland;
|
2019-02-08 22:11:48 +00:00
|
|
|
int side;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
|
2019-02-08 22:11:48 +00:00
|
|
|
if (context_wayland->cursor_rect.x == rect->x &&
|
|
|
|
context_wayland->cursor_rect.y == rect->y &&
|
|
|
|
context_wayland->cursor_rect.width == rect->width &&
|
|
|
|
context_wayland->cursor_rect.height == rect->height)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Reset the gesture if the cursor changes too far (eg. clicking
|
|
|
|
* between disjoint positions in the text).
|
|
|
|
*
|
|
|
|
* Still Allow some jittering (a square almost double the cursor rect height
|
|
|
|
* on either side) as clicking on the exact same position between characters
|
|
|
|
* is hard.
|
|
|
|
*/
|
|
|
|
side = context_wayland->cursor_rect.height;
|
|
|
|
|
|
|
|
if (context_wayland->gesture &&
|
|
|
|
(ABS (rect->x - context_wayland->cursor_rect.x) >= side ||
|
|
|
|
ABS (rect->y - context_wayland->cursor_rect.y) >= side))
|
|
|
|
gtk_event_controller_reset (GTK_EVENT_CONTROLLER (context_wayland->gesture));
|
|
|
|
|
2017-11-30 17:53:26 +00:00
|
|
|
context_wayland->cursor_rect = *rect;
|
|
|
|
notify_cursor_location (context_wayland);
|
|
|
|
commit_state (context_wayland);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_set_use_preedit (GtkIMContext *context,
|
|
|
|
gboolean use_preedit)
|
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
|
|
|
|
context_wayland->use_preedit = !!use_preedit;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_set_surrounding (GtkIMContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *text,
|
2020-07-24 13:54:49 +00:00
|
|
|
int len,
|
2021-03-04 18:30:55 +00:00
|
|
|
int cursor_index,
|
|
|
|
int selection_bound)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland;
|
|
|
|
|
|
|
|
context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
|
|
|
|
g_free (context_wayland->surrounding.text);
|
2020-03-16 15:53:18 +00:00
|
|
|
context_wayland->surrounding.text = g_strndup (text, len);
|
2017-11-30 17:53:26 +00:00
|
|
|
context_wayland->surrounding.cursor_idx = cursor_index;
|
2021-03-04 18:30:55 +00:00
|
|
|
context_wayland->surrounding.anchor_idx = selection_bound;
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
notify_surrounding_text (context_wayland);
|
2018-03-29 17:49:10 +00:00
|
|
|
/* State changes coming from reset don't have any other opportunity to get
|
|
|
|
* committed. */
|
|
|
|
if (context_wayland->surrounding_change !=
|
|
|
|
ZWP_TEXT_INPUT_V3_CHANGE_CAUSE_INPUT_METHOD)
|
|
|
|
commit_state (context_wayland);
|
2017-11-30 17:53:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gtk_im_context_wayland_get_surrounding (GtkIMContext *context,
|
2020-07-24 18:40:36 +00:00
|
|
|
char **text,
|
2021-03-04 18:30:55 +00:00
|
|
|
int *cursor_index,
|
|
|
|
int *selection_bound)
|
2017-11-30 17:53:26 +00:00
|
|
|
{
|
|
|
|
GtkIMContextWayland *context_wayland;
|
|
|
|
|
|
|
|
context_wayland = GTK_IM_CONTEXT_WAYLAND (context);
|
|
|
|
|
|
|
|
if (!context_wayland->surrounding.text)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
*text = context_wayland->surrounding.text;
|
|
|
|
*cursor_index = context_wayland->surrounding.cursor_idx;
|
2021-03-04 18:30:55 +00:00
|
|
|
*selection_bound = context_wayland->surrounding.anchor_idx;
|
2017-11-30 17:53:26 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_class_init (GtkIMContextWaylandClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkIMContextClass *im_context_class = GTK_IM_CONTEXT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_im_context_wayland_finalize;
|
|
|
|
|
|
|
|
im_context_class->set_client_widget = gtk_im_context_wayland_set_client_widget;
|
|
|
|
im_context_class->get_preedit_string = gtk_im_context_wayland_get_preedit_string;
|
|
|
|
im_context_class->filter_keypress = gtk_im_context_wayland_filter_keypress;
|
|
|
|
im_context_class->focus_in = gtk_im_context_wayland_focus_in;
|
|
|
|
im_context_class->focus_out = gtk_im_context_wayland_focus_out;
|
|
|
|
im_context_class->reset = gtk_im_context_wayland_reset;
|
|
|
|
im_context_class->set_cursor_location = gtk_im_context_wayland_set_cursor_location;
|
|
|
|
im_context_class->set_use_preedit = gtk_im_context_wayland_set_use_preedit;
|
2021-03-04 18:30:55 +00:00
|
|
|
im_context_class->set_surrounding_with_selection = gtk_im_context_wayland_set_surrounding;
|
|
|
|
im_context_class->get_surrounding_with_selection = gtk_im_context_wayland_get_surrounding;
|
2018-02-19 23:29:00 +00:00
|
|
|
}
|
2017-11-30 17:53:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
on_content_type_changed (GtkIMContextWayland *context)
|
|
|
|
{
|
|
|
|
notify_content_type (context);
|
|
|
|
commit_state (context);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_im_context_wayland_init (GtkIMContextWayland *context)
|
|
|
|
{
|
|
|
|
context->use_preedit = TRUE;
|
|
|
|
g_signal_connect_swapped (context, "notify::input-purpose",
|
|
|
|
G_CALLBACK (on_content_type_changed), context);
|
|
|
|
g_signal_connect_swapped (context, "notify::input-hints",
|
|
|
|
G_CALLBACK (on_content_type_changed), context);
|
|
|
|
}
|