ClientConnection.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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/Badge.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibGfx/StandardCursor.h>
  29. #include <LibGfx/SystemTheme.h>
  30. #include <WindowServer/AppletManager.h>
  31. #include <WindowServer/ClientConnection.h>
  32. #include <WindowServer/Compositor.h>
  33. #include <WindowServer/Menu.h>
  34. #include <WindowServer/MenuBar.h>
  35. #include <WindowServer/MenuItem.h>
  36. #include <WindowServer/Screen.h>
  37. #include <WindowServer/Window.h>
  38. #include <WindowServer/WindowClientEndpoint.h>
  39. #include <WindowServer/WindowManager.h>
  40. #include <WindowServer/WindowSwitcher.h>
  41. #include <errno.h>
  42. #include <serenity.h>
  43. #include <stdio.h>
  44. #include <unistd.h>
  45. namespace WindowServer {
  46. HashMap<int, NonnullRefPtr<ClientConnection>>* s_connections;
  47. void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
  48. {
  49. if (!s_connections)
  50. return;
  51. for (auto& it : *s_connections) {
  52. callback(*it.value);
  53. }
  54. }
  55. ClientConnection* ClientConnection::from_client_id(int client_id)
  56. {
  57. if (!s_connections)
  58. return nullptr;
  59. auto it = s_connections->find(client_id);
  60. if (it == s_connections->end())
  61. return nullptr;
  62. return (*it).value.ptr();
  63. }
  64. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  65. : IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
  66. {
  67. if (!s_connections)
  68. s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;
  69. s_connections->set(client_id, *this);
  70. }
  71. ClientConnection::~ClientConnection()
  72. {
  73. if (m_has_display_link)
  74. Compositor::the().decrement_display_link_count({});
  75. MenuManager::the().close_all_menus_from_client({}, *this);
  76. auto windows = move(m_windows);
  77. for (auto& window : windows) {
  78. window.value->detach_client({});
  79. if (window.value->type() == WindowType::MenuApplet)
  80. AppletManager::the().remove_applet(window.value);
  81. }
  82. }
  83. void ClientConnection::die()
  84. {
  85. deferred_invoke([this](auto&) {
  86. s_connections->remove(client_id());
  87. });
  88. }
  89. void ClientConnection::notify_about_new_screen_rect(const Gfx::IntRect& rect)
  90. {
  91. post_message(Messages::WindowClient::ScreenRectChanged(rect));
  92. }
  93. OwnPtr<Messages::WindowServer::CreateMenubarResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenubar&)
  94. {
  95. int menubar_id = m_next_menubar_id++;
  96. auto menubar = make<MenuBar>(*this, menubar_id);
  97. m_menubars.set(menubar_id, move(menubar));
  98. return make<Messages::WindowServer::CreateMenubarResponse>(menubar_id);
  99. }
  100. OwnPtr<Messages::WindowServer::DestroyMenubarResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenubar& message)
  101. {
  102. int menubar_id = message.menubar_id();
  103. auto it = m_menubars.find(menubar_id);
  104. if (it == m_menubars.end()) {
  105. did_misbehave("DestroyMenubar: Bad menubar ID");
  106. return {};
  107. }
  108. auto& menubar = *(*it).value;
  109. MenuManager::the().close_menubar(menubar);
  110. m_menubars.remove(it);
  111. return make<Messages::WindowServer::DestroyMenubarResponse>();
  112. }
  113. OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
  114. {
  115. int menu_id = m_next_menu_id++;
  116. auto menu = Menu::construct(this, menu_id, message.menu_title());
  117. m_menus.set(menu_id, move(menu));
  118. return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
  119. }
  120. OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
  121. {
  122. int menu_id = message.menu_id();
  123. auto it = m_menus.find(menu_id);
  124. if (it == m_menus.end()) {
  125. did_misbehave("DestroyMenu: Bad menu ID");
  126. return {};
  127. }
  128. auto& menu = *(*it).value;
  129. menu.close();
  130. m_menus.remove(it);
  131. remove_child(menu);
  132. return make<Messages::WindowServer::DestroyMenuResponse>();
  133. }
  134. OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetApplicationMenubar& message)
  135. {
  136. int menubar_id = message.menubar_id();
  137. auto it = m_menubars.find(menubar_id);
  138. if (it == m_menubars.end()) {
  139. did_misbehave("SetApplicationMenubar: Bad menubar ID");
  140. return {};
  141. }
  142. auto& menubar = *(*it).value;
  143. m_app_menubar = menubar.make_weak_ptr();
  144. WindowManager::the().notify_client_changed_app_menubar(*this);
  145. return make<Messages::WindowServer::SetApplicationMenubarResponse>();
  146. }
  147. OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
  148. {
  149. int menubar_id = message.menubar_id();
  150. int menu_id = message.menu_id();
  151. auto it = m_menubars.find(menubar_id);
  152. auto jt = m_menus.find(menu_id);
  153. if (it == m_menubars.end()) {
  154. did_misbehave("AddMenuToMenubar: Bad menubar ID");
  155. return {};
  156. }
  157. if (jt == m_menus.end()) {
  158. did_misbehave("AddMenuToMenubar: Bad menu ID");
  159. return {};
  160. }
  161. auto& menubar = *(*it).value;
  162. auto& menu = *(*jt).value;
  163. menubar.add_menu(menu);
  164. return make<Messages::WindowServer::AddMenuToMenubarResponse>();
  165. }
  166. OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
  167. {
  168. int menu_id = message.menu_id();
  169. unsigned identifier = message.identifier();
  170. auto it = m_menus.find(menu_id);
  171. if (it == m_menus.end()) {
  172. dbgln("AddMenuItem: Bad menu ID: {}", menu_id);
  173. return {};
  174. }
  175. auto& menu = *(*it).value;
  176. auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
  177. if (message.is_default())
  178. menu_item->set_default(true);
  179. menu_item->set_icon(message.icon().bitmap());
  180. menu_item->set_submenu_id(message.submenu_id());
  181. menu_item->set_exclusive(message.exclusive());
  182. menu.add_item(move(menu_item));
  183. return make<Messages::WindowServer::AddMenuItemResponse>();
  184. }
  185. OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
  186. {
  187. int menu_id = message.menu_id();
  188. auto position = message.screen_position();
  189. auto it = m_menus.find(menu_id);
  190. if (it == m_menus.end()) {
  191. did_misbehave("PopupMenu: Bad menu ID");
  192. return {};
  193. }
  194. auto& menu = *(*it).value;
  195. menu.popup(position);
  196. return make<Messages::WindowServer::PopupMenuResponse>();
  197. }
  198. OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
  199. {
  200. int menu_id = message.menu_id();
  201. auto it = m_menus.find(menu_id);
  202. if (it == m_menus.end()) {
  203. did_misbehave("DismissMenu: Bad menu ID");
  204. return {};
  205. }
  206. auto& menu = *(*it).value;
  207. menu.close();
  208. return make<Messages::WindowServer::DismissMenuResponse>();
  209. }
  210. OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
  211. {
  212. int menu_id = message.menu_id();
  213. auto it = m_menus.find(menu_id);
  214. if (it == m_menus.end()) {
  215. did_misbehave("UpdateMenuItem: Bad menu ID");
  216. return {};
  217. }
  218. auto& menu = *(*it).value;
  219. auto* menu_item = menu.item_with_identifier(message.identifier());
  220. if (!menu_item) {
  221. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  222. return {};
  223. }
  224. menu_item->set_text(message.text());
  225. menu_item->set_shortcut_text(message.shortcut());
  226. menu_item->set_enabled(message.enabled());
  227. menu_item->set_checkable(message.checkable());
  228. menu_item->set_default(message.is_default());
  229. if (message.checkable())
  230. menu_item->set_checked(message.checked());
  231. return make<Messages::WindowServer::UpdateMenuItemResponse>();
  232. }
  233. OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuSeparator& message)
  234. {
  235. int menu_id = message.menu_id();
  236. auto it = m_menus.find(menu_id);
  237. if (it == m_menus.end()) {
  238. did_misbehave("AddMenuSeparator: Bad menu ID");
  239. return {};
  240. }
  241. auto& menu = *(*it).value;
  242. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  243. return make<Messages::WindowServer::AddMenuSeparatorResponse>();
  244. }
  245. OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> ClientConnection::handle(const Messages::WindowServer::MoveWindowToFront& message)
  246. {
  247. auto it = m_windows.find(message.window_id());
  248. if (it == m_windows.end()) {
  249. did_misbehave("MoveWindowToFront: Bad window ID");
  250. return {};
  251. }
  252. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  253. return make<Messages::WindowServer::MoveWindowToFrontResponse>();
  254. }
  255. OwnPtr<Messages::WindowServer::SetFullscreenResponse> ClientConnection::handle(const Messages::WindowServer::SetFullscreen& message)
  256. {
  257. auto it = m_windows.find(message.window_id());
  258. if (it == m_windows.end()) {
  259. did_misbehave("SetFullscreen: Bad window ID");
  260. return {};
  261. }
  262. it->value->set_fullscreen(message.fullscreen());
  263. return make<Messages::WindowServer::SetFullscreenResponse>();
  264. }
  265. OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOpacity& message)
  266. {
  267. auto it = m_windows.find(message.window_id());
  268. if (it == m_windows.end()) {
  269. did_misbehave("SetWindowOpacity: Bad window ID");
  270. return {};
  271. }
  272. it->value->set_opacity(message.opacity());
  273. return make<Messages::WindowServer::SetWindowOpacityResponse>();
  274. }
  275. void ClientConnection::handle(const Messages::WindowServer::AsyncSetWallpaper& message)
  276. {
  277. Compositor::the().set_wallpaper(message.path(), [&](bool success) {
  278. post_message(Messages::WindowClient::AsyncSetWallpaperFinished(success));
  279. });
  280. }
  281. OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> ClientConnection::handle(const Messages::WindowServer::SetBackgroundColor& message)
  282. {
  283. Compositor::the().set_background_color(message.background_color());
  284. return make<Messages::WindowServer::SetBackgroundColorResponse>();
  285. }
  286. OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> ClientConnection::handle(const Messages::WindowServer::SetWallpaperMode& message)
  287. {
  288. Compositor::the().set_wallpaper_mode(message.mode());
  289. return make<Messages::WindowServer::SetWallpaperModeResponse>();
  290. }
  291. OwnPtr<Messages::WindowServer::GetWallpaperResponse> ClientConnection::handle(const Messages::WindowServer::GetWallpaper&)
  292. {
  293. return make<Messages::WindowServer::GetWallpaperResponse>(Compositor::the().wallpaper_path());
  294. }
  295. OwnPtr<Messages::WindowServer::SetResolutionResponse> ClientConnection::handle(const Messages::WindowServer::SetResolution& message)
  296. {
  297. return make<Messages::WindowServer::SetResolutionResponse>(WindowManager::the().set_resolution(message.resolution().width(), message.resolution().height(), message.scale_factor()), WindowManager::the().resolution(), WindowManager::the().scale_factor());
  298. }
  299. OwnPtr<Messages::WindowServer::SetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowTitle& message)
  300. {
  301. auto it = m_windows.find(message.window_id());
  302. if (it == m_windows.end()) {
  303. did_misbehave("SetWindowTitle: Bad window ID");
  304. return {};
  305. }
  306. it->value->set_title(message.title());
  307. return make<Messages::WindowServer::SetWindowTitleResponse>();
  308. }
  309. OwnPtr<Messages::WindowServer::GetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowTitle& message)
  310. {
  311. auto it = m_windows.find(message.window_id());
  312. if (it == m_windows.end()) {
  313. did_misbehave("GetWindowTitle: Bad window ID");
  314. return {};
  315. }
  316. return make<Messages::WindowServer::GetWindowTitleResponse>(it->value->title());
  317. }
  318. OwnPtr<Messages::WindowServer::IsMaximizedResponse> ClientConnection::handle(const Messages::WindowServer::IsMaximized& message)
  319. {
  320. auto it = m_windows.find(message.window_id());
  321. if (it == m_windows.end()) {
  322. did_misbehave("IsMaximized: Bad window ID");
  323. return {};
  324. }
  325. return make<Messages::WindowServer::IsMaximizedResponse>(it->value->is_maximized());
  326. }
  327. OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
  328. {
  329. auto it = m_windows.find(message.window_id());
  330. if (it == m_windows.end()) {
  331. did_misbehave("SetWindowIconBitmap: Bad window ID");
  332. return {};
  333. }
  334. auto& window = *(*it).value;
  335. if (message.icon().is_valid()) {
  336. window.set_icon(*message.icon().bitmap());
  337. } else {
  338. window.set_default_icon();
  339. }
  340. window.frame().invalidate_title_bar();
  341. WindowManager::the().tell_wm_listeners_window_icon_changed(window);
  342. return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
  343. }
  344. OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
  345. {
  346. int window_id = message.window_id();
  347. auto it = m_windows.find(window_id);
  348. if (it == m_windows.end()) {
  349. did_misbehave("SetWindowRect: Bad window ID");
  350. return {};
  351. }
  352. auto& window = *(*it).value;
  353. if (window.is_fullscreen()) {
  354. dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
  355. return {};
  356. }
  357. if (message.rect().location() != window.rect().location()) {
  358. window.set_default_positioned(false);
  359. }
  360. auto rect = message.rect();
  361. window.apply_minimum_size(rect);
  362. window.set_rect(rect);
  363. window.nudge_into_desktop();
  364. window.request_update(window.rect());
  365. return make<Messages::WindowServer::SetWindowRectResponse>(window.rect());
  366. }
  367. OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& message)
  368. {
  369. int window_id = message.window_id();
  370. auto it = m_windows.find(window_id);
  371. if (it == m_windows.end()) {
  372. did_misbehave("GetWindowRect: Bad window ID");
  373. return {};
  374. }
  375. return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
  376. }
  377. OwnPtr<Messages::WindowServer::GetWindowRectInMenubarResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRectInMenubar& message)
  378. {
  379. int window_id = message.window_id();
  380. auto it = m_windows.find(window_id);
  381. if (it == m_windows.end()) {
  382. did_misbehave("GetWindowRectInMenubar: Bad window ID");
  383. return {};
  384. }
  385. return make<Messages::WindowServer::GetWindowRectInMenubarResponse>(it->value->rect_in_menubar());
  386. }
  387. Window* ClientConnection::window_from_id(i32 window_id)
  388. {
  389. auto it = m_windows.find(window_id);
  390. if (it == m_windows.end())
  391. return nullptr;
  392. return it->value.ptr();
  393. }
  394. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  395. {
  396. Window* parent_window = nullptr;
  397. if (message.parent_window_id()) {
  398. parent_window = window_from_id(message.parent_window_id());
  399. if (!parent_window) {
  400. did_misbehave("CreateWindow with bad parent_window_id");
  401. return {};
  402. }
  403. }
  404. int window_id = m_next_window_id++;
  405. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.frameless(), message.resizable(), message.fullscreen(), message.accessory(), parent_window);
  406. window->set_has_alpha_channel(message.has_alpha_channel());
  407. window->set_title(message.title());
  408. if (!message.fullscreen()) {
  409. auto rect = message.rect();
  410. if (message.auto_position() && window->type() == WindowType::Normal) {
  411. rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), message.rect().size() };
  412. window->set_default_positioned(true);
  413. }
  414. window->apply_minimum_size(rect);
  415. window->set_rect(rect);
  416. window->nudge_into_desktop();
  417. }
  418. if (window->type() == WindowType::Desktop) {
  419. window->set_rect(WindowManager::the().desktop_rect());
  420. window->recalculate_rect();
  421. }
  422. window->set_opacity(message.opacity());
  423. window->set_size_increment(message.size_increment());
  424. window->set_base_size(message.base_size());
  425. window->set_resize_aspect_ratio(message.resize_aspect_ratio());
  426. window->invalidate();
  427. if (window->type() == WindowType::MenuApplet)
  428. AppletManager::the().add_applet(*window);
  429. m_windows.set(window_id, move(window));
  430. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  431. }
  432. void ClientConnection::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  433. {
  434. for (auto& child_window : window.child_windows()) {
  435. if (!child_window)
  436. continue;
  437. ASSERT(child_window->window_id() != window.window_id());
  438. destroy_window(*child_window, destroyed_window_ids);
  439. }
  440. for (auto& accessory_window : window.accessory_windows()) {
  441. if (!accessory_window)
  442. continue;
  443. ASSERT(accessory_window->window_id() != window.window_id());
  444. destroy_window(*accessory_window, destroyed_window_ids);
  445. }
  446. destroyed_window_ids.append(window.window_id());
  447. if (window.type() == WindowType::MenuApplet)
  448. AppletManager::the().remove_applet(window);
  449. window.destroy();
  450. remove_child(window);
  451. m_windows.remove(window.window_id());
  452. }
  453. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  454. {
  455. auto it = m_windows.find(message.window_id());
  456. if (it == m_windows.end()) {
  457. did_misbehave("DestroyWindow: Bad window ID");
  458. return {};
  459. }
  460. auto& window = *(*it).value;
  461. Vector<i32> destroyed_window_ids;
  462. destroy_window(window, destroyed_window_ids);
  463. return make<Messages::WindowServer::DestroyWindowResponse>(destroyed_window_ids);
  464. }
  465. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  466. {
  467. auto rect_set = window.take_pending_paint_rects();
  468. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  469. return;
  470. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  471. }
  472. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  473. {
  474. auto it = m_windows.find(message.window_id());
  475. if (it == m_windows.end()) {
  476. did_misbehave("InvalidateRect: Bad window ID");
  477. return;
  478. }
  479. auto& window = *(*it).value;
  480. for (size_t i = 0; i < message.rects().size(); ++i)
  481. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  482. }
  483. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  484. {
  485. int window_id = message.window_id();
  486. auto it = m_windows.find(window_id);
  487. if (it == m_windows.end()) {
  488. did_misbehave("DidFinishPainting: Bad window ID");
  489. return;
  490. }
  491. auto& window = *(*it).value;
  492. for (auto& rect : message.rects())
  493. window.invalidate(rect);
  494. WindowSwitcher::the().refresh_if_needed();
  495. }
  496. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  497. {
  498. int window_id = message.window_id();
  499. auto it = m_windows.find(window_id);
  500. if (it == m_windows.end()) {
  501. did_misbehave("SetWindowBackingStore: Bad window ID");
  502. return {};
  503. }
  504. auto& window = *(*it).value;
  505. if (window.last_backing_store() && window.last_backing_store_serial() == message.serial()) {
  506. window.swap_backing_stores();
  507. } else {
  508. // FIXME: Plumb scale factor here eventually.
  509. auto backing_store = Gfx::Bitmap::create_with_anon_fd(
  510. message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
  511. message.anon_file().take_fd(),
  512. message.size(),
  513. 1,
  514. {},
  515. Gfx::Bitmap::ShouldCloseAnonymousFile::Yes);
  516. window.set_backing_store(move(backing_store), message.serial());
  517. }
  518. if (message.flush_immediately())
  519. window.invalidate(false);
  520. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  521. }
  522. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  523. {
  524. int window_id = message.window_id();
  525. auto it = m_windows.find(window_id);
  526. if (it == m_windows.end()) {
  527. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  528. return {};
  529. }
  530. it->value->set_global_cursor_tracking_enabled(message.enabled());
  531. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  532. }
  533. OwnPtr<Messages::WindowServer::SetWindowCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCursor& message)
  534. {
  535. auto it = m_windows.find(message.window_id());
  536. if (it == m_windows.end()) {
  537. did_misbehave("SetWindowCursor: Bad window ID");
  538. return {};
  539. }
  540. auto& window = *(*it).value;
  541. if (message.cursor_type() < 0 || message.cursor_type() >= (i32)Gfx::StandardCursor::__Count) {
  542. did_misbehave("SetWindowCursor: Bad cursor type");
  543. return {};
  544. }
  545. window.set_cursor(Cursor::create((Gfx::StandardCursor)message.cursor_type()));
  546. Compositor::the().invalidate_cursor();
  547. return make<Messages::WindowServer::SetWindowCursorResponse>();
  548. }
  549. OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomCursor& message)
  550. {
  551. auto it = m_windows.find(message.window_id());
  552. if (it == m_windows.end()) {
  553. did_misbehave("SetWindowCustomCursor: Bad window ID");
  554. return {};
  555. }
  556. auto& window = *(*it).value;
  557. if (!message.cursor().is_valid()) {
  558. did_misbehave("SetWindowCustomCursor: Bad cursor");
  559. return {};
  560. }
  561. window.set_cursor(Cursor::create(*message.cursor().bitmap()));
  562. Compositor::the().invalidate_cursor();
  563. return make<Messages::WindowServer::SetWindowCustomCursorResponse>();
  564. }
  565. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  566. {
  567. auto it = m_windows.find(message.window_id());
  568. if (it == m_windows.end()) {
  569. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  570. return {};
  571. }
  572. it->value->set_has_alpha_channel(message.has_alpha_channel());
  573. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  574. }
  575. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  576. {
  577. auto* client = ClientConnection::from_client_id(message.client_id());
  578. if (!client) {
  579. did_misbehave("WM_SetActiveWindow: Bad client ID");
  580. return;
  581. }
  582. auto it = client->m_windows.find(message.window_id());
  583. if (it == client->m_windows.end()) {
  584. did_misbehave("WM_SetActiveWindow: Bad window ID");
  585. return;
  586. }
  587. auto& window = *(*it).value;
  588. WindowManager::the().minimize_windows(window, false);
  589. WindowManager::the().move_to_front_and_make_active(window);
  590. }
  591. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  592. {
  593. auto* client = ClientConnection::from_client_id(message.client_id());
  594. if (!client) {
  595. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  596. return;
  597. }
  598. auto it = client->m_windows.find(message.window_id());
  599. if (it == client->m_windows.end()) {
  600. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  601. return;
  602. }
  603. auto& window = *(*it).value;
  604. if (auto* modal_window = window.blocking_modal_window()) {
  605. modal_window->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  606. } else {
  607. window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  608. }
  609. }
  610. void ClientConnection::handle(const Messages::WindowServer::StartWindowResize& request)
  611. {
  612. auto it = m_windows.find(request.window_id());
  613. if (it == m_windows.end()) {
  614. did_misbehave("WM_StartWindowResize: Bad window ID");
  615. return;
  616. }
  617. auto& window = *(*it).value;
  618. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  619. // Maybe the client should be allowed to specify what initiated this request?
  620. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  621. }
  622. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  623. {
  624. auto* client = ClientConnection::from_client_id(request.client_id());
  625. if (!client) {
  626. did_misbehave("WM_StartWindowResize: Bad client ID");
  627. return;
  628. }
  629. auto it = client->m_windows.find(request.window_id());
  630. if (it == client->m_windows.end()) {
  631. did_misbehave("WM_StartWindowResize: Bad window ID");
  632. return;
  633. }
  634. auto& window = *(*it).value;
  635. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  636. // Maybe the client should be allowed to specify what initiated this request?
  637. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  638. }
  639. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  640. {
  641. auto* client = ClientConnection::from_client_id(message.client_id());
  642. if (!client) {
  643. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  644. return;
  645. }
  646. auto it = client->m_windows.find(message.window_id());
  647. if (it == client->m_windows.end()) {
  648. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  649. return;
  650. }
  651. auto& window = *(*it).value;
  652. WindowManager::the().minimize_windows(window, message.minimized());
  653. }
  654. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  655. {
  656. return make<Messages::WindowServer::GreetResponse>(Screen::the().rect(), Gfx::current_system_theme_buffer());
  657. }
  658. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  659. {
  660. // Because the Taskbar (which should be the only user of this API) does not own the
  661. // window or the client id, there is a possibility that it may send this message for
  662. // a window or client that may have been destroyed already. This is not an error,
  663. // and we should not call did_misbehave() for either.
  664. auto* client = ClientConnection::from_client_id(message.client_id());
  665. if (!client)
  666. return;
  667. auto it = client->m_windows.find(message.window_id());
  668. if (it == client->m_windows.end())
  669. return;
  670. auto& window = *(*it).value;
  671. window.set_taskbar_rect(message.rect());
  672. }
  673. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  674. {
  675. auto& wm = WindowManager::the();
  676. if (wm.dnd_client())
  677. return make<Messages::WindowServer::StartDragResponse>(false);
  678. wm.start_dnd_drag(*this, message.text(), message.drag_bitmap().bitmap(), Core::MimeData::construct(message.mime_data()));
  679. return make<Messages::WindowServer::StartDragResponse>(true);
  680. }
  681. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  682. {
  683. auto it = m_menus.find(message.menu_id());
  684. if (it == m_menus.end()) {
  685. did_misbehave("SetSystemMenu called with invalid menu ID");
  686. return {};
  687. }
  688. auto& menu = it->value;
  689. MenuManager::the().set_system_menu(menu);
  690. return make<Messages::WindowServer::SetSystemMenuResponse>();
  691. }
  692. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  693. {
  694. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  695. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  696. }
  697. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  698. {
  699. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  700. auto name = wm_config->read_entry("Theme", "Name");
  701. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  702. }
  703. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  704. {
  705. auto it = m_windows.find(message.window_id());
  706. if (it == m_windows.end()) {
  707. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  708. return {};
  709. }
  710. auto& window = *it->value;
  711. window.set_base_size(message.base_size());
  712. window.set_size_increment(message.size_increment());
  713. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  714. }
  715. OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowResizeAspectRatio& message)
  716. {
  717. auto it = m_windows.find(message.window_id());
  718. if (it == m_windows.end()) {
  719. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  720. return {};
  721. }
  722. auto& window = *it->value;
  723. window.set_resize_aspect_ratio(message.resize_aspect_ratio());
  724. return make<Messages::WindowServer::SetWindowResizeAspectRatioResponse>();
  725. }
  726. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  727. {
  728. if (m_has_display_link)
  729. return;
  730. m_has_display_link = true;
  731. Compositor::the().increment_display_link_count({});
  732. }
  733. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  734. {
  735. if (!m_has_display_link)
  736. return;
  737. m_has_display_link = false;
  738. Compositor::the().decrement_display_link_count({});
  739. }
  740. void ClientConnection::notify_display_link(Badge<Compositor>)
  741. {
  742. if (!m_has_display_link)
  743. return;
  744. post_message(Messages::WindowClient::DisplayLinkNotification());
  745. }
  746. void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
  747. {
  748. auto it = m_windows.find(message.window_id());
  749. if (it == m_windows.end()) {
  750. did_misbehave("SetWindowProgress with bad window ID");
  751. return;
  752. }
  753. it->value->set_progress(message.progress());
  754. }
  755. void ClientConnection::handle(const Messages::WindowServer::RefreshSystemTheme&)
  756. {
  757. // Post the client an UpdateSystemTheme message to refresh its theme.
  758. post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  759. }
  760. void ClientConnection::handle(const Messages::WindowServer::Pong&)
  761. {
  762. m_ping_timer = nullptr;
  763. set_unresponsive(false);
  764. }
  765. OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> ClientConnection::handle(const Messages::WindowServer::GetGlobalCursorPosition&)
  766. {
  767. return make<Messages::WindowServer::GetGlobalCursorPositionResponse>(Screen::the().cursor_location());
  768. }
  769. OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::SetMouseAcceleration& message)
  770. {
  771. if (message.factor() < mouse_accel_min || message.factor() > mouse_accel_max) {
  772. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  773. return {};
  774. }
  775. WindowManager::the().set_acceleration_factor(message.factor());
  776. return make<Messages::WindowServer::SetMouseAccelerationResponse>();
  777. }
  778. OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::GetMouseAcceleration&)
  779. {
  780. return make<Messages::WindowServer::GetMouseAccelerationResponse>(Screen::the().acceleration_factor());
  781. }
  782. OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetScrollStepSize& message)
  783. {
  784. if (message.step_size() < scroll_step_size_min) {
  785. did_misbehave("SetScrollStepSize with bad scroll step size");
  786. return {};
  787. }
  788. WindowManager::the().set_scroll_step_size(message.step_size());
  789. return make<Messages::WindowServer::SetScrollStepSizeResponse>();
  790. }
  791. OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetScrollStepSize&)
  792. {
  793. return make<Messages::WindowServer::GetScrollStepSizeResponse>(Screen::the().scroll_step_size());
  794. }
  795. void ClientConnection::set_unresponsive(bool unresponsive)
  796. {
  797. if (m_unresponsive == unresponsive)
  798. return;
  799. m_unresponsive = unresponsive;
  800. for (auto& it : m_windows) {
  801. auto& window = *it.value;
  802. window.invalidate();
  803. if (unresponsive)
  804. window.set_cursor(WindowManager::the().wait_cursor());
  805. }
  806. Compositor::the().invalidate_cursor();
  807. }
  808. void ClientConnection::may_have_become_unresponsive()
  809. {
  810. post_message(Messages::WindowClient::Ping());
  811. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  812. set_unresponsive(true);
  813. });
  814. m_ping_timer->start();
  815. }
  816. void ClientConnection::did_become_responsive()
  817. {
  818. set_unresponsive(false);
  819. }
  820. }