Use a more natural guard for the callback definition
[kugel-rb.git] / apps / plugins / calendar.c
blob9327ac6aa2dee2fa53a193822d2d117bde80819d
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 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
252 #define CALENDAR_QUIT BUTTON_REC
253 #define CALENDAR_SELECT BUTTON_OK
254 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
255 #define CALENDAR_PREV_WEEK BUTTON_UP
256 #define CALENDAR_NEXT_DAY BUTTON_NEXT
257 #define CALENDAR_PREV_DAY BUTTON_PREV
258 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
259 #define CALENDAR_PREV_MONTH BUTTON_MENU
261 #else
262 #error "No keypad setting."
263 #endif
265 #ifdef HAVE_TOUCHSCREEN
266 #ifndef CALENDAR_QUIT
267 #define CALENDAR_QUIT BUTTON_MIDLEFT
268 #endif
269 #ifndef CALENDAR_SELECT
270 #define CALENDAR_SELECT BUTTON_CENTER
271 #endif
272 #ifndef CALENDAR_NEXT_DAY
273 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
274 #endif
275 #ifndef CALENDAR_PREV_DAY
276 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
277 #endif
278 #ifndef CALENDAR_NEXT_WEEK
279 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
280 #endif
281 #ifndef CALENDAR_PREV_WEEK
282 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
283 #endif
284 #ifndef CALENDAR_NEXT_MONTH
285 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
286 #endif
287 #ifndef CALENDAR_PREV_MONTH
288 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
289 #endif
290 #endif
292 #define MEMO_FILE PLUGIN_APPS_DIR "/.memo"
293 #define TEMP_FILE PLUGIN_APPS_DIR "/~temp"
295 #define X_OFFSET ((LCD_WIDTH%7)/2)
296 #if LCD_HEIGHT <= 80
297 #define Y_OFFSET 1
298 #else
299 #define Y_OFFSET 4
300 #endif
301 #define CELL_WIDTH (LCD_WIDTH / 7)
302 #define CELL_HEIGHT (LCD_HEIGHT / 7)
304 #define CFG_FILE "calendar.cfg"
305 struct info {
306 int first_wday;
307 #if (CONFIG_RTC == 0)
308 int last_mon;
309 int last_year;
310 #endif
312 static struct info info = { .first_wday = 0 }, old_info;
313 static struct configdata config[] = {
314 { TYPE_INT, 0, 6, { .int_p = &(info.first_wday) }, "first wday", NULL },
315 #if (CONFIG_RTC == 0)
316 { TYPE_INT, 1, 12, { .int_p = &(info.last_mon) }, "last mon", NULL },
317 { TYPE_INT, 1, 3000, { .int_p = &(info.last_year) }, "last year", NULL },
318 #endif
321 static bool leap_year;
322 /* days_in_month[][0] is for December */
323 static const int days_in_month[2][13] = {
324 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
325 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
328 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
329 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
331 struct shown {
332 int mday; /* day of the month */
333 int mon; /* month */
334 int year; /* year since 1900 */
335 int wday; /* day of the week */
336 int firstday; /* first (w)day of month */
337 int lastday; /* last (w)day of month */
340 static bool use_system_font = false;
342 static bool been_in_usb_mode = false;
344 /* leap year -- account for gregorian reformation in 1752 */
345 static int is_leap_year(int yr)
347 return ((yr) <= 1752 ? !((yr) % 4) : \
348 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
351 /* searches the weekday of the first day in month,
352 * relative to the given values */
353 static int calc_weekday( struct shown *shown )
355 return ( shown->wday + 36 - shown->mday ) % 7 ;
358 #if (CONFIG_RTC == 0)
359 /* from timefunc.c */
360 static void my_set_day_of_week(struct shown *shown)
362 int y = shown->year;
363 int d = shown->mday;
364 int m = shown->mon-1;
365 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
367 if(m == 0 || m == 1) y--;
368 shown->wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7 - 1;
370 #endif
372 static void calendar_init(struct shown *shown)
374 int w, h;
375 #if CONFIG_RTC
376 struct tm *tm;
377 #endif
378 rb->lcd_getstringsize("A", &w, &h);
379 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
381 use_system_font = true;
383 #if CONFIG_RTC
384 tm = rb->get_time();
385 shown->mday = tm->tm_mday;
386 shown->mon = tm->tm_mon + 1;
387 shown->year = 2000 + (tm->tm_year%100);
388 shown->wday = tm->tm_wday - 1;
389 #else
390 #define S100(x) 1 ## x
391 #define C2DIG2DEC(x) (S100(x)-100)
392 if(info.last_mon == 0 || info.last_year == 0)
394 shown->mon = C2DIG2DEC(MONTH);
395 shown->year = YEAR;
397 else
399 shown->mon = info.last_mon;
400 shown->year = info.last_year;
402 shown->mday = 1;
403 my_set_day_of_week(shown);
404 #endif
405 shown->firstday = calc_weekday(shown);
406 leap_year = is_leap_year(shown->year);
409 static void draw_headers(void)
411 int i, w, h;
412 int x = X_OFFSET;
413 int wday;
414 const char **dayname = dayname_long;
416 for (i = 0; i < 7; i++)
418 rb->lcd_getstringsize(dayname[i], &w, &h);
419 if (w > CELL_WIDTH)
421 dayname = dayname_short;
422 break;
426 wday = info.first_wday;
427 rb->lcd_getstringsize("A", &w, &h);
428 for (i = 0; i < 7; i++)
430 if (wday >= 7) wday = 0;
431 rb->lcd_putsxy(x, 0, dayname[wday++]);
432 x += CELL_WIDTH;
434 rb->lcd_hline(0, LCD_WIDTH-1, h);
437 static bool day_has_memo[32];
438 static bool wday_has_memo[7];
439 static void draw_calendar(struct shown *shown)
441 int w, h;
442 int x, y, pos, days_per_month, j;
443 int wday;
444 char buffer[12];
445 const char *monthname[] = {
446 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
447 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
449 if(use_system_font)
451 rb->lcd_setfont(FONT_SYSFIXED);
453 rb->lcd_getstringsize("A", &w, &h);
454 rb->lcd_clear_display();
455 draw_headers();
456 wday = shown->firstday;
457 pos = wday + 7 - info.first_wday;
458 if (pos >= 7) pos -= 7;
460 days_per_month = days_in_month[leap_year][shown->mon];
461 x = X_OFFSET + (pos * CELL_WIDTH);
462 y = Y_OFFSET + h;
463 for (j = 1; j <= days_per_month; j++)
465 if ( (day_has_memo[j]) || (wday_has_memo[wday]) )
466 rb->snprintf(buffer, 4, "%02d.", j);
467 else
468 rb->snprintf(buffer, 4, "%02d", j);
469 if (shown->mday == j)
471 rb->lcd_set_drawmode(DRMODE_SOLID);
472 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
473 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
474 shown->wday = wday;
476 else
478 rb->lcd_set_drawmode(DRMODE_SOLID);
480 rb->lcd_putsxy(x, y, buffer);
481 x += CELL_WIDTH;
482 wday++;
483 if (wday >= 7)
484 wday = 0;
485 pos++;
486 if (pos >= 7)
488 pos = 0;
489 x = X_OFFSET;
490 y += CELL_HEIGHT;
493 shown->lastday = wday;
494 rb->lcd_set_drawmode(DRMODE_SOLID);
495 rb->lcd_vline(LCD_WIDTH-w*8-10, LCD_HEIGHT-h-3, LCD_HEIGHT-1);
496 rb->lcd_hline(LCD_WIDTH-w*8-10, LCD_WIDTH-1, LCD_HEIGHT-h-3);
497 rb->snprintf(buffer, sizeof(buffer), "%s %04d",
498 monthname[shown->mon-1], shown->year);
499 rb->lcd_putsxy(LCD_WIDTH-w*8-8, LCD_HEIGHT-h-1, buffer);
500 rb->lcd_update();
503 #define MAX_CHAR_MEMO_LEN 64
504 #define MAX_MEMOS_IN_A_MONTH 128
505 struct memo {
506 char message[MAX_CHAR_MEMO_LEN];
507 int day;
508 int month;
509 int file_pointer_start;
510 int file_pointer_end;
511 int year;
512 int wday;
513 int type;
514 } memos[MAX_MEMOS_IN_A_MONTH];
515 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
516 static int memos_in_memory = 0;
517 static int memos_in_shown_memory = 0;
519 static void load_memo(struct shown *shown)
521 int k, fp;
522 bool exit = false;
523 char temp_memo1[2];
524 char temp_memo2[3];
525 char temp_memo4[5];
526 temp_memo1[1] = 0;
527 temp_memo2[2] = 0;
528 temp_memo4[4] = 0;
529 for (k = 1; k < 32; k++)
530 day_has_memo[k] = false;
531 for (k = 0; k < 7; k++)
532 wday_has_memo[k] = false;
533 memos_in_memory = 0;
534 fp = rb->open(MEMO_FILE, O_RDONLY);
535 if (fp > -1)
537 rb->lseek(fp, 0, SEEK_SET);
538 while (!exit)
540 bool load_to_memory;
541 struct memo *memo = &memos[memos_in_memory];
542 rb->memset(memo, 0, sizeof(*memo));
543 memo->file_pointer_start = rb->lseek(fp, 0, SEEK_CUR);
544 if (rb->read(fp, temp_memo2, 2) == 2)
545 memo->day = rb->atoi(temp_memo2);
546 if (rb->read(fp, temp_memo2, 2) == 2)
547 memo->month = rb->atoi(temp_memo2);
548 if (rb->read(fp, temp_memo4, 4) == 4)
549 memo->year = rb->atoi(temp_memo4);
550 if (rb->read(fp, temp_memo1, 1) == 1)
551 memo->wday = rb->atoi(temp_memo1);
552 if (rb->read(fp, temp_memo1, 1) == 1)
553 memo->type = rb->atoi(temp_memo1);
554 load_to_memory = ((memo->type < 2) ||
555 ((memo->type == 2) &&
556 (memo->month == shown->mon)) ||
557 ((memo->type > 2) &&
558 (memo->month == shown->mon) &&
559 (memo->year == shown->year)));
560 k = 0;
561 while (1)
563 if (rb->read(fp, temp_memo1, 1) != 1)
565 memo->day = 0;
566 memo->month = 0;
567 memo->file_pointer_start = 0;
568 memo->file_pointer_end = 0;
569 memo->year = 0;
570 memo->type = 0;
571 memo->wday = 0;
572 memo->message[0] = 0;
573 exit = true;
574 break;
576 if (load_to_memory)
578 if (temp_memo1[0] == '\n')
580 if (memo->type > 0)
581 day_has_memo[memo->day] = true;
582 else
583 wday_has_memo[memo->wday] = true;
584 memo->file_pointer_end = rb->lseek(fp, 0, SEEK_CUR);
585 memos_in_memory++;
587 else if ( (temp_memo1[0] != '\r') &&
588 (temp_memo1[0] != '\t') &&
589 k < MAX_CHAR_MEMO_LEN-1 )
590 memo->message[k++] = temp_memo1[0];
592 if (temp_memo1[0] == '\n')
593 break;
596 rb->close(fp);
600 static bool save_memo(int changed, bool new_mod, struct shown *shown)
602 int fp, fq;
603 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
604 fp = rb->open(MEMO_FILE, O_RDWR|O_CREAT);
605 fq = rb->creat(TEMP_FILE);
606 if ( (fq > -1) && (fp > -1) )
608 int i;
609 char temp[MAX_CHAR_MEMO_LEN];
610 struct memo *memo = &memos[changed];
611 rb->lseek(fp, 0, SEEK_SET);
612 for (i = 0; i < memo->file_pointer_start; i++)
614 rb->read(fp, temp, 1);
615 rb->write(fq, temp, 1);
617 if (new_mod)
619 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
620 memo->day, memo->month, memo->year, memo->wday,
621 memo->type, memo->message);
623 rb->lseek(fp, memo->file_pointer_end, SEEK_SET);
624 while(rb->read(fp, temp, 1) == 1)
626 rb->write(fq, temp, 1);
628 rb->close(fp);
629 rb->close(fq);
630 rb->remove(MEMO_FILE);
631 rb->rename(TEMP_FILE, MEMO_FILE);
632 load_memo(shown);
633 return true;
635 else if (fp > -1)
636 rb->close(fp);
637 else if (fq > -1)
638 rb->close(fq);
639 return false;
642 static void add_memo(struct shown *shown, int type)
644 bool saved = false;
645 struct memo *memo = &memos[memos_in_memory];
646 if (rb->kbd_input(memo->message, MAX_CHAR_MEMO_LEN) == 0)
648 if (memo->message[0])
650 memo->file_pointer_start = 0;
651 memo->file_pointer_end = 0;
652 memo->day = shown->mday;
653 memo->month = shown->mon;
654 memo->wday = shown->wday;
655 memo->year = shown->year;
656 memo->type = type;
657 if (save_memo(memos_in_memory, true, shown))
659 saved = true;
661 else
663 memo->file_pointer_start = 0;
664 memo->file_pointer_end = 0;
665 memo->day = 0;
666 memo->month = 0;
667 memo->year = 0;
668 memo->type = 0;
669 memo->wday = 0;
673 rb->lcd_clear_display();
674 if (saved)
675 rb->splash(HZ/2, "Event added");
676 else
677 rb->splash(HZ/2, "Event not added");
680 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
682 int i = (intptr_t)this_item;
683 if (action == ACTION_REQUEST_MENUITEM
684 && memos_in_shown_memory <= 0 && (i==0 || i==1))
685 return ACTION_EXIT_MENUITEM;
686 return action;
689 static bool edit_memo(int change, struct shown *shown)
691 bool exit = false;
692 int selected = 0;
694 static const struct opt_items modes[7] = {
695 { "Mon", -1 },
696 { "Tue", -1 },
697 { "Wed", -1 },
698 { "Thu", -1 },
699 { "Fri", -1 },
700 { "Sat", -1 },
701 { "Sun", -1 },
704 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
705 "Remove", "Edit",
706 "New Weekly", "New Monthly",
707 "New Yearly", "New One off",
708 "First Day of Week",
709 "Playback Control");
711 while (!exit)
713 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
715 case 0: /* remove */
716 save_memo(change, false, shown);
717 return false;
719 case 1: /* edit */
720 if(rb->kbd_input(memos[change].message,
721 MAX_CHAR_MEMO_LEN) == 0)
722 save_memo(change, true, shown);
723 return false;
725 case 2: /* weekly */
726 add_memo(shown, 0);
727 return false;
729 case 3: /* monthly */
730 add_memo(shown, 1);
731 return false;
733 case 4: /* yearly */
734 add_memo(shown, 2);
735 return false;
737 case 5: /* one off */
738 add_memo(shown, 3);
739 return false;
741 case 6: /* weekday */
742 rb->set_option("First Day of Week", &info.first_wday,
743 INT, modes, 7, NULL);
744 break;
746 case 7: /* playback control */
747 playback_control(NULL);
748 break;
750 case GO_TO_PREVIOUS:
751 return false;
753 case MENU_ATTACHED_USB:
754 been_in_usb_mode = true;
755 break;
758 return false;
761 static const char* get_event_text(int selected, void *data,
762 char *buffer, size_t buffer_len)
764 struct shown *shown = (struct shown *) data;
765 struct memo *memo;
766 if (selected < 0 || memos_in_shown_memory <= selected)
768 return NULL;
770 memo = &memos[pointer_array[selected]];
771 if (memo->type == 2)
772 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
773 memo->message, shown->year - memo->year);
774 else
775 rb->snprintf(buffer, buffer_len, "%s", memo->message);
776 return buffer;
779 static bool view_events(int selected, struct shown *shown)
781 struct gui_synclist gui_memos;
782 bool exit=false;
783 int button;
785 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
786 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
787 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
788 rb->gui_synclist_select_item(&gui_memos, selected);
789 rb->gui_synclist_draw(&gui_memos);
791 while (!exit)
793 button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
794 rb->gui_synclist_do_button(&gui_memos, &button, LIST_WRAP_UNLESS_HELD);
796 switch (button)
798 case ACTION_STD_OK:
799 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
800 return edit_memo(pointer_array[selected], shown);
801 break;
803 case ACTION_STD_CANCEL:
804 return false;
805 break;
807 default:
808 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
809 been_in_usb_mode = true;
810 break;
814 return false;
817 static void update_memos_shown(struct shown *shown)
819 int i;
820 struct memo *memo;
821 memos_in_shown_memory = 0;
822 for (i = 0; i < memos_in_memory; i++)
824 memo = &memos[i];
825 if (((memo->type >= 1) && (memo->day == shown->mday)) ||
826 ((memo->type < 1) && (memo->wday == shown->wday)))
827 pointer_array[memos_in_shown_memory++] = i;
831 static bool any_events(struct shown *shown, bool force)
833 update_memos_shown(shown);
835 if (memos_in_shown_memory > 0)
836 return view_events(0, shown);
837 else if (force)
838 return edit_memo(0, shown);
839 else
840 return false;
842 return false;
845 static void next_month(struct shown *shown, int step)
847 shown->mon++;
848 if (shown->mon > 12)
850 shown->mon = 1;
851 shown->year++;
852 leap_year = is_leap_year(shown->year);
854 if (step > 0)
855 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
856 else if (shown->mday > days_in_month[leap_year][shown->mon])
857 shown->mday = days_in_month[leap_year][shown->mon];
858 shown->firstday = shown->lastday;
859 load_memo(shown);
860 draw_calendar(shown);
863 static void prev_month(struct shown *shown, int step)
865 shown->mon--;
866 if (shown->mon < 1)
868 shown->mon = 12;
869 shown->year--;
870 leap_year = is_leap_year(shown->year);
872 if (step > 0)
873 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
874 else if (shown->mday > days_in_month[leap_year][shown->mon])
875 shown->mday = days_in_month[leap_year][shown->mon];
876 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
877 if (shown->firstday >= 7)
878 shown->firstday -= 7;
879 load_memo(shown);
880 draw_calendar(shown);
883 static void next_day(struct shown *shown, int step)
885 shown->mday += step;
886 if (shown->mday > days_in_month[leap_year][shown->mon])
887 next_month(shown, step);
888 else
889 draw_calendar(shown);
892 static void prev_day(struct shown *shown, int step)
894 shown->mday -= step;
895 if (shown->mday < 1)
896 prev_month(shown, step);
897 else
898 draw_calendar(shown);
901 enum plugin_status plugin_start(const void* parameter)
903 struct shown shown;
904 bool exit = false;
905 int button;
907 (void)(parameter);
909 configfile_load(CFG_FILE, config, ARRAYLEN(config), 0);
910 rb->memcpy(&old_info, &info, sizeof(struct info));
912 calendar_init(&shown);
913 load_memo(&shown);
914 any_events(&shown, false);
915 draw_calendar(&shown);
917 while (!exit)
919 button = rb->button_get(true);
920 switch (button)
922 case CALENDAR_QUIT:
923 exit = true;
924 break;
926 case CALENDAR_NEXT_MONTH:
927 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
928 next_month(&shown, 0);
929 break;
931 case CALENDAR_PREV_MONTH:
932 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
933 prev_month(&shown, 0);
934 break;
936 case CALENDAR_NEXT_WEEK:
937 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
938 next_day(&shown, 7);
939 break;
941 case CALENDAR_PREV_WEEK:
942 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
943 prev_day(&shown, 7);
944 break;
946 case CALENDAR_PREV_DAY:
947 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
948 prev_day(&shown, 1);
949 break;
951 case CALENDAR_NEXT_DAY:
952 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
953 next_day(&shown, 1);
954 break;
956 case CALENDAR_SELECT:
957 any_events(&shown, true);
958 draw_calendar(&shown);
959 break;
961 default:
962 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
963 been_in_usb_mode = true;
964 draw_calendar(&shown);
965 break;
970 #if (CONFIG_RTC == 0)
971 info.last_mon = shown.mon;
972 info.last_year = shown.year;
973 #endif
974 if (rb->memcmp(&old_info, &info, sizeof(struct info)))
975 configfile_save(CFG_FILE, config, ARRAYLEN(config), 0);
976 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;