Subversion Repositories freemyipod

Rev

Rev 509 | Rev 755 | Go to most recent revision | 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
    {
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)) - spi / 2;
73
                data->selected = &data->info->items[data->position / spi];
74
                mutex_unlock(&data->statemutex);
75
                return CHOOSER_RESULT_REDRAW;
76
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_SELECT:
77
                return CHOOSER_RESULT_FINISHED;
78
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_CANCEL:
79
                return CHOOSER_RESULT_CANCEL;
80
            }
81
    case WHEEL_MOVED_ACCEL:
82
        mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
509 theseven 83
        data->position = MIN(data->info->itemcount * spi - 1, MAX(0, data->position + value));
504 theseven 84
        data->selected = &data->info->items[data->position / spi];
85
        mutex_unlock(&data->statemutex);
86
        return CHOOSER_RESULT_REDRAW;
87
    }
88
    return CHOOSER_RESULT_OK;
89
}
90
 
526 theseven 91
static enum chooser_result chooser_action_handler_wheel_handletick(struct chooser_data* data)
92
{
93
    const struct chooser_action_handler_wheel_params* params;
94
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
95
    struct chooser_action_handler_wheel_data* adata;
96
    adata = (struct chooser_action_handler_wheel_data*)(data->actionhandlerdata);
97
    if (adata->timeout_remaining == TIMEOUT_BLOCK) return CHOOSER_RESULT_OK;
98
    long time = USEC_TIMER;
99
    adata->timeout_remaining -= time - adata->lasttick;
100
    adata->lasttick = time;
101
    if (adata->timeout_remaining < 0)
102
    {
103
        if (params->timeout_item == CHOOSER_ACTION_HANDLER_WHEEL_TIMEOUT_ITEM_NULL)
104
            return CHOOSER_RESULT_CANCEL;
105
        else if (params->timeout_item != CHOOSER_ACTION_HANDLER_WHEEL_TIMEOUT_ITEM_KEEP)
106
            data->selected = &data->info->items[params->timeout_item];
107
        return CHOOSER_RESULT_FINISHED;
108
    }
109
    if (params->tick_force_redraw) return CHOOSER_RESULT_REDRAW;
110
    return CHOOSER_RESULT_OK;
111
}
112
 
504 theseven 113
static int chooser_action_handler_wheel_stepsperitem(struct chooser_data* data)
114
{
115
    const struct chooser_action_handler_wheel_params* params;
116
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
117
    return params->stepsperitem;
118
}
119
 
526 theseven 120
static void chooser_ction_handler_wheel_destroy(struct chooser_data* data)
121
{
122
    free(data->actionhandlerdata);
123
}
504 theseven 124
 
526 theseven 125
 
504 theseven 126
const struct chooser_action_handler chooser_action_handler_wheel =
127
{
128
    .version = CHOOSER_ACTION_HANDLER_VERSION,
129
    .init = chooser_action_handler_wheel_init,
130
    .handleevent = chooser_action_handler_wheel_handleevent,
526 theseven 131
    .handletick = chooser_action_handler_wheel_handletick,
504 theseven 132
    .stepsperitem = chooser_action_handler_wheel_stepsperitem,
526 theseven 133
    .destroy = chooser_ction_handler_wheel_destroy
504 theseven 134
};