From 8348d361e1783bdfcf9c1a0efe38963354e3118b Mon Sep 17 00:00:00 2001 From: hankei6km Date: Tue, 20 Feb 2018 00:15:51 +0900 Subject: [PATCH] Implement version 0.1.0 --- CMakeLists.txt | 18 +++ LICENSE.txt | 32 ++++++ README.md | 69 +++++++++++ po/CMakeLists.txt | 5 + po/fcitx-at-esc.pot | 47 ++++++++ po/ja.po | 47 ++++++++ src/CMakeLists.txt | 10 ++ src/at-esc.c | 241 +++++++++++++++++++++++++++++++++++++++ src/at-esc.h | 45 ++++++++ src/fcitx-at-esc.conf.in | 8 ++ src/fcitx-at-esc.desc | 22 ++++ 11 files changed, 544 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 po/CMakeLists.txt create mode 100644 po/fcitx-at-esc.pot create mode 100644 po/ja.po create mode 100644 src/CMakeLists.txt create mode 100644 src/at-esc.c create mode 100644 src/at-esc.h create mode 100644 src/fcitx-at-esc.conf.in create mode 100644 src/fcitx-at-esc.desc diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..23e53f6 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,18 @@ +project(fcitx-at-esc) +cmake_minimum_required(VERSION 2.6) + +set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH}) +find_package(Fcitx 4.2.7 REQUIRED) +find_package(PkgConfig REQUIRED) +find_package(Gettext REQUIRED) +# pkg_check_modules(LIBSKK "libskk" REQUIRED) + +_fcitx_add_uninstall_target() + +set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -fvisibility=hidden ${CMAKE_C_FLAGS}") +set(CMAKE_CXX_FLAGS "-Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -fvisibility=hidden ${CMAKE_CXX_FLAGS}") +set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed ${CMAKE_SHARED_LINKER_FLAGS}") +set(CMAKE_MODULE_LINKER_FLAGS "-Wl,--as-needed ${CMAKE_MODULE_LINKER_FLAGS}") + +add_subdirectory(src) +add_subdirectory(po) diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..1b0f410 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,32 @@ +MIT License + +Copyright (c) 2018 hankei6km + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +This repository based from Fcitx Tsundere Addon (https://github.com/felixonmars/fcitx-tsundere) +/*************************************************************************** + * Copyright (C) 2010~2010 by Felix Yan * + * felixonmars@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e952af7 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# @esc addon for Fcitx + +@esc は、 +Vim ライクなキーバインド用に、esc キー押下で IME をオフにさせるための +Fcitx 用 addon です. + + +## Installation + +cmake で C言語用のビルドができる環境であれば、以下の手順でインストールできると思います(Arch Linux で確認しています). + +``` +$ git clone https://github.com/hankei6km/fcitx-at-esc.git +$ cd fcitx-at-esc +$ cmake -DCMAKE_INSTALL_PREFIX=/usr . +$ sudo make install +``` + +## Usage + +@esc が有効な状態で日本語入力を行っているとき、`esc` を押下することで IME がオフ(disabel) になります. +なお、変換途中の文字ある場合の `esc` の押下については、 +後述の設定によって変化します +(デフォルトでは、`esc` の押下は IME 側で処理されます). + +## Configuration + +Fcitx の設定パネルから「アドオン」タブを開き「@esc」項目を選択すると、以下の項目を設定できます. + +### 有効化(デフォルト値はオン) + +@esc の機能の有効化/無効化を切り替えます. +なお、この項目は挙動を変更するだけで、addon 自体の有効化/無効化には反映されません. + +### 強制的にIMEをオフ(デフォルト値はオフ) + +この項目をオンにしていると、「変換入力中か?」といった状態に関係なく IME をオフにします. +変換途中の未確定文字の扱いについては、後述の「未確定文字をコミットする」項目の設定に依存します. + +なお、この項目をオンにしていると +IME に対しての `esc` 押下による操作が行えなくなります. +これについては、IME の設定等で他のキーに `esc` 相当の機能を割り当てる、 +といった回避方法が考えられます. + +### 未確定文字をコミットする(デフォルト値はオフ) + +上記の「強制的にIMEをオフ」と、この項目をオンにしていると、 +`esc` 押下時に未確定の文字があればコミット(確定)します. +なお、チェックを外しても IME や Fcitx の設定によってはコミットされるかもしれません. + +この項目がオンの時の挙動が少しわかりにくいのですが、 +例えば Vim 上で変換入力している場合に `esc` を押下すると「未確定文字がコミット(確定)され IME はオフになり、Vim はNORMAL モードに切り替わる」といった動作となります. + +### 有効化/無効化の切り替え(デフォルト値は空) + +@esc の機能の有効化と無効化を切り替えるホットキーを指定できます. + +## Known Issues + +現状では mozc との組み合わせのみで利用しているので、他の IME との組み合わせについては殆ど動作確認していません. +skk との組み合わせでは以下のような不具合がわかっています. + +* @esc から未確定文字をコミット(確定)すると、`▼` 等もコミットされてしまう. + +## License + +Copyright (c) 2018 hankei6km + +Licensed under the MIT License. See LICENSE.txt in the project root. diff --git a/po/CMakeLists.txt b/po/CMakeLists.txt new file mode 100644 index 0000000..a4f70e1 --- /dev/null +++ b/po/CMakeLists.txt @@ -0,0 +1,5 @@ +set(PO_LANGS ja) +foreach(lang ${PO_LANGS}) + fcitx_translate_add_po_file("${lang}" "${lang}.po") +endforeach() +fcitx_translate_set_pot_target(pot fcitx-at-esc fcitx-at-esc.pot) diff --git a/po/fcitx-at-esc.pot b/po/fcitx-at-esc.pot new file mode 100644 index 0000000..f1e4103 --- /dev/null +++ b/po/fcitx-at-esc.pot @@ -0,0 +1,47 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: fcitx-dev@googlegroups.com\n" +"POT-Creation-Date: 2018-02-19 23:33+0900\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: LANG\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: src/fcitx-at-esc.conf.in:3 +msgid "@esc" +msgstr "" + +#: src/fcitx-at-esc.conf.in:4 +msgid "Hook esc key to disable IME for Vim like key bind." +msgstr "" + +#: src/fcitx-at-esc.desc:1 src/fcitx-at-esc.desc:6 src/fcitx-at-esc.desc:11 +#: src/fcitx-at-esc.desc:16 +msgid "Basic" +msgstr "" + +#: src/fcitx-at-esc.desc:4 +msgid "Enabled" +msgstr "" + +#: src/fcitx-at-esc.desc:9 +msgid "Force disable IME" +msgstr "" + +#: src/fcitx-at-esc.desc:14 +msgid "Commit preedit text" +msgstr "" + +#: src/fcitx-at-esc.desc:19 +msgid "Toggle for enable or disable" +msgstr "" diff --git a/po/ja.po b/po/ja.po new file mode 100644 index 0000000..1e8de5e --- /dev/null +++ b/po/ja.po @@ -0,0 +1,47 @@ +# Japanese translations for PACKAGE package. +# Copyright (C) 2018 THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# Automatically generated, 2018. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: fcitx-dev@googlegroups.com\n" +"POT-Creation-Date: 2018-02-19 23:33+0900\n" +"PO-Revision-Date: 2018-02-18 18:41+0900\n" +"Last-Translator: Automatically generated\n" +"Language-Team: none\n" +"Language: ja\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=1; plural=0;\n" + +#: src/fcitx-at-esc.conf.in:3 +msgid "@esc" +msgstr "" + +#: src/fcitx-at-esc.desc:1 src/fcitx-at-esc.desc:6 src/fcitx-at-esc.desc:11 +#: src/fcitx-at-esc.desc:16 +msgid "Basic" +msgstr "基本" + +#: src/fcitx-at-esc.desc:14 +msgid "Commit preedit text" +msgstr "未確定文字をコミットする" + +#: src/fcitx-at-esc.desc:4 +msgid "Enabled" +msgstr "有効化" + +#: src/fcitx-at-esc.desc:9 +msgid "Force disable IME" +msgstr "強制的にIMEをオフ" + +#: src/fcitx-at-esc.conf.in:4 +msgid "Hook esc key to disable IME for Vim like key bind." +msgstr "Vim ライクなキーバインド用に、esc キーをフックし IME を disable させる" + +#: src/fcitx-at-esc.desc:19 +msgid "Toggle for enable or disable" +msgstr "有効化/無効化の切り替え" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..9a5c90e --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,10 @@ +include_directories(${PROJECT_SOURCE_DIR}/src) +# include_directories(${LIBSKK_INCLUDE_DIRS}) +# link_directories(${LIBSKK_LIBRARY_DIRS}) + +set(FCITX_AT_ESC_SOURCES + at-esc.c + ) +fcitx_add_addon_full(at-esc DESC + HEADERS at-esc.h + SOURCES ${FCITX_AT_ESC_SOURCES}) diff --git a/src/at-esc.c b/src/at-esc.c new file mode 100644 index 0000000..608b0ee --- /dev/null +++ b/src/at-esc.c @@ -0,0 +1,241 @@ +/*************************************************************************** + * + * fcitx-at-esc + * + * Copyright (c) 2018 hankei6km + * Licensed under the MIT License. See LICENSE.txt in the project root. + * + * + ***************************************************************************/ + +/*************************************************************************** + * Copyright (C) 2010~2010 by Felix Yan * + * felixonmars@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "fcitx/fcitx.h" + +#include +#include + +#include "fcitx/module.h" +#include "fcitx-utils/utf8.h" +#include "fcitx-utils/uthash.h" +#include "fcitx-config/xdg.h" +#include "fcitx/hook.h" +#include "fcitx/keys.h" +#include "fcitx/ui.h" +#include "fcitx-utils/log.h" +#include "fcitx/instance.h" +#include "fcitx/context.h" +#include "fcitx-utils/utils.h" +#include "at-esc.h" + +#define _(x) dgettext("fcitx-at-esc", (x)) + +typedef struct _FcitxAtEsc { + FcitxGenericConfig gconfig; + boolean enabled; + boolean forceDisableIME; + boolean commitPreedit; + FcitxHotkey toggleHotkey[2]; + FcitxInstance* owner; +} FcitxAtEsc; + +void* AtEscCreate(FcitxInstance* instance); +void ToggleAtEscState(void* arg); +boolean GetAtEscEnabled(void* arg); +boolean LoadAtEscConfig(FcitxAtEsc* atEscState); +static FcitxConfigFileDesc* GetAtEscConfigDesc(); +void SaveAtEscConfig(FcitxAtEsc* atEscState); +void ReloadAtEsc(void* arg); +INPUT_RETURN_VALUE HotkeyToggleAtEscState(void* arg); +INPUT_RETURN_VALUE HotkeyEscapeAtEscState(void* arg); +boolean IMSelectorAtEscPreFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retval); +boolean IMSelectorAtEscPostFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retval); + +CONFIG_BINDING_BEGIN(FcitxAtEsc) +CONFIG_BINDING_REGISTER("Basic", "Enabled", enabled) +CONFIG_BINDING_REGISTER("Basic", "ForceDisableIME", forceDisableIME) +CONFIG_BINDING_REGISTER("Basic", "CommitPreedit", commitPreedit) +CONFIG_BINDING_REGISTER("Basic", "ToggleHotkey", toggleHotkey) +CONFIG_BINDING_END() + +FCITX_EXPORT_API +FcitxModule module = { + AtEscCreate, + NULL, + NULL, + NULL, + ReloadAtEsc +}; +// FcitxModule module = { +// AtEscCreate, +// MySetFD, +// EventHandler, +// NULL, +// ReloadAtEsc +// }; + +FCITX_EXPORT_API +int ABI_VERSION = FCITX_ABI_VERSION; + +void* AtEscCreate(FcitxInstance* instance) +{ + FcitxAtEsc* atEscState = fcitx_utils_malloc0(sizeof(FcitxAtEsc)); + atEscState->owner = instance; + if (!LoadAtEscConfig(atEscState)) { + free(atEscState); + return NULL; + } + + FcitxHotkeyHook thk; + thk.arg = atEscState; + thk.hotkey = atEscState->toggleHotkey; + thk.hotkeyhandle = HotkeyToggleAtEscState; + + FcitxKeyFilterHook prekf; + prekf.arg = atEscState; + prekf.func = IMSelectorAtEscPreFilter; + + FcitxKeyFilterHook postkf; + postkf.arg = atEscState; + postkf.func = IMSelectorAtEscPostFilter; + + FcitxInstanceRegisterHotkeyFilter(instance, thk); + FcitxInstanceRegisterPreInputFilter(instance, prekf); + FcitxInstanceRegisterPostInputFilter(instance, postkf); + // FcitxUIRegisterStatus(instance, atEscState, "@esc", _("@esc"), _("@esc"), ToggleAtEscState, GetAtEscEnabled); + + return atEscState; +} + +void ToggleAtEscState(void* arg) +{ + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + atEscState->enabled = !atEscState->enabled; + SaveAtEscConfig(atEscState); +} + +boolean GetAtEscEnabled(void* arg) +{ + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + return atEscState->enabled; +} + +boolean LoadAtEscConfig(FcitxAtEsc* atEscState) +{ + FcitxConfigFileDesc* configDesc = GetAtEscConfigDesc(); + if (configDesc == NULL) + return false; + + FILE *fp; + char *file; + fp = FcitxXDGGetFileUserWithPrefix("conf", "fcitx-at-esc.config", "r", &file); + FcitxLog(DEBUG, "Load Config File %s", file); + free(file); + if (!fp) { + if (errno == ENOENT) + SaveAtEscConfig(atEscState); + } + + FcitxConfigFile *cfile = FcitxConfigParseConfigFileFp(fp, configDesc); + + FcitxAtEscConfigBind(atEscState, cfile, configDesc); + FcitxConfigBindSync((FcitxGenericConfig*)atEscState); + + if (fp) + fclose(fp); + + return true; +} + +CONFIG_DESC_DEFINE(GetAtEscConfigDesc, "fcitx-at-esc.desc") + +void SaveAtEscConfig(FcitxAtEsc* atEscState) +{ + FcitxConfigFileDesc* configDesc = GetAtEscConfigDesc(); + char *file; + FILE *fp = FcitxXDGGetFileUserWithPrefix("conf", "fcitx-at-esc.config", "w", &file); + FcitxLog(DEBUG, "Save Config to %s", file); + FcitxConfigSaveConfigFileFp(fp, &atEscState->gconfig, configDesc); + free(file); + if (fp) + fclose(fp); +} + +void ReloadAtEsc(void* arg) +{ + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + LoadAtEscConfig(atEscState); +} + +INPUT_RETURN_VALUE HotkeyToggleAtEscState(void* arg) +{ + // FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + + // FcitxUIStatus *status = FcitxUIGetStatusByName(atEscState->owner, "@esc"); + // if (status->visible){ + // FcitxUIUpdateStatus(atEscState->owner, "@esc"); + // return IRV_DO_NOTHING; + // } + // else + // return IRV_TO_PROCESS; + ToggleAtEscState(arg); + return IRV_DO_NOTHING; +} + +boolean HandleAtEscFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retval) +{ + boolean handled = true; + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + + if(atEscState->enabled){ + FcitxLog(DEBUG, "hooked esc in pre filter"); + if(sym == FcitxKey_Escape){ + if(atEscState->commitPreedit){ + FcitxUICommitPreedit(atEscState->owner); + } + FcitxInstanceCloseIM(atEscState->owner, + FcitxInstanceGetCurrentIC(atEscState->owner)); + } + } + return handled; +} + +boolean IMSelectorAtEscPreFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retval) +{ + boolean handled = true; + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + + if(atEscState->forceDisableIME){ + handled = HandleAtEscFilter(arg, sym, state, retval); + } + return handled; +} + +boolean IMSelectorAtEscPostFilter(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retval) +{ + boolean handled = true; + FcitxAtEsc* atEscState = (FcitxAtEsc*) arg; + + if(!atEscState->forceDisableIME){ + handled = HandleAtEscFilter(arg, sym, state, retval); + } + return handled; +} diff --git a/src/at-esc.h b/src/at-esc.h new file mode 100644 index 0000000..8d56652 --- /dev/null +++ b/src/at-esc.h @@ -0,0 +1,45 @@ +/*************************************************************************** + * + * fcitx-at-esc + * + * Copyright (c) 2018 hankei6km + * Licensed under the MIT License. See LICENSE.txt in the project root. + * + * + ***************************************************************************/ + +/*************************************************************************** + * Copyright (C) 2012~2012 by Felix Yan * + * felixonmars@gmail.com * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef _FCITX_MODULE_AT_ESC_H_ +#define _FCITX_MODULE_AT_ESC_H_ + + +#ifdef __cplusplus +extern "C" { +#endif + +#define FCITX_AT_ESC_NAME "fcitx-at-esc" + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/fcitx-at-esc.conf.in b/src/fcitx-at-esc.conf.in new file mode 100644 index 0000000..5eda347 --- /dev/null +++ b/src/fcitx-at-esc.conf.in @@ -0,0 +1,8 @@ +[Addon] +Name=fcitx-at-esc +_GeneralName=@esc +_Comment=Hook esc key to disable IME for Vim like key bind. +Category=Module +Enabled=True +Library=fcitx-at-esc.so +Type=SharedLibrary diff --git a/src/fcitx-at-esc.desc b/src/fcitx-at-esc.desc new file mode 100644 index 0000000..ea35469 --- /dev/null +++ b/src/fcitx-at-esc.desc @@ -0,0 +1,22 @@ +[Basic/Enabled] +Type=Boolean +DefaultValue=True +Description=Enabled + +[Basic/ForceDisableIME] +Type=Boolean +DefaultValue=False +Description=Force disable IME + +[Basic/CommitPreedit] +Type=Boolean +DefaultValue=False +Description=Commit preedit text + +[Basic/ToggleHotkey] +Type=Hotkey +DefaultValue= +Description=Toggle for enable or disable + +[DescriptionFile] +LocaleDomain=fcitx-at-esc