rtgui_system.c 15 KB

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