Skip to content

Commit

Permalink
Clean up fcitx5-mozc include header
Browse files Browse the repository at this point in the history
  • Loading branch information
wengxt committed May 27, 2024
1 parent 4b49992 commit fe465bb
Show file tree
Hide file tree
Showing 18 changed files with 216 additions and 113 deletions.
14 changes: 11 additions & 3 deletions src/unix/fcitx5/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ mozc_cc_library(
]
)

mozc_cc_library(
name = "i18nwrapper",
hdrs = [
"i18nwrapper.h"
],
deps = [
"@fcitx5//:fcitx5"
]
)

mozc_cc_library(
name = "mozc_engine",
srcs = [
Expand All @@ -43,10 +53,8 @@ mozc_cc_library(
"mozc_response_parser.h",
"mozc_state.h"
],
local_defines = [
'FCITX_GETTEXT_DOMAIN=\\"fcitx5-mozc\\"',
],
deps = [
":i18nwrapper",
":mozc_connection",
":mozc_client_pool",
":fcitx_key_util",
Expand Down
25 changes: 16 additions & 9 deletions src/unix/fcitx5/fcitx_key_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,19 @@

#include "unix/fcitx5/fcitx_key_event_handler.h"

#include <fcitx-utils/key.h>

#include <cstdint>
#include <map>
#include <memory>
#include <set>

#include "base/logging.h"
#include "absl/log/check.h"
#include "absl/log/log.h"
#include "base/singleton.h"
#include "base/vlog.h"
#include "protocol/commands.pb.h"
#include "protocol/config.pb.h"
#include "unix/fcitx5/fcitx_key_translator.h"

namespace fcitx {

Expand Down Expand Up @@ -72,11 +80,9 @@ void AddAdditionalModifiers(
mozc::Singleton<AdditionalModifiersData>::get()->data();

// Adds MODIFIER if there are (LEFT|RIGHT)_MODIFIER like LEFT_SHIFT.
for (std::set<mozc::commands::KeyEvent::ModifierKey>::const_iterator it =
modifier_keys_set->begin();
it != modifier_keys_set->end(); ++it) {
std::map<uint32_t, mozc::commands::KeyEvent::ModifierKey>::const_iterator
item = data.find(*it);
for (auto it = modifier_keys_set->begin(); it != modifier_keys_set->end();
++it) {
auto item = data.find(*it);
if (item != data.end()) {
modifier_keys_set->insert(item->second);
}
Expand All @@ -97,7 +103,8 @@ bool IsModifierToBeSentOnKeyUp(const mozc::commands::KeyEvent &key_event) {
}
} // namespace

KeyEventHandler::KeyEventHandler() : key_translator_(new KeyTranslator) {
KeyEventHandler::KeyEventHandler()
: key_translator_(std::make_unique<KeyTranslator>()) {
Clear();
}

Expand Down Expand Up @@ -225,7 +232,7 @@ bool KeyEventHandler::ProcessModifiers(bool is_key_up, uint32_t keyval,
// implementation does NOT do it.
if (currently_pressed_modifiers_.empty() ||
!modifiers_to_be_sent_.empty()) {
for (size_t i = 0; i < key_event->modifier_keys_size(); ++i) {
for (int i = 0; i < key_event->modifier_keys_size(); ++i) {
modifiers_to_be_sent_.insert(key_event->modifier_keys(i));
}
AddAdditionalModifiers(&modifiers_to_be_sent_);
Expand Down
2 changes: 1 addition & 1 deletion src/unix/fcitx5/fcitx_key_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@

#include <fcitx-utils/key.h>

#include <cstdint>
#include <memory>
#include <set>

#include "base/port.h"
#include "protocol/commands.pb.h"
#include "protocol/config.pb.h"
#include "unix/fcitx5/fcitx_key_translator.h"
Expand Down
38 changes: 25 additions & 13 deletions src/unix/fcitx5/fcitx_key_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@

#include "unix/fcitx5/fcitx_key_translator.h"

#include "base/logging.h"
#include <fcitx-utils/key.h>
#include <fcitx-utils/keysym.h>

#include <cstdint>
#include <map>
#include <string>
#include <utility>

#include "absl/log/check.h"
#include "base/vlog.h"
#include "protocol/commands.pb.h"
#include "protocol/config.pb.h"

namespace fcitx {
using namespace mozc;
namespace {
static const auto kSpecialKeyMap =
const auto kSpecialKeyMap =
new std::map<uint32_t, commands::KeyEvent::SpecialKey>({
{FcitxKey_space, commands::KeyEvent::SPACE},
{FcitxKey_Return, commands::KeyEvent::ENTER},
Expand Down Expand Up @@ -136,7 +146,7 @@ static const auto kSpecialKeyMap =
// - FcitxKey_Kana_Lock? FcitxKey_KEY_Kana_Shift?
});

static const auto kIbusModifierMaskMap = new std::map<uint32_t, KeyState>({
const auto kFcitxModifierMaskMap = new std::map<uint32_t, KeyState>({
{FcitxKey_Shift_L, KeyState::Shift},
{FcitxKey_Shift_R, KeyState::Shift},
{FcitxKey_Control_L, KeyState::Ctrl},
Expand All @@ -149,8 +159,8 @@ static const auto kIbusModifierMaskMap = new std::map<uint32_t, KeyState>({
// '4' is mapped to Japanese 'Hiragana Letter U' (without Shift modifier) and
// 'Hiragana Letter Small U' (with Shift modifier).
// TODO(team): Add kana_map_dv to support Dvoraklayout.
typedef std::map<uint32_t, std::pair<const char *, const char *>> KanaMap;
static const KanaMap *kKanaJpMap = new KanaMap({
using KanaMap = std::map<uint32_t, std::pair<const char *, const char *>>;
const KanaMap *kKanaJpMap = new KanaMap({
{'1', {"", ""}},
{'!', {"", ""}},
{'2', {"", ""}},
Expand Down Expand Up @@ -251,7 +261,7 @@ static const KanaMap *kKanaJpMap = new KanaMap({
{U'¥', {"", ""}}, // U+00A5
});

static const KanaMap *kKanaUsMap = new KanaMap({
const KanaMap *kKanaUsMap = new KanaMap({
{'`', {"", ""}}, {'~', {"", ""}}, {'1', {"", ""}},
{'!', {"", ""}}, {'2', {"", ""}}, {'@', {"", ""}},
{'3', {"", ""}}, {'#', {"", ""}}, {'4', {"", ""}},
Expand Down Expand Up @@ -307,7 +317,9 @@ bool KeyTranslator::Translate(KeySym keyval, uint32_t keycode,
out_event->Clear();

/* this is key we cannot handle, don't process it */
if (modifiers & KeyState::Super) return false;
if (modifiers & KeyState::Super) {
return false;
}

// Due to historical reasons, many linux ditributions set Hiragana_Katakana
// key as Hiragana key (which is Katkana key with shift modifier). So, we
Expand All @@ -329,9 +341,9 @@ bool KeyTranslator::Translate(KeySym keyval, uint32_t keycode,
out_event->add_modifier_keys(mozc::commands::KeyEvent::CAPS);
}
out_event->set_key_code(keyval);
} else if (auto it = kIbusModifierMaskMap->find(keyval);
it != kIbusModifierMaskMap->end()) {
// Convert Ibus modifier key to mask (e.g. FcitxKey_Shift_L to
} else if (auto it = kFcitxModifierMaskMap->find(keyval);
it != kFcitxModifierMaskMap->end()) {
// Convert Fcitx modifier key to mask (e.g. FcitxKey_Shift_L to
// KeyState::Shift)
modifiers |= it->second;
} else if (auto it = kSpecialKeyMap->find(keyval);
Expand Down Expand Up @@ -359,7 +371,7 @@ bool KeyTranslator::Translate(KeySym keyval, uint32_t keycode,
}

bool KeyTranslator::IsHiraganaKatakanaKeyWithShift(KeySym keyval,
uint32_t keycode,
uint32_t /*keycode*/,
KeyStates modifiers) {
return ((modifiers & KeyState::Shift) &&
(keyval == FcitxKey_Hiragana_Katakana));
Expand Down Expand Up @@ -407,8 +419,8 @@ bool KeyTranslator::IsPrintable(KeySym keyval, uint32_t keycode,
return IsAscii(keyval, keycode, modifiers);
}

bool KeyTranslator::IsAscii(KeySym keyval, uint32_t keycode,
KeyStates modifiers) {
bool KeyTranslator::IsAscii(KeySym keyval, uint32_t /*keycode*/,
KeyStates /*modifiers*/) {
return (keyval > FcitxKey_space &&
// Note: Space key (0x20) is a special key in Mozc.
keyval <= FcitxKey_asciitilde); // 0x7e.
Expand Down
4 changes: 1 addition & 3 deletions src/unix/fcitx5/fcitx_key_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@
#include <fcitx-utils/key.h>

#include <cstdint>
#include <map>
#include <set>
#include <string>

#include "base/port.h"
#include "protocol/config.pb.h"
#include "protocol/commands.pb.h"

namespace fcitx {
Expand Down
27 changes: 27 additions & 0 deletions src/unix/fcitx5/i18nwrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (C) 2017~2017 by CSSlayer
* wengxt@gmail.com
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not,
* see <http://www.gnu.org/licenses/>.
*/

#ifndef UNIX_FCITX5_I18NWRAPPER_H_
#define UNIX_FCITX5_I18NWRAPPER_H_

#define FCITX_GETTEXT_DOMAIN "fcitx5-mozc"

#include <fcitx-utils/i18n.h> // IWYU pragma: export

#endif
10 changes: 8 additions & 2 deletions src/unix/fcitx5/mozc_client_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,19 @@
#include "unix/fcitx5/mozc_client_pool.h"

#include <fcitx-utils/charutils.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/stringutils.h>
#include <fcitx/inputcontext.h>
#include <fcitx/inputcontextmanager.h>

#include <cassert>
#include <memory>
#include <string>

#include "unix/fcitx5/mozc_connection.h"

namespace fcitx {

MozcClientHolder::MozcClientHolder() {}

MozcClientHolder::~MozcClientHolder() {
if (pool_) {
pool_->unregisterClient(key_);
Expand Down
7 changes: 5 additions & 2 deletions src/unix/fcitx5/mozc_client_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@
#ifndef UNIX_FCITX5_MOZC_CLIENT_POOL_H_
#define UNIX_FCITX5_MOZC_CLIENT_POOL_H_

#include <fcitx/inputcontext.h>
#include <fcitx/inputcontextmanager.h>

#include <memory>
#include <string>
#include <unordered_map>

#include "client/client_interface.h"
Expand All @@ -45,7 +48,7 @@ class MozcClientHolder {
friend class MozcClientPool;

public:
MozcClientHolder();
MozcClientHolder() = default;

MozcClientHolder(MozcClientHolder &&) = delete;

Expand Down Expand Up @@ -84,4 +87,4 @@ class MozcClientPool {

} // namespace fcitx

#endif
#endif
4 changes: 1 addition & 3 deletions src/unix/fcitx5/mozc_connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@

#include "unix/fcitx5/mozc_connection.h"

#include <string>
#include <memory>

#include "base/logging.h"
#include "base/util.h"
#include "base/vlog.h"
#include "client/client.h"
#include "ipc/ipc.h"
Expand Down
5 changes: 0 additions & 5 deletions src/unix/fcitx5/mozc_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
#define MOZC_UNIX_FCITX5_MOZC_CONNECTION_H_

#include <memory>
#include <string>

#include "base/port.h"
#include "protocol/commands.pb.h"
#include "unix/fcitx5/fcitx_key_event_handler.h"

namespace mozc {

Expand Down
Loading

0 comments on commit fe465bb

Please sign in to comment.