Esempio n. 1
0
FCITX_EXPORT_API
FcitxCandidateWord* FcitxCandidateWordGetCurrentWindowNext(FcitxCandidateWordList* candList, FcitxCandidateWord* candWord)
{
    FcitxCandidateWord* nextCandWord = (FcitxCandidateWord*) utarray_next(&candList->candWords, candWord);
    FcitxCandidateWord* startCandWord = FcitxCandidateWordGetCurrentWindow(candList);
    if (nextCandWord == NULL)
        return NULL;
    int delta = utarray_eltidx(&candList->candWords, nextCandWord) - utarray_eltidx(&candList->candWords, startCandWord);
    if (delta < 0 || delta >= candList->wordPerPage)
        return NULL;
    return nextCandWord;
}
Esempio n. 2
0
FCITX_EXPORT_API void
FcitxCandidateWordRemove(FcitxCandidateWordList *candList,
                         FcitxCandidateWord *candWord)
{
    int idx = utarray_eltidx(&candList->candWords, candWord);
    FcitxCandidateWordRemoveByIndex(candList, idx);
}
Esempio n. 3
0
void drain_clients() {
  int rc, *fd, pos;
  char buf[1024];

  fd=NULL;
  while ( (fd=(int*)utarray_next(cfg.fds,fd))) {
    do {
      rc = read(*fd, buf, sizeof(buf));
      switch(rc) { 
        default: fprintf(stderr,"received %d bytes\n", rc);         break;
        case  0: fprintf(stderr,"fd %d closed\n", *fd);             break;
        case -1: if (errno == EWOULDBLOCK || errno == EAGAIN)       break;
                 fprintf(stderr, "recv: %s\n", strerror(errno));    break;
      }
    } while(rc > 0);

    if (rc==0) {
      fprintf(stderr,"client %d has closed\n", *fd);
      close(*fd);
      *fd = -1; /* mark for cleanup after forward iteration */
    }
  }

  /* cleanup any sockets that we closed, reverse iteration */
  fd=NULL;
  while ( (fd=(int*)utarray_prev(cfg.fds,fd))) {
    pos = utarray_eltidx(cfg.fds,fd);
    if (*fd == -1) utarray_erase(cfg.fds,pos,1);
  }

}
Esempio n. 4
0
int
param_reset(param_t param)
{
	struct param_wbuf_s *s = NULL;
	bool param_found = false;

	param_lock();

	if (handle_in_range(param)) {

		/* look for a saved value */
		s = param_find_changed(param);

		/* if we found one, erase it */
		if (s != NULL) {
			int pos = utarray_eltidx(param_values, s);
			utarray_erase(param_values, pos, 1);
		}

		param_found = true;
	}

	param_unlock();

	if (s != NULL) {
		param_notify_changes();
	}

	return (!param_found);
}
Esempio n. 5
0
FCITX_EXPORT_API
void FcitxCandidateWordRemove(FcitxCandidateWordList* candList, FcitxCandidateWord* candWord)
{
    int idx = utarray_eltidx(&candList->candWords, candWord);
    if (idx < 0 || idx >= utarray_len(&candList->candWords))
        return;
    utarray_erase(&candList->candWords, idx, 1);
}
Esempio n. 6
0
static bool valid_exec(Cmdline *cmdline, Cmdstr *cmd, Token *token)
{
  if (utarray_eltidx(cmdline->tokens, token) != 1)
    return false;

  Token *prev = (Token*)utarray_prev(cmdline->tokens, token);
  cmd->rev = prev->end == token->start;

  return cmd->rev;
}
Esempio n. 7
0
void dicom_import(const char *dirpath)
{
    DIR *dir;
    struct dirent *dirent;
    dicom_t dicom, *dptr;
    static UT_icd dicom_icd = {sizeof(dicom_t), NULL, NULL, NULL};
    UT_array *all_files;
    int w, h, d;    // Dimensions of the full data cube.
    int i;
    uint16_t *data;
    uvec4b_t *cube;

    // First we parse all the dicom images into a sorted array.
    // XXX: how to propery iter a directory?
    utarray_new(all_files, &dicom_icd);
    dir = opendir(dirpath);
    while ((dirent = readdir(dir))) {
        if (dirent->d_name[0] == '.') continue;
        asprintf(&dicom.path, "%s/%s", dirpath, dirent->d_name);
        dicom_load(dicom.path, &dicom, NULL, 0);
        CHECK(dicom.rows && dicom.columns);
        utarray_push_back(all_files, &dicom);
    }
    closedir(dir);
    utarray_sort(all_files, dicom_sort);

    // Read all the file data one by one into the data cube.
    w = dicom.columns;
    h = dicom.rows;
    d = utarray_len(all_files);
    data = calloc(w * h * d, 2);

    dptr = NULL;
    while( (dptr = (dicom_t*)utarray_next(all_files, dptr))) {
        i = utarray_eltidx(all_files, dptr);
        dicom_load(dptr->path, &dicom,
                   (char*)&data[w * h * i], w * h * 2);
        free(dptr->path);
    }
    utarray_free(all_files);

    // Generate 4 * 8bit RGBA values.
    // XXX: we should maybe support voxel data in 2 bytes monochrome.
    cube = malloc(w * h * d * sizeof(*cube));
    for (i = 0; i < w * h * d; i++) {
        cube[i] = uvec4b(255, 255, 255, clamp(data[i], 0, 255));
    }
    free(data);

    // This could belong to the caller function.
    mesh_blit(goxel()->image->active_layer->mesh, cube,
              -w / 2, -h / 2, -d / 2, w, h, d);

    free(cube);
}
Esempio n. 8
0
/**
 * Delete firewall rule.
 * @param[in] fire Firewall handle.
 * @param[in] proto Protocol.
 * @param[in] rule Rule type.
 * @param[in] port Port number (network order).
 */
void zfwall_del_rule(struct zfirewall *fire, enum ipproto proto, enum port_rule rule, uint16_t port)
{
    pthread_spin_lock(&fire->lock);

    uint16_t *ptr = utarray_find(&fire->rules[proto][rule], &port, uint16_cmp);
    if (NULL != ptr) {
        size_t idx = utarray_eltidx(&fire->rules[proto][rule], ptr);
        utarray_erase(&fire->rules[proto][rule], idx, 1);
    }

    pthread_spin_unlock(&fire->lock);
}
Esempio n. 9
0
FCITX_EXPORT_API
boolean FcitxInstanceRemoveTimeoutByFunc(FcitxInstance* instance, FcitxTimeoutCallback callback)
{
    TimeoutItem* ti;
    for (ti = (TimeoutItem*) utarray_front(&instance->timeout);
         ti != NULL;
         ti = (TimeoutItem*) utarray_next(&instance->timeout, ti))
    {
        if (ti->callback == callback) {
            int idx = utarray_eltidx(&instance->timeout, ti);
            utarray_remove_quick(&instance->timeout, idx);
            return true;
        }
    }
    return false;
}
Esempio n. 10
0
FCITX_EXPORT_API
boolean FcitxInstanceRemoveTimeoutById(FcitxInstance* instance, uint64_t id)
{
    if (id == 0)
        return false;
    TimeoutItem* ti;
    for (ti = (TimeoutItem*) utarray_front(&instance->timeout);
         ti != NULL;
         ti = (TimeoutItem*) utarray_next(&instance->timeout, ti)) {
        if (ti->idx == id) {
            unsigned int idx = utarray_eltidx(&instance->timeout, ti);
            utarray_remove_quick(&instance->timeout, idx);
            return true;
        }
    }
    return false;
}
Esempio n. 11
0
/**
 * Deactivate monitor connection.
 * @param[in] conn Monitor connection.
 */
void zmonitor_conn_deactivate(struct zmonitor_conn *conn)
{
    assert(conn->active);

    if (conn->active) {
        pthread_rwlock_wrlock(&conn->mon->lock);

        struct zmonitor_conn **ptr = (struct zmonitor_conn **) utarray_front(&conn->mon->monitors);
        while (ptr) {
            if (*ptr == conn) {
                ssize_t idx = utarray_eltidx(&conn->mon->monitors, ptr);
                utarray_erase(&conn->mon->monitors, idx, 1);
                conn->active = false;
                break;
            }
            ptr = (struct zmonitor_conn **) utarray_next(&conn->mon->monitors, ptr);
        }

        pthread_rwlock_unlock(&conn->mon->lock);
    }
}
Esempio n. 12
0
FCITX_EXPORT_API
void FcitxUIUnRegisterMenu(FcitxInstance* instance, FcitxUIMenu* menu)
{
    UT_array* uimenus = &instance->uimenus;

    if (!menu)
        return;

    utarray_foreach(menup, uimenus, FcitxUIMenu*) {
        if (*menup == menu) {
            utarray_remove_quick(uimenus, utarray_eltidx(uimenus, menup));

            if (UI_FUNC_IS_VALID(UnRegisterMenu))
                instance->ui->ui->UnRegisterMenu(instance->ui->addonInstance, menu);
            if (UI_FUNC_IS_VALID_FALLBACK(UnRegisterMenu))
                instance->uifallback->ui->UnRegisterMenu(instance->uifallback->addonInstance, menu);
            break;
        }
    }

}
Esempio n. 13
0
void scan_spool(int do_unlink) {
  int i;
  glob_t g;
  struct stat sb;
  UT_array *files;
  UT_array *xtras;
  utarray_new(files,&ut_str_icd);
  utarray_new(xtras,&xtra_icd);
  char **f, *file;
  file_xtra x, *xp;

  if (chdir(dir) == -1) exit(-1);
  glob(wild, 0, NULL, &g);
  for(i=0; i < g.gl_pathc; i++) {
    utarray_push_back(files, &g.gl_pathv[i]);
  }
  utarray_sort(files, strsort);

  f=NULL;
  while ( (f=(char**)utarray_next(files,f))) {
    file = *f;
    stat(file,&sb);
    if (do_unlink) unlink(file);
    x.file_idx = utarray_eltidx(files,f);
    x.file_len = sb.st_size;
    utarray_push_back(xtras, &x);
  }
  utarray_sort(xtras, xtrasort);
  xp=NULL;
  while ( (xp=(file_xtra*)utarray_next(xtras,xp))) {
    f = (char**)utarray_eltptr(files,xp->file_idx);
    file = *f;
    printf("file %s, len %d\n", blot(file),xp->file_len);
  }
  globfree(&g);
  utarray_free(files);
  utarray_free(xtras);
}
Esempio n. 14
0
void FcitxInstanceResolveAddonDependencyInternal(FcitxInstance* instance, FcitxAddon* startAddon)
{
    UT_array* addons = &instance->addons;
    boolean remove = true;
    FcitxAddon *addon;
    FcitxAddon *uiaddon = NULL, *uifallbackaddon = NULL;
    boolean reloadIM = true;

    if (!startAddon) {
        startAddon = (FcitxAddon*) utarray_front(addons);
        reloadIM = false;
    }

    /* check "all" */
    if (instance->disableList
        && utarray_len(instance->disableList) == 1
        && fcitx_utils_string_list_contains(instance->disableList, "all"))
    {
        for (addon = startAddon;
            addon != NULL;
            addon = (FcitxAddon *) utarray_next(addons, addon)) {
            addon->bEnabled = false;
        }
    }

    /* override the enable and disable option */
    for (addon = startAddon;
         addon != NULL;
         addon = (FcitxAddon *) utarray_next(addons, addon)) {
        if (instance->enableList && fcitx_utils_string_list_contains(instance->enableList, addon->name))
            addon->bEnabled = true;
        else if (instance->disableList && fcitx_utils_string_list_contains(instance->disableList, addon->name))
            addon->bEnabled = false;
    }

    if (!reloadIM) {
        /* choose ui */
        for (addon = startAddon;
             addon != NULL;
             addon = (FcitxAddon *) utarray_next(addons, addon)) {
            if (addon->category == AC_UI) {
                if (instance->uiname == NULL) {
                    if (addon->bEnabled) {
                        uiaddon = addon;
                        break;
                    }
                } else {
                    if (strcmp(instance->uiname, addon->name) == 0) {
                        addon->bEnabled = true;
                        uiaddon = addon;
                        break;
                    }
                }
            }
        }

        if (uiaddon && uiaddon->uifallback) {
            for (addon = startAddon;
                 addon != NULL;
                 addon = (FcitxAddon *) utarray_next(addons, addon)) {
                if (addon->category == AC_UI && addon->bEnabled && strcmp(uiaddon->uifallback, addon->name) == 0) {
                    FcitxAddon temp;
                    int uiidx = utarray_eltidx(addons, uiaddon);
                    int fallbackidx = utarray_eltidx(addons, addon);
                    if (fallbackidx < uiidx) {
                        temp = *uiaddon;
                        *uiaddon = *addon;
                        *addon = temp;

                        /* they swapped, addon is normal ui, and ui addon is fallback */
                        uifallbackaddon = uiaddon;
                        uiaddon = addon;
                    }
                    else {
                        uifallbackaddon = addon;
                    }
                    break;
                }
            }
        }

        for (addon = startAddon;
                addon != NULL;
                addon = (FcitxAddon *) utarray_next(addons, addon)) {
            if (addon->category == AC_UI && addon != uiaddon && addon != uifallbackaddon) {
                addon->bEnabled = false;
            }
        }
    }

    while (remove) {
        remove = false;
        for (addon = startAddon;
                addon != NULL;
                addon = (FcitxAddon *) utarray_next(addons, addon)) {
            if (!addon->bEnabled)
                continue;
            UT_array* dependlist = fcitx_utils_split_string(addon->depend, ',');
            boolean valid = true;
            char **depend = NULL;
            for (depend = (char **) utarray_front(dependlist);
                    depend != NULL;
                    depend = (char **) utarray_next(dependlist, depend)) {
                if (!FcitxAddonsIsAddonAvailable(addons, *depend)) {
                    valid = false;
                    break;
                }
            }

            utarray_free(dependlist);
            if (!valid) {
                FcitxLog(WARNING, _("Disable addon %s, dependency %s can not be satisfied."), addon->name, addon->depend);
                addon->bEnabled = false;
            }
        }
    }
}
Esempio n. 15
0
static int
param_set_internal(param_t param, const void *val, bool mark_saved)
{
	int result = -1;
	bool params_changed = false;

	param_lock();

	if (param_values == NULL)
		utarray_new(param_values, &param_icd);

	if (param_values == NULL) {
		debug("failed to allocate modified values array");
		goto out;
	}

	if (handle_in_range(param)) {

		struct param_wbuf_s *s = param_find_changed(param);

		if (s == NULL) {

			/* construct a new parameter */
			struct param_wbuf_s buf = {
				.param = param,
				.val.p = NULL,
				.unsaved = false
			};

			/* add it to the array and sort */
			utarray_push_back(param_values, &buf);
			utarray_sort(param_values, param_compare_values);

			/* find it after sorting */
			s = param_find_changed(param);
		}

		/* update the changed value */
		switch (param_type(param)) {
		case PARAM_TYPE_INT32:
			s->val.i = *(int32_t *)val;
			break;

		case PARAM_TYPE_FLOAT:
			s->val.f = *(float *)val;
			break;

		case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX:
			if (s->val.p == NULL) {
				s->val.p = malloc(param_size(param));

				if (s->val.p == NULL) {
					debug("failed to allocate parameter storage");
					goto out;
				}
			}

			memcpy(s->val.p, val, param_size(param));
			break;

		default:
			goto out;
		}

		s->unsaved = !mark_saved;
		params_changed = true;
		result = 0;
	}

out:
	param_unlock();

	/*
	 * If we set something, now that we have unlocked, go ahead and advertise that
	 * a thing has been set.
	 */
	if (params_changed)
		param_notify_changes();

	return result;
}

int
param_set(param_t param, const void *val)
{
	return param_set_internal(param, val, false);
}

void
param_reset(param_t param)
{
	struct param_wbuf_s *s = NULL;

	param_lock();

	if (handle_in_range(param)) {

		/* look for a saved value */
		s = param_find_changed(param);

		/* if we found one, erase it */
		if (s != NULL) {
			int pos = utarray_eltidx(param_values, s);
			utarray_erase(param_values, pos, 1);
		}
	}

	param_unlock();

	if (s != NULL)
		param_notify_changes();
}

void
param_reset_all(void)
{
	param_lock();

	if (param_values != NULL) {
		utarray_free(param_values);
	}

	/* mark as reset / deleted */
	param_values = NULL;

	param_unlock();

	param_notify_changes();
}
Esempio n. 16
0
/* flush as much pending output to the client as it can handle. */
void feed_client(int ready_fd, int events) {
  int *fd=NULL, rc, pos, rv, *p;
  char *buf, tmp[100];
  size_t len;
  UT_string **s=NULL;

  /* find the fd in our list */
  while ( (fd=(int*)utarray_next(cfg.clients,fd))) {
    s=(UT_string**)utarray_next(cfg.outbufs,s); assert(s);
    pos = utarray_eltidx(cfg.clients, fd);
    if (ready_fd == *fd) break;
  }
  assert(fd);

  if (cfg.verbose > 1) {
    fprintf(stderr, "pollout:%c pollin: %c\n", (events & EPOLLOUT)?'1':'0',
                                               (events & EPOLLIN) ?'1':'0');
  }

  /* before we write to the client, drain any input or closure */
  rv = recv(*fd, tmp, sizeof(tmp), MSG_DONTWAIT);
  if (rv == 0) {
    fprintf(stderr,"client closed (eof)\n");
    close(*fd); /* deletes epoll instances on *fd too */
    discard_client_buffers(pos);
    return;
  }

  if ((events & EPOLLOUT) == 0) return;

  /* send the pending buffer to the client */
  p = (int*)utarray_eltptr(cfg.outidxs,pos);
  buf = utstring_body(*s) + *p;
  len = utstring_len(*s)  - *p;
  rc = send(*fd, buf, len, MSG_DONTWAIT);
  if (cfg.verbose) fprintf(stderr,"sent %d/%d bytes\n", rc, (int)len);

  /* test for client closure or error. */
  if (rc < 0) {
    if ((errno == EWOULDBLOCK) || (errno == EAGAIN)) return;
    fprintf(stderr,"client closed (%s)\n", strerror(errno));
    close(*fd); /* deletes all epoll instances on *fd too */
    discard_client_buffers(pos);
    return;
  }

  /* advance output index in the output buffer; we wrote rc bytes */
  if (rc < len) {
    *p += rc;
    cfg.obpp += rc;
  } else {
    *p = 0;
    utstring_clear(*s);     // buffer emptied
    mod_epoll(EPOLLIN,*fd); // remove EPOLLOUT 
  }

#if 1
  shift_buffers();
  int kc;
  while (have_capacity()) {
    kc = kv_spool_read(cfg.sp,cfg.set,0);
    if (kc <  0) goto done; // error
    if (kc == 0) break;     // no data
    cfg.ompp++;
    if (set_to_binary(cfg.set, cfg.s)) goto done;
    append_to_client_buf(cfg.s);
  }
  mark_writable();
 done:
  return;
#endif
}
Esempio n. 17
0
FCITX_EXPORT_API
void FcitxCandidateWordMoveByWord(FcitxCandidateWordList* candList, FcitxCandidateWord* candWord, int to)
{
    int from = utarray_eltidx(&candList->candWords, candWord);
    FcitxCandidateWordMove(candList, from, to);
}
Esempio n. 18
0
INPUT_RETURN_VALUE QuickPhraseGetCandWords(QuickPhraseState* qpstate)
{
    int iInputLen;
    QUICK_PHRASE searchKey, *pKey, *currentQuickPhrase, *lastQuickPhrase;
    FcitxInputState *input = FcitxInstanceGetInputState(qpstate->owner);
    FcitxInstance *instance = qpstate->owner;
    FcitxGlobalConfig* config = FcitxInstanceGetGlobalConfig(instance);
    FcitxInstanceCleanInputWindowDown(qpstate->owner);

    FcitxCandidateWordSetPageSize(FcitxInputStateGetCandidateList(input), config->iMaxCandWord);
    FcitxCandidateWordSetChoose(FcitxInputStateGetCandidateList(input), DIGIT_STR_CHOOSE);

    pKey = &searchKey;

    {
        FcitxModuleFunctionArg arg;
        char *text = FcitxInputStateGetRawInputBuffer(input);
        arg.args[0] = text;
        arg.args[1] = QuickPhraseGetLuaCandWord;
        arg.args[2] = qpstate;
        InvokeFunction(qpstate->owner, FCITX_LUA, CALLCOMMAND, arg);
    }

    if (!qpstate->quickPhrases)
        return IRV_DISPLAY_MESSAGE;

    iInputLen = strlen(FcitxInputStateGetRawInputBuffer(input));
    if (iInputLen > QUICKPHRASE_CODE_LEN)
        return IRV_DISPLAY_MESSAGE;

    strcpy(searchKey.strCode, FcitxInputStateGetRawInputBuffer(input));

    currentQuickPhrase = utarray_custom_bsearch(pKey, qpstate->quickPhrases, false, PhraseCmp);
    qpstate->iFirstQuickPhrase = utarray_eltidx(qpstate->quickPhrases, currentQuickPhrase);
    lastQuickPhrase = utarray_custom_bsearch(pKey, qpstate->quickPhrases, false, PhraseCmpA);
    qpstate->iLastQuickPhrase = utarray_eltidx(qpstate->quickPhrases, lastQuickPhrase);
    if (qpstate->iLastQuickPhrase < 0)
        qpstate->iLastQuickPhrase = utarray_len(qpstate->quickPhrases);
    if (!currentQuickPhrase || strncmp(FcitxInputStateGetRawInputBuffer(input), currentQuickPhrase->strCode, iInputLen)) {
        currentQuickPhrase = NULL;
        return IRV_DISPLAY_MESSAGE;
    }

    for (currentQuickPhrase = (QUICK_PHRASE*) utarray_eltptr(qpstate->quickPhrases,
                              qpstate->iFirstQuickPhrase);
            currentQuickPhrase != NULL;
            currentQuickPhrase = (QUICK_PHRASE*) utarray_next(qpstate->quickPhrases, currentQuickPhrase)) {
        if (!strncmp(FcitxInputStateGetRawInputBuffer(input), currentQuickPhrase->strCode, iInputLen)) {
            QuickPhraseCand* qpcand = fcitx_utils_malloc0(sizeof(QuickPhraseCand));
            qpcand->cand = currentQuickPhrase;
            FcitxCandidateWord candWord;
            candWord.callback = QuickPhraseGetCandWord;
            candWord.owner = qpstate;
            candWord.priv = qpcand;
            candWord.strExtra = strdup(currentQuickPhrase->strCode + iInputLen);
            candWord.strWord = strdup(currentQuickPhrase->strPhrase);
            candWord.wordType = MSG_OTHER;
            candWord.extraType = MSG_CODE;
            FcitxCandidateWordAppend(FcitxInputStateGetCandidateList(input), &candWord);
        }
    }

    return IRV_DISPLAY_MESSAGE;
}