server.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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/rtgui_object.h>
  18. #include <rtgui/rtgui_app.h>
  19. #include <rtgui/driver.h>
  20. #include "mouse.h"
  21. #include "topwin.h"
  22. static struct rt_thread *rtgui_server_tid;
  23. static struct rtgui_app *rtgui_server_application = RT_NULL;
  24. static struct rtgui_app *rtgui_wm_application = RT_NULL;
  25. void rtgui_server_handle_update(struct rtgui_event_update_end* event)
  26. {
  27. struct rtgui_graphic_driver* driver;
  28. driver = rtgui_graphic_driver_get_default();
  29. if (driver != RT_NULL)
  30. {
  31. rtgui_graphic_driver_screen_update(driver, &(event->rect));
  32. }
  33. }
  34. void rtgui_server_handle_monitor_add(struct rtgui_event_monitor* event)
  35. {
  36. /* add monitor rect to top window list */
  37. rtgui_topwin_append_monitor_rect(event->wid, &(event->rect));
  38. }
  39. void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor* event)
  40. {
  41. /* add monitor rect to top window list */
  42. rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect));
  43. }
  44. void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse* event)
  45. {
  46. struct rtgui_topwin* wnd;
  47. /* re-init to server thread */
  48. RTGUI_EVENT_MOUSE_BUTTON_INIT(event);
  49. #ifdef RTGUI_USING_WINMOVE
  50. if (rtgui_winrect_is_moved() &&
  51. event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP))
  52. {
  53. struct rtgui_topwin* topwin;
  54. rtgui_rect_t rect;
  55. if (rtgui_winrect_moved_done(&rect, &topwin) == RT_TRUE)
  56. {
  57. struct rtgui_event_win_move ewin;
  58. /* move window */
  59. RTGUI_EVENT_WIN_MOVE_INIT(&ewin);
  60. ewin.wid = topwin->wid;
  61. if (topwin->title != RT_NULL)
  62. {
  63. if (topwin->flag & WINTITLE_BORDER)
  64. {
  65. ewin.x = rect.x1 + WINTITLE_BORDER_SIZE;
  66. ewin.y = rect.y1 + WINTITLE_BORDER_SIZE;
  67. }
  68. if (!(topwin->flag & WINTITLE_NO)) ewin.y += WINTITLE_HEIGHT;
  69. }
  70. else
  71. {
  72. ewin.x = rect.x1;
  73. ewin.y = rect.y1;
  74. }
  75. /* send to client thread */
  76. rtgui_send(topwin->tid, &(ewin.parent), sizeof(ewin));
  77. return;
  78. }
  79. }
  80. #endif
  81. /* get the wnd which contains the mouse */
  82. wnd = rtgui_topwin_get_wnd_no_modaled(event->x, event->y);
  83. if (wnd != RT_NULL)
  84. {
  85. event->wid = wnd->wid;
  86. if (rtgui_topwin_get_focus() != wnd)
  87. {
  88. /* raise this window */
  89. rtgui_topwin_activate_topwin(wnd);
  90. }
  91. if (wnd->title != RT_NULL &&
  92. rtgui_rect_contains_point(&(RTGUI_WIDGET(wnd->title)->extent), event->x, event->y) == RT_EOK)
  93. {
  94. rtgui_topwin_title_onmouse(wnd, event);
  95. }
  96. else
  97. {
  98. /* send mouse event to thread */
  99. rtgui_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_mouse));
  100. }
  101. return ;
  102. }
  103. }
  104. static struct rtgui_topwin* last_monitor_topwin = RT_NULL;
  105. void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse* event)
  106. {
  107. /* the topwin contains current mouse */
  108. struct rtgui_topwin* win = RT_NULL;
  109. /* re-init mouse event */
  110. RTGUI_EVENT_MOUSE_MOTION_INIT(event);
  111. win = rtgui_topwin_get_wnd_no_modaled(event->x, event->y);
  112. if (win != RT_NULL && win->monitor_list.next != RT_NULL)
  113. {
  114. // FIXME:
  115. /* check whether the monitor exist */
  116. if (rtgui_mouse_monitor_contains_point(&(win->monitor_list),
  117. event->x, event->y) != RT_TRUE)
  118. {
  119. win = RT_NULL;
  120. }
  121. }
  122. if (last_monitor_topwin != RT_NULL)
  123. {
  124. event->wid = last_monitor_topwin->wid;
  125. /* send mouse motion event */
  126. rtgui_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  127. }
  128. if (last_monitor_topwin != win)
  129. {
  130. last_monitor_topwin = win;
  131. if (last_monitor_topwin != RT_NULL)
  132. {
  133. event->wid = last_monitor_topwin->wid;
  134. /* send mouse motion event */
  135. rtgui_send(last_monitor_topwin->tid, &(event->parent), sizeof(struct rtgui_event_mouse));
  136. }
  137. }
  138. /* move mouse to (x, y) */
  139. rtgui_mouse_moveto(event->x, event->y);
  140. }
  141. void rtgui_server_handle_kbd(struct rtgui_event_kbd* event)
  142. {
  143. struct rtgui_topwin* wnd;
  144. /* re-init to server thread */
  145. RTGUI_EVENT_KBD_INIT(event);
  146. /* todo: handle input method and global shortcut */
  147. wnd = rtgui_topwin_get_focus();
  148. if (wnd != RT_NULL)
  149. {
  150. RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
  151. /* send to focus window */
  152. event->wid = wnd->wid;
  153. /* send keyboard event to thread */
  154. rtgui_send(wnd->tid, (struct rtgui_event*)event, sizeof(struct rtgui_event_kbd));
  155. return;
  156. }
  157. }
  158. #ifdef _WIN32
  159. #include <windows.h>
  160. #endif
  161. static rt_bool_t rtgui_server_event_handler(struct rtgui_object *object,
  162. struct rtgui_event *event)
  163. {
  164. RT_ASSERT(object != RT_NULL);
  165. RT_ASSERT(event != RT_NULL);
  166. /* dispatch event */
  167. switch (event->type)
  168. {
  169. case RTGUI_EVENT_APP_CREATE:
  170. case RTGUI_EVENT_APP_DESTROY:
  171. if (rtgui_wm_application != RT_NULL)
  172. {
  173. /* forward event to wm application */
  174. rtgui_send(rtgui_wm_application->tid, event, sizeof(struct rtgui_event_application));
  175. }
  176. else
  177. {
  178. /* always ack with OK */
  179. rtgui_ack(event, RTGUI_STATUS_OK);
  180. }
  181. break;
  182. /* mouse and keyboard event */
  183. case RTGUI_EVENT_MOUSE_MOTION:
  184. /* handle mouse motion event */
  185. rtgui_server_handle_mouse_motion((struct rtgui_event_mouse*)event);
  186. break;
  187. case RTGUI_EVENT_MOUSE_BUTTON:
  188. /* handle mouse button */
  189. rtgui_server_handle_mouse_btn((struct rtgui_event_mouse*)event);
  190. break;
  191. case RTGUI_EVENT_KBD:
  192. /* handle keyboard event */
  193. rtgui_server_handle_kbd((struct rtgui_event_kbd*)event);
  194. break;
  195. /* window event */
  196. case RTGUI_EVENT_WIN_CREATE:
  197. if (rtgui_topwin_add((struct rtgui_event_win_create*)event) == RT_EOK)
  198. rtgui_ack(event, RTGUI_STATUS_OK);
  199. else
  200. rtgui_ack(event, RTGUI_STATUS_ERROR);
  201. break;
  202. case RTGUI_EVENT_WIN_SHOW:
  203. if (rtgui_topwin_show((struct rtgui_event_win*)event) == RT_EOK)
  204. rtgui_ack(event, RTGUI_STATUS_OK);
  205. else
  206. rtgui_ack(event, RTGUI_STATUS_ERROR);
  207. break;
  208. case RTGUI_EVENT_WIN_HIDE:
  209. if (rtgui_topwin_hide((struct rtgui_event_win*)event) == RT_EOK)
  210. rtgui_ack(event, RTGUI_STATUS_OK);
  211. else
  212. rtgui_ack(event, RTGUI_STATUS_ERROR);
  213. break;
  214. case RTGUI_EVENT_WIN_MOVE:
  215. if (rtgui_topwin_move((struct rtgui_event_win_move*)event) == RT_EOK)
  216. rtgui_ack(event, RTGUI_STATUS_OK);
  217. else
  218. rtgui_ack(event, RTGUI_STATUS_ERROR);
  219. break;
  220. case RTGUI_EVENT_WIN_MODAL_ENTER:
  221. if (rtgui_topwin_modal_enter((struct rtgui_event_win_modal_enter*)event) == RT_EOK)
  222. rtgui_ack(event, RTGUI_STATUS_OK);
  223. else
  224. rtgui_ack(event, RTGUI_STATUS_ERROR);
  225. break;
  226. case RTGUI_EVENT_WIN_ACTIVATE:
  227. if (rtgui_topwin_activate((struct rtgui_event_win_activate*)event) == RT_EOK)
  228. rtgui_ack(event, RTGUI_STATUS_OK);
  229. else
  230. rtgui_ack(event, RTGUI_STATUS_ERROR);
  231. break;
  232. case RTGUI_EVENT_WIN_DESTROY:
  233. if (last_monitor_topwin != RT_NULL &&
  234. last_monitor_topwin->wid == ((struct rtgui_event_win*)event)->wid)
  235. last_monitor_topwin = RT_NULL;
  236. if (rtgui_topwin_remove(((struct rtgui_event_win*)event)->wid) == RT_EOK)
  237. rtgui_ack(event, RTGUI_STATUS_OK);
  238. else
  239. rtgui_ack(event, RTGUI_STATUS_ERROR);
  240. break;
  241. case RTGUI_EVENT_WIN_RESIZE:
  242. rtgui_topwin_resize(((struct rtgui_event_win_resize*)event)->wid,
  243. &(((struct rtgui_event_win_resize*)event)->rect));
  244. break;
  245. case RTGUI_EVENT_SET_WM:
  246. if (rtgui_wm_application != RT_NULL)
  247. {
  248. rtgui_ack(event, RTGUI_STATUS_ERROR);
  249. }
  250. else
  251. {
  252. struct rtgui_event_set_wm *set_wm;
  253. set_wm = (struct rtgui_event_set_wm*) event;
  254. rtgui_wm_application = set_wm->app;
  255. rtgui_ack(event, RTGUI_STATUS_OK);
  256. }
  257. break;
  258. /* other event */
  259. case RTGUI_EVENT_COMMAND:
  260. break;
  261. case RTGUI_EVENT_UPDATE_BEGIN:
  262. #ifdef RTGUI_USING_MOUSE_CURSOR
  263. /* hide cursor */
  264. rtgui_mouse_hide_cursor();
  265. #endif
  266. break;
  267. case RTGUI_EVENT_UPDATE_END:
  268. /* handle screen update */
  269. rtgui_server_handle_update((struct rtgui_event_update_end*)event);
  270. #ifdef RTGUI_USING_MOUSE_CURSOR
  271. /* show cursor */
  272. rtgui_mouse_show_cursor();
  273. #endif
  274. break;
  275. case RTGUI_EVENT_MONITOR_ADD:
  276. /* handle mouse monitor */
  277. rtgui_server_handle_monitor_add((struct rtgui_event_monitor*)event);
  278. break;
  279. }
  280. return RT_TRUE;
  281. }
  282. /**
  283. * rtgui server thread's entry
  284. */
  285. static void rtgui_server_entry(void* parameter)
  286. {
  287. #ifdef _WIN32
  288. /* set the server thread to highest */
  289. HANDLE hCurrentThread = GetCurrentThread();
  290. SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
  291. #endif
  292. /* create rtgui server application */
  293. rtgui_server_application = rtgui_app_create(rtgui_server_tid,
  294. "rtgui");
  295. if (rtgui_server_application == RT_NULL)
  296. return;
  297. rtgui_object_set_event_handler(RTGUI_OBJECT(rtgui_server_application),
  298. rtgui_server_event_handler);
  299. /* init mouse and show */
  300. rtgui_mouse_init();
  301. #ifdef RTGUI_USING_MOUSE_CURSOR
  302. rtgui_mouse_show_cursor();
  303. #endif
  304. rtgui_app_run(rtgui_server_application);
  305. rtgui_app_destroy(rtgui_server_application);
  306. rtgui_server_application = RT_NULL;
  307. }
  308. void rtgui_server_post_event(struct rtgui_event* event, rt_size_t size)
  309. {
  310. if (rtgui_server_tid != RT_NULL)
  311. rtgui_send(rtgui_server_tid, event, size);
  312. else
  313. rt_kprintf("post when server is not running\n");
  314. }
  315. rt_err_t rtgui_server_post_event_sync(struct rtgui_event* event, rt_size_t size)
  316. {
  317. if (rtgui_server_tid != RT_NULL)
  318. return rtgui_send_sync(rtgui_server_tid, event, size);
  319. else
  320. {
  321. rt_kprintf("post when server is not running\n");
  322. return -RT_ENOSYS;
  323. }
  324. }
  325. void rtgui_server_init(void)
  326. {
  327. if (rtgui_server_tid != RT_NULL)
  328. return;
  329. rtgui_server_tid = rt_thread_create("rtgui",
  330. rtgui_server_entry, RT_NULL,
  331. RTGUI_SVR_THREAD_STACK_SIZE,
  332. RTGUI_SVR_THREAD_PRIORITY,
  333. RTGUI_SVR_THREAD_TIMESLICE);
  334. /* start rtgui server thread */
  335. if (rtgui_server_tid != RT_NULL)
  336. rt_thread_startup(rtgui_server_tid);
  337. }