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 ****************************************************************************/
20 #include "playback_control.h"
22 #if PLUGIN_BUFFER_SIZE > 0x45000
23 #define MAX_CHARS 0x40000 /* 128 kiB */
25 #define MAX_CHARS 0x6000 /* 24 kiB */
27 #define MAX_LINE_LEN 2048
29 static struct plugin_api
* rb
;
31 static char buffer
[MAX_CHARS
];
33 static int char_count
= 0;
34 static int line_count
= 0;
35 static int last_action_line
= 0;
36 static int last_char_index
= 0;
38 #define ACTION_INSERT 0
40 #define ACTION_REMOVE 2
41 #define ACTION_UPDATE 3
42 #define ACTION_CONCAT 4
44 int _do_action(int action
, char* str
, int line
);
45 #ifndef HAVE_ADJUSTABLE_CPU_FREQ
46 #define do_action _do_action
48 int do_action(int action
, char* str
, int line
)
52 r
= _do_action(action
,str
,line
);
58 int _do_action(int action
, char* str
, int line
)
62 if (line
>=last_action_line
)
67 while (i
<line
&& i
<line_count
)
69 c
+= rb
->strlen(&buffer
[c
])+1;
75 len
= rb
->strlen(str
)+1;
76 if ( char_count
+ len
> MAX_CHARS
)
78 rb
->memmove(&buffer
[c
+len
],&buffer
[c
],char_count
);
79 rb
->strcpy(&buffer
[c
],str
);
84 if (line
> line_count
)
91 if (line
> line_count
)
93 len
= rb
->strlen(&buffer
[c
])+1;
95 rb
->memmove(&buffer
[c
],&buffer
[c
+len
],char_count
);
99 if (line
> line_count
)
101 len
= rb
->strlen(&buffer
[c
])+1;
102 rb
->memmove(&buffer
[c
+rb
->strlen(str
)+1],&buffer
[c
+len
],char_count
);
103 rb
->strcpy(&buffer
[c
],str
);
104 char_count
+= rb
->strlen(str
)+1-len
;
107 if (line
> line_count
)
109 rb
->memmove(&buffer
[c
-1],&buffer
[c
],char_count
);
114 last_action_line
= i
;
118 char *list_get_name_cb(int selected_item
,void* data
,char* buf
)
120 char *b
= &buffer
[do_action(ACTION_GET
,0,selected_item
)];
122 if (rb
->strlen(b
) >= MAX_PATH
)
124 char t
= b
[MAX_PATH
-10];
125 b
[MAX_PATH
-10] = '\0';
126 rb
->snprintf(buf
,MAX_PATH
,"%s ...",b
);
129 else rb
->strcpy(buf
,b
);
132 char filename
[MAX_PATH
];
133 int get_eol_string(char* fn
)
141 fd
= rb
->PREFIX(open(fn
,O_RDONLY
));
147 if (!rb
->read(fd
,&t
,1))
149 rb
->strcpy(eol
,"\n");
154 if (rb
->read(fd
,&t
,1) && t
=='\n')
155 rb
->strcpy(eol
,"\r\n");
156 else rb
->strcpy(eol
,"\r");
160 rb
->strcpy(eol
,"\n");
167 void save_changes(int overwrite
)
172 if (!filename
[0] || !overwrite
)
174 rb
->strcpy(filename
,"/");
175 rb
->kbd_input(filename
,MAX_PATH
);
178 fd
= rb
->open(filename
,O_WRONLY
|O_CREAT
|O_TRUNC
);
181 rb
->splash(HZ
*2, "Changes NOT saved");
186 /* current directory may have changed */
187 rb
->reload_directory();
189 rb
->lcd_clear_display();
190 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
193 for (i
=0;i
<line_count
;i
++)
195 rb
->fdprintf(fd
,"%s%s",&buffer
[do_action(ACTION_GET
,0,i
)],eol
);
197 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
203 void setup_lists(struct gui_synclist
*lists
, int sel
)
205 rb
->gui_synclist_init(lists
,list_get_name_cb
,0, false, 1);
206 rb
->gui_synclist_set_icon_callback(lists
,NULL
);
207 rb
->gui_synclist_set_nb_items(lists
,line_count
);
208 rb
->gui_synclist_limit_scroll(lists
,true);
209 rb
->gui_synclist_select_item(lists
, sel
);
210 rb
->gui_synclist_draw(lists
);
217 int do_item_menu(int cur_sel
, char* copy_buffer
)
220 MENUITEM_STRINGLIST(menu
, "Line Options", NULL
,
221 "Cut/Delete", "Copy",
222 "Insert Above", "Insert Below",
223 "Concat To Above", "Save");
225 switch (rb
->do_menu(&menu
, NULL
))
228 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
229 do_action(ACTION_REMOVE
,0,cur_sel
);
230 ret
= MENU_RET_UPDATE
;
233 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
234 ret
= MENU_RET_NO_UPDATE
;
236 case 2: /* insert above */
237 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
239 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
);
241 ret
= MENU_RET_UPDATE
;
244 case 3: /* insert below */
245 if (!rb
->kbd_input(copy_buffer
,MAX_LINE_LEN
))
247 do_action(ACTION_INSERT
,copy_buffer
,cur_sel
+1);
249 ret
= MENU_RET_UPDATE
;
252 case 4: /* cat to above */
255 do_action(ACTION_CONCAT
,0,cur_sel
);
256 ret
= MENU_RET_UPDATE
;
263 ret
= MENU_RET_NO_UPDATE
;
269 #ifdef HAVE_LCD_COLOR
270 /* in misc.h but no need to polute the api */
271 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
272 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
273 || (c>='0' && c<= '9'))
274 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
275 (toupper(c)) - 'A' + 10)
276 int hex_to_rgb(const char* hex
)
279 int red
, green
, blue
;
281 if (rb
->strlen(hex
) == 6) {
282 for (i
=0; i
< 6; i
++ ) {
283 if (!isxdigit(hex
[i
])) {
290 red
= (hex2dec(hex
[0]) << 4) | hex2dec(hex
[1]);
291 green
= (hex2dec(hex
[2]) << 4) | hex2dec(hex
[3]);
292 blue
= (hex2dec(hex
[4]) << 4) | hex2dec(hex
[5]);
293 return LCD_RGBPACK(red
,green
,blue
);
299 #endif /* HAVE_LCD_COLOR */
301 /* this is the plugin entry point */
302 enum plugin_status
plugin_start(struct plugin_api
* api
, void* parameter
)
305 static char temp_line
[MAX_LINE_LEN
];
307 struct gui_synclist lists
;
310 bool changed
= false;
312 static char copy_buffer
[MAX_LINE_LEN
];
313 bool prev_show_statusbar
;
314 #ifdef HAVE_LCD_COLOR
315 bool edit_colors_file
= false;
321 prev_show_statusbar
= rb
->global_settings
->statusbar
;
322 rb
->global_settings
->statusbar
= false;
325 rb
->lcd_set_backdrop(NULL
);
328 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
333 #ifdef HAVE_LCD_COLOR
336 rb
->strcpy(filename
,(char*)parameter
);
337 if (!get_eol_string(filename
))
339 rb
->strcpy(eol
,"\n");
341 fd
= rb
->open(filename
,O_RDONLY
);
344 rb
->splash(HZ
*2,"Couldnt open file: %s",(char*)parameter
);
347 #ifdef HAVE_LCD_COLOR
348 c
= rb
->strrchr(filename
, '.');
349 if (c
&& !rb
->strcmp(c
, ".colours"))
350 edit_colors_file
= true;
352 /* read in the file */
353 while (rb
->read_line(fd
,temp_line
,MAX_LINE_LEN
))
355 if (!do_action(ACTION_INSERT
,temp_line
,line_count
))
357 rb
->splash(HZ
*2,"Error reading file: %s",(char*)parameter
);
367 rb
->strcpy(eol
,"\n");
369 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
372 /* now dump it in the list */
373 setup_lists(&lists
,0);
377 rb
->gui_synclist_draw(&lists
);
378 cur_sel
= rb
->gui_synclist_get_sel_pos(&lists
);
379 button
= rb
->get_action(CONTEXT_LIST
,TIMEOUT_BLOCK
);
380 if (rb
->gui_synclist_do_button(&lists
,&button
,LIST_WRAP_UNLESS_HELD
))
386 bool edit_text
= true;
387 #ifdef HAVE_LCD_COLOR
391 rb
->strcpy(temp_line
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
392 #ifdef HAVE_LCD_COLOR
393 if (edit_colors_file
)
395 char *name
= temp_line
, *value
= NULL
;
396 char extension
[MAX_LINE_LEN
];
397 rb
->settings_parseline(temp_line
, &name
, &value
);
400 MENUITEM_STRINGLIST(menu
, "Edit What?", NULL
,
401 "Extension", "Color",);
402 switch (rb
->do_menu(&menu
, NULL
))
410 color
= hex_to_rgb(value
);
412 rb
->strcpy(extension
, name
);
413 rb
->set_color(rb
->screens
[SCREEN_MAIN
], name
, &color
, -1);
414 rb
->snprintf(temp_line
, MAX_LINE_LEN
, "%s: %02X%02X%02X",
415 extension
, RGB_UNPACK_RED(color
),
416 RGB_UNPACK_GREEN(color
),
417 RGB_UNPACK_BLUE(color
));
420 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
422 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
429 if (edit_text
&&!rb
->kbd_input(temp_line
,MAX_LINE_LEN
))
433 do_action(ACTION_UPDATE
,temp_line
,cur_sel
);
435 else do_action(ACTION_INSERT
,temp_line
,cur_sel
);
440 case ACTION_STD_CONTEXT
:
441 if (!line_count
) break;
442 rb
->strcpy(copy_buffer
,&buffer
[do_action(ACTION_GET
,0,cur_sel
)]);
443 do_action(ACTION_REMOVE
,0,cur_sel
);
446 case ACTION_STD_MENU
:
447 { /* do the item menu */
448 switch (do_item_menu(cur_sel
, copy_buffer
))
454 case MENU_RET_UPDATE
:
457 case MENU_RET_NO_UPDATE
:
462 case ACTION_STD_CANCEL
:
465 MENUITEM_STRINGLIST(menu
, "Do What?", NULL
,
467 "Show Playback Menu", "Save Changes",
468 "Save As...", "Save and Exit",
469 "Ignore Changes and Exit");
470 switch (rb
->do_menu(&menu
, NULL
))
475 playback_control(rb
);
477 case 2: //save to disk
498 rb
->gui_synclist_set_nb_items(&lists
,line_count
);
500 rb
->global_settings
->statusbar
= prev_show_statusbar
;