| 1 | /* |
| 2 | * Copyright © 2008-2010 dragchan <[email protected]> |
| 3 | * This file is part of FbTerm. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * FbTerm provides a set of wrapped API to simply the IM server development. The API encapsulates input method message sending, |
| 23 | * receiving and processing. |
| 24 | */ |
| 25 | |
| 26 | #ifndef IM_API_H |
| 27 | #define IM_API_H |
| 28 | |
| 29 | #ifdef __cplusplus |
| 30 | extern "C" { |
| 31 | #endif |
| 32 | |
| 33 | #include "immessage.h" |
| 34 | |
| 35 | typedef void (*ActiveFun)(); |
| 36 | typedef void (*DeactiveFun)(); |
| 37 | |
| 38 | /// @param winid indicates which window should be redrawn, -1 means redraw all UI window. @see set_im_window() |
| 39 | typedef void (*ShowUIFun)(unsigned winid); |
| 40 | typedef void (*HideUIFun)(); |
| 41 | typedef void (*SendKeyFun)(char *keys, unsigned len); |
| 42 | typedef void (*CursorPositionFun)(unsigned x, unsigned y); |
| 43 | typedef void (*FbTermInfoFun)(Info *info); |
| 44 | typedef void (*TermModeFun)(char crlf, char appkey, char curo); |
| 45 | |
| 46 | typedef struct { |
| 47 | ActiveFun active; ///< called when receiving a Active message |
| 48 | DeactiveFun deactive; ///< called when receiving a Deactive message |
| 49 | ShowUIFun show_ui; ///< called when receiving a ShowUI message |
| 50 | HideUIFun hide_ui; ///< called when receiving a HideUI message |
| 51 | SendKeyFun send_key; ///< called when receiving a SendKey message |
| 52 | CursorPositionFun cursor_position; ///< called when receiving a CursorPosition message |
| 53 | FbTermInfoFun fbterm_info; ///< called when receiving a FbTermInfo message |
| 54 | TermModeFun term_mode; ///< called when receiving a TermMode message |
| 55 | } ImCallbacks; |
| 56 | |
| 57 | /** |
| 58 | * @brief register message call-back functions: |
| 59 | * @param callbacks |
| 60 | */ |
| 61 | extern void register_im_callbacks(ImCallbacks callbacks); |
| 62 | |
| 63 | /** |
| 64 | * @brief send message Connect to FbTerm |
| 65 | * @param mode keyboard input mode |
| 66 | * |
| 67 | * FbTerm provides two kinds of keyboard input modes, IM server can choose a favorite one with parameter mode of |
| 68 | * connect_fbterm(). |
| 69 | * |
| 70 | * If mode equals zero, IM server will receive characters translated by kernel with current keymap in SendKey messages. |
| 71 | * There are some disadvantages in this mode, for example, shortcuts composed with only modifiers are impossible. |
| 72 | * ( shortcuts composed with modifiers and normal keys can create a map if they are not mapped in current kernel keymap, |
| 73 | * maybe we need add new messages like RequestKeyMap and AckKeyMap used by IM server to ask FbTerm to do this work. ) |
| 74 | |
| 75 | * If mode is non-zero, FbTerm sets keyboard mode to K_MEDIUMRAW after Active Message, IM server will receive raw keycode |
| 76 | * from kernel and have full control for keyboard, except FbTerm's shortcuts, e.g. Ctrl + Space. |
| 77 | * Under this mode, IM server should translate keycodes to keysyms according current kernel keymap, change keyboard led |
| 78 | * state and translate keysyms to characters if it want to send user input back to FbTerm. Because some translation of |
| 79 | * keysyms to characters are not fixed (e.g. cursor movement keysym K_LEFT can be 'ESC [ D' or 'ESC O D'), message TermMode |
| 80 | * sent by FbTerm is used to help IM server do this translation. |
| 81 | * |
| 82 | * keycode.c/keycode.h provides some functions to translate keycode to keysym and keysym to characters. |
| 83 | */ |
| 84 | extern void connect_fbterm(char mode); |
| 85 | |
| 86 | /** |
| 87 | * @brief get the file descriptor of the unix socket used to transfer IM messages. |
| 88 | * @return file id of the socket connected to FbTerm |
| 89 | * |
| 90 | * Because check_im_message() blocks until a message arrives, IM server can use select/poll to monitor other file |
| 91 | * descriptors among with the one from get_im_socket(). |
| 92 | */ |
| 93 | extern int get_im_socket(); |
| 94 | |
| 95 | /** |
| 96 | * @brief receive IM messages from FbTerm and dispatch them to functions registered with register_im_callbacks() |
| 97 | * @return zero if DisconnectIM messages has been received, otherwise non-zero |
| 98 | */ |
| 99 | extern int check_im_message(); |
| 100 | |
| 101 | /** |
| 102 | * @brief send message PutText to FbTerm |
| 103 | * @param text translated text from user keyboard input, must be encoded with utf8 |
| 104 | * @param len text's length |
| 105 | */ |
| 106 | extern void put_im_text(const char *text, unsigned len); |
| 107 | |
| 108 | /** |
| 109 | * @brief send message SetWin to FbTerm and wait until AckWin has arrived |
| 110 | * @param winid the window id, must less than NR_IM_WINS |
| 111 | * @param rect the rectangle of this window area |
| 112 | * |
| 113 | * every UI window (e.g. status bar, candidate table, etc) should have a unique win id. |
| 114 | */ |
| 115 | extern void set_im_window(unsigned winid, Rectangle rect); |
| 116 | |
| 117 | /** |
| 118 | * The colors of xterm's 256 color mode supported by FbTerm can be used in fill_rect() and draw_text(). |
| 119 | * ColorType defines the first 16 colors with standard linux console. |
| 120 | * view 256colors.png for all colors. |
| 121 | */ |
| 122 | typedef enum { Black = 0, DarkRed, DarkGreen, DarkYellow, DarkBlue, DarkMagenta, DarkCyan, Gray, |
| 123 | DarkGray, Red, Green, Yellow, Blue, Magenta, Cyan, White } ColorType; |
| 124 | |
| 125 | /** |
| 126 | * @brief send message FillRect to FbTerm |
| 127 | * @param rect the rectangle to be filled |
| 128 | * @param color |
| 129 | */ |
| 130 | extern void fill_rect(Rectangle rect, unsigned char color); |
| 131 | |
| 132 | /** |
| 133 | * @breif send message DrawText to FbTerm |
| 134 | * @param x x axes of top left point |
| 135 | * @param y y axes of top left point |
| 136 | * @param fc foreground color |
| 137 | * @param bc background color |
| 138 | * @param text text to be drawn, must be encoding with utf8 |
| 139 | * @param len text's length |
| 140 | */ |
| 141 | extern void draw_text(unsigned x, unsigned y, unsigned char fc, unsigned char bc, const char *text, unsigned len); |
| 142 | |
| 143 | #ifdef __cplusplus |
| 144 | } |
| 145 | #endif |
| 146 | |
| 147 | #endif |