/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2007 Copyright Kévin Ferrare * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "clock.h" #include "clock_draw.h" #include "clock_draw_digital.h" #include "clock_draw_analog.h" #include "clock_draw_binary.h" #include "clock_settings.h" static void black_background(struct screen* display){ #if (LCD_DEPTH > 1) || (defined(LCD_REMOTE_DEPTH) && (LCD_REMOTE_DEPTH > 1)) if(display->depth>1){ display->set_background(LCD_BLACK); display->clear_display(); }else #endif { display->clear_display(); display->fillrect(0,0,display->getwidth(),display->getheight()); } } static void white_background(struct screen* display){ #if (LCD_DEPTH > 1) || (defined(LCD_REMOTE_DEPTH) && (LCD_REMOTE_DEPTH > 1)) if(display->depth>1){ #if defined(HAVE_LCD_COLOR) if(display->is_color)/* restore to the bitmap's background */ display->set_background(LCD_RGBPACK(180,200,230)); else #endif display->set_background(LCD_WHITE); } #endif display->clear_display(); } static bool skin_require_black_background(int mode, int skin){ return((mode==BINARY && skin==2) || (mode==DIGITAL && skin==1 )); } static void skin_set_background(struct screen* display, int mode, int skin){ if(skin_require_black_background(mode, skin) ) black_background(display); else white_background(display); } static void skin_restore_background(struct screen* display, int mode, int skin){ if(skin_require_black_background(mode, skin) ) white_background(display); } void clock_draw_set_colors(void){ FOR_NB_SCREENS(i) skin_set_background(rb->screens[i], clock_settings.mode, clock_settings.skin[clock_settings.mode]); } void clock_draw_restore_colors(void){ FOR_NB_SCREENS(i){ skin_restore_background(rb->screens[i], clock_settings.mode, clock_settings.skin[clock_settings.mode]); rb->screens[i]->update(); } } void clock_draw(struct screen* display, struct time* time, struct counter* counter){ if(!clock_settings.general.show_counter) counter=0; int skin=clock_settings.skin[clock_settings.mode]; skin_set_background(display, clock_settings.mode, skin); if(clock_settings.mode == ANALOG) analog_clock_draw(display, time, &clock_settings, counter, skin); else if(clock_settings.mode == DIGITAL) digital_clock_draw(display, time, &clock_settings, counter, skin); else if(clock_settings.mode == BINARY) binary_clock_draw(display, time, skin); display->update(); } ef='#n20'>20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2010 Daniel Rigby
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "plugin.h"
#include "lib/playback_control.h"



#define MAX_LIST_SIZE 400
#define DESC_SIZE 40
#define MAX_LINE_LEN (DESC_SIZE + 1)

enum flag_type {
    FL_CLEARED = 0,
    FL_SET,
    FL_CATEGORY
};

enum view_type {
    EDIT_SHOPPING_LIST = 0,
    VIEW_SHOPPING_LIST
};

#define VIEW_TYPE_SIZE VIEW_SHOPPING_LIST + 1

struct items_list_s {
    unsigned int id;
    enum flag_type flag;
    char desc[DESC_SIZE];
};

static struct items_list_s items_list[MAX_LIST_SIZE];
static int total_item_count = 0;
static int view_id_list[MAX_LIST_SIZE];
static int view_item_count;
static enum view_type view = EDIT_SHOPPING_LIST;
static char filename[MAX_PATH];
static bool changed = false;
static bool show_categories = true;
static char category_string[] = "Hide categories";

static const char *list_get_name_cb(int selected_item, void* data,
                                    char* buf, size_t buf_len)
{
    (void)data;
    rb->strlcpy(buf, items_list[view_id_list[selected_item]].desc, buf_len);
    return buf;
}

static enum themable_icons list_get_icon_cb(int selected_item, void *data)
{
    (void)data;
    if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
        return Icon_Rockbox;
    else if (items_list[view_id_list[selected_item]].flag == FL_SET)
        return Icon_Cursor;
    else
        return Icon_NOICON;
}

static bool save_changes(void)
{
    int fd;
    int i;

    fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
    if (fd < 0)
    {
        rb->splash(HZ*2, "Changes NOT saved");
        return false;
    }

    rb->lcd_clear_display();
#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(1);
#endif
    for (i = 0;i < total_item_count; i++)
    {
        switch (items_list[i].flag)
        {
            case FL_CATEGORY:
            {
                rb->fdprintf(fd,"#%s\n",items_list[i].desc);
                break;
            }
            case FL_SET:
            {
                rb->fdprintf(fd,"!%s\n",items_list[i].desc);
                break;
            }
            case FL_CLEARED:
            {
                rb->fdprintf(fd," %s\n",items_list[i].desc);
                break;
            }
        }
    }
    /* save current view */
    rb->fdprintf(fd,"$%d%d\n",view, show_categories);

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(0);
#endif
    rb->close(fd);

    return true;
}

static void create_view(struct gui_synclist *lists)
{
    unsigned int cnt = 0;
    int i, j;

    switch (view)
    {
        case EDIT_SHOPPING_LIST:
        {
            for (i = 0; i < total_item_count; i++)
            {
                if (show_categories || (items_list[i].flag != FL_CATEGORY))
                    view_id_list[cnt++] = i;
            }
            view_item_count = cnt;
            rb->gui_synclist_set_title(lists,"Select items",Icon_Playlist);
            break;
        }
        case VIEW_SHOPPING_LIST:
        {
            for (i = 0; i < total_item_count; i++)
            {
                if ((items_list[i].flag == FL_CATEGORY) && show_categories)
                {
                    for (j = i+1; j < total_item_count; j++)
                    {
                        if (items_list[j].flag == FL_SET)
                        {
                            view_id_list[cnt++] = i;
                            break;
                        }
                        if (items_list[j].flag == FL_CATEGORY)
                            break;
                    }
                }
                else if (items_list[i].flag == FL_SET)
                    view_id_list[cnt++] = i;
            }
            view_item_count = cnt;
            rb->gui_synclist_set_title(lists,"Shopping list",Icon_Playlist);
            break;
        }
    }
}

static bool toggle(int selected_item)
{
    if (items_list[view_id_list[selected_item]].flag == FL_CATEGORY)
        return false;
    else if (items_list[view_id_list[selected_item]].flag == FL_SET)
        items_list[view_id_list[selected_item]].flag = FL_CLEARED;
    else
        items_list[view_id_list[selected_item]].flag = FL_SET;
    return true;
}

static void update_category_string(void)
{
    if (show_categories)
        rb->strcpy(category_string,"Hide categories");
    else
        rb->strcpy(category_string,"Show categories");
}

static enum plugin_status load_file(void)
{
    int fd;
    static char temp_line[DESC_SIZE];
    static struct items_list_s new_item;
    static int count = 0;
    int linelen;
    total_item_count = 0;

    fd = rb->open(filename,O_RDONLY);
    if (fd < 0)
    {
        rb->splashf(HZ*2,"Couldn't open file: %s",filename);
        return PLUGIN_ERROR;
    }

    /* read in the file */
    while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
    {
        if (rb->strncmp(temp_line, "$", 1) == 0)
        {
            /* read view preferences */
            linelen = rb->strlen(temp_line);
            if (linelen >= 2)
            {
                unsigned int val = temp_line[1] - '0';
                if (val < VIEW_TYPE_SIZE)
                {
                    view = val;
                }
            }
            if (linelen >= 3)
            {
                unsigned int val = temp_line[2] - '0';
                if (val <= 2)
                {
                    show_categories = val;
                    update_category_string();
                }
            }
        }
        else
        {
            new_item.id = count;
            if (rb->strncmp(temp_line, " ", 1) == 0)
            {
                /* read description, flag = cleared */
                new_item.flag = FL_CLEARED;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else if (rb->strncmp(temp_line, "!", 1) == 0)
            {
                /* read description, flag = set */
                new_item.flag = FL_SET;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else if (rb->strncmp(temp_line, "#", 1) == 0)
            {
                /* read description, flag = category */
                new_item.flag = FL_CATEGORY;
                rb->memcpy(new_item.desc, &temp_line[1], DESC_SIZE);
            }
            else
            {
                /* read description, flag = cleared */
                new_item.flag = FL_CLEARED;
                rb->memcpy(new_item.desc, temp_line, DESC_SIZE);
            }
            items_list[total_item_count] = new_item;
            total_item_count++;
            if (total_item_count == MAX_LIST_SIZE)
            {
                total_item_count = MAX_LIST_SIZE - 1;
                rb->splashf(HZ*2, "Truncating shopping list to %d items",
                            MAX_LIST_SIZE - 1);
                changed = true;
                rb->close(fd);
                return PLUGIN_OK;
            }
        }
    }
    rb->close(fd);
    changed = false;
    return PLUGIN_OK;
}

/* this is the plugin entry point */
enum plugin_status plugin_start(const void* parameter)
{
    struct gui_synclist lists;
    bool exit = false;
    int button;
    int cur_sel = 0;

#if LCD_DEPTH > 1
    rb->lcd_set_backdrop(NULL);
#endif

#ifdef HAVE_ADJUSTABLE_CPU_FREQ
    rb->cpu_boost(1);
#endif
    if (parameter)
    {
        rb->strcpy(filename,(char*)parameter);

        if (load_file() == PLUGIN_ERROR)
            return PLUGIN_ERROR;
    }
    else
        return PLUGIN_ERROR;