ClientConnection.cpp 37 KB

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