Get sims.pl working again after target rename
[kugel-rb.git] / apps / plugins / mpegplayer / mpegplayer.c
blob31b7c68373b16ba6694295186ce3f5d35cf8cc19
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * mpegplayer main entrypoint and UI implementation
12 * Copyright (c) 2007 Michael Sevakis
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 /****************************************************************************
25 * NOTES:
27 * mpegplayer is structured as follows:
29 * +-->Video Thread-->Video Output-->LCD
30 * |
31 * UI-->Stream Manager-->+-->Audio Thread-->PCM buffer--Audio Device
32 * | | | | (ref. clock)
33 * | | +-->Buffer Thread |
34 * Stream Data | | (clock intf./
35 * Requests | File Cache drift adj.)
36 * | Disk I/O
37 * Stream services
38 * (timing, etc.)
40 * Thread list:
41 * 1) The main thread - Handles user input, settings, basic playback control
42 * and USB connect.
44 * 2) Stream Manager thread - Handles playback state, events from streams
45 * such as when a stream is finished, stream commands, PCM state. The
46 * layer in which this thread run also handles arbitration of data
47 * requests between the streams and the disk buffer. The actual specific
48 * transport layer code may get moved out to support multiple container
49 * formats.
51 * 3) Buffer thread - Buffers data in the background, generates notifications
52 * to streams when their data has been buffered, and watches streams'
53 * progress to keep data available during playback. Handles synchronous
54 * random access requests when the file cache is missed.
56 * 4) Video thread (running on the COP for PortalPlayer targets) - Decodes
57 * the video stream and renders video frames to the LCD. Handles
58 * miscellaneous video tasks like frame and thumbnail printing.
60 * 5) Audio thread (running on the main CPU to maintain consistency with the
61 * audio FIQ hander on PP) - Decodes audio frames and places them into
62 * the PCM buffer for rendering by the audio device.
64 * Streams are neither aware of one another nor care about one another. All
65 * streams shall have their own thread (unless it is _really_ efficient to
66 * have a single thread handle a couple minor streams). All coordination of
67 * the streams is done through the stream manager. The clocking is controlled
68 * by and exposed by the stream manager to other streams and implemented at
69 * the PCM level.
71 * Notes about MPEG files:
73 * MPEG System Clock is 27MHz - i.e. 27000000 ticks/second.
75 * FPS is represented in terms of a frame period - this is always an
76 * integer number of 27MHz ticks.
78 * e.g. 29.97fps (30000/1001) NTSC video has an exact frame period of
79 * 900900 27MHz ticks.
81 * In libmpeg2, info->sequence->frame_period contains the frame_period.
83 * Working with Rockbox's 100Hz tick, the common frame rates would need
84 * to be as follows (1):
86 * FPS | 27Mhz | 100Hz | 44.1KHz | 48KHz
87 * --------|-----------------------------------------------------------
88 * 10* | 2700000 | 10 | 4410 | 4800
89 * 12* | 2250000 | 8.3333 | 3675 | 4000
90 * 15* | 1800000 | 6.6667 | 2940 | 3200
91 * 23.9760 | 1126125 | 4.170833333 | 1839.3375 | 2002
92 * 24 | 1125000 | 4.166667 | 1837.5 | 2000
93 * 25 | 1080000 | 4 | 1764 | 1920
94 * 29.9700 | 900900 | 3.336667 | 1471,47 | 1601.6
95 * 30 | 900000 | 3.333333 | 1470 | 1600
97 * *Unofficial framerates
99 * (1) But we don't really care since the audio clock is used anyway and has
100 * very fine resolution ;-)
101 *****************************************************************************/
102 #include "plugin.h"
103 #include "mpegplayer.h"
104 #include "lib/helper.h"
105 #include "mpeg_settings.h"
106 #include "mpeg2.h"
107 #include "video_out.h"
108 #include "stream_thread.h"
109 #include "stream_mgr.h"
111 PLUGIN_HEADER
112 PLUGIN_IRAM_DECLARE
114 /* button definitions */
115 #if (CONFIG_KEYPAD == IRIVER_H100_PAD) || (CONFIG_KEYPAD == IRIVER_H300_PAD)
116 #define MPEG_MENU BUTTON_MODE
117 #define MPEG_STOP BUTTON_OFF
118 #define MPEG_PAUSE BUTTON_ON
119 #define MPEG_VOLDOWN BUTTON_DOWN
120 #define MPEG_VOLUP BUTTON_UP
121 #define MPEG_RW BUTTON_LEFT
122 #define MPEG_FF BUTTON_RIGHT
124 #elif (CONFIG_KEYPAD == IPOD_4G_PAD) || (CONFIG_KEYPAD == IPOD_3G_PAD) || \
125 (CONFIG_KEYPAD == IPOD_1G2G_PAD)
126 #define MPEG_MENU BUTTON_MENU
127 #define MPEG_PAUSE (BUTTON_PLAY | BUTTON_REL)
128 #define MPEG_STOP (BUTTON_PLAY | BUTTON_REPEAT)
129 #define MPEG_VOLDOWN BUTTON_SCROLL_BACK
130 #define MPEG_VOLUP BUTTON_SCROLL_FWD
131 #define MPEG_RW BUTTON_LEFT
132 #define MPEG_FF BUTTON_RIGHT
134 #elif CONFIG_KEYPAD == IAUDIO_X5M5_PAD
135 #define MPEG_MENU (BUTTON_REC | BUTTON_REL)
136 #define MPEG_STOP BUTTON_POWER
137 #define MPEG_PAUSE BUTTON_PLAY
138 #define MPEG_VOLDOWN BUTTON_DOWN
139 #define MPEG_VOLUP BUTTON_UP
140 #define MPEG_RW BUTTON_LEFT
141 #define MPEG_FF BUTTON_RIGHT
143 #elif CONFIG_KEYPAD == GIGABEAT_PAD
144 #define MPEG_MENU BUTTON_MENU
145 #define MPEG_STOP BUTTON_POWER
146 #define MPEG_PAUSE BUTTON_SELECT
147 #define MPEG_PAUSE2 BUTTON_A
148 #define MPEG_VOLDOWN BUTTON_LEFT
149 #define MPEG_VOLUP BUTTON_RIGHT
150 #define MPEG_VOLDOWN2 BUTTON_VOL_DOWN
151 #define MPEG_VOLUP2 BUTTON_VOL_UP
152 #define MPEG_RW BUTTON_UP
153 #define MPEG_FF BUTTON_DOWN
154 #define MPEG_RC_MENU BUTTON_RC_DSP
155 #define MPEG_RC_STOP (BUTTON_RC_PLAY | BUTTON_REPEAT)
156 #define MPEG_RC_PAUSE (BUTTON_RC_PLAY | BUTTON_REL)
157 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
158 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
159 #define MPEG_RC_RW BUTTON_RC_REW
160 #define MPEG_RC_FF BUTTON_RC_FF
162 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
163 #define MPEG_MENU BUTTON_MENU
164 #define MPEG_STOP BUTTON_POWER
165 #define MPEG_PAUSE BUTTON_SELECT
166 #define MPEG_PAUSE2 BUTTON_PLAY
167 #define MPEG_VOLDOWN BUTTON_LEFT
168 #define MPEG_VOLUP BUTTON_RIGHT
169 #define MPEG_VOLDOWN2 BUTTON_VOL_DOWN
170 #define MPEG_VOLUP2 BUTTON_VOL_UP
171 #define MPEG_RW BUTTON_UP
172 #define MPEG_FF BUTTON_DOWN
173 #define MPEG_RC_MENU BUTTON_RC_DSP
174 #define MPEG_RC_STOP (BUTTON_RC_PLAY | BUTTON_REPEAT)
175 #define MPEG_RC_PAUSE (BUTTON_RC_PLAY | BUTTON_REL)
176 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
177 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
178 #define MPEG_RC_RW BUTTON_RC_REW
179 #define MPEG_RC_FF BUTTON_RC_FF
181 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
182 #define MPEG_MENU BUTTON_LEFT
183 #define MPEG_STOP BUTTON_POWER
184 #define MPEG_PAUSE BUTTON_PLAY
185 #define MPEG_VOLDOWN BUTTON_SCROLL_DOWN
186 #define MPEG_VOLUP BUTTON_SCROLL_UP
187 #define MPEG_RW BUTTON_REW
188 #define MPEG_FF BUTTON_FF
190 #elif CONFIG_KEYPAD == SANSA_E200_PAD
191 #define MPEG_MENU BUTTON_SELECT
192 #define MPEG_STOP BUTTON_POWER
193 #define MPEG_PAUSE BUTTON_RIGHT
194 #define MPEG_VOLDOWN BUTTON_SCROLL_BACK
195 #define MPEG_VOLUP BUTTON_SCROLL_FWD
196 #define MPEG_RW BUTTON_UP
197 #define MPEG_FF BUTTON_DOWN
199 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
200 #define MPEG_MENU BUTTON_SELECT
201 #define MPEG_STOP (BUTTON_HOME|BUTTON_REPEAT)
202 #define MPEG_PAUSE BUTTON_UP
203 #define MPEG_VOLDOWN BUTTON_SCROLL_BACK
204 #define MPEG_VOLUP BUTTON_SCROLL_FWD
205 #define MPEG_RW BUTTON_LEFT
206 #define MPEG_FF BUTTON_RIGHT
209 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
210 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
211 CONFIG_KEYPAD == SANSA_M200_PAD
212 #define MPEG_MENU BUTTON_SELECT
213 #define MPEG_STOP BUTTON_POWER
214 #define MPEG_PAUSE BUTTON_UP
215 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
216 #define MPEG_VOLUP BUTTON_VOL_UP
217 #define MPEG_RW BUTTON_LEFT
218 #define MPEG_FF BUTTON_RIGHT
220 #elif CONFIG_KEYPAD == MROBE500_PAD
221 #define MPEG_STOP BUTTON_POWER
223 #define MPEG_RC_MENU BUTTON_RC_HEART
224 #define MPEG_RC_STOP BUTTON_RC_DOWN
225 #define MPEG_RC_PAUSE BUTTON_RC_PLAY
226 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
227 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
228 #define MPEG_RC_RW BUTTON_RC_REW
229 #define MPEG_RC_FF BUTTON_RC_FF
231 #elif CONFIG_KEYPAD == MROBE100_PAD
232 #define MPEG_MENU BUTTON_MENU
233 #define MPEG_STOP BUTTON_POWER
234 #define MPEG_PAUSE BUTTON_PLAY
235 #define MPEG_VOLDOWN BUTTON_DOWN
236 #define MPEG_VOLUP BUTTON_UP
237 #define MPEG_RW BUTTON_LEFT
238 #define MPEG_FF BUTTON_RIGHT
240 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
241 #define MPEG_MENU BUTTON_RC_MENU
242 #define MPEG_STOP BUTTON_RC_REC
243 #define MPEG_PAUSE BUTTON_RC_PLAY
244 #define MPEG_VOLDOWN BUTTON_RC_VOL_DOWN
245 #define MPEG_VOLUP BUTTON_RC_VOL_UP
246 #define MPEG_RW BUTTON_RC_REW
247 #define MPEG_FF BUTTON_RC_FF
249 #elif CONFIG_KEYPAD == COWOND2_PAD
250 #define MPEG_MENU (BUTTON_MENU|BUTTON_REL)
251 //#define MPEG_STOP BUTTON_POWER
252 #define MPEG_VOLDOWN BUTTON_MINUS
253 #define MPEG_VOLUP BUTTON_PLUS
255 #elif CONFIG_KEYPAD == IAUDIO67_PAD
256 #define MPEG_MENU BUTTON_MENU
257 #define MPEG_STOP BUTTON_STOP
258 #define MPEG_PAUSE BUTTON_PLAY
259 #define MPEG_VOLDOWN BUTTON_VOLDOWN
260 #define MPEG_VOLUP BUTTON_VOLUP
261 #define MPEG_RW BUTTON_LEFT
262 #define MPEG_FF BUTTON_RIGHT
264 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
265 #define MPEG_MENU BUTTON_MENU
266 #define MPEG_STOP BUTTON_BACK
267 #define MPEG_PAUSE BUTTON_PLAY
268 #define MPEG_VOLDOWN BUTTON_UP
269 #define MPEG_VOLUP BUTTON_DOWN
270 #define MPEG_RW BUTTON_LEFT
271 #define MPEG_FF BUTTON_RIGHT
273 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
274 #define MPEG_MENU BUTTON_MENU
275 #define MPEG_STOP BUTTON_POWER
276 #define MPEG_PAUSE BUTTON_SELECT
277 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
278 #define MPEG_VOLUP BUTTON_VOL_UP
279 #define MPEG_RW BUTTON_LEFT
280 #define MPEG_FF BUTTON_RIGHT
282 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
283 #define MPEG_MENU BUTTON_MENU
284 #define MPEG_STOP BUTTON_POWER
285 #define MPEG_PAUSE BUTTON_PLAY
286 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
287 #define MPEG_VOLUP BUTTON_VOL_UP
288 #define MPEG_RW BUTTON_UP
289 #define MPEG_FF BUTTON_DOWN
291 #elif CONFIG_KEYPAD == ONDAVX747_PAD
292 #define MPEG_MENU (BUTTON_MENU|BUTTON_REL)
293 //#define MPEG_STOP BUTTON_POWER
294 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
295 #define MPEG_VOLUP BUTTON_VOL_UP
297 #elif CONFIG_KEYPAD == ONDAVX777_PAD
298 #define MPEG_MENU BUTTON_POWER
300 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
301 #define MPEG_MENU BUTTON_LEFT
302 #define MPEG_STOP BUTTON_RIGHT
303 #define MPEG_PAUSE BUTTON_PLAY
304 #define MPEG_VOLDOWN BUTTON_DOWN
305 #define MPEG_VOLUP BUTTON_UP
306 #define MPEG_RW BUTTON_REW
307 #define MPEG_FF BUTTON_FFWD
309 #else
310 #error No keymap defined!
311 #endif
313 #ifdef HAVE_TOUCHSCREEN
314 #ifndef MPEG_MENU
315 #define MPEG_MENU (BUTTON_TOPRIGHT|BUTTON_REL)
316 #endif
317 #ifndef MPEG_STOP
318 #define MPEG_STOP BUTTON_TOPLEFT
319 #endif
320 #ifndef MPEG_PAUSE
321 #define MPEG_PAUSE BUTTON_CENTER
322 #endif
323 #ifndef MPEG_VOLDOWN
324 #define MPEG_VOLDOWN BUTTON_BOTTOMMIDDLE
325 #endif
326 #ifndef MPEG_VOLUP
327 #define MPEG_VOLUP BUTTON_TOPMIDDLE
328 #endif
329 #ifndef MPEG_RW
330 #define MPEG_RW BUTTON_MIDLEFT
331 #endif
332 #ifndef MPEG_FF
333 #define MPEG_FF BUTTON_MIDRIGHT
334 #endif
335 #endif
337 /* One thing we can do here for targets with remotes is having a display
338 * always on the remote instead of always forcing a popup on the main display */
340 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
341 /* 3% of 30min file == 54s step size */
342 #define MIN_FF_REWIND_STEP (TS_SECOND/2)
343 #define WVS_MIN_UPDATE_INTERVAL (HZ/2)
345 /* WVS status - same order as icon array */
346 enum wvs_status_enum
348 WVS_STATUS_STOPPED = 0,
349 WVS_STATUS_PAUSED,
350 WVS_STATUS_PLAYING,
351 WVS_STATUS_FF,
352 WVS_STATUS_RW,
353 WVS_STATUS_COUNT,
354 WVS_STATUS_MASK = 0x7
357 enum wvs_bits
359 WVS_REFRESH_DEFAULT = 0x0000, /* Only refresh elements when due */
360 /* Refresh the... */
361 WVS_REFRESH_VOLUME = 0x0001, /* ...volume display */
362 WVS_REFRESH_TIME = 0x0002, /* ...time display+progress */
363 WVS_REFRESH_STATUS = 0x0004, /* ...playback status icon */
364 WVS_REFRESH_BACKGROUND = 0x0008, /* ...background (implies ALL) */
365 WVS_REFRESH_VIDEO = 0x0010, /* ...video image upon timeout */
366 WVS_REFRESH_RESUME = 0x0020, /* Resume playback upon timeout */
367 WVS_NODRAW = 0x8000, /* OR bitflag - don't draw anything */
368 WVS_SHOW = 0x4000, /* OR bitflag - show the WVS */
369 WVS_HP_PAUSE = 0x2000,
370 WVS_HIDE = 0x0000, /* hide the WVS (aid readability) */
371 WVS_REFRESH_ALL = 0x000f, /* Only immediate graphical elements */
374 /* Status icons selected according to font height */
375 extern const unsigned char mpegplayer_status_icons_8x8x1[];
376 extern const unsigned char mpegplayer_status_icons_12x12x1[];
377 extern const unsigned char mpegplayer_status_icons_16x16x1[];
379 /* Main border areas that contain WVS elements */
380 #define WVS_BDR_L 2
381 #define WVS_BDR_T 2
382 #define WVS_BDR_R 2
383 #define WVS_BDR_B 2
385 struct wvs
387 long hide_tick;
388 long show_for;
389 long print_tick;
390 long print_delay;
391 long resume_tick;
392 long resume_delay;
393 long next_auto_refresh;
394 int x;
395 int y;
396 int width;
397 int height;
398 unsigned fgcolor;
399 unsigned bgcolor;
400 unsigned prog_fillcolor;
401 struct vo_rect update_rect;
402 struct vo_rect prog_rect;
403 struct vo_rect time_rect;
404 struct vo_rect dur_rect;
405 struct vo_rect vol_rect;
406 const unsigned char *icons;
407 struct vo_rect stat_rect;
408 int status;
409 uint32_t curr_time;
410 unsigned auto_refresh;
411 unsigned flags;
414 static struct wvs wvs;
416 static void wvs_show(unsigned show);
418 #ifdef LCD_LANDSCAPE
419 #define _X (x + wvs.x)
420 #define _Y (y + wvs.y)
421 #define _W width
422 #define _H height
423 #else
424 #define _X (LCD_WIDTH - (y + wvs.y) - height)
425 #define _Y (x + wvs.x)
426 #define _W height
427 #define _H width
428 #endif
430 #ifdef HAVE_LCD_COLOR
431 /* Blend two colors in 0-100% (0-255) mix of c2 into c1 */
432 static unsigned draw_blendcolor(unsigned c1, unsigned c2, unsigned char amount)
434 int r1 = RGB_UNPACK_RED(c1);
435 int g1 = RGB_UNPACK_GREEN(c1);
436 int b1 = RGB_UNPACK_BLUE(c1);
438 int r2 = RGB_UNPACK_RED(c2);
439 int g2 = RGB_UNPACK_GREEN(c2);
440 int b2 = RGB_UNPACK_BLUE(c2);
442 return LCD_RGBPACK(amount*(r2 - r1) / 255 + r1,
443 amount*(g2 - g1) / 255 + g1,
444 amount*(b2 - b1) / 255 + b1);
446 #endif
448 /* Drawing functions that operate rotated on LCD_PORTRAIT displays -
449 * most are just wrappers of lcd_* functions with transforms applied.
450 * The origin is the upper-left corner of the WVS area */
451 static void draw_update_rect(int x, int y, int width, int height)
453 lcd_(update_rect)(_X, _Y, _W, _H);
456 static void draw_clear_area(int x, int y, int width, int height)
458 #ifdef HAVE_LCD_COLOR
459 rb->screen_clear_area(rb->screens[SCREEN_MAIN], _X, _Y, _W, _H);
460 #else
461 int oldmode = grey_get_drawmode();
462 grey_set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
463 grey_fillrect(_X, _Y, _W, _H);
464 grey_set_drawmode(oldmode);
465 #endif
468 static void draw_clear_area_rect(const struct vo_rect *rc)
470 int x = rc->l;
471 int y = rc->t;
472 int width = rc->r - rc->l;
473 int height = rc->b - rc->t;
474 #ifdef HAVE_LCD_COLOR
475 rb->screen_clear_area(rb->screens[SCREEN_MAIN], _X, _Y, _W, _H);
476 #else
477 int oldmode = grey_get_drawmode();
478 grey_set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
479 grey_fillrect(_X, _Y, _W, _H);
480 grey_set_drawmode(oldmode);
481 #endif
484 static void draw_fillrect(int x, int y, int width, int height)
486 lcd_(fillrect)(_X, _Y, _W, _H);
489 static void draw_hline(int x1, int x2, int y)
491 #ifdef LCD_LANDSCAPE
492 lcd_(hline)(x1 + wvs.x, x2 + wvs.x, y + wvs.y);
493 #else
494 y = LCD_WIDTH - (y + wvs.y) - 1;
495 lcd_(vline)(y, x1 + wvs.x, x2 + wvs.x);
496 #endif
499 static void draw_vline(int x, int y1, int y2)
501 #ifdef LCD_LANDSCAPE
502 lcd_(vline)(x + wvs.x, y1 + wvs.y, y2 + wvs.y);
503 #else
504 y1 = LCD_WIDTH - (y1 + wvs.y) - 1;
505 y2 = LCD_WIDTH - (y2 + wvs.y) - 1;
506 lcd_(hline)(y1, y2, x + wvs.x);
507 #endif
510 static void draw_scrollbar_draw(int x, int y, int width, int height,
511 uint32_t min, uint32_t max, uint32_t val)
513 unsigned oldfg = lcd_(get_foreground)();
515 draw_hline(x + 1, x + width - 2, y);
516 draw_hline(x + 1, x + width - 2, y + height - 1);
517 draw_vline(x, y + 1, y + height - 2);
518 draw_vline(x + width - 1, y + 1, y + height - 2);
520 val = muldiv_uint32(width - 2, val, max - min);
521 val = MIN(val, (uint32_t)(width - 2));
523 draw_fillrect(x + 1, y + 1, val, height - 2);
525 lcd_(set_foreground)(wvs.prog_fillcolor);
527 draw_fillrect(x + 1 + val, y + 1, width - 2 - val, height - 2);
529 lcd_(set_foreground)(oldfg);
532 static void draw_scrollbar_draw_rect(const struct vo_rect *rc, int min,
533 int max, int val)
535 draw_scrollbar_draw(rc->l, rc->t, rc->r - rc->l, rc->b - rc->t,
536 min, max, val);
539 #ifdef LCD_PORTRAIT
540 /* Portrait displays need rotated text rendering */
542 /* Limited function that only renders in DRMODE_FG and uses absolute screen
543 * coordinates */
544 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
545 int src_x, int src_y,
546 int stride, int x, int y,
547 int width, int height)
549 const unsigned char *src_end;
550 fb_data *dst, *dst_end;
551 unsigned fg_pattern, bg_pattern;
553 if (x + width > SCREEN_WIDTH)
554 width = SCREEN_WIDTH - x; /* Clip right */
555 if (x < 0)
556 width += x, x = 0; /* Clip left */
557 if (width <= 0)
558 return; /* nothing left to do */
560 if (y + height > SCREEN_HEIGHT)
561 height = SCREEN_HEIGHT - y; /* Clip bottom */
562 if (y < 0)
563 height += y, y = 0; /* Clip top */
564 if (height <= 0)
565 return; /* nothing left to do */
567 fg_pattern = rb->lcd_get_foreground();
568 bg_pattern = rb->lcd_get_background();
570 src += stride * (src_y >> 3) + src_x; /* move starting point */
571 src_y &= 7;
572 src_end = src + width;
574 dst = rb->lcd_framebuffer + (LCD_WIDTH - y) + x*LCD_WIDTH;
577 const unsigned char *src_col = src++;
578 unsigned data = *src_col >> src_y;
579 int numbits = 8 - src_y;
581 fb_data *dst_col = dst;
582 dst_end = dst_col - height;
583 dst += LCD_WIDTH;
587 dst_col--;
589 if (data & 1)
590 *dst_col = fg_pattern;
591 #if 0
592 else
593 *dst_col = bg_pattern;
594 #endif
595 data >>= 1;
596 if (--numbits == 0) {
597 src_col += stride;
598 data = *src_col;
599 numbits = 8;
602 while (dst_col > dst_end);
604 while (src < src_end);
607 static void draw_putsxy_oriented(int x, int y, const char *str)
609 unsigned short ch;
610 unsigned short *ucs;
611 int ofs = MIN(x, 0);
612 struct font* pf = rb->font_get(FONT_UI);
614 ucs = rb->bidi_l2v(str, 1);
616 x += wvs.x;
617 y += wvs.y;
619 while ((ch = *ucs++) != 0 && x < SCREEN_WIDTH)
621 int width;
622 const unsigned char *bits;
624 /* get proportional width and glyph bits */
625 width = rb->font_get_width(pf, ch);
627 if (ofs > width) {
628 ofs -= width;
629 continue;
632 bits = rb->font_get_bits(pf, ch);
634 draw_oriented_mono_bitmap_part(bits, ofs, 0, width, x, y,
635 width - ofs, pf->height);
637 x += width - ofs;
638 ofs = 0;
641 #else
642 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
643 int src_x, int src_y,
644 int stride, int x, int y,
645 int width, int height)
647 int mode = lcd_(get_drawmode)();
648 lcd_(set_drawmode)(DRMODE_FG);
649 lcd_(mono_bitmap_part)(src, src_x, src_y, stride, x, y, width, height);
650 lcd_(set_drawmode)(mode);
653 static void draw_putsxy_oriented(int x, int y, const char *str)
655 int mode = lcd_(get_drawmode)();
656 lcd_(set_drawmode)(DRMODE_FG);
657 lcd_(putsxy)(x + wvs.x, y + wvs.y, str);
658 lcd_(set_drawmode)(mode);
660 #endif /* LCD_PORTRAIT */
662 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
663 /* So we can refresh the overlay */
664 static void wvs_lcd_enable_hook(void* param)
666 (void)param;
667 rb->queue_post(rb->button_queue, LCD_ENABLE_EVENT_1, 0);
669 #endif
671 static void wvs_backlight_on_video_mode(bool video_on)
673 if (video_on) {
674 /* Turn off backlight timeout */
675 /* backlight control in lib/helper.c */
676 backlight_force_on();
677 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
678 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
679 #endif
680 } else {
681 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
682 rb->add_event(LCD_EVENT_ACTIVATION, false, wvs_lcd_enable_hook);
683 #endif
684 /* Revert to user's backlight settings */
685 backlight_use_settings();
689 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
690 static void wvs_backlight_brightness_video_mode(bool video_on)
692 if (settings.backlight_brightness < 0)
693 return;
695 mpeg_backlight_update_brightness(
696 video_on ? settings.backlight_brightness : -1);
698 #else
699 #define wvs_backlight_brightness_video_mode(video_on)
700 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
702 static void wvs_text_init(void)
704 struct hms hms;
705 char buf[32];
706 int phys;
707 int spc_width;
709 lcd_(setfont)(FONT_UI);
711 wvs.x = 0;
712 wvs.width = SCREEN_WIDTH;
714 vo_rect_clear(&wvs.time_rect);
715 vo_rect_clear(&wvs.stat_rect);
716 vo_rect_clear(&wvs.prog_rect);
717 vo_rect_clear(&wvs.vol_rect);
719 ts_to_hms(stream_get_duration(), &hms);
720 hms_format(buf, sizeof (buf), &hms);
721 lcd_(getstringsize)(buf, &wvs.time_rect.r, &wvs.time_rect.b);
723 /* Choose well-sized bitmap images relative to font height */
724 if (wvs.time_rect.b < 12) {
725 wvs.icons = mpegplayer_status_icons_8x8x1;
726 wvs.stat_rect.r = wvs.stat_rect.b = 8;
727 } else if (wvs.time_rect.b < 16) {
728 wvs.icons = mpegplayer_status_icons_12x12x1;
729 wvs.stat_rect.r = wvs.stat_rect.b = 12;
730 } else {
731 wvs.icons = mpegplayer_status_icons_16x16x1;
732 wvs.stat_rect.r = wvs.stat_rect.b = 16;
735 if (wvs.stat_rect.b < wvs.time_rect.b) {
736 vo_rect_offset(&wvs.stat_rect, 0,
737 (wvs.time_rect.b - wvs.stat_rect.b) / 2 + WVS_BDR_T);
738 vo_rect_offset(&wvs.time_rect, WVS_BDR_L, WVS_BDR_T);
739 } else {
740 vo_rect_offset(&wvs.time_rect, WVS_BDR_L,
741 wvs.stat_rect.b - wvs.time_rect.b + WVS_BDR_T);
742 vo_rect_offset(&wvs.stat_rect, 0, WVS_BDR_T);
745 wvs.dur_rect = wvs.time_rect;
747 phys = rb->sound_val2phys(SOUND_VOLUME, rb->sound_min(SOUND_VOLUME));
748 rb->snprintf(buf, sizeof(buf), "%d%s", phys,
749 rb->sound_unit(SOUND_VOLUME));
751 lcd_(getstringsize)(" ", &spc_width, NULL);
752 lcd_(getstringsize)(buf, &wvs.vol_rect.r, &wvs.vol_rect.b);
754 wvs.prog_rect.r = SCREEN_WIDTH - WVS_BDR_L - spc_width -
755 wvs.vol_rect.r - WVS_BDR_R;
756 wvs.prog_rect.b = 3*wvs.stat_rect.b / 4;
757 vo_rect_offset(&wvs.prog_rect, wvs.time_rect.l,
758 wvs.time_rect.b);
760 vo_rect_offset(&wvs.stat_rect,
761 (wvs.prog_rect.r + wvs.prog_rect.l - wvs.stat_rect.r) / 2,
764 vo_rect_offset(&wvs.dur_rect,
765 wvs.prog_rect.r - wvs.dur_rect.r, 0);
767 vo_rect_offset(&wvs.vol_rect, wvs.prog_rect.r + spc_width,
768 (wvs.prog_rect.b + wvs.prog_rect.t - wvs.vol_rect.b) / 2);
770 wvs.height = WVS_BDR_T + MAX(wvs.prog_rect.b, wvs.vol_rect.b) -
771 MIN(wvs.time_rect.t, wvs.stat_rect.t) + WVS_BDR_B;
773 #ifdef HAVE_LCD_COLOR
774 wvs.height = ALIGN_UP(wvs.height, 2);
775 #endif
776 wvs.y = SCREEN_HEIGHT - wvs.height;
778 lcd_(setfont)(FONT_SYSFIXED);
781 static void wvs_init(void)
783 wvs.flags = 0;
784 wvs.show_for = HZ*4;
785 wvs.print_delay = 75*HZ/100;
786 wvs.resume_delay = HZ/2;
787 #ifdef HAVE_LCD_COLOR
788 wvs.bgcolor = LCD_RGBPACK(0x73, 0x75, 0xbd);
789 wvs.fgcolor = LCD_WHITE;
790 wvs.prog_fillcolor = LCD_BLACK;
791 #else
792 wvs.bgcolor = GREY_LIGHTGRAY;
793 wvs.fgcolor = GREY_BLACK;
794 wvs.prog_fillcolor = GREY_WHITE;
795 #endif
796 wvs.curr_time = 0;
797 wvs.status = WVS_STATUS_STOPPED;
798 wvs.auto_refresh = WVS_REFRESH_TIME;
799 wvs.next_auto_refresh = *rb->current_tick;
800 wvs_text_init();
803 static void wvs_schedule_refresh(unsigned refresh)
805 long tick = *rb->current_tick;
807 if (refresh & WVS_REFRESH_VIDEO)
808 wvs.print_tick = tick + wvs.print_delay;
810 if (refresh & WVS_REFRESH_RESUME)
811 wvs.resume_tick = tick + wvs.resume_delay;
813 wvs.auto_refresh |= refresh;
816 static void wvs_cancel_refresh(unsigned refresh)
818 wvs.auto_refresh &= ~refresh;
821 /* Refresh the background area */
822 static void wvs_refresh_background(void)
824 char buf[32];
825 struct hms hms;
827 unsigned bg = lcd_(get_background)();
828 lcd_(set_drawmode)(DRMODE_SOLID | DRMODE_INVERSEVID);
830 #ifdef HAVE_LCD_COLOR
831 /* Draw a "raised" area for our graphics */
832 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 192));
833 draw_hline(0, wvs.width, 0);
835 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 80));
836 draw_hline(0, wvs.width, 1);
838 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 48));
839 draw_hline(0, wvs.width, wvs.height-2);
841 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 128));
842 draw_hline(0, wvs.width, wvs.height-1);
844 lcd_(set_background)(bg);
845 draw_clear_area(0, 2, wvs.width, wvs.height - 4);
846 #else
847 /* Give contrast with the main background */
848 lcd_(set_background)(GREY_WHITE);
849 draw_hline(0, wvs.width, 0);
851 lcd_(set_background)(GREY_DARKGRAY);
852 draw_hline(0, wvs.width, wvs.height-1);
854 lcd_(set_background)(bg);
855 draw_clear_area(0, 1, wvs.width, wvs.height - 2);
856 #endif
858 vo_rect_set_ext(&wvs.update_rect, 0, 0, wvs.width, wvs.height);
859 lcd_(set_drawmode)(DRMODE_SOLID);
861 if (stream_get_duration() != INVALID_TIMESTAMP) {
862 /* Draw the movie duration */
863 ts_to_hms(stream_get_duration(), &hms);
864 hms_format(buf, sizeof (buf), &hms);
865 draw_putsxy_oriented(wvs.dur_rect.l, wvs.dur_rect.t, buf);
867 /* else don't know the duration */
870 /* Refresh the current time display + the progress bar */
871 static void wvs_refresh_time(void)
873 char buf[32];
874 struct hms hms;
876 uint32_t duration = stream_get_duration();
878 draw_scrollbar_draw_rect(&wvs.prog_rect, 0, duration,
879 wvs.curr_time);
881 ts_to_hms(wvs.curr_time, &hms);
882 hms_format(buf, sizeof (buf), &hms);
884 draw_clear_area_rect(&wvs.time_rect);
885 draw_putsxy_oriented(wvs.time_rect.l, wvs.time_rect.t, buf);
887 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
888 &wvs.prog_rect);
889 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
890 &wvs.time_rect);
893 /* Refresh the volume display area */
894 static void wvs_refresh_volume(void)
896 char buf[32];
897 int width;
899 int volume = rb->global_settings->volume;
900 rb->snprintf(buf, sizeof (buf), "%d%s",
901 rb->sound_val2phys(SOUND_VOLUME, volume),
902 rb->sound_unit(SOUND_VOLUME));
903 lcd_(getstringsize)(buf, &width, NULL);
905 /* Right-justified */
906 draw_clear_area_rect(&wvs.vol_rect);
907 draw_putsxy_oriented(wvs.vol_rect.r - width, wvs.vol_rect.t, buf);
909 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.vol_rect);
912 /* Refresh the status icon */
913 static void wvs_refresh_status(void)
915 int icon_size = wvs.stat_rect.r - wvs.stat_rect.l;
917 draw_clear_area_rect(&wvs.stat_rect);
919 #ifdef HAVE_LCD_COLOR
920 /* Draw status icon with a drop shadow */
921 unsigned oldfg = lcd_(get_foreground)();
922 int i = 1;
924 lcd_(set_foreground)(draw_blendcolor(lcd_(get_background)(),
925 DRAW_BLACK, 96));
927 while (1)
929 draw_oriented_mono_bitmap_part(wvs.icons,
930 icon_size*wvs.status,
932 icon_size*WVS_STATUS_COUNT,
933 wvs.stat_rect.l + wvs.x + i,
934 wvs.stat_rect.t + wvs.y + i,
935 icon_size, icon_size);
937 if (--i < 0)
938 break;
940 lcd_(set_foreground)(oldfg);
943 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
944 #else
945 draw_oriented_mono_bitmap_part(wvs.icons,
946 icon_size*wvs.status,
948 icon_size*WVS_STATUS_COUNT,
949 wvs.stat_rect.l + wvs.x,
950 wvs.stat_rect.t + wvs.y,
951 icon_size, icon_size);
952 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
953 #endif
956 /* Update the current status which determines which icon is displayed */
957 static bool wvs_update_status(void)
959 int status;
961 switch (stream_status())
963 default:
964 status = WVS_STATUS_STOPPED;
965 break;
966 case STREAM_PAUSED:
967 /* If paused with a pending resume, coerce it to WVS_STATUS_PLAYING */
968 status = (wvs.auto_refresh & WVS_REFRESH_RESUME) ?
969 WVS_STATUS_PLAYING : WVS_STATUS_PAUSED;
970 break;
971 case STREAM_PLAYING:
972 status = WVS_STATUS_PLAYING;
973 break;
976 if (status != wvs.status) {
977 /* A refresh is needed */
978 wvs.status = status;
979 return true;
982 return false;
985 /* Update the current time that will be displayed */
986 static void wvs_update_time(void)
988 uint32_t start;
989 wvs.curr_time = stream_get_seek_time(&start);
990 wvs.curr_time -= start;
993 /* Refresh various parts of the WVS - showing it if it is hidden */
994 static void wvs_refresh(int hint)
996 long tick;
997 unsigned oldbg, oldfg;
999 tick = *rb->current_tick;
1001 if (hint == WVS_REFRESH_DEFAULT) {
1002 /* The default which forces no updates */
1004 /* Make sure Rockbox doesn't turn off the player because of
1005 too little activity */
1006 if (wvs.status == WVS_STATUS_PLAYING)
1007 rb->reset_poweroff_timer();
1009 /* Redraw the current or possibly extract a new video frame */
1010 if ((wvs.auto_refresh & WVS_REFRESH_VIDEO) &&
1011 TIME_AFTER(tick, wvs.print_tick)) {
1012 wvs.auto_refresh &= ~WVS_REFRESH_VIDEO;
1013 stream_draw_frame(false);
1016 /* Restart playback if the timout was reached */
1017 if ((wvs.auto_refresh & WVS_REFRESH_RESUME) &&
1018 TIME_AFTER(tick, wvs.resume_tick)) {
1019 wvs.auto_refresh &= ~(WVS_REFRESH_RESUME | WVS_REFRESH_VIDEO);
1020 stream_resume();
1023 /* If not visible, return */
1024 if (!(wvs.flags & WVS_SHOW))
1025 return;
1027 /* Hide if the visibility duration was reached */
1028 if (TIME_AFTER(tick, wvs.hide_tick)) {
1029 wvs_show(WVS_HIDE);
1030 return;
1032 } else {
1033 /* A forced update of some region */
1035 /* Show if currently invisible */
1036 if (!(wvs.flags & WVS_SHOW)) {
1037 /* Avoid call back into this function - it will be drawn */
1038 wvs_show(WVS_SHOW | WVS_NODRAW);
1039 hint = WVS_REFRESH_ALL;
1042 /* Move back timeouts for frame print and hide */
1043 wvs.print_tick = tick + wvs.print_delay;
1044 wvs.hide_tick = tick + wvs.show_for;
1047 if (TIME_AFTER(tick, wvs.next_auto_refresh)) {
1048 /* Refresh whatever graphical elements are due automatically */
1049 wvs.next_auto_refresh = tick + WVS_MIN_UPDATE_INTERVAL;
1051 if (wvs.auto_refresh & WVS_REFRESH_STATUS) {
1052 if (wvs_update_status())
1053 hint |= WVS_REFRESH_STATUS;
1056 if (wvs.auto_refresh & WVS_REFRESH_TIME) {
1057 wvs_update_time();
1058 hint |= WVS_REFRESH_TIME;
1062 if (hint == 0)
1063 return; /* No drawing needed */
1065 /* Set basic drawing params that are used. Elements that perform variations
1066 * will restore them. */
1067 oldfg = lcd_(get_foreground)();
1068 oldbg = lcd_(get_background)();
1070 lcd_(setfont)(FONT_UI);
1071 lcd_(set_foreground)(wvs.fgcolor);
1072 lcd_(set_background)(wvs.bgcolor);
1074 vo_rect_clear(&wvs.update_rect);
1076 if (hint & WVS_REFRESH_BACKGROUND) {
1077 wvs_refresh_background();
1078 hint |= WVS_REFRESH_ALL; /* Requires a redraw of everything */
1081 if (hint & WVS_REFRESH_TIME) {
1082 wvs_refresh_time();
1085 if (hint & WVS_REFRESH_VOLUME) {
1086 wvs_refresh_volume();
1089 if (hint & WVS_REFRESH_STATUS) {
1090 wvs_refresh_status();
1093 /* Go back to defaults */
1094 lcd_(setfont)(FONT_SYSFIXED);
1095 lcd_(set_foreground)(oldfg);
1096 lcd_(set_background)(oldbg);
1098 /* Update the dirty rectangle */
1099 vo_lock();
1101 draw_update_rect(wvs.update_rect.l,
1102 wvs.update_rect.t,
1103 wvs.update_rect.r - wvs.update_rect.l,
1104 wvs.update_rect.b - wvs.update_rect.t);
1106 vo_unlock();
1109 /* Show/Hide the WVS */
1110 static void wvs_show(unsigned show)
1112 if (((show ^ wvs.flags) & WVS_SHOW) == 0)
1114 if (show & WVS_SHOW) {
1115 wvs.hide_tick = *rb->current_tick + wvs.show_for;
1117 return;
1120 if (show & WVS_SHOW) {
1121 /* Clip away the part of video that is covered */
1122 struct vo_rect rc = { 0, 0, SCREEN_WIDTH, wvs.y };
1124 wvs.flags |= WVS_SHOW;
1126 if (wvs.status != WVS_STATUS_PLAYING) {
1127 /* Not playing - set brightness to mpegplayer setting */
1128 wvs_backlight_brightness_video_mode(true);
1131 stream_vo_set_clip(&rc);
1133 if (!(show & WVS_NODRAW))
1134 wvs_refresh(WVS_REFRESH_ALL);
1135 } else {
1136 /* Uncover clipped video area and redraw it */
1137 wvs.flags &= ~WVS_SHOW;
1139 draw_clear_area(0, 0, wvs.width, wvs.height);
1141 if (!(show & WVS_NODRAW)) {
1142 vo_lock();
1143 draw_update_rect(0, 0, wvs.width, wvs.height);
1144 vo_unlock();
1146 stream_vo_set_clip(NULL);
1147 stream_draw_frame(false);
1148 } else {
1149 stream_vo_set_clip(NULL);
1152 if (wvs.status != WVS_STATUS_PLAYING) {
1153 /* Not playing - restore backlight brightness */
1154 wvs_backlight_brightness_video_mode(false);
1159 /* Set the current status - update screen if specified */
1160 static void wvs_set_status(int status)
1162 bool draw = (status & WVS_NODRAW) == 0;
1164 status &= WVS_STATUS_MASK;
1166 if (wvs.status != status) {
1168 wvs.status = status;
1170 if (draw)
1171 wvs_refresh(WVS_REFRESH_STATUS);
1175 /* Get the current status value */
1176 static int wvs_get_status(void)
1178 return wvs.status & WVS_STATUS_MASK;
1181 /* Handle Fast-forward/Rewind keys using WPS settings (and some nicked code ;) */
1182 static uint32_t wvs_ff_rw(int btn, unsigned refresh)
1184 unsigned int step = TS_SECOND*rb->global_settings->ff_rewind_min_step;
1185 const long ff_rw_accel = (rb->global_settings->ff_rewind_accel + 3);
1186 uint32_t start;
1187 uint32_t time = stream_get_seek_time(&start);
1188 const uint32_t duration = stream_get_duration();
1189 unsigned int max_step = 0;
1190 uint32_t ff_rw_count = 0;
1191 unsigned status = wvs.status;
1193 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME |
1194 WVS_REFRESH_TIME);
1196 time -= start; /* Absolute clock => stream-relative */
1198 switch (btn)
1200 case MPEG_FF:
1201 #ifdef MPEG_RC_FF
1202 case MPEG_RC_FF:
1203 #endif
1204 wvs_set_status(WVS_STATUS_FF);
1205 break;
1206 case MPEG_RW:
1207 #ifdef MPEG_RC_RW
1208 case MPEG_RC_RW:
1209 #endif
1210 wvs_set_status(WVS_STATUS_RW);
1211 break;
1212 default:
1213 btn = -1;
1216 btn |= BUTTON_REPEAT;
1218 while (1)
1220 stream_keep_disk_active();
1222 switch (btn)
1224 case BUTTON_NONE:
1225 wvs_refresh(WVS_REFRESH_DEFAULT);
1226 break;
1228 case MPEG_FF | BUTTON_REPEAT:
1229 case MPEG_RW | BUTTON_REPEAT:
1230 #ifdef MPEG_RC_FF
1231 case MPEG_RC_FF | BUTTON_REPEAT:
1232 case MPEG_RC_RW | BUTTON_REPEAT:
1233 #endif
1234 break;
1236 case MPEG_FF | BUTTON_REL:
1237 case MPEG_RW | BUTTON_REL:
1238 #ifdef MPEG_RC_FF
1239 case MPEG_RC_FF | BUTTON_REL:
1240 case MPEG_RC_RW | BUTTON_REL:
1241 #endif
1242 if (wvs.status == WVS_STATUS_FF)
1243 time += ff_rw_count;
1244 else if (wvs.status == WVS_STATUS_RW)
1245 time -= ff_rw_count;
1247 /* Fall-through */
1248 case -1:
1249 default:
1250 wvs_schedule_refresh(refresh);
1251 wvs_set_status(status);
1252 wvs_schedule_refresh(WVS_REFRESH_TIME);
1253 return time;
1256 if (wvs.status == WVS_STATUS_FF) {
1257 /* fast forwarding, calc max step relative to end */
1258 max_step = muldiv_uint32(duration - (time + ff_rw_count),
1259 FF_REWIND_MAX_PERCENT, 100);
1260 } else {
1261 /* rewinding, calc max step relative to start */
1262 max_step = muldiv_uint32(time - ff_rw_count,
1263 FF_REWIND_MAX_PERCENT, 100);
1266 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
1268 if (step > max_step)
1269 step = max_step;
1271 ff_rw_count += step;
1273 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
1274 step += step >> ff_rw_accel;
1276 if (wvs.status == WVS_STATUS_FF) {
1277 if (duration - time <= ff_rw_count)
1278 ff_rw_count = duration - time;
1280 wvs.curr_time = time + ff_rw_count;
1281 } else {
1282 if (time <= ff_rw_count)
1283 ff_rw_count = time;
1285 wvs.curr_time = time - ff_rw_count;
1288 wvs_refresh(WVS_REFRESH_TIME);
1290 btn = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1294 static int wvs_status(void)
1296 int status = stream_status();
1298 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1299 if (status == STREAM_PAUSED) {
1300 if (wvs.auto_refresh & WVS_REFRESH_RESUME)
1301 status = STREAM_PLAYING;
1304 return status;
1307 /* Change the current audio volume by a specified amount */
1308 static void wvs_set_volume(int delta)
1310 int vol = rb->global_settings->volume;
1311 int limit;
1313 vol += delta;
1315 if (delta < 0) {
1316 /* Volume down - clip to lower limit */
1317 limit = rb->sound_min(SOUND_VOLUME);
1318 if (vol < limit)
1319 vol = limit;
1320 } else {
1321 /* Volume up - clip to upper limit */
1322 limit = rb->sound_max(SOUND_VOLUME);
1323 if (vol > limit)
1324 vol = limit;
1327 /* Sync the global settings */
1328 if (vol != rb->global_settings->volume) {
1329 rb->sound_set(SOUND_VOLUME, vol);
1330 rb->global_settings->volume = vol;
1333 /* Update the volume display */
1334 wvs_refresh(WVS_REFRESH_VOLUME);
1337 /* Begin playback at the specified time */
1338 static int wvs_play(uint32_t time)
1340 int retval;
1342 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1344 retval = stream_seek(time, SEEK_SET);
1346 if (retval >= STREAM_OK) {
1347 wvs_backlight_on_video_mode(true);
1348 wvs_backlight_brightness_video_mode(true);
1349 stream_show_vo(true);
1350 retval = stream_play();
1352 if (retval >= STREAM_OK)
1353 wvs_set_status(WVS_STATUS_PLAYING | WVS_NODRAW);
1356 return retval;
1359 /* Halt playback - pause engine and return logical state */
1360 static int wvs_halt(void)
1362 int status = stream_pause();
1364 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1365 if (status == STREAM_PAUSED) {
1366 if (wvs_get_status() == WVS_STATUS_PLAYING)
1367 status = STREAM_PLAYING;
1370 /* Cancel some auto refreshes - caller will restart them if desired */
1371 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1373 /* No backlight fiddling here - callers does the right thing */
1375 return status;
1378 /* Pause playback if playing */
1379 static int wvs_pause(void)
1381 unsigned refresh = wvs.auto_refresh;
1382 int status = wvs_halt();
1384 if (status == STREAM_PLAYING && (refresh & WVS_REFRESH_RESUME)) {
1385 /* Resume pending - change to a still video frame update */
1386 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1389 wvs_set_status(WVS_STATUS_PAUSED);
1391 wvs_backlight_on_video_mode(false);
1392 /* Leave brightness alone and restore it when WVS is hidden */
1394 return status;
1397 /* Resume playback if halted or paused */
1398 static void wvs_resume(void)
1400 /* Cancel video and resume auto refresh - the resyc when starting
1401 * playback will perform those tasks */
1402 wvs_backlight_on_video_mode(true);
1403 wvs_backlight_brightness_video_mode(true);
1404 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1405 wvs_set_status(WVS_STATUS_PLAYING);
1406 stream_resume();
1409 /* Stop playback - remember the resume point if not closed */
1410 static void wvs_stop(void)
1412 uint32_t resume_time;
1414 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1415 wvs_set_status(WVS_STATUS_STOPPED | WVS_NODRAW);
1416 wvs_show(WVS_HIDE | WVS_NODRAW);
1418 stream_stop();
1420 resume_time = stream_get_resume_time();
1422 if (resume_time != INVALID_TIMESTAMP)
1423 settings.resume_time = resume_time;
1425 wvs_backlight_on_video_mode(false);
1426 wvs_backlight_brightness_video_mode(false);
1429 /* Perform a seek if seeking is possible for this stream - if playing, a delay
1430 * will be inserted before restarting in case the user decides to seek again */
1431 static void wvs_seek(int btn)
1433 int status;
1434 unsigned refresh;
1435 uint32_t time;
1437 if (!stream_can_seek())
1438 return;
1440 /* Halt playback - not strictly nescessary but nice */
1441 status = wvs_halt();
1443 if (status == STREAM_STOPPED)
1444 return;
1446 wvs_show(WVS_SHOW);
1448 if (status == STREAM_PLAYING)
1449 refresh = WVS_REFRESH_RESUME; /* delay resume if playing */
1450 else
1451 refresh = WVS_REFRESH_VIDEO; /* refresh if paused */
1453 /* Obtain a new playback point */
1454 time = wvs_ff_rw(btn, refresh);
1456 /* Tell engine to resume at that time */
1457 stream_seek(time, SEEK_SET);
1460 #ifdef HAVE_HEADPHONE_DETECTION
1461 /* Handle SYS_PHONE_PLUGGED/UNPLUGGED */
1462 static void wvs_handle_phone_plug(bool inserted)
1464 if (rb->global_settings->unplug_mode == 0)
1465 return;
1467 /* Wait for any incomplete state transition to complete first */
1468 stream_wait_status();
1470 int status = wvs_status();
1472 if (inserted) {
1473 if (rb->global_settings->unplug_mode > 1) {
1474 if (status == STREAM_PAUSED) {
1475 wvs_resume();
1478 } else {
1479 if (status == STREAM_PLAYING) {
1480 wvs_pause();
1482 if (stream_can_seek() && rb->global_settings->unplug_rw) {
1483 stream_seek(-rb->global_settings->unplug_rw*TS_SECOND,
1484 SEEK_CUR);
1485 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1486 /* Update time display now */
1487 wvs_update_time();
1488 wvs_refresh(WVS_REFRESH_TIME);
1493 #endif
1495 static void button_loop(void)
1497 rb->lcd_setfont(FONT_SYSFIXED);
1498 rb->lcd_clear_display();
1499 rb->lcd_update();
1501 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1502 rb->lcd_set_mode(LCD_MODE_YUV);
1503 #endif
1505 wvs_init();
1507 /* Start playback at the specified starting time */
1508 if (wvs_play(settings.resume_time) < STREAM_OK) {
1509 rb->splash(HZ*2, "Playback failed");
1510 return;
1513 /* Gently poll the video player for EOS and handle UI */
1514 while (stream_status() != STREAM_STOPPED)
1516 int button;
1518 mpeg_menu_sysevent_clear();
1519 button = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1521 button = mpeg_menu_sysevent_callback(button, NULL);
1523 switch (button)
1525 case BUTTON_NONE:
1527 wvs_refresh(WVS_REFRESH_DEFAULT);
1528 continue;
1529 } /* BUTTON_NONE: */
1531 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1532 case LCD_ENABLE_EVENT_1:
1534 /* Draw the current frame if prepared already */
1535 stream_draw_frame(true);
1536 break;
1537 } /* LCD_ENABLE_EVENT_1: */
1538 #endif
1540 case MPEG_VOLUP:
1541 case MPEG_VOLUP|BUTTON_REPEAT:
1542 #ifdef MPEG_VOLUP2
1543 case MPEG_VOLUP2:
1544 case MPEG_VOLUP2|BUTTON_REPEAT:
1545 #endif
1546 #ifdef MPEG_RC_VOLUP
1547 case MPEG_RC_VOLUP:
1548 case MPEG_RC_VOLUP|BUTTON_REPEAT:
1549 #endif
1551 wvs_set_volume(+1);
1552 break;
1553 } /* MPEG_VOLUP*: */
1555 case MPEG_VOLDOWN:
1556 case MPEG_VOLDOWN|BUTTON_REPEAT:
1557 #ifdef MPEG_VOLDOWN2
1558 case MPEG_VOLDOWN2:
1559 case MPEG_VOLDOWN2|BUTTON_REPEAT:
1560 #endif
1561 #ifdef MPEG_RC_VOLDOWN
1562 case MPEG_RC_VOLDOWN:
1563 case MPEG_RC_VOLDOWN|BUTTON_REPEAT:
1564 #endif
1566 wvs_set_volume(-1);
1567 break;
1568 } /* MPEG_VOLDOWN*: */
1570 case MPEG_MENU:
1571 #ifdef MPEG_RC_MENU
1572 case MPEG_RC_MENU:
1573 #endif
1575 int state = wvs_halt(); /* save previous state */
1576 int result;
1578 /* Hide video output */
1579 wvs_show(WVS_HIDE | WVS_NODRAW);
1580 stream_show_vo(false);
1581 wvs_backlight_brightness_video_mode(false);
1583 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1584 rb->lcd_set_mode(LCD_MODE_RGB565);
1585 #endif
1587 result = mpeg_menu();
1589 /* The menu can change the font, so restore */
1590 rb->lcd_setfont(FONT_SYSFIXED);
1592 switch (result)
1594 case MPEG_MENU_QUIT:
1595 wvs_stop();
1596 break;
1598 default:
1599 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1600 rb->lcd_set_mode(LCD_MODE_YUV);
1601 #endif
1602 /* If not stopped, show video again */
1603 if (state != STREAM_STOPPED) {
1604 wvs_show(WVS_SHOW);
1605 stream_show_vo(true);
1608 /* If stream was playing, restart it */
1609 if (state == STREAM_PLAYING) {
1610 wvs_resume();
1612 break;
1614 break;
1615 } /* MPEG_MENU: */
1617 case MPEG_STOP:
1618 #ifdef MPEG_RC_STOP
1619 case MPEG_RC_STOP:
1620 #endif
1621 case ACTION_STD_CANCEL:
1623 wvs_stop();
1624 break;
1625 } /* MPEG_STOP: */
1627 case MPEG_PAUSE:
1628 #ifdef MPEG_PAUSE2
1629 case MPEG_PAUSE2:
1630 #endif
1631 #ifdef MPEG_RC_PAUSE
1632 case MPEG_RC_PAUSE:
1633 #endif
1635 int status = wvs_status();
1637 if (status == STREAM_PLAYING) {
1638 /* Playing => Paused */
1639 wvs_pause();
1641 else if (status == STREAM_PAUSED) {
1642 /* Paused => Playing */
1643 wvs_resume();
1646 break;
1647 } /* MPEG_PAUSE*: */
1649 case MPEG_RW:
1650 case MPEG_FF:
1651 #ifdef MPEG_RC_RW
1652 case MPEG_RC_RW:
1653 case MPEG_RC_FF:
1654 #endif
1656 wvs_seek(button);
1657 break;
1658 } /* MPEG_RW: MPEG_FF: */
1660 #ifdef HAVE_HEADPHONE_DETECTION
1661 case SYS_PHONE_PLUGGED:
1662 case SYS_PHONE_UNPLUGGED:
1664 wvs_handle_phone_plug(button == SYS_PHONE_PLUGGED);
1665 break;
1666 } /* SYS_PHONE_*: */
1667 #endif
1669 default:
1671 rb->default_event_handler(button);
1672 break;
1673 } /* default: */
1676 rb->yield();
1677 } /* end while */
1679 wvs_stop();
1681 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1682 /* Be sure hook is removed before exiting since the stop will put it
1683 * back because of the backlight restore. */
1684 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
1685 #endif
1687 rb->lcd_setfont(FONT_UI);
1690 enum plugin_status plugin_start(const void* parameter)
1692 int status = PLUGIN_ERROR; /* assume failure */
1693 int result;
1694 int err;
1695 const char *errstring;
1697 if (parameter == NULL) {
1698 /* No file = GTFO */
1699 rb->splash(HZ*2, "No File");
1700 return PLUGIN_ERROR;
1703 /* Disable all talking before initializing IRAM */
1704 rb->talk_disable(true);
1706 /* Initialize IRAM - stops audio and voice as well */
1707 PLUGIN_IRAM_INIT(rb)
1709 #ifdef HAVE_LCD_COLOR
1710 rb->lcd_set_backdrop(NULL);
1711 rb->lcd_set_foreground(LCD_WHITE);
1712 rb->lcd_set_background(LCD_BLACK);
1713 #endif
1715 rb->lcd_clear_display();
1716 rb->lcd_update();
1718 if (stream_init() < STREAM_OK) {
1719 DEBUGF("Could not initialize streams\n");
1720 } else {
1721 rb->splash(0, "Loading...");
1722 init_settings((char*)parameter);
1724 err = stream_open((char *)parameter);
1726 if (err >= STREAM_OK) {
1727 /* start menu */
1728 rb->lcd_clear_display();
1729 rb->lcd_update();
1730 result = mpeg_start_menu(stream_get_duration());
1732 if (result != MPEG_START_QUIT) {
1733 /* Enter button loop and process UI */
1734 button_loop();
1737 stream_close();
1739 rb->lcd_clear_display();
1740 rb->lcd_update();
1742 save_settings(); /* Save settings (if they have changed) */
1743 status = PLUGIN_OK;
1745 mpeg_menu_sysevent_handle();
1746 } else {
1747 DEBUGF("Could not open %s\n", (char*)parameter);
1748 switch (err)
1750 case STREAM_UNSUPPORTED:
1751 errstring = "Unsupported format";
1752 break;
1753 default:
1754 errstring = "Error opening file: %d";
1757 rb->splashf(HZ*2, errstring, err);
1761 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1762 rb->lcd_set_mode(LCD_MODE_RGB565);
1763 #endif
1765 stream_exit();
1767 rb->talk_disable(false);
1768 return status;