rtgui_app.c 14 KB

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