server.c 16 KB

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