Input method in FreeBSD virtual terminal (Week4)
Date: 06/27-07/04
Python Bindings
This week I decided to replace SWIG with ctypes. The reason is that I found it's quite complicated to return a void pointer or a C-style string in SWIG. While in ctypes I can set a functions's return type to c_void_p to return a void pointer, and one can even cast it into c_char_p to convert it into a Python string.
Take function rime_wrapper_get_input_str() as an example:
rime_wrapper_get_input_str.restype = c_void_p
Then convert the type to char pointer, copy the string and free the origin pointer:
_input_str = rime_wrapper_get_input_str(self.rime_wrapper_ptr) input_str = cast(_input_str, c_char_p).value.decode('utf-8') rime_wrapper_free_str(_input_str) return input_str
Moreover, I found it's not as difficult as I thought few weeks ago to define the contents of a C struct in Python, and it turned out that it paid off since I could directly access the members in a structure in Python without processing them in C.
Tmux-rime
The origin idea of the server is acted as a rime session manager, which can handle multiple sessions and send different actions to different sessions depending on the session_id sent by the client. Later I found RimeAPI only can be initialized once during the program runtime, so the server can no longer handle multiple sessions simultaneously. Therefore I modified both client and server such that only when the user presses the tmux-rime prefix key do the server start.
I also had established and implemented the six basic commands for the server:
commit_raw_str: Commit the raw input text
delete_char: Delete a char in composing
handle_key: Handle a regular key
clear_state: Clear the current composition
get_output_text: Get the text that will be inserted later
get_status_str: Get the display string of the status bar
As for the tmux part, I had implemented these two functions:
- Text inserting: Inserting the CJK characters in where the cursor places
- IME status bar: Show preedit string and candidates in the tmux status bar