aboutsummaryrefslogtreecommitdiffstats
path: root/com32/elflink/ldlinux/msg.c
blob: 502b58dec0e10064ebdf4581d4e72e7d32259cf1 (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
#include <com32.h>
#include <stdio.h>
#include <bios.h>
#include <graphics.h>

static uint8_t TextAttribute;	/* Text attribute for message file */
extern uint8_t DisplayMask;	/* Display modes mask */

/* Routine to interpret next print char */
static void (*NextCharJump)(uint8_t);

void msg_initvars(void);
static void msg_setfg(uint8_t data);
static void msg_putchar(uint8_t ch);

/*
 *
 * get_msg_file: Load a text file and write its contents to the screen,
 *               interpreting color codes.
 *
 * Returns 0 on success, -1 on failure.
 */
int get_msg_file(char *filename)
{
	FILE *f;
	char ch;

	f = fopen(filename, "r");
	if (!f)
		return -1;

	TextAttribute = 0x7;	/* Default grey on white */
	DisplayMask = 0x7;	/* Display text in all modes */
	msg_initvars();

	/*
	 * Read the text file a byte at a time and interpret that
	 * byte.
	 */
	while ((ch = getc(f)) != EOF) {
		/* DOS EOF? */
		if (ch == 0x1A)
			break;

		NextCharJump(ch);	/* Do what shall be done */
	}

	DisplayMask = 0x07;

	fclose(f);
	return 0;
}

static void msg_setbg(uint8_t data)
{
	if (unhexchar(&data) == 0) {
		data <<= 4;
		if (DisplayMask & UsingVGA) {
			TextAttribute = data;
		}

		NextCharJump = msg_setfg;
	} else {
		TextAttribute = 0x7;	/* Default attribute */
		NextCharJump = msg_putchar;
	}
}

static void msg_setfg(uint8_t data)
{
	if (unhexchar(&data) == 0) {
		if (DisplayMask & UsingVGA) {
			/* setbg set foreground to 0 */
			TextAttribute |= data;
		}
	} else
		TextAttribute = 0x7;	/* Default attribute */

	NextCharJump = msg_putchar;
}

static inline void msg_ctrl_o(void)
{
	NextCharJump = msg_setbg;
}

static void msg_formfeed(void)
{
	printf("\033[2J\033[H");
}

static void msg_novga(void)
{
	syslinux_force_text_mode();
	msg_initvars();
}

static void msg_viewimage(void)
{
	FILE *f;

	*VGAFilePtr = '\0';	/* Zero-terminate filename */

	mangle_name(VGAFileMBuf, VGAFileBuf);
	f = fopen(VGAFileMBuf, "r");
	if (!f) {
		/* Not there */
		NextCharJump = msg_putchar;
		return;
	}

	vgadisplayfile(f);
	fclose(f);
	msg_initvars();
}

/*
 * Getting VGA filename
 */
static void msg_filename(uint8_t data)
{
	/* <LF> = end of filename */
	if (data == 0x0A) {
		msg_viewimage();
		return;
	}

	/* Ignore space/control char */
	if (data > ' ') {
		if ((char *)VGAFilePtr < (VGAFileBuf + sizeof(VGAFileBuf)))
			*VGAFilePtr++ = data;
	}
}

static void msg_vga(void)
{
	NextCharJump = msg_filename;
	VGAFilePtr = (uint16_t *)VGAFileBuf;
}

/* Convert ANSI colors to PC display attributes */
static int convert_to_pcdisplay[] = { 0, 4, 2, 6, 1, 5, 3, 7 };

static void msg_normal(uint8_t data)
{
	uint8_t bg, fg;
	uint8_t mask = UsingVGA & 0x1;

	/* 0x1 = text mode, 0x2 = graphics mode */
	if (!(DisplayMask & ++mask) || !(DisplayCon & 0x01)) {
		/* Write to serial port */
		if (DisplayMask & 0x4)
			write_serial(data);

		return;		/* Not screen */
	}

	fg = convert_to_pcdisplay[(TextAttribute & 0x7)];
	bg = convert_to_pcdisplay[((TextAttribute >> 4) & 0x7)];

	printf("\033[");
	if (TextAttribute & 0x40)
		printf("1;"); /* Foreground bright */

	printf("3%dm\033[", fg);

	if (TextAttribute & 0x80)
		printf("5;"); /* Foreground blink */

	printf("4%dm%c\033[0m", bg, data);
}

static void msg_modectl(uint8_t data)
{
	data &= 0x07;
	DisplayMask = data;
	NextCharJump = msg_putchar;
}

static void msg_putchar(uint8_t ch)
{
	/* 10h to 17h are mode controls */
	if (ch >= 0x10 && ch < 0x18) {
		msg_modectl(ch);
		return;
	}

	switch (ch) {
	case 0x0F:		/* ^O = color code follows */
		msg_ctrl_o();
		break;
	case 0x0D:		/* Ignore <CR> */
		break;
	case 0x0C:		/* <FF> = clear screen */
		msg_formfeed();
		break;
	case 0x19:		/* <EM> = return to text mode */
		msg_novga();
		break;
	case 0x18:		/* <CAN> = VGA filename follows */
		msg_vga();
		break;
	default:
		msg_normal(ch);
		break;
	}
}

/*
 * Subroutine to initialize variables, also needed after loading
 * graphics file.
 */
void msg_initvars(void)
{
	/* Initialize state machine */
	NextCharJump = msg_putchar;
}