rtgui_system.c 15 KB

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