WindowServerConnection.cpp 13 KB

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