wsemul_dumb.c revision 1.1 1 /* $NetBSD: wsemul_dumb.c,v 1.1 1998/03/22 14:24:03 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.1 1998/03/22 14:24:03 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));
51 void *wsemul_dumb_attach __P((int console, const struct wsscreen_descr *,
52 void *, int, int, void *));
53 void wsemul_dumb_output __P((void *cookie, const u_char *data, u_int count));
54 void wsemul_dumb_detach __P((void *cookie, u_int *crowp, u_int *ccolp));
55
56 const struct wsemul_ops wsemul_dumb_ops = {
57 "dumb",
58 wsemul_dumb_cnattach,
59 wsemul_dumb_attach,
60 wsemul_dumb_output,
61 wsemul_dumb_detach,
62 };
63
64 struct wsemul_dumb_emuldata {
65 const struct wsdisplay_emulops *emulops;
66 void *emulcookie;
67 void *cbcookie;
68 u_int nrows, ncols, crow, ccol;
69 };
70
71 struct wsemul_dumb_emuldata wsemul_dumb_console_emuldata;
72
73 void *
74 wsemul_dumb_cnattach(type, cookie, ccol, crow)
75 const struct wsscreen_descr *type;
76 void *cookie;
77 int ccol, crow;
78 {
79 struct wsemul_dumb_emuldata *edp;
80
81 edp = &wsemul_dumb_console_emuldata;
82
83 edp->emulops = type->textops;
84 edp->emulcookie = cookie;
85 edp->nrows = type->nrows;
86 edp->ncols = type->ncols;
87 edp->crow = crow;
88 edp->ccol = ccol;
89 edp->cbcookie = NULL;
90
91 return (edp);
92 }
93
94 void *
95 wsemul_dumb_attach(console, type, cookie, ccol, crow, cbcookie)
96 int console;
97 const struct wsscreen_descr *type;
98 void *cookie;
99 int ccol, crow;
100 void *cbcookie;
101 {
102 struct wsemul_dumb_emuldata *edp;
103
104 if (console)
105 edp = &wsemul_dumb_console_emuldata;
106 else {
107 edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
108
109 edp->emulops = type->textops;
110 edp->emulcookie = cookie;
111 edp->nrows = type->nrows;
112 edp->ncols = type->ncols;
113 edp->crow = crow;
114 edp->ccol = ccol;
115 }
116
117 edp->cbcookie = cbcookie;
118
119 return (edp);
120 }
121
122 void
123 wsemul_dumb_output(cookie, data, count)
124 void *cookie;
125 const u_char *data;
126 u_int count;
127 {
128 struct wsemul_dumb_emuldata *edp = cookie;
129 u_char c;
130 int n;
131
132 /* XXX */
133 (*edp->emulops->cursor)(edp->emulcookie, 0, edp->crow, edp->ccol);
134 while (count-- > 0) {
135 c = *data++;
136 switch (c) {
137 case ASCII_BEL:
138 wsdisplay_emulbell(edp->cbcookie);
139 break;
140
141 case ASCII_BS:
142 if (edp->ccol > 0)
143 edp->ccol--;
144 break;
145
146 case ASCII_CR:
147 edp->ccol = 0;
148 break;
149
150 case ASCII_HT:
151 n = min(8 - (edp->ccol & 7),
152 edp->ncols - edp->ccol - 1);
153 (*edp->emulops->erasecols)(edp->emulcookie,
154 edp->crow, edp->ccol, n);
155 edp->ccol += n;
156 break;
157
158 case ASCII_NP:
159 (*edp->emulops->eraserows)(edp->emulcookie, 0,
160 edp->nrows);
161 edp->ccol = 0;
162 edp->crow = 0;
163 break;
164
165 case ASCII_VT:
166 if (edp->crow > 0)
167 edp->crow--;
168 break;
169
170 default:
171 (*edp->emulops->putstr)(edp->emulcookie, edp->crow,
172 edp->ccol, &c, 1);
173 edp->ccol++;
174
175 /* if cur col is still on cur line, done. */
176 if (edp->ccol < edp->ncols)
177 break;
178
179 /* wrap the column around. */
180 edp->ccol = 0;
181
182 /* FALLTHRU */
183
184 case ASCII_LF:
185 /* if the cur line isn't the last, incr and leave. */
186 if (edp->crow < edp->nrows - 1) {
187 edp->crow++;
188 break;
189 }
190 n = 1; /* number of lines to scroll */
191 (*edp->emulops->copyrows)(edp->emulcookie, n, 0,
192 edp->nrows - n);
193 (*edp->emulops->eraserows)(edp->emulcookie,
194 edp->nrows - n, n);
195 edp->crow -= n - 1;
196 break;
197 }
198 }
199 /* XXX */
200 (*edp->emulops->cursor)(edp->emulcookie, 1, edp->crow, edp->ccol);
201 }
202
203 void
204 wsemul_dumb_detach(cookie, crowp, ccolp)
205 void *cookie;
206 u_int *crowp, *ccolp;
207 {
208 struct wsemul_dumb_emuldata *edp = cookie;
209
210 *crowp = edp->crow;
211 *ccolp = edp->ccol;
212 if (edp != &wsemul_dumb_console_emuldata)
213 free(edp, M_DEVBUF);
214 }
215