Home | History | Annotate | Line # | Download | only in wscons
wsemul_dumb.c revision 1.6
      1 /* $NetBSD: wsemul_dumb.c,v 1.6 1999/01/17 15:44:57 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.6 1999/01/17 15:44:57 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 void	wsemul_dumb_resetop __P((void *, enum wsemul_resetops));
     58 
     59 const struct wsemul_ops wsemul_dumb_ops = {
     60 	"dumb",
     61 	wsemul_dumb_cnattach,
     62 	wsemul_dumb_attach,
     63 	wsemul_dumb_output,
     64 	wsemul_dumb_translate,
     65 	wsemul_dumb_detach,
     66 	wsemul_dumb_resetop
     67 };
     68 
     69 struct wsemul_dumb_emuldata {
     70 	const struct wsdisplay_emulops *emulops;
     71 	void *emulcookie;
     72 	void *cbcookie;
     73 	u_int nrows, ncols, crow, ccol;
     74 	long defattr;
     75 };
     76 
     77 struct wsemul_dumb_emuldata wsemul_dumb_console_emuldata;
     78 
     79 void *
     80 wsemul_dumb_cnattach(type, cookie, ccol, crow, defattr)
     81 	const struct wsscreen_descr *type;
     82 	void *cookie;
     83 	int ccol, crow;
     84 	long defattr;
     85 {
     86 	struct wsemul_dumb_emuldata *edp;
     87 
     88 	edp = &wsemul_dumb_console_emuldata;
     89 
     90 	edp->emulops = type->textops;
     91 	edp->emulcookie = cookie;
     92 	edp->nrows = type->nrows;
     93 	edp->ncols = type->ncols;
     94 	edp->crow = crow;
     95 	edp->ccol = ccol;
     96 	edp->defattr = defattr;
     97 	edp->cbcookie = NULL;
     98 
     99 	return (edp);
    100 }
    101 
    102 void *
    103 wsemul_dumb_attach(console, type, cookie, ccol, crow, cbcookie, defattr)
    104 	int console;
    105 	const struct wsscreen_descr *type;
    106 	void *cookie;
    107 	int ccol, crow;
    108 	void *cbcookie;
    109 	long defattr;
    110 {
    111 	struct wsemul_dumb_emuldata *edp;
    112 
    113 	if (console)
    114 		edp = &wsemul_dumb_console_emuldata;
    115 	else {
    116 		edp = malloc(sizeof *edp, M_DEVBUF, M_WAITOK);
    117 
    118 		edp->emulops = type->textops;
    119 		edp->emulcookie = cookie;
    120 		edp->nrows = type->nrows;
    121 		edp->ncols = type->ncols;
    122 		edp->crow = crow;
    123 		edp->ccol = ccol;
    124 		edp->defattr = defattr;
    125 	}
    126 
    127 	edp->cbcookie = cbcookie;
    128 
    129 	return (edp);
    130 }
    131 
    132 void
    133 wsemul_dumb_output(cookie, data, count, kernel)
    134 	void *cookie;
    135 	const u_char *data;
    136 	u_int count;
    137 	int kernel; /* ignored */
    138 {
    139 	struct wsemul_dumb_emuldata *edp = cookie;
    140 	u_char c;
    141 	int n;
    142 
    143 	/* XXX */
    144 	(*edp->emulops->cursor)(edp->emulcookie, 0, edp->crow, edp->ccol);
    145 	while (count-- > 0) {
    146 		c = *data++;
    147 		switch (c) {
    148 		case ASCII_BEL:
    149 			wsdisplay_emulbell(edp->cbcookie);
    150 			break;
    151 
    152 		case ASCII_BS:
    153 			if (edp->ccol > 0)
    154 				edp->ccol--;
    155 			break;
    156 
    157 		case ASCII_CR:
    158 			edp->ccol = 0;
    159 			break;
    160 
    161 		case ASCII_HT:
    162 			n = min(8 - (edp->ccol & 7),
    163 			    edp->ncols - edp->ccol - 1);
    164 			(*edp->emulops->erasecols)(edp->emulcookie,
    165 			    edp->crow, edp->ccol, n, edp->defattr);
    166 			edp->ccol += n;
    167 			break;
    168 
    169 		case ASCII_FF:
    170 			(*edp->emulops->eraserows)(edp->emulcookie, 0,
    171 			    edp->nrows, edp->defattr);
    172 			edp->ccol = 0;
    173 			edp->crow = 0;
    174 			break;
    175 
    176 		case ASCII_VT:
    177 			if (edp->crow > 0)
    178 				edp->crow--;
    179 			break;
    180 
    181 		default:
    182 			(*edp->emulops->putchar)(edp->emulcookie, edp->crow,
    183 			    edp->ccol, c, edp->defattr);
    184 			edp->ccol++;
    185 
    186 			/* if cur col is still on cur line, done. */
    187 			if (edp->ccol < edp->ncols)
    188 				break;
    189 
    190 			/* wrap the column around. */
    191 			edp->ccol = 0;
    192 
    193                 	/* FALLTHRU */
    194 
    195 		case ASCII_LF:
    196 	                /* if the cur line isn't the last, incr and leave. */
    197 			if (edp->crow < edp->nrows - 1) {
    198 				edp->crow++;
    199 				break;
    200 			}
    201 			n = 1;		/* number of lines to scroll */
    202 			(*edp->emulops->copyrows)(edp->emulcookie, n, 0,
    203 			    edp->nrows - n);
    204 			(*edp->emulops->eraserows)(edp->emulcookie,
    205 			    edp->nrows - n, n, edp->defattr);
    206 			edp->crow -= n - 1;
    207 			break;
    208 		}
    209 	}
    210 	/* XXX */
    211 	(*edp->emulops->cursor)(edp->emulcookie, 1, edp->crow, edp->ccol);
    212 }
    213 
    214 int
    215 wsemul_dumb_translate(cookie, in, out)
    216 	void *cookie;
    217 	keysym_t in;
    218 	char **out;
    219 {
    220 	return (0);
    221 }
    222 
    223 void
    224 wsemul_dumb_detach(cookie, crowp, ccolp)
    225 	void *cookie;
    226 	u_int *crowp, *ccolp;
    227 {
    228 	struct wsemul_dumb_emuldata *edp = cookie;
    229 
    230 	*crowp = edp->crow;
    231 	*ccolp = edp->ccol;
    232 	if (edp != &wsemul_dumb_console_emuldata)
    233 		free(edp, M_DEVBUF);
    234 }
    235 
    236 void
    237 wsemul_dumb_resetop(cookie, op)
    238 	void *cookie;
    239 	enum wsemul_resetops op;
    240 {
    241 	struct wsemul_dumb_emuldata *edp = cookie;
    242 
    243 	switch (op) {
    244 	case WSEMUL_CLEARSCREEN:
    245 		(*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows,
    246 					   edp->defattr);
    247 		edp->ccol = edp->crow = 0;
    248 		(*edp->emulops->cursor)(edp->emulcookie, 1, 0, 0);
    249 		break;
    250 	default:
    251 		break;
    252 	}
    253 }
    254