Exemplo n.º 1
0
void CloudPinyinFillCandidateWord(FcitxCloudPinyin* cloudpinyin,
                                  const char* pinyin)
{
    CloudPinyinCache* cacheEntry = CloudPinyinCacheLookup(cloudpinyin, pinyin);
    FcitxInputState* input = FcitxInstanceGetInputState(cloudpinyin->owner);
    FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
    if (cacheEntry) {
        int cloudidx;
        FcitxCandidateWord *candWord;
        for (cloudidx = 0;
             (candWord = FcitxCandidateWordGetByTotalIndex(candList, cloudidx));
             cloudidx++) {
            if (candWord->owner == cloudpinyin)
                break;
        }

        if (candWord == NULL)
            return;

        CloudCandWord* cloudCand = candWord->priv;
        if (cloudCand->filled)
            return;

        FcitxCandidateWord *cand;
        int i;
        int pagesize = FcitxCandidateWordGetPageSize(candList);
        int size = pagesize * CLOUDPINYIN_CHECK_PAGE_NUMBER;
        for (i = 0;i < size &&
                 (cand = FcitxCandidateWordGetByTotalIndex(candList, i));i++) {
            if (strcmp(cand->strWord, cacheEntry->str) == 0) {
                uint64_t ts = cloudCand->timestamp;
                uint64_t curTs = CloudGetTimeStamp();
                FcitxCandidateWordRemove(candList, candWord);
                /* if cloud word is not on the first page.. impossible */
                if (cloudidx < pagesize) {
                    /* if the duplication before cloud word */
                    if (i < cloudidx) {
                        if (curTs - ts
                            > LOADING_TIME_QUICK_THRESHOLD) {
                            FcitxCandidateWordInsertPlaceHolder(candList, cloudidx);
                            FcitxCandidateWord* placeHolder = FcitxCandidateWordGetByTotalIndex(candList, cloudidx);
                            if (placeHolder && placeHolder->strWord == NULL)
                                placeHolder->strWord = strdup(DUP_PLACE_HOLDER);
                        }
                    } else {
                        if (i >= pagesize) {
                            FcitxCandidateWordMove(candList, i - 1, cloudidx);
                        } else {
                            if (curTs - ts > LOADING_TIME_QUICK_THRESHOLD) {
                                FcitxCandidateWordInsertPlaceHolder(candList, cloudidx);
                                FcitxCandidateWord* placeHolder = FcitxCandidateWordGetByTotalIndex(candList, cloudidx);
                                if (placeHolder && placeHolder->strWord == NULL)
                                    placeHolder->strWord = strdup(DUP_PLACE_HOLDER);
                            }
                        }
                    }
                }
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
                candWord = NULL;
                break;
            }
        }

        if (candWord) {
            if (cloudCand->filled == false) {
                cloudCand->filled = true;
                free(candWord->strWord);
                candWord->strWord = strdup(cacheEntry->str);
                if (cloudpinyin->config.iCandidateOrder <= 1 &&
                    (CloudGetTimeStamp() - cloudCand->timestamp
                     <= LOADING_TIME_QUICK_THRESHOLD)) {
                    FcitxCandidateWordMoveByWord(candList, candWord, 0);
                    CloudSetClientPreedit(cloudpinyin, cacheEntry->str);
                }
                FcitxUIUpdateInputWindow(cloudpinyin->owner);
            }
        }
    }
}
Exemplo n.º 2
0
boolean UnicodePreFilter(void* arg, FcitxKeySym sym, unsigned int state,
                         INPUT_RETURN_VALUE *r)
{
    INPUT_RETURN_VALUE retVal = IRV_TO_PROCESS;
    do {
        UnicodeModule *uni = arg;
        if (!uni->enable)
            break;
        FcitxInstance *instance = uni->owner;
        FcitxInputState *input = FcitxInstanceGetInputState(instance);
        FcitxGlobalConfig *fc = FcitxInstanceGetGlobalConfig(instance);
        FcitxCandidateWordList *candList;
        candList = FcitxInputStateGetCandidateList(input);

        FcitxCandidateWordSetPageSize(candList, 4);
        FcitxCandidateWordSetChooseAndModifier(candList, DIGIT_STR_CHOOSE,
                                               FcitxKeyState_Alt);
        if (FcitxHotkeyIsHotKey(sym, state,
                                FcitxConfigNextPageKey(instance, fc))) {
            if (FcitxCandidateWordGoPrevPage(candList))
                retVal = IRV_DISPLAY_MESSAGE;
            else
                retVal = IRV_DO_NOTHING;
        } else if (FcitxHotkeyIsHotKey(sym, state,
                                       FcitxConfigNextPageKey(instance, fc))) {
            if (FcitxCandidateWordGoNextPage(candList))
                retVal = IRV_DISPLAY_MESSAGE;
            else
                retVal = IRV_DO_NOTHING;
        } else if (FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE)) {
            size_t len = strlen(uni->buffer);
            if (len > 0)
                uni->buffer[--len] = '\0';
            if (len == 0) {
                retVal = IRV_CLEAN;
            } else {
                retVal = UnicodeGetCandWords(uni);
            }
        } else if (FcitxHotkeyIsHotKey(sym, state, FCITX_ESCAPE)) {
            retVal = IRV_CLEAN;
        }

        if (retVal == IRV_TO_PROCESS) {
            int index = FcitxCandidateWordCheckChooseKey(candList, sym, state);
            if (index >= 0)
                retVal = FcitxCandidateWordChooseByIndex(candList, index);
        }

        FcitxKeySym keymain = FcitxHotkeyPadToMain(sym);
        if (retVal == IRV_TO_PROCESS && FcitxHotkeyIsHotKeySimple(keymain, state)) {
            char buf[2];
            buf[0] = keymain;
            buf[1] = '\0';
            if (strlen(uni->buffer) < MAX_USER_INPUT)
                strcat(uni->buffer, buf);
            retVal = UnicodeGetCandWords(uni);
        }
    } while(0);

    *r = retVal;
    if (retVal == IRV_TO_PROCESS)
        return false;
    return true;
}
Exemplo n.º 3
0
static INPUT_RETURN_VALUE
AutoEngCheckSelect(FcitxAutoEngState *autoEngState,
                   FcitxKeySym sym, unsigned int state)
{
    FcitxInstance *instance = autoEngState->owner;
    FcitxCandidateWordList *cand_list = FcitxInputStateGetCandidateList(
        FcitxInstanceGetInputState(autoEngState->owner));
    if (!FcitxCandidateWordGetListSize(cand_list))
        return IRV_TO_PROCESS;
    FcitxInputState *input = FcitxInstanceGetInputState(instance);
    FcitxGlobalConfig *fc = FcitxInstanceGetGlobalConfig(instance);
    int key;
    FcitxCandidateWord *cand_word;
    if (FcitxHotkeyIsHotKey(sym, state, fc->nextWord)) {
        if (!autoEngState->cursor_moved) {
            cand_word = FcitxCandidateWordGetCurrentWindow(cand_list);
        } else {
            cand_word = FcitxCandidateWordGetFocus(cand_list, true);
            cand_word = FcitxCandidateWordGetNext(cand_list, cand_word);
            if (!cand_word) {
                FcitxCandidateWordSetPage(cand_list, 0);
            } else {
                FcitxCandidateWordSetFocus(
                    cand_list, FcitxCandidateWordGetIndex(cand_list,
                                                          cand_word));
            }
        }
    } else if (FcitxHotkeyIsHotKey(sym, state, fc->prevWord)) {
        if (!autoEngState->cursor_moved) {
            cand_word = FcitxCandidateWordGetByIndex(
                cand_list,
                FcitxCandidateWordGetCurrentWindowSize(cand_list) - 1);
        } else {
            cand_word = FcitxCandidateWordGetFocus(cand_list, true);
            cand_word = FcitxCandidateWordGetPrev(cand_list, cand_word);
            if (cand_word) {
                FcitxCandidateWordSetFocus(
                    cand_list, FcitxCandidateWordGetIndex(cand_list,
                                                          cand_word));
            }
        }
    } else if (FcitxHotkeyIsHotKey(sym, state,
                                   FcitxConfigPrevPageKey(instance, fc))) {
        boolean has_prev_page;
        cand_word = FcitxCandidateWordGetFocus(cand_list, true);
        has_prev_page = FcitxCandidateWordGoPrevPage(cand_list);
        if (!autoEngState->cursor_moved) {
            cand_word = NULL;
        } else if (has_prev_page) {
            cand_word = FcitxCandidateWordGetCurrentWindow(cand_list) +
                FcitxCandidateWordGetCurrentWindowSize(cand_list) - 1;
        }
    } else if (FcitxHotkeyIsHotKey(sym, state,
                                   FcitxConfigNextPageKey(instance, fc))) {
        boolean has_next_page;
        cand_word = FcitxCandidateWordGetFocus(cand_list, true);
        has_next_page = FcitxCandidateWordGoNextPage(cand_list);
        if (!autoEngState->cursor_moved) {
            cand_word = NULL;
        } else if (has_next_page) {
            cand_word = FcitxCandidateWordGetCurrentWindow(cand_list);
        }
    } else if ((key = FcitxCandidateWordCheckChooseKey(cand_list,
                                                       sym, state)) >= 0) {
        return FcitxCandidateWordChooseByIndex(cand_list, key);
    } else {
        return IRV_TO_PROCESS;
    }
    if (cand_word) {
        FcitxCandidateWordSetType(cand_word, MSG_CANDIATE_CURSOR);
        if (!autoEngState->cursor_moved)
            AutoEngSwapBuff(autoEngState);
        AutoEngSetBuff(autoEngState, cand_word->strWord, '\0');
        autoEngState->cursor_moved = true;
    } else if (autoEngState->cursor_moved) {
        AutoEngSwapBuff(autoEngState);
        autoEngState->cursor_moved = false;
    } else {
        return IRV_FLAG_UPDATE_INPUT_WINDOW;
    }
    FcitxMessages *client_preedit = FcitxInputStateGetClientPreedit(input);
    FcitxMessages *preedit = FcitxInputStateGetPreedit(input);
    FcitxMessagesSetMessageCount(client_preedit, 0);
    FcitxMessagesSetMessageCount(preedit, 0);
    FcitxMessagesAddMessageStringsAtLast(client_preedit, MSG_INPUT,
                                         autoEngState->buf);
    FcitxMessagesAddMessageStringsAtLast(preedit, MSG_INPUT,
                                         autoEngState->buf);
    FcitxInputStateSetCursorPos(input, autoEngState->index);
    FcitxInputStateSetClientCursorPos(input, autoEngState->index);
    return IRV_FLAG_UPDATE_INPUT_WINDOW;
}
Exemplo n.º 4
0
INPUT_RETURN_VALUE FcitxKkcGetCandWords(void* arg)
{
    FcitxKkc *kkc = (FcitxKkc*)arg;
    FcitxInputState* input = FcitxInstanceGetInputState(kkc->owner);
    FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
    FcitxMessages* clientPreedit = FcitxInputStateGetClientPreedit(input);
    FcitxMessages* preedit = FcitxInputStateGetPreedit(input);
    FcitxInstanceCleanInputWindow(kkc->owner);

    FcitxMessages* message = FcitxInstanceICSupportPreedit(kkc->owner, FcitxInstanceGetCurrentIC(kkc->owner)) ? clientPreedit : preedit;

    FcitxCandidateWordSetChoose(candList, DIGIT_STR_CHOOSE);
    FcitxCandidateWordSetPageSize(candList, kkc->config.pageSize);
    FcitxCandidateWordSetLayoutHint(candList, kkc->config.candidateLayout);
    FcitxInputStateSetShowCursor(input, true);

    KkcSegmentList* segments = kkc_context_get_segments(kkc->context);
    if (kkc_segment_list_get_cursor_pos(segments) >= 0) {
        int i = 0;
        int offset = 0;
        for (i = 0; i < kkc_segment_list_get_size(segments); i ++) {
            KkcSegment* segment = kkc_segment_list_get(segments, i);
            const gchar* str = kkc_segment_get_output(segment);
            FcitxMessageType messageType = MSG_INPUT;
            if (i < kkc_segment_list_get_cursor_pos(segments)) {
                offset += strlen(str);
            }
            if (i == kkc_segment_list_get_cursor_pos(segments)) {
                messageType = (FcitxMessageType) (MSG_HIGHLIGHT | MSG_OTHER);
            }
            FcitxMessagesAddMessageAtLast(message, messageType, "%s", str);
        }

        if (message == clientPreedit) {
            FcitxInputStateSetClientCursorPos(input, offset);
        } else {
            FcitxInputStateSetCursorPos(input, offset);
        }
    } else {
        gchar* str = kkc_context_get_input(kkc->context);
        if (str && str[0]) {
            FcitxMessagesAddMessageAtLast(message, MSG_INPUT, "%s", str);

            if (message == clientPreedit) {
                FcitxInputStateSetClientCursorPos(input, strlen(str));
            } else {
                FcitxInputStateSetCursorPos(input, strlen(str));
            }
        }
        g_free(str);
    }

    KkcCandidateList* kkcCandidates = kkc_context_get_candidates(kkc->context);
    if (kkc_candidate_list_get_page_visible(kkcCandidates)) {
        int i, j;
        guint size = kkc_candidate_list_get_size(kkcCandidates);
        gint cursor_pos = kkc_candidate_list_get_cursor_pos(kkcCandidates);
        guint page_start = kkc_candidate_list_get_page_start(kkcCandidates);
        guint page_size = kkc_candidate_list_get_page_size(kkcCandidates);
        for (i = kkc_candidate_list_get_page_start(kkcCandidates), j = 0; i < size; i ++, j++) {
            FcitxCandidateWord word;
            word.callback = FcitxKkcGetCandWord;
            word.extraType = MSG_OTHER;
            word.owner = kkc;
            int* id = fcitx_utils_new(int);
            *id = j;
            word.priv = id;
            word.strExtra = NULL;
            word.strExtra = MSG_TIPS;
            KkcCandidate* kkcCandidate = kkc_candidate_list_get(kkcCandidates, i);
            if (kkc->config.showAnnotation && kkc_candidate_get_annotation(kkcCandidate)) {
                fcitx_utils_alloc_cat_str(word.strExtra, " [", kkc_candidate_get_annotation(kkcCandidate), "]");
            }
            word.strWord = strdup(kkc_candidate_get_text(kkcCandidate));
            if (i == cursor_pos) {
                word.wordType = MSG_CANDIATE_CURSOR;
            } else {
                word.wordType = MSG_OTHER;
            }

            FcitxCandidateWordAppend(candList, &word);
        }
        FcitxCandidateWordSetFocus(candList, cursor_pos - page_start);

        FcitxCandidateWordSetOverridePaging(candList,
                                            (cursor_pos - page_start) >= page_size,
                                            (size - page_start) / page_size != (cursor_pos - page_start) / page_size,
                                            FcitxKkcPaging,
                                            kkc,
                                            NULL);
    }

    if (kkc_context_has_output(kkc->context)) {
        gchar* str = kkc_context_poll_output(kkc->context);
        FcitxInstanceCommitString(kkc->owner, FcitxInstanceGetCurrentIC(kkc->owner), str);
        g_free(str);
    }

    return IRV_DISPLAY_CANDWORDS;
}
Exemplo n.º 5
0
Arquivo: punc.c Projeto: adaptee/fcitx
boolean ProcessPunc(void* arg, FcitxKeySym sym, unsigned int state, INPUT_RETURN_VALUE* retVal)
{
    FcitxPuncState* puncState = (FcitxPuncState*) arg;
    FcitxInstance* instance = puncState->owner;
    FcitxInputState* input = FcitxInstanceGetInputState(puncState->owner);
    FcitxProfile* profile = FcitxInstanceGetProfile(instance);
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(instance);

    char *pPunc = NULL;

    if (*retVal != IRV_TO_PROCESS)
        return false;

    FcitxCandidateWordList* candList = FcitxInputStateGetCandidateList(input);
    if (FcitxCandidateWordPageCount(candList) != 0 && FcitxCandidateWordGetHasGoneToNextPage(candList) ) {
        const FcitxHotkey* hkPrevPage = FcitxInstanceGetContextHotkey(instance, CONTEXT_ALTERNATIVE_PREVPAGE_KEY);
        if (hkPrevPage == NULL)
            hkPrevPage = config->hkPrevPage;

        if (FcitxHotkeyIsHotKey(sym, state, hkPrevPage)) {
            return false;
        }
    }

    /*
     * comparing with upper case, if paging is occupied,
     * punc will not let next page pass
     */
    if (FcitxCandidateWordPageCount(candList) != 0) {
        const FcitxHotkey* hkNextPage = FcitxInstanceGetContextHotkey(instance, CONTEXT_ALTERNATIVE_NEXTPAGE_KEY);
        if (hkNextPage == NULL)
            hkNextPage = config->hkNextPage;

        if (FcitxHotkeyIsHotKey(sym, state, hkNextPage)) {
            return false;
        }
    }

    FcitxKeySym origsym = sym;
    sym = FcitxHotkeyPadToMain(sym);
    if (profile->bUseWidePunc) {

        if (puncState->bLastIsNumber && config->bEngPuncAfterNumber
                && (FcitxHotkeyIsHotKey(origsym, state, FCITX_PERIOD)
                    || FcitxHotkeyIsHotKey(origsym, state, FCITX_SEMICOLON)
                    || FcitxHotkeyIsHotKey(origsym, state, FCITX_COMMA))) {
            puncState->cLastIsAutoConvert = origsym;
            puncState->bLastIsNumber = false;
            *retVal = IRV_DONOT_PROCESS;
            return true;
        }
        if (FcitxHotkeyIsHotKeySimple(sym, state))
            pPunc = GetPunc(puncState, origsym);
    }

    /*
     * 在有候选词未输入的情况下,选择第一个候选词并输入标点
     */
    if (IsHotKeyPunc(sym, state)) {
        FcitxInputStateGetOutputString(input)[0] = '\0';
        INPUT_RETURN_VALUE ret = IRV_TO_PROCESS;
        if (!FcitxInputStateGetIsInRemind(input))
            ret = FcitxCandidateWordChooseByTotalIndex(FcitxInputStateGetCandidateList(input), 0);

        /* if there is nothing to commit */
        if (ret == IRV_TO_PROCESS) {
            if (pPunc) {
                strcat(FcitxInputStateGetOutputString(input), pPunc);
                *retVal = IRV_PUNC;
                FcitxInstanceCleanInputWindow(instance);
                return true;
            } else
                return false;
        } else {
            if (pPunc)
                strcat(FcitxInputStateGetOutputString(input), pPunc);
            else {
                char buf[2] = { sym, 0 };
                strcat(FcitxInputStateGetOutputString(input), buf);
            }

            FcitxInstanceCleanInputWindow(instance);
            *retVal = IRV_PUNC;
            return true;
        }

        return false;
    }

    if (profile->bUseWidePunc) {
        if (FcitxHotkeyIsHotKey(sym, state, FCITX_BACKSPACE)
                && puncState->cLastIsAutoConvert) {
            char *pPunc;

            FcitxInstanceForwardKey(puncState->owner, FcitxInstanceGetCurrentIC(instance), FCITX_PRESS_KEY, sym, state);
            pPunc = GetPunc(puncState, puncState->cLastIsAutoConvert);
            if (pPunc)
                FcitxInstanceCommitString(puncState->owner, FcitxInstanceGetCurrentIC(instance), pPunc);

            puncState->cLastIsAutoConvert = 0;
            *retVal = IRV_DO_NOTHING;
            return true;
        } else if (FcitxHotkeyIsHotKeySimple(sym, state)) {
            if (FcitxHotkeyIsHotKeyDigit(sym, state))
                puncState->bLastIsNumber = true;
            else {
                puncState->bLastIsNumber = false;
            }
        }
    }
    puncState->cLastIsAutoConvert = 0;
    return false;
}