player_ui.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. }
  230. void function_filelist(void* parameter)
  231. {
  232. rtgui_rect_t rect;
  233. filelist_view_t *view;
  234. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  235. view = filelist_view_create(workbench, "/", "*.*", &rect);
  236. if (view != RT_NULL)
  237. {
  238. if (rtgui_view_show(RTGUI_VIEW(view), RT_TRUE) == RTGUI_MODAL_OK)
  239. {
  240. char fn[64];
  241. /* get open file */
  242. rt_snprintf(fn, 64, "%s/%s", view->current_directory,
  243. view->items[view->current_item].name);
  244. if (strstr(view->items[view->current_item].name , ".mp3") != RT_NULL ||
  245. strstr(view->items[view->current_item].name , ".MP3") != RT_NULL ||
  246. strstr(view->items[view->current_item].name , ".wav") != RT_NULL ||
  247. strstr(view->items[view->current_item].name , ".WAV") != RT_NULL)
  248. {
  249. /* clear old play list */
  250. play_list_clear();
  251. play_list_append(fn);
  252. player_mode = PLAYER_PLAY_FILE;
  253. next_step = PLAYER_STEP_STOP;
  254. player_play_file(play_list_start());
  255. }
  256. else if (strstr(view->items[view->current_item].name , ".m3u") != RT_NULL ||
  257. strstr(view->items[view->current_item].name , ".M3U") != RT_NULL)
  258. {
  259. /* read all of music filename to a list */
  260. int fd;
  261. char line[64];
  262. fd = open(fn, O_RDONLY, 0);
  263. if (fd >= 0)
  264. {
  265. rt_uint32_t length;
  266. length = read_line(fd, line, sizeof(line));
  267. /* clear old play list */
  268. play_list_clear();
  269. do
  270. {
  271. length = read_line(fd, line, sizeof(line));
  272. if (length > 0)
  273. {
  274. if (line[0] != '/')
  275. {
  276. rt_snprintf(fn, sizeof(fn),
  277. "%s/%s", view->current_directory, line);
  278. play_list_append(fn);
  279. }
  280. else play_list_append(line);
  281. }
  282. } while (length > 0);
  283. close(fd);
  284. if (play_list_items() > 0)
  285. {
  286. player_mode = PLAYER_PLAY_FILE;
  287. next_step = PLAYER_STEP_NEXT;
  288. player_play_file(play_list_start());
  289. }
  290. }
  291. }
  292. }
  293. /* destroy view */
  294. filelist_view_destroy(view);
  295. }
  296. return;
  297. }
  298. void function_device(void* parameter)
  299. {
  300. rtgui_view_t *view;
  301. extern rtgui_view_t* device_view_create(rtgui_workbench_t* workbench);
  302. view = device_view_create(workbench);
  303. if (view != RT_NULL)
  304. {
  305. rtgui_view_show(view, RT_FALSE);
  306. }
  307. return;
  308. }
  309. void function_player(void* parameter)
  310. {
  311. rtgui_view_show(home_view, RT_FALSE);
  312. return;
  313. }
  314. void function_action(void* parameter)
  315. {
  316. rt_kprintf("item action!\n");
  317. return;
  318. }
  319. struct list_item function_list[] =
  320. {
  321. {"选择电台", RT_NULL, function_play_radio, RT_NULL},
  322. {"更新电台", RT_NULL, function_action, RT_NULL},
  323. {"播放文件", RT_NULL, function_filelist, RT_NULL},
  324. {"设备信息", RT_NULL, function_device, RT_NULL},
  325. {"选项设置", RT_NULL, function_action, RT_NULL},
  326. {"返回播放器", RT_NULL, function_player, RT_NULL},
  327. };
  328. static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtgui_event* event)
  329. {
  330. if (event->type == RTGUI_EVENT_PAINT)
  331. {
  332. struct rtgui_dc* dc;
  333. struct rtgui_rect rect;
  334. rtgui_color_t saved;
  335. dc = rtgui_dc_begin_drawing(widget);
  336. if (dc == RT_NULL) return RT_FALSE;
  337. rtgui_widget_get_rect(widget, &rect);
  338. saved = RTGUI_WIDGET_FOREGROUND(widget);
  339. /* draw background */
  340. background = rtgui_image_create_from_file("hdc",
  341. "/resource/bg.hdc", RT_FALSE);
  342. if (background != RT_NULL)
  343. {
  344. rtgui_image_t *play;
  345. rtgui_rect_t play_rect;
  346. rtgui_image_blit(background, dc, &rect);
  347. rtgui_image_destroy(background);
  348. background = RT_NULL;
  349. if (player_mode == PLAYER_STOP)
  350. play = rtgui_image_create_from_mem("hdc",
  351. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  352. else
  353. play = rtgui_image_create_from_mem("hdc",
  354. play_hdh, sizeof(play_hdh), RT_FALSE);
  355. play_rect.x1 = 32; play_rect.y1 = 92;
  356. play_rect.x2 = 61; play_rect.y2 = 114;
  357. rtgui_image_blit(play, dc, &play_rect);
  358. rtgui_image_destroy(play);
  359. }
  360. else
  361. {
  362. rtgui_dc_fill_rect(dc, &rect);
  363. }
  364. /* draw playing information */
  365. rtgui_dc_set_color(dc, black);
  366. {
  367. char line[32];
  368. rect.x1 = 28; rect.y1 = 12;
  369. rect.x2 = 220; rect.y2 = rect.y1 + 16;
  370. if (player_mode == PLAYER_STOP)
  371. {
  372. rt_snprintf(line, sizeof(line),
  373. "网络收音机");
  374. rtgui_dc_draw_text(dc, line, &rect);
  375. }
  376. else
  377. rtgui_dc_draw_text(dc, tinfo.title, &rect);
  378. rect.x1 = 28; rect.y1 = 39;
  379. rect.x2 = 220; rect.y2 = 59;
  380. if (player_mode == PLAYER_STOP)
  381. {
  382. rt_snprintf(line, sizeof(line),
  383. "radio.rt-thread.org");
  384. rtgui_dc_draw_text(dc, line, &rect);
  385. }
  386. else
  387. rtgui_dc_draw_text(dc, tinfo.artist, &rect);
  388. if ((tinfo.duration != 0) && (player_mode == PLAYER_PLAY_FILE))
  389. {
  390. rect.x1 = rect.x2 - 64;
  391. rt_snprintf(line, sizeof(line), "%02d:%02d:%02d",
  392. tinfo.duration / 3600, tinfo.duration / 60, tinfo.duration % 60);
  393. rtgui_dc_draw_text(dc, line, &rect);
  394. }
  395. }
  396. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  397. rtgui_dc_draw_hline(dc, 14, 226, 75);
  398. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  399. if (player_mode == PLAYER_PLAY_FILE)
  400. {
  401. char line[32];
  402. rt_uint32_t index;
  403. struct play_item* item;
  404. rect.x1 = 20; rect.y1 = 150;
  405. rect.x2 = 220; rect.y2 = 168;
  406. for (index = 0; index < play_list_items() && index < 8; index ++)
  407. {
  408. item = play_list_item(index);
  409. rtgui_dc_draw_text(dc, item->title, &rect);
  410. rect.x1 = rect.x2 - 64;
  411. rt_snprintf(line, sizeof(line), "%02d:%02d:%02d",
  412. item->duration / 3600,
  413. item->duration / 60,
  414. item->duration % 60);
  415. rtgui_dc_draw_text(dc, line, &rect);
  416. /* move to next item */
  417. rect.x1 = 20;
  418. rect.y1 += 18; rect.y2 += 18;
  419. }
  420. }
  421. rtgui_dc_end_drawing(dc);
  422. return RT_FALSE;
  423. }
  424. else if (event->type == RTGUI_EVENT_KBD)
  425. {
  426. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  427. if (ekbd->type == RTGUI_KEYDOWN)
  428. {
  429. switch (ekbd->key)
  430. {
  431. case RTGUIK_RIGHT:
  432. if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
  433. {
  434. player_stop();
  435. next_step = PLAYER_STEP_NEXT;
  436. }
  437. break;
  438. case RTGUIK_LEFT:
  439. if (player_mode == PLAYER_PLAY_FILE && play_list_items() > 0)
  440. {
  441. player_stop();
  442. next_step = PLAYER_STEP_PREV;
  443. }
  444. break;
  445. case RTGUIK_RETURN:
  446. if (player_is_playing() == RT_TRUE)
  447. {
  448. player_stop();
  449. next_step = PLAYER_STEP_STOP;
  450. }
  451. else
  452. {
  453. if ((player_mode == PLAYER_STOP) && (play_list_items() > 0))
  454. {
  455. next_step = PLAYER_STEP_NEXT;
  456. player_play_file(play_list_current_item());
  457. }
  458. }
  459. break;
  460. case RTGUIK_DOWN:
  461. rtgui_view_show(function_view, RT_FALSE);
  462. break;
  463. }
  464. }
  465. return RT_FALSE;
  466. }
  467. else if (event->type == RTGUI_EVENT_COMMAND)
  468. {
  469. struct rtgui_event_command* ecmd = (struct rtgui_event_command*)event;
  470. switch (ecmd->command_id)
  471. {
  472. case PLAYER_REQUEST_PLAY_SINGLE_FILE:
  473. case PLAYER_REQUEST_PLAY_LIST:
  474. rtgui_timer_start(info_timer);
  475. break;
  476. case PLAYER_REQUEST_STOP:
  477. {
  478. rtgui_timer_stop(info_timer);
  479. switch (next_step)
  480. {
  481. case PLAYER_STEP_STOP:
  482. {
  483. struct rtgui_dc* dc;
  484. rtgui_color_t saved;
  485. rtgui_image_t *button;
  486. rtgui_rect_t play_rect;
  487. player_mode = PLAYER_STOP;
  488. dc = rtgui_dc_begin_drawing(widget);
  489. if (dc == RT_NULL) return RT_FALSE;
  490. player_update_tag_info(dc);
  491. saved = RTGUI_WIDGET_FOREGROUND(widget);
  492. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  493. rtgui_dc_draw_hline(dc, 14, 226, 75);
  494. /* update play button */
  495. button = rtgui_image_create_from_mem("hdc",
  496. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  497. play_rect.x1 = 32; play_rect.y1 = 92;
  498. play_rect.x2 = 61; play_rect.y2 = 114;
  499. rtgui_image_blit(button, dc, &play_rect);
  500. rtgui_image_destroy(button);
  501. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  502. rtgui_dc_end_drawing(dc);
  503. }
  504. break;
  505. case PLAYER_STEP_NEXT:
  506. if (play_list_is_end() == RT_TRUE)
  507. {
  508. struct rtgui_dc* dc;
  509. rtgui_color_t saved;
  510. rtgui_image_t *button;
  511. rtgui_rect_t play_rect;
  512. /* set stat */
  513. next_step = PLAYER_STEP_STOP;
  514. player_mode = PLAYER_STOP;
  515. /* update UI */
  516. dc = rtgui_dc_begin_drawing(widget);
  517. if (dc == RT_NULL) return RT_FALSE;
  518. player_update_tag_info(dc);
  519. saved = RTGUI_WIDGET_FOREGROUND(widget);
  520. RTGUI_WIDGET_FOREGROUND(widget) = RTGUI_RGB(82, 199, 16);
  521. rtgui_dc_draw_hline(dc, 14, 226, 75);
  522. /* update play button */
  523. button = rtgui_image_create_from_mem("hdc",
  524. stop_hdh, sizeof(stop_hdh), RT_FALSE);
  525. play_rect.x1 = 32; play_rect.y1 = 92;
  526. play_rect.x2 = 61; play_rect.y2 = 114;
  527. rtgui_image_blit(button, dc, &play_rect);
  528. rtgui_image_destroy(button);
  529. RTGUI_WIDGET_FOREGROUND(widget) = saved;
  530. rtgui_dc_end_drawing(dc);
  531. }
  532. else
  533. {
  534. player_play_file(play_list_next());
  535. next_step = PLAYER_STEP_NEXT;
  536. }
  537. break;
  538. case PLAYER_STEP_PREV:
  539. player_play_file(play_list_prev());
  540. next_step = PLAYER_STEP_NEXT;
  541. break;
  542. };
  543. }
  544. break;
  545. default:
  546. break;
  547. }
  548. return RT_FALSE;
  549. }
  550. return rtgui_view_event_handler(widget, event);
  551. }
  552. rt_bool_t player_workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *event)
  553. {
  554. if (event->type == RTGUI_EVENT_KBD)
  555. {
  556. struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
  557. if ((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
  558. {
  559. /* active home view */
  560. if (workbench->current_view != home_view)
  561. {
  562. rtgui_view_show(home_view, RT_FALSE);
  563. return RT_FALSE;
  564. }
  565. }
  566. }
  567. return rtgui_workbench_event_handler(widget, event);
  568. }
  569. static void player_entry(void* parameter)
  570. {
  571. rt_mq_t mq;
  572. rtgui_rect_t rect;
  573. mq = rt_mq_create("ply_ui", 256, 4, RT_IPC_FLAG_FIFO);
  574. rtgui_thread_register(rt_thread_self(), mq);
  575. /* create information timer */
  576. info_timer = rtgui_timer_create(RT_TICK_PER_SECOND,
  577. RT_TIMER_FLAG_PERIODIC,
  578. info_timer_timeout, RT_NULL);
  579. workbench = rtgui_workbench_create("main", "workbench");
  580. if (workbench == RT_NULL) return;
  581. rtgui_widget_set_event_handler(RTGUI_WIDGET(workbench), player_workbench_event_handler);
  582. /* add home view */
  583. home_view = rtgui_view_create("Home");
  584. rtgui_widget_set_event_handler(RTGUI_WIDGET(home_view), home_view_event_handler);
  585. rtgui_workbench_add_view(workbench, home_view);
  586. /* this view can be focused */
  587. RTGUI_WIDGET(home_view)->flag |= RTGUI_WIDGET_FLAG_FOCUSABLE;
  588. /* set widget focus */
  589. rtgui_widget_focus(RTGUI_WIDGET(home_view));
  590. rtgui_view_show(home_view, RT_FALSE);
  591. /* add function view */
  592. rtgui_widget_get_rect(RTGUI_WIDGET(workbench), &rect);
  593. function_view = (struct rtgui_view*)list_view_create(function_list,
  594. sizeof(function_list)/sizeof(struct list_item),
  595. &rect);
  596. rtgui_workbench_add_view(workbench, function_view);
  597. rtgui_workbench_event_loop(workbench);
  598. rtgui_thread_deregister(rt_thread_self());
  599. rt_mq_delete(mq);
  600. }
  601. void player_notify_play(void)
  602. {
  603. struct rtgui_event_command ecmd;
  604. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  605. ecmd.type = RTGUI_CMD_USER_INT;
  606. ecmd.command_id = PLAYER_REQUEST_PLAY_SINGLE_FILE;
  607. rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
  608. }
  609. void player_notify_stop()
  610. {
  611. struct rtgui_event_command ecmd;
  612. RTGUI_EVENT_COMMAND_INIT(&ecmd);
  613. ecmd.type = RTGUI_CMD_USER_INT;
  614. ecmd.command_id = PLAYER_REQUEST_STOP;
  615. rtgui_thread_send(player_ui_tid, &ecmd.parent, sizeof(ecmd));
  616. }
  617. void player_ui_init()
  618. {
  619. player_ui_tid = rt_thread_create("ply_ui", player_entry, RT_NULL,
  620. 4096, 25, 5);
  621. if (player_ui_tid != RT_NULL)
  622. rt_thread_startup(player_ui_tid);
  623. }