mpegplayer: reset foreground/background color and clear display in case they are...
[kugel-rb.git] / apps / plugins / mpegplayer / mpegplayer.c
blob31d4ef971e25d62aad61e217b195ad02d9dac544
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
155 #define MPEG_RC_MENU BUTTON_RC_DSP
156 #define MPEG_RC_STOP (BUTTON_RC_PLAY | BUTTON_REPEAT)
157 #define MPEG_RC_PAUSE (BUTTON_RC_PLAY | BUTTON_REL)
158 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
159 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
160 #define MPEG_RC_RW BUTTON_RC_REW
161 #define MPEG_RC_FF BUTTON_RC_FF
163 #elif CONFIG_KEYPAD == GIGABEAT_S_PAD
164 #define MPEG_MENU BUTTON_MENU
165 #define MPEG_STOP BUTTON_POWER
166 #define MPEG_PAUSE BUTTON_SELECT
167 #define MPEG_PAUSE2 BUTTON_PLAY
168 #define MPEG_VOLDOWN BUTTON_LEFT
169 #define MPEG_VOLUP BUTTON_RIGHT
170 #define MPEG_VOLDOWN2 BUTTON_VOL_DOWN
171 #define MPEG_VOLUP2 BUTTON_VOL_UP
172 #define MPEG_RW BUTTON_UP
173 #define MPEG_FF BUTTON_DOWN
175 #define MPEG_RC_MENU BUTTON_RC_DSP
176 #define MPEG_RC_STOP (BUTTON_RC_PLAY | BUTTON_REPEAT)
177 #define MPEG_RC_PAUSE (BUTTON_RC_PLAY | BUTTON_REL)
178 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
179 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
180 #define MPEG_RC_RW BUTTON_RC_REW
181 #define MPEG_RC_FF BUTTON_RC_FF
183 #elif CONFIG_KEYPAD == IRIVER_H10_PAD
184 #define MPEG_MENU BUTTON_LEFT
185 #define MPEG_STOP BUTTON_POWER
186 #define MPEG_PAUSE BUTTON_PLAY
187 #define MPEG_VOLDOWN BUTTON_SCROLL_DOWN
188 #define MPEG_VOLUP BUTTON_SCROLL_UP
189 #define MPEG_RW BUTTON_REW
190 #define MPEG_FF BUTTON_FF
192 #elif CONFIG_KEYPAD == SANSA_E200_PAD
193 #define MPEG_MENU BUTTON_SELECT
194 #define MPEG_STOP BUTTON_POWER
195 #define MPEG_PAUSE BUTTON_RIGHT
196 #define MPEG_VOLDOWN BUTTON_SCROLL_BACK
197 #define MPEG_VOLUP BUTTON_SCROLL_FWD
198 #define MPEG_RW BUTTON_UP
199 #define MPEG_FF BUTTON_DOWN
201 #elif CONFIG_KEYPAD == SANSA_FUZE_PAD
202 #define MPEG_MENU BUTTON_SELECT
203 #define MPEG_STOP (BUTTON_HOME|BUTTON_REPEAT)
204 #define MPEG_PAUSE BUTTON_UP
205 #define MPEG_VOLDOWN BUTTON_SCROLL_BACK
206 #define MPEG_VOLUP BUTTON_SCROLL_FWD
207 #define MPEG_RW BUTTON_LEFT
208 #define MPEG_FF BUTTON_RIGHT
211 #elif CONFIG_KEYPAD == SANSA_C200_PAD || \
212 CONFIG_KEYPAD == SANSA_CLIP_PAD || \
213 CONFIG_KEYPAD == SANSA_M200_PAD
214 #define MPEG_MENU BUTTON_SELECT
215 #define MPEG_STOP BUTTON_POWER
216 #define MPEG_PAUSE BUTTON_UP
217 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
218 #define MPEG_VOLUP BUTTON_VOL_UP
219 #define MPEG_RW BUTTON_LEFT
220 #define MPEG_FF BUTTON_RIGHT
222 #elif CONFIG_KEYPAD == MROBE500_PAD
223 #define MPEG_STOP BUTTON_POWER
225 #define MPEG_RC_MENU BUTTON_RC_HEART
226 #define MPEG_RC_STOP BUTTON_RC_DOWN
227 #define MPEG_RC_PAUSE BUTTON_RC_PLAY
228 #define MPEG_RC_VOLDOWN BUTTON_RC_VOL_DOWN
229 #define MPEG_RC_VOLUP BUTTON_RC_VOL_UP
230 #define MPEG_RC_RW BUTTON_RC_REW
231 #define MPEG_RC_FF BUTTON_RC_FF
233 #elif CONFIG_KEYPAD == MROBE100_PAD
234 #define MPEG_MENU BUTTON_MENU
235 #define MPEG_STOP BUTTON_POWER
236 #define MPEG_PAUSE BUTTON_PLAY
237 #define MPEG_VOLDOWN BUTTON_DOWN
238 #define MPEG_VOLUP BUTTON_UP
239 #define MPEG_RW BUTTON_LEFT
240 #define MPEG_FF BUTTON_RIGHT
242 #elif CONFIG_KEYPAD == IAUDIO_M3_PAD
243 #define MPEG_MENU BUTTON_RC_MENU
244 #define MPEG_STOP BUTTON_RC_REC
245 #define MPEG_PAUSE BUTTON_RC_PLAY
246 #define MPEG_VOLDOWN BUTTON_RC_VOL_DOWN
247 #define MPEG_VOLUP BUTTON_RC_VOL_UP
248 #define MPEG_RW BUTTON_RC_REW
249 #define MPEG_FF BUTTON_RC_FF
251 #elif CONFIG_KEYPAD == COWON_D2_PAD
252 #define MPEG_MENU (BUTTON_MENU|BUTTON_REL)
253 //#define MPEG_STOP BUTTON_POWER
254 #define MPEG_VOLDOWN BUTTON_MINUS
255 #define MPEG_VOLUP BUTTON_PLUS
257 #elif CONFIG_KEYPAD == IAUDIO67_PAD
258 #define MPEG_MENU BUTTON_MENU
259 #define MPEG_STOP BUTTON_STOP
260 #define MPEG_PAUSE BUTTON_PLAY
261 #define MPEG_VOLDOWN BUTTON_VOLDOWN
262 #define MPEG_VOLUP BUTTON_VOLUP
263 #define MPEG_RW BUTTON_LEFT
264 #define MPEG_FF BUTTON_RIGHT
266 #elif CONFIG_KEYPAD == CREATIVEZVM_PAD
267 #define MPEG_MENU BUTTON_MENU
268 #define MPEG_STOP BUTTON_BACK
269 #define MPEG_PAUSE BUTTON_PLAY
270 #define MPEG_VOLDOWN BUTTON_UP
271 #define MPEG_VOLUP BUTTON_DOWN
272 #define MPEG_RW BUTTON_LEFT
273 #define MPEG_FF BUTTON_RIGHT
275 #elif CONFIG_KEYPAD == PHILIPS_HDD1630_PAD
276 #define MPEG_MENU BUTTON_MENU
277 #define MPEG_STOP BUTTON_POWER
278 #define MPEG_PAUSE BUTTON_SELECT
279 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
280 #define MPEG_VOLUP BUTTON_VOL_UP
281 #define MPEG_RW BUTTON_LEFT
282 #define MPEG_FF BUTTON_RIGHT
284 #elif CONFIG_KEYPAD == PHILIPS_SA9200_PAD
285 #define MPEG_MENU BUTTON_MENU
286 #define MPEG_STOP BUTTON_POWER
287 #define MPEG_PAUSE BUTTON_PLAY
288 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
289 #define MPEG_VOLUP BUTTON_VOL_UP
290 #define MPEG_RW BUTTON_UP
291 #define MPEG_FF BUTTON_DOWN
293 #elif CONFIG_KEYPAD == ONDAVX747_PAD
294 #define MPEG_MENU (BUTTON_MENU|BUTTON_REL)
295 //#define MPEG_STOP BUTTON_POWER
296 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
297 #define MPEG_VOLUP BUTTON_VOL_UP
299 #elif CONFIG_KEYPAD == ONDAVX777_PAD
300 #define MPEG_MENU BUTTON_POWER
302 #elif CONFIG_KEYPAD == SAMSUNG_YH_PAD
303 #define MPEG_MENU BUTTON_LEFT
304 #define MPEG_STOP BUTTON_RIGHT
305 #define MPEG_PAUSE BUTTON_PLAY
306 #define MPEG_VOLDOWN BUTTON_DOWN
307 #define MPEG_VOLUP BUTTON_UP
308 #define MPEG_RW BUTTON_REW
309 #define MPEG_FF BUTTON_FFWD
311 #else
312 #error No keymap defined!
313 #endif
315 #ifdef HAVE_TOUCHSCREEN
316 #ifndef MPEG_MENU
317 #define MPEG_MENU (BUTTON_TOPRIGHT|BUTTON_REL)
318 #endif
319 #ifndef MPEG_STOP
320 #define MPEG_STOP BUTTON_TOPLEFT
321 #endif
322 #ifndef MPEG_PAUSE
323 #define MPEG_PAUSE BUTTON_CENTER
324 #endif
325 #ifndef MPEG_VOLDOWN
326 #define MPEG_VOLDOWN BUTTON_BOTTOMMIDDLE
327 #endif
328 #ifndef MPEG_VOLUP
329 #define MPEG_VOLUP BUTTON_TOPMIDDLE
330 #endif
331 #ifndef MPEG_RW
332 #define MPEG_RW BUTTON_MIDLEFT
333 #endif
334 #ifndef MPEG_FF
335 #define MPEG_FF BUTTON_MIDRIGHT
336 #endif
337 #endif
339 /* One thing we can do here for targets with remotes is having a display
340 * always on the remote instead of always forcing a popup on the main display */
342 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
343 /* 3% of 30min file == 54s step size */
344 #define MIN_FF_REWIND_STEP (TS_SECOND/2)
345 #define WVS_MIN_UPDATE_INTERVAL (HZ/2)
347 /* WVS status - same order as icon array */
348 enum wvs_status_enum
350 WVS_STATUS_STOPPED = 0,
351 WVS_STATUS_PAUSED,
352 WVS_STATUS_PLAYING,
353 WVS_STATUS_FF,
354 WVS_STATUS_RW,
355 WVS_STATUS_COUNT,
356 WVS_STATUS_MASK = 0x7
359 enum wvs_bits
361 WVS_REFRESH_DEFAULT = 0x0000, /* Only refresh elements when due */
362 /* Refresh the... */
363 WVS_REFRESH_VOLUME = 0x0001, /* ...volume display */
364 WVS_REFRESH_TIME = 0x0002, /* ...time display+progress */
365 WVS_REFRESH_STATUS = 0x0004, /* ...playback status icon */
366 WVS_REFRESH_BACKGROUND = 0x0008, /* ...background (implies ALL) */
367 WVS_REFRESH_VIDEO = 0x0010, /* ...video image upon timeout */
368 WVS_REFRESH_RESUME = 0x0020, /* Resume playback upon timeout */
369 WVS_NODRAW = 0x8000, /* OR bitflag - don't draw anything */
370 WVS_SHOW = 0x4000, /* OR bitflag - show the WVS */
371 WVS_HP_PAUSE = 0x2000,
372 WVS_HIDE = 0x0000, /* hide the WVS (aid readability) */
373 WVS_REFRESH_ALL = 0x000f, /* Only immediate graphical elements */
376 /* Status icons selected according to font height */
377 extern const unsigned char mpegplayer_status_icons_8x8x1[];
378 extern const unsigned char mpegplayer_status_icons_12x12x1[];
379 extern const unsigned char mpegplayer_status_icons_16x16x1[];
381 /* Main border areas that contain WVS elements */
382 #define WVS_BDR_L 2
383 #define WVS_BDR_T 2
384 #define WVS_BDR_R 2
385 #define WVS_BDR_B 2
387 struct wvs
389 long hide_tick;
390 long show_for;
391 long print_tick;
392 long print_delay;
393 long resume_tick;
394 long resume_delay;
395 long next_auto_refresh;
396 int x;
397 int y;
398 int width;
399 int height;
400 unsigned fgcolor;
401 unsigned bgcolor;
402 unsigned prog_fillcolor;
403 struct vo_rect update_rect;
404 struct vo_rect prog_rect;
405 struct vo_rect time_rect;
406 struct vo_rect dur_rect;
407 struct vo_rect vol_rect;
408 const unsigned char *icons;
409 struct vo_rect stat_rect;
410 int status;
411 uint32_t curr_time;
412 unsigned auto_refresh;
413 unsigned flags;
416 static struct wvs wvs;
418 static void wvs_show(unsigned show);
420 #ifdef LCD_LANDSCAPE
421 #define _X (x + wvs.x)
422 #define _Y (y + wvs.y)
423 #define _W width
424 #define _H height
425 #else
426 #define _X (LCD_WIDTH - (y + wvs.y) - height)
427 #define _Y (x + wvs.x)
428 #define _W height
429 #define _H width
430 #endif
432 #ifdef HAVE_LCD_COLOR
433 /* Blend two colors in 0-100% (0-255) mix of c2 into c1 */
434 static unsigned draw_blendcolor(unsigned c1, unsigned c2, unsigned char amount)
436 int r1 = RGB_UNPACK_RED(c1);
437 int g1 = RGB_UNPACK_GREEN(c1);
438 int b1 = RGB_UNPACK_BLUE(c1);
440 int r2 = RGB_UNPACK_RED(c2);
441 int g2 = RGB_UNPACK_GREEN(c2);
442 int b2 = RGB_UNPACK_BLUE(c2);
444 return LCD_RGBPACK(amount*(r2 - r1) / 255 + r1,
445 amount*(g2 - g1) / 255 + g1,
446 amount*(b2 - b1) / 255 + b1);
448 #endif
450 /* Drawing functions that operate rotated on LCD_PORTRAIT displays -
451 * most are just wrappers of lcd_* functions with transforms applied.
452 * The origin is the upper-left corner of the WVS area */
453 static void draw_update_rect(int x, int y, int width, int height)
455 lcd_(update_rect)(_X, _Y, _W, _H);
458 static void draw_clear_area(int x, int y, int width, int height)
460 #ifdef HAVE_LCD_COLOR
461 rb->screen_clear_area(rb->screens[SCREEN_MAIN], _X, _Y, _W, _H);
462 #else
463 int oldmode = grey_get_drawmode();
464 grey_set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
465 grey_fillrect(_X, _Y, _W, _H);
466 grey_set_drawmode(oldmode);
467 #endif
470 static void draw_clear_area_rect(const struct vo_rect *rc)
472 draw_clear_area(rc->l, rc->t, rc->r - rc->l, rc->b - rc->t);
475 static void draw_fillrect(int x, int y, int width, int height)
477 lcd_(fillrect)(_X, _Y, _W, _H);
480 static void draw_hline(int x1, int x2, int y)
482 #ifdef LCD_LANDSCAPE
483 lcd_(hline)(x1 + wvs.x, x2 + wvs.x, y + wvs.y);
484 #else
485 y = LCD_WIDTH - (y + wvs.y) - 1;
486 lcd_(vline)(y, x1 + wvs.x, x2 + wvs.x);
487 #endif
490 static void draw_vline(int x, int y1, int y2)
492 #ifdef LCD_LANDSCAPE
493 lcd_(vline)(x + wvs.x, y1 + wvs.y, y2 + wvs.y);
494 #else
495 y1 = LCD_WIDTH - (y1 + wvs.y) - 1;
496 y2 = LCD_WIDTH - (y2 + wvs.y) - 1;
497 lcd_(hline)(y1, y2, x + wvs.x);
498 #endif
501 static void draw_scrollbar_draw(int x, int y, int width, int height,
502 uint32_t min, uint32_t max, uint32_t val)
504 unsigned oldfg = lcd_(get_foreground)();
506 draw_hline(x + 1, x + width - 2, y);
507 draw_hline(x + 1, x + width - 2, y + height - 1);
508 draw_vline(x, y + 1, y + height - 2);
509 draw_vline(x + width - 1, y + 1, y + height - 2);
511 val = muldiv_uint32(width - 2, val, max - min);
512 val = MIN(val, (uint32_t)(width - 2));
514 draw_fillrect(x + 1, y + 1, val, height - 2);
516 lcd_(set_foreground)(wvs.prog_fillcolor);
518 draw_fillrect(x + 1 + val, y + 1, width - 2 - val, height - 2);
520 lcd_(set_foreground)(oldfg);
523 static void draw_scrollbar_draw_rect(const struct vo_rect *rc, int min,
524 int max, int val)
526 draw_scrollbar_draw(rc->l, rc->t, rc->r - rc->l, rc->b - rc->t,
527 min, max, val);
530 #ifdef LCD_PORTRAIT
531 /* Portrait displays need rotated text rendering */
533 /* Limited function that only renders in DRMODE_FG and uses absolute screen
534 * coordinates */
535 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
536 int src_x, int src_y,
537 int stride, int x, int y,
538 int width, int height)
540 const unsigned char *src_end;
541 fb_data *dst, *dst_end;
542 unsigned fg_pattern, bg_pattern;
544 if (x + width > SCREEN_WIDTH)
545 width = SCREEN_WIDTH - x; /* Clip right */
546 if (x < 0)
547 width += x, x = 0; /* Clip left */
548 if (width <= 0)
549 return; /* nothing left to do */
551 if (y + height > SCREEN_HEIGHT)
552 height = SCREEN_HEIGHT - y; /* Clip bottom */
553 if (y < 0)
554 height += y, y = 0; /* Clip top */
555 if (height <= 0)
556 return; /* nothing left to do */
558 fg_pattern = rb->lcd_get_foreground();
559 bg_pattern = rb->lcd_get_background();
561 src += stride * (src_y >> 3) + src_x; /* move starting point */
562 src_y &= 7;
563 src_end = src + width;
565 dst = rb->lcd_framebuffer + (LCD_WIDTH - y) + x*LCD_WIDTH;
568 const unsigned char *src_col = src++;
569 unsigned data = *src_col >> src_y;
570 int numbits = 8 - src_y;
572 fb_data *dst_col = dst;
573 dst_end = dst_col - height;
574 dst += LCD_WIDTH;
578 dst_col--;
580 if (data & 1)
581 *dst_col = fg_pattern;
582 #if 0
583 else
584 *dst_col = bg_pattern;
585 #endif
586 data >>= 1;
587 if (--numbits == 0) {
588 src_col += stride;
589 data = *src_col;
590 numbits = 8;
593 while (dst_col > dst_end);
595 while (src < src_end);
598 static void draw_putsxy_oriented(int x, int y, const char *str)
600 unsigned short ch;
601 unsigned short *ucs;
602 int ofs = MIN(x, 0);
603 struct font* pf = rb->font_get(FONT_UI);
605 ucs = rb->bidi_l2v(str, 1);
607 x += wvs.x;
608 y += wvs.y;
610 while ((ch = *ucs++) != 0 && x < SCREEN_WIDTH)
612 int width;
613 const unsigned char *bits;
615 /* get proportional width and glyph bits */
616 width = rb->font_get_width(pf, ch);
618 if (ofs > width) {
619 ofs -= width;
620 continue;
623 bits = rb->font_get_bits(pf, ch);
625 draw_oriented_mono_bitmap_part(bits, ofs, 0, width, x, y,
626 width - ofs, pf->height);
628 x += width - ofs;
629 ofs = 0;
632 #else
633 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
634 int src_x, int src_y,
635 int stride, int x, int y,
636 int width, int height)
638 int mode = lcd_(get_drawmode)();
639 lcd_(set_drawmode)(DRMODE_FG);
640 lcd_(mono_bitmap_part)(src, src_x, src_y, stride, x, y, width, height);
641 lcd_(set_drawmode)(mode);
644 static void draw_putsxy_oriented(int x, int y, const char *str)
646 int mode = lcd_(get_drawmode)();
647 lcd_(set_drawmode)(DRMODE_FG);
648 lcd_(putsxy)(x + wvs.x, y + wvs.y, str);
649 lcd_(set_drawmode)(mode);
651 #endif /* LCD_PORTRAIT */
653 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
654 /* So we can refresh the overlay */
655 static void wvs_lcd_enable_hook(void* param)
657 (void)param;
658 rb->queue_post(rb->button_queue, LCD_ENABLE_EVENT_1, 0);
660 #endif
662 static void wvs_backlight_on_video_mode(bool video_on)
664 if (video_on) {
665 /* Turn off backlight timeout */
666 /* backlight control in lib/helper.c */
667 backlight_force_on();
668 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
669 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
670 #endif
671 } else {
672 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
673 rb->add_event(LCD_EVENT_ACTIVATION, false, wvs_lcd_enable_hook);
674 #endif
675 /* Revert to user's backlight settings */
676 backlight_use_settings();
680 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
681 static void wvs_backlight_brightness_video_mode(bool video_on)
683 if (settings.backlight_brightness < 0)
684 return;
686 mpeg_backlight_update_brightness(
687 video_on ? settings.backlight_brightness : -1);
689 #else
690 #define wvs_backlight_brightness_video_mode(video_on)
691 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
693 static void wvs_text_init(void)
695 struct hms hms;
696 char buf[32];
697 int phys;
698 int spc_width;
700 lcd_(setfont)(FONT_UI);
702 wvs.x = 0;
703 wvs.width = SCREEN_WIDTH;
705 vo_rect_clear(&wvs.time_rect);
706 vo_rect_clear(&wvs.stat_rect);
707 vo_rect_clear(&wvs.prog_rect);
708 vo_rect_clear(&wvs.vol_rect);
710 ts_to_hms(stream_get_duration(), &hms);
711 hms_format(buf, sizeof (buf), &hms);
712 lcd_(getstringsize)(buf, &wvs.time_rect.r, &wvs.time_rect.b);
714 /* Choose well-sized bitmap images relative to font height */
715 if (wvs.time_rect.b < 12) {
716 wvs.icons = mpegplayer_status_icons_8x8x1;
717 wvs.stat_rect.r = wvs.stat_rect.b = 8;
718 } else if (wvs.time_rect.b < 16) {
719 wvs.icons = mpegplayer_status_icons_12x12x1;
720 wvs.stat_rect.r = wvs.stat_rect.b = 12;
721 } else {
722 wvs.icons = mpegplayer_status_icons_16x16x1;
723 wvs.stat_rect.r = wvs.stat_rect.b = 16;
726 if (wvs.stat_rect.b < wvs.time_rect.b) {
727 vo_rect_offset(&wvs.stat_rect, 0,
728 (wvs.time_rect.b - wvs.stat_rect.b) / 2 + WVS_BDR_T);
729 vo_rect_offset(&wvs.time_rect, WVS_BDR_L, WVS_BDR_T);
730 } else {
731 vo_rect_offset(&wvs.time_rect, WVS_BDR_L,
732 wvs.stat_rect.b - wvs.time_rect.b + WVS_BDR_T);
733 vo_rect_offset(&wvs.stat_rect, 0, WVS_BDR_T);
736 wvs.dur_rect = wvs.time_rect;
738 phys = rb->sound_val2phys(SOUND_VOLUME, rb->sound_min(SOUND_VOLUME));
739 rb->snprintf(buf, sizeof(buf), "%d%s", phys,
740 rb->sound_unit(SOUND_VOLUME));
742 lcd_(getstringsize)(" ", &spc_width, NULL);
743 lcd_(getstringsize)(buf, &wvs.vol_rect.r, &wvs.vol_rect.b);
745 wvs.prog_rect.r = SCREEN_WIDTH - WVS_BDR_L - spc_width -
746 wvs.vol_rect.r - WVS_BDR_R;
747 wvs.prog_rect.b = 3*wvs.stat_rect.b / 4;
748 vo_rect_offset(&wvs.prog_rect, wvs.time_rect.l,
749 wvs.time_rect.b);
751 vo_rect_offset(&wvs.stat_rect,
752 (wvs.prog_rect.r + wvs.prog_rect.l - wvs.stat_rect.r) / 2,
755 vo_rect_offset(&wvs.dur_rect,
756 wvs.prog_rect.r - wvs.dur_rect.r, 0);
758 vo_rect_offset(&wvs.vol_rect, wvs.prog_rect.r + spc_width,
759 (wvs.prog_rect.b + wvs.prog_rect.t - wvs.vol_rect.b) / 2);
761 wvs.height = WVS_BDR_T + MAX(wvs.prog_rect.b, wvs.vol_rect.b) -
762 MIN(wvs.time_rect.t, wvs.stat_rect.t) + WVS_BDR_B;
764 #ifdef HAVE_LCD_COLOR
765 wvs.height = ALIGN_UP(wvs.height, 2);
766 #endif
767 wvs.y = SCREEN_HEIGHT - wvs.height;
769 lcd_(setfont)(FONT_SYSFIXED);
772 static void wvs_init(void)
774 wvs.flags = 0;
775 wvs.show_for = HZ*4;
776 wvs.print_delay = 75*HZ/100;
777 wvs.resume_delay = HZ/2;
778 #ifdef HAVE_LCD_COLOR
779 wvs.bgcolor = LCD_RGBPACK(0x73, 0x75, 0xbd);
780 wvs.fgcolor = LCD_WHITE;
781 wvs.prog_fillcolor = LCD_BLACK;
782 #else
783 wvs.bgcolor = GREY_LIGHTGRAY;
784 wvs.fgcolor = GREY_BLACK;
785 wvs.prog_fillcolor = GREY_WHITE;
786 #endif
787 wvs.curr_time = 0;
788 wvs.status = WVS_STATUS_STOPPED;
789 wvs.auto_refresh = WVS_REFRESH_TIME;
790 wvs.next_auto_refresh = *rb->current_tick;
791 wvs_text_init();
794 static void wvs_schedule_refresh(unsigned refresh)
796 long tick = *rb->current_tick;
798 if (refresh & WVS_REFRESH_VIDEO)
799 wvs.print_tick = tick + wvs.print_delay;
801 if (refresh & WVS_REFRESH_RESUME)
802 wvs.resume_tick = tick + wvs.resume_delay;
804 wvs.auto_refresh |= refresh;
807 static void wvs_cancel_refresh(unsigned refresh)
809 wvs.auto_refresh &= ~refresh;
812 /* Refresh the background area */
813 static void wvs_refresh_background(void)
815 char buf[32];
816 struct hms hms;
818 unsigned bg = lcd_(get_background)();
819 lcd_(set_drawmode)(DRMODE_SOLID | DRMODE_INVERSEVID);
821 #ifdef HAVE_LCD_COLOR
822 /* Draw a "raised" area for our graphics */
823 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 192));
824 draw_hline(0, wvs.width, 0);
826 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 80));
827 draw_hline(0, wvs.width, 1);
829 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 48));
830 draw_hline(0, wvs.width, wvs.height-2);
832 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 128));
833 draw_hline(0, wvs.width, wvs.height-1);
835 lcd_(set_background)(bg);
836 draw_clear_area(0, 2, wvs.width, wvs.height - 4);
837 #else
838 /* Give contrast with the main background */
839 lcd_(set_background)(GREY_WHITE);
840 draw_hline(0, wvs.width, 0);
842 lcd_(set_background)(GREY_DARKGRAY);
843 draw_hline(0, wvs.width, wvs.height-1);
845 lcd_(set_background)(bg);
846 draw_clear_area(0, 1, wvs.width, wvs.height - 2);
847 #endif
849 vo_rect_set_ext(&wvs.update_rect, 0, 0, wvs.width, wvs.height);
850 lcd_(set_drawmode)(DRMODE_SOLID);
852 if (stream_get_duration() != INVALID_TIMESTAMP) {
853 /* Draw the movie duration */
854 ts_to_hms(stream_get_duration(), &hms);
855 hms_format(buf, sizeof (buf), &hms);
856 draw_putsxy_oriented(wvs.dur_rect.l, wvs.dur_rect.t, buf);
858 /* else don't know the duration */
861 /* Refresh the current time display + the progress bar */
862 static void wvs_refresh_time(void)
864 char buf[32];
865 struct hms hms;
867 uint32_t duration = stream_get_duration();
869 draw_scrollbar_draw_rect(&wvs.prog_rect, 0, duration,
870 wvs.curr_time);
872 ts_to_hms(wvs.curr_time, &hms);
873 hms_format(buf, sizeof (buf), &hms);
875 draw_clear_area_rect(&wvs.time_rect);
876 draw_putsxy_oriented(wvs.time_rect.l, wvs.time_rect.t, buf);
878 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
879 &wvs.prog_rect);
880 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
881 &wvs.time_rect);
884 /* Refresh the volume display area */
885 static void wvs_refresh_volume(void)
887 char buf[32];
888 int width;
890 int volume = rb->global_settings->volume;
891 rb->snprintf(buf, sizeof (buf), "%d%s",
892 rb->sound_val2phys(SOUND_VOLUME, volume),
893 rb->sound_unit(SOUND_VOLUME));
894 lcd_(getstringsize)(buf, &width, NULL);
896 /* Right-justified */
897 draw_clear_area_rect(&wvs.vol_rect);
898 draw_putsxy_oriented(wvs.vol_rect.r - width, wvs.vol_rect.t, buf);
900 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.vol_rect);
903 /* Refresh the status icon */
904 static void wvs_refresh_status(void)
906 int icon_size = wvs.stat_rect.r - wvs.stat_rect.l;
908 draw_clear_area_rect(&wvs.stat_rect);
910 #ifdef HAVE_LCD_COLOR
911 /* Draw status icon with a drop shadow */
912 unsigned oldfg = lcd_(get_foreground)();
913 int i = 1;
915 lcd_(set_foreground)(draw_blendcolor(lcd_(get_background)(),
916 DRAW_BLACK, 96));
918 while (1)
920 draw_oriented_mono_bitmap_part(wvs.icons,
921 icon_size*wvs.status,
923 icon_size*WVS_STATUS_COUNT,
924 wvs.stat_rect.l + wvs.x + i,
925 wvs.stat_rect.t + wvs.y + i,
926 icon_size, icon_size);
928 if (--i < 0)
929 break;
931 lcd_(set_foreground)(oldfg);
934 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
935 #else
936 draw_oriented_mono_bitmap_part(wvs.icons,
937 icon_size*wvs.status,
939 icon_size*WVS_STATUS_COUNT,
940 wvs.stat_rect.l + wvs.x,
941 wvs.stat_rect.t + wvs.y,
942 icon_size, icon_size);
943 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
944 #endif
947 /* Update the current status which determines which icon is displayed */
948 static bool wvs_update_status(void)
950 int status;
952 switch (stream_status())
954 default:
955 status = WVS_STATUS_STOPPED;
956 break;
957 case STREAM_PAUSED:
958 /* If paused with a pending resume, coerce it to WVS_STATUS_PLAYING */
959 status = (wvs.auto_refresh & WVS_REFRESH_RESUME) ?
960 WVS_STATUS_PLAYING : WVS_STATUS_PAUSED;
961 break;
962 case STREAM_PLAYING:
963 status = WVS_STATUS_PLAYING;
964 break;
967 if (status != wvs.status) {
968 /* A refresh is needed */
969 wvs.status = status;
970 return true;
973 return false;
976 /* Update the current time that will be displayed */
977 static void wvs_update_time(void)
979 uint32_t start;
980 wvs.curr_time = stream_get_seek_time(&start);
981 wvs.curr_time -= start;
984 /* Refresh various parts of the WVS - showing it if it is hidden */
985 static void wvs_refresh(int hint)
987 long tick;
988 unsigned oldbg, oldfg;
990 tick = *rb->current_tick;
992 if (hint == WVS_REFRESH_DEFAULT) {
993 /* The default which forces no updates */
995 /* Make sure Rockbox doesn't turn off the player because of
996 too little activity */
997 if (wvs.status == WVS_STATUS_PLAYING)
998 rb->reset_poweroff_timer();
1000 /* Redraw the current or possibly extract a new video frame */
1001 if ((wvs.auto_refresh & WVS_REFRESH_VIDEO) &&
1002 TIME_AFTER(tick, wvs.print_tick)) {
1003 wvs.auto_refresh &= ~WVS_REFRESH_VIDEO;
1004 stream_draw_frame(false);
1007 /* Restart playback if the timout was reached */
1008 if ((wvs.auto_refresh & WVS_REFRESH_RESUME) &&
1009 TIME_AFTER(tick, wvs.resume_tick)) {
1010 wvs.auto_refresh &= ~(WVS_REFRESH_RESUME | WVS_REFRESH_VIDEO);
1011 stream_resume();
1014 /* If not visible, return */
1015 if (!(wvs.flags & WVS_SHOW))
1016 return;
1018 /* Hide if the visibility duration was reached */
1019 if (TIME_AFTER(tick, wvs.hide_tick)) {
1020 wvs_show(WVS_HIDE);
1021 return;
1023 } else {
1024 /* A forced update of some region */
1026 /* Show if currently invisible */
1027 if (!(wvs.flags & WVS_SHOW)) {
1028 /* Avoid call back into this function - it will be drawn */
1029 wvs_show(WVS_SHOW | WVS_NODRAW);
1030 hint = WVS_REFRESH_ALL;
1033 /* Move back timeouts for frame print and hide */
1034 wvs.print_tick = tick + wvs.print_delay;
1035 wvs.hide_tick = tick + wvs.show_for;
1038 if (TIME_AFTER(tick, wvs.next_auto_refresh)) {
1039 /* Refresh whatever graphical elements are due automatically */
1040 wvs.next_auto_refresh = tick + WVS_MIN_UPDATE_INTERVAL;
1042 if (wvs.auto_refresh & WVS_REFRESH_STATUS) {
1043 if (wvs_update_status())
1044 hint |= WVS_REFRESH_STATUS;
1047 if (wvs.auto_refresh & WVS_REFRESH_TIME) {
1048 wvs_update_time();
1049 hint |= WVS_REFRESH_TIME;
1053 if (hint == 0)
1054 return; /* No drawing needed */
1056 /* Set basic drawing params that are used. Elements that perform variations
1057 * will restore them. */
1058 oldfg = lcd_(get_foreground)();
1059 oldbg = lcd_(get_background)();
1061 lcd_(setfont)(FONT_UI);
1062 lcd_(set_foreground)(wvs.fgcolor);
1063 lcd_(set_background)(wvs.bgcolor);
1065 vo_rect_clear(&wvs.update_rect);
1067 if (hint & WVS_REFRESH_BACKGROUND) {
1068 wvs_refresh_background();
1069 hint |= WVS_REFRESH_ALL; /* Requires a redraw of everything */
1072 if (hint & WVS_REFRESH_TIME) {
1073 wvs_refresh_time();
1076 if (hint & WVS_REFRESH_VOLUME) {
1077 wvs_refresh_volume();
1080 if (hint & WVS_REFRESH_STATUS) {
1081 wvs_refresh_status();
1084 /* Go back to defaults */
1085 lcd_(setfont)(FONT_SYSFIXED);
1086 lcd_(set_foreground)(oldfg);
1087 lcd_(set_background)(oldbg);
1089 /* Update the dirty rectangle */
1090 vo_lock();
1092 draw_update_rect(wvs.update_rect.l,
1093 wvs.update_rect.t,
1094 wvs.update_rect.r - wvs.update_rect.l,
1095 wvs.update_rect.b - wvs.update_rect.t);
1097 vo_unlock();
1100 /* Show/Hide the WVS */
1101 static void wvs_show(unsigned show)
1103 if (((show ^ wvs.flags) & WVS_SHOW) == 0)
1105 if (show & WVS_SHOW) {
1106 wvs.hide_tick = *rb->current_tick + wvs.show_for;
1108 return;
1111 if (show & WVS_SHOW) {
1112 /* Clip away the part of video that is covered */
1113 struct vo_rect rc = { 0, 0, SCREEN_WIDTH, wvs.y };
1115 wvs.flags |= WVS_SHOW;
1117 if (wvs.status != WVS_STATUS_PLAYING) {
1118 /* Not playing - set brightness to mpegplayer setting */
1119 wvs_backlight_brightness_video_mode(true);
1122 stream_vo_set_clip(&rc);
1124 if (!(show & WVS_NODRAW))
1125 wvs_refresh(WVS_REFRESH_ALL);
1126 } else {
1127 /* Uncover clipped video area and redraw it */
1128 wvs.flags &= ~WVS_SHOW;
1130 draw_clear_area(0, 0, wvs.width, wvs.height);
1132 if (!(show & WVS_NODRAW)) {
1133 vo_lock();
1134 draw_update_rect(0, 0, wvs.width, wvs.height);
1135 vo_unlock();
1137 stream_vo_set_clip(NULL);
1138 stream_draw_frame(false);
1139 } else {
1140 stream_vo_set_clip(NULL);
1143 if (wvs.status != WVS_STATUS_PLAYING) {
1144 /* Not playing - restore backlight brightness */
1145 wvs_backlight_brightness_video_mode(false);
1150 /* Set the current status - update screen if specified */
1151 static void wvs_set_status(int status)
1153 bool draw = (status & WVS_NODRAW) == 0;
1155 status &= WVS_STATUS_MASK;
1157 if (wvs.status != status) {
1159 wvs.status = status;
1161 if (draw)
1162 wvs_refresh(WVS_REFRESH_STATUS);
1166 /* Get the current status value */
1167 static int wvs_get_status(void)
1169 return wvs.status & WVS_STATUS_MASK;
1172 /* Handle Fast-forward/Rewind keys using WPS settings (and some nicked code ;) */
1173 static uint32_t wvs_ff_rw(int btn, unsigned refresh)
1175 unsigned int step = TS_SECOND*rb->global_settings->ff_rewind_min_step;
1176 const long ff_rw_accel = (rb->global_settings->ff_rewind_accel + 3);
1177 uint32_t start;
1178 uint32_t time = stream_get_seek_time(&start);
1179 const uint32_t duration = stream_get_duration();
1180 unsigned int max_step = 0;
1181 uint32_t ff_rw_count = 0;
1182 unsigned status = wvs.status;
1184 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME |
1185 WVS_REFRESH_TIME);
1187 time -= start; /* Absolute clock => stream-relative */
1189 switch (btn)
1191 case MPEG_FF:
1192 #ifdef MPEG_RC_FF
1193 case MPEG_RC_FF:
1194 #endif
1195 wvs_set_status(WVS_STATUS_FF);
1196 break;
1197 case MPEG_RW:
1198 #ifdef MPEG_RC_RW
1199 case MPEG_RC_RW:
1200 #endif
1201 wvs_set_status(WVS_STATUS_RW);
1202 break;
1203 default:
1204 btn = -1;
1207 btn |= BUTTON_REPEAT;
1209 while (1)
1211 stream_keep_disk_active();
1213 switch (btn)
1215 case BUTTON_NONE:
1216 wvs_refresh(WVS_REFRESH_DEFAULT);
1217 break;
1219 case MPEG_FF | BUTTON_REPEAT:
1220 case MPEG_RW | BUTTON_REPEAT:
1221 #ifdef MPEG_RC_FF
1222 case MPEG_RC_FF | BUTTON_REPEAT:
1223 case MPEG_RC_RW | BUTTON_REPEAT:
1224 #endif
1225 break;
1227 case MPEG_FF | BUTTON_REL:
1228 case MPEG_RW | BUTTON_REL:
1229 #ifdef MPEG_RC_FF
1230 case MPEG_RC_FF | BUTTON_REL:
1231 case MPEG_RC_RW | BUTTON_REL:
1232 #endif
1233 if (wvs.status == WVS_STATUS_FF)
1234 time += ff_rw_count;
1235 else if (wvs.status == WVS_STATUS_RW)
1236 time -= ff_rw_count;
1238 /* Fall-through */
1239 case -1:
1240 default:
1241 wvs_schedule_refresh(refresh);
1242 wvs_set_status(status);
1243 wvs_schedule_refresh(WVS_REFRESH_TIME);
1244 return time;
1247 if (wvs.status == WVS_STATUS_FF) {
1248 /* fast forwarding, calc max step relative to end */
1249 max_step = muldiv_uint32(duration - (time + ff_rw_count),
1250 FF_REWIND_MAX_PERCENT, 100);
1251 } else {
1252 /* rewinding, calc max step relative to start */
1253 max_step = muldiv_uint32(time - ff_rw_count,
1254 FF_REWIND_MAX_PERCENT, 100);
1257 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
1259 if (step > max_step)
1260 step = max_step;
1262 ff_rw_count += step;
1264 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
1265 step += step >> ff_rw_accel;
1267 if (wvs.status == WVS_STATUS_FF) {
1268 if (duration - time <= ff_rw_count)
1269 ff_rw_count = duration - time;
1271 wvs.curr_time = time + ff_rw_count;
1272 } else {
1273 if (time <= ff_rw_count)
1274 ff_rw_count = time;
1276 wvs.curr_time = time - ff_rw_count;
1279 wvs_refresh(WVS_REFRESH_TIME);
1281 btn = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1285 static int wvs_status(void)
1287 int status = stream_status();
1289 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1290 if (status == STREAM_PAUSED) {
1291 if (wvs.auto_refresh & WVS_REFRESH_RESUME)
1292 status = STREAM_PLAYING;
1295 return status;
1298 /* Change the current audio volume by a specified amount */
1299 static void wvs_set_volume(int delta)
1301 int vol = rb->global_settings->volume;
1302 int limit;
1304 vol += delta;
1306 if (delta < 0) {
1307 /* Volume down - clip to lower limit */
1308 limit = rb->sound_min(SOUND_VOLUME);
1309 if (vol < limit)
1310 vol = limit;
1311 } else {
1312 /* Volume up - clip to upper limit */
1313 limit = rb->sound_max(SOUND_VOLUME);
1314 if (vol > limit)
1315 vol = limit;
1318 /* Sync the global settings */
1319 if (vol != rb->global_settings->volume) {
1320 rb->sound_set(SOUND_VOLUME, vol);
1321 rb->global_settings->volume = vol;
1324 /* Update the volume display */
1325 wvs_refresh(WVS_REFRESH_VOLUME);
1328 /* Begin playback at the specified time */
1329 static int wvs_play(uint32_t time)
1331 int retval;
1333 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1335 retval = stream_seek(time, SEEK_SET);
1337 if (retval >= STREAM_OK) {
1338 wvs_backlight_on_video_mode(true);
1339 wvs_backlight_brightness_video_mode(true);
1340 stream_show_vo(true);
1341 retval = stream_play();
1343 if (retval >= STREAM_OK)
1344 wvs_set_status(WVS_STATUS_PLAYING | WVS_NODRAW);
1347 return retval;
1350 /* Halt playback - pause engine and return logical state */
1351 static int wvs_halt(void)
1353 int status = stream_pause();
1355 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1356 if (status == STREAM_PAUSED) {
1357 if (wvs_get_status() == WVS_STATUS_PLAYING)
1358 status = STREAM_PLAYING;
1361 /* Cancel some auto refreshes - caller will restart them if desired */
1362 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1364 /* No backlight fiddling here - callers does the right thing */
1366 return status;
1369 /* Pause playback if playing */
1370 static int wvs_pause(void)
1372 unsigned refresh = wvs.auto_refresh;
1373 int status = wvs_halt();
1375 if (status == STREAM_PLAYING && (refresh & WVS_REFRESH_RESUME)) {
1376 /* Resume pending - change to a still video frame update */
1377 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1380 wvs_set_status(WVS_STATUS_PAUSED);
1382 wvs_backlight_on_video_mode(false);
1383 /* Leave brightness alone and restore it when WVS is hidden */
1385 return status;
1388 /* Resume playback if halted or paused */
1389 static void wvs_resume(void)
1391 /* Cancel video and resume auto refresh - the resyc when starting
1392 * playback will perform those tasks */
1393 wvs_backlight_on_video_mode(true);
1394 wvs_backlight_brightness_video_mode(true);
1395 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1396 wvs_set_status(WVS_STATUS_PLAYING);
1397 stream_resume();
1400 /* Stop playback - remember the resume point if not closed */
1401 static void wvs_stop(void)
1403 uint32_t resume_time;
1405 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1406 wvs_set_status(WVS_STATUS_STOPPED | WVS_NODRAW);
1407 wvs_show(WVS_HIDE | WVS_NODRAW);
1409 stream_stop();
1411 resume_time = stream_get_resume_time();
1413 if (resume_time != INVALID_TIMESTAMP)
1414 settings.resume_time = resume_time;
1416 wvs_backlight_on_video_mode(false);
1417 wvs_backlight_brightness_video_mode(false);
1420 /* Perform a seek if seeking is possible for this stream - if playing, a delay
1421 * will be inserted before restarting in case the user decides to seek again */
1422 static void wvs_seek(int btn)
1424 int status;
1425 unsigned refresh;
1426 uint32_t time;
1428 if (!stream_can_seek())
1429 return;
1431 /* Halt playback - not strictly nescessary but nice */
1432 status = wvs_halt();
1434 if (status == STREAM_STOPPED)
1435 return;
1437 wvs_show(WVS_SHOW);
1439 if (status == STREAM_PLAYING)
1440 refresh = WVS_REFRESH_RESUME; /* delay resume if playing */
1441 else
1442 refresh = WVS_REFRESH_VIDEO; /* refresh if paused */
1444 /* Obtain a new playback point */
1445 time = wvs_ff_rw(btn, refresh);
1447 /* Tell engine to resume at that time */
1448 stream_seek(time, SEEK_SET);
1451 #ifdef HAVE_HEADPHONE_DETECTION
1452 /* Handle SYS_PHONE_PLUGGED/UNPLUGGED */
1453 static void wvs_handle_phone_plug(bool inserted)
1455 if (rb->global_settings->unplug_mode == 0)
1456 return;
1458 /* Wait for any incomplete state transition to complete first */
1459 stream_wait_status();
1461 int status = wvs_status();
1463 if (inserted) {
1464 if (rb->global_settings->unplug_mode > 1) {
1465 if (status == STREAM_PAUSED) {
1466 wvs_resume();
1469 } else {
1470 if (status == STREAM_PLAYING) {
1471 wvs_pause();
1473 if (stream_can_seek() && rb->global_settings->unplug_rw) {
1474 stream_seek(-rb->global_settings->unplug_rw*TS_SECOND,
1475 SEEK_CUR);
1476 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1477 /* Update time display now */
1478 wvs_update_time();
1479 wvs_refresh(WVS_REFRESH_TIME);
1484 #endif
1486 static void button_loop(void)
1488 rb->lcd_setfont(FONT_SYSFIXED);
1489 #ifdef HAVE_LCD_COLOR
1490 rb->lcd_set_foreground(LCD_WHITE);
1491 rb->lcd_set_background(LCD_BLACK);
1492 #endif
1493 rb->lcd_clear_display();
1494 rb->lcd_update();
1496 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1497 rb->lcd_set_mode(LCD_MODE_YUV);
1498 #endif
1500 wvs_init();
1502 /* Start playback at the specified starting time */
1503 if (wvs_play(settings.resume_time) < STREAM_OK) {
1504 rb->splash(HZ*2, "Playback failed");
1505 return;
1508 /* Gently poll the video player for EOS and handle UI */
1509 while (stream_status() != STREAM_STOPPED)
1511 int button;
1513 mpeg_menu_sysevent_clear();
1514 button = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1516 button = mpeg_menu_sysevent_callback(button, NULL);
1518 switch (button)
1520 case BUTTON_NONE:
1522 wvs_refresh(WVS_REFRESH_DEFAULT);
1523 continue;
1524 } /* BUTTON_NONE: */
1526 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1527 case LCD_ENABLE_EVENT_1:
1529 /* Draw the current frame if prepared already */
1530 stream_draw_frame(true);
1531 break;
1532 } /* LCD_ENABLE_EVENT_1: */
1533 #endif
1535 case MPEG_VOLUP:
1536 case MPEG_VOLUP|BUTTON_REPEAT:
1537 #ifdef MPEG_VOLUP2
1538 case MPEG_VOLUP2:
1539 case MPEG_VOLUP2|BUTTON_REPEAT:
1540 #endif
1541 #ifdef MPEG_RC_VOLUP
1542 case MPEG_RC_VOLUP:
1543 case MPEG_RC_VOLUP|BUTTON_REPEAT:
1544 #endif
1546 wvs_set_volume(+1);
1547 break;
1548 } /* MPEG_VOLUP*: */
1550 case MPEG_VOLDOWN:
1551 case MPEG_VOLDOWN|BUTTON_REPEAT:
1552 #ifdef MPEG_VOLDOWN2
1553 case MPEG_VOLDOWN2:
1554 case MPEG_VOLDOWN2|BUTTON_REPEAT:
1555 #endif
1556 #ifdef MPEG_RC_VOLDOWN
1557 case MPEG_RC_VOLDOWN:
1558 case MPEG_RC_VOLDOWN|BUTTON_REPEAT:
1559 #endif
1561 wvs_set_volume(-1);
1562 break;
1563 } /* MPEG_VOLDOWN*: */
1565 case MPEG_MENU:
1566 #ifdef MPEG_RC_MENU
1567 case MPEG_RC_MENU:
1568 #endif
1570 int state = wvs_halt(); /* save previous state */
1571 int result;
1573 /* Hide video output */
1574 wvs_show(WVS_HIDE | WVS_NODRAW);
1575 stream_show_vo(false);
1576 wvs_backlight_brightness_video_mode(false);
1578 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1579 rb->lcd_set_mode(LCD_MODE_RGB565);
1580 #endif
1582 result = mpeg_menu();
1584 /* The menu can change the font, so restore */
1585 rb->lcd_setfont(FONT_SYSFIXED);
1586 #ifdef HAVE_LCD_COLOR
1587 rb->lcd_set_foreground(LCD_WHITE);
1588 rb->lcd_set_background(LCD_BLACK);
1589 #endif
1590 rb->lcd_clear_display();
1591 rb->lcd_update();
1593 switch (result)
1595 case MPEG_MENU_QUIT:
1596 wvs_stop();
1597 break;
1599 default:
1600 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1601 rb->lcd_set_mode(LCD_MODE_YUV);
1602 #endif
1603 /* If not stopped, show video again */
1604 if (state != STREAM_STOPPED) {
1605 wvs_show(WVS_SHOW);
1606 stream_show_vo(true);
1609 /* If stream was playing, restart it */
1610 if (state == STREAM_PLAYING) {
1611 wvs_resume();
1613 break;
1615 break;
1616 } /* MPEG_MENU: */
1618 case MPEG_STOP:
1619 #ifdef MPEG_RC_STOP
1620 case MPEG_RC_STOP:
1621 #endif
1622 case ACTION_STD_CANCEL:
1624 wvs_stop();
1625 break;
1626 } /* MPEG_STOP: */
1628 case MPEG_PAUSE:
1629 #ifdef MPEG_PAUSE2
1630 case MPEG_PAUSE2:
1631 #endif
1632 #ifdef MPEG_RC_PAUSE
1633 case MPEG_RC_PAUSE:
1634 #endif
1636 int status = wvs_status();
1638 if (status == STREAM_PLAYING) {
1639 /* Playing => Paused */
1640 wvs_pause();
1642 else if (status == STREAM_PAUSED) {
1643 /* Paused => Playing */
1644 wvs_resume();
1647 break;
1648 } /* MPEG_PAUSE*: */
1650 case MPEG_RW:
1651 case MPEG_FF:
1652 #ifdef MPEG_RC_RW
1653 case MPEG_RC_RW:
1654 case MPEG_RC_FF:
1655 #endif
1657 wvs_seek(button);
1658 break;
1659 } /* MPEG_RW: MPEG_FF: */
1661 #ifdef HAVE_HEADPHONE_DETECTION
1662 case SYS_PHONE_PLUGGED:
1663 case SYS_PHONE_UNPLUGGED:
1665 wvs_handle_phone_plug(button == SYS_PHONE_PLUGGED);
1666 break;
1667 } /* SYS_PHONE_*: */
1668 #endif
1670 default:
1672 rb->default_event_handler(button);
1673 break;
1674 } /* default: */
1677 rb->yield();
1678 } /* end while */
1680 wvs_stop();
1682 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1683 /* Be sure hook is removed before exiting since the stop will put it
1684 * back because of the backlight restore. */
1685 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
1686 #endif
1688 rb->lcd_setfont(FONT_UI);
1691 enum plugin_status plugin_start(const void* parameter)
1693 int status = PLUGIN_ERROR; /* assume failure */
1694 int result;
1695 int err;
1696 const char *errstring;
1698 if (parameter == NULL) {
1699 /* No file = GTFO */
1700 rb->splash(HZ*2, "No File");
1701 return PLUGIN_ERROR;
1704 /* Disable all talking before initializing IRAM */
1705 rb->talk_disable(true);
1707 /* Initialize IRAM - stops audio and voice as well */
1708 PLUGIN_IRAM_INIT(rb)
1710 #ifdef HAVE_LCD_COLOR
1711 rb->lcd_set_backdrop(NULL);
1712 rb->lcd_set_foreground(LCD_WHITE);
1713 rb->lcd_set_background(LCD_BLACK);
1714 #endif
1716 rb->lcd_clear_display();
1717 rb->lcd_update();
1719 if (stream_init() < STREAM_OK) {
1720 DEBUGF("Could not initialize streams\n");
1721 } else {
1722 rb->splash(0, "Loading...");
1723 init_settings((char*)parameter);
1725 err = stream_open((char *)parameter);
1727 if (err >= STREAM_OK) {
1728 /* start menu */
1729 rb->lcd_clear_display();
1730 rb->lcd_update();
1731 result = mpeg_start_menu(stream_get_duration());
1733 if (result != MPEG_START_QUIT) {
1734 /* Enter button loop and process UI */
1735 button_loop();
1738 stream_close();
1740 rb->lcd_clear_display();
1741 rb->lcd_update();
1743 save_settings();
1744 status = PLUGIN_OK;
1746 mpeg_menu_sysevent_handle();
1747 } else {
1748 DEBUGF("Could not open %s\n", (char*)parameter);
1749 switch (err)
1751 case STREAM_UNSUPPORTED:
1752 errstring = "Unsupported format";
1753 break;
1754 default:
1755 errstring = "Error opening file: %d";
1758 rb->splashf(HZ*2, errstring, err);
1762 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1763 rb->lcd_set_mode(LCD_MODE_RGB565);
1764 #endif
1766 stream_exit();
1768 rb->talk_disable(false);
1769 return status;