New plugin: FFT, A frequency analyzer plugin
[kugel-rb.git] / apps / plugins / calendar.c
blob1106c38d4bde6031c67d902065a47cd1fa3fa6b4
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"
28 PLUGIN_HEADER
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_SA9200_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 == ONDAVX747_PAD)
226 #define CALENDAR_QUIT BUTTON_POWER
227 #define CALENDAR_SELECT BUTTON_MENU
228 #define CALENDAR_NEXT_WEEK BUTTON_VOL_DOWN
229 #define CALENDAR_PREV_WEEK BUTTON_VOL_UP
230 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
231 #define CALENDAR_PREV_DAY BUTTON_LEFT
232 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
233 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
235 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
236 #define CALENDAR_QUIT BUTTON_POWER
238 #elif CONFIG_KEYPAD == MROBE500_PAD
239 #define CALENDAR_QUIT BUTTON_POWER
241 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
242 #define CALENDAR_QUIT BUTTON_REC
243 #define CALENDAR_SELECT BUTTON_PLAY
244 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
245 #define CALENDAR_PREV_WEEK BUTTON_UP
246 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
247 #define CALENDAR_PREV_DAY BUTTON_LEFT
248 #define CALENDAR_NEXT_MONTH BUTTON_FFWD
249 #define CALENDAR_PREV_MONTH BUTTON_REW
251 #else
252 #error "No keypad setting."
253 #endif
255 #ifdef HAVE_TOUCHSCREEN
256 #ifndef CALENDAR_QUIT
257 #define CALENDAR_QUIT BUTTON_MIDLEFT
258 #endif
259 #ifndef CALENDAR_SELECT
260 #define CALENDAR_SELECT BUTTON_CENTER
261 #endif
262 #ifndef CALENDAR_NEXT_DAY
263 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
264 #endif
265 #ifndef CALENDAR_PREV_DAY
266 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
267 #endif
268 #ifndef CALENDAR_NEXT_WEEK
269 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
270 #endif
271 #ifndef CALENDAR_PREV_WEEK
272 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
273 #endif
274 #ifndef CALENDAR_NEXT_MONTH
275 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
276 #endif
277 #ifndef CALENDAR_PREV_MONTH
278 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
279 #endif
280 #endif
282 #define MEMO_FILE PLUGIN_APPS_DIR "/.memo"
283 #define TEMP_FILE PLUGIN_APPS_DIR "/~temp"
285 #define X_OFFSET ((LCD_WIDTH%7)/2)
286 #if LCD_HEIGHT <= 80
287 #define Y_OFFSET 1
288 #else
289 #define Y_OFFSET 4
290 #endif
291 #define CELL_WIDTH (LCD_WIDTH / 7)
292 #define CELL_HEIGHT (LCD_HEIGHT / 7)
294 #define CFG_FILE "calendar.cfg"
295 struct info {
296 int first_wday;
297 #if (CONFIG_RTC == 0)
298 int last_mon;
299 int last_year;
300 #endif
302 static struct info info = { .first_wday = 0 }, old_info;
303 static struct configdata config[] = {
304 { TYPE_INT, 0, 6, { .int_p = &(info.first_wday) }, "first wday", NULL },
305 #if (CONFIG_RTC == 0)
306 { TYPE_INT, 1, 12, { .int_p = &(info.last_mon) }, "last mon", NULL },
307 { TYPE_INT, 1, 3000, { .int_p = &(info.last_year) }, "last year", NULL },
308 #endif
311 static bool leap_year;
312 /* days_in_month[][0] is for December */
313 static const int days_in_month[2][13] = {
314 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
315 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
318 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
319 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
321 struct shown {
322 int mday; /* day of the month */
323 int mon; /* month */
324 int year; /* year since 1900 */
325 int wday; /* day of the week */
326 int firstday; /* first (w)day of month */
327 int lastday; /* last (w)day of month */
330 static bool use_system_font = false;
332 static bool been_in_usb_mode = false;
334 /* leap year -- account for gregorian reformation in 1752 */
335 static int is_leap_year(int yr)
337 return ((yr) <= 1752 ? !((yr) % 4) : \
338 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
341 /* searches the weekday of the first day in month,
342 * relative to the given values */
343 static int calc_weekday( struct shown *shown )
345 return ( shown->wday + 36 - shown->mday ) % 7 ;
348 #if (CONFIG_RTC == 0)
349 /* from timefunc.c */
350 static void my_set_day_of_week(struct shown *shown)
352 int y = shown->year;
353 int d = shown->mday;
354 int m = shown->mon-1;
355 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
357 if(m == 0 || m == 1) y--;
358 shown->wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7 - 1;
360 #endif
362 static void calendar_init(struct shown *shown)
364 int w, h;
365 #if CONFIG_RTC
366 struct tm *tm;
367 #endif
368 rb->lcd_getstringsize("A", &w, &h);
369 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
371 use_system_font = true;
373 #if CONFIG_RTC
374 tm = rb->get_time();
375 shown->mday = tm->tm_mday;
376 shown->mon = tm->tm_mon + 1;
377 shown->year = 2000 + (tm->tm_year%100);
378 shown->wday = tm->tm_wday - 1;
379 #else
380 #define S100(x) 1 ## x
381 #define C2DIG2DEC(x) (S100(x)-100)
382 if(info.last_mon == 0 || info.last_year == 0)
384 shown->mon = C2DIG2DEC(MONTH);
385 shown->year = YEAR;
387 else
389 shown->mon = info.last_mon;
390 shown->year = info.last_year;
392 shown->mday = 1;
393 my_set_day_of_week(shown);
394 #endif
395 shown->firstday = calc_weekday(shown);
396 leap_year = is_leap_year(shown->year);
399 static void draw_headers(void)
401 int i, w, h;
402 int x = X_OFFSET;
403 int wday;
404 const char **dayname = dayname_long;
406 for (i = 0; i < 7; i++)
408 rb->lcd_getstringsize(dayname[i], &w, &h);
409 if (w > CELL_WIDTH)
411 dayname = dayname_short;
412 break;
416 wday = info.first_wday;
417 rb->lcd_getstringsize("A", &w, &h);
418 for (i = 0; i < 7; i++)
420 if (wday >= 7) wday = 0;
421 rb->lcd_putsxy(x, 0, dayname[wday++]);
422 x += CELL_WIDTH;
424 rb->lcd_hline(0, LCD_WIDTH-1, h);
427 static bool day_has_memo[32];
428 static bool wday_has_memo[7];
429 static void draw_calendar(struct shown *shown)
431 int w, h;
432 int x, y, pos, days_per_month, j;
433 int wday;
434 char buffer[12];
435 const char *monthname[] = {
436 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
437 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
439 if(use_system_font)
441 rb->lcd_setfont(FONT_SYSFIXED);
443 rb->lcd_getstringsize("A", &w, &h);
444 rb->lcd_clear_display();
445 draw_headers();
446 wday = shown->firstday;
447 pos = wday + 7 - info.first_wday;
448 if (pos >= 7) pos -= 7;
450 days_per_month = days_in_month[leap_year][shown->mon];
451 x = X_OFFSET + (pos * CELL_WIDTH);
452 y = Y_OFFSET + h;
453 for (j = 1; j <= days_per_month; j++)
455 if ( (day_has_memo[j]) || (wday_has_memo[wday]) )
456 rb->snprintf(buffer, 4, "%02d.", j);
457 else
458 rb->snprintf(buffer, 4, "%02d", j);
459 if (shown->mday == j)
461 rb->lcd_set_drawmode(DRMODE_SOLID);
462 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
463 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
464 shown->wday = wday;
466 else
468 rb->lcd_set_drawmode(DRMODE_SOLID);
470 rb->lcd_putsxy(x, y, buffer);
471 x += CELL_WIDTH;
472 wday++;
473 if (wday >= 7)
474 wday = 0;
475 pos++;
476 if (pos >= 7)
478 pos = 0;
479 x = X_OFFSET;
480 y += CELL_HEIGHT;
483 shown->lastday = wday;
484 rb->lcd_set_drawmode(DRMODE_SOLID);
485 rb->lcd_vline(LCD_WIDTH-w*8-10, LCD_HEIGHT-h-3, LCD_HEIGHT-1);
486 rb->lcd_hline(LCD_WIDTH-w*8-10, LCD_WIDTH-1, LCD_HEIGHT-h-3);
487 rb->snprintf(buffer, sizeof(buffer), "%s %04d",
488 monthname[shown->mon-1], shown->year);
489 rb->lcd_putsxy(LCD_WIDTH-w*8-8, LCD_HEIGHT-h-1, buffer);
490 rb->lcd_update();
493 #define MAX_CHAR_MEMO_LEN 64
494 #define MAX_MEMOS_IN_A_MONTH 128
495 struct memo {
496 char message[MAX_CHAR_MEMO_LEN];
497 int day;
498 int month;
499 int file_pointer_start;
500 int file_pointer_end;
501 int year;
502 int wday;
503 int type;
504 } memos[MAX_MEMOS_IN_A_MONTH];
505 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
506 static int memos_in_memory = 0;
507 static int memos_in_shown_memory = 0;
509 static void load_memo(struct shown *shown)
511 int k, fp;
512 bool exit = false;
513 char temp_memo1[2];
514 char temp_memo2[3];
515 char temp_memo4[5];
516 temp_memo1[1] = 0;
517 temp_memo2[2] = 0;
518 temp_memo4[4] = 0;
519 for (k = 1; k < 32; k++)
520 day_has_memo[k] = false;
521 for (k = 0; k < 7; k++)
522 wday_has_memo[k] = false;
523 memos_in_memory = 0;
524 fp = rb->open(MEMO_FILE, O_RDONLY);
525 if (fp > -1)
527 rb->lseek(fp, 0, SEEK_SET);
528 while (!exit)
530 bool load_to_memory;
531 struct memo *memo = &memos[memos_in_memory];
532 rb->memset(memo, 0, sizeof(*memo));
533 memo->file_pointer_start = rb->lseek(fp, 0, SEEK_CUR);
534 if (rb->read(fp, temp_memo2, 2) == 2)
535 memo->day = rb->atoi(temp_memo2);
536 if (rb->read(fp, temp_memo2, 2) == 2)
537 memo->month = rb->atoi(temp_memo2);
538 if (rb->read(fp, temp_memo4, 4) == 4)
539 memo->year = rb->atoi(temp_memo4);
540 if (rb->read(fp, temp_memo1, 1) == 1)
541 memo->wday = rb->atoi(temp_memo1);
542 if (rb->read(fp, temp_memo1, 1) == 1)
543 memo->type = rb->atoi(temp_memo1);
544 load_to_memory = ((memo->type < 2) ||
545 ((memo->type == 2) &&
546 (memo->month == shown->mon)) ||
547 ((memo->type > 2) &&
548 (memo->month == shown->mon) &&
549 (memo->year == shown->year)));
550 k = 0;
551 while (1)
553 if (rb->read(fp, temp_memo1, 1) != 1)
555 memo->day = 0;
556 memo->month = 0;
557 memo->file_pointer_start = 0;
558 memo->file_pointer_end = 0;
559 memo->year = 0;
560 memo->type = 0;
561 memo->wday = 0;
562 memo->message[0] = 0;
563 exit = true;
564 break;
566 if (load_to_memory)
568 if (temp_memo1[0] == '\n')
570 if (memo->type > 0)
571 day_has_memo[memo->day] = true;
572 else
573 wday_has_memo[memo->wday] = true;
574 memo->file_pointer_end = rb->lseek(fp, 0, SEEK_CUR);
575 memos_in_memory++;
577 else if ( (temp_memo1[0] != '\r') &&
578 (temp_memo1[0] != '\t') &&
579 k < MAX_CHAR_MEMO_LEN-1 )
580 memo->message[k++] = temp_memo1[0];
582 if (temp_memo1[0] == '\n')
583 break;
586 rb->close(fp);
590 static bool save_memo(int changed, bool new_mod, struct shown *shown)
592 int fp, fq;
593 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
594 fp = rb->open(MEMO_FILE, O_RDWR|O_CREAT);
595 fq = rb->creat(TEMP_FILE);
596 if ( (fq > -1) && (fp > -1) )
598 int i;
599 char temp[MAX_CHAR_MEMO_LEN];
600 struct memo *memo = &memos[changed];
601 rb->lseek(fp, 0, SEEK_SET);
602 for (i = 0; i < memo->file_pointer_start; i++)
604 rb->read(fp, temp, 1);
605 rb->write(fq, temp, 1);
607 if (new_mod)
609 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
610 memo->day, memo->month, memo->year, memo->wday,
611 memo->type, memo->message);
613 rb->lseek(fp, memo->file_pointer_end, SEEK_SET);
614 while(rb->read(fp, temp, 1) == 1)
616 rb->write(fq, temp, 1);
618 rb->close(fp);
619 rb->close(fq);
620 rb->remove(MEMO_FILE);
621 rb->rename(TEMP_FILE, MEMO_FILE);
622 load_memo(shown);
623 return true;
625 else if (fp > -1)
626 rb->close(fp);
627 else if (fq > -1)
628 rb->close(fq);
629 return false;
632 static void add_memo(struct shown *shown, int type)
634 bool saved = false;
635 struct memo *memo = &memos[memos_in_memory];
636 if (rb->kbd_input(memo->message, MAX_CHAR_MEMO_LEN) == 0)
638 if (memo->message[0])
640 memo->file_pointer_start = 0;
641 memo->file_pointer_end = 0;
642 memo->day = shown->mday;
643 memo->month = shown->mon;
644 memo->wday = shown->wday;
645 memo->year = shown->year;
646 memo->type = type;
647 if (save_memo(memos_in_memory, true, shown))
649 saved = true;
651 else
653 memo->file_pointer_start = 0;
654 memo->file_pointer_end = 0;
655 memo->day = 0;
656 memo->month = 0;
657 memo->year = 0;
658 memo->type = 0;
659 memo->wday = 0;
663 rb->lcd_clear_display();
664 if (saved)
665 rb->splash(HZ/2, "Event added");
666 else
667 rb->splash(HZ/2, "Event not added");
670 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
672 int i = (intptr_t)this_item;
673 if (action == ACTION_REQUEST_MENUITEM
674 && memos_in_shown_memory <= 0 && (i==0 || i==1))
675 return ACTION_EXIT_MENUITEM;
676 return action;
679 static bool edit_memo(int change, struct shown *shown)
681 bool exit = false;
682 int selected = 0;
684 static const struct opt_items modes[7] = {
685 { "Mon", -1 },
686 { "Tue", -1 },
687 { "Wed", -1 },
688 { "Thu", -1 },
689 { "Fri", -1 },
690 { "Sat", -1 },
691 { "Sun", -1 },
694 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
695 "Remove", "Edit",
696 "New Weekly", "New Monthly",
697 "New Yearly", "New One off",
698 "First Day of Week",
699 "Playback Control");
701 while (!exit)
703 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
705 case 0: /* remove */
706 save_memo(change, false, shown);
707 return false;
709 case 1: /* edit */
710 if(rb->kbd_input(memos[change].message,
711 MAX_CHAR_MEMO_LEN) == 0)
712 save_memo(change, true, shown);
713 return false;
715 case 2: /* weekly */
716 add_memo(shown, 0);
717 return false;
719 case 3: /* monthly */
720 add_memo(shown, 1);
721 return false;
723 case 4: /* yearly */
724 add_memo(shown, 2);
725 return false;
727 case 5: /* one off */
728 add_memo(shown, 3);
729 return false;
731 case 6: /* weekday */
732 rb->set_option("First Day of Week", &info.first_wday,
733 INT, modes, 7, NULL);
734 break;
736 case 7: /* playback control */
737 playback_control(NULL);
738 break;
740 case GO_TO_PREVIOUS:
741 return false;
743 case MENU_ATTACHED_USB:
744 been_in_usb_mode = true;
745 break;
748 return false;
751 static const char* get_event_text(int selected, void *data,
752 char *buffer, size_t buffer_len)
754 struct shown *shown = (struct shown *) data;
755 struct memo *memo;
756 if (selected < 0 || memos_in_shown_memory <= selected)
758 return NULL;
760 memo = &memos[pointer_array[selected]];
761 if (memo->type == 2)
762 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
763 memo->message, shown->year - memo->year);
764 else
765 rb->snprintf(buffer, buffer_len, "%s", memo->message);
766 return buffer;
769 static bool view_events(int selected, struct shown *shown)
771 struct gui_synclist gui_memos;
772 bool exit=false;
773 int button;
775 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
776 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
777 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
778 rb->gui_synclist_select_item(&gui_memos, selected);
779 rb->gui_synclist_draw(&gui_memos);
781 while (!exit)
783 button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
784 rb->gui_synclist_do_button(&gui_memos, &button, LIST_WRAP_UNLESS_HELD);
786 switch (button)
788 case ACTION_STD_OK:
789 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
790 return edit_memo(pointer_array[selected], shown);
791 break;
793 case ACTION_STD_CANCEL:
794 return false;
795 break;
797 default:
798 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
799 been_in_usb_mode = true;
800 break;
804 return false;
807 static void update_memos_shown(struct shown *shown)
809 int i;
810 struct memo *memo;
811 memos_in_shown_memory = 0;
812 for (i = 0; i < memos_in_memory; i++)
814 memo = &memos[i];
815 if (((memo->type >= 1) && (memo->day == shown->mday)) ||
816 ((memo->type < 1) && (memo->wday == shown->wday)))
817 pointer_array[memos_in_shown_memory++] = i;
821 static bool any_events(struct shown *shown, bool force)
823 update_memos_shown(shown);
825 if (memos_in_shown_memory > 0)
826 return view_events(0, shown);
827 else if (force)
828 return edit_memo(0, shown);
829 else
830 return false;
832 return false;
835 static void next_month(struct shown *shown, int step)
837 shown->mon++;
838 if (shown->mon > 12)
840 shown->mon = 1;
841 shown->year++;
842 leap_year = is_leap_year(shown->year);
844 if (step > 0)
845 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
846 else if (shown->mday > days_in_month[leap_year][shown->mon])
847 shown->mday = days_in_month[leap_year][shown->mon];
848 shown->firstday = shown->lastday;
849 load_memo(shown);
850 draw_calendar(shown);
853 static void prev_month(struct shown *shown, int step)
855 shown->mon--;
856 if (shown->mon < 1)
858 shown->mon = 12;
859 shown->year--;
860 leap_year = is_leap_year(shown->year);
862 if (step > 0)
863 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
864 else if (shown->mday > days_in_month[leap_year][shown->mon])
865 shown->mday = days_in_month[leap_year][shown->mon];
866 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
867 if (shown->firstday >= 7)
868 shown->firstday -= 7;
869 load_memo(shown);
870 draw_calendar(shown);
873 static void next_day(struct shown *shown, int step)
875 shown->mday += step;
876 if (shown->mday > days_in_month[leap_year][shown->mon])
877 next_month(shown, step);
878 else
879 draw_calendar(shown);
882 static void prev_day(struct shown *shown, int step)
884 shown->mday -= step;
885 if (shown->mday < 1)
886 prev_month(shown, step);
887 else
888 draw_calendar(shown);
891 enum plugin_status plugin_start(const void* parameter)
893 struct shown shown;
894 bool exit = false;
895 int button;
897 (void)(parameter);
899 configfile_load(CFG_FILE, config, ARRAYLEN(config), 0);
900 rb->memcpy(&old_info, &info, sizeof(struct info));
902 calendar_init(&shown);
903 load_memo(&shown);
904 any_events(&shown, false);
905 draw_calendar(&shown);
907 while (!exit)
909 button = rb->button_get(true);
910 switch (button)
912 case CALENDAR_QUIT:
913 exit = true;
914 break;
916 case CALENDAR_NEXT_MONTH:
917 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
918 next_month(&shown, 0);
919 break;
921 case CALENDAR_PREV_MONTH:
922 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
923 prev_month(&shown, 0);
924 break;
926 case CALENDAR_NEXT_WEEK:
927 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
928 next_day(&shown, 7);
929 break;
931 case CALENDAR_PREV_WEEK:
932 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
933 prev_day(&shown, 7);
934 break;
936 case CALENDAR_PREV_DAY:
937 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
938 prev_day(&shown, 1);
939 break;
941 case CALENDAR_NEXT_DAY:
942 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
943 next_day(&shown, 1);
944 break;
946 case CALENDAR_SELECT:
947 any_events(&shown, true);
948 draw_calendar(&shown);
949 break;
951 default:
952 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
953 been_in_usb_mode = true;
954 draw_calendar(&shown);
955 break;
960 #if (CONFIG_RTC == 0)
961 info.last_mon = shown.mon;
962 info.last_year = shown.year;
963 #endif
964 if (rb->memcmp(&old_info, &info, sizeof(struct info)))
965 configfile_save(CFG_FILE, config, ARRAYLEN(config), 0);
966 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;