server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * File : server.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-04 Bernard first version
  23. */
  24. #include <rtgui/rtgui.h>
  25. #include <rtgui/event.h>
  26. #include <rtgui/rtgui_system.h>
  27. #include <rtgui/rtgui_object.h>
  28. #include <rtgui/rtgui_app.h>
  29. #include <rtgui/driver.h>
  30. //#include <rtgui/touch.h>
  31. #include <rtgui/widgets/window.h>
  32. #include "mouse.h"
  33. #include "topwin.h"
  34. static struct rtgui_app *rtgui_server_app = RT_NULL;
  35. static struct rtgui_app *rtgui_wm_application = RT_NULL;
  36. static void (*_show_win_hook)(void);
  37. static void (*_act_win_hook)(void);
  38. void rtgui_server_install_show_win_hook(void (*hk)(void))
  39. {
  40. _show_win_hook = hk;
  41. }
  42. void rtgui_server_install_act_win_hook(void (*hk)(void))
  43. {
  44. _act_win_hook = hk;
  45. }
  46. void rtgui_server_handle_update(struct rtgui_event_update_end *event)
  47. {
  48. struct rtgui_graphic_driver *driver;
  49. driver = rtgui_graphic_driver_get_default();
  50. if (driver != RT_NULL)
  51. {
  52. rtgui_graphic_driver_screen_update(driver, &(event->rect));
  53. }
  54. }
  55. void rtgui_server_handle_monitor_add(struct rtgui_event_monitor *event)
  56. {
  57. /* add monitor rect to top window list */
  58. rtgui_topwin_append_monitor_rect(event->wid, &(event->rect));
  59. }
  60. void rtgui_server_handle_monitor_remove(struct rtgui_event_monitor *event)
  61. {
  62. /* add monitor rect to top window list */
  63. rtgui_topwin_remove_monitor_rect(event->wid, &(event->rect));
  64. }
  65. void rtgui_server_handle_mouse_btn(struct rtgui_event_mouse *event)
  66. {
  67. struct rtgui_topwin *wnd;
  68. /* re-init to server thread */
  69. RTGUI_EVENT_MOUSE_BUTTON_INIT(event);
  70. /* set cursor position */
  71. rtgui_mouse_set_position(event->x, event->y);
  72. #ifdef RTGUI_USING_WINMOVE
  73. if (rtgui_winrect_is_moved() &&
  74. event->button & (RTGUI_MOUSE_BUTTON_LEFT | RTGUI_MOUSE_BUTTON_UP))
  75. {
  76. struct rtgui_win *win;
  77. rtgui_rect_t rect;
  78. if (rtgui_winrect_moved_done(&rect, &win) == RT_TRUE)
  79. {
  80. struct rtgui_event_win_move ewin;
  81. /* move window */
  82. RTGUI_EVENT_WIN_MOVE_INIT(&ewin);
  83. ewin.wid = win;
  84. ewin.x = rect.x1;
  85. ewin.y = rect.y1;
  86. /* send to client thread */
  87. rtgui_send(win->app, &(ewin.parent), sizeof(ewin));
  88. return;
  89. }
  90. }
  91. #endif
  92. /* get the wnd which contains the mouse */
  93. wnd = rtgui_topwin_get_wnd_no_modaled(event->x, event->y);
  94. if (wnd == RT_NULL)
  95. return;
  96. event->wid = wnd->wid;
  97. event->win_acti_cnt = rtgui_app_get_win_acti_cnt();
  98. /* only raise window if the button is pressed down */
  99. if (event->button & RTGUI_MOUSE_BUTTON_DOWN &&
  100. rtgui_topwin_get_focus() != wnd)
  101. {
  102. rtgui_topwin_activate_topwin(wnd);
  103. }
  104. /* send mouse event to thread */
  105. while (rtgui_send(wnd->app, (struct rtgui_event *)event, sizeof(struct rtgui_event_mouse)) != RT_EOK)
  106. {
  107. rt_thread_delay(RT_TICK_PER_SECOND / 50);
  108. }
  109. }
  110. void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse *event)
  111. {
  112. /* the topwin contains current mouse */
  113. struct rtgui_topwin *win = RT_NULL;
  114. /* re-init mouse event */
  115. RTGUI_EVENT_MOUSE_MOTION_INIT(event);
  116. win = rtgui_topwin_get_wnd_no_modaled(event->x, event->y);
  117. if (win != RT_NULL && win->monitor_list.next != RT_NULL)
  118. {
  119. // FIXME:
  120. /* check whether the monitor exist */
  121. if (rtgui_mouse_monitor_contains_point(&(win->monitor_list),
  122. event->x, event->y) != RT_TRUE)
  123. {
  124. win = RT_NULL;
  125. }
  126. }
  127. if (win)
  128. {
  129. event->wid = win->wid;
  130. event->win_acti_cnt = rtgui_app_get_win_acti_cnt();
  131. rtgui_send(win->wid->app, &(event->parent), sizeof(*event));
  132. }
  133. /* move mouse to (x, y) */
  134. rtgui_mouse_moveto(event->x, event->y);
  135. }
  136. void rtgui_server_handle_kbd(struct rtgui_event_kbd *event)
  137. {
  138. struct rtgui_topwin *wnd;
  139. /* re-init to server thread */
  140. RTGUI_EVENT_KBD_INIT(event);
  141. /* todo: handle input method and global shortcut */
  142. wnd = rtgui_topwin_get_focus();
  143. if (wnd != RT_NULL)
  144. {
  145. RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
  146. /* send to focus window */
  147. event->wid = wnd->wid;
  148. event->win_acti_cnt = rtgui_app_get_win_acti_cnt();
  149. /* send keyboard event to thread */
  150. rtgui_send(wnd->app, (struct rtgui_event *)event, sizeof(struct rtgui_event_kbd));
  151. return;
  152. }
  153. }
  154. void rtgui_server_handle_touch(struct rtgui_event_touch *event)
  155. {
  156. // if (rtgui_touch_do_calibration(event) == RT_TRUE)
  157. // {
  158. // struct rtgui_event_mouse emouse;
  159. // /* convert it as a mouse event to rtgui */
  160. // if (event->up_down == RTGUI_TOUCH_MOTION)
  161. // {
  162. // RTGUI_EVENT_MOUSE_MOTION_INIT(&emouse);
  163. // emouse.x = event->x;
  164. // emouse.y = event->y;
  165. // emouse.button = 0;
  166. // rtgui_server_handle_mouse_motion(&emouse);
  167. // }
  168. // else
  169. // {
  170. // RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  171. // emouse.x = event->x;
  172. // emouse.y = event->y;
  173. // emouse.button = RTGUI_MOUSE_BUTTON_LEFT;
  174. // if (event->up_down == RTGUI_TOUCH_UP)
  175. // emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  176. // else
  177. // emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  178. // rtgui_server_handle_mouse_btn(&emouse);
  179. // }
  180. // }
  181. }
  182. #ifdef _WIN32_NATIVE
  183. #include <windows.h>
  184. #endif
  185. static rt_bool_t rtgui_server_event_handler(struct rtgui_object *object,
  186. struct rtgui_event *event)
  187. {
  188. RT_ASSERT(object != RT_NULL);
  189. RT_ASSERT(event != RT_NULL);
  190. /* dispatch event */
  191. switch (event->type)
  192. {
  193. case RTGUI_EVENT_APP_CREATE:
  194. case RTGUI_EVENT_APP_DESTROY:
  195. if (rtgui_wm_application != RT_NULL)
  196. {
  197. /* forward event to wm application */
  198. rtgui_send(rtgui_wm_application, event, sizeof(struct rtgui_event_application));
  199. }
  200. else
  201. {
  202. /* always ack with OK */
  203. rtgui_ack(event, RTGUI_STATUS_OK);
  204. }
  205. break;
  206. /* mouse and keyboard event */
  207. case RTGUI_EVENT_MOUSE_MOTION:
  208. /* handle mouse motion event */
  209. rtgui_server_handle_mouse_motion((struct rtgui_event_mouse *)event);
  210. break;
  211. case RTGUI_EVENT_MOUSE_BUTTON:
  212. /* handle mouse button */
  213. rtgui_server_handle_mouse_btn((struct rtgui_event_mouse *)event);
  214. break;
  215. case RTGUI_EVENT_TOUCH:
  216. /* handle touch event */
  217. rtgui_server_handle_touch((struct rtgui_event_touch *)event);
  218. break;
  219. case RTGUI_EVENT_KBD:
  220. /* handle keyboard event */
  221. rtgui_server_handle_kbd((struct rtgui_event_kbd *)event);
  222. break;
  223. /* window event */
  224. case RTGUI_EVENT_WIN_CREATE:
  225. if (rtgui_topwin_add((struct rtgui_event_win_create *)event) == RT_EOK)
  226. rtgui_ack(event, RTGUI_STATUS_OK);
  227. else
  228. rtgui_ack(event, RTGUI_STATUS_ERROR);
  229. break;
  230. case RTGUI_EVENT_WIN_SHOW:
  231. if (_show_win_hook) _show_win_hook();
  232. if (rtgui_topwin_show((struct rtgui_event_win *)event) == RT_EOK)
  233. rtgui_ack(event, RTGUI_STATUS_OK);
  234. else
  235. rtgui_ack(event, RTGUI_STATUS_ERROR);
  236. break;
  237. case RTGUI_EVENT_WIN_HIDE:
  238. if (rtgui_topwin_hide((struct rtgui_event_win *)event) == 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_MOVE:
  244. if (rtgui_topwin_move((struct rtgui_event_win_move *)event) == RT_EOK)
  245. rtgui_ack(event, RTGUI_STATUS_OK);
  246. else
  247. rtgui_ack(event, RTGUI_STATUS_ERROR);
  248. break;
  249. case RTGUI_EVENT_WIN_MODAL_ENTER:
  250. if (rtgui_topwin_modal_enter((struct rtgui_event_win_modal_enter *)event) == RT_EOK)
  251. rtgui_ack(event, RTGUI_STATUS_OK);
  252. else
  253. rtgui_ack(event, RTGUI_STATUS_ERROR);
  254. break;
  255. case RTGUI_EVENT_WIN_ACTIVATE:
  256. if (_act_win_hook) _act_win_hook();
  257. if (rtgui_topwin_activate((struct rtgui_event_win_activate *)event) == RT_EOK)
  258. rtgui_ack(event, RTGUI_STATUS_OK);
  259. else
  260. rtgui_ack(event, RTGUI_STATUS_ERROR);
  261. break;
  262. case RTGUI_EVENT_WIN_DESTROY:
  263. if (rtgui_topwin_remove(((struct rtgui_event_win *)event)->wid) == RT_EOK)
  264. rtgui_ack(event, RTGUI_STATUS_OK);
  265. else
  266. rtgui_ack(event, RTGUI_STATUS_ERROR);
  267. break;
  268. case RTGUI_EVENT_WIN_RESIZE:
  269. rtgui_topwin_resize(((struct rtgui_event_win_resize *)event)->wid,
  270. &(((struct rtgui_event_win_resize *)event)->rect));
  271. break;
  272. case RTGUI_EVENT_SET_WM:
  273. if (rtgui_wm_application != RT_NULL)
  274. {
  275. rtgui_ack(event, RTGUI_STATUS_ERROR);
  276. }
  277. else
  278. {
  279. struct rtgui_event_set_wm *set_wm;
  280. set_wm = (struct rtgui_event_set_wm *) event;
  281. rtgui_wm_application = set_wm->app;
  282. rtgui_ack(event, RTGUI_STATUS_OK);
  283. }
  284. break;
  285. /* other event */
  286. case RTGUI_EVENT_COMMAND:
  287. break;
  288. case RTGUI_EVENT_UPDATE_BEGIN:
  289. #ifdef RTGUI_USING_MOUSE_CURSOR
  290. /* hide cursor */
  291. rtgui_mouse_hide_cursor();
  292. #endif
  293. break;
  294. case RTGUI_EVENT_UPDATE_END:
  295. /* handle screen update */
  296. rtgui_server_handle_update((struct rtgui_event_update_end *)event);
  297. #ifdef RTGUI_USING_MOUSE_CURSOR
  298. /* show cursor */
  299. rtgui_mouse_show_cursor();
  300. #endif
  301. break;
  302. case RTGUI_EVENT_MONITOR_ADD:
  303. /* handle mouse monitor */
  304. rtgui_server_handle_monitor_add((struct rtgui_event_monitor *)event);
  305. break;
  306. default:
  307. rt_kprintf("RTGUI: wrong event sent to server: %d\n", event->type);
  308. return RT_FALSE;
  309. }
  310. return RT_TRUE;
  311. }
  312. /**
  313. * rtgui server thread's entry
  314. */
  315. static void rtgui_server_entry(void *parameter)
  316. {
  317. #ifdef _WIN32_NATIVE
  318. /* set the server thread to highest */
  319. HANDLE hCurrentThread = GetCurrentThread();
  320. SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
  321. #endif
  322. /* create rtgui server application */
  323. rtgui_server_app = rtgui_app_create("rtgui");
  324. if (rtgui_server_app == RT_NULL)
  325. {
  326. rt_kprintf("Create GUI server failed.\n");
  327. return;
  328. }
  329. rtgui_object_set_event_handler(RTGUI_OBJECT(rtgui_server_app),
  330. rtgui_server_event_handler);
  331. /* init mouse and show */
  332. rtgui_mouse_init();
  333. #ifdef RTGUI_USING_MOUSE_CURSOR
  334. rtgui_mouse_show_cursor();
  335. #endif
  336. rtgui_app_run(rtgui_server_app);
  337. rtgui_app_destroy(rtgui_server_app);
  338. rtgui_server_app = RT_NULL;
  339. }
  340. rt_err_t rtgui_server_post_event(struct rtgui_event *event, rt_size_t size)
  341. {
  342. rt_err_t result;
  343. if (rtgui_server_app != RT_NULL)
  344. {
  345. result = rtgui_send(rtgui_server_app, event, size);
  346. }
  347. else
  348. {
  349. rt_kprintf("post when server is not running\n");
  350. result = -RT_ENOSYS;
  351. }
  352. return result;
  353. }
  354. rt_err_t rtgui_server_post_event_sync(struct rtgui_event *event, rt_size_t size)
  355. {
  356. if (rtgui_server_app != RT_NULL)
  357. return rtgui_send_sync(rtgui_server_app, event, size);
  358. else
  359. {
  360. rt_kprintf("post when server is not running\n");
  361. return -RT_ENOSYS;
  362. }
  363. }
  364. struct rtgui_app* rtgui_get_server(void)
  365. {
  366. return rtgui_server_app;
  367. }
  368. RTM_EXPORT(rtgui_get_server);
  369. void rtgui_server_init(void)
  370. {
  371. rt_thread_t tid;
  372. tid = rt_thread_create("rtgui",
  373. rtgui_server_entry, RT_NULL,
  374. RTGUI_SVR_THREAD_STACK_SIZE,
  375. RTGUI_SVR_THREAD_PRIORITY,
  376. RTGUI_SVR_THREAD_TIMESLICE);
  377. /* start rtgui server thread */
  378. if (tid != RT_NULL)
  379. rt_thread_startup(tid);
  380. }