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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: sleep_timer_menu.c 23867 2009-12-05 11:24:50Z nickp $
 *
 * Copyright (C) 2007 Jonathan Gordon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdbool.h>
#include <stdio.h>
#include "config.h"
#include "string.h"
#include "lang.h"
#include "settings.h"
#include "menu.h"
#include "keyboard.h"
#include "exported_menus.h"
#include "powermgmt.h"
#include "action.h"

static char* get_current_status(int selected_item, void * data, char *buffer)
{
    (void)selected_item;
    (void)data;
    int remain = get_sleep_timer();
    if (remain)
    {
        remain += 59; /* round up minutes */
        snprintf(buffer, MAX_PATH, "%s (%d:%02d)",
            str(LANG_SLEEP_TIMER_STOP),
            remain / 3600,
            ( remain % 3600 ) / 60
        );
    }
    else
    {
        remain = global_settings.sleeptimer_duration;
        snprintf(buffer, MAX_PATH, "%s (%d:%02d)",
            str(LANG_SLEEP_TIMER_START),
            remain / 60,
            remain % 60
        );
    }
    return buffer;
}

static int toggle_sleeptimer(void)
{
    if (get_sleep_timer())
        set_sleep_timer(0);
    else
        set_sleep_timer(global_settings.sleeptimer_duration * 60);
    return 0;
}

static int initial_duration;
static int sleeptimer_duration_cb(int action,const struct menu_item_ex *this_item)
{
    (void)this_item;
    switch (action)
    {
        case ACTION_ENTER_MENUITEM:
            initial_duration = global_settings.sleeptimer_duration;
            break;
        case ACTION_EXIT_MENUITEM:
            if (initial_duration != global_settings.sleeptimer_duration && get_sleep_timer())
                set_sleep_timer(global_settings.sleeptimer_duration * 60);
    }
    return action;
}

MENUITEM_FUNCTION_DYNTEXT(menu_toggle_sleeptimer, 0, toggle_sleeptimer, NULL,
                             get_current_status, NULL, NULL, NULL, Icon_NOICON);

MENUITEM_SETTING(menu_sleeptimer_duration, &global_settings.sleeptimer_duration,
                    sleeptimer_duration_cb);

MENUITEM_SETTING(menu_sleeptimer_on_startup, &global_settings.sleeptimer_on_startup, NULL);

MAKE_MENU(sleeptimer_menu, ID2P(LANG_SLEEP_TIMER), NULL, Icon_NOICON,
            &menu_toggle_sleeptimer, &menu_sleeptimer_duration, &menu_sleeptimer_on_startup);
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: sleep_timer_menu.c 23867 2009-12-05 11:24:50Z nickp $
 *
 * Copyright (C) 2007 Jonathan Gordon
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdbool.h>
#include <stdio.h>
#include "config.h"
#include "string.h"
#include "lang.h"
#include "settings.h"
#include "menu.h"
#include "keyboard.h"
#include "exported_menus.h"
#include "powermgmt.h"
#include "action.h"

static char* get_current_status(int selected_item, void * data, char *buffer)
{
    (void)selected_item;
    (void)data;
    int remain = get_sleep_timer();
    if (remain)
    {
        remain += 59; /* round up minutes */
        snprintf(buffer, MAX_PATH, "%s (%d:%02d)",
            str(LANG_SLEEP_TIMER_STOP),
            remain / 3600,
            ( remain % 3600 ) / 60
        );
    }
    else
    {
        remain = global_settings.sleeptimer_duration;
        snprintf(buffer, MAX_PATH, "%s (%d:%02d)",
            str(LANG_SLEEP_TIMER_START),
            remain / 60,
            remain % 60
        );
    }
    return buffer;
}

static int toggle_sleeptimer(void)
{
    if (get_sleep_timer())
        set_sleep_timer(0);
    else
        set_sleep_timer(global_settings.sleeptimer_duration * 60);
    return 0;
}

static int initial_duration;
static int sleeptimer_duration_cb(int action,const struct menu_item_ex *this_item)
{
    (void)this_item;
    switch (action)
    {
        case ACTION_ENTER_MENUITEM:
            initial_duration = global_settings.sleeptimer_duration;
            break;
        case ACTION_EXIT_MENUITEM:
            if (initial_duration != global_settings.sleeptimer_duration && get_sleep_timer())
                set_sleep_timer(global_settings.sleeptimer_duration * 60);
    }
    return action;
}

MENUITEM_FUNCTION_DYNTEXT(menu_toggle_sleeptimer, 0, toggle_sleeptimer, NULL,
                             get_current_status, NULL, NULL, NULL, Icon_NOICON);

MENUITEM_SETTING(menu_sleeptimer_duration, &global_settings.sleeptimer_duration,
                    sleeptimer_duration_cb);

MENUITEM_SETTING(menu_sleeptimer_on_startup, &global_settings.sleeptimer_on_startup, NULL);
MENUITEM_SETTING(menu_sleeptimer_pause_mode, &global_settings.sleeptimer_pause_mode, NULL);

MAKE_MENU(sleeptimer_menu, ID2P(LANG_SLEEP_TIMER), NULL, Icon_NOICON,
            &menu_toggle_sleeptimer, &menu_sleeptimer_duration, &menu_sleeptimer_on_startup,
            &menu_sleeptimer_pause_mode);