Skip to content

Commit

Permalink
feat: implement RimeKeyEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Nov 10, 2024
1 parent 1ed4c7b commit 844a78a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/src/main/java/com/osfans/trime/core/RimeKeyEvent.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 2 additions & 0 deletions app/src/main/jni/librime_jni/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
find_package(Opencc REQUIRED)

set(RIME_JNI_SOURCES
key.cc
key_table.cc
levers.cc
opencc.cc
Expand All @@ -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}"
)
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/jni/librime_jni/jni-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -268,6 +271,11 @@ class GlobalRefSingleton {
env->NewGlobalRef(env->FindClass("com/osfans/trime/core/SchemaItem")));
SchemaListItemInit = env->GetMethodID(
SchemaListItem, "<init>", "(Ljava/lang/String;Ljava/lang/String;)V");

KeyEvent = reinterpret_cast<jclass>(env->NewGlobalRef(
env->FindClass("com/osfans/trime/core/RimeKeyEvent")));
KeyEventInit =
env->GetMethodID(KeyEvent, "<init>", "(IILjava/lang/String;)V");
}

const JEnv AttachEnv() const { return JEnv(jvm); }
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/jni/librime_jni/key.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2015 - 2024 Rime community
// SPDX-License-Identifier: GPL-3.0-or-later

#include <rime/key_event.h>

#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()));
}

0 comments on commit 844a78a

Please sign in to comment.