server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. /* only raise window if the button is pressed down */
  98. if (event->button & RTGUI_MOUSE_BUTTON_DOWN &&
  99. rtgui_topwin_get_focus() != wnd)
  100. {
  101. rtgui_topwin_activate_topwin(wnd);
  102. }
  103. /* send mouse event to thread */
  104. rtgui_send(wnd->app,
  105. (struct rtgui_event *)event,
  106. sizeof(struct rtgui_event_mouse));
  107. }
  108. void rtgui_server_handle_mouse_motion(struct rtgui_event_mouse *event)
  109. {
  110. /* the topwin contains current mouse */
  111. struct rtgui_topwin *win = RT_NULL;
  112. /* re-init mouse event */
  113. RTGUI_EVENT_MOUSE_MOTION_INIT(event);
  114. win = rtgui_topwin_get_wnd_no_modaled(event->x, event->y);
  115. if (win != RT_NULL && win->monitor_list.next != RT_NULL)
  116. {
  117. // FIXME:
  118. /* check whether the monitor exist */
  119. if (rtgui_mouse_monitor_contains_point(&(win->monitor_list),
  120. event->x, event->y) != RT_TRUE)
  121. {
  122. win = RT_NULL;
  123. }
  124. }
  125. if (win)
  126. {
  127. event->wid = win->wid;
  128. rtgui_send(win->wid->app, &(event->parent), sizeof(*event));
  129. }
  130. /* move mouse to (x, y) */
  131. rtgui_mouse_moveto(event->x, event->y);
  132. }
  133. void rtgui_server_handle_kbd(struct rtgui_event_kbd *event)
  134. {
  135. struct rtgui_topwin *wnd;
  136. /* re-init to server thread */
  137. RTGUI_EVENT_KBD_INIT(event);
  138. /* todo: handle input method and global shortcut */
  139. wnd = rtgui_topwin_get_focus();
  140. if (wnd != RT_NULL)
  141. {
  142. RT_ASSERT(wnd->flag & WINTITLE_ACTIVATE)
  143. /* send to focus window */
  144. event->wid = wnd->wid;
  145. /* send keyboard event to thread */
  146. rtgui_send(wnd->app, (struct rtgui_event *)event, sizeof(struct rtgui_event_kbd));
  147. return;
  148. }
  149. }
  150. void rtgui_server_handle_touch(struct rtgui_event_touch *event)
  151. {
  152. // if (rtgui_touch_do_calibration(event) == RT_TRUE)
  153. // {
  154. // struct rtgui_event_mouse emouse;
  155. // /* convert it as a mouse event to rtgui */
  156. // if (event->up_down == RTGUI_TOUCH_MOTION)
  157. // {
  158. // RTGUI_EVENT_MOUSE_MOTION_INIT(&emouse);
  159. // emouse.x = event->x;
  160. // emouse.y = event->y;
  161. // emouse.button = 0;
  162. // rtgui_server_handle_mouse_motion(&emouse);
  163. // }
  164. // else
  165. // {
  166. // RTGUI_EVENT_MOUSE_BUTTON_INIT(&emouse);
  167. // emouse.x = event->x;
  168. // emouse.y = event->y;
  169. // emouse.button = RTGUI_MOUSE_BUTTON_LEFT;
  170. // if (event->up_down == RTGUI_TOUCH_UP)
  171. // emouse.button |= RTGUI_MOUSE_BUTTON_UP;
  172. // else
  173. // emouse.button |= RTGUI_MOUSE_BUTTON_DOWN;
  174. // rtgui_server_handle_mouse_btn(&emouse);
  175. // }
  176. // }
  177. }
  178. #ifdef _WIN32_NATIVE
  179. #include <windows.h>
  180. #endif
  181. static rt_bool_t rtgui_server_event_handler(struct rtgui_object *object,
  182. struct rtgui_event *event)
  183. {
  184. RT_ASSERT(object != RT_NULL);
  185. RT_ASSERT(event != RT_NULL);
  186. /* dispatch event */
  187. switch (event->type)
  188. {
  189. case RTGUI_EVENT_APP_CREATE:
  190. case RTGUI_EVENT_APP_DESTROY:
  191. if (rtgui_wm_application != RT_NULL)
  192. {
  193. /* forward event to wm application */
  194. rtgui_send(rtgui_wm_application, event, sizeof(struct rtgui_event_application));
  195. }
  196. else
  197. {
  198. /* always ack with OK */
  199. rtgui_ack(event, RTGUI_STATUS_OK);
  200. }
  201. break;
  202. /* mouse and keyboard event */
  203. case RTGUI_EVENT_MOUSE_MOTION:
  204. /* handle mouse motion event */
  205. rtgui_server_handle_mouse_motion((struct rtgui_event_mouse *)event);
  206. break;
  207. case RTGUI_EVENT_MOUSE_BUTTON:
  208. /* handle mouse button */
  209. rtgui_server_handle_mouse_btn((struct rtgui_event_mouse *)event);
  210. break;
  211. case RTGUI_EVENT_TOUCH:
  212. /* handle touch event */
  213. rtgui_server_handle_touch((struct rtgui_event_touch *)event);
  214. break;
  215. case RTGUI_EVENT_KBD:
  216. /* handle keyboard event */
  217. rtgui_server_handle_kbd((struct rtgui_event_kbd *)event);
  218. break;
  219. /* window event */
  220. case RTGUI_EVENT_WIN_CREATE:
  221. if (rtgui_topwin_add((struct rtgui_event_win_create *)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_SHOW:
  227. if (_show_win_hook) _show_win_hook();
  228. if (rtgui_topwin_show((struct rtgui_event_win *)event) == RT_EOK)
  229. rtgui_ack(event, RTGUI_STATUS_OK);
  230. else
  231. rtgui_ack(event, RTGUI_STATUS_ERROR);
  232. break;
  233. case RTGUI_EVENT_WIN_HIDE:
  234. if (rtgui_topwin_hide((struct rtgui_event_win *)event) == RT_EOK)
  235. rtgui_ack(event, RTGUI_STATUS_OK);
  236. else
  237. rtgui_ack(event, RTGUI_STATUS_ERROR);
  238. break;
  239. case RTGUI_EVENT_WIN_MOVE:
  240. if (rtgui_topwin_move((struct rtgui_event_win_move *)event) == RT_EOK)
  241. rtgui_ack(event, RTGUI_STATUS_OK);
  242. else
  243. rtgui_ack(event, RTGUI_STATUS_ERROR);
  244. break;
  245. case RTGUI_EVENT_WIN_MODAL_ENTER:
  246. if (rtgui_topwin_modal_enter((struct rtgui_event_win_modal_enter *)event) == RT_EOK)
  247. rtgui_ack(event, RTGUI_STATUS_OK);
  248. else
  249. rtgui_ack(event, RTGUI_STATUS_ERROR);
  250. break;
  251. case RTGUI_EVENT_WIN_ACTIVATE:
  252. if (_act_win_hook) _act_win_hook();
  253. if (rtgui_topwin_activate((struct rtgui_event_win_activate *)event) == RT_EOK)
  254. rtgui_ack(event, RTGUI_STATUS_OK);
  255. else
  256. rtgui_ack(event, RTGUI_STATUS_ERROR);
  257. break;
  258. case RTGUI_EVENT_WIN_DESTROY:
  259. if (rtgui_topwin_remove(((struct rtgui_event_win *)event)->wid) == RT_EOK)
  260. rtgui_ack(event, RTGUI_STATUS_OK);
  261. else
  262. rtgui_ack(event, RTGUI_STATUS_ERROR);
  263. break;
  264. case RTGUI_EVENT_WIN_RESIZE:
  265. rtgui_topwin_resize(((struct rtgui_event_win_resize *)event)->wid,
  266. &(((struct rtgui_event_win_resize *)event)->rect));
  267. break;
  268. case RTGUI_EVENT_SET_WM:
  269. if (rtgui_wm_application != RT_NULL)
  270. {
  271. rtgui_ack(event, RTGUI_STATUS_ERROR);
  272. }
  273. else
  274. {
  275. struct rtgui_event_set_wm *set_wm;
  276. set_wm = (struct rtgui_event_set_wm *) event;
  277. rtgui_wm_application = set_wm->app;
  278. rtgui_ack(event, RTGUI_STATUS_OK);
  279. }
  280. break;
  281. /* other event */
  282. case RTGUI_EVENT_COMMAND:
  283. break;
  284. case RTGUI_EVENT_UPDATE_BEGIN:
  285. #ifdef RTGUI_USING_MOUSE_CURSOR
  286. /* hide cursor */
  287. rtgui_mouse_hide_cursor();
  288. #endif
  289. break;
  290. case RTGUI_EVENT_UPDATE_END:
  291. /* handle screen update */
  292. rtgui_server_handle_update((struct rtgui_event_update_end *)event);
  293. #ifdef RTGUI_USING_MOUSE_CURSOR
  294. /* show cursor */
  295. rtgui_mouse_show_cursor();
  296. #endif
  297. break;
  298. case RTGUI_EVENT_MONITOR_ADD:
  299. /* handle mouse monitor */
  300. rtgui_server_handle_monitor_add((struct rtgui_event_monitor *)event);
  301. break;
  302. default:
  303. rt_kprintf("RTGUI: wrong event sent to server: %d\n", event->type);
  304. return RT_FALSE;
  305. }
  306. return RT_TRUE;
  307. }
  308. /**
  309. * rtgui server thread's entry
  310. */
  311. static void rtgui_server_entry(void *parameter)
  312. {
  313. #ifdef _WIN32_NATIVE
  314. /* set the server thread to highest */
  315. HANDLE hCurrentThread = GetCurrentThread();
  316. SetThreadPriority(hCurrentThread, THREAD_PRIORITY_HIGHEST);
  317. #endif
  318. /* create rtgui server application */
  319. rtgui_server_app = rtgui_app_create("rtgui");
  320. if (rtgui_server_app == RT_NULL)
  321. {
  322. rt_kprintf("Create GUI server failed.\n");
  323. return;
  324. }
  325. rtgui_object_set_event_handler(RTGUI_OBJECT(rtgui_server_app),
  326. rtgui_server_event_handler);
  327. /* init mouse and show */
  328. rtgui_mouse_init();
  329. #ifdef RTGUI_USING_MOUSE_CURSOR
  330. rtgui_mouse_show_cursor();
  331. #endif
  332. rtgui_app_run(rtgui_server_app);
  333. rtgui_app_destroy(rtgui_server_app);
  334. rtgui_server_app = RT_NULL;
  335. }
  336. rt_err_t rtgui_server_post_event(struct rtgui_event *event, rt_size_t size)
  337. {
  338. rt_err_t result;
  339. if (rtgui_server_app != RT_NULL)
  340. {
  341. result = rtgui_send(rtgui_server_app, event, size);
  342. }
  343. else
  344. {
  345. rt_kprintf("post when server is not running\n");
  346. result = -RT_ENOSYS;
  347. }
  348. return result;
  349. }
  350. rt_err_t rtgui_server_post_event_sync(struct rtgui_event *event, rt_size_t size)
  351. {
  352. if (rtgui_server_app != RT_NULL)
  353. return rtgui_send_sync(rtgui_server_app, event, size);
  354. else
  355. {
  356. rt_kprintf("post when server is not running\n");
  357. return -RT_ENOSYS;
  358. }
  359. }
  360. struct rtgui_app* rtgui_get_server(void)
  361. {
  362. return rtgui_server_app;
  363. }
  364. RTM_EXPORT(rtgui_get_server);
  365. void rtgui_server_init(void)
  366. {
  367. rt_thread_t tid;
  368. tid = rt_thread_create("rtgui",
  369. rtgui_server_entry, RT_NULL,
  370. RTGUI_SVR_THREAD_STACK_SIZE,
  371. RTGUI_SVR_THREAD_PRIORITY,
  372. RTGUI_SVR_THREAD_TIMESLICE);
  373. /* start rtgui server thread */
  374. if (tid != RT_NULL)
  375. rt_thread_startup(tid);
  376. }