PP502x: Add some important information about CPU/COP_CTL register to the header glean...
[Rockbox.git] / apps / plugins / text_editor.c
blob3c9bf7d7bfc396de12f89d229b426d9e080f70b2
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include "plugin.h"
20 #include "action.h"
21 #include "playback_control.h"
23 #if PLUGIN_BUFFER_SIZE > 0x45000
24 #define MAX_CHARS 0x40000 /* 128 kiB */
25 #else
26 #define MAX_CHARS 0x6000 /* 24 kiB */
27 #endif
28 #define MAX_LINE_LEN 2048
29 PLUGIN_HEADER
30 static struct plugin_api* rb;
32 static char buffer[MAX_CHARS];
33 static char eol[3];
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
40 #define ACTION_GET 1
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
48 #else
49 int do_action(int action, char* str, int line)
51 int r;
52 rb->cpu_boost(1);
53 r = _do_action(action,str,line);
54 rb->cpu_boost(0);
55 return r;
57 #endif
59 int _do_action(int action, char* str, int line)
61 int len;
62 int i=0,c=0;
63 if (line>=last_action_line)
65 i = last_action_line;
66 c = last_char_index;
68 while (i<line && i<line_count)
70 c += rb->strlen(&buffer[c])+1;
71 i++;
73 switch (action)
75 case ACTION_INSERT:
76 len = rb->strlen(str)+1;
77 if ( char_count+ len > MAX_CHARS )
78 return 0;
79 rb->memmove(&buffer[c+len],&buffer[c],char_count);
80 rb->strcpy(&buffer[c],str);
81 char_count += len;
82 line_count++;
83 break;
84 case ACTION_GET:
85 if (line > line_count)
86 return 0;
87 last_action_line = i;
88 last_char_index = c;
89 return c;
90 break;
91 case ACTION_REMOVE:
92 if (line > line_count)
93 return 0;
94 len = rb->strlen(&buffer[c])+1;
95 char_count -= len;
96 rb->memmove(&buffer[c],&buffer[c+len],char_count);
97 line_count--;
98 break;
99 case ACTION_UPDATE:
100 if (line > line_count)
101 return 0;
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;
106 break;
107 case ACTION_CONCAT:
108 if (line > line_count)
109 return 0;
110 rb->memmove(&buffer[c-1],&buffer[c],char_count);
111 break;
112 default:
113 return 0;
115 last_action_line = i;
116 last_char_index = c;
117 return 1;
119 char *list_get_name_cb(int selected_item,void* data,char* buf)
121 char *b = &buffer[do_action(ACTION_GET,0,selected_item)];
122 (void)data;
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);
128 b[MAX_PATH-10] = t;
130 else rb->strcpy(buf,b);
131 return buf;
133 char filename[MAX_PATH];
134 int get_eol_string(char* fn)
136 int fd=-1;
137 char t;
138 if (!fn)
139 return 0;
140 else if (!fn[0])
141 return 0;
142 fd = rb->PREFIX(open(fn,O_RDONLY));
143 if (fd<0)
144 return 0;
145 eol[0] = '\0';
146 while (!eol[0])
148 if (!rb->read(fd,&t,1))
150 rb->strcpy(eol,"\n");
151 return 0;
153 if (t == '\r')
155 if (rb->read(fd,&t,1) && t=='\n')
156 rb->strcpy(eol,"\r\n");
157 else rb->strcpy(eol,"\r");
159 else if (t == '\n')
161 rb->strcpy(eol,"\n");
164 rb->close(fd);
165 return 1;
168 void save_changes(int overwrite)
170 int fd;
171 int i;
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);
180 if (fd < 0)
182 rb->splash(HZ*2, "Changes NOT saved");
183 return;
186 if (!overwrite)
187 /* current directory may have changed */
188 rb->reload_directory();
190 rb->lcd_clear_display();
191 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
192 rb->cpu_boost(1);
193 #endif
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
199 rb->cpu_boost(0);
200 #endif
201 rb->close(fd);
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);
213 enum {
214 MENU_RET_SAVE = -1,
215 MENU_RET_NO_UPDATE,
216 MENU_RET_UPDATE,
218 int do_item_menu(int cur_sel, char* copy_buffer)
220 int ret = 0;
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))
228 case 0: /* cut */
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;
232 break;
233 case 1: /* copy */
234 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
235 ret = MENU_RET_NO_UPDATE;
236 break;
237 case 2: /* insert above */
238 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
240 do_action(ACTION_INSERT,copy_buffer,cur_sel);
241 copy_buffer[0]='\0';
242 ret = MENU_RET_UPDATE;
244 break;
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);
249 copy_buffer[0]='\0';
250 ret = MENU_RET_UPDATE;
252 break;
253 case 4: /* cat to above */
254 if (cur_sel>0)
256 do_action(ACTION_CONCAT,0,cur_sel);
257 ret = MENU_RET_UPDATE;
259 break;
260 case 5: /* save */
261 ret = MENU_RET_SAVE;
262 break;
263 default:
264 ret = MENU_RET_NO_UPDATE;
265 break;
267 return ret;
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)
278 { int ok = 1;
279 int i;
280 int red, green, blue;
282 if (rb->strlen(hex) == 6) {
283 for (i=0; i < 6; i++ ) {
284 if (!isxdigit(hex[i])) {
285 ok=0;
286 break;
290 if (ok) {
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);
298 return 0;
300 #endif /* HAVE_LCD_COLOR */
302 /* this is the plugin entry point */
303 enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
305 int fd;
306 static char temp_line[MAX_LINE_LEN];
308 struct gui_synclist lists;
309 bool exit = false;
310 int button;
311 bool changed = false;
312 int cur_sel=0;
313 static char copy_buffer[MAX_LINE_LEN];
314 bool prev_show_statusbar;
315 #ifdef HAVE_LCD_COLOR
316 bool edit_colors_file = false;
317 #endif
319 rb = api;
321 copy_buffer[0]='\0';
322 prev_show_statusbar = rb->global_settings->statusbar;
323 rb->global_settings->statusbar = false;
325 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
326 rb->cpu_boost(1);
327 #endif
328 if (parameter)
330 #ifdef HAVE_LCD_COLOR
331 char *c = NULL;
332 #endif
333 rb->strcpy(filename,(char*)parameter);
334 if (!get_eol_string(filename))
336 rb->strcpy(eol,"\n");
338 fd = rb->open(filename,O_RDONLY);
339 if (fd<0)
341 rb->splash(HZ*2,"Couldnt open file: %s",(char*)parameter);
342 return PLUGIN_ERROR;
344 #ifdef HAVE_LCD_COLOR
345 c = rb->strrchr(filename, '.');
346 if (c && !rb->strcmp(c, ".colours"))
347 edit_colors_file = true;
348 #endif
349 /* read in the file */
350 while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
352 if (!do_action(ACTION_INSERT,temp_line,line_count))
354 rb->splash(HZ*2,"Error reading file: %s",(char*)parameter);
355 rb->close(fd);
356 return PLUGIN_ERROR;
359 rb->close(fd);
361 else
363 filename[0] = '\0';
364 rb->strcpy(eol,"\n");
366 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
367 rb->cpu_boost(0);
368 #endif
369 /* now dump it in the list */
370 setup_lists(&lists,0);
371 rb->lcd_update();
372 while (!exit)
374 rb->gui_synclist_draw(&lists);
375 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
376 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
377 if (rb->gui_synclist_do_button(&lists,button,LIST_WRAP_UNLESS_HELD))
378 continue;
379 switch (button)
381 case ACTION_STD_OK:
383 bool edit_text = true;
384 #ifdef HAVE_LCD_COLOR
385 int color;
386 #endif
387 if (line_count)
388 rb->strcpy(temp_line,&buffer[do_action(ACTION_GET,0,cur_sel)]);
389 #ifdef HAVE_LCD_COLOR
390 if (edit_colors_file)
392 char *name = temp_line, *value = NULL;
393 char extension[MAX_LINE_LEN];
394 rb->settings_parseline(temp_line, &name, &value);
395 if (line_count)
397 MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
398 "Extension", "Color",);
399 switch (rb->do_menu(&menu, NULL))
401 case 0:
402 edit_text = true;
403 break;
404 case 1:
405 edit_text = false;
406 if (value)
407 color = hex_to_rgb(value);
408 else color = 0;
409 rb->strcpy(extension, name);
410 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
411 rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X",
412 extension, RGB_UNPACK_RED(color),
413 RGB_UNPACK_GREEN(color),
414 RGB_UNPACK_BLUE(color));
415 if (line_count)
417 do_action(ACTION_UPDATE,temp_line,cur_sel);
419 else do_action(ACTION_INSERT,temp_line,cur_sel);
420 changed = true;
421 break;
425 #endif
426 if (edit_text &&!rb->kbd_input(temp_line,MAX_LINE_LEN))
428 if (line_count)
430 do_action(ACTION_UPDATE,temp_line,cur_sel);
432 else do_action(ACTION_INSERT,temp_line,cur_sel);
433 changed = true;
436 break;
437 #ifdef TEXT_EDITOR_DELETE
438 case TEXT_EDITOR_DELETE:
439 #ifdef TEXT_EDITOR_DELETE_PRE
440 if (last_button != TEXT_EDITOR_DELETE_PRE)
441 break;
442 #endif
443 if (!line_count) break;
444 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
445 do_action(ACTION_REMOVE,0,cur_sel);
446 changed = true;
447 break;
448 #endif
449 case ACTION_STD_MENU:
450 { /* do the item menu */
451 switch (do_item_menu(cur_sel, copy_buffer))
453 case MENU_RET_SAVE:
454 save_changes(1);
455 changed = false;
456 break;
457 case MENU_RET_UPDATE:
458 changed = true;
459 break;
460 case MENU_RET_NO_UPDATE:
461 break;
464 break;
465 case ACTION_STD_CANCEL:
466 if (changed)
468 MENUITEM_STRINGLIST(menu, "Do What?", NULL,
469 "Return",
470 "Show Playback Menu", "Save Changes",
471 "Save As...", "Save and Exit",
472 "Ignore Changes and Exit");
473 switch (rb->do_menu(&menu, NULL))
475 case 0:
476 break;
477 case 1:
478 playback_control(rb);
479 break;
480 case 2: //save to disk
481 save_changes(1);
482 changed = 0;
483 break;
484 case 3:
485 save_changes(0);
486 changed = 0;
487 break;
489 case 4:
490 save_changes(1);
491 exit=1;
492 break;
493 case 5:
494 exit=1;
495 break;
498 else exit=1;
499 break;
501 rb->gui_synclist_set_nb_items(&lists,line_count);
503 rb->global_settings->statusbar = prev_show_statusbar;
504 return PLUGIN_OK;