File: WebKitInputMethodContextImplWPE.cpp

package info (click to toggle)
wpewebkit 2.48.3-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 421,720 kB
  • sloc: cpp: 3,670,389; javascript: 194,411; ansic: 165,592; python: 46,476; asm: 19,276; ruby: 18,528; perl: 16,602; xml: 4,650; yacc: 2,360; java: 1,993; sh: 1,948; lex: 1,327; pascal: 366; makefile: 85
file content (209 lines) | stat: -rw-r--r-- 9,219 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (C) 2024 Igalia S.L.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "WebKitInputMethodContextImplWPE.h"

#if ENABLE(WPE_PLATFORM)

#include <wpe/wpe-platform.h>
#include <wtf/MathExtras.h>
#include <wtf/glib/GRefPtr.h>
#include <wtf/glib/WTFGType.h>

struct _WebKitInputMethodContextImplWPEPrivate {
    GRefPtr<WPEInputMethodContext> context;
};

WEBKIT_DEFINE_FINAL_TYPE(WebKitInputMethodContextImplWPE, webkit_input_method_context_impl_wpe, WEBKIT_TYPE_INPUT_METHOD_CONTEXT, WebKitInputMethodContext)

static WPEInputPurpose toWPEInputPurpose(WebKitInputPurpose purpose)
{
    switch (purpose) {
    case WEBKIT_INPUT_PURPOSE_FREE_FORM:
        return WPE_INPUT_PURPOSE_FREE_FORM;
    case WEBKIT_INPUT_PURPOSE_DIGITS:
        return WPE_INPUT_PURPOSE_DIGITS;
    case WEBKIT_INPUT_PURPOSE_NUMBER:
        return WPE_INPUT_PURPOSE_NUMBER;
    case WEBKIT_INPUT_PURPOSE_PHONE:
        return WPE_INPUT_PURPOSE_PHONE;
    case WEBKIT_INPUT_PURPOSE_URL:
        return WPE_INPUT_PURPOSE_URL;
    case WEBKIT_INPUT_PURPOSE_EMAIL:
        return WPE_INPUT_PURPOSE_EMAIL;
    case WEBKIT_INPUT_PURPOSE_PASSWORD:
        return WPE_INPUT_PURPOSE_PASSWORD;
    }

    RELEASE_ASSERT_NOT_REACHED();
}

static WPEInputHints toWPEInputHints(WebKitInputHints hints)
{
    unsigned wpeHints = 0;
    if (hints & WEBKIT_INPUT_HINT_SPELLCHECK)
        wpeHints |= WPE_INPUT_HINT_SPELLCHECK;
    if (hints & WEBKIT_INPUT_HINT_LOWERCASE)
        wpeHints |= WPE_INPUT_HINT_LOWERCASE;
    if (hints & WEBKIT_INPUT_HINT_UPPERCASE_CHARS)
        wpeHints |= WPE_INPUT_HINT_UPPERCASE_CHARS;
    if (hints & WEBKIT_INPUT_HINT_UPPERCASE_WORDS)
        wpeHints |= WPE_INPUT_HINT_UPPERCASE_WORDS;
    if (hints & WEBKIT_INPUT_HINT_UPPERCASE_SENTENCES)
        wpeHints |= WPE_INPUT_HINT_UPPERCASE_SENTENCES;
    if (hints & WEBKIT_INPUT_HINT_INHIBIT_OSK)
        wpeHints |= WPE_INPUT_HINT_INHIBIT_OSK;
    return static_cast<WPEInputHints>(wpeHints);
}

static void inputPurposeChangedCallback(WebKitInputMethodContextImplWPE* context)
{
    g_object_set(context->priv->context.get(), "input-purpose", toWPEInputPurpose(webkit_input_method_context_get_input_purpose(WEBKIT_INPUT_METHOD_CONTEXT(context))), nullptr);
}

static void inputHintsChangedCallback(WebKitInputMethodContextImplWPE* context)
{
    g_object_set(context->priv->context.get(), "input-hints", toWPEInputHints(webkit_input_method_context_get_input_hints(WEBKIT_INPUT_METHOD_CONTEXT(context))), nullptr);
}

static void wpeIMContextPreeditStartCallback(WebKitInputMethodContextImplWPE* context)
{
    g_signal_emit_by_name(context, "preedit-started", nullptr);
}

static void wpeIMContextPreeditChangedCallback(WebKitInputMethodContextImplWPE* context)
{
    g_signal_emit_by_name(context, "preedit-changed", nullptr);
}

static void wpeIMContextPreeditEndCallback(WebKitInputMethodContextImplWPE* context)
{
    g_signal_emit_by_name(context, "preedit-finished", nullptr);
}

static void wpeIMContextCommitCallback(WebKitInputMethodContextImplWPE* context, const char* text)
{
    g_signal_emit_by_name(context, "committed", text, nullptr);
}

static void wpeIMContextDeleteSurrounding(WebKitInputMethodContextImplWPE* context, gint offset, guint characterCount)
{
    g_signal_emit_by_name(context, "delete-surrounding", offset, characterCount, nullptr);
}

static void webkitInputMethodContextImplWPEGetPreedit(WebKitInputMethodContext* context, char** text, GList** underlines, guint* cursorOffset)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    GList* wpeUnderlines = nullptr;
    guint offset;
    wpe_input_method_context_get_preedit_string(priv->context.get(), text, underlines ? &wpeUnderlines : nullptr, &offset);

    if (underlines) {
        *underlines = nullptr;

        if (wpeUnderlines) {
            for (auto* it = wpeUnderlines; it; it = g_list_next(it)) {
                auto* wpeUnderline = static_cast<WPEInputMethodUnderline*>(it->data);
                auto* wpeColor = wpe_input_method_underline_get_color(wpeUnderline);
                auto* underline = webkit_input_method_underline_new(
                    clampTo<unsigned>(wpe_input_method_underline_get_start_offset(wpeUnderline)),
                    clampTo<unsigned>(wpe_input_method_underline_get_end_offset(wpeUnderline)));
                WebKitColor color = { wpeColor->red, wpeColor->green, wpeColor->blue, wpeColor->alpha };
                webkit_input_method_underline_set_color(underline, &color);

                *underlines = g_list_prepend(*underlines, underline);
            }
            g_list_free_full(wpeUnderlines, reinterpret_cast<GDestroyNotify>(wpe_input_method_underline_free));
        }
    }

    if (cursorOffset)
        *cursorOffset = clampTo<unsigned>(offset);
}

static gboolean webkitInputMethodContextImplWPEFilterKeyEvent(WebKitInputMethodContext* context, void* keyEvent)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    auto* wpeEvent = static_cast<WPEEvent*>(keyEvent);
    return wpe_input_method_context_filter_key_event(priv->context.get(), wpeEvent);
}

static void webkitInputMethodContextImplWPENotifyFocusIn(WebKitInputMethodContext* context)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    wpe_input_method_context_focus_in(priv->context.get());
}

static void webkitInputMethodContextImplWPENotifyFocusOut(WebKitInputMethodContext* context)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    wpe_input_method_context_focus_out(priv->context.get());
}

static void webkitInputMethodContextImplWPENotifyCursorArea(WebKitInputMethodContext* context, int x, int y, int width, int height)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    wpe_input_method_context_set_cursor_area(priv->context.get(), x, y, width, height);
}

static void webkitInputMethodContextImplWPENotifySurrounding(WebKitInputMethodContext* context, const gchar* text, unsigned length, unsigned cursorIndex, unsigned selectionIndex)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    wpe_input_method_context_set_surrounding(priv->context.get(), text, length, cursorIndex, selectionIndex);
}

static void webkitInputMethodContextImplWPEReset(WebKitInputMethodContext* context)
{
    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    wpe_input_method_context_reset(priv->context.get());
}

static void webkit_input_method_context_impl_wpe_class_init(WebKitInputMethodContextImplWPEClass* klass)
{
    auto* imClass = WEBKIT_INPUT_METHOD_CONTEXT_CLASS(klass);
    imClass->get_preedit = webkitInputMethodContextImplWPEGetPreedit;
    imClass->filter_key_event = webkitInputMethodContextImplWPEFilterKeyEvent;
    imClass->notify_focus_in = webkitInputMethodContextImplWPENotifyFocusIn;
    imClass->notify_focus_out = webkitInputMethodContextImplWPENotifyFocusOut;
    imClass->notify_cursor_area = webkitInputMethodContextImplWPENotifyCursorArea;
    imClass->notify_surrounding = webkitInputMethodContextImplWPENotifySurrounding;
    imClass->reset = webkitInputMethodContextImplWPEReset;
}

WebKitInputMethodContext* webkitInputMethodContextImplWPENew(WPEView *view)
{
    auto* context = WEBKIT_INPUT_METHOD_CONTEXT(g_object_new(WEBKIT_TYPE_INPUT_METHOD_CONTEXT_IMPL_WPE, nullptr));

    g_signal_connect_swapped(context, "notify::input-purpose", G_CALLBACK(inputPurposeChangedCallback), context);
    g_signal_connect_swapped(context, "notify::input-hints", G_CALLBACK(inputHintsChangedCallback), context);

    auto* priv = WEBKIT_INPUT_METHOD_CONTEXT_IMPL_WPE(context)->priv;
    priv->context = adoptGRef(wpe_input_method_context_new(view) );

    g_signal_connect_object(priv->context.get(), "preedit-started", G_CALLBACK(wpeIMContextPreeditStartCallback), context, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->context.get(), "preedit-changed", G_CALLBACK(wpeIMContextPreeditChangedCallback), context, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->context.get(), "preedit-finished", G_CALLBACK(wpeIMContextPreeditEndCallback), context, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->context.get(), "committed", G_CALLBACK(wpeIMContextCommitCallback), context, G_CONNECT_SWAPPED);
    g_signal_connect_object(priv->context.get(), "delete-surrounding", G_CALLBACK(wpeIMContextDeleteSurrounding), context, G_CONNECT_SWAPPED);

    return context;
}

#endif // ENABLE(WPE_PLATFORM)