rtgui_app.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #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. rt_thread_t srv_tid;
  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_tid = rtgui_get_server();
  117. if (srv_tid != rt_thread_self()) /* must not the server thread */
  118. {
  119. struct rtgui_event_application event;
  120. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  121. event.app = app;
  122. if (rtgui_send_sync(srv_tid, 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. 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. void rtgui_app_set_onidle(rtgui_idle_func_t onidle)
  142. {
  143. struct rtgui_app *app;
  144. app = rtgui_app_self();
  145. if (app != RT_NULL)
  146. app->on_idle = onidle;
  147. }
  148. rtgui_idle_func_t rtgui_app_get_onidle(void)
  149. {
  150. struct rtgui_app *app;
  151. app = rtgui_app_self();
  152. if (app != RT_NULL)
  153. return app->on_idle;
  154. else
  155. return RT_NULL;
  156. }
  157. rt_inline rt_bool_t _rtgui_application_dest_handle(
  158. struct rtgui_app *app,
  159. struct rtgui_event *event)
  160. {
  161. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  162. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  163. if (dest_object != RT_NULL)
  164. {
  165. if (dest_object->event_handler != RT_NULL)
  166. return dest_object->event_handler(RTGUI_OBJECT(dest_object), event);
  167. else
  168. return RT_FALSE;
  169. }
  170. else
  171. {
  172. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  173. return RT_FALSE;
  174. }
  175. }
  176. rt_bool_t rtgui_app_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  177. {
  178. struct rtgui_app* app;
  179. RT_ASSERT(object != RT_NULL);
  180. RT_ASSERT(event != RT_NULL);
  181. app = RTGUI_APP(object);
  182. switch (event->type)
  183. {
  184. case RTGUI_EVENT_PAINT:
  185. case RTGUI_EVENT_CLIP_INFO:
  186. case RTGUI_EVENT_WIN_ACTIVATE:
  187. case RTGUI_EVENT_WIN_DEACTIVATE:
  188. case RTGUI_EVENT_WIN_CLOSE:
  189. case RTGUI_EVENT_WIN_MOVE:
  190. case RTGUI_EVENT_KBD:
  191. _rtgui_application_dest_handle(app, event);
  192. break;
  193. case RTGUI_EVENT_APP_ACTIVATE:
  194. if (app->main_object != RT_NULL)
  195. {
  196. rtgui_win_activate(RTGUI_WIN(app->main_object));
  197. if (app->modal_object != RT_NULL)
  198. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  199. }
  200. else if (app->modal_object != RT_NULL)
  201. {
  202. rtgui_win_activate(RTGUI_WIN(app->modal_object));
  203. }
  204. break;
  205. case RTGUI_EVENT_APP_DESTROY:
  206. rtgui_app_exit(app, 0);
  207. break;
  208. case RTGUI_EVENT_MOUSE_BUTTON:
  209. case RTGUI_EVENT_MOUSE_MOTION:
  210. {
  211. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  212. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  213. // FIXME: let application determine the dest_wiget but not in sever
  214. // so we can combine this handler with above one
  215. if (app->modal_object != RT_NULL &&
  216. dest_object != app->modal_object)
  217. {
  218. // rt_kprintf("discard event %s that is not sent to modal object\n",
  219. // event_string[event->type]);
  220. }
  221. else
  222. {
  223. _rtgui_application_dest_handle(app, event);
  224. }
  225. }
  226. break;
  227. case RTGUI_EVENT_TIMER:
  228. {
  229. struct rtgui_timer* timer;
  230. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  231. timer = etimer->timer;
  232. if (timer->timeout != RT_NULL)
  233. {
  234. /* call timeout function */
  235. timer->timeout(timer, timer->user_data);
  236. }
  237. }
  238. break;
  239. case RTGUI_EVENT_COMMAND:
  240. {
  241. struct rtgui_event_command *ecmd = (struct rtgui_event_command*)event;
  242. if (ecmd->wid != RT_NULL)
  243. return _rtgui_application_dest_handle(app, event);
  244. }
  245. }
  246. return rtgui_object_event_handler(object, event);
  247. }
  248. rt_inline void _rtgui_application_event_loop(struct rtgui_app *app)
  249. {
  250. rt_err_t result;
  251. rt_uint16_t current_ref;
  252. struct rtgui_event *event;
  253. _rtgui_application_check(app);
  254. /* point to event buffer */
  255. event = (struct rtgui_event*)app->event_buffer;
  256. current_ref = ++app->ref_count;
  257. while (current_ref <= app->ref_count)
  258. {
  259. RT_ASSERT(current_ref == app->ref_count);
  260. if (app->on_idle != RT_NULL)
  261. {
  262. result = rtgui_recv_nosuspend(event, sizeof(union rtgui_event_generic));
  263. if (result == RT_EOK)
  264. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  265. else if (result == -RT_ETIMEOUT)
  266. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  267. }
  268. else
  269. {
  270. result = rtgui_recv(event, sizeof(union rtgui_event_generic));
  271. if (result == RT_EOK)
  272. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  273. }
  274. }
  275. }
  276. rt_base_t rtgui_app_run(struct rtgui_app *app)
  277. {
  278. _rtgui_application_check(app);
  279. app->state_flag &= ~RTGUI_APP_FLAG_EXITED;
  280. _rtgui_application_event_loop(app);
  281. if (app->ref_count == 0)
  282. app->state_flag |= RTGUI_APP_FLAG_EXITED;
  283. return app->exit_code;
  284. }
  285. void rtgui_app_exit(struct rtgui_app* app, rt_uint16_t code)
  286. {
  287. --app->ref_count;
  288. app->exit_code = code;
  289. }
  290. void rtgui_app_activate(struct rtgui_app *app)
  291. {
  292. struct rtgui_event_application event;
  293. RTGUI_EVENT_APP_ACTIVATE_INIT(&event);
  294. event.app = app;
  295. rtgui_send(app->tid, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  296. }
  297. void rtgui_app_close(struct rtgui_app *app)
  298. {
  299. struct rtgui_event_application event;
  300. RTGUI_EVENT_APP_DESTROY_INIT(&event);
  301. event.app = app;
  302. rtgui_send(app->tid, RTGUI_EVENT(&event), sizeof(struct rtgui_event_application));
  303. }
  304. /**
  305. * set this application as window manager
  306. */
  307. rt_err_t rtgui_app_set_as_wm(void)
  308. {
  309. rt_thread_t srv_tid;
  310. struct rtgui_event_set_wm event;
  311. struct rtgui_app* app;
  312. srv_tid = rtgui_get_server();
  313. app = rtgui_app_self();
  314. if (app != RT_NULL && srv_tid != RT_NULL)
  315. {
  316. /* notify rtgui server, this is a window manager */
  317. RTGUI_EVENT_SET_WM_INIT(&event);
  318. event.app = app;
  319. rtgui_send_sync(srv_tid, RTGUI_EVENT(&event), sizeof(event));
  320. return RT_EOK;
  321. }
  322. return RT_ERROR;
  323. }
  324. void rtgui_app_set_main_win(struct rtgui_win* win)
  325. {
  326. struct rtgui_app *app;
  327. app = rtgui_app_self();
  328. if (app != RT_NULL)
  329. {
  330. app->main_object = RTGUI_OBJECT(win);
  331. }
  332. }