| 1 |
|
| 2 |
> |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include "imapi.h" |
| 25 |
#include "keycode.h" |
| 26 |
|
| 27 |
static char raw_mode = 1; |
| 28 |
static unsigned cursorx, cursory; |
| 29 |
|
| 30 |
static void im_active() |
| 31 |
{ |
| 32 |
if (raw_mode) { |
| 33 |
init_keycode_state(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
static void im_deactive() |
| 38 |
{ |
| 39 |
Rectangle rect = { 0, 0, 0, 0 }; |
| 40 |
set_im_window(0, rect); |
| 41 |
set_im_window(1, rect); |
| 42 |
} |
| 43 |
|
| 44 |
static void process_raw_key(char *buf, unsigned len) |
| 45 |
{ |
| 46 |
unsigned i; |
| 47 |
for (i = 0; i < len; i++) { |
| 48 |
char down = !(buf[i] & 0x80); |
| 49 |
short code = buf[i] & 0x7f; |
| 50 |
|
| 51 |
if (!code) { |
| 52 |
if (i + 2 >= len) break; |
| 53 |
|
| 54 |
code = (buf[++i] & 0x7f) << 7; |
| 55 |
code |= buf[++i] & 0x7f; |
| 56 |
if (!(buf[i] & 0x80) || !(buf[i - 1] & 0x80)) continue; |
| 57 |
} |
| 58 |
|
| 59 |
unsigned short keysym = keycode_to_keysym(code, down); |
| 60 |
char *str = keysym_to_term_string(keysym, down); |
| 61 |
|
| 62 |
put_im_text(str, strlen(str)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
static void process_key(char *keys, unsigned len) |
| 67 |
{ |
| 68 |
if (raw_mode) { |
| 69 |
process_raw_key(keys, len); |
| 70 |
} else { |
| 71 |
put_im_text(keys, len); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
static void im_show(unsigned winid) |
| 76 |
{ |
| 77 |
static const char str[] = "a IM example"; |
| 78 |
|
| 79 |
Rectangle rect; |
| 80 |
rect.x = cursorx + 10, rect.y = cursory + 10; |
| 81 |
rect.w = 40, rect.h = 20; |
| 82 |
|
| 83 |
set_im_window(0, rect); |
| 84 |
fill_rect(rect, Gray); |
| 85 |
|
| 86 |
rect.y += rect.h + 10; |
| 87 |
rect.w = 180; |
| 88 |
rect.h = 30; |
| 89 |
|
| 90 |
set_im_window(1, rect); |
| 91 |
fill_rect(rect, Gray); |
| 92 |
draw_text(rect.x + 5, rect.y + 5, Black, Gray, str, sizeof(str) - 1); |
| 93 |
} |
| 94 |
|
| 95 |
static void im_hide() |
| 96 |
{ |
| 97 |
} |
| 98 |
|
| 99 |
static void cursor_pos_changed(unsigned x, unsigned y) |
| 100 |
{ |
| 101 |
cursorx = x; |
| 102 |
cursory = y; |
| 103 |
im_show(-1); |
| 104 |
} |
| 105 |
|
| 106 |
static void update_fbterm_info(Info *info) |
| 107 |
{ |
| 108 |
} |
| 109 |
|
| 110 |
static ImCallbacks cbs = { |
| 111 |
im_active, |
| 112 |
im_deactive, |
| 113 |
im_show, |
| 114 |
im_hide, |
| 115 |
process_key, |
| 116 |
cursor_pos_changed, |
| 117 |
update_fbterm_info, |
| 118 |
update_term_mode |
| 119 |
}; |
| 120 |
|
| 121 |
int main() |
| 122 |
{ |
| 123 |
register_im_callbacks(cbs); |
| 124 |
connect_fbterm(raw_mode); |
| 125 |
while (check_im_message()) ; |
| 126 |
|
| 127 |
return 0; |
| 128 |
} |