fcitx5-hooks: Created fcitx module to help with on-screen keyboard input

This commit is contained in:
Yuki Joou 2023-07-22 10:39:31 +02:00
parent 2049494676
commit 72b8ea80ae
6 changed files with 127 additions and 1 deletions

4
.gitignore vendored
View file

@ -2,4 +2,6 @@ wayland-protos/*.c
wayland-protos/*.h
wayland-protos/*.xml
*~
convertablet
convertablet
fcitx5-hooks/libhooks.so

26
fcitx5-hooks/README.md Normal file
View file

@ -0,0 +1,26 @@
# fcitx5-hooks
This is a fcitx5 module that allows the user to run scripts on various fcitx events!
## Build
```bash
./build.sh
```
## Install
```bash
./install.sh
```
Now, restart fcitx, and you should get the hooks module!
## Hooks available
All hooks are stored in `~/.config/fcitx5-hooks/`
| Name | File |
|----------------------|------------------------------|
| InputContextFocusIn | input-context-focus-in-hook |
| InputContextFocusOut | input-context-focus-out-hook |

5
fcitx5-hooks/build.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh
set -ex
clang++ -fPIC -std=c++20 -Wall -Wextra -pedantic -shared -o libhooks.so hooks.cpp -I/usr/include/Fcitx5/Core/ -I/usr/include/Fcitx5/Config -I/usr/include/Fcitx5/Utils -lFcitx5Core -lFcitx5Config -lFcitx5Utils

7
fcitx5-hooks/hooks.conf Normal file
View file

@ -0,0 +1,7 @@
[Addon]
Name=Hooks
Category=Module
Library=libhooks
Type=SharedLibrary
OnDemand=False
Configurable=False

76
fcitx5-hooks/hooks.cpp Normal file
View file

@ -0,0 +1,76 @@
#include <fcitx-utils/handlertable_details.h>
#include <fcitx/addonfactory.h>
#include <fcitx/addoninstance.h>
#include <fcitx/addonmanager.h>
#include <fcitx/instance.h>
#include <filesystem>
#include <stdlib.h>
#include <unistd.h>
namespace fs = std::filesystem;
static fs::path const hooksConfigDir =
fs::path(getenv("XDG_CONFIG_DIR")
? getenv("XDG_CONFIG_DIR")
: (fs::path(getenv("HOME")) / ".config")) /
"fcitx5-hooks";
void runCommand(std::string_view hookName)
{
auto full_path = hooksConfigDir / hookName;
pid_t pid = fork();
if (pid < 0) {
FCITX_ERROR() << "Couldn't fork to run command";
return;
} else if (pid == 0) {
char* argv[] = {strdup(full_path.c_str()), 0};
execve(full_path.c_str(), argv, environ);
exit(-1);
}
return;
}
static void eventHandler(fcitx::Event& event)
{
using ET = fcitx::EventType;
switch (event.type()) {
case ET::InputContextFocusIn:
runCommand("input-context-focus-in-hook");
case ET::InputContextFocusOut:
runCommand("input-context-focus-out-hook");
default:
}
}
class Hooks : public fcitx::AddonInstance
{
public:
Hooks(fcitx::Instance* instance)
{
#define WATCH_EVENT(event_name) \
instance->watchEvent(fcitx::EventType::event_name, \
fcitx::EventWatcherPhase::InputMethod, \
eventHandler)
focusEvent_ = WATCH_EVENT(InputContextFocusIn);
unfocusEvent_ = WATCH_EVENT(InputContextFocusOut);
}
private:
std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>> focusEvent_;
std::unique_ptr<fcitx::HandlerTableEntry<fcitx::EventHandler>> unfocusEvent_;
};
class HooksFactory : public fcitx::AddonFactory
{
fcitx::AddonInstance* create(fcitx::AddonManager* manager) override
{
FCITX_UNUSED(manager);
return new Hooks(manager->instance());
}
};
FCITX_ADDON_FACTORY(HooksFactory);

10
fcitx5-hooks/install.sh Executable file
View file

@ -0,0 +1,10 @@
#!/bin/sh
[ ! "$UID" = 0 ] && echo "You must run this script as root" && exit 1
echo "Installing..."
set -ex
cp libhooks.so /usr/lib64/fcitx5/
cp hooks.conf /usr/share/fcitx5/addon/