-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxintfc.cpp
371 lines (299 loc) · 8.58 KB
/
xintfc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright Timothy Miller, 1999
#include "pseudo.hpp"
#include "gterm.hpp"
#include "xintfc.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
Display *display;
Window window, root;
GC gc;
int screen;
unsigned long fgcolor, bgcolor;
Colormap cmap;
int master_fd;
XFontStruct *font_info = NULL;
int font_height, font_width;
XFontStruct *load_font(char *name)
{
XFontStruct *inf;
if (font_info) XFreeFont(display, font_info);
inf = XLoadQueryFont(display, name);
if (!inf) {
fprintf(stderr, "Can't load this font\n");
exit(-1);
}
font_info = inf;
font_width = inf->max_bounds.width;
font_height = inf->ascent + inf->descent;
return inf;
}
int colors[8], cursor_color;
int alloccolor(int red, int green, int blue)
{
XColor col;
col.red = red;
col.green = green;
col.blue = blue;
XAllocColor(display, cmap, &col);
XFlush(display);
return col.pixel;
}
void set_hints(Window d, int w, int h)
{
XSizeHints hints;
hints.width_inc = w;
hints.height_inc = h;
hints.flags = PResizeInc;
XSetWMNormalHints(display, d, &hints);
}
int init_x(char *winname)
{
display = XOpenDisplay("");
if (!display) return -1;
screen = DefaultScreen(display);
root = RootWindow(display, screen);
bgcolor = WhitePixel(display, screen);
fgcolor = BlackPixel(display, screen);
gc = XCreateGC(display, root, 0, 0);
font_info = load_font("6x13");
XSetFont(display, gc, font_info->fid);
window = XCreateSimpleWindow(display, root,
1, 1, 80*font_width, 24*font_height, 1, bgcolor, fgcolor);
set_hints(window, font_width, font_height);
XSetBackground(display, gc, bgcolor);
XSetForeground(display, gc, fgcolor);
cmap = XDefaultColormapOfScreen(DefaultScreenOfDisplay(display));
colors[0] = alloccolor(0, 0, 0);
colors[1] = alloccolor(65535, 0, 0);
colors[2] = alloccolor(0, 65535, 0);
colors[3] = alloccolor(65535, 65535, 0);
colors[4] = alloccolor(0, 0, 65535);
colors[5] = alloccolor(65535, 0, 65535);
colors[6] = alloccolor(0, 65535, 65535);
colors[7] = alloccolor(65535, 65535, 65535);
cursor_color = alloccolor(32768, 32768, 32768);
XMapRaised(display, window);
XSelectInput(display, window, ExposureMask | KeyPressMask |
StructureNotifyMask | KeyPressMask | StructureNotifyMask);
XStoreName(display, window, winname);
XFlush(display);
return ConnectionNumber(display);
}
void bye_x()
{
XFreeGC(display, gc);
XDestroyWindow(display, window);
XCloseDisplay(display);
}
void key_hit(GTerm *t, KeySym keysym, char *kbuf, int count)
{
int mode;
char *str = 0, *crlf = "\r\n";
mode = t->GetMode();
if (mode & GTerm::KEYAPPMODE) {
str = find_key(keysym, keypadappkeys);
} else {
str = find_key(keysym, keypadkeys);
}
if (!str) {
if (mode & GTerm::CURSORAPPMODE) {
str = find_key(keysym, cursorappkeys);
} else {
str = find_key(keysym, cursorkeys);
}
}
if (!str) {
str = find_key(keysym, otherkeys);
}
if (!str && !count) return;
if (!str) {
str = kbuf;
str[count] = 0;
}
if ((mode & GTerm::NEWLINE) && str[0] == 13 && str[1] == 0)
str = crlf;
count = strlen(str);
write(master_fd, str, count);
if (mode & GTerm::LOCALECHO)
t->ProcessInput(count, (unsigned char *)str);
}
int check_event(GTerm *t)
{
XEvent event;
int x1, y1, x2, y2;
KeySym keysym;
char kbuf[20];
int charcount;
int ret = 0;
while (XCheckMaskEvent(display, -1, &event)) {
switch (event.type) {
case MappingNotify:
XRefreshKeyboardMapping((XMappingEvent *)&event);
break;
case KeyPress:
charcount = XLookupString((XKeyEvent *)&event, kbuf,
20, &keysym, 0);
key_hit(t, keysym, kbuf, charcount);
if (t->GetMode() & GTerm::LOCALECHO) ret = 1;
break;
case Expose:
case GraphicsExpose:
x1 = event.xexpose.x / font_width;
y1 = event.xexpose.y / font_height;
x2 = (event.xexpose.x+event.xexpose.width-1)/font_width;
y2 = (event.xexpose.y+event.xexpose.height-1)/font_height;
t->ExposeArea(x1, y1, x2-x1+1, y2-y1+1);
ret = 1;
break;
case ConfigureNotify:
t->ResizeTerminal(event.xconfigure.width / font_width,
event.xconfigure.height / font_height);
break;
}
}
return ret;
}
void XTerm::DrawText(int fg_color, int bg_color, int flags,
int x, int y, int len, unsigned char *string)
{
int t;
if (fg_color == 0 && (flags&BOLD)) {
fg_color = 7;
flags &= ~BOLD;
}
if (fg_color == 0 && (flags&BOLD)) {
fg_color = 7;
flags &= ~BOLD;
}
if (flags & INVERSE) {
t = fg_color;
fg_color = bg_color;
bg_color = t;
}
XSetBackground(display, gc, colors[bg_color]);
XSetForeground(display, gc, colors[fg_color]);
XDrawImageString(display, window, gc, x*font_width, y*font_height+font_info->ascent,
(char *)string, len);
if (flags&BOLD)
XDrawString(display, window, gc, x*font_width+1, y*font_height+font_info->ascent,
(char *)string, len);
if (flags&UNDERLINE)
XDrawLine(display, window, gc, x*font_width, y*font_height+font_height-1, (x+len)*font_width-1, y*font_height+font_height-1);
}
void XTerm::DrawCursor(int fg_color, int bg_color, int flags,
int x, int y, unsigned char c)
{
unsigned char str[2];
str[0] = c;
str[1] = 0;
DrawText(7-fg_color, 7-bg_color, flags, x, y, 1, str);
#if 0
XSetForeground(display, gc, cursor_color);
XDrawLine(display, window, gc, x*font_width, y*font_height, x*font_width+font_width-1, y*font_height);
XDrawLine(display, window, gc, x*font_width+font_width-1, y*font_height, x*font_width+font_width-1, y*font_height+font_height-1);
XDrawLine(display, window, gc, x*font_width+font_width-1, y*font_height+font_height-1, x*font_width, y*font_height+font_height-1);
XDrawLine(display, window, gc, x*font_width, y*font_height+font_height-1, x*font_width, y*font_height);
#endif
}
void XTerm::MoveChars(int sx, int sy, int dx, int dy, int w, int h)
{
XCopyArea(display, window, window, gc,
sx*font_width, sy*font_height, w*font_width, h*font_height, dx*font_width, dy*font_height);
// Must always check for GraphicsExpose events immediately
XSync(display, False);
check_event(this);
}
void XTerm::ClearChars(int bg_color, int x, int y, int w, int h)
{
XSetForeground(display, gc, colors[bg_color]);
XFillRectangle(display, window, gc, x*font_width, y*font_height, w*font_width, h*font_height);
}
void XTerm::SendBack(char *data)
{
write(master_fd, data, strlen(data));
}
void XTerm::Bell()
{
XBell(display, 0);
}
void XTerm::RequestSizeChange(int w, int h)
{
if (w != Width() || h != Height()) {
XResizeWindow(display, window, w*font_width, h*font_height);
ResizeTerminal(w, h);
}
}
static volatile int got_sigchild = 0;
static void sigchild_handler(int signum)
{
got_sigchild++;
}
int main()
{
unsigned char buf[1000];
XTerm term;
fd_set rset;
PseudoTerminal p;
int retval = 0,xfd, high, i, ret, need_timeout = 1;
struct timeval tv;
struct sigaction child_action;
/* setup X11 */
xfd = init_x("Terminal");
if (xfd<0) exit(-1);
child_action.sa_handler = sigchild_handler;
sigemptyset (&child_action.sa_mask);
child_action.sa_flags |= SA_NOCLDSTOP;
sigaction(SIGCHLD,&child_action,NULL);
/* spawn shell in pseudo terminal */
if(!p.init() || !p.spawn("/bin/sh")) {
fprintf(stderr,
"Error: Could not open pty or could not spawn shell.\n");
exit(-1);
}
master_fd = p.get_master();
/* we want non-blocking reads from pty output */
fcntl(master_fd, F_SETFL, O_NONBLOCK);
/* configure terminal for deferred display updates */
term.set_mode_flag(GTerm::DEFERUPDATE);
i = 1;
while (i>=0) {
/* flush Xlib buffer and then check for any new events */
XFlush(display);
if (check_event(&term)) need_timeout = 1;
/* select on input from terminal and x server */
FD_ZERO(&rset);
FD_SET(master_fd, &rset); high = master_fd;
FD_SET(xfd, &rset); if (xfd>high) high=xfd;
/* delay for terminal update */
tv.tv_sec = 0;
tv.tv_usec = 10000;
ret = select(high+1, &rset, NULL, NULL,
need_timeout ? (&tv) : NULL);
if (!ret) { /* timer expired */
term.Update();
need_timeout = 0;
}
if (FD_ISSET(master_fd, &rset)) {
i = read(master_fd, buf, 1000);
if (i>0) term.ProcessInput(i, buf);
need_timeout = 1;
}
while(got_sigchild) {
got_sigchild = 0;
if(waitpid(p.get_pid(),&retval,WNOHANG)==p.get_pid() &&
(WIFEXITED(retval)||WIFSIGNALED(retval))) {
i = -1;
}
}
/* no need to check xfd since that is done
unconditionally at top of loop */
}
p.done();
return retval;
}