File: display-test.c

package info (click to toggle)
aoeui 1.7%2B20160302.git4e5dee9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 532 kB
  • sloc: ansic: 6,860; makefile: 294; sh: 11
file content (129 lines) | stat: -rw-r--r-- 3,896 bytes parent folder | download | duplicates (4)
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include "types.h"
#include "utf8.h"
#include "display.h"

void die(const char *, ...);
Boolean_t multiplexor(Boolean_t);

static struct display *D;
static int rows, columns;

static void dfill(int row, int rows, int col, int cols,
		  int ch, rgba_t fgrgba, rgba_t bgrgba)
{
	int j, k;
	for (j = 0; j < rows; j++)
		for (k = 0; k < cols; k++)
			display_put(D, row+j, col+k, ch, fgrgba, bgrgba);
}

static void dprint(int row, int col, rgba_t fgrgba, rgba_t bgrgba,
		   const char *format, ...)
{
	char buffer[256], *p;
	va_list ap;
	va_start(ap, format);
	vsnprintf(buffer, sizeof buffer, format, ap);
	va_end(ap);
	for (p = buffer; *p; p++)
		display_put(D, row, col++, *p, fgrgba, bgrgba);
}

static void dpause(void)
{
	int ch;
	dfill(rows-1, 1, 0, columns, ' ', RED_RGBA, WHITE_RGBA);
	dprint(rows-1, 0, RED_RGBA, WHITE_RGBA, "Hit Q to quit, or any other key to continue...");
	while ((ch = display_getch(D, 1)) == ERROR_CHANGED)
		display_get_geometry(D, &rows, &columns);
	if (ch == 'q' || ch == 'Q') {
		display_end(D);
		exit(EXIT_SUCCESS);
	}
}

void die(const char *msg, ...)
{
	va_list ap;
	display_end(D);
	va_start(ap, msg);
	vfprintf(stderr, msg, ap);
	va_end(ap);
	exit(EXIT_FAILURE);
}

Boolean_t multiplexor(Boolean_t block)
{
	return TRUE;
}

int main(void)
{
	if (tcgetattr(1, &original_termios))
		die("not running in a terminal");
	D = display_init();
	display_get_geometry(D, &rows, &columns);
	display_title(D, "display-test");
	dprint(0, 0, BLACK_RGBA, WHITE_RGBA, "geometry: %d rows, %d columns", rows, columns);
	dprint(1, 0, DEFAULT_FGRGBA, DEFAULT_BGRGBA, "default foreground and background");
	dprint(2, 0, WHITE_RGBA, BLACK_RGBA, "white foreground on black background");
	dprint(3, 0, BLACK_RGBA, WHITE_RGBA, "black foreground on white background");
	dprint(4, 0, RED_RGBA, BLUE_RGBA, "red foreground on blue background");
	dprint(5, 0, BLUE_RGBA, RED_RGBA, "blue foreground on red background");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "filled with red dots on green");
	dpause();

	display_erase(D, 0, 0, rows, columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after ERASEALL display_erase()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_erase(D, rows/2, 0, rows - rows/2, columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after ERASETOEND display_erase()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_erase(D, 1, columns/2, 1, columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after ERASELINE display_erase()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_erase(D, 1, 0, 1, columns/2);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after ERASECOLS display_erase()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_insert_lines(D, 2, 0, rows/2, rows-2, columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after display_insert_lines()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	dfill(rows-1, 1, 0, columns, '*', RED_RGBA, GREEN_RGBA);
	display_delete_lines(D, 2, 0, rows/2, rows-2, columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after display_delete_lines()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_insert_spaces(D, 2, 0, columns/2, columns);
	display_insert_spaces(D, 3, columns/2, columns - (columns/2), columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after display_insert_spaces()");
	dpause();

	dfill(0, rows, 0, columns, '.', RED_RGBA, GREEN_RGBA);
	display_delete_chars(D, 2, 0, columns/2, columns);
	display_delete_chars(D, 3, columns/2, columns - (columns/2), columns);
	dprint(0, 0, BLUE_RGBA, RED_RGBA, "after display_delete_chars()");
	dpause();

	display_end(D);
	printf("display-test done\n");
	return EXIT_SUCCESS;
}