server.c 9.5 KB

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