Debian Codegraph @

modules/fullwidth/fullwidth.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7
8 #include "fullwidth.h"
9 #include "notifications_public.h"
10 #include <fcitx-config/iniparser.h>
11 #include <fcitx-utils/i18n.h>
12 #include <fcitx-utils/standardpaths.h>
13 #include <fcitx-utils/utf8.h>
14 #include <fcitx/addonfactory.h>
15 #include <fcitx/inputcontext.h>
16 #include <fcitx/inputmethodentry.h>
17 #include <fcitx/statusarea.h>
18 #include <fcitx/userinterfacemanager.h>
19 #include <fcntl.h>
20
21 using namespace fcitx;
22
23 const char *sCornerTrans[] = {
24 " ", "!", """, "#", "¥", "%", "&", "'", "(", ")", "*", "+",
25 ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7",
26 "8", "9", ":", ";", "<", "=", ">", "?", "@", "A", "B", "C",
27 "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
28 "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[",
29 "\", "]", "^", "_", "`", "a", "b", "c", "d", "e", "f", "g",
30 "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
31 "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~",
32 };
33
34 Fullwidth::Fullwidth(Instance *instance) : instance_(instance) {
35 instance_->userInterfaceManager().registerAction("fullwidth",
36 &toggleAction_);
37 auto filterKey = [this](Event &event) {
38 if (!enabled_) {
39 return;
40 }
41 auto &keyEvent = static_cast<KeyEventBase &>(event);
42 if (!inWhiteList(keyEvent.inputContext())) {
43 return;
44 }
45 if (keyEvent.key().states() || keyEvent.isRelease()) {
46 return;
47 }
48 auto key = static_cast<uint32_t>(keyEvent.key().sym());
49 if (key >= 32 && key - 32 < FCITX_ARRAY_SIZE(sCornerTrans)) {
50 keyEvent.accept();
51 keyEvent.inputContext()->commitString(sCornerTrans[key - 32]);
52 }
53 };
54
55 eventHandlers_.emplace_back(instance->watchEvent(
56 EventType::InputContextKeyEvent, EventWatcherPhase::Default,
57 [this, filterKey](Event &event) {
58 auto &keyEvent = static_cast<KeyEvent &>(event);
59 if (keyEvent.isRelease()) {
60 return;
61 }
62 if (!inWhiteList(keyEvent.inputContext())) {
63 return;
64 }
65
66 if (keyEvent.key().checkKeyList(config_.hotkey.value())) {
67 setEnabled(!enabled_, keyEvent.inputContext());
68 if (notifications()) {
69 notifications()->call<INotifications::showTip>(
70 "fcitx-fullwidth-toggle", _("Full width character"),
71 enabled_ ? "fcitx-fullwidth-active"
72 : "fcitx-fullwidth-inactive",
73 _("Full width Character"),
74 enabled_ ? _("Full width Character is enabled.")
75 : _("Full width Character is disabled."),
76 1000);
77 }
78 keyEvent.filterAndAccept();
79 return;
80 }
81
82 return filterKey(event);
83 }));
84 commitFilterConn_ = instance_->connect<Instance::CommitFilter>(
85 [this](InputContext *inputContext, std::string &str) {
86 if (!enabled_ || !inWhiteList(inputContext)) {
87 return;
88 }
89 auto len = utf8::length(str);
90 std::string result;
91 const auto *ps = str.c_str();
92 for (size_t i = 0; i < len; ++i) {
93 uint32_t wc;
94 char *nps;
95 nps = fcitx_utf8_get_char(ps, &wc);
96 int chr_len = nps - ps;
97 if (wc > 32 && wc - 32 < FCITX_ARRAY_SIZE(sCornerTrans)) {
98 result.append(sCornerTrans[wc - 32]);
99 } else {
100 result.append(ps, chr_len);
101 }
102
103 ps = nps;
104 }
105 str = std::move(result);
106 });
107
108 reloadConfig();
109 }
110
111 void Fullwidth::reloadConfig() {
112 readAsIni(config_, "conf/fullwidth.conf");
113 toggleAction_.setHotkey(config_.hotkey.value());
114 }
115
116 bool Fullwidth::inWhiteList(InputContext *inputContext) const {
117 return toggleAction_.isParent(&inputContext->statusArea());
118 }
119
120 class FullwidthModuleFactory : public AddonFactory {
121 AddonInstance *create(AddonManager *manager) override {
122 registerDomain("fcitx5-chinese-addons", FCITX_INSTALL_LOCALEDIR);
123 return new Fullwidth(manager->instance());
124 }
125 };
126
127 FCITX_ADDON_FACTORY_V2(fullwidth, FullwidthModuleFactory)