xref: /openbsd-src/sys/dev/wscons/wsemul_dumb.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* $OpenBSD: wsemul_dumb.c,v 1.1 2000/05/16 23:49:11 mickey Exp $ */
2 /* $NetBSD: wsemul_dumb.c,v 1.7 2000/01/05 11:19:36 drochner Exp $ */
3 
4 /*
5  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Christopher G. Demetriou
18  *	for the NetBSD Project.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39 #include <sys/malloc.h>
40 #include <sys/fcntl.h>
41 
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsdisplayvar.h>
44 #include <dev/wscons/wsemulvar.h>
45 #include <dev/wscons/ascii.h>
46 
47 void	*wsemul_dumb_cnattach __P((const struct wsscreen_descr *, void *,
48 				   int, int, long));
49 void	*wsemul_dumb_attach __P((int console, const struct wsscreen_descr *,
50 				 void *, int, int, void *, long));
51 void	wsemul_dumb_output __P((void *cookie, const u_char *data, u_int count,
52 				int));
53 int	wsemul_dumb_translate __P((void *cookie, keysym_t, char **));
54 void	wsemul_dumb_detach __P((void *cookie, u_int *crowp, u_int *ccolp));
55 void	wsemul_dumb_resetop __P((void *, enum wsemul_resetops));
56 
57 const struct wsemul_ops wsemul_dumb_ops = {
58 	"dumb",
59 	wsemul_dumb_cnattach,
60 	wsemul_dumb_attach,
61 	wsemul_dumb_output,
62 	wsemul_dumb_translate,
63 	wsemul_dumb_detach,
64 	wsemul_dumb_resetop
65 };
66 
67 struct wsemul_dumb_emuldata {
68 	const struct wsdisplay_emulops *emulops;
69 	void *emulcookie;
70 	void *cbcookie;
71 	u_int nrows, ncols, crow, ccol;
72 	long defattr;
73 };
74 
75 struct wsemul_dumb_emuldata wsemul_dumb_console_emuldata;
76 
77 void *
78 wsemul_dumb_cnattach(type, cookie, ccol, crow, defattr)
79 	const struct wsscreen_descr *type;
80 	void *cookie;
81 	int ccol, crow;
82 	long defattr;
83 {
84 	struct wsemul_dumb_emuldata *edp;
85 
86 	edp = &wsemul_dumb_console_emuldata;
87 
88 	edp->emulops = type->textops;
89 	edp->emulcookie = cookie;
90 	edp->nrows = type->nrows;
91 	edp->ncols = type->ncols;
92 	edp->crow = crow;
93 	edp->ccol = ccol;
94 	edp->defattr = defattr;
95 	edp->cbcookie = NULL;
96 
97 	return (edp);
98 }
99 
100 void *
101 wsemul_dumb_attach(console, type, cookie, ccol, crow, cbcookie, defattr)
102 	int console;
103 	const struct wsscreen_descr *type;
104 	void *cookie;
105 	int ccol, crow;
106 	void *cbcookie;
107 	long defattr;
108 {
109 	struct wsemul_dumb_emuldata *edp;
110 
111 	if (console)
112 		edp = &wsemul_dumb_console_emuldata;
113 	else {
114 		edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
115 
116 		edp->emulops = type->textops;
117 		edp->emulcookie = cookie;
118 		edp->nrows = type->nrows;
119 		edp->ncols = type->ncols;
120 		edp->crow = crow;
121 		edp->ccol = ccol;
122 		edp->defattr = defattr;
123 	}
124 
125 	edp->cbcookie = cbcookie;
126 
127 	return (edp);
128 }
129 
130 void
131 wsemul_dumb_output(cookie, data, count, kernel)
132 	void *cookie;
133 	const u_char *data;
134 	u_int count;
135 	int kernel; /* ignored */
136 {
137 	struct wsemul_dumb_emuldata *edp = cookie;
138 	u_char c;
139 	int n;
140 
141 	/* XXX */
142 	(*edp->emulops->cursor)(edp->emulcookie, 0, edp->crow, edp->ccol);
143 	while (count-- > 0) {
144 		c = *data++;
145 		switch (c) {
146 		case ASCII_BEL:
147 			wsdisplay_emulbell(edp->cbcookie);
148 			break;
149 
150 		case ASCII_BS:
151 			if (edp->ccol > 0)
152 				edp->ccol--;
153 			break;
154 
155 		case ASCII_CR:
156 			edp->ccol = 0;
157 			break;
158 
159 		case ASCII_HT:
160 			n = min(8 - (edp->ccol & 7),
161 			    edp->ncols - edp->ccol - 1);
162 			(*edp->emulops->erasecols)(edp->emulcookie,
163 			    edp->crow, edp->ccol, n, edp->defattr);
164 			edp->ccol += n;
165 			break;
166 
167 		case ASCII_FF:
168 			(*edp->emulops->eraserows)(edp->emulcookie, 0,
169 			    edp->nrows, edp->defattr);
170 			edp->ccol = 0;
171 			edp->crow = 0;
172 			break;
173 
174 		case ASCII_VT:
175 			if (edp->crow > 0)
176 				edp->crow--;
177 			break;
178 
179 		default:
180 			(*edp->emulops->putchar)(edp->emulcookie, edp->crow,
181 			    edp->ccol, c, edp->defattr);
182 			edp->ccol++;
183 
184 			/* if cur col is still on cur line, done. */
185 			if (edp->ccol < edp->ncols)
186 				break;
187 
188 			/* wrap the column around. */
189 			edp->ccol = 0;
190 
191                 	/* FALLTHRU */
192 
193 		case ASCII_LF:
194 	                /* if the cur line isn't the last, incr and leave. */
195 			if (edp->crow < edp->nrows - 1) {
196 				edp->crow++;
197 				break;
198 			}
199 			n = 1;		/* number of lines to scroll */
200 			(*edp->emulops->copyrows)(edp->emulcookie, n, 0,
201 			    edp->nrows - n);
202 			(*edp->emulops->eraserows)(edp->emulcookie,
203 			    edp->nrows - n, n, edp->defattr);
204 			edp->crow -= n - 1;
205 			break;
206 		}
207 	}
208 	/* XXX */
209 	(*edp->emulops->cursor)(edp->emulcookie, 1, edp->crow, edp->ccol);
210 }
211 
212 int
213 wsemul_dumb_translate(cookie, in, out)
214 	void *cookie;
215 	keysym_t in;
216 	char **out;
217 {
218 	return (0);
219 }
220 
221 void
222 wsemul_dumb_detach(cookie, crowp, ccolp)
223 	void *cookie;
224 	u_int *crowp, *ccolp;
225 {
226 	struct wsemul_dumb_emuldata *edp = cookie;
227 
228 	*crowp = edp->crow;
229 	*ccolp = edp->ccol;
230 	if (edp != &wsemul_dumb_console_emuldata)
231 		free(edp, M_DEVBUF);
232 }
233 
234 void
235 wsemul_dumb_resetop(cookie, op)
236 	void *cookie;
237 	enum wsemul_resetops op;
238 {
239 	struct wsemul_dumb_emuldata *edp = cookie;
240 
241 	switch (op) {
242 	case WSEMUL_CLEARSCREEN:
243 		(*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows,
244 					   edp->defattr);
245 		edp->ccol = edp->crow = 0;
246 		(*edp->emulops->cursor)(edp->emulcookie, 1, 0, 0);
247 		break;
248 	default:
249 		break;
250 	}
251 }
252