Home | History | Annotate | Line # | Download | only in wsconsctl
keysym.c revision 1.9
      1  1.9     lukem /*	$NetBSD: keysym.c,v 1.9 2009/04/06 12:35:20 lukem Exp $ */
      2  1.1   hannken 
      3  1.1   hannken /*-
      4  1.1   hannken  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1   hannken  * All rights reserved.
      6  1.1   hannken  *
      7  1.1   hannken  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   hannken  * by Juergen Hannken-Illjes.
      9  1.1   hannken  *
     10  1.1   hannken  * Redistribution and use in source and binary forms, with or without
     11  1.1   hannken  * modification, are permitted provided that the following conditions
     12  1.1   hannken  * are met:
     13  1.1   hannken  * 1. Redistributions of source code must retain the above copyright
     14  1.1   hannken  *    notice, this list of conditions and the following disclaimer.
     15  1.1   hannken  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   hannken  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   hannken  *    documentation and/or other materials provided with the distribution.
     18  1.1   hannken  *
     19  1.1   hannken  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   hannken  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   hannken  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   hannken  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   hannken  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   hannken  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   hannken  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   hannken  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   hannken  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   hannken  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   hannken  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   hannken  */
     31  1.1   hannken 
     32  1.1   hannken #include <dev/wscons/wsksymdef.h>
     33  1.7      jmmv 
     34  1.1   hannken #include <stdio.h>
     35  1.1   hannken #include <stdlib.h>
     36  1.4      matt #include <string.h>
     37  1.1   hannken #include <unistd.h>
     38  1.7      jmmv 
     39  1.1   hannken #include "keysym.h"
     40  1.1   hannken #include "wsconsctl.h"
     41  1.1   hannken 
     42  1.1   hannken #define NUMKSYMS	(sizeof(ksym_tab_by_name)/sizeof(ksym_tab_by_name[0]))
     43  1.1   hannken 
     44  1.1   hannken static int first_time = 1;
     45  1.1   hannken static struct ksym ksym_tab_by_ksym[NUMKSYMS];
     46  1.1   hannken 
     47  1.1   hannken /* copied from dev/wscons/wskbdutil.c ... */
     48  1.1   hannken 
     49  1.1   hannken static const u_char latin1_to_upper[256] = {
     50  1.7      jmmv /*	0  8  1  9  2  a  3  b  4  c  5  d  6  e  7  f		     */
     51  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
     52  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 0 */
     53  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
     54  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 1 */
     55  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
     56  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 2 */
     57  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
     58  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 3 */
     59  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
     60  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 4 */
     61  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
     62  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 5 */
     63  1.1   hannken 	0x00,  'A',  'B',  'C',  'D',  'E',  'F',  'G',		/* 6 */
     64  1.1   hannken 	 'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',		/* 6 */
     65  1.1   hannken 	 'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',		/* 7 */
     66  1.1   hannken 	 'X',  'Y',  'Z', 0x00, 0x00, 0x00, 0x00, 0x00,		/* 7 */
     67  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
     68  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 8 */
     69  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
     70  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* 9 */
     71  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
     72  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* a */
     73  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
     74  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* b */
     75  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
     76  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* c */
     77  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
     78  1.1   hannken 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,		/* d */
     79  1.1   hannken 	0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,		/* e */
     80  1.1   hannken 	0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,		/* e */
     81  1.1   hannken 	0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0x00,		/* f */
     82  1.1   hannken 	0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0x00,		/* f */
     83  1.1   hannken };
     84  1.1   hannken 
     85  1.5   xtraeme static int qcmp_name(const void *, const void *);
     86  1.5   xtraeme static int qcmp_ksym(const void *, const void *);
     87  1.5   xtraeme static int bcmp_name(const void *, const void *);
     88  1.5   xtraeme static int bcmp_ksym(const void *, const void *);
     89  1.1   hannken 
     90  1.5   xtraeme static void sort_ksym_tab(void);
     91  1.1   hannken 
     92  1.1   hannken static int
     93  1.5   xtraeme qcmp_name(const void *a, const void *b)
     94  1.1   hannken {
     95  1.7      jmmv 
     96  1.7      jmmv 	return strcmp(((const struct ksym *) a)->name,
     97  1.7      jmmv 	    ((const struct ksym *) b)->name);
     98  1.1   hannken }
     99  1.1   hannken 
    100  1.1   hannken static int
    101  1.5   xtraeme qcmp_ksym(const void *a, const void *b)
    102  1.1   hannken {
    103  1.7      jmmv 
    104  1.7      jmmv 	return ((const struct ksym *) b)->value -
    105  1.7      jmmv 	    ((const struct ksym *) a)->value;
    106  1.1   hannken }
    107  1.1   hannken 
    108  1.1   hannken static int
    109  1.5   xtraeme bcmp_name(const void *a, const void *b)
    110  1.1   hannken {
    111  1.7      jmmv 
    112  1.7      jmmv 	return strcmp((const char *) a, ((const struct ksym *) b)->name);
    113  1.1   hannken }
    114  1.1   hannken 
    115  1.1   hannken static int
    116  1.5   xtraeme bcmp_ksym(const void *a, const void *b)
    117  1.1   hannken {
    118  1.7      jmmv 
    119  1.7      jmmv 	return ((const struct ksym *) b)->value - *((const int *) a);
    120  1.1   hannken }
    121  1.1   hannken 
    122  1.1   hannken static void
    123  1.5   xtraeme sort_ksym_tab(void)
    124  1.1   hannken {
    125  1.9     lukem 	size_t i;
    126  1.1   hannken 
    127  1.1   hannken 	for (i = 0; i < NUMKSYMS; i++)
    128  1.1   hannken 		ksym_tab_by_ksym[i] = ksym_tab_by_name[i];
    129  1.1   hannken 
    130  1.1   hannken 	qsort(ksym_tab_by_name, NUMKSYMS, sizeof(struct ksym), qcmp_name);
    131  1.1   hannken 	qsort(ksym_tab_by_ksym, NUMKSYMS, sizeof(struct ksym), qcmp_ksym);
    132  1.1   hannken 
    133  1.1   hannken 	first_time = 0;
    134  1.1   hannken }
    135  1.1   hannken 
    136  1.6  christos const char *
    137  1.5   xtraeme ksym2name(int k)
    138  1.1   hannken {
    139  1.1   hannken 	static char tmp[20];
    140  1.1   hannken 	struct ksym *r;
    141  1.1   hannken 
    142  1.1   hannken 	if (first_time)
    143  1.1   hannken 		sort_ksym_tab();
    144  1.1   hannken 
    145  1.2   nathanw 	r = bsearch(&k, ksym_tab_by_ksym,
    146  1.1   hannken 		    NUMKSYMS, sizeof(struct ksym), bcmp_ksym);
    147  1.1   hannken 
    148  1.1   hannken 	if (r != NULL)
    149  1.7      jmmv 		return r->name;
    150  1.1   hannken 	else {
    151  1.7      jmmv 		(void)snprintf(tmp, sizeof(tmp), "unknown_%d", k);
    152  1.7      jmmv 		return tmp;
    153  1.1   hannken 	}
    154  1.1   hannken }
    155  1.1   hannken 
    156  1.1   hannken int
    157  1.5   xtraeme name2ksym(char *n)
    158  1.1   hannken {
    159  1.3   hannken 	int res;
    160  1.1   hannken 	struct ksym *r;
    161  1.1   hannken 
    162  1.1   hannken 	if (first_time)
    163  1.1   hannken 		sort_ksym_tab();
    164  1.1   hannken 
    165  1.1   hannken 	r = bsearch(n, ksym_tab_by_name,
    166  1.1   hannken 		    NUMKSYMS, sizeof(struct ksym), bcmp_name);
    167  1.1   hannken 
    168  1.1   hannken 	if (r != NULL)
    169  1.7      jmmv 		return r->value;
    170  1.3   hannken 	else if (sscanf(n, "unknown_%d", &res) == 1)
    171  1.7      jmmv 		return res;
    172  1.1   hannken 	else
    173  1.7      jmmv 		return -1;
    174  1.1   hannken }
    175  1.1   hannken 
    176  1.1   hannken keysym_t
    177  1.5   xtraeme ksym_upcase(keysym_t ksym)
    178  1.1   hannken {
    179  1.7      jmmv 
    180  1.1   hannken 	if (ksym >= KS_f1 && ksym <= KS_f20)
    181  1.7      jmmv 		return KS_F1 - KS_f1 + ksym;
    182  1.1   hannken 
    183  1.1   hannken 	if (KS_GROUP(ksym) == KS_GROUP_Ascii && ksym <= 0xff &&
    184  1.1   hannken 	    latin1_to_upper[ksym] != 0x00)
    185  1.7      jmmv 		return latin1_to_upper[ksym];
    186  1.1   hannken 
    187  1.7      jmmv 	return ksym;
    188  1.1   hannken }
    189