server.c 16 KB

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