Reverting parts of r19760 that was mistakenly committed.
[kugel-rb.git] / apps / plugins / calendar.c
blob866adceaee7ef64725cc040bbd45e6b1ea7671f1
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>
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 == IRIVER_H100_PAD) || \
51 (CONFIG_KEYPAD == IRIVER_H300_PAD)
52 #define CALENDAR_QUIT BUTTON_OFF
53 #define CALENDAR_SELECT BUTTON_SELECT
54 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
55 #define CALENDAR_PREV_WEEK BUTTON_UP
56 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
57 #define CALENDAR_PREV_DAY BUTTON_LEFT
58 #define CALENDAR_NEXT_MONTH BUTTON_MODE
59 #define CALENDAR_PREV_MONTH BUTTON_REC
61 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
62 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
63 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
64 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
65 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_REL)
66 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
67 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
68 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
69 #define CALENDAR_PREV_DAY BUTTON_LEFT
70 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
71 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_REL)
73 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
74 #define CALENDAR_QUIT BUTTON_POWER
75 #define CALENDAR_SELECT BUTTON_SELECT
76 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
77 #define CALENDAR_PREV_WEEK BUTTON_UP
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_REC
83 #elif CONFIG_KEYPAD == GIGABEAT_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_VOL_DOWN
91 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
93 #elif CONFIG_KEYPAD == SANSA_E200_PAD || \
94 CONFIG_KEYPAD == SANSA_FUZE_PAD
95 #define CALENDAR_QUIT BUTTON_POWER
96 #define CALENDAR_SELECT BUTTON_SELECT
97 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
98 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
99 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
100 #define CALENDAR_PREV_DAY BUTTON_LEFT
101 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
102 #define CALENDAR_PREV_MONTH BUTTON_UP
104 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
105 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
106 CONFIG_KEYPAD == SANSA_M200_PAD
107 #define CALENDAR_QUIT BUTTON_POWER
108 #define CALENDAR_SELECT BUTTON_SELECT
109 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
110 #define CALENDAR_PREV_WEEK BUTTON_UP
111 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
112 #define CALENDAR_PREV_DAY BUTTON_LEFT
113 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
114 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
116 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
117 #define CALENDAR_QUIT BUTTON_POWER
118 #define CALENDAR_SELECT BUTTON_PLAY
119 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
120 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
121 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
122 #define CALENDAR_PREV_DAY BUTTON_LEFT
123 #define CALENDAR_NEXT_MONTH BUTTON_FF
124 #define CALENDAR_PREV_MONTH BUTTON_REW
126 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
127 #define CALENDAR_QUIT BUTTON_BACK
128 #define CALENDAR_SELECT BUTTON_SELECT
129 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
130 #define CALENDAR_PREV_WEEK BUTTON_UP
131 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
132 #define CALENDAR_PREV_DAY BUTTON_LEFT
133 #define CALENDAR_NEXT_MONTH BUTTON_NEXT
134 #define CALENDAR_PREV_MONTH BUTTON_PREV
136 #elif CONFIG_KEYPAD == MROBE100_PAD
137 #define CALENDAR_QUIT BUTTON_POWER
138 #define CALENDAR_SELECT BUTTON_SELECT
139 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
140 #define CALENDAR_PREV_WEEK BUTTON_UP
141 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
142 #define CALENDAR_PREV_DAY BUTTON_LEFT
143 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
144 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
146 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
147 #define CALENDAR_QUIT BUTTON_RC_REC
148 #define CALENDAR_SELECT BUTTON_RC_PLAY
149 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
150 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
151 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
152 #define CALENDAR_PREV_DAY BUTTON_RC_REW
153 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
154 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
156 #elif (CONFIG_KEYPAD == COWOND2_PAD)
157 #define CALENDAR_QUIT BUTTON_POWER
158 #define CALENDAR_SELECT BUTTON_CENTER
159 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
160 #define CALENDAR_PREV_WEEK BUTTON_UP
161 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
162 #define CALENDAR_PREV_DAY BUTTON_LEFT
163 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
164 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
166 #else
167 #error "No keypad setting."
168 #endif
170 #define X_OFFSET ((LCD_WIDTH%7)/2)
171 #if LCD_HEIGHT <= 80
172 #define Y_OFFSET 1
173 #else
174 #define Y_OFFSET 4
175 #endif
176 #define CELL_WIDTH (LCD_WIDTH / 7)
177 #define CELL_HEIGHT (LCD_HEIGHT / 7)
179 static const struct plugin_api* rb;
181 MEM_FUNCTION_WRAPPERS(rb)
183 static bool leap_year;
184 /* days_in_month[][0] is for December */
185 static const int days_in_month[2][13] = {
186 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
187 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
190 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
191 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
193 struct today {
194 int mday; /* day of the month */
195 int mon; /* month */
196 int year; /* year since 1900 */
197 int wday; /* day of the week */
200 struct shown {
201 int mday; /* day of the month */
202 int mon; /* month */
203 int year; /* year since 1900 */
204 int wday; /* day of the week */
205 int firstday; /* first (w)day of month */
206 int lastday; /* last (w)day of month */
209 static bool use_system_font = false;
211 static bool been_in_usb_mode = false;
213 /* leap year -- account for gregorian reformation in 1752 */
214 static int is_leap_year(int yr)
216 return ((yr) <= 1752 ? !((yr) % 4) : \
217 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
220 /* searches the weekday of the first day in month,
221 * relative to the given values */
222 static int calc_weekday( struct shown *shown )
224 return ( shown->wday + 36 - shown->mday ) % 7 ;
228 static void calendar_init(struct today *today, struct shown *shown)
230 int w,h;
231 #if CONFIG_RTC
232 struct tm *tm;
233 #else
234 (void)today;
235 #endif
236 rb->lcd_getstringsize("A",&w,&h);
237 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
239 rb->lcd_setfont(FONT_SYSFIXED);
240 use_system_font = true;
242 rb->lcd_clear_display();
243 #if CONFIG_RTC
244 tm = rb->get_time();
245 today->mon = tm->tm_mon +1;
246 today->year = 2000+tm->tm_year%100;
247 today->wday = tm->tm_wday-1;
248 today->mday = tm->tm_mday;
249 shown->mday = today->mday;
250 shown->mon = today->mon;
251 shown->year = today->year;
252 shown->wday = today->wday;
253 #endif
254 shown->firstday = calc_weekday(shown);
255 leap_year = is_leap_year(shown->year);
258 static void draw_headers(void)
260 int i,w,h;
261 int x = X_OFFSET;
262 const char **dayname = (const char**)&dayname_long;
264 for (i = 0; i < 7; i++)
266 rb->lcd_getstringsize(dayname[i],&w,&h);
267 if (w > CELL_WIDTH)
269 dayname = (const char**)&dayname_short;
270 break;
274 rb->lcd_getstringsize("A",&w,&h);
275 for (i = 0; i < 7; i++)
277 rb->lcd_putsxy(x, 0 , dayname[i]);
278 x += CELL_WIDTH;
280 rb->lcd_hline(0, LCD_WIDTH-1 ,h);
283 static bool day_has_memo[32];
284 static bool wday_has_memo[7];
285 static void draw_calendar(struct shown *shown)
287 int w,h;
288 int x,y,pos,days_per_month,j;
289 char buffer[9];
290 const char *monthname[] = {
291 "Jan",
292 "Feb",
293 "Mar",
294 "Apr",
295 "May",
296 "Jun",
297 "Jul",
298 "Aug",
299 "Sep",
300 "Oct",
301 "Nov",
302 "Dec"
304 if(use_system_font)
305 rb->lcd_setfont(FONT_SYSFIXED);
306 rb->lcd_getstringsize("A",&w,&h);
307 rb->lcd_clear_display();
308 draw_headers();
309 if (shown->firstday > 6)
310 shown->firstday -= 7;
311 pos = shown->firstday;
312 days_per_month = days_in_month[leap_year][shown->mon];
313 x = X_OFFSET + (pos * CELL_WIDTH);
314 y = Y_OFFSET + h;
315 for (j = 1; j <= days_per_month; j++)
317 if ( (day_has_memo[j]) || (wday_has_memo[pos]) )
318 rb->snprintf(buffer,4,"%02d.", j);
319 else
320 rb->snprintf(buffer,4,"%02d", j);
321 if (shown->mday == j)
323 rb->lcd_set_drawmode(DRMODE_SOLID);
324 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
325 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
326 shown->wday = pos;
328 else
330 rb->lcd_set_drawmode(DRMODE_SOLID);
332 rb->lcd_putsxy(x, y, buffer);
333 x += CELL_WIDTH;
334 pos++;
335 if (pos >= 7)
337 pos = 0;
338 x = X_OFFSET;
339 y += CELL_HEIGHT;
342 rb->lcd_set_drawmode(DRMODE_SOLID);
343 rb->lcd_vline(LCD_WIDTH-w*8-10,LCD_HEIGHT-h-3,LCD_HEIGHT-1);
344 rb->lcd_hline(LCD_WIDTH-w*8-10,LCD_WIDTH-1,LCD_HEIGHT-h-3);
345 rb->snprintf(buffer,9,"%s %04d",monthname[shown->mon-1],shown->year);
346 rb->lcd_putsxy(LCD_WIDTH-w*8-8,LCD_HEIGHT-h-1,buffer);
347 shown->lastday = pos;
348 rb->lcd_update();
351 #define MAX_CHAR_MEMO_LEN 64
352 #define MAX_MEMOS_IN_A_MONTH 128
353 struct memo {
354 char message[MAX_CHAR_MEMO_LEN];
355 int day;
356 int month;
357 int file_pointer_start;
358 int file_pointer_end;
359 int year;
360 int wday;
361 int type;
362 } memos[MAX_MEMOS_IN_A_MONTH];
363 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
364 static int memos_in_memory = 0;
365 static int memos_in_shown_memory = 0;
367 static void load_memo(struct shown *shown)
369 int i, k, fp;
370 bool exit = false;
371 char temp_memo1[2];
372 char temp_memo2[3];
373 char temp_memo4[5];
374 temp_memo1[1] = 0;
375 temp_memo2[2] = 0;
376 temp_memo4[4] = 0;
377 for (k = 0; k < memos_in_memory; k++)
379 memos[k].day = 0;
380 memos[k].month = 0;
381 memos[k].file_pointer_start = 0;
382 memos[k].file_pointer_end = 0;
383 memos[k].year = 0;
384 memos[k].type = 0;
385 memos[k].wday = 0;
386 for (i = 0; i < MAX_CHAR_MEMO_LEN; i++)
387 memos[k].message[i] = 0;
389 for (k = 1; k < 32; k++)
390 day_has_memo[k] = false;
391 for (k = 0; k < 7; k++)
392 wday_has_memo[k] = false;
393 memos_in_memory = 0;
394 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY);
395 if (fp > -1)
397 int count = rb->filesize(fp);
398 rb->lseek(fp, 0, SEEK_SET);
399 while (!exit)
401 memos[memos_in_memory].file_pointer_start = rb->lseek(fp, 0,
402 SEEK_CUR);
403 if (rb->read(fp, temp_memo2, 2) == 2)
404 memos[memos_in_memory].day = rb->atoi(&temp_memo2[0]);
405 else
406 memos[memos_in_memory].day = 0;
407 if (rb->read(fp, temp_memo2, 2) == 2)
408 memos[memos_in_memory].month = rb->atoi(&temp_memo2[0]);
409 else
410 memos[memos_in_memory].month = 0;
411 if (rb->read(fp, temp_memo4, 4) == 4)
412 memos[memos_in_memory].year = rb->atoi(&temp_memo4[0]);
413 else
414 memos[memos_in_memory].year = 0;
415 /* as the year returned is sometimes yearmonth, ie if yr should =
416 2003, and month = 06, then it returns 200306 */
417 if (memos[memos_in_memory].year > (shown->year * 10))
418 memos[memos_in_memory].year = (memos[memos_in_memory].year -
419 memos[memos_in_memory].month) /
420 100;
421 if (rb->read(fp, temp_memo1, 1) == 1)
422 memos[memos_in_memory].wday = rb->atoi(&temp_memo1[0]);
423 else
424 memos[memos_in_memory].wday = 0;
425 if (rb->read(fp, temp_memo1, 1) == 1)
426 memos[memos_in_memory].type = rb->atoi(&temp_memo1[0]);
427 else
428 memos[memos_in_memory].type = 0;
429 for (k = 0; k <= count; k++)
431 if (rb->read(fp, temp_memo1, 1) == 1)
433 if (
434 (memos[memos_in_memory].type < 2)
437 (memos[memos_in_memory].type == 2)
439 (memos[memos_in_memory].month == shown->mon)
443 (memos[memos_in_memory].type > 2)
445 (memos[memos_in_memory].month == shown->mon)
447 (memos[memos_in_memory].year == shown->year)
451 if (temp_memo1[0] == '\n')
453 if (memos[memos_in_memory].type > 0)
454 day_has_memo[memos[memos_in_memory].day] =
455 true;
456 else
457 wday_has_memo[memos[memos_in_memory].wday] =
458 true;
459 memos[memos_in_memory++].file_pointer_end =
460 rb->lseek(fp, 0, SEEK_CUR);
462 else if ( (temp_memo1[0] != '\r') &&
463 (temp_memo1[0] != '\t') &&
464 k < MAX_CHAR_MEMO_LEN-1 )
465 memos[memos_in_memory].message[k] = temp_memo1[0];
467 if (temp_memo1[0] == '\n')
468 break;
470 else
472 memos[memos_in_memory].day = 0;
473 memos[memos_in_memory].month = 0;
474 memos[memos_in_memory].file_pointer_start = 0;
475 memos[memos_in_memory].file_pointer_end = 0;
476 memos[memos_in_memory].year = 0;
477 memos[memos_in_memory].type = 0;
478 memos[memos_in_memory].wday = 0;
479 memos[memos_in_memory].message[0] = 0;
480 exit = true;
481 break;
486 rb->close(fp);
489 static bool save_memo(int changed, bool new_mod, struct shown *shown)
491 int fp,fq;
492 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY | O_CREAT);
493 fq = rb->creat(ROCKBOX_DIR "/~temp");
494 if ( (fq != -1) && (fp != -1) )
496 int i;
497 char temp[MAX_CHAR_MEMO_LEN];
498 rb->lseek(fp, 0, SEEK_SET);
499 for (i = 0; i < memos[changed].file_pointer_start; i++)
501 rb->read(fp, temp, 1);
502 rb->write(fq,temp,1);
504 if (new_mod)
506 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
507 memos[changed].day,
508 memos[changed].month,
509 memos[changed].year,
510 memos[changed].wday,
511 memos[changed].type,
512 memos[changed].message);
514 rb->lseek(fp, memos[changed].file_pointer_end, SEEK_SET);
515 while(rb->read(fp, temp, 1) == 1)
517 rb->write(fq,temp,1);
519 rb->close(fp);
520 rb->close(fq);
521 rb->remove(ROCKBOX_DIR "/.memo");
522 rb->rename(ROCKBOX_DIR "/~temp", ROCKBOX_DIR "/.memo");
523 load_memo(shown);
524 return true;
526 else if (fp != -1)
527 rb->close(fp);
528 else if (fq != -1)
529 rb->close(fq);
530 return false;
533 static void add_memo(struct shown *shown, int type)
535 bool saved = false;
536 if (rb->kbd_input(memos[memos_in_memory].message,
537 sizeof memos[memos_in_memory].message) != -1)
539 if (rb->strlen(memos[memos_in_memory].message))
541 memos[memos_in_memory].file_pointer_start = 0;
542 memos[memos_in_memory].file_pointer_end = 0;
543 memos[memos_in_memory].day = shown->mday;
544 memos[memos_in_memory].month = shown->mon;
545 memos[memos_in_memory].wday = shown->wday;
546 memos[memos_in_memory].year = shown->year;
547 memos[memos_in_memory].type = type;
548 if (save_memo(memos_in_memory,true,shown))
550 saved = true;
552 else
554 memos[memos_in_memory].file_pointer_start = 0;
555 memos[memos_in_memory].file_pointer_end = 0;
556 memos[memos_in_memory].day = 0;
557 memos[memos_in_memory].month = 0;
558 memos[memos_in_memory].year = 0;
559 memos[memos_in_memory].type = 0;
560 memos[memos_in_memory].wday = 0;
564 rb->lcd_clear_display();
565 if(use_system_font)
566 rb->lcd_setfont(FONT_SYSFIXED);
567 if (saved)
568 rb->splash(HZ/2,"Event added");
569 else
570 rb->splash(HZ/2,"Event not added");
573 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
575 (void) this_item;
576 if (action == ACTION_REQUEST_MENUITEM && memos_in_shown_memory <= 0)
577 return ACTION_EXIT_MENUITEM;
578 return action;
581 static bool edit_memo(int change, struct shown *shown)
583 bool exit = false;
584 int selected = 0;
586 MENUITEM_RETURNVALUE(edit_menu_remove, "Remove", 0,
587 edit_menu_cb, Icon_NOICON);
588 MENUITEM_RETURNVALUE(edit_menu_edit, "Edit", 1,
589 edit_menu_cb, Icon_NOICON);
590 MENUITEM_RETURNVALUE(edit_menu_weekly, "New Weekly", 2,
591 NULL, Icon_NOICON);
592 MENUITEM_RETURNVALUE(edit_menu_monthly, "New Monthly", 3,
593 NULL, Icon_NOICON);
594 MENUITEM_RETURNVALUE(edit_menu_yearly, "New Yearly", 4,
595 NULL, Icon_NOICON);
596 MENUITEM_RETURNVALUE(edit_menu_oneoff, "New One off", 5,
597 NULL, Icon_NOICON);
599 MAKE_MENU(edit_menu, "Edit menu",
600 NULL, Icon_NOICON,
601 &edit_menu_remove, &edit_menu_edit,
602 &edit_menu_weekly, &edit_menu_monthly,
603 &edit_menu_yearly, &edit_menu_oneoff);
605 while (!exit)
607 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
609 case 0: /* remove */
610 save_memo(pointer_array[change],false,shown);
611 return false;
613 case 1: /* edit */
614 if(rb->kbd_input(memos[pointer_array[change]].message,
615 sizeof memos[pointer_array[change]].message) != -1)
616 save_memo(pointer_array[change],true,shown);
617 return false;
619 case 2: /* weekly */
620 add_memo(shown,0);
621 return false;
623 case 3: /* monthly */
624 add_memo(shown,1);
625 return false;
627 case 4: /* yearly */
628 add_memo(shown,2);
629 return false;
631 case 5: /* one off */
632 add_memo(shown,3);
633 return false;
635 case GO_TO_PREVIOUS:
636 return false;
638 case MENU_ATTACHED_USB:
639 been_in_usb_mode = true;
640 break;
643 return false;
646 static char * get_event_text(int selected, void *data,
647 char *buffer, size_t buffer_len)
649 struct shown *shown = (struct shown *) data;
650 if (selected < 0 || memos_in_shown_memory <= selected)
652 return NULL;
654 if (memos[pointer_array[selected]].type == 2)
655 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
656 memos[pointer_array[selected]].message,
657 shown->year - memos[pointer_array[selected]].year);
658 else
659 rb->snprintf(buffer, buffer_len, "%s",
660 memos[pointer_array[selected]].message);
661 return buffer;
664 static bool view_events(int selected, struct shown *shown)
666 struct gui_synclist gui_memos;
667 bool exit=false;
668 int button;
670 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
671 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
672 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
673 rb->gui_synclist_select_item(&gui_memos, selected);
674 rb->gui_synclist_draw(&gui_memos);
676 while (!exit)
678 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
679 rb->gui_synclist_do_button(&gui_memos,&button,LIST_WRAP_UNLESS_HELD);
681 switch (button)
683 case ACTION_STD_OK:
684 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
685 return edit_memo(selected, shown);
686 break;
688 case ACTION_STD_CANCEL:
689 return false;
690 break;
692 default:
693 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
694 been_in_usb_mode = true;
695 break;
699 return false;
702 static void update_memos_shown(struct shown *shown)
704 int i;
705 memos_in_shown_memory = 0;
706 for (i = 0; i < memos_in_memory; i++)
707 if (
709 (memos[i].type >= 1)
711 (memos[i].day == shown->mday)
715 (memos[i].type < 1)
717 (memos[i].wday == shown->wday)
720 pointer_array[memos_in_shown_memory++] = i;
723 static bool any_events(struct shown *shown, bool force)
725 update_memos_shown(shown);
727 if (memos_in_shown_memory > 0)
728 return view_events(0, shown);
729 else if (force)
730 return edit_memo(0, shown);
731 else
732 return false;
734 return false;
737 static void next_month(struct shown *shown, int step)
739 shown->mon++;
740 if (shown->mon > 12)
742 shown->mon=1;
743 shown->year++;
744 leap_year = is_leap_year(shown->year);
746 if (step > 0)
747 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
748 else if (shown->mday > days_in_month[leap_year][shown->mon])
749 shown->mday = days_in_month[leap_year][shown->mon];
750 shown->firstday = shown->lastday;
751 load_memo(shown);
752 draw_calendar(shown);
755 static void prev_month(struct shown *shown, int step)
757 shown->mon--;
758 if (shown->mon < 1)
760 shown->mon = 12;
761 shown->year--;
762 leap_year = is_leap_year(shown->year);
764 if (step > 0)
765 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
766 else if (shown->mday > days_in_month[leap_year][shown->mon])
767 shown->mday = days_in_month[leap_year][shown->mon];
768 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
769 load_memo(shown);
770 draw_calendar(shown);
773 static void next_day(struct shown *shown, int step)
775 shown->mday += step;
776 if (shown->mday > days_in_month[leap_year][shown->mon])
777 next_month(shown, step);
778 else
779 draw_calendar(shown);
782 static void prev_day(struct shown *shown, int step)
784 shown->mday -= step;
785 if (shown->mday < 1)
786 prev_month(shown, step);
787 else
788 draw_calendar(shown);
791 enum plugin_status plugin_start(const struct plugin_api* api, const void* parameter)
793 struct today today;
794 struct shown shown;
795 bool exit = false;
796 int button;
798 (void)(parameter);
800 rb = api;
802 calendar_init(&today, &shown);
803 load_memo(&shown);
804 any_events(&shown, false);
805 draw_calendar(&shown);
806 while (!exit)
808 button = rb->button_get(true);
809 switch (button)
811 case CALENDAR_QUIT:
812 return PLUGIN_OK;
814 case CALENDAR_NEXT_MONTH:
815 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
816 next_month(&shown, 0);
817 break;
819 case CALENDAR_PREV_MONTH:
820 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
821 prev_month(&shown, 0);
822 break;
824 case CALENDAR_NEXT_WEEK:
825 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
826 next_day(&shown, 7);
827 break;
829 case CALENDAR_PREV_WEEK:
830 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
831 prev_day(&shown, 7);
832 break;
834 case CALENDAR_PREV_DAY:
835 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
836 prev_day(&shown, 1);
837 break;
839 case CALENDAR_NEXT_DAY:
840 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
841 next_day(&shown, 1);
842 break;
844 case CALENDAR_SELECT:
845 any_events(&shown, true);
846 draw_calendar(&shown);
847 break;
849 default:
850 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
851 been_in_usb_mode = true;
852 draw_calendar(&shown);
853 break;
856 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;
859 #endif