1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Jonathan Gordon
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "lib/playback_control.h"
24 #if PLUGIN_BUFFER_SIZE > 0x45000
25 #define MAX_CHARS 0x40000 /* 128 kiB */
27 #define MAX_CHARS 0x6000 /* 24 kiB */
29 #define MAX_LINE_LEN 2048
32 static char buffer
[MAX_CHARS
];
34 static int char_count
= 0;
35 static int line_count
= 0;
36 static int last_action_line
= 0;
37 static int last_char_index
= 0;
39 #define ACTION_INSERT 0
41 #define ACTION_REMOVE 2
42 #define ACTION_UPDATE 3
43 #define ACTION_CONCAT 4
45 int _do_action(int action
, char* str
, int line
);
46 #ifndef HAVE_ADJUSTABLE_CPU_FREQ
47 #define do_action _do_action
49 int do_action(int action
, char* str
, int line
)
53 r
= _do_action(action
,str
,line
);
59 int _do_action(int action
, char* str
, int line
)
63 if (line
>=last_action_line
)
68 while (i
<line
&& i
<line_count
)
70 c
+= rb
->strlen(&buffer
[c
])+1;
76 len
= rb
->strlen(str
)+1;
77 if ( char_count
+ len
> MAX_CHARS
)
79 rb
->memmove(&buffer
[c
+len
],&buffer
[c
],char_count
);
80 rb
->strcpy(&buffer
[c
],str
);
85 if (line
> line_count
)
92 if (line
> line_count
)
94 len
= rb
->strlen(&buffer
[c
])+1;
96 rb
->memmove(&buffer
[c
],&buffer
[c
+len
],char_count
);
100 if (line
> line_count
)
102 len
= rb
->strlen(&buffer
[c
])+1;
103 rb
->memmove(&buffer
[c
+rb
->strlen(str
)+1],&buffer
[c
+len
],char_count
);
104 rb
->strcpy(&buffer
[c
],str
);
105 char_count
+= rb
->strlen(str
)+1-len
;
108 if (line
> line_count
)
110 rb
->memmove(&buffer
[c
-1],&buffer
[c
],char_count
);
115 last_action_line
= i
;
119 char *list_get_name_cb(int selected_item
, void* data
,
120 char* buf
, size_t buf_len
)
123 char *b
= &buffer
[do_action(ACTION_GET
,0,selected_item
)];
124 if (rb
->strlen(b
) >= buf_len
)
126 char t
= b
[buf_len
-10];
127 b
[buf_len
-10] = '\0';
128 rb
->snprintf(buf
, buf_len
, "%s ...", b
);
131 else rb
->strncpy(buf
, b
, buf_len
);
134 char filename
[MAX_PATH
];
135 int get_eol_string(char* fn
)
143 fd
= rb
->open(fn
,O_RDONLY
);
149 if (!rb
->read(fd
,&t
,1))
151 rb
->strcpy(eol
,"\n");
156 if (rb
->read(fd
,&t
,1) && t
=='\n')
157 rb
->strcpy(eol
,"\r\n");
158 else rb
->strcpy(eol
,"\r");
162 rb
->strcpy(eol
,"\n");
169 void save_changes(int overwrite
)
174 if (!filename
[0] || !overwrite
)
176 rb
->strcpy(filename
,"/");
177 rb
->kbd_input(filename
,MAX_PATH
);
180 fd
= rb
->open(filename
,O_WRONLY
|O_CREAT
|O_TRUNC
);
183 rb
->splash(HZ
*2, "Changes NOT saved");
188 /* current directory may have changed */
189 rb
->reload_directory();
191 rb
->lcd_clear_display();
192 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
195 for (i
=0;i
<line_count
;i
++)
197 rb
->fdprintf(fd
,"%s%s",&buffer
[do_action(ACTION_GET
,0,i
)],eol
);
199 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
205 void setup_lists(struct gui_synclist
*lists
, int sel
)
207 rb
->gui_synclist_init(lists
,list_get_name_cb
,0, false, 1, NULL
);
208 rb
->gui_synclist_set_icon_callback(lists
,NULL
);
209 rb
->gui_synclist_set_nb_items(lists
,line_count
);
210 rb
->gui_synclist_limit_scroll(lists
,true);
211 rb
->gui_synclist_select_item(lists
, sel
);
212 rb
->gui_synclist_draw(lists
);
219 int do_item_menu(int cur_sel
, char* copy_buffer
)
222 MENUITEM_STRINGLIST(menu
, "Line Options", NULL
,
223 "Cut/Delete", "Copy",
224 "Insert Above", "Insert Below",
225 "Concat To Above", "Save",
226 "Show Playback Menu",);
228 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
231 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
232 do_action(ACTION_REMOVE
,0,cur_sel
);
233 ret
= MENU_RET_UPDATE
;
236 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
237 ret
= MENU_RET_NO_UPDATE
;
239 case 2: /* insert above */
240 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
242 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
);
244 ret
= MENU_RET_UPDATE
;
247 case 3: /* insert below */
248 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
250 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
+1);
252 ret
= MENU_RET_UPDATE
;
255 case 4: /* cat to above */
258 do_action(ACTION_CONCAT
,0,cur_sel
);
259 ret
= MENU_RET_UPDATE
;
265 case 6: /* playback menu */
266 playback_control(NULL
);
267 ret
= MENU_RET_UPDATE
;
270 ret
= MENU_RET_NO_UPDATE
;
276 #ifdef HAVE_LCD_COLOR
277 /* in misc.h but no need to polute the api */
278 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
279 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
280 || (c>='0' && c<= '9'))
281 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
282 (toupper(c)) - 'A' + 10)
283 int hex_to_rgb(const char* hex
, int* color
)
286 int red
, green
, blue
;
288 if (rb
->strlen(hex
) == 6) {
289 for (i
=0; i
< 6; i
++ ) {
290 if (!isxdigit(hex
[i
])) {
297 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
298 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
299 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
300 *color
= LCD_RGBPACK(red
,green
,blue
);
307 #endif /* HAVE_LCD_COLOR */
309 /* this is the plugin entry point */
310 enum plugin_status
plugin_start(const void* parameter
)
313 static char temp_line
[MAX_LINE_LEN
];
315 struct gui_synclist lists
;
318 bool changed
= false;
320 static char copy_buffer
[MAX_LINE_LEN
];
321 bool prev_show_statusbar
;
322 #ifdef HAVE_LCD_COLOR
323 bool edit_colors_file
= false;
327 prev_show_statusbar
= rb
->global_settings
->statusbar
;
328 rb
->global_settings
->statusbar
= false;
331 rb
->lcd_set_backdrop(NULL
);
334 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
339 #ifdef HAVE_LCD_COLOR
342 rb
->strcpy(filename
,(char*)parameter
);
343 if (!get_eol_string(filename
))
345 rb
->strcpy(eol
,"\n");
347 fd
= rb
->open(filename
,O_RDONLY
);
350 rb
->splashf(HZ
*2,"Couldnt open file: %s",(char*)parameter
);
353 #ifdef HAVE_LCD_COLOR
354 c
= rb
->strrchr(filename
, '.');
355 if (c
&& !rb
->strcmp(c
, ".colours"))
356 edit_colors_file
= true;
358 /* read in the file */
359 while (rb
->read_line(fd
,temp_line
,MAX_LINE_LEN
))
361 if (!do_action(ACTION_INSERT
,temp_line
,line_count
))
363 rb
->splashf(HZ
*2,"Error reading file: %s",(char*)parameter
);
373 rb
->strcpy(eol
,"\n");
375 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
378 /* now dump it in the list */
379 setup_lists(&lists
,0);
383 rb
->gui_synclist_draw(&lists
);
384 cur_sel
= rb
->gui_synclist_get_sel_pos(&lists
);
385 button
= rb
->get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
386 if (rb
->gui_synclist_do_button(&lists
,&button
,LIST_WRAP_UNLESS_HELD
))
392 bool edit_text
= true;
393 #ifdef HAVE_LCD_COLOR
397 rb
->strcpy(temp_line
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
398 #ifdef HAVE_LCD_COLOR
399 if (edit_colors_file
)
401 char *name
= temp_line
, *value
= NULL
;
402 char extension
[MAX_LINE_LEN
];
403 rb
->settings_parseline(temp_line
, &name
, &value
);
406 MENUITEM_STRINGLIST(menu
, "Edit What?", NULL
,
407 "Extension", "Colour",);
408 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
416 hex_to_rgb(value
, &color
);
418 rb
->strcpy(extension
, name
);
419 rb
->set_color(rb
->screens
[SCREEN_MAIN
], name
, &color
, -1);
420 rb
->snprintf(temp_line
, MAX_LINE_LEN
, "%s: %02X%02X%02X",
421 extension
, RGB_UNPACK_RED(color
),
422 RGB_UNPACK_GREEN(color
),
423 RGB_UNPACK_BLUE(color
));
426 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
428 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
435 if (edit_text
&&!rb
->kbd_input(temp_line
,MAX_LINE_LEN
))
439 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
441 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
446 case ACTION_STD_CONTEXT
:
447 if (!line_count
) break;
448 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
449 do_action(ACTION_REMOVE
,0,cur_sel
);
452 case ACTION_STD_MENU
:
453 { /* do the item menu */
454 switch (do_item_menu(cur_sel
, copy_buffer
))
460 case MENU_RET_UPDATE
:
463 case MENU_RET_NO_UPDATE
:
468 case ACTION_STD_CANCEL
:
471 MENUITEM_STRINGLIST(menu
, "Do What?", NULL
,
473 "Show Playback Menu", "Save Changes",
474 "Save As...", "Save and Exit",
475 "Ignore Changes and Exit");
476 switch (rb
->do_menu(&menu
, NULL
, NULL
, false))
481 playback_control(NULL
);
483 case 2: //save to disk
504 rb
->gui_synclist_set_nb_items(&lists
,line_count
);
506 rb
->global_settings
->statusbar
= prev_show_statusbar
;