rtgui_app.c 13 KB

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