Debian Codegraph @

tek4014.c

raw
1 /* $Id: tek4014.c,v 1.27 2025/12/05 21:53:14 tom Exp $ */
2
3 #include <vttest.h>
4 #include <esc.h>
5 #include <ttymodes.h>
6
7 #define TEKHEIGHT 3072
8 #define TEKWIDTH 4096
9
10 #define FIVEBITS 037
11 #define HIBITS (FIVEBITS << SHIFTHI)
12 #define LOBITS (FIVEBITS << SHIFTLO)
13 #define SHIFTHI 7
14 #define SHIFTLO 2
15 #define TWOBITS 03
16
17 #undef GS
18 #undef RS
19 #undef US
20
21 #define ENQ 0x0005
22 #define FF 0x000C
23 #define GS 0x001D
24 #define RS 0x001E
25 #define US 0x001F
26
27 static float scale = 1.0;
28 static char empty[1];
29
30 /*
31 * Switch to/from tek4014/vt100 mode.
32 */
33 static void
34 tek_enable(int flag)
35 {
36 if (flag)
37 do_csi("?38h");
38 else
39 esc("\003");
40 }
41
42 /*
43 * Switch to GIN (graphics-in) mode.
44 */
45 static void
46 tek_GIN(void)
47 {
48 esc("\032");
49 }
50
51 /*
52 * Switch back to alpha (text) mode.
53 */
54 static void
55 tek_ALP(void)
56 {
57 put_char(stdout, US);
58 }
59
60 /*
61 * Decode 2 bytes from a mouse report as a coordinate value.
62 */
63 static int
64 tek_coord(const char *report, int offset)
65 {
66 int hi = FIVEBITS & CharOf(report[offset]);
67 int lo = FIVEBITS & CharOf(report[offset + 1]);
68 return (hi << 5) + lo;
69 }
70
71 /*
72 * Move to home (lower-left)
73 */
74 static void
75 tek_home(void)
76 {
77 esc("\014");
78 }
79
80 /*
81 * Move left one column (wraps at margin).
82 */
83 static void
84 tek_left(void)
85 {
86 put_char(stdout, BS);
87 }
88
89 /*
90 * Move down one row (wraps at margin).
91 */
92 static void
93 tek_down(void)
94 {
95 put_char(stdout, LF);
96 }
97
98 /*
99 * Get the current cursor position.
100 */
101 static void
102 tek_get_position(int *ypos, int *xpos)
103 {
104 char *report;
105 char buffer[10];
106
107 set_tty_raw(TRUE);
108 set_tty_echo(FALSE);
109
110 sprintf(buffer, "%c%c", ESC, ENQ);
111 put_string(stdout, buffer);
112 report = instr();
113
114 restore_ttymodes();
115
116 *xpos = tek_coord(report, 1);
117 *ypos = tek_coord(report, 3);
118 if (LOG_ENABLED) {
119 fprintf(log_fp, NOTE_STR "position %d,%d\n", *ypos, *xpos);
120 fputs(READ_STR, log_fp);
121 put_string(log_fp, report);
122 fputs("\n", log_fp);
123 }
124 }
125
126 #define SCALED(n) (int) ((float) (n) * scale)
127
128 /*
129 * Obtain the screensize.
130 */
131 static void
132 tek_get_screensize(void)
133 {
134 int y, x;
135 int height, width;
136
137 pause_replay();
138 tek_home();
139 tek_left();
140 tek_get_position(&y, &width);
141
142 tek_home();
143 tek_down();
144 tek_get_position(&height, &x);
145
146 if (width > 0 && height > 0) {
147 float yscale = TEKHEIGHT / (float) height;
148 float xscale = TEKWIDTH / (float) width;
149 scale = xscale > yscale ? xscale : yscale;
150 } else {
151 scale = 1.0;
152 }
153 if (LOG_ENABLED) {
154 fprintf(log_fp, NOTE_STR "screensize %d,%d\n", height, width);
155 fprintf(log_fp, NOTE_STR "scale %.1f\n", scale);
156 }
157
158 tek_home();
159 resume_replay();
160 }
161
162 /*
163 * Select a font
164 */
165 static void
166 tek_font(int code)
167 {
168 switch (code) {
169 case 1:
170 esc("8"); /* large */
171 break;
172 case 2:
173 esc("9"); /* #2 */
174 break;
175 case 3:
176 esc(":"); /* #3 */
177 break;
178 default:
179 esc(";"); /* small */
180 break;
181 }
182 }
183
184 /*
185 * Send coordinates for a single point. xterm reads this info in getpoint().
186 * There are several possibilities for encoding the coordinates.
187 */
188 static void
189 tek_point(int pen, int y, int x)
190 {
191 char temp[20];
192
193 if (pen) {
194 sprintf(temp, "%c\007", GS);
195 } else {
196 sprintf(temp, "%c", GS);
197 }
198 sprintf(temp + strlen(temp), "%c%c%c%c%c",
199 0x20 | (((y & HIBITS) >> SHIFTHI) & FIVEBITS),
200 0x60 | (((y & TWOBITS) << SHIFTLO) | (x & TWOBITS)), /* tests/sets lo_y */
201 0x60 | (((y & LOBITS) >> SHIFTLO) & FIVEBITS), /* sets lo_y */
202 0x20 | (((x & HIBITS) >> SHIFTHI) & FIVEBITS),
203 0x40 | (((x & LOBITS) >> SHIFTLO) & FIVEBITS)); /* must be last */
204 fprintf(stdout, "%s", temp);
205 if (LOG_ENABLED) {
206 fprintf(log_fp, NOTE_STR "set point (%d,%d)\n", y, x);
207 fputs(SEND_STR, log_fp);
208 put_string(log_fp, temp);
209 fputs("\n", log_fp);
210 }
211 }
212
213 static void
214 tek_linestyle(int style)
215 {
216 char temp[10];
217 sprintf(temp, "%c", 0x60 + style);
218 esc(temp);
219 }
220
221 static void
222 log_mouse_click(char *report)
223 {
224 if (LOG_ENABLED) {
225 int new_x = tek_coord(report, 1);
226 int new_y = tek_coord(report, 3);
227 if ((report[0] & 0x80) != 0
228 && strchr("lmrLMR", report[0] & 0x7f) != NULL) {
229 fprintf(log_fp, NOTE_STR "mouse %c", report[0] & 0x7f);
230 } else {
231 fprintf(log_fp, NOTE_STR "key %d", CharOf(report[0]));
232 }
233 fprintf(log_fp, " (%d,%d)\n", new_y, new_x);
234 fflush(log_fp);
235 }
236 }
237
238 /*
239 * Clear the display
240 */
241 static int
242 tek_clear(MENU_ARGS)
243 {
244 tek_enable(1);
245 tek_get_screensize();
246 esc("\014");
247 tek_enable(0);
248 return FALSE;
249 }
250
251 static int
252 tek_hello(MENU_ARGS)
253 {
254 int n;
255
256 tek_clear(PASS_ARGS);
257
258 tek_enable(1);
259 for (n = 0; n < 4; ++n) {
260 tek_font(n);
261 println("Hello world!\r");
262 }
263 tek_enable(0);
264 return FALSE;
265 }
266
267 /*
268 * Wait for a mouse click, printing its coordinates. While in GIN mode, we
269 * may also see keys pressed. Exit the test when we see the same event twice
270 * in a row.
271 */
272 static int
273 tek_mouse_coords(MENU_ARGS)
274 {
275 char *report;
276 char status[6];
277
278 tek_clear(PASS_ARGS);
279
280 tek_enable(1);
281 set_tty_raw(TRUE);
282 set_tty_echo(FALSE);
283
284 report = empty;
285 println("Any key or mouse click twice to exit...");
286 status[0] = '\0';
287
288 pause_replay();
289 do {
290 int new_x;
291 int new_y;
292
293 strncpy(status, report, (size_t) 5)[5] = 0;
294 /*
295 * The graphics-in mode is reset each time users send a mouse click. So we
296 * set it in the loop.
297 */
298 tek_GIN();
299 log_mouse_click(report = instr());
300 new_x = tek_coord(report, 1);
301 new_y = tek_coord(report, 3);
302 /*
303 * If we do not start a new line after reading the mouse, we will see no
304 * text. So we do it before the rest of the report rather than after.
305 */
306 printxx("\r\n");
307 if ((report[0] & 0x80) != 0
308 && strchr("lmrLMR", report[0] & 0x7f) != NULL) {
309 printxx("mouse %c:", report[0] & 0x7f);
310 } else {
311 printxx("key: %d", CharOf(report[0]));
312 }
313 printxx(" (%d,%d)", new_y, new_x);
314 fflush(stdout);
315 } while (strcmp(report, status));
316
317 tek_ALP();
318
319 resume_replay();
320 restore_ttymodes();
321 tek_enable(0);
322 return FALSE;
323 }
324
325 /*
326 * Wait for a mouse click, drawing a line point-to-point from each click.
327 * Ignore keys pressed. Exit the test when we see the same event twice
328 * in a row.
329 *
330 * xterm pretends the screen is 4096 by 3072 (height by width). So the lines
331 * appear in the lower-right of the screen. Since we cannot ask xterm how
332 * large the screen actually is, we do not try to (cannot) scale the lines.
333 */
334 static int
335 tek_mouse_lines(MENU_ARGS)
336 {
337 char *report;
338 char status[6];
339 int old_x = -1;
340 int old_y = -1;
341 int new_x = -1;
342 int new_y = -1;
343
344 tek_clear(PASS_ARGS);
345
346 tek_enable(1);
347 set_tty_raw(TRUE);
348 set_tty_echo(FALSE);
349
350 report = empty;
351 println("Any mouse click twice to exit...");
352 status[0] = '\0';
353
354 pause_replay();
355 do {
356 strncpy(status, report, (size_t) 5)[5] = 0;
357 if (old_x >= 0 && old_y >= 0) {
358 tek_point(0, SCALED(old_y), SCALED(old_x));
359 tek_point(1, SCALED(new_y), SCALED(new_x));
360 fflush(stdout);
361 }
362 old_x = new_x;
363 old_y = new_y;
364 tek_GIN();
365 log_mouse_click(report = instr());
366 new_x = tek_coord(report, 1);
367 new_y = tek_coord(report, 3);
368 } while (strcmp(report, status));
369
370 resume_replay();
371 restore_ttymodes();
372 tek_enable(0);
373 return FALSE;
374 }
375
376 /*
377 * Draw a grid using a different line-type for each line, if possible.
378 */
379 static int
380 tek_grid_demo(MENU_ARGS)
381 {
382 int y, x;
383 int style = 0;
384
385 tek_clear(PASS_ARGS);
386 tek_enable(1);
387 for (y = 0; y <= TEKHEIGHT; y += TEKHEIGHT / 16) {
388 tek_linestyle(style++ % 4);
389 tek_point(0, y, 0);
390 tek_point(1, y, TEKWIDTH - 1);
391 }
392 for (x = 0; x <= TEKWIDTH; x += TEKWIDTH / 16) {
393 tek_linestyle(style++ % 4);
394 tek_point(0, 0, x);
395 tek_point(1, TEKHEIGHT - 1, x);
396 }
397 tek_ALP(); /* flush the plot */
398 tek_enable(0);
399 return FALSE;
400 }
401
402 /*
403 * The common versions of xterm (X11R5, X11R6, XFree86) provide a Tektronix
404 * 4014 emulation. It can be configured out of XFree86 xterm, but that fact is
405 * ignored by the cretins who quote rxvt's misleading manpage. (The emulation
406 * if unused accounts for 30kb of shared memory use).
407 *
408 * Other than its association with xterm, these tests do not really belong
409 * in vttest, since the Tektronix is not even an ANSI terminal.
410 */
411 int
412 tst_tek4014(MENU_ARGS)
413 {
414 /* *INDENT-OFF* */
415 static MENU my_menu[] = {
416 { "Exit", NULL },
417 { "Clear screen", tek_clear },
418 { "'Hello World!' in each font", tek_hello },
419 { "Get mouse-clicks, showing coordinates", tek_mouse_coords },
420 { "Get mouse-clicks, drawing lines between", tek_mouse_lines },
421 { "Draw a grid", tek_grid_demo },
422 { "", NULL }
423 };
424 /* *INDENT-ON* */
425
426 do {
427 vt_clear(2);
428 __(title(0), println("XTERM/tek4014 features"));
429 __(title(2), println("Choose test type:"));
430 } while (menu(my_menu));
431 return MENU_NOHOLD;
432 }