Debian Codegraph @

im/example.c

raw symbols SCIP
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 #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, // .active
112 im_deactive, // .deactive
113 im_show, // .show_ui
114 im_hide, // .hide_ui
115 process_key, // .send_key
116 cursor_pos_changed, // .cursor_position
117 update_fbterm_info, // .fbterm_info
118 update_term_mode // .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 }