Fuze+/calendar's plugin update keymaps and manual
[maemo-rb.git] / apps / plugins / calendar.c
blob7dcb8ee34e8f2fcd33fba8891081e364bc3c4e11
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 * (based upon 1.1 by calpefrosch) updated by www.HuwSy.ukhackers.net
11 * Copyright (C) 2002
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #include "plugin.h"
24 #include <timefuncs.h>
25 #include "lib/playback_control.h"
26 #include "lib/configfile.h"
30 #if CONFIG_KEYPAD == RECORDER_PAD
31 #define CALENDAR_QUIT BUTTON_OFF
32 #define CALENDAR_SELECT BUTTON_PLAY
33 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
34 #define CALENDAR_PREV_WEEK BUTTON_UP
35 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
36 #define CALENDAR_PREV_DAY BUTTON_LEFT
37 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
38 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
40 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
41 #define CALENDAR_QUIT BUTTON_OFF
42 #define CALENDAR_SELECT BUTTON_SELECT
43 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
44 #define CALENDAR_PREV_WEEK BUTTON_UP
45 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
46 #define CALENDAR_PREV_DAY BUTTON_LEFT
47 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
48 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
50 #elif CONFIG_KEYPAD == ONDIO_PAD
51 #define CALENDAR_QUIT BUTTON_OFF
52 #define CALENDAR_SELECT (BUTTON_MENU|BUTTON_REL)
53 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
54 #define CALENDAR_PREV_WEEK BUTTON_UP
55 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
56 #define CALENDAR_PREV_DAY BUTTON_LEFT
57 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
58 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
60 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
61 (CONFIG_KEYPAD == IRIVER_H300_PAD)
62 #define CALENDAR_QUIT BUTTON_OFF
63 #define CALENDAR_SELECT BUTTON_SELECT
64 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
65 #define CALENDAR_PREV_WEEK BUTTON_UP
66 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
67 #define CALENDAR_PREV_DAY BUTTON_LEFT
68 #define CALENDAR_NEXT_MONTH BUTTON_MODE
69 #define CALENDAR_PREV_MONTH BUTTON_REC
71 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
72 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
73 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
74 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
75 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_REL)
76 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
77 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
78 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
79 #define CALENDAR_PREV_DAY BUTTON_LEFT
80 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
81 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_REL)
83 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
84 #define CALENDAR_QUIT BUTTON_POWER
85 #define CALENDAR_SELECT BUTTON_SELECT
86 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
87 #define CALENDAR_PREV_WEEK BUTTON_UP
88 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
89 #define CALENDAR_PREV_DAY BUTTON_LEFT
90 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
91 #define CALENDAR_PREV_MONTH BUTTON_REC
93 #elif CONFIG_KEYPAD == GIGABEAT_PAD
94 #define CALENDAR_QUIT BUTTON_POWER
95 #define CALENDAR_SELECT BUTTON_SELECT
96 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
97 #define CALENDAR_PREV_WEEK BUTTON_UP
98 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
99 #define CALENDAR_PREV_DAY BUTTON_LEFT
100 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
101 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
103 #elif CONFIG_KEYPAD == IRIVER_IFP7XX_PAD
104 #define CALENDAR_QUIT BUTTON_PLAY
105 #define CALENDAR_SELECT BUTTON_SELECT
106 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
107 #define CALENDAR_PREV_WEEK BUTTON_UP
108 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
109 #define CALENDAR_PREV_DAY BUTTON_LEFT
110 #define CALENDAR_NEXT_MONTH BUTTON_MODE
111 #define CALENDAR_PREV_MONTH BUTTON_EQ
113 #elif CONFIG_KEYPAD == SANSA_E200_PAD
114 #define CALENDAR_QUIT BUTTON_POWER
115 #define CALENDAR_SELECT BUTTON_SELECT
116 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
117 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
118 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
119 #define CALENDAR_PREV_DAY BUTTON_LEFT
120 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
121 #define CALENDAR_PREV_MONTH BUTTON_UP
123 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
124 #define CALENDAR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
125 #define CALENDAR_SELECT BUTTON_SELECT
126 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
127 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
128 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
129 #define CALENDAR_PREV_DAY BUTTON_LEFT
130 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
131 #define CALENDAR_PREV_MONTH BUTTON_UP
133 #elif (CONFIG_KEYPAD == SANSA_C200_PAD) || \
134 (CONFIG_KEYPAD == SANSA_CLIP_PAD) || \
135 (CONFIG_KEYPAD == SANSA_M200_PAD)
136 #define CALENDAR_QUIT BUTTON_POWER
137 #define CALENDAR_SELECT BUTTON_SELECT
138 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
139 #define CALENDAR_PREV_WEEK BUTTON_UP
140 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
141 #define CALENDAR_PREV_DAY BUTTON_LEFT
142 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
143 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
145 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
146 #define CALENDAR_QUIT BUTTON_POWER
147 #define CALENDAR_SELECT BUTTON_PLAY
148 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
149 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
150 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
151 #define CALENDAR_PREV_DAY BUTTON_LEFT
152 #define CALENDAR_NEXT_MONTH BUTTON_FF
153 #define CALENDAR_PREV_MONTH BUTTON_REW
155 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
156 #define CALENDAR_QUIT BUTTON_BACK
157 #define CALENDAR_SELECT BUTTON_SELECT
158 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
159 #define CALENDAR_PREV_WEEK BUTTON_UP
160 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
161 #define CALENDAR_PREV_DAY BUTTON_LEFT
162 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
163 #define CALENDAR_PREV_MONTH BUTTON_PREV
165 #elif CONFIG_KEYPAD == MROBE100_PAD
166 #define CALENDAR_QUIT BUTTON_POWER
167 #define CALENDAR_SELECT BUTTON_SELECT
168 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
169 #define CALENDAR_PREV_WEEK BUTTON_UP
170 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
171 #define CALENDAR_PREV_DAY BUTTON_LEFT
172 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
173 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
175 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
176 #define CALENDAR_QUIT BUTTON_RC_REC
177 #define CALENDAR_SELECT BUTTON_RC_PLAY
178 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
179 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
180 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
181 #define CALENDAR_PREV_DAY BUTTON_RC_REW
182 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
183 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
185 #elif (CONFIG_KEYPAD == COWON_D2_PAD)
186 #define CALENDAR_QUIT BUTTON_POWER
187 #define CALENDAR_SELECT BUTTON_CENTER
188 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
189 #define CALENDAR_PREV_WEEK BUTTON_UP
190 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
191 #define CALENDAR_PREV_DAY BUTTON_LEFT
192 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
193 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
195 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
196 #define CALENDAR_QUIT BUTTON_BACK
197 #define CALENDAR_SELECT BUTTON_SELECT
198 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
199 #define CALENDAR_PREV_WEEK BUTTON_UP
200 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
201 #define CALENDAR_PREV_DAY BUTTON_LEFT
202 #define CALENDAR_NEXT_MONTH BUTTON_CUSTOM
203 #define CALENDAR_PREV_MONTH BUTTON_PLAY
205 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
206 #define CALENDAR_QUIT BUTTON_POWER
207 #define CALENDAR_SELECT BUTTON_SELECT
208 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
209 #define CALENDAR_PREV_WEEK BUTTON_UP
210 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
211 #define CALENDAR_PREV_DAY BUTTON_LEFT
212 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
213 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
215 #elif CONFIG_KEYPAD == PHILIPS_HDD6330_PAD
216 #define CALENDAR_QUIT BUTTON_POWER
217 #define CALENDAR_SELECT BUTTON_PLAY
218 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
219 #define CALENDAR_PREV_WEEK BUTTON_UP
220 #define CALENDAR_NEXT_DAY BUTTON_NEXT
221 #define CALENDAR_PREV_DAY BUTTON_PREV
222 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
223 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
225 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
226 #define CALENDAR_QUIT BUTTON_POWER
227 #define CALENDAR_SELECT BUTTON_PLAY
228 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
229 #define CALENDAR_PREV_WEEK BUTTON_UP
230 #define CALENDAR_NEXT_DAY BUTTON_NEXT
231 #define CALENDAR_PREV_DAY BUTTON_PREV
232 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
233 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
235 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
236 #define CALENDAR_QUIT BUTTON_POWER
237 #define CALENDAR_SELECT BUTTON_MENU
238 #define CALENDAR_NEXT_WEEK BUTTON_VOL_DOWN
239 #define CALENDAR_PREV_WEEK BUTTON_VOL_UP
240 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
241 #define CALENDAR_PREV_DAY BUTTON_LEFT
242 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
243 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
245 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
246 #define CALENDAR_QUIT BUTTON_POWER
248 #elif CONFIG_KEYPAD == MROBE500_PAD
249 #define CALENDAR_QUIT BUTTON_POWER
251 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
252 #define CALENDAR_QUIT BUTTON_REC
253 #define CALENDAR_SELECT BUTTON_PLAY
254 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
255 #define CALENDAR_PREV_WEEK BUTTON_UP
256 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
257 #define CALENDAR_PREV_DAY BUTTON_LEFT
258 #define CALENDAR_NEXT_MONTH BUTTON_FFWD
259 #define CALENDAR_PREV_MONTH BUTTON_REW
261 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
262 #define CALENDAR_QUIT BUTTON_REC
263 #define CALENDAR_SELECT BUTTON_OK
264 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
265 #define CALENDAR_PREV_WEEK BUTTON_UP
266 #define CALENDAR_NEXT_DAY BUTTON_NEXT
267 #define CALENDAR_PREV_DAY BUTTON_PREV
268 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
269 #define CALENDAR_PREV_MONTH BUTTON_MENU
271 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
272 #define CALENDAR_QUIT (BUTTON_REC|BUTTON_PLAY)
273 #define CALENDAR_SELECT BUTTON_FUNC
274 #define CALENDAR_NEXT_WEEK BUTTON_FF
275 #define CALENDAR_PREV_WEEK BUTTON_REW
276 #define CALENDAR_NEXT_DAY BUTTON_VOL_UP
277 #define CALENDAR_PREV_DAY BUTTON_VOL_DOWN
278 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
279 #define CALENDAR_PREV_MONTH BUTTON_REC
281 #elif CONFIG_KEYPAD == MPIO_HD300_PAD
282 #define CALENDAR_QUIT (BUTTON_MENU|BUTTON_REPEAT)
283 #define CALENDAR_SELECT BUTTON_ENTER
284 #define CALENDAR_NEXT_WEEK BUTTON_FF
285 #define CALENDAR_PREV_WEEK BUTTON_REW
286 #define CALENDAR_NEXT_DAY BUTTON_UP
287 #define CALENDAR_PREV_DAY BUTTON_DOWN
288 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
289 #define CALENDAR_PREV_MONTH BUTTON_REC
291 #elif CONFIG_KEYPAD == SANSA_FUZEPLUS_PAD
292 #define CALENDAR_QUIT BUTTON_POWER
293 #define CALENDAR_SELECT BUTTON_SELECT
294 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
295 #define CALENDAR_PREV_WEEK BUTTON_UP
296 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
297 #define CALENDAR_PREV_DAY BUTTON_LEFT
298 #define CALENDAR_NEXT_MONTH BUTTON_PLAYPAUSE
299 #define CALENDAR_PREV_MONTH BUTTON_BACK
301 #elif CONFIG_KEYPAD == SANSA_CONNECT_PAD
302 #define CALENDAR_QUIT BUTTON_POWER
303 #define CALENDAR_SELECT BUTTON_SELECT
304 #define CALENDAR_NEXT_WEEK BUTTON_RIGHT
305 #define CALENDAR_PREV_WEEK BUTTON_LEFT
306 #define CALENDAR_NEXT_DAY BUTTON_UP
307 #define CALENDAR_PREV_DAY BUTTON_DOWN
308 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
309 #define CALENDAR_PREV_MONTH BUTTON_PREV
311 #elif CONFIG_KEYPAD == SAMSUNG_YPR0_PAD
312 #define CALENDAR_QUIT BUTTON_BACK
313 #define CALENDAR_SELECT BUTTON_SELECT
314 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
315 #define CALENDAR_PREV_WEEK BUTTON_UP
316 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
317 #define CALENDAR_PREV_DAY BUTTON_LEFT
318 #define CALENDAR_NEXT_MONTH BUTTON_POWER
319 #define CALENDAR_PREV_MONTH BUTTON_USER
321 #else
322 #error "No keypad setting."
323 #endif
325 #ifdef HAVE_TOUCHSCREEN
326 #ifndef CALENDAR_QUIT
327 #define CALENDAR_QUIT BUTTON_MIDLEFT
328 #endif
329 #ifndef CALENDAR_SELECT
330 #define CALENDAR_SELECT BUTTON_CENTER
331 #endif
332 #ifndef CALENDAR_NEXT_DAY
333 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
334 #endif
335 #ifndef CALENDAR_PREV_DAY
336 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
337 #endif
338 #ifndef CALENDAR_NEXT_WEEK
339 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
340 #endif
341 #ifndef CALENDAR_PREV_WEEK
342 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
343 #endif
344 #ifndef CALENDAR_NEXT_MONTH
345 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
346 #endif
347 #ifndef CALENDAR_PREV_MONTH
348 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
349 #endif
350 #endif
352 #define MEMO_FILE PLUGIN_APPS_DATA_DIR "/.memo"
353 #define TEMP_FILE PLUGIN_APPS_DATA_DIR "/~temp"
355 #define X_OFFSET ((LCD_WIDTH%7)/2)
356 #if LCD_HEIGHT <= 80
357 #define Y_OFFSET 1
358 #else
359 #define Y_OFFSET 4
360 #endif
361 #define CELL_WIDTH (LCD_WIDTH / 7)
362 #define CELL_HEIGHT (LCD_HEIGHT / 7)
364 #define CFG_FILE "calendar.cfg"
365 struct info {
366 int first_wday;
367 #if (CONFIG_RTC == 0)
368 int last_mon;
369 int last_year;
370 #endif
372 static struct info info = { .first_wday = 0 }, old_info;
373 static struct configdata config[] = {
374 { TYPE_INT, 0, 6, { .int_p = &(info.first_wday) }, "first wday", NULL },
375 #if (CONFIG_RTC == 0)
376 { TYPE_INT, 1, 12, { .int_p = &(info.last_mon) }, "last mon", NULL },
377 { TYPE_INT, 1, 3000, { .int_p = &(info.last_year) }, "last year", NULL },
378 #endif
381 static bool leap_year;
382 /* days_in_month[][0] is for December */
383 static const int days_in_month[2][13] = {
384 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
385 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
388 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
389 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
391 struct shown {
392 int mday; /* day of the month */
393 int mon; /* month */
394 int year; /* year since 1900 */
395 int wday; /* day of the week */
396 int firstday; /* first (w)day of month */
397 int lastday; /* last (w)day of month */
400 static bool use_system_font = false;
402 static bool been_in_usb_mode = false;
404 /* leap year -- account for gregorian reformation in 1752 */
405 static int is_leap_year(int yr)
407 return ((yr) <= 1752 ? !((yr) % 4) : \
408 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
411 /* searches the weekday of the first day in month,
412 * relative to the given values */
413 static int calc_weekday( struct shown *shown )
415 return ( shown->wday + 36 - shown->mday ) % 7 ;
418 #if (CONFIG_RTC == 0)
419 /* from timefunc.c */
420 static void my_set_day_of_week(struct shown *shown)
422 int y = shown->year;
423 int d = shown->mday;
424 int m = shown->mon-1;
425 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
427 if(m == 0 || m == 1) y--;
428 shown->wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7 - 1;
430 #endif
432 static void calendar_init(struct shown *shown)
434 int w, h;
435 #if CONFIG_RTC
436 struct tm *tm;
437 #endif
438 rb->lcd_getstringsize("A", &w, &h);
439 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
441 use_system_font = true;
443 #if CONFIG_RTC
444 tm = rb->get_time();
445 shown->mday = tm->tm_mday;
446 shown->mon = tm->tm_mon + 1;
447 shown->year = 2000 + (tm->tm_year%100);
448 shown->wday = tm->tm_wday - 1;
449 #else
450 #define S100(x) 1 ## x
451 #define C2DIG2DEC(x) (S100(x)-100)
452 if(info.last_mon == 0 || info.last_year == 0)
454 shown->mon = C2DIG2DEC(MONTH);
455 shown->year = YEAR;
457 else
459 shown->mon = info.last_mon;
460 shown->year = info.last_year;
462 shown->mday = 1;
463 my_set_day_of_week(shown);
464 #endif
465 shown->firstday = calc_weekday(shown);
466 leap_year = is_leap_year(shown->year);
469 static void draw_headers(void)
471 int i, w, h;
472 int x = X_OFFSET;
473 int wday;
474 const char **dayname = dayname_long;
476 for (i = 0; i < 7; i++)
478 rb->lcd_getstringsize(dayname[i], &w, &h);
479 if (w > CELL_WIDTH)
481 dayname = dayname_short;
482 break;
486 wday = info.first_wday;
487 rb->lcd_getstringsize("A", &w, &h);
488 for (i = 0; i < 7; i++)
490 if (wday >= 7) wday = 0;
491 rb->lcd_putsxy(x, 0, dayname[wday++]);
492 x += CELL_WIDTH;
494 rb->lcd_hline(0, LCD_WIDTH-1, h);
497 static bool day_has_memo[32];
498 static bool wday_has_memo[7];
499 static void draw_calendar(struct shown *shown)
501 int w, h;
502 int x, y, pos, days_per_month, j;
503 int wday;
504 const char *monthname[] = {
505 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
506 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
508 if(use_system_font)
510 rb->lcd_setfont(FONT_SYSFIXED);
512 rb->lcd_getstringsize("A", &w, &h);
513 rb->lcd_clear_display();
514 draw_headers();
515 wday = shown->firstday;
516 pos = wday + 7 - info.first_wday;
517 if (pos >= 7) pos -= 7;
519 days_per_month = days_in_month[leap_year][shown->mon];
520 x = X_OFFSET + (pos * CELL_WIDTH);
521 y = Y_OFFSET + h;
522 for (j = 1; j <= days_per_month; j++)
524 if (shown->mday == j)
526 rb->lcd_set_drawmode(DRMODE_SOLID);
527 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
528 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
529 shown->wday = wday;
531 else
533 rb->lcd_set_drawmode(DRMODE_SOLID);
535 if ( (day_has_memo[j]) || (wday_has_memo[wday]) )
536 rb->lcd_putsxyf(x, y, "%02d.", j);
537 else
538 rb->lcd_putsxyf(x, y, "%02d", j);
539 x += CELL_WIDTH;
540 wday++;
541 if (wday >= 7)
542 wday = 0;
543 pos++;
544 if (pos >= 7)
546 pos = 0;
547 x = X_OFFSET;
548 y += CELL_HEIGHT;
551 shown->lastday = wday;
552 rb->lcd_set_drawmode(DRMODE_SOLID);
553 rb->lcd_vline(LCD_WIDTH-w*8-10, LCD_HEIGHT-h-3, LCD_HEIGHT-1);
554 rb->lcd_hline(LCD_WIDTH-w*8-10, LCD_WIDTH-1, LCD_HEIGHT-h-3);
555 rb->lcd_putsxyf(LCD_WIDTH-w*8-8, LCD_HEIGHT-h-1, "%s %04d",
556 monthname[shown->mon-1], shown->year);
557 rb->lcd_update();
560 #define MAX_CHAR_MEMO_LEN 64
561 #define MAX_MEMOS_IN_A_MONTH 128
562 struct memo {
563 char message[MAX_CHAR_MEMO_LEN];
564 int day;
565 int month;
566 int file_pointer_start;
567 int file_pointer_end;
568 int year;
569 int wday;
570 int type;
571 } memos[MAX_MEMOS_IN_A_MONTH];
572 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
573 static int memos_in_memory = 0;
574 static int memos_in_shown_memory = 0;
576 static void load_memo(struct shown *shown)
578 int k, fp;
579 bool exit = false;
580 char temp_memo1[2];
581 char temp_memo2[3];
582 char temp_memo4[5];
583 temp_memo1[1] = 0;
584 temp_memo2[2] = 0;
585 temp_memo4[4] = 0;
586 for (k = 1; k < 32; k++)
587 day_has_memo[k] = false;
588 for (k = 0; k < 7; k++)
589 wday_has_memo[k] = false;
590 memos_in_memory = 0;
591 fp = rb->open(MEMO_FILE, O_RDONLY);
592 if (fp > -1)
594 rb->lseek(fp, 0, SEEK_SET);
595 while (!exit)
597 bool load_to_memory;
598 struct memo *memo = &memos[memos_in_memory];
599 rb->memset(memo, 0, sizeof(*memo));
600 memo->file_pointer_start = rb->lseek(fp, 0, SEEK_CUR);
601 if (rb->read(fp, temp_memo2, 2) == 2)
602 memo->day = rb->atoi(temp_memo2);
603 if (rb->read(fp, temp_memo2, 2) == 2)
604 memo->month = rb->atoi(temp_memo2);
605 if (rb->read(fp, temp_memo4, 4) == 4)
606 memo->year = rb->atoi(temp_memo4);
607 if (rb->read(fp, temp_memo1, 1) == 1)
608 memo->wday = rb->atoi(temp_memo1);
609 if (rb->read(fp, temp_memo1, 1) == 1)
610 memo->type = rb->atoi(temp_memo1);
611 load_to_memory = ((memo->type < 2) ||
612 ((memo->type == 2) &&
613 (memo->month == shown->mon)) ||
614 ((memo->type > 2) &&
615 (memo->month == shown->mon) &&
616 (memo->year == shown->year)));
617 k = 0;
618 while (1)
620 if (rb->read(fp, temp_memo1, 1) != 1)
622 memo->day = 0;
623 memo->month = 0;
624 memo->file_pointer_start = 0;
625 memo->file_pointer_end = 0;
626 memo->year = 0;
627 memo->type = 0;
628 memo->wday = 0;
629 memo->message[0] = 0;
630 exit = true;
631 break;
633 if (load_to_memory)
635 if (temp_memo1[0] == '\n')
637 if (memo->type > 0)
638 day_has_memo[memo->day] = true;
639 else
640 wday_has_memo[memo->wday] = true;
641 memo->file_pointer_end = rb->lseek(fp, 0, SEEK_CUR);
642 memos_in_memory++;
644 else if ( (temp_memo1[0] != '\r') &&
645 (temp_memo1[0] != '\t') &&
646 k < MAX_CHAR_MEMO_LEN-1 )
647 memo->message[k++] = temp_memo1[0];
649 if (temp_memo1[0] == '\n')
650 break;
653 rb->close(fp);
657 static bool save_memo(int changed, bool new_mod, struct shown *shown)
659 int fp, fq;
660 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
661 fp = rb->open(MEMO_FILE, O_RDWR|O_CREAT, 0666);
662 fq = rb->creat(TEMP_FILE, 0666);
663 if ( (fq > -1) && (fp > -1) )
665 int i;
666 char temp[MAX_CHAR_MEMO_LEN];
667 struct memo *memo = &memos[changed];
668 rb->lseek(fp, 0, SEEK_SET);
669 for (i = 0; i < memo->file_pointer_start; i++)
671 rb->read(fp, temp, 1);
672 rb->write(fq, temp, 1);
674 if (new_mod)
676 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
677 memo->day, memo->month, memo->year, memo->wday,
678 memo->type, memo->message);
680 rb->lseek(fp, memo->file_pointer_end, SEEK_SET);
681 while(rb->read(fp, temp, 1) == 1)
683 rb->write(fq, temp, 1);
685 rb->close(fp);
686 rb->close(fq);
687 rb->remove(MEMO_FILE);
688 rb->rename(TEMP_FILE, MEMO_FILE);
689 load_memo(shown);
690 return true;
692 else if (fp > -1)
693 rb->close(fp);
694 else if (fq > -1)
695 rb->close(fq);
696 return false;
699 static void add_memo(struct shown *shown, int type)
701 bool saved = false;
702 struct memo *memo = &memos[memos_in_memory];
703 if (rb->kbd_input(memo->message, MAX_CHAR_MEMO_LEN) == 0)
705 if (memo->message[0])
707 memo->file_pointer_start = 0;
708 memo->file_pointer_end = 0;
709 memo->day = shown->mday;
710 memo->month = shown->mon;
711 memo->wday = shown->wday;
712 memo->year = shown->year;
713 memo->type = type;
714 if (save_memo(memos_in_memory, true, shown))
716 saved = true;
718 else
720 memo->file_pointer_start = 0;
721 memo->file_pointer_end = 0;
722 memo->day = 0;
723 memo->month = 0;
724 memo->year = 0;
725 memo->type = 0;
726 memo->wday = 0;
730 rb->lcd_clear_display();
731 if (saved)
732 rb->splash(HZ/2, "Event added");
733 else
734 rb->splash(HZ/2, "Event not added");
737 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
739 int i = (intptr_t)this_item;
740 if (action == ACTION_REQUEST_MENUITEM
741 && memos_in_shown_memory <= 0 && (i==0 || i==1))
742 return ACTION_EXIT_MENUITEM;
743 return action;
746 static bool edit_memo(int change, struct shown *shown)
748 bool exit = false;
749 int selected = 0;
751 static const struct opt_items modes[7] = {
752 { "Mon", -1 },
753 { "Tue", -1 },
754 { "Wed", -1 },
755 { "Thu", -1 },
756 { "Fri", -1 },
757 { "Sat", -1 },
758 { "Sun", -1 },
761 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
762 "Remove", "Edit",
763 "New Weekly", "New Monthly",
764 "New Yearly", "New One off",
765 "First Day of Week",
766 "Playback Control");
768 while (!exit)
770 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
772 case 0: /* remove */
773 save_memo(change, false, shown);
774 return false;
776 case 1: /* edit */
777 if(rb->kbd_input(memos[change].message,
778 MAX_CHAR_MEMO_LEN) == 0)
779 save_memo(change, true, shown);
780 return false;
782 case 2: /* weekly */
783 add_memo(shown, 0);
784 return false;
786 case 3: /* monthly */
787 add_memo(shown, 1);
788 return false;
790 case 4: /* yearly */
791 add_memo(shown, 2);
792 return false;
794 case 5: /* one off */
795 add_memo(shown, 3);
796 return false;
798 case 6: /* weekday */
799 rb->set_option("First Day of Week", &info.first_wday,
800 INT, modes, 7, NULL);
801 break;
803 case 7: /* playback control */
804 playback_control(NULL);
805 break;
807 case GO_TO_PREVIOUS:
808 return false;
810 case MENU_ATTACHED_USB:
811 been_in_usb_mode = true;
812 break;
815 return false;
818 static const char* get_event_text(int selected, void *data,
819 char *buffer, size_t buffer_len)
821 struct shown *shown = (struct shown *) data;
822 struct memo *memo;
823 if (selected < 0 || memos_in_shown_memory <= selected)
825 return NULL;
827 memo = &memos[pointer_array[selected]];
828 if (memo->type == 2)
829 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
830 memo->message, shown->year - memo->year);
831 else
832 rb->snprintf(buffer, buffer_len, "%s", memo->message);
833 return buffer;
836 static bool view_events(int selected, struct shown *shown)
838 struct gui_synclist gui_memos;
839 bool exit=false;
840 int button;
842 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
843 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
844 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
845 rb->gui_synclist_select_item(&gui_memos, selected);
846 rb->gui_synclist_draw(&gui_memos);
848 while (!exit)
850 button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
851 rb->gui_synclist_do_button(&gui_memos, &button, LIST_WRAP_UNLESS_HELD);
853 switch (button)
855 case ACTION_STD_OK:
856 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
857 return edit_memo(pointer_array[selected], shown);
858 break;
860 case ACTION_STD_CANCEL:
861 return false;
862 break;
864 default:
865 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
866 been_in_usb_mode = true;
867 break;
871 return false;
874 static void update_memos_shown(struct shown *shown)
876 int i;
877 struct memo *memo;
878 memos_in_shown_memory = 0;
879 for (i = 0; i < memos_in_memory; i++)
881 memo = &memos[i];
882 if (((memo->type >= 1) && (memo->day == shown->mday)) ||
883 ((memo->type < 1) && (memo->wday == shown->wday)))
884 pointer_array[memos_in_shown_memory++] = i;
888 static bool any_events(struct shown *shown, bool force)
890 update_memos_shown(shown);
892 if (memos_in_shown_memory > 0)
893 return view_events(0, shown);
894 else if (force)
895 return edit_memo(0, shown);
896 else
897 return false;
899 return false;
902 static void next_month(struct shown *shown, int step)
904 shown->mon++;
905 if (shown->mon > 12)
907 shown->mon = 1;
908 shown->year++;
909 leap_year = is_leap_year(shown->year);
911 if (step > 0)
912 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
913 else if (shown->mday > days_in_month[leap_year][shown->mon])
914 shown->mday = days_in_month[leap_year][shown->mon];
915 shown->firstday = shown->lastday;
916 load_memo(shown);
917 draw_calendar(shown);
920 static void prev_month(struct shown *shown, int step)
922 shown->mon--;
923 if (shown->mon < 1)
925 shown->mon = 12;
926 shown->year--;
927 leap_year = is_leap_year(shown->year);
929 if (step > 0)
930 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
931 else if (shown->mday > days_in_month[leap_year][shown->mon])
932 shown->mday = days_in_month[leap_year][shown->mon];
933 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
934 if (shown->firstday >= 7)
935 shown->firstday -= 7;
936 load_memo(shown);
937 draw_calendar(shown);
940 static void next_day(struct shown *shown, int step)
942 shown->mday += step;
943 if (shown->mday > days_in_month[leap_year][shown->mon])
944 next_month(shown, step);
945 else
946 draw_calendar(shown);
949 static void prev_day(struct shown *shown, int step)
951 shown->mday -= step;
952 if (shown->mday < 1)
953 prev_month(shown, step);
954 else
955 draw_calendar(shown);
958 enum plugin_status plugin_start(const void* parameter)
960 struct shown shown;
961 bool exit = false;
962 int button;
964 (void)(parameter);
966 configfile_load(CFG_FILE, config, ARRAYLEN(config), 0);
967 rb->memcpy(&old_info, &info, sizeof(struct info));
969 calendar_init(&shown);
970 load_memo(&shown);
971 any_events(&shown, false);
972 draw_calendar(&shown);
974 while (!exit)
976 button = rb->button_get(true);
977 switch (button)
979 case CALENDAR_QUIT:
980 exit = true;
981 break;
983 case CALENDAR_NEXT_MONTH:
984 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
985 next_month(&shown, 0);
986 break;
988 case CALENDAR_PREV_MONTH:
989 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
990 prev_month(&shown, 0);
991 break;
993 case CALENDAR_NEXT_WEEK:
994 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
995 next_day(&shown, 7);
996 break;
998 case CALENDAR_PREV_WEEK:
999 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
1000 prev_day(&shown, 7);
1001 break;
1003 case CALENDAR_PREV_DAY:
1004 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
1005 prev_day(&shown, 1);
1006 break;
1008 case CALENDAR_NEXT_DAY:
1009 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
1010 next_day(&shown, 1);
1011 break;
1013 case CALENDAR_SELECT:
1014 any_events(&shown, true);
1015 draw_calendar(&shown);
1016 break;
1018 default:
1019 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
1020 been_in_usb_mode = true;
1021 draw_calendar(&shown);
1022 break;
1027 #if (CONFIG_RTC == 0)
1028 info.last_mon = shown.mon;
1029 info.last_year = shown.year;
1030 #endif
1031 if (rb->memcmp(&old_info, &info, sizeof(struct info)))
1032 configfile_save(CFG_FILE, config, ARRAYLEN(config), 0);
1033 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;