server.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * File : server.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/event.h>
  16. #include <rtgui/rtgui_system.h>
  17. #include <rtgui/driver.h>
  18. #include "mouse.h"
  19. #include "panel.h"
  20. #include "topwin.h"
  21. static struct rt_thread *rtgui_server_tid;
  22. static struct rt_messagequeue *rtgui_server_mq;
  23. extern struct rtgui_topwin* rtgui_server_focus_topwin;
  24. struct rtgui_panel* rtgui_server_focus_panel = RT_NULL;
  25. void rtgui_server_create_application(struct rtgui_event_panel_attach* event)
  26. {
  27. struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
  28. if (panel != RT_NULL)
  29. {
  30. struct rtgui_event_panel_info ep;
  31. RTGUI_EVENT_PANEL_INFO_INIT(&ep);
  32. if (panel->wm_thread != RT_NULL)
  33. {
  34. /* notify to workbench */
  35. rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_attach));
  36. }
  37. /* send the responses - ok */
  38. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  39. /* send the panel info */
  40. ep.panel = panel;
  41. ep.extent = panel->extent;
  42. rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&ep, sizeof(ep));
  43. rtgui_panel_thread_add(event->panel_name, event->parent.sender);
  44. }
  45. else
  46. {
  47. /* send the responses - failure */
  48. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
  49. }
  50. }
  51. void rtgui_server_destroy_application(struct rtgui_event_panel_detach* event)
  52. {
  53. struct rtgui_panel* panel = event->panel;
  54. if (panel != RT_NULL)
  55. {
  56. if (panel->wm_thread != RT_NULL)
  57. {
  58. /* notify to workbench */
  59. rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_detach));
  60. }
  61. /* send the responses */
  62. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  63. rtgui_panel_thread_remove(panel, event->parent.sender);
  64. {
  65. /* get next thread and active it */
  66. rt_thread_t tid = rtgui_panel_get_active_thread(panel);
  67. if (tid != RT_NULL)
  68. {
  69. /* let this thread repaint */
  70. struct rtgui_event_paint epaint;
  71. RTGUI_EVENT_PAINT_INIT(&epaint);
  72. epaint.wid = RT_NULL;
  73. rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
  74. }
  75. }
  76. }
  77. else
  78. {
  79. /* send the responses - failure */
  80. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_NRC);
  81. }
  82. }
  83. void rtgui_server_thread_panel_show(struct rtgui_event_panel_show* event)
  84. {
  85. struct rtgui_panel* panel = event->panel;
  86. if (panel != RT_NULL)
  87. {
  88. struct rtgui_event_paint epaint;
  89. /* send the responses */
  90. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  91. if (panel->wm_thread != RT_NULL)
  92. {
  93. /* notify to workbench */
  94. rtgui_thread_send(panel->wm_thread, &(event->parent), sizeof(struct rtgui_event_panel_show));
  95. }
  96. rtgui_panel_set_active_thread(panel, event->parent.sender);
  97. /* send all topwin clip info */
  98. rtgui_topwin_update_clip_to_panel(panel);
  99. /* send paint event */
  100. RTGUI_EVENT_PAINT_INIT(&epaint);
  101. epaint.wid = RT_NULL;
  102. rtgui_thread_send(event->parent.sender, (struct rtgui_event*)&epaint,
  103. sizeof(epaint));
  104. }
  105. else
  106. {
  107. /* send failed */
  108. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  109. }
  110. }
  111. void rtgui_server_thread_panel_hide(struct rtgui_event_panel_hide* event)
  112. {
  113. struct rtgui_panel* panel = event->panel;
  114. if (panel != RT_NULL)
  115. {
  116. rt_thread_t tid;
  117. /* send the responses */
  118. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_OK);
  119. if (panel->thread_list.next != RT_NULL)
  120. {
  121. struct rtgui_panel_thread* thread;
  122. thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
  123. /* remove it */
  124. panel->thread_list.next = thread->list.next;
  125. /* append to the tail of thread list */
  126. rtgui_list_append(&(panel->thread_list), &(thread->list));
  127. }
  128. /* send all topwin clip info */
  129. rtgui_topwin_update_clip_to_panel(panel);
  130. /* get new active thread */
  131. tid = rtgui_panel_get_active_thread(panel);
  132. if (tid != RT_NULL)
  133. {
  134. struct rtgui_event_paint epaint;
  135. /* send paint event */
  136. RTGUI_EVENT_PAINT_INIT(&epaint);
  137. epaint.wid = RT_NULL;
  138. rtgui_thread_send(tid, (struct rtgui_event*)&epaint, sizeof(epaint));
  139. }
  140. }
  141. else
  142. {
  143. /* send failed */
  144. rtgui_thread_ack(RTGUI_EVENT(event), RTGUI_STATUS_ERROR);
  145. }
  146. }
  147. void rtgui_server_handle_set_wm(struct rtgui_event_set_wm *event)
  148. {
  149. struct rtgui_panel* panel = rtgui_panel_find(event->panel_name);
  150. if (panel != RT_NULL)
  151. {
  152. rtgui_panel_set_wm(panel, event->parent.sender);
  153. }
  154. }
  155. void rtgui_server_handle_update(struct rtgui_event_update_end* event)
  156. {
  157. struct rtgui_graphic_driver* driver;
  158. driver = rtgui_graphic_driver_get_default();
  159. if (driver != RT_NULL)
  160. {
  161. rtgui_graphic_driver_screen_update(driver, &(event->rect));
  162. }
  163. }
  164. void rtgui_server_handle_monitor_add(struct rtgui_event_monitor* event)
  165. {
  166. if (event->panel != RT_NULL)
  167. {
  168. /* append monitor rect to panel list */
  169. rtgui_panel_append_monitor_rect(event->panel, event->parent.sender, &(event->rect));
  170. }
  171. else
  172. {
  173. /* add monitor rect to top window list */
  174. rtgui_topwin_append_monitor_rect(event->wid, &(event->rect));
  175. }
  176. }
  177. void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor* event)
  178. {
  179. if (event->panel != RT_NULL)
  180. {
  181. /* add monitor rect to panel list */
  182. rtgui_panel_remove_monitor_rect(event->panel, event->parent.sender, &(event->rect));
  183. }
  184. else
  185. {
  186. /* add monitor rect to top window list */
  187. rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect));
  188. }
  189. }
  190. void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse* event)
  191. {
  192. struct rtgui_topwin* wnd;
  193. struct rtgui_panel* panel;
  194. /* re-init to server thread */
  195. RTGUI_EVENT_MOUSE_BUTTON_INIT(event);
  196. #ifdef RTGUI_USING_WINMOVE
  197. if (rtgui_winrect_is_moved() &&
  198. event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP))
  199. {
  200. struct rtgui_topwin* topwin;
  201. rtgui_rect_t rect;
  202. if (rtgui_winrect_moved_done(&rect, &topwin) == RT_TRUE)
  203. {
  204. struct rtgui_event_win_move ewin;
  205. /* move window */
  206. RTGUI_EVENT_WIN_MOVE_INIT(&ewin);
  207. ewin.wid = topwin->wid;
  208. if (topwin->title != RT_NULL)
  209. {
  210. if (topwin->flag & WINTITLE_BORDER)
  211. {
  212. ewin.x = rect.x1 + WINTITLE_BORDER_SIZE;
  213. ewin.y = rect.y1 + WINTITLE_BORDER_SIZE;
  214. }
  215. if (!(topwin->flag & WINTITLE_NO)) ewin.y += WINTITLE_HEIGHT;
  216. }
  217. else
  218. {
  219. ewin.x = rect.x1;
  220. ewin.y = rect.y1;
  221. }
  222. /* send to client thread */
  223. rtgui_thread_send(topwin->tid, &(ewin.parent), sizeof(ewin));
  224. return;
  225. }
  226. }
  227. #endif
  228. /* get the wnd which contains the mouse */
  229. wnd = rtgui_topwin_get_wnd(event->x, event->y);
  230. if (wnd != RT_NULL)
  231. {
  232. event->wid = wnd->wid;
  233. if (rtgui_server_focus_topwin != wnd)
  234. {
  235. /* raise this window */
  236. rtgui_topwin_activate_win(wnd);
  237. rtgui_server_focus_panel = RT_NULL;
  238. }
  239. if (wnd->title != RT_NULL &&
  240. rtgui_rect_contains_point(&(RTGUI_WIDGET(wnd->title)->extent), event->x, event->y) == RT_EOK)
  241. {
  242. rtgui_topwin_title_onmouse(wnd, event);
  243. }
  244. else
  245. {
  246. /* send mouse event to thread */
  247. rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_mouse));
  248. }
  249. return ;
  250. }
  251. /* get the panel which contains the mouse */
  252. panel = rtgui_panel_get_contain(event->x, event->y);
  253. if ((panel != RT_NULL) && (panel->is_focusable == RT_TRUE))
  254. {
  255. /* deactivate old window */
  256. if (rtgui_server_focus_topwin != RT_NULL)
  257. {
  258. rtgui_topwin_deactivate_win(rtgui_server_focus_topwin);
  259. }
  260. rtgui_server_focus_panel = panel;
  261. rtgui_server_focus_topwin = RT_NULL;
  262. /* set destination window to null */
  263. event->wid = RT_NULL;
  264. /* send mouse event to thread */
  265. if (rtgui_panel_get_active_thread(panel) != RT_NULL)
  266. {
  267. rtgui_thread_send(rtgui_panel_get_active_thread(panel),
  268. (struct rtgui_event*)event,
  269. sizeof(struct rtgui_event_mouse));
  270. }
  271. }
  272. }
  273. static struct rtgui_panel* last_monitor_panel = RT_NULL;
  274. static struct rtgui_topwin* last_monitor_topwin = RT_NULL;
  275. void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse* event)
  276. {
  277. /* the topwin contains current mouse */
  278. struct rtgui_topwin* win = RT_NULL;
  279. struct rtgui_panel* panel = RT_NULL;
  280. /* re-init mouse event */
  281. RTGUI_EVENT_MOUSE_MOTION_INIT(event);
  282. /* find the panel or topwin which monitor the mouse motion */
  283. win = rtgui_topwin_get_wnd(event->x, event->y);
  284. if (win == RT_NULL)
  285. {
  286. /* try to find monitor on the panel */
  287. panel = rtgui_panel_get_contain(event->x, event->y);
  288. if (panel != RT_NULL)
  289. {
  290. struct rtgui_panel_thread* thread;
  291. /* get active panel thread */
  292. if (panel->thread_list.next != RT_NULL)
  293. {
  294. thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
  295. if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
  296. event->x, event->y) == RT_TRUE)
  297. {
  298. /* no monitor in this panel */
  299. panel = RT_NULL;
  300. }
  301. }
  302. }
  303. }
  304. else if (win->monitor_list.next != RT_NULL)
  305. {
  306. /* check whether the monitor exist */
  307. if (rtgui_mouse_monitor_contains_point(&(win->monitor_list), event->x, event->y) != RT_TRUE)
  308. {
  309. win = RT_NULL;
  310. /* try to find monitor on the panel */
  311. panel = rtgui_panel_get_contain(event->x, event->y);
  312. if (panel != RT_NULL)
  313. {
  314. struct rtgui_panel_thread* thread;
  315. /* get active panel thread */
  316. if (panel->thread_list.next != RT_NULL)
  317. {
  318. thread = rtgui_list_entry(panel->thread_list.next, struct rtgui_panel_thread, list);
  319. if (!rtgui_mouse_monitor_contains_point(&(thread->monitor_list),
  320. event->x, event->y) == RT_TRUE)
  321. {
  322. /* no monitor in this panel */
  323. panel = RT_NULL;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. else
  330. {
  331. win = RT_NULL;
  332. }
  333. /* check old panel or window */
  334. if (last_monitor_panel != RT_NULL)
  335. {
  336. rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
  337. /* send mouse motion event */
  338. if (tid != RT_NULL)
  339. {
  340. event->wid = RT_NULL;
  341. rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  342. }
  343. }
  344. else if (last_monitor_topwin != RT_NULL)
  345. {
  346. event->wid = last_monitor_topwin->wid;
  347. /* send mouse motion event */
  348. rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  349. }
  350. if (last_monitor_panel != panel)
  351. {
  352. last_monitor_panel = panel;
  353. if (last_monitor_panel != RT_NULL)
  354. {
  355. rt_thread_t tid = rtgui_panel_get_active_thread(last_monitor_panel);
  356. event->wid = RT_NULL;
  357. /* send mouse motion event */
  358. if (tid != RT_NULL)
  359. rtgui_thread_send(tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  360. }
  361. }
  362. if (last_monitor_topwin != win)
  363. {
  364. last_monitor_topwin = win;
  365. if (last_monitor_topwin != RT_NULL)
  366. {
  367. event->wid = last_monitor_topwin->wid;
  368. /* send mouse motion event */
  369. rtgui_thread_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  370. }
  371. }
  372. /* move mouse to (x, y) */
  373. rtgui_mouse_moveto(event->x, event->y);
  374. }
  375. void rtgui_server_handle_kbd(struct rtgui_event_kbd* event)
  376. {
  377. struct rtgui_topwin* wnd;
  378. struct rtgui_panel* panel;
  379. /* re-init to server thread */
  380. RTGUI_EVENT_KBD_INIT(event);
  381. /* todo: handle input method and global shortcut */
  382. /* send to focus window or focus panel */
  383. wnd = rtgui_server_focus_topwin;
  384. if (wnd != RT_NULL && wnd->flag & WINTITLE_ACTIVATE)
  385. {
  386. /* send to focus window */
  387. event->wid = wnd->wid;
  388. /* send keyboard event to thread */
  389. rtgui_thread_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
  390. return;
  391. }
  392. panel = rtgui_server_focus_panel;
  393. if (panel != RT_NULL)
  394. {
  395. rt_thread_t tid;
  396. /* get active thread in this panel */
  397. tid = rtgui_panel_get_active_thread(panel);
  398. if (tid != RT_NULL)
  399. {
  400. /* send to focus panel */
  401. event->wid = RT_NULL;
  402. /* send keyboard event to thread */
  403. rtgui_thread_send(tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
  404. }
  405. }
  406. }
  407. #ifdef __WIN32__
  408. #include <windows.h>
  409. #endif
  410. /**
  411. * rtgui server thread's entry
  412. */
  413. static void rtgui_server_entry(void* parameter)
  414. {
  415. #ifdef __WIN32__
  416. /* set the server thread to highest */
  417. HANDLE hCurrentThread = GetCurrentThread();
  418. SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
  419. #endif
  420. #ifdef RTGUI_USING_SMALL_SIZE
  421. /* create rtgui server msgq */
  422. rtgui_server_mq = rt_mq_create("rtgui",
  423. 32, 16, RT_IPC_FLAG_FIFO);
  424. #else
  425. /* create rtgui server msgq */
  426. rtgui_server_mq = rt_mq_create("rtgui",
  427. 256, 8, RT_IPC_FLAG_FIFO);
  428. #endif
  429. /* register rtgui server thread */
  430. rtgui_thread_register(rtgui_server_tid, rtgui_server_mq);
  431. /* init mouse and show */
  432. rtgui_mouse_init();
  433. #ifdef RTGUI_USING_MOUSE_CURSOR
  434. rtgui_mouse_show_cursor();
  435. #endif
  436. while (1)
  437. {
  438. /* the buffer uses to receive event */
  439. #ifdef RTGUI_USING_SMALL_SIZE
  440. char event_buf[64];
  441. #else
  442. char event_buf[256];
  443. #endif
  444. struct rtgui_event* event = (struct rtgui_event*)&(event_buf[0]);
  445. if (rtgui_thread_recv(event, sizeof(event_buf)) == RT_EOK)
  446. {
  447. /* dispatch event */
  448. switch (event->type)
  449. {
  450. /* panel event */
  451. case RTGUI_EVENT_PANEL_ATTACH:
  452. /* register an application in panel */
  453. rtgui_server_create_application((struct rtgui_event_panel_attach*)event);
  454. break;
  455. case RTGUI_EVENT_PANEL_DETACH:
  456. /* unregister an application */
  457. rtgui_server_destroy_application((struct rtgui_event_panel_detach*)event);
  458. break;
  459. case RTGUI_EVENT_PANEL_SHOW:
  460. /* handle raise an application */
  461. rtgui_server_thread_panel_show((struct rtgui_event_panel_show*)event);
  462. break;
  463. case RTGUI_EVENT_PANEL_HIDE:
  464. /* handle hide an application */
  465. rtgui_server_thread_panel_hide((struct rtgui_event_panel_hide*)event);
  466. break;
  467. case RTGUI_EVENT_SET_WM:
  468. /* handle set workbench manager event */
  469. rtgui_server_handle_set_wm((struct rtgui_event_set_wm*)event);
  470. break;
  471. /* window event */
  472. case RTGUI_EVENT_WIN_CREATE:
  473. rtgui_thread_ack(event, RTGUI_STATUS_OK);
  474. rtgui_topwin_add((struct rtgui_event_win_create*)event);
  475. break;
  476. case RTGUI_EVENT_WIN_DESTROY:
  477. if (rtgui_topwin_remove(((struct rtgui_event_win*)event)->wid) == RT_EOK)
  478. rtgui_thread_ack(event, RTGUI_STATUS_OK);
  479. else
  480. rtgui_thread_ack(event, RTGUI_STATUS_ERROR);
  481. break;
  482. case RTGUI_EVENT_WIN_SHOW:
  483. rtgui_topwin_show((struct rtgui_event_win*)event);
  484. break;
  485. case RTGUI_EVENT_WIN_HIDE:
  486. rtgui_topwin_hide((struct rtgui_event_win*)event);
  487. break;
  488. case RTGUI_EVENT_WIN_MOVE:
  489. rtgui_topwin_move((struct rtgui_event_win_move*)event);
  490. break;
  491. case RTGUI_EVENT_WIN_RESIZE:
  492. rtgui_topwin_resize(((struct rtgui_event_win_resize*)event)->wid,
  493. &(((struct rtgui_event_win_resize*)event)->rect));
  494. break;
  495. /* other event */
  496. case RTGUI_EVENT_UPDATE_BEGIN:
  497. #ifdef RTGUI_USING_MOUSE_CURSOR
  498. /* hide cursor */
  499. rtgui_mouse_hide_cursor();
  500. #endif
  501. break;
  502. case RTGUI_EVENT_UPDATE_END:
  503. /* handle screen update */
  504. rtgui_server_handle_update((struct rtgui_event_update_end*)event);
  505. #ifdef RTGUI_USING_MOUSE_CURSOR
  506. /* show cursor */
  507. rtgui_mouse_show_cursor();
  508. #endif
  509. break;
  510. case RTGUI_EVENT_MONITOR_ADD:
  511. /* handle mouse monitor */
  512. rtgui_server_handle_monitor_add((struct rtgui_event_monitor*)event);
  513. break;
  514. /* mouse and keyboard event */
  515. case RTGUI_EVENT_MOUSE_MOTION:
  516. /* handle mouse motion event */
  517. rtgui_server_handle_mouse_motion((struct rtgui_event_mouse*)event);
  518. break;
  519. case RTGUI_EVENT_MOUSE_BUTTON:
  520. /* handle mouse button */
  521. rtgui_server_handle_mouse_btn((struct rtgui_event_mouse*)event);
  522. break;
  523. case RTGUI_EVENT_KBD:
  524. /* handle keyboard event */
  525. rtgui_server_handle_kbd((struct rtgui_event_kbd*)event);
  526. break;
  527. case RTGUI_EVENT_COMMAND:
  528. break;
  529. }
  530. }
  531. }
  532. /* unregister in rtgui thread */
  533. // rtgui_thread_deregister(rt_thread_self());
  534. }
  535. void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size)
  536. {
  537. rt_mq_send(rtgui_server_mq, event, size);
  538. }
  539. void rtgui_server_init()
  540. {
  541. rtgui_server_tid = rt_thread_create("rtgui",
  542. rtgui_server_entry, RT_NULL,
  543. RTGUI_SVR_THREAD_STACK_SIZE,
  544. RTGUI_SVR_THREAD_PRIORITY,
  545. RTGUI_SVR_THREAD_TIMESLICE);
  546. /* start rtgui server thread */
  547. if (rtgui_server_tid != RT_NULL)
  548. rt_thread_startup(rtgui_server_tid);
  549. }