1 | /* |
---|
2 | * $Log: xtty.cc,v $ |
---|
3 | * Revision 1.2 2005/09/26 10:45:31 wahid |
---|
4 | * X11 application name changed |
---|
5 | * |
---|
6 | * Revision 1.1.1.1 2005/01/27 13:42:45 wahid |
---|
7 | * First project import |
---|
8 | * Wahid |
---|
9 | * |
---|
10 | * Revision 1.1 2002/03/19 15:03:22 boris |
---|
11 | * Vcitty model addition |
---|
12 | * |
---|
13 | * Revision 1.1.1.1 2002/02/28 12:58:33 disydent |
---|
14 | * Creation of Disydent CVS Tree |
---|
15 | * |
---|
16 | * Revision 1.1.1.1 2001/11/19 16:55:32 pwet |
---|
17 | * Changing the CVS tree structure of disydent |
---|
18 | * |
---|
19 | * Revision 1.1.1.1 2001/07/24 13:31:45 pwet |
---|
20 | * cvs tree of part of disydent |
---|
21 | * |
---|
22 | * Revision 1.1.1.1 2001/07/19 14:32:25 pwet |
---|
23 | * New cvs tree |
---|
24 | * |
---|
25 | * Revision 1.3 2000/11/14 14:04:02 pwet |
---|
26 | * Added an argument to main in tty.c and an argument in the call to xtty in |
---|
27 | * pitty.c in order for the tty name to reflect the name of the file used |
---|
28 | * to in the netlist. |
---|
29 | * |
---|
30 | * Revision 1.2 1998/09/16 16:11:32 pwet |
---|
31 | * passage a cvs |
---|
32 | * |
---|
33 | * Revision 1.1 1998/09/01 09:49:14 pwet |
---|
34 | * Initial revision |
---|
35 | * |
---|
36 | * Revision 1.1 1998/07/16 18:03:59 pwet |
---|
37 | * Initial revision |
---|
38 | * |
---|
39 | * Authors: Frédéric Pétrot and Denis Hommais |
---|
40 | */ |
---|
41 | |
---|
42 | //#ident "$Id: xtty.cc 81 2008-04-15 18:40:01Z rosiere $" |
---|
43 | #include <stdio.h> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <string.h> |
---|
46 | #include <sys/time.h> |
---|
47 | #include <sys/types.h> |
---|
48 | #include <fcntl.h> |
---|
49 | #include <unistd.h> |
---|
50 | #include <X11/Xlib.h> |
---|
51 | #include <signal.h> |
---|
52 | #include "xtty.h" |
---|
53 | |
---|
54 | #include <X11/Xutil.h> |
---|
55 | #include <X11/Xos.h> |
---|
56 | |
---|
57 | |
---|
58 | #ifdef __STDC__ |
---|
59 | static void ttySetGraphics(Colormap cmap, char *fname, xtty *tty) |
---|
60 | #else |
---|
61 | static void ttySetGraphics(cmap, fname, tty) |
---|
62 | Colormap cmap; |
---|
63 | char *fname; |
---|
64 | xtty *tty; |
---|
65 | #endif |
---|
66 | { |
---|
67 | XColor def, fit; |
---|
68 | register int i; |
---|
69 | static char *colorName[2] = {"Black", "Green"}; |
---|
70 | int colors[2]; |
---|
71 | XGCValues vGC; |
---|
72 | Font font; |
---|
73 | XFontStruct *qfst; |
---|
74 | |
---|
75 | |
---|
76 | XFreeColormap(tty->display, cmap); |
---|
77 | |
---|
78 | if ((qfst = XLoadQueryFont(tty->display, fname)) == NULL) { |
---|
79 | fprintf(stderr, "Font %s not available from X11 database.\n", fname); |
---|
80 | exit(-1); |
---|
81 | } |
---|
82 | tty->font_height = qfst->ascent + qfst->descent; |
---|
83 | tty->font_width = qfst->max_bounds.rbearing - qfst->min_bounds.lbearing; |
---|
84 | font = qfst->fid; |
---|
85 | /* XFreeFontInfo(&fname, qfst, 1); */ |
---|
86 | |
---|
87 | for (i = 0; i < 2; i++) { |
---|
88 | if (!XAllocNamedColor(tty->display, cmap, |
---|
89 | colorName[i], &def, &fit)) { |
---|
90 | colorName[i]=strdup("white"); |
---|
91 | XAllocNamedColor(tty->display, cmap, colorName[i], &def, &fit); |
---|
92 | } |
---|
93 | colors[i] = def.pixel; |
---|
94 | } |
---|
95 | |
---|
96 | vGC.fill_style = FillSolid; |
---|
97 | vGC.foreground = colors[1]; |
---|
98 | vGC.background = colors[0]; |
---|
99 | vGC.plane_mask = AllPlanes; |
---|
100 | vGC.font = font; |
---|
101 | tty->gc = XCreateGC(tty->display, |
---|
102 | RootWindow(tty->display, |
---|
103 | DefaultScreen(tty->display)), |
---|
104 | (GCForeground | GCBackground | GCFillStyle | |
---|
105 | GCPlaneMask | GCFont), &vGC); |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | #define OFFSET 3 |
---|
110 | |
---|
111 | #ifdef __STDC__ |
---|
112 | void RefreshXtty(xtty *tty) |
---|
113 | #else |
---|
114 | void RefreshXtty(tty) |
---|
115 | xtty *tty; |
---|
116 | #endif |
---|
117 | { |
---|
118 | int y; |
---|
119 | |
---|
120 | for (y = 0; y < TTYHEIGHT; y++) |
---|
121 | XDrawImageString(tty->display, |
---|
122 | tty->window, |
---|
123 | tty->gc, |
---|
124 | 0, |
---|
125 | y * tty->font_height - OFFSET, |
---|
126 | &tty->video[y][0], |
---|
127 | TTYWIDTH); |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | #ifdef __STDC__ |
---|
132 | static void UpdateXtty(xtty *tty, int refresh) |
---|
133 | #else |
---|
134 | static void UpdateXtty(tty, refresh) |
---|
135 | xtty *tty; |
---|
136 | int refresh; |
---|
137 | #endif |
---|
138 | { |
---|
139 | int x, y; |
---|
140 | |
---|
141 | if (tty->xcurse == 0) { |
---|
142 | x = TTYWIDTH - 1; |
---|
143 | y = tty->ycurse - 1; |
---|
144 | } else { |
---|
145 | x = tty->xcurse - 1; |
---|
146 | y = tty->ycurse; |
---|
147 | } |
---|
148 | if (refresh) |
---|
149 | RefreshXtty(tty); |
---|
150 | else |
---|
151 | XDrawImageString(tty->display, |
---|
152 | tty->window, |
---|
153 | tty->gc, |
---|
154 | x * tty->font_width, |
---|
155 | y * tty->font_height - OFFSET, |
---|
156 | &tty->video[y][x], 1); |
---|
157 | } |
---|
158 | |
---|
159 | #undef OFFSET |
---|
160 | |
---|
161 | #ifdef __STDC__ |
---|
162 | static int ManageXtty(xtty *tty, char c) |
---|
163 | #else |
---|
164 | static int ManageXtty(tty, c) |
---|
165 | xtty *tty; |
---|
166 | char c; |
---|
167 | #endif |
---|
168 | { |
---|
169 | int yy; |
---|
170 | int i; |
---|
171 | |
---|
172 | switch (c) { |
---|
173 | case 0x08: |
---|
174 | if (tty->xcurse > 0) { |
---|
175 | tty->video[tty->ycurse][--tty->xcurse] = ' '; |
---|
176 | return 1; |
---|
177 | } else |
---|
178 | XBell(tty->display, 10); |
---|
179 | break; |
---|
180 | |
---|
181 | case 0x09: |
---|
182 | for (i=0; i<8; i++) { |
---|
183 | tty->video[tty->ycurse][tty->xcurse] = ' '; |
---|
184 | tty->xcurse++; |
---|
185 | if (tty->xcurse >= TTYWIDTH) { |
---|
186 | tty->xcurse = 0; |
---|
187 | tty->ycurse++; |
---|
188 | } |
---|
189 | if (tty->ycurse >= TTYHEIGHT) { |
---|
190 | for (yy = 1; yy < TTYHEIGHT; yy++) |
---|
191 | memcpy(tty->video[yy - 1], tty->video[yy], TTYWIDTH); |
---|
192 | memset(tty->video[yy - 1], ' ', TTYWIDTH); |
---|
193 | tty->ycurse = yy - 1; |
---|
194 | return 1; |
---|
195 | } |
---|
196 | } |
---|
197 | break; |
---|
198 | |
---|
199 | case 0x0A: |
---|
200 | case 0x0D: |
---|
201 | tty->xcurse = 0; |
---|
202 | tty->ycurse++; |
---|
203 | break; |
---|
204 | |
---|
205 | default: |
---|
206 | tty->video[tty->ycurse][tty->xcurse] = c; |
---|
207 | tty->xcurse++; |
---|
208 | } |
---|
209 | if (tty->xcurse >= TTYWIDTH) { |
---|
210 | tty->xcurse = 0; |
---|
211 | tty->ycurse++; |
---|
212 | } |
---|
213 | if (tty->ycurse >= TTYHEIGHT) { |
---|
214 | for (yy = 1; yy < TTYHEIGHT; yy++) |
---|
215 | memcpy(tty->video[yy - 1], tty->video[yy], TTYWIDTH); |
---|
216 | memset(tty->video[yy - 1], ' ', TTYWIDTH); |
---|
217 | tty->ycurse = yy - 1; |
---|
218 | return 1; |
---|
219 | } |
---|
220 | return 0; |
---|
221 | } |
---|
222 | |
---|
223 | #ifdef __STDC__ |
---|
224 | static void EventXtty(xtty *tty, XEvent *event) |
---|
225 | #else |
---|
226 | static void EventXtty(tty, event) |
---|
227 | xtty *tty; |
---|
228 | XEvent *event; |
---|
229 | #endif |
---|
230 | { |
---|
231 | KeySym key; |
---|
232 | char c; |
---|
233 | |
---|
234 | switch (event->type) { |
---|
235 | case Expose: |
---|
236 | RefreshXtty(tty); |
---|
237 | break; |
---|
238 | |
---|
239 | case KeyPress: |
---|
240 | XLookupString(&event->xkey, &c, 1, &key, NULL); |
---|
241 | |
---|
242 | if ((key & 0x100) != 0 |
---|
243 | && key != XK_BackSpace |
---|
244 | && key != XK_BackSpace |
---|
245 | && key != XK_Tab |
---|
246 | && key != XK_Linefeed |
---|
247 | && key != XK_Return |
---|
248 | && key != XK_Escape |
---|
249 | && key != XK_Delete) |
---|
250 | |
---|
251 | break ; |
---|
252 | |
---|
253 | if (tty->wptr == tty->rptr && (tty->keybuf[tty->wptr] & 0x100) == 0x100) |
---|
254 | XBell(tty->display, 10); |
---|
255 | |
---|
256 | else { |
---|
257 | write( 1, &c, 1) ; |
---|
258 | // UpdateXtty(tty, ManageXtty(tty, c)); |
---|
259 | tty->keybuf[tty->wptr++] = 0 << 8 | c; //1 << 8 | c; |
---|
260 | tty->wptr %= 16; |
---|
261 | |
---|
262 | } |
---|
263 | break; |
---|
264 | |
---|
265 | default : |
---|
266 | return; |
---|
267 | } |
---|
268 | } |
---|
269 | |
---|
270 | #ifdef __STDC__ |
---|
271 | static Display *connect(Colormap *cmap) |
---|
272 | #else |
---|
273 | static Display *connect(cmap) |
---|
274 | Colormap *cmap; |
---|
275 | #endif |
---|
276 | { |
---|
277 | Display *display; |
---|
278 | |
---|
279 | if ((display = XOpenDisplay(NULL)) == NULL) { |
---|
280 | fputs("tty: cannot open X11 display\n", stderr); |
---|
281 | exit(1); |
---|
282 | } |
---|
283 | *cmap = DefaultColormap(display, DefaultScreen(display)); |
---|
284 | return display; |
---|
285 | } |
---|
286 | |
---|
287 | #ifdef __STDC__ |
---|
288 | static void DestroyXtty(xtty *tty) |
---|
289 | #else |
---|
290 | static void DestroyXtty(tty) |
---|
291 | xtty *tty; |
---|
292 | #endif |
---|
293 | { |
---|
294 | XDestroyWindow(tty->display, tty->window); |
---|
295 | XCloseDisplay(tty->display); |
---|
296 | } |
---|
297 | |
---|
298 | int main(int argc, char *argv[]) |
---|
299 | { |
---|
300 | timeval timeout ; |
---|
301 | xtty *tty; |
---|
302 | char *title; |
---|
303 | XSizeHints shint; |
---|
304 | //XWMHints wmhint; |
---|
305 | XEvent event; |
---|
306 | Colormap Cmap; |
---|
307 | char c; |
---|
308 | int rs; |
---|
309 | |
---|
310 | //fd_set rfds, wfds; |
---|
311 | fd_set rfds; |
---|
312 | int xfd; |
---|
313 | |
---|
314 | if (argc == 2) |
---|
315 | title = argv[1]; |
---|
316 | else |
---|
317 | title = "SoCLib TTY"; |
---|
318 | |
---|
319 | tty = (xtty *)malloc(sizeof(xtty)); |
---|
320 | memset(tty->video, ' ', TTYHEIGHT * TTYWIDTH); |
---|
321 | |
---|
322 | tty->xcurse = 0; |
---|
323 | tty->ycurse = 1; |
---|
324 | memset(tty->keybuf, 0, MAXCHAR * sizeof(short)); |
---|
325 | tty->rptr = 0; |
---|
326 | tty->wptr = 0; |
---|
327 | |
---|
328 | tty->display = connect(&Cmap); |
---|
329 | ttySetGraphics(Cmap, FONT, tty); |
---|
330 | |
---|
331 | shint.x = 100; |
---|
332 | shint.y = 0; |
---|
333 | shint.min_width = shint.max_width = shint.width = TTYWIDTH * tty->font_width; |
---|
334 | shint.min_height = shint.max_height = shint.height |
---|
335 | = (TTYHEIGHT - 1) * tty->font_height; |
---|
336 | |
---|
337 | shint.flags = PSize | PMinSize | PMaxSize; |
---|
338 | |
---|
339 | tty->window = XCreateSimpleWindow( |
---|
340 | tty->display, |
---|
341 | DefaultRootWindow(tty->display), |
---|
342 | shint.x, shint.y, shint.width, shint.height, 5, |
---|
343 | WhitePixelOfScreen(DefaultScreenOfDisplay(tty->display)), |
---|
344 | BlackPixelOfScreen(DefaultScreenOfDisplay(tty->display))); |
---|
345 | |
---|
346 | XSetStandardProperties(tty->display, tty->window, |
---|
347 | title, "Pouet", |
---|
348 | None, NULL, 0, &shint); |
---|
349 | |
---|
350 | /* |
---|
351 | if (XpmCreatePixmapFromData(tty->display, tty->window, afraid_xpm, |
---|
352 | &pixmap, &mask, NULL)) |
---|
353 | */ |
---|
354 | XSelectInput(tty->display, tty->window,// ExposureMask); |
---|
355 | |
---|
356 | KeyPressMask | ExposureMask); |
---|
357 | |
---|
358 | XMapRaised(tty->display, tty->window); |
---|
359 | |
---|
360 | /* |
---|
361 | for (i=0; i<15; i++) |
---|
362 | memcpy(tty->video[i+4], pouet[i], 65); |
---|
363 | */ |
---|
364 | |
---|
365 | xfd=ConnectionNumber(tty->display); |
---|
366 | FD_ZERO(&rfds); |
---|
367 | FD_SET(xfd, &rfds); |
---|
368 | FD_SET(0, &rfds); |
---|
369 | |
---|
370 | signal(SIGINT, SIG_IGN); |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | XFlush(tty->display); |
---|
377 | |
---|
378 | while (1) { |
---|
379 | |
---|
380 | XFlush(tty->display); |
---|
381 | |
---|
382 | FD_ZERO(&rfds); |
---|
383 | FD_SET(xfd, &rfds); |
---|
384 | FD_SET(0, &rfds); |
---|
385 | |
---|
386 | timeout.tv_sec = 2 ; |
---|
387 | timeout.tv_usec = 0 ; |
---|
388 | rs = select (xfd+1, &rfds, NULL, NULL, &timeout); |
---|
389 | |
---|
390 | if (rs !=0){ |
---|
391 | |
---|
392 | if ( FD_ISSET(0, &rfds) == 1) { |
---|
393 | read(0, &c, 1); |
---|
394 | UpdateXtty(tty, ManageXtty(tty, c)); |
---|
395 | RefreshXtty(tty); |
---|
396 | } |
---|
397 | |
---|
398 | |
---|
399 | if ( FD_ISSET(xfd, &rfds) == 1) { |
---|
400 | XNextEvent(tty->display, &event); |
---|
401 | EventXtty (tty, &event) ; |
---|
402 | RefreshXtty(tty); |
---|
403 | } |
---|
404 | |
---|
405 | } |
---|
406 | |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | //getchar(); |
---|
411 | |
---|
412 | |
---|
413 | |
---|
414 | |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | /* |
---|
424 | |
---|
425 | |
---|
426 | while (1) { |
---|
427 | FD_SET(xfd, &rfds); |
---|
428 | FD_SET(0, &rfds); |
---|
429 | XFlush(tty->display); |
---|
430 | rs=select(xfd+1, &rfds, NULL, NULL, NULL); |
---|
431 | if (FD_ISSET(0, &rfds)) { |
---|
432 | memset(tty->video, ' ', TTYHEIGHT * TTYWIDTH); |
---|
433 | RefreshXtty(tty); |
---|
434 | read(0, &c, 1); |
---|
435 | UpdateXtty(tty, ManageXtty(tty, c)); |
---|
436 | break; |
---|
437 | } else { |
---|
438 | while (XCheckWindowEvent(tty->display, tty->window, |
---|
439 | ExposureMask, &event)) |
---|
440 | RefreshXtty(tty); |
---|
441 | } |
---|
442 | } |
---|
443 | |
---|
444 | |
---|
445 | |
---|
446 | |
---|
447 | while (1) { |
---|
448 | FD_SET(xfd, &rfds); |
---|
449 | FD_SET(0, &rfds); |
---|
450 | XFlush(tty->display); |
---|
451 | rs=select(xfd+1, &rfds, NULL, NULL, NULL); |
---|
452 | if (FD_ISSET(0, &rfds)) { |
---|
453 | read(0, &c, 1); |
---|
454 | UpdateXtty(tty, ManageXtty(tty, c)); |
---|
455 | } else { |
---|
456 | while (XCheckWindowEvent(tty->display, tty->window, |
---|
457 | ExposureMask, &event)) |
---|
458 | RefreshXtty(tty); |
---|
459 | } |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | */ |
---|
464 | |
---|
465 | |
---|
466 | exit(0); |
---|
467 | } |
---|
468 | |
---|