player_ui.c 22 KB

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