On the yanhuang Chinese platform, you can add an input method. The program design of the input part of the Chinese Language Platform of yanhuang uses a dominant module as an interface with the platform. It adopts a unified and standardized interface with each input module. Each input method can be regarded as a filtering, input ASCII code, and return Chinese characters.
The leading module of the input method exists in the form of a dynamic Connection Library (libimpi. a), and is connected to each input method module into a separate executable file. The module level is as follows:
For each specific input method, a new executable file can be linked according to a certain data structure and added to the Chinese Language Platform of yanhuang. These data structures include external variables and external functions.
1. external variables
Array extcode []: string type, indicating the input external code string;
Array candstr []: string type, indicating the string to be selected with duplicate codes;
Array result []: string type, indicating the obtained result string;
Array imname []: string type, indicating the name of the input method;
Array imdscrpt []: string type, indicating the description of the input method.
Variable maxeclen: integer, indicating the maximum length of the external code;
Variable eclen: integer, indicating the length of the external code.
These variables are defined as follows:
Extern int maxeclen;/* The maximum length of external code */
Extern int eclen;/* The length of external code */
Extern char extcode [];/* External code */
Extern char candstr [];/* Candinate string */
Extern char result [];/* Result string */
2. External Functions
There are four external functions: IMInit, IMCleanup, IMSelect, and IMFilter ). The four functions are defined as follows:
Int IMInit ();
Int IMClearup ();
Int IMSelect (int select );
Int IMFilter (int ch );
Among them, IMInit) function is used to initialize the input method; IMClearup) function is used to process the situation when the input method is released; IMSelect) function is used to process the current input method selected; IMFilter) is the core function of the input method. It is used to filter received strings and obtain correct input results.
Here is a brief introduction to the location input method:
Preparations
First, you need to obtain the libimpi. a file, and then you can create a qvwei directory.
Code Writing
Compile a qvwei. c file and initialize the input method. qvwei. c is as follows.
/* The qvwei input method
*/
# Include
# Include
# Include
# Include
# Include "impi. h"
Char imname [] = "location ";
Char imdscrpt [] = "location Input Method ";
Static void reset ();
Static void append (int ch );
Static void comp_appe (int prevch, int ch );
Int IMInit ()
{
Return 0;
}
Int IMClearup ()
{
Return 0;
}
Int IMSelect (select)
Int select;
{
Return 0;
}
Int IMFilter (ch)
Int ch;
{
}
The next step is to write the IMInit), IMClearup), IMSelect), and IMFilter functions.
Add the following code to the IMInit function:
Maxeclen = 5;
Reset ();
Return 0;
Because the location input method is relatively simple, IMClearup) and IMSelect functions do not require additional code. In IMFilter) Add the following processing code to this function:
Candstr [0] = '\ 0 ';
Result [0] = '\ 0 ';
/* Process currnet keystroke */
If (isdigit (ch ))
{
If (eclen = 1) | (eclen = 3 ))
Comp_appe (extcode [eclen-1], ch );
Else
Append (ch );
If (eclen = 4)
{
Int qv, wei;
Qv = (extcode [0]-'0') * 10 + (extcode [1]-'0 ');
Wei = (extcode [2]-'0') * 10 + (extcode [3]-'0 ');
Reset ();
Result [0] = qv + 0xa0;
Result [1] = wei + 0xa0;
Result [2] = '\ 0 ';
}
}
Else
{
If (ch = KEY_BACKSPACE) & (eclen> 0 ))
Extcode [-- eclen] = '\ 0 ';
Else
{
Result [0] = ch;
Result [1] = '\ 0 ';
}
}
/* Generate candinates */
If (eclen> 0) & (eclen <4 ))
{
Int qv, wei, I, w;
Char buf [3];
Char * p;
If (eclen> 1)
Qv = (extcode [0]-'0') * 10 + (extcode [1]-'0 ');
Else if (extcode [0] = '0 ')
Qv = 1;
Else
Qv = (extcode [0]-'0') * 10;
Wei = (eclen <3 )? 0: (extcode [2]-'0') * 10;
P = candstr;
For (I = 0; I <= 9; I ++)
{
W = wei + I;
If (w> 0) & (w <= 94 ))
{
Buf [0] = qv + 0xa0;
Buf [1] = wei + I + 0xa0;
Buf [2] = '\ 0 ';
P + = sprintf (p, "% d: % s", I, buf );
}
}
}
Return 0;
The above Code uses three other functions, which can be defined as local functions. Their implementation code is as follows:
Static void reset (void)
{
Eclen = 0;
Extcode [0] = '\ 0 ';
Candstr [0] = '\ 0 ';
Result [0] = '\ 0 ';
}
Static void append (ch)
Int ch;
{
Extcode [eclen ++] = ch;
Extcode [eclen] = '\ 0 ';
}
Static void comp_appe (prevch, ch)
Int prevch;
Int ch;
{
If (prevch = '0 ')
{
If (ch! = '0 ')
Append (ch );
}
Else if (prevch = '9 ')
{
If (ch <= '4 ')
Append (ch );
}
Else
Append (ch );
}
Splicing the above Code is a complete qvwei. c file.
Compilation and link
Finally, you can write a MakeFile to create an executable Input Method file. An example of a Makefile is as follows:
Qvwei:
Cc-c qvwei. c
Cc-o $ @ qvwei. o libimpi.
Strip $ @
Rm-f *. o
Then, you can create an executable qvwei file by typing make in this directory. After running the qvwei file on the Chinese Language Platform of yanhuang, you can add this input method to the Chinese Language Platform of yanhuang. The platform automatically assigns a shortcut key for switching the input method.
Note:
1: There are four files packaged along with them: impi. c, impi. h, incommon. c, and imcommon. h. They are used to generate libimpi.. The source code of these four functions is provided to help you understand the input method. But do not change these four files and re-compile libimi. a. Otherwise, the Chinese language platform may not be able to recognize the newly generated input method.
2: The above is just a simple example. For different input methods, the core code of the input method may be much more complicated.