player_ui.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. #include <rtgui/rtgui.h>
  2. #include <rtgui/image.h>
  3. #include <rtgui/rtgui_system.h>
  4. #include <rtgui/widgets/view.h>
  5. #include <rtgui/widgets/workbench.h>
  6. #include <string.h>
  7. #include <dfs_posix.h>
  8. #include "player_ui.h"
  9. #include "player_bg.h"
  10. #include "play_list.h"
  11. #include "listview.h"
  12. #include "filelist.h"
  13. #include "play.hdh"
  14. #include "stop.hdh"
  15. static rtgui_image_t *background = RT_NULL;
  16. static struct rtgui_view* function_view;
  17. static struct rtgui_view* home_view;
  18. static struct rtgui_workbench* workbench;
  19. static rtgui_timer_t* info_timer;
  20. static rt_thread_t player_ui_tid = RT_NULL;
  21. static enum PLAYER_MODE player_mode = PLAYER_STOP;
  22. static enum PLAYER_STEP next_step = PLAYER_STEP_STOP;
  23. static struct tag_info tinfo;
  24. void player_set_position(rt_uint32_t position)
  25. {
  26. if (player_mode != PLAYER_PLAY_RADIO)
  27. {
  28. tinfo.position = position / (tinfo.bit_rate / 8);
  29. }
  30. else
  31. {
  32. tinfo.position = position;
  33. }
  34. }
  35. void player_set_title(const char* title)
  36. {
  37. strncpy(tinfo.title, title, 40);
  38. }
  39. void player_set_buffer_status(rt_bool_t buffering)
  40. {
  41. if (buffering == RT_TRUE)
  42. strncpy(tinfo.artist, "缓冲中...", 40);
  43. else
  44. strncpy(tinfo.artist, "播放中...", 40);
  45. }
  46. void info_timer_timeout(rtgui_timer_t* timer, void* parameter)
  47. {
  48. struct rtgui_dc* dc;
  49. rtgui_color_t saved;
  50. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
  51. if (dc == RT_NULL) return ;
  52. saved = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view));
  53. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(206, 231, 255);
  54. rtgui_dc_draw_hline(dc, 14, 14 + (tinfo.position * 212)/ tinfo.duration,
  55. 75);
  56. if (player_mode == PLAYER_PLAY_RADIO)
  57. {
  58. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(82, 199, 16);
  59. rtgui_dc_draw_hline(dc, 14 + (tinfo.position * 212)/ tinfo.duration, 226, 75);
  60. }
  61. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = saved;
  62. rtgui_dc_end_drawing(dc);
  63. }
  64. static rt_uint32_t read_line(int fd, char* line, rt_uint32_t line_size)
  65. {
  66. char *pos, *next;
  67. rt_uint32_t length;
  68. length = read(fd, line, line_size);
  69. if (length > 0)
  70. {
  71. pos = strstr(line, "\r\n");
  72. if (pos == RT_NULL)
  73. {
  74. pos = strstr(line, "\n");
  75. next = pos ++;
  76. }
  77. else next = pos + 2;
  78. if (pos != RT_NULL)
  79. {
  80. *pos = '\0';
  81. /* move back */
  82. lseek(fd, -(length - (next - line)), SEEK_CUR);
  83. length = pos - line;
  84. }
  85. else length = 0;
  86. }
  87. rt_kprintf("line %s\n", line);
  88. return length;
  89. }
  90. static void player_update_tag_info(struct rtgui_dc* dc)
  91. {
  92. rtgui_rect_t rect;
  93. char line[32];
  94. rtgui_color_t saved;
  95. saved = rtgui_dc_get_color(dc);
  96. rtgui_dc_set_color(dc, black);
  97. rect.x1 = 0; rect.y1 = 0;
  98. rect.x2 = 240; rect.y2 = 65;
  99. /* draw background */
  100. background = rtgui_image_create_from_file("hdc",
  101. "/resource/bg.hdc", RT_FALSE);
  102. if (background != RT_NULL)
  103. {
  104. rtgui_image_blit(background, dc, &rect);
  105. rtgui_image_destroy(background);
  106. background = RT_NULL;
  107. }
  108. else
  109. {
  110. rtgui_dc_fill_rect(dc, &rect);
  111. }
  112. /* draw playing information */
  113. rect.x1 = 28; rect.y1 = 12;
  114. rect.x2 = 220; rect.y2 = rect.y1 + 16;
  115. if (player_mode == PLAYER_STOP)
  116. {
  117. rt_snprintf(line, sizeof(line),
  118. "网络收音机");
  119. rtgui_dc_draw_text(dc, line, &rect);
  120. }
  121. else
  122. rtgui_dc_draw_text(dc, tinfo.title, &rect);
  123. rect.x1 = 28; rect.y1 = 39;
  124. rect.x2 = 220; rect.y2 = 59;
  125. if (player_mode == PLAYER_STOP)
  126. {
  127. rt_snprintf(line, sizeof(line),
  128. "radio.rt-thread.org");
  129. rtgui_dc_draw_text(dc, line, &rect);
  130. }
  131. else
  132. rtgui_dc_draw_text(dc, tinfo.artist, &rect);
  133. if ((tinfo.duration != 0) && player_mode == PLAYER_PLAY_FILE)
  134. {
  135. rect.x1 = rect.x2 - 64;
  136. rt_snprintf(line, sizeof(line), "%02d:%02d:%02d",
  137. tinfo.duration / 3600, tinfo.duration / 60, tinfo.duration % 60);
  138. rtgui_dc_draw_text(dc, line, &rect);
  139. }
  140. rtgui_dc_set_color(dc, saved);
  141. }
  142. void player_play_file(const char* fn)
  143. {
  144. struct rtgui_dc* dc;
  145. rtgui_color_t saved;
  146. rt_bool_t is_mp3;
  147. is_mp3 = RT_FALSE;
  148. if (strstr(fn, ".mp3") != RT_NULL ||
  149. strstr(fn, ".MP3") != RT_NULL)
  150. is_mp3 = RT_TRUE;
  151. else if (strstr(fn, ".wav") != RT_NULL ||
  152. strstr(fn, ".wav") != RT_NULL)
  153. is_mp3 = RT_FALSE;
  154. else return; /* not supported audio format */
  155. if (is_mp3 == RT_TRUE)
  156. {
  157. /* get music tag information */
  158. mp3_get_info(fn, &tinfo);
  159. if (tinfo.title[0] == '\0')
  160. rt_snprintf(tinfo.title, sizeof(tinfo.title), "<未知名音乐>");
  161. }
  162. else
  163. {
  164. /* wav file */
  165. rt_snprintf(tinfo.title, sizeof(tinfo.title), "<未知名音乐>");
  166. rt_snprintf(tinfo.artist, sizeof(tinfo.title), "<wav音乐>");
  167. tinfo.duration = 0;
  168. }
  169. /* set player mode */
  170. player_mode = PLAYER_PLAY_FILE;
  171. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
  172. if (dc != RT_NULL)
  173. {
  174. rtgui_rect_t play_rect;
  175. rtgui_image_t *button;
  176. /* update tag information */
  177. player_update_tag_info(dc);
  178. /* reset progress bar */
  179. saved = RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view));
  180. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = RTGUI_RGB(82, 199, 16);
  181. rtgui_dc_draw_hline(dc, 14, 226, 75);
  182. RTGUI_WIDGET_FOREGROUND(RTGUI_WIDGET(home_view)) = saved;
  183. /* update play button */
  184. button = rtgui_image_create_from_mem("hdc",
  185. play_hdh, sizeof(play_hdh), RT_FALSE);
  186. play_rect.x1 = 32; play_rect.y1 = 92;
  187. play_rect.x2 = 61; play_rect.y2 = 114;
  188. rtgui_image_blit(button, dc, &play_rect);
  189. rtgui_image_destroy(button);
  190. rtgui_dc_end_drawing(dc);
  191. }
  192. rtgui_view_show(home_view, RT_FALSE);
  193. rt_kprintf("play file: %s\n", fn);
  194. player_play_req(fn);
  195. }
  196. void player_play_url(const char* url)
  197. {
  198. struct rtgui_dc* dc;
  199. /* set music tag information */
  200. strncpy(tinfo.title, "网络电台", 40);
  201. player_set_buffer_status(RT_TRUE);
  202. tinfo.duration = 320 * 1024; /* 320 k */
  203. /* set player mode */
  204. player_mode = PLAYER_PLAY_RADIO;
  205. dc = rtgui_dc_begin_drawing(RTGUI_WIDGET(home_view));
  206. if (dc != RT_NULL)
  207. {
  208. rtgui_rect_t play_rect;
  209. rtgui_image_t *button;
  210. /* update tag information */
  211. player_update_tag_info(dc);
  212. /* update play button */
  213. button = rtgui_image_create_from_mem("hdc",
  214. play_hdh, sizeof(play_hdh), RT_FALSE);
  215. play_rect.x1 = 32; play_rect.y1 = 92;
  216. play_rect.x2 = 61; play_rect.y2 = 114;
  217. rtgui_image_blit(button, dc, &play_rect);
  218. rtgui_image_destroy(button);
  219. rtgui_dc_end_drawing(dc);
  220. }
  221. rtgui_view_show(home_view, RT_FALSE);
  222. rt_kprintf("play radio url: %s\n", url);
  223. player_play_req(url);
  224. }
  225. void function_play_radio(void* parameter)
  226. {
  227. next_step = PLAYER_STEP_STOP;
  228. player_play_url("http://syragon.com:8000/ices");
  229. // player_play_url("http://192.168.1.6:8000/stream");
  230. // player_play_url("http://radio.aozima.com:8000/stream");
  231. }
  232. void function_filelist(void* parameter)
  233. {
  234. rtgui_rect_t rect;
  235. filelist_view_t *view;
  236. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  237. view = filelist_view_create(workbench, "/", "*.*", &rect);
  238. if (view != RT_NULL)
  239. {
  240. if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
  241. {
  242. char fn[64];
  243. /* get open file */
  244. rt_snprintf(fn, 64, "%s/%s", view->current_directory,
  245. view->items[view->current_item].name);
  246. if (strstr(view->items[view->current_item].name , ".mp3") != RT_NULL ||
  247. strstr(view->items[view->current_item].name , ".MP3") != RT_NULL ||
  248. strstr(view->items[view->current_item].name , ".wav") != RT_NULL ||
  249. strstr(view->items[view->current_item].name , ".WAV") != RT_NULL)
  250. {
  251. /* clear old play list */
  252. play_list_clear();
  253. play_list_append(fn);
  254. player_mode = PLAYER_PLAY_FILE;
  255. next_step = PLAYER_STEP_STOP;
  256. player_play_file(play_list_start());
  257. }
  258. else if (strstr(view->items[view->current_item].name , ".m3u") != RT_NULL ||
  259. strstr(view->items[view->current_item].name , ".M3U") != RT_NULL)
  260. {
  261. /* read all of music filename to a list */
  262. int fd;
  263. char line[64];
  264. fd = open(fn, O_RDONLY, 0);
  265. if (fd >= 0)
  266. {
  267. rt_uint32_t length;
  268. length = read_line(fd, line, sizeof(line));
  269. /* clear old play list */
  270. play_list_clear();
  271. do
  272. {
  273. length = read_line(fd, line, sizeof(line));
  274. if (length > 0)
  275. {
  276. if (line[0] != '/')
  277. {
  278. rt_snprintf(fn, sizeof(fn),
  279. "%s/%s", view->current_directory, line);
  280. play_list_append(fn);
  281. }
  282. else play_list_append(line);
  283. }
  284. } while (length > 0);
  285. close(fd);
  286. if (play_list_items() > 0)
  287. {
  288. player_mode = PLAYER_PLAY_FILE;
  289. next_step = PLAYER_STEP_NEXT;
  290. player_play_file(play_list_start());
  291. }
  292. }
  293. }
  294. }
  295. /* destroy view */
  296. filelist_view_destroy(view);
  297. }
  298. return;
  299. }
  300. void function_device(void* parameter)
  301. {
  302. rtgui_view_t *view;
  303. extern rtgui_view_t* device_view_create(rtgui_workbench_t* workbench);
  304. view = device_view_create(workbench);
  305. if (view != RT_NULL)
  306. {
  307. rtgui_view_show(view, RT_FALSE);
  308. }
  309. return;
  310. }
  311. void function_player(void* parameter)
  312. {
  313. rtgui_view_show(home_view, RT_FALSE);
  314. return;
  315. }
  316. #include "picture.h"
  317. void function_show_picure(void* parameter)
  318. {
  319. rtgui_view_t *view;
  320. view = picture_view_create(workbench);
  321. if (view != RT_NULL)
  322. {
  323. rtgui_view_show(view, RT_TRUE);
  324. rtgui_view_destroy(view);
  325. }
  326. return;
  327. }
  328. void function_action(void* parameter)
  329. {
  330. rt_kprintf("item action!\n");
  331. return;
  332. }
  333. extern void USB_cable(void);
  334. void function_cable(void)
  335. {
  336. USB_cable();
  337. }
  338. struct list_item function_list[] =
  339. {
  340. {"选择电台", RT_NULL, function_play_radio, RT_NULL},
  341. {"更新电台", RT_NULL, function_action, RT_NULL},
  342. {"播放文件", RT_NULL, function_filelist, RT_NULL},
  343. {"浏览图片", RT_NULL, function_show_picure, RT_NULL},
  344. {"设备信息", RT_NULL, function_device, RT_NULL},
  345. {"选项设置", RT_NULL, function_action, RT_NULL},
  346. {"USB 联机", RT_NULL, function_cable, RT_NULL},
  347. {"返回播放器", RT_NULL, function_player, RT_NULL},
  348. };
  349. static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  350. {
  351. if (event->type == RTGUI_EVENT_PAINT)
  352. {
  353. struct rtgui_dc* dc;
  354. struct rtgui_rect rect;
  355. rtgui_color_t saved;
  356. dc = rtgui_dc_begin_drawing(widget);
  357. if (dc == RT_NULL) return RT_FALSE;
  358. rtgui_widget_get_rect(widget, &rect);
  359. saved = RTGUI_WIDGET_FOREGROUND(widget);
  360. /* draw background */
  361. background = rtgui_image_create_from_file("hdc",
  362. "/resource/bg.hdc", RT_FALSE);
  363. if (background != RT_NULL)
  364. {
  365. rtgui_image_t *play;
  366. rtgui_rect_t play_rect;
  367. rtgui_image_blit(background, dc, &rect);
  368. rtgui_image_destroy(background);
  369. background = RT_NULL;
  370. if (player_mode == PLAYER_STOP)
  371. play = rtgui_image_create_from_mem("hdc",
  372. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  373. else
  374. play = rtgui_image_create_from_mem("hdc",
  375. play_hdh, sizeof(play_hdh), RT_FALSE);
  376. play_rect.x1 = 32; play_rect.y1 = 92;
  377. play_rect.x2 = 61; play_rect.y2 = 114;
  378. rtgui_image_blit(play, dc, &play_rect);
  379. rtgui_image_destroy(play);
  380. }
  381. else
  382. {
  383. rtgui_dc_fill_rect(dc, &rect);
  384. }
  385. /* draw playing information */
  386. rtgui_dc_set_color(dc, black);
  387. {
  388. char line[32];
  389. rect.x1 = 28; rect.y1 = 12;
  390. rect.x2 = 220; rect.y2 = rect.y1 + 16;
  391. if (player_mode == PLAYER_STOP)
  392. {
  393. rt_snprintf(line, sizeof(line),
  394. "网络收音机");
  395. rtgui_dc_draw_text(dc, line, &rect);
  396. }
  397. else
  398. rtgui_dc_draw_text(dc, tinfo.title, &rect);
  399. rect.x1 = 28; rect.y1 = 39;
  400. rect.x2 = 220; rect.y2 = 59;
  401. if (player_mode == PLAYER_STOP)
  402. {
  403. rt_snprintf(line, sizeof(line),
  404. "radio.rt-thread.org");
  405. rtgui_dc_draw_text(dc, line, &rect);
  406. }
  407. else
  408. rtgui_dc_draw_text(dc, tinfo.artist, &rect);
  409. if ((tinfo.duration != 0) && (player_mode == PLAYER_PLAY_FILE))
  410. {
  411. rect.x1 = rect.x2 - 64;
  412. rt_snprintf(line, sizeof(line), "%02d:%02d:%02d",
  413. tinfo.duration / 3600, tinfo.duration / 60, tinfo.duration % 60);
  414. rtgui_dc_draw_text(dc, line, &rect);
  415. }
  416. }
  417. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  418. rtgui_dc_draw_hline(dc, 14, 226, 75);
  419. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  420. if (player_mode == PLAYER_PLAY_FILE)
  421. {
  422. char line[32];
  423. rt_uint32_t index;
  424. struct play_item* item;
  425. rect.x1 = 20; rect.y1 = 150;
  426. rect.x2 = 220; rect.y2 = 168;
  427. for (index = 0; index < play_list_items() && index < 8; index ++)
  428. {
  429. item = play_list_item(index);
  430. rtgui_dc_draw_text(dc, item->title, &rect);
  431. rect.x1 = rect.x2 - 64;
  432. rt_snprintf(line, sizeof(line), "%02d:%02d:%02d",
  433. item->duration / 3600,
  434. item->duration / 60,
  435. item->duration % 60);
  436. rtgui_dc_draw_text(dc, line, &rect);
  437. /* move to next item */
  438. rect.x1 = 20;
  439. rect.y1 += 18; rect.y2 += 18;
  440. }
  441. }
  442. rtgui_dc_end_drawing(dc);
  443. return RT_FALSE;
  444. }
  445. else if (event->type == RTGUI_EVENT_KBD)
  446. {
  447. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  448. if (ekbd->type == RTGUI_KEYDOWN)
  449. {
  450. switch (ekbd->key)
  451. {
  452. case RTGUIK_RIGHT:
  453. if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
  454. {
  455. player_stop();
  456. next_step = PLAYER_STEP_NEXT;
  457. }
  458. break;
  459. case RTGUIK_LEFT:
  460. if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
  461. {
  462. player_stop();
  463. next_step = PLAYER_STEP_PREV;
  464. }
  465. break;
  466. case RTGUIK_RETURN:
  467. if (player_is_playing() == RT_TRUE)
  468. {
  469. player_stop();
  470. next_step = PLAYER_STEP_STOP;
  471. }
  472. else
  473. {
  474. if ((player_mode == PLAYER_STOP) && (play_list_items() > 0))
  475. {
  476. next_step = PLAYER_STEP_NEXT;
  477. player_play_file(play_list_current_item());
  478. }
  479. }
  480. break;
  481. case RTGUIK_DOWN:
  482. rtgui_view_show(function_view, RT_FALSE);
  483. break;
  484. }
  485. }
  486. return RT_FALSE;
  487. }
  488. else if (event->type == RTGUI_EVENT_COMMAND)
  489. {
  490. struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;
  491. switch (ecmd->command_id)
  492. {
  493. case PLAYER_REQUEST_PLAY_SINGLE_FILE:
  494. case PLAYER_REQUEST_PLAY_LIST:
  495. rtgui_timer_start(info_timer);
  496. break;
  497. case PLAYER_REQUEST_STOP:
  498. {
  499. rtgui_timer_stop(info_timer);
  500. switch (next_step)
  501. {
  502. case PLAYER_STEP_STOP:
  503. {
  504. struct rtgui_dc* dc;
  505. rtgui_color_t saved;
  506. rtgui_image_t *button;
  507. rtgui_rect_t play_rect;
  508. player_mode = PLAYER_STOP;
  509. dc = rtgui_dc_begin_drawing(widget);
  510. if (dc == RT_NULL) return RT_FALSE;
  511. player_update_tag_info(dc);
  512. saved = RTGUI_WIDGET_FOREGROUND(widget);
  513. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  514. rtgui_dc_draw_hline(dc, 14, 226, 75);
  515. /* update play button */
  516. button = rtgui_image_create_from_mem("hdc",
  517. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  518. play_rect.x1 = 32; play_rect.y1 = 92;
  519. play_rect.x2 = 61; play_rect.y2 = 114;
  520. rtgui_image_blit(button, dc, &play_rect);
  521. rtgui_image_destroy(button);
  522. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  523. rtgui_dc_end_drawing(dc);
  524. }
  525. break;
  526. case PLAYER_STEP_NEXT:
  527. if (play_list_is_end() == RT_TRUE)
  528. {
  529. struct rtgui_dc* dc;
  530. rtgui_color_t saved;
  531. rtgui_image_t *button;
  532. rtgui_rect_t play_rect;
  533. /* set stat */
  534. next_step = PLAYER_STEP_STOP;
  535. player_mode = PLAYER_STOP;
  536. /* update UI */
  537. dc = rtgui_dc_begin_drawing(widget);
  538. if (dc == RT_NULL) return RT_FALSE;
  539. player_update_tag_info(dc);
  540. saved = RTGUI_WIDGET_FOREGROUND(widget);
  541. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  542. rtgui_dc_draw_hline(dc, 14, 226, 75);
  543. /* update play button */
  544. button = rtgui_image_create_from_mem("hdc",
  545. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  546. play_rect.x1 = 32; play_rect.y1 = 92;
  547. play_rect.x2 = 61; play_rect.y2 = 114;
  548. rtgui_image_blit(button, dc, &play_rect);
  549. rtgui_image_destroy(button);
  550. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  551. rtgui_dc_end_drawing(dc);
  552. }
  553. else
  554. {
  555. player_play_file(play_list_next());
  556. next_step = PLAYER_STEP_NEXT;
  557. }
  558. break;
  559. case PLAYER_STEP_PREV:
  560. player_play_file(play_list_prev());
  561. next_step = PLAYER_STEP_NEXT;
  562. break;
  563. };
  564. }
  565. break;
  566. default:
  567. break;
  568. }
  569. return RT_FALSE;
  570. }
  571. return rtgui_view_event_handler(widget, event);
  572. }
  573. rt_bool_t player_workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
  574. {
  575. if (event->type == RTGUI_EVENT_KBD)
  576. {
  577. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  578. if ((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
  579. {
  580. /* active home view */
  581. if (workbench->current_view != home_view)
  582. {
  583. rtgui_view_show(home_view, RT_FALSE);
  584. return RT_FALSE;
  585. }
  586. }
  587. }
  588. return rtgui_workbench_event_handler(widget, event);
  589. }
  590. static void player_entry(void* parameter)
  591. {
  592. rt_mq_t mq;
  593. rtgui_rect_t rect;
  594. mq = rt_mq_create("ply_ui", 256, 4, RT_IPC_FLAG_FIFO);
  595. rtgui_thread_register(rt_thread_self(), mq);
  596. /* create information timer */
  597. info_timer = rtgui_timer_create(RT_TICK_PER_SECOND,
  598. RT_TIMER_FLAG_PERIODIC,
  599. info_timer_timeout, RT_NULL);
  600. workbench = rtgui_workbench_create("main", "workbench");
  601. if (workbench == RT_NULL) return;
  602. rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), player_workbench_event_handler);
  603. /* add home view */
  604. home_view = rtgui_view_create("Home");
  605. rtgui_widget_set_event_handler(RTGUI_WIDGET(home_view), home_view_event_handler);
  606. rtgui_workbench_add_view(workbench, home_view);
  607. /* this view can be focused */
  608. RTGUI_WIDGET(home_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  609. /* set widget focus */
  610. rtgui_widget_focus(RTGUI_WIDGET(home_view));
  611. rtgui_view_show(home_view, RT_FALSE);
  612. /* add function view */
  613. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  614. function_view = (struct rtgui_view*)list_view_create(function_list,
  615. sizeof(function_list)/sizeof(struct list_item),
  616. &rect);
  617. rtgui_workbench_add_view(workbench, function_view);
  618. rtgui_workbench_event_loop(workbench);
  619. rtgui_thread_deregister(rt_thread_self());
  620. rt_mq_delete(mq);
  621. }
  622. void player_notify_play(void)
  623. {
  624. struct rtgui_event_command ecmd;
  625. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  626. ecmd.type = RTGUI_CMD_USER_INT;
  627. ecmd.command_id = PLAYER_REQUEST_PLAY_SINGLE_FILE;
  628. rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
  629. }
  630. void player_notify_stop()
  631. {
  632. struct rtgui_event_command ecmd;
  633. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  634. ecmd.type = RTGUI_CMD_USER_INT;
  635. ecmd.command_id = PLAYER_REQUEST_STOP;
  636. rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
  637. }
  638. void player_ui_init()
  639. {
  640. player_ui_tid = rt_thread_create("ply_ui", player_entry, RT_NULL,
  641. 4096, 25, 5);
  642. if (player_ui_tid != RT_NULL)
  643. rt_thread_startup(player_ui_tid);
  644. }