summaryrefslogtreecommitdiffstats
path: root/apps/gui/option_select.c
blob: 4f446fc8de35c74d2f0da6895720e59c22cfc585 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2005 by Kevin Ferrare
 * Copyright (C) 2007 by Jonathan Gordon
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stdlib.h>
#include "config.h"
#include "option_select.h"
#include "sprintf.h"
#include "kernel.h"
#include "lang.h"
#include "talk.h"
#include "settings_list.h"
#include "sound.h"
#include "list.h"
#include "action.h"
#include "statusbar.h"
#include "misc.h"
#include "splash.h"

#if defined (HAVE_SCROLLWHEEL)      || \
    (CONFIG_KEYPAD == IPOD_3G_PAD)  || \
    (CONFIG_KEYPAD == IPOD_4G_PAD)  || \
    (CONFIG_KEYPAD == IPOD_1G2G_PAD) || \
    (CONFIG_KEYPAD == PLAYER_PAD)
/* Define this if your target makes sense to have 
   smaller values at the top of the list increasing down the list */
#define ASCENDING_INT_SETTINGS
#endif

static const char *unit_strings[] = 
{   
    [UNIT_INT] = "",    [UNIT_MS]  = "ms",
    [UNIT_SEC] = "s",   [UNIT_MIN] = "min", 
    [UNIT_HOUR]= "hr",  [UNIT_KHZ] = "KHz", 
    [UNIT_DB]  = "dB",  [UNIT_PERCENT] = "%",
    [UNIT_MAH] = "mAh", [UNIT_PIXEL] = "px",
    [UNIT_PER_SEC] = "per sec",
    [UNIT_HERTZ] = "Hz",
    [UNIT_MB]  = "MB",  [UNIT_KBIT]  = "kb/s",
    [UNIT_PM_TICK] = "units/10ms",
};

static char *option_get_valuestring(struct settings_list *setting, 
                             char *buffer, int buf_len,
                             intptr_t temp_var)
{
    if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
    {
        bool val = (bool)temp_var;
        snprintf(buffer, buf_len, "%s", 
            str(val? setting->bool_setting->lang_yes :
                     setting->bool_setting->lang_no));
    }
#if 0 /* probably dont need this one */
    else if ((setting->flags & F_FILENAME) == F_FILENAME)
    {
        struct filename_setting *info = setting->filename_setting;
        snprintf(buffer, buf_len, "%s%s%s", info->prefix,
                 (char*)temp_var, info->suffix);
    }
#endif
    else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
    {
        struct int_setting *info = setting->int_setting;
        if (info->formatter)
            info->formatter(buffer, buf_len, (int)temp_var,
                            unit_strings[info->unit]);
        else
            snprintf(buffer, buf_len, "%d %s", (int)temp_var,
                     unit_strings[info->unit]?
                             unit_strings[info->unit]:"");
    }
    else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
    {
        char sign = ' ', *unit;
        unit = (char*)sound_unit(setting->sound_setting->setting);
        if (sound_numdecimals(setting->sound_setting->setting))
        {
            int integer, dec;
            int val = sound_val2phys(setting->sound_setting->setting,
                                 (int)temp_var);
            if(val < 0)
            {
                sign = '-';
                val = abs(val);
            }
            integer = val / 10; dec = val % 10;
            snprintf(buffer, buf_len, "%c%d.%d %s", sign, integer, dec, unit);
        }
        else
            snprintf(buffer, buf_len, "%d %s", (int)temp_var, unit);
    }
    else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
    {
        if (setting->flags & F_CHOICETALKS)
        {
            int setting_id;
            struct choice_setting *info = setting->choice_setting;
            if (info->talks[(int)temp_var] < LANG_LAST_INDEX_IN_ARRAY)
            {
                snprintf(buffer, buf_len, "%s", str(info->talks[(int)temp_var]));
            }
            else
            {
                find_setting(setting->setting, &setting_id);
                cfg_int_to_string(setting_id, (int)temp_var, buffer, buf_len);
            }
        }
        else
        {
            int value= (int)temp_var;
            char *val = P2STR(setting->choice_setting->desc[value]);
            snprintf(buffer, buf_len, "%s", val);
        }
    }
    return buffer;
}

static void option_talk(struct settings_list *setting, int temp_var)
{
    if (!talk_menus_enabled())
        return;
    if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
    {
        bool val = temp_var==1?true:false;
        talk_id(val? setting->bool_setting->lang_yes :
                setting->bool_setting->lang_no, false);
    }
#if 0 /* probably dont need this one */
    else if ((setting->flags & F_FILENAME) == F_FILENAME)
    {
    }
#endif
    else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
    {
        struct int_setting *info = setting->int_setting;
        if (info->get_talk_id)
            talk_id(info->get_talk_id((int)temp_var), false);
        else 
            talk_value((int)temp_var, info->unit, false);
    }
    else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
    {
        int talkunit = UNIT_DB;
        const char *unit = sound_unit(setting->sound_setting->setting);
        /* crude reconstruction */
        if (*unit == '%')
            talkunit = UNIT_PERCENT;
        else if (*unit == 'H')
            talkunit = UNIT_HERTZ;
        talk_value((int)temp_var, talkunit, false);
    }
    else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
    {
        int value = (int)temp_var;
        if (setting->flags & F_CHOICETALKS)
        {
            talk_id(setting->choice_setting->talks[value], false);
        }
        else
        {
            talk_id(P2ID(setting->choice_setting->desc[value]), false);
        }
    }
}
#if 0
int option_select_next_val(struct settings_list *setting,
                           intptr_t temp_var)
{
    int val = 0;
    if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
    {
        val = (bool)temp_var ? 0 : 1;
    }
    else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
    {
        struct int_setting *info = setting->int_setting;
        val = (int)temp_var + info->step;
        if (val > info->max)
            val = info->min;
    }
    else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
    {
        int setting_id = setting->sound_setting->setting;
        int steps = sound_steps(setting_id);
        int min = sound_min(setting_id);
        int max = sound_max(setting_id);
        val = (int)temp_var + steps;
        if (val > max)
            val = min;
    }
    else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
    {
        struct choice_setting *info = setting->choice_setting;
        val = (int)temp_var;
        if (val > info->count)
            val = 0;
    }
    return val;
}

int option_select_prev_val(struct settings_list *setting,
                           intptr_t temp_var)
{
    int val = 0;
    if ((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING)
    {
        val = (bool)temp_var ? 0 : 1;
    }
    else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
    {
        struct int_setting *info = setting->int_setting;
        val = (int)temp_var - info->step;
        if (val < info->min)
            val = info->max;
    }
    else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
    {
        int setting_id = setting->sound_setting->setting;
        int steps = sound_steps(setting_id);
        int min = sound_min(setting_id);
        int max = sound_max(setting_id);
        val = (int)temp_var -+ steps;
        if (val < min)
            val = max;
    }
    else if ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING)
    {
        struct choice_setting *info = setting->choice_setting;
        val = (int)temp_var;
        if (val < 0)
            val = info->count - 1;
    }
    return val;
}
#endif

static int selection_to_val(struct settings_list *setting, int selection)
{
    int min = 0, max = 0, step = 1;
    if (((setting->flags & F_BOOL_SETTING) == F_BOOL_SETTING) ||
          ((setting->flags & F_CHOICE_SETTING) == F_CHOICE_SETTING))
        return selection;
    else if ((setting->flags & F_T_SOUND) == F_T_SOUND)
    {
        int setting_id = setting->sound_setting->setting;
#ifndef ASCENDING_INT_SETTINGS
        step = sound_steps(setting_id);
        max = sound_max(setting_id);
        min = sound_min(setting_id);
#else
        step = -sound_steps(setting_id);
        min = sound_max(setting_id);
        max = sound_min(setting_id);
#endif
    }
    else if ((setting->flags & F_INT_SETTING) == F_INT_SETTING)
    {
        struct int_setting *info = setting->int_setting;
#ifndef ASCENDING_INT_SETTINGS
        min = info->min;
        max = info->max;
        step = info->step;
#else
        max = info->min;
        min = info->max;
        step = -info->step;
#endif
    }
    return max- (selection * step);
}
static char * value_setting_get_name_cb(int selected_item, 
                                        void * data, char *buffer)
{
    selected_item = selection_to_val(data, selected_item);
    return option_get_valuestring(data, buffer, MAX_PATH, selected_item);
}

/* wrapper to convert from int param to bool param in option_screen */
static void (*boolfunction)(bool);
static void bool_funcwrapper(int value)
{
    if (value)
        boolfunction(true);
    else
        boolfunction(false);
}

bool option_screen(struct settings_list *setting,
                   bool use_temp_var, unsigned char* option_title)
{
    int action;
    bool done = false;
    struct gui_synclist lists;
    int oldvalue, nb_items = 0, selected = 0, temp_var;
    int *variable;
    bool allow_wrap = setting->flags & F_NO_WRAP ? false : true;
    int var_type = setting->flags&F_T_MASK;
    void (*function)(int) = NULL;
    char *title;
    if (var_type == F_T_INT || var_type == F_T_UINT)
    {
        variable = use_temp_var ? &temp_var: (int*)setting->setting;
        temp_var = oldvalue = *(int*)setting->setting;
    }
    else if (var_type == F_T_BOOL)
    {
        /* bools always use the temp variable...
        if use_temp_var is false it will be copied to setting->setting every change */
        variable = &temp_var;
        temp_var = oldvalue = *(bool*)setting->setting?1:0;
    }
    else return false; /* only int/bools can go here */
    gui_synclist_init(&lists, value_setting_get_name_cb, 
                      (void*)setting, false, 1);
    if (setting->lang_id == -1)
        title = (char*)setting->cfg_vals;
    else
        title = P2STR(option_title);
    
    gui_synclist_set_title(&lists, title, Icon_Questionmark);
    gui_synclist_set_icon_callback(&lists, NULL);
    
    /* set the number of items and current selection */
    if (var_type == F_T_INT || var_type == F_T_UINT)
    {
        if (setting->flags&F_CHOICE_SETTING)
        {
            nb_items = setting->choice_setting->count;
            selected = oldvalue;
            function = setting->choice_setting->option_callback;
        }
        else if (setting->flags&F_T_SOUND)
        {
            int setting_id = setting->sound_setting->setting;
            int steps = sound_steps(setting_id);
            int min = sound_min(setting_id);
            int max = sound_max(setting_id);
            nb_items = (max-min)/steps + 1;
#ifndef ASCENDING_INT_SETTINGS
            selected = (max - oldvalue) / steps;
#else
            selected = (oldvalue - min) / steps;
#endif
            function = sound_get_fn(setting_id);
        }
        else
        {
            struct int_setting *info = setting->int_setting;
            int min, max, step;
            max = info->max;
            min = info->min;
            step = info->step;
            nb_items = (max-min)/step + 1;
#ifndef ASCENDING_INT_SETTINGS
            selected = (max - oldvalue) / step;
#else
            selected = (oldvalue - min) / step;
#endif
            function = info->option_callback;
        }
    }
    else if (var_type == F_T_BOOL)
    {
        selected = oldvalue;
        nb_items = 2;
        boolfunction = setting->bool_setting->option_callback;
        if (boolfunction)
            function = bool_funcwrapper;
    }
    
    gui_synclist_set_nb_items(&lists, nb_items);
    gui_synclist_select_item(&lists, selected);
    
    gui_synclist_limit_scroll(&lists, true);
    gui_synclist_draw(&lists);
    /* talk the item */
    option_talk(setting, *variable);
    while (!done)
    {
        action = get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
        if (action == ACTION_NONE)
            continue;
        if (gui_synclist_do_button(&lists, &action,
            allow_wrap? LIST_WRAP_UNLESS_HELD: LIST_WRAP_OFF))
        {
            selected = gui_synclist_get_sel_pos(&lists);
            *variable = selection_to_val(setting, selected);
            if (var_type == F_T_BOOL)
            {
                if (!use_temp_var)
                    *(bool*)setting->setting = selected==1?true:false;
            }
            /* talk */
            option_talk(setting, *variable);
        }
        else if (action == ACTION_STD_CANCEL)
        {
            bool show_cancel = false;
            if (use_temp_var)
                show_cancel = true;
            else if (var_type == F_T_INT || var_type == F_T_UINT)
            {
                if (*variable != oldvalue)
                {
                    show_cancel = true;
                    *variable = oldvalue;
                }
            }
            else
            {
                if (*variable != oldvalue)
                {
                    show_cancel = true;
                    if (!use_temp_var)
                        *(bool*)setting->setting = oldvalue==1?true:false;
                    *variable = oldvalue;
                }
            }
            if (show_cancel)
                gui_syncsplash(HZ/2, str(LANG_CANCEL));
            done = true;
        }
        else if (action == ACTION_STD_OK)
        {
            done = true;
        }
        else if(default_event_handler(action) == SYS_USB_CONNECTED)
            return true;
        gui_syncstatusbar_draw(&statusbars, false);
        /* callback */
        if ( function )
            function(*variable);
    }
    
    if (oldvalue != *variable && (action != ACTION_STD_CANCEL))
    {
        if (use_temp_var)
        {
            if (var_type == F_T_INT || var_type == F_T_UINT)
                *(int*)setting->setting = *variable;
            else 
                *(bool*)setting->setting = *variable?true:false;
        }
        settings_save();
    }
    
    return false;
}

/******************************************************
    Compatability functions 
*******************************************************/
#define MAX_OPTIONS 32
const struct opt_items *set_option_options;
void set_option_formatter(char* buf, size_t size, int item, const char* unit)
{
    (void)unit;
    const unsigned char *text = set_option_options[item].string;
    snprintf(buf, size, "%s", P2STR(text));
}
long set_option_get_talk_id(int value)
{
    return set_option_options[value].voice_id;
}
bool set_option(const char* string, void* variable, enum optiontype type,
                const struct opt_items* options, 
                int numoptions, void (*function)(int))
{
    int temp;
    struct settings_list item;
    struct int_setting data = {
        function, UNIT_INT, 0, numoptions-1, 1,
        set_option_formatter, set_option_get_talk_id
    };
    set_option_options = options;
    item.int_setting = &data;
    item.flags = F_INT_SETTING|F_T_INT;
    item.lang_id = -1;
    item.cfg_vals = (char*)string;
    item.setting = &temp;
    if (type == BOOL)
        temp = *(bool*)variable? 1: 0;
    else 
        temp = *(int*)variable;
    option_screen(&item, false, NULL);
    if (type == BOOL)
        *(bool*)variable = (temp == 1? true: false);
    else
        *(int*)variable = temp;
    return false;
}

bool set_int_ex(const unsigned char* string,
                const char* unit,
                int voice_unit,
                int* variable,
                void (*function)(int),
                int step,
                int min,
                int max,
                void (*formatter)(char*, size_t, int, const char*),
                long (*get_talk_id)(int))
{
    (void)unit;
    struct settings_list item;
    struct int_setting data = {
        function, voice_unit, min, max, step, 
        formatter, get_talk_id
    };
    item.int_setting = &data;
    item.flags = F_INT_SETTING|F_T_INT;
    item.lang_id = -1;
    item.cfg_vals = (char*)string;
    item.setting = variable;
    return option_screen(&item, false, NULL);
}

/* to be replaced */
void option_select_init_items(struct option_select * opt,
                              const char * title,
                              int selected,
                              const struct opt_items * items,
                              int nb_items)
{
    opt->title=title;
    opt->min_value=0;
    opt->max_value=nb_items;
    opt->option=selected;
    opt->items=items;
}

void option_select_next(struct option_select * opt)
{
    if(opt->option + 1 >= opt->max_value)
    {
            if(opt->option==opt->max_value-1)
                opt->option=opt->min_value;
            else
                opt->option=opt->max_value-1;
    }
    else
        opt->option+=1;
}

void option_select_prev(struct option_select * opt)
{
    if(opt->option - 1 < opt->min_value)
    {
        /* the dissimilarity to option_select_next() arises from the 
         * sleep timer problem (bug #5000 and #5001): 
         * there we have min=0, step = 5 but the value itself might
         * not be a multiple of 5 -- as time elapsed;
         * We need to be able to set timer to 0 (= Off) nevertheless. */
        if(opt->option!=opt->min_value)
            opt->option=opt->min_value;
        else
            opt->option=opt->max_value-1;
    }
    else
        opt->option-=1;
}

const char * option_select_get_text(struct option_select * opt)
{
        return(P2STR(opt->items[opt->option].string));
}