Subversion Repositories freemyipod

Rev

Rev 820 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
504 theseven 1
//
2
//
3
//    Copyright 2011 TheSeven
4
//
5
//
6
//    This file is part of emCORE.
7
//
8
//    emCORE is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emCORE is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "emcorelib.h"
25
#include "libui.h"
26
#include "chooser.h"
27
#include "chooser_action_handler_wheel.h"
28
 
29
 
30
static int chooser_action_handler_wheel_init(struct chooser_data* data)
31
{
32
    const struct chooser_action_handler_wheel_params* params;
33
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
34
    if (params->version != CHOOSER_ACTION_HANDLER_WHEEL_PARAMS_VERSION) return -1;
526 theseven 35
    data->actionhandlerdata = malloc(sizeof(struct chooser_action_handler_wheel_data));
36
    if (!data->actionhandlerdata) return -2;
37
    struct chooser_action_handler_wheel_data* adata;
38
    adata = (struct chooser_action_handler_wheel_data*)(data->actionhandlerdata);
39
    adata->timeout_remaining = params->timeout_initial;
40
    adata->lasttick = USEC_TIMER;
504 theseven 41
    return 0;
42
}
43
 
44
static enum chooser_result chooser_action_handler_wheel_handleevent(struct chooser_data* data,
45
                                                                    enum button_event event,
46
                                                                    int which, int value)
47
{
48
    const struct chooser_action_handler_wheel_params* params;
49
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
526 theseven 50
    struct chooser_action_handler_wheel_data* adata;
51
    adata = (struct chooser_action_handler_wheel_data*)(data->actionhandlerdata);
504 theseven 52
    if (params->eventfilter && params->eventfilter(data, event, which, value))
53
        return CHOOSER_RESULT_OK;
526 theseven 54
    adata->timeout_remaining = params->timeout_idle;
504 theseven 55
    int spi = params->stepsperitem;
56
    switch (event)
57
    {
820 theseven 58
        case BUTTON_PRESS:
59
            if (which < params->buttoncount)
60
                switch (params->buttonmap[which])
61
                {
62
                    case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_PREV:
63
                        mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
64
                        data->position = MIN(data->info->itemcount * spi,
65
                                             MAX(spi, data->position & ~(spi - 1))) - spi / 2;
66
                        data->selected = &data->info->items[data->position / spi];
67
                        mutex_unlock(&data->statemutex);
68
                        return CHOOSER_RESULT_REDRAW;
69
                    case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_NEXT:
70
                        mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
71
                        data->position = MIN(data->info->itemcount * spi,
72
                                             MAX(spi, (data->position & ~(spi - 1)) + 2 * spi))
73
                                                    - spi / 2;
74
                        data->selected = &data->info->items[data->position / spi];
75
                        mutex_unlock(&data->statemutex);
76
                        return CHOOSER_RESULT_REDRAW;
77
                    case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_SELECT:
78
                        return CHOOSER_RESULT_FINISHED;
79
                    case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_CANCEL:
80
                        return CHOOSER_RESULT_CANCEL;
81
                }
82
        case WHEEL_MOVED_ACCEL:
83
            mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
84
            data->position = MIN(data->info->itemcount * spi - 1, MAX(0, data->position + value));
85
            data->selected = &data->info->items[data->position / spi];
86
            mutex_unlock(&data->statemutex);
87
            return CHOOSER_RESULT_REDRAW;
504 theseven 88
    }
89
    return CHOOSER_RESULT_OK;
90
}
91
 
526 theseven 92
static enum chooser_result chooser_action_handler_wheel_handletick(struct chooser_data* data)
93
{
831 theseven 94
    enum chooser_result rc = CHOOSER_RESULT_OK;
526 theseven 95
    const struct chooser_action_handler_wheel_params* params;
96
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
97
    struct chooser_action_handler_wheel_data* adata;
98
    adata = (struct chooser_action_handler_wheel_data*)(data->actionhandlerdata);
831 theseven 99
    if (params->tick_force_redraw) rc = CHOOSER_RESULT_REDRAW;
100
    if (adata->timeout_remaining == TIMEOUT_BLOCK) return rc;
526 theseven 101
    long time = USEC_TIMER;
102
    adata->timeout_remaining -= time - adata->lasttick;
103
    adata->lasttick = time;
104
    if (adata->timeout_remaining < 0)
105
    {
106
        if (params->timeout_item == CHOOSER_ACTION_HANDLER_WHEEL_TIMEOUT_ITEM_NULL)
107
            return CHOOSER_RESULT_CANCEL;
108
        else if (params->timeout_item != CHOOSER_ACTION_HANDLER_WHEEL_TIMEOUT_ITEM_KEEP)
109
            data->selected = &data->info->items[params->timeout_item];
110
        return CHOOSER_RESULT_FINISHED;
111
    }
831 theseven 112
    return rc;
526 theseven 113
}
114
 
504 theseven 115
static int chooser_action_handler_wheel_stepsperitem(struct chooser_data* data)
116
{
117
    const struct chooser_action_handler_wheel_params* params;
118
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
119
    return params->stepsperitem;
120
}
121
 
755 user890104 122
static void chooser_action_handler_wheel_destroy(struct chooser_data* data)
526 theseven 123
{
124
    free(data->actionhandlerdata);
125
}
504 theseven 126
 
526 theseven 127
 
504 theseven 128
const struct chooser_action_handler chooser_action_handler_wheel =
129
{
130
    .version = CHOOSER_ACTION_HANDLER_VERSION,
131
    .init = chooser_action_handler_wheel_init,
132
    .handleevent = chooser_action_handler_wheel_handleevent,
526 theseven 133
    .handletick = chooser_action_handler_wheel_handletick,
504 theseven 134
    .stepsperitem = chooser_action_handler_wheel_stepsperitem,
755 user890104 135
    .destroy = chooser_action_handler_wheel_destroy
504 theseven 136
};