Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / apps / plugins / mpegplayer / mpegplayer.c
blob4c7245dc9482e40fae426a5797bf3b1a363d3a68
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 #elif CONFIG_KEYPAD == PBELL_VIBE500_PAD
312 #define MPEG_MENU BUTTON_MENU
313 #define MPEG_STOP BUTTON_REC
314 #define MPEG_PAUSE BUTTON_PLAY
315 #define MPEG_VOLDOWN BUTTON_DOWN
316 #define MPEG_VOLUP BUTTON_UP
317 #define MPEG_RW BUTTON_PREV
318 #define MPEG_FF BUTTON_NEXT
320 #elif CONFIG_KEYPAD == MPIO_HD200_PAD
321 #define MPEG_MENU BUTTON_SELECT
322 #define MPEG_PAUSE (BUTTON_PLAY | BUTTON_REL)
323 #define MPEG_STOP (BUTTON_PLAY | BUTTON_REPEAT)
324 #define MPEG_VOLDOWN BUTTON_VOL_DOWN
325 #define MPEG_VOLUP BUTTON_VOL_UP
326 #define MPEG_RW BUTTON_PREV
327 #define MPEG_FF BUTTON_NEXT
329 #else
330 #error No keymap defined!
331 #endif
333 #ifdef HAVE_TOUCHSCREEN
334 #ifndef MPEG_MENU
335 #define MPEG_MENU (BUTTON_TOPRIGHT|BUTTON_REL)
336 #endif
337 #ifndef MPEG_STOP
338 #define MPEG_STOP BUTTON_TOPLEFT
339 #endif
340 #ifndef MPEG_PAUSE
341 #define MPEG_PAUSE BUTTON_CENTER
342 #endif
343 #ifndef MPEG_VOLDOWN
344 #define MPEG_VOLDOWN BUTTON_BOTTOMMIDDLE
345 #endif
346 #ifndef MPEG_VOLUP
347 #define MPEG_VOLUP BUTTON_TOPMIDDLE
348 #endif
349 #ifndef MPEG_RW
350 #define MPEG_RW BUTTON_MIDLEFT
351 #endif
352 #ifndef MPEG_FF
353 #define MPEG_FF BUTTON_MIDRIGHT
354 #endif
355 #endif
357 /* One thing we can do here for targets with remotes is having a display
358 * always on the remote instead of always forcing a popup on the main display */
360 #define FF_REWIND_MAX_PERCENT 3 /* cap ff/rewind step size at max % of file */
361 /* 3% of 30min file == 54s step size */
362 #define MIN_FF_REWIND_STEP (TS_SECOND/2)
363 #define WVS_MIN_UPDATE_INTERVAL (HZ/2)
365 /* WVS status - same order as icon array */
366 enum wvs_status_enum
368 WVS_STATUS_STOPPED = 0,
369 WVS_STATUS_PAUSED,
370 WVS_STATUS_PLAYING,
371 WVS_STATUS_FF,
372 WVS_STATUS_RW,
373 WVS_STATUS_COUNT,
374 WVS_STATUS_MASK = 0x7
377 enum wvs_bits
379 WVS_REFRESH_DEFAULT = 0x0000, /* Only refresh elements when due */
380 /* Refresh the... */
381 WVS_REFRESH_VOLUME = 0x0001, /* ...volume display */
382 WVS_REFRESH_TIME = 0x0002, /* ...time display+progress */
383 WVS_REFRESH_STATUS = 0x0004, /* ...playback status icon */
384 WVS_REFRESH_BACKGROUND = 0x0008, /* ...background (implies ALL) */
385 WVS_REFRESH_VIDEO = 0x0010, /* ...video image upon timeout */
386 WVS_REFRESH_RESUME = 0x0020, /* Resume playback upon timeout */
387 WVS_NODRAW = 0x8000, /* OR bitflag - don't draw anything */
388 WVS_SHOW = 0x4000, /* OR bitflag - show the WVS */
389 WVS_HP_PAUSE = 0x2000,
390 WVS_HIDE = 0x0000, /* hide the WVS (aid readability) */
391 WVS_REFRESH_ALL = 0x000f, /* Only immediate graphical elements */
394 /* Status icons selected according to font height */
395 extern const unsigned char mpegplayer_status_icons_8x8x1[];
396 extern const unsigned char mpegplayer_status_icons_12x12x1[];
397 extern const unsigned char mpegplayer_status_icons_16x16x1[];
399 /* Main border areas that contain WVS elements */
400 #define WVS_BDR_L 2
401 #define WVS_BDR_T 2
402 #define WVS_BDR_R 2
403 #define WVS_BDR_B 2
405 struct wvs
407 long hide_tick;
408 long show_for;
409 long print_tick;
410 long print_delay;
411 long resume_tick;
412 long resume_delay;
413 long next_auto_refresh;
414 int x;
415 int y;
416 int width;
417 int height;
418 unsigned fgcolor;
419 unsigned bgcolor;
420 unsigned prog_fillcolor;
421 struct vo_rect update_rect;
422 struct vo_rect prog_rect;
423 struct vo_rect time_rect;
424 struct vo_rect dur_rect;
425 struct vo_rect vol_rect;
426 const unsigned char *icons;
427 struct vo_rect stat_rect;
428 int status;
429 uint32_t curr_time;
430 unsigned auto_refresh;
431 unsigned flags;
434 static struct wvs wvs;
436 static void wvs_show(unsigned show);
438 #ifdef LCD_LANDSCAPE
439 #define _X (x + wvs.x)
440 #define _Y (y + wvs.y)
441 #define _W width
442 #define _H height
443 #else
444 #define _X (LCD_WIDTH - (y + wvs.y) - height)
445 #define _Y (x + wvs.x)
446 #define _W height
447 #define _H width
448 #endif
450 #ifdef HAVE_LCD_COLOR
451 /* Blend two colors in 0-100% (0-255) mix of c2 into c1 */
452 static unsigned draw_blendcolor(unsigned c1, unsigned c2, unsigned char amount)
454 int r1 = RGB_UNPACK_RED(c1);
455 int g1 = RGB_UNPACK_GREEN(c1);
456 int b1 = RGB_UNPACK_BLUE(c1);
458 int r2 = RGB_UNPACK_RED(c2);
459 int g2 = RGB_UNPACK_GREEN(c2);
460 int b2 = RGB_UNPACK_BLUE(c2);
462 return LCD_RGBPACK(amount*(r2 - r1) / 255 + r1,
463 amount*(g2 - g1) / 255 + g1,
464 amount*(b2 - b1) / 255 + b1);
466 #endif
468 /* Drawing functions that operate rotated on LCD_PORTRAIT displays -
469 * most are just wrappers of lcd_* functions with transforms applied.
470 * The origin is the upper-left corner of the WVS area */
471 static void draw_update_rect(int x, int y, int width, int height)
473 lcd_(update_rect)(_X, _Y, _W, _H);
476 static void draw_clear_area(int x, int y, int width, int height)
478 #ifdef HAVE_LCD_COLOR
479 rb->screen_clear_area(rb->screens[SCREEN_MAIN], _X, _Y, _W, _H);
480 #else
481 int oldmode = grey_get_drawmode();
482 grey_set_drawmode(DRMODE_SOLID | DRMODE_INVERSEVID);
483 grey_fillrect(_X, _Y, _W, _H);
484 grey_set_drawmode(oldmode);
485 #endif
488 static void draw_clear_area_rect(const struct vo_rect *rc)
490 draw_clear_area(rc->l, rc->t, rc->r - rc->l, rc->b - rc->t);
493 static void draw_fillrect(int x, int y, int width, int height)
495 lcd_(fillrect)(_X, _Y, _W, _H);
498 static void draw_hline(int x1, int x2, int y)
500 #ifdef LCD_LANDSCAPE
501 lcd_(hline)(x1 + wvs.x, x2 + wvs.x, y + wvs.y);
502 #else
503 y = LCD_WIDTH - (y + wvs.y) - 1;
504 lcd_(vline)(y, x1 + wvs.x, x2 + wvs.x);
505 #endif
508 static void draw_vline(int x, int y1, int y2)
510 #ifdef LCD_LANDSCAPE
511 lcd_(vline)(x + wvs.x, y1 + wvs.y, y2 + wvs.y);
512 #else
513 y1 = LCD_WIDTH - (y1 + wvs.y) - 1;
514 y2 = LCD_WIDTH - (y2 + wvs.y) - 1;
515 lcd_(hline)(y1, y2, x + wvs.x);
516 #endif
519 static void draw_scrollbar_draw(int x, int y, int width, int height,
520 uint32_t min, uint32_t max, uint32_t val)
522 unsigned oldfg = lcd_(get_foreground)();
524 draw_hline(x + 1, x + width - 2, y);
525 draw_hline(x + 1, x + width - 2, y + height - 1);
526 draw_vline(x, y + 1, y + height - 2);
527 draw_vline(x + width - 1, y + 1, y + height - 2);
529 val = muldiv_uint32(width - 2, val, max - min);
530 val = MIN(val, (uint32_t)(width - 2));
532 draw_fillrect(x + 1, y + 1, val, height - 2);
534 lcd_(set_foreground)(wvs.prog_fillcolor);
536 draw_fillrect(x + 1 + val, y + 1, width - 2 - val, height - 2);
538 lcd_(set_foreground)(oldfg);
541 static void draw_scrollbar_draw_rect(const struct vo_rect *rc, int min,
542 int max, int val)
544 draw_scrollbar_draw(rc->l, rc->t, rc->r - rc->l, rc->b - rc->t,
545 min, max, val);
548 #ifdef LCD_PORTRAIT
549 /* Portrait displays need rotated text rendering */
551 /* Limited function that only renders in DRMODE_FG and uses absolute screen
552 * coordinates */
553 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
554 int src_x, int src_y,
555 int stride, int x, int y,
556 int width, int height)
558 const unsigned char *src_end;
559 fb_data *dst, *dst_end;
560 unsigned fg_pattern, bg_pattern;
562 if (x + width > SCREEN_WIDTH)
563 width = SCREEN_WIDTH - x; /* Clip right */
564 if (x < 0)
565 width += x, x = 0; /* Clip left */
566 if (width <= 0)
567 return; /* nothing left to do */
569 if (y + height > SCREEN_HEIGHT)
570 height = SCREEN_HEIGHT - y; /* Clip bottom */
571 if (y < 0)
572 height += y, y = 0; /* Clip top */
573 if (height <= 0)
574 return; /* nothing left to do */
576 fg_pattern = rb->lcd_get_foreground();
577 bg_pattern = rb->lcd_get_background();
579 src += stride * (src_y >> 3) + src_x; /* move starting point */
580 src_y &= 7;
581 src_end = src + width;
583 dst = rb->lcd_framebuffer + (LCD_WIDTH - y) + x*LCD_WIDTH;
586 const unsigned char *src_col = src++;
587 unsigned data = *src_col >> src_y;
588 int numbits = 8 - src_y;
590 fb_data *dst_col = dst;
591 dst_end = dst_col - height;
592 dst += LCD_WIDTH;
596 dst_col--;
598 if (data & 1)
599 *dst_col = fg_pattern;
600 #if 0
601 else
602 *dst_col = bg_pattern;
603 #endif
604 data >>= 1;
605 if (--numbits == 0) {
606 src_col += stride;
607 data = *src_col;
608 numbits = 8;
611 while (dst_col > dst_end);
613 while (src < src_end);
616 static void draw_putsxy_oriented(int x, int y, const char *str)
618 unsigned short ch;
619 unsigned short *ucs;
620 int ofs = MIN(x, 0);
621 struct font* pf = rb->font_get(FONT_UI);
623 ucs = rb->bidi_l2v(str, 1);
625 x += wvs.x;
626 y += wvs.y;
628 while ((ch = *ucs++) != 0 && x < SCREEN_WIDTH)
630 int width;
631 const unsigned char *bits;
633 /* get proportional width and glyph bits */
634 width = rb->font_get_width(pf, ch);
636 if (ofs > width) {
637 ofs -= width;
638 continue;
641 bits = rb->font_get_bits(pf, ch);
643 draw_oriented_mono_bitmap_part(bits, ofs, 0, width, x, y,
644 width - ofs, pf->height);
646 x += width - ofs;
647 ofs = 0;
650 #else
651 static void draw_oriented_mono_bitmap_part(const unsigned char *src,
652 int src_x, int src_y,
653 int stride, int x, int y,
654 int width, int height)
656 int mode = lcd_(get_drawmode)();
657 lcd_(set_drawmode)(DRMODE_FG);
658 lcd_(mono_bitmap_part)(src, src_x, src_y, stride, x, y, width, height);
659 lcd_(set_drawmode)(mode);
662 static void draw_putsxy_oriented(int x, int y, const char *str)
664 int mode = lcd_(get_drawmode)();
665 lcd_(set_drawmode)(DRMODE_FG);
666 lcd_(putsxy)(x + wvs.x, y + wvs.y, str);
667 lcd_(set_drawmode)(mode);
669 #endif /* LCD_PORTRAIT */
671 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
672 /* So we can refresh the overlay */
673 static void wvs_lcd_enable_hook(void* param)
675 (void)param;
676 rb->queue_post(rb->button_queue, LCD_ENABLE_EVENT_1, 0);
678 #endif
680 static void wvs_backlight_on_video_mode(bool video_on)
682 if (video_on) {
683 /* Turn off backlight timeout */
684 /* backlight control in lib/helper.c */
685 backlight_force_on();
686 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
687 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
688 #endif
689 } else {
690 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
691 rb->add_event(LCD_EVENT_ACTIVATION, false, wvs_lcd_enable_hook);
692 #endif
693 /* Revert to user's backlight settings */
694 backlight_use_settings();
698 #ifdef HAVE_BACKLIGHT_BRIGHTNESS
699 static void wvs_backlight_brightness_video_mode(bool video_on)
701 if (settings.backlight_brightness < 0)
702 return;
704 mpeg_backlight_update_brightness(
705 video_on ? settings.backlight_brightness : -1);
707 #else
708 #define wvs_backlight_brightness_video_mode(video_on)
709 #endif /* HAVE_BACKLIGHT_BRIGHTNESS */
711 static void wvs_text_init(void)
713 struct hms hms;
714 char buf[32];
715 int phys;
716 int spc_width;
718 lcd_(setfont)(FONT_UI);
720 wvs.x = 0;
721 wvs.width = SCREEN_WIDTH;
723 vo_rect_clear(&wvs.time_rect);
724 vo_rect_clear(&wvs.stat_rect);
725 vo_rect_clear(&wvs.prog_rect);
726 vo_rect_clear(&wvs.vol_rect);
728 ts_to_hms(stream_get_duration(), &hms);
729 hms_format(buf, sizeof (buf), &hms);
730 lcd_(getstringsize)(buf, &wvs.time_rect.r, &wvs.time_rect.b);
732 /* Choose well-sized bitmap images relative to font height */
733 if (wvs.time_rect.b < 12) {
734 wvs.icons = mpegplayer_status_icons_8x8x1;
735 wvs.stat_rect.r = wvs.stat_rect.b = 8;
736 } else if (wvs.time_rect.b < 16) {
737 wvs.icons = mpegplayer_status_icons_12x12x1;
738 wvs.stat_rect.r = wvs.stat_rect.b = 12;
739 } else {
740 wvs.icons = mpegplayer_status_icons_16x16x1;
741 wvs.stat_rect.r = wvs.stat_rect.b = 16;
744 if (wvs.stat_rect.b < wvs.time_rect.b) {
745 vo_rect_offset(&wvs.stat_rect, 0,
746 (wvs.time_rect.b - wvs.stat_rect.b) / 2 + WVS_BDR_T);
747 vo_rect_offset(&wvs.time_rect, WVS_BDR_L, WVS_BDR_T);
748 } else {
749 vo_rect_offset(&wvs.time_rect, WVS_BDR_L,
750 wvs.stat_rect.b - wvs.time_rect.b + WVS_BDR_T);
751 vo_rect_offset(&wvs.stat_rect, 0, WVS_BDR_T);
754 wvs.dur_rect = wvs.time_rect;
756 phys = rb->sound_val2phys(SOUND_VOLUME, rb->sound_min(SOUND_VOLUME));
757 rb->snprintf(buf, sizeof(buf), "%d%s", phys,
758 rb->sound_unit(SOUND_VOLUME));
760 lcd_(getstringsize)(" ", &spc_width, NULL);
761 lcd_(getstringsize)(buf, &wvs.vol_rect.r, &wvs.vol_rect.b);
763 wvs.prog_rect.r = SCREEN_WIDTH - WVS_BDR_L - spc_width -
764 wvs.vol_rect.r - WVS_BDR_R;
765 wvs.prog_rect.b = 3*wvs.stat_rect.b / 4;
766 vo_rect_offset(&wvs.prog_rect, wvs.time_rect.l,
767 wvs.time_rect.b);
769 vo_rect_offset(&wvs.stat_rect,
770 (wvs.prog_rect.r + wvs.prog_rect.l - wvs.stat_rect.r) / 2,
773 vo_rect_offset(&wvs.dur_rect,
774 wvs.prog_rect.r - wvs.dur_rect.r, 0);
776 vo_rect_offset(&wvs.vol_rect, wvs.prog_rect.r + spc_width,
777 (wvs.prog_rect.b + wvs.prog_rect.t - wvs.vol_rect.b) / 2);
779 wvs.height = WVS_BDR_T + MAX(wvs.prog_rect.b, wvs.vol_rect.b) -
780 MIN(wvs.time_rect.t, wvs.stat_rect.t) + WVS_BDR_B;
782 #ifdef HAVE_LCD_COLOR
783 wvs.height = ALIGN_UP(wvs.height, 2);
784 #endif
785 wvs.y = SCREEN_HEIGHT - wvs.height;
787 lcd_(setfont)(FONT_SYSFIXED);
790 static void wvs_init(void)
792 wvs.flags = 0;
793 wvs.show_for = HZ*4;
794 wvs.print_delay = 75*HZ/100;
795 wvs.resume_delay = HZ/2;
796 #ifdef HAVE_LCD_COLOR
797 wvs.bgcolor = LCD_RGBPACK(0x73, 0x75, 0xbd);
798 wvs.fgcolor = LCD_WHITE;
799 wvs.prog_fillcolor = LCD_BLACK;
800 #else
801 wvs.bgcolor = GREY_LIGHTGRAY;
802 wvs.fgcolor = GREY_BLACK;
803 wvs.prog_fillcolor = GREY_WHITE;
804 #endif
805 wvs.curr_time = 0;
806 wvs.status = WVS_STATUS_STOPPED;
807 wvs.auto_refresh = WVS_REFRESH_TIME;
808 wvs.next_auto_refresh = *rb->current_tick;
809 wvs_text_init();
812 static void wvs_schedule_refresh(unsigned refresh)
814 long tick = *rb->current_tick;
816 if (refresh & WVS_REFRESH_VIDEO)
817 wvs.print_tick = tick + wvs.print_delay;
819 if (refresh & WVS_REFRESH_RESUME)
820 wvs.resume_tick = tick + wvs.resume_delay;
822 wvs.auto_refresh |= refresh;
825 static void wvs_cancel_refresh(unsigned refresh)
827 wvs.auto_refresh &= ~refresh;
830 /* Refresh the background area */
831 static void wvs_refresh_background(void)
833 char buf[32];
834 struct hms hms;
836 unsigned bg = lcd_(get_background)();
837 lcd_(set_drawmode)(DRMODE_SOLID | DRMODE_INVERSEVID);
839 #ifdef HAVE_LCD_COLOR
840 /* Draw a "raised" area for our graphics */
841 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 192));
842 draw_hline(0, wvs.width, 0);
844 lcd_(set_background)(draw_blendcolor(bg, DRAW_WHITE, 80));
845 draw_hline(0, wvs.width, 1);
847 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 48));
848 draw_hline(0, wvs.width, wvs.height-2);
850 lcd_(set_background)(draw_blendcolor(bg, DRAW_BLACK, 128));
851 draw_hline(0, wvs.width, wvs.height-1);
853 lcd_(set_background)(bg);
854 draw_clear_area(0, 2, wvs.width, wvs.height - 4);
855 #else
856 /* Give contrast with the main background */
857 lcd_(set_background)(GREY_WHITE);
858 draw_hline(0, wvs.width, 0);
860 lcd_(set_background)(GREY_DARKGRAY);
861 draw_hline(0, wvs.width, wvs.height-1);
863 lcd_(set_background)(bg);
864 draw_clear_area(0, 1, wvs.width, wvs.height - 2);
865 #endif
867 vo_rect_set_ext(&wvs.update_rect, 0, 0, wvs.width, wvs.height);
868 lcd_(set_drawmode)(DRMODE_SOLID);
870 if (stream_get_duration() != INVALID_TIMESTAMP) {
871 /* Draw the movie duration */
872 ts_to_hms(stream_get_duration(), &hms);
873 hms_format(buf, sizeof (buf), &hms);
874 draw_putsxy_oriented(wvs.dur_rect.l, wvs.dur_rect.t, buf);
876 /* else don't know the duration */
879 /* Refresh the current time display + the progress bar */
880 static void wvs_refresh_time(void)
882 char buf[32];
883 struct hms hms;
885 uint32_t duration = stream_get_duration();
887 draw_scrollbar_draw_rect(&wvs.prog_rect, 0, duration,
888 wvs.curr_time);
890 ts_to_hms(wvs.curr_time, &hms);
891 hms_format(buf, sizeof (buf), &hms);
893 draw_clear_area_rect(&wvs.time_rect);
894 draw_putsxy_oriented(wvs.time_rect.l, wvs.time_rect.t, buf);
896 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
897 &wvs.prog_rect);
898 vo_rect_union(&wvs.update_rect, &wvs.update_rect,
899 &wvs.time_rect);
902 /* Refresh the volume display area */
903 static void wvs_refresh_volume(void)
905 char buf[32];
906 int width;
908 int volume = rb->global_settings->volume;
909 rb->snprintf(buf, sizeof (buf), "%d%s",
910 rb->sound_val2phys(SOUND_VOLUME, volume),
911 rb->sound_unit(SOUND_VOLUME));
912 lcd_(getstringsize)(buf, &width, NULL);
914 /* Right-justified */
915 draw_clear_area_rect(&wvs.vol_rect);
916 draw_putsxy_oriented(wvs.vol_rect.r - width, wvs.vol_rect.t, buf);
918 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.vol_rect);
921 /* Refresh the status icon */
922 static void wvs_refresh_status(void)
924 int icon_size = wvs.stat_rect.r - wvs.stat_rect.l;
926 draw_clear_area_rect(&wvs.stat_rect);
928 #ifdef HAVE_LCD_COLOR
929 /* Draw status icon with a drop shadow */
930 unsigned oldfg = lcd_(get_foreground)();
931 int i = 1;
933 lcd_(set_foreground)(draw_blendcolor(lcd_(get_background)(),
934 DRAW_BLACK, 96));
936 while (1)
938 draw_oriented_mono_bitmap_part(wvs.icons,
939 icon_size*wvs.status,
941 icon_size*WVS_STATUS_COUNT,
942 wvs.stat_rect.l + wvs.x + i,
943 wvs.stat_rect.t + wvs.y + i,
944 icon_size, icon_size);
946 if (--i < 0)
947 break;
949 lcd_(set_foreground)(oldfg);
952 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
953 #else
954 draw_oriented_mono_bitmap_part(wvs.icons,
955 icon_size*wvs.status,
957 icon_size*WVS_STATUS_COUNT,
958 wvs.stat_rect.l + wvs.x,
959 wvs.stat_rect.t + wvs.y,
960 icon_size, icon_size);
961 vo_rect_union(&wvs.update_rect, &wvs.update_rect, &wvs.stat_rect);
962 #endif
965 /* Update the current status which determines which icon is displayed */
966 static bool wvs_update_status(void)
968 int status;
970 switch (stream_status())
972 default:
973 status = WVS_STATUS_STOPPED;
974 break;
975 case STREAM_PAUSED:
976 /* If paused with a pending resume, coerce it to WVS_STATUS_PLAYING */
977 status = (wvs.auto_refresh & WVS_REFRESH_RESUME) ?
978 WVS_STATUS_PLAYING : WVS_STATUS_PAUSED;
979 break;
980 case STREAM_PLAYING:
981 status = WVS_STATUS_PLAYING;
982 break;
985 if (status != wvs.status) {
986 /* A refresh is needed */
987 wvs.status = status;
988 return true;
991 return false;
994 /* Update the current time that will be displayed */
995 static void wvs_update_time(void)
997 uint32_t start;
998 wvs.curr_time = stream_get_seek_time(&start);
999 wvs.curr_time -= start;
1002 /* Refresh various parts of the WVS - showing it if it is hidden */
1003 static void wvs_refresh(int hint)
1005 long tick;
1006 unsigned oldbg, oldfg;
1008 tick = *rb->current_tick;
1010 if (hint == WVS_REFRESH_DEFAULT) {
1011 /* The default which forces no updates */
1013 /* Make sure Rockbox doesn't turn off the player because of
1014 too little activity */
1015 if (wvs.status == WVS_STATUS_PLAYING)
1016 rb->reset_poweroff_timer();
1018 /* Redraw the current or possibly extract a new video frame */
1019 if ((wvs.auto_refresh & WVS_REFRESH_VIDEO) &&
1020 TIME_AFTER(tick, wvs.print_tick)) {
1021 wvs.auto_refresh &= ~WVS_REFRESH_VIDEO;
1022 stream_draw_frame(false);
1025 /* Restart playback if the timout was reached */
1026 if ((wvs.auto_refresh & WVS_REFRESH_RESUME) &&
1027 TIME_AFTER(tick, wvs.resume_tick)) {
1028 wvs.auto_refresh &= ~(WVS_REFRESH_RESUME | WVS_REFRESH_VIDEO);
1029 stream_resume();
1032 /* If not visible, return */
1033 if (!(wvs.flags & WVS_SHOW))
1034 return;
1036 /* Hide if the visibility duration was reached */
1037 if (TIME_AFTER(tick, wvs.hide_tick)) {
1038 wvs_show(WVS_HIDE);
1039 return;
1041 } else {
1042 /* A forced update of some region */
1044 /* Show if currently invisible */
1045 if (!(wvs.flags & WVS_SHOW)) {
1046 /* Avoid call back into this function - it will be drawn */
1047 wvs_show(WVS_SHOW | WVS_NODRAW);
1048 hint = WVS_REFRESH_ALL;
1051 /* Move back timeouts for frame print and hide */
1052 wvs.print_tick = tick + wvs.print_delay;
1053 wvs.hide_tick = tick + wvs.show_for;
1056 if (TIME_AFTER(tick, wvs.next_auto_refresh)) {
1057 /* Refresh whatever graphical elements are due automatically */
1058 wvs.next_auto_refresh = tick + WVS_MIN_UPDATE_INTERVAL;
1060 if (wvs.auto_refresh & WVS_REFRESH_STATUS) {
1061 if (wvs_update_status())
1062 hint |= WVS_REFRESH_STATUS;
1065 if (wvs.auto_refresh & WVS_REFRESH_TIME) {
1066 wvs_update_time();
1067 hint |= WVS_REFRESH_TIME;
1071 if (hint == 0)
1072 return; /* No drawing needed */
1074 /* Set basic drawing params that are used. Elements that perform variations
1075 * will restore them. */
1076 oldfg = lcd_(get_foreground)();
1077 oldbg = lcd_(get_background)();
1079 lcd_(setfont)(FONT_UI);
1080 lcd_(set_foreground)(wvs.fgcolor);
1081 lcd_(set_background)(wvs.bgcolor);
1083 vo_rect_clear(&wvs.update_rect);
1085 if (hint & WVS_REFRESH_BACKGROUND) {
1086 wvs_refresh_background();
1087 hint |= WVS_REFRESH_ALL; /* Requires a redraw of everything */
1090 if (hint & WVS_REFRESH_TIME) {
1091 wvs_refresh_time();
1094 if (hint & WVS_REFRESH_VOLUME) {
1095 wvs_refresh_volume();
1098 if (hint & WVS_REFRESH_STATUS) {
1099 wvs_refresh_status();
1102 /* Go back to defaults */
1103 lcd_(setfont)(FONT_SYSFIXED);
1104 lcd_(set_foreground)(oldfg);
1105 lcd_(set_background)(oldbg);
1107 /* Update the dirty rectangle */
1108 vo_lock();
1110 draw_update_rect(wvs.update_rect.l,
1111 wvs.update_rect.t,
1112 wvs.update_rect.r - wvs.update_rect.l,
1113 wvs.update_rect.b - wvs.update_rect.t);
1115 vo_unlock();
1118 /* Show/Hide the WVS */
1119 static void wvs_show(unsigned show)
1121 if (((show ^ wvs.flags) & WVS_SHOW) == 0)
1123 if (show & WVS_SHOW) {
1124 wvs.hide_tick = *rb->current_tick + wvs.show_for;
1126 return;
1129 if (show & WVS_SHOW) {
1130 /* Clip away the part of video that is covered */
1131 struct vo_rect rc = { 0, 0, SCREEN_WIDTH, wvs.y };
1133 wvs.flags |= WVS_SHOW;
1135 if (wvs.status != WVS_STATUS_PLAYING) {
1136 /* Not playing - set brightness to mpegplayer setting */
1137 wvs_backlight_brightness_video_mode(true);
1140 stream_vo_set_clip(&rc);
1142 if (!(show & WVS_NODRAW))
1143 wvs_refresh(WVS_REFRESH_ALL);
1144 } else {
1145 /* Uncover clipped video area and redraw it */
1146 wvs.flags &= ~WVS_SHOW;
1148 draw_clear_area(0, 0, wvs.width, wvs.height);
1150 if (!(show & WVS_NODRAW)) {
1151 vo_lock();
1152 draw_update_rect(0, 0, wvs.width, wvs.height);
1153 vo_unlock();
1155 stream_vo_set_clip(NULL);
1156 stream_draw_frame(false);
1157 } else {
1158 stream_vo_set_clip(NULL);
1161 if (wvs.status != WVS_STATUS_PLAYING) {
1162 /* Not playing - restore backlight brightness */
1163 wvs_backlight_brightness_video_mode(false);
1168 /* Set the current status - update screen if specified */
1169 static void wvs_set_status(int status)
1171 bool draw = (status & WVS_NODRAW) == 0;
1173 status &= WVS_STATUS_MASK;
1175 if (wvs.status != status) {
1177 wvs.status = status;
1179 if (draw)
1180 wvs_refresh(WVS_REFRESH_STATUS);
1184 /* Get the current status value */
1185 static int wvs_get_status(void)
1187 return wvs.status & WVS_STATUS_MASK;
1190 /* Handle Fast-forward/Rewind keys using WPS settings (and some nicked code ;) */
1191 static uint32_t wvs_ff_rw(int btn, unsigned refresh)
1193 unsigned int step = TS_SECOND*rb->global_settings->ff_rewind_min_step;
1194 const long ff_rw_accel = (rb->global_settings->ff_rewind_accel + 3);
1195 uint32_t start;
1196 uint32_t time = stream_get_seek_time(&start);
1197 const uint32_t duration = stream_get_duration();
1198 unsigned int max_step = 0;
1199 uint32_t ff_rw_count = 0;
1200 unsigned status = wvs.status;
1202 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME |
1203 WVS_REFRESH_TIME);
1205 time -= start; /* Absolute clock => stream-relative */
1207 switch (btn)
1209 case MPEG_FF:
1210 #ifdef MPEG_RC_FF
1211 case MPEG_RC_FF:
1212 #endif
1213 wvs_set_status(WVS_STATUS_FF);
1214 break;
1215 case MPEG_RW:
1216 #ifdef MPEG_RC_RW
1217 case MPEG_RC_RW:
1218 #endif
1219 wvs_set_status(WVS_STATUS_RW);
1220 break;
1221 default:
1222 btn = -1;
1225 btn |= BUTTON_REPEAT;
1227 while (1)
1229 stream_keep_disk_active();
1231 switch (btn)
1233 case BUTTON_NONE:
1234 wvs_refresh(WVS_REFRESH_DEFAULT);
1235 break;
1237 case MPEG_FF | BUTTON_REPEAT:
1238 case MPEG_RW | BUTTON_REPEAT:
1239 #ifdef MPEG_RC_FF
1240 case MPEG_RC_FF | BUTTON_REPEAT:
1241 case MPEG_RC_RW | BUTTON_REPEAT:
1242 #endif
1243 break;
1245 case MPEG_FF | BUTTON_REL:
1246 case MPEG_RW | BUTTON_REL:
1247 #ifdef MPEG_RC_FF
1248 case MPEG_RC_FF | BUTTON_REL:
1249 case MPEG_RC_RW | BUTTON_REL:
1250 #endif
1251 if (wvs.status == WVS_STATUS_FF)
1252 time += ff_rw_count;
1253 else if (wvs.status == WVS_STATUS_RW)
1254 time -= ff_rw_count;
1256 /* Fall-through */
1257 case -1:
1258 default:
1259 wvs_schedule_refresh(refresh);
1260 wvs_set_status(status);
1261 wvs_schedule_refresh(WVS_REFRESH_TIME);
1262 return time;
1265 if (wvs.status == WVS_STATUS_FF) {
1266 /* fast forwarding, calc max step relative to end */
1267 max_step = muldiv_uint32(duration - (time + ff_rw_count),
1268 FF_REWIND_MAX_PERCENT, 100);
1269 } else {
1270 /* rewinding, calc max step relative to start */
1271 max_step = muldiv_uint32(time - ff_rw_count,
1272 FF_REWIND_MAX_PERCENT, 100);
1275 max_step = MAX(max_step, MIN_FF_REWIND_STEP);
1277 if (step > max_step)
1278 step = max_step;
1280 ff_rw_count += step;
1282 /* smooth seeking by multiplying step by: 1 + (2 ^ -accel) */
1283 step += step >> ff_rw_accel;
1285 if (wvs.status == WVS_STATUS_FF) {
1286 if (duration - time <= ff_rw_count)
1287 ff_rw_count = duration - time;
1289 wvs.curr_time = time + ff_rw_count;
1290 } else {
1291 if (time <= ff_rw_count)
1292 ff_rw_count = time;
1294 wvs.curr_time = time - ff_rw_count;
1297 wvs_refresh(WVS_REFRESH_TIME);
1299 btn = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1303 static int wvs_status(void)
1305 int status = stream_status();
1307 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1308 if (status == STREAM_PAUSED) {
1309 if (wvs.auto_refresh & WVS_REFRESH_RESUME)
1310 status = STREAM_PLAYING;
1313 return status;
1316 /* Change the current audio volume by a specified amount */
1317 static void wvs_set_volume(int delta)
1319 int vol = rb->global_settings->volume;
1320 int limit;
1322 vol += delta;
1324 if (delta < 0) {
1325 /* Volume down - clip to lower limit */
1326 limit = rb->sound_min(SOUND_VOLUME);
1327 if (vol < limit)
1328 vol = limit;
1329 } else {
1330 /* Volume up - clip to upper limit */
1331 limit = rb->sound_max(SOUND_VOLUME);
1332 if (vol > limit)
1333 vol = limit;
1336 /* Sync the global settings */
1337 if (vol != rb->global_settings->volume) {
1338 rb->sound_set(SOUND_VOLUME, vol);
1339 rb->global_settings->volume = vol;
1342 /* Update the volume display */
1343 wvs_refresh(WVS_REFRESH_VOLUME);
1346 /* Begin playback at the specified time */
1347 static int wvs_play(uint32_t time)
1349 int retval;
1351 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1353 retval = stream_seek(time, SEEK_SET);
1355 if (retval >= STREAM_OK) {
1356 wvs_backlight_on_video_mode(true);
1357 wvs_backlight_brightness_video_mode(true);
1358 stream_show_vo(true);
1359 retval = stream_play();
1361 if (retval >= STREAM_OK)
1362 wvs_set_status(WVS_STATUS_PLAYING | WVS_NODRAW);
1365 return retval;
1368 /* Halt playback - pause engine and return logical state */
1369 static int wvs_halt(void)
1371 int status = stream_pause();
1373 /* Coerce to STREAM_PLAYING if paused with a pending resume */
1374 if (status == STREAM_PAUSED) {
1375 if (wvs_get_status() == WVS_STATUS_PLAYING)
1376 status = STREAM_PLAYING;
1379 /* Cancel some auto refreshes - caller will restart them if desired */
1380 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1382 /* No backlight fiddling here - callers does the right thing */
1384 return status;
1387 /* Pause playback if playing */
1388 static int wvs_pause(void)
1390 unsigned refresh = wvs.auto_refresh;
1391 int status = wvs_halt();
1393 if (status == STREAM_PLAYING && (refresh & WVS_REFRESH_RESUME)) {
1394 /* Resume pending - change to a still video frame update */
1395 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1398 wvs_set_status(WVS_STATUS_PAUSED);
1400 wvs_backlight_on_video_mode(false);
1401 /* Leave brightness alone and restore it when WVS is hidden */
1403 return status;
1406 /* Resume playback if halted or paused */
1407 static void wvs_resume(void)
1409 /* Cancel video and resume auto refresh - the resyc when starting
1410 * playback will perform those tasks */
1411 wvs_backlight_on_video_mode(true);
1412 wvs_backlight_brightness_video_mode(true);
1413 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1414 wvs_set_status(WVS_STATUS_PLAYING);
1415 stream_resume();
1418 /* Stop playback - remember the resume point if not closed */
1419 static void wvs_stop(void)
1421 uint32_t resume_time;
1423 wvs_cancel_refresh(WVS_REFRESH_VIDEO | WVS_REFRESH_RESUME);
1424 wvs_set_status(WVS_STATUS_STOPPED | WVS_NODRAW);
1425 wvs_show(WVS_HIDE | WVS_NODRAW);
1427 stream_stop();
1429 resume_time = stream_get_resume_time();
1431 if (resume_time != INVALID_TIMESTAMP)
1432 settings.resume_time = resume_time;
1434 wvs_backlight_on_video_mode(false);
1435 wvs_backlight_brightness_video_mode(false);
1438 /* Perform a seek if seeking is possible for this stream - if playing, a delay
1439 * will be inserted before restarting in case the user decides to seek again */
1440 static void wvs_seek(int btn)
1442 int status;
1443 unsigned refresh;
1444 uint32_t time;
1446 if (!stream_can_seek())
1447 return;
1449 /* Halt playback - not strictly nescessary but nice */
1450 status = wvs_halt();
1452 if (status == STREAM_STOPPED)
1453 return;
1455 wvs_show(WVS_SHOW);
1457 if (status == STREAM_PLAYING)
1458 refresh = WVS_REFRESH_RESUME; /* delay resume if playing */
1459 else
1460 refresh = WVS_REFRESH_VIDEO; /* refresh if paused */
1462 /* Obtain a new playback point */
1463 time = wvs_ff_rw(btn, refresh);
1465 /* Tell engine to resume at that time */
1466 stream_seek(time, SEEK_SET);
1469 #ifdef HAVE_HEADPHONE_DETECTION
1470 /* Handle SYS_PHONE_PLUGGED/UNPLUGGED */
1471 static void wvs_handle_phone_plug(bool inserted)
1473 if (rb->global_settings->unplug_mode == 0)
1474 return;
1476 /* Wait for any incomplete state transition to complete first */
1477 stream_wait_status();
1479 int status = wvs_status();
1481 if (inserted) {
1482 if (rb->global_settings->unplug_mode > 1) {
1483 if (status == STREAM_PAUSED) {
1484 wvs_resume();
1487 } else {
1488 if (status == STREAM_PLAYING) {
1489 wvs_pause();
1491 if (stream_can_seek() && rb->global_settings->unplug_rw) {
1492 stream_seek(-rb->global_settings->unplug_rw*TS_SECOND,
1493 SEEK_CUR);
1494 wvs_schedule_refresh(WVS_REFRESH_VIDEO);
1495 /* Update time display now */
1496 wvs_update_time();
1497 wvs_refresh(WVS_REFRESH_TIME);
1502 #endif
1504 static void button_loop(void)
1506 rb->lcd_setfont(FONT_SYSFIXED);
1507 #ifdef HAVE_LCD_COLOR
1508 rb->lcd_set_foreground(LCD_WHITE);
1509 rb->lcd_set_background(LCD_BLACK);
1510 #endif
1511 rb->lcd_clear_display();
1512 rb->lcd_update();
1514 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1515 rb->lcd_set_mode(LCD_MODE_YUV);
1516 #endif
1518 wvs_init();
1520 /* Start playback at the specified starting time */
1521 if (wvs_play(settings.resume_time) < STREAM_OK) {
1522 rb->splash(HZ*2, "Playback failed");
1523 return;
1526 /* Gently poll the video player for EOS and handle UI */
1527 while (stream_status() != STREAM_STOPPED)
1529 int button;
1531 mpeg_menu_sysevent_clear();
1532 button = rb->button_get_w_tmo(WVS_MIN_UPDATE_INTERVAL);
1534 button = mpeg_menu_sysevent_callback(button, NULL);
1536 switch (button)
1538 case BUTTON_NONE:
1540 wvs_refresh(WVS_REFRESH_DEFAULT);
1541 continue;
1542 } /* BUTTON_NONE: */
1544 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1545 case LCD_ENABLE_EVENT_1:
1547 /* Draw the current frame if prepared already */
1548 stream_draw_frame(true);
1549 break;
1550 } /* LCD_ENABLE_EVENT_1: */
1551 #endif
1553 case MPEG_VOLUP:
1554 case MPEG_VOLUP|BUTTON_REPEAT:
1555 #ifdef MPEG_VOLUP2
1556 case MPEG_VOLUP2:
1557 case MPEG_VOLUP2|BUTTON_REPEAT:
1558 #endif
1559 #ifdef MPEG_RC_VOLUP
1560 case MPEG_RC_VOLUP:
1561 case MPEG_RC_VOLUP|BUTTON_REPEAT:
1562 #endif
1564 wvs_set_volume(+1);
1565 break;
1566 } /* MPEG_VOLUP*: */
1568 case MPEG_VOLDOWN:
1569 case MPEG_VOLDOWN|BUTTON_REPEAT:
1570 #ifdef MPEG_VOLDOWN2
1571 case MPEG_VOLDOWN2:
1572 case MPEG_VOLDOWN2|BUTTON_REPEAT:
1573 #endif
1574 #ifdef MPEG_RC_VOLDOWN
1575 case MPEG_RC_VOLDOWN:
1576 case MPEG_RC_VOLDOWN|BUTTON_REPEAT:
1577 #endif
1579 wvs_set_volume(-1);
1580 break;
1581 } /* MPEG_VOLDOWN*: */
1583 case MPEG_MENU:
1584 #ifdef MPEG_RC_MENU
1585 case MPEG_RC_MENU:
1586 #endif
1588 int state = wvs_halt(); /* save previous state */
1589 int result;
1591 /* Hide video output */
1592 wvs_show(WVS_HIDE | WVS_NODRAW);
1593 stream_show_vo(false);
1594 wvs_backlight_brightness_video_mode(false);
1596 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1597 rb->lcd_set_mode(LCD_MODE_RGB565);
1598 #endif
1600 result = mpeg_menu();
1602 /* The menu can change the font, so restore */
1603 rb->lcd_setfont(FONT_SYSFIXED);
1604 #ifdef HAVE_LCD_COLOR
1605 rb->lcd_set_foreground(LCD_WHITE);
1606 rb->lcd_set_background(LCD_BLACK);
1607 #endif
1608 rb->lcd_clear_display();
1609 rb->lcd_update();
1611 switch (result)
1613 case MPEG_MENU_QUIT:
1614 wvs_stop();
1615 break;
1617 default:
1618 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1619 rb->lcd_set_mode(LCD_MODE_YUV);
1620 #endif
1621 /* If not stopped, show video again */
1622 if (state != STREAM_STOPPED) {
1623 wvs_show(WVS_SHOW);
1624 stream_show_vo(true);
1627 /* If stream was playing, restart it */
1628 if (state == STREAM_PLAYING) {
1629 wvs_resume();
1631 break;
1633 break;
1634 } /* MPEG_MENU: */
1636 case MPEG_STOP:
1637 #ifdef MPEG_RC_STOP
1638 case MPEG_RC_STOP:
1639 #endif
1640 case ACTION_STD_CANCEL:
1642 wvs_stop();
1643 break;
1644 } /* MPEG_STOP: */
1646 case MPEG_PAUSE:
1647 #ifdef MPEG_PAUSE2
1648 case MPEG_PAUSE2:
1649 #endif
1650 #ifdef MPEG_RC_PAUSE
1651 case MPEG_RC_PAUSE:
1652 #endif
1654 int status = wvs_status();
1656 if (status == STREAM_PLAYING) {
1657 /* Playing => Paused */
1658 wvs_pause();
1660 else if (status == STREAM_PAUSED) {
1661 /* Paused => Playing */
1662 wvs_resume();
1665 break;
1666 } /* MPEG_PAUSE*: */
1668 case MPEG_RW:
1669 case MPEG_FF:
1670 #ifdef MPEG_RC_RW
1671 case MPEG_RC_RW:
1672 case MPEG_RC_FF:
1673 #endif
1675 wvs_seek(button);
1676 break;
1677 } /* MPEG_RW: MPEG_FF: */
1679 #ifdef HAVE_HEADPHONE_DETECTION
1680 case SYS_PHONE_PLUGGED:
1681 case SYS_PHONE_UNPLUGGED:
1683 wvs_handle_phone_plug(button == SYS_PHONE_PLUGGED);
1684 break;
1685 } /* SYS_PHONE_*: */
1686 #endif
1688 default:
1690 rb->default_event_handler(button);
1691 break;
1692 } /* default: */
1695 rb->yield();
1696 } /* end while */
1698 wvs_stop();
1700 #if defined(HAVE_LCD_ENABLE) || defined(HAVE_LCD_SLEEP)
1701 /* Be sure hook is removed before exiting since the stop will put it
1702 * back because of the backlight restore. */
1703 rb->remove_event(LCD_EVENT_ACTIVATION, wvs_lcd_enable_hook);
1704 #endif
1706 rb->lcd_setfont(FONT_UI);
1709 enum plugin_status plugin_start(const void* parameter)
1711 int status = PLUGIN_ERROR; /* assume failure */
1712 int result;
1713 int err;
1714 const char *errstring;
1716 if (parameter == NULL) {
1717 /* No file = GTFO */
1718 rb->splash(HZ*2, "No File");
1719 return PLUGIN_ERROR;
1722 /* Disable all talking before initializing IRAM */
1723 rb->talk_disable(true);
1725 /* Initialize IRAM - stops audio and voice as well */
1726 PLUGIN_IRAM_INIT(rb)
1728 #ifdef HAVE_LCD_COLOR
1729 rb->lcd_set_backdrop(NULL);
1730 rb->lcd_set_foreground(LCD_WHITE);
1731 rb->lcd_set_background(LCD_BLACK);
1732 #endif
1734 rb->lcd_clear_display();
1735 rb->lcd_update();
1737 if (stream_init() < STREAM_OK) {
1738 DEBUGF("Could not initialize streams\n");
1739 } else {
1740 rb->splash(0, "Loading...");
1741 init_settings((char*)parameter);
1743 err = stream_open((char *)parameter);
1745 if (err >= STREAM_OK) {
1746 /* start menu */
1747 rb->lcd_clear_display();
1748 rb->lcd_update();
1749 result = mpeg_start_menu(stream_get_duration());
1751 if (result != MPEG_START_QUIT) {
1752 /* Enter button loop and process UI */
1753 button_loop();
1756 stream_close();
1758 rb->lcd_clear_display();
1759 rb->lcd_update();
1761 save_settings();
1762 status = PLUGIN_OK;
1764 mpeg_menu_sysevent_handle();
1765 } else {
1766 DEBUGF("Could not open %s\n", (char*)parameter);
1767 switch (err)
1769 case STREAM_UNSUPPORTED:
1770 errstring = "Unsupported format";
1771 break;
1772 default:
1773 errstring = "Error opening file: %d";
1776 rb->splashf(HZ*2, errstring, err);
1780 #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_YUV)
1781 rb->lcd_set_mode(LCD_MODE_RGB565);
1782 #endif
1784 stream_exit();
1786 rb->talk_disable(false);
1787 return status;