1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 Jonathan Gordon
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
21 #include "playback_control.h"
23 #if PLUGIN_BUFFER_SIZE > 0x45000
24 #define MAX_CHARS 0x40000 /* 128 kiB */
26 #define MAX_CHARS 0x6000 /* 24 kiB */
28 #define MAX_LINE_LEN 2048
30 static struct plugin_api
* rb
;
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
,char* buf
)
121 char *b
= &buffer
[do_action(ACTION_GET
,0,selected_item
)];
123 if (rb
->strlen(b
) >= MAX_PATH
)
125 char t
= b
[MAX_PATH
-10];
126 b
[MAX_PATH
-10] = '\0';
127 rb
->snprintf(buf
,MAX_PATH
,"%s ...",b
);
130 else rb
->strcpy(buf
,b
);
133 char filename
[MAX_PATH
];
134 int get_eol_string(char* fn
)
142 fd
= rb
->PREFIX(open(fn
,O_RDONLY
));
148 if (!rb
->read(fd
,&t
,1))
150 rb
->strcpy(eol
,"\n");
155 if (rb
->read(fd
,&t
,1) && t
=='\n')
156 rb
->strcpy(eol
,"\r\n");
157 else rb
->strcpy(eol
,"\r");
161 rb
->strcpy(eol
,"\n");
168 void save_changes(int overwrite
)
173 if (!filename
[0] || !overwrite
)
175 rb
->strcpy(filename
,"/");
176 rb
->kbd_input(filename
,MAX_PATH
);
179 fd
= rb
->open(filename
,O_WRONLY
|O_CREAT
|O_TRUNC
);
182 rb
->splash(HZ
*2, "Changes NOT saved");
187 /* current directory may have changed */
188 rb
->reload_directory();
190 rb
->lcd_clear_display();
191 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
194 for (i
=0;i
<line_count
;i
++)
196 rb
->fdprintf(fd
,"%s%s",&buffer
[do_action(ACTION_GET
,0,i
)],eol
);
198 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
204 void setup_lists(struct gui_synclist
*lists
, int sel
)
206 rb
->gui_synclist_init(lists
,list_get_name_cb
,0, false, 1);
207 rb
->gui_synclist_set_icon_callback(lists
,NULL
);
208 rb
->gui_synclist_set_nb_items(lists
,line_count
);
209 rb
->gui_synclist_limit_scroll(lists
,true);
210 rb
->gui_synclist_select_item(lists
, sel
);
211 rb
->gui_synclist_draw(lists
);
218 int do_item_menu(int cur_sel
, char* copy_buffer
)
221 MENUITEM_STRINGLIST(menu
, "Line Options", NULL
,
222 "Cut/Delete", "Copy",
223 "Insert Above", "Insert Below",
224 "Concat To Above", "Save");
226 switch (rb
->do_menu(&menu
, NULL
))
229 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
230 do_action(ACTION_REMOVE
,0,cur_sel
);
231 ret
= MENU_RET_UPDATE
;
234 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
235 ret
= MENU_RET_NO_UPDATE
;
237 case 2: /* insert above */
238 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
240 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
);
242 ret
= MENU_RET_UPDATE
;
245 case 3: /* insert below */
246 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
248 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
+1);
250 ret
= MENU_RET_UPDATE
;
253 case 4: /* cat to above */
256 do_action(ACTION_CONCAT
,0,cur_sel
);
257 ret
= MENU_RET_UPDATE
;
264 ret
= MENU_RET_NO_UPDATE
;
270 #ifdef HAVE_LCD_COLOR
271 /* in misc.h but no need to polute the api */
272 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
273 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
274 || (c>='0' && c<= '9'))
275 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
276 (toupper(c)) - 'A' + 10)
277 int hex_to_rgb(const char* hex
)
280 int red
, green
, blue
;
282 if (rb
->strlen(hex
) == 6) {
283 for (i
=0; i
< 6; i
++ ) {
284 if (!isxdigit(hex
[i
])) {
291 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
292 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
293 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
294 return LCD_RGBPACK(red
,green
,blue
);
300 #endif /* HAVE_LCD_COLOR */
302 /* this is the plugin entry point */
303 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
306 static char temp_line
[MAX_LINE_LEN
];
308 struct gui_synclist lists
;
311 bool changed
= false;
313 static char copy_buffer
[MAX_LINE_LEN
];
314 bool prev_show_statusbar
;
315 #ifdef HAVE_LCD_COLOR
316 bool edit_colors_file
= false;
322 prev_show_statusbar
= rb
->global_settings
->statusbar
;
323 rb
->global_settings
->statusbar
= false;
326 rb
->lcd_set_backdrop(NULL
);
329 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
334 #ifdef HAVE_LCD_COLOR
337 rb
->strcpy(filename
,(char*)parameter
);
338 if (!get_eol_string(filename
))
340 rb
->strcpy(eol
,"\n");
342 fd
= rb
->open(filename
,O_RDONLY
);
345 rb
->splash(HZ
*2,"Couldnt open file: %s",(char*)parameter
);
348 #ifdef HAVE_LCD_COLOR
349 c
= rb
->strrchr(filename
, '.');
350 if (c
&& !rb
->strcmp(c
, ".colours"))
351 edit_colors_file
= true;
353 /* read in the file */
354 while (rb
->read_line(fd
,temp_line
,MAX_LINE_LEN
))
356 if (!do_action(ACTION_INSERT
,temp_line
,line_count
))
358 rb
->splash(HZ
*2,"Error reading file: %s",(char*)parameter
);
368 rb
->strcpy(eol
,"\n");
370 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
373 /* now dump it in the list */
374 setup_lists(&lists
,0);
378 rb
->gui_synclist_draw(&lists
);
379 cur_sel
= rb
->gui_synclist_get_sel_pos(&lists
);
380 button
= rb
->get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
381 if (rb
->gui_synclist_do_button(&lists
,&button
,LIST_WRAP_UNLESS_HELD
))
387 bool edit_text
= true;
388 #ifdef HAVE_LCD_COLOR
392 rb
->strcpy(temp_line
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
393 #ifdef HAVE_LCD_COLOR
394 if (edit_colors_file
)
396 char *name
= temp_line
, *value
= NULL
;
397 char extension
[MAX_LINE_LEN
];
398 rb
->settings_parseline(temp_line
, &name
, &value
);
401 MENUITEM_STRINGLIST(menu
, "Edit What?", NULL
,
402 "Extension", "Color",);
403 switch (rb
->do_menu(&menu
, NULL
))
411 color
= hex_to_rgb(value
);
413 rb
->strcpy(extension
, name
);
414 rb
->set_color(rb
->screens
[SCREEN_MAIN
], name
, &color
, -1);
415 rb
->snprintf(temp_line
, MAX_LINE_LEN
, "%s: %02X%02X%02X",
416 extension
, RGB_UNPACK_RED(color
),
417 RGB_UNPACK_GREEN(color
),
418 RGB_UNPACK_BLUE(color
));
421 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
423 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
430 if (edit_text
&&!rb
->kbd_input(temp_line
,MAX_LINE_LEN
))
434 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
436 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
441 case ACTION_STD_CONTEXT
:
442 if (!line_count
) break;
443 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
444 do_action(ACTION_REMOVE
,0,cur_sel
);
447 case ACTION_STD_MENU
:
448 { /* do the item menu */
449 switch (do_item_menu(cur_sel
, copy_buffer
))
455 case MENU_RET_UPDATE
:
458 case MENU_RET_NO_UPDATE
:
463 case ACTION_STD_CANCEL
:
466 MENUITEM_STRINGLIST(menu
, "Do What?", NULL
,
468 "Show Playback Menu", "Save Changes",
469 "Save As...", "Save and Exit",
470 "Ignore Changes and Exit");
471 switch (rb
->do_menu(&menu
, NULL
))
476 playback_control(rb
);
478 case 2: //save to disk
499 rb
->gui_synclist_set_nb_items(&lists
,line_count
);
501 rb
->global_settings
->statusbar
= prev_show_statusbar
;