Subversion Repositories freemyipod

Rev

Rev 509 | Go to most recent revision | Details | 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;
35
    return 0;
36
}
37
 
38
static enum chooser_result chooser_action_handler_wheel_handleevent(struct chooser_data* data,
39
                                                                    enum button_event event,
40
                                                                    int which, int value)
41
{
42
    const struct chooser_action_handler_wheel_params* params;
43
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
44
    if (params->eventfilter && params->eventfilter(data, event, which, value))
45
        return CHOOSER_RESULT_OK;
46
    int spi = params->stepsperitem;
47
    switch (event)
48
    {
49
    case BUTTON_PRESS:
50
        if (which < params->buttoncount)
51
            switch (params->buttonmap[which])
52
            {
53
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_PREV:
54
                mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
55
                data->position = MIN(data->info->itemcount * spi,
56
                                     MAX(spi, data->position & ~(spi - 1))) - spi / 2;
57
                data->selected = &data->info->items[data->position / spi];
58
                mutex_unlock(&data->statemutex);
59
                return CHOOSER_RESULT_REDRAW;
60
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_NEXT:
61
                mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
62
                data->position = MIN(data->info->itemcount * spi,
63
                                     MAX(spi, (data->position & ~(spi - 1)) + 2 * spi)) - spi / 2;
64
                data->selected = &data->info->items[data->position / spi];
65
                mutex_unlock(&data->statemutex);
66
                return CHOOSER_RESULT_REDRAW;
67
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_SELECT:
68
                return CHOOSER_RESULT_FINISHED;
69
            case CHOOSER_ACTION_HANDLER_WHEEL_ACTION_CANCEL:
70
                return CHOOSER_RESULT_CANCEL;
71
            }
72
    case WHEEL_MOVED_ACCEL:
73
        mutex_lock(&data->statemutex, TIMEOUT_BLOCK);
74
        data->position = MIN((data->info->itemcount - 1) * spi, MAX(0, data->position + value));
75
        data->selected = &data->info->items[data->position / spi];
76
        mutex_unlock(&data->statemutex);
77
        return CHOOSER_RESULT_REDRAW;
78
    }
79
    return CHOOSER_RESULT_OK;
80
}
81
 
82
static int chooser_action_handler_wheel_stepsperitem(struct chooser_data* data)
83
{
84
    const struct chooser_action_handler_wheel_params* params;
85
    params = (const struct chooser_action_handler_wheel_params*)(data->info->actionhandlerparams);
86
    return params->stepsperitem;
87
}
88
 
89
 
90
const struct chooser_action_handler chooser_action_handler_wheel =
91
{
92
    .version = CHOOSER_ACTION_HANDLER_VERSION,
93
    .init = chooser_action_handler_wheel_init,
94
    .handleevent = chooser_action_handler_wheel_handleevent,
95
    .handletick = NULL,
96
    .stepsperitem = chooser_action_handler_wheel_stepsperitem,
97
    .destroy = NULL
98
};