Ensure that the file handle is always closed in text editor plugin (part of FS#10138...
[kugel-rb/myfork.git] / apps / plugins / text_editor.c
blob911b64e7ab2759c397d0179d528a3dd30980142b
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 0x6000 /* 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->strncpy(buf, b, buf_len);
137 return buf;
140 char filename[MAX_PATH];
141 int get_eol_string(char* fn)
143 int fd, result;
144 char t;
146 if (!fn || !fn[0])
147 return 0;
148 fd = rb->open(fn,O_RDONLY);
149 if (fd<0)
150 return 0;
152 eol[0] = '\0';
153 result = 1;
154 while (!eol[0])
156 if (!rb->read(fd,&t,1))
158 rb->strcpy(eol,"\n");
159 result = 0;
161 if (t == '\r')
163 if (rb->read(fd,&t,1) && t=='\n')
164 rb->strcpy(eol,"\r\n");
165 else
166 rb->strcpy(eol,"\r");
168 else if (t == '\n')
170 rb->strcpy(eol,"\n");
173 rb->close(fd);
174 return result;
177 void save_changes(int overwrite)
179 int fd;
180 int i;
182 if (!filename[0] || !overwrite)
184 rb->strcpy(filename,"/");
185 rb->kbd_input(filename,MAX_PATH);
188 fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC);
189 if (fd < 0)
191 rb->splash(HZ*2, "Changes NOT saved");
192 return;
195 if (!overwrite)
196 /* current directory may have changed */
197 rb->reload_directory();
199 rb->lcd_clear_display();
200 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
201 rb->cpu_boost(1);
202 #endif
203 for (i=0;i<line_count;i++)
205 rb->fdprintf(fd,"%s%s",&buffer[do_action(ACTION_GET,0,i)],eol);
207 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
208 rb->cpu_boost(0);
209 #endif
210 rb->close(fd);
213 void setup_lists(struct gui_synclist *lists, int sel)
215 rb->gui_synclist_init(lists,list_get_name_cb,0, false, 1, NULL);
216 rb->gui_synclist_set_icon_callback(lists,NULL);
217 rb->gui_synclist_set_nb_items(lists,line_count);
218 rb->gui_synclist_limit_scroll(lists,true);
219 rb->gui_synclist_select_item(lists, sel);
220 rb->gui_synclist_draw(lists);
222 enum {
223 MENU_RET_SAVE = -1,
224 MENU_RET_NO_UPDATE,
225 MENU_RET_UPDATE,
227 int do_item_menu(int cur_sel, char* copy_buffer)
229 int ret = 0;
230 MENUITEM_STRINGLIST(menu, "Line Options", NULL,
231 "Cut/Delete", "Copy",
232 "Insert Above", "Insert Below",
233 "Concat To Above", "Save",
234 "Show Playback Menu",);
236 switch (rb->do_menu(&menu, NULL, NULL, false))
238 case 0: /* cut */
239 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
240 do_action(ACTION_REMOVE,0,cur_sel);
241 ret = MENU_RET_UPDATE;
242 break;
243 case 1: /* copy */
244 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
245 ret = MENU_RET_NO_UPDATE;
246 break;
247 case 2: /* insert above */
248 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
250 do_action(ACTION_INSERT,copy_buffer,cur_sel);
251 copy_buffer[0]='\0';
252 ret = MENU_RET_UPDATE;
254 break;
255 case 3: /* insert below */
256 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
258 do_action(ACTION_INSERT,copy_buffer,cur_sel+1);
259 copy_buffer[0]='\0';
260 ret = MENU_RET_UPDATE;
262 break;
263 case 4: /* cat to above */
264 if (cur_sel>0)
266 do_action(ACTION_CONCAT,0,cur_sel);
267 ret = MENU_RET_UPDATE;
269 break;
270 case 5: /* save */
271 ret = MENU_RET_SAVE;
272 break;
273 case 6: /* playback menu */
274 playback_control(NULL);
275 ret = MENU_RET_NO_UPDATE;
276 break;
277 default:
278 ret = MENU_RET_NO_UPDATE;
279 break;
281 return ret;
284 #ifdef HAVE_LCD_COLOR
285 /* in misc.h but no need to polute the api */
286 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
287 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
288 || (c>='0' && c<= '9'))
289 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
290 (toupper(c)) - 'A' + 10)
291 int hex_to_rgb(const char* hex, int* color)
292 { int ok = 1;
293 int i;
294 int red, green, blue;
296 if (rb->strlen(hex) == 6) {
297 for (i=0; i < 6; i++ ) {
298 if (!isxdigit(hex[i])) {
299 ok=0;
300 break;
304 if (ok) {
305 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
306 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
307 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
308 *color = LCD_RGBPACK(red,green,blue);
309 return 0;
313 return -1;
315 #endif /* HAVE_LCD_COLOR */
317 /* this is the plugin entry point */
318 enum plugin_status plugin_start(const void* parameter)
320 int fd;
321 static char temp_line[MAX_LINE_LEN];
323 struct gui_synclist lists;
324 bool exit = false;
325 int button;
326 bool changed = false;
327 int cur_sel=0;
328 static char copy_buffer[MAX_LINE_LEN];
329 bool prev_show_statusbar;
330 #ifdef HAVE_LCD_COLOR
331 bool edit_colors_file = false;
332 #endif
334 copy_buffer[0]='\0';
335 prev_show_statusbar = rb->global_settings->statusbar;
336 rb->global_settings->statusbar = false;
338 #if LCD_DEPTH > 1
339 rb->lcd_set_backdrop(NULL);
340 #endif
342 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
343 rb->cpu_boost(1);
344 #endif
345 if (parameter)
347 #ifdef HAVE_LCD_COLOR
348 char *c = NULL;
349 #endif
350 rb->strcpy(filename,(char*)parameter);
351 if (!get_eol_string(filename))
353 rb->strcpy(eol,"\n");
355 fd = rb->open(filename,O_RDONLY);
356 if (fd<0)
358 rb->splashf(HZ*2,"Couldnt open file: %s",(char*)parameter);
359 return PLUGIN_ERROR;
361 #ifdef HAVE_LCD_COLOR
362 c = rb->strrchr(filename, '.');
363 if (c && !rb->strcmp(c, ".colours"))
364 edit_colors_file = true;
365 #endif
366 /* read in the file */
367 while (rb->read_line(fd,temp_line,MAX_LINE_LEN))
369 if (!do_action(ACTION_INSERT,temp_line,line_count))
371 rb->splashf(HZ*2,"Error reading file: %s",(char*)parameter);
372 rb->close(fd);
373 return PLUGIN_ERROR;
376 rb->close(fd);
378 else
380 filename[0] = '\0';
381 rb->strcpy(eol,"\n");
383 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
384 rb->cpu_boost(0);
385 #endif
386 /* now dump it in the list */
387 setup_lists(&lists,0);
388 rb->lcd_update();
389 while (!exit)
391 rb->gui_synclist_draw(&lists);
392 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
393 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
394 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
395 continue;
396 switch (button)
398 case ACTION_STD_OK:
400 if (line_count)
401 rb->strcpy(temp_line,&buffer[do_action(ACTION_GET,0,cur_sel)]);
402 #ifdef HAVE_LCD_COLOR
403 if (edit_colors_file && line_count)
405 char *name = temp_line, *value = NULL;
406 char extension[MAX_LINE_LEN];
407 int color, old_color;
408 bool temp_changed;
409 rb->settings_parseline(temp_line, &name, &value);
410 if (line_count)
412 MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
413 "Extension", "Colour",);
414 rb->strcpy(extension, name);
415 if (value)
416 hex_to_rgb(value, &color);
417 else
418 color = 0;
420 switch (rb->do_menu(&menu, NULL, NULL, false))
422 case 0:
423 temp_changed = !rb->kbd_input(extension,MAX_LINE_LEN);
424 break;
425 case 1:
426 old_color = color;
427 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
428 temp_changed = (value == NULL) || (color != old_color);
429 break;
430 default:
431 /* Should never happen but makes compiler happy */
432 temp_changed = false;
435 if (temp_changed)
437 rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X",
438 extension, RGB_UNPACK_RED(color),
439 RGB_UNPACK_GREEN(color),
440 RGB_UNPACK_BLUE(color));
441 do_action(ACTION_UPDATE, temp_line, cur_sel);
442 changed = true;
446 else
447 #endif
448 if (!rb->kbd_input(temp_line,MAX_LINE_LEN))
450 if (line_count)
451 do_action(ACTION_UPDATE,temp_line,cur_sel);
452 else
453 do_action(ACTION_INSERT,temp_line,cur_sel);
454 changed = true;
457 break;
458 case ACTION_STD_CONTEXT:
459 if (!line_count) break;
460 rb->strcpy(copy_buffer,&buffer[do_action(ACTION_GET,0,cur_sel)]);
461 do_action(ACTION_REMOVE,0,cur_sel);
462 changed = true;
463 break;
464 case ACTION_STD_MENU:
465 { /* do the item menu */
466 switch (do_item_menu(cur_sel, copy_buffer))
468 case MENU_RET_SAVE:
469 save_changes(1);
470 changed = false;
471 break;
472 case MENU_RET_UPDATE:
473 changed = true;
474 break;
475 case MENU_RET_NO_UPDATE:
476 break;
479 break;
480 case ACTION_STD_CANCEL:
481 if (changed)
483 MENUITEM_STRINGLIST(menu, "Do What?", NULL,
484 "Return",
485 "Show Playback Menu", "Save Changes",
486 "Save As...", "Save and Exit",
487 "Ignore Changes and Exit");
488 switch (rb->do_menu(&menu, NULL, NULL, false))
490 case 0:
491 break;
492 case 1:
493 playback_control(NULL);
494 break;
495 case 2: //save to disk
496 save_changes(1);
497 changed = 0;
498 break;
499 case 3:
500 save_changes(0);
501 changed = 0;
502 break;
504 case 4:
505 save_changes(1);
506 exit=1;
507 break;
508 case 5:
509 exit=1;
510 break;
513 else exit=1;
514 break;
516 rb->gui_synclist_set_nb_items(&lists,line_count);
518 rb->global_settings->statusbar = prev_show_statusbar;
519 return PLUGIN_OK;