server.c 11 KB

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