Esempio n. 1
0
struct result
selection_add_item(struct selection *selection,
                   char const *description,
                   selection_action_fn *action)
{
    if (!selection) return result_set_system_error(EINVAL);
    if (!description) return result_set_system_error(EINVAL);
    if (!description[0]) return result_set_system_error(EINVAL);
        
    int index = selection->items_count;
    int null_index = index + 1;
    ++selection->items_count;
    selection->items = reallocarray_or_die(selection->items,
                                           selection->items_count + 1,
                                           sizeof(ITEM *));
    
    char name[] = "1";
    name[0] += index;
    char *name_dup = strdup_or_die(name);
    char *description_dup = strdup_or_die(description);
    selection->items[index] = new_item(name_dup, description_dup);
    if (!selection->items[index]) {
        free_or_die(description_dup);
        free_or_die(name_dup);
        return result_system_error();
    }
    
    struct selection_item *selection_item = calloc_or_die(1, sizeof(struct selection_item));
    selection_item->action = action;
    set_item_userptr(selection->items[index], selection_item);
    
    selection->items[null_index] = NULL;
    
    return result_success();
}
Esempio n. 2
0
struct result
app_run(struct app *app)
{
    sig_t previous_sigwinch_handler = signal(SIGWINCH, terminal_window_did_change);
    if (SIG_ERR == previous_sigwinch_handler) {
        return result_system_error();
    }
    
    struct result result = result_success();
    
    for (int i = 0; i < app->views_count; ++i) {
        result = app->views[i]->create(app->views[i], app);
        if (!result_is_success(result)) break;
    }
    
    if (result_is_success(result)) {
        for (int i = 0; i < app->views_count; ++i) {
            result = app->views[i]->draw(app->views[i], app);
            if (!result_is_success(result)) break;
            wnoutrefresh(app->views[i]->window);
        }
    }
    
    if (!app->active_view) {
        int i = app->views_count - 1;
        result = app_activate_view(app, app->views[i]);
    }
    
    doupdate();
    
    if (result_is_success(result)) {
        result = dispatch_events(app);
    }
    
    for (int i = app->views_count - 1; i >= 0; --i) {
        app->views[i]->destroy(app->views[i], app);
    }
    
    signal(SIGWINCH, previous_sigwinch_handler);
    return result;
}