File: parsekey.c

package info (click to toggle)
kinput2 3.1-10.2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,012 kB
  • ctags: 5,584
  • sloc: ansic: 49,959; makefile: 106; sh: 68
file content (156 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#ifndef lint
static char *rcsid = "$Id: parsekey.c,v 1.1 1994/05/16 05:42:30 ishisone Rel $";
#endif
/*- 
 * Copyright (c) 1991, 1994  Software Research Associates, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Software Research Associates not be
 * used in advertising or publicity pertaining to distribution of the
 * software without specific, written prior permission.  Software Research
 * Associates makes no representations about the suitability of this software
 * for any purpose.  It is provided "as is" without express or implied
 * warranty.
 *
 * Author:  Makoto Ishisone, Software Research Associates, Inc., Japan
 */

#include <X11/Xos.h>
#include <X11/IntrinsicP.h>
#include "ParseKey.h"

/*- parseModifiers: parse modifier list -*/
static void
parseModifiers(s, modp, chkmodp)
String s;
long *modp;		/* RETURN: modifiers which must be set */
long *chkmodp;		/* RETURN: modifiers to be checked */
{
    String p;
    int i;
    int c;
    static struct _moddesc_ {
	String modname;		/* modifier name */
	long modmask;		/* modifier mask */
    } mods[] = {
	{ "Shift",	ShiftMask },
	{ "Lock",	LockMask },
	{ "Ctrl",	ControlMask },
	{ "Meta",	Mod1Mask },
	{ "Alt",	Mod1Mask },
	{ "Mod1",	Mod1Mask },
	{ "Mod2",	Mod2Mask },
	{ "Mod3",	Mod3Mask },
	{ "Mod4",	Mod4Mask },
	{ "c",		ControlMask },
	{ "s",		ShiftMask },
	{ "l",		LockMask },
	{ "m",		Mod1Mask },
	{ "a",		Mod1Mask },
    };

    *modp = *chkmodp = 0L;

#define SKIPBLANK \
    while ((c = *s) == ' ' || c == '\t' || c == '\n') s++; \
    if (c == '\0') return;
#define SEARCHBLANK \
    p = s; while ((c = *p) != '\0' && c != ' ' && c != '\t' && c != '\n') p++;

    while (*s != '\0') {
	int tilde = 0;

	SKIPBLANK;
	if (c == '~') {
	    tilde = 1;
	    s++;
	    SKIPBLANK;
	}
	SEARCHBLANK;
	*p = '\0';
	for (i = 0; i < XtNumber(mods); i++) {
	    if (!strcmp(s, mods[i].modname)) {
		*chkmodp |= mods[i].modmask;
		if (!tilde) *modp |= mods[i].modmask;
		break;
	    }
	}
	if (c == '\0') break;
	s = p + 1;
    }
#undef SKIPBLANK
#undef SEARCHBLANK
}

/*- mystrstr: not-so-good implementaion of ANSI strstr() -*/
static char *
mystrstr(s1, s2)
char *s1;
char *s2;
{
    register char *p, *q;

    while (*(p = s1++) != '\0') {
	q = s2;
	do {
	    if (*q == '\0') return s1 - 1;
	} while (*p++ == *q++);
    }
    return NULL;
}


/*
 * Public function
 */

int
ParseKeyEvent(s, keysymp, modp, chkmodp)
String s;		/* IN: keyevent description */
KeySym *keysymp;	/* OUT: keysym */
long *modp;		/* OUT: mask of modifiers which must be pressed */
long *chkmodp;		/* OUT: mask of modifiers of interest */
{
    String key;
    String p;
    KeySym keysym;

    /*
     * keyevent description (stored in  argument 's') must be of the
     * following format (subset of Xt translation table syntax):
     *		modifier-list<Key>keysym
     * modifier-list is a combination of:
     *		Ctrl, Shift, Lock, Meta, Alt, Mod1, Mod2, Mod3, Mod4, Mod5
     * if '~' is prepended before a modifier, it means the modifier key should
     * not be pressed.
     */

    /* find "<Key>" */
    if ((p = mystrstr(s, "<Key>")) != NULL) {
	key = p + 5;	/* p + strlen("<Key>") */
    } else if ((p = mystrstr(s, "<KeyPress>")) != NULL) {
	key = p + 10;	/* p + strlen("<KeyPress>") */
    } else if ((p = mystrstr(s, "<KeyDown>")) != NULL) {
	key = p + 9;	/* p + strlen("<KeyDown>") */
    } else {
	return 0;
    }
    *p = '\0';
    while (*key == ' ' || *key == '\t') key++;
    p = key;
    while (*p != '\0' && *p != ' ' && *p != '\t') p++;
    *p = '\0';

    /* get modifier mask */
    parseModifiers(s, modp, chkmodp);

    /* get keycode */
    if ((keysym = XStringToKeysym(key)) == NoSymbol) return 0;

    *keysymp = keysym;

    return 1;
}