Debian Codegraph @

unikey/unikeyinputcontext.cpp

raw
1 /*
2 * SPDX-FileCopyrightText: 2018-2018 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 */
7 #include "unikeyinputcontext.h"
8 #include "ukengine.h"
9 #include "usrkeymap.h"
10 #include <ctype.h>
11 #include <iostream>
12 #include <memory.h>
13 #include <stdio.h>
14
15 using namespace std;
16
17 //--------------------------------------------
18 void CreateDefaultUnikeyOptions(UnikeyOptions *pOpt) {
19 pOpt->freeMarking = 1;
20 pOpt->modernStyle = 0;
21 pOpt->macroEnabled = 0;
22 pOpt->useUnicodeClipboard = 0;
23 pOpt->alwaysMacro = 0;
24 pOpt->spellCheckEnabled = 1;
25 pOpt->autoNonVnRestore = 0;
26 }
27
28 UnikeyInputMethod::UnikeyInputMethod()
29 : sharedMem_(std::make_unique<UkSharedMem>()) {
30 SetupUnikeyEngine();
31 sharedMem_->input.init();
32 sharedMem_->macStore.init();
33 sharedMem_->vietKey = true;
34 sharedMem_->usrKeyMapLoaded = false;
35 setInputMethod(UkTelex);
36 setOutputCharset(CONV_CHARSET_XUTF8);
37 CreateDefaultUnikeyOptions(&sharedMem_->options);
38 }
39
40 //--------------------------------------------
41 void UnikeyInputMethod::setInputMethod(UkInputMethod im) {
42 if (im == UkTelex || im == UkVni || im == UkSimpleTelex ||
43 im == UkSimpleTelex2 || im == UkViqr || im == UkMsVi) {
44 sharedMem_->input.setIM(im);
45 } else if (im == UkUsrIM && sharedMem_->usrKeyMapLoaded) {
46 // cout << "Switched to user mode\n"; //DEBUG
47 sharedMem_->input.setIM(sharedMem_->usrKeyMap);
48 }
49 emit<Reset>();
50 // cout << "IM changed to: " << im << endl; //DEBUG
51 }
52
53 void UnikeyInputMethod::setOutputCharset(int charset) {
54 sharedMem_->charsetId = charset;
55 emit<Reset>();
56 }
57
58 //--------------------------------------------
59 void UnikeyInputMethod::setOptions(UnikeyOptions *pOpt) {
60 sharedMem_->options.freeMarking = pOpt->freeMarking;
61 sharedMem_->options.modernStyle = pOpt->modernStyle;
62 sharedMem_->options.macroEnabled = pOpt->macroEnabled;
63 sharedMem_->options.useUnicodeClipboard = pOpt->useUnicodeClipboard;
64 sharedMem_->options.alwaysMacro = pOpt->alwaysMacro;
65 sharedMem_->options.spellCheckEnabled = pOpt->spellCheckEnabled;
66 sharedMem_->options.autoNonVnRestore = pOpt->autoNonVnRestore;
67 }
68
69 //--------------------------------------------
70 void UnikeyInputContext::setCapsState(int shiftPressed, int CapsLockOn) {
71 // UnikeyCapsAll = (shiftPressed && !CapsLockOn) || (!shiftPressed &&
72 // CapsLockOn);
73 capsLockOn_ = CapsLockOn;
74 shiftPressed_ = shiftPressed;
75 }
76
77 //--------------------------------------------
78 UnikeyInputContext::UnikeyInputContext(UnikeyInputMethod *im) {
79 conn_ =
80 im->connect<UnikeyInputMethod::Reset>([this]() { engine_.reset(); });
81 engine_.setCtrlInfo(im->sharedMem());
82 engine_.setCheckKbCaseFunc([this](int *pShiftPressed, int *pCapsLockOn) {
83 *pShiftPressed = shiftPressed_;
84 *pCapsLockOn = capsLockOn_;
85 });
86 }
87
88 //--------------------------------------------
89 UnikeyInputContext::~UnikeyInputContext() {}
90
91 //--------------------------------------------
92 void UnikeyInputContext::filter(unsigned int ch) {
93 bufChars_ = sizeof(buf_);
94 engine_.process(ch, backspaces_, buf_, bufChars_, output_);
95 }
96
97 //--------------------------------------------
98 void UnikeyInputContext::putChar(unsigned int ch) {
99 engine_.pass(ch);
100 bufChars_ = 0;
101 backspaces_ = 0;
102 }
103
104 //--------------------------------------------
105 void UnikeyInputContext::rebuildChar(VnLexiName ch) {
106 bufChars_ = sizeof(buf_);
107 engine_.rebuildChar(ch, backspaces_, buf_, bufChars_);
108 }
109
110 //--------------------------------------------
111 void UnikeyInputContext::resetBuf() { engine_.reset(); }
112
113 //--------------------------------------------
114 void UnikeyInputContext::backspacePress() {
115 bufChars_ = sizeof(buf_);
116 engine_.processBackspace(backspaces_, buf_, bufChars_, output_);
117 // printf("Backspaces: %d\n",UnikeyBackspaces);
118 }
119
120 //--------------------------------------------
121 void UnikeyInputContext::restoreKeyStrokes() {
122 bufChars_ = sizeof(buf_);
123 engine_.restoreKeyStrokes(backspaces_, buf_, bufChars_, output_);
124 }
125
126 bool UnikeyInputContext::isAtWordBeginning() const {
127 return engine_.atWordBeginning();
128 }