void WaitZarInstance::typeSentence(const wchar_t* extra) { //Commit the modified string wcscat(prefix_string, postfix_string); wcscat(prefix_string, extra); commit_string(prefix_string); //Reset resetInstance(); }
void HangulInstance::select_candidate (unsigned int index) { SCIM_DEBUG_IMENGINE(2) << "select_candidate.\n"; if ((int)index >= m_lookup_table.get_current_page_size ()) return; WideString candidate = m_lookup_table.get_candidate_in_current_page(index); WideString commit_str = candidate; WideString preedit = get_preedit_string(); if (is_hanja_mode() || m_factory->m_commit_by_word) { // prefix method int len = m_surrounding_text.length(); if (len > 0) delete_surrounding_text(-len, len); if (candidate.length() <= m_surrounding_text.length()) { len = m_surrounding_text.length() - candidate.length(); commit_str.append(m_surrounding_text, candidate.length(), len); m_surrounding_text.erase(0, candidate.length()); } else if (candidate.length() <= m_surrounding_text.length() + preedit.length()) { len = candidate.length() - m_surrounding_text.length(); if (len > (int)m_preedit.length()) { m_preedit.clear(); hangul_ic_reset(m_hic); } else { m_preedit.erase(0, len); } m_surrounding_text.clear(); } else { m_preedit.clear(); hangul_ic_reset(m_hic); m_surrounding_text.clear(); } } else { // suffix method if (candidate.length() > preedit.length()) { int len = candidate.length() - preedit.length(); delete_surrounding_text(-len, len); } hangul_ic_reset(m_hic); m_surrounding_text.clear(); } commit_string(commit_str); hangul_update_preedit_string (); if (is_hanja_mode()) { update_candidates(); } else { delete_candidates(); } }
bool ThaiInstance::process_key_event (const KeyEvent& key) { if (key.is_key_release() || key.code == 0 || __is_context_intact_key (key.code)) { return false; } if (key.mask & (SCIM_KEY_AllMasks & ~(SCIM_KEY_ShiftMask | SCIM_KEY_CapsLockMask)) || __is_context_lost_key (key.code)) { _forget_previous_chars (); return false; } KeyEvent thai_key = m_keymap.map_key (key); ucs4_t thai_uni = thai_key.get_unicode_code (); if (!th_wcistis (thai_uni)) return false; thchar_t thai_tis = th_uni2tis (thai_uni); thcell_t context_cell = _get_previous_cell (); thinpconv_t conv; if (th_validate (context_cell, thai_tis, &conv)) { if (conv.offset < 0) if (!delete_surrounding_text (conv.offset, -conv.offset)) return false; _forget_previous_chars (); _remember_previous_char (thai_tis); WideString str; for (int i = 0; conv.conv [i]; i++) str.push_back (th_tis2uni (conv.conv [i])); commit_string (str); } else { beep (); } return true; }
int main(int argc, char *argv[]) { ChewingContext *ctx; int i; FILE *fp = stdin; /* Initialize libchewing */ putenv("CHEWING_PATH=" CHEWING_DATA_PREFIX); /* for the sake of testing, we should not change existing hash data */ putenv("CHEWING_USER_PATH=" TEST_HASH_DIR); if (argc == 2) { fp = fopen(argv[1], "r"); if (!fp) { fprintf(stderr, "failed to open '%s'\n", argv[1]); return 1; } } /* Request handle to ChewingContext */ ctx = chewing_new(); /* Set keyboard type */ chewing_set_KBType(ctx, chewing_KBStr2Num("KB_DEFAULT")); chewing_set_candPerPage(ctx, 9); chewing_set_maxChiSymbolLen(ctx, 16); chewing_set_addPhraseDirection(ctx, 1); chewing_set_selKey(ctx, selKey_define, 10); chewing_set_spaceAsSelection(ctx, 1); while (1) { i = get_keystroke(get_char_from_fp, fp); if (i == END) goto end; type_single_keystroke(ctx, i); commit_string(ctx); } end: /* Free Chewing IM handle */ chewing_delete(ctx); if (fp) fclose(fp); return 0; }
void HangulInstance::flush() { SCIM_DEBUG_IMENGINE(2) << "flush.\n"; hide_preedit_string(); WideString wstr = m_preedit; const ucschar *str = hangul_ic_flush(m_hic); while (*str != 0) wstr.push_back (*str++); if (wstr.length()) commit_string(wstr); delete_candidates (); m_preedit.clear(); }
int main( int argc, char *argv[] ) #endif { ChewingContext *ctx; char *prefix = CHEWING_DATA_PREFIX; int i; int ctrl_shifted; /* Initialize libchewing */ /* for the sake of testing, we should not change existing hash data */ chewing_Init( prefix, TEST_HASH_DIR ); /* Request handle to ChewingContext */ ctx = chewing_new(); /* Set keyboard type */ chewing_set_KBType( ctx, chewing_KBStr2Num( "KB_DEFAULT" ) ); chewing_set_candPerPage( ctx, 9 ); chewing_set_maxChiSymbolLen( ctx, 16 ); chewing_set_addPhraseDirection( ctx, 1 ); chewing_set_selKey( ctx, selKey_define, 10 ); chewing_set_spaceAsSelection( ctx, 1 ); while ( 1 ) { i = get_keystroke(); switch ( i ) { case KEY_LEFT: chewing_handle_Left( ctx ); break; case KEY_SLEFT: chewing_handle_ShiftLeft( ctx ); break; case KEY_RIGHT: chewing_handle_Right( ctx ); break; case KEY_SRIGHT: chewing_handle_ShiftRight( ctx ); break; case KEY_UP: chewing_handle_Up( ctx ); break; case KEY_DOWN: chewing_handle_Down( ctx ); break; case KEY_SPACE: chewing_handle_Space( ctx ); break; case KEY_ENTER: chewing_handle_Enter( ctx ); break; case KEY_BACKSPACE: chewing_handle_Backspace( ctx ); break; case KEY_ESC: chewing_handle_Esc( ctx ); break; case KEY_DELETE: chewing_handle_Del( ctx ); break; case KEY_HOME: chewing_handle_Home( ctx ); break; case KEY_END: chewing_handle_End( ctx ); break; case KEY_TAB: chewing_handle_Tab( ctx ); break; case KEY_CAPSLOCK: chewing_handle_Capslock( ctx ); break; case END: goto end; default: ctrl_shifted = ( i - KEY_CTRL_BASE ); if ( ( ctrl_shifted >= '0' ) && ( ctrl_shifted <= '9' ) ) { chewing_handle_CtrlNum( ctx, ctrl_shifted ); } else { chewing_handle_Default( ctx, (char) i ); } break; } commit_string( ctx ); #ifdef USED_IN_SIMULATION if ( i == KEY_ENTER ) compare_per_run(); #endif } end: /* Free Chewing IM handle */ chewing_delete( ctx ); /* Termate Chewing services */ chewing_Terminate(); #ifndef USED_IN_SIMULATION printf( "\n" ); #endif return 0; }
bool SocketInstance::do_transaction (Transaction &trans, bool &ret) { int cmd = -1; bool cont = false; ret = false; SCIM_DEBUG_IMENGINE(2) << " Do transaction:\n"; if (trans.get_command (cmd) && cmd == SCIM_TRANS_CMD_REPLY) { while (trans.get_command (cmd)) { switch (cmd) { case SCIM_TRANS_CMD_SHOW_PREEDIT_STRING: { SCIM_DEBUG_IMENGINE(3) << " show_preedit_string ()\n"; show_preedit_string (); break; } case SCIM_TRANS_CMD_SHOW_AUX_STRING: { SCIM_DEBUG_IMENGINE(3) << " show_aux_string ()\n"; show_aux_string (); break; } case SCIM_TRANS_CMD_SHOW_LOOKUP_TABLE: { SCIM_DEBUG_IMENGINE(3) << " show_lookup_table ()\n"; show_lookup_table (); break; } case SCIM_TRANS_CMD_HIDE_PREEDIT_STRING: { SCIM_DEBUG_IMENGINE(3) << " hide_preedit_string ()\n"; hide_preedit_string (); break; } case SCIM_TRANS_CMD_HIDE_AUX_STRING: { SCIM_DEBUG_IMENGINE(3) << " hide_aux_string ()\n"; hide_aux_string (); break; } case SCIM_TRANS_CMD_HIDE_LOOKUP_TABLE: { SCIM_DEBUG_IMENGINE(3) << " hide_lookup_table ()\n"; hide_lookup_table (); break; } case SCIM_TRANS_CMD_UPDATE_PREEDIT_CARET: { uint32 caret; if (trans.get_data (caret)) { SCIM_DEBUG_IMENGINE(3) << " update_preedit_caret (" << caret << ")\n"; update_preedit_caret (caret); } break; } case SCIM_TRANS_CMD_UPDATE_PREEDIT_STRING: { WideString str; AttributeList attrs; if (trans.get_data (str) && trans.get_data (attrs)) { SCIM_DEBUG_IMENGINE(3) << " update_preedit_string ()\n"; update_preedit_string (str, attrs); } break; } case SCIM_TRANS_CMD_UPDATE_AUX_STRING: { WideString str; AttributeList attrs; if (trans.get_data (str) && trans.get_data (attrs)) { SCIM_DEBUG_IMENGINE(3) << " update_aux_string ()\n"; update_aux_string (str, attrs); } break; } case SCIM_TRANS_CMD_UPDATE_LOOKUP_TABLE: { CommonLookupTable table; if (trans.get_data (table)) { SCIM_DEBUG_IMENGINE(3) << " update_lookup_table ()\n"; update_lookup_table (table); } break; } case SCIM_TRANS_CMD_COMMIT_STRING: { WideString str; if (trans.get_data (str)) { SCIM_DEBUG_IMENGINE(3) << " commit_string ()\n"; commit_string (str); } break; } case SCIM_TRANS_CMD_FORWARD_KEY_EVENT: { KeyEvent key; if (trans.get_data (key)) { SCIM_DEBUG_IMENGINE(3) << " forward_key_event ()\n"; forward_key_event (key); } break; } case SCIM_TRANS_CMD_REGISTER_PROPERTIES: { PropertyList proplist; if (trans.get_data (proplist)) { SCIM_DEBUG_IMENGINE(3) << " register_properties ()\n"; // Load icon files of these properties from remote SocketFrontEnd. for (PropertyList::iterator it = proplist.begin (); it != proplist.end (); ++it) it->set_icon (global->load_icon (it->get_icon ())); register_properties (proplist); } break; } case SCIM_TRANS_CMD_UPDATE_PROPERTY: { Property prop; if (trans.get_data (prop)) { SCIM_DEBUG_IMENGINE(3) << " update_property ()\n"; // Load the icon file of this property from remote SocketFrontEnd. prop.set_icon (global->load_icon (prop.get_icon ())); update_property (prop); } break; } case SCIM_TRANS_CMD_BEEP: { SCIM_DEBUG_IMENGINE(3) << " beep ()\n"; beep (); break; } case SCIM_TRANS_CMD_START_HELPER: { String helper_uuid; if (trans.get_data (helper_uuid)) { SCIM_DEBUG_IMENGINE(3) << " start_helper (" << helper_uuid << ")\n"; start_helper (helper_uuid); } break; } case SCIM_TRANS_CMD_STOP_HELPER: { String helper_uuid; if (trans.get_data (helper_uuid)) { SCIM_DEBUG_IMENGINE(3) << " stop_helper (" << helper_uuid << ")\n"; stop_helper (helper_uuid); } break; } case SCIM_TRANS_CMD_SEND_HELPER_EVENT: { String helper_uuid; Transaction temp_trans; if (trans.get_data (helper_uuid) && trans.get_data (temp_trans)) { SCIM_DEBUG_IMENGINE(3) << " send_helper_event (" << helper_uuid << ")\n"; send_helper_event (helper_uuid, temp_trans); } break; } case SCIM_TRANS_CMD_OK: { SCIM_DEBUG_IMENGINE(3) << " ret = true\n"; ret = true; break; } case SCIM_TRANS_CMD_GET_SURROUNDING_TEXT: { WideString text; int cursor; uint32 maxlen_before; uint32 maxlen_after; Transaction temp_trans; if (trans.get_data (maxlen_before) && trans.get_data (maxlen_after)) { global->init_transaction (temp_trans); if (get_surrounding_text (text, cursor, (int) maxlen_before, (int) maxlen_after)) { temp_trans.put_command (SCIM_TRANS_CMD_GET_SURROUNDING_TEXT); temp_trans.put_data (text); temp_trans.put_data ((uint32) cursor); } else { temp_trans.put_command (SCIM_TRANS_CMD_FAIL); } global->send_transaction (temp_trans); } cont = true; break; } case SCIM_TRANS_CMD_DELETE_SURROUNDING_TEXT: { uint32 offset; uint32 len; Transaction temp_trans; if (trans.get_data (offset) && trans.get_data (len)) { global->init_transaction (temp_trans); if (delete_surrounding_text ((int) offset, (int) len)) { temp_trans.put_command (SCIM_TRANS_CMD_DELETE_SURROUNDING_TEXT); temp_trans.put_command (SCIM_TRANS_CMD_OK); } else { temp_trans.put_command (SCIM_TRANS_CMD_FAIL); } global->send_transaction (temp_trans); } cont = true; break; } default: SCIM_DEBUG_IMENGINE(3) << " Strange cmd: " << cmd << "\n";; } } } else { SCIM_DEBUG_IMENGINE(3) << " Failed to get cmd: " << cmd << "\n"; } SCIM_DEBUG_IMENGINE(2) << " End of Do transaction\n"; return cont; }
int main(int argc, char *argv[]) { size_t i; int flag_random_init = 0; int flag_random_extra = 0; int flag_loop = -1; int (*get_input)() = &random256; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-init") == 0) flag_random_init = 1; else if (strcmp(argv[i], "-extra") == 0 && argv[i + 1]) flag_random_extra = 1; else if (strcmp(argv[i], "-loop") == 0 && argv[i + 1]) flag_loop = atoi(argv[++i]); else if (strcmp(argv[i], "-stdin") == 0) get_input = &get_stdin; else { printf("Usage: %s [-init] [-extra] [-loop N] [-stdin]\n", argv[0]); printf("\t-init Random initial configuration\n"); printf("\t-extra Random change all configurations during input.\n"); printf("\t This is usually unexpected.\n"); printf("\t-stdin Get random input from stdin\n"); printf("\t-loop N How many iterations to test (default infinite=-1)\n"); exit(1); } } /* Initialize libchewing */ putenv("CHEWING_PATH=" CHEWING_DATA_PREFIX); /* for the sake of testing, we should not change existing hash data */ putenv("CHEWING_USER_PATH=" TEST_HASH_DIR); for (i = 0; i != flag_loop; i++) { ChewingContext *ctx = chewing_new(); /* typical configuration */ chewing_set_KBType(ctx, chewing_KBStr2Num("KB_DEFAULT")); chewing_set_candPerPage(ctx, 9); chewing_set_maxChiSymbolLen(ctx, 16); chewing_set_addPhraseDirection(ctx, 1); chewing_set_selKey(ctx, selKey_define, 10); chewing_set_spaceAsSelection(ctx, 1); if (flag_random_init) { chewing_set_KBType(ctx, get_input()); chewing_set_candPerPage(ctx, get_input()); chewing_set_maxChiSymbolLen(ctx, get_input()); chewing_set_addPhraseDirection(ctx, get_input()); chewing_set_selKey(ctx, selKey_define, get_input() % 11); chewing_set_spaceAsSelection(ctx, get_input()); chewing_set_escCleanAllBuf(ctx, get_input()); chewing_set_autoShiftCur(ctx, get_input()); chewing_set_easySymbolInput(ctx, get_input()); chewing_set_phraseChoiceRearward(ctx, get_input()); } while (1) { /* Random value: [0, max_key) for keys, [max_key, 0xff] for * configurations. Use a fixed range here because I don't want the * meaning of input changed a lot frequently if we add more keys in * the future. */ const int max_key = 192; /* arbitrary number */ int v = get_input(); if (v == EOF) break; assert(max_key * sizeof(all_key[0]) >= sizeof(all_key)); if (v >= max_key) { const int typical = 2; int handled = 1; v = v - max_key; if (flag_random_extra || v < typical) { switch (v) { /* typical configurations may be changed during input */ case 0: chewing_set_ChiEngMode(ctx, get_input()); break; case 1: chewing_set_ShapeMode(ctx, get_input()); break; /* usually not changed during input */ case 2: chewing_set_KBType(ctx, get_input()); break; case 3: chewing_set_candPerPage(ctx, get_input()); break; case 4: chewing_set_maxChiSymbolLen(ctx, get_input()); break; case 5: chewing_set_addPhraseDirection(ctx, get_input()); break; case 6: chewing_set_selKey(ctx, selKey_define, get_input() % 11); break; case 7: chewing_set_spaceAsSelection(ctx, get_input()); break; case 8: chewing_set_escCleanAllBuf(ctx, get_input()); break; case 9: chewing_set_autoShiftCur(ctx, get_input()); break; case 10: chewing_set_easySymbolInput(ctx, get_input()); break; case 11: chewing_set_phraseChoiceRearward(ctx, get_input()); break; default: handled = 0; break; } } else { handled = 0; } if (!handled) break; } else { if (0 <= v && v < max_key) { int key = all_key[v]; type_single_keystroke(ctx, key); } else { break; } } commit_string(ctx); } chewing_delete(ctx); if (i % 10000 == 0) printf("%zu\n", i); if (getenv("AFL_PERSISTENT")) raise(SIGSTOP); } return 0; }
bool HangulInstance::process_key_event (const KeyEvent& rawkey) { SCIM_DEBUG_IMENGINE(1) << "process_key_event.\n"; KeyEvent key = rawkey.map_to_layout(SCIM_KEYBOARD_Default); m_prev_key = key; if (use_ascii_mode() && !is_hangul_mode()) { if (is_hangul_key(key)) { toggle_hangul_mode(); return true; } return false; } /* ignore key release. */ if (key.is_key_release ()) return false; /* mode change */ if (use_ascii_mode() && is_hangul_key(key)) { toggle_hangul_mode(); return true; } /* hanja mode */ if (is_hanja_mode_key (key)) { toggle_hanja_mode(); } /* toggle candidate table */ if (is_hanja_key (key)) { if (is_hanja_mode()) { update_candidates (); } else { if (m_lookup_table.number_of_candidates ()) delete_candidates (); else update_candidates (); } return true; } /* ignore shift keys */ if (key.code == SCIM_KEY_Shift_L || key.code == SCIM_KEY_Shift_R) return false; /* flush on modifier-on keys */ if (key.is_control_down() || key.is_alt_down()) { flush (); return false; } /* candidate keys */ if (m_lookup_table.number_of_candidates ()) { if (candidate_key_event(key)) return true; } /* change to ascii mode on ESCAPE key, for vi users. * We should process this key after processing candidate keys, * or input mode will be changed to non-hangul mode when the user presses * escape key to close candidate window. */ if (use_ascii_mode() && !is_hanja_mode()) { if (key.code == SCIM_KEY_Escape) { toggle_hangul_mode(); } } /* backspace */ if (is_backspace_key(key)) { bool ret = hangul_ic_backspace(m_hic); if (ret) { hangul_update_preedit_string (); } else if (m_preedit.length() > 0) { ret = true; m_preedit.erase(m_preedit.length() - 1, 1); hangul_update_preedit_string(); } else { if (m_surrounding_text.length() > 0) { m_surrounding_text.erase(m_surrounding_text.length() - 1, 1); if (m_surrounding_text.empty()) { delete_candidates(); return ret; } } } if (is_hanja_mode() && m_lookup_table.number_of_candidates()) { update_candidates(); } return ret; } if (key.code >= SCIM_KEY_exclam && key.code <= SCIM_KEY_asciitilde) { /* main hangul composing process */ int ascii = key.get_ascii_code(); if (key.is_caps_lock_down()) { if (isupper(ascii)) ascii = tolower(ascii); else if (islower(ascii)) ascii = toupper(ascii); } bool ret = hangul_ic_process(m_hic, ascii); WideString wstr; wstr = get_commit_string (); if (wstr.length ()) { /* Before commit, we set preedit string to null to work arround * some buggy IM implementation, ex) Qt, Evolution */ hide_preedit_string (); if (is_hanja_mode() || m_factory->m_commit_by_word) { m_preedit += wstr; } else { commit_string(wstr); } } if (is_hanja_mode() || m_factory->m_commit_by_word) { if (hangul_ic_is_empty(m_hic)) { flush(); } } hangul_update_preedit_string (); if (is_hanja_mode()) { update_candidates(); } return ret; } flush(); return false; }
bool ComposeKeyInstance::process_key_event (const KeyEvent& key) { if (key.is_key_release ()) return false; // Ignore modifier key presses. if (std::binary_search (__scim_compose_ignores, __scim_compose_ignores + SCIM_NUM_COMPOSE_IGNORES, (uint16) key.code)) return false; // Ignore the key if ctrl or alt is down. if (key.is_control_down () || key.is_alt_down ()) return false; int n_compose = 0; while (m_compose_buffer [n_compose] != 0 && n_compose < SCIM_MAX_COMPOSE_LEN) ++ n_compose; // The buffer is full, then reset the buffer first. if (n_compose == SCIM_MAX_COMPOSE_LEN) { reset (); n_compose = 0; } m_compose_buffer [n_compose] = (uint32) key.code; const ComposeSequence *it = std::lower_bound (__scim_compose_seqs, __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS, m_compose_buffer, ComposeSequenceLessByKeys ()); // Not result found, reset the buffer and return false. if (it == __scim_compose_seqs + SCIM_NUM_COMPOSE_SEQS) { reset (); return false; } // Check if the compose sequence is match. for (n_compose = 0; n_compose < SCIM_MAX_COMPOSE_LEN; ++ n_compose) { if (m_compose_buffer [n_compose] == 0) break; // Not match, reset the buffer and return. // If it's the first key press, then return false to forward it. // Otherwise return true to ignore it. if (m_compose_buffer [n_compose] != it->keys [n_compose]) { reset (); return n_compose != 0; } } // Match exactly, commit the result. if (n_compose == SCIM_MAX_COMPOSE_LEN || it->keys [n_compose] == 0) { WideString wstr; wstr.push_back (it->unicode); commit_string (wstr); reset (); } return true; }
void FilterInstanceBase::filter_commit_string (const WideString &str) { commit_string (str); }