checkwps: Do not error out on rwps for non-remote targets.
[maemo-rb.git] / apps / plugins / alarmclock.c
blob604c9ceac4630632da6495536ea9d277cb7f9b0e
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 (void)parameter;
129 if (!can_play()) {
130 rb->splash(HZ*2, "No track to resume! "
131 "Play or pause one first.");
132 return PLUGIN_ERROR;
134 pause();
136 while(!quit) {
137 button = get_button();
139 if (button == PLA_EXIT || button == PLA_CANCEL)
140 quit = true;
142 FOR_NB_SCREENS(i) {
143 draw(rb->screens[i]);
145 if (waiting) {
146 if (rem_seconds() <= 0) {
147 quit = done = true;
148 play();
151 else {
152 switch (button) {
153 case PLA_UP:
154 case PLA_UP_REPEAT:
155 #ifdef HAVE_SCROLLWHEEL
156 case PLA_SCROLL_FWD:
157 case PLA_SCROLL_FWD_REPEAT:
158 #endif
159 alarm[current] = (alarm[current] + 1) % maxval[current];
160 break;
161 case PLA_DOWN:
162 case PLA_DOWN_REPEAT:
163 #ifdef HAVE_SCROLLWHEEL
164 case PLA_SCROLL_BACK:
165 case PLA_SCROLL_BACK_REPEAT:
166 #endif
167 alarm[current] = (alarm[current] + maxval[current] - 1)
168 % maxval[current];
169 break;
171 case PLA_LEFT:
172 case PLA_LEFT_REPEAT:
173 case PLA_RIGHT:
174 case PLA_RIGHT_REPEAT:
175 current = (current + 1) % 2;
176 break;
178 case PLA_SELECT:
179 case PLA_SELECT_REPEAT: {
180 if (rem_seconds() < 0)
181 tomorrow = true;
183 waiting = true;
184 break;
187 default:
188 if (rb->default_event_handler(button) == SYS_USB_CONNECTED)
189 quit = usb = true;
190 break;
195 return (usb) ? PLUGIN_USB_CONNECTED
196 : (done ? PLUGIN_GOTO_WPS : PLUGIN_OK);