ClientConnection.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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/StandardCursor.h>
  30. #include <LibGfx/SystemTheme.h>
  31. #include <WindowServer/AppletManager.h>
  32. #include <WindowServer/ClientConnection.h>
  33. #include <WindowServer/Compositor.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 {};
  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 {};
  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 {};
  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 {};
  165. }
  166. if (jt == m_menus.end()) {
  167. did_misbehave("AddMenuToMenubar: Bad menu ID");
  168. return {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  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(), message.scale_factor()), WindowManager::the().resolution(), WindowManager::the().scale_factor());
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  367. }
  368. auto& window = *(*it).value;
  369. if (window.is_fullscreen()) {
  370. dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
  371. return {};
  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 {};
  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 {};
  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 {};
  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 {};
  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 {};
  516. }
  517. auto& window = *(*it).value;
  518. if (window.last_backing_store() && window.last_backing_store_serial() == message.serial()) {
  519. window.swap_backing_stores();
  520. } else {
  521. auto backing_store = Gfx::Bitmap::create_with_anon_fd(
  522. message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
  523. message.anon_file().take_fd(),
  524. message.size(),
  525. Gfx::Bitmap::ShouldCloseAnonymousFile::Yes);
  526. window.set_backing_store(move(backing_store), message.serial());
  527. }
  528. if (message.flush_immediately())
  529. window.invalidate(false);
  530. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  531. }
  532. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  533. {
  534. int window_id = message.window_id();
  535. auto it = m_windows.find(window_id);
  536. if (it == m_windows.end()) {
  537. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  538. return {};
  539. }
  540. it->value->set_global_cursor_tracking_enabled(message.enabled());
  541. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  542. }
  543. OwnPtr<Messages::WindowServer::SetWindowCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCursor& message)
  544. {
  545. auto it = m_windows.find(message.window_id());
  546. if (it == m_windows.end()) {
  547. did_misbehave("SetWindowCursor: Bad window ID");
  548. return {};
  549. }
  550. auto& window = *(*it).value;
  551. if (message.cursor_type() < 0 || message.cursor_type() >= (i32)Gfx::StandardCursor::__Count) {
  552. did_misbehave("SetWindowCursor: Bad cursor type");
  553. return {};
  554. }
  555. window.set_cursor(Cursor::create((Gfx::StandardCursor)message.cursor_type()));
  556. Compositor::the().invalidate_cursor();
  557. return make<Messages::WindowServer::SetWindowCursorResponse>();
  558. }
  559. OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomCursor& message)
  560. {
  561. auto it = m_windows.find(message.window_id());
  562. if (it == m_windows.end()) {
  563. did_misbehave("SetWindowCustomCursor: Bad window ID");
  564. return {};
  565. }
  566. auto& window = *(*it).value;
  567. if (!message.cursor().is_valid()) {
  568. did_misbehave("SetWindowCustomCursor: Bad cursor");
  569. return {};
  570. }
  571. window.set_cursor(Cursor::create(*message.cursor().bitmap()));
  572. Compositor::the().invalidate_cursor();
  573. return make<Messages::WindowServer::SetWindowCustomCursorResponse>();
  574. }
  575. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  576. {
  577. auto it = m_windows.find(message.window_id());
  578. if (it == m_windows.end()) {
  579. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  580. return {};
  581. }
  582. it->value->set_has_alpha_channel(message.has_alpha_channel());
  583. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  584. }
  585. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  586. {
  587. auto* client = ClientConnection::from_client_id(message.client_id());
  588. if (!client) {
  589. did_misbehave("WM_SetActiveWindow: Bad client ID");
  590. return;
  591. }
  592. auto it = client->m_windows.find(message.window_id());
  593. if (it == client->m_windows.end()) {
  594. did_misbehave("WM_SetActiveWindow: Bad window ID");
  595. return;
  596. }
  597. auto& window = *(*it).value;
  598. WindowManager::the().minimize_windows(window, false);
  599. WindowManager::the().move_to_front_and_make_active(window);
  600. }
  601. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  602. {
  603. auto* client = ClientConnection::from_client_id(message.client_id());
  604. if (!client) {
  605. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  606. return;
  607. }
  608. auto it = client->m_windows.find(message.window_id());
  609. if (it == client->m_windows.end()) {
  610. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  611. return;
  612. }
  613. auto& window = *(*it).value;
  614. if (auto* modal_window = window.blocking_modal_window()) {
  615. modal_window->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  616. } else {
  617. window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  618. }
  619. }
  620. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  621. {
  622. auto* client = ClientConnection::from_client_id(request.client_id());
  623. if (!client) {
  624. did_misbehave("WM_StartWindowResize: Bad client ID");
  625. return;
  626. }
  627. auto it = client->m_windows.find(request.window_id());
  628. if (it == client->m_windows.end()) {
  629. did_misbehave("WM_StartWindowResize: Bad window ID");
  630. return;
  631. }
  632. auto& window = *(*it).value;
  633. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  634. // Maybe the client should be allowed to specify what initiated this request?
  635. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  636. }
  637. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  638. {
  639. auto* client = ClientConnection::from_client_id(message.client_id());
  640. if (!client) {
  641. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  642. return;
  643. }
  644. auto it = client->m_windows.find(message.window_id());
  645. if (it == client->m_windows.end()) {
  646. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  647. return;
  648. }
  649. auto& window = *(*it).value;
  650. WindowManager::the().minimize_windows(window, message.minimized());
  651. }
  652. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  653. {
  654. return make<Messages::WindowServer::GreetResponse>(client_id(), Screen::the().rect(), Gfx::current_system_theme_buffer_id());
  655. }
  656. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  657. {
  658. // Because the Taskbar (which should be the only user of this API) does not own the
  659. // window or the client id, there is a possibility that it may send this message for
  660. // a window or client that may have been destroyed already. This is not an error,
  661. // and we should not call did_misbehave() for either.
  662. auto* client = ClientConnection::from_client_id(message.client_id());
  663. if (!client)
  664. return;
  665. auto it = client->m_windows.find(message.window_id());
  666. if (it == client->m_windows.end())
  667. return;
  668. auto& window = *(*it).value;
  669. window.set_taskbar_rect(message.rect());
  670. }
  671. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  672. {
  673. auto& wm = WindowManager::the();
  674. if (wm.dnd_client())
  675. return make<Messages::WindowServer::StartDragResponse>(false);
  676. wm.start_dnd_drag(*this, message.text(), message.drag_bitmap().bitmap(), Core::MimeData::construct(message.mime_data()));
  677. return make<Messages::WindowServer::StartDragResponse>(true);
  678. }
  679. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  680. {
  681. auto it = m_menus.find(message.menu_id());
  682. if (it == m_menus.end()) {
  683. did_misbehave("SetSystemMenu called with invalid menu ID");
  684. return {};
  685. }
  686. auto& menu = it->value;
  687. MenuManager::the().set_system_menu(menu);
  688. return make<Messages::WindowServer::SetSystemMenuResponse>();
  689. }
  690. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  691. {
  692. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  693. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  694. }
  695. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  696. {
  697. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  698. auto name = wm_config->read_entry("Theme", "Name");
  699. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  700. }
  701. void ClientConnection::boost()
  702. {
  703. // FIXME: Re-enable this when we have a solution for boosting.
  704. #if 0
  705. if (set_process_boost(client_pid(), 10) < 0)
  706. perror("boost: set_process_boost");
  707. #endif
  708. }
  709. void ClientConnection::deboost()
  710. {
  711. // FIXME: Re-enable this when we have a solution for boosting.
  712. #if 0
  713. if (set_process_boost(client_pid(), 0) < 0)
  714. perror("deboost: set_process_boost");
  715. #endif
  716. }
  717. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  718. {
  719. auto it = m_windows.find(message.window_id());
  720. if (it == m_windows.end()) {
  721. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  722. return {};
  723. }
  724. auto& window = *it->value;
  725. window.set_base_size(message.base_size());
  726. window.set_size_increment(message.size_increment());
  727. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  728. }
  729. OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowResizeAspectRatio& message)
  730. {
  731. auto it = m_windows.find(message.window_id());
  732. if (it == m_windows.end()) {
  733. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  734. return {};
  735. }
  736. auto& window = *it->value;
  737. window.set_resize_aspect_ratio(message.resize_aspect_ratio());
  738. return make<Messages::WindowServer::SetWindowResizeAspectRatioResponse>();
  739. }
  740. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  741. {
  742. if (m_has_display_link)
  743. return;
  744. m_has_display_link = true;
  745. Compositor::the().increment_display_link_count({});
  746. }
  747. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  748. {
  749. if (!m_has_display_link)
  750. return;
  751. m_has_display_link = false;
  752. Compositor::the().decrement_display_link_count({});
  753. }
  754. void ClientConnection::notify_display_link(Badge<Compositor>)
  755. {
  756. if (!m_has_display_link)
  757. return;
  758. post_message(Messages::WindowClient::DisplayLinkNotification());
  759. }
  760. void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
  761. {
  762. auto it = m_windows.find(message.window_id());
  763. if (it == m_windows.end()) {
  764. did_misbehave("SetWindowProgress with bad window ID");
  765. return;
  766. }
  767. it->value->set_progress(message.progress());
  768. }
  769. void ClientConnection::handle(const Messages::WindowServer::RefreshSystemTheme&)
  770. {
  771. // Post the client an UpdateSystemTheme message to refresh its theme.
  772. post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  773. }
  774. void ClientConnection::handle(const Messages::WindowServer::Pong&)
  775. {
  776. m_ping_timer = nullptr;
  777. set_unresponsive(false);
  778. }
  779. OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> ClientConnection::handle(const Messages::WindowServer::GetGlobalCursorPosition&)
  780. {
  781. return make<Messages::WindowServer::GetGlobalCursorPositionResponse>(Screen::the().cursor_location());
  782. }
  783. OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::SetMouseAcceleration& message)
  784. {
  785. if (message.factor() < mouse_accel_min || message.factor() > mouse_accel_max) {
  786. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  787. return {};
  788. }
  789. WindowManager::the().set_acceleration_factor(message.factor());
  790. return make<Messages::WindowServer::SetMouseAccelerationResponse>();
  791. }
  792. OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::GetMouseAcceleration&)
  793. {
  794. return make<Messages::WindowServer::GetMouseAccelerationResponse>(Screen::the().acceleration_factor());
  795. }
  796. OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetScrollStepSize& message)
  797. {
  798. if (message.step_size() < scroll_step_size_min) {
  799. did_misbehave("SetScrollStepSize with bad scroll step size");
  800. return {};
  801. }
  802. WindowManager::the().set_scroll_step_size(message.step_size());
  803. return make<Messages::WindowServer::SetScrollStepSizeResponse>();
  804. }
  805. OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetScrollStepSize&)
  806. {
  807. return make<Messages::WindowServer::GetScrollStepSizeResponse>(Screen::the().scroll_step_size());
  808. }
  809. void ClientConnection::set_unresponsive(bool unresponsive)
  810. {
  811. if (m_unresponsive == unresponsive)
  812. return;
  813. m_unresponsive = unresponsive;
  814. for (auto& it : m_windows) {
  815. auto& window = *it.value;
  816. window.invalidate();
  817. if (unresponsive)
  818. window.set_cursor(WindowManager::the().wait_cursor());
  819. }
  820. Compositor::the().invalidate_cursor();
  821. }
  822. void ClientConnection::may_have_become_unresponsive()
  823. {
  824. post_message(Messages::WindowClient::Ping());
  825. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  826. set_unresponsive(true);
  827. });
  828. }
  829. void ClientConnection::did_become_responsive()
  830. {
  831. set_unresponsive(false);
  832. }
  833. }