wsemul_dumb.c revision 1.5 1 /* $NetBSD: wsemul_dumb.c,v 1.5 1998/06/20 21:52:50 drochner Exp $ */
2
3 /*
4 * Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 static const char _copyright[] __attribute__ ((unused)) =
34 "Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.";
35 static const char _rcsid[] __attribute__ ((unused)) =
36 "$NetBSD: wsemul_dumb.c,v 1.5 1998/06/20 21:52:50 drochner Exp $";
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/time.h>
41 #include <sys/malloc.h>
42 #include <sys/fcntl.h>
43
44 #include <dev/wscons/wsconsio.h>
45 #include <dev/wscons/wsdisplayvar.h>
46 #include <dev/wscons/wsemulvar.h>
47 #include <dev/wscons/ascii.h>
48
49 void *wsemul_dumb_cnattach __P((const struct wsscreen_descr *, void *,
50 int, int, long));
51 void *wsemul_dumb_attach __P((int console, const struct wsscreen_descr *,
52 void *, int, int, void *, long));
53 void wsemul_dumb_output __P((void *cookie, const u_char *data, u_int count,
54 int));
55 int wsemul_dumb_translate __P((void *cookie, keysym_t, char **));
56 void wsemul_dumb_detach __P((void *cookie, u_int *crowp, u_int *ccolp));
57
58 const struct wsemul_ops wsemul_dumb_ops = {
59 "dumb",
60 wsemul_dumb_cnattach,
61 wsemul_dumb_attach,
62 wsemul_dumb_output,
63 wsemul_dumb_translate,
64 wsemul_dumb_detach,
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