rtgui_application.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. */
  14. #include <rtgui/rtgui_system.h>
  15. #include <rtgui/rtgui_application.h>
  16. #include <rtgui/widgets/window.h>
  17. #ifdef _WIN32
  18. #define RTGUI_EVENT_DEBUG
  19. #endif
  20. #ifdef RTGUI_EVENT_DEBUG
  21. const char *event_string[] =
  22. {
  23. /* window event */
  24. "WIN_CREATE", /* create a window */
  25. "WIN_DESTROY", /* destroy a window */
  26. "WIN_SHOW", /* show a window */
  27. "WIN_HIDE", /* hide a window */
  28. "WIN_ACTIVATE", /* activate a window */
  29. "WIN_DEACTIVATE", /* deactivate a window */
  30. "WIN_CLOSE", /* close a window */
  31. "WIN_MOVE", /* move a window */
  32. "WIN_RESIZE", /* resize a window */
  33. "WIN_MODAL_ENTER", /* a window modals */
  34. "SET_WM", /* set window manager */
  35. "UPDATE_BEGIN", /* begin of update rect */
  36. "UPDATE_END", /* end of update rect */
  37. "MONITOR_ADD", /* add a monitor rect */
  38. "MONITOR_REMOVE", /* remove a monitor rect*/
  39. "PAINT", /* paint on screen */
  40. "TIMER", /* timer */
  41. /* clip rect information */
  42. "CLIP_INFO", /* clip rect info */
  43. /* mouse and keyboard event */
  44. "MOUSE_MOTION", /* mouse motion */
  45. "MOUSE_BUTTON", /* mouse button info */
  46. "KBD", /* keyboard info */
  47. /* user command event */
  48. "COMMAND", /* user command */
  49. /* request's status event */
  50. "STATUS", /* request result */
  51. "SCROLLED", /* scroll bar scrolled */
  52. "RESIZE", /* widget resize */
  53. };
  54. #define DBG_MSG(x) rt_kprintf x
  55. static void rtgui_event_dump(rt_thread_t tid, rtgui_event_t* event)
  56. {
  57. char* sender = "(unknown)";
  58. if ((event->type == RTGUI_EVENT_TIMER) ||
  59. (event->type == RTGUI_EVENT_UPDATE_BEGIN) ||
  60. (event->type == RTGUI_EVENT_MOUSE_MOTION) ||
  61. (event->type == RTGUI_EVENT_UPDATE_END))
  62. {
  63. /* don't dump timer event */
  64. return ;
  65. }
  66. if (event->sender != RT_NULL)
  67. sender = event->sender->name;
  68. rt_kprintf("%s -- %s --> %s ", sender, event_string[event->type], tid->name);
  69. switch (event->type)
  70. {
  71. case RTGUI_EVENT_PAINT:
  72. {
  73. struct rtgui_event_paint *paint = (struct rtgui_event_paint *)event;
  74. if(paint->wid != RT_NULL)
  75. rt_kprintf("win: %s", paint->wid->title);
  76. }
  77. break;
  78. case RTGUI_EVENT_KBD:
  79. {
  80. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*) event;
  81. if (ekbd->wid != RT_NULL)
  82. rt_kprintf("win: %s", ekbd->wid->title);
  83. if (RTGUI_KBD_IS_UP(ekbd)) rt_kprintf(", up");
  84. else rt_kprintf(", down");
  85. }
  86. break;
  87. case RTGUI_EVENT_CLIP_INFO:
  88. {
  89. struct rtgui_event_clip_info *info = (struct rtgui_event_clip_info *)event;
  90. if(info->wid != RT_NULL)
  91. rt_kprintf("win: %s", info->wid->title);
  92. }
  93. break;
  94. case RTGUI_EVENT_WIN_CREATE:
  95. {
  96. struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
  97. rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d), addr: %p",
  98. #ifdef RTGUI_USING_SMALL_SIZE
  99. create->wid->title,
  100. RTGUI_WIDGET(create->wid)->extent.x1,
  101. RTGUI_WIDGET(create->wid)->extent.y1,
  102. RTGUI_WIDGET(create->wid)->extent.x2,
  103. RTGUI_WIDGET(create->wid)->extent.y2,
  104. #else
  105. create->title,
  106. create->extent.x1,
  107. create->extent.y1,
  108. create->extent.x2,
  109. create->extent.y2,
  110. #endif
  111. create->wid
  112. );
  113. }
  114. break;
  115. case RTGUI_EVENT_UPDATE_END:
  116. {
  117. struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
  118. rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
  119. update_end->rect.y1,
  120. update_end->rect.x2,
  121. update_end->rect.y2);
  122. }
  123. break;
  124. case RTGUI_EVENT_WIN_ACTIVATE:
  125. case RTGUI_EVENT_WIN_DEACTIVATE:
  126. case RTGUI_EVENT_WIN_SHOW:
  127. case RTGUI_EVENT_WIN_MODAL_ENTER:
  128. {
  129. struct rtgui_event_win *win = (struct rtgui_event_win *)event;
  130. if(win->wid != RT_NULL)
  131. rt_kprintf("win: %s", win->wid->title);
  132. }
  133. break;
  134. case RTGUI_EVENT_WIN_MOVE:
  135. {
  136. struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
  137. if(win->wid != RT_NULL)
  138. {
  139. rt_kprintf("win: %s", win->wid->title);
  140. rt_kprintf(" to (x:%d, y:%d)", win->x, win->y);
  141. }
  142. }
  143. break;
  144. case RTGUI_EVENT_WIN_RESIZE:
  145. {
  146. struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
  147. if (win->wid != RT_NULL)
  148. {
  149. rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
  150. RTGUI_WIDGET(win->wid)->extent.x1,
  151. RTGUI_WIDGET(win->wid)->extent.y1,
  152. RTGUI_WIDGET(win->wid)->extent.x2,
  153. RTGUI_WIDGET(win->wid)->extent.y2);
  154. }
  155. }
  156. break;
  157. case RTGUI_EVENT_MOUSE_BUTTON:
  158. case RTGUI_EVENT_MOUSE_MOTION:
  159. {
  160. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
  161. if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
  162. else rt_kprintf("right ");
  163. if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
  164. else rt_kprintf("up ");
  165. if (mouse->wid != RT_NULL)
  166. rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
  167. mouse->x, mouse->y);
  168. else
  169. rt_kprintf("(%d, %d)", mouse->x, mouse->y);
  170. }
  171. break;
  172. case RTGUI_EVENT_MONITOR_ADD:
  173. {
  174. struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
  175. if (monitor->wid != RT_NULL)
  176. {
  177. rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
  178. monitor->rect.x1, monitor->rect.y1,
  179. monitor->rect.x2, monitor->rect.y2);
  180. }
  181. }
  182. break;
  183. }
  184. rt_kprintf("\n");
  185. }
  186. #else
  187. #define DBG_MSG(x)
  188. #define rtgui_event_dump(tid, event)
  189. #endif
  190. rt_bool_t rtgui_application_event_handler(struct rtgui_object* obj, rtgui_event_t* event);
  191. static void _rtgui_application_constructor(struct rtgui_application *app)
  192. {
  193. /* set event handler */
  194. rtgui_object_set_event_handler(RTGUI_OBJECT(app),
  195. rtgui_application_event_handler);
  196. app->name = RT_NULL;
  197. /* set EXITED so we can destroy an application that just created */
  198. app->state_flag = RTGUI_APPLICATION_FLAG_EXITED;
  199. app->ref_count = 0;
  200. app->exit_code = 0;
  201. app->tid = RT_NULL;
  202. app->server = RT_NULL;
  203. app->mq = RT_NULL;
  204. app->modal_object = RT_NULL;
  205. app->on_idle = RT_NULL;
  206. }
  207. static void _rtgui_application_destructor(struct rtgui_application *app)
  208. {
  209. RT_ASSERT(app != RT_NULL);
  210. rt_free(app->name);
  211. app->name = RT_NULL;
  212. }
  213. DEFINE_CLASS_TYPE(application, "application",
  214. RTGUI_OBJECT_TYPE,
  215. _rtgui_application_constructor,
  216. _rtgui_application_destructor,
  217. sizeof(struct rtgui_application));
  218. struct rtgui_application* rtgui_application_create(
  219. rt_thread_t tid,
  220. const char *myname)
  221. {
  222. struct rtgui_application *app;
  223. RT_ASSERT(tid != RT_NULL);
  224. RT_ASSERT(myname != RT_NULL);
  225. /* create application */
  226. app = RTGUI_APPLICATION(rtgui_object_create(RTGUI_APPLICATION_TYPE));
  227. if (app == RT_NULL)
  228. return RT_NULL;
  229. DBG_MSG(("register a rtgui application(%s) on thread %s\n", myname, tid->name));
  230. app->tid = tid;
  231. /* set user thread */
  232. tid->user_data = (rt_uint32_t)app;
  233. app->mq = rt_mq_create("rtgui", sizeof(union rtgui_event_generic), 32, RT_IPC_FLAG_FIFO);
  234. if (app->mq == RT_NULL)
  235. {
  236. rt_kprintf("mq err\n");
  237. goto __mq_err;
  238. }
  239. /* set application title */
  240. app->name = (unsigned char*)rt_strdup((char*)myname);
  241. if (app->name != RT_NULL)
  242. return app;
  243. __mq_err:
  244. rtgui_object_destroy(RTGUI_OBJECT(app));
  245. tid->user_data = 0;
  246. return RT_NULL;
  247. }
  248. #define _rtgui_application_check(app) \
  249. do { \
  250. RT_ASSERT(app != RT_NULL); \
  251. RT_ASSERT(app->tid != RT_NULL); \
  252. RT_ASSERT(app->tid->user_data != 0); \
  253. RT_ASSERT(app->mq != RT_NULL); \
  254. } while (0)
  255. void rtgui_application_destroy(struct rtgui_application *app)
  256. {
  257. _rtgui_application_check(app);
  258. if (!(app->state_flag & RTGUI_APPLICATION_FLAG_EXITED))
  259. {
  260. rt_kprintf("cannot destroy a running application: %s.\n",
  261. app->name);
  262. return;
  263. }
  264. app->tid->user_data = 0;
  265. rt_mq_delete(app->mq);
  266. rtgui_object_destroy(RTGUI_OBJECT(app));
  267. }
  268. struct rtgui_application* rtgui_application_self(void)
  269. {
  270. struct rtgui_application *app;
  271. rt_thread_t self;
  272. /* get current thread */
  273. self = rt_thread_self();
  274. app = (struct rtgui_application*)(self->user_data);
  275. return app;
  276. }
  277. void rtgui_application_set_onidle(rtgui_idle_func onidle)
  278. {
  279. struct rtgui_application *app;
  280. app = rtgui_application_self();
  281. if (app != RT_NULL)
  282. app->on_idle = onidle;
  283. }
  284. rtgui_idle_func rtgui_application_get_onidle(void)
  285. {
  286. struct rtgui_application *app;
  287. app = rtgui_application_self();
  288. if (app != RT_NULL)
  289. return app->on_idle;
  290. else
  291. return RT_NULL;
  292. }
  293. extern rt_thread_t rt_thread_find(char* name);
  294. rt_thread_t rtgui_application_get_server(void)
  295. {
  296. return rt_thread_find("rtgui");
  297. }
  298. rt_err_t rtgui_application_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  299. {
  300. rt_err_t result;
  301. struct rtgui_application *app;
  302. RT_ASSERT(tid != RT_NULL);
  303. RT_ASSERT(event != RT_NULL);
  304. RT_ASSERT(event_size != 0);
  305. rtgui_event_dump(tid, event);
  306. /* find struct rtgui_application */
  307. app = (struct rtgui_application*) (tid->user_data);
  308. if (app == RT_NULL)
  309. return -RT_ERROR;
  310. result = rt_mq_send(app->mq, event, event_size);
  311. if (result != RT_EOK)
  312. {
  313. if (event->type != RTGUI_EVENT_TIMER)
  314. rt_kprintf("send event to %s failed\n", app->tid->name);
  315. }
  316. return result;
  317. }
  318. rt_err_t rtgui_application_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  319. {
  320. rt_err_t result;
  321. struct rtgui_application *app;
  322. RT_ASSERT(tid != RT_NULL);
  323. RT_ASSERT(event != RT_NULL);
  324. RT_ASSERT(event_size != 0);
  325. rtgui_event_dump(tid, event);
  326. /* find rtgui_application */
  327. app = (struct rtgui_application*) (tid->user_data);
  328. if (app == RT_NULL)
  329. return -RT_ERROR;
  330. result = rt_mq_urgent(app->mq, event, event_size);
  331. if (result != RT_EOK)
  332. rt_kprintf("send ergent event failed\n");
  333. return result;
  334. }
  335. rt_err_t rtgui_application_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  336. {
  337. rt_err_t r;
  338. struct rtgui_application *app;
  339. rt_int32_t ack_buffer, ack_status;
  340. struct rt_mailbox ack_mb;
  341. RT_ASSERT(tid != RT_NULL);
  342. RT_ASSERT(event != RT_NULL);
  343. RT_ASSERT(event_size != 0);
  344. rtgui_event_dump(tid, event);
  345. /* init ack mailbox */
  346. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  347. if (r!= RT_EOK)
  348. goto __return;
  349. app = (struct rtgui_application*) (tid->user_data);
  350. if (app == RT_NULL)
  351. {
  352. r = -RT_ERROR;
  353. goto __return;
  354. }
  355. event->ack = &ack_mb;
  356. r = rt_mq_send(app->mq, event, event_size);
  357. if (r != RT_EOK)
  358. {
  359. rt_kprintf("send sync event failed\n");
  360. goto __return;
  361. }
  362. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  363. if (r!= RT_EOK)
  364. goto __return;
  365. if (ack_status != RTGUI_STATUS_OK)
  366. r = -RT_ERROR;
  367. else
  368. r = RT_EOK;
  369. __return:
  370. /* fini ack mailbox */
  371. rt_mb_detach(&ack_mb);
  372. return r;
  373. }
  374. rt_err_t rtgui_application_ack(rtgui_event_t* event, rt_int32_t status)
  375. {
  376. RT_ASSERT(event != RT_NULL);
  377. RT_ASSERT(event->ack != RT_NULL);
  378. rt_mb_send(event->ack, status);
  379. return RT_EOK;
  380. }
  381. rt_err_t rtgui_application_recv(rtgui_event_t* event, rt_size_t event_size)
  382. {
  383. struct rtgui_application* app;
  384. rt_err_t r;
  385. RT_ASSERT(event != RT_NULL);
  386. RT_ASSERT(event_size != 0);
  387. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  388. if (app == RT_NULL)
  389. return -RT_ERROR;
  390. r = rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER);
  391. return r;
  392. }
  393. rt_err_t rtgui_application_recv_nosuspend(rtgui_event_t* event, rt_size_t event_size)
  394. {
  395. struct rtgui_application *app;
  396. rt_err_t r;
  397. RT_ASSERT(event != RT_NULL);
  398. RT_ASSERT(event != 0);
  399. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  400. if (app == RT_NULL)
  401. return -RT_ERROR;
  402. r = rt_mq_recv(app->mq, event, event_size, 0);
  403. return r;
  404. }
  405. rt_err_t rtgui_application_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  406. {
  407. struct rtgui_application *app;
  408. RT_ASSERT(event != RT_NULL);
  409. RT_ASSERT(event_size != 0);
  410. app = (struct rtgui_application*) (rt_thread_self()->user_data);
  411. if (app == RT_NULL)
  412. return -RT_ERROR;
  413. while (rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  414. {
  415. if (event->type == type)
  416. {
  417. return RT_EOK;
  418. }
  419. else
  420. {
  421. if (RTGUI_OBJECT(app)->event_handler != RT_NULL)
  422. {
  423. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  424. }
  425. }
  426. }
  427. return -RT_ERROR;
  428. }
  429. rt_inline rt_bool_t _rtgui_application_dest_handle(
  430. struct rtgui_application *app,
  431. struct rtgui_event *event)
  432. {
  433. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  434. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  435. if (dest_object != RT_NULL)
  436. {
  437. if (dest_object->event_handler != RT_NULL)
  438. return dest_object->event_handler(RTGUI_OBJECT(dest_object), event);
  439. else
  440. return RT_FALSE;
  441. }
  442. else
  443. {
  444. rt_kprintf("RTGUI ERROR:server sent a event(%d) without wid\n", event->type);
  445. return RT_FALSE;
  446. }
  447. }
  448. rt_bool_t rtgui_application_event_handler(struct rtgui_object* object, rtgui_event_t* event)
  449. {
  450. struct rtgui_application* app;
  451. RT_ASSERT(object != RT_NULL);
  452. RT_ASSERT(event != RT_NULL);
  453. app = RTGUI_APPLICATION(object);
  454. switch (event->type)
  455. {
  456. case RTGUI_EVENT_PAINT:
  457. case RTGUI_EVENT_CLIP_INFO:
  458. case RTGUI_EVENT_WIN_ACTIVATE:
  459. case RTGUI_EVENT_WIN_DEACTIVATE:
  460. case RTGUI_EVENT_WIN_CLOSE:
  461. case RTGUI_EVENT_WIN_MOVE:
  462. case RTGUI_EVENT_KBD:
  463. _rtgui_application_dest_handle(app, event);
  464. break;
  465. case RTGUI_EVENT_MOUSE_BUTTON:
  466. case RTGUI_EVENT_MOUSE_MOTION:
  467. {
  468. struct rtgui_event_win* wevent = (struct rtgui_event_win*)event;
  469. struct rtgui_object* dest_object = RTGUI_OBJECT(wevent->wid);
  470. // FIXME: let application determine the dest_wiget but not in sever
  471. // so we can combine this handler with above one
  472. if (app->modal_object != RT_NULL &&
  473. dest_object != app->modal_object)
  474. {
  475. // rt_kprintf("discard event %s that is not sent to modal object\n",
  476. // event_string[event->type]);
  477. }
  478. else
  479. {
  480. _rtgui_application_dest_handle(app, event);
  481. }
  482. }
  483. break;
  484. case RTGUI_EVENT_TIMER:
  485. {
  486. struct rtgui_timer* timer;
  487. struct rtgui_event_timer* etimer = (struct rtgui_event_timer*) event;
  488. timer = etimer->timer;
  489. if (timer->timeout != RT_NULL)
  490. {
  491. /* call timeout function */
  492. timer->timeout(timer, timer->user_data);
  493. }
  494. }
  495. break;
  496. case RTGUI_EVENT_COMMAND:
  497. {
  498. struct rtgui_event_command *ecmd = (struct rtgui_event_command*)event;
  499. if (ecmd->wid != RT_NULL)
  500. return _rtgui_application_dest_handle(app, event);
  501. }
  502. default:
  503. return rtgui_object_event_handler(object, event);
  504. }
  505. return RT_TRUE;
  506. }
  507. rt_inline void _rtgui_application_event_loop(struct rtgui_application *app)
  508. {
  509. rt_err_t result;
  510. rt_uint16_t current_ref;
  511. struct rtgui_event *event;
  512. _rtgui_application_check(app);
  513. /* point to event buffer */
  514. event = (struct rtgui_event*)app->event_buffer;
  515. current_ref = ++app->ref_count;
  516. while (current_ref <= app->ref_count)
  517. {
  518. RT_ASSERT(current_ref == app->ref_count);
  519. if (app->on_idle != RT_NULL)
  520. {
  521. result = rtgui_application_recv_nosuspend(event, sizeof(union rtgui_event_generic));
  522. if (result == RT_EOK)
  523. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  524. else if (result == -RT_ETIMEOUT)
  525. app->on_idle(RTGUI_OBJECT(app), RT_NULL);
  526. }
  527. else
  528. {
  529. result = rtgui_application_recv(event, sizeof(union rtgui_event_generic));
  530. if (result == RT_EOK)
  531. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  532. }
  533. }
  534. }
  535. rt_base_t rtgui_application_run(struct rtgui_application *app)
  536. {
  537. _rtgui_application_check(app);
  538. app->state_flag &= ~RTGUI_APPLICATION_FLAG_EXITED;
  539. _rtgui_application_event_loop(app);
  540. if (app->ref_count == 0)
  541. app->state_flag |= RTGUI_APPLICATION_FLAG_EXITED;
  542. return app->exit_code;
  543. }
  544. void rtgui_application_exit(struct rtgui_application* app, rt_uint16_t code)
  545. {
  546. --app->ref_count;
  547. app->exit_code = code;
  548. }