comparison lib/parsekey.c @ 0:92745d501b9a

initial import from kinput2-v3.1
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Mon, 08 Mar 2010 04:44:30 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:92745d501b9a
1 #ifndef lint
2 static char *rcsid = "$Id: parsekey.c,v 1.1 1994/05/16 05:42:30 ishisone Rel $";
3 #endif
4 /*-
5 * Copyright (c) 1991, 1994 Software Research Associates, Inc.
6 *
7 * Permission to use, copy, modify, and distribute this software and its
8 * documentation for any purpose and without fee is hereby granted, provided
9 * that the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Software Research Associates not be
12 * used in advertising or publicity pertaining to distribution of the
13 * software without specific, written prior permission. Software Research
14 * Associates makes no representations about the suitability of this software
15 * for any purpose. It is provided "as is" without express or implied
16 * warranty.
17 *
18 * Author: Makoto Ishisone, Software Research Associates, Inc., Japan
19 */
20
21 #include <X11/Xos.h>
22 #include <X11/IntrinsicP.h>
23 #include "ParseKey.h"
24
25 /*- parseModifiers: parse modifier list -*/
26 static void
27 parseModifiers(s, modp, chkmodp)
28 String s;
29 long *modp; /* RETURN: modifiers which must be set */
30 long *chkmodp; /* RETURN: modifiers to be checked */
31 {
32 String p;
33 int i;
34 int c;
35 static struct _moddesc_ {
36 String modname; /* modifier name */
37 long modmask; /* modifier mask */
38 } mods[] = {
39 { "Shift", ShiftMask },
40 { "Lock", LockMask },
41 { "Ctrl", ControlMask },
42 { "Meta", Mod1Mask },
43 { "Alt", Mod1Mask },
44 { "Mod1", Mod1Mask },
45 { "Mod2", Mod2Mask },
46 { "Mod3", Mod3Mask },
47 { "Mod4", Mod4Mask },
48 { "c", ControlMask },
49 { "s", ShiftMask },
50 { "l", LockMask },
51 { "m", Mod1Mask },
52 { "a", Mod1Mask },
53 };
54
55 *modp = *chkmodp = 0L;
56
57 #define SKIPBLANK \
58 while ((c = *s) == ' ' || c == '\t' || c == '\n') s++; \
59 if (c == '\0') return;
60 #define SEARCHBLANK \
61 p = s; while ((c = *p) != '\0' && c != ' ' && c != '\t' && c != '\n') p++;
62
63 while (*s != '\0') {
64 int tilde = 0;
65
66 SKIPBLANK;
67 if (c == '~') {
68 tilde = 1;
69 s++;
70 SKIPBLANK;
71 }
72 SEARCHBLANK;
73 *p = '\0';
74 for (i = 0; i < XtNumber(mods); i++) {
75 if (!strcmp(s, mods[i].modname)) {
76 *chkmodp |= mods[i].modmask;
77 if (!tilde) *modp |= mods[i].modmask;
78 break;
79 }
80 }
81 if (c == '\0') break;
82 s = p + 1;
83 }
84 #undef SKIPBLANK
85 #undef SEARCHBLANK
86 }
87
88 /*- mystrstr: not-so-good implementaion of ANSI strstr() -*/
89 static char *
90 mystrstr(s1, s2)
91 char *s1;
92 char *s2;
93 {
94 register char *p, *q;
95
96 while (*(p = s1++) != '\0') {
97 q = s2;
98 do {
99 if (*q == '\0') return s1 - 1;
100 } while (*p++ == *q++);
101 }
102 return NULL;
103 }
104
105
106 /*
107 * Public function
108 */
109
110 int
111 ParseKeyEvent(s, keysymp, modp, chkmodp)
112 String s; /* IN: keyevent description */
113 KeySym *keysymp; /* OUT: keysym */
114 long *modp; /* OUT: mask of modifiers which must be pressed */
115 long *chkmodp; /* OUT: mask of modifiers of interest */
116 {
117 String key;
118 String p;
119 KeySym keysym;
120
121 /*
122 * keyevent description (stored in argument 's') must be of the
123 * following format (subset of Xt translation table syntax):
124 * modifier-list<Key>keysym
125 * modifier-list is a combination of:
126 * Ctrl, Shift, Lock, Meta, Alt, Mod1, Mod2, Mod3, Mod4, Mod5
127 * if '~' is prepended before a modifier, it means the modifier key should
128 * not be pressed.
129 */
130
131 /* find "<Key>" */
132 if ((p = mystrstr(s, "<Key>")) != NULL) {
133 key = p + 5; /* p + strlen("<Key>") */
134 } else if ((p = mystrstr(s, "<KeyPress>")) != NULL) {
135 key = p + 10; /* p + strlen("<KeyPress>") */
136 } else if ((p = mystrstr(s, "<KeyDown>")) != NULL) {
137 key = p + 9; /* p + strlen("<KeyDown>") */
138 } else {
139 return 0;
140 }
141 *p = '\0';
142 while (*key == ' ' || *key == '\t') key++;
143 p = key;
144 while (*p != '\0' && *p != ' ' && *p != '\t') p++;
145 *p = '\0';
146
147 /* get modifier mask */
148 parseModifiers(s, modp, chkmodp);
149
150 /* get keycode */
151 if ((keysym = XStringToKeysym(key)) == NoSymbol) return 0;
152
153 *keysymp = keysym;
154
155 return 1;
156 }