Tizen 2.1 base
[platform/core/uifw/ise-engine-sunpinyin.git] / wrapper / xim / xim_trigger.c
1 /*
2  * Copyright (c) 2010 Mike Qin <mikeandmore@gmail.com>
3  *
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
13  * following notice:
14  *
15  * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
16  * (CDDL)
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.
22  *
23  * Contributor(s):
24  *
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.
34  */
35
36 #include "xim.h"
37 #include "ic.h"
38 #include "common.h"
39 #include "settings.h"
40
41 #include <stdio.h>
42
43 static void
44 __toggle_preedit_status(XIMHandle* handle, bool flag)
45 {
46     if (flag)
47         xim_start_preedit(handle);
48     else
49         xim_cancel_preedit(handle);
50 }
51
52 static bool last_shift_available = false;
53
54 static bool
55 __filter_forward_triger_key(XIMHandle* handle, IC* ic, XKeyEvent* evt)
56 {
57     KeySym sym = 0;
58     hotkey_t hk;
59
60     settings_get(TRIGGER_KEY, &hk);
61     XLookupString(evt, NULL, 0, &sym, NULL);
62
63     unsigned int masked_state = (evt->state & STATE_MASK);
64
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);
69         icmgr_refresh();
70         return true;
71     }
72
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;
77             return true;
78         } else if (evt->type == KeyRelease && last_shift_available) {
79             last_shift_available = false;
80             if (ic->is_enabled) {
81                 ic->is_english = !ic->is_english;
82                 icmgr_refresh();
83             }
84         }
85         return true;
86     } else {
87         last_shift_available = false;
88     }
89
90     return false;
91 }
92
93 static void
94 __do_forward_event(XIMHandle* handle, IMForwardEventStruct* proto)
95 {
96     /* by pass the event */
97     IMForwardEventStruct fe;
98     memset(&fe, 0, sizeof (fe));
99
100     fe.major_code = XIM_FORWARD_EVENT;
101     fe.icid = proto->icid;
102     fe.connect_id = proto->connect_id;
103     fe.sync_bit = 0;
104     fe.serial_number = 0L;
105     fe.event = proto->event;
106
107     IMForwardEvent(handle, (XPointer) &fe);
108 }
109
110 extern void __move_preedit(IC* ic);
111
112 int
113 _xim_forward_event(XIMHandle* handle, IMForwardEventStruct* proto)
114 {
115     LOG("%d", proto->icid);
116     XKeyEvent* evt = (XKeyEvent*) &(proto->event);
117     IC* ic = icmgr_get_current();
118     if (ic == NULL) {
119         icmgr_set_current(proto->icid);
120         ic = icmgr_get_current();
121         if (ic == NULL)
122             return 1;
123     }
124
125     /* some of swing applications like netbeans won't set focus,
126      * we have to set focus ourself
127      */
128     if (ic->icid != proto->icid || preedit_status() == false) {
129         icmgr_set_current(proto->icid);
130         ic = icmgr_get_current();
131         icmgr_refresh();
132     }
133
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);
140         } else {
141             KeySym sym;
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) {
147                 // digit key pressed
148                 if (settings_get_int(SMART_PUNCT) && evt->type == KeyPress) {
149                     preedit_omit_next_punct();
150                 }
151
152                 __do_forward_event(handle, proto);
153             } else {
154                 if (evt->type == KeyPress) {
155                     __move_preedit(ic);
156                     preedit_on_key(handle, sym, evt->state);
157                 } else {
158                     LOG("ignore");
159                 }
160                 return 0;
161             }
162         }
163     }
164     return 1;
165 }