FS#10649: calendar: make it selectable what day is first day of week.
[kugel-rb.git] / apps / plugins / calendar.c
blob4ac3128ab0e7af09589c89ae13acf16c20c74531
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 #if defined(HAVE_LCD_BITMAP) && (CONFIG_RTC != 0)
26 #include <timefuncs.h>
27 #include "lib/playback_control.h"
28 #include "lib/configfile.h"
30 PLUGIN_HEADER
32 #if CONFIG_KEYPAD == RECORDER_PAD
33 #define CALENDAR_QUIT BUTTON_OFF
34 #define CALENDAR_SELECT BUTTON_PLAY
35 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
36 #define CALENDAR_PREV_WEEK BUTTON_UP
37 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
38 #define CALENDAR_PREV_DAY BUTTON_LEFT
39 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
40 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
42 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
43 #define CALENDAR_QUIT BUTTON_OFF
44 #define CALENDAR_SELECT BUTTON_SELECT
45 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
46 #define CALENDAR_PREV_WEEK BUTTON_UP
47 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
48 #define CALENDAR_PREV_DAY BUTTON_LEFT
49 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
50 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
52 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
53 (CONFIG_KEYPAD == IRIVER_H300_PAD)
54 #define CALENDAR_QUIT BUTTON_OFF
55 #define CALENDAR_SELECT BUTTON_SELECT
56 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
57 #define CALENDAR_PREV_WEEK BUTTON_UP
58 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
59 #define CALENDAR_PREV_DAY BUTTON_LEFT
60 #define CALENDAR_NEXT_MONTH BUTTON_MODE
61 #define CALENDAR_PREV_MONTH BUTTON_REC
63 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
64 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
65 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
66 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
67 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_REL)
68 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
69 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
70 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
71 #define CALENDAR_PREV_DAY BUTTON_LEFT
72 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
73 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_REL)
75 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
76 #define CALENDAR_QUIT BUTTON_POWER
77 #define CALENDAR_SELECT BUTTON_SELECT
78 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
79 #define CALENDAR_PREV_WEEK BUTTON_UP
80 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
81 #define CALENDAR_PREV_DAY BUTTON_LEFT
82 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
83 #define CALENDAR_PREV_MONTH BUTTON_REC
85 #elif CONFIG_KEYPAD == GIGABEAT_PAD
86 #define CALENDAR_QUIT BUTTON_POWER
87 #define CALENDAR_SELECT BUTTON_SELECT
88 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
89 #define CALENDAR_PREV_WEEK BUTTON_UP
90 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
91 #define CALENDAR_PREV_DAY BUTTON_LEFT
92 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
93 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
95 #elif CONFIG_KEYPAD == SANSA_E200_PAD
96 #define CALENDAR_QUIT BUTTON_POWER
97 #define CALENDAR_SELECT BUTTON_SELECT
98 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
99 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
100 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
101 #define CALENDAR_PREV_DAY BUTTON_LEFT
102 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
103 #define CALENDAR_PREV_MONTH BUTTON_UP
105 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
106 #define CALENDAR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
107 #define CALENDAR_SELECT BUTTON_SELECT
108 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
109 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
110 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
111 #define CALENDAR_PREV_DAY BUTTON_LEFT
112 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
113 #define CALENDAR_PREV_MONTH BUTTON_UP
115 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
116 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
117 CONFIG_KEYPAD == SANSA_M200_PAD
118 #define CALENDAR_QUIT BUTTON_POWER
119 #define CALENDAR_SELECT BUTTON_SELECT
120 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
121 #define CALENDAR_PREV_WEEK BUTTON_UP
122 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
123 #define CALENDAR_PREV_DAY BUTTON_LEFT
124 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
125 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
127 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
128 #define CALENDAR_QUIT BUTTON_POWER
129 #define CALENDAR_SELECT BUTTON_PLAY
130 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
131 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
132 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
133 #define CALENDAR_PREV_DAY BUTTON_LEFT
134 #define CALENDAR_NEXT_MONTH BUTTON_FF
135 #define CALENDAR_PREV_MONTH BUTTON_REW
137 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
138 #define CALENDAR_QUIT BUTTON_BACK
139 #define CALENDAR_SELECT BUTTON_SELECT
140 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
141 #define CALENDAR_PREV_WEEK BUTTON_UP
142 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
143 #define CALENDAR_PREV_DAY BUTTON_LEFT
144 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
145 #define CALENDAR_PREV_MONTH BUTTON_PREV
147 #elif CONFIG_KEYPAD == MROBE100_PAD
148 #define CALENDAR_QUIT BUTTON_POWER
149 #define CALENDAR_SELECT BUTTON_SELECT
150 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
151 #define CALENDAR_PREV_WEEK BUTTON_UP
152 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
153 #define CALENDAR_PREV_DAY BUTTON_LEFT
154 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
155 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
157 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
158 #define CALENDAR_QUIT BUTTON_RC_REC
159 #define CALENDAR_SELECT BUTTON_RC_PLAY
160 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
161 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
162 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
163 #define CALENDAR_PREV_DAY BUTTON_RC_REW
164 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
165 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
167 #elif (CONFIG_KEYPAD == COWOND2_PAD)
168 #define CALENDAR_QUIT BUTTON_POWER
169 #define CALENDAR_SELECT BUTTON_CENTER
170 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
171 #define CALENDAR_PREV_WEEK BUTTON_UP
172 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
173 #define CALENDAR_PREV_DAY BUTTON_LEFT
174 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
175 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
177 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
178 #define CALENDAR_QUIT BUTTON_POWER
179 #define CALENDAR_SELECT BUTTON_SELECT
180 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
181 #define CALENDAR_PREV_WEEK BUTTON_UP
182 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
183 #define CALENDAR_PREV_DAY BUTTON_LEFT
184 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
185 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
187 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
188 #define CALENDAR_QUIT BUTTON_POWER
189 #define CALENDAR_SELECT BUTTON_MENU
190 #define CALENDAR_NEXT_WEEK BUTTON_VOL_DOWN
191 #define CALENDAR_PREV_WEEK BUTTON_VOL_UP
192 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
193 #define CALENDAR_PREV_DAY BUTTON_LEFT
194 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
195 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
197 #elif (CONFIG_KEYPAD == ONDAVX777_PAD)
198 #define CALENDAR_QUIT BUTTON_POWER
200 #elif CONFIG_KEYPAD == MROBE500_PAD
201 #define CALENDAR_QUIT BUTTON_POWER
203 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
204 #define CALENDAR_QUIT BUTTON_REC
205 #define CALENDAR_SELECT BUTTON_PLAY
206 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
207 #define CALENDAR_PREV_WEEK BUTTON_UP
208 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
209 #define CALENDAR_PREV_DAY BUTTON_LEFT
210 #define CALENDAR_NEXT_MONTH BUTTON_FFWD
211 #define CALENDAR_PREV_MONTH BUTTON_REW
213 #else
214 #error "No keypad setting."
215 #endif
217 #ifdef HAVE_TOUCHSCREEN
218 #ifndef CALENDAR_QUIT
219 #define CALENDAR_QUIT BUTTON_MIDLEFT
220 #endif
221 #ifndef CALENDAR_SELECT
222 #define CALENDAR_SELECT BUTTON_CENTER
223 #endif
224 #ifndef CALENDAR_NEXT_DAY
225 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
226 #endif
227 #ifndef CALENDAR_PREV_DAY
228 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
229 #endif
230 #ifndef CALENDAR_NEXT_WEEK
231 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
232 #endif
233 #ifndef CALENDAR_PREV_WEEK
234 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
235 #endif
236 #ifndef CALENDAR_NEXT_MONTH
237 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
238 #endif
239 #ifndef CALENDAR_PREV_MONTH
240 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
241 #endif
242 #endif
244 #define MEMO_FILE PLUGIN_APPS_DIR "/.memo"
245 #define TEMP_FILE PLUGIN_APPS_DIR "/~temp"
247 #define X_OFFSET ((LCD_WIDTH%7)/2)
248 #if LCD_HEIGHT <= 80
249 #define Y_OFFSET 1
250 #else
251 #define Y_OFFSET 4
252 #endif
253 #define CELL_WIDTH (LCD_WIDTH / 7)
254 #define CELL_HEIGHT (LCD_HEIGHT / 7)
256 #define CFG_FILE "calendar.cfg"
257 static int first_wday = 0, old_first_wday;
258 static struct configdata config[] = {
259 { TYPE_INT, 0, 6, { .int_p = &first_wday }, "first wday", NULL },
262 static bool leap_year;
263 /* days_in_month[][0] is for December */
264 static const int days_in_month[2][13] = {
265 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
266 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
269 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
270 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
272 struct shown {
273 int mday; /* day of the month */
274 int mon; /* month */
275 int year; /* year since 1900 */
276 int wday; /* day of the week */
277 int firstday; /* first (w)day of month */
278 int lastday; /* last (w)day of month */
281 static bool use_system_font = false;
283 static bool been_in_usb_mode = false;
285 /* leap year -- account for gregorian reformation in 1752 */
286 static int is_leap_year(int yr)
288 return ((yr) <= 1752 ? !((yr) % 4) : \
289 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
292 /* searches the weekday of the first day in month,
293 * relative to the given values */
294 static int calc_weekday( struct shown *shown )
296 return ( shown->wday + 36 - shown->mday ) % 7 ;
299 static void calendar_init(struct shown *shown)
301 int w, h;
302 #if CONFIG_RTC
303 struct tm *tm;
304 #endif
305 rb->lcd_getstringsize("A", &w, &h);
306 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
308 use_system_font = true;
310 #if CONFIG_RTC
311 tm = rb->get_time();
312 shown->mday = tm->tm_mday;
313 shown->mon = tm->tm_mon + 1;
314 shown->year = 2000 + (tm->tm_year%100);
315 shown->wday = tm->tm_wday - 1;
316 #endif
317 shown->firstday = calc_weekday(shown);
318 leap_year = is_leap_year(shown->year);
321 static void draw_headers(void)
323 int i, w, h;
324 int x = X_OFFSET;
325 int wday;
326 const char **dayname = dayname_long;
328 for (i = 0; i < 7; i++)
330 rb->lcd_getstringsize(dayname[i], &w, &h);
331 if (w > CELL_WIDTH)
333 dayname = dayname_short;
334 break;
338 wday = first_wday;
339 rb->lcd_getstringsize("A", &w, &h);
340 for (i = 0; i < 7; i++)
342 if (wday >= 7) wday = 0;
343 rb->lcd_putsxy(x, 0, dayname[wday++]);
344 x += CELL_WIDTH;
346 rb->lcd_hline(0, LCD_WIDTH-1, h);
349 static bool day_has_memo[32];
350 static bool wday_has_memo[7];
351 static void draw_calendar(struct shown *shown)
353 int w, h;
354 int x, y, pos, days_per_month, j;
355 int wday;
356 char buffer[12];
357 const char *monthname[] = {
358 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
359 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
361 if(use_system_font)
363 rb->lcd_setfont(FONT_SYSFIXED);
365 rb->lcd_getstringsize("A", &w, &h);
366 rb->lcd_clear_display();
367 draw_headers();
368 wday = shown->firstday;
369 pos = wday + 7 - first_wday;
370 if (pos >= 7) pos -= 7;
372 days_per_month = days_in_month[leap_year][shown->mon];
373 x = X_OFFSET + (pos * CELL_WIDTH);
374 y = Y_OFFSET + h;
375 for (j = 1; j <= days_per_month; j++)
377 if ( (day_has_memo[j]) || (wday_has_memo[wday]) )
378 rb->snprintf(buffer, 4, "%02d.", j);
379 else
380 rb->snprintf(buffer, 4, "%02d", j);
381 if (shown->mday == j)
383 rb->lcd_set_drawmode(DRMODE_SOLID);
384 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
385 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
386 shown->wday = wday;
388 else
390 rb->lcd_set_drawmode(DRMODE_SOLID);
392 rb->lcd_putsxy(x, y, buffer);
393 x += CELL_WIDTH;
394 wday++;
395 if (wday >= 7)
396 wday = 0;
397 pos++;
398 if (pos >= 7)
400 pos = 0;
401 x = X_OFFSET;
402 y += CELL_HEIGHT;
405 shown->lastday = wday;
406 rb->lcd_set_drawmode(DRMODE_SOLID);
407 rb->lcd_vline(LCD_WIDTH-w*8-10, LCD_HEIGHT-h-3, LCD_HEIGHT-1);
408 rb->lcd_hline(LCD_WIDTH-w*8-10, LCD_WIDTH-1, LCD_HEIGHT-h-3);
409 rb->snprintf(buffer, sizeof(buffer), "%s %04d",
410 monthname[shown->mon-1], shown->year);
411 rb->lcd_putsxy(LCD_WIDTH-w*8-8, LCD_HEIGHT-h-1, buffer);
412 rb->lcd_update();
415 #define MAX_CHAR_MEMO_LEN 64
416 #define MAX_MEMOS_IN_A_MONTH 128
417 struct memo {
418 char message[MAX_CHAR_MEMO_LEN];
419 int day;
420 int month;
421 int file_pointer_start;
422 int file_pointer_end;
423 int year;
424 int wday;
425 int type;
426 } memos[MAX_MEMOS_IN_A_MONTH];
427 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
428 static int memos_in_memory = 0;
429 static int memos_in_shown_memory = 0;
431 static void load_memo(struct shown *shown)
433 int k, fp;
434 bool exit = false;
435 char temp_memo1[2];
436 char temp_memo2[3];
437 char temp_memo4[5];
438 temp_memo1[1] = 0;
439 temp_memo2[2] = 0;
440 temp_memo4[4] = 0;
441 for (k = 1; k < 32; k++)
442 day_has_memo[k] = false;
443 for (k = 0; k < 7; k++)
444 wday_has_memo[k] = false;
445 memos_in_memory = 0;
446 fp = rb->open(MEMO_FILE, O_RDONLY);
447 if (fp > -1)
449 rb->lseek(fp, 0, SEEK_SET);
450 while (!exit)
452 bool load_to_memory;
453 struct memo *memo = &memos[memos_in_memory];
454 rb->memset(memo, 0, sizeof(*memo));
455 memo->file_pointer_start = rb->lseek(fp, 0, SEEK_CUR);
456 if (rb->read(fp, temp_memo2, 2) == 2)
457 memo->day = rb->atoi(temp_memo2);
458 if (rb->read(fp, temp_memo2, 2) == 2)
459 memo->month = rb->atoi(temp_memo2);
460 if (rb->read(fp, temp_memo4, 4) == 4)
461 memo->year = rb->atoi(temp_memo4);
462 if (rb->read(fp, temp_memo1, 1) == 1)
463 memo->wday = rb->atoi(temp_memo1);
464 if (rb->read(fp, temp_memo1, 1) == 1)
465 memo->type = rb->atoi(temp_memo1);
466 load_to_memory = ((memo->type < 2) ||
467 ((memo->type == 2) &&
468 (memo->month == shown->mon)) ||
469 ((memo->type > 2) &&
470 (memo->month == shown->mon) &&
471 (memo->year == shown->year)));
472 k = 0;
473 while (1)
475 if (rb->read(fp, temp_memo1, 1) != 1)
477 memo->day = 0;
478 memo->month = 0;
479 memo->file_pointer_start = 0;
480 memo->file_pointer_end = 0;
481 memo->year = 0;
482 memo->type = 0;
483 memo->wday = 0;
484 memo->message[0] = 0;
485 exit = true;
486 break;
488 if (load_to_memory)
490 if (temp_memo1[0] == '\n')
492 if (memo->type > 0)
493 day_has_memo[memo->day] = true;
494 else
495 wday_has_memo[memo->wday] = true;
496 memo->file_pointer_end = rb->lseek(fp, 0, SEEK_CUR);
497 memos_in_memory++;
499 else if ( (temp_memo1[0] != '\r') &&
500 (temp_memo1[0] != '\t') &&
501 k < MAX_CHAR_MEMO_LEN-1 )
502 memo->message[k++] = temp_memo1[0];
504 if (temp_memo1[0] == '\n')
505 break;
508 rb->close(fp);
512 static bool save_memo(int changed, bool new_mod, struct shown *shown)
514 int fp, fq;
515 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
516 fp = rb->open(MEMO_FILE, O_RDWR|O_CREAT);
517 fq = rb->creat(TEMP_FILE);
518 if ( (fq > -1) && (fp > -1) )
520 int i;
521 char temp[MAX_CHAR_MEMO_LEN];
522 struct memo *memo = &memos[changed];
523 rb->lseek(fp, 0, SEEK_SET);
524 for (i = 0; i < memo->file_pointer_start; i++)
526 rb->read(fp, temp, 1);
527 rb->write(fq, temp, 1);
529 if (new_mod)
531 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
532 memo->day, memo->month, memo->year, memo->wday,
533 memo->type, memo->message);
535 rb->lseek(fp, memo->file_pointer_end, SEEK_SET);
536 while(rb->read(fp, temp, 1) == 1)
538 rb->write(fq, temp, 1);
540 rb->close(fp);
541 rb->close(fq);
542 rb->remove(MEMO_FILE);
543 rb->rename(TEMP_FILE, MEMO_FILE);
544 load_memo(shown);
545 return true;
547 else if (fp > -1)
548 rb->close(fp);
549 else if (fq > -1)
550 rb->close(fq);
551 return false;
554 static void add_memo(struct shown *shown, int type)
556 bool saved = false;
557 struct memo *memo = &memos[memos_in_memory];
558 if (rb->kbd_input(memo->message, MAX_CHAR_MEMO_LEN) == 0)
560 if (memo->message[0])
562 memo->file_pointer_start = 0;
563 memo->file_pointer_end = 0;
564 memo->day = shown->mday;
565 memo->month = shown->mon;
566 memo->wday = shown->wday;
567 memo->year = shown->year;
568 memo->type = type;
569 if (save_memo(memos_in_memory, true, shown))
571 saved = true;
573 else
575 memo->file_pointer_start = 0;
576 memo->file_pointer_end = 0;
577 memo->day = 0;
578 memo->month = 0;
579 memo->year = 0;
580 memo->type = 0;
581 memo->wday = 0;
585 rb->lcd_clear_display();
586 if (saved)
587 rb->splash(HZ/2, "Event added");
588 else
589 rb->splash(HZ/2, "Event not added");
592 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
594 int i = (intptr_t)this_item;
595 if (action == ACTION_REQUEST_MENUITEM
596 && memos_in_shown_memory <= 0 && (i==0 || i==1))
597 return ACTION_EXIT_MENUITEM;
598 return action;
601 static bool edit_memo(int change, struct shown *shown)
603 bool exit = false;
604 int selected = 0;
606 static const struct opt_items modes[7] = {
607 { "Mon", -1 },
608 { "Tue", -1 },
609 { "Wed", -1 },
610 { "Thu", -1 },
611 { "Fri", -1 },
612 { "Sat", -1 },
613 { "Sun", -1 },
616 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
617 "Remove", "Edit",
618 "New Weekly", "New Monthly",
619 "New Yearly", "New One off",
620 "First Day of Week",
621 "Playback Control");
623 while (!exit)
625 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
627 case 0: /* remove */
628 save_memo(change, false, shown);
629 return false;
631 case 1: /* edit */
632 if(rb->kbd_input(memos[change].message,
633 MAX_CHAR_MEMO_LEN) == 0)
634 save_memo(change, true, shown);
635 return false;
637 case 2: /* weekly */
638 add_memo(shown, 0);
639 return false;
641 case 3: /* monthly */
642 add_memo(shown, 1);
643 return false;
645 case 4: /* yearly */
646 add_memo(shown, 2);
647 return false;
649 case 5: /* one off */
650 add_memo(shown, 3);
651 return false;
653 case 6: /* weekday */
654 rb->set_option("First Day of Week", &first_wday,
655 INT, modes, 7, NULL);
656 break;
658 case 7: /* playback control */
659 playback_control(NULL);
660 break;
662 case GO_TO_PREVIOUS:
663 return false;
665 case MENU_ATTACHED_USB:
666 been_in_usb_mode = true;
667 break;
670 return false;
673 static const char* get_event_text(int selected, void *data,
674 char *buffer, size_t buffer_len)
676 struct shown *shown = (struct shown *) data;
677 struct memo *memo;
678 if (selected < 0 || memos_in_shown_memory <= selected)
680 return NULL;
682 memo = &memos[pointer_array[selected]];
683 if (memo->type == 2)
684 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
685 memo->message, shown->year - memo->year);
686 else
687 rb->snprintf(buffer, buffer_len, "%s", memo->message);
688 return buffer;
691 static bool view_events(int selected, struct shown *shown)
693 struct gui_synclist gui_memos;
694 bool exit=false;
695 int button;
697 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
698 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
699 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
700 rb->gui_synclist_select_item(&gui_memos, selected);
701 rb->gui_synclist_draw(&gui_memos);
703 while (!exit)
705 button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
706 rb->gui_synclist_do_button(&gui_memos, &button, LIST_WRAP_UNLESS_HELD);
708 switch (button)
710 case ACTION_STD_OK:
711 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
712 return edit_memo(pointer_array[selected], shown);
713 break;
715 case ACTION_STD_CANCEL:
716 return false;
717 break;
719 default:
720 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
721 been_in_usb_mode = true;
722 break;
726 return false;
729 static void update_memos_shown(struct shown *shown)
731 int i;
732 struct memo *memo;
733 memos_in_shown_memory = 0;
734 for (i = 0; i < memos_in_memory; i++)
736 memo = &memos[i];
737 if (((memo->type >= 1) && (memo->day == shown->mday)) ||
738 ((memo->type < 1) && (memo->wday == shown->wday)))
739 pointer_array[memos_in_shown_memory++] = i;
743 static bool any_events(struct shown *shown, bool force)
745 update_memos_shown(shown);
747 if (memos_in_shown_memory > 0)
748 return view_events(0, shown);
749 else if (force)
750 return edit_memo(0, shown);
751 else
752 return false;
754 return false;
757 static void next_month(struct shown *shown, int step)
759 shown->mon++;
760 if (shown->mon > 12)
762 shown->mon = 1;
763 shown->year++;
764 leap_year = is_leap_year(shown->year);
766 if (step > 0)
767 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
768 else if (shown->mday > days_in_month[leap_year][shown->mon])
769 shown->mday = days_in_month[leap_year][shown->mon];
770 shown->firstday = shown->lastday;
771 load_memo(shown);
772 draw_calendar(shown);
775 static void prev_month(struct shown *shown, int step)
777 shown->mon--;
778 if (shown->mon < 1)
780 shown->mon = 12;
781 shown->year--;
782 leap_year = is_leap_year(shown->year);
784 if (step > 0)
785 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
786 else if (shown->mday > days_in_month[leap_year][shown->mon])
787 shown->mday = days_in_month[leap_year][shown->mon];
788 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
789 if (shown->firstday >= 7)
790 shown->firstday -= 7;
791 load_memo(shown);
792 draw_calendar(shown);
795 static void next_day(struct shown *shown, int step)
797 shown->mday += step;
798 if (shown->mday > days_in_month[leap_year][shown->mon])
799 next_month(shown, step);
800 else
801 draw_calendar(shown);
804 static void prev_day(struct shown *shown, int step)
806 shown->mday -= step;
807 if (shown->mday < 1)
808 prev_month(shown, step);
809 else
810 draw_calendar(shown);
813 enum plugin_status plugin_start(const void* parameter)
815 struct shown shown;
816 bool exit = false;
817 int button;
819 (void)(parameter);
821 configfile_load(CFG_FILE, config, 1, 0);
822 old_first_wday = first_wday;
824 calendar_init(&shown);
825 load_memo(&shown);
826 any_events(&shown, false);
827 draw_calendar(&shown);
829 while (!exit)
831 button = rb->button_get(true);
832 switch (button)
834 case CALENDAR_QUIT:
835 exit = true;
836 break;
838 case CALENDAR_NEXT_MONTH:
839 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
840 next_month(&shown, 0);
841 break;
843 case CALENDAR_PREV_MONTH:
844 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
845 prev_month(&shown, 0);
846 break;
848 case CALENDAR_NEXT_WEEK:
849 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
850 next_day(&shown, 7);
851 break;
853 case CALENDAR_PREV_WEEK:
854 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
855 prev_day(&shown, 7);
856 break;
858 case CALENDAR_PREV_DAY:
859 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
860 prev_day(&shown, 1);
861 break;
863 case CALENDAR_NEXT_DAY:
864 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
865 next_day(&shown, 1);
866 break;
868 case CALENDAR_SELECT:
869 any_events(&shown, true);
870 draw_calendar(&shown);
871 break;
873 default:
874 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
875 been_in_usb_mode = true;
876 draw_calendar(&shown);
877 break;
881 if (old_first_wday != first_wday)
882 configfile_save(CFG_FILE, config, 1, 0);
883 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;
886 #endif