rtgui_system.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * File : rtgui_system.c
  3. * This file is part of RTGUI in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, 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. * 2009-10-04 Bernard first version
  13. */
  14. #include <rtgui/rtgui.h>
  15. #include <rtgui/image.h>
  16. #include <rtgui/font.h>
  17. #include <rtgui/event.h>
  18. #include <rtgui/rtgui_app.h>
  19. #include <rtgui/rtgui_server.h>
  20. #include <rtgui/rtgui_system.h>
  21. #include <rtgui/widgets/window.h>
  22. #include <rtgui/rtgui_theme.h>
  23. #ifdef _WIN32
  24. #define RTGUI_MEM_TRACE
  25. #endif
  26. void rtgui_system_server_init()
  27. {
  28. /* init image */
  29. rtgui_system_image_init();
  30. /* init font */
  31. rtgui_font_system_init();
  32. /* init rtgui server */
  33. rtgui_topwin_init();
  34. rtgui_server_init();
  35. /* init theme */
  36. rtgui_system_theme_init();
  37. }
  38. /************************************************************************/
  39. /* RTGUI Timer */
  40. /************************************************************************/
  41. static void rtgui_time_out(void* parameter)
  42. {
  43. rtgui_timer_t* timer;
  44. rtgui_event_timer_t event;
  45. timer = (rtgui_timer_t*)parameter;
  46. /*
  47. * Note: event_timer can not use RTGUI_EVENT_TIMER_INIT to init, for there is no
  48. * thread context
  49. */
  50. event.parent.type = RTGUI_EVENT_TIMER;
  51. event.parent.sender = RT_NULL;
  52. event.timer = timer;
  53. rtgui_send(timer->tid, &(event.parent), sizeof(rtgui_event_timer_t));
  54. }
  55. rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_int32_t flag, rtgui_timeout_func timeout, void* parameter)
  56. {
  57. rtgui_timer_t* timer;
  58. timer = (rtgui_timer_t*) rtgui_malloc(sizeof(rtgui_timer_t));
  59. timer->tid = rt_thread_self();
  60. timer->timeout = timeout;
  61. timer->user_data = parameter;
  62. /* init rt-thread timer */
  63. rt_timer_init(&(timer->timer), "rtgui", rtgui_time_out, timer, time, (rt_uint8_t)flag);
  64. return timer;
  65. }
  66. void rtgui_timer_destory(rtgui_timer_t* timer)
  67. {
  68. RT_ASSERT(timer != RT_NULL);
  69. /* stop timer firstly */
  70. rtgui_timer_stop(timer);
  71. /* detach rt-thread timer */
  72. rt_timer_detach(&(timer->timer));
  73. rtgui_free(timer);
  74. }
  75. void rtgui_timer_start(rtgui_timer_t* timer)
  76. {
  77. RT_ASSERT(timer != RT_NULL);
  78. /* start rt-thread timer */
  79. rt_timer_start(&(timer->timer));
  80. }
  81. void rtgui_timer_stop (rtgui_timer_t* timer)
  82. {
  83. RT_ASSERT(timer != RT_NULL);
  84. /* stop rt-thread timer */
  85. rt_timer_stop(&(timer->timer));
  86. }
  87. /************************************************************************/
  88. /* RTGUI Memory Management */
  89. /************************************************************************/
  90. #ifdef RTGUI_MEM_TRACE
  91. struct rtgui_mem_info
  92. {
  93. rt_uint32_t allocated_size;
  94. rt_uint32_t max_allocated;
  95. };
  96. struct rtgui_mem_info mem_info;
  97. #define MEMTRACE_MAX 4096
  98. #define MEMTRACE_HASH_SIZE 256
  99. struct rti_memtrace_item
  100. {
  101. void* mb_ptr; /* memory block pointer */
  102. rt_uint32_t mb_len; /* memory block length */
  103. struct rti_memtrace_item* next;
  104. };
  105. struct rti_memtrace_item trace_list[MEMTRACE_MAX];
  106. struct rti_memtrace_item *item_hash[MEMTRACE_HASH_SIZE];
  107. struct rti_memtrace_item *item_free;
  108. rt_bool_t rti_memtrace_inited = 0;
  109. void rti_memtrace_init()
  110. {
  111. struct rti_memtrace_item *item;
  112. rt_uint32_t index;
  113. rt_memset(trace_list, 0, sizeof(trace_list));
  114. rt_memset(item_hash, 0, sizeof(item_hash));
  115. item_free = &trace_list[0];
  116. item = &trace_list[0];
  117. for (index = 1; index < MEMTRACE_HASH_SIZE; index ++)
  118. {
  119. item->next = &trace_list[index];
  120. item = item->next;
  121. }
  122. item->next = RT_NULL;
  123. }
  124. void rti_malloc_hook(void* ptr, rt_uint32_t len)
  125. {
  126. rt_uint32_t index;
  127. struct rti_memtrace_item* item;
  128. if (item_free == RT_NULL) return;
  129. mem_info.allocated_size += len;
  130. if (mem_info.max_allocated < mem_info.allocated_size)
  131. mem_info.max_allocated = mem_info.allocated_size;
  132. /* lock context */
  133. item = item_free;
  134. item_free = item->next;
  135. item->mb_ptr = ptr;
  136. item->mb_len = len;
  137. item->next = RT_NULL;
  138. /* get hash item index */
  139. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  140. if (item_hash[index] != RT_NULL)
  141. {
  142. /* add to list */
  143. item->next = item_hash[index];
  144. item_hash[index] = item;
  145. }
  146. else
  147. {
  148. /* set list header */
  149. item_hash[index] = item;
  150. }
  151. /* unlock context */
  152. }
  153. void rti_free_hook(void* ptr)
  154. {
  155. rt_uint32_t index;
  156. struct rti_memtrace_item *item;
  157. /* get hash item index */
  158. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  159. if (item_hash[index] != RT_NULL)
  160. {
  161. item = item_hash[index];
  162. if (item->mb_ptr == ptr)
  163. {
  164. /* delete item from list */
  165. item_hash[index] = item->next;
  166. }
  167. else
  168. {
  169. /* find ptr in list */
  170. while (item->next != RT_NULL && item->next->mb_ptr != ptr)
  171. item = item->next;
  172. /* delete item from list */
  173. if (item->next != RT_NULL)
  174. {
  175. struct rti_memtrace_item* i;
  176. i = item->next;
  177. item->next = item->next->next;
  178. item = i;
  179. }
  180. else
  181. {
  182. /* not found */
  183. return;
  184. }
  185. }
  186. /* reduce allocated size */
  187. mem_info.allocated_size -= item->mb_len;
  188. /* clear item */
  189. rt_memset(item, 0, sizeof(struct rti_memtrace_item));
  190. /* add item to the free list */
  191. item->next = item_free;
  192. item_free = item;
  193. }
  194. }
  195. #endif
  196. void* rtgui_malloc(rt_size_t size)
  197. {
  198. void* ptr;
  199. ptr = rt_malloc(size);
  200. #ifdef RTGUI_MEM_TRACE
  201. if (rti_memtrace_inited == 0)
  202. {
  203. rti_memtrace_init();
  204. rti_memtrace_inited = 1;
  205. }
  206. if (ptr != RT_NULL)
  207. rti_malloc_hook(ptr, size);
  208. #endif
  209. return ptr;
  210. }
  211. void* rtgui_realloc(void* ptr, rt_size_t size)
  212. {
  213. void* new_ptr;
  214. #ifdef RTGUI_MEM_TRACE
  215. new_ptr = rtgui_malloc(size);
  216. if ((new_ptr != RT_NULL) && (ptr != RT_NULL))
  217. {
  218. rt_memcpy(new_ptr, ptr, size);
  219. rtgui_free(ptr);
  220. }
  221. #else
  222. new_ptr = rt_realloc(ptr, size);
  223. #endif
  224. return new_ptr;
  225. }
  226. void rtgui_free(void* ptr)
  227. {
  228. #ifdef RTGUI_MEM_TRACE
  229. if (ptr != RT_NULL)
  230. rti_free_hook(ptr);
  231. #endif
  232. rt_free(ptr);
  233. }
  234. #if defined(RTGUI_MEM_TRACE) && defined(RT_USING_FINSH)
  235. #include <finsh.h>
  236. void list_mem(void)
  237. {
  238. rt_kprintf("Current Used: %d, Maximal Used: %d\n", mem_info.allocated_size, mem_info.max_allocated);
  239. }
  240. FINSH_FUNCTION_EXPORT(list_mem, display memory information);
  241. #endif
  242. /************************************************************************/
  243. /* RTGUI Event Dump */
  244. /************************************************************************/
  245. #ifdef _WIN32
  246. #define RTGUI_EVENT_DEBUG
  247. #endif
  248. #ifdef RTGUI_EVENT_DEBUG
  249. const char *event_string[] =
  250. {
  251. /* application event */
  252. "APP_CREATE", /* create an application */
  253. "APP_DESTROY", /* destroy an application */
  254. "APP_ACTIVATE", /* activate an application */
  255. /* window event */
  256. "WIN_CREATE", /* create a window */
  257. "WIN_DESTROY", /* destroy a window */
  258. "WIN_SHOW", /* show a window */
  259. "WIN_HIDE", /* hide a window */
  260. "WIN_ACTIVATE", /* activate a window */
  261. "WIN_DEACTIVATE", /* deactivate a window */
  262. "WIN_CLOSE", /* close a window */
  263. "WIN_MOVE", /* move a window */
  264. "WIN_RESIZE", /* resize a window */
  265. "WIN_MODAL_ENTER", /* a window modals */
  266. "SET_WM", /* set window manager */
  267. "UPDATE_BEGIN", /* begin of update rect */
  268. "UPDATE_END", /* end of update rect */
  269. "MONITOR_ADD", /* add a monitor rect */
  270. "MONITOR_REMOVE", /* remove a monitor rect*/
  271. "SHOW", /* the widget is going to be shown */
  272. "HIDE", /* the widget is going to be hidden */
  273. "PAINT", /* paint on screen */
  274. "TIMER", /* timer */
  275. /* clip rect information */
  276. "CLIP_INFO", /* clip rect info */
  277. /* mouse and keyboard event */
  278. "MOUSE_MOTION", /* mouse motion */
  279. "MOUSE_BUTTON", /* mouse button info */
  280. "KBD", /* keyboard info */
  281. /* user command event */
  282. "COMMAND", /* user command */
  283. /* request's status event */
  284. "STATUS", /* request result */
  285. "SCROLLED", /* scroll bar scrolled */
  286. "RESIZE", /* widget resize */
  287. };
  288. #define DBG_MSG(x) rt_kprintf x
  289. static void rtgui_event_dump(rt_thread_t tid, rtgui_event_t* event)
  290. {
  291. char* sender = "(unknown)";
  292. if ((event->type == RTGUI_EVENT_TIMER) ||
  293. (event->type == RTGUI_EVENT_UPDATE_BEGIN) ||
  294. (event->type == RTGUI_EVENT_MOUSE_MOTION) ||
  295. (event->type == RTGUI_EVENT_UPDATE_END))
  296. {
  297. /* don't dump timer event */
  298. return ;
  299. }
  300. if (event->sender != RT_NULL)
  301. sender = event->sender->name;
  302. rt_kprintf("%s -- %s --> %s ", sender, event_string[event->type], tid->name);
  303. switch (event->type)
  304. {
  305. case RTGUI_EVENT_APP_CREATE:
  306. case RTGUI_EVENT_APP_DESTROY:
  307. case RTGUI_EVENT_APP_ACTIVATE:
  308. {
  309. struct rtgui_event_application *eapp = (struct rtgui_event_application *)event;
  310. rt_kprintf("app: %s", eapp->app->name);
  311. }
  312. break;
  313. case RTGUI_EVENT_PAINT:
  314. {
  315. struct rtgui_event_paint *paint = (struct rtgui_event_paint *)event;
  316. if(paint->wid != RT_NULL)
  317. rt_kprintf("win: %s", paint->wid->title);
  318. }
  319. break;
  320. case RTGUI_EVENT_KBD:
  321. {
  322. struct rtgui_event_kbd *ekbd = (struct rtgui_event_kbd*) event;
  323. if (ekbd->wid != RT_NULL)
  324. rt_kprintf("win: %s", ekbd->wid->title);
  325. if (RTGUI_KBD_IS_UP(ekbd)) rt_kprintf(", up");
  326. else rt_kprintf(", down");
  327. }
  328. break;
  329. case RTGUI_EVENT_CLIP_INFO:
  330. {
  331. struct rtgui_event_clip_info *info = (struct rtgui_event_clip_info *)event;
  332. if(info->wid != RT_NULL)
  333. rt_kprintf("win: %s", info->wid->title);
  334. }
  335. break;
  336. case RTGUI_EVENT_WIN_CREATE:
  337. {
  338. struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
  339. rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d), addr: %p",
  340. #ifdef RTGUI_USING_SMALL_SIZE
  341. create->wid->title,
  342. RTGUI_WIDGET(create->wid)->extent.x1,
  343. RTGUI_WIDGET(create->wid)->extent.y1,
  344. RTGUI_WIDGET(create->wid)->extent.x2,
  345. RTGUI_WIDGET(create->wid)->extent.y2,
  346. #else
  347. create->title,
  348. create->extent.x1,
  349. create->extent.y1,
  350. create->extent.x2,
  351. create->extent.y2,
  352. #endif
  353. create->wid
  354. );
  355. }
  356. break;
  357. case RTGUI_EVENT_UPDATE_END:
  358. {
  359. struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
  360. rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
  361. update_end->rect.y1,
  362. update_end->rect.x2,
  363. update_end->rect.y2);
  364. }
  365. break;
  366. case RTGUI_EVENT_WIN_ACTIVATE:
  367. case RTGUI_EVENT_WIN_DEACTIVATE:
  368. case RTGUI_EVENT_WIN_SHOW:
  369. case RTGUI_EVENT_WIN_MODAL_ENTER:
  370. {
  371. struct rtgui_event_win *win = (struct rtgui_event_win *)event;
  372. if(win->wid != RT_NULL)
  373. rt_kprintf("win: %s", win->wid->title);
  374. }
  375. break;
  376. case RTGUI_EVENT_WIN_MOVE:
  377. {
  378. struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
  379. if(win->wid != RT_NULL)
  380. {
  381. rt_kprintf("win: %s", win->wid->title);
  382. rt_kprintf(" to (x:%d, y:%d)", win->x, win->y);
  383. }
  384. }
  385. break;
  386. case RTGUI_EVENT_WIN_RESIZE:
  387. {
  388. struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
  389. if (win->wid != RT_NULL)
  390. {
  391. rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
  392. RTGUI_WIDGET(win->wid)->extent.x1,
  393. RTGUI_WIDGET(win->wid)->extent.y1,
  394. RTGUI_WIDGET(win->wid)->extent.x2,
  395. RTGUI_WIDGET(win->wid)->extent.y2);
  396. }
  397. }
  398. break;
  399. case RTGUI_EVENT_MOUSE_BUTTON:
  400. case RTGUI_EVENT_MOUSE_MOTION:
  401. {
  402. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
  403. if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
  404. else rt_kprintf("right ");
  405. if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
  406. else rt_kprintf("up ");
  407. if (mouse->wid != RT_NULL)
  408. rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
  409. mouse->x, mouse->y);
  410. else
  411. rt_kprintf("(%d, %d)", mouse->x, mouse->y);
  412. }
  413. break;
  414. case RTGUI_EVENT_MONITOR_ADD:
  415. {
  416. struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
  417. if (monitor->wid != RT_NULL)
  418. {
  419. rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
  420. monitor->rect.x1, monitor->rect.y1,
  421. monitor->rect.x2, monitor->rect.y2);
  422. }
  423. }
  424. break;
  425. }
  426. rt_kprintf("\n");
  427. }
  428. #else
  429. #define DBG_MSG(x)
  430. #define rtgui_event_dump(tid, event)
  431. #endif
  432. /************************************************************************/
  433. /* RTGUI IPC APIs */
  434. /************************************************************************/
  435. rt_err_t rtgui_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  436. {
  437. rt_err_t result;
  438. struct rtgui_app *app;
  439. RT_ASSERT(tid != RT_NULL);
  440. RT_ASSERT(event != RT_NULL);
  441. RT_ASSERT(event_size != 0);
  442. rtgui_event_dump(tid, event);
  443. /* find struct rtgui_application */
  444. app = (struct rtgui_app*) (tid->user_data);
  445. if (app == RT_NULL)
  446. return -RT_ERROR;
  447. result = rt_mq_send(app->mq, event, event_size);
  448. if (result != RT_EOK)
  449. {
  450. if (event->type != RTGUI_EVENT_TIMER)
  451. rt_kprintf("send event to %s failed\n", app->tid->name);
  452. }
  453. return result;
  454. }
  455. rt_err_t rtgui_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  456. {
  457. rt_err_t result;
  458. struct rtgui_app *app;
  459. RT_ASSERT(tid != RT_NULL);
  460. RT_ASSERT(event != RT_NULL);
  461. RT_ASSERT(event_size != 0);
  462. rtgui_event_dump(tid, event);
  463. /* find rtgui_application */
  464. app = (struct rtgui_app*) (tid->user_data);
  465. if (app == RT_NULL)
  466. return -RT_ERROR;
  467. result = rt_mq_urgent(app->mq, event, event_size);
  468. if (result != RT_EOK)
  469. rt_kprintf("send ergent event failed\n");
  470. return result;
  471. }
  472. rt_err_t rtgui_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  473. {
  474. rt_err_t r;
  475. struct rtgui_app *app;
  476. rt_int32_t ack_buffer, ack_status;
  477. struct rt_mailbox ack_mb;
  478. RT_ASSERT(tid != RT_NULL);
  479. RT_ASSERT(event != RT_NULL);
  480. RT_ASSERT(event_size != 0);
  481. rtgui_event_dump(tid, event);
  482. /* init ack mailbox */
  483. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  484. if (r!= RT_EOK)
  485. goto __return;
  486. app = (struct rtgui_app*) (tid->user_data);
  487. if (app == RT_NULL)
  488. {
  489. r = -RT_ERROR;
  490. goto __return;
  491. }
  492. event->ack = &ack_mb;
  493. r = rt_mq_send(app->mq, event, event_size);
  494. if (r != RT_EOK)
  495. {
  496. rt_kprintf("send sync event failed\n");
  497. goto __return;
  498. }
  499. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  500. if (r!= RT_EOK)
  501. goto __return;
  502. if (ack_status != RTGUI_STATUS_OK)
  503. r = -RT_ERROR;
  504. else
  505. r = RT_EOK;
  506. __return:
  507. /* fini ack mailbox */
  508. rt_mb_detach(&ack_mb);
  509. return r;
  510. }
  511. rt_err_t rtgui_ack(rtgui_event_t* event, rt_int32_t status)
  512. {
  513. RT_ASSERT(event != RT_NULL);
  514. RT_ASSERT(event->ack != RT_NULL);
  515. rt_mb_send(event->ack, status);
  516. return RT_EOK;
  517. }
  518. rt_err_t rtgui_recv(rtgui_event_t* event, rt_size_t event_size)
  519. {
  520. struct rtgui_app* app;
  521. rt_err_t r;
  522. RT_ASSERT(event != RT_NULL);
  523. RT_ASSERT(event_size != 0);
  524. app = (struct rtgui_app*) (rt_thread_self()->user_data);
  525. if (app == RT_NULL)
  526. return -RT_ERROR;
  527. r = rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER);
  528. return r;
  529. }
  530. rt_err_t rtgui_recv_nosuspend(rtgui_event_t* event, rt_size_t event_size)
  531. {
  532. struct rtgui_app *app;
  533. rt_err_t r;
  534. RT_ASSERT(event != RT_NULL);
  535. RT_ASSERT(event != 0);
  536. app = (struct rtgui_app*) (rt_thread_self()->user_data);
  537. if (app == RT_NULL)
  538. return -RT_ERROR;
  539. r = rt_mq_recv(app->mq, event, event_size, 0);
  540. return r;
  541. }
  542. rt_err_t rtgui_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  543. {
  544. struct rtgui_app *app;
  545. RT_ASSERT(event != RT_NULL);
  546. RT_ASSERT(event_size != 0);
  547. app = (struct rtgui_app*) (rt_thread_self()->user_data);
  548. if (app == RT_NULL)
  549. return -RT_ERROR;
  550. while (rt_mq_recv(app->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  551. {
  552. if (event->type == type)
  553. {
  554. return RT_EOK;
  555. }
  556. else
  557. {
  558. if (RTGUI_OBJECT(app)->event_handler != RT_NULL)
  559. {
  560. RTGUI_OBJECT(app)->event_handler(RTGUI_OBJECT(app), event);
  561. }
  562. }
  563. }
  564. return -RT_ERROR;
  565. }
  566. rt_thread_t rtgui_get_server(void)
  567. {
  568. return rt_thread_find("rtgui");
  569. }