rtgui_app.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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->server = RT_NULL;
  31. app->mq = RT_NULL;
  32. app->modal_object = RT_NULL;
  33. app->main_object = RT_NULL;
  34. app->on_idle = RT_NULL;
  35. }
  36. static void _rtgui_app_destructor(struct rtgui_app *app)
  37. {
  38. RT_ASSERT(app != RT_NULL);
  39. rt_free(app->name);
  40. app->name = RT_NULL;
  41. }
  42. DEFINE_CLASS_TYPE(application, "application",
  43. RTGUI_OBJECT_TYPE,
  44. _rtgui_app_constructor,
  45. _rtgui_app_destructor,
  46. sizeof(struct rtgui_app));
  47. struct rtgui_app* rtgui_app_create(
  48. rt_thread_t tid,
  49. const char *title)
  50. {
  51. rt_thread_t srv_tid;
  52. struct rtgui_app *app;
  53. struct rtgui_event_application event;
  54. RT_ASSERT(tid != RT_NULL);
  55. RT_ASSERT(title != RT_NULL);
  56. /* create application */
  57. app = RTGUI_APP(rtgui_object_create(RTGUI_APP_TYPE));
  58. if (app == RT_NULL)
  59. return RT_NULL;
  60. /* one thread only can create one rtgui application */
  61. RT_ASSERT(tid->user_data == 0);
  62. app->tid = tid;
  63. /* set user thread */
  64. tid->user_data = (rt_uint32_t)app;
  65. app->mq = rt_mq_create("rtgui", sizeof(union rtgui_event_generic), 32, 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. /* send a message to notify rtgui server */
  76. srv_tid = rtgui_get_server();
  77. if (srv_tid == RT_NULL)
  78. {
  79. rt_kprintf("gui server is not running.\n");
  80. goto __err;
  81. }
  82. /* create the rtgui server application */
  83. if (srv_tid == rt_thread_self())
  84. return app;
  85. RTGUI_EVENT_APP_CREATE_INIT(&event);
  86. event.app = app;
  87. /* notify rtgui server to one application has been created */
  88. if (rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event)) == RT_EOK)
  89. {
  90. return app;
  91. }
  92. __err:
  93. __mq_err:
  94. rtgui_object_destroy(RTGUI_OBJECT(app));
  95. tid->user_data = 0;
  96. return RT_NULL;
  97. }
  98. RTM_EXPORT(rtgui_app_create);
  99. #define _rtgui_application_check(app) \
  100. do { \
  101. RT_ASSERT(app != RT_NULL); \
  102. RT_ASSERT(app->tid != RT_NULL); \
  103. RT_ASSERT(app->tid->user_data != 0); \
  104. RT_ASSERT(app->mq != RT_NULL); \
  105. } while (0)
  106. void rtgui_app_destroy(struct rtgui_app *app)
  107. {
  108. rt_thread_t srv_tid;
  109. _rtgui_application_check(app);
  110. if (!(app->state_flag & RTGUI_APP_FLAG_EXITED))
  111. {
  112. rt_kprintf("cannot destroy a running application: %s.\n",
  113. app->name);
  114. return;
  115. }
  116. /* send a message to notify rtgui server */
  117. srv_tid = rtgui_get_server();
  118. if (srv_tid != rt_thread_self()) /* must not the server thread */
  119. {
  120. struct rtgui_event_application event;
  121. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  122. event.app = app;
  123. if (rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event)) != RT_EOK)
  124. {
  125. rt_kprintf("destroy an application in server failed\n");
  126. return ;
  127. }
  128. }
  129. app->tid->user_data = 0;
  130. rt_mq_delete(app->mq);
  131. rtgui_object_destroy(RTGUI_OBJECT(app));
  132. }
  133. RTM_EXPORT(rtgui_app_destroy);
  134. struct rtgui_app* rtgui_app_self(void)
  135. {
  136. struct rtgui_app *app;
  137. rt_thread_t self;
  138. /* get current thread */
  139. self = rt_thread_self();
  140. app = (struct rtgui_app*)(self->user_data);
  141. return app;
  142. }
  143. RTM_EXPORT(rtgui_app_self);
  144. void rtgui_app_set_onidle(rtgui_idle_func_t onidle)
  145. {
  146. struct rtgui_app *app;
  147. app = rtgui_app_self();
  148. if (app != RT_NULL)
  149. app->on_idle = onidle;
  150. }
  151. RTM_EXPORT(rtgui_app_set_onidle);
  152. rtgui_idle_func_t rtgui_app_get_onidle(void)
  153. {
  154. struct rtgui_app *app;
  155. app = rtgui_app_self();
  156. if (app != RT_NULL)
  157. return app->on_idle;
  158. else
  159. return RT_NULL;
  160. }
  161. RTM_EXPORT(rtgui_app_get_onidle);
  162. rt_inline rt_bool_t _rtgui_application_dest_handle(
  163. struct rtgui_app *app,
  164. struct rtgui_event *event)
  165. {
  166. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  167. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  168. if (dest_object != RT_NULL)
  169. {
  170. if (dest_object->event_handler != RT_NULL)
  171. return dest_object->event_handler(RTGUI_OBJECT(dest_object), event);
  172. else
  173. return RT_FALSE;
  174. }
  175. else
  176. {
  177. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  178. return RT_FALSE;
  179. }
  180. }
  181. rt_bool_t rtgui_app_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  182. {
  183. struct rtgui_app* app;
  184. RT_ASSERT(object != RT_NULL);
  185. RT_ASSERT(event != RT_NULL);
  186. app = RTGUI_APP(object);
  187. switch (event->type)
  188. {
  189. case RTGUI_EVENT_PAINT:
  190. case RTGUI_EVENT_CLIP_INFO:
  191. case RTGUI_EVENT_WIN_ACTIVATE:
  192. case RTGUI_EVENT_WIN_DEACTIVATE:
  193. case RTGUI_EVENT_WIN_CLOSE:
  194. case RTGUI_EVENT_WIN_MOVE:
  195. case RTGUI_EVENT_KBD:
  196. _rtgui_application_dest_handle(app, event);
  197. break;
  198. case RTGUI_EVENT_APP_ACTIVATE:
  199. if (app->main_object != RT_NULL)
  200. {
  201. rtgui_win_activate(RTGUI_WIN(app->main_object));
  202. if (app->modal_object != RT_NULL)
  203. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  204. }
  205. else if (app->modal_object != RT_NULL)
  206. {
  207. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  208. }
  209. break;
  210. case RTGUI_EVENT_APP_DESTROY:
  211. rtgui_app_exit(app, 0);
  212. break;
  213. case RTGUI_EVENT_MOUSE_BUTTON:
  214. case RTGUI_EVENT_MOUSE_MOTION:
  215. {
  216. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  217. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  218. // FIXME: let application determine the dest_wiget but not in sever
  219. // so we can combine this handler with above one
  220. if (app->modal_object != RT_NULL &&
  221. dest_object != app->modal_object)
  222. {
  223. // rt_kprintf("discard event %s that is not sent to modal object\n",
  224. // event_string[event->type]);
  225. }
  226. else
  227. {
  228. _rtgui_application_dest_handle(app, event);
  229. }
  230. }
  231. break;
  232. case RTGUI_EVENT_TIMER:
  233. {
  234. struct rtgui_timer* timer;
  235. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  236. timer = etimer->timer;
  237. if (timer->timeout != RT_NULL)
  238. {
  239. /* call timeout function */
  240. timer->timeout(timer, timer->user_data);
  241. }
  242. }
  243. break;
  244. case RTGUI_EVENT_COMMAND:
  245. {
  246. struct rtgui_event_command *ecmd = (struct rtgui_event_command*)event;
  247. if (ecmd->wid != RT_NULL)
  248. return _rtgui_application_dest_handle(app, event);
  249. }
  250. }
  251. return rtgui_object_event_handler(object, event);
  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->tid, 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->tid, 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(void)
  317. {
  318. rt_thread_t srv_tid;
  319. struct rtgui_event_set_wm event;
  320. struct rtgui_app* app;
  321. srv_tid = rtgui_get_server();
  322. app = rtgui_app_self();
  323. if (app != RT_NULL && srv_tid != RT_NULL)
  324. {
  325. /* notify rtgui server, this is a window manager */
  326. RTGUI_EVENT_SET_WM_INIT(&event);
  327. event.app = app;
  328. rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event));
  329. return RT_EOK;
  330. }
  331. return RT_ERROR;
  332. }
  333. RTM_EXPORT(rtgui_app_set_as_wm);
  334. void rtgui_app_set_main_win(struct rtgui_win* win)
  335. {
  336. struct rtgui_app *app;
  337. app = rtgui_app_self();
  338. if (app != RT_NULL)
  339. {
  340. app->main_object = RTGUI_OBJECT(win);
  341. }
  342. }
  343. RTM_EXPORT(rtgui_app_set_main_win);