1 /*
2
3 Copyright (c) 2007-2013 uim Project https://github.com/uim/uim
4
5 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
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. Neither the name of authors nor the names of its contributors
17 may be used to endorse or promote products derived from this software
18 without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 SUCH DAMAGE.
31
32 */
33
34 /*
35 * A hack to distinguish Japanese kana_RO key from yen sign key (both
36 * keys normally generates backslash on ASCII input). See [uim-en 11]
37 * and the follow messages for the discussion.
38 *
39 * This hack assumes that the xmodmap for the Japanese kana keyboard
40 * is defined as follows:
41 *
42 * yen sign key: keycode X = backslash bar
43 * kana_RO key: keycode Y = backslash underscore
44 *
45 * And also assumes that Japanese kana_RO key is the only one key that
46 * has 'backslash underscore' modmap.
47 */
48
49 #include <config.h>
50
51 #include <X11/Xlib.h>
52 #include <X11/keysym.h>
53
54 #include "uim.h"
55 #include "uim-x-util.h"
56
57 enum KeySymIndex {
58 UNMODIFIED_KEYSYM_INDEX = 0,
59 SHIFTED_KEYSYM_INDEX = 1
60 };
61
62 static uim_bool is_japanese_keyboard;
63 static KeyCode kana_RO_keycode, yen_sign_keycode;
64
65
66 int
uim_x_kana_input_hack_translate_key(int ukey,KeyCode hardware_keycode)67 uim_x_kana_input_hack_translate_key(int ukey, KeyCode hardware_keycode)
68 {
69 if (ukey == '\\'
70 && is_japanese_keyboard
71 && hardware_keycode == yen_sign_keycode
72 && hardware_keycode != kana_RO_keycode)
73 {
74 ukey = UKey_Yen;
75 }
76
77 return ukey;
78 }
79
80 int
uim_x_kana_input_hack_filter_event(uim_context uc,XEvent * event)81 uim_x_kana_input_hack_filter_event(uim_context uc, XEvent *event)
82 {
83 unsigned int keycode;
84 int translated_key;
85 KeySym keysym;
86
87 if (event->type != KeyPress && event->type != KeyRelease)
88 return UIM_FALSE;
89
90 /* Only unmodified keys are translated. */
91 if (!event->xkey.state) {
92 keycode = event->xkey.keycode;
93 keysym = XLookupKeysym(&event->xkey, UNMODIFIED_KEYSYM_INDEX);
94 translated_key = uim_x_kana_input_hack_translate_key(keysym, keycode);
95
96 if (translated_key == UKey_Yen) {
97 int not_filtered;
98
99 if (event->type == KeyPress)
100 not_filtered = uim_press_key(uc, translated_key, 0);
101 else
102 not_filtered = uim_release_key(uc, translated_key, 0);
103
104 if (!not_filtered)
105 return UIM_TRUE;
106 }
107 }
108 return UIM_FALSE;
109 }
110
111 void
uim_x_kana_input_hack_init(Display * display)112 uim_x_kana_input_hack_init(Display *display)
113 {
114 int min_keycode, max_keycode, keysyms_per_keycode, keycode_count, i;
115 KeySym *map, *syms, unmodified, shifted;
116
117 /* To allow refreshing keyboard encoding configuration by call this
118 * function again, the global variables are explicitly initialized
119 * with zero here. */
120 is_japanese_keyboard = UIM_FALSE;
121 kana_RO_keycode = 0;
122
123 XDisplayKeycodes(display, &min_keycode, &max_keycode);
124 keycode_count = max_keycode - min_keycode + 1;
125 map = XGetKeyboardMapping(display,
126 min_keycode, keycode_count, &keysyms_per_keycode);
127
128 if (keysyms_per_keycode >= SHIFTED_KEYSYM_INDEX + 1) {
129 for (i = 0, syms = map;
130 i < keycode_count;
131 i++, syms += keysyms_per_keycode)
132 {
133 unmodified = syms[UNMODIFIED_KEYSYM_INDEX];
134 shifted = syms[SHIFTED_KEYSYM_INDEX];
135
136 /* Assumes that Japanese kana_RO key is the only one key that
137 * has 'backslash underscore' modmap. */
138 if (unmodified == XK_backslash) {
139 if (shifted == XK_underscore) {
140 is_japanese_keyboard = UIM_TRUE;
141 kana_RO_keycode = min_keycode + i;
142 } else if (shifted == XK_bar) {
143 yen_sign_keycode = min_keycode + i;
144 }
145 }
146 }
147 }
148
149 XFree(map);
150 }
151