From 844a78aed516984507b940fa92208b12e554e18f Mon Sep 17 00:00:00 2001 From: WhiredPlanck Date: Sat, 9 Nov 2024 14:01:02 +0800 Subject: [PATCH] feat: implement RimeKeyEvent --- .../com/osfans/trime/core/RimeKeyEvent.kt | 25 +++++++++++++++++++ app/src/main/jni/librime_jni/CMakeLists.txt | 2 ++ app/src/main/jni/librime_jni/jni-utils.h | 8 ++++++ app/src/main/jni/librime_jni/key.cc | 15 +++++++++++ 4 files changed, 50 insertions(+) create mode 100644 app/src/main/java/com/osfans/trime/core/RimeKeyEvent.kt create mode 100644 app/src/main/jni/librime_jni/key.cc diff --git a/app/src/main/java/com/osfans/trime/core/RimeKeyEvent.kt b/app/src/main/java/com/osfans/trime/core/RimeKeyEvent.kt new file mode 100644 index 0000000000..2af8c5ef99 --- /dev/null +++ b/app/src/main/java/com/osfans/trime/core/RimeKeyEvent.kt @@ -0,0 +1,25 @@ +/* + * SPDX-FileCopyrightText: 2015 - 2024 Rime community + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package com.osfans.trime.core + +data class RimeKeyEvent( + val value: Int, + val modifiers: Int, + val repr: String, +) { + val keyVal by lazy { KeyValue(value) } + + val keyModifiers by lazy { KeyModifiers.of(modifiers) } + + override fun toString() = repr + + companion object { + val None = RimeKeyEvent(0, 0, "0x0000") + + @JvmStatic + external fun parse(repr: String): RimeKeyEvent + } +} diff --git a/app/src/main/jni/librime_jni/CMakeLists.txt b/app/src/main/jni/librime_jni/CMakeLists.txt index bb8f93031e..fccc7f9ae7 100644 --- a/app/src/main/jni/librime_jni/CMakeLists.txt +++ b/app/src/main/jni/librime_jni/CMakeLists.txt @@ -5,6 +5,7 @@ find_package(Opencc REQUIRED) set(RIME_JNI_SOURCES + key.cc key_table.cc levers.cc opencc.cc @@ -13,6 +14,7 @@ set(RIME_JNI_SOURCES add_library(rime_jni SHARED ${RIME_JNI_SOURCES}) target_link_libraries(rime_jni rime-static ${Opencc_LIBRARY}) target_include_directories(rime_jni PRIVATE + "${CMAKE_BINARY_DIR}/librime/src" "${CMAKE_SOURCE_DIR}/librime/src" "${Opencc_INCLUDE_PATH}" ) diff --git a/app/src/main/jni/librime_jni/jni-utils.h b/app/src/main/jni/librime_jni/jni-utils.h index e0eb5aed39..2fdb0f5c6d 100644 --- a/app/src/main/jni/librime_jni/jni-utils.h +++ b/app/src/main/jni/librime_jni/jni-utils.h @@ -177,6 +177,9 @@ class GlobalRefSingleton { jclass SchemaListItem; jmethodID SchemaListItemInit; + jclass KeyEvent; + jmethodID KeyEventInit; + GlobalRefSingleton(JavaVM *jvm_) : jvm(jvm_) { JNIEnv *env; jvm->AttachCurrentThread(&env, nullptr); @@ -268,6 +271,11 @@ class GlobalRefSingleton { env->NewGlobalRef(env->FindClass("com/osfans/trime/core/SchemaItem"))); SchemaListItemInit = env->GetMethodID( SchemaListItem, "", "(Ljava/lang/String;Ljava/lang/String;)V"); + + KeyEvent = reinterpret_cast(env->NewGlobalRef( + env->FindClass("com/osfans/trime/core/RimeKeyEvent"))); + KeyEventInit = + env->GetMethodID(KeyEvent, "", "(IILjava/lang/String;)V"); } const JEnv AttachEnv() const { return JEnv(jvm); } diff --git a/app/src/main/jni/librime_jni/key.cc b/app/src/main/jni/librime_jni/key.cc new file mode 100644 index 0000000000..756dd55d34 --- /dev/null +++ b/app/src/main/jni/librime_jni/key.cc @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2015 - 2024 Rime community +// SPDX-License-Identifier: GPL-3.0-or-later + +#include + +#include "jni-utils.h" + +extern "C" JNIEXPORT jobject JNICALL +Java_com_osfans_trime_core_RimeKeyEvent_parse(JNIEnv *env, jclass clazz, + jstring repr) { + rime::KeyEvent ke; + ke.Parse(*CString(env, repr)); + return env->NewObject(GlobalRef->KeyEvent, GlobalRef->KeyEventInit, + ke.keycode(), ke.modifier(), *JString(env, ke.repr())); +}