"remote_control: don't use goto.
[kugel-rb.git] / apps / logfdisp.c
blob2057a3b88664661ee9f3a1b50ccd6f68a454ab10
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Daniel Stenberg
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 "config.h"
23 #ifdef ROCKBOX_HAS_LOGF
24 #include <file.h>
25 #include <sprintf.h>
26 #include <timefuncs.h>
27 #include <string.h>
28 #include <kernel.h>
29 #include <action.h>
31 #include <lcd.h>
32 #include <font.h>
33 #include "menu.h"
34 #include "logf.h"
35 #include "settings.h"
36 #include "logfdisp.h"
37 #include "action.h"
38 #include "splash.h"
40 #ifdef HAVE_LCD_BITMAP
41 int compute_nb_lines(int w, struct font* font)
43 int i, nb_lines;
44 int cur_x, delta_x;
46 if(logfindex == 0 && !logfwrap)
47 return 0;
49 if(logfwrap)
50 i = logfindex;
51 else
52 i = 0;
54 cur_x = 0;
55 nb_lines = 0;
57 do {
58 if(logfbuffer[i] == '\0')
60 cur_x = 0;
61 nb_lines++;
63 else
65 /* does character fit on this line ? */
66 delta_x = font_get_width(font, logfbuffer[i]);
68 if(cur_x + delta_x > w)
70 cur_x = 0;
71 nb_lines++;
74 /* update pointer */
75 cur_x += delta_x;
78 i++;
79 if(i >= MAX_LOGF_SIZE)
80 i = 0;
81 } while(i != logfindex);
83 return nb_lines;
86 bool logfdisplay(void)
88 int action;
89 int w, h, i, index;
90 int fontnr;
91 int cur_x, cur_y, delta_y, delta_x;
92 struct font* font;
93 int user_index;/* user_index will be number of the first line to display (warning: line!=logf entry) */
94 char buf[2];
96 fontnr = lcd_getfont();
97 font = font_get(fontnr);
99 /* get the horizontal size of each line */
100 font_getstringsize("A", NULL, &delta_y, fontnr);
102 buf[1] = '\0';
103 w = LCD_WIDTH;
104 h = LCD_HEIGHT;
105 /* start at the end of the log */
106 user_index = compute_nb_lines(w, font) - h/delta_y -1; /* if negative, will be set 0 to zero later */
108 do {
109 lcd_clear_display();
111 if(user_index < 0)
112 user_index = 0;
114 if(logfwrap)
115 i = logfindex;
116 else
117 i = 0;
119 index = 0;
120 cur_x = 0;
121 cur_y = 0;
123 /* nothing to print ? */
124 if(logfindex == 0 && !logfwrap)
125 goto end_print;
127 do {
128 if(logfbuffer[i] == '\0')
130 /* should be display a newline ? */
131 if(index >= user_index)
132 cur_y += delta_y;
133 cur_x = 0;
134 index++;
136 else
138 /* does character fit on this line ? */
139 delta_x = font_get_width(font, logfbuffer[i]);
141 if(cur_x + delta_x > w)
143 /* should be display a newline ? */
144 if(index >= user_index)
145 cur_y += delta_y;
146 cur_x = 0;
147 index++;
150 /* should we print character ? */
151 if(index >= user_index)
153 buf[0] = logfbuffer[i];
154 lcd_putsxy(cur_x, cur_y, buf);
157 /* update pointer */
158 cur_x += delta_x;
161 /* did we fill the screen ? */
162 if(cur_y > h)
163 break;
165 i++;
166 if(i >= MAX_LOGF_SIZE)
167 i = 0;
168 } while(i != logfindex);
170 end_print:
171 lcd_update();
173 action = get_action(CONTEXT_STD, HZ);
174 switch( action )
176 case ACTION_STD_NEXT:
177 case ACTION_STD_NEXTREPEAT:
178 user_index++;
179 break;
180 case ACTION_STD_PREV:
181 case ACTION_STD_PREVREPEAT:
182 user_index--;
183 break;
184 case ACTION_STD_OK:
185 user_index = 0;
186 break;
187 #ifdef HAVE_TOUCHSCREEN
188 case ACTION_TOUCHSCREEN:
190 short x, y;
191 static int prev_y;
193 action = action_get_touchscreen_press(&x, &y);
195 if(action & BUTTON_REL)
196 prev_y = 0;
197 else
199 if(prev_y != 0)
200 user_index += (prev_y - y) / delta_y;
202 prev_y = y;
205 #endif
206 default:
207 break;
209 } while(action != ACTION_STD_CANCEL);
211 return false;
213 #else /* HAVE_LCD_BITMAP */
214 bool logfdisplay(void)
217 /* TODO: implement a browser for charcell bitmaps */
218 return false;
220 #endif /* HAVE_LCD_BITMAP */
222 /* Store the logf log to logf.txt in the .rockbox directory. The order of the
223 * entries will be "reversed" so that the most recently logged entry is on the
224 * top of the file */
225 bool logfdump(void)
227 int fd;
229 splashf(HZ, "Log File Dumped");
231 /* nothing to print ? */
232 if(logfindex == 0 && !logfwrap)
233 /* nothing is logged just yet */
234 return false;
236 fd = open(ROCKBOX_DIR "/logf.txt", O_CREAT|O_WRONLY|O_TRUNC);
237 if(-1 != fd) {
238 int i;
240 if(logfwrap)
241 i = logfindex;
242 else
243 i = 0;
245 do {
246 if(logfbuffer[i]=='\0')
247 fdprintf(fd, "\n");
248 else
249 fdprintf(fd, "%c", logfbuffer[i]);
251 i++;
252 if(i >= MAX_LOGF_SIZE)
253 i = 0;
254 } while(i != logfindex);
256 close(fd);
258 return false;
261 #endif /* ROCKBOX_HAS_LOGF */