player_ui.c 24.6 KB
Newer Older
1 2 3 4 5
#include <rtgui/rtgui.h>
#include <rtgui/image.h>
#include <rtgui/rtgui_system.h>

#include <rtgui/widgets/view.h>
6
#include <rtgui/widgets/label.h>
7
#include <rtgui/widgets/list_view.h>
8
#include <rtgui/widgets/workbench.h>
9
#include <rtgui/widgets/filelist_view.h>
10

11
#include <string.h>
12 13
#include <dfs_posix.h>

14 15
#include "mp3.h"
#include "picture.h"
16 17 18
#include "player_ui.h"
#include "player_bg.h"
#include "play_list.h"
19
#include "station_list.h"
20 21 22 23

#include "play.hdh"
#include "stop.hdh"

24
#define RADIO_FN    "/radio.pls"
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
const static char *time_bg_xpm[] = {
"48 20 7 1",
".	c #007DC6",
"+	c #0079C6",
"@	c #0079BD",
"#	c #0075BD",
"$	c #0071BD",
"%	c #0071B5",
"&	c #006DB5",
"................................................",
"................................................",
"................................................",
"................................................",
"................................................",
"++++++++++++++++++++++++++++++++++++++++++++++++",
"++++++++++++++++++++++++++++++++++++++++++++++++",
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
"################################################",
"################################################",
"################################################",
"################################################",
"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&",
"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"};

57
static struct rtgui_view* home_view;
58
static struct rtgui_list_view* function_view;
59 60 61 62 63 64
static struct rtgui_workbench* workbench;
static rtgui_timer_t* info_timer;
static rt_thread_t player_ui_tid = RT_NULL;
static enum PLAYER_MODE player_mode = PLAYER_STOP;
static enum PLAYER_STEP next_step = PLAYER_STEP_STOP;
static struct tag_info tinfo;
65 66
static rt_uint32_t play_time;
static rtgui_image_t *time_bg_image;
67

68 69
void player_play_file(const char* fn);
void player_play_url(const char* url);
B
bernard.xiong 已提交
70

71
static void info_timer_timeout(rtgui_timer_t* timer, void* parameter)
72 73 74 75 76 77 78 79 80 81
{
	struct rtgui_dc* dc;
	rtgui_color_t saved;

	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
	if (dc == RT_NULL) return ;

	saved = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view));

	RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(206, 231, 255);
82
	rtgui_dc_draw_hline(dc, 14, 14	+ (tinfo.position * 212) / tinfo.duration, 75);
83

84
	if ((player_mode == PLAYER_PLAY_RADIO) && ((tinfo.position * 212 + 14)/tinfo.duration) < 226)
B
bernard.xiong 已提交
85 86
	{
		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(82, 199, 16);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
		rtgui_dc_draw_hline(dc, 14  + (tinfo.position * 212) / tinfo.duration, 226, 75);
	}
    RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = saved;

	if (player_mode == PLAYER_PLAY_FILE)
	{
		rtgui_rect_t rect;
	    char line[32];

	    play_time++;
		rt_snprintf(line, sizeof(line), "%3d:%02d", play_time / 60, play_time % 60);

		rect.x1 = 172; rect.y1 = 39;
	    rect.x2 = 220; rect.y2 = 59;
	    rtgui_image_blit(time_bg_image, dc, &rect);
	    rtgui_dc_draw_text(dc, line, &rect);
B
bernard.xiong 已提交
103
	}
104

105 106 107 108 109 110 111 112
	rtgui_dc_end_drawing(dc);
}

static void player_update_tag_info(struct rtgui_dc* dc)
{
	rtgui_rect_t rect;
    char line[32];
	rtgui_color_t saved;
113
	rtgui_image_t *background;
114 115 116 117 118 119 120

	saved = rtgui_dc_get_color(dc);
	rtgui_dc_set_color(dc, black);

	rect.x1 = 0; rect.y1 = 0;
	rect.x2 = 240; rect.y2 = 65;
	/* draw background */
121
    background = rtgui_image_create_from_file("hdc", "/resource/bg.hdc", RT_FALSE);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	if (background != RT_NULL)
	{
        rtgui_image_blit(background, dc, &rect);
		rtgui_image_destroy(background);

		background = RT_NULL;
	}
	else
	{
		rtgui_dc_fill_rect(dc, &rect);
	}

	/* draw playing information */
    rect.x1 = 28; rect.y1 = 12;
    rect.x2 = 220; rect.y2 = rect.y1 + 16;
	if (player_mode == PLAYER_STOP)
	{
139
		rt_snprintf(line, sizeof(line), "网络收音机");
140 141 142 143 144 145 146 147 148
	    rtgui_dc_draw_text(dc, line, &rect);
	}
	else
	    rtgui_dc_draw_text(dc, tinfo.title, &rect);

    rect.x1 = 28; rect.y1 = 39;
    rect.x2 = 220; rect.y2 = 59;
	if (player_mode == PLAYER_STOP)
	{
149
		rt_snprintf(line, sizeof(line), "radio.rt-thread.org");
150 151 152 153 154
	    rtgui_dc_draw_text(dc, line, &rect);
	}
	else
	    rtgui_dc_draw_text(dc, tinfo.artist, &rect);

B
bernard.xiong 已提交
155
	if ((tinfo.duration != 0) && player_mode == PLAYER_PLAY_FILE)
156
	{
157 158 159 160
		play_time = 0;

        rect.x1 = 172;
		rt_snprintf(line, sizeof(line), "  0:00");
161 162 163 164 165 166
        rtgui_dc_draw_text(dc, line, &rect);
	}

	rtgui_dc_set_color(dc, saved);
}

167
static rt_uint32_t read_line(int fd, char* line, rt_uint32_t line_size)
168
{
169 170
    char *pos, *next;
    rt_uint32_t length;
171

172 173 174 175 176 177 178 179 180 181
    length = read(fd, line, line_size);
    if (length > 0)
    {
        pos = strstr(line, "\r\n");
		if (pos == RT_NULL)
		{
			pos = strstr(line, "\n");
			next = pos ++;
		}
		else next = pos + 2;
182

183 184 185
        if (pos != RT_NULL)
        {
            *pos = '\0';
186

187 188
            /* move back */
            lseek(fd, -(length - (next - line)), SEEK_CUR);
189

190 191 192 193
            length = pos - line;
        }
        else length = 0;
    }
194

195
    return length;
B
bernard.xiong 已提交
196 197
}

198
static void function_play_radio(void* parameter)
B
bernard.xiong 已提交
199
{
200 201
	struct station_list* list;
	struct station_item* item;
B
bernard.xiong 已提交
202

203 204
	list = station_list_create(RADIO_FN);
	if (list != RT_NULL)
B
bernard.xiong 已提交
205
	{
206 207 208 209 210
		item = station_list_select(list, workbench);
		if (item != RT_NULL)
		{
			player_play_url(item->url);
		}
B
bernard.xiong 已提交
211

212
		station_list_destroy(list);
B
bernard.xiong 已提交
213 214 215
	}
}

wuyangyong's avatar
wuyangyong 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static void function_radio_list_update(void* parameter)
{
	extern void update_radio_list_req(void);
	extern void update_radio_thread(void* parameter);
	extern rt_mq_t update_radio_mq;
	rt_thread_t update_radio_list_thread;


	if(update_radio_mq == RT_NULL)
	{
		update_radio_mq = rt_mq_create("updateRadioList", sizeof(struct player_request),
			1, RT_IPC_FLAG_FIFO);
		RT_ASSERT(update_radio_mq != RT_NULL);

		update_radio_list_thread = rt_thread_create("update_bg", update_radio_thread, RT_NULL,
			1024 ,20, 5);

		if (update_radio_list_thread == RT_NULL) rt_kprintf("updateRadioList thread init failed\n");
		else
		{
			rt_thread_startup(update_radio_list_thread);
			update_radio_list_req();
		}
	}
	return;
}

243
static void function_filelist(void* parameter)
244 245
{
	rtgui_rect_t rect;
246
	rtgui_filelist_view_t *view;
247 248

	rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
249
	view = rtgui_filelist_view_create(workbench, "/", "*.*", &rect);
250 251 252 253 254 255 256 257 258 259 260
	if (view != RT_NULL)
	{
	    if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
	    {
			char fn[64];

            /* get open file */
            rt_snprintf(fn, 64, "%s/%s", view->current_directory,
                view->items[view->current_item].name);

            if (strstr(view->items[view->current_item].name , ".mp3") != RT_NULL ||
B
bernard.xiong 已提交
261 262 263
                strstr(view->items[view->current_item].name , ".MP3") != RT_NULL ||
                strstr(view->items[view->current_item].name , ".wav") != RT_NULL ||
                strstr(view->items[view->current_item].name , ".WAV") != RT_NULL)
264 265 266 267 268 269 270
            {
				/* clear old play list */
				play_list_clear();
				play_list_append(fn);

			    player_mode = PLAYER_PLAY_FILE;
				next_step = PLAYER_STEP_STOP;
B
bernard.xiong 已提交
271
				player_play_file(play_list_start());
272 273 274 275 276 277 278 279 280 281 282 283 284 285
            }
            else if (strstr(view->items[view->current_item].name , ".m3u") != RT_NULL ||
                strstr(view->items[view->current_item].name , ".M3U") != RT_NULL)
            {
                /* read all of music filename to a list */
                int fd;
                char line[64];

                fd = open(fn, O_RDONLY, 0);
                if (fd >= 0)
                {
                    rt_uint32_t length;

                    length = read_line(fd, line, sizeof(line));
286 287
					/* clear old play list */
					play_list_clear();
288

289 290 291 292
                    do
                    {
                        length = read_line(fd, line, sizeof(line));
                        if (length > 0)
293
                        {
294 295 296 297 298
                        	if (strstr(line, "http:") != RT_NULL)
                        	{
                        		play_list_append(line);
                        	}
							else if (line[0] != '/')
299
                            {
300
                                rt_snprintf(fn, sizeof(fn), "%s/%s", view->current_directory, line);
301
                                play_list_append(fn);
302
                            }
303 304 305
                            else play_list_append(line);
                        }
                    } while (length > 0);
306 307 308 309 310 311 312

                    close(fd);

					if (play_list_items() > 0)
					{
					    player_mode = PLAYER_PLAY_FILE;
						next_step = PLAYER_STEP_NEXT;
B
bernard.xiong 已提交
313
		                player_play_file(play_list_start());
314 315 316 317 318 319
					}
                }
            }
	    }

		/* destroy view */
320
		rtgui_filelist_view_destroy(view);
321 322 323 324 325
	}

	return;
}

326
static void function_device(void* parameter)
327 328 329 330 331 332 333 334 335 336 337 338 339
{
	rtgui_view_t *view;
	extern rtgui_view_t* device_view_create(rtgui_workbench_t* workbench);

	view = device_view_create(workbench);
	if (view != RT_NULL)
	{
	    rtgui_view_show(view, RT_FALSE);
	}

	return;
}

340
static void function_player(void* parameter)
341 342 343 344 345
{
    rtgui_view_show(home_view, RT_FALSE);
	return;
}

346
static void function_show_picure(void* parameter)
347 348 349 350 351 352 353 354 355 356 357 358 359
{
	rtgui_view_t *view;

	view = picture_view_create(workbench);
	if (view != RT_NULL)
	{
		rtgui_view_show(view, RT_TRUE);
		rtgui_view_destroy(view);
	}

	return;
}

360 361 362 363 364 365
void function_action(void* parameter)
{
	rt_kprintf("item action!\n");
	return;
}

366
void function_cable(void* parameter)
367
{
368
	extern void USB_cable(void);
369 370 371
    USB_cable();
}

372
const struct rtgui_list_item function_list[] =
373
{
B
bernard.xiong 已提交
374
	{"选择电台", RT_NULL, function_play_radio, RT_NULL},
wuyangyong's avatar
wuyangyong 已提交
375
	{"更新电台", RT_NULL, function_radio_list_update, RT_NULL},
376
	{"播放文件", RT_NULL, function_filelist, RT_NULL},
377
	{"浏览图片", RT_NULL, function_show_picure, RT_NULL},
378 379
	{"设备信息", RT_NULL, function_device, RT_NULL},
	{"选项设置", RT_NULL, function_action, RT_NULL},
380
	{"USB 联机", RT_NULL, function_cable, RT_NULL},
381 382 383
	{"返回播放器", RT_NULL, function_player, RT_NULL},
};

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
void player_set_position(rt_uint32_t position)
{
	if (player_mode != PLAYER_PLAY_RADIO)
	{
	    tinfo.position = position / (tinfo.bit_rate / 8);
	}
	else
	{
		tinfo.position = position;
	}
}

void player_set_title(const char* title)
{
	strncpy(tinfo.title, title, 40);
}

void player_set_buffer_status(rt_bool_t buffering)
{
	if (buffering == RT_TRUE)
		strncpy(tinfo.artist, "缓冲中...", 40);
	else
		strncpy(tinfo.artist, "播放中...", 40);
}

409 410 411 412 413
enum PLAYER_MODE player_get_mode()
{
	return player_mode;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
void player_play_file(const char* fn)
{
	struct rtgui_dc* dc;
	rtgui_color_t saved;
	rt_bool_t is_mp3;

	is_mp3 = RT_FALSE;

	if (strstr(fn, ".mp3") != RT_NULL ||
		strstr(fn, ".MP3") != RT_NULL)
		is_mp3 = RT_TRUE;
	else if (strstr(fn, ".wav") != RT_NULL ||
		strstr(fn, ".wav") != RT_NULL)
		is_mp3 = RT_FALSE;
	else return; /* not supported audio format */

	if (is_mp3 == RT_TRUE)
	{
		/* get music tag information */
		mp3_get_info(fn, &tinfo);
		if (tinfo.title[0] == '\0')
			rt_snprintf(tinfo.title, sizeof(tinfo.title), "<未知名音乐>");
	}
	else
	{
		/* wav file */
		rt_snprintf(tinfo.title, sizeof(tinfo.title), "<未知名音乐>");
		rt_snprintf(tinfo.artist, sizeof(tinfo.title), "<wav音乐>");

		tinfo.duration = 0;
	}

	/* set player mode */
	player_mode = PLAYER_PLAY_FILE;

	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
	if (dc != RT_NULL)
	{
		rtgui_rect_t play_rect;
		rtgui_image_t *button;

		/* update tag information */
		player_update_tag_info(dc);

		/* reset progress bar */
		saved = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view));
		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(82, 199, 16);
		rtgui_dc_draw_hline(dc, 14, 226, 75);
		RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = saved;

		/* update play button */
		button = rtgui_image_create_from_mem("hdc",
            play_hdh, sizeof(play_hdh), RT_FALSE);
        play_rect.x1 = 32; play_rect.y1 = 92;
        play_rect.x2 = 61; play_rect.y2 = 114;
        rtgui_image_blit(button, dc, &play_rect);
        rtgui_image_destroy(button);

		rtgui_dc_end_drawing(dc);
	}

    rtgui_view_show(home_view, RT_FALSE);

    player_play_req(fn);
}

void player_play_url(const char* url)
{
	struct rtgui_dc* dc;

    /* set music tag information */
	strncpy(tinfo.title, "网络电台", 40);
	player_set_buffer_status(RT_TRUE);
	tinfo.duration = 320 * 1024; /* 320 k */

	/* set player mode */
	player_mode = PLAYER_PLAY_RADIO;

	dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
	if (dc != RT_NULL)
	{
		rtgui_rect_t play_rect;
		rtgui_image_t *button;

		/* update tag information */
		player_update_tag_info(dc);

		/* update play button */
		button = rtgui_image_create_from_mem("hdc",
            play_hdh, sizeof(play_hdh), RT_FALSE);
        play_rect.x1 = 32; play_rect.y1 = 92;
        play_rect.x2 = 61; play_rect.y2 = 114;
        rtgui_image_blit(button, dc, &play_rect);
        rtgui_image_destroy(button);

		rtgui_dc_end_drawing(dc);
	}

    rtgui_view_show(home_view, RT_FALSE);

    player_play_req(url);
}

517 518 519 520 521 522 523
static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
{
	if (event->type == RTGUI_EVENT_PAINT)
	{
		struct rtgui_dc* dc;
		struct rtgui_rect rect;
		rtgui_color_t saved;
524
		rtgui_image_t *background;
525 526 527 528 529 530 531

		dc = rtgui_dc_begin_drawing(widget);
		if (dc == RT_NULL) return RT_FALSE;
		rtgui_widget_get_rect(widget, &rect);
		saved = RTGUI_WIDGET_FOREGROUND(widget);

		/* draw background */
532
	    background = rtgui_image_create_from_file("hdc", "/resource/bg.hdc", RT_FALSE);
533 534 535 536 537 538 539 540 541 542 543
		if (background != RT_NULL)
		{
            rtgui_image_t *play;
            rtgui_rect_t  play_rect;

            rtgui_image_blit(background, dc, &rect);
			rtgui_image_destroy(background);

			background = RT_NULL;

			if (player_mode == PLAYER_STOP)
544
				play = rtgui_image_create_from_mem("hdc", stop_hdh, sizeof(stop_hdh), RT_FALSE);
545
			else
546
				play = rtgui_image_create_from_mem("hdc", play_hdh, sizeof(play_hdh), RT_FALSE);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
            play_rect.x1 = 32; play_rect.y1 = 92;
            play_rect.x2 = 61; play_rect.y2 = 114;
            rtgui_image_blit(play, dc, &play_rect);
            rtgui_image_destroy(play);
		}
		else
		{
			rtgui_dc_fill_rect(dc, &rect);
		}

		/* draw playing information */
		rtgui_dc_set_color(dc, black);
        {
            char line[32];

            rect.x1 = 28; rect.y1 = 12;
            rect.x2 = 220; rect.y2 = rect.y1 + 16;
			if (player_mode == PLAYER_STOP)
			{
566
				rt_snprintf(line, sizeof(line), "网络收音机");
567 568 569 570 571 572
			    rtgui_dc_draw_text(dc, line, &rect);
			}
			else
			    rtgui_dc_draw_text(dc, tinfo.title, &rect);

            rect.x1 = 28; rect.y1 = 39;
573
            rect.x2 = 170; rect.y2 = 59;
574 575
			if (player_mode == PLAYER_STOP)
			{
576 577
				rect.x2 = 220;
				rt_snprintf(line, sizeof(line), "radio.rt-thread.org");
578 579 580 581 582
			    rtgui_dc_draw_text(dc, line, &rect);
			}
			else
			    rtgui_dc_draw_text(dc, tinfo.artist, &rect);

B
bernard.xiong 已提交
583
			if ((tinfo.duration != 0) && (player_mode == PLAYER_PLAY_FILE))
584
			{
585 586 587 588
	            rt_uint32_t t = player_mode == PLAYER_STOP ? tinfo.duration : play_time;

	            rect.x1 = 172; rect.x2 = 220;
				rt_snprintf(line, sizeof(line), "%3d:%02d", t / 60, t % 60);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
	            rtgui_dc_draw_text(dc, line, &rect);
			}
        }

		RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
		rtgui_dc_draw_hline(dc, 14, 226, 75);

		RTGUI_WIDGET_FOREGROUND(widget) = saved;

		if (player_mode == PLAYER_PLAY_FILE)
		{
			char line[32];
			rt_uint32_t index;
			struct play_item* item;

			rect.x1 = 20; rect.y1 = 150;
605
			rect.x2 = 170; rect.y2 = 168;
606 607 608 609 610
			for (index = 0; index < play_list_items() && index < 8; index ++)
			{
				item = play_list_item(index);
				rtgui_dc_draw_text(dc, item->title, &rect);

611 612
	            rect.x1 = 172; rect.x2 = 220;
				rt_snprintf(line, sizeof(line), "%3d:%02d", item->duration / 60, item->duration % 60);
613 614 615
	            rtgui_dc_draw_text(dc, line, &rect);

				/* move to next item */
616
				rect.x1 = 20; rect.x2 = 170;
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
				rect.y1 += 18; rect.y2 += 18;
			}
		}
		rtgui_dc_end_drawing(dc);

		return RT_FALSE;
	}
	else if (event->type == RTGUI_EVENT_KBD)
	{
		struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
		if (ekbd->type == RTGUI_KEYDOWN)
		{
			switch (ekbd->key)
			{
			case RTGUIK_RIGHT:
                if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
                {
634
					player_stop_req();
635 636 637
					next_step = PLAYER_STEP_NEXT;
                }
                break;
638

639 640 641
			case RTGUIK_LEFT:
                if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
                {
642
					player_stop_req();
643 644 645 646 647 648 649
					next_step = PLAYER_STEP_PREV;
                }
				break;

			case RTGUIK_RETURN:
				if (player_is_playing() == RT_TRUE)
				{
650
					player_stop_req();
651 652 653 654 655 656 657
					next_step = PLAYER_STEP_STOP;
				}
				else
				{
					if ((player_mode == PLAYER_STOP) && (play_list_items() > 0))
					{
						next_step = PLAYER_STEP_NEXT;
B
bernard.xiong 已提交
658
						player_play_file(play_list_current_item());
659 660 661 662 663
					}
				}
				break;

			case RTGUIK_DOWN:
664
                rtgui_view_show(RTGUI_VIEW(function_view), RT_FALSE);
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
				break;
			}
		}
		return RT_FALSE;
	}
	else if (event->type == RTGUI_EVENT_COMMAND)
	{
		struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;

		switch (ecmd->command_id)
		{
		case PLAYER_REQUEST_PLAY_SINGLE_FILE:
		case PLAYER_REQUEST_PLAY_LIST:
			rtgui_timer_start(info_timer);
			break;
680

681 682 683 684 685 686 687
		case PLAYER_REQUEST_STOP:
		{
			rtgui_timer_stop(info_timer);

			switch (next_step)
			{
			case PLAYER_STEP_STOP:
688 689 690 691 692
// #define TEST_MODE
#ifdef TEST_MODE
				player_play_file(play_list_start());
				next_step = PLAYER_STEP_STOP;
#else
693 694 695 696 697 698 699 700 701 702
			{
				struct rtgui_dc* dc;
				rtgui_color_t saved;
	            rtgui_image_t *button;
	            rtgui_rect_t  play_rect;

				player_mode = PLAYER_STOP;

				dc = rtgui_dc_begin_drawing(widget);
				if (dc == RT_NULL) return RT_FALSE;
703

704 705
				player_update_tag_info(dc);

706 707
				saved = RTGUI_WIDGET_FOREGROUND(widget);

708 709
				RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
				rtgui_dc_draw_hline(dc, 14, 226, 75);
710

711
				/* update play button */
712
				button = rtgui_image_create_from_mem("hdc", stop_hdh, sizeof(stop_hdh), RT_FALSE);
713 714 715 716
	            play_rect.x1 = 32; play_rect.y1 = 92;
	            play_rect.x2 = 61; play_rect.y2 = 114;
	            rtgui_image_blit(button, dc, &play_rect);
	            rtgui_image_destroy(button);
717

718 719 720
				RTGUI_WIDGET_FOREGROUND(widget) = saved;
				rtgui_dc_end_drawing(dc);
			}
721
#endif
722
				break;
723

724 725 726 727 728 729 730
			case PLAYER_STEP_NEXT:
				if (play_list_is_end() == RT_TRUE)
				{
					struct rtgui_dc* dc;
					rtgui_color_t saved;
		            rtgui_image_t *button;
		            rtgui_rect_t  play_rect;
731

732 733 734 735 736 737 738 739 740 741
					/* set stat */
					next_step = PLAYER_STEP_STOP;
					player_mode = PLAYER_STOP;

					/* update UI */
					dc = rtgui_dc_begin_drawing(widget);
					if (dc == RT_NULL) return RT_FALSE;

					player_update_tag_info(dc);

742 743
					saved = RTGUI_WIDGET_FOREGROUND(widget);

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
					RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
					rtgui_dc_draw_hline(dc, 14, 226, 75);

					/* update play button */
					button = rtgui_image_create_from_mem("hdc",
		                stop_hdh, sizeof(stop_hdh), RT_FALSE);
		            play_rect.x1 = 32; play_rect.y1 = 92;
		            play_rect.x2 = 61; play_rect.y2 = 114;
		            rtgui_image_blit(button, dc, &play_rect);
		            rtgui_image_destroy(button);

					RTGUI_WIDGET_FOREGROUND(widget) = saved;
					rtgui_dc_end_drawing(dc);
				}
				else
				{
B
bernard.xiong 已提交
760
	                player_play_file(play_list_next());
761 762 763 764 765
					next_step = PLAYER_STEP_NEXT;
				}
				break;

			case PLAYER_STEP_PREV:
B
bernard.xiong 已提交
766
				player_play_file(play_list_prev());
767 768 769 770 771 772
				next_step = PLAYER_STEP_NEXT;
				break;
			};
		}
			break;

773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
		case PLAYER_REQUEST_FREEZE:
		{
			/* stop play */
			if (player_is_playing() == RT_TRUE)
			{
				player_stop_req();
				next_step = PLAYER_STEP_STOP;
			}

			/* delay some tick */
			rt_thread_delay(50);

			/* show a modal view */
			{
				rtgui_view_t *view;
				rtgui_label_t *label;
				rtgui_rect_t rect = {0, 0, 150, 150}, container_rect;
wuyangyong's avatar
wuyangyong 已提交
790

791 792 793 794 795
				rtgui_graphic_driver_get_default_rect(&container_rect);
				/* set centre */
				rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
				view = rtgui_view_create("USB");
				rtgui_workbench_add_view(workbench, view);
wuyangyong's avatar
wuyangyong 已提交
796

797 798
				/* set container to window rect */
				container_rect = rect;
wuyangyong's avatar
wuyangyong 已提交
799

800 801 802 803 804 805
				rect.x1 = 0; rect.y1 = 0;
				rect.x2 = 120; rect.y2 = 20;
				label = rtgui_label_create("USB 联机中...");
				rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
				rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
				rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label));
wuyangyong's avatar
wuyangyong 已提交
806

807 808 809 810
				rtgui_view_show(view, RT_TRUE);
				/* never reach hear */
			}
		}
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
		default:
			break;
		}

		return RT_FALSE;
	}

	return rtgui_view_event_handler(widget, event);
}

rt_bool_t player_workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
{
    if (event->type == RTGUI_EVENT_KBD)
    {
        struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
wuyangyong's avatar
wuyangyong 已提交
826
        if (((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
827
			&& !RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
        {
            /* active home view */
            if (workbench->current_view != home_view)
            {
                rtgui_view_show(home_view, RT_FALSE);
                return RT_FALSE;
            }
        }
    }

    return rtgui_workbench_event_handler(widget, event);
}

static void player_entry(void* parameter)
{
	rt_mq_t mq;
	rtgui_rect_t rect;

	mq = rt_mq_create("ply_ui", 256, 4, RT_IPC_FLAG_FIFO);
	rtgui_thread_register(rt_thread_self(), mq);

	/* create information timer */
850 851 852
	info_timer = rtgui_timer_create(RT_TICK_PER_SECOND, RT_TIMER_FLAG_PERIODIC, info_timer_timeout, RT_NULL);

	time_bg_image = rtgui_image_create_from_mem("xpm", (rt_uint8_t *) time_bg_xpm, sizeof(time_bg_xpm), RT_TRUE);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

	workbench = rtgui_workbench_create("main", "workbench");
	if (workbench == RT_NULL) return;
	rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), player_workbench_event_handler);

    /* add home view */
	home_view = rtgui_view_create("Home");
	rtgui_widget_set_event_handler(RTGUI_WIDGET(home_view), home_view_event_handler);

	rtgui_workbench_add_view(workbench, home_view);
	/* this view can be focused */
	RTGUI_WIDGET(home_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
	/* set widget focus */
	rtgui_widget_focus(RTGUI_WIDGET(home_view));

	rtgui_view_show(home_view, RT_FALSE);

    /* add function view */
	rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
872 873
	function_view = rtgui_list_view_create(function_list,
		sizeof(function_list)/sizeof(struct rtgui_list_item),
874
		&rect);
875
	rtgui_workbench_add_view(workbench, RTGUI_VIEW(function_view));
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

	rtgui_workbench_event_loop(workbench);

	rtgui_thread_deregister(rt_thread_self());
	rt_mq_delete(mq);
}

void player_notify_play(void)
{
    struct rtgui_event_command ecmd;
    RTGUI_EVENT_COMMAND_INIT(&ecmd);
    ecmd.type = RTGUI_CMD_USER_INT;
    ecmd.command_id = PLAYER_REQUEST_PLAY_SINGLE_FILE;

    rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
}

void player_notify_stop()
{
    struct rtgui_event_command ecmd;
    RTGUI_EVENT_COMMAND_INIT(&ecmd);
    ecmd.type = RTGUI_CMD_USER_INT;
    ecmd.command_id = PLAYER_REQUEST_STOP;

    rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
}

void player_ui_init()
{
	player_ui_tid = rt_thread_create("ply_ui", player_entry, RT_NULL,
906
		0x800, 25, 5);
907 908 909
	if (player_ui_tid != RT_NULL)
		rt_thread_startup(player_ui_tid);
}
910

911 912 913 914 915 916 917 918 919 920 921 922 923 924
void player_ui_freeze()
{
    struct rtgui_event_command ecmd;

	/* check whether UI starts. */
	if (home_view == RT_NULL || workbench == RT_NULL) return;

    RTGUI_EVENT_COMMAND_INIT(&ecmd);
    ecmd.type = RTGUI_CMD_USER_INT;
    ecmd.command_id = PLAYER_REQUEST_FREEZE;

    rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
}