| 1 |
// -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- |
| 2 |
/*------------------------------------------------------------------------------ |
| 3 |
UniKey - Open-source Vietnamese Keyboard |
| 4 |
Copyright (C) 1998-2004 Pham Kim Long |
| 5 |
Contact: |
| 6 |
[email protected] |
| 7 |
http://unikey.sf.net |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or |
| 10 |
modify it under the terms of the GNU General Public License |
| 11 |
as published by the Free Software Foundation; either version 2 |
| 12 |
of the License, or (at your option) any later version. |
| 13 |
|
| 14 |
This program is distributed in the hope that it will be useful, |
| 15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
GNU General Public License for more details. |
| 18 |
|
| 19 |
You should have received a copy of the GNU General Public License |
| 20 |
along with this program; if not, write to the Free Software |
| 21 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 |
--------------------------------------------------------------------------------*/ |
| 23 |
|
| 24 |
#include <ctype.h> |
| 25 |
#include <memory.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <iostream> |
| 28 |
#include "unikey.h" |
| 29 |
#include "ukengine.h" |
| 30 |
#include "usrkeymap.h" |
| 31 |
|
| 32 |
using namespace std; |
| 33 |
|
| 34 |
//---- exported variables for use in UkEnginge class ---- |
| 35 |
UkSharedMem *pShMem = 0; |
| 36 |
|
| 37 |
UkEngine MyKbEngine; |
| 38 |
|
| 39 |
int UnikeyCapsLockOn = 0; |
| 40 |
int UnikeyShiftPressed = 0; |
| 41 |
//---------------------------------------------------- |
| 42 |
|
| 43 |
unsigned char UnikeyBuf[1024]; |
| 44 |
int UnikeyBackspaces; |
| 45 |
int UnikeyBufChars; |
| 46 |
UkOutputType UnikeyOutput; |
| 47 |
|
| 48 |
//-------------------------------------------- |
| 49 |
void UnikeySetInputMethod(UkInputMethod im) |
| 50 |
{ |
| 51 |
if (im == UkTelex || im == UkVni || im == UkSimpleTelex || im == UkSimpleTelex2 || im == UkViqr || im == UkMsVi) { |
| 52 |
pShMem->input.setIM(im); |
| 53 |
MyKbEngine.reset(); |
| 54 |
} |
| 55 |
else if (im == UkUsrIM && pShMem->usrKeyMapLoaded) { |
| 56 |
//cout << "Switched to user mode\n"; //DEBUG |
| 57 |
pShMem->input.setIM(pShMem->usrKeyMap); |
| 58 |
MyKbEngine.reset(); |
| 59 |
} |
| 60 |
|
| 61 |
//cout << "IM changed to: " << im << endl; //DEBUG |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
//-------------------------------------------- |
| 66 |
void UnikeySetCapsState(int shiftPressed, int CapsLockOn) |
| 67 |
{ |
| 68 |
//UnikeyCapsAll = (shiftPressed && !CapsLockOn) || (!shiftPressed && CapsLockOn); |
| 69 |
UnikeyCapsLockOn = CapsLockOn; |
| 70 |
UnikeyShiftPressed = shiftPressed; |
| 71 |
} |
| 72 |
|
| 73 |
//-------------------------------------------- |
| 74 |
int UnikeySetOutputCharset(int charset) |
| 75 |
{ |
| 76 |
pShMem->charsetId = charset; |
| 77 |
MyKbEngine.reset(); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
//-------------------------------------------- |
| 82 |
void UnikeySetOptions(UnikeyOptions *pOpt) |
| 83 |
{ |
| 84 |
pShMem->options.freeMarking = pOpt->freeMarking; |
| 85 |
pShMem->options.modernStyle = pOpt->modernStyle; |
| 86 |
pShMem->options.macroEnabled = pOpt->macroEnabled; |
| 87 |
pShMem->options.useUnicodeClipboard = pOpt->useUnicodeClipboard; |
| 88 |
pShMem->options.alwaysMacro = pOpt->alwaysMacro; |
| 89 |
pShMem->options.spellCheckEnabled = pOpt->spellCheckEnabled; |
| 90 |
pShMem->options.autoNonVnRestore = pOpt->autoNonVnRestore; |
| 91 |
} |
| 92 |
|
| 93 |
//-------------------------------------------- |
| 94 |
void UnikeyGetOptions(UnikeyOptions *pOpt) |
| 95 |
{ |
| 96 |
*pOpt = pShMem->options; |
| 97 |
} |
| 98 |
|
| 99 |
//-------------------------------------------- |
| 100 |
void CreateDefaultUnikeyOptions(UnikeyOptions *pOpt) |
| 101 |
{ |
| 102 |
pOpt->freeMarking = 1; |
| 103 |
pOpt->modernStyle = 0; |
| 104 |
pOpt->macroEnabled = 0; |
| 105 |
pOpt->useUnicodeClipboard = 0; |
| 106 |
pOpt->alwaysMacro = 0; |
| 107 |
pOpt->spellCheckEnabled = 1; |
| 108 |
pOpt->autoNonVnRestore = 0; |
| 109 |
} |
| 110 |
|
| 111 |
//-------------------------------------------- |
| 112 |
void UnikeyCheckKbCase(int *pShiftPressed, int *pCapsLockOn) |
| 113 |
{ |
| 114 |
*pShiftPressed = UnikeyShiftPressed; |
| 115 |
*pCapsLockOn = UnikeyCapsLockOn; |
| 116 |
} |
| 117 |
|
| 118 |
//-------------------------------------------- |
| 119 |
void UnikeySetup() |
| 120 |
{ |
| 121 |
SetupUnikeyEngine(); |
| 122 |
pShMem = new UkSharedMem; |
| 123 |
pShMem->input.init(); |
| 124 |
pShMem->macStore.init(); |
| 125 |
pShMem->vietKey = 1; |
| 126 |
pShMem->usrKeyMapLoaded = 0; |
| 127 |
MyKbEngine.setCtrlInfo(pShMem); |
| 128 |
MyKbEngine.setCheckKbCaseFunc(&UnikeyCheckKbCase); |
| 129 |
UnikeySetInputMethod(UkTelex); |
| 130 |
UnikeySetOutputCharset(CONV_CHARSET_XUTF8); |
| 131 |
pShMem->initialized = 1; |
| 132 |
CreateDefaultUnikeyOptions(&pShMem->options); |
| 133 |
} |
| 134 |
|
| 135 |
//-------------------------------------------- |
| 136 |
void UnikeyCleanup() |
| 137 |
{ |
| 138 |
delete pShMem; |
| 139 |
} |
| 140 |
|
| 141 |
//-------------------------------------------- |
| 142 |
void UnikeyFilter(unsigned int ch) |
| 143 |
{ |
| 144 |
UnikeyBufChars = sizeof(UnikeyBuf); |
| 145 |
MyKbEngine.process(ch, UnikeyBackspaces, UnikeyBuf, UnikeyBufChars, UnikeyOutput); |
| 146 |
} |
| 147 |
|
| 148 |
//-------------------------------------------- |
| 149 |
void UnikeyPutChar(unsigned int ch) |
| 150 |
{ |
| 151 |
MyKbEngine.pass(ch); |
| 152 |
UnikeyBufChars = 0; |
| 153 |
UnikeyBackspaces = 0; |
| 154 |
} |
| 155 |
|
| 156 |
//-------------------------------------------- |
| 157 |
void UnikeyResetBuf() |
| 158 |
{ |
| 159 |
MyKbEngine.reset(); |
| 160 |
} |
| 161 |
|
| 162 |
//-------------------------------------------- |
| 163 |
void UnikeySetSingleMode() |
| 164 |
{ |
| 165 |
MyKbEngine.setSingleMode(); |
| 166 |
} |
| 167 |
|
| 168 |
//-------------------------------------------- |
| 169 |
void UnikeyBackspacePress() |
| 170 |
{ |
| 171 |
UnikeyBufChars = sizeof(UnikeyBuf); |
| 172 |
MyKbEngine.processBackspace(UnikeyBackspaces, UnikeyBuf, UnikeyBufChars, UnikeyOutput); |
| 173 |
// printf("Backspaces: %d\n",UnikeyBackspaces); |
| 174 |
} |
| 175 |
|
| 176 |
//-------------------------------------------- |
| 177 |
int UnikeyLoadMacroTable(const char *fileName) |
| 178 |
{ |
| 179 |
return pShMem->macStore.loadFromFile(fileName); |
| 180 |
} |
| 181 |
|
| 182 |
//-------------------------------------------- |
| 183 |
int UnikeyLoadUserKeyMap(const char *fileName) |
| 184 |
{ |
| 185 |
if (UkLoadKeyMap(fileName, pShMem->usrKeyMap)) { |
| 186 |
//cout << "User key map loaded!\n"; //DEBUG |
| 187 |
pShMem->usrKeyMapLoaded = 1; |
| 188 |
return 1; |
| 189 |
} |
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
//-------------------------------------------- |
| 194 |
void UnikeyRestoreKeyStrokes() |
| 195 |
{ |
| 196 |
UnikeyBufChars = sizeof(UnikeyBuf); |
| 197 |
MyKbEngine.restoreKeyStrokes(UnikeyBackspaces, UnikeyBuf, UnikeyBufChars, UnikeyOutput); |
| 198 |
} |
| 199 |
|
| 200 |
bool UnikeyAtWordBeginning() |
| 201 |
{ |
| 202 |
return MyKbEngine.atWordBeginning(); |
| 203 |
} |
| 204 |
|