Fcitx
instance.cpp
1 /*
2  * SPDX-FileCopyrightText: 2016-2016 CSSlayer <wengxt@gmail.com>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  */
7 #include "config.h"
8 
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <algorithm>
12 #include <array>
13 #include <cassert>
14 #include <csignal>
15 #include <cstdint>
16 #include <cstdlib>
17 #include <filesystem>
18 #include <functional>
19 #include <iostream>
20 #include <iterator>
21 #include <memory>
22 #include <optional>
23 #include <stdexcept>
24 #include <string>
25 #include <string_view>
26 #include <tuple>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <utility>
30 #include <vector>
31 #include <getopt.h>
32 #include "fcitx-config/configuration.h"
33 #include "fcitx-config/iniparser.h"
34 #include "fcitx-config/option.h"
36 #include "fcitx-utils/cutf8.h"
37 #include "fcitx-utils/environ.h"
38 #include "fcitx-utils/event.h"
39 #include "fcitx-utils/eventdispatcher.h"
40 #include "fcitx-utils/eventloopinterface.h"
41 #include "fcitx-utils/fs.h"
42 #include "fcitx-utils/handlertable.h"
43 #include "fcitx-utils/i18n.h"
44 #include "fcitx-utils/key.h"
45 #include "fcitx-utils/keysym.h"
46 #include "fcitx-utils/log.h"
47 #include "fcitx-utils/macros.h"
48 #include "fcitx-utils/misc.h"
49 #include "fcitx-utils/misc_p.h"
53 #include "fcitx-utils/utf8.h"
54 #include "../../modules/notifications/notifications_public.h"
55 #include "addonmanager.h"
56 #include "event.h"
57 #include "focusgroup.h"
58 #include "globalconfig.h"
59 #include "inputcontextmanager.h"
60 #include "inputcontextproperty.h"
61 #include "inputmethodengine.h"
62 #include "inputmethodentry.h"
63 #include "inputmethodgroup.h"
64 #include "inputmethodmanager.h"
65 #include "instance.h"
66 #include "instance_p.h"
67 #include "misc_p.h"
68 #include "statusarea.h"
69 #include "text.h"
70 #include "userinterface.h"
71 #include "userinterfacemanager.h"
72 
73 #ifdef HAVE_SYS_WAIT_H
74 #include <sys/wait.h>
75 #endif
76 
77 #ifdef ENABLE_X11
78 #define FCITX_NO_XCB
79 #include <../modules/xcb/xcb_public.h>
80 #endif
81 
82 #ifdef ENABLE_KEYBOARD
83 #include <xkbcommon/xkbcommon-compose.h>
84 #include <xkbcommon/xkbcommon.h>
85 #endif
86 
87 FCITX_DEFINE_LOG_CATEGORY(keyTrace, "key_trace");
88 
89 namespace fcitx {
90 
91 namespace {
92 
93 constexpr uint64_t AutoSaveMinInUsecs = 60ULL * 1000000ULL; // 30 minutes
94 constexpr uint64_t AutoSaveIdleTime = 60ULL * 1000000ULL; // 1 minutes
95 
96 FCITX_CONFIGURATION(DefaultInputMethod,
97  Option<std::vector<std::string>> defaultInputMethods{
98  this, "DefaultInputMethod", "DefaultInputMethod"};
99  Option<std::vector<std::string>> extraLayouts{
100  this, "ExtraLayout", "ExtraLayout"};);
101 
102 void initAsDaemon() {
103 #ifndef _WIN32
104  pid_t pid = fork();
105  if (pid > 0) {
106  waitpid(pid, nullptr, 0);
107  exit(0);
108  }
109  setsid();
110  auto oldint = signal(SIGINT, SIG_IGN);
111  auto oldhup = signal(SIGHUP, SIG_IGN);
112  auto oldquit = signal(SIGQUIT, SIG_IGN);
113  auto oldpipe = signal(SIGPIPE, SIG_IGN);
114  auto oldttou = signal(SIGTTOU, SIG_IGN);
115  auto oldttin = signal(SIGTTIN, SIG_IGN);
116  auto oldchld = signal(SIGCHLD, SIG_IGN);
117  if (fork() > 0) {
118  exit(0);
119  }
120  (void)chdir("/");
121 
122  signal(SIGINT, oldint);
123  signal(SIGHUP, oldhup);
124  signal(SIGQUIT, oldquit);
125  signal(SIGPIPE, oldpipe);
126  signal(SIGTTOU, oldttou);
127  signal(SIGTTIN, oldttin);
128  signal(SIGCHLD, oldchld);
129 #endif
130 }
131 
132 // Switch IM when these capabilities change.
133 bool shouldSwitchIM(const CapabilityFlags &oldFlags,
134  const CapabilityFlags &newFlags) {
135  const bool oldDisable = oldFlags.testAny(
136  CapabilityFlags{CapabilityFlag::Password, CapabilityFlag::Disable});
137  const bool newDisable = newFlags.testAny(
138  CapabilityFlags{CapabilityFlag::Password, CapabilityFlag::Disable});
139  return oldDisable != newDisable;
140 }
141 
142 } // namespace
143 
144 void InstanceArgument::printUsage() const {
145  std::cout
146  << "Usage: " << argv0 << " [Option]\n"
147  << " --disable <addon names>\tA comma separated list of addons to "
148  "be disabled.\n"
149  << "\t\t\t\t\"all\" can be used to disable all addons.\n"
150  << " --enable <addon names>\tA comma separated list of addons to "
151  "be enabled.\n"
152  << "\t\t\t\t\"all\" can be used to enable all addons.\n"
153  << "\t\t\t\tThis value will override the value in the flag "
154  "--disable.\n"
155  << " --verbose <logging rule>\tSet the logging rule for "
156  "displaying message.\n"
157  << "\t\t\t\tSyntax: category1=level1,category2=level2, ...\n"
158  << "\t\t\t\tE.g. default=4,key_trace=5\n"
159  << "\t\t\t\tLevels are numbers ranging from 0 to 5.\n"
160  << "\t\t\t\t\t0 - NoLog\n"
161  << "\t\t\t\t\t1 - Fatal\n"
162  << "\t\t\t\t\t2 - Error\n"
163  << "\t\t\t\t\t3 - Warn\n"
164  << "\t\t\t\t\t4 - Info (default)\n"
165  << "\t\t\t\t\t5 - Debug\n"
166  << "\t\t\t\tSome built-in categories are:\n"
167  << "\t\t\t\t\tdefault - miscellaneous category used by fcitx own "
168  "library.\n"
169  << "\t\t\t\t\tkey_trace - print the key event received by fcitx.\n"
170  << "\t\t\t\t\t\"*\" may be used to represent all logging "
171  "category.\n"
172  << " -u, --ui <addon name>\t\tSet the UI addon to be used.\n"
173  << " -d\t\t\t\tRun as a daemon.\n"
174  << " -D\t\t\t\tDo not run as a daemon (default).\n"
175  << " -s <seconds>\t\t\tNumber of seconds to wait before start.\n"
176  << " -k, --keep\t\t\tKeep running even the main display is "
177  "disconnected.\n"
178  << " -r, --replace\t\t\tReplace the existing instance.\n"
179  << " -o --option <option>\t\tPass the option to addons\n"
180  << "\t\t\t\t<option> is in format like:\n"
181  << "\t\t\t\tname1=opt1a:opt1b,name2=opt2a:opt2b... .\n"
182  << " -v, --version\t\t\tShow version and quit.\n"
183  << " -h, --help\t\t\tShow this help message and quit.\n";
184 }
185 
186 InstancePrivate::InstancePrivate(Instance *q) : QPtrHolder<Instance>(q) {
187 #ifdef ENABLE_KEYBOARD
188  const auto &locale = getCurrentLocale();
189  assert(!locale.empty());
190  xkbContext_.reset(xkb_context_new(XKB_CONTEXT_NO_FLAGS));
191  if (xkbContext_) {
192  xkb_context_set_log_level(xkbContext_.get(), XKB_LOG_LEVEL_CRITICAL);
193  xkbComposeTable_.reset(xkb_compose_table_new_from_locale(
194  xkbContext_.get(), locale.data(), XKB_COMPOSE_COMPILE_NO_FLAGS));
195  if (!xkbComposeTable_) {
196  FCITX_INFO()
197  << "Trying to fallback to compose table for en_US.UTF-8";
198  xkbComposeTable_.reset(xkb_compose_table_new_from_locale(
199  xkbContext_.get(), "en_US.UTF-8",
200  XKB_COMPOSE_COMPILE_NO_FLAGS));
201  }
202  if (!xkbComposeTable_) {
203  FCITX_WARN()
204  << "No compose table is loaded, you may want to check your "
205  "locale settings.";
206  }
207  }
208 #endif
209 }
210 
211 std::unique_ptr<HandlerTableEntry<EventHandler>>
212 InstancePrivate::watchEvent(EventType type, EventWatcherPhase phase,
213  EventHandler callback) {
214  return eventHandlers_[type][phase].add(std::move(callback));
215 }
216 
217 #ifdef ENABLE_KEYBOARD
218 xkb_keymap *InstancePrivate::keymap(const std::string &display,
219  const std::string &layout,
220  const std::string &variant) {
221  auto layoutAndVariant = stringutils::concat(layout, "-", variant);
222  if (auto *keymapPtr = findValue(keymapCache_[display], layoutAndVariant)) {
223  return (*keymapPtr).get();
224  }
225  struct xkb_rule_names names;
226  names.layout = layout.c_str();
227  names.variant = variant.c_str();
228  std::tuple<std::string, std::string, std::string> xkbParam;
229  if (auto *param = findValue(xkbParams_, display)) {
230  xkbParam = *param;
231  } else {
232  if (!xkbParams_.empty()) {
233  xkbParam = xkbParams_.begin()->second;
234  } else {
235  xkbParam = std::make_tuple(DEFAULT_XKB_RULES, "pc101", "");
236  }
237  }
238  if (globalConfig_.overrideXkbOption()) {
239  std::get<2>(xkbParam) = globalConfig_.customXkbOption();
240  }
241  names.rules = std::get<0>(xkbParam).c_str();
242  names.model = std::get<1>(xkbParam).c_str();
243  names.options = std::get<2>(xkbParam).c_str();
244  UniqueCPtr<xkb_keymap, xkb_keymap_unref> keymap(xkb_keymap_new_from_names(
245  xkbContext_.get(), &names, XKB_KEYMAP_COMPILE_NO_FLAGS));
246  auto result =
247  keymapCache_[display].emplace(layoutAndVariant, std::move(keymap));
248  assert(result.second);
249  return result.first->second.get();
250 }
251 #endif
252 
253 std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>>
254 InstancePrivate::overrideAddons() {
255  std::unordered_set<std::string> enabled;
256  std::unordered_set<std::string> disabled;
257  for (const auto &addon : globalConfig_.enabledAddons()) {
258  enabled.insert(addon);
259  }
260  for (const auto &addon : globalConfig_.disabledAddons()) {
261  enabled.erase(addon);
262  disabled.insert(addon);
263  }
264  for (const auto &addon : arg_.enableList) {
265  disabled.erase(addon);
266  enabled.insert(addon);
267  }
268  for (const auto &addon : arg_.disableList) {
269  enabled.erase(addon);
270  disabled.insert(addon);
271  }
272  return {enabled, disabled};
273 }
274 
275 void InstancePrivate::buildDefaultGroup() {
276  /// Figure out XKB layout information from system.
277  auto *defaultGroup = q_func()->defaultFocusGroup();
278  bool infoFound = false;
279  std::string layouts;
280  std::string variants;
281  auto guessLayout = [this, &layouts, &variants,
282  &infoFound](FocusGroup *focusGroup) {
283  // For now we can only do this on X11.
284  if (!focusGroup->display().starts_with("x11:")) {
285  return true;
286  }
287 #ifdef ENABLE_X11
288  auto *xcb = addonManager_.addon("xcb");
289  auto x11Name = focusGroup->display().substr(4);
290  if (xcb) {
291  auto rules = xcb->call<IXCBModule::xkbRulesNames>(x11Name);
292  if (!rules[2].empty()) {
293  layouts = rules[2];
294  variants = rules[3];
295  infoFound = true;
296  return false;
297  }
298  }
299 #else
300  FCITX_UNUSED(this);
301  FCITX_UNUSED(layouts);
302  FCITX_UNUSED(variants);
303  FCITX_UNUSED(infoFound);
304 #endif
305  return true;
306  };
307  if (!defaultGroup || guessLayout(defaultGroup)) {
308  icManager_.foreachGroup(
309  [defaultGroup, &guessLayout](FocusGroup *focusGroup) {
310  if (defaultGroup == focusGroup) {
311  return true;
312  }
313  return guessLayout(focusGroup);
314  });
315  }
316  if (!infoFound) {
317  layouts = "us";
318  variants = "";
319  }
320 
321  // layouts and variants are comma separated list for layout information.
322  constexpr char imNamePrefix[] = "keyboard-";
323  auto layoutTokens =
324  stringutils::split(layouts, ",", stringutils::SplitBehavior::KeepEmpty);
325  auto variantTokens = stringutils::split(
326  variants, ",", stringutils::SplitBehavior::KeepEmpty);
327  auto size = std::max(layoutTokens.size(), variantTokens.size());
328  // Make sure we have token to be the same size.
329  layoutTokens.resize(size);
330  variantTokens.resize(size);
331 
332  OrderedSet<std::string> imLayouts;
333  for (decltype(size) i = 0; i < size; i++) {
334  if (layoutTokens[i].empty()) {
335  continue;
336  }
337  std::string layoutName = layoutTokens[i];
338  if (!variantTokens[i].empty()) {
339  layoutName = stringutils::concat(layoutName, "-", variantTokens[i]);
340  }
341 
342  // Skip the layout if we don't have it.
343  if (!imManager_.entry(stringutils::concat(imNamePrefix, layoutName))) {
344  continue;
345  }
346  // Avoid add duplicate entry. layout might have weird duplicate.
347  imLayouts.pushBack(layoutName);
348  }
349 
350  // Load the default profile.
351  auto lang = stripLanguage(getCurrentLanguage());
352  DefaultInputMethod defaultIMConfig;
353  readAsIni(defaultIMConfig, StandardPathsType::PkgData,
354  std::filesystem::path("default") / lang);
355 
356  // Add extra layout from profile.
357  for (const auto &extraLayout : defaultIMConfig.extraLayouts.value()) {
358  if (!imManager_.entry(stringutils::concat(imNamePrefix, extraLayout))) {
359  continue;
360  }
361  imLayouts.pushBack(extraLayout);
362  }
363 
364  // Make sure imLayouts is not empty.
365  if (imLayouts.empty()) {
366  imLayouts.pushBack("us");
367  }
368 
369  // Figure out the first available default input method.
370  std::string defaultIM;
371  for (const auto &im : defaultIMConfig.defaultInputMethods.value()) {
372  if (imManager_.entry(im)) {
373  defaultIM = im;
374  break;
375  }
376  }
377 
378  // Create a group for each layout.
379  std::vector<std::string> groupOrders;
380  for (const auto &imLayout : imLayouts) {
381  std::string groupName;
382  if (imLayouts.size() == 1) {
383  groupName = _("Default");
384  } else {
385  groupName = _("Group {}", imManager_.groupCount() + 1);
386  }
387  imManager_.addEmptyGroup(groupName);
388  groupOrders.push_back(groupName);
389  InputMethodGroup group(groupName);
390  group.inputMethodList().emplace_back(
391  InputMethodGroupItem(stringutils::concat(imNamePrefix, imLayout)));
392  if (!defaultIM.empty()) {
393  group.inputMethodList().emplace_back(
394  InputMethodGroupItem(defaultIM));
395  }
396  FCITX_INFO() << "Items in " << groupName << ": "
397  << group.inputMethodList();
398  group.setDefaultLayout(imLayout);
399  imManager_.setGroup(std::move(group));
400  }
401  FCITX_INFO() << "Generated groups: " << groupOrders;
402  imManager_.setGroupOrder(groupOrders);
403 }
404 
405 void InstancePrivate::showInputMethodInformation(InputContext *ic) {
406  FCITX_Q();
407  auto *inputState = ic->propertyFor(&inputStateFactory_);
408  auto *engine = q->inputMethodEngine(ic);
409  const auto *entry = q->inputMethodEntry(ic);
410  auto &imManager = q->inputMethodManager();
411 
412  if (!inputState->isActive() &&
413  !globalConfig_.showFirstInputMethodInformation()) {
414  return;
415  }
416 
417  std::string display;
418  if (engine) {
419  auto subMode = engine->subMode(*entry, *ic);
420  auto subModeLabel = engine->subModeLabel(*entry, *ic);
421  auto name = globalConfig_.compactInputMethodInformation() &&
422  !entry->label().empty()
423  ? entry->label()
424  : entry->name();
425  if (globalConfig_.compactInputMethodInformation() &&
426  !subModeLabel.empty()) {
427  display = std::move(subModeLabel);
428  } else if (subMode.empty()) {
429  display = std::move(name);
430  } else {
431  display = _("{0} ({1})", name, subMode);
432  }
433  } else if (entry) {
434  display = _("{0} (Not available)", entry->name());
435  } else {
436  display = _("(Not available)");
437  }
438  if (!globalConfig_.compactInputMethodInformation() &&
439  imManager.groupCount() > 1) {
440  display = _("Group {0}: {1}", imManager.currentGroup().name(), display);
441  }
442  inputState->showInputMethodInformation(display);
443 }
444 
445 bool InstancePrivate::canActivate(InputContext *ic) {
446  FCITX_Q();
447  if (!q->canTrigger()) {
448  return false;
449  }
450  auto *inputState = ic->propertyFor(&inputStateFactory_);
451  return !inputState->isActive();
452 }
453 
454 bool InstancePrivate::canDeactivate(InputContext *ic) {
455  FCITX_Q();
456  if (!q->canTrigger()) {
457  return false;
458  }
459  auto *inputState = ic->propertyFor(&inputStateFactory_);
460  return inputState->isActive();
461 }
462 
463 void InstancePrivate::navigateGroup(InputContext *ic, const Key &key,
464  bool forward) {
465  auto *inputState = ic->propertyFor(&inputStateFactory_);
466  inputState->pendingGroupIndex_ =
467  (inputState->pendingGroupIndex_ +
468  (forward ? 1 : imManager_.groupCount() - 1)) %
469  imManager_.groupCount();
470  FCITX_DEBUG() << "Switch to group " << inputState->pendingGroupIndex_;
471 
472  if (notifications_ && !isSingleKey(key)) {
473  notifications_->call<INotifications::showTip>(
474  "enumerate-group", _("Input Method"), "input-keyboard",
475  _("Switch group"),
476  _("Switch group to {0}",
477  imManager_.groups()[inputState->pendingGroupIndex_]),
478  3000);
479  }
480 }
481 
482 void InstancePrivate::acceptGroupChange(const Key &key, InputContext *ic) {
483  FCITX_DEBUG() << "Accept group change, isSingleKey: " << key;
484 
485  auto *inputState = ic->propertyFor(&inputStateFactory_);
486  auto groups = imManager_.groups();
487  if (groups.size() > inputState->pendingGroupIndex_) {
488  if (isSingleKey(key)) {
489  FCITX_DEBUG() << "EnumerateGroupTo: "
490  << inputState->pendingGroupIndex_ << " " << key;
491  imManager_.enumerateGroupTo(groups[inputState->pendingGroupIndex_]);
492  } else {
493  FCITX_DEBUG() << "SetCurrentGroup: "
494  << inputState->pendingGroupIndex_ << " " << key;
495  imManager_.setCurrentGroup(groups[inputState->pendingGroupIndex_]);
496  }
497  }
498  inputState->pendingGroupIndex_ = 0;
499 }
500 
501 InputState::InputState(InstancePrivate *d, InputContext *ic)
502  : d_ptr(d), ic_(ic) {
503  active_ = d->globalConfig_.activeByDefault();
504 #ifdef ENABLE_KEYBOARD
505  if (d->xkbComposeTable_) {
506  xkbComposeState_.reset(xkb_compose_state_new(
507  d->xkbComposeTable_.get(), XKB_COMPOSE_STATE_NO_FLAGS));
508  }
509 #endif
510 }
511 
512 void InputState::showInputMethodInformation(const std::string &name) {
513  ic_->inputPanel().setAuxUp(Text(name));
514  ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
515  lastInfo_ = name;
516  imInfoTimer_ = d_ptr->eventLoop_.addTimeEvent(
517  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 1000000, 0,
518  [this](EventSourceTime *, uint64_t) {
519  hideInputMethodInfo();
520  return true;
521  });
522 }
523 
524 #ifdef ENABLE_KEYBOARD
525 xkb_state *InputState::customXkbState(bool refresh) {
526  auto *instance = d_ptr->q_func();
527  const InputMethodGroup &group = d_ptr->imManager_.currentGroup();
528  const auto im = instance->inputMethod(ic_);
529  auto layout = group.layoutFor(im);
530  if (layout.empty() && im.starts_with("keyboard-")) {
531  layout = im.substr(9);
532  }
533  if (layout.empty() || layout == group.defaultLayout()) {
534  // Use system one.
535  xkbState_.reset();
536  modsAllReleased_ = false;
537  lastXkbLayout_.clear();
538  return nullptr;
539  }
540 
541  if (layout == lastXkbLayout_ && !refresh) {
542  return xkbState_.get();
543  }
544 
545  lastXkbLayout_ = layout;
546  const auto layoutAndVariant = parseLayout(layout);
547  if (auto *keymap = d_ptr->keymap(ic_->display(), layoutAndVariant.first,
548  layoutAndVariant.second)) {
549  xkbState_.reset(xkb_state_new(keymap));
550  } else {
551  xkbState_.reset();
552  }
553  modsAllReleased_ = false;
554  return xkbState_.get();
555 }
556 #endif
557 
558 void InputState::setActive(bool active) {
559  if (active_ != active) {
560  active_ = active;
561  ic_->updateProperty(&d_ptr->inputStateFactory_);
562  }
563 }
564 
565 void InputState::setLocalIM(const std::string &localIM) {
566  if (localIM_ != localIM) {
567  localIM_ = localIM;
568  ic_->updateProperty(&d_ptr->inputStateFactory_);
569  }
570 }
571 
572 void InputState::copyTo(InputContextProperty *other) {
573  auto *otherState = static_cast<InputState *>(other);
574  if (otherState->active_ == active_ && otherState->localIM_ == localIM_) {
575  return;
576  }
577 
578  if (otherState->ic_->hasFocus()) {
579  FCITX_DEBUG() << "Sync state to focused ic: "
580  << otherState->ic_->program();
581  CheckInputMethodChanged imChangedRAII(otherState->ic_, d_ptr);
582  otherState->active_ = active_;
583  otherState->localIM_ = localIM_;
584  } else {
585  otherState->active_ = active_;
586  otherState->localIM_ = localIM_;
587  }
588 }
589 
590 void InputState::reset() {
591 #ifdef ENABLE_KEYBOARD
592  if (xkbComposeState_) {
593  xkb_compose_state_reset(xkbComposeState_.get());
594  }
595 #endif
596  pendingGroupIndex_ = 0;
597  keyReleased_ = -1;
598  lastKeyPressed_ = Key();
599  lastKeyPressedTime_ = 0;
600  totallyReleased_ = true;
601 }
602 
603 void InputState::hideInputMethodInfo() {
604  if (!imInfoTimer_) {
605  return;
606  }
607  imInfoTimer_.reset();
608  auto &panel = ic_->inputPanel();
609  if (panel.auxDown().empty() && panel.preedit().empty() &&
610  panel.clientPreedit().empty() &&
611  (!panel.candidateList() || panel.candidateList()->empty()) &&
612  panel.auxUp().size() == 1 && panel.auxUp().stringAt(0) == lastInfo_) {
613  panel.reset();
614  ic_->updateUserInterface(UserInterfaceComponent::InputPanel);
615  }
616 }
617 
618 #ifdef ENABLE_KEYBOARD
619 void InputState::resetXkbState() {
620  lastXkbLayout_.clear();
621  xkbState_.reset();
622 }
623 #endif
624 
625 CheckInputMethodChanged::CheckInputMethodChanged(InputContext *ic,
626  InstancePrivate *instance)
627  : instance_(instance->q_func()), instancePrivate_(instance),
628  ic_(ic->watch()), inputMethod_(instance_->inputMethod(ic)),
629  reason_(InputMethodSwitchedReason::Other) {
630  auto *inputState = ic->propertyFor(&instance->inputStateFactory_);
631  if (!inputState->imChanged_) {
632  inputState->imChanged_ = this;
633  } else {
634  ic_.unwatch();
635  }
636 }
637 
638 CheckInputMethodChanged::~CheckInputMethodChanged() {
639  if (!ic_.isValid()) {
640  return;
641  }
642  auto *ic = ic_.get();
643  auto *inputState = ic->propertyFor(&instancePrivate_->inputStateFactory_);
644  inputState->imChanged_ = nullptr;
645  if (inputMethod_ != instance_->inputMethod(ic) && !ignore_) {
646  instance_->postEvent(
647  InputContextSwitchInputMethodEvent(reason_, inputMethod_, ic));
648  }
649 }
650 
651 Instance::Instance(int argc, char **argv) {
652  InstanceArgument arg;
653  arg.parseOption(argc, argv);
654  if (arg.quietQuit) {
655  throw InstanceQuietQuit();
656  }
657 
658  if (arg.runAsDaemon) {
659  initAsDaemon();
660  }
661 
662  if (arg.overrideDelay > 0) {
663  sleep(arg.overrideDelay);
664  }
665 
666  // we need fork before this
667  d_ptr = std::make_unique<InstancePrivate>(this);
668  FCITX_D();
669  d->arg_ = arg;
670  d->eventDispatcher_.attach(&d->eventLoop_);
671  d->addonManager_.setInstance(this);
672  d->addonManager_.setAddonOptions(arg.addonOptions_);
673  d->icManager_.setInstance(this);
674  d->connections_.emplace_back(
675  d->imManager_.connect<InputMethodManager::CurrentGroupAboutToChange>(
676  [this, d](const std::string &lastGroup) {
677  d->icManager_.foreachFocused([this](InputContext *ic) {
678  assert(ic->hasFocus());
679  InputContextSwitchInputMethodEvent event(
680  InputMethodSwitchedReason::GroupChange, inputMethod(ic),
681  ic);
682  deactivateInputMethod(event);
683  return true;
684  });
685  d->lastGroup_ = lastGroup;
687  }));
688  d->connections_.emplace_back(
689  d->imManager_.connect<InputMethodManager::CurrentGroupChanged>(
690  [this, d](const std::string &newGroup) {
691  d->icManager_.foreachFocused([this](InputContext *ic) {
692  assert(ic->hasFocus());
693  InputContextSwitchInputMethodEvent event(
694  InputMethodSwitchedReason::GroupChange, "", ic);
695  activateInputMethod(event);
696  return true;
697  });
698  postEvent(InputMethodGroupChangedEvent());
699  if (!d->lastGroup_.empty() && !newGroup.empty() &&
700  d->lastGroup_ != newGroup && d->notifications_ &&
701  d->imManager_.groupCount() > 1) {
702  d->notifications_->call<INotifications::showTip>(
703  "enumerate-group", _("Input Method"), "input-keyboard",
704  _("Switch group"),
705  _("Switched group to {0}",
706  d->imManager_.currentGroup().name()),
707  3000);
708  }
709  d->lastGroup_ = newGroup;
710  }));
711 
712  d->eventWatchers_.emplace_back(d->watchEvent(
713  EventType::InputContextCapabilityAboutToChange,
714  EventWatcherPhase::ReservedFirst, [this](Event &event) {
715  auto &capChanged =
716  static_cast<CapabilityAboutToChangeEvent &>(event);
717  if (!capChanged.inputContext()->hasFocus()) {
718  return;
719  }
720 
721  if (!shouldSwitchIM(capChanged.oldFlags(), capChanged.newFlags())) {
722  return;
723  }
724 
727  inputMethod(capChanged.inputContext()),
728  capChanged.inputContext());
729  deactivateInputMethod(switchIM);
730  }));
731  d->eventWatchers_.emplace_back(d->watchEvent(
732  EventType::InputContextCapabilityChanged,
733  EventWatcherPhase::ReservedFirst, [this](Event &event) {
734  auto &capChanged = static_cast<CapabilityChangedEvent &>(event);
735  if (!capChanged.inputContext()->hasFocus()) {
736  return;
737  }
738 
739  if (!shouldSwitchIM(capChanged.oldFlags(), capChanged.newFlags())) {
740  return;
741  }
742 
745  capChanged.inputContext());
746  activateInputMethod(switchIM);
747  }));
748 
749  d->eventWatchers_.emplace_back(watchEvent(
750  EventType::InputContextKeyEvent, EventWatcherPhase::InputMethod,
751  [this, d](Event &event) {
752  auto &keyEvent = static_cast<KeyEvent &>(event);
753  auto *ic = keyEvent.inputContext();
754  CheckInputMethodChanged imChangedRAII(ic, d);
755  auto origKey = keyEvent.origKey().normalize();
756 
757  struct {
758  const KeyList &list;
759  std::function<bool()> check;
760  std::function<void(bool)> trigger;
761  } keyHandlers[] = {
762  {.list = d->globalConfig_.triggerKeys(),
763  .check = [this]() { return canTrigger(); },
764  .trigger =
765  [this, ic](bool totallyReleased) {
766  return trigger(ic, totallyReleased);
767  }},
768  {.list = d->globalConfig_.altTriggerKeys(),
769  .check = [this, ic]() { return canAltTrigger(ic); },
770  .trigger = [this, ic](bool) { return altTrigger(ic); }},
771  {.list = d->globalConfig_.activateKeys(),
772  .check = [ic, d]() { return d->canActivate(ic); },
773  .trigger = [this, ic](bool) { return activate(ic); }},
774  {.list = d->globalConfig_.deactivateKeys(),
775  .check = [ic, d]() { return d->canDeactivate(ic); },
776  .trigger = [this, ic](bool) { return deactivate(ic); }},
777  {.list = d->globalConfig_.enumerateForwardKeys(),
778  .check = [this, ic]() { return canEnumerate(ic); },
779  .trigger = [this, ic](bool) { return enumerate(ic, true); }},
780  {.list = d->globalConfig_.enumerateBackwardKeys(),
781  .check = [this, ic]() { return canEnumerate(ic); },
782  .trigger = [this, ic](bool) { return enumerate(ic, false); }},
783  {.list = d->globalConfig_.enumerateGroupForwardKeys(),
784  .check = [this]() { return canChangeGroup(); },
785  .trigger = [ic, d, origKey](
786  bool) { d->navigateGroup(ic, origKey, true); }},
787  {.list = d->globalConfig_.enumerateGroupBackwardKeys(),
788  .check = [this]() { return canChangeGroup(); },
789  .trigger =
790  [ic, d, origKey](bool) {
791  d->navigateGroup(ic, origKey, false);
792  }},
793  };
794 
795  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
796  int keyReleased = inputState->keyReleased_;
797  Key lastKeyPressed = inputState->lastKeyPressed_;
798  // Keep this value, and reset them in the state
799  inputState->keyReleased_ = -1;
800  const bool isModifier = origKey.isModifier();
801  if (keyEvent.isRelease()) {
802  int idx = 0;
803  for (auto &keyHandler : keyHandlers) {
804  if (keyReleased == idx &&
805  origKey.isReleaseOfModifier(lastKeyPressed) &&
806  keyHandler.check()) {
807  if (isModifier) {
808  if (d->globalConfig_.checkModifierOnlyKeyTimeout(
809  inputState->lastKeyPressedTime_)) {
810  keyHandler.trigger(
811  inputState->totallyReleased_);
812  }
813  inputState->lastKeyPressedTime_ = 0;
814  if (origKey.hasModifier()) {
815  inputState->totallyReleased_ = false;
816  }
817  }
818  keyEvent.filter();
819  break;
820  }
821  idx++;
822  }
823  if (isSingleModifier(origKey)) {
824  inputState->totallyReleased_ = true;
825  }
826  }
827 
828  if (inputState->pendingGroupIndex_ &&
829  inputState->totallyReleased_) {
830  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
831  if (inputState->imChanged_) {
832  inputState->imChanged_->ignore();
833  }
834  d->acceptGroupChange(lastKeyPressed, ic);
835  inputState->lastKeyPressed_ = Key();
836  }
837 
838  if (!keyEvent.filtered() && !keyEvent.isRelease()) {
839  int idx = 0;
840  for (auto &keyHandler : keyHandlers) {
841  auto keyIdx = origKey.keyListIndex(keyHandler.list);
842  if (keyIdx >= 0 && keyHandler.check()) {
843  inputState->keyReleased_ = idx;
844  inputState->lastKeyPressed_ = origKey;
845  if (isModifier) {
846  inputState->lastKeyPressedTime_ =
847  now(CLOCK_MONOTONIC);
848  // don't forward to input method, but make it pass
849  // through to client.
850  keyEvent.filter();
851  return;
852  }
853  keyHandler.trigger(inputState->totallyReleased_);
854  if (origKey.hasModifier()) {
855  inputState->totallyReleased_ = false;
856  }
857  keyEvent.filterAndAccept();
858  return;
859  }
860  idx++;
861  }
862  }
863  }));
864  d->eventWatchers_.emplace_back(watchEvent(
865  EventType::InputContextKeyEvent, EventWatcherPhase::PreInputMethod,
866  [d](Event &event) {
867  auto &keyEvent = static_cast<KeyEvent &>(event);
868  auto *ic = keyEvent.inputContext();
869  if (!keyEvent.isRelease() &&
870  keyEvent.key().checkKeyList(
871  d->globalConfig_.togglePreeditKeys())) {
872  // Clear client preedit on disable.
873  ic->reset();
874  ic->setEnablePreedit(!ic->isPreeditEnabled());
875  if (d->notifications_) {
876  d->notifications_->call<INotifications::showTip>(
877  "toggle-preedit", _("Input Method"), "", _("Preedit"),
878  ic->isPreeditEnabled() ? _("Preedit enabled")
879  : _("Preedit disabled"),
880  3000);
881  }
882  keyEvent.filterAndAccept();
883  }
884  }));
885  d->eventWatchers_.emplace_back(d->watchEvent(
886  EventType::InputContextKeyEvent, EventWatcherPhase::ReservedFirst,
887  [d](Event &event) {
888  // Update auto save.
889  d->idleStartTimestamp_ = now(CLOCK_MONOTONIC);
890  auto &keyEvent = static_cast<KeyEvent &>(event);
891  auto *ic = keyEvent.inputContext();
892  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
893 #ifdef ENABLE_KEYBOARD
894  auto *xkbState = inputState->customXkbState();
895  if (xkbState) {
896  if (auto *mods = findValue(d->stateMask_, ic->display())) {
897  FCITX_KEYTRACE() << "Update mask to customXkbState";
898  // Keep latched, but propagate depressed optionally and
899  // locked.
900  uint32_t depressed;
901  if (inputState->isModsAllReleased()) {
902  depressed = xkb_state_serialize_mods(
903  xkbState, XKB_STATE_MODS_DEPRESSED);
904  } else {
905  depressed = std::get<0>(*mods);
906  }
907  if (std::get<0>(*mods) == 0) {
908  inputState->setModsAllReleased();
909  }
910  auto latched = xkb_state_serialize_mods(
911  xkbState, XKB_STATE_MODS_LATCHED);
912  auto locked = std::get<2>(*mods);
913 
914  // set modifiers in depressed if they don't appear in any of
915  // the final masks
916  // depressed |= ~(depressed | latched | locked);
917  FCITX_DEBUG()
918  << depressed << " " << latched << " " << locked;
919  xkb_state_update_mask(xkbState, depressed, latched, locked,
920  0, 0, 0);
921  }
922  const uint32_t effective = xkb_state_serialize_mods(
923  xkbState, XKB_STATE_MODS_EFFECTIVE);
924  auto newSym = xkb_state_key_get_one_sym(
925  xkbState, keyEvent.rawKey().code());
926  auto newModifier = KeyStates(effective);
927  auto *keymap = xkb_state_get_keymap(xkbState);
928  if (keyEvent.rawKey().states().test(KeyState::Repeat) &&
929  xkb_keymap_key_repeats(keymap, keyEvent.rawKey().code())) {
930  newModifier |= KeyState::Repeat;
931  }
932 
933  const uint32_t modsDepressed = xkb_state_serialize_mods(
934  xkbState, XKB_STATE_MODS_DEPRESSED);
935  const uint32_t modsLatched =
936  xkb_state_serialize_mods(xkbState, XKB_STATE_MODS_LATCHED);
937  const uint32_t modsLocked =
938  xkb_state_serialize_mods(xkbState, XKB_STATE_MODS_LOCKED);
939  FCITX_KEYTRACE() << "Current mods: " << modsDepressed << " "
940  << modsLatched << " " << modsLocked;
941  auto newCode = keyEvent.rawKey().code();
942  Key key(static_cast<KeySym>(newSym), newModifier, newCode);
943  FCITX_KEYTRACE()
944  << "Custom Xkb translated Key: " << key.toString();
945  keyEvent.setRawKey(key);
946  }
947 #endif
948  FCITX_KEYTRACE() << "KeyEvent: " << keyEvent.key()
949  << " rawKey: " << keyEvent.rawKey()
950  << " origKey: " << keyEvent.origKey()
951  << " Release:" << keyEvent.isRelease()
952  << " keycode: " << keyEvent.origKey().code()
953  << " program: " << ic->program();
954 
955  if (keyEvent.isRelease()) {
956  return;
957  }
958  inputState->hideInputMethodInfo();
959  }));
960  d->eventWatchers_.emplace_back(
962  EventWatcherPhase::InputMethod, [this](Event &event) {
963  auto &keyEvent = static_cast<KeyEvent &>(event);
964  auto *ic = keyEvent.inputContext();
965  auto *engine = inputMethodEngine(ic);
966  const auto *entry = inputMethodEntry(ic);
967  if (!engine || !entry) {
968  return;
969  }
970  engine->keyEvent(*entry, keyEvent);
971  }));
972  d->eventWatchers_.emplace_back(watchEvent(
973  EventType::InputContextVirtualKeyboardEvent,
974  EventWatcherPhase::InputMethod, [this](Event &event) {
975  auto &keyEvent = static_cast<VirtualKeyboardEvent &>(event);
976  auto *ic = keyEvent.inputContext();
977  auto *engine = inputMethodEngine(ic);
978  const auto *entry = inputMethodEntry(ic);
979  if (!engine || !entry) {
980  return;
981  }
982  engine->virtualKeyboardEvent(*entry, keyEvent);
983  }));
984  d->eventWatchers_.emplace_back(watchEvent(
985  EventType::InputContextInvokeAction, EventWatcherPhase::InputMethod,
986  [this](Event &event) {
987  auto &invokeActionEvent = static_cast<InvokeActionEvent &>(event);
988  auto *ic = invokeActionEvent.inputContext();
989  auto *engine = inputMethodEngine(ic);
990  const auto *entry = inputMethodEntry(ic);
991  if (!engine || !entry) {
992  return;
993  }
994  engine->invokeAction(*entry, invokeActionEvent);
995  }));
996  d->eventWatchers_.emplace_back(d->watchEvent(
997  EventType::InputContextKeyEvent, EventWatcherPhase::ReservedLast,
998  [this](Event &event) {
999  auto &keyEvent = static_cast<KeyEvent &>(event);
1000  auto *ic = keyEvent.inputContext();
1001  auto *engine = inputMethodEngine(ic);
1002  const auto *entry = inputMethodEntry(ic);
1003  if (!engine || !entry) {
1004  return;
1005  }
1006  engine->filterKey(*entry, keyEvent);
1007  emit<Instance::KeyEventResult>(keyEvent);
1008 #ifdef ENABLE_KEYBOARD
1009  if (keyEvent.forward()) {
1010  FCITX_D();
1011  // Always let the release key go through, since it shouldn't
1012  // produce character. Otherwise it may wrongly trigger wayland
1013  // client side repetition.
1014  if (keyEvent.isRelease()) {
1015  keyEvent.filter();
1016  return;
1017  }
1018  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1019  if (auto *xkbState = inputState->customXkbState()) {
1020  if (auto utf32 = xkb_state_key_get_utf32(
1021  xkbState, keyEvent.key().code())) {
1022  // Ignore newline, return, backspace, tab, and delete.
1023  if (utf32 == '\n' || utf32 == '\b' || utf32 == '\r' ||
1024  utf32 == '\t' || utf32 == '\033' ||
1025  utf32 == '\x7f') {
1026  return;
1027  }
1028  if (keyEvent.key().states().testAny(
1029  KeyStates{KeyState::Ctrl, KeyState::Alt}) ||
1030  keyEvent.rawKey().sym() ==
1031  keyEvent.origKey().sym()) {
1032  return;
1033  }
1034  FCITX_KEYTRACE() << "Will commit char: " << utf32;
1035  ic->commitString(utf8::UCS4ToUTF8(utf32));
1036  keyEvent.filterAndAccept();
1037  } else if (!keyEvent.key().states().testAny(
1038  KeyStates{KeyState::Ctrl, KeyState::Alt}) &&
1039  keyEvent.rawKey().sym() !=
1040  keyEvent.origKey().sym() &&
1041  Key::keySymToUnicode(keyEvent.origKey().sym()) !=
1042  0) {
1043  // filter key for the case that: origKey will produce
1044  // character, while the translated will not.
1045  keyEvent.filterAndAccept();
1046  }
1047  }
1048  }
1049 #endif
1050  }));
1051  d->eventWatchers_.emplace_back(d->watchEvent(
1052  EventType::InputContextFocusIn, EventWatcherPhase::ReservedFirst,
1053  [this, d](Event &event) {
1054  auto &icEvent = static_cast<InputContextEvent &>(event);
1055  auto isSameProgram = [&icEvent, d]() {
1056  // Check if they are same IC, or they are same program.
1057  return (icEvent.inputContext() == d->lastUnFocusedIc_.get()) ||
1058  (!icEvent.inputContext()->program().empty() &&
1059  (icEvent.inputContext()->program() ==
1060  d->lastUnFocusedProgram_));
1061  };
1062 
1063  if (d->globalConfig_.resetStateWhenFocusIn() ==
1064  PropertyPropagatePolicy::All ||
1065  (d->globalConfig_.resetStateWhenFocusIn() ==
1066  PropertyPropagatePolicy::Program &&
1067  !isSameProgram())) {
1068  if (d->globalConfig_.activeByDefault()) {
1069  activate(icEvent.inputContext());
1070  } else {
1071  deactivate(icEvent.inputContext());
1072  }
1073  }
1074 
1075  activateInputMethod(icEvent);
1076 
1077  auto *inputContext = icEvent.inputContext();
1078  if (!inputContext->clientControlVirtualkeyboardShow()) {
1079  inputContext->showVirtualKeyboard();
1080  }
1081 
1082  if (!d->globalConfig_.showInputMethodInformationWhenFocusIn() ||
1083  icEvent.inputContext()->capabilityFlags().test(
1085  return;
1086  }
1087  // Give some time because the cursor location may need some time
1088  // to be updated.
1089  d->focusInImInfoTimer_ = d->eventLoop_.addTimeEvent(
1090  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 30000, 0,
1091  [d, icRef = icEvent.inputContext()->watch()](EventSourceTime *,
1092  uint64_t) {
1093  // Check if ic is still valid and has focus.
1094  if (auto *ic = icRef.get(); ic && ic->hasFocus()) {
1095  d->showInputMethodInformation(ic);
1096  }
1097  return true;
1098  });
1099  }));
1100  d->eventWatchers_.emplace_back(d->watchEvent(
1101  EventType::InputContextFocusOut, EventWatcherPhase::ReservedFirst,
1102  [d](Event &event) {
1103  auto &icEvent = static_cast<InputContextEvent &>(event);
1104  auto *ic = icEvent.inputContext();
1105  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1106  inputState->reset();
1107  if (!ic->capabilityFlags().test(
1108  CapabilityFlag::ClientUnfocusCommit)) {
1109  // do server side commit
1110  auto commit =
1111  ic->inputPanel().clientPreedit().toStringForCommit();
1112  if (!commit.empty()) {
1113  ic->commitString(commit);
1114  }
1115  }
1116  }));
1117  d->eventWatchers_.emplace_back(d->watchEvent(
1118  EventType::InputContextFocusOut, EventWatcherPhase::InputMethod,
1119  [this, d](Event &event) {
1120  auto &icEvent = static_cast<InputContextEvent &>(event);
1121  d->lastUnFocusedProgram_ = icEvent.inputContext()->program();
1122  d->lastUnFocusedIc_ = icEvent.inputContext()->watch();
1123  deactivateInputMethod(icEvent);
1124 
1125  auto *inputContext = icEvent.inputContext();
1126  if (!inputContext->clientControlVirtualkeyboardHide()) {
1127  inputContext->hideVirtualKeyboard();
1128  }
1129  }));
1130  d->eventWatchers_.emplace_back(d->watchEvent(
1131  EventType::InputContextReset, EventWatcherPhase::ReservedFirst,
1132  [d](Event &event) {
1133  auto &icEvent = static_cast<InputContextEvent &>(event);
1134  auto *ic = icEvent.inputContext();
1135  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1136  inputState->reset();
1137  }));
1138  d->eventWatchers_.emplace_back(
1139  watchEvent(EventType::InputContextReset, EventWatcherPhase::InputMethod,
1140  [this](Event &event) {
1141  auto &icEvent = static_cast<InputContextEvent &>(event);
1142  auto *ic = icEvent.inputContext();
1143  if (!ic->hasFocus()) {
1144  return;
1145  }
1146  auto *engine = inputMethodEngine(ic);
1147  const auto *entry = inputMethodEntry(ic);
1148  if (!engine || !entry) {
1149  return;
1150  }
1151  engine->reset(*entry, icEvent);
1152  }));
1153  d->eventWatchers_.emplace_back(d->watchEvent(
1155  EventWatcherPhase::ReservedFirst, [this](Event &event) {
1156  auto &icEvent =
1157  static_cast<InputContextSwitchInputMethodEvent &>(event);
1158  auto *ic = icEvent.inputContext();
1159  if (!ic->hasFocus()) {
1160  return;
1161  }
1162  deactivateInputMethod(icEvent);
1163  activateInputMethod(icEvent);
1164  }));
1165  d->eventWatchers_.emplace_back(d->watchEvent(
1167  EventWatcherPhase::ReservedLast, [this, d](Event &event) {
1168  auto &icEvent =
1169  static_cast<InputContextSwitchInputMethodEvent &>(event);
1170  auto *ic = icEvent.inputContext();
1171  if (!ic->hasFocus()) {
1172  return;
1173  }
1174 
1175  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1176  inputState->lastIMChangeIsAltTrigger_ =
1177  icEvent.reason() == InputMethodSwitchedReason::AltTrigger;
1178 
1179  if ((icEvent.reason() != InputMethodSwitchedReason::Trigger &&
1180  icEvent.reason() != InputMethodSwitchedReason::AltTrigger &&
1181  icEvent.reason() != InputMethodSwitchedReason::Enumerate &&
1182  icEvent.reason() != InputMethodSwitchedReason::Activate &&
1183  icEvent.reason() != InputMethodSwitchedReason::Other &&
1184  icEvent.reason() != InputMethodSwitchedReason::GroupChange &&
1185  icEvent.reason() != InputMethodSwitchedReason::Deactivate)) {
1186  return;
1187  }
1188  showInputMethodInformation(ic);
1189  }));
1190  d->eventWatchers_.emplace_back(
1191  d->watchEvent(EventType::InputMethodGroupChanged,
1192  EventWatcherPhase::ReservedLast, [this, d](Event &) {
1193  // Use a timer here. so we can get focus back to real
1194  // window.
1195  d->imGroupInfoTimer_ = d->eventLoop_.addTimeEvent(
1196  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 30000, 0,
1197  [this](EventSourceTime *, uint64_t) {
1198  inputContextManager().foreachFocused(
1199  [this](InputContext *ic) {
1200  showInputMethodInformation(ic);
1201  return true;
1202  });
1203  return true;
1204  });
1205  }));
1206 
1207  d->eventWatchers_.emplace_back(d->watchEvent(
1208  EventType::InputContextUpdateUI, EventWatcherPhase::ReservedFirst,
1209  [d](Event &event) {
1210  auto &icEvent = static_cast<InputContextUpdateUIEvent &>(event);
1211  if (icEvent.immediate()) {
1212  d->uiManager_.update(icEvent.component(),
1213  icEvent.inputContext());
1214  d->uiManager_.flush();
1215  } else {
1216  d->uiManager_.update(icEvent.component(),
1217  icEvent.inputContext());
1218  d->uiUpdateEvent_->setOneShot();
1219  }
1220  }));
1221  d->eventWatchers_.emplace_back(d->watchEvent(
1222  EventType::InputContextDestroyed, EventWatcherPhase::ReservedFirst,
1223  [d](Event &event) {
1224  auto &icEvent = static_cast<InputContextEvent &>(event);
1225  d->uiManager_.expire(icEvent.inputContext());
1226  }));
1227  d->eventWatchers_.emplace_back(d->watchEvent(
1228  EventType::InputMethodModeChanged, EventWatcherPhase::ReservedFirst,
1229  [d](Event &) { d->uiManager_.updateAvailability(); }));
1230  d->uiUpdateEvent_ = d->eventLoop_.addDeferEvent([d](EventSource *) {
1231  d->uiManager_.flush();
1232  return true;
1233  });
1234  d->uiUpdateEvent_->setEnabled(false);
1235  d->periodicalSave_ = d->eventLoop_.addTimeEvent(
1236  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 1000000, AutoSaveIdleTime,
1237  [this, d](EventSourceTime *time, uint64_t) {
1238  if (exiting()) {
1239  return true;
1240  }
1241 
1242  // Check if the idle time is long enough.
1243  auto currentTime = now(CLOCK_MONOTONIC);
1244  if (currentTime <= d->idleStartTimestamp_ ||
1245  currentTime - d->idleStartTimestamp_ < AutoSaveIdleTime) {
1246  // IF not idle, shorten the next checking period.
1247  time->setNextInterval(2 * AutoSaveIdleTime);
1248  time->setOneShot();
1249  return true;
1250  }
1251 
1252  FCITX_INFO() << "Running autosave...";
1253  save();
1254  FCITX_INFO() << "End autosave";
1255  if (d->globalConfig_.autoSavePeriod() > 0) {
1256  time->setNextInterval(d->globalConfig_.autoSavePeriod() *
1257  AutoSaveMinInUsecs);
1258  time->setOneShot();
1259  }
1260  return true;
1261  });
1262  d->periodicalSave_->setEnabled(false);
1263 }
1264 
1265 Instance::~Instance() {
1266  FCITX_D();
1267  d->icManager_.finalize();
1268  d->addonManager_.unload();
1269  d->notifications_ = nullptr;
1270  d->icManager_.setInstance(nullptr);
1271 }
1272 
1273 void InstanceArgument::parseOption(int argc, char **argv) {
1274  if (argc >= 1) {
1275  argv0 = argv[0];
1276  } else {
1277  argv0 = "fcitx5";
1278  }
1279  struct option longOptions[] = {{"enable", required_argument, nullptr, 0},
1280  {"disable", required_argument, nullptr, 0},
1281  {"verbose", required_argument, nullptr, 0},
1282  {"keep", no_argument, nullptr, 'k'},
1283  {"ui", required_argument, nullptr, 'u'},
1284  {"replace", no_argument, nullptr, 'r'},
1285  {"version", no_argument, nullptr, 'v'},
1286  {"help", no_argument, nullptr, 'h'},
1287  {"option", required_argument, nullptr, 'o'},
1288  {nullptr, 0, 0, 0}};
1289 
1290  int optionIndex = 0;
1291  int c;
1292  std::string addonOptionString;
1293  while ((c = getopt_long(argc, argv, "ru:dDs:hvo:k", longOptions,
1294  &optionIndex)) != EOF) {
1295  switch (c) {
1296  case 0: {
1297  switch (optionIndex) {
1298  case 0:
1299  enableList = stringutils::split(optarg, ",");
1300  break;
1301  case 1:
1302  disableList = stringutils::split(optarg, ",");
1303  break;
1304  case 2:
1305  Log::setLogRule(optarg);
1306  break;
1307  default:
1308  quietQuit = true;
1309  printUsage();
1310  break;
1311  }
1312  } break;
1313  case 'r':
1314  tryReplace = true;
1315  break;
1316  case 'u':
1317  uiName = optarg;
1318  break;
1319  case 'd':
1320  runAsDaemon = true;
1321  break;
1322  case 'D':
1323  runAsDaemon = false;
1324  break;
1325  case 'k':
1326  exitWhenMainDisplayDisconnected = false;
1327  break;
1328  case 's':
1329  overrideDelay = std::atoi(optarg);
1330  break;
1331  case 'h':
1332  quietQuit = true;
1333  printUsage();
1334  break;
1335  case 'v':
1336  quietQuit = true;
1337  printVersion();
1338  break;
1339  case 'o':
1340  addonOptionString = optarg;
1341  break;
1342  default:
1343  quietQuit = true;
1344  printUsage();
1345  }
1346  if (quietQuit) {
1347  break;
1348  }
1349  }
1350 
1351  std::unordered_map<std::string, std::vector<std::string>> addonOptions;
1352  for (const std::string_view item :
1353  stringutils::split(addonOptionString, ",")) {
1354  auto tokens = stringutils::split(item, "=");
1355  if (tokens.size() != 2) {
1356  continue;
1357  }
1358  addonOptions[tokens[0]] = stringutils::split(tokens[1], ":");
1359  }
1360  addonOptions_ = std::move(addonOptions);
1361 }
1362 
1364 #ifdef _WIN32
1365  FCITX_UNUSED(fd);
1366 #else
1367  FCITX_D();
1368  d->signalPipe_ = fd;
1369  d->signalPipeEvent_ = d->eventLoop_.addIOEvent(
1370  fd, IOEventFlag::In, [this](EventSource *, int, IOEventFlags) {
1371  handleSignal();
1372  return true;
1373  });
1374 #endif
1375 }
1376 
1378  FCITX_D();
1379  return d->arg_.tryReplace;
1380 }
1381 
1383  FCITX_D();
1384  return d->arg_.exitWhenMainDisplayDisconnected;
1385 }
1386 
1387 bool Instance::exiting() const {
1388  FCITX_D();
1389  return d->exit_;
1390 }
1391 
1392 void Instance::handleSignal() {
1393 #ifndef _WIN32
1394  FCITX_D();
1395  uint8_t signo = 0;
1396  while (fs::safeRead(d->signalPipe_, &signo, sizeof(signo)) > 0) {
1397  if (signo == SIGINT || signo == SIGTERM || signo == SIGQUIT ||
1398  signo == SIGXCPU) {
1399  exit();
1400  } else if (signo == SIGUSR1) {
1401  reloadConfig();
1402  } else if (signo == SIGCHLD) {
1403  d->zombieReaper_->setNextInterval(2000000);
1404  d->zombieReaper_->setOneShot();
1405  }
1406  }
1407 #endif
1408 }
1409 
1411  FCITX_D();
1412  if (!d->arg_.uiName.empty()) {
1413  d->arg_.enableList.push_back(d->arg_.uiName);
1414  }
1415  reloadConfig();
1416  d->icManager_.registerProperty("inputState", &d->inputStateFactory_);
1417  std::unordered_set<std::string> enabled;
1418  std::unordered_set<std::string> disabled;
1419  std::tie(enabled, disabled) = d->overrideAddons();
1420  FCITX_INFO() << "Override Enabled Addons: " << enabled;
1421  FCITX_INFO() << "Override Disabled Addons: " << disabled;
1422  d->addonManager_.load(enabled, disabled);
1423  if (d->exit_) {
1424  return;
1425  }
1426  d->imManager_.load([d](InputMethodManager &) { d->buildDefaultGroup(); });
1427  d->uiManager_.load(d->arg_.uiName);
1428 
1429  const auto *entry = d->imManager_.entry("keyboard-us");
1430  FCITX_LOG_IF(Error, !entry) << "Couldn't find keyboard-us";
1431  d->preloadInputMethodEvent_ = d->eventLoop_.addTimeEvent(
1432  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 1000000, 0,
1433  [this](EventSourceTime *, uint64_t) {
1434  FCITX_D();
1435  if (d->exit_ || !d->globalConfig_.preloadInputMethod()) {
1436  return false;
1437  }
1438  // Preload first input method.
1439  if (!d->imManager_.currentGroup().inputMethodList().empty()) {
1440  if (const auto *entry =
1441  d->imManager_.entry(d->imManager_.currentGroup()
1442  .inputMethodList()[0]
1443  .name())) {
1444  d->addonManager_.addon(entry->addon(), true);
1445  }
1446  }
1447  // Preload default input method.
1448  if (!d->imManager_.currentGroup().defaultInputMethod().empty()) {
1449  if (const auto *entry = d->imManager_.entry(
1450  d->imManager_.currentGroup().defaultInputMethod())) {
1451  d->addonManager_.addon(entry->addon(), true);
1452  }
1453  }
1454  return false;
1455  });
1456 #ifndef _WIN32
1457  d->zombieReaper_ = d->eventLoop_.addTimeEvent(
1458  CLOCK_MONOTONIC, now(CLOCK_MONOTONIC), 0,
1459  [](EventSourceTime *, uint64_t) {
1460  pid_t res;
1461  while ((res = waitpid(-1, nullptr, WNOHANG)) > 0) {
1462  }
1463  return false;
1464  });
1465  d->zombieReaper_->setEnabled(false);
1466 #endif
1467 
1468  d->exitEvent_ = d->eventLoop_.addExitEvent([this](EventSource *) {
1469  FCITX_DEBUG() << "Running save...";
1470  save();
1471  return false;
1472  });
1473  d->notifications_ = d->addonManager_.addon("notifications", true);
1474 }
1475 
1477  FCITX_D();
1478  if (d->arg_.quietQuit) {
1479  return 0;
1480  }
1481  d->exit_ = false;
1482  d->exitCode_ = 0;
1483  initialize();
1484  if (d->exit_) {
1485  return d->exitCode_;
1486  }
1487  d->running_ = true;
1488  auto r = eventLoop().exec();
1489  d->running_ = false;
1490 
1491  return r ? d->exitCode_ : 1;
1492 }
1493 
1494 void Instance::setRunning(bool running) {
1495  FCITX_D();
1496  d->running_ = running;
1497 }
1498 
1499 bool Instance::isRunning() const {
1500  FCITX_D();
1501  return d->running_;
1502 }
1503 
1504 InputMethodMode Instance::inputMethodMode() const {
1505  FCITX_D();
1506  return d->inputMethodMode_;
1507 }
1508 
1509 void Instance::setInputMethodMode(InputMethodMode mode) {
1510  FCITX_D();
1511  if (d->inputMethodMode_ == mode) {
1512  return;
1513  }
1514  d->inputMethodMode_ = mode;
1515  postEvent(InputMethodModeChangedEvent());
1516 }
1517 
1519  FCITX_D();
1520  return d->restart_;
1521 }
1522 
1523 bool Instance::virtualKeyboardAutoShow() const {
1524  FCITX_D();
1525  return d->virtualKeyboardAutoShow_;
1526 }
1527 
1528 void Instance::setVirtualKeyboardAutoShow(bool autoShow) {
1529  FCITX_D();
1530  d->virtualKeyboardAutoShow_ = autoShow;
1531 }
1532 
1533 bool Instance::virtualKeyboardAutoHide() const {
1534  FCITX_D();
1535  return d->virtualKeyboardAutoHide_;
1536 }
1537 
1538 void Instance::setVirtualKeyboardAutoHide(bool autoHide) {
1539  FCITX_D();
1540  d->virtualKeyboardAutoHide_ = autoHide;
1541 }
1542 
1543 VirtualKeyboardFunctionMode Instance::virtualKeyboardFunctionMode() const {
1544  FCITX_D();
1545  return d->virtualKeyboardFunctionMode_;
1546 }
1547 
1548 void Instance::setVirtualKeyboardFunctionMode(
1549  VirtualKeyboardFunctionMode mode) {
1550  FCITX_D();
1551  d->virtualKeyboardFunctionMode_ = mode;
1552 }
1553 
1555  FCITX_D();
1556  d->binaryMode_ = true;
1557 }
1558 
1559 bool Instance::canRestart() const {
1560  FCITX_D();
1561  const auto &addonNames = d->addonManager_.loadedAddonNames();
1562  return d->binaryMode_ &&
1563  std::all_of(addonNames.begin(), addonNames.end(),
1564  [d](const std::string &name) {
1565  auto *addon = d->addonManager_.lookupAddon(name);
1566  if (!addon) {
1567  return true;
1568  }
1569  return addon->canRestart();
1570  });
1571 }
1572 
1573 InstancePrivate *Instance::privateData() {
1574  FCITX_D();
1575  return d;
1576 }
1577 
1579  FCITX_D();
1580  return d->eventLoop_;
1581 }
1582 
1584  FCITX_D();
1585  return d->eventDispatcher_;
1586 }
1587 
1589  FCITX_D();
1590  return d->icManager_;
1591 }
1592 
1594  FCITX_D();
1595  return d->addonManager_;
1596 }
1597 
1599  FCITX_D();
1600  return d->imManager_;
1601 }
1602 
1604  FCITX_D();
1605  return d->imManager_;
1606 }
1607 
1609  FCITX_D();
1610  return d->uiManager_;
1611 }
1612 
1614  FCITX_D();
1615  return d->globalConfig_;
1616 }
1617 
1618 bool Instance::postEvent(Event &event) {
1619  return std::as_const(*this).postEvent(event);
1620 }
1621 
1622 bool Instance::postEvent(Event &event) const {
1623  FCITX_D();
1624  if (d->exit_) {
1625  return false;
1626  }
1627  auto iter = d->eventHandlers_.find(event.type());
1628  if (iter != d->eventHandlers_.end()) {
1629  const auto &handlers = iter->second;
1630  EventWatcherPhase phaseOrder[] = {
1631  EventWatcherPhase::ReservedFirst, EventWatcherPhase::PreInputMethod,
1632  EventWatcherPhase::InputMethod, EventWatcherPhase::PostInputMethod,
1633  EventWatcherPhase::ReservedLast};
1634 
1635  for (auto phase : phaseOrder) {
1636  if (auto iter2 = handlers.find(phase); iter2 != handlers.end()) {
1637  for (auto &handler : iter2->second.view()) {
1638  handler(event);
1639  if (event.filtered()) {
1640  break;
1641  }
1642  }
1643  }
1644  if (event.filtered()) {
1645  break;
1646  }
1647  }
1648 
1649  // Make sure this part of fix is always executed regardless of the
1650  // filter.
1651  if (event.type() == EventType::InputContextKeyEvent) {
1652  auto &keyEvent = static_cast<KeyEvent &>(event);
1653  auto *ic = keyEvent.inputContext();
1654 #ifdef ENABLE_KEYBOARD
1655  do {
1656  if (!keyEvent.forward() && !keyEvent.origKey().code()) {
1657  break;
1658  }
1659  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1660  auto *xkbState = inputState->customXkbState();
1661  if (!xkbState) {
1662  break;
1663  }
1664  // This need to be called after xkb_state_key_get_*, and should
1665  // be called against all Key regardless whether they are
1666  // filtered or not.
1667  xkb_state_update_key(xkbState, keyEvent.origKey().code(),
1668  keyEvent.isRelease() ? XKB_KEY_UP
1669  : XKB_KEY_DOWN);
1670  } while (0);
1671 #endif
1672  if (ic->capabilityFlags().test(CapabilityFlag::KeyEventOrderFix) &&
1673  !keyEvent.accepted() && ic->hasPendingEventsStrictOrder()) {
1674  // Re-forward the event to ensure we got delivered later than
1675  // commit.
1676  keyEvent.filterAndAccept();
1677  ic->forwardKey(keyEvent.origKey(), keyEvent.isRelease(),
1678  keyEvent.time());
1679  }
1680  d_ptr->uiManager_.flush();
1681  }
1682  }
1683  return event.accepted();
1684 }
1685 
1686 std::unique_ptr<HandlerTableEntry<EventHandler>>
1687 Instance::watchEvent(EventType type, EventWatcherPhase phase,
1688  EventHandler callback) {
1689  FCITX_D();
1690  if (phase == EventWatcherPhase::ReservedFirst ||
1691  phase == EventWatcherPhase::ReservedLast) {
1692  throw std::invalid_argument("Reserved Phase is only for internal use");
1693  }
1694  return d->watchEvent(type, phase, std::move(callback));
1695 }
1696 
1697 bool groupContains(const InputMethodGroup &group, const std::string &name) {
1698  const auto &list = group.inputMethodList();
1699  auto iter = std::find_if(list.begin(), list.end(),
1700  [&name](const InputMethodGroupItem &item) {
1701  return item.name() == name;
1702  });
1703  return iter != list.end();
1704 }
1705 
1707  FCITX_D();
1708  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
1709  // Small hack to make sure when InputMethodEngine::deactivate is called,
1710  // current im is the right one.
1711  if (!inputState->overrideDeactivateIM_.empty()) {
1712  return inputState->overrideDeactivateIM_;
1713  }
1714 
1715  const auto &group = d->imManager_.currentGroup();
1716  if (ic->capabilityFlags().test(CapabilityFlag::Disable) ||
1717  (ic->capabilityFlags().test(CapabilityFlag::Password) &&
1718  !d->globalConfig_.allowInputMethodForPassword())) {
1719  auto defaultLayoutIM =
1720  stringutils::concat("keyboard-", group.defaultLayout());
1721  const auto *entry = d->imManager_.entry(defaultLayoutIM);
1722  if (!entry) {
1723  entry = d->imManager_.entry("keyboard-us");
1724  }
1725  return entry ? entry->uniqueName() : "";
1726  }
1727 
1728  if (group.inputMethodList().empty()) {
1729  return "";
1730  }
1731  if (inputState->isActive()) {
1732  if (!inputState->localIM_.empty() &&
1733  groupContains(group, inputState->localIM_)) {
1734  return inputState->localIM_;
1735  }
1736  return group.defaultInputMethod();
1737  }
1738 
1739  return group.inputMethodList()[0].name();
1740 }
1741 
1743  FCITX_D();
1744  auto imName = inputMethod(ic);
1745  if (imName.empty()) {
1746  return nullptr;
1747  }
1748  return d->imManager_.entry(imName);
1749 }
1750 
1752  FCITX_D();
1753  const auto *entry = inputMethodEntry(ic);
1754  if (!entry) {
1755  return nullptr;
1756  }
1757  return static_cast<InputMethodEngine *>(
1758  d->addonManager_.addon(entry->addon(), true));
1759 }
1760 
1762  FCITX_D();
1763  const auto *entry = d->imManager_.entry(name);
1764  if (!entry) {
1765  return nullptr;
1766  }
1767  return static_cast<InputMethodEngine *>(
1768  d->addonManager_.addon(entry->addon(), true));
1769 }
1770 
1772  std::string icon;
1773  const auto *entry = inputMethodEntry(ic);
1774  if (entry) {
1775  auto *engine = inputMethodEngine(ic);
1776  if (engine) {
1777  icon = engine->subModeIcon(*entry, *ic);
1778  }
1779  if (icon.empty()) {
1780  icon = entry->icon();
1781  }
1782  } else {
1783  icon = "input-keyboard";
1784  }
1785  return icon;
1786 }
1787 
1789  std::string label;
1790 
1791  const auto *entry = inputMethodEntry(ic);
1792  auto *engine = inputMethodEngine(ic);
1793 
1794  if (engine && entry) {
1795  label = engine->subModeLabel(*entry, *ic);
1796  }
1797  if (label.empty() && entry) {
1798  label = entry->label();
1799  }
1800  return label;
1801 }
1802 
1803 uint32_t Instance::processCompose(InputContext *ic, KeySym keysym) {
1804 #ifdef ENABLE_KEYBOARD
1805  FCITX_D();
1806  auto *state = ic->propertyFor(&d->inputStateFactory_);
1807 
1808  auto *xkbComposeState = state->xkbComposeState();
1809  if (!xkbComposeState) {
1810  return 0;
1811  }
1812 
1813  auto keyval = static_cast<xkb_keysym_t>(keysym);
1814 
1815  enum xkb_compose_feed_result result =
1816  xkb_compose_state_feed(xkbComposeState, keyval);
1817  if (result == XKB_COMPOSE_FEED_IGNORED) {
1818  return 0;
1819  }
1820 
1821  enum xkb_compose_status status =
1822  xkb_compose_state_get_status(xkbComposeState);
1823  if (status == XKB_COMPOSE_NOTHING) {
1824  return 0;
1825  }
1826  if (status == XKB_COMPOSE_COMPOSED) {
1827  char buffer[FCITX_UTF8_MAX_LENGTH + 1] = {'\0', '\0', '\0', '\0',
1828  '\0', '\0', '\0'};
1829  int length =
1830  xkb_compose_state_get_utf8(xkbComposeState, buffer, sizeof(buffer));
1831  xkb_compose_state_reset(xkbComposeState);
1832  if (length == 0) {
1833  return FCITX_INVALID_COMPOSE_RESULT;
1834  }
1835 
1836  uint32_t c = utf8::getChar(buffer);
1837  return utf8::isValidChar(c) ? c : 0;
1838  }
1839  if (status == XKB_COMPOSE_CANCELLED) {
1840  xkb_compose_state_reset(xkbComposeState);
1841  }
1842 
1843  return FCITX_INVALID_COMPOSE_RESULT;
1844 #else
1845  FCITX_UNUSED(ic);
1846  FCITX_UNUSED(keysym);
1847  return 0;
1848 #endif
1849 }
1850 
1851 std::optional<std::string> Instance::processComposeString(InputContext *ic,
1852  KeySym keysym) {
1853 #ifdef ENABLE_KEYBOARD
1854  FCITX_D();
1855  auto *state = ic->propertyFor(&d->inputStateFactory_);
1856 
1857  auto *xkbComposeState = state->xkbComposeState();
1858  if (!xkbComposeState) {
1859  return std::string();
1860  }
1861 
1862  auto keyval = static_cast<xkb_keysym_t>(keysym);
1863  enum xkb_compose_feed_result result =
1864  xkb_compose_state_feed(xkbComposeState, keyval);
1865 
1866  if (result == XKB_COMPOSE_FEED_IGNORED) {
1867  return std::string();
1868  }
1869 
1870  enum xkb_compose_status status =
1871  xkb_compose_state_get_status(xkbComposeState);
1872  if (status == XKB_COMPOSE_NOTHING) {
1873  return std::string();
1874  }
1875  if (status == XKB_COMPOSE_COMPOSED) {
1876  // This may not be NUL-terminiated.
1877  std::array<char, 256> buffer;
1878  auto length = xkb_compose_state_get_utf8(xkbComposeState, buffer.data(),
1879  buffer.size());
1880  xkb_compose_state_reset(xkbComposeState);
1881  if (length == 0) {
1882  return std::nullopt;
1883  }
1884 
1885  auto bufferBegin = buffer.begin();
1886  auto bufferEnd = std::next(bufferBegin, length);
1887  if (utf8::validate(bufferBegin, bufferEnd)) {
1888  return std::string(bufferBegin, bufferEnd);
1889  }
1890  return std::nullopt;
1891  }
1892  if (status == XKB_COMPOSE_CANCELLED) {
1893  xkb_compose_state_reset(xkbComposeState);
1894  }
1895  return std::nullopt;
1896 #else
1897  FCITX_UNUSED(ic);
1898  FCITX_UNUSED(keysym);
1899  return std::string();
1900 #endif
1901 }
1902 
1904 #ifdef ENABLE_KEYBOARD
1905  FCITX_D();
1906  auto *state = inputContext->propertyFor(&d->inputStateFactory_);
1907 
1908  auto *xkbComposeState = state->xkbComposeState();
1909  if (!xkbComposeState) {
1910  return false;
1911  }
1912 
1913  return xkb_compose_state_get_status(xkbComposeState) ==
1914  XKB_COMPOSE_COMPOSING;
1915 #else
1916  FCITX_UNUSED(inputContext);
1917  return false;
1918 #endif
1919 }
1920 
1922 #ifdef ENABLE_KEYBOARD
1923  FCITX_D();
1924  auto *state = inputContext->propertyFor(&d->inputStateFactory_);
1925  auto *xkbComposeState = state->xkbComposeState();
1926  if (!xkbComposeState) {
1927  return;
1928  }
1929  xkb_compose_state_reset(xkbComposeState);
1930 #else
1931  FCITX_UNUSED(inputContext);
1932 #endif
1933 }
1934 
1936  FCITX_D();
1937  // Refresh timestamp for next auto save.
1938  d->idleStartTimestamp_ = now(CLOCK_MONOTONIC);
1939  d->imManager_.save();
1940  d->addonManager_.saveAll();
1941 }
1942 
1944  FCITX_D();
1945  if (auto *ic = mostRecentInputContext()) {
1946  CheckInputMethodChanged imChangedRAII(ic, d);
1947  activate(ic);
1948  }
1949 }
1950 
1951 std::string Instance::addonForInputMethod(const std::string &imName) {
1952 
1953  if (const auto *entry = inputMethodManager().entry(imName)) {
1954  return entry->uniqueName();
1955  }
1956  return {};
1957 }
1958 
1960  startProcess(
1961  {StandardPaths::fcitxPath("bindir", "fcitx5-configtool").string()});
1962 }
1963 
1964 void Instance::configureAddon(const std::string & /*unused*/) {}
1965 
1966 void Instance::configureInputMethod(const std::string & /*unused*/) {}
1967 
1969  if (auto *ic = mostRecentInputContext()) {
1970  if (const auto *entry = inputMethodEntry(ic)) {
1971  return entry->uniqueName();
1972  }
1973  }
1974  return {};
1975 }
1976 
1977 std::string Instance::currentUI() {
1978  FCITX_D();
1979  return d->uiManager_.currentUI();
1980 }
1981 
1983  FCITX_D();
1984  if (auto *ic = mostRecentInputContext()) {
1985  CheckInputMethodChanged imChangedRAII(ic, d);
1986  deactivate(ic);
1987  }
1988 }
1989 
1990 void Instance::exit() { exit(0); }
1991 
1992 void Instance::exit(int exitCode) {
1993  FCITX_D();
1994  d->exit_ = true;
1995  d->exitCode_ = exitCode;
1996  if (d->running_) {
1997  d->eventLoop_.exit();
1998  }
1999 }
2000 
2001 void Instance::reloadAddonConfig(const std::string &addonName) {
2002  auto *addon = addonManager().addon(addonName);
2003  if (addon) {
2004  addon->reloadConfig();
2005  }
2006 }
2007 
2009  FCITX_D();
2010  auto [enabled, disabled] = d->overrideAddons();
2011  d->addonManager_.load(enabled, disabled);
2012  d->imManager_.refresh();
2013 }
2014 
2016  FCITX_D();
2017  readAsIni(d->globalConfig_.config(), StandardPathsType::PkgConfig,
2018  "config");
2019  FCITX_DEBUG() << "Trigger Key: "
2020  << Key::keyListToString(d->globalConfig_.triggerKeys());
2021  d->icManager_.setPropertyPropagatePolicy(
2022  d->globalConfig_.shareInputState());
2023  if (d->globalConfig_.preeditEnabledByDefault() !=
2024  d->icManager_.isPreeditEnabledByDefault()) {
2025  d->icManager_.setPreeditEnabledByDefault(
2026  d->globalConfig_.preeditEnabledByDefault());
2027  d->icManager_.foreach([d](InputContext *ic) {
2028  ic->setEnablePreedit(d->globalConfig_.preeditEnabledByDefault());
2029  return true;
2030  });
2031  }
2032 #ifdef ENABLE_KEYBOARD
2033  d->keymapCache_.clear();
2034  if (d->inputStateFactory_.registered()) {
2035  d->icManager_.foreach([d](InputContext *ic) {
2036  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2037  inputState->resetXkbState();
2038  return true;
2039  });
2040  }
2041 #endif
2042  if (d->running_) {
2043  postEvent(GlobalConfigReloadedEvent());
2044  }
2045 
2046  if (d->globalConfig_.autoSavePeriod() <= 0) {
2047  d->periodicalSave_->setEnabled(false);
2048  } else {
2049  d->periodicalSave_->setNextInterval(AutoSaveMinInUsecs *
2050  d->globalConfig_.autoSavePeriod());
2051  d->periodicalSave_->setOneShot();
2052  }
2053 }
2054 
2056  FCITX_D();
2057  d->imManager_.reset([d](InputMethodManager &) { d->buildDefaultGroup(); });
2058 }
2059 
2061  FCITX_D();
2062  if (!canRestart()) {
2063  return;
2064  }
2065  d->restart_ = true;
2066  exit();
2067 }
2068 
2069 void Instance::setCurrentInputMethod(const std::string &name) {
2070  setCurrentInputMethod(mostRecentInputContext(), name, false);
2071 }
2072 
2073 void Instance::setCurrentInputMethod(InputContext *ic, const std::string &name,
2074  bool local) {
2075  FCITX_D();
2076  if (!canTrigger()) {
2077  return;
2078  }
2079 
2080  auto &imManager = inputMethodManager();
2081  const auto &imList = imManager.currentGroup().inputMethodList();
2082  auto iter = std::find_if(imList.begin(), imList.end(),
2083  [&name](const InputMethodGroupItem &item) {
2084  return item.name() == name;
2085  });
2086  if (iter == imList.end()) {
2087  return;
2088  }
2089 
2090  auto setGlobalDefaultInputMethod = [d](const std::string &name) {
2091  std::vector<std::unique_ptr<CheckInputMethodChanged>> groupRAIICheck;
2092  d->icManager_.foreachFocused([d, &groupRAIICheck](InputContext *ic) {
2093  assert(ic->hasFocus());
2094  groupRAIICheck.push_back(
2095  std::make_unique<CheckInputMethodChanged>(ic, d));
2096  return true;
2097  });
2098  d->imManager_.setDefaultInputMethod(name);
2099  };
2100 
2101  auto idx = std::distance(imList.begin(), iter);
2102  if (ic) {
2103  CheckInputMethodChanged imChangedRAII(ic, d);
2104  auto currentIM = inputMethod(ic);
2105  if (currentIM == name) {
2106  return;
2107  }
2108  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2109 
2110  if (idx != 0) {
2111  if (local) {
2112  inputState->setLocalIM(name);
2113  } else {
2114  inputState->setLocalIM({});
2115 
2116  setGlobalDefaultInputMethod(name);
2117  }
2118  inputState->setActive(true);
2119  } else {
2120  inputState->setActive(false);
2121  }
2122  if (inputState->imChanged_) {
2123  inputState->imChanged_->setReason(InputMethodSwitchedReason::Other);
2124  }
2125  } else {
2126  // We can't set local input method if we don't have a IC, but we should
2127  // still to change the global default.
2128  if (local) {
2129  return;
2130  }
2131  if (idx != 0) {
2132  setGlobalDefaultInputMethod(name);
2133  }
2134  return;
2135  }
2136 }
2137 
2139  FCITX_D();
2140  if (auto *ic = mostRecentInputContext()) {
2141  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2142  return inputState->isActive() ? 2 : 1;
2143  }
2144  return 0;
2145 }
2146 
2148  FCITX_D();
2149  if (auto *ic = mostRecentInputContext()) {
2150  CheckInputMethodChanged imChangedRAII(ic, d);
2151  trigger(ic, true);
2152  }
2153 }
2154 
2155 void Instance::enumerate(bool forward) {
2156  FCITX_D();
2157  if (auto *ic = mostRecentInputContext()) {
2158  CheckInputMethodChanged imChangedRAII(ic, d);
2159  enumerate(ic, forward);
2160  }
2161 }
2162 
2163 bool Instance::canTrigger() const {
2164  const auto &imManager = inputMethodManager();
2165  return (imManager.currentGroup().inputMethodList().size() > 1);
2166 }
2167 
2168 bool Instance::canAltTrigger(InputContext *ic) const {
2169  if (!canTrigger()) {
2170  return false;
2171  }
2172  FCITX_D();
2173  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2174  if (inputState->isActive()) {
2175  return true;
2176  }
2177  return inputState->lastIMChangeIsAltTrigger_;
2178 }
2179 
2180 bool Instance::canEnumerate(InputContext *ic) const {
2181  FCITX_D();
2182  if (!canTrigger()) {
2183  return false;
2184  }
2185 
2186  if (d->globalConfig_.enumerateSkipFirst()) {
2187  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2188  if (!inputState->isActive()) {
2189  return false;
2190  }
2191  return d->imManager_.currentGroup().inputMethodList().size() > 2;
2192  }
2193 
2194  return true;
2195 }
2196 
2197 bool Instance::canChangeGroup() const {
2198  const auto &imManager = inputMethodManager();
2199  return (imManager.groupCount() > 1);
2200 }
2201 
2203  FCITX_D();
2204  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2205  if (!canTrigger()) {
2206  return false;
2207  }
2208  inputState->setActive(!inputState->isActive());
2209  if (inputState->imChanged_) {
2210  inputState->imChanged_->setReason(reason);
2211  }
2212  return true;
2213 }
2214 
2215 bool Instance::trigger(InputContext *ic, bool totallyReleased) {
2216  FCITX_D();
2217  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2218  if (!canTrigger()) {
2219  return false;
2220  }
2221  // Active -> inactive -> enumerate.
2222  // Inactive -> active -> inactive -> enumerate.
2223  if (totallyReleased) {
2224  toggle(ic);
2225  inputState->firstTrigger_ = true;
2226  } else {
2227  if (!d->globalConfig_.enumerateWithTriggerKeys() ||
2228  (inputState->firstTrigger_ && inputState->isActive()) ||
2229  (d->globalConfig_.enumerateSkipFirst() &&
2230  d->imManager_.currentGroup().inputMethodList().size() <= 2)) {
2231  toggle(ic);
2232  } else {
2233  enumerate(ic, true);
2234  }
2235  inputState->firstTrigger_ = false;
2236  }
2237  return true;
2238 }
2239 
2240 bool Instance::altTrigger(InputContext *ic) {
2241  if (!canAltTrigger(ic)) {
2242  return false;
2243  }
2244 
2246  return true;
2247 }
2248 
2249 bool Instance::activate(InputContext *ic) {
2250  FCITX_D();
2251  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2252  if (!canTrigger()) {
2253  return false;
2254  }
2255  if (inputState->isActive()) {
2256  return true;
2257  }
2258  inputState->setActive(true);
2259  if (inputState->imChanged_) {
2260  inputState->imChanged_->setReason(InputMethodSwitchedReason::Activate);
2261  }
2262  return true;
2263 }
2264 
2266  FCITX_D();
2267  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2268  if (!canTrigger()) {
2269  return false;
2270  }
2271  if (!inputState->isActive()) {
2272  return true;
2273  }
2274  inputState->setActive(false);
2275  if (inputState->imChanged_) {
2276  inputState->imChanged_->setReason(
2278  }
2279  return true;
2280 }
2281 
2282 bool Instance::enumerate(InputContext *ic, bool forward) {
2283  FCITX_D();
2284  auto &imManager = inputMethodManager();
2285  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2286  const auto &imList = imManager.currentGroup().inputMethodList();
2287  if (!canTrigger()) {
2288  return false;
2289  }
2290 
2291  if (d->globalConfig_.enumerateSkipFirst() && imList.size() <= 2) {
2292  return false;
2293  }
2294 
2295  auto currentIM = inputMethod(ic);
2296 
2297  auto iter = std::ranges::find_if(
2298  imList, [&currentIM](const InputMethodGroupItem &item) {
2299  return item.name() == currentIM;
2300  });
2301  if (iter == imList.end()) {
2302  return false;
2303  }
2304  int idx = std::distance(imList.begin(), iter);
2305  auto nextIdx = [forward, &imList](int idx) {
2306  // be careful not to use negative to avoid overflow.
2307  return (idx + (forward ? 1 : (imList.size() - 1))) % imList.size();
2308  };
2309 
2310  idx = nextIdx(idx);
2311  if (d->globalConfig_.enumerateSkipFirst() && idx == 0) {
2312  idx = nextIdx(idx);
2313  }
2314  if (idx != 0) {
2315  std::vector<std::unique_ptr<CheckInputMethodChanged>> groupRAIICheck;
2316  d->icManager_.foreachFocused([d, &groupRAIICheck](InputContext *ic) {
2317  assert(ic->hasFocus());
2318  groupRAIICheck.push_back(
2319  std::make_unique<CheckInputMethodChanged>(ic, d));
2320  return true;
2321  });
2322  imManager.setDefaultInputMethod(imList[idx].name());
2323  inputState->setActive(true);
2324  inputState->setLocalIM({});
2325  } else {
2326  inputState->setActive(false);
2327  }
2328  if (inputState->imChanged_) {
2329  inputState->imChanged_->setReason(InputMethodSwitchedReason::Enumerate);
2330  }
2331 
2332  return true;
2333 }
2334 
2335 std::string Instance::commitFilter(InputContext *inputContext,
2336  const std::string &orig) {
2337  std::string result = orig;
2338  emit<Instance::CommitFilter>(inputContext, result);
2339  return result;
2340 }
2341 
2342 Text Instance::outputFilter(InputContext *inputContext, const Text &orig) {
2343  Text result = orig;
2344  emit<Instance::OutputFilter>(inputContext, result);
2345  if ((&orig == &inputContext->inputPanel().clientPreedit() ||
2346  &orig == &inputContext->inputPanel().preedit()) &&
2347  !globalConfig().showPreeditForPassword() &&
2348  inputContext->capabilityFlags().test(CapabilityFlag::Password)) {
2349  Text newText;
2350  for (int i = 0, e = result.size(); i < e; i++) {
2351  auto length = utf8::length(result.stringAt(i));
2352  std::string dot;
2353  dot.reserve(length * 3);
2354  while (length != 0) {
2355  dot += "\xe2\x80\xa2";
2356  length -= 1;
2357  }
2358  newText.append(std::move(dot),
2359  result.formatAt(i) | TextFormatFlag::DontCommit);
2360  }
2361  result = std::move(newText);
2362  }
2363  return result;
2364 }
2365 
2367  FCITX_D();
2368  return d->icManager_.lastFocusedInputContext();
2369 }
2370 
2372  FCITX_D();
2373  return d->icManager_.mostRecentInputContext();
2374 }
2375 
2377  FCITX_D();
2378  d->uiManager_.flush();
2379 }
2380 
2381 int scoreForGroup(FocusGroup *group, const std::string &displayHint) {
2382  // Hardcode wayland over X11.
2383  if (displayHint.empty()) {
2384  if (group->display() == "x11:") {
2385  return 2;
2386  }
2387  if (group->display().starts_with("x11:")) {
2388  return 1;
2389  }
2390  if (group->display() == "wayland:") {
2391  return 4;
2392  }
2393  if (group->display().starts_with("wayland:")) {
2394  return 3;
2395  }
2396  } else {
2397  if (group->display() == displayHint) {
2398  return 2;
2399  }
2400  if (group->display().starts_with(displayHint)) {
2401  return 1;
2402  }
2403  }
2404  return -1;
2405 }
2406 
2407 FocusGroup *Instance::defaultFocusGroup(const std::string &displayHint) {
2408  FCITX_D();
2409  FocusGroup *defaultFocusGroup = nullptr;
2410 
2411  int score = 0;
2412  d->icManager_.foreachGroup(
2413  [&score, &displayHint, &defaultFocusGroup](FocusGroup *group) {
2414  auto newScore = scoreForGroup(group, displayHint);
2415  if (newScore > score) {
2416  defaultFocusGroup = group;
2417  score = newScore;
2418  }
2419 
2420  return true;
2421  });
2422  return defaultFocusGroup;
2423 }
2424 
2425 void Instance::activateInputMethod(InputContextEvent &event) {
2426  FCITX_D();
2427  FCITX_DEBUG() << "Instance::activateInputMethod";
2428  InputContext *ic = event.inputContext();
2429  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2430  const auto *entry = inputMethodEntry(ic);
2431  if (entry) {
2432  FCITX_DEBUG() << "Activate: "
2433  << "[Last]:" << inputState->lastIM_
2434  << " [Activating]:" << entry->uniqueName();
2435  assert(inputState->lastIM_.empty());
2436  inputState->lastIM_ = entry->uniqueName();
2437  }
2438  auto *engine = inputMethodEngine(ic);
2439  if (!engine || !entry) {
2440  return;
2441  }
2442 #ifdef ENABLE_KEYBOARD
2443  if (auto *xkbState = inputState->customXkbState(true)) {
2444  if (auto *mods = findValue(d->stateMask_, ic->display())) {
2445  FCITX_KEYTRACE() << "Update mask to customXkbState";
2446  auto depressed = std::get<0>(*mods);
2447  auto latched = std::get<1>(*mods);
2448  auto locked = std::get<2>(*mods);
2449 
2450  // set modifiers in depressed if they don't appear in any of the
2451  // final masks
2452  // depressed |= ~(depressed | latched | locked);
2453  FCITX_KEYTRACE() << depressed << " " << latched << " " << locked;
2454  if (depressed == 0) {
2455  inputState->setModsAllReleased();
2456  }
2457  xkb_state_update_mask(xkbState, depressed, latched, locked, 0, 0,
2458  0);
2459  }
2460  }
2461 #endif
2462  ic->statusArea().clearGroup(StatusGroup::InputMethod);
2463  engine->activate(*entry, event);
2464  postEvent(InputMethodActivatedEvent(entry->uniqueName(), ic));
2465 }
2466 
2467 void Instance::deactivateInputMethod(InputContextEvent &event) {
2468  FCITX_D();
2469  FCITX_DEBUG() << "Instance::deactivateInputMethod event_type="
2470  << static_cast<uint32_t>(event.type());
2471  InputContext *ic = event.inputContext();
2472  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2473  const InputMethodEntry *entry = nullptr;
2474  InputMethodEngine *engine = nullptr;
2475 
2477  auto &icEvent =
2478  static_cast<InputContextSwitchInputMethodEvent &>(event);
2479  FCITX_DEBUG() << "Switch reason: "
2480  << static_cast<int>(icEvent.reason());
2481  FCITX_DEBUG() << "Old Input method: " << icEvent.oldInputMethod();
2482  entry = d->imManager_.entry(icEvent.oldInputMethod());
2483  } else {
2484  entry = inputMethodEntry(ic);
2485  }
2486  if (entry) {
2487  FCITX_DEBUG() << "Deactivate: "
2488  << "[Last]:" << inputState->lastIM_
2489  << " [Deactivating]:" << entry->uniqueName();
2490  assert(entry->uniqueName() == inputState->lastIM_);
2491  engine = static_cast<InputMethodEngine *>(
2492  d->addonManager_.addon(entry->addon()));
2493  }
2494  inputState->lastIM_.clear();
2495  if (!engine || !entry) {
2496  return;
2497  }
2498  inputState->overrideDeactivateIM_ = entry->uniqueName();
2499  engine->deactivate(*entry, event);
2500  inputState->overrideDeactivateIM_.clear();
2501  postEvent(InputMethodDeactivatedEvent(entry->uniqueName(), ic));
2502 }
2503 
2504 bool Instance::enumerateGroup(bool forward) {
2505  auto &imManager = inputMethodManager();
2506  auto groups = imManager.groups();
2507  if (groups.size() <= 1) {
2508  return false;
2509  }
2510  if (forward) {
2511  imManager.setCurrentGroup(groups[1]);
2512  } else {
2513  imManager.setCurrentGroup(groups.back());
2514  }
2515  return true;
2516 }
2517 
2519  FCITX_DEBUG() << "Input method switched";
2520  FCITX_D();
2521  if (!d->globalConfig_.showInputMethodInformation()) {
2522  return;
2523  }
2524  d->showInputMethodInformation(ic);
2525 }
2526 
2528  const std::string &message) {
2529  FCITX_DEBUG() << "Input method switched";
2530  FCITX_D();
2531  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2532  inputState->showInputMethodInformation(message);
2533 }
2534 
2536  FCITX_D();
2537  return (isInFlatpak() &&
2538  std::filesystem::is_regular_file("/app/.updated")) ||
2539  d->addonManager_.checkUpdate() || d->imManager_.checkUpdate() ||
2540  postEvent(CheckUpdateEvent());
2541 }
2542 
2543 void Instance::setXkbParameters(const std::string &display,
2544  const std::string &rule,
2545  const std::string &model,
2546  const std::string &options) {
2547 #ifdef ENABLE_KEYBOARD
2548  FCITX_D();
2549  bool resetState = false;
2550  if (auto *param = findValue(d->xkbParams_, display)) {
2551  if (std::get<0>(*param) != rule || std::get<1>(*param) != model ||
2552  std::get<2>(*param) != options) {
2553  std::get<0>(*param) = rule;
2554  std::get<1>(*param) = model;
2555  std::get<2>(*param) = options;
2556  resetState = true;
2557  }
2558  } else {
2559  d->xkbParams_.emplace(display, std::make_tuple(rule, model, options));
2560  }
2561 
2562  if (resetState) {
2563  d->keymapCache_[display].clear();
2564  d->icManager_.foreach([d, &display](InputContext *ic) {
2565  if (ic->display() == display ||
2566  !d->xkbParams_.contains(ic->display())) {
2567  auto *inputState = ic->propertyFor(&d->inputStateFactory_);
2568  inputState->resetXkbState();
2569  }
2570  return true;
2571  });
2572  }
2573 #else
2574  FCITX_UNUSED(display);
2575  FCITX_UNUSED(rule);
2576  FCITX_UNUSED(model);
2577  FCITX_UNUSED(options);
2578 #endif
2579 }
2580 
2581 void Instance::updateXkbStateMask(const std::string &display,
2582  uint32_t depressed_mods,
2583  uint32_t latched_mods, uint32_t locked_mods) {
2584  FCITX_D();
2585  d->stateMask_[display] =
2586  std::make_tuple(depressed_mods, latched_mods, locked_mods);
2587 }
2588 
2589 void Instance::clearXkbStateMask(const std::string &display) {
2590  FCITX_D();
2591  d->stateMask_.erase(display);
2592 }
2593 
2594 const char *Instance::version() { return FCITX_VERSION_STRING; }
2595 
2596 } // namespace fcitx
void updateXkbStateMask(const std::string &display, uint32_t depressed_mods, uint32_t latched_mods, uint32_t locked_mods)
Update xkb state mask for given display.
Definition: instance.cpp:2581
Describe a Key in fcitx.
Definition: key.h:41
void restart()
Restart fcitx instance, this should only be used within a regular Fcitx server, not within embedded m...
Definition: instance.cpp:2060
void reloadAddonConfig(const std::string &addonName)
Reload certain addon config.
Definition: instance.cpp:2001
CapabilityFlags capabilityFlags() const
Returns the current capability flags.
FCITXCORE_DEPRECATED uint32_t processCompose(InputContext *ic, KeySym keysym)
Handle current XCompose state.
Definition: instance.cpp:1803
std::string inputMethodIcon(InputContext *ic)
Return the input method icon for input context.
Definition: instance.cpp:1771
void setXkbParameters(const std::string &display, const std::string &rule, const std::string &model, const std::string &options)
Set xkb RLVMO tuple for given display.
Definition: instance.cpp:2543
void activate()
Activate last focused input context. (Switch to the active input method)
Definition: instance.cpp:1943
EventType
Type of input method events.
Definition: event.h:65
Switched by alternative trigger key.
ResetEvent is generated.
void resetCompose(InputContext *inputContext)
Reset the compose state.
Definition: instance.cpp:1921
T::PropertyType * propertyFor(const T *factory)
Helper function to return the input context property in specific type by given factory.
Definition: inputcontext.h:288
std::string UCS4ToUTF8(uint32_t code)
Convert UCS4 to UTF8 string.
Definition: utf8.cpp:21
FocusInEvent is generated when client gets focused.
Formatted string commonly used in user interface.
Whether client request input method to be disabled.
FocusGroup * defaultFocusGroup(const std::string &displayHint={})
Get the default focus group with given display hint.
Definition: instance.cpp:2407
static uint32_t keySymToUnicode(KeySym sym)
Convert keysym to a unicode.
Definition: key.cpp:738
void flushUI()
All user interface update is batched internally.
Definition: instance.cpp:2376
This is generated when input method group changed.
std::string commitFilter(InputContext *inputContext, const std::string &orig)
Update the commit string to frontend.
Definition: instance.cpp:2335
AddonManager & addonManager()
Get the addon manager.
Definition: instance.cpp:1593
std::string currentInputMethod()
Return the current input method of last focused input context.
Definition: instance.cpp:1968
Simple file system related API for checking file status.
size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.
Definition: utf8.h:33
bool validate(Iter start, Iter end)
Check if the string iterator range is valid utf8 string.
Definition: utf8.h:74
when user switch to a different input method by hand such as ctrl+shift by default, or by ui, default behavior is reset IM.
InputMethodSwitchedReason
The reason why input method is switched to another.
Definition: event.h:41
void deactivate()
Deactivate last focused input context.
Definition: instance.cpp:1982
int state()
Return a fcitx5-remote compatible value for the state.
Definition: instance.cpp:2138
Definition: action.cpp:17
InputMethodEngine * inputMethodEngine(InputContext *ic)
Return the input method engine object for given input context.
Definition: instance.cpp:1751
void setSignalPipe(int fd)
Set the pipe forwarding unix signal information.
Definition: instance.cpp:1363
InputMethodMode inputMethodMode() const
The current global input method mode.
Definition: instance.cpp:1504
void initialize()
Initialize fcitx.
Definition: instance.cpp:1410
EventLoop & eventLoop()
Get the fcitx event loop.
Definition: instance.cpp:1578
std::string currentUI()
Return the name of current user interface addon.
Definition: instance.cpp:1977
bool exitWhenMainDisplayDisconnected() const
Check whether command line specify whether to keep fcitx running.
Definition: instance.cpp:1382
C++ Utility functions for handling utf8 strings.
InputContext * mostRecentInputContext()
Return the most recent focused input context.
Definition: instance.cpp:2371
FCITX_NODISCARD std::unique_ptr< HandlerTableEntry< EventHandler > > watchEvent(EventType type, EventWatcherPhase phase, EventHandler callback)
Add a callback to for certain event type.
Definition: instance.cpp:1687
InputContextManager & inputContextManager()
Get the input context manager.
Definition: instance.cpp:1588
void setEnablePreedit(bool enable)
Override the preedit hint from client.
std::vector< std::string > split(std::string_view str, std::string_view delim, SplitBehavior behavior)
Split the string by delim.
virtual bool filtered() const
Whether a event is filtered by handler.
Definition: event.h:241
void clearGroup(StatusGroup group)
Clear only given status group.
Definition: statusarea.cpp:80
bool hasFocus() const
Returns whether the input context holds the input focus.
bool isRunning() const
Whether event loop is started and still running.
Definition: instance.cpp:1499
A class represents a formatted string.
Definition: text.h:27
bool willTryReplace() const
Check whether command line specify if it will replace an existing fcitx server.
Definition: instance.cpp:1377
Class to manage all the input method relation information.
bool isRestartRequested() const
Whether restart is requested.
Definition: instance.cpp:1518
Manager class for user interface.
Instance(int argc, char *argv[])
A main function like construct to be used to create Fcitx Instance.
Definition: instance.cpp:651
Base class for fcitx event.
Definition: event.h:212
virtual void deactivate(const InputMethodEntry &entry, InputContextEvent &event)
Called when input context switch its input method.
bool isValidChar(uint32_t c)
Check the chr value is not two invalid value above.
Definition: utf8.h:97
std::string inputMethodLabel(InputContext *ic)
Return the input method label for input context.
Definition: instance.cpp:1788
void save()
Save everything including input method profile and addon data.
Definition: instance.cpp:1935
Enum type for input context capability.
Class for status area in UI.
void resetInputMethodList()
Reset the input method configuration and recreate based on system language.
Definition: instance.cpp:2055
Switched by capability change (e.g. password field)
Base class for User Interface addon.
A thread safe class to post event to a certain EventLoop.
void clearXkbStateMask(const std::string &display)
Clear xkb state mask for given display.
Definition: instance.cpp:2589
Enum flag for text formatting.
void configure()
Launch configtool.
Definition: instance.cpp:1959
bool checkUpdate() const
Check if need to invoke Instance::refresh.
Definition: instance.cpp:2535
New Utility classes to handle application specific path.
uint32_t getChar(Iter iter, Iter end)
Get next UCS4 char from iter, do not cross end.
Definition: utf8.h:104
InvokeAction event is generated when client click on the preedit.
InputMethodManager & inputMethodManager()
Get the input method manager.
Definition: instance.cpp:1598
void reloadConfig()
Reload global config.
Definition: instance.cpp:2015
int exec()
Start the event loop of Fcitx.
Definition: instance.cpp:1476
C-style utf8 utility functions.
void setBinaryMode()
Set if this instance is running as fcitx5 binary.
Definition: instance.cpp:1554
bool isModifier() const
Check if the key is a modifier press.
Definition: key.cpp:460
InputContext * lastFocusedInputContext()
Return a focused input context.
Definition: instance.cpp:2366
std::string display() const
Returns the display server of the client.
void toggle()
Toggle between the first input method and active input method.
Definition: instance.cpp:2147
Notify the input method mode is changed.
Definition: event.h:646
void setCurrentInputMethod(const std::string &imName)
Set the input method of last focused input context.
Definition: instance.cpp:2069
void setRunning(bool running)
Let other know that event loop is already running.
Definition: instance.cpp:1494
bool canRestart() const
Check if fcitx 5 can safely restart by itself.
Definition: instance.cpp:1559
void showCustomInputMethodInformation(InputContext *ic, const std::string &message)
Show a small popup with input popup window with current input method information. ...
Definition: instance.cpp:2527
std::optional< std::string > processComposeString(InputContext *ic, KeySym keysym)
Handle current XCompose state.
Definition: instance.cpp:1851
void refresh()
Load newly installed input methods and addons.
Definition: instance.cpp:2008
Input method mode changed.
std::string inputMethod(InputContext *ic)
Return the unique name of input method for given input context.
Definition: instance.cpp:1706
UserInterfaceManager & userInterfaceManager()
Get the user interface manager.
Definition: instance.cpp:1608
void enumerate(bool forward)
Enumerate input method with in current group.
Definition: instance.cpp:2155
bool exiting() const
Check whether fcitx is in exiting process.
Definition: instance.cpp:1387
Key sym related types.
void showInputMethodInformation(InputContext *ic)
Show a small popup with input popup window with current input method information. ...
Definition: instance.cpp:2518
StatusArea & statusArea()
Returns the associated StatusArea.
String handle utilities.
Input Method Manager For fcitx.
EventType type() const
Type of event, can be used to decide event class.
Definition: event.h:222
bool empty() const
Whether input panel is totally empty.
Definition: inputpanel.cpp:122
void setInputMethodMode(InputMethodMode mode)
Set the current global input method mode.
Definition: instance.cpp:1509
FCITXCORE_DEPRECATED void reset(ResetReason reason)
Called when input context state need to be reset.
ssize_t safeRead(int fd, void *data, size_t maxlen)
a simple wrapper around read(), ignore EINTR.
Definition: fs.cpp:230
std::string addonForInputMethod(const std::string &imName)
Return the addon name of given input method.
Definition: instance.cpp:1951
Input Context Property for Fcitx.
static std::filesystem::path fcitxPath(const char *path, const std::filesystem::path &subPath={})
Return fcitx specific path defined at compile time.
An input context represents a client of Fcitx.
Definition: inputcontext.h:50
GlobalConfig & globalConfig()
Get the global config.
Definition: instance.cpp:1613
void exit()
Exit the fcitx event loop.
Definition: instance.cpp:1990
EventDispatcher & eventDispatcher()
Return a shared event dispatcher that is already attached to instance&#39;s event loop.
Definition: instance.cpp:1583
Key event is generated when client press or release a key.
const InputMethodEntry * inputMethodEntry(InputContext *ic)
Return the input method entry for given input context.
Definition: instance.cpp:1742
Class to represent a key.
Log utilities.
bool isComposing(InputContext *inputContext)
Check whether input context is composing or not.
Definition: instance.cpp:1903
static std::string keyListToString(const Container &container, KeyStringFormat format=KeyStringFormat::Portable)
Convert a key list to string.
Definition: key.h:204
Notify the global config is reloaded.
Definition: event.h:657
when using lost focus this might be variance case to case.
Addon Manager class.
static const char * version()
Return the version string of Fcitx.
Definition: instance.cpp:2594
Text outputFilter(InputContext *inputContext, const Text &orig)
Update the string that will be displayed in user interface.
Definition: instance.cpp:2342