Make kbd_input() show a cancel splash to indicate user abort better and for better...
[kugel-rb/myfork.git] / apps / plugins / text_editor.c
blob473bb68eadc94941154ce5b8dc0828a52bf07b9f
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "plugin.h"
22 #include "lib/playback_control.h"
24 #if PLUGIN_BUFFER_SIZE > 0x45000
25 #define MAX_CHARS 0x40000 /* 256 kiB */
26 #else
27 #define MAX_CHARS 0x5FE0 /* a bit less than 24 kiB */
28 #endif
29 #define MAX_LINE_LEN 2048
30 PLUGIN_HEADER
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, lennew;
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-c);
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 rb->memmove(&buffer[c],&buffer[c+len],char_count-c-len);
96 char_count -= len;
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 lennew = rb->strlen(str)+1;
104 if ( char_count+ lennew-len > MAX_CHARS )
105 return 0;
106 rb->memmove(&buffer[c+lennew],&buffer[c+len],char_count-c-len);
107 rb->strcpy(&buffer[c],str);
108 char_count += lennew-len;
109 break;
110 case ACTION_CONCAT:
111 if (line > line_count)
112 return 0;
113 rb->memmove(&buffer[c-1],&buffer[c],char_count-c);
114 char_count--;
115 line_count--;
116 break;
117 default:
118 return 0;
120 last_action_line = i;
121 last_char_index = c;
122 return 1;
124 char *list_get_name_cb(int selected_item, void* data,
125 char* buf, size_t buf_len)
127 (void)data;
128 char *b = &buffer[do_action(ACTION_GET,0,selected_item)];
129 if (rb->strlen(b) >= buf_len)
131 char t = b[buf_len-10];
132 b[buf_len-10] = '\0';
133 rb->snprintf(buf , buf_len, "%s ...", b);
134 b[buf_len-10] = t;
136 else rb->strlcpy(buf, b, buf_len);
137 return buf;
140 char filename[MAX_PATH];
141 bool newfile;
142 int get_eol_string(char* fn)
144 int fd, result;
145 char t;
147 if (!fn || !fn[0])
148 return 0;
149 fd = rb->open(fn,O_RDONLY);
150 if (fd<0)
151 return 0;
153 eol[0] = '\0';
154 result = 1;
155 while (!eol[0])
157 if (!rb->read(fd,&t,1))
159 rb->strcpy(eol,"\n");
160 result = 0;
162 if (t == '\r')
164 if (rb->read(fd,&t,1) && t=='\n')
165 rb->strcpy(eol,"\r\n");
166 else
167 rb->strcpy(eol,"\r");
169 else if (t == '\n')
171 rb->strcpy(eol,"\n");
174 rb->close(fd);
175 return result;
178 bool save_changes(int overwrite)
180 int fd;
181 int i;
183 if (newfile || !overwrite)
185 if(rb->kbd_input(filename,MAX_PATH) < 0)
187 newfile = true;
188 return false;
192 fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
193 if (fd < 0)
195 newfile = true;
196 rb->splash(HZ*2, "Changes NOT saved");
197 return false;
200 if (!overwrite)
201 /* current directory may have changed */
202 rb->reload_directory();
204 rb->lcd_clear_display();
205 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
206 rb->cpu_boost(1);
207 #endif
208 for (i=0;i<line_count;i++)
210 rb->fdprintf(fd,"%s%s",&buffer[do_action(ACTION_GET,0,i)],eol);
212 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
213 rb->cpu_boost(0);
214 #endif
215 rb->close(fd);
216 newfile = false;
217 return true;
220 void setup_lists(struct gui_synclist *lists, int sel)
222 rb->gui_synclist_init(lists,list_get_name_cb,0, false, 1, NULL);
223 rb->gui_synclist_set_icon_callback(lists,NULL);
224 rb->gui_synclist_set_nb_items(lists,line_count);
225 rb->gui_synclist_limit_scroll(lists,true);
226 rb->gui_synclist_select_item(lists, sel);
227 rb->gui_synclist_draw(lists);
229 enum {
230 MENU_RET_SAVE = -1,
231 MENU_RET_NO_UPDATE,
232 MENU_RET_UPDATE,
234 int do_item_menu(int cur_sel, char* copy_buffer)
236 int ret = 0;
237 MENUITEM_STRINGLIST(menu, "Line Options", NULL,
238 "Cut/Delete", "Copy",
239 "Insert Above", "Insert Below",
240 "Concat To Above", "Save",
241 "Show Playback Menu",);
243 switch (rb->do_menu(&menu, NULL, NULL, false))
245 case 0: /* cut */
246 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
247 do_action(ACTION_REMOVE,0,cur_sel);
248 ret = MENU_RET_UPDATE;
249 break;
250 case 1: /* copy */
251 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
252 ret = MENU_RET_NO_UPDATE;
253 break;
254 case 2: /* insert above */
255 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
257 do_action(ACTION_INSERT,copy_buffer,cur_sel);
258 copy_buffer[0]='\0';
259 ret = MENU_RET_UPDATE;
261 break;
262 case 3: /* insert below */
263 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
265 do_action(ACTION_INSERT,copy_buffer,cur_sel+1);
266 copy_buffer[0]='\0';
267 ret = MENU_RET_UPDATE;
269 break;
270 case 4: /* cat to above */
271 if (cur_sel>0)
273 do_action(ACTION_CONCAT,0,cur_sel);
274 ret = MENU_RET_UPDATE;
276 break;
277 case 5: /* save */
278 ret = MENU_RET_SAVE;
279 break;
280 case 6: /* playback menu */
281 playback_control(NULL);
282 ret = MENU_RET_NO_UPDATE;
283 break;
284 default:
285 ret = MENU_RET_NO_UPDATE;
286 break;
288 return ret;
291 #ifdef HAVE_LCD_COLOR
292 /* in misc.h but no need to polute the api */
293 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
294 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
295 || (c>='0' && c<= '9'))
296 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
297 (toupper(c)) - 'A' + 10)
298 int hex_to_rgb(const char* hex, int* color)
299 { int ok = 1;
300 int i;
301 int red, green, blue;
303 if (rb->strlen(hex) == 6) {
304 for (i=0; i < 6; i++ ) {
305 if (!isxdigit(hex[i])) {
306 ok=0;
307 break;
311 if (ok) {
312 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
313 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
314 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
315 *color = LCD_RGBPACK(red,green,blue);
316 return 0;
320 return -1;
322 #endif /* HAVE_LCD_COLOR */
324 /* this is the plugin entry point */
325 enum plugin_status plugin_start(const void* parameter)
327 int fd;
328 static char temp_line[MAX_LINE_LEN];
330 struct gui_synclist lists;
331 bool exit = false;
332 int button;
333 bool changed = false;
334 int cur_sel=0;
335 static char copy_buffer[MAX_LINE_LEN];
336 #ifdef HAVE_LCD_COLOR
337 bool edit_colors_file = false;
338 #endif
340 copy_buffer[0]='\0';
342 #if LCD_DEPTH > 1
343 rb->lcd_set_backdrop(NULL);
344 #endif
346 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
347 rb->cpu_boost(1);
348 #endif
349 if (parameter)
351 #ifdef HAVE_LCD_COLOR
352 char *c = NULL;
353 #endif
354 rb->strcpy(filename,(char*)parameter);
355 if (!get_eol_string(filename))
357 rb->strcpy(eol,"\n");
359 fd = rb->open(filename,O_RDONLY);
360 if (fd<0)
362 rb->splashf(HZ*2,"Couldnt open file: %s",(char*)parameter);
363 return PLUGIN_ERROR;
365 #ifdef HAVE_LCD_COLOR
366 c = rb->strrchr(filename, '.');
367 if (c && !rb->strcmp(c, ".colours"))
368 edit_colors_file = true;
369 #endif
370 /* read in the file */
371 while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
373 if (!do_action(ACTION_INSERT,temp_line,line_count))
375 rb->splashf(HZ*2,"Error reading file: %s",(char*)parameter);
376 rb->close(fd);
377 return PLUGIN_ERROR;
380 rb->close(fd);
381 newfile = false;
383 else
385 rb->strcpy(filename,"/");
386 rb->strcpy(eol,"\n");
387 newfile = true;
389 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
390 rb->cpu_boost(0);
391 #endif
392 /* now dump it in the list */
393 setup_lists(&lists,0);
394 rb->lcd_update();
395 while (!exit)
397 rb->gui_synclist_draw(&lists);
398 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
399 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
400 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
401 continue;
402 switch (button)
404 case ACTION_STD_OK:
406 if (line_count)
407 rb->strcpy(temp_line,&buffer[do_action(ACTION_GET,0,cur_sel)]);
408 #ifdef HAVE_LCD_COLOR
409 if (edit_colors_file && line_count)
411 char *name = temp_line, *value = NULL;
412 char extension[MAX_LINE_LEN];
413 int color, old_color;
414 bool temp_changed;
415 rb->settings_parseline(temp_line, &name, &value);
416 if (line_count)
418 MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
419 "Extension", "Colour",);
420 rb->strcpy(extension, name);
421 if (value)
422 hex_to_rgb(value, &color);
423 else
424 color = 0;
426 switch (rb->do_menu(&menu, NULL, NULL, false))
428 case 0:
429 temp_changed = !rb->kbd_input(extension,MAX_LINE_LEN);
430 break;
431 case 1:
432 old_color = color;
433 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
434 temp_changed = (value == NULL) || (color != old_color);
435 break;
436 default:
437 /* Should never happen but makes compiler happy */
438 temp_changed = false;
441 if (temp_changed)
443 rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X",
444 extension, RGB_UNPACK_RED(color),
445 RGB_UNPACK_GREEN(color),
446 RGB_UNPACK_BLUE(color));
447 do_action(ACTION_UPDATE, temp_line, cur_sel);
448 changed = true;
452 else
453 #endif
454 if (!rb->kbd_input(temp_line,MAX_LINE_LEN))
456 if (line_count)
457 do_action(ACTION_UPDATE,temp_line,cur_sel);
458 else
459 do_action(ACTION_INSERT,temp_line,cur_sel);
460 changed = true;
463 break;
464 case ACTION_STD_CONTEXT:
465 if (!line_count) break;
466 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
467 do_action(ACTION_REMOVE,0,cur_sel);
468 changed = true;
469 break;
470 case ACTION_STD_MENU:
471 { /* do the item menu */
472 switch (do_item_menu(cur_sel, copy_buffer))
474 case MENU_RET_SAVE:
475 if(save_changes(1))
476 changed = false;
477 break;
478 case MENU_RET_UPDATE:
479 changed = true;
480 break;
481 case MENU_RET_NO_UPDATE:
482 break;
485 break;
486 case ACTION_STD_CANCEL:
487 if (changed)
489 MENUITEM_STRINGLIST(menu, "Do What?", NULL,
490 "Return",
491 "Show Playback Menu", "Save Changes",
492 "Save As...", "Save and Exit",
493 "Ignore Changes and Exit");
494 switch (rb->do_menu(&menu, NULL, NULL, false))
496 case 0:
497 break;
498 case 1:
499 playback_control(NULL);
500 break;
501 case 2: //save to disk
502 if(save_changes(1))
503 changed = 0;
504 break;
505 case 3:
506 if(save_changes(0))
507 changed = 0;
508 break;
510 case 4:
511 if(save_changes(1))
512 exit=1;
513 break;
514 case 5:
515 exit=1;
516 break;
519 else exit=1;
520 break;
522 rb->gui_synclist_set_nb_items(&lists,line_count);
523 if(line_count > 0 && line_count <= cur_sel)
524 rb->gui_synclist_select_item(&lists,line_count-1);
526 return PLUGIN_OK;