summaryrefslogtreecommitdiff
path: root/tek4014-2.c
blob: d836c5f9bc276e14e5533331cf446448764f4e54 (plain)
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
/* $Id$ */

#include <vttest.h>
#include <esc.h>
#include <ttymodes.h>

#define TEKHEIGHT 3072
#define TEKWIDTH  4096

#define FIVEBITS  037
#define HIBITS    (FIVEBITS << SHIFTHI)
#define LOBITS    (FIVEBITS << SHIFTLO)
#define SHIFTHI   7
#define SHIFTLO   2
#define TWOBITS   03

#undef GS
#undef RS
#undef US

#define GS  0x001D
#define RS  0x001E
#define US  0x001F

/*
 * Switch to/from tek4014/vt100 mode.
 */
static void
tek_enable(int flag)
{
  if (flag)
    do_csi("?38h");
  else
    esc("\003");
}

/*
 * Switch to GIN (graphics-in) mode.
 */
static void
tek_GIN(void)
{
  esc("\032");
}

/*
 * Switch back to alpha (text) mode.
 */
static void
tek_ALP(void)
{
  printf("%c", US);
}

/*
 * Select a font
 */
static void
tek_font(int code)
{
  switch (code) {
  case 1:
    esc("8");   /* large */
    break;
  case 2:
    esc("9");   /* #2 */
    break;
  case 3:
    esc(":");   /* #3 */
    break;
  default:
    esc(";");   /* small */
    break;
  }
}

/*
 * Decode 2 bytes from a mouse report as a coordinate value.
 */
static int
tek_coord(char *report, int offset)
{
  int hi = FIVEBITS & CharOf(report[offset]);
  int lo = FIVEBITS & CharOf(report[offset + 1]);
  return (hi << 5) + lo;
}

/*
 * Send coordinates for a single point.  xterm reads this info in getpoint().
 * There are several possibilities for encoding the coordinates.
 */
static void
tek_point(int pen, int y, int x)
{
  char temp[20];

  if (pen) {
    sprintf(temp, "%c\007", GS);
  } else {
    sprintf(temp, "%c", GS);
  }
  sprintf(temp + strlen(temp), "%c%c%c%c%c",
         0x20 | (((y & HIBITS) >> SHIFTHI) & FIVEBITS),
         0x60 | (((y & TWOBITS) << SHIFTLO) | (x & TWOBITS)), /* tests/sets lo_y */
         0x60 | (((y & LOBITS) >> SHIFTLO) & FIVEBITS), /* sets lo_y */
         0x20 | (((x & HIBITS) >> SHIFTHI) & FIVEBITS),
         0x40 | (((x & LOBITS) >> SHIFTLO) & FIVEBITS)); /* must be last */
  fprintf(stdout, "%s", temp);
  if (LOG_ENABLED) {
    fprintf(log_fp, "*Set point (%d,%d)\n", y, x);
    fputs("Send: ", log_fp);
    put_string(log_fp, temp);
    fputs("\n", log_fp);
  }
}

static void
tek_linestyle(int style)
{
  char temp[10];
  sprintf(temp, "%c", 0x60 + style);
  esc(temp);
}

static void
log_mouse_click(char *report)
{
  if (LOG_ENABLED) {
    int new_x = tek_coord(report, 1);
    int new_y = tek_coord(report, 3);
    fprintf(log_fp, "Report: ");
    if ((report[0] & 0x80) != 0
     && strchr("lmrLMR", report[0] & 0x7f) != 0) {
      fprintf(log_fp, "mouse %c", report[0] & 0x7f);
    } else {
      fprintf(log_fp, "key %d", CharOf(report[0]));
    }
    fprintf(log_fp, " (%d,%d)\n", new_y, new_x);
    fflush(log_fp);
  }
}

/*
 * Clear the display
 */
static int
tek_clear(MENU_ARGS)
{
  tek_enable(1);
  esc("\014");
  tek_enable(0);
  return FALSE;
}

static int
tek_hello(MENU_ARGS)
{
  int n;

  tek_clear(PASS_ARGS);

  tek_enable(1);
  for (n = 0; n < 4; ++n) {
    tek_font(n);
    println("Hello world!\r");
  }
  tek_enable(0);
  return FALSE;
}

/*
 * Wait for a mouse click, printing its coordinates.  While in GIN mode, we
 * may also see keys pressed.  Exit the test when we see the same event twice
 * in a row.
 */
static int
tek_mouse_coords(MENU_ARGS)
{
  char *report;
  char status[6];
  int new_x = -1;
  int new_y = -1;

  tek_clear(PASS_ARGS);

  tek_enable(1);
  set_tty_raw(TRUE);
  set_tty_echo(FALSE);

  report = "";
  println("Any key or mouse click twice to exit...");
  do {
    strncpy(status, report, 5)[5] = 0;
    /*
     * The graphics-in mode is reset each time users send a mouse click.  So we
     * set it in the loop.
     */
    tek_GIN();
    log_mouse_click(report = instr());
    new_x = tek_coord(report, 1);
    new_y = tek_coord(report, 3);
    /*
     * If we do not start a new line after reading the mouse, we will see no
     * text.  So we do it before the rest of the report rather than after.
     */
    printf("\r\n");
    if ((report[0] & 0x80) != 0
     && strchr("lmrLMR", report[0] & 0x7f) != 0) {
      printf("mouse %c:", report[0] & 0x7f);
    } else {
      printf("key: %d", CharOf(report[0]));
    }
    printf(" (%d,%d)", new_y, new_x);
    fflush(stdout);
  } while (strcmp(report, status));

  tek_ALP();
  restore_ttymodes();
  tek_enable(0);
  return FALSE;
}

/*
 * Wait for a mouse click, drawing a line point-to-point from each click.
 * Ignore keys pressed.  Exit the test when we see the same event twice
 * in a row.
 *
 * xterm pretends the screen is 4096 by 3072 (height by width).  So the lines
 * appear in the lower-right of the screen.  Since we cannot ask xterm how
 * large the screen actually is, we do not try to (cannot) scale the lines.
 */
static int
tek_mouse_lines(MENU_ARGS)
{
  char *report;
  char status[6];
  int old_x = -1;
  int old_y = -1;
  int new_x = -1;
  int new_y = -1;

  tek_clear(PASS_ARGS);

  tek_enable(1);
  set_tty_raw(TRUE);
  set_tty_echo(FALSE);

  report = "";
  println("Any mouse click twice to exit...");
  do {
    strncpy(status, report, 5)[5] = 0;
    if (old_x >= 0 && old_y >= 0) {
      tek_point(0, old_y, old_x);
      tek_point(1, new_y, new_x);
      fflush(stdout);
    }
    old_x = new_x;
    old_y = new_y;
    tek_GIN();
    log_mouse_click(report = instr());
    new_x = tek_coord(report, 1);
    new_y = tek_coord(report, 3);
  } while (strcmp(report, status));

  restore_ttymodes();
  tek_enable(0);
  return FALSE;
}

/*
 * Draw a grid using a different line-type for each line, if possible.
 */
static int
tek_grid_demo(MENU_ARGS)
{
  int y, x;
  int style = 0;

  tek_clear(PASS_ARGS);
  tek_enable(1);
  for (y = 0; y <= TEKHEIGHT; y += TEKHEIGHT / 16) {
    tek_linestyle(style++ % 4);
    tek_point(0, y, 0);
    tek_point(1, y, TEKWIDTH - 1);
  }
  for (x = 0; x <= TEKWIDTH; x += TEKWIDTH / 16) {
    tek_linestyle(style++ % 4);
    tek_point(0, 0, x);
    tek_point(1, TEKHEIGHT - 1, x);
  }
  tek_ALP();    /* flush the plot */
  tek_enable(0);
  return FALSE;
}

/*
 * The common versions of xterm (X11R5, X11R6, XFree86) provide a Tektronix
 * 4014 emulation.  It can be configured out of XFree86 xterm, but that fact is
 * ignored by the cretins who quote rxvt's misleading manpage.  (The emulation
 * if unused accounts for 30kb of shared memory use).
 *
 * Other than its association with xterm, these tests do not really belong
 * in vttest, since the Tektronix is not even an ANSI terminal.
 */
int
tst_tek4014(MENU_ARGS)
{
  static MENU my_menu[] = {
    { "Exit",                                                0 },
    { "Clear screen",                                        tek_clear },
    { "'Hello World!' in each font",                         tek_hello },
    { "Get mouse-clicks, showing coordinates",               tek_mouse_coords },
    { "Get mouse-clicks, drawing lines between",             tek_mouse_lines },
    { "Draw a grid",                                         tek_grid_demo },
    { "",                                                    0 }
  };

  do {
    vt_clear(2);
    title(0); println("XTERM/tek4014 features");
    title(2); println("Choose test type:");
  } while (menu(my_menu));
  return MENU_NOHOLD;
}