ClientConnection.cpp 34 KB

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