Extract config file saving code to a function; Do not write the NUL character to...
[kugel-rb.git] / apps / plugins / alarmclock.c
blob0afd7a1c137a056c82af04aecfb8852ad96b4661
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Clément Pit-Claudel
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 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/pluginlib_actions.h"
27 const struct button_mapping *plugin_contexts[] = { pla_main_ctx };
29 static int current = 0;
30 static bool tomorrow = false;
31 static int alarm[2] = {0, 0}, maxval[2] = {24, 60}, prev_tick = 3600 * 24;
32 static bool quit = false, usb = false, waiting = false, done = false;
34 static inline int get_button(void)
36 return pluginlib_getaction(HZ/2, plugin_contexts,
37 ARRAYLEN(plugin_contexts));
40 static int rem_seconds(void)
42 int seconds = (((alarm[0] - rb->get_time()->tm_hour) * 3600)
43 +((alarm[1] - rb->get_time()->tm_min) * 60)
44 -(rb->get_time()->tm_sec));
46 /* The tomorrow flag means that the alarm should ring on the next day */
47 if (seconds > prev_tick) tomorrow = false;
48 prev_tick = seconds;
50 return seconds + (tomorrow ? 24 * 3600 : 0);
53 static void draw_centered_string(struct screen * display, char * string)
55 int w, h;
56 display->getstringsize(string, &w, &h);
58 if (w > display->lcdwidth || h > display->lcdheight) {
59 rb->splash(0, string);
60 } else {
61 display->putsxy((display->lcdwidth - w) / 2,
62 (display->lcdheight - h) / 2,
63 string);
64 display->update();
68 static void draw(struct screen * display)
70 char info[128];
71 display->clear_display();
73 int secs = rem_seconds();
75 if (waiting)
76 rb->snprintf(info, sizeof(info), "Next alarm in %02dh,"
77 " %02dmn, and %02ds.",
78 secs / 3600, (secs / 60) % 60, secs % 60);
79 else {
80 if (current == 0)
81 rb->snprintf(info, sizeof(info), "Set alarm at [%02d]:%02d.",
82 alarm[0],
83 alarm[1]);
84 else
85 rb->snprintf(info, sizeof(info), "Set alarm at %02d:[%02d].",
86 alarm[0],
87 alarm[1]);
89 draw_centered_string(display, info);
92 static bool can_play(void)
94 int audio_status = rb->audio_status();
95 if ((!audio_status && rb->global_status->resume_index != -1)
96 && (rb->playlist_resume() != -1)) {
97 return true;
99 else if (audio_status & AUDIO_STATUS_PLAY)
100 return true;
102 return false;
105 static void play(void)
107 int audio_status = rb->audio_status();
108 if (!audio_status && rb->global_status->resume_index != -1) {
109 if (rb->playlist_resume() != -1) {
110 rb->playlist_start(rb->global_status->resume_index,
111 rb->global_status->resume_offset);
114 else if (audio_status & AUDIO_STATUS_PLAY)
115 rb->audio_resume();
118 static void pause(void)
120 if (rb->audio_status() & AUDIO_STATUS_PLAY)
121 rb->audio_pause();
124 enum plugin_status plugin_start(const void* parameter)
126 int button;
127 int i;
128 (void)parameter;
130 if (!can_play()) {
131 rb->splash(HZ*2, "No track to resume! "
132 "Play or pause one first.");
133 return PLUGIN_ERROR;
135 pause();
137 while(!quit) {
138 button = get_button();
140 if (button == PLA_EXIT || button == PLA_CANCEL)
141 quit = true;
143 FOR_NB_SCREENS(i) {
144 draw(rb->screens[i]);
146 if (waiting) {
147 if (rem_seconds() <= 0) {
148 quit = done = true;
149 play();
152 else {
153 switch (button) {
154 case PLA_UP:
155 case PLA_UP_REPEAT:
156 #ifdef HAVE_SCROLLWHEEL
157 case PLA_SCROLL_FWD:
158 case PLA_SCROLL_FWD_REPEAT:
159 #endif
160 alarm[current] = (alarm[current] + 1) % maxval[current];
161 break;
162 case PLA_DOWN:
163 case PLA_DOWN_REPEAT:
164 #ifdef HAVE_SCROLLWHEEL
165 case PLA_SCROLL_BACK:
166 case PLA_SCROLL_BACK_REPEAT:
167 #endif
168 alarm[current] = (alarm[current] + maxval[current] - 1)
169 % maxval[current];
170 break;
172 case PLA_LEFT:
173 case PLA_LEFT_REPEAT:
174 case PLA_RIGHT:
175 case PLA_RIGHT_REPEAT:
176 current = (current + 1) % 2;
177 break;
179 case PLA_SELECT:
180 case PLA_SELECT_REPEAT: {
181 if (rem_seconds() < 0)
182 tomorrow = true;
184 waiting = true;
185 break;
188 default:
189 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
190 quit = usb = true;
191 break;
196 return (usb) ? PLUGIN_USB_CONNECTED
197 : (done ? PLUGIN_GOTO_WPS : PLUGIN_OK);