/*
* Copyright (c) 1996, 1997 Christopher G. Demetriou. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou
* for the NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
u_int state; /* processing state */
u_int args[SUN_EMUL_NARGS]; /* command args, if CONTROL */
u_int scrolldist; /* distance to scroll */
long defattr; /* default attribute (rendition) */
long bowattr; /* attribute for reversed mode */
int rendflags;
#define REND_BOW 1
#define REND_SO 2
long curattr; /* currently used attribute */
long kernattr; /* attribute for kernel output */
#ifdef DIAGNOSTIC
int console;
#endif
};
void *
wsemul_sun_cnattach(const struct wsscreen_descr *type, void *cookie,
int ccol, int crow, long defattr)
{
struct wsemul_sun_emuldata *edp;
int res;
case ASCII_BS: /* "Backspace (BS)" */
if (edp->ccol > 0)
edp->ccol--;
break;
case ASCII_CR: /* "Return (CR)" */
edp->ccol = 0;
break;
case ASCII_HT: /* "Tab (TAB)" */
n = uimin(8 - (edp->ccol & 7), COLS_LEFT);
(*edp->emulops->erasecols)(edp->emulcookie, edp->crow,
edp->ccol, n,
kernel ? edp->kernattr : edp->curattr);
edp->ccol += n;
break;
case ASCII_FF: /* "Form Feed (FF)" */
(*edp->emulops->eraserows)(edp->emulcookie, 0, edp->nrows,
kernel ? edp->kernattr : edp->curattr);
/* XXX possible in kernel output? */
edp->ccol = 0;
edp->crow = 0;
break;
case ASCII_VT: /* "Reverse Line Feed" */
if (edp->crow > 0)
edp->crow--;
break;
case ASCII_ESC: /* "Escape (ESC)" */
if (kernel) {
printf("wsemul_sun_output_normal: ESC in kernel output ignored\n");
break; /* ignore the ESC */
}
if (edp->state == SUN_EMUL_STATE_NORMAL) {
newstate = SUN_EMUL_STATE_HAVEESC;
break;
}
/* special case: fall through, we're printing one out */
/* FALLTHRU */
default: /* normal character */
(*edp->emulops->putchar)(edp->emulcookie, edp->crow, edp->ccol,
c, kernel ? edp->kernattr : edp->curattr);
edp->ccol++;
/* if cur col is still on cur line, done. */
if (edp->ccol < edp->ncols)
break;
/* wrap the column around. */
edp->ccol = 0;
/* FALLTHRU */
case ASCII_LF: /* "Line Feed (LF)" */
/* if the cur line isn't the last, incr and leave. */
if (edp->crow < edp->nrows - 1) {
edp->crow++;
break;
}
/*
* if we're in wrap-around mode, go to the first
* line and clear it.
*/
if (edp->scrolldist == 0) {
edp->crow = 0;
(*edp->emulops->eraserows)(edp->emulcookie, 0, 1,
edp->curattr);
break;
}
switch (c) {
case '[': /* continuation of multi-char sequence */
memset(edp->args, 0, sizeof (edp->args));
newstate = SUN_EMUL_STATE_CONTROL;
break;
default:
/* spit out the escape char (???), then the new character */
wsemul_sun_output_normal(edp, ASCII_ESC, 0); /* ??? */
newstate = wsemul_sun_output_normal(edp, c, 0);
break;
}
return (newstate);
}
static inline void
wsemul_sun_control(struct wsemul_sun_emuldata *edp, u_char c)
{
u_int n, src, dst;
switch (c) {
case '0': case '1': case '2': case '3': case '4': /* argument digit */
case '5': case '6': case '7': case '8': case '9':
edp->args[0] = (edp->args[0] * 10) + (c - '0');
break;
case ';': /* argument terminator */
for (i = 1; i < SUN_EMUL_NARGS; i++)
edp->args[i] = edp->args[i - 1];
edp->args[0] = 0;
break;
default: /* end of escape sequence */
wsemul_sun_control(edp, c);
newstate = SUN_EMUL_STATE_NORMAL;
break;
}
return (newstate);
}
int
wsemul_sun_translate(void *cookie, keysym_t in, const char **out)
{
static char c;
if (KS_GROUP(in) == KS_GROUP_Plain) {
/* allow ISO-1 */
c = KS_VALUE(in);
*out = &c;
return (1);
}
if (KS_GROUP(in) == KS_GROUP_Keypad && (in & 0x80) == 0) {
c = in & 0xff; /* turn into ASCII */
*out = &c;
return (1);
}
if (in >= KS_f1 && in <= KS_f10) {
*out = sun_fkeys[in - KS_f1];
return (6);
}
if (in >= KS_F1 && in <= KS_F10) {
*out = sun_fkeys[in - KS_F1];
return (6);
}
if (in >= KS_KP_F1 && in <= KS_KP_F4) {
*out = sun_fkeys[in - KS_KP_F1];
return (6);
}
switch (in) {
case KS_Home:
case KS_KP_Home:
case KS_KP_Begin:
*out = "\033[214z";
return (6);
case KS_End:
case KS_KP_End:
*out = "\033[220z";
return (6);
case KS_Prior:
case KS_KP_Prior:
*out = "\033[216z";
return (6);
case KS_Next:
case KS_KP_Next:
*out = "\033[222z";
return (6);
case KS_Up:
case KS_KP_Up:
*out = "\033[A";
return (3);
case KS_Down:
case KS_KP_Down:
*out = "\033[B";
return (3);
case KS_Left:
case KS_KP_Left:
*out = "\033[D";
return (3);
case KS_Right:
case KS_KP_Right:
*out = "\033[C";
return (3);
case KS_KP_Delete:
*out = "\177";
return (1);
}
return (0);
}