player_ui.c 24 KB

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