WindowServerConnection.cpp 13 KB

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