cast ssize_t to long
[kugel-rb.git] / apps / plugins / calendar.c
blob13abdfc29a946d5da96626576d4e7c9e889b7e6b
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 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
262 #define CALENDAR_QUIT (BUTTON_REC|BUTTON_PLAY)
263 #define CALENDAR_SELECT BUTTON_SELECT
264 #define CALENDAR_NEXT_WEEK BUTTON_NEXT
265 #define CALENDAR_PREV_WEEK BUTTON_PREV
266 #define CALENDAR_NEXT_DAY BUTTON_VOL_UP
267 #define CALENDAR_PREV_DAY BUTTON_VOL_DOWN
268 #define CALENDAR_NEXT_MONTH BUTTON_PLAY
269 #define CALENDAR_PREV_MONTH BUTTON_REC
271 #else
272 #error "No keypad setting."
273 #endif
275 #ifdef HAVE_TOUCHSCREEN
276 #ifndef CALENDAR_QUIT
277 #define CALENDAR_QUIT BUTTON_MIDLEFT
278 #endif
279 #ifndef CALENDAR_SELECT
280 #define CALENDAR_SELECT BUTTON_CENTER
281 #endif
282 #ifndef CALENDAR_NEXT_DAY
283 #define CALENDAR_NEXT_DAY BUTTON_TOPLEFT
284 #endif
285 #ifndef CALENDAR_PREV_DAY
286 #define CALENDAR_PREV_DAY BUTTON_BOTTOMLEFT
287 #endif
288 #ifndef CALENDAR_NEXT_WEEK
289 #define CALENDAR_NEXT_WEEK BUTTON_TOPMIDDLE
290 #endif
291 #ifndef CALENDAR_PREV_WEEK
292 #define CALENDAR_PREV_WEEK BUTTON_BOTTOMMIDDLE
293 #endif
294 #ifndef CALENDAR_NEXT_MONTH
295 #define CALENDAR_NEXT_MONTH BUTTON_TOPRIGHT
296 #endif
297 #ifndef CALENDAR_PREV_MONTH
298 #define CALENDAR_PREV_MONTH BUTTON_BOTTOMRIGHT
299 #endif
300 #endif
302 #define MEMO_FILE PLUGIN_APPS_DIR "/.memo"
303 #define TEMP_FILE PLUGIN_APPS_DIR "/~temp"
305 #define X_OFFSET ((LCD_WIDTH%7)/2)
306 #if LCD_HEIGHT <= 80
307 #define Y_OFFSET 1
308 #else
309 #define Y_OFFSET 4
310 #endif
311 #define CELL_WIDTH (LCD_WIDTH / 7)
312 #define CELL_HEIGHT (LCD_HEIGHT / 7)
314 #define CFG_FILE "calendar.cfg"
315 struct info {
316 int first_wday;
317 #if (CONFIG_RTC == 0)
318 int last_mon;
319 int last_year;
320 #endif
322 static struct info info = { .first_wday = 0 }, old_info;
323 static struct configdata config[] = {
324 { TYPE_INT, 0, 6, { .int_p = &(info.first_wday) }, "first wday", NULL },
325 #if (CONFIG_RTC == 0)
326 { TYPE_INT, 1, 12, { .int_p = &(info.last_mon) }, "last mon", NULL },
327 { TYPE_INT, 1, 3000, { .int_p = &(info.last_year) }, "last year", NULL },
328 #endif
331 static bool leap_year;
332 /* days_in_month[][0] is for December */
333 static const int days_in_month[2][13] = {
334 {31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
335 {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
338 static const char *dayname_long[7] = {"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
339 static const char *dayname_short[7] = {"M","T","W","T","F","S","S"};
341 struct shown {
342 int mday; /* day of the month */
343 int mon; /* month */
344 int year; /* year since 1900 */
345 int wday; /* day of the week */
346 int firstday; /* first (w)day of month */
347 int lastday; /* last (w)day of month */
350 static bool use_system_font = false;
352 static bool been_in_usb_mode = false;
354 /* leap year -- account for gregorian reformation in 1752 */
355 static int is_leap_year(int yr)
357 return ((yr) <= 1752 ? !((yr) % 4) : \
358 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) ? 1:0 ;
361 /* searches the weekday of the first day in month,
362 * relative to the given values */
363 static int calc_weekday( struct shown *shown )
365 return ( shown->wday + 36 - shown->mday ) % 7 ;
368 #if (CONFIG_RTC == 0)
369 /* from timefunc.c */
370 static void my_set_day_of_week(struct shown *shown)
372 int y = shown->year;
373 int d = shown->mday;
374 int m = shown->mon-1;
375 static const char mo[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
377 if(m == 0 || m == 1) y--;
378 shown->wday = (d + mo[m] + y + y/4 - y/100 + y/400) % 7 - 1;
380 #endif
382 static void calendar_init(struct shown *shown)
384 int w, h;
385 #if CONFIG_RTC
386 struct tm *tm;
387 #endif
388 rb->lcd_getstringsize("A", &w, &h);
389 if ( ((w * 14) > LCD_WIDTH) || ((h * 7) > LCD_HEIGHT) )
391 use_system_font = true;
393 #if CONFIG_RTC
394 tm = rb->get_time();
395 shown->mday = tm->tm_mday;
396 shown->mon = tm->tm_mon + 1;
397 shown->year = 2000 + (tm->tm_year%100);
398 shown->wday = tm->tm_wday - 1;
399 #else
400 #define S100(x) 1 ## x
401 #define C2DIG2DEC(x) (S100(x)-100)
402 if(info.last_mon == 0 || info.last_year == 0)
404 shown->mon = C2DIG2DEC(MONTH);
405 shown->year = YEAR;
407 else
409 shown->mon = info.last_mon;
410 shown->year = info.last_year;
412 shown->mday = 1;
413 my_set_day_of_week(shown);
414 #endif
415 shown->firstday = calc_weekday(shown);
416 leap_year = is_leap_year(shown->year);
419 static void draw_headers(void)
421 int i, w, h;
422 int x = X_OFFSET;
423 int wday;
424 const char **dayname = dayname_long;
426 for (i = 0; i < 7; i++)
428 rb->lcd_getstringsize(dayname[i], &w, &h);
429 if (w > CELL_WIDTH)
431 dayname = dayname_short;
432 break;
436 wday = info.first_wday;
437 rb->lcd_getstringsize("A", &w, &h);
438 for (i = 0; i < 7; i++)
440 if (wday >= 7) wday = 0;
441 rb->lcd_putsxy(x, 0, dayname[wday++]);
442 x += CELL_WIDTH;
444 rb->lcd_hline(0, LCD_WIDTH-1, h);
447 static bool day_has_memo[32];
448 static bool wday_has_memo[7];
449 static void draw_calendar(struct shown *shown)
451 int w, h;
452 int x, y, pos, days_per_month, j;
453 int wday;
454 char buffer[12];
455 const char *monthname[] = {
456 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
457 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
459 if(use_system_font)
461 rb->lcd_setfont(FONT_SYSFIXED);
463 rb->lcd_getstringsize("A", &w, &h);
464 rb->lcd_clear_display();
465 draw_headers();
466 wday = shown->firstday;
467 pos = wday + 7 - info.first_wday;
468 if (pos >= 7) pos -= 7;
470 days_per_month = days_in_month[leap_year][shown->mon];
471 x = X_OFFSET + (pos * CELL_WIDTH);
472 y = Y_OFFSET + h;
473 for (j = 1; j <= days_per_month; j++)
475 if ( (day_has_memo[j]) || (wday_has_memo[wday]) )
476 rb->snprintf(buffer, 4, "%02d.", j);
477 else
478 rb->snprintf(buffer, 4, "%02d", j);
479 if (shown->mday == j)
481 rb->lcd_set_drawmode(DRMODE_SOLID);
482 rb->lcd_fillrect(x, y - 1, CELL_WIDTH - 1, CELL_HEIGHT);
483 rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID);
484 shown->wday = wday;
486 else
488 rb->lcd_set_drawmode(DRMODE_SOLID);
490 rb->lcd_putsxy(x, y, buffer);
491 x += CELL_WIDTH;
492 wday++;
493 if (wday >= 7)
494 wday = 0;
495 pos++;
496 if (pos >= 7)
498 pos = 0;
499 x = X_OFFSET;
500 y += CELL_HEIGHT;
503 shown->lastday = wday;
504 rb->lcd_set_drawmode(DRMODE_SOLID);
505 rb->lcd_vline(LCD_WIDTH-w*8-10, LCD_HEIGHT-h-3, LCD_HEIGHT-1);
506 rb->lcd_hline(LCD_WIDTH-w*8-10, LCD_WIDTH-1, LCD_HEIGHT-h-3);
507 rb->snprintf(buffer, sizeof(buffer), "%s %04d",
508 monthname[shown->mon-1], shown->year);
509 rb->lcd_putsxy(LCD_WIDTH-w*8-8, LCD_HEIGHT-h-1, buffer);
510 rb->lcd_update();
513 #define MAX_CHAR_MEMO_LEN 64
514 #define MAX_MEMOS_IN_A_MONTH 128
515 struct memo {
516 char message[MAX_CHAR_MEMO_LEN];
517 int day;
518 int month;
519 int file_pointer_start;
520 int file_pointer_end;
521 int year;
522 int wday;
523 int type;
524 } memos[MAX_MEMOS_IN_A_MONTH];
525 static int pointer_array[MAX_MEMOS_IN_A_MONTH];
526 static int memos_in_memory = 0;
527 static int memos_in_shown_memory = 0;
529 static void load_memo(struct shown *shown)
531 int k, fp;
532 bool exit = false;
533 char temp_memo1[2];
534 char temp_memo2[3];
535 char temp_memo4[5];
536 temp_memo1[1] = 0;
537 temp_memo2[2] = 0;
538 temp_memo4[4] = 0;
539 for (k = 1; k < 32; k++)
540 day_has_memo[k] = false;
541 for (k = 0; k < 7; k++)
542 wday_has_memo[k] = false;
543 memos_in_memory = 0;
544 fp = rb->open(MEMO_FILE, O_RDONLY);
545 if (fp > -1)
547 rb->lseek(fp, 0, SEEK_SET);
548 while (!exit)
550 bool load_to_memory;
551 struct memo *memo = &memos[memos_in_memory];
552 rb->memset(memo, 0, sizeof(*memo));
553 memo->file_pointer_start = rb->lseek(fp, 0, SEEK_CUR);
554 if (rb->read(fp, temp_memo2, 2) == 2)
555 memo->day = rb->atoi(temp_memo2);
556 if (rb->read(fp, temp_memo2, 2) == 2)
557 memo->month = rb->atoi(temp_memo2);
558 if (rb->read(fp, temp_memo4, 4) == 4)
559 memo->year = rb->atoi(temp_memo4);
560 if (rb->read(fp, temp_memo1, 1) == 1)
561 memo->wday = rb->atoi(temp_memo1);
562 if (rb->read(fp, temp_memo1, 1) == 1)
563 memo->type = rb->atoi(temp_memo1);
564 load_to_memory = ((memo->type < 2) ||
565 ((memo->type == 2) &&
566 (memo->month == shown->mon)) ||
567 ((memo->type > 2) &&
568 (memo->month == shown->mon) &&
569 (memo->year == shown->year)));
570 k = 0;
571 while (1)
573 if (rb->read(fp, temp_memo1, 1) != 1)
575 memo->day = 0;
576 memo->month = 0;
577 memo->file_pointer_start = 0;
578 memo->file_pointer_end = 0;
579 memo->year = 0;
580 memo->type = 0;
581 memo->wday = 0;
582 memo->message[0] = 0;
583 exit = true;
584 break;
586 if (load_to_memory)
588 if (temp_memo1[0] == '\n')
590 if (memo->type > 0)
591 day_has_memo[memo->day] = true;
592 else
593 wday_has_memo[memo->wday] = true;
594 memo->file_pointer_end = rb->lseek(fp, 0, SEEK_CUR);
595 memos_in_memory++;
597 else if ( (temp_memo1[0] != '\r') &&
598 (temp_memo1[0] != '\t') &&
599 k < MAX_CHAR_MEMO_LEN-1 )
600 memo->message[k++] = temp_memo1[0];
602 if (temp_memo1[0] == '\n')
603 break;
606 rb->close(fp);
610 static bool save_memo(int changed, bool new_mod, struct shown *shown)
612 int fp, fq;
613 /* use O_RDWR|O_CREAT so that file is created if it doesn't exist. */
614 fp = rb->open(MEMO_FILE, O_RDWR|O_CREAT, 0666);
615 fq = rb->creat(TEMP_FILE, 0666);
616 if ( (fq > -1) && (fp > -1) )
618 int i;
619 char temp[MAX_CHAR_MEMO_LEN];
620 struct memo *memo = &memos[changed];
621 rb->lseek(fp, 0, SEEK_SET);
622 for (i = 0; i < memo->file_pointer_start; i++)
624 rb->read(fp, temp, 1);
625 rb->write(fq, temp, 1);
627 if (new_mod)
629 rb->fdprintf(fq, "%02d%02d%04d%01d%01d%s\n",
630 memo->day, memo->month, memo->year, memo->wday,
631 memo->type, memo->message);
633 rb->lseek(fp, memo->file_pointer_end, SEEK_SET);
634 while(rb->read(fp, temp, 1) == 1)
636 rb->write(fq, temp, 1);
638 rb->close(fp);
639 rb->close(fq);
640 rb->remove(MEMO_FILE);
641 rb->rename(TEMP_FILE, MEMO_FILE);
642 load_memo(shown);
643 return true;
645 else if (fp > -1)
646 rb->close(fp);
647 else if (fq > -1)
648 rb->close(fq);
649 return false;
652 static void add_memo(struct shown *shown, int type)
654 bool saved = false;
655 struct memo *memo = &memos[memos_in_memory];
656 if (rb->kbd_input(memo->message, MAX_CHAR_MEMO_LEN) == 0)
658 if (memo->message[0])
660 memo->file_pointer_start = 0;
661 memo->file_pointer_end = 0;
662 memo->day = shown->mday;
663 memo->month = shown->mon;
664 memo->wday = shown->wday;
665 memo->year = shown->year;
666 memo->type = type;
667 if (save_memo(memos_in_memory, true, shown))
669 saved = true;
671 else
673 memo->file_pointer_start = 0;
674 memo->file_pointer_end = 0;
675 memo->day = 0;
676 memo->month = 0;
677 memo->year = 0;
678 memo->type = 0;
679 memo->wday = 0;
683 rb->lcd_clear_display();
684 if (saved)
685 rb->splash(HZ/2, "Event added");
686 else
687 rb->splash(HZ/2, "Event not added");
690 static int edit_menu_cb(int action, const struct menu_item_ex *this_item)
692 int i = (intptr_t)this_item;
693 if (action == ACTION_REQUEST_MENUITEM
694 && memos_in_shown_memory <= 0 && (i==0 || i==1))
695 return ACTION_EXIT_MENUITEM;
696 return action;
699 static bool edit_memo(int change, struct shown *shown)
701 bool exit = false;
702 int selected = 0;
704 static const struct opt_items modes[7] = {
705 { "Mon", -1 },
706 { "Tue", -1 },
707 { "Wed", -1 },
708 { "Thu", -1 },
709 { "Fri", -1 },
710 { "Sat", -1 },
711 { "Sun", -1 },
714 MENUITEM_STRINGLIST(edit_menu, "Edit menu", edit_menu_cb,
715 "Remove", "Edit",
716 "New Weekly", "New Monthly",
717 "New Yearly", "New One off",
718 "First Day of Week",
719 "Playback Control");
721 while (!exit)
723 switch (rb->do_menu(&edit_menu, &selected, NULL, false))
725 case 0: /* remove */
726 save_memo(change, false, shown);
727 return false;
729 case 1: /* edit */
730 if(rb->kbd_input(memos[change].message,
731 MAX_CHAR_MEMO_LEN) == 0)
732 save_memo(change, true, shown);
733 return false;
735 case 2: /* weekly */
736 add_memo(shown, 0);
737 return false;
739 case 3: /* monthly */
740 add_memo(shown, 1);
741 return false;
743 case 4: /* yearly */
744 add_memo(shown, 2);
745 return false;
747 case 5: /* one off */
748 add_memo(shown, 3);
749 return false;
751 case 6: /* weekday */
752 rb->set_option("First Day of Week", &info.first_wday,
753 INT, modes, 7, NULL);
754 break;
756 case 7: /* playback control */
757 playback_control(NULL);
758 break;
760 case GO_TO_PREVIOUS:
761 return false;
763 case MENU_ATTACHED_USB:
764 been_in_usb_mode = true;
765 break;
768 return false;
771 static const char* get_event_text(int selected, void *data,
772 char *buffer, size_t buffer_len)
774 struct shown *shown = (struct shown *) data;
775 struct memo *memo;
776 if (selected < 0 || memos_in_shown_memory <= selected)
778 return NULL;
780 memo = &memos[pointer_array[selected]];
781 if (memo->type == 2)
782 rb->snprintf(buffer, buffer_len, "%s (%d yrs)",
783 memo->message, shown->year - memo->year);
784 else
785 rb->snprintf(buffer, buffer_len, "%s", memo->message);
786 return buffer;
789 static bool view_events(int selected, struct shown *shown)
791 struct gui_synclist gui_memos;
792 bool exit=false;
793 int button;
795 rb->gui_synclist_init(&gui_memos, &get_event_text, shown, false, 1, NULL);
796 rb->gui_synclist_set_title(&gui_memos, "Events (play : menu)", NOICON);
797 rb->gui_synclist_set_nb_items(&gui_memos, memos_in_shown_memory);
798 rb->gui_synclist_select_item(&gui_memos, selected);
799 rb->gui_synclist_draw(&gui_memos);
801 while (!exit)
803 button = rb->get_action(CONTEXT_LIST, TIMEOUT_BLOCK);
804 rb->gui_synclist_do_button(&gui_memos, &button, LIST_WRAP_UNLESS_HELD);
806 switch (button)
808 case ACTION_STD_OK:
809 selected = rb->gui_synclist_get_sel_pos(&gui_memos);
810 return edit_memo(pointer_array[selected], shown);
811 break;
813 case ACTION_STD_CANCEL:
814 return false;
815 break;
817 default:
818 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
819 been_in_usb_mode = true;
820 break;
824 return false;
827 static void update_memos_shown(struct shown *shown)
829 int i;
830 struct memo *memo;
831 memos_in_shown_memory = 0;
832 for (i = 0; i < memos_in_memory; i++)
834 memo = &memos[i];
835 if (((memo->type >= 1) && (memo->day == shown->mday)) ||
836 ((memo->type < 1) && (memo->wday == shown->wday)))
837 pointer_array[memos_in_shown_memory++] = i;
841 static bool any_events(struct shown *shown, bool force)
843 update_memos_shown(shown);
845 if (memos_in_shown_memory > 0)
846 return view_events(0, shown);
847 else if (force)
848 return edit_memo(0, shown);
849 else
850 return false;
852 return false;
855 static void next_month(struct shown *shown, int step)
857 shown->mon++;
858 if (shown->mon > 12)
860 shown->mon = 1;
861 shown->year++;
862 leap_year = is_leap_year(shown->year);
864 if (step > 0)
865 shown->mday = shown->mday - days_in_month[leap_year][shown->mon-1];
866 else if (shown->mday > days_in_month[leap_year][shown->mon])
867 shown->mday = days_in_month[leap_year][shown->mon];
868 shown->firstday = shown->lastday;
869 load_memo(shown);
870 draw_calendar(shown);
873 static void prev_month(struct shown *shown, int step)
875 shown->mon--;
876 if (shown->mon < 1)
878 shown->mon = 12;
879 shown->year--;
880 leap_year = is_leap_year(shown->year);
882 if (step > 0)
883 shown->mday = shown->mday + days_in_month[leap_year][shown->mon];
884 else if (shown->mday > days_in_month[leap_year][shown->mon])
885 shown->mday = days_in_month[leap_year][shown->mon];
886 shown->firstday += 7 - (days_in_month[leap_year][shown->mon] % 7);
887 if (shown->firstday >= 7)
888 shown->firstday -= 7;
889 load_memo(shown);
890 draw_calendar(shown);
893 static void next_day(struct shown *shown, int step)
895 shown->mday += step;
896 if (shown->mday > days_in_month[leap_year][shown->mon])
897 next_month(shown, step);
898 else
899 draw_calendar(shown);
902 static void prev_day(struct shown *shown, int step)
904 shown->mday -= step;
905 if (shown->mday < 1)
906 prev_month(shown, step);
907 else
908 draw_calendar(shown);
911 enum plugin_status plugin_start(const void* parameter)
913 struct shown shown;
914 bool exit = false;
915 int button;
917 (void)(parameter);
919 configfile_load(CFG_FILE, config, ARRAYLEN(config), 0);
920 rb->memcpy(&old_info, &info, sizeof(struct info));
922 calendar_init(&shown);
923 load_memo(&shown);
924 any_events(&shown, false);
925 draw_calendar(&shown);
927 while (!exit)
929 button = rb->button_get(true);
930 switch (button)
932 case CALENDAR_QUIT:
933 exit = true;
934 break;
936 case CALENDAR_NEXT_MONTH:
937 case CALENDAR_NEXT_MONTH | BUTTON_REPEAT:
938 next_month(&shown, 0);
939 break;
941 case CALENDAR_PREV_MONTH:
942 case CALENDAR_PREV_MONTH | BUTTON_REPEAT:
943 prev_month(&shown, 0);
944 break;
946 case CALENDAR_NEXT_WEEK:
947 case CALENDAR_NEXT_WEEK | BUTTON_REPEAT:
948 next_day(&shown, 7);
949 break;
951 case CALENDAR_PREV_WEEK:
952 case CALENDAR_PREV_WEEK | BUTTON_REPEAT:
953 prev_day(&shown, 7);
954 break;
956 case CALENDAR_PREV_DAY:
957 case CALENDAR_PREV_DAY | BUTTON_REPEAT:
958 prev_day(&shown, 1);
959 break;
961 case CALENDAR_NEXT_DAY:
962 case CALENDAR_NEXT_DAY | BUTTON_REPEAT:
963 next_day(&shown, 1);
964 break;
966 case CALENDAR_SELECT:
967 any_events(&shown, true);
968 draw_calendar(&shown);
969 break;
971 default:
972 if(rb->default_event_handler(button) == SYS_USB_CONNECTED)
973 been_in_usb_mode = true;
974 draw_calendar(&shown);
975 break;
980 #if (CONFIG_RTC == 0)
981 info.last_mon = shown.mon;
982 info.last_year = shown.year;
983 #endif
984 if (rb->memcmp(&old_info, &info, sizeof(struct info)))
985 configfile_save(CFG_FILE, config, ARRAYLEN(config), 0);
986 return been_in_usb_mode?PLUGIN_USB_CONNECTED:PLUGIN_OK;