Remove plugin-specific defines from the pdbox makefile, and add them to m_pd.h, which...
[kugel-rb.git] / apps / plugins / text_editor.c
blobe54935265c4b560ccd340535d8918d28333c3895
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 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
39 #define ACTION_GET 1
40 #define ACTION_REMOVE 2
41 #define ACTION_UPDATE 3
42 #define ACTION_CONCAT 4
44 char* _do_action(int action, char* str, int line);
45 #ifndef HAVE_ADJUSTABLE_CPU_FREQ
46 #define do_action _do_action
47 #else
48 char* do_action(int action, char* str, int line)
50 char *r;
51 rb->cpu_boost(1);
52 r = _do_action(action,str,line);
53 rb->cpu_boost(0);
54 return r;
56 #endif
58 char* _do_action(int action, char* str, int line)
60 int len, lennew;
61 int i=0,c=0;
62 if (line>=last_action_line)
64 i = last_action_line;
65 c = last_char_index;
67 while (i<line && i<line_count)
69 c += rb->strlen(&buffer[c])+1;
70 i++;
72 switch (action)
74 case ACTION_INSERT:
75 len = rb->strlen(str)+1;
76 if ( char_count+ len > MAX_CHARS )
77 return NULL;
78 rb->memmove(&buffer[c+len],&buffer[c],char_count-c);
79 rb->strcpy(&buffer[c],str);
80 char_count += len;
81 line_count++;
82 break;
83 case ACTION_GET:
84 if (line > line_count)
85 return &buffer[0];
86 break;
87 case ACTION_REMOVE:
88 if (line > line_count)
89 return NULL;
90 len = rb->strlen(&buffer[c])+1;
91 rb->memmove(&buffer[c],&buffer[c+len],char_count-c-len);
92 char_count -= len;
93 line_count--;
94 break;
95 case ACTION_UPDATE:
96 if (line > line_count)
97 return NULL;
98 len = rb->strlen(&buffer[c])+1;
99 lennew = rb->strlen(str)+1;
100 if ( char_count+ lennew-len > MAX_CHARS )
101 return NULL;
102 rb->memmove(&buffer[c+lennew],&buffer[c+len],char_count-c-len);
103 rb->strcpy(&buffer[c],str);
104 char_count += lennew-len;
105 break;
106 case ACTION_CONCAT:
107 if (line > line_count)
108 return NULL;
109 rb->memmove(&buffer[c-1],&buffer[c],char_count-c);
110 char_count--;
111 line_count--;
112 break;
113 default:
114 return NULL;
116 last_action_line = i;
117 last_char_index = c;
118 return &buffer[c];
120 static const char* list_get_name_cb(int selected_item, void* data,
121 char* buf, size_t buf_len)
123 (void)data;
124 char *b = do_action(ACTION_GET, 0, selected_item);
125 /* strlcpy(dst, src, siz) returns strlen(src) */
126 if (rb->strlcpy(buf, b, buf_len) >= buf_len)
128 rb->strcpy(&buf[buf_len-10], " ...");
130 return buf;
133 char filename[MAX_PATH];
134 char eol[3];
135 bool newfile;
136 void get_eol_string(char* fn)
138 int fd;
139 char t;
141 /* assume LF first */
142 rb->strcpy(eol,"\n");
144 if (!fn || !fn[0])
145 return;
146 fd = rb->open(fn,O_RDONLY);
147 if (fd<0)
148 return;
150 while (1)
152 if (!rb->read(fd,&t,1) || t == '\n')
154 break;
156 if (t == '\r')
158 if (rb->read(fd,&t,1) && t=='\n')
159 rb->strcpy(eol,"\r\n");
160 else
161 rb->strcpy(eol,"\r");
162 break;
165 rb->close(fd);
166 return;
169 bool save_changes(int overwrite)
171 int fd;
172 int i;
174 if (newfile || !overwrite)
176 if(rb->kbd_input(filename,MAX_PATH) < 0)
178 newfile = true;
179 return false;
183 fd = rb->open(filename,O_WRONLY|O_CREAT|O_TRUNC, 0666);
184 if (fd < 0)
186 newfile = true;
187 rb->splash(HZ*2, "Changes NOT saved");
188 return false;
191 if (!overwrite)
192 /* current directory may have changed */
193 rb->reload_directory();
195 rb->lcd_clear_display();
196 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
197 rb->cpu_boost(1);
198 #endif
199 for (i=0;i<line_count;i++)
201 rb->fdprintf(fd,"%s%s", do_action(ACTION_GET, 0, i), eol);
203 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
204 rb->cpu_boost(0);
205 #endif
206 rb->close(fd);
207 newfile = false;
208 return true;
211 void setup_lists(struct gui_synclist *lists, int sel)
213 rb->gui_synclist_init(lists,list_get_name_cb,0, false, 1, NULL);
214 rb->gui_synclist_set_icon_callback(lists,NULL);
215 rb->gui_synclist_set_nb_items(lists,line_count);
216 rb->gui_synclist_limit_scroll(lists,true);
217 rb->gui_synclist_select_item(lists, sel);
218 rb->gui_synclist_draw(lists);
221 enum {
222 MENU_RET_SAVE = -1,
223 MENU_RET_NO_UPDATE,
224 MENU_RET_UPDATE,
226 int do_item_menu(int cur_sel, char* copy_buffer)
228 int ret = MENU_RET_NO_UPDATE;
229 MENUITEM_STRINGLIST(menu, "Line Options", NULL,
230 "Cut/Delete", "Copy",
231 "Insert Above", "Insert Below",
232 "Concat To Above",
233 "Save", "Playback Control");
235 switch (rb->do_menu(&menu, NULL, NULL, false))
237 case 0: /* cut */
238 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
239 MAX_LINE_LEN);
240 do_action(ACTION_REMOVE, 0, cur_sel);
241 ret = MENU_RET_UPDATE;
242 break;
243 case 1: /* copy */
244 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
245 MAX_LINE_LEN);
246 ret = MENU_RET_NO_UPDATE;
247 break;
248 case 2: /* insert above */
249 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
251 do_action(ACTION_INSERT,copy_buffer,cur_sel);
252 copy_buffer[0]='\0';
253 ret = MENU_RET_UPDATE;
255 break;
256 case 3: /* insert below */
257 if (!rb->kbd_input(copy_buffer,MAX_LINE_LEN))
259 do_action(ACTION_INSERT,copy_buffer,cur_sel+1);
260 copy_buffer[0]='\0';
261 ret = MENU_RET_UPDATE;
263 break;
264 case 4: /* cat to above */
265 if (cur_sel>0)
267 do_action(ACTION_CONCAT,0,cur_sel);
268 ret = MENU_RET_UPDATE;
270 break;
271 case 5: /* save */
272 ret = MENU_RET_SAVE;
273 break;
274 case 6: /* playback menu */
275 playback_control(NULL);
276 ret = MENU_RET_NO_UPDATE;
277 break;
278 default:
279 ret = MENU_RET_NO_UPDATE;
280 break;
282 return ret;
285 #ifdef HAVE_LCD_COLOR
286 /* in misc.h but no need to polute the api */
287 #define toupper(c) (((c >= 'a') && (c <= 'z'))?c+'A':c)
288 #define isxdigit(c) ((c>='a' && c<= 'f') || (c>='A' && c<= 'F') \
289 || (c>='0' && c<= '9'))
290 #define hex2dec(c) (((c) >= '0' && ((c) <= '9')) ? (toupper(c)) - '0' : \
291 (toupper(c)) - 'A' + 10)
292 int hex_to_rgb(const char* hex, int* color)
293 { int ok = 1;
294 int i;
295 int red, green, blue;
297 if (rb->strlen(hex) == 6) {
298 for (i=0; i < 6; i++ ) {
299 if (!isxdigit(hex[i])) {
300 ok=0;
301 break;
305 if (ok) {
306 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
307 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
308 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
309 *color = LCD_RGBPACK(red,green,blue);
310 return 0;
314 return -1;
316 #endif /* HAVE_LCD_COLOR */
318 /* this is the plugin entry point */
319 enum plugin_status plugin_start(const void* parameter)
321 int fd;
322 static char temp_line[MAX_LINE_LEN];
324 struct gui_synclist lists;
325 bool exit = false;
326 int button;
327 bool changed = false;
328 int cur_sel=0;
329 static char copy_buffer[MAX_LINE_LEN];
330 #ifdef HAVE_LCD_COLOR
331 bool edit_colors_file = false;
332 #endif
334 copy_buffer[0]='\0';
336 #if LCD_DEPTH > 1
337 rb->lcd_set_backdrop(NULL);
338 #endif
340 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
341 rb->cpu_boost(1);
342 #endif
343 if (parameter)
345 #ifdef HAVE_LCD_COLOR
346 char *c = NULL;
347 #endif
348 rb->strlcpy(filename, (char*)parameter, MAX_PATH);
349 get_eol_string(filename);
350 fd = rb->open(filename,O_RDONLY);
351 if (fd<0)
353 rb->splashf(HZ*2, "Couldnt open file: %s", filename);
354 return PLUGIN_ERROR;
356 #ifdef HAVE_LCD_COLOR
357 c = rb->strrchr(filename, '.');
358 if (c && !rb->strcmp(c, ".colours"))
359 edit_colors_file = true;
360 #endif
361 /* read in the file */
362 while (rb->read_line(fd,temp_line,MAX_LINE_LEN) > 0)
364 if (!do_action(ACTION_INSERT,temp_line,line_count))
366 rb->splashf(HZ*2,"Error reading file: %s",(char*)parameter);
367 rb->close(fd);
368 return PLUGIN_ERROR;
371 rb->close(fd);
372 newfile = false;
374 else
376 rb->strcpy(filename,"/");
377 rb->strcpy(eol,"\n");
378 newfile = true;
381 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
382 rb->cpu_boost(0);
383 #endif
384 /* now dump it in the list */
385 setup_lists(&lists,0);
386 rb->lcd_update();
387 while (!exit)
389 rb->gui_synclist_draw(&lists);
390 cur_sel = rb->gui_synclist_get_sel_pos(&lists);
391 button = rb->get_action(CONTEXT_LIST,TIMEOUT_BLOCK);
392 if (rb->gui_synclist_do_button(&lists,&button,LIST_WRAP_UNLESS_HELD))
393 continue;
394 switch (button)
396 case ACTION_STD_OK:
398 if (line_count)
399 rb->strlcpy(temp_line, do_action(ACTION_GET, 0, cur_sel),
400 MAX_LINE_LEN);
401 #ifdef HAVE_LCD_COLOR
402 if (edit_colors_file && line_count)
404 char *name = temp_line, *value = NULL;
405 char extension[MAX_LINE_LEN];
406 int color, old_color;
407 bool temp_changed = false;
408 rb->settings_parseline(temp_line, &name, &value);
409 if (line_count)
411 MENUITEM_STRINGLIST(menu, "Edit What?", NULL,
412 "Extension", "Colour");
413 rb->strcpy(extension, name);
414 if (value)
415 hex_to_rgb(value, &color);
416 else
417 color = 0;
419 switch (rb->do_menu(&menu, NULL, NULL, false))
421 case 0:
422 temp_changed = !rb->kbd_input(extension,MAX_LINE_LEN);
423 break;
424 case 1:
425 old_color = color;
426 rb->set_color(rb->screens[SCREEN_MAIN], name, &color, -1);
427 temp_changed = (value == NULL) || (color != old_color);
428 break;
431 if (temp_changed)
433 rb->snprintf(temp_line, MAX_LINE_LEN, "%s: %02X%02X%02X",
434 extension, RGB_UNPACK_RED(color),
435 RGB_UNPACK_GREEN(color),
436 RGB_UNPACK_BLUE(color));
437 do_action(ACTION_UPDATE, temp_line, cur_sel);
438 changed = true;
442 else
443 #endif
444 if (!rb->kbd_input(temp_line,MAX_LINE_LEN))
446 if (line_count)
447 do_action(ACTION_UPDATE,temp_line,cur_sel);
448 else
449 do_action(ACTION_INSERT,temp_line,cur_sel);
450 changed = true;
453 break;
454 case ACTION_STD_CONTEXT:
455 if (!line_count) break;
456 rb->strlcpy(copy_buffer, do_action(ACTION_GET, 0, cur_sel),
457 MAX_LINE_LEN);
458 do_action(ACTION_REMOVE, 0, cur_sel);
459 changed = true;
460 break;
461 case ACTION_STD_MENU:
462 { /* do the item menu */
463 switch (do_item_menu(cur_sel, copy_buffer))
465 case MENU_RET_SAVE:
466 if(save_changes(1))
467 changed = false;
468 break;
469 case MENU_RET_UPDATE:
470 changed = true;
471 break;
472 case MENU_RET_NO_UPDATE:
473 break;
476 break;
477 case ACTION_STD_CANCEL:
478 if (changed)
480 MENUITEM_STRINGLIST(menu, "Do What?", NULL,
481 "Return",
482 "Playback Control", "Save Changes",
483 "Save As...", "Save and Exit",
484 "Ignore Changes and Exit");
485 switch (rb->do_menu(&menu, NULL, NULL, false))
487 case 0:
488 break;
489 case 1:
490 playback_control(NULL);
491 break;
492 case 2: //save to disk
493 if(save_changes(1))
494 changed = 0;
495 break;
496 case 3:
497 if(save_changes(0))
498 changed = 0;
499 break;
500 case 4:
501 if(save_changes(1))
502 exit=1;
503 break;
504 case 5:
505 exit=1;
506 break;
509 else exit=1;
510 break;
512 rb->gui_synclist_set_nb_items(&lists,line_count);
513 if(line_count > 0 && line_count <= cur_sel)
514 rb->gui_synclist_select_item(&lists,line_count-1);
516 return PLUGIN_OK;