Bump version numbers for 3.13
[maemo-rb.git] / apps / plugins / fractals / fractal.c
blob8c5d93e0b752f113751f7d748081bf44548f1cef
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2004 Matthias Wientapper
11 * Heavily extended 2005 Jens Arnold
12 * Extended 2009 Tomer Shalev
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
24 #include "plugin.h"
26 #include "fractal.h"
27 #include "fractal_rect.h"
28 #include "fractal_sets.h"
29 #include "mandelbrot_set.h"
30 #include "lib/pluginlib_exit.h"
32 #ifdef USEGSLIB
33 GREY_INFO_STRUCT
34 static unsigned char *gbuf;
35 static size_t gbuf_size = 0;
36 #endif
38 #define REDRAW_NONE 0
39 #define REDRAW_PARTIAL 1
40 #define REDRAW_FULL 2
41 #define REDRAW_FULL_OVERLAY 3
45 /* returns 1 if a button has been pressed, 0 otherwise */
46 static int button_yield(void *ctx)
48 long *button = (long *)ctx;
50 *button = rb->button_get(false);
51 switch (*button)
53 case FRACTAL_QUIT:
54 case FRACTAL_UP:
55 case FRACTAL_DOWN:
56 case FRACTAL_LEFT:
57 case FRACTAL_RIGHT:
58 case FRACTAL_ZOOM_IN:
59 case FRACTAL_ZOOM_OUT:
60 case FRACTAL_PRECISION_INC:
61 case FRACTAL_PRECISION_DEC:
62 case FRACTAL_RESET:
63 #ifdef FRACTAL_ZOOM_IN2
64 case FRACTAL_ZOOM_IN2:
65 #endif
66 #ifdef FRACTAL_ZOOM_IN_PRE
67 case FRACTAL_ZOOM_IN_PRE:
68 #endif
69 #if defined(FRACTAL_ZOOM_OUT_PRE) && \
70 (FRACTAL_ZOOM_OUT_PRE != FRACTAL_ZOOM_IN_PRE)
71 case FRACTAL_ZOOM_OUT_PRE:
72 #endif
73 #ifdef FRACTAL_PRECISION_INC_PRE
74 case FRACTAL_PRECISION_INC_PRE:
75 #endif
76 #if defined(FRACTAL_PRECISION_DEC_PRE) && \
77 (FRACTAL_PRECISION_DEC_PRE != FRACTAL_PRECISION_INC_PRE)
78 case FRACTAL_PRECISION_DEC_PRE:
79 #endif
80 return 1;
81 default:
82 *button = BUTTON_NONE;
83 return 0;
87 static void cleanup(void)
89 #ifdef USEGSLIB
90 grey_release();
91 #endif
94 enum plugin_status plugin_start(const void* parameter)
96 #if defined(FRACTAL_ZOOM_OUT_PRE) || \
97 defined(FRACTAL_ZOOM_IN_PRE) || \
98 defined(FRACTAL_PRECISION_DEC_PRE) || \
99 defined(FRACTAL_PRECISION_INC_PRE)
100 long lastbutton = BUTTON_NONE;
101 #endif
102 int redraw = REDRAW_FULL;
103 struct fractal_ops *ops = &mandelbrot_ops;
105 (void)parameter;
107 #ifdef USEGSLIB
108 /* get the remainder of the plugin buffer */
109 gbuf = (unsigned char *)rb->plugin_get_buffer(&gbuf_size);
111 /* initialize the greyscale buffer.*/
112 if (!grey_init(gbuf, gbuf_size, GREY_ON_COP, LCD_WIDTH, LCD_HEIGHT, NULL))
114 rb->splash(HZ, "Couldn't init greyscale display");
115 return PLUGIN_ERROR;
117 grey_show(true); /* switch on greyscale overlay */
118 #endif
120 /* release greylib on exit */
121 atexit(cleanup);
122 #if LCD_DEPTH > 1
123 rb->lcd_set_backdrop(NULL);
124 #endif
126 ops->init();
128 /* main loop */
129 while (true)
131 long button = BUTTON_NONE;
133 if (redraw != REDRAW_NONE)
135 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
136 rb->cpu_boost(true);
137 #endif
138 switch (redraw)
140 case REDRAW_FULL:
141 mylcd_ub_clear_display();
142 mylcd_ub_update();
143 /* fall-through */
144 case REDRAW_FULL_OVERLAY:
145 rects_queue_init();
146 break;
147 default:
148 break;
151 /* paint all rects */
152 rects_calc_all(ops->calc, button_yield, (void *)&button);
153 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
154 rb->cpu_boost(false);
155 #endif
156 /* not interrupted by button press - screen is fully drawn */
157 redraw = (button == BUTTON_NONE) ? REDRAW_NONE : REDRAW_PARTIAL;
160 if (button == BUTTON_NONE)
161 button = rb->button_get(true);
163 switch (button)
165 #ifdef FRACTAL_RC_QUIT
166 case FRACTAL_RC_QUIT:
167 #endif
168 case FRACTAL_QUIT:
169 return PLUGIN_OK;
171 case FRACTAL_ZOOM_OUT:
172 #ifdef FRACTAL_ZOOM_OUT_PRE
173 if (lastbutton != FRACTAL_ZOOM_OUT_PRE)
174 break;
175 #endif
176 if (!ops->zoom(-1))
177 redraw = REDRAW_FULL;
178 break;
181 case FRACTAL_ZOOM_IN:
182 #ifdef FRACTAL_ZOOM_IN_PRE
183 if (lastbutton != FRACTAL_ZOOM_IN_PRE)
184 break;
185 #endif
186 #ifdef FRACTAL_ZOOM_IN2
187 case FRACTAL_ZOOM_IN2:
188 #endif
189 if (!ops->zoom(1))
190 redraw = REDRAW_FULL;
191 break;
193 case FRACTAL_UP:
194 ops->move(0, +1);
195 mylcd_ub_scroll_down(LCD_SHIFT_Y);
196 mylcd_ub_update();
197 if (redraw != REDRAW_FULL)
198 redraw = rects_move_down() ? REDRAW_FULL : REDRAW_PARTIAL;
199 break;
201 case FRACTAL_DOWN:
202 ops->move(0, -1);
203 mylcd_ub_scroll_up(LCD_SHIFT_Y);
204 mylcd_ub_update();
205 if (redraw != REDRAW_FULL)
206 redraw = rects_move_up() ? REDRAW_FULL : REDRAW_PARTIAL;
207 break;
209 case FRACTAL_LEFT:
210 ops->move(-1, 0);
211 mylcd_ub_scroll_right(LCD_SHIFT_X);
212 mylcd_ub_update();
213 if (redraw != REDRAW_FULL)
214 redraw = rects_move_right() ? REDRAW_FULL : REDRAW_PARTIAL;
215 break;
217 case FRACTAL_RIGHT:
218 ops->move(+1, 0);
219 mylcd_ub_scroll_left(LCD_SHIFT_X);
220 mylcd_ub_update();
221 if (redraw != REDRAW_FULL)
222 redraw = rects_move_left() ? REDRAW_FULL : REDRAW_PARTIAL;
223 break;
225 case FRACTAL_PRECISION_DEC:
226 #ifdef FRACTAL_PRECISION_DEC_PRE
227 if (lastbutton != FRACTAL_PRECISION_DEC_PRE)
228 break;
229 #endif
230 if (ops->precision(-1))
231 redraw = REDRAW_FULL_OVERLAY;
233 break;
235 case FRACTAL_PRECISION_INC:
236 #ifdef FRACTAL_PRECISION_INC_PRE
237 if (lastbutton != FRACTAL_PRECISION_INC_PRE)
238 break;
239 #endif
240 if (ops->precision(+1))
241 redraw = REDRAW_FULL_OVERLAY;
243 break;
245 case FRACTAL_RESET:
246 ops->init();
247 redraw = REDRAW_FULL;
248 break;
250 default:
251 exit_on_usb(button);
252 break;
254 #if defined(FRACTAL_ZOOM_OUT_PRE) || \
255 defined(FRACTAL_ZOOM_IN_PRE) || \
256 defined(FRACTAL_PRECISION_DEC_PRE) || \
257 defined(FRACTAL_PRECISION_INC_PRE)
258 if (button != BUTTON_NONE)
259 lastbutton = button;
260 #endif
262 return PLUGIN_OK;