WindowServerConnection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/SharedBuffer.h>
  27. #include <LibCore/EventLoop.h>
  28. #include <LibCore/MimeData.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Application.h>
  31. #include <LibGUI/Clipboard.h>
  32. #include <LibGUI/Desktop.h>
  33. #include <LibGUI/DisplayLink.h>
  34. #include <LibGUI/DragOperation.h>
  35. #include <LibGUI/Event.h>
  36. #include <LibGUI/Menu.h>
  37. #include <LibGUI/Widget.h>
  38. #include <LibGUI/Window.h>
  39. #include <LibGUI/WindowServerConnection.h>
  40. #include <LibGfx/Bitmap.h>
  41. #include <LibGfx/Palette.h>
  42. #include <LibGfx/SystemTheme.h>
  43. //#define GEVENTLOOP_DEBUG
  44. namespace GUI {
  45. WindowServerConnection& WindowServerConnection::the()
  46. {
  47. static WindowServerConnection* s_connection = nullptr;
  48. if (!s_connection)
  49. s_connection = new WindowServerConnection;
  50. return *s_connection;
  51. }
  52. static void set_system_theme_from_shbuf_id(int id)
  53. {
  54. auto system_theme = SharedBuffer::create_from_shbuf_id(id);
  55. ASSERT(system_theme);
  56. Gfx::set_system_theme(*system_theme);
  57. Application::the().set_system_palette(*system_theme);
  58. }
  59. void WindowServerConnection::handshake()
  60. {
  61. auto response = send_sync<Messages::WindowServer::Greet>();
  62. set_my_client_id(response->client_id());
  63. set_system_theme_from_shbuf_id(response->system_theme_buffer_id());
  64. Desktop::the().did_receive_screen_rect({}, response->screen_rect());
  65. }
  66. void WindowServerConnection::handle(const Messages::WindowClient::UpdateSystemTheme& message)
  67. {
  68. set_system_theme_from_shbuf_id(message.system_theme_buffer_id());
  69. Window::update_all_windows({});
  70. Window::for_each_window({}, [](auto& window) {
  71. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  72. });
  73. }
  74. void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)
  75. {
  76. #ifdef GEVENTLOOP_DEBUG
  77. dbgprintf("WID=%d Paint\n", message.window_id());
  78. #endif
  79. if (auto* window = Window::from_window_id(message.window_id()))
  80. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(message.rects(), message.window_size()));
  81. }
  82. void WindowServerConnection::handle(const Messages::WindowClient::WindowResized& message)
  83. {
  84. if (auto* window = Window::from_window_id(message.window_id())) {
  85. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(message.old_rect().size(), message.new_rect().size()));
  86. }
  87. }
  88. void WindowServerConnection::handle(const Messages::WindowClient::WindowActivated& message)
  89. {
  90. #ifdef GEVENTLOOP_DEBUG
  91. dbgprintf("(%d) WID=%d WindowActivated\n", getpid(), message.window_id());
  92. #endif
  93. if (auto* window = Window::from_window_id(message.window_id()))
  94. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  95. }
  96. void WindowServerConnection::handle(const Messages::WindowClient::WindowDeactivated& message)
  97. {
  98. #ifdef GEVENTLOOP_DEBUG
  99. dbgprintf("(%d) WID=%d WindowDeactivated\n", getpid(), message.window_id());
  100. #endif
  101. if (auto* window = Window::from_window_id(message.window_id()))
  102. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  103. }
  104. void WindowServerConnection::handle(const Messages::WindowClient::WindowCloseRequest& message)
  105. {
  106. if (auto* window = Window::from_window_id(message.window_id()))
  107. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  108. }
  109. void WindowServerConnection::handle(const Messages::WindowClient::WindowEntered& message)
  110. {
  111. if (auto* window = Window::from_window_id(message.window_id()))
  112. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  113. }
  114. void WindowServerConnection::handle(const Messages::WindowClient::WindowLeft& message)
  115. {
  116. if (auto* window = Window::from_window_id(message.window_id()))
  117. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  118. }
  119. void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& message)
  120. {
  121. #ifdef GEVENTLOOP_DEBUG
  122. dbgprintf("WID=%d KeyDown character=0x%02x\n", message.window_id(), message.character());
  123. #endif
  124. auto* window = Window::from_window_id(message.window_id());
  125. if (!window)
  126. return;
  127. auto key_event = make<KeyEvent>(Event::KeyDown, message.key(), message.modifiers());
  128. if (message.character() != '\0') {
  129. char ch = message.character();
  130. key_event->m_text = String(&ch, 1);
  131. }
  132. Action* action = nullptr;
  133. if (auto* focused_widget = window->focused_widget()) {
  134. for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget())
  135. action = focused_widget->action_for_key_event(*key_event);
  136. }
  137. if (!action)
  138. action = window->action_for_key_event(*key_event);
  139. if (!action)
  140. action = Application::the().action_for_key_event(*key_event);
  141. if (action && action->is_enabled()) {
  142. action->activate();
  143. return;
  144. }
  145. Core::EventLoop::current().post_event(*window, move(key_event));
  146. }
  147. void WindowServerConnection::handle(const Messages::WindowClient::KeyUp& message)
  148. {
  149. #ifdef GEVENTLOOP_DEBUG
  150. dbgprintf("WID=%d KeyUp character=0x%02x\n", message.window_id(), message.character());
  151. #endif
  152. auto* window = Window::from_window_id(message.window_id());
  153. if (!window)
  154. return;
  155. auto key_event = make<KeyEvent>(Event::KeyUp, message.key(), message.modifiers());
  156. if (message.character() != '\0') {
  157. char ch = message.character();
  158. key_event->m_text = String(&ch, 1);
  159. }
  160. Core::EventLoop::current().post_event(*window, move(key_event));
  161. }
  162. MouseButton to_gmousebutton(u32 button)
  163. {
  164. switch (button) {
  165. case 0:
  166. return MouseButton::None;
  167. case 1:
  168. return MouseButton::Left;
  169. case 2:
  170. return MouseButton::Right;
  171. case 4:
  172. return MouseButton::Middle;
  173. default:
  174. ASSERT_NOT_REACHED();
  175. break;
  176. }
  177. }
  178. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  179. {
  180. #ifdef GEVENTLOOP_DEBUG
  181. dbgprintf("WID=%d MouseDown %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  182. #endif
  183. if (auto* window = Window::from_window_id(message.window_id()))
  184. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  185. }
  186. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  187. {
  188. #ifdef GEVENTLOOP_DEBUG
  189. dbgprintf("WID=%d MouseUp %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  190. #endif
  191. if (auto* window = Window::from_window_id(message.window_id()))
  192. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  193. }
  194. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  195. {
  196. #ifdef GEVENTLOOP_DEBUG
  197. dbgprintf("WID=%d MouseMove %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  198. #endif
  199. if (auto* window = Window::from_window_id(message.window_id())) {
  200. if (message.is_drag())
  201. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.drag_data_type()));
  202. else
  203. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  204. }
  205. }
  206. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  207. {
  208. #ifdef GEVENTLOOP_DEBUG
  209. dbgprintf("WID=%d MouseDoubleClick %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  210. #endif
  211. if (auto* window = Window::from_window_id(message.window_id()))
  212. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  213. }
  214. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  215. {
  216. #ifdef GEVENTLOOP_DEBUG
  217. dbgprintf("WID=%d MouseWheel %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  218. #endif
  219. if (auto* window = Window::from_window_id(message.window_id()))
  220. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  221. }
  222. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  223. {
  224. auto* menu = Menu::from_menu_id(message.menu_id());
  225. if (!menu) {
  226. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  227. return;
  228. }
  229. if (auto* action = menu->action_at(message.identifier()))
  230. action->activate(menu);
  231. }
  232. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  233. {
  234. #ifdef GEVENTLOOP_DEBUG
  235. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  236. #endif
  237. if (auto* window = Window::from_window_id(message.wm_id()))
  238. Core::EventLoop::current().post_event(*window, make<WMWindowStateChangedEvent>(message.client_id(), message.window_id(), message.title(), message.rect(), message.is_active(), static_cast<WindowType>(message.window_type()), message.is_minimized()));
  239. }
  240. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  241. {
  242. #ifdef GEVENTLOOP_DEBUG
  243. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  244. #endif
  245. if (auto* window = Window::from_window_id(message.wm_id()))
  246. Core::EventLoop::current().post_event(*window, make<WMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  247. }
  248. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowIconBitmapChanged& message)
  249. {
  250. #ifdef GEVENTLOOP_DEBUG
  251. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  252. #endif
  253. if (auto* window = Window::from_window_id(message.wm_id()))
  254. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  255. }
  256. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  257. {
  258. #ifdef GEVENTLOOP_DEBUG
  259. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  260. #endif
  261. if (auto* window = Window::from_window_id(message.wm_id()))
  262. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  263. }
  264. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  265. {
  266. Desktop::the().did_receive_screen_rect({}, message.rect());
  267. }
  268. void WindowServerConnection::handle(const Messages::WindowClient::ClipboardContentsChanged& message)
  269. {
  270. Clipboard::the().did_receive_clipboard_contents_changed({}, message.content_type());
  271. }
  272. void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpaperFinished&)
  273. {
  274. // This is handled manually by Desktop::set_wallpaper().
  275. }
  276. void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
  277. {
  278. if (auto* window = Window::from_window_id(message.window_id())) {
  279. auto mime_data = Core::MimeData::construct();
  280. mime_data->set_data(message.data_type(), message.data().to_byte_buffer());
  281. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  282. }
  283. }
  284. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  285. {
  286. DragOperation::notify_accepted({});
  287. }
  288. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  289. {
  290. DragOperation::notify_cancelled({});
  291. }
  292. void WindowServerConnection::handle(const Messages::WindowClient::WindowStateChanged& message)
  293. {
  294. if (auto* window = Window::from_window_id(message.window_id()))
  295. window->notify_state_changed({}, message.minimized(), message.occluded());
  296. }
  297. void WindowServerConnection::handle(const Messages::WindowClient::DisplayLinkNotification&)
  298. {
  299. if (m_display_link_notification_pending)
  300. return;
  301. m_display_link_notification_pending = true;
  302. deferred_invoke([this](auto&) {
  303. DisplayLink::notify({});
  304. m_display_link_notification_pending = false;
  305. });
  306. }
  307. }