rtgui_app.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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->exit_code = 0;
  29. app->tid = RT_NULL;
  30. app->mq = RT_NULL;
  31. app->modal_object = 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_OBJECT_TYPE,
  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, sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
  64. if (app->mq == RT_NULL)
  65. {
  66. rt_kprintf("create msgq failed.\n");
  67. goto __mq_err;
  68. }
  69. /* set application title */
  70. app->name = (unsigned char *)rt_strdup((char *)title);
  71. if (app->name == RT_NULL)
  72. goto __err;
  73. /* the first app should be the server */
  74. srv_app = rtgui_get_server();
  75. if (srv_app == RT_NULL)
  76. {
  77. /* set user thread */
  78. tid->user_data = (rt_uint32_t)app;
  79. rt_kprintf("RTGUI: creating the server app %p.\n", app);
  80. return app;
  81. }
  82. RTGUI_EVENT_APP_CREATE_INIT(&event);
  83. event.app = app;
  84. /* notify rtgui server to one application has been created */
  85. if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
  86. {
  87. /* set user thread */
  88. tid->user_data = (rt_uint32_t)app;
  89. return app;
  90. }
  91. __err:
  92. __mq_err:
  93. rtgui_object_destroy(RTGUI_OBJECT(app));
  94. return RT_NULL;
  95. }
  96. RTM_EXPORT(rtgui_app_create);
  97. #define _rtgui_application_check(app) \
  98. do { \
  99. RT_ASSERT(app != RT_NULL); \
  100. RT_ASSERT(app->tid != RT_NULL); \
  101. RT_ASSERT(app->tid->user_data != 0); \
  102. RT_ASSERT(app->mq != RT_NULL); \
  103. } while (0)
  104. void rtgui_app_destroy(struct rtgui_app *app)
  105. {
  106. struct rtgui_app *srv_app;
  107. _rtgui_application_check(app);
  108. if (!(app->state_flag & RTGUI_APP_FLAG_EXITED))
  109. {
  110. rt_kprintf("cannot destroy a running application: %s.\n",
  111. app->name);
  112. return;
  113. }
  114. /* send a message to notify rtgui server */
  115. srv_app = rtgui_get_server();
  116. if (srv_app != rtgui_app_self())
  117. {
  118. struct rtgui_event_application event;
  119. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  120. event.app = app;
  121. if (rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event)) != RT_EOK)
  122. {
  123. rt_kprintf("destroy an application in server failed\n");
  124. return ;
  125. }
  126. }
  127. app->tid->user_data = 0;
  128. rt_mq_delete(app->mq);
  129. rtgui_object_destroy(RTGUI_OBJECT(app));
  130. }
  131. RTM_EXPORT(rtgui_app_destroy);
  132. struct rtgui_app *rtgui_app_self(void)
  133. {
  134. struct rtgui_app *app;
  135. rt_thread_t self;
  136. /* get current thread */
  137. self = rt_thread_self();
  138. app = (struct rtgui_app *)(self->user_data);
  139. return app;
  140. }
  141. RTM_EXPORT(rtgui_app_self);
  142. void rtgui_app_set_onidle(struct rtgui_app *app, rtgui_idle_func_t onidle)
  143. {
  144. _rtgui_application_check(app);
  145. app->on_idle = onidle;
  146. }
  147. RTM_EXPORT(rtgui_app_set_onidle);
  148. rtgui_idle_func_t rtgui_app_get_onidle(struct rtgui_app *app)
  149. {
  150. _rtgui_application_check(app);
  151. return app->on_idle;
  152. }
  153. RTM_EXPORT(rtgui_app_get_onidle);
  154. rt_inline rt_bool_t _rtgui_application_dest_handle(
  155. struct rtgui_app *app,
  156. struct rtgui_event *event)
  157. {
  158. struct rtgui_event_win *wevent = (struct rtgui_event_win *)event;
  159. struct rtgui_object *dest_object = RTGUI_OBJECT(wevent->wid);
  160. if (dest_object != RT_NULL)
  161. {
  162. if (dest_object->event_handler != RT_NULL)
  163. return dest_object->event_handler(RTGUI_OBJECT(dest_object), event);
  164. else
  165. return RT_FALSE;
  166. }
  167. else
  168. {
  169. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  170. return RT_FALSE;
  171. }
  172. }
  173. rt_bool_t rtgui_app_event_handler(struct rtgui_object *object, rtgui_event_t *event)
  174. {
  175. struct rtgui_app *app;
  176. RT_ASSERT(object != RT_NULL);
  177. RT_ASSERT(event != RT_NULL);
  178. app = RTGUI_APP(object);
  179. switch (event->type)
  180. {
  181. case RTGUI_EVENT_PAINT:
  182. case RTGUI_EVENT_CLIP_INFO:
  183. case RTGUI_EVENT_WIN_ACTIVATE:
  184. case RTGUI_EVENT_WIN_DEACTIVATE:
  185. case RTGUI_EVENT_WIN_CLOSE:
  186. case RTGUI_EVENT_WIN_MOVE:
  187. case RTGUI_EVENT_KBD:
  188. _rtgui_application_dest_handle(app, event);
  189. break;
  190. case RTGUI_EVENT_APP_ACTIVATE:
  191. if (app->main_object != RT_NULL)
  192. {
  193. rtgui_win_activate(RTGUI_WIN(app->main_object));
  194. if (app->modal_object != RT_NULL)
  195. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  196. }
  197. else if (app->modal_object != RT_NULL)
  198. {
  199. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  200. }
  201. break;
  202. case RTGUI_EVENT_APP_DESTROY:
  203. rtgui_app_exit(app, 0);
  204. break;
  205. case RTGUI_EVENT_MOUSE_BUTTON:
  206. case RTGUI_EVENT_MOUSE_MOTION:
  207. {
  208. struct rtgui_event_win *wevent = (struct rtgui_event_win *)event;
  209. struct rtgui_object *dest_object = RTGUI_OBJECT(wevent->wid);
  210. // FIXME: let application determine the dest_wiget but not in sever
  211. // so we can combine this handler with above one
  212. if (app->modal_object != RT_NULL &&
  213. dest_object != app->modal_object)
  214. {
  215. // rt_kprintf("discard event %s that is not sent to modal object\n",
  216. // event_string[event->type]);
  217. }
  218. else
  219. {
  220. _rtgui_application_dest_handle(app, event);
  221. }
  222. }
  223. break;
  224. case RTGUI_EVENT_TIMER:
  225. {
  226. struct rtgui_timer *timer;
  227. struct rtgui_event_timer *etimer = (struct rtgui_event_timer *) event;
  228. timer = etimer->timer;
  229. if (timer->timeout != RT_NULL)
  230. {
  231. /* call timeout function */
  232. timer->timeout(timer, timer->user_data);
  233. }
  234. }
  235. break;
  236. case RTGUI_EVENT_MV_MODEL:
  237. {
  238. struct rtgui_event_mv_model *emodel = (struct rtgui_event_mv_model *)event;
  239. RT_ASSERT(emodel->view);
  240. return rtgui_object_handle(RTGUI_OBJECT(emodel->view), event);
  241. }
  242. case RTGUI_EVENT_COMMAND:
  243. {
  244. struct rtgui_event_command *ecmd = (struct rtgui_event_command *)event;
  245. if (ecmd->wid != RT_NULL)
  246. return _rtgui_application_dest_handle(app, event);
  247. }
  248. default:
  249. return rtgui_object_event_handler(object, event);
  250. }
  251. return RT_TRUE;
  252. }
  253. rt_inline void _rtgui_application_event_loop(struct rtgui_app *app)
  254. {
  255. rt_err_t result;
  256. rt_uint16_t current_ref;
  257. struct rtgui_event *event;
  258. _rtgui_application_check(app);
  259. /* point to event buffer */
  260. event = (struct rtgui_event *)app->event_buffer;
  261. current_ref = ++app->ref_count;
  262. while (current_ref <= app->ref_count)
  263. {
  264. RT_ASSERT(current_ref == app->ref_count);
  265. if (app->on_idle != RT_NULL)
  266. {
  267. result = rtgui_recv_nosuspend(event, sizeof(union rtgui_event_generic));
  268. if (result == RT_EOK)
  269. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  270. else if (result == -RT_ETIMEOUT)
  271. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  272. }
  273. else
  274. {
  275. result = rtgui_recv(event, sizeof(union rtgui_event_generic));
  276. if (result == RT_EOK)
  277. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  278. }
  279. }
  280. }
  281. rt_base_t rtgui_app_run(struct rtgui_app *app)
  282. {
  283. _rtgui_application_check(app);
  284. app->state_flag &= ~RTGUI_APP_FLAG_EXITED;
  285. _rtgui_application_event_loop(app);
  286. if (app->ref_count == 0)
  287. app->state_flag |= RTGUI_APP_FLAG_EXITED;
  288. return app->exit_code;
  289. }
  290. RTM_EXPORT(rtgui_app_run);
  291. void rtgui_app_exit(struct rtgui_app *app, rt_uint16_t code)
  292. {
  293. --app->ref_count;
  294. app->exit_code = code;
  295. }
  296. RTM_EXPORT(rtgui_app_exit);
  297. void rtgui_app_activate(struct rtgui_app *app)
  298. {
  299. struct rtgui_event_application event;
  300. RTGUI_EVENT_APP_ACTIVATE_INIT(&event);
  301. event.app = app;
  302. rtgui_send(app, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  303. }
  304. RTM_EXPORT(rtgui_app_activate);
  305. void rtgui_app_close(struct rtgui_app *app)
  306. {
  307. struct rtgui_event_application event;
  308. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  309. event.app = app;
  310. rtgui_send(app, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  311. }
  312. RTM_EXPORT(rtgui_app_close);
  313. /**
  314. * set this application as window manager
  315. */
  316. rt_err_t rtgui_app_set_as_wm(struct rtgui_app *app)
  317. {
  318. struct rtgui_app *srv_app;
  319. struct rtgui_event_set_wm event;
  320. _rtgui_application_check(app);
  321. srv_app = rtgui_get_server();
  322. if (srv_app != RT_NULL)
  323. {
  324. /* notify rtgui server, this is a window manager */
  325. RTGUI_EVENT_SET_WM_INIT(&event);
  326. event.app = app;
  327. rtgui_send_sync(srv_app, RTGUI_EVENT(&event), sizeof(event));
  328. return RT_EOK;
  329. }
  330. return RT_ERROR;
  331. }
  332. RTM_EXPORT(rtgui_app_set_as_wm);
  333. void rtgui_app_set_main_win(struct rtgui_app *app, struct rtgui_win *win)
  334. {
  335. _rtgui_application_check(app);
  336. app->main_object = RTGUI_OBJECT(win);
  337. }
  338. RTM_EXPORT(rtgui_app_set_main_win);