server.c 12 KB

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