2 * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
4 * The contents of this file are subject to the terms of either the GNU Lesser
5 * General Public License Version 2.1 only ("LGPL") or the Common Development and
6 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
7 * file except in compliance with the License. You can obtain a copy of the CDDL at
8 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
9 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
10 * specific language governing permissions and limitations under the License. When
11 * distributing the software, include this License Header Notice in each file and
12 * include the full text of the License in the License file as well as the
15 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
17 * For Covered Software in this distribution, this License shall be governed by the
18 * laws of the State of California (excluding conflict-of-law provisions).
19 * Any litigation relating to this License shall be subject to the jurisdiction of
20 * the Federal Courts of the Northern District of California and the state courts
21 * of the State of California, with venue lying in Santa Clara County, California.
25 * If you wish your version of this file to be governed by only the CDDL or only
26 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
27 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
28 * license." If you don't indicate a single choice of license, a recipient has the
29 * option to distribute your version of this file under either the CDDL or the LGPL
30 * Version 2.1, or to extend the choice of license to its licensees as provided
31 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
32 * Version 2 license, then the option applies only if the new code is made subject
33 * to such option by the copyright holder.
44 __toggle_preedit_status(XIMHandle* handle, bool flag)
47 xim_start_preedit(handle);
49 xim_cancel_preedit(handle);
52 static bool last_shift_available = false;
55 __filter_forward_triger_key(XIMHandle* handle, IC* ic, XKeyEvent* evt)
60 settings_get(TRIGGER_KEY, &hk);
61 XLookupString(evt, NULL, 0, &sym, NULL);
63 unsigned int masked_state = (evt->state & STATE_MASK);
65 if (masked_state == hk.modifiers && sym == hk.keysym) {
66 if (evt->type == KeyRelease) return true;
67 ic->is_enabled = !ic->is_enabled;
68 __toggle_preedit_status(handle, ic->is_enabled);
73 settings_get(ENG_KEY, &hk);
74 if ((masked_state & hk.modifiers) == hk.modifiers && sym == hk.keysym) {
75 if (evt->type == KeyPress) {
76 last_shift_available = true;
78 } else if (evt->type == KeyRelease && last_shift_available) {
79 last_shift_available = false;
81 ic->is_english = !ic->is_english;
87 last_shift_available = false;
94 __do_forward_event(XIMHandle* handle, IMForwardEventStruct* proto)
96 /* by pass the event */
97 IMForwardEventStruct fe;
98 memset(&fe, 0, sizeof (fe));
100 fe.major_code = XIM_FORWARD_EVENT;
101 fe.icid = proto->icid;
102 fe.connect_id = proto->connect_id;
104 fe.serial_number = 0L;
105 fe.event = proto->event;
107 IMForwardEvent(handle, (XPointer) &fe);
110 extern void __move_preedit(IC* ic);
113 _xim_forward_event(XIMHandle* handle, IMForwardEventStruct* proto)
115 LOG("%d", proto->icid);
116 XKeyEvent* evt = (XKeyEvent*) &(proto->event);
117 IC* ic = icmgr_get_current();
119 icmgr_set_current(proto->icid);
120 ic = icmgr_get_current();
125 /* some of swing applications like netbeans won't set focus,
126 * we have to set focus ourself
128 if (ic->icid != proto->icid || preedit_status() == false) {
129 icmgr_set_current(proto->icid);
130 ic = icmgr_get_current();
134 if (!__filter_forward_triger_key(handle, ic, evt)) {
135 int masked_state = evt->state & STATE_MASK;
136 if (masked_state != ShiftMask && masked_state != 0) {
137 __do_forward_event(handle, proto);
138 } else if (!ic->is_enabled || ic->is_english) {
139 __do_forward_event(handle, proto);
142 XLookupString(evt, NULL, 0, &sym, NULL);
143 if ((sym <= 0x20 || sym > 0x7E) && preedit_status() == false) {
144 __do_forward_event(handle, proto);
145 } else if (sym >= 0x30 && sym <= 0x39
146 && preedit_status() == false) {
148 if (settings_get_int(SMART_PUNCT) && evt->type == KeyPress) {
149 preedit_omit_next_punct();
152 __do_forward_event(handle, proto);
154 if (evt->type == KeyPress) {
156 preedit_on_key(handle, sym, evt->state);