Debian Codegraph @

im/imapi.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 <stdint.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include "imapi.h"
27
28 #define OFFSET(TYPE, MEMBER) ((size_t)(&(((TYPE *)0)->MEMBER)))
29 #define MSG(a) ((Message *)(a))
30
31 static int imfd = -1;
32 static ImCallbacks cbs;
33 static char pending_msg_buf[10240];
34 static unsigned pending_msg_buf_len = 0;
35 static int im_active = 0;
36
37 static void wait_message(MessageType type);
38
39 void register_im_callbacks(ImCallbacks callbacks)
40 {
41 cbs = callbacks;
42 }
43
44 int get_im_socket()
45 {
46 static char init = 0;
47 if (!init) {
48 init = 1;
49
50 char *val = getenv("FBTERM_IM_SOCKET");
51 if (val) {
52 char *tail;
53 int fd = strtol(val, &tail, 0);
54 if (!*tail) imfd = fd;
55 }
56 }
57
58 return imfd;
59 }
60
61 void connect_fbterm(char raw)
62 {
63 get_im_socket();
64 if (imfd == -1) return;
65
66 Message msg;
67 msg.type = Connect;
68 msg.len = sizeof(msg);
69 msg.raw = (raw ? 1 : 0);
70 int ret = write(imfd, (char *)&msg, sizeof(msg));
71 }
72
73 void put_im_text(const char *text, unsigned len)
74 {
75 if (imfd == -1 || !im_active || !text || !len || (OFFSET(Message, texts) + len > UINT16_MAX)) return;
76
77 char buf[OFFSET(Message, texts) + len];
78
79 MSG(buf)->type = PutText;
80 MSG(buf)->len = sizeof(buf);
81 memcpy(MSG(buf)->texts, text, len);
82
83 int ret = write(imfd, buf, MSG(buf)->len);
84 }
85
86 void set_im_window(unsigned id, Rectangle rect)
87 {
88 if (imfd == -1 || !im_active || id >= NR_IM_WINS) return;
89
90 Message msg;
91 msg.type = SetWin;
92 msg.len = sizeof(msg);
93 msg.win.winid = id;
94 msg.win.rect = rect;
95
96 int ret = write(imfd, (char *)&msg, sizeof(msg));
97 wait_message(AckWin);
98 }
99
100 void fill_rect(Rectangle rect, unsigned char color)
101 {
102 Message msg;
103 msg.type = FillRect;
104 msg.len = sizeof(msg);
105
106 msg.fillRect.rect = rect;
107 msg.fillRect.color = color;
108
109 int ret = write(imfd, (char *)&msg, sizeof(msg));
110 }
111
112 void draw_text(unsigned x, unsigned y, unsigned char fc, unsigned char bc, const char *text, unsigned len)
113 {
114 if (!text || !len) return;
115
116 char buf[OFFSET(Message, drawText.texts) + len];
117
118 MSG(buf)->type = DrawText;
119 MSG(buf)->len = sizeof(buf);
120
121 MSG(buf)->drawText.x = x;
122 MSG(buf)->drawText.y = y;
123 MSG(buf)->drawText.fc = fc;
124 MSG(buf)->drawText.bc = bc;
125 memcpy(MSG(buf)->drawText.texts, text, len);
126
127 int ret = write(imfd, buf, MSG(buf)->len);
128 }
129
130 static int process_message(Message *msg)
131 {
132 int exit = 0;
133
134 switch (msg->type) {
135 case Disconnect:
136 close(imfd);
137 imfd = -1;
138 exit = 1;
139 break;
140
141 case FbTermInfo:
142 if (cbs.fbterm_info) {
143 cbs.fbterm_info(&msg->info);
144 }
145 break;
146
147 case Active:
148 im_active = 1;
149 if (cbs.active) {
150 cbs.active();
151 }
152 break;
153
154 case Deactive:
155 if (cbs.deactive) {
156 cbs.deactive();
157 }
158 im_active = 0;
159 break;
160
161 case ShowUI:
162 if (im_active && cbs.show_ui) {
163 cbs.show_ui(msg->winid);
164 }
165 break;
166
167 case HideUI: {
168 if (im_active && cbs.hide_ui) {
169 cbs.hide_ui();
170 }
171
172 Message msg;
173 msg.type = AckHideUI;
174 msg.len = sizeof(msg);
175 int ret = write(imfd, (char *)&msg, sizeof(msg));
176 break;
177 }
178
179 case SendKey:
180 if (im_active && cbs.send_key) {
181 cbs.send_key(msg->keys, msg->len - OFFSET(Message, keys));
182 }
183 break;
184
185 case CursorPosition:
186 if (im_active && cbs.cursor_position) {
187 cbs.cursor_position(msg->cursor.x, msg->cursor.y);
188 }
189 break;
190
191 case TermMode:
192 if (im_active && cbs.term_mode) {
193 cbs.term_mode(msg->term.crWithLf, msg->term.applicKeypad, msg->term.cursorEscO);
194 }
195 break;
196
197 default:
198 break;
199 }
200
201 return exit;
202 }
203
204 static int process_messages(char *buf, int len)
205 {
206 char *cur = buf, *end = cur + len;
207 int exit = 0;
208
209 for (; cur < end && MSG(cur)->len <= (end - cur); cur += MSG(cur)->len) {
210 exit |= process_message(MSG(cur));
211 }
212
213 return exit;
214 }
215
216 static void wait_message(MessageType type)
217 {
218 int ack = 0;
219 while (!ack) {
220 char *cur = pending_msg_buf + pending_msg_buf_len;
221 int len = read(imfd, cur, sizeof(pending_msg_buf) - pending_msg_buf_len);
222
223 if (len == -1 && (errno == EAGAIN || errno == EINTR)) continue;
224 else if (len <= 0) {
225 close(imfd);
226 imfd = -1;
227 return;
228 }
229
230 pending_msg_buf_len += len;
231
232 char *end = cur + len;
233 for (; cur < end && MSG(cur)->len <= (end - cur); cur += MSG(cur)->len) {
234 if (MSG(cur)->type == type) {
235 memcpy(cur, cur + MSG(cur)->len, end - cur - MSG(cur)->len);
236 pending_msg_buf_len -= MSG(cur)->len;
237
238 ack = 1;
239 break;
240 }
241 }
242 }
243
244 if (pending_msg_buf_len) {
245 Message msg;
246 msg.type = Ping;
247 msg.len = sizeof(msg);
248 int ret = write(imfd, (char *)&msg, sizeof(msg));
249 }
250 }
251
252 int check_im_message()
253 {
254 if (imfd == -1) return 0;
255
256 char buf[sizeof(pending_msg_buf)];
257 int len, exit = 0;
258
259 if (pending_msg_buf_len) {
260 len = pending_msg_buf_len;
261 pending_msg_buf_len = 0;
262
263 memcpy(buf, pending_msg_buf, len);
264 exit |= process_messages(buf, len);
265 }
266
267 len = read(imfd, buf, sizeof(buf));
268
269 if (len == -1 && (errno == EAGAIN || errno == EINTR)) return 1;
270 else if (len <= 0) {
271 close(imfd);
272 imfd = -1;
273 return 0;
274 }
275
276 exit |= process_messages(buf, len);
277
278 return !exit;
279 }