rtgui_system.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 image */
  31. rtgui_system_image_init();
  32. /* init font */
  33. rtgui_font_system_init();
  34. /* init rtgui server */
  35. rtgui_panel_init();
  36. rtgui_topwin_init();
  37. rtgui_server_init();
  38. /* init theme */
  39. rtgui_system_theme_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. #ifdef RTGUI_USING_SMALL_SIZE
  112. rt_kprintf(" clip no. %d", info->num_rect);
  113. #endif
  114. }
  115. break;
  116. case RTGUI_EVENT_WIN_CREATE:
  117. {
  118. struct rtgui_event_win_create *create = (struct rtgui_event_win_create*)event;
  119. rt_kprintf(" win: %s at (x1:%d, y1:%d, x2:%d, y2:%d)",
  120. create->title,
  121. create->extent.x1,
  122. create->extent.y1,
  123. create->extent.x2,
  124. create->extent.y2);
  125. }
  126. break;
  127. case RTGUI_EVENT_UPDATE_END:
  128. {
  129. struct rtgui_event_update_end* update_end = (struct rtgui_event_update_end*)event;
  130. rt_kprintf("(x:%d, y1:%d, x2:%d, y2:%d)", update_end->rect.x1,
  131. update_end->rect.y1,
  132. update_end->rect.x2,
  133. update_end->rect.y2);
  134. }
  135. break;
  136. case RTGUI_EVENT_WIN_ACTIVATE:
  137. case RTGUI_EVENT_WIN_DEACTIVATE:
  138. case RTGUI_EVENT_WIN_SHOW:
  139. {
  140. struct rtgui_event_win *win = (struct rtgui_event_win *)event;
  141. if(win->wid != RT_NULL)
  142. rt_kprintf("win: %s", win->wid->title);
  143. }
  144. break;
  145. case RTGUI_EVENT_WIN_MOVE:
  146. {
  147. struct rtgui_event_win_move *win = (struct rtgui_event_win_move *)event;
  148. if(win->wid != RT_NULL)
  149. rt_kprintf("win: %s", win->wid->title);
  150. }
  151. break;
  152. case RTGUI_EVENT_WIN_RESIZE:
  153. {
  154. struct rtgui_event_win_resize* win = (struct rtgui_event_win_resize *)event;
  155. if (win->wid != RT_NULL)
  156. {
  157. rt_kprintf("win: %s, rect(x1:%d, y1:%d, x2:%d, y2:%d)", win->wid->title,
  158. RTGUI_WIDGET(win->wid)->extent.x1,
  159. RTGUI_WIDGET(win->wid)->extent.y1,
  160. RTGUI_WIDGET(win->wid)->extent.x2,
  161. RTGUI_WIDGET(win->wid)->extent.y2);
  162. }
  163. }
  164. break;
  165. case RTGUI_EVENT_MOUSE_BUTTON:
  166. case RTGUI_EVENT_MOUSE_MOTION:
  167. {
  168. struct rtgui_event_mouse *mouse = (struct rtgui_event_mouse*)event;
  169. if (mouse->button & RTGUI_MOUSE_BUTTON_LEFT) rt_kprintf("left ");
  170. else rt_kprintf("right ");
  171. if (mouse->button & RTGUI_MOUSE_BUTTON_DOWN) rt_kprintf("down ");
  172. else rt_kprintf("up ");
  173. if (mouse->wid != RT_NULL)
  174. rt_kprintf("win: %s at (%d, %d)", mouse->wid->title,
  175. mouse->x, mouse->y);
  176. else
  177. rt_kprintf("(%d, %d)", mouse->x, mouse->y);
  178. }
  179. break;
  180. case RTGUI_EVENT_MONITOR_ADD:
  181. {
  182. struct rtgui_event_monitor *monitor = (struct rtgui_event_monitor*)event;
  183. if (monitor->panel != RT_NULL)
  184. {
  185. #if 0
  186. rt_kprintf("panel: %s, the rect is:(%d, %d) - (%d, %d)", monitor->panel->name,
  187. monitor->rect.x1, monitor->rect.y1,
  188. monitor->rect.x2, monitor->rect.y2);
  189. #endif
  190. rt_kprintf("the rect is:(%d, %d) - (%d, %d)",
  191. monitor->rect.x1, monitor->rect.y1,
  192. monitor->rect.x2, monitor->rect.y2);
  193. }
  194. else if (monitor->wid != RT_NULL)
  195. {
  196. rt_kprintf("win: %s, the rect is:(%d, %d) - (%d, %d)", monitor->wid->title,
  197. monitor->rect.x1, monitor->rect.y1,
  198. monitor->rect.x2, monitor->rect.y2);
  199. }
  200. }
  201. break;
  202. }
  203. rt_kprintf("\n");
  204. }
  205. #else
  206. #define DBG_MSG(x)
  207. #define rtgui_event_dump(tid, event)
  208. #endif
  209. struct rt_semaphore _rtgui_thread_hash_semaphore;
  210. void rtgui_thread_system_init()
  211. {
  212. rt_sem_init(&_rtgui_thread_hash_semaphore, "rtgui", 1, RT_IPC_FLAG_FIFO);
  213. }
  214. rtgui_thread_t* rtgui_thread_register(rt_thread_t tid, rt_mq_t mq)
  215. {
  216. rtgui_thread_t* thread = rtgui_malloc(sizeof(struct rtgui_thread));
  217. if (thread != RT_NULL)
  218. {
  219. DBG_MSG(("register a rtgui thread: %s, tid: 0x%p\n", tid->name, tid));
  220. /* set tid and mq */
  221. thread->tid = tid;
  222. thread->mq = mq;
  223. thread->widget = RT_NULL;
  224. /* take semaphore */
  225. rt_sem_take(&_rtgui_thread_hash_semaphore, RT_WAITING_FOREVER);
  226. /* set user thread */
  227. tid->user_data = (rt_uint32_t)thread;
  228. /* release semaphore */
  229. rt_sem_release(&_rtgui_thread_hash_semaphore);
  230. }
  231. return thread;
  232. }
  233. void rtgui_thread_deregister(rt_thread_t tid)
  234. {
  235. struct rtgui_thread* thread;
  236. /* find rtgui_thread */
  237. thread = (struct rtgui_thread*) (tid->user_data);
  238. if (thread != RT_NULL)
  239. {
  240. /* take semaphore */
  241. rt_sem_take(&_rtgui_thread_hash_semaphore, RT_WAITING_FOREVER);
  242. /* remove rtgui_thread */
  243. tid->user_data = 0;
  244. /* release semaphore */
  245. rt_sem_release(&_rtgui_thread_hash_semaphore);
  246. /* free rtgui_thread */
  247. rtgui_free(thread);
  248. }
  249. }
  250. extern rt_thread_t rt_thread_find(char* name);
  251. rt_thread_t rtgui_thread_get_server()
  252. {
  253. return rt_thread_find("rtgui");
  254. }
  255. void rtgui_thread_set_widget(struct rtgui_widget* widget)
  256. {
  257. struct rtgui_thread* thread;
  258. /* get rtgui_thread */
  259. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  260. if (thread != RT_NULL) thread->widget = widget;
  261. }
  262. struct rtgui_widget* rtgui_thread_get_widget()
  263. {
  264. struct rtgui_thread* thread;
  265. /* get rtgui_thread */
  266. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  267. return thread == RT_NULL? RT_NULL : thread->widget;
  268. }
  269. rt_err_t rtgui_thread_send(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  270. {
  271. struct rtgui_thread* thread;
  272. rtgui_event_dump(tid, event);
  273. if (event->type != RTGUI_EVENT_TIMER)
  274. rt_kprintf("event size: %d\n", event_size);
  275. /* find rtgui_thread */
  276. thread = (struct rtgui_thread*) (tid->user_data);
  277. if (thread == RT_NULL) return -RT_ERROR;
  278. return rt_mq_send(thread->mq, event, event_size);
  279. }
  280. rt_err_t rtgui_thread_send_urgent(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  281. {
  282. struct rtgui_thread* thread;
  283. rtgui_event_dump(tid, event);
  284. rt_kprintf("event size: %d\n", event_size);
  285. /* find rtgui_thread */
  286. thread = (struct rtgui_thread*) (tid->user_data);
  287. if (thread == RT_NULL) return -RT_ERROR;
  288. return rt_mq_urgent(thread->mq, event, event_size);
  289. }
  290. rt_err_t rtgui_thread_send_sync(rt_thread_t tid, rtgui_event_t* event, rt_size_t event_size)
  291. {
  292. rt_err_t r;
  293. struct rtgui_thread* thread;
  294. rt_int32_t ack_buffer, ack_status;
  295. struct rt_mailbox ack_mb;
  296. rtgui_event_dump(tid, event);
  297. rt_kprintf("event size: %d\n", event_size);
  298. /* init ack mailbox */
  299. r = rt_mb_init(&ack_mb, "ack", &ack_buffer, 1, 0);
  300. if ( r!= RT_EOK) goto __return;
  301. /* find rtgui_thread */
  302. thread = (struct rtgui_thread*) (tid->user_data);
  303. if (thread == RT_NULL){ r = RT_ERROR; goto __return; }
  304. event->ack = &ack_mb;
  305. r = rt_mq_send(thread->mq, event, event_size);
  306. if (r != RT_EOK) goto __return;
  307. r = rt_mb_recv(&ack_mb, (rt_uint32_t*)&ack_status, RT_WAITING_FOREVER);
  308. if ( r!= RT_EOK) goto __return;
  309. if (ack_status != RTGUI_STATUS_OK) r = -RT_ERROR;
  310. else r = RT_EOK;
  311. /* fini ack mailbox */
  312. rt_mb_detach(&ack_mb);
  313. __return:
  314. return r;
  315. }
  316. rt_err_t rtgui_thread_ack(rtgui_event_t* event, rt_int32_t status)
  317. {
  318. if (event != RT_NULL &&
  319. event->ack != RT_NULL)
  320. {
  321. rt_mb_send(event->ack, status);
  322. }
  323. return RT_EOK;
  324. }
  325. rt_err_t rtgui_thread_recv(rtgui_event_t* event, rt_size_t event_size)
  326. {
  327. struct rtgui_thread* thread;
  328. rt_err_t r;
  329. /* find rtgui_thread */
  330. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  331. if (thread == RT_NULL) return -RT_ERROR;
  332. r = rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER);
  333. return r;
  334. }
  335. rt_err_t rtgui_thread_recv_filter(rt_uint32_t type, rtgui_event_t* event, rt_size_t event_size)
  336. {
  337. struct rtgui_thread* thread;
  338. /* find rtgui_thread */
  339. thread = (struct rtgui_thread*) (rt_thread_self()->user_data);
  340. if (thread == RT_NULL) return -RT_ERROR;
  341. while (rt_mq_recv(thread->mq, event, event_size, RT_WAITING_FOREVER) == RT_EOK)
  342. {
  343. if (event->type == type)
  344. {
  345. return RT_EOK;
  346. }
  347. else
  348. {
  349. /* let widget to handle event */
  350. if (thread->widget != RT_NULL &&
  351. thread->widget->event_handler != RT_NULL)
  352. {
  353. thread->widget->event_handler(thread->widget, event);
  354. }
  355. }
  356. }
  357. return -RT_ERROR;
  358. }
  359. /************************************************************************/
  360. /* RTGUI Timer */
  361. /************************************************************************/
  362. static void rtgui_time_out(void* parameter)
  363. {
  364. rtgui_timer_t* timer;
  365. struct rtgui_event_timer event;
  366. timer = (rtgui_timer_t*)parameter;
  367. /*
  368. * Note: event_timer can not use RTGUI_EVENT_TIMER_INIT to init, for there is no
  369. * thread context
  370. */
  371. event.parent.type = RTGUI_EVENT_TIMER;
  372. event.parent.sender = RT_NULL;
  373. event.timer = timer;
  374. rtgui_thread_send(timer->tid, &(event.parent), sizeof(struct rtgui_event_timer));
  375. }
  376. rtgui_timer_t* rtgui_timer_create(rt_int32_t time, rt_base_t flag, rtgui_timeout_func timeout, void* parameter)
  377. {
  378. rtgui_timer_t* timer;
  379. timer = (rtgui_timer_t*) rtgui_malloc(sizeof(struct rtgui_timer));
  380. timer->tid = rt_thread_self();
  381. timer->timeout = timeout;
  382. timer->user_data = parameter;
  383. /* init rt-thread timer */
  384. rt_timer_init(&(timer->timer), "rtgui", rtgui_time_out, timer, time, (rt_uint8_t)flag);
  385. return timer;
  386. }
  387. void rtgui_timer_destory(rtgui_timer_t* timer)
  388. {
  389. RT_ASSERT(timer != RT_NULL);
  390. /* stop timer firstly */
  391. rtgui_timer_stop(timer);
  392. /* detach rt-thread timer */
  393. rt_timer_detach(&(timer->timer));
  394. rtgui_free(timer);
  395. }
  396. void rtgui_timer_start(rtgui_timer_t* timer)
  397. {
  398. RT_ASSERT(timer != RT_NULL);
  399. /* start rt-thread timer */
  400. rt_timer_start(&(timer->timer));
  401. }
  402. void rtgui_timer_stop (rtgui_timer_t* timer)
  403. {
  404. RT_ASSERT(timer != RT_NULL);
  405. /* stop rt-thread timer */
  406. rt_timer_stop(&(timer->timer));
  407. }
  408. /************************************************************************/
  409. /* RTGUI Memory Management */
  410. /************************************************************************/
  411. #ifdef RTGUI_MEM_TRACE
  412. struct rtgui_mem_info
  413. {
  414. rt_uint32_t allocated_size;
  415. rt_uint32_t max_allocated;
  416. };
  417. struct rtgui_mem_info mem_info;
  418. #define MEMTRACE_MAX 4096
  419. #define MEMTRACE_HASH_SIZE 256
  420. struct rti_memtrace_item
  421. {
  422. void* mb_ptr; /* memory block pointer */
  423. rt_uint32_t mb_len; /* memory block length */
  424. struct rti_memtrace_item* next;
  425. };
  426. struct rti_memtrace_item trace_list[MEMTRACE_MAX];
  427. struct rti_memtrace_item *item_hash[MEMTRACE_HASH_SIZE];
  428. struct rti_memtrace_item *item_free;
  429. rt_bool_t rti_memtrace_inited = 0;
  430. void rti_memtrace_init()
  431. {
  432. struct rti_memtrace_item *item;
  433. rt_uint32_t index;
  434. rt_memset(trace_list, 0, sizeof(trace_list));
  435. rt_memset(item_hash, 0, sizeof(item_hash));
  436. item_free = &trace_list[0];
  437. item = &trace_list[0];
  438. for (index = 1; index < MEMTRACE_HASH_SIZE; index ++)
  439. {
  440. item->next = &trace_list[index];
  441. item = item->next;
  442. }
  443. item->next = RT_NULL;
  444. }
  445. void rti_malloc_hook(void* ptr, rt_uint32_t len)
  446. {
  447. rt_uint32_t index;
  448. struct rti_memtrace_item* item;
  449. if (item_free == RT_NULL) return;
  450. mem_info.allocated_size += len;
  451. if (mem_info.max_allocated < mem_info.allocated_size)
  452. mem_info.max_allocated = mem_info.allocated_size;
  453. /* lock context */
  454. item = item_free;
  455. item_free = item->next;
  456. item->mb_ptr = ptr;
  457. item->mb_len = len;
  458. item->next = RT_NULL;
  459. /* get hash item index */
  460. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  461. if (item_hash[index] != RT_NULL)
  462. {
  463. /* add to list */
  464. item->next = item_hash[index];
  465. item_hash[index] = item;
  466. }
  467. else
  468. {
  469. /* set list header */
  470. item_hash[index] = item;
  471. }
  472. /* unlock context */
  473. }
  474. void rti_free_hook(void* ptr)
  475. {
  476. rt_uint32_t index;
  477. struct rti_memtrace_item *item;
  478. /* get hash item index */
  479. index = ((rt_uint32_t)ptr) % MEMTRACE_HASH_SIZE;
  480. if (item_hash[index] != RT_NULL)
  481. {
  482. item = item_hash[index];
  483. if (item->mb_ptr == ptr)
  484. {
  485. /* delete item from list */
  486. item_hash[index] = item->next;
  487. }
  488. else
  489. {
  490. /* find ptr in list */
  491. while (item->next != RT_NULL && item->next->mb_ptr != ptr)
  492. item = item->next;
  493. /* delete item from list */
  494. if (item->next != RT_NULL)
  495. {
  496. struct rti_memtrace_item* i;
  497. i = item->next;
  498. item->next = item->next->next;
  499. item = i;
  500. }
  501. else
  502. {
  503. /* not found */
  504. return;
  505. }
  506. }
  507. /* reduce allocated size */
  508. mem_info.allocated_size -= item->mb_len;
  509. /* clear item */
  510. rt_memset(item, 0, sizeof(struct rti_memtrace_item));
  511. /* add item to the free list */
  512. item->next = item_free;
  513. item_free = item;
  514. }
  515. }
  516. #endif
  517. void* rtgui_malloc(rt_size_t size)
  518. {
  519. void* ptr;
  520. ptr = rt_malloc(size);
  521. #ifdef RTGUI_MEM_TRACE
  522. if (rti_memtrace_inited == 0)
  523. {
  524. rti_memtrace_init();
  525. rti_memtrace_inited = 1;
  526. }
  527. if (ptr != RT_NULL)
  528. rti_malloc_hook(ptr, size);
  529. #endif
  530. return ptr;
  531. }
  532. void rtgui_free(void* ptr)
  533. {
  534. #ifdef RTGUI_MEM_TRACE
  535. if (ptr != RT_NULL)
  536. rti_free_hook(ptr);
  537. #endif
  538. rt_free(ptr);
  539. }