rtgui_app.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * File : rtgui_app.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-01-13 Grissiom first version(just a prototype of application API)
  23. * 2012-07-07 Bernard move the send/recv message to the rtgui_system.c
  24. */
  25. #include <rthw.h>
  26. #include <rtthread.h>
  27. #include <rtgui/rtgui_system.h>
  28. #include <rtgui/rtgui_app.h>
  29. #include <rtgui/widgets/window.h>
  30. #include <topwin.h>
  31. static void _rtgui_app_constructor(struct rtgui_app *app)
  32. {
  33. /* set event handler */
  34. rtgui_object_set_event_handler(RTGUI_OBJECT(app),
  35. rtgui_app_event_handler);
  36. app->name = RT_NULL;
  37. app->icon = RT_NULL;
  38. /* set EXITED so we can destroy an application that just created */
  39. app->state_flag = RTGUI_APP_FLAG_EXITED;
  40. app->ref_count = 0;
  41. app->window_cnt = 0;
  42. app->win_acti_cnt = 0;
  43. app->exit_code = 0;
  44. app->tid = RT_NULL;
  45. app->mq = RT_NULL;
  46. app->main_object = RT_NULL;
  47. app->on_idle = RT_NULL;
  48. }
  49. static void _rtgui_app_destructor(struct rtgui_app *app)
  50. {
  51. RT_ASSERT(app != RT_NULL);
  52. rt_free(app->name);
  53. app->name = RT_NULL;
  54. }
  55. DEFINE_CLASS_TYPE(application, "application",
  56. RTGUI_PARENT_TYPE(object),
  57. _rtgui_app_constructor,
  58. _rtgui_app_destructor,
  59. sizeof(struct rtgui_app));
  60. struct rtgui_app *rtgui_app_create(const char *title)
  61. {
  62. rt_thread_t tid = rt_thread_self();
  63. struct rtgui_app *app;
  64. struct rtgui_app *srv_app;
  65. struct rtgui_event_application event;
  66. char mq_name[RT_NAME_MAX];
  67. RT_ASSERT(tid != RT_NULL);
  68. RT_ASSERT(title != RT_NULL);
  69. /* create application */
  70. app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE));
  71. if (app == RT_NULL)
  72. return RT_NULL;
  73. /* one thread only can create one rtgui application */
  74. RT_ASSERT(tid->user_data == 0);
  75. app->tid = tid;
  76. rt_snprintf(mq_name, RT_NAME_MAX, "g%s", title);
  77. app->mq = rt_mq_create(mq_name,
  78. sizeof(union rtgui_event_generic), 256,
  79. RT_IPC_FLAG_FIFO);
  80. if (app->mq == RT_NULL)
  81. {
  82. rt_kprintf("create msgq failed.\n");
  83. goto __mq_err;
  84. }
  85. /* set application title */
  86. app->name = (unsigned char *)rt_strdup((char *)title);
  87. if (app->name == RT_NULL)
  88. goto __err;
  89. /* the first app should be the server */
  90. srv_app = rtgui_get_server();
  91. if (srv_app == RT_NULL)
  92. {
  93. /* set user thread */
  94. tid->user_data = (rt_uint32_t)app;
  95. return app;
  96. }
  97. RTGUI_EVENT_APP_CREATE_INIT(&event);
  98. event.app = app;
  99. /* notify rtgui server to one application has been created */
  100. if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
  101. {
  102. /* set user thread */
  103. tid->user_data = (rt_uint32_t)app;
  104. return app;
  105. }
  106. __err:
  107. __mq_err:
  108. rtgui_object_destroy(RTGUI_OBJECT(app));
  109. return RT_NULL;
  110. }
  111. RTM_EXPORT(rtgui_app_create);
  112. #define _rtgui_application_check(app) \
  113. do { \
  114. RT_ASSERT(app != RT_NULL); \
  115. RT_ASSERT(app->tid != RT_NULL); \
  116. RT_ASSERT(app->tid->user_data != 0); \
  117. RT_ASSERT(app->mq != RT_NULL); \
  118. } while (0)
  119. void rtgui_app_destroy(struct rtgui_app *app)
  120. {
  121. struct rtgui_app *srv_app;
  122. _rtgui_application_check(app);
  123. if (!(app->state_flag & RTGUI_APP_FLAG_EXITED))
  124. {
  125. rt_kprintf("cannot destroy a running application: %s.\n",
  126. app->name);
  127. return;
  128. }
  129. /* send a message to notify rtgui server */
  130. srv_app = rtgui_get_server();
  131. if (srv_app != rtgui_app_self())
  132. {
  133. struct rtgui_event_application event;
  134. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  135. event.app = app;
  136. if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) != RT_EOK)
  137. {
  138. rt_kprintf("destroy an application in server failed\n");
  139. return ;
  140. }
  141. }
  142. app->tid->user_data = 0;
  143. rt_mq_delete(app->mq);
  144. rtgui_object_destroy(RTGUI_OBJECT(app));
  145. }
  146. RTM_EXPORT(rtgui_app_destroy);
  147. struct rtgui_app *rtgui_app_self(void)
  148. {
  149. struct rtgui_app *app;
  150. rt_thread_t self;
  151. /* get current thread */
  152. self = rt_thread_self();
  153. app = (struct rtgui_app *)(self->user_data);
  154. return app;
  155. }
  156. RTM_EXPORT(rtgui_app_self);
  157. void rtgui_app_set_onidle(struct rtgui_app *app, rtgui_idle_func_t onidle)
  158. {
  159. _rtgui_application_check(app);
  160. app->on_idle = onidle;
  161. }
  162. RTM_EXPORT(rtgui_app_set_onidle);
  163. rtgui_idle_func_t rtgui_app_get_onidle(struct rtgui_app *app)
  164. {
  165. _rtgui_application_check(app);
  166. return app->on_idle;
  167. }
  168. RTM_EXPORT(rtgui_app_get_onidle);
  169. rt_inline rt_bool_t _rtgui_application_dest_handle(
  170. struct rtgui_app *app,
  171. struct rtgui_event *event)
  172. {
  173. struct rtgui_event_win *wevent = (struct rtgui_event_win *)event;
  174. struct rtgui_object *dest_object;
  175. if (wevent->wid == RT_NULL)
  176. return RT_FALSE;
  177. if (wevent->wid->magic != RTGUI_WIN_MAGIC)
  178. {
  179. return RT_FALSE;
  180. }
  181. /* this window has been closed. */
  182. if (wevent->wid != RT_NULL && wevent->wid->flag & RTGUI_WIN_FLAG_CLOSED)
  183. return RT_TRUE;
  184. /* The dest window may have been destroyed when this event arrived. Check
  185. * against this condition. NOTE: we cannot use the RTGUI_OBJECT because it
  186. * may be invalid already. */
  187. dest_object = (struct rtgui_object*)wevent->wid;
  188. if ((dest_object->flag & RTGUI_OBJECT_FLAG_VALID) !=
  189. RTGUI_OBJECT_FLAG_VALID)
  190. {
  191. return RT_TRUE;
  192. }
  193. dest_object = RTGUI_OBJECT(wevent->wid);
  194. if (dest_object != RT_NULL)
  195. {
  196. if (dest_object->event_handler != RT_NULL)
  197. return dest_object->event_handler(dest_object, event);
  198. else
  199. return RT_FALSE;
  200. }
  201. else
  202. {
  203. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  204. return RT_FALSE;
  205. }
  206. }
  207. rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  208. {
  209. struct rtgui_app *app;
  210. RT_ASSERT(object != RT_NULL);
  211. RT_ASSERT(event != RT_NULL);
  212. app = RTGUI_APP(object);
  213. switch (event->type)
  214. {
  215. case RTGUI_EVENT_KBD:
  216. {
  217. struct rtgui_event_kbd *kbd = (struct rtgui_event_kbd*)event;
  218. if (kbd->win_acti_cnt != app->win_acti_cnt)
  219. break;
  220. _rtgui_application_dest_handle(app, event);
  221. }
  222. break;
  223. case RTGUI_EVENT_MOUSE_BUTTON:
  224. case RTGUI_EVENT_MOUSE_MOTION:
  225. {
  226. struct rtgui_event_mouse *wevent = (struct rtgui_event_mouse *)event;
  227. if (wevent->win_acti_cnt != app->win_acti_cnt)
  228. break;
  229. _rtgui_application_dest_handle(app, event);
  230. }
  231. break;
  232. case RTGUI_EVENT_GESTURE:
  233. {
  234. struct rtgui_event_gesture *wevent = (struct rtgui_event_gesture *)event;
  235. if (wevent->win_acti_cnt != app->win_acti_cnt)
  236. break;
  237. _rtgui_application_dest_handle(app, event);
  238. }
  239. break;
  240. case RTGUI_EVENT_WIN_ACTIVATE:
  241. {
  242. app->win_acti_cnt++;
  243. _rtgui_application_dest_handle(app, event);
  244. }
  245. break;
  246. case RTGUI_EVENT_PAINT:
  247. case RTGUI_EVENT_VPAINT_REQ:
  248. case RTGUI_EVENT_CLIP_INFO:
  249. case RTGUI_EVENT_WIN_DEACTIVATE:
  250. case RTGUI_EVENT_WIN_CLOSE:
  251. case RTGUI_EVENT_WIN_MOVE:
  252. case RTGUI_EVENT_WIN_SHOW:
  253. case RTGUI_EVENT_WIN_HIDE:
  254. _rtgui_application_dest_handle(app, event);
  255. break;
  256. case RTGUI_EVENT_APP_ACTIVATE:
  257. if (app->main_object != RT_NULL)
  258. {
  259. /* Let the polymorphism work. */
  260. struct rtgui_event_win_show wev;
  261. RTGUI_EVENT_WIN_SHOW_INIT(&wev);
  262. wev.wid = (struct rtgui_win*)app->main_object;
  263. rtgui_object_handle(app->main_object, &wev.parent);
  264. }
  265. break;
  266. case RTGUI_EVENT_APP_DESTROY:
  267. rtgui_app_exit(app, 0);
  268. break;
  269. case RTGUI_EVENT_TIMER:
  270. {
  271. rt_base_t level;
  272. struct rtgui_timer *timer;
  273. struct rtgui_event_timer *etimer = (struct rtgui_event_timer *) event;
  274. timer = etimer->timer;
  275. level = rt_hw_interrupt_disable();
  276. timer->pending_cnt--;
  277. rt_hw_interrupt_enable(level);
  278. RT_ASSERT(timer->pending_cnt >= 0);
  279. if (timer->state == RTGUI_TIMER_ST_DESTROY_PENDING)
  280. {
  281. /* Truly destroy the timer when there is no pending event. */
  282. if (timer->pending_cnt == 0)
  283. rtgui_timer_destory(timer);
  284. }
  285. else if (timer->state == RTGUI_TIMER_ST_RUNNING && timer->timeout != RT_NULL)
  286. {
  287. /* call timeout function */
  288. timer->timeout(timer, timer->user_data);
  289. }
  290. }
  291. break;
  292. case RTGUI_EVENT_MV_MODEL:
  293. {
  294. struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event;
  295. RT_ASSERT(emodel->view);
  296. return rtgui_object_handle(RTGUI_OBJECT(emodel->view), event);
  297. }
  298. case RTGUI_EVENT_COMMAND:
  299. {
  300. struct rtgui_event_command *ecmd = (struct rtgui_event_command *)event;
  301. if (ecmd->wid != RT_NULL)
  302. return _rtgui_application_dest_handle(app, event);
  303. else
  304. {
  305. struct rtgui_topwin *wnd;
  306. wnd = rtgui_topwin_get_focus();
  307. if (wnd != RT_NULL)
  308. {
  309. RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
  310. /* send to focus window */
  311. ecmd->wid = wnd->wid;
  312. return _rtgui_application_dest_handle(app, event);
  313. }
  314. }
  315. }
  316. default:
  317. return rtgui_object_event_handler(object, event);
  318. }
  319. return RT_TRUE;
  320. }
  321. RTM_EXPORT(rtgui_app_event_handler);
  322. rt_inline void _rtgui_application_event_loop(struct rtgui_app *app)
  323. {
  324. rt_err_t result;
  325. rt_uint16_t current_ref;
  326. struct rtgui_event *event;
  327. _rtgui_application_check(app);
  328. /* point to event buffer */
  329. event = (struct rtgui_event *)app->event_buffer;
  330. current_ref = ++app->ref_count;
  331. while (current_ref <= app->ref_count)
  332. {
  333. RT_ASSERT(current_ref == app->ref_count);
  334. if (app->on_idle != RT_NULL)
  335. {
  336. result = rtgui_recv(event, sizeof(union rtgui_event_generic), 0);
  337. if (result == RT_EOK)
  338. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  339. else if (result == -RT_ETIMEOUT)
  340. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  341. }
  342. else
  343. {
  344. result = rtgui_recv(event, sizeof(union rtgui_event_generic), RT_WAITING_FOREVER);
  345. if (result == RT_EOK)
  346. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  347. }
  348. }
  349. }
  350. rt_base_t rtgui_app_run(struct rtgui_app *app)
  351. {
  352. _rtgui_application_check(app);
  353. app->state_flag &= ~RTGUI_APP_FLAG_EXITED;
  354. _rtgui_application_event_loop(app);
  355. if (app->ref_count == 0)
  356. app->state_flag |= RTGUI_APP_FLAG_EXITED;
  357. return app->exit_code;
  358. }
  359. RTM_EXPORT(rtgui_app_run);
  360. void rtgui_app_exit(struct rtgui_app *app, rt_uint16_t code)
  361. {
  362. if (app->ref_count == 0)
  363. return;
  364. --app->ref_count;
  365. app->exit_code = code;
  366. }
  367. RTM_EXPORT(rtgui_app_exit);
  368. void rtgui_app_activate(struct rtgui_app *app)
  369. {
  370. struct rtgui_event_application event;
  371. RTGUI_EVENT_APP_ACTIVATE_INIT(&event);
  372. event.app = app;
  373. rtgui_send(app, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  374. }
  375. RTM_EXPORT(rtgui_app_activate);
  376. void rtgui_app_sleep(struct rtgui_app *app, int millisecond)
  377. {
  378. rt_err_t result;
  379. rt_uint16_t current_ref;
  380. struct rtgui_event *event;
  381. rt_tick_t tick, sleep_tick;
  382. int delta_tick;
  383. tick = rt_tick_get();
  384. millisecond = rt_tick_from_millisecond(millisecond);
  385. if (millisecond == 0) return;
  386. sleep_tick = tick + millisecond;
  387. delta_tick = millisecond;
  388. /* point to event buffer */
  389. event = (struct rtgui_event *)app->event_buffer;
  390. current_ref = ++app->ref_count;
  391. while (current_ref <= app->ref_count && delta_tick)
  392. {
  393. RT_ASSERT(current_ref == app->ref_count);
  394. if (app->on_idle != RT_NULL)
  395. {
  396. result = rtgui_recv(event, sizeof(union rtgui_event_generic), 0);
  397. if (result == RT_EOK)
  398. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  399. else if (result == -RT_ETIMEOUT)
  400. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  401. }
  402. else
  403. {
  404. result = rtgui_recv(event, sizeof(union rtgui_event_generic), sleep_tick - rt_tick_get());
  405. if (result == RT_EOK)
  406. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  407. }
  408. delta_tick = sleep_tick - rt_tick_get();
  409. }
  410. app->ref_count --;
  411. }
  412. RTM_EXPORT(rtgui_app_sleep);
  413. void rtgui_app_close(struct rtgui_app *app)
  414. {
  415. struct rtgui_event_application event;
  416. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  417. event.app = app;
  418. rtgui_send(app, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  419. }
  420. RTM_EXPORT(rtgui_app_close);
  421. /**
  422. * set this application as window manager
  423. */
  424. rt_err_t rtgui_app_set_as_wm(struct rtgui_app *app)
  425. {
  426. struct rtgui_app *srv_app;
  427. struct rtgui_event_set_wm event;
  428. _rtgui_application_check(app);
  429. srv_app = rtgui_get_server();
  430. if (srv_app != RT_NULL)
  431. {
  432. /* notify rtgui server, this is a window manager */
  433. RTGUI_EVENT_SET_WM_INIT(&event);
  434. event.app = app;
  435. rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event));
  436. return RT_EOK;
  437. }
  438. return RT_ERROR;
  439. }
  440. RTM_EXPORT(rtgui_app_set_as_wm);
  441. void rtgui_app_set_main_win(struct rtgui_app *app, struct rtgui_win *win)
  442. {
  443. _rtgui_application_check(app);
  444. app->main_object = RTGUI_OBJECT(win);
  445. }
  446. RTM_EXPORT(rtgui_app_set_main_win);
  447. struct rtgui_win *rtgui_app_get_main_win(struct rtgui_app *app)
  448. {
  449. return RTGUI_WIN(app->main_object);
  450. }
  451. RTM_EXPORT(rtgui_app_get_main_win);
  452. unsigned int rtgui_app_get_win_acti_cnt(void)
  453. {
  454. struct rtgui_app *app = rtgui_topwin_app_get_focus();
  455. if (app)
  456. {
  457. return app->win_acti_cnt;
  458. }
  459. return 0;
  460. }
  461. RTM_EXPORT(rtgui_app_get_win_acti_cnt);