player_ui.c 20 KB

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