summaryrefslogtreecommitdiffstats
path: root/src/common/android/applets/software_keyboard.cpp
blob: 477e62b169a1c7b75947c450d58434cf23d833e7 (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <map>
#include <thread>

#include <jni.h>

#include "common/android/android_common.h"
#include "common/android/applets/software_keyboard.h"
#include "common/android/id_cache.h"
#include "common/logging/log.h"
#include "common/string_util.h"
#include "core/core.h"

static jclass s_software_keyboard_class;
static jclass s_keyboard_config_class;
static jclass s_keyboard_data_class;
static jmethodID s_swkbd_execute_normal;
static jmethodID s_swkbd_execute_inline;

namespace Common::Android::SoftwareKeyboard {

static jobject ToJKeyboardParams(const Core::Frontend::KeyboardInitializeParameters& config) {
    JNIEnv* env = GetEnvForThread();
    jobject object = env->AllocObject(s_keyboard_config_class);

    env->SetObjectField(object,
                        env->GetFieldID(s_keyboard_config_class, "ok_text", "Ljava/lang/String;"),
                        ToJString(env, config.ok_text));
    env->SetObjectField(
        object, env->GetFieldID(s_keyboard_config_class, "header_text", "Ljava/lang/String;"),
        ToJString(env, config.header_text));
    env->SetObjectField(object,
                        env->GetFieldID(s_keyboard_config_class, "sub_text", "Ljava/lang/String;"),
                        ToJString(env, config.sub_text));
    env->SetObjectField(
        object, env->GetFieldID(s_keyboard_config_class, "guide_text", "Ljava/lang/String;"),
        ToJString(env, config.guide_text));
    env->SetObjectField(
        object, env->GetFieldID(s_keyboard_config_class, "initial_text", "Ljava/lang/String;"),
        ToJString(env, config.initial_text));
    env->SetShortField(object,
                       env->GetFieldID(s_keyboard_config_class, "left_optional_symbol_key", "S"),
                       static_cast<jshort>(config.left_optional_symbol_key));
    env->SetShortField(object,
                       env->GetFieldID(s_keyboard_config_class, "right_optional_symbol_key", "S"),
                       static_cast<jshort>(config.right_optional_symbol_key));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "max_text_length", "I"),
                     static_cast<jint>(config.max_text_length));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "min_text_length", "I"),
                     static_cast<jint>(config.min_text_length));
    env->SetIntField(object,
                     env->GetFieldID(s_keyboard_config_class, "initial_cursor_position", "I"),
                     static_cast<jint>(config.initial_cursor_position));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "type", "I"),
                     static_cast<jint>(config.type));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "password_mode", "I"),
                     static_cast<jint>(config.password_mode));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "text_draw_type", "I"),
                     static_cast<jint>(config.text_draw_type));
    env->SetIntField(object, env->GetFieldID(s_keyboard_config_class, "key_disable_flags", "I"),
                     static_cast<jint>(config.key_disable_flags.raw));
    env->SetBooleanField(object,
                         env->GetFieldID(s_keyboard_config_class, "use_blur_background", "Z"),
                         static_cast<jboolean>(config.use_blur_background));
    env->SetBooleanField(object,
                         env->GetFieldID(s_keyboard_config_class, "enable_backspace_button", "Z"),
                         static_cast<jboolean>(config.enable_backspace_button));
    env->SetBooleanField(object,
                         env->GetFieldID(s_keyboard_config_class, "enable_return_button", "Z"),
                         static_cast<jboolean>(config.enable_return_button));
    env->SetBooleanField(object,
                         env->GetFieldID(s_keyboard_config_class, "disable_cancel_button", "Z"),
                         static_cast<jboolean>(config.disable_cancel_button));

    return object;
}

AndroidKeyboard::ResultData AndroidKeyboard::ResultData::CreateFromFrontend(jobject object) {
    JNIEnv* env = GetEnvForThread();
    const jstring string = reinterpret_cast<jstring>(env->GetObjectField(
        object, env->GetFieldID(s_keyboard_data_class, "text", "Ljava/lang/String;")));
    return ResultData{GetJString(env, string),
                      static_cast<Service::AM::Frontend::SwkbdResult>(env->GetIntField(
                          object, env->GetFieldID(s_keyboard_data_class, "result", "I")))};
}

AndroidKeyboard::~AndroidKeyboard() = default;

void AndroidKeyboard::InitializeKeyboard(
    bool is_inline, Core::Frontend::KeyboardInitializeParameters initialize_parameters,
    SubmitNormalCallback submit_normal_callback_, SubmitInlineCallback submit_inline_callback_) {
    if (is_inline) {
        LOG_WARNING(
            Frontend,
            "(STUBBED) called, backend requested to initialize the inline software keyboard.");

        submit_inline_callback = std::move(submit_inline_callback_);
    } else {
        LOG_WARNING(
            Frontend,
            "(STUBBED) called, backend requested to initialize the normal software keyboard.");

        submit_normal_callback = std::move(submit_normal_callback_);
    }

    parameters = std::move(initialize_parameters);

    LOG_INFO(Frontend,
             "\nKeyboardInitializeParameters:"
             "\nok_text={}"
             "\nheader_text={}"
             "\nsub_text={}"
             "\nguide_text={}"
             "\ninitial_text={}"
             "\nmax_text_length={}"
             "\nmin_text_length={}"
             "\ninitial_cursor_position={}"
             "\ntype={}"
             "\npassword_mode={}"
             "\ntext_draw_type={}"
             "\nkey_disable_flags={}"
             "\nuse_blur_background={}"
             "\nenable_backspace_button={}"
             "\nenable_return_button={}"
             "\ndisable_cancel_button={}",
             Common::UTF16ToUTF8(parameters.ok_text), Common::UTF16ToUTF8(parameters.header_text),
             Common::UTF16ToUTF8(parameters.sub_text), Common::UTF16ToUTF8(parameters.guide_text),
             Common::UTF16ToUTF8(parameters.initial_text), parameters.max_text_length,
             parameters.min_text_length, parameters.initial_cursor_position, parameters.type,
             parameters.password_mode, parameters.text_draw_type, parameters.key_disable_flags.raw,
             parameters.use_blur_background, parameters.enable_backspace_button,
             parameters.enable_return_button, parameters.disable_cancel_button);
}

void AndroidKeyboard::ShowNormalKeyboard() const {
    LOG_DEBUG(Frontend, "called, backend requested to show the normal software keyboard.");

    ResultData data{};

    // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber.
    std::thread([&] {
        data = ResultData::CreateFromFrontend(GetEnvForThread()->CallStaticObjectMethod(
            s_software_keyboard_class, s_swkbd_execute_normal, ToJKeyboardParams(parameters)));
    }).join();

    SubmitNormalText(data);
}

void AndroidKeyboard::ShowTextCheckDialog(
    Service::AM::Frontend::SwkbdTextCheckResult text_check_result,
    std::u16string text_check_message) const {
    LOG_WARNING(Frontend, "(STUBBED) called, backend requested to show the text check dialog.");
}

void AndroidKeyboard::ShowInlineKeyboard(
    Core::Frontend::InlineAppearParameters appear_parameters) const {
    LOG_WARNING(Frontend,
                "(STUBBED) called, backend requested to show the inline software keyboard.");

    LOG_INFO(Frontend,
             "\nInlineAppearParameters:"
             "\nmax_text_length={}"
             "\nmin_text_length={}"
             "\nkey_top_scale_x={}"
             "\nkey_top_scale_y={}"
             "\nkey_top_translate_x={}"
             "\nkey_top_translate_y={}"
             "\ntype={}"
             "\nkey_disable_flags={}"
             "\nkey_top_as_floating={}"
             "\nenable_backspace_button={}"
             "\nenable_return_button={}"
             "\ndisable_cancel_button={}",
             appear_parameters.max_text_length, appear_parameters.min_text_length,
             appear_parameters.key_top_scale_x, appear_parameters.key_top_scale_y,
             appear_parameters.key_top_translate_x, appear_parameters.key_top_translate_y,
             appear_parameters.type, appear_parameters.key_disable_flags.raw,
             appear_parameters.key_top_as_floating, appear_parameters.enable_backspace_button,
             appear_parameters.enable_return_button, appear_parameters.disable_cancel_button);

    // Pivot to a new thread, as we cannot call GetEnvForThread() from a Fiber.
    m_is_inline_active = true;
    std::thread([&] {
        GetEnvForThread()->CallStaticVoidMethod(s_software_keyboard_class, s_swkbd_execute_inline,
                                                ToJKeyboardParams(parameters));
    }).join();
}

void AndroidKeyboard::HideInlineKeyboard() const {
    LOG_WARNING(Frontend,
                "(STUBBED) called, backend requested to hide the inline software keyboard.");
}

void AndroidKeyboard::InlineTextChanged(
    Core::Frontend::InlineTextParameters text_parameters) const {
    LOG_WARNING(Frontend,
                "(STUBBED) called, backend requested to change the inline keyboard text.");

    LOG_INFO(Frontend,
             "\nInlineTextParameters:"
             "\ninput_text={}"
             "\ncursor_position={}",
             Common::UTF16ToUTF8(text_parameters.input_text), text_parameters.cursor_position);

    submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString,
                           text_parameters.input_text, text_parameters.cursor_position);
}

void AndroidKeyboard::ExitKeyboard() const {
    LOG_WARNING(Frontend, "(STUBBED) called, backend requested to exit the software keyboard.");
}

void AndroidKeyboard::SubmitInlineKeyboardText(std::u16string submitted_text) {
    if (!m_is_inline_active) {
        return;
    }

    m_current_text += submitted_text;

    submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text,
                           static_cast<int>(m_current_text.size()));
}

void AndroidKeyboard::SubmitInlineKeyboardInput(int key_code) {
    static constexpr int KEYCODE_BACK = 4;
    static constexpr int KEYCODE_ENTER = 66;
    static constexpr int KEYCODE_DEL = 67;

    if (!m_is_inline_active) {
        return;
    }

    switch (key_code) {
    case KEYCODE_BACK:
    case KEYCODE_ENTER:
        m_is_inline_active = false;
        submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::DecidedEnter, m_current_text,
                               static_cast<s32>(m_current_text.size()));
        break;
    case KEYCODE_DEL:
        m_current_text.pop_back();
        submit_inline_callback(Service::AM::Frontend::SwkbdReplyType::ChangedString, m_current_text,
                               static_cast<int>(m_current_text.size()));
        break;
    }
}

void AndroidKeyboard::SubmitNormalText(const ResultData& data) const {
    submit_normal_callback(data.result, Common::UTF8ToUTF16(data.text), true);
}

void InitJNI(JNIEnv* env) {
    s_software_keyboard_class = reinterpret_cast<jclass>(
        env->NewGlobalRef(env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard")));
    s_keyboard_config_class = reinterpret_cast<jclass>(env->NewGlobalRef(
        env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig")));
    s_keyboard_data_class = reinterpret_cast<jclass>(env->NewGlobalRef(
        env->FindClass("org/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardData")));

    s_swkbd_execute_normal = env->GetStaticMethodID(
        s_software_keyboard_class, "executeNormal",
        "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)Lorg/yuzu/yuzu_emu/"
        "applets/keyboard/SoftwareKeyboard$KeyboardData;");
    s_swkbd_execute_inline = env->GetStaticMethodID(
        s_software_keyboard_class, "executeInline",
        "(Lorg/yuzu/yuzu_emu/applets/keyboard/SoftwareKeyboard$KeyboardConfig;)V");
}

void CleanupJNI(JNIEnv* env) {
    env->DeleteGlobalRef(s_software_keyboard_class);
    env->DeleteGlobalRef(s_keyboard_config_class);
    env->DeleteGlobalRef(s_keyboard_data_class);
}

} // namespace Common::Android::SoftwareKeyboard