Home | History | Annotate | Line # | Download | only in wscons
      1 /* $NetBSD: wsemul_vt100_keys.c,v 1.11 2010/02/10 19:39:39 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.11 2010/02/10 19:39:39 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 	struct vt100base_data *vd = &edp->bd;
     89 	static char c;
     90 
     91 	if (KS_GROUP(in) == KS_GROUP_Plain) {
     92 		/* catch ISO-1 */
     93 		c = KS_VALUE(in);
     94 		*out = &c;
     95 		return (1);
     96 	}
     97 	if (in >= KS_f1 && in <= KS_f20) {
     98 		*out = vt100_fkeys[in - KS_f1];
     99 		return (5);
    100 	}
    101 	if (in >= KS_F1 && in <= KS_F20) {
    102 		*out = vt100_fkeys[in - KS_F1];
    103 		return (5);
    104 	}
    105 	if (in >= KS_KP_F1 && in <= KS_KP_F4) {
    106 		*out = vt100_pfkeys[in - KS_KP_F1];
    107 		return (3);
    108 	}
    109 	if (vd->flags & VTFL_APPLKEYPAD) {
    110 		if (in >= KS_KP_0 && in <= KS_KP_9) {
    111 			*out = vt100_numpad[in - KS_KP_0];
    112 			return (3);
    113 		}
    114 		switch (in) {
    115 		    case KS_KP_Tab:
    116 			*out = "\033OI";
    117 			return (3);
    118 		    case KS_KP_Enter:
    119 			*out = "\033OM";
    120 			return (3);
    121 		    case KS_KP_Multiply:
    122 			*out = "\033Oj";
    123 			return (3);
    124 		    case KS_KP_Add:
    125 			*out = "\033Ok";
    126 			return (3);
    127 		    case KS_KP_Separator:
    128 			*out = "\033Ol";
    129 			return (3);
    130 		    case KS_KP_Subtract:
    131 			*out = "\033Om";
    132 			return (3);
    133 		    case KS_KP_Decimal:
    134 			*out = "\033On";
    135 			return (3);
    136 		    case KS_KP_Divide:
    137 			*out = "\033Oo";
    138 			return (3);
    139 		}
    140 	} else {
    141 		if (!(in & 0x80)) {
    142 			c = in & 0xff; /* turn into ASCII */
    143 			*out = &c;
    144 			return (1);
    145 		}
    146 	}
    147 	switch (in) {
    148 	    case KS_Help:
    149 		*out = vt100_fkeys[15 - 1];
    150 		return (5);
    151 	    case KS_Execute: /* "Do" */
    152 		*out = vt100_fkeys[16 - 1];
    153 		return (5);
    154 	    case KS_Find:
    155 		*out = "\033[1~";
    156 		return (4);
    157 	    case KS_Insert:
    158 	    case KS_KP_Insert:
    159 		*out = "\033[2~";
    160 		return (4);
    161 	    case KS_KP_Delete:
    162 		*out = "\033[3~";
    163 		return (4);
    164 	    case KS_Select:
    165 		*out = "\033[4~";
    166 		return (4);
    167 	    case KS_Prior:
    168 	    case KS_KP_Prior:
    169 		*out = "\033[5~";
    170 		return (4);
    171 	    case KS_Next:
    172 	    case KS_KP_Next:
    173 		*out = "\033[6~";
    174 		return (4);
    175 	    case KS_Home:
    176 	    case KS_KP_Home:
    177 		*out = "\033[7~";
    178 		return (4);
    179 	    case KS_End:
    180 	    case KS_KP_End:
    181 		*out = "\033[8~";
    182 		return (4);
    183 	    case KS_Up:
    184 	    case KS_KP_Up:
    185 		if (vd->flags & VTFL_APPLCURSOR)
    186 			*out = "\033OA";
    187 		else
    188 			*out = "\033[A";
    189 		return (3);
    190 	    case KS_Down:
    191 	    case KS_KP_Down:
    192 		if (vd->flags & VTFL_APPLCURSOR)
    193 			*out = "\033OB";
    194 		else
    195 			*out = "\033[B";
    196 		return (3);
    197 	    case KS_Left:
    198 	    case KS_KP_Left:
    199 		if (vd->flags & VTFL_APPLCURSOR)
    200 			*out = "\033OD";
    201 		else
    202 			*out = "\033[D";
    203 		return (3);
    204 	    case KS_Right:
    205 	    case KS_KP_Right:
    206 		if (vd->flags & VTFL_APPLCURSOR)
    207 			*out = "\033OC";
    208 		else
    209 			*out = "\033[C";
    210 		return (3);
    211 	}
    212 	return (0);
    213 }
    214