Make kbd_input() show a cancel splash to indicate user abort better and for better...
[kugel-rb/myfork.git] / apps / plugins / calendar.c
blobcfb92909bfeb8721cec8aa1a60c4b69f2c3af662
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"
29 PLUGIN_HEADER
31 #if CONFIG_KEYPAD == RECORDER_PAD
32 #define CALENDAR_QUIT BUTTON_OFF
33 #define CALENDAR_SELECT BUTTON_PLAY
34 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
35 #define CALENDAR_PREV_WEEK BUTTON_UP
36 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
37 #define CALENDAR_PREV_DAY BUTTON_LEFT
38 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
39 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
41 #elif CONFIG_KEYPAD == ARCHOS_AV300_PAD
42 #define CALENDAR_QUIT BUTTON_OFF
43 #define CALENDAR_SELECT BUTTON_SELECT
44 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
45 #define CALENDAR_PREV_WEEK BUTTON_UP
46 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
47 #define CALENDAR_PREV_DAY BUTTON_LEFT
48 #define CALENDAR_NEXT_MONTH (BUTTON_ON|BUTTON_DOWN)
49 #define CALENDAR_PREV_MONTH (BUTTON_ON|BUTTON_UP)
51 #elif (CONFIG_KEYPAD == IRIVER_H100_PAD) || \
52 (CONFIG_KEYPAD == IRIVER_H300_PAD)
53 #define CALENDAR_QUIT BUTTON_OFF
54 #define CALENDAR_SELECT BUTTON_SELECT
55 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
56 #define CALENDAR_PREV_WEEK BUTTON_UP
57 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
58 #define CALENDAR_PREV_DAY BUTTON_LEFT
59 #define CALENDAR_NEXT_MONTH BUTTON_MODE
60 #define CALENDAR_PREV_MONTH BUTTON_REC
62 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || \
63 (CONFIG_KEYPAD == IPOD_3G_PAD) || \
64 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
65 #define CALENDAR_QUIT (BUTTON_SELECT|BUTTON_MENU)
66 #define CALENDAR_SELECT (BUTTON_SELECT|BUTTON_REL)
67 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
68 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
69 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
70 #define CALENDAR_PREV_DAY BUTTON_LEFT
71 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
72 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_REL)
74 #elif (CONFIG_KEYPAD == IAUDIO_X5M5_PAD)
75 #define CALENDAR_QUIT BUTTON_POWER
76 #define CALENDAR_SELECT BUTTON_SELECT
77 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
78 #define CALENDAR_PREV_WEEK BUTTON_UP
79 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
80 #define CALENDAR_PREV_DAY BUTTON_LEFT
81 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
82 #define CALENDAR_PREV_MONTH BUTTON_REC
84 #elif CONFIG_KEYPAD == GIGABEAT_PAD
85 #define CALENDAR_QUIT BUTTON_POWER
86 #define CALENDAR_SELECT BUTTON_SELECT
87 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
88 #define CALENDAR_PREV_WEEK BUTTON_UP
89 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
90 #define CALENDAR_PREV_DAY BUTTON_LEFT
91 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
92 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
94 #elif CONFIG_KEYPAD == SANSA_E200_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_FUZE_PAD
105 #define CALENDAR_QUIT (BUTTON_HOME|BUTTON_REPEAT)
106 #define CALENDAR_SELECT BUTTON_SELECT
107 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_FWD
108 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_BACK
109 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
110 #define CALENDAR_PREV_DAY BUTTON_LEFT
111 #define CALENDAR_NEXT_MONTH BUTTON_DOWN
112 #define CALENDAR_PREV_MONTH BUTTON_UP
114 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
115 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
116 CONFIG_KEYPAD == SANSA_M200_PAD
117 #define CALENDAR_QUIT BUTTON_POWER
118 #define CALENDAR_SELECT BUTTON_SELECT
119 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
120 #define CALENDAR_PREV_WEEK BUTTON_UP
121 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
122 #define CALENDAR_PREV_DAY BUTTON_LEFT
123 #define CALENDAR_NEXT_MONTH BUTTON_VOL_UP
124 #define CALENDAR_PREV_MONTH BUTTON_VOL_DOWN
126 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
127 #define CALENDAR_QUIT BUTTON_POWER
128 #define CALENDAR_SELECT BUTTON_PLAY
129 #define CALENDAR_NEXT_WEEK BUTTON_SCROLL_DOWN
130 #define CALENDAR_PREV_WEEK BUTTON_SCROLL_UP
131 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
132 #define CALENDAR_PREV_DAY BUTTON_LEFT
133 #define CALENDAR_NEXT_MONTH BUTTON_FF
134 #define CALENDAR_PREV_MONTH BUTTON_REW
136 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
137 #define CALENDAR_QUIT BUTTON_BACK
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_NEXT
144 #define CALENDAR_PREV_MONTH BUTTON_PREV
146 #elif CONFIG_KEYPAD == MROBE100_PAD
147 #define CALENDAR_QUIT BUTTON_POWER
148 #define CALENDAR_SELECT BUTTON_SELECT
149 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
150 #define CALENDAR_PREV_WEEK BUTTON_UP
151 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
152 #define CALENDAR_PREV_DAY BUTTON_LEFT
153 #define CALENDAR_NEXT_MONTH (BUTTON_MENU|BUTTON_DOWN)
154 #define CALENDAR_PREV_MONTH (BUTTON_MENU|BUTTON_UP)
156 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
157 #define CALENDAR_QUIT BUTTON_RC_REC
158 #define CALENDAR_SELECT BUTTON_RC_PLAY
159 #define CALENDAR_NEXT_WEEK BUTTON_RC_VOL_DOWN
160 #define CALENDAR_PREV_WEEK BUTTON_RC_VOL_UP
161 #define CALENDAR_NEXT_DAY BUTTON_RC_FF
162 #define CALENDAR_PREV_DAY BUTTON_RC_REW
163 #define CALENDAR_NEXT_MONTH BUTTON_RC_MODE
164 #define CALENDAR_PREV_MONTH BUTTON_RC_MENU
166 #elif (CONFIG_KEYPAD == COWOND2_PAD)
167 #define CALENDAR_QUIT BUTTON_POWER
168 #define CALENDAR_SELECT BUTTON_CENTER
169 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
170 #define CALENDAR_PREV_WEEK BUTTON_UP
171 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
172 #define CALENDAR_PREV_DAY BUTTON_LEFT
173 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
174 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
176 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
177 #define CALENDAR_QUIT BUTTON_POWER
178 #define CALENDAR_SELECT BUTTON_SELECT
179 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
180 #define CALENDAR_PREV_WEEK BUTTON_UP
181 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
182 #define CALENDAR_PREV_DAY BUTTON_LEFT
183 #define CALENDAR_NEXT_MONTH BUTTON_VOL_DOWN
184 #define CALENDAR_PREV_MONTH BUTTON_VOL_UP
186 #elif (CONFIG_KEYPAD == ONDAVX747_PAD)
187 #define CALENDAR_QUIT BUTTON_POWER
188 #define CALENDAR_SELECT BUTTON_MENU
189 #define CALENDAR_NEXT_WEEK BUTTON_VOL_DOWN
190 #define CALENDAR_PREV_WEEK BUTTON_VOL_UP
191 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
192 #define CALENDAR_PREV_DAY BUTTON_LEFT
193 #define CALENDAR_NEXT_MONTH BUTTON_BOTTOMRIGHT
194 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMLEFT
196 #elif CONFIG_KEYPAD == MROBE500_PAD
197 #define CALENDAR_QUIT BUTTON_POWER
199 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
200 #define CALENDAR_QUIT BUTTON_REC
201 #define CALENDAR_SELECT BUTTON_PLAY
202 #define CALENDAR_NEXT_WEEK BUTTON_DOWN
203 #define CALENDAR_PREV_WEEK BUTTON_UP
204 #define CALENDAR_NEXT_DAY BUTTON_RIGHT
205 #define CALENDAR_PREV_DAY BUTTON_LEFT
206 #define CALENDAR_NEXT_MONTH BUTTON_FFWD
207 #define CALENDAR_PREV_MONTH BUTTON_REW
209 #else
210 #error "No keypad setting."
211 #endif
213 #ifdef HAVE_TOUCHSCREEN
214 #ifndef CALENDAR_QUIT
215 #define CALENDAR_QUIT BUTTON_MIDLEFT
216 #endif
217 #ifndef CALENDAR_SELECT
218 #define CALENDAR_SELECT BUTTON_CENTER
219 #endif
220 #ifndef CALENDAR_NEXT_DAY
221 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
222 #endif
223 #ifndef CALENDAR_PREV_DAY
224 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
225 #endif
226 #ifndef CALENDAR_NEXT_WEEK
227 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
228 #endif
229 #ifndef CALENDAR_PREV_WEEK
230 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
231 #endif
232 #ifndef CALENDAR_NEXT_MONTH
233 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
234 #endif
235 #ifndef CALENDAR_PREV_MONTH
236 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
237 #endif
238 #endif
240 #define X_OFFSET ((LCD_WIDTH%7)/2)
241 #if LCD_HEIGHT <= 80
242 #define Y_OFFSET 1
243 #else
244 #define Y_OFFSET 4
245 #endif
246 #define CELL_WIDTH (LCD_WIDTH / 7)
247 #define CELL_HEIGHT (LCD_HEIGHT / 7)
249 static bool leap_year;
250 /* days_in_month[][0] is for December */
251 static const int days_in_month[2][13] = {
252 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
253 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
256 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
257 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
259 struct today {
260 int mday; /* day of the month */
261 int mon; /* month */
262 int year; /* year since 1900 */
263 int wday; /* day of the week */
266 struct shown {
267 int mday; /* day of the month */
268 int mon; /* month */
269 int year; /* year since 1900 */
270 int wday; /* day of the week */
271 int firstday; /* first (w)day of month */
272 int lastday; /* last (w)day of month */
275 static bool use_system_font = false;
277 static bool been_in_usb_mode = false;
279 /* leap year -- account for gregorian reformation in 1752 */
280 static int is_leap_year(int yr)
282 return ((yr) <= 1752 ? !((yr) % 4) : \
283 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
286 /* searches the weekday of the first day in month,
287 * relative to the given values */
288 static int calc_weekday( struct shown *shown )
290 return ( shown->wday + 36 - shown->mday ) % 7 ;
294 static void calendar_init(struct today *today, struct shown *shown)
296 int w,h;
297 #if CONFIG_RTC
298 struct tm *tm;
299 #else
300 (void)today;
301 #endif
302 rb->lcd_getstringsize("A",&w,&h);
303 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
305 rb->lcd_setfont(FONT_SYSFIXED);
306 use_system_font = true;
308 rb->lcd_clear_display();
309 #if CONFIG_RTC
310 tm = rb->get_time();
311 today->mon = tm->tm_mon +1;
312 today->year = 2000+tm->tm_year%100;
313 today->wday = tm->tm_wday-1;
314 today->mday = tm->tm_mday;
315 shown->mday = today->mday;
316 shown->mon = today->mon;
317 shown->year = today->year;
318 shown->wday = today->wday;
319 #endif
320 shown->firstday = calc_weekday(shown);
321 leap_year = is_leap_year(shown->year);
324 static void draw_headers(void)
326 int i,w,h;
327 int x = X_OFFSET;
328 const char **dayname = (const char**)&dayname_long;
330 for (i = 0; i < 7; i++)
332 rb->lcd_getstringsize(dayname[i],&w,&h);
333 if (w > CELL_WIDTH)
335 dayname = (const char**)&dayname_short;
336 break;
340 rb->lcd_getstringsize("A",&w,&h);
341 for (i = 0; i < 7; i++)
343 rb->lcd_putsxy(x, 0 , dayname[i]);
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 char buffer[9];
356 const char *monthname[] = {
357 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
358 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
360 if(use_system_font)
361 rb->lcd_setfont(FONT_SYSFIXED);
362 rb->lcd_getstringsize("A",&w,&h);
363 rb->lcd_clear_display();
364 draw_headers();
365 if (shown->firstday > 6)
366 shown->firstday -= 7;
367 pos = shown->firstday;
368 days_per_month = days_in_month[leap_year][shown->mon];
369 x = X_OFFSET + (pos * CELL_WIDTH);
370 y = Y_OFFSET + h;
371 for (j = 1; j <= days_per_month; j++)
373 if ( (day_has_memo[j]) || (wday_has_memo[pos]) )
374 rb->snprintf(buffer,4,"%02d.", j);
375 else
376 rb->snprintf(buffer,4,"%02d", j);
377 if (shown->mday == j)
379 rb->lcd_set_drawmode(DRMODE_SOLID);
380 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
381 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
382 shown->wday = pos;
384 else
386 rb->lcd_set_drawmode(DRMODE_SOLID);
388 rb->lcd_putsxy(x, y, buffer);
389 x += CELL_WIDTH;
390 pos++;
391 if (pos >= 7)
393 pos = 0;
394 x = X_OFFSET;
395 y += CELL_HEIGHT;
398 rb->lcd_set_drawmode(DRMODE_SOLID);
399 rb->lcd_vline(LCD_WIDTH-w*8-10,LCD_HEIGHT-h-3,LCD_HEIGHT-1);
400 rb->lcd_hline(LCD_WIDTH-w*8-10,LCD_WIDTH-1,LCD_HEIGHT-h-3);
401 rb->snprintf(buffer,9,"%s %04d",monthname[shown->mon-1],shown->year);
402 rb->lcd_putsxy(LCD_WIDTH-w*8-8,LCD_HEIGHT-h-1,buffer);
403 shown->lastday = pos;
404 rb->lcd_update();
407 #define MAX_CHAR_MEMO_LEN 64
408 #define MAX_MEMOS_IN_A_MONTH 128
409 struct memo {
410 char message[MAX_CHAR_MEMO_LEN];
411 int day;
412 int month;
413 int file_pointer_start;
414 int file_pointer_end;
415 int year;
416 int wday;
417 int type;
418 } memos[MAX_MEMOS_IN_A_MONTH];
419 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
420 static int memos_in_memory = 0;
421 static int memos_in_shown_memory = 0;
423 static void load_memo(struct shown *shown)
425 int k, fp;
426 bool exit = false;
427 char temp_memo1[2];
428 char temp_memo2[3];
429 char temp_memo4[5];
430 temp_memo1[1] = 0;
431 temp_memo2[2] = 0;
432 temp_memo4[4] = 0;
433 for (k = 1; k < 32; k++)
434 day_has_memo[k] = false;
435 for (k = 0; k < 7; k++)
436 wday_has_memo[k] = false;
437 memos_in_memory = 0;
438 fp = rb->open(ROCKBOX_DIR "/.memo",O_RDONLY);
439 if (fp > -1)
441 rb->lseek(fp, 0, SEEK_SET);
442 while (!exit)
444 bool load_to_memory;
445 rb->memset(&memos[memos_in_memory].message, 0, MAX_CHAR_MEMO_LEN);
446 memos[memos_in_memory].file_pointer_start =
447 rb->lseek(fp, 0, SEEK_CUR);
448 if (rb->read(fp, temp_memo2, 2) == 2)
449 memos[memos_in_memory].day = rb->atoi(&temp_memo2[0]);
450 else
451 memos[memos_in_memory].day = 0;
452 if (rb->read(fp, temp_memo2, 2) == 2)
453 memos[memos_in_memory].month = rb->atoi(&temp_memo2[0]);
454 else
455 memos[memos_in_memory].month = 0;
456 if (rb->read(fp, temp_memo4, 4) == 4)
457 memos[memos_in_memory].year = rb->atoi(&temp_memo4[0]);
458 else
459 memos[memos_in_memory].year = 0;
460 /* as the year returned is sometimes yearmonth, ie if yr should =
461 2003, and month = 06, then it returns 200306 */
462 if (memos[memos_in_memory].year > (shown->year * 10))
463 memos[memos_in_memory].year = (memos[memos_in_memory].year -
464 memos[memos_in_memory].month) /
465 100;
466 if (rb->read(fp, temp_memo1, 1) == 1)
467 memos[memos_in_memory].wday = rb->atoi(&temp_memo1[0]);
468 else
469 memos[memos_in_memory].wday = 0;
470 if (rb->read(fp, temp_memo1, 1) == 1)
471 memos[memos_in_memory].type = rb->atoi(&temp_memo1[0]);
472 else
473 memos[memos_in_memory].type = 0;
474 load_to_memory = ((memos[memos_in_memory].type < 2) ||
475 ((memos[memos_in_memory].type == 2) &&
476 (memos[memos_in_memory].month == shown->mon)) ||
477 ((memos[memos_in_memory].type > 2) &&
478 (memos[memos_in_memory].month == shown->mon) &&
479 (memos[memos_in_memory].year == shown->year)));
480 k = 0;
481 while (1)
483 if (rb->read(fp, temp_memo1, 1) != 1)
485 memos[memos_in_memory].day = 0;
486 memos[memos_in_memory].month = 0;
487 memos[memos_in_memory].file_pointer_start = 0;
488 memos[memos_in_memory].file_pointer_end = 0;
489 memos[memos_in_memory].year = 0;
490 memos[memos_in_memory].type = 0;
491 memos[memos_in_memory].wday = 0;
492 memos[memos_in_memory].message[0] = 0;
493 exit = true;
494 break;
496 if (load_to_memory)
498 if (temp_memo1[0] == '\n')
500 if (memos[memos_in_memory].type > 0)
501 day_has_memo[memos[memos_in_memory].day] =
502 true;
503 else
504 wday_has_memo[memos[memos_in_memory].wday] =
505 true;
506 memos[memos_in_memory++].file_pointer_end =
507 rb->lseek(fp, 0, SEEK_CUR);
509 else if ( (temp_memo1[0] != '\r') &&
510 (temp_memo1[0] != '\t') &&
511 k < MAX_CHAR_MEMO_LEN-1 )
512 memos[memos_in_memory].message[k++] = temp_memo1[0];
514 if (temp_memo1[0] == '\n')
515 break;
518 rb->close(fp);
522 static bool save_memo(int changed, bool new_mod, struct shown *shown)
524 int fp,fq;
525 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
526 fp = rb->open(ROCKBOX_DIR "/.memo", O_RDWR|O_CREAT);
527 fq = rb->creat(ROCKBOX_DIR "/~temp");
528 if ( (fq > -1) && (fp > -1) )
530 int i;
531 char temp[MAX_CHAR_MEMO_LEN];
532 rb->lseek(fp, 0, SEEK_SET);
533 for (i = 0; i < memos[changed].file_pointer_start; i++)
535 rb->read(fp, temp, 1);
536 rb->write(fq,temp,1);
538 if (new_mod)
540 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
541 memos[changed].day,
542 memos[changed].month,
543 memos[changed].year,
544 memos[changed].wday,
545 memos[changed].type,
546 memos[changed].message);
548 rb->lseek(fp, memos[changed].file_pointer_end, SEEK_SET);
549 while(rb->read(fp, temp, 1) == 1)
551 rb->write(fq,temp,1);
553 rb->close(fp);
554 rb->close(fq);
555 rb->remove(ROCKBOX_DIR "/.memo");
556 rb->rename(ROCKBOX_DIR "/~temp", ROCKBOX_DIR "/.memo");
557 load_memo(shown);
558 return true;
560 else if (fp > -1)
561 rb->close(fp);
562 else if (fq > -1)
563 rb->close(fq);
564 return false;
567 static void add_memo(struct shown *shown, int type)
569 bool saved = false;
570 if (rb->kbd_input(memos[memos_in_memory].message,
571 sizeof memos[memos_in_memory].message) == 0)
573 if (rb->strlen(memos[memos_in_memory].message))
575 memos[memos_in_memory].file_pointer_start = 0;
576 memos[memos_in_memory].file_pointer_end = 0;
577 memos[memos_in_memory].day = shown->mday;
578 memos[memos_in_memory].month = shown->mon;
579 memos[memos_in_memory].wday = shown->wday;
580 memos[memos_in_memory].year = shown->year;
581 memos[memos_in_memory].type = type;
582 if (save_memo(memos_in_memory,true,shown))
584 saved = true;
586 else
588 memos[memos_in_memory].file_pointer_start = 0;
589 memos[memos_in_memory].file_pointer_end = 0;
590 memos[memos_in_memory].day = 0;
591 memos[memos_in_memory].month = 0;
592 memos[memos_in_memory].year = 0;
593 memos[memos_in_memory].type = 0;
594 memos[memos_in_memory].wday = 0;
598 rb->lcd_clear_display();
599 if(use_system_font)
600 rb->lcd_setfont(FONT_SYSFIXED);
601 if (saved)
602 rb->splash(HZ/2,"Event added");
603 else
604 rb->splash(HZ/2,"Event not added");
607 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
609 int i = (intptr_t)this_item;
610 if (action == ACTION_REQUEST_MENUITEM
611 && memos_in_shown_memory <= 0 && (i==0 || i==1))
612 return ACTION_EXIT_MENUITEM;
613 return action;
616 static bool edit_memo(int change, struct shown *shown)
618 bool exit = false;
619 int selected = 0;
621 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
622 "Remove", "Edit",
623 "New Weekly", "New Monthly",
624 "New Yearly", "New One off",
625 "Playback Control");
627 while (!exit)
629 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
631 case 0: /* remove */
632 save_memo(pointer_array[change],false,shown);
633 return false;
635 case 1: /* edit */
636 if(rb->kbd_input(memos[pointer_array[change]].message,
637 sizeof memos[pointer_array[change]].message) == 0)
638 save_memo(pointer_array[change],true,shown);
639 return false;
641 case 2: /* weekly */
642 add_memo(shown,0);
643 return false;
645 case 3: /* monthly */
646 add_memo(shown,1);
647 return false;
649 case 4: /* yearly */
650 add_memo(shown,2);
651 return false;
653 case 5: /* one off */
654 add_memo(shown,3);
655 return false;
657 case 6: /* playback control */
658 playback_control(NULL);
659 break;
661 case GO_TO_PREVIOUS:
662 return false;
664 case MENU_ATTACHED_USB:
665 been_in_usb_mode = true;
666 break;
669 return false;
672 static char * get_event_text(int selected, void *data,
673 char *buffer, size_t buffer_len)
675 struct shown *shown = (struct shown *) data;
676 if (selected < 0 || memos_in_shown_memory <= selected)
678 return NULL;
680 if (memos[pointer_array[selected]].type == 2)
681 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
682 memos[pointer_array[selected]].message,
683 shown->year - memos[pointer_array[selected]].year);
684 else
685 rb->snprintf(buffer, buffer_len, "%s",
686 memos[pointer_array[selected]].message);
687 return buffer;
690 static bool view_events(int selected, struct shown *shown)
692 struct gui_synclist gui_memos;
693 bool exit=false;
694 int button;
696 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
697 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
698 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
699 rb->gui_synclist_select_item(&gui_memos, selected);
700 rb->gui_synclist_draw(&gui_memos);
702 while (!exit)
704 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
705 rb->gui_synclist_do_button(&gui_memos,&button,LIST_WRAP_UNLESS_HELD);
707 switch (button)
709 case ACTION_STD_OK:
710 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
711 return edit_memo(selected, shown);
712 break;
714 case ACTION_STD_CANCEL:
715 return false;
716 break;
718 default:
719 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
720 been_in_usb_mode = true;
721 break;
725 return false;
728 static void update_memos_shown(struct shown *shown)
730 int i;
731 memos_in_shown_memory = 0;
732 for (i = 0; i < memos_in_memory; i++)
733 if (((memos[i].type >= 1) &&
734 (memos[i].day == shown->mday)) ||
735 ((memos[i].type < 1) &&
736 (memos[i].wday == shown->wday)))
737 pointer_array[memos_in_shown_memory++] = i;
740 static bool any_events(struct shown *shown, bool force)
742 update_memos_shown(shown);
744 if (memos_in_shown_memory > 0)
745 return view_events(0, shown);
746 else if (force)
747 return edit_memo(0, shown);
748 else
749 return false;
751 return false;
754 static void next_month(struct shown *shown, int step)
756 shown->mon++;
757 if (shown->mon > 12)
759 shown->mon=1;
760 shown->year++;
761 leap_year = is_leap_year(shown->year);
763 if (step > 0)
764 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
765 else if (shown->mday > days_in_month[leap_year][shown->mon])
766 shown->mday = days_in_month[leap_year][shown->mon];
767 shown->firstday = shown->lastday;
768 load_memo(shown);
769 draw_calendar(shown);
772 static void prev_month(struct shown *shown, int step)
774 shown->mon--;
775 if (shown->mon < 1)
777 shown->mon = 12;
778 shown->year--;
779 leap_year = is_leap_year(shown->year);
781 if (step > 0)
782 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
783 else if (shown->mday > days_in_month[leap_year][shown->mon])
784 shown->mday = days_in_month[leap_year][shown->mon];
785 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
786 load_memo(shown);
787 draw_calendar(shown);
790 static void next_day(struct shown *shown, int step)
792 shown->mday += step;
793 if (shown->mday > days_in_month[leap_year][shown->mon])
794 next_month(shown, step);
795 else
796 draw_calendar(shown);
799 static void prev_day(struct shown *shown, int step)
801 shown->mday -= step;
802 if (shown->mday < 1)
803 prev_month(shown, step);
804 else
805 draw_calendar(shown);
808 enum plugin_status plugin_start(const void* parameter)
810 struct today today;
811 struct shown shown;
812 bool exit = false;
813 int button;
815 (void)(parameter);
817 calendar_init(&today, &shown);
818 load_memo(&shown);
819 any_events(&shown, false);
820 draw_calendar(&shown);
821 while (!exit)
823 button = rb->button_get(true);
824 switch (button)
826 case CALENDAR_QUIT:
827 return PLUGIN_OK;
829 case CALENDAR_NEXT_MONTH:
830 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
831 next_month(&shown, 0);
832 break;
834 case CALENDAR_PREV_MONTH:
835 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
836 prev_month(&shown, 0);
837 break;
839 case CALENDAR_NEXT_WEEK:
840 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
841 next_day(&shown, 7);
842 break;
844 case CALENDAR_PREV_WEEK:
845 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
846 prev_day(&shown, 7);
847 break;
849 case CALENDAR_PREV_DAY:
850 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
851 prev_day(&shown, 1);
852 break;
854 case CALENDAR_NEXT_DAY:
855 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
856 next_day(&shown, 1);
857 break;
859 case CALENDAR_SELECT:
860 any_events(&shown, true);
861 draw_calendar(&shown);
862 break;
864 default:
865 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
866 been_in_usb_mode = true;
867 draw_calendar(&shown);
868 break;
871 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;
874 #endif