2 * Copyright (c) 2007 Kov Chai <tchaikov@gmail.com>
3 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 * The contents of this file are subject to the terms of either the GNU Lesser
6 * General Public License Version 2.1 only ("LGPL") or the Common Development and
7 * Distribution License ("CDDL")(collectively, the "License"). You may not use this
8 * file except in compliance with the License. You can obtain a copy of the CDDL at
9 * http://www.opensource.org/licenses/cddl1.php and a copy of the LGPLv2.1 at
10 * http://www.opensource.org/licenses/lgpl-license.php. See the License for the
11 * specific language governing permissions and limitations under the License. When
12 * distributing the software, include this License Header Notice in each file and
13 * include the full text of the License in the License file as well as the
16 * NOTICE PURSUANT TO SECTION 9 OF THE COMMON DEVELOPMENT AND DISTRIBUTION LICENSE
18 * For Covered Software in this distribution, this License shall be governed by the
19 * laws of the State of California (excluding conflict-of-law provisions).
20 * Any litigation relating to this License shall be subject to the jurisdiction of
21 * the Federal Courts of the Northern District of California and the state courts
22 * of the State of California, with venue lying in Santa Clara County, California.
26 * If you wish your version of this file to be governed by only the CDDL or only
27 * the LGPL Version 2.1, indicate your decision by adding "[Contributor]" elects to
28 * include this software in this distribution under the [CDDL or LGPL Version 2.1]
29 * license." If you don't indicate a single choice of license, a recipient has the
30 * option to distribute your version of this file under either the CDDL or the LGPL
31 * Version 2.1, or to extend the choice of license to its licensees as provided
32 * above. However, if you add LGPL Version 2.1 code and therefore, elected the LGPL
33 * Version 2 license, then the option applies only if the new code is made subject
34 * to such option by the copyright holder.
36 * Modifications by Samsung Electronics Co., Ltd.
38 * 1.Added always candidate show feature
39 * 2.Added auto commit feature for mobile user
42 #define Uses_STL_AUTOPTR
43 #define Uses_STL_FUNCTIONAL
44 #define Uses_STL_VECTOR
45 #define Uses_STL_IOSTREAM
46 #define Uses_STL_FSTREAM
47 #define Uses_STL_ALGORITHM
49 #define Uses_STL_UTILITY
50 #define Uses_STL_IOMANIP
52 #define Uses_SCIM_UTILITY
53 #define Uses_SCIM_IMENGINE
54 #define Uses_SCIM_ICONV
55 #define Uses_SCIM_CONFIG_BASE
56 #define Uses_SCIM_CONFIG_PATH
57 #define Uses_SCIM_LOOKUP_TABLE
58 #define Uses_SCIM_DEBUG
60 #include <sys/types.h>
64 #include <imi_options.h>
66 #include <ic_history.h>
70 #include "imi_scimwin.h"
71 #include "sunpinyin_utils.h"
72 #include "sunpinyin_keycode.h"
73 #include "sunpinyin_lookup_table.h"
74 #include "sunpinyin_imengine.h"
75 #include "sunpinyin_imengine_config_keys.h"
76 #include "sunpinyin_private.h"
78 #define SCIM_PROP_STATUS "/IMEngine/SunPinyin/Status"
79 #define SCIM_PROP_LETTER "/IMEngine/SunPinyin/Letter"
80 #define SCIM_PROP_PUNCT "/IMEngine/SunPinyin/Punct"
82 #ifndef SCIM_SUNPINYIN_DATADIR
83 #define SCIM_SUNPINYIN_DATADIR "/usr/share/scim/sunpinyin"
87 #define SCIM_ICONDIR "/usr/share/scim/icons"
90 #ifndef SCIM_SUNPINYIN_ICON_FILE
91 #define SCIM_SUNPINYIN_ICON_FILE (SCIM_ICONDIR "/sunpinyin_logo.png")
94 #define SCIM_FULL_LETTER_ICON (SCIM_ICONDIR "/full-letter.png")
95 #define SCIM_HALF_LETTER_ICON (SCIM_ICONDIR "/half-letter.png")
96 #define SCIM_FULL_PUNCT_ICON (SCIM_ICONDIR "/full-punct.png")
97 #define SCIM_HALF_PUNCT_ICON (SCIM_ICONDIR "/half-punct.png")
101 static IMEngineFactoryPointer _scim_pinyin_factory (0);
103 static ConfigPointer _scim_config (0);
105 static Property _status_property (SCIM_PROP_STATUS, "");
106 static Property _letter_property (SCIM_PROP_LETTER, "");
107 static Property _punct_property (SCIM_PROP_PUNCT, "");
109 static char g_common_symbol[]={'#','$','%','^','&','*','@'};
112 void scim_module_init (void)
114 SCIM_DEBUG_IMENGINE (3) << "scim_module_init\n";
115 bindtextdomain (GETTEXT_PACKAGE, SCIM_SUNPINYIN_LOCALEDIR);
116 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
119 void scim_module_exit (void)
121 _scim_pinyin_factory.reset ();
122 _scim_config.reset ();
125 uint32 scim_imengine_module_init (const ConfigPointer &config)
127 SCIM_DEBUG_IMENGINE (3) << "module_init\n";
128 _status_property.set_tip (_("The status of the current input method. Click to change it."));
129 _status_property.set_label ("英");
131 _letter_property.set_icon (SCIM_HALF_LETTER_ICON);
132 _letter_property.set_tip (_("The input mode of the letters. Click to toggle between half and full."));
133 _letter_property.set_label (_("Full/Half Letter"));
135 _punct_property.set_icon (SCIM_HALF_PUNCT_ICON);
136 _punct_property.set_tip (_("The input mode of the puncutations. Click to toggle between half and full."));
137 _punct_property.set_label (_("Full/Half Punct"));
139 _scim_config = config;
143 IMEngineFactoryPointer scim_imengine_module_create_factory (uint32 engine)
145 SCIM_DEBUG_IMENGINE (3) << "entering scim_imengine_module_create_factory()\n";
146 if (engine != 0) return IMEngineFactoryPointer (0);
147 if (_scim_pinyin_factory.null ()) {
148 SunPyFactory *factory = new SunPyFactory (_scim_config);
149 if (factory->valid ())
150 _scim_pinyin_factory = factory;
154 return _scim_pinyin_factory;
158 // implementation of SunPyFactory
159 SunPyFactory::SunPyFactory (const ConfigPointer &config)
163 SCIM_DEBUG_IMENGINE (3) << "SunPyFactory()\n";
164 set_languages ("zh_CN");
165 m_name = utf8_mbstowcs ("SunPinyin");
167 m_reload_signal_connection = m_config->signal_connect_reload (slot (this, &SunPyFactory::reload_config));
168 m_hotkey_profile = new CHotkeyProfile();
172 SunPyFactory::init ()
177 valid = load_user_config();
180 // postpone the load_user_data() to the ctor of SunPyInstance
185 SunPyFactory::load_user_config()
187 // Load configurations.
192 SunPyFactory::~SunPyFactory ()
194 SCIM_DEBUG_IMENGINE (3) << "~SunPyFactory()\n";
195 m_reload_signal_connection.disconnect ();
196 delete m_hotkey_profile;
200 SunPyFactory::get_name () const
206 SunPyFactory::get_authors () const
208 return utf8_mbstowcs (
209 String (_("Lei Zhang, <Phill.Zhang@sun.com>; Shuguagn Yan, <Ervin.Yan@sun.com>")));
213 SunPyFactory::get_credits () const
215 return utf8_mbstowcs (
216 String (_("Ported by Kov Chai, <tchaikov@gmail.com>")));
220 SunPyFactory::get_help () const
223 String (_("Hot Keys:"
225 " Switch between English/Chinese mode."
226 "\n\n Control+period:\n"
227 " Switch between full/half width punctuation mode."
228 "\n\n Shift+space:\n"
229 " Switch between full/half width letter mode."
231 " Page up in lookup table."
233 " Page down in lookup table."
235 " Cancel current syllable.\n"));
236 return utf8_mbstowcs (help);
240 SunPyFactory::get_uuid () const
242 return String ("org.tizen.ise-engine-sunpinyin");
246 SunPyFactory::get_icon_file () const
248 return String (SCIM_SUNPINYIN_ICON_FILE);
251 IMEngineInstancePointer
252 SunPyFactory::create_instance (const String& encoding, int id)
254 SCIM_DEBUG_IMENGINE (3) << "SunPyFactory::create_instance(" << id << ")\n";
255 return new SunPyInstance (this, m_hotkey_profile, encoding, id);
259 SunPyFactory::reload_config (const ConfigPointer &config)
265 // implementation of SunPyInstance
266 SunPyInstance::SunPyInstance (SunPyFactory *factory,
267 CHotkeyProfile *hotkey_profile,
268 const String& encoding,
270 : IMEngineInstanceBase (factory, encoding, id),
274 m_hotkey_profile (hotkey_profile),
276 m_common_lookup_table(0),
278 m_lookup_table_always_on (false)
280 SCIM_DEBUG_IMENGINE (3) << get_id() << ": SunPyInstance()\n";
281 create_session(hotkey_profile);
283 m_reload_signal_connection = factory->m_config->signal_connect_reload (slot (this, &SunPyInstance::reload_config));
284 init_lookup_table_labels ();
287 SunPyInstance::~SunPyInstance ()
289 SCIM_DEBUG_IMENGINE (3) << get_id() << ": ~SunPyInstance()\n";
290 m_reload_signal_connection.disconnect ();
295 translate_key(const KeyEvent& key)
297 // XXX: may need to move this logic into CKeyEvent
298 if (!(key.code & 0xff00) && isprint(key.code) && !isspace(key.code) && !(key.mask & IM_CTRL_MASK)) {
299 // we only care about key_val here
300 return CKeyEvent(0, key.code, key.mask);
302 // what matters is key_code, but ibus sents me key_code as key_val
303 return CKeyEvent(key.code, 0, key.mask);
308 SunPyInstance::process_key_event (const KeyEvent& key)
310 SCIM_DEBUG_IMENGINE (3) << get_id() << ": process_key_event(" << m_focused << ", " <<
315 if (!m_focused) return false;
317 CKeyEvent ev = translate_key(key);
319 if ( !m_pv->getStatusAttrValue(CScimWinHandler::STATUS_ID_CN) ) {
320 // we are in English input mode
321 if ( !m_hotkey_profile->isModeSwitchKey(ev) ) {
322 m_hotkey_profile->rememberLastKey(ev);
326 return ( key.is_key_release() ||
327 m_pv->onKeyEvent(ev) );
331 SunPyInstance::select_candidate (unsigned int item)
333 if(m_lookup_table->number_of_candidates() == 0 && item < sizeof(g_common_symbol)/sizeof(char)) {
334 char _str[2]={g_common_symbol[item],0};
335 commit_string(utf8_mbstowcs(_str));
338 m_pv->onCandidateSelectRequest(item);
339 // m_pv->makeSelection(item);
343 SunPyInstance::update_lookup_table_page_size (unsigned int page_size)
346 SCIM_DEBUG_IMENGINE (3) << ": update_lookup_table_page_size(" << page_size << ")\n";
347 m_pv->setCandiWindowSize(page_size);
348 m_lookup_table->set_page_size(page_size);
353 SunPyInstance::lookup_table_page_up ()
356 m_pv->onCandidatePageRequest(-1, true);
360 SunPyInstance::lookup_page_up()
362 m_lookup_table->page_up();
363 // m_lookup_table->set_page_size(m_pv->s_CandiWindowSize);
367 SunPyInstance::lookup_page_down()
369 m_lookup_table->page_down();
370 // m_lookup_table->set_page_size(m_pv->s_CandiWindowSize);
374 SunPyInstance::lookup_table_page_down ()
376 // XXX, it would be great, if View class expose a page_up() method
377 // m_pv->onKeyEvent(IM_VK_PAGE_DOWN, 0, 0);
378 // classic View overrides this method
379 // but modern View uses the default dummy implementation
381 m_pv->onCandidatePageRequest(1, true);
385 SunPyInstance::move_preedit_caret (unsigned int /*pos*/)
390 SunPyInstance::reset ()
392 SCIM_DEBUG_IMENGINE (3) << get_id() << ": reset()\n";
394 m_lookup_table->clear ();
395 hide_preedit_string ();
396 //hide_aux_string ();
397 //m_pv->updateWindows(m_pv->clearIC());
398 //refresh_all_properties ();
399 if (m_lookup_table_always_on) {
401 show_lookup_table ();
403 hide_lookup_table ();
405 m_pv->updateWindows(CIMIView::PREEDIT_MASK | CIMIView::CANDIDATE_MASK);
409 SunPyInstance::focus_in ()
411 SCIM_DEBUG_IMENGINE(3) << get_id() << ": focus_in ()\n";
413 //show_lookup_table ();
414 initialize_all_properties ();
416 hide_preedit_string ();
417 //hide_aux_string ();
419 init_lookup_table_labels ();
421 //hide_aux_string ();
423 m_pv->updateWindows(CIMIView::PREEDIT_MASK | CIMIView::CANDIDATE_MASK);
427 SunPyInstance::focus_out ()
429 SCIM_DEBUG_IMENGINE(3) << get_id() << ": focus_out ()\n";
434 SunPyInstance::flush ()
436 SCIM_DEBUG_IMENGINE(3) << get_id() << ": flush ()\n";
437 m_pv->onCandidateSelectRequest(0);
441 SunPyInstance::trigger_property (const String &property)
443 SCIM_DEBUG_IMENGINE (3) << get_id() << ": trigger_property(" << property << ")\n";
445 if (property == SCIM_PROP_STATUS) {
446 const int is_CN = m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_CN);
447 m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_CN, is_CN?0:1);
448 } else if (property == SCIM_PROP_LETTER) {
449 const int is_fullsymbol = m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL);
450 m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL, is_fullsymbol?0:1);
451 } else if (property == SCIM_PROP_PUNCT) {
452 const int is_fullpunc = m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC);
453 m_pv->setStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC, is_fullpunc?0:1);
458 SunPyInstance::set_layout (unsigned int layout)
460 show_lookup_table ();
461 m_lookup_table_always_on = true;
465 SunPyInstance::init_lookup_table_labels ()
467 m_pv->setCandiWindowSize(50);
468 m_lookup_table->set_page_size (50);
469 m_lookup_table->show_cursor ();
473 SunPyInstance::initialize_all_properties ()
475 PropertyList proplist;
477 proplist.push_back (_status_property);
478 proplist.push_back (_letter_property);
479 proplist.push_back (_punct_property);
481 register_properties (proplist);
482 refresh_all_properties ();
486 SunPyInstance::refresh_all_properties ()
488 SCIM_DEBUG_IMENGINE (3) << get_id() << ": refresh_all_properties()\n";
489 m_wh->updateStatus(CIMIWinHandler::STATUS_ID_CN,
490 m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_CN));
491 m_wh->updateStatus(CIMIWinHandler::STATUS_ID_FULLPUNC,
492 m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLPUNC));
493 m_wh->updateStatus(CIMIWinHandler::STATUS_ID_FULLSYMBOL,
494 m_pv->getStatusAttrValue(CIMIWinHandler::STATUS_ID_FULLSYMBOL));
499 SunPyInstance::refresh_status_property(bool cn)
501 SCIM_DEBUG_IMENGINE (3) << get_id() << ": refresh_status_property(" << cn << ")\n";
505 _status_property.set_label(cn ? "中" : "英");
506 update_property(_status_property);
510 SunPyInstance::refresh_fullsymbol_property(bool full)
512 SCIM_DEBUG_IMENGINE (3) << get_id() << ": refresh_fullsimbol_property(" << full << ")\n";
513 _letter_property.set_icon(
514 full ? SCIM_FULL_LETTER_ICON : SCIM_HALF_LETTER_ICON);
515 update_property(_letter_property);
519 SunPyInstance::refresh_fullpunc_property(bool full)
521 _punct_property.set_icon(
522 full ? SCIM_FULL_PUNCT_ICON : SCIM_HALF_PUNCT_ICON);
523 update_property(_punct_property);
527 SunPyInstance::create_session(CHotkeyProfile *hotkey_profile)
529 SCIM_DEBUG_IMENGINE (3) << get_id() << ": create_session()\n";
531 CSunpinyinSessionFactory& factory = CSunpinyinSessionFactory::getFactory();
532 factory.setPinyinScheme(CSunpinyinSessionFactory::QUANPIN);
533 factory.setCandiWindowSize(50);
534 m_pv = factory.createSession();
536 SCIM_DEBUG_IMENGINE (3) << get_id() << " factory.createSession() failed\n";
540 m_pv->setHotkeyProfile(hotkey_profile);
542 m_lookup_table = new SunLookupTable();
543 m_common_lookup_table = new CommonLookupTable();
544 for(int i = 0;i < sizeof(g_common_symbol)/sizeof(char);i++) {
545 char _str[2]={g_common_symbol[i],0};
546 m_common_lookup_table->append_candidate (utf8_mbstowcs ((const char*)_str),attrs);
548 m_wh = new CScimWinHandler(this, m_lookup_table);
549 m_pv->attachWinHandler(m_wh);
553 SunPyInstance::destroy_session()
555 SCIM_DEBUG_IMENGINE (3) << get_id() << ": destroy_session()\n";
557 // wh and ic are not pointers, I don't think it's necessary to delete them
561 delete m_lookup_table;
562 delete m_common_lookup_table;
570 SunPyInstance::build_preedit_attribs (const IPreeditString* ppd)
573 const int sz = ppd->charTypeSize();
574 for (int i = 0; i < sz; ) {
575 const int ct = ppd->charTypeAt(i);
576 if (ct & IPreeditString::ILLEGAL) {
578 for (++i; (i<sz) && (ppd->charTypeAt(i) & IPreeditString::ILLEGAL); ++i) ;
579 attrs.push_back( Attribute(start, i-start,
580 SCIM_ATTR_DECORATE, SCIM_ATTR_DECORATE_REVERSE));
581 } else if (ct & IPreeditString::NORMAL_CHAR) {
582 if (ct & IPreeditString::USER_CHOICE) {
584 for (++i; (i<sz) && (ppd->charTypeAt(i) & IPreeditString::USER_CHOICE); ++i) ;
585 attrs.push_back( Attribute(start, i-start,
586 SCIM_ATTR_DECORATE, SCIM_ATTR_DECORATE_UNDERLINE));
598 SunPyInstance::redraw_preedit_string (const IPreeditString* ppd)
600 SCIM_DEBUG_IMENGINE (3) << get_id() << ": redraw_preedit_string()\n";
601 if (ppd->size() != 0) {
603 const int caret = ppd->caret();
604 if (caret > 0 && caret <= ppd->size()) {
605 attrs.push_back( Attribute(ppd->candi_start(),
607 SCIM_ATTR_DECORATE, SCIM_ATTR_DECORATE_REVERSE));
610 for ( ;hanzi_end < ppd->size(); hanzi_end++)
611 if (!(ppd->charTypeAt(hanzi_end) & IPreeditString::USER_CHOICE))
613 WideString preedit = wstr_to_widestr(ppd->string(), ppd->size());
614 update_preedit_string( preedit, preedit.substr(0, hanzi_end) + m_lookup_table->get_candidate(0), attrs, caret);
615 show_preedit_string ();
617 hide_preedit_string ();
622 SunPyInstance::redraw_lookup_table(const ICandidateList* pcl)
624 SCIM_DEBUG_IMENGINE (3) << get_id() << ": redraw_lookup_table()\n";
626 m_lookup_table->update(*pcl);
627 if (m_lookup_table->number_of_candidates()) {
628 update_lookup_table(*m_lookup_table);
630 update_lookup_table(*m_common_lookup_table);
635 SunPyInstance::reload_config(const ConfigPointer &config)
637 SCIM_DEBUG_IMENGINE (3) << get_id() << ": reload_config()\n";
639 if (m_factory->valid()) {
640 m_factory->load_user_config();