ClientConnection.cpp 33 KB

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