Home | History | Annotate | Line # | Download | only in wscons
wsemul_vt100_keys.c revision 1.10
      1 /* $NetBSD: wsemul_vt100_keys.c,v 1.10 2010/01/28 22:36:19 drochner Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998
      5  *	Matthias Drochner.  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  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: wsemul_vt100_keys.c,v 1.10 2010/01/28 22:36:19 drochner Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <dev/wscons/wsconsio.h>
     36 #include <dev/wscons/wsdisplayvar.h>
     37 #include <dev/wscons/wsksymvar.h>
     38 #include <dev/wscons/wsksymdef.h>
     39 #include <dev/wscons/wsemul_vt100var.h>
     40 
     41 static const char *vt100_fkeys[] = {
     42 	"\033[11~",	/* F1 */
     43 	"\033[12~",
     44 	"\033[13~",		/* F1-F5 normally don't send codes */
     45 	"\033[14~",
     46 	"\033[15~",	/* F5 */
     47 	"\033[17~",	/* F6 */
     48 	"\033[18~",
     49 	"\033[19~",
     50 	"\033[20~",
     51 	"\033[21~",
     52 	"\033[23~",	/* VT100: ESC */
     53 	"\033[24~",	/* VT100: BS */
     54 	"\033[25~",	/* VT100: LF */
     55 	"\033[26~",
     56 	"\033[28~",	/* help */
     57 	"\033[29~",	/* do */
     58 	"\033[31~",
     59 	"\033[32~",
     60 	"\033[33~",
     61 	"\033[34~",	/* F20 */
     62 };
     63 
     64 static const char *vt100_pfkeys[] = {
     65 	"\033OP",	/* PF1 */
     66 	"\033OQ",
     67 	"\033OR",
     68 	"\033OS",	/* PF4 */
     69 };
     70 
     71 static const char *vt100_numpad[] = {
     72 	"\033Op",	/* KP 0 */
     73 	"\033Oq",	/* KP 1 */
     74 	"\033Or",	/* KP 2 */
     75 	"\033Os",	/* KP 3 */
     76 	"\033Ot",	/* KP 4 */
     77 	"\033Ou",	/* KP 5 */
     78 	"\033Ov",	/* KP 6 */
     79 	"\033Ow",	/* KP 7 */
     80 	"\033Ox",	/* KP 8 */
     81 	"\033Oy",	/* KP 9 */
     82 };
     83 
     84 int
     85 wsemul_vt100_translate(void *cookie, keysym_t in, const char **out)
     86 {
     87 	struct wsemul_vt100_emuldata *edp = cookie;
     88 	static char c;
     89 
     90 	if (KS_GROUP(in) == KS_GROUP_Plain) {
     91 		/* catch ISO-1 */
     92 		c = KS_VALUE(in);
     93 		*out = &c;
     94 		return (1);
     95 	}
     96 	if (in >= KS_f1 && in <= KS_f20) {
     97 		*out = vt100_fkeys[in - KS_f1];
     98 		return (5);
     99 	}
    100 	if (in >= KS_F1 && in <= KS_F20) {
    101 		*out = vt100_fkeys[in - KS_F1];
    102 		return (5);
    103 	}
    104 	if (in >= KS_KP_F1 && in <= KS_KP_F4) {
    105 		*out = vt100_pfkeys[in - KS_KP_F1];
    106 		return (3);
    107 	}
    108 	if (edp->flags & VTFL_APPLKEYPAD) {
    109 		if (in >= KS_KP_0 && in <= KS_KP_9) {
    110 			*out = vt100_numpad[in - KS_KP_0];
    111 			return (3);
    112 		}
    113 		switch (in) {
    114 		    case KS_KP_Tab:
    115 			*out = "\033OI";
    116 			return (3);
    117 		    case KS_KP_Enter:
    118 			*out = "\033OM";
    119 			return (3);
    120 		    case KS_KP_Multiply:
    121 			*out = "\033Oj";
    122 			return (3);
    123 		    case KS_KP_Add:
    124 			*out = "\033Ok";
    125 			return (3);
    126 		    case KS_KP_Separator:
    127 			*out = "\033Ol";
    128 			return (3);
    129 		    case KS_KP_Subtract:
    130 			*out = "\033Om";
    131 			return (3);
    132 		    case KS_KP_Decimal:
    133 			*out = "\033On";
    134 			return (3);
    135 		    case KS_KP_Divide:
    136 			*out = "\033Oo";
    137 			return (3);
    138 		}
    139 	} else {
    140 		if (!(in & 0x80)) {
    141 			c = in & 0xff; /* turn into ASCII */
    142 			*out = &c;
    143 			return (1);
    144 		}
    145 	}
    146 	switch (in) {
    147 	    case KS_Help:
    148 		*out = vt100_fkeys[15 - 1];
    149 		return (5);
    150 	    case KS_Execute: /* "Do" */
    151 		*out = vt100_fkeys[16 - 1];
    152 		return (5);
    153 	    case KS_Find:
    154 		*out = "\033[1~";
    155 		return (4);
    156 	    case KS_Insert:
    157 	    case KS_KP_Insert:
    158 		*out = "\033[2~";
    159 		return (4);
    160 	    case KS_KP_Delete:
    161 		*out = "\033[3~";
    162 		return (4);
    163 	    case KS_Select:
    164 		*out = "\033[4~";
    165 		return (4);
    166 	    case KS_Prior:
    167 	    case KS_KP_Prior:
    168 		*out = "\033[5~";
    169 		return (4);
    170 	    case KS_Next:
    171 	    case KS_KP_Next:
    172 		*out = "\033[6~";
    173 		return (4);
    174 	    case KS_Home:
    175 	    case KS_KP_Home:
    176 		*out = "\033[7~";
    177 		return (4);
    178 	    case KS_End:
    179 	    case KS_KP_End:
    180 		*out = "\033[8~";
    181 		return (4);
    182 	    case KS_Up:
    183 	    case KS_KP_Up:
    184 		if (edp->flags & VTFL_APPLCURSOR)
    185 			*out = "\033OA";
    186 		else
    187 			*out = "\033[A";
    188 		return (3);
    189 	    case KS_Down:
    190 	    case KS_KP_Down:
    191 		if (edp->flags & VTFL_APPLCURSOR)
    192 			*out = "\033OB";
    193 		else
    194 			*out = "\033[B";
    195 		return (3);
    196 	    case KS_Left:
    197 	    case KS_KP_Left:
    198 		if (edp->flags & VTFL_APPLCURSOR)
    199 			*out = "\033OD";
    200 		else
    201 			*out = "\033[D";
    202 		return (3);
    203 	    case KS_Right:
    204 	    case KS_KP_Right:
    205 		if (edp->flags & VTFL_APPLCURSOR)
    206 			*out = "\033OC";
    207 		else
    208 			*out = "\033[C";
    209 		return (3);
    210 	}
    211 	return (0);
    212 }
    213