Simplify touchscreen scrollbar handling code
[Rockbox.git] / apps / alarm_menu.c
blob3866a99bc192d9044865a84a48d90cb71cd44aa7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2003 Uwe Freese
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #ifdef HAVE_RTC_ALARM
25 #include <stdbool.h>
27 #include "lcd.h"
28 #include "action.h"
29 #include "kernel.h"
30 #include "sprintf.h"
31 #include <string.h>
32 #include "settings.h"
33 #include "power.h"
34 #include "icons.h"
35 #include "rtc.h"
36 #include "misc.h"
37 #include "screens.h"
38 #include "talk.h"
39 #include "lang.h"
40 #include "power.h"
41 #include "alarm_menu.h"
42 #include "backlight.h"
43 #include "splash.h"
44 #include "statusbar.h"
45 #include "viewport.h"
47 static void speak_time(int hours, int minutes, bool speak_hours)
49 if (global_settings.talk_menu){
50 if(speak_hours) {
51 talk_value(hours, UNIT_HOUR, false);
52 talk_value(minutes, UNIT_MIN, true);
53 } else {
54 talk_value(minutes, UNIT_MIN, false);
59 bool alarm_screen(void)
61 int h, m;
62 bool done=false;
63 char buf[32];
64 struct tm *tm;
65 int togo;
66 int button;
67 int i;
68 bool update = true;
69 bool hour_wrapped = false;
70 struct viewport vp[NB_SCREENS];
72 rtc_get_alarm(&h, &m);
74 /* After a battery change the RTC values are out of range */
75 if (m > 60 || h > 24) {
76 m = 0;
77 h = 12;
78 } else {
79 m = m / 5 * 5; /* 5 min accuracy should be enough */
81 FOR_NB_SCREENS(i)
83 viewport_set_defaults(&vp[i], i);
86 while(!done) {
87 if(update)
89 FOR_NB_SCREENS(i)
91 screens[i].set_viewport(&vp[i]);
92 screens[i].clear_viewport();
93 screens[i].puts(0, 3, str(LANG_ALARM_MOD_KEYS));
95 /* Talk when entering the wakeup screen */
96 if (global_settings.talk_menu)
98 talk_value(h, UNIT_HOUR, true);
99 talk_value(m, UNIT_MIN, true);
101 update = false;
104 snprintf(buf, 32, str(LANG_ALARM_MOD_TIME), h, m);
105 FOR_NB_SCREENS(i)
107 screens[i].set_viewport(&vp[i]);
108 screens[i].puts(0, 1, buf);
109 screens[i].update_viewport();
110 screens[i].set_viewport(NULL);
112 button = get_action(CONTEXT_SETTINGS,HZ);
114 switch(button) {
115 case ACTION_STD_OK:
116 /* prevent that an alarm occurs in the shutdown procedure */
117 /* accept alarms only if they are in 2 minutes or more */
118 tm = get_time();
119 togo = (m + h * 60 - tm->tm_min - tm->tm_hour * 60 + 1440) % 1440;
121 if (togo > 1) {
122 rtc_init();
123 rtc_set_alarm(h,m);
124 rtc_enable_alarm(true);
125 if (global_settings.talk_menu)
127 talk_id(LANG_ALARM_MOD_TIME_TO_GO, true);
128 talk_value(togo / 60, UNIT_HOUR, true);
129 talk_value(togo % 60, UNIT_MIN, true);
130 talk_force_enqueue_next();
132 gui_syncsplash(HZ*2, str(LANG_ALARM_MOD_TIME_TO_GO),
133 togo / 60, togo % 60);
134 done = true;
135 } else {
136 gui_syncsplash(HZ, ID2P(LANG_ALARM_MOD_ERROR));
137 update = true;
139 break;
141 /* inc(m) */
142 case ACTION_SETTINGS_INC:
143 case ACTION_SETTINGS_INCREPEAT:
144 m += 5;
145 if (m == 60) {
146 h += 1;
147 m = 0;
148 hour_wrapped = true;
150 if (h == 24)
151 h = 0;
153 speak_time(h, m, hour_wrapped);
154 break;
156 /* dec(m) */
157 case ACTION_SETTINGS_DEC:
158 case ACTION_SETTINGS_DECREPEAT:
159 m -= 5;
160 if (m == -5) {
161 h -= 1;
162 m = 55;
163 hour_wrapped = true;
165 if (h == -1)
166 h = 23;
168 speak_time(h, m, hour_wrapped);
169 break;
171 /* inc(h) */
172 case ACTION_STD_NEXT:
173 case ACTION_STD_NEXTREPEAT:
174 h = (h+1) % 24;
176 if (global_settings.talk_menu)
177 talk_value(h, UNIT_HOUR, false);
178 break;
180 /* dec(h) */
181 case ACTION_STD_PREV:
182 case ACTION_STD_PREVREPEAT:
183 h = (h+23) % 24;
185 if (global_settings.talk_menu)
186 talk_value(h, UNIT_HOUR, false);
187 break;
189 case ACTION_STD_CANCEL:
190 rtc_enable_alarm(false);
191 gui_syncsplash(HZ*2, ID2P(LANG_ALARM_MOD_DISABLE));
192 done = true;
193 break;
195 case ACTION_NONE:
196 gui_syncstatusbar_draw(&statusbars, false);
197 hour_wrapped = false;
198 break;
200 default:
201 if(default_event_handler(button) == SYS_USB_CONNECTED)
203 rtc_enable_alarm(false);
204 return true;
206 break;
209 return false;
212 #endif /* HAVE_RTC_ALARM */