server.c 16 KB

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