Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Jun 4, 2022
1 parent 94e5db9 commit 5fd3b57
Show file tree
Hide file tree
Showing 26 changed files with 9,957 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
build*/
.*
!.git*
.git/
*.tar.*
*.kdev4
*.kate-swp
*.orig
tags
astyle.sh
cscope.*
*.part
XF86keysym.h
keysymdef.h
*~
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "bamboo-core/bamboo-core"]
path = bamboo-core/bamboo-core
url = https://github.com/BambooEngine/bamboo-core
[submodule "bamboo/bamboo-core"]
path = bamboo/bamboo-core
url = https://github.com/BambooEngine/bamboo-core
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
cmake_minimum_required(VERSION 3.6)

project(fcitx5-bamboo VERSION 5.0.6)

find_package(ECM 1.0.0 REQUIRED)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
include(FeatureSummary)
include(GNUInstallDirs)
include(ECMUninstallTarget)

find_package(Fcitx5Core REQUIRED)
find_package(Fcitx5Module REQUIRED COMPONENTS TestFrontend)
find_package(Gettext REQUIRED)
find_package(Pthread REQUIRED)

include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake")
add_definitions(-DFCITX_GETTEXT_DOMAIN=\"fcitx5-bamboo\")
fcitx5_add_i18n_definition()

add_subdirectory(po)
add_subdirectory(bamboo)
add_subdirectory(src)
add_subdirectory(data)

fcitx5_translate_desktop_file(org.fcitx.Fcitx5.Addon.Bamboo.metainfo.xml.in
org.fcitx.Fcitx5.Addon.Bamboo.metainfo.xml XML)

install(FILES "${CMAKE_CURRENT_BINARY_DIR}/org.fcitx.Fcitx5.Addon.Bamboo.metainfo.xml" DESTINATION ${CMAKE_INSTALL_DATADIR}/metainfo)

feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES)

468 changes: 468 additions & 0 deletions LICENSES/LGPL-2.1-or-later.txt

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Messages.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

gen_pot cxx:appdata:ui:desktop fcitx5-bamboo po .
19 changes: 19 additions & 0 deletions bamboo/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

file(GLOB BAMBOO_CORE_GO_SRCS bamboo-core/*.go)
file(GLOB BAMBOO_GO_SRCS *.go)

# Custom command for 'go build -buildmode=c-archive ...'
# to create a library from Go codes.
add_custom_command(OUTPUT bamboo-core.a bamboo-core.h
DEPENDS ${BAMBOO_GO_SRCS} ${BAMBOO_CORE_GO_SRCS} go.mod
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND env go build -buildmode=c-archive
-o "${CMAKE_CURRENT_BINARY_DIR}/bamboo-core.a"
${BAMBOO_GO_SRCS})
add_custom_target(bamboo-core DEPENDS bamboo-core.a)
# Add a custom target for the library.
add_library(Bamboo::Core UNKNOWN IMPORTED GLOBAL)
add_dependencies(Bamboo::Core bamboo-core)
set_target_properties(Bamboo::Core PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/bamboo-core.a"
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}")
236 changes: 236 additions & 0 deletions bamboo/bamboo-c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
package main

import (
/*
#include <stdint.h>
#include <stdbool.h>
typedef const char cchar;
typedef struct {
bool autoNonVnRestore;
bool ddFreeStyle;
bool macroEnabled;
bool autoCapitalizeMacro;
bool spellCheckWithDicts;
const char *outputCharset;
} FcitxBambooEngineOption;
*/
"C"
"bamboo-core"
"runtime/cgo"
"unsafe"
"os/signal"
"syscall"
)
import (
"bufio"
"os"
"strings"
)

//export Init
func Init() {
signal.Ignore(syscall.SIGPIPE)
}

//export EngineProcessKeyEvent
func EngineProcessKeyEvent(engine uintptr, keyVal, state uint32) bool {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return false
}
return bambooEngine.preeditProcessKeyEvent(keyVal, state)
}

//export EngineSetRestoreKeyStroke
func EngineSetRestoreKeyStroke(engine uintptr) {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return
}
bambooEngine.shouldRestoreKeyStrokes = true
}


//export EnginePullPreedit
func EnginePullPreedit(engine uintptr) *C.char {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return nil
}
return C.CString(bambooEngine.preeditText)
}

//export EnginePullCommit
func EnginePullCommit(engine uintptr) *C.char {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return nil
}
var commitText = bambooEngine.commitText
bambooEngine.commitText = ""
return C.CString(commitText)
}

//export EngineSetOption
func EngineSetOption(engine uintptr, option *C.FcitxBambooEngineOption) {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return
}
bambooEngine.autoNonVnRestore = bool(option.autoNonVnRestore)
bambooEngine.ddFreeStyle = bool(option.ddFreeStyle)
bambooEngine.macroEnabled = bool(option.macroEnabled)
bambooEngine.autoCapitalizeMacro = bool(option.autoCapitalizeMacro)
bambooEngine.spellCheckWithDicts = bool(option.spellCheckWithDicts)
bambooEngine.outputCharset = C.GoString(option.outputCharset)
}

//export NewEngine
func NewEngine(name *C.cchar, dictHandle uintptr, tableHandle uintptr) uintptr {
dict, ok := cgo.Handle(dictHandle).Value().(*map[string]bool)
if !ok {
return 0
}

table, ok := cgo.Handle(tableHandle).Value().(*MacroTable)
if !ok {
return 0
}

imName := C.GoString(name)

var engine = &FcitxBambooEngine{
preeditor: bamboo.NewEngine(bamboo.ParseInputMethod(bamboo.InputMethodDefinitions, imName), 0),
macroTable: table,
dictionary: *dict,
autoNonVnRestore: true,
ddFreeStyle: true,
macroEnabled: false,
autoCapitalizeMacro: false,
lastKeyWithShift: false,
spellCheckWithDicts: true,
preeditText: "",
commitText: "",
shouldRestoreKeyStrokes: false,
outputCharset: "Unicode",
}
return uintptr(cgo.NewHandle(engine))
}

//export NewCustomEngine
func NewCustomEngine(definition **C.char, dictHandle uintptr, tableHandle uintptr) uintptr {
dict, ok := cgo.Handle(dictHandle).Value().(*map[string]bool)
if !ok {
return 0
}

table, ok := cgo.Handle(tableHandle).Value().(*MacroTable)
if !ok {
return 0
}
var definitions = map[string]bamboo.InputMethodDefinition{}
def := (*[1<<30 - 1]*C.char)(unsafe.Pointer(definition))

i := 0
for def[i] != nil {
definitions["Custom"][C.GoString(def[i])] = C.GoString(def[i+1])
i += 2
}

var engine = &FcitxBambooEngine{
preeditor: bamboo.NewEngine(bamboo.ParseInputMethod(definitions, "Custom"), 0),
macroTable: table,
dictionary: *dict,
autoNonVnRestore: true,
ddFreeStyle: true,
macroEnabled: false,
autoCapitalizeMacro: false,
lastKeyWithShift: false,
spellCheckWithDicts: false,
preeditText: "",
commitText: "",
shouldRestoreKeyStrokes: false,
outputCharset: "Unicode",
}

return uintptr(cgo.NewHandle(engine))
}

//export NewMacroTable
func NewMacroTable(definition **C.char) uintptr {
var table = &MacroTable{
mTable: map[string]string{},
}
def := (*[1<<30 - 1]*C.char)(unsafe.Pointer(definition))
i := 0
for def[i] != nil {
table.mTable[C.GoString(def[i])] = C.GoString(def[i+1])
i += 2
}

return uintptr(cgo.NewHandle(table))
}

//export DeleteObject
func DeleteObject(handle uintptr) {
cgo.Handle(handle).Delete()
}

//export ResetEngine
func ResetEngine(engine uintptr) {
bambooEngine, ok := cgo.Handle(engine).Value().(*FcitxBambooEngine)
if !ok {
return
}
bambooEngine.preeditor.Reset()
}

func toCStringArray(strs []string) **C.char {

array := C.malloc(C.size_t(len(strs)+1) * C.size_t(unsafe.Sizeof(uintptr(0))))
// convert the C array to a Go Array so we can index it
a := (*[1<<30 - 1]*C.char)(array)

for idx, substring := range strs {
a[idx] = C.CString(substring)
}
a[len(strs)] = nil
return (**C.char)(array)
}

//export GetCharsetNames
func GetCharsetNames() **C.char {
return toCStringArray(bamboo.GetCharsetNames())
}

//export GetInputMethodNames
func GetInputMethodNames() **C.char {
names := make([]string, len(bamboo.InputMethodDefinitions))
i := 0
for imName := range bamboo.InputMethodDefinitions {
names[i] = imName
i++
}
return toCStringArray(names)
}

//export NewDictionary
func NewDictionary(fd uintptr) uintptr {
var data = map[string]bool{}
f := os.NewFile(fd, "dict")
rd := bufio.NewReader(f)
for {
line, _, err := rd.ReadLine()
if err != nil {
break
}
if len(line) == 0 {
continue
}
var tmp = []byte(strings.ToLower(string(line)))
data[string(tmp)] = true
}
return uintptr(cgo.NewHandle(&data))
}

func main() {}
1 change: 1 addition & 0 deletions bamboo/bamboo-core
Submodule bamboo-core added at f0ca53
Loading

0 comments on commit 5fd3b57

Please sign in to comment.