WindowServerConnection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/StringBuilder.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_anonymous_buffer(Core::AnonymousBuffer buffer)
  54. {
  55. Gfx::set_system_theme(buffer);
  56. Application::the()->set_system_palette(buffer);
  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_anonymous_buffer(response->theme_buffer());
  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_anonymous_buffer(message.theme_buffer());
  68. Window::update_all_windows({});
  69. Window::for_each_window({}, [](auto& window) {
  70. Core::EventLoop::current().post_event(window, make<ThemeChangeEvent>());
  71. });
  72. }
  73. void WindowServerConnection::handle(const Messages::WindowClient::Paint& message)
  74. {
  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.new_rect().size()));
  82. }
  83. }
  84. void WindowServerConnection::handle(const Messages::WindowClient::WindowActivated& message)
  85. {
  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. if (auto* window = Window::from_window_id(message.window_id()))
  92. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
  93. }
  94. void WindowServerConnection::handle(const Messages::WindowClient::WindowInputEntered& message)
  95. {
  96. if (auto* window = Window::from_window_id(message.window_id()))
  97. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  98. }
  99. void WindowServerConnection::handle(const Messages::WindowClient::WindowInputLeft& message)
  100. {
  101. if (auto* window = Window::from_window_id(message.window_id()))
  102. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputLeft));
  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. auto* window = Window::from_window_id(message.window_id());
  122. if (!window)
  123. return;
  124. auto key_event = make<KeyEvent>(Event::KeyDown, (KeyCode)message.key(), message.modifiers(), message.code_point(), message.scancode());
  125. Action* action = nullptr;
  126. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  127. dbg() << "Looking up action for " << key_event->to_string();
  128. #endif
  129. if (auto* focused_widget = window->focused_widget()) {
  130. for (auto* widget = focused_widget; widget && !action; widget = widget->parent_widget()) {
  131. action = widget->action_for_key_event(*key_event);
  132. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  133. dbg() << " > Focused widget " << *widget << " gave action: " << action;
  134. #endif
  135. }
  136. }
  137. if (!action) {
  138. action = window->action_for_key_event(*key_event);
  139. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  140. dbg() << " > Asked window " << *window << ", got action: " << action;
  141. #endif
  142. }
  143. // NOTE: Application-global shortcuts are ignored while a modal window is up.
  144. if (!action && !window->is_modal()) {
  145. action = Application::the()->action_for_key_event(*key_event);
  146. #ifdef KEYBOARD_SHORTCUTS_DEBUG
  147. dbg() << " > Asked application, got action: " << action;
  148. #endif
  149. }
  150. if (action) {
  151. if (action->is_enabled()) {
  152. action->activate();
  153. return;
  154. }
  155. if (action->swallow_key_event_when_disabled())
  156. return;
  157. }
  158. bool focused_widget_accepts_emoji_input = window->focused_widget() && window->focused_widget()->accepts_emoji_input();
  159. if (focused_widget_accepts_emoji_input && (message.modifiers() == (Mod_Ctrl | Mod_Alt)) && message.key() == Key_Space) {
  160. auto emoji_input_dialog = EmojiInputDialog::construct(window);
  161. if (emoji_input_dialog->exec() != EmojiInputDialog::ExecOK)
  162. return;
  163. key_event->m_key = Key_Invalid;
  164. key_event->m_modifiers = 0;
  165. AK::Utf8View m_utf8_view(emoji_input_dialog->selected_emoji_text().characters());
  166. u32 code_point = *m_utf8_view.begin();
  167. key_event->m_code_point = code_point;
  168. }
  169. Core::EventLoop::current().post_event(*window, move(key_event));
  170. }
  171. void WindowServerConnection::handle(const Messages::WindowClient::KeyUp& message)
  172. {
  173. auto* window = Window::from_window_id(message.window_id());
  174. if (!window)
  175. return;
  176. auto key_event = make<KeyEvent>(Event::KeyUp, (KeyCode)message.key(), message.modifiers(), message.code_point(), message.scancode());
  177. Core::EventLoop::current().post_event(*window, move(key_event));
  178. }
  179. static MouseButton to_gmousebutton(u32 button)
  180. {
  181. switch (button) {
  182. case 0:
  183. return MouseButton::None;
  184. case 1:
  185. return MouseButton::Left;
  186. case 2:
  187. return MouseButton::Right;
  188. case 4:
  189. return MouseButton::Middle;
  190. case 8:
  191. return MouseButton::Back;
  192. case 16:
  193. return MouseButton::Forward;
  194. default:
  195. ASSERT_NOT_REACHED();
  196. break;
  197. }
  198. }
  199. void WindowServerConnection::handle(const Messages::WindowClient::MouseDown& message)
  200. {
  201. if (auto* window = Window::from_window_id(message.window_id()))
  202. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDown, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  203. }
  204. void WindowServerConnection::handle(const Messages::WindowClient::MouseUp& message)
  205. {
  206. if (auto* window = Window::from_window_id(message.window_id()))
  207. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseUp, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  208. }
  209. void WindowServerConnection::handle(const Messages::WindowClient::MouseMove& message)
  210. {
  211. if (auto* window = Window::from_window_id(message.window_id())) {
  212. if (message.is_drag())
  213. Core::EventLoop::current().post_event(*window, make<DragEvent>(Event::DragMove, message.mouse_position(), message.mime_types()));
  214. else
  215. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseMove, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  216. }
  217. }
  218. void WindowServerConnection::handle(const Messages::WindowClient::MouseDoubleClick& message)
  219. {
  220. if (auto* window = Window::from_window_id(message.window_id()))
  221. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseDoubleClick, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  222. }
  223. void WindowServerConnection::handle(const Messages::WindowClient::MouseWheel& message)
  224. {
  225. if (auto* window = Window::from_window_id(message.window_id()))
  226. Core::EventLoop::current().post_event(*window, make<MouseEvent>(Event::MouseWheel, message.mouse_position(), message.buttons(), to_gmousebutton(message.button()), message.modifiers(), message.wheel_delta()));
  227. }
  228. void WindowServerConnection::handle(const Messages::WindowClient::MenuItemActivated& message)
  229. {
  230. auto* menu = Menu::from_menu_id(message.menu_id());
  231. if (!menu) {
  232. dbgprintf("EventLoop received event for invalid menu ID %d\n", message.menu_id());
  233. return;
  234. }
  235. if (auto* action = menu->action_at(message.identifier()))
  236. action->activate(menu);
  237. }
  238. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowStateChanged& message)
  239. {
  240. if (auto* window = Window::from_window_id(message.wm_id()))
  241. Core::EventLoop::current().post_event(*window, make<WMWindowStateChangedEvent>(message.client_id(), message.window_id(), message.parent_client_id(), message.parent_window_id(), message.title(), message.rect(), message.is_active(), message.is_modal(), static_cast<WindowType>(message.window_type()), message.is_minimized(), message.is_frameless(), message.progress()));
  242. }
  243. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRectChanged& message)
  244. {
  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. if (auto* window = Window::from_window_id(message.wm_id())) {
  251. Core::EventLoop::current().post_event(*window, make<WMWindowIconBitmapChangedEvent>(message.client_id(), message.window_id(), message.bitmap().bitmap()));
  252. }
  253. }
  254. void WindowServerConnection::handle(const Messages::WindowClient::WM_WindowRemoved& message)
  255. {
  256. if (auto* window = Window::from_window_id(message.wm_id()))
  257. Core::EventLoop::current().post_event(*window, make<WMWindowRemovedEvent>(message.client_id(), message.window_id()));
  258. }
  259. void WindowServerConnection::handle(const Messages::WindowClient::ScreenRectChanged& message)
  260. {
  261. Desktop::the().did_receive_screen_rect({}, message.rect());
  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(message.mime_data());
  271. Core::EventLoop::current().post_event(*window, make<DropEvent>(message.mouse_position(), message.text(), mime_data));
  272. }
  273. }
  274. void WindowServerConnection::handle(const Messages::WindowClient::DragAccepted&)
  275. {
  276. DragOperation::notify_accepted({});
  277. }
  278. void WindowServerConnection::handle(const Messages::WindowClient::DragCancelled&)
  279. {
  280. DragOperation::notify_cancelled({});
  281. Application::the()->notify_drag_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. void WindowServerConnection::handle(const Messages::WindowClient::DisplayLinkNotification&)
  289. {
  290. if (m_display_link_notification_pending)
  291. return;
  292. m_display_link_notification_pending = true;
  293. deferred_invoke([this](auto&) {
  294. DisplayLink::notify({});
  295. m_display_link_notification_pending = false;
  296. });
  297. }
  298. void WindowServerConnection::handle(const Messages::WindowClient::Ping&)
  299. {
  300. post_message(Messages::WindowServer::Pong());
  301. }
  302. }