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