Prepare new maemo release
[maemo-rb.git] / apps / plugins / demystify.c
bloba389018c4ea6e83c87745dbafd92ea47d76b1049
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Kevin Ferrare
12 * Mystify demo plugin
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ****************************************************************************/
24 #include "plugin.h"
25 #include "lib/pluginlib_exit.h"
27 #include "lib/pluginlib_actions.h"
28 #include "lib/helper.h"
31 #define DEFAULT_WAIT_TIME 3
32 #define DEFAULT_NB_POLYGONS 7
33 #define NB_POINTS 4
34 #define MAX_STEP_RANGE 7
35 #define MIN_STEP_RANGE 3
36 #define MAX_POLYGONS 40
37 #define MIN_POLYGONS 1
39 /* Key assignement */
40 #define DEMYSTIFY_QUIT PLA_CANCEL
42 #ifdef HAVE_SCROLLWHEEL
44 #define DEMYSTIFY_INCREASE_SPEED PLA_SCROLL_FWD
45 #define DEMYSTIFY_DECREASE_SPEED PLA_SCROLL_BACK
46 #define DEMYSTIFY_INCREASE_SPEED_REPEAT PLA_SCROLL_FWD_REPEAT
47 #define DEMYSTIFY_DECREASE_SPEED_REPEAT PLA_SCROLL_BACK_REPEAT
48 #else
49 #define DEMYSTIFY_INCREASE_SPEED PLA_RIGHT
50 #define DEMYSTIFY_DECREASE_SPEED PLA_LEFT
51 #define DEMYSTIFY_INCREASE_SPEED_REPEAT PLA_RIGHT_REPEAT
52 #define DEMYSTIFY_DECREASE_SPEED_REPEAT PLA_LEFT_REPEAT
53 #endif
55 #define DEMYSTIFY_ADD_POLYGON PLA_UP
56 #define DEMYSTIFY_REMOVE_POLYGON PLA_DOWN
57 #define DEMYSTIFY_ADD_POLYGON_REPEAT PLA_UP_REPEAT
58 #define DEMYSTIFY_REMOVE_POLYGON_REPEAT PLA_DOWN_REPEAT
60 const struct button_mapping *plugin_contexts[]
61 = {pla_main_ctx,
62 #if defined(HAVE_REMOTE_LCD)
63 pla_remote_ctx,
64 #endif
67 #ifdef HAVE_LCD_COLOR
68 struct line_color
70 int r,g,b;
71 int current_r,current_g,current_b;
73 #endif
75 /******************************* Globals ***********************************/
78 * Compute a new random step to make the point bounce the borders of the screen
81 static int get_new_step(int step)
83 if(step>0)
84 return -(MIN_STEP_RANGE + rb->rand() % (MAX_STEP_RANGE-MIN_STEP_RANGE));
85 else
86 return (MIN_STEP_RANGE + rb->rand() % (MAX_STEP_RANGE-MIN_STEP_RANGE));
90 * Point Stuffs
93 struct point
95 int x;
96 int y;
100 * Polygon Stuffs
103 struct polygon
105 struct point points[NB_POINTS];
109 * Generates a random polygon (which fits the screen size though)
111 static void polygon_init(struct polygon * polygon, struct screen * display)
113 int i;
114 for(i=0;i<NB_POINTS;++i)
116 polygon->points[i].x=(rb->rand() % (display->getwidth()));
117 polygon->points[i].y=(rb->rand() % (display->getheight()));
122 * Draw the given polygon onto the screen
125 static void polygon_draw(struct polygon * polygon, struct screen * display)
127 int i;
128 for(i=0;i<NB_POINTS-1;++i)
130 display->drawline(polygon->points[i].x, polygon->points[i].y,
131 polygon->points[i+1].x, polygon->points[i+1].y);
133 display->drawline(polygon->points[0].x, polygon->points[0].y,
134 polygon->points[NB_POINTS-1].x,
135 polygon->points[NB_POINTS-1].y);
139 * Polygon moving data Stuffs
142 struct polygon_move
144 struct point move_steps[NB_POINTS];
147 static void polygon_move_init(struct polygon_move * polygon_move)
149 int i;
150 for(i=0;i<NB_POINTS;++i)
152 polygon_move->move_steps[i].x=get_new_step(-1);
153 /* -1 because we want a positive random step */
154 polygon_move->move_steps[i].y=get_new_step(-1);
159 * Update the given polygon's position according to the given informations in
160 * polygon_move (polygon_move may be updated)
162 static void polygon_update(struct polygon *polygon, struct screen * display,
163 struct polygon_move *polygon_move)
165 int i, x, y, step;
166 for(i=0;i<NB_POINTS;++i)
168 x=polygon->points[i].x;
169 step=polygon_move->move_steps[i].x;
170 x+=step;
171 if(x<=0)
173 x=1;
174 polygon_move->move_steps[i].x=get_new_step(step);
176 else if(x>=display->getwidth())
178 x=display->getwidth()-1;
179 polygon_move->move_steps[i].x=get_new_step(step);
181 polygon->points[i].x=x;
183 y=polygon->points[i].y;
184 step=polygon_move->move_steps[i].y;
185 y+=step;
186 if(y<=0)
188 y=1;
189 polygon_move->move_steps[i].y=get_new_step(step);
191 else if(y>=display->getheight())
193 y=display->getheight()-1;
194 polygon_move->move_steps[i].y=get_new_step(step);
196 polygon->points[i].y=y;
201 * Polygon fifo Stuffs
204 struct polygon_fifo
206 int fifo_tail;
207 int fifo_head;
208 int nb_items;
209 struct polygon tab[MAX_POLYGONS];
212 static void fifo_init(struct polygon_fifo * fifo)
214 fifo->fifo_tail=0;
215 fifo->fifo_head=0;
216 fifo->nb_items=0;
219 static void fifo_push(struct polygon_fifo * fifo, struct polygon * polygon)
221 if(fifo->nb_items>=MAX_POLYGONS)
222 return;
223 ++(fifo->nb_items);
226 * Workaround for gcc (which uses memcpy internally) to avoid link error
227 * fifo->tab[fifo->fifo_head]=polygon
229 rb->memcpy(&(fifo->tab[fifo->fifo_head]), polygon, sizeof(struct polygon));
230 ++(fifo->fifo_head);
231 if(fifo->fifo_head>=MAX_POLYGONS)
232 fifo->fifo_head=0;
235 static struct polygon * fifo_pop(struct polygon_fifo * fifo)
237 int index;
238 if(fifo->nb_items==0)
239 return(NULL);
240 --(fifo->nb_items);
241 index=fifo->fifo_tail;
242 ++(fifo->fifo_tail);
243 if(fifo->fifo_tail>=MAX_POLYGONS)
244 fifo->fifo_tail=0;
245 return(&(fifo->tab[index]));
249 * Drawing stuffs
252 static void polygons_draw(struct polygon_fifo * polygons, struct screen * display)
254 int i, j;
255 for(i=0, j=polygons->fifo_tail;i<polygons->nb_items;++i, ++j)
257 if(j>=MAX_POLYGONS)
258 j=0;
259 polygon_draw(&(polygons->tab[j]), display);
263 static void cleanup(void)
265 backlight_use_settings();
266 #ifdef HAVE_REMOTE_LCD
267 remote_backlight_use_settings();
268 #endif
271 #ifdef HAVE_LCD_COLOR
272 static void color_randomize(struct line_color * color)
274 color->r = rb->rand()%255;
275 color->g = rb->rand()%255;
276 color->b = rb->rand()%255;
279 static void color_init(struct line_color * color)
281 color_randomize(color);
282 color->current_r=color->r;
283 color->current_g=color->g;
284 color->current_b=color->b;
287 static void color_change(struct line_color * color)
289 if(color->current_r<color->r)
290 ++color->current_r;
291 else if(color->current_r>color->r)
292 --color->current_r;
293 if(color->current_g<color->g)
294 ++color->current_g;
295 else if(color->current_g>color->g)
296 --color->current_g;
297 if(color->current_b<color->b)
298 ++color->current_b;
299 else if(color->current_b>color->b)
300 --color->current_b;
302 if(color->current_r==color->r &&
303 color->current_g==color->g &&
304 color->current_b==color->b)
305 color_randomize(color);
308 #define COLOR_RGBPACK(color) \
309 LCD_RGBPACK((color)->current_r, (color)->current_g, (color)->current_b)
311 static void color_apply(struct line_color * color, struct screen * display)
313 if (display->is_color){
314 unsigned foreground=
315 SCREEN_COLOR_TO_NATIVE(display,COLOR_RGBPACK(color));
316 display->set_foreground(foreground);
319 #endif
322 * Main function
325 static int plugin_main(void)
327 int action;
328 int sleep_time=DEFAULT_WAIT_TIME;
329 int nb_wanted_polygons=DEFAULT_NB_POLYGONS;
330 struct polygon_fifo polygons[NB_SCREENS];
331 struct polygon_move move[NB_SCREENS]; /* This describes the movement of the leading
332 polygon, the others just follow */
333 struct polygon leading_polygon[NB_SCREENS];
334 FOR_NB_SCREENS(i)
336 #ifdef HAVE_LCD_COLOR
337 struct screen *display = rb->screens[i];
338 if (display->is_color)
339 display->set_background(LCD_BLACK);
340 #endif
341 fifo_init(&polygons[i]);
342 polygon_move_init(&move[i]);
343 polygon_init(&leading_polygon[i], rb->screens[i]);
346 #ifdef HAVE_LCD_COLOR
347 struct line_color color;
348 color_init(&color);
349 #endif
351 while (true)
353 FOR_NB_SCREENS(i)
355 struct screen * display=rb->screens[i];
356 if(polygons[i].nb_items>nb_wanted_polygons)
357 { /* We have too many polygons, we must drop some of them */
358 fifo_pop(&polygons[i]);
360 if(nb_wanted_polygons==polygons[i].nb_items)
361 { /* We have the good number of polygons, we can safely drop
362 the last one to add the new one later */
363 fifo_pop(&polygons[i]);
365 fifo_push(&polygons[i], &leading_polygon[i]);
368 * Then we update the leading polygon for the next round acording to
369 * current move (the move may be altered in case of sreen border
370 * collision)
372 polygon_update(&leading_polygon[i], display, &move[i]);
374 /* Now the drawing part */
375 #ifdef HAVE_LCD_COLOR
376 color_apply(&color, display);
377 #endif
378 display->clear_display();
379 polygons_draw(&polygons[i], display);
380 display->update();
382 #ifdef HAVE_LCD_COLOR
383 color_change(&color);
384 #endif
385 /* Speed handling*/
386 if (sleep_time<0)/* full speed */
387 rb->yield();
388 else
389 rb->sleep(sleep_time);
390 action = pluginlib_getaction(TIMEOUT_NOBLOCK,
391 plugin_contexts, ARRAYLEN(plugin_contexts));
392 switch(action)
394 case DEMYSTIFY_QUIT:
395 return PLUGIN_OK;
397 case DEMYSTIFY_ADD_POLYGON:
398 case DEMYSTIFY_ADD_POLYGON_REPEAT:
399 if(nb_wanted_polygons<MAX_POLYGONS)
400 ++nb_wanted_polygons;
401 break;
403 case DEMYSTIFY_REMOVE_POLYGON:
404 case DEMYSTIFY_REMOVE_POLYGON_REPEAT:
405 if(nb_wanted_polygons>MIN_POLYGONS)
406 --nb_wanted_polygons;
407 break;
409 case DEMYSTIFY_INCREASE_SPEED:
410 case DEMYSTIFY_INCREASE_SPEED_REPEAT:
411 if(sleep_time>=0)
412 --sleep_time;
413 break;
415 case DEMYSTIFY_DECREASE_SPEED:
416 case DEMYSTIFY_DECREASE_SPEED_REPEAT:
417 ++sleep_time;
418 break;
420 default:
421 exit_on_usb(action);
422 break;
427 /*************************** Plugin entry point ****************************/
429 enum plugin_status plugin_start(const void* parameter)
431 int ret;
433 (void)parameter;
434 atexit(cleanup);
436 #if LCD_DEPTH > 1
437 rb->lcd_set_backdrop(NULL);
438 #endif
439 backlight_ignore_timeout();
440 #ifdef HAVE_REMOTE_LCD
441 remote_backlight_ignore_timeout();
442 #endif
443 ret = plugin_main();
445 return ret;