Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ partial_accept = true
esc_dismisses = true
blocklist_apps = ["konsole", "alacritty", "keepassxc", "1password"]

[ghost]
style = "inline" # "inline": in the app's text (the app draws it, usually underlined)
# "popup": gray text drawn by Fcitx5 at the caret
popup_font = "Sans 11" # popup only: font of the gray text
popup_theme = "xtype-ghost" # popup only: Fcitx5 theme used while popup is on
hyprland_inline = false # popup + Hyprland only: show the popup on the caret line

[learning]
enabled = false # opt-in
corpus_path = "~/.local/share/xtype/corpus.txt"
Expand All @@ -250,6 +257,8 @@ mode = "Default"
| `min_context_chars` | Minimum buffered characters before the engine asks for a suggestion. |
| `context_window` | Sliding-window size (chars) handed to the model as prefill. |
| `blocklist_apps` | Case-insensitive component match against `ic->program()`. |
| `ghost.style` | `"inline"` keeps the app's own (underlined) preedit. `"popup"` shows gray ghost text through Fcitx5's popup; XType switches Fcitx5's classic UI to `popup_theme`/`popup_font` and restores your own theme when you switch back. |
| `ghost.hyprland_inline` | Hyprland only. Loads the bundled `xtype-inline` plugin, which moves the popup from below the caret onto the caret line. **Not every app benefits:** it only affects native Wayland apps using the text-input protocol, and only lines up where the app reports its caret correctly. X11/XWayland apps and Qt apps using Fcitx5's Qt module (`QT_IM_MODULE=fcitx`) draw their popup elsewhere, so it stays below the caret; Electron/Chromium apps (VS Code, Teams, Edge, …) report a stale caret, so the popup lags behind the text. Use the inline style for those apps. The plugin is built against the installed Hyprland headers — rebuild and reinstall XType after Hyprland updates. |
| `learning.voice_strength` | 0 = no corpus examples in prompt; 100 = all exemplars included; proportional. |
| `learning.forget_after_days` | Prune corpus entries older than N days. 0 disables pruning. |
| `user_prompt.description` | Free-text description of yourself injected into the system prompt (≤ 500 chars). |
Expand Down
22 changes: 22 additions & 0 deletions fcitx5-engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ set_target_properties(config_loader_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_library(xtype-fcitx5 SHARED
src/xtype.cpp
src/inference_client.cpp
src/ghost_popup.cpp
)
target_include_directories(xtype-fcitx5 PRIVATE src)
target_link_libraries(xtype-fcitx5 PRIVATE
Expand All @@ -67,6 +68,27 @@ install(TARGETS xtype-fcitx5
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/data/xtype.conf"
DESTINATION "${FCITX_INSTALL_PKGDATADIR}/addon"
)
# Popup ghost style: gray-text Fcitx5 theme, selected by the engine while
# ghost.style = "popup" (see src/ghost_popup.h).
install(FILES data/themes/xtype-ghost/theme.conf
DESTINATION "${FCITX_INSTALL_PKGDATADIR}/themes/xtype-ghost"
)

# Optional Hyprland plugin for ghost.hyprland_inline. Built when the Hyprland
# plugin headers are installed; must be rebuilt after every Hyprland update.
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
pkg_check_modules(HYPRLAND_PLUGIN_DEPS QUIET IMPORTED_TARGET
hyprland pixman-1 libdrm libinput libudev wayland-server xkbcommon)
endif()
option(XTYPE_HYPRLAND_PLUGIN "Build the xtype-inline Hyprland plugin" ${HYPRLAND_PLUGIN_DEPS_FOUND})
if(XTYPE_HYPRLAND_PLUGIN)
set(XTYPE_HYPRLAND_PLUGIN_DIR "${CMAKE_INSTALL_PREFIX}/lib/xtype")
add_subdirectory(hyprland-plugin)
target_compile_definitions(xtype-fcitx5 PRIVATE
XTYPE_HYPRLAND_PLUGIN_PATH="${XTYPE_HYPRLAND_PLUGIN_DIR}/xtype-inline.so")
endif()

install(FILES data/xtype-im.conf
DESTINATION "${FCITX_INSTALL_PKGDATADIR}/inputmethod"
RENAME xtype.conf
Expand Down
84 changes: 84 additions & 0 deletions fcitx5-engine/data/themes/xtype-ghost/theme.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# XType ghost theme. Fcitx5's input panel as bare gray text on a
# transparent background, so XType's popup ghost style reads as ghost text.
# Applies to every Fcitx5 popup while selected (Theme=xtype-ghost in
# ~/.config/fcitx5/conf/classicui.conf).
[Metadata]
Name=XType Ghost
Version=1
Author=XType
Description=Gray ghost text on a transparent background (XType popup suggestions)
ScaleWithDPI=True

[InputPanel]
NormalColor=#8c8c8c
HighlightCandidateColor=#8c8c8c
HighlightColor=#8c8c8c
HighlightBackgroundColor=#00000000
EnableBlur=False

[InputPanel/TextMargin]
Left=0
Right=0
Top=0
Bottom=0

[InputPanel/ContentMargin]
Left=0
Right=0
Top=0
Bottom=0

[InputPanel/Background]
Color=#00000000
BorderColor=#00000000
BorderWidth=0

[InputPanel/Background/Margin]
Left=0
Right=0
Top=0
Bottom=0

[InputPanel/Highlight]
Color=#00000000

[InputPanel/Highlight/Margin]
Left=0
Right=0
Top=0
Bottom=0

[Menu/Background]
Color=#202020
BorderColor=#404040
BorderWidth=1

[Menu/Background/Margin]
Left=2
Right=2
Top=2
Bottom=2

[Menu/ContentMargin]
Left=2
Right=2
Top=2
Bottom=2

[Menu/Highlight]
Color=#404040

[Menu/Highlight/Margin]
Left=5
Right=5
Top=5
Bottom=5

[Menu/Separator]
Color=#404040

[Menu/TextMargin]
Left=5
Right=5
Top=5
Bottom=5
9 changes: 9 additions & 0 deletions fcitx5-engine/hyprland-plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# xtype-inline: Hyprland plugin for ghost.hyprland_inline (see main.cpp).
# Plugins link against the running compositor's internals, so this must be
# built with the headers of the Hyprland version that loads it.
add_library(xtype-inline MODULE main.cpp)
set_target_properties(xtype-inline PROPERTIES PREFIX "" CXX_STANDARD 23)
target_compile_options(xtype-inline PRIVATE -Wno-c++11-narrowing
$<$<CXX_COMPILER_ID:GNU>:--no-gnu-unique>)
target_link_libraries(xtype-inline PRIVATE PkgConfig::HYPRLAND_PLUGIN_DEPS)
install(TARGETS xtype-inline LIBRARY DESTINATION "${XTYPE_HYPRLAND_PLUGIN_DIR}")
150 changes: 150 additions & 0 deletions fcitx5-engine/hyprland-plugin/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// xtype-inline — Hyprland plugin for XType's popup ghost style.
//
// Hyprland places input-method popups *below* the text caret (see
// CInputPopup::updateBox). With XType's popup ghost style that popup holds the
// suggestion, so moving it onto the caret's own line — starting right after the
// caret and vertically centred on it — makes it read as inline ghost text.
//
// Every consumer of the popup position (rendering, damage, pointer hit-tests)
// goes through CInputPopup::globalBox(), so hooking that one function is
// enough. isVecInPopup() is hooked too so the popup is click-through and never
// steals clicks from the text underneath it.
//
// The caret comes from the app (text-input cursor rectangle). When the app
// reports none, or one that can't be right (no height, outside its own
// surface), the popup is hidden instead of drawn somewhere unrelated.
//
// Debug: with ~/.local/share/xtype/.debug_verbose present when the plugin
// loads, every change of caret / surface / popup box is appended to
// ~/.local/share/xtype/hyprland-inline.log.
#include <hyprland/src/plugins/PluginAPI.hpp>
#include <hyprland/src/plugins/HookSystem.hpp>
#include <hyprland/src/managers/input/InputManager.hpp>
#include <hyprland/src/managers/input/InputMethodPopup.hpp>
#include <hyprland/src/managers/input/TextInput.hpp>
#include <hyprland/src/desktop/view/WLSurface.hpp>

#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <format>
#include <stdexcept>
#include <string>

inline HANDLE PHANDLE = nullptr;
static CFunctionHook* g_globalBoxHook = nullptr;
static CFunctionHook* g_isVecInPopupHook = nullptr;

using GlobalBoxFn = CBox (*)(CInputPopup*);
using IsVecInPopupFn = bool (*)(CInputPopup*, const Vector2D&);

static FILE* g_debugLog = nullptr; // open only in debug mode
static std::string g_lastLogged;

static void debugLog(const std::string& line) {
if (!g_debugLog || line == g_lastLogged)
return; // globalBox runs every frame; log changes only
g_lastLogged = line;
std::fprintf(g_debugLog, "%s\n", line.c_str());
std::fflush(g_debugLog);
}

static std::string str(const CBox& b) {
return std::format("{:.0f},{:.0f} {:.0f}x{:.0f}", b.x, b.y, b.w, b.h);
}

// A box far outside every monitor: the popup is still mapped but not visible.
static CBox offscreen(CBox box) {
box.x = -100000;
box.y = -100000;
return box;
}

static CBox hkGlobalBox(CInputPopup* self) {
CBox box = reinterpret_cast<GlobalBoxFn>(g_globalBoxHook->m_original)(self);

const auto TI = g_pInputManager->m_relay.getFocusedTextInput();
if (!TI)
return box; // not attached to a text input: leave Hyprland's placement
if (!TI->hasCursorRectangle()) {
if (g_debugLog) debugLog("hide: app reports no caret");
return offscreen(box);
}

const auto SURF = Desktop::View::CWLSurface::fromResource(TI->focusedSurface());
if (!SURF)
return box;
const auto PARENT = SURF->getSurfaceBoxGlobal();
if (!PARENT)
return box;

const CBox CARET = TI->cursorBox(); // surface-local
const bool plausible = CARET.h > 0 && CARET.x >= 0 && CARET.y >= 0 &&
CARET.x <= PARENT->w && CARET.y + CARET.h <= PARENT->h;
if (!plausible) {
if (g_debugLog) debugLog("hide: caret " + str(CARET) + " outside surface " + str(*PARENT));
return offscreen(box);
}

box.x = PARENT->x + CARET.x + CARET.w;
box.y = PARENT->y + CARET.y + (CARET.h - box.h) / 2.0;
if (g_debugLog) debugLog("caret " + str(CARET) + " surface " + str(*PARENT) + " popup " + str(box));
return box;
}

static bool hkIsVecInPopup(CInputPopup*, const Vector2D&) {
return false;
}

static void* findMember(const std::string& name, const std::string& qualified) {
for (const auto& m : HyprlandAPI::findFunctionsByName(PHANDLE, name))
if (m.demangled.find(qualified) != std::string::npos)
return m.address;
return nullptr;
}

APICALL EXPORT std::string PLUGIN_API_VERSION() {
return HYPRLAND_API_VERSION;
}

APICALL EXPORT PLUGIN_DESCRIPTION_INFO PLUGIN_INIT(HANDLE handle) {
PHANDLE = handle;

if (std::string(__hyprland_api_get_hash()) != __hyprland_api_get_client_hash()) {
HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] Version mismatch: rebuild against the running Hyprland",
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[xtype-inline] version mismatch");
}

void* globalBox = findMember("globalBox", "CInputPopup::globalBox");
void* isVecInPopup = findMember("isVecInPopup", "CInputPopup::isVecInPopup");
if (!globalBox || !isVecInPopup) {
HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] CInputPopup functions not found; plugin inactive",
CHyprColor{1.0, 0.2, 0.2, 1.0}, 5000);
throw std::runtime_error("[xtype-inline] symbols not found");
}

g_globalBoxHook = HyprlandAPI::createFunctionHook(PHANDLE, globalBox, reinterpret_cast<void*>(&hkGlobalBox));
g_isVecInPopupHook = HyprlandAPI::createFunctionHook(PHANDLE, isVecInPopup, reinterpret_cast<void*>(&hkIsVecInPopup));
if (!g_globalBoxHook->hook() || !g_isVecInPopupHook->hook())
throw std::runtime_error("[xtype-inline] failed to hook");

if (const char* home = std::getenv("HOME")) {
const std::filesystem::path dir = std::filesystem::path(home) / ".local/share/xtype";
std::error_code ec;
if (std::filesystem::exists(dir / ".debug_verbose", ec))
g_debugLog = std::fopen((dir / "hyprland-inline.log").c_str(), "a");
}

HyprlandAPI::addNotification(PHANDLE, "[xtype-inline] loaded: input-method popups now sit on the caret line",
CHyprColor{0.52, 0.44, 0.9, 1.0}, 3000);
return {"xtype-inline", "Places input-method popups on the caret line (XType popup suggestions)", "XType", "0.1"};
}

APICALL EXPORT void PLUGIN_EXIT() {
// Hooks created through HyprlandAPI are removed automatically on unload.
if (g_debugLog) {
std::fclose(g_debugLog);
g_debugLog = nullptr;
}
}
20 changes: 20 additions & 0 deletions fcitx5-engine/src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
#include <vector>

enum class TriggerMode { Pause, Manual };
// How the suggestion is shown. Inline: as preedit in the app's own text field
// (the app styles it — usually underlined). Popup: drawn by Fcitx5's UI in a
// popup at the caret, so a Fcitx5 theme controls its look (gray text).
enum class GhostStyle { Inline, Popup };

enum class AcceptKey { Tab, Enter, Right };

struct InferenceConfig {
Expand Down Expand Up @@ -59,11 +64,26 @@ struct AppOverride {
std::optional<int> num_predict;
std::optional<std::string> mode; // Default/Code-aware/Email tone/Casual/Off
std::optional<std::string> prompt_addendum;
std::optional<GhostStyle> ghost_style; // overrides [ghost] style for this app
};

struct GhostConfig {
GhostStyle style = GhostStyle::Inline;
// Popup style only. While it is on, XType switches Fcitx5's classic UI to
// popup_theme / popup_font and restores the user's own settings after.
std::string popup_font = "Sans 11";
std::string popup_theme = "xtype-ghost";
// Popup style on Hyprland only: load the xtype-inline Hyprland plugin,
// which moves the popup from below the caret onto the caret's line. Only
// native Wayland text-input apps are affected (not X11, nor Qt apps using
// Fcitx5's Qt module), and Chromium-based apps report a stale caret.
bool hyprland_inline = false;
};

struct XTypeConfig {
InferenceConfig inference;
BehaviourConfig behaviour;
GhostConfig ghost;
LearningConfig learning;
UserPromptConfig user_prompt;
std::map<std::string, AppOverride> apps;
Expand Down
22 changes: 22 additions & 0 deletions fcitx5-engine/src/config_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ XTypeConfig config_loader::load(const std::string& path) {
}
}

// [ghost]
if (auto* t = tbl["ghost"].as_table()) {
if (auto v = (*t)["style"].value<std::string>())
cfg.ghost.style = (*v == "popup") ? GhostStyle::Popup : GhostStyle::Inline;
if (auto v = (*t)["popup_font"].value<std::string>()) cfg.ghost.popup_font = *v;
if (auto v = (*t)["popup_theme"].value<std::string>()) cfg.ghost.popup_theme = *v;
if (auto v = (*t)["hyprland_inline"].value<bool>()) cfg.ghost.hyprland_inline = *v;
}

// [learning]
if (auto* t = tbl["learning"].as_table()) {
if (auto v = (*t)["enabled"].value<bool>()) cfg.learning.enabled = *v;
Expand Down Expand Up @@ -116,10 +125,23 @@ XTypeConfig config_loader::load(const std::string& path) {
if (auto v = (*at)["num_predict"].value<int64_t>()) ov.num_predict = (int)*v;
if (auto v = (*at)["mode"].value<std::string>()) ov.mode = *v;
if (auto v = (*at)["prompt_addendum"].value<std::string>()) ov.prompt_addendum = *v;
if (auto v = (*at)["ghost_style"].value<std::string>())
ov.ghost_style = (*v == "popup") ? GhostStyle::Popup : GhostStyle::Inline;
cfg.apps[std::string(key.str())] = ov;
}
}
}

return cfg;
}

GhostStyle config_loader::ghostStyleFor(const XTypeConfig& cfg, const AppOverride* ov) {
return (ov && ov->ghost_style) ? *ov->ghost_style : cfg.ghost.style;
}

GhostConfig config_loader::ghostSetupConfig(const XTypeConfig& cfg) {
GhostConfig g = cfg.ghost;
for (const auto& [_, ov] : cfg.apps)
if (ov.ghost_style == GhostStyle::Popup) g.style = GhostStyle::Popup;
return g;
}
10 changes: 10 additions & 0 deletions fcitx5-engine/src/config_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ XTypeConfig load(const std::string& path = "");
// Expands ~ using $HOME; returns empty string on failure.
std::string default_path();

// The suggestion style for an app: its [apps."<name>"] ghost_style if set,
// else [ghost] style. `ov` may be null (no override for the app).
GhostStyle ghostStyleFor(const XTypeConfig& cfg, const AppOverride* ov);

// What the popup setup (classicui theme, Hyprland plugin) should apply:
// [ghost] with style = Popup when [ghost] or any app's override is popup.
// The setup stays on across app switches; each app's own style then picks
// popup or inline when a suggestion is shown.
GhostConfig ghostSetupConfig(const XTypeConfig& cfg);

} // namespace config_loader
Loading