WindowServerConnection.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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/DragOperation.h>
  34. #include <LibGUI/Event.h>
  35. #include <LibGUI/Menu.h>
  36. #include <LibGUI/Widget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGUI/WindowServerConnection.h>
  39. #include <LibGfx/Bitmap.h>
  40. #include <LibGfx/Palette.h>
  41. #include <LibGfx/SystemTheme.h>
  42. //#define GEVENTLOOP_DEBUG
  43. namespace GUI {
  44. WindowServerConnection& WindowServerConnection::the()
  45. {
  46. static WindowServerConnection* s_connection = nullptr;
  47. if (!s_connection)
  48. s_connection = new WindowServerConnection;
  49. return *s_connection;
  50. }
  51. static void set_system_theme_from_shbuf_id(int id)
  52. {
  53. auto system_theme = SharedBuffer::create_from_shbuf_id(id);
  54. ASSERT(system_theme);
  55. Gfx::set_system_theme(*system_theme);
  56. Application::the().set_system_palette(*system_theme);
  57. }
  58. void WindowServerConnection::handshake()
  59. {
  60. auto response = send_sync<Messages::WindowServer::Greet>();
  61. set_my_client_id(response->client_id());
  62. set_system_theme_from_shbuf_id(response->system_theme_buffer_id());
  63. Desktop::the().did_receive_screen_rect({}, response->screen_rect());
  64. }
  65. void WindowServerConnection::handle(const Messages::WindowClient::UpdateSystemTheme& message)
  66. {
  67. set_system_theme_from_shbuf_id(message.system_theme_buffer_id());
  68. Window::update_all_windows({});
  69. }
  70. void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)
  71. {
  72. #ifdef GEVENTLOOP_DEBUG
  73. dbgprintf("WID=%d Paint\n", message.window_id());
  74. #endif
  75. if (auto* window = Window::from_window_id(message.window_id()))
  76. Core::EventLoop::current().post_event(*window, make<MultiPaintEvent>(message.rects(), message.window_size()));
  77. }
  78. void WindowServerConnection::handle(const Messages::WindowClient::WindowResized& message)
  79. {
  80. if (auto* window = Window::from_window_id(message.window_id())) {
  81. Core::EventLoop::current().post_event(*window, make<ResizeEvent>(message.old_rect().size(), message.new_rect().size()));
  82. }
  83. }
  84. void WindowServerConnection::handle(const Messages::WindowClient::WindowActivated& message)
  85. {
  86. #ifdef GEVENTLOOP_DEBUG
  87. dbgprintf("(%d) WID=%d WindowActivated\n", getpid(), message.window_id());
  88. #endif
  89. if (auto* window = Window::from_window_id(message.window_id()))
  90. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameActive));
  91. }
  92. void WindowServerConnection::handle(const Messages::WindowClient::WindowDeactivated& message)
  93. {
  94. #ifdef GEVENTLOOP_DEBUG
  95. dbgprintf("(%d) WID=%d WindowDeactivated\n", getpid(), message.window_id());
  96. #endif
  97. if (auto* window = Window::from_window_id(message.window_id()))
  98. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  99. }
  100. void WindowServerConnection::handle(const Messages::WindowClient::WindowCloseRequest& message)
  101. {
  102. if (auto* window = Window::from_window_id(message.window_id()))
  103. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowCloseRequest));
  104. }
  105. void WindowServerConnection::handle(const Messages::WindowClient::WindowEntered& message)
  106. {
  107. if (auto* window = Window::from_window_id(message.window_id()))
  108. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowEntered));
  109. }
  110. void WindowServerConnection::handle(const Messages::WindowClient::WindowLeft& message)
  111. {
  112. if (auto* window = Window::from_window_id(message.window_id()))
  113. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowLeft));
  114. }
  115. void WindowServerConnection::handle(const Messages::WindowClient::KeyDown& message)
  116. {
  117. #ifdef GEVENTLOOP_DEBUG
  118. dbgprintf("WID=%d KeyDown character=0x%02x\n", message.window_id(), message.character());
  119. #endif
  120. auto* window = Window::from_window_id(message.window_id());
  121. if (!window)
  122. return;
  123. auto key_event = make<KeyEvent>(Event::KeyDown, message.key(), message.modifiers());
  124. if (message.character() != '\0') {
  125. char ch = message.character();
  126. key_event->m_text = String(&ch, 1);
  127. }
  128. Action* action = nullptr;
  129. if (auto* focused_widget = window->focused_widget())
  130. action = focused_widget->action_for_key_event(*key_event);
  131. if (!action)
  132. action = window->action_for_key_event(*key_event);
  133. if (!action)
  134. action = Application::the().action_for_key_event(*key_event);
  135. if (action && action->is_enabled()) {
  136. action->activate();
  137. return;
  138. }
  139. Core::EventLoop::current().post_event(*window, move(key_event));
  140. }
  141. void WindowServerConnection::handle(const Messages::WindowClient::KeyUp& message)
  142. {
  143. #ifdef GEVENTLOOP_DEBUG
  144. dbgprintf("WID=%d KeyUp character=0x%02x\n", message.window_id(), message.character());
  145. #endif
  146. auto* window = Window::from_window_id(message.window_id());
  147. if (!window)
  148. return;
  149. auto key_event = make<KeyEvent>(Event::KeyUp, message.key(), message.modifiers());
  150. if (message.character() != '\0') {
  151. char ch = message.character();
  152. key_event->m_text = String(&ch, 1);
  153. }
  154. Core::EventLoop::current().post_event(*window, move(key_event));
  155. }
  156. MouseButton to_gmousebutton(u32 button)
  157. {
  158. switch (button) {
  159. case 0:
  160. return MouseButton::None;
  161. case 1:
  162. return MouseButton::Left;
  163. case 2:
  164. return MouseButton::Right;
  165. case 4:
  166. return MouseButton::Middle;
  167. default:
  168. ASSERT_NOT_REACHED();
  169. break;
  170. }
  171. }
  172. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  173. {
  174. #ifdef GEVENTLOOP_DEBUG
  175. dbgprintf("WID=%d MouseDown %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  176. #endif
  177. if (auto* window = Window::from_window_id(message.window_id()))
  178. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  179. }
  180. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  181. {
  182. #ifdef GEVENTLOOP_DEBUG
  183. dbgprintf("WID=%d MouseUp %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  184. #endif
  185. if (auto* window = Window::from_window_id(message.window_id()))
  186. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  187. }
  188. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  189. {
  190. #ifdef GEVENTLOOP_DEBUG
  191. dbgprintf("WID=%d MouseMove %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  192. #endif
  193. if (auto* window = Window::from_window_id(message.window_id())) {
  194. if (message.is_drag())
  195. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.drag_data_type()));
  196. else
  197. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  198. }
  199. }
  200. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  201. {
  202. #ifdef GEVENTLOOP_DEBUG
  203. dbgprintf("WID=%d MouseDoubleClick %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  204. #endif
  205. if (auto* window = Window::from_window_id(message.window_id()))
  206. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  207. }
  208. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  209. {
  210. #ifdef GEVENTLOOP_DEBUG
  211. dbgprintf("WID=%d MouseWheel %d,%d,%d\n", message.window_id(), message.mouse_position().x(), message.mouse_position().y(), message.wheel_delta();
  212. #endif
  213. if (auto* window = Window::from_window_id(message.window_id()))
  214. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  215. }
  216. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  217. {
  218. auto* menu = Menu::from_menu_id(message.menu_id());
  219. if (!menu) {
  220. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  221. return;
  222. }
  223. if (auto* action = menu->action_at(message.identifier()))
  224. action->activate(menu);
  225. }
  226. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  227. {
  228. #ifdef GEVENTLOOP_DEBUG
  229. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  230. #endif
  231. if (auto* window = Window::from_window_id(message.wm_id()))
  232. 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()));
  233. }
  234. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  235. {
  236. #ifdef GEVENTLOOP_DEBUG
  237. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  238. #endif
  239. if (auto* window = Window::from_window_id(message.wm_id()))
  240. Core::EventLoop::current().post_event(*window, make<WMWindowRectChangedEvent>(message.client_id(), message.window_id(), message.rect()));
  241. }
  242. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowIconBitmapChanged& message)
  243. {
  244. #ifdef GEVENTLOOP_DEBUG
  245. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  246. #endif
  247. if (auto* window = Window::from_window_id(message.wm_id()))
  248. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.icon_buffer_id(), message.icon_size()));
  249. }
  250. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  251. {
  252. #ifdef GEVENTLOOP_DEBUG
  253. dbgprintf("EventLoop: handle_wm_event: %d\n", (int)event.type);
  254. #endif
  255. if (auto* window = Window::from_window_id(message.wm_id()))
  256. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  257. }
  258. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  259. {
  260. Desktop::the().did_receive_screen_rect({}, message.rect());
  261. }
  262. void WindowServerConnection::handle(const Messages::WindowClient::ClipboardContentsChanged& message)
  263. {
  264. Clipboard::the().did_receive_clipboard_contents_changed({}, message.content_type());
  265. }
  266. void WindowServerConnection::handle(const Messages::WindowClient::AsyncSetWallpaperFinished&)
  267. {
  268. // This is handled manually by Desktop::set_wallpaper().
  269. }
  270. void WindowServerConnection::handle(const Messages::WindowClient::DragDropped& message)
  271. {
  272. if (auto* window = Window::from_window_id(message.window_id())) {
  273. auto mime_data = Core::MimeData::construct();
  274. mime_data->set_data(message.data_type(), message.data().to_byte_buffer());
  275. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  276. }
  277. }
  278. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  279. {
  280. DragOperation::notify_accepted({});
  281. }
  282. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  283. {
  284. DragOperation::notify_cancelled({});
  285. }
  286. void WindowServerConnection::handle(const Messages::WindowClient::WindowStateChanged& message)
  287. {
  288. if (auto* window = Window::from_window_id(message.window_id()))
  289. window->notify_state_changed({}, message.minimized(), message.occluded());
  290. }
  291. }