rtgui_app.c 8.6 KB

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