Пример #1
0
FCITX_EXPORT_API
void FcitxHotkeySetKey(const char *str, FcitxHotkey * hotkey)
{
    char           *p;
    char           *strKey;
    int             i = 0, j = 0, k;

    char* strKeys = fcitx_utils_trim(str);
    p = strKeys;

    for (k = 0; k < 2; k++) {
        FcitxKeySym sym;
        unsigned int state;
        i = 0;

        while (p[i] != ' ' && p[i] != '\0')
            i++;

        strKey = strndup(p, i);

        strKey[i] = '\0';

        if (FcitxHotkeyParseKey(strKey, &sym, &state)) {
            hotkey[j].sym = sym;
            hotkey[j].state = state;
            hotkey[j].desc = fcitx_utils_trim(strKey);
            j ++;
        }

        free(strKey);

        if (p[i] == '\0')
            break;

        p = &p[i + 1];
    }

    for (; j < 2; j++) {
        hotkey[j].sym = 0;
        hotkey[j].state = 0;
        hotkey[j].desc = NULL;
    }

    free(strKeys);
}
Пример #2
0
int main(int argc, char* argv[])
{
    char* localedir = fcitx_utils_get_fcitx_path("localedir");
    setlocale(LC_ALL, "");
    bindtextdomain("fcitx", localedir);
    free(localedir);
    bind_textdomain_codeset("fcitx", "UTF-8");
    textdomain("fcitx");

    FcitxLogSetLevel(FCITX_NONE);

    int c;
    char* addonList = NULL, *sandboxDirectory = NULL;
    char *buf = NULL, *buf1 = NULL;
    FILE* fp = NULL;
    char* enableAddon = NULL;
    char* imname = NULL;
    int ret = 1;
    int fd = -1;
    while ((c = getopt(argc, argv, "d:i:h")) != EOF) {
        switch (c) {
            case 'i':
                imname = strdup(optarg);
                break;
            case 'd':
                sandboxDirectory = strdup(optarg);
                break;
            case 'h':
            default:
                goto option_error_end;
        }
    }

    /* processs [addon list] */
    if (optind >= argc)
        goto option_error_end;
    addonList = strdup(argv[optind]);

    /* script file */
    if (optind + 1 < argc) {
        fp = fopen(argv[optind + 1], "rt");
    }
    else {
        fp = stdin;
    }
    if (!fp) {
        goto option_error_end;
    }

    if (!addonList) {
        goto option_error_end;
    }

    sem_t sem;
    sem_init(&sem, 0, 0);
    asprintf(&enableAddon, "fcitx-simple-module,fcitx-simple-frontend,%s", addonList);

    /* reset optind, since FcitxInstanceCreatePause will use getopt again */
    optind = 1;

    char* args[] = {
        argv[0],
        "-D",
        "--disable",
        "all",
        "--enable",
        enableAddon,
        "--ui",
        "fcitx-simple-ui"
    };

    char temp[] = "/tmp/fcitx_sandbox_XXXXXX";
    if (sandboxDirectory) {
        setenv("XDG_CONFIG_HOME", sandboxDirectory, 1);
    } else {
        /*
         * we make a file on purpose, since XDG_CONFIG_HOME should be a directory
         * hence this will prevent every write operation under XDG_CONFIG_HOME
         */
        fd = mkstemp(temp);
        if (fd == -1) {
            setenv("XDG_CONFIG_HOME", "/", 1);
        }
        else {
            close(fd);
            setenv("XDG_CONFIG_HOME", temp, 1);
        }
    }

    instance = FcitxInstanceCreatePause(&sem, 8, args, -1);
    if (sem_trywait(&sem) == 0)
        goto option_error_end;

    FcitxSimpleInit(instance, TestbedCallback, NULL);
    FcitxInstanceStart(instance);
    size_t len = 0;

    if (imname) {
        FcitxSimpleSetCurrentIM(instance, imname);
    }
    while (getline(&buf, &len, fp) != -1) {
        fcitx_utils_free(buf1);
        buf1 = fcitx_utils_trim(buf);

        FcitxKeySym sym = FcitxKey_None;
        unsigned int state = 0;
        FcitxHotkeyParseKey(buf1, &sym, &state);

        if (FcitxSimpleSendKeyEvent(instance, false, sym, state, 0) == 0) {
            fprintf(stderr, "FORWARD:%s\n", buf1);
        }

        usleep(1000);
    }

    FcitxSimpleEnd(instance);
    FcitxInstanceWaitForEnd(instance);
    ret = 0;
option_error_end:
    if (fd >= 0)
        unlink(temp);

    if (fp)
        fclose(fp);

    if (ret)
        usage();

    fcitx_utils_free(imname);
    fcitx_utils_free(buf);
    fcitx_utils_free(buf1);
    fcitx_utils_free(enableAddon);
    fcitx_utils_free(addonList);
    fcitx_utils_free(sandboxDirectory);
    return ret;
}