Bump version numbers for 3.13
[maemo-rb.git] / apps / plugins / clock / clock.c
blobd287c755986caaec696b98174e38c85e7f2203d1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Kévin Ferrare, graphic design 2003 Zakk Roberts
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 "time.h"
24 #include "lib/pluginlib_actions.h"
25 #include "lib/pluginlib_exit.h"
26 #include "lib/xlcd.h"
28 #include "clock.h"
29 #include "clock_counter.h"
30 #include "clock_draw.h"
31 #include "clock_menu.h"
32 #include "clock_settings.h"
36 /* Keymaps */
37 const struct button_mapping* plugin_contexts[]={
38 pla_main_ctx,
39 #ifdef HAVE_REMOTE_LCD
40 pla_remote_ctx,
41 #endif
43 #define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0])
45 #define ACTION_COUNTER_TOGGLE PLA_SELECT
46 #define ACTION_COUNTER_RESET PLA_SELECT_REPEAT
47 #define ACTION_MENU PLA_CANCEL
48 #define ACTION_MODE_NEXT PLA_RIGHT
49 #define ACTION_MODE_NEXT_REPEAT PLA_RIGHT_REPEAT
50 #define ACTION_MODE_PREV PLA_LEFT
51 #define ACTION_MODE_PREV_REPEAT PLA_LEFT_REPEAT
52 #define ACTION_SKIN_NEXT PLA_UP
53 #define ACTION_SKIN_NEXT_REPEAT PLA_UP_REPEAT
54 #define ACTION_SKIN_PREV PLA_DOWN
55 #define ACTION_SKIN_PREV_REPEAT PLA_DOWN_REPEAT
57 /**************************
58 * Cleanup on plugin return
59 *************************/
60 static void cleanup(void)
62 clock_draw_restore_colors();
63 if(clock_settings.general.save_settings == 1)
64 save_settings();
66 /* restore set backlight timeout */
67 rb->backlight_set_timeout(rb->global_settings->backlight_timeout);
70 /* puts the current time into the time struct */
71 static void clock_update_time( struct time* time){
72 struct tm* current_time = rb->get_time();
73 time->hour = current_time->tm_hour;
74 time->minute = current_time->tm_min;
75 time->second = current_time->tm_sec;
77 /*********************
78 * Date info
79 *********************/
80 time->year = current_time->tm_year + 1900;
81 time->day = current_time->tm_mday;
82 time->month = current_time->tm_mon + 1;
86 void format_date(char* buffer, struct time* time, enum date_format format){
87 switch(format){
88 case JAPANESE:
89 rb->snprintf(buffer, 20, "%04d/%02d/%02d",
90 time->year, time->month, time->day);
91 break;
92 case EUROPEAN:
93 rb->snprintf(buffer, 20, "%02d/%02d/%04d",
94 time->day, time->month, time->year);
95 break;
96 case ENGLISH:
97 rb->snprintf(buffer, 20, "%02d/%02d/%04d",
98 time->month, time->day, time->year);
99 break;
100 case NONE:
101 default:
102 break;
106 /**********************************************************************
107 * Plugin starts here
108 **********************************************************************/
109 enum plugin_status plugin_start(const void* parameter){
110 int button;
111 int last_second = -1;
112 bool redraw=true;
113 struct time time;
114 struct counter counter;
115 bool exit_clock = false;
116 (void)parameter;
117 atexit(cleanup);
119 #if LCD_DEPTH > 1
120 rb->lcd_set_backdrop(NULL);
121 #endif
123 load_settings();
125 /* init xlcd functions */
126 counter_init(&counter);
127 clock_draw_set_colors();
129 while(!exit_clock){
130 clock_update_time(&time);
132 if(!clock_settings.general.idle_poweroff)
133 rb->reset_poweroff_timer();
135 /*************************
136 * Scan for button presses
137 ************************/
138 button = pluginlib_getaction(HZ/10, plugin_contexts, PLA_ARRAY_COUNT);
139 redraw=true;/* we'll set it to false afterwards if there was no action */
140 switch (button){
141 case ACTION_COUNTER_TOGGLE: /* start/stop counter */
142 if(clock_settings.general.show_counter)
143 counter_toggle(&counter);
144 break;
146 case ACTION_COUNTER_RESET: /* reset counter */
147 if(clock_settings.general.show_counter)
148 counter_reset(&counter);
149 break;
151 case ACTION_MODE_NEXT_REPEAT:
152 case ACTION_MODE_NEXT:
153 clock_settings.mode++;
154 if(clock_settings.mode >= NB_CLOCK_MODES)
155 clock_settings.mode = 0;
156 break;
158 case ACTION_MODE_PREV_REPEAT:
159 case ACTION_MODE_PREV:
160 clock_settings.mode--;
161 if(clock_settings.mode < 0)
162 clock_settings.mode = NB_CLOCK_MODES-1;
163 break;
164 case ACTION_SKIN_PREV_REPEAT:
165 case ACTION_SKIN_PREV:
166 clock_settings_skin_next(&clock_settings);
167 break;
168 case ACTION_SKIN_NEXT_REPEAT:
169 case ACTION_SKIN_NEXT:
170 clock_settings_skin_previous(&clock_settings);
171 break;
172 case ACTION_MENU:
173 clock_draw_restore_colors();
174 exit_clock=main_menu();
175 break;
176 default:
177 exit_on_usb(button);
178 if(time.second != last_second){
179 last_second=time.second;
180 redraw=true;
181 }else
182 redraw=false;
183 break;
186 if(redraw){
187 clock_draw_set_colors();
188 FOR_NB_SCREENS(i)
189 clock_draw(rb->screens[i], &time, &counter);
190 redraw=false;
194 return PLUGIN_OK;