rtgui_app.c 13 KB

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