Skip to content

Commit

Permalink
Clang-tidy and use addon factory v2
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed Jan 18, 2025
1 parent e94a3ca commit eeed2b6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)
project(fcitx5-libthai VERSION 5.1.4)

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

find_package(PkgConfig REQUIRED)
find_package(Fcitx5Core 5.0.2 REQUIRED)
find_package(Fcitx5Core ${REQUIRED_FCITX_VERSION} REQUIRED)
find_package(Iconv REQUIRED)
find_package(Gettext REQUIRED)

Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

add_library(iconvwrapper STATIC iconvwrapper.cpp)
add_library(iconvwrapper OBJECT iconvwrapper.cpp)
set_target_properties(iconvwrapper PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_link_libraries(iconvwrapper Fcitx5::Utils Iconv::Iconv)

set(LIBTHAI_SOURCES
engine.cpp
thaikb.cpp
)
add_library(libthai MODULE ${LIBTHAI_SOURCES})
add_fcitx5_addon(libthai ${LIBTHAI_SOURCES})
target_link_libraries(libthai iconvwrapper Fcitx5::Core ${THAI_TARGET} Iconv::Iconv)
target_include_directories(libthai PRIVATE ${PROJECT_BINARY_DIR})
set_target_properties(libthai PROPERTIES PREFIX "")
Expand Down
51 changes: 37 additions & 14 deletions src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,38 @@
*
*/
#include "engine.h"
#include "thaikb.h"
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <deque>
#include <fcitx-utils/capabilityflags.h>
#include <fcitx-utils/key.h>
#include <fcitx-utils/keysym.h>
#include <fcitx-utils/keysymgen.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/addoninstance.h>
#include <fcitx/event.h>
#include <fcitx/inputcontext.h>
#include <fcitx/inputcontextmanager.h>
#include <fcitx/inputmethodentry.h>
#include <stdexcept>
#include <string>
#include <string_view>
#include <thai/thailib.h>
#include <thai/thcell.h>
#include <thai/thinp.h>
#include <vector>

namespace fcitx {
namespace {

FCITX_DEFINE_LOG_CATEGORY(libthai_log, "libthai");

}

namespace fcitx {

#define LIBTHAI_DEBUG() FCITX_LOGC(libthai_log, Debug)

constexpr auto FALLBACK_BUFF_SIZE = 4;
Expand Down Expand Up @@ -99,11 +119,13 @@ LibThaiEngine::LibThaiEngine(Instance *instance)

LibThaiEngine::~LibThaiEngine() {}

void LibThaiEngine::activate(const InputMethodEntry &, InputContextEvent &) {}
void LibThaiEngine::activate(const InputMethodEntry & /*entry*/,
InputContextEvent & /*event*/) {}

void LibThaiEngine::deactivate(const InputMethodEntry &, InputContextEvent &) {}
void LibThaiEngine::deactivate(const InputMethodEntry & /*entry*/,
InputContextEvent & /*event*/) {}

bool isContextIntactKey(Key key) {
static bool isContextIntactKey(Key key) {

return (((key.sym() & 0xFF00) == 0xFF00) &&
(/* IsModifierKey */
Expand All @@ -114,7 +136,7 @@ bool isContextIntactKey(Key key) {
key.sym() <= FcitxKey_ISO_Last_Group_Lock));
}

bool isContextLostKey(Key key) {
static bool isContextLostKey(Key key) {

return ((key.sym() & 0xFF00) == 0xFF00) &&
(key.sym() == FcitxKey_BackSpace || key.sym() == FcitxKey_Tab ||
Expand All @@ -134,12 +156,13 @@ bool isContextLostKey(Key key) {
(FcitxKey_F1 <= key.sym() && key.sym() <= FcitxKey_F35));
}

void LibThaiEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
void LibThaiEngine::keyEvent(const InputMethodEntry & /*entry*/,
KeyEvent &keyEvent) {
auto key = keyEvent.rawKey();
if (keyEvent.isRelease()) {
return;
}
auto state = keyEvent.inputContext()->propertyFor(&factory_);
auto *state = keyEvent.inputContext()->propertyFor(&factory_);
// If any ctrl alt super modifier is pressed, ignore.
if (key.states().testAny(KeyStates{KeyState::Ctrl_Alt, KeyState::Super}) ||
isContextLostKey(key)) {
Expand Down Expand Up @@ -187,7 +210,7 @@ void LibThaiEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
prevChar = prevChars.back();
}
if (!th_isaccept(prevChar, newChar, *config_.strictness)) {
return keyEvent.filterAndAccept();
keyEvent.filterAndAccept(); return;
}
if (state->commitString(&newChar, 1)) {
keyEvent.filterAndAccept();
Expand All @@ -200,14 +223,14 @@ void LibThaiEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
state->prevCell(&contextCell);
if (!th_validate_leveled(contextCell, newChar, &conv,
*config_.strictness)) {
return keyEvent.filterAndAccept();
keyEvent.filterAndAccept(); return;
}

if (conv.offset < 0) {
// SurroundingText not supported, so just reject the key.
if (!keyEvent.inputContext()->capabilityFlags().test(
CapabilityFlag::SurroundingText)) {
return keyEvent.filter();
keyEvent.filter(); return;
}

keyEvent.inputContext()->deleteSurroundingText(conv.offset,
Expand All @@ -217,15 +240,15 @@ void LibThaiEngine::keyEvent(const InputMethodEntry &, KeyEvent &keyEvent) {
state->rememberPrevChars(newChar);
if (state->commitString(conv.conv,
strlen(reinterpret_cast<char *>(conv.conv)))) {
return keyEvent.filterAndAccept();
keyEvent.filterAndAccept(); return;
}
}

void LibThaiEngine::reset(const InputMethodEntry &, InputContextEvent &event) {
auto state = event.inputContext()->propertyFor(&factory_);
void LibThaiEngine::reset(const InputMethodEntry & /*entry*/, InputContextEvent &event) {
auto *state = event.inputContext()->propertyFor(&factory_);
state->forgetPrevChars();
}

} // namespace fcitx

FCITX_ADDON_FACTORY(fcitx::LibThaiFactory);
FCITX_ADDON_FACTORY_V2(libthai, fcitx::LibThaiFactory);
8 changes: 7 additions & 1 deletion src/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@

#include "iconvwrapper.h"
#include "thaikb.h"
#include <fcitx-config/configuration.h>
#include <fcitx-config/enum.h>
#include <fcitx-config/iniparser.h>
#include <fcitx-config/option.h>
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/i18n.h>
#include <fcitx/addonfactory.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <fcitx/event.h>
#include <fcitx/inputcontextproperty.h>
#include <fcitx/inputmethodengine.h>
#include <fcitx/instance.h>
Expand Down Expand Up @@ -47,7 +53,7 @@ class LibThaiEngine final : public InputMethodEngine {
void keyEvent(const InputMethodEntry &entry, KeyEvent &keyEvent) override;
void reset(const InputMethodEntry &entry,
InputContextEvent &event) override;
void deactivate(const fcitx::InputMethodEntry &,
void deactivate(const fcitx::InputMethodEntry & /*entry*/,
fcitx::InputContextEvent &event) override;
const fcitx::Configuration *getConfig() const override { return &config_; }
void setConfig(const fcitx::RawConfig &raw) override {
Expand Down
8 changes: 7 additions & 1 deletion src/iconvwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
*/

#include "iconvwrapper.h"
#include <cstddef>
#include <cstdint>
#include <fcitx-utils/utf8.h>
#include <iconv.h>
#include <iterator>
#include <memory>
#include <string_view>
#include <vector>

class IconvWrapperPrivate {
public:
Expand All @@ -30,7 +36,7 @@ IconvWrapper::operator bool() const {
}

std::vector<uint8_t> IconvWrapper::tryConvert(std::string_view s) const {
auto conv = d_ptr->conv_;
iconv_t conv = d_ptr->conv_;
for (auto iter = std::begin(s), e = std::end(s); iter != e;
iter = fcitx::utf8::nextChar(iter)) {
std::vector<uint8_t> result;
Expand Down
3 changes: 3 additions & 0 deletions src/libthai-addon.conf.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ Library=libthai
Type=SharedLibrary
OnDemand=True
Configurable=True

[Addon/Dependencies]
0=core:@REQUIRED_FCITX_VERSION@

0 comments on commit eeed2b6

Please sign in to comment.