ClientConnection.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibGfx/StandardCursor.h>
  9. #include <LibGfx/SystemTheme.h>
  10. #include <WindowServer/AppletManager.h>
  11. #include <WindowServer/ClientConnection.h>
  12. #include <WindowServer/Compositor.h>
  13. #include <WindowServer/Menu.h>
  14. #include <WindowServer/MenuItem.h>
  15. #include <WindowServer/Screen.h>
  16. #include <WindowServer/Window.h>
  17. #include <WindowServer/WindowClientEndpoint.h>
  18. #include <WindowServer/WindowManager.h>
  19. #include <WindowServer/WindowSwitcher.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. namespace WindowServer {
  24. HashMap<int, NonnullRefPtr<ClientConnection>>* s_connections;
  25. void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
  26. {
  27. if (!s_connections)
  28. return;
  29. for (auto& it : *s_connections) {
  30. callback(*it.value);
  31. }
  32. }
  33. ClientConnection* ClientConnection::from_client_id(int client_id)
  34. {
  35. if (!s_connections)
  36. return nullptr;
  37. auto it = s_connections->find(client_id);
  38. if (it == s_connections->end())
  39. return nullptr;
  40. return (*it).value.ptr();
  41. }
  42. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  43. : IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
  44. {
  45. if (!s_connections)
  46. s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;
  47. s_connections->set(client_id, *this);
  48. auto& wm = WindowManager::the();
  49. async_fast_greet(Screen::rects(), Screen::main().index(), wm.window_stack_rows(), wm.window_stack_columns(), Gfx::current_system_theme_buffer(), Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), client_id);
  50. }
  51. ClientConnection::~ClientConnection()
  52. {
  53. auto& wm = WindowManager::the();
  54. if (wm.dnd_client() == this)
  55. wm.end_dnd_drag();
  56. if (m_has_display_link)
  57. Compositor::the().decrement_display_link_count({});
  58. MenuManager::the().close_all_menus_from_client({}, *this);
  59. auto windows = move(m_windows);
  60. for (auto& window : windows) {
  61. window.value->detach_client({});
  62. if (window.value->type() == WindowType::Applet)
  63. AppletManager::the().remove_applet(window.value);
  64. }
  65. if (m_show_screen_number)
  66. Compositor::the().decrement_show_screen_number({});
  67. }
  68. void ClientConnection::die()
  69. {
  70. deferred_invoke([this] {
  71. s_connections->remove(client_id());
  72. });
  73. }
  74. void ClientConnection::notify_about_new_screen_rects()
  75. {
  76. auto& wm = WindowManager::the();
  77. async_screen_rects_changed(Screen::rects(), Screen::main().index(), wm.window_stack_rows(), wm.window_stack_columns());
  78. }
  79. void ClientConnection::create_menu(i32 menu_id, String const& menu_title)
  80. {
  81. auto menu = Menu::construct(this, menu_id, menu_title);
  82. m_menus.set(menu_id, move(menu));
  83. }
  84. void ClientConnection::destroy_menu(i32 menu_id)
  85. {
  86. auto it = m_menus.find(menu_id);
  87. if (it == m_menus.end()) {
  88. did_misbehave("DestroyMenu: Bad menu ID");
  89. return;
  90. }
  91. auto& menu = *(*it).value;
  92. menu.close();
  93. m_menus.remove(it);
  94. remove_child(menu);
  95. }
  96. void ClientConnection::add_menu(i32 window_id, i32 menu_id)
  97. {
  98. auto it = m_windows.find(window_id);
  99. auto jt = m_menus.find(menu_id);
  100. if (it == m_windows.end()) {
  101. did_misbehave("AddMenu: Bad window ID");
  102. return;
  103. }
  104. if (jt == m_menus.end()) {
  105. did_misbehave("AddMenu: Bad menu ID");
  106. return;
  107. }
  108. auto& window = *(*it).value;
  109. auto& menu = *(*jt).value;
  110. window.add_menu(menu);
  111. }
  112. void ClientConnection::add_menu_item(i32 menu_id, i32 identifier, i32 submenu_id,
  113. String const& text, bool enabled, bool checkable, bool checked, bool is_default,
  114. String const& shortcut, Gfx::ShareableBitmap const& icon, bool exclusive)
  115. {
  116. auto it = m_menus.find(menu_id);
  117. if (it == m_menus.end()) {
  118. dbgln("AddMenuItem: Bad menu ID: {}", menu_id);
  119. return;
  120. }
  121. auto& menu = *(*it).value;
  122. auto menu_item = make<MenuItem>(menu, identifier, text, shortcut, enabled, checkable, checked);
  123. if (is_default)
  124. menu_item->set_default(true);
  125. menu_item->set_icon(icon.bitmap());
  126. menu_item->set_submenu_id(submenu_id);
  127. menu_item->set_exclusive(exclusive);
  128. menu.add_item(move(menu_item));
  129. }
  130. void ClientConnection::popup_menu(i32 menu_id, Gfx::IntPoint const& screen_position)
  131. {
  132. auto position = screen_position;
  133. auto it = m_menus.find(menu_id);
  134. if (it == m_menus.end()) {
  135. did_misbehave("PopupMenu: Bad menu ID");
  136. return;
  137. }
  138. auto& menu = *(*it).value;
  139. menu.popup(position);
  140. }
  141. void ClientConnection::dismiss_menu(i32 menu_id)
  142. {
  143. auto it = m_menus.find(menu_id);
  144. if (it == m_menus.end()) {
  145. did_misbehave("DismissMenu: Bad menu ID");
  146. return;
  147. }
  148. auto& menu = *(*it).value;
  149. menu.close();
  150. }
  151. void ClientConnection::update_menu_item(i32 menu_id, i32 identifier, [[maybe_unused]] i32 submenu_id,
  152. String const& text, bool enabled, bool checkable, bool checked, bool is_default,
  153. String const& shortcut)
  154. {
  155. auto it = m_menus.find(menu_id);
  156. if (it == m_menus.end()) {
  157. did_misbehave("UpdateMenuItem: Bad menu ID");
  158. return;
  159. }
  160. auto& menu = *(*it).value;
  161. auto* menu_item = menu.item_with_identifier(identifier);
  162. if (!menu_item) {
  163. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  164. return;
  165. }
  166. menu_item->set_text(text);
  167. menu_item->set_shortcut_text(shortcut);
  168. menu_item->set_enabled(enabled);
  169. menu_item->set_checkable(checkable);
  170. menu_item->set_default(is_default);
  171. if (checkable)
  172. menu_item->set_checked(checked);
  173. }
  174. void ClientConnection::add_menu_separator(i32 menu_id)
  175. {
  176. auto it = m_menus.find(menu_id);
  177. if (it == m_menus.end()) {
  178. did_misbehave("AddMenuSeparator: Bad menu ID");
  179. return;
  180. }
  181. auto& menu = *(*it).value;
  182. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  183. }
  184. void ClientConnection::move_window_to_front(i32 window_id)
  185. {
  186. auto it = m_windows.find(window_id);
  187. if (it == m_windows.end()) {
  188. did_misbehave("MoveWindowToFront: Bad window ID");
  189. return;
  190. }
  191. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  192. }
  193. void ClientConnection::set_fullscreen(i32 window_id, bool fullscreen)
  194. {
  195. auto it = m_windows.find(window_id);
  196. if (it == m_windows.end()) {
  197. did_misbehave("SetFullscreen: Bad window ID");
  198. return;
  199. }
  200. it->value->set_fullscreen(fullscreen);
  201. }
  202. void ClientConnection::set_frameless(i32 window_id, bool frameless)
  203. {
  204. auto it = m_windows.find(window_id);
  205. if (it == m_windows.end()) {
  206. did_misbehave("SetFrameless: Bad window ID");
  207. return;
  208. }
  209. it->value->set_frameless(frameless);
  210. WindowManager::the().tell_wms_window_state_changed(*it->value);
  211. }
  212. void ClientConnection::set_forced_shadow(i32 window_id, bool shadow)
  213. {
  214. auto it = m_windows.find(window_id);
  215. if (it == m_windows.end()) {
  216. did_misbehave("SetForcedShadow: Bad window ID");
  217. return;
  218. }
  219. it->value->set_forced_shadow(shadow);
  220. it->value->invalidate();
  221. Compositor::the().invalidate_occlusions();
  222. }
  223. void ClientConnection::set_window_opacity(i32 window_id, float opacity)
  224. {
  225. auto it = m_windows.find(window_id);
  226. if (it == m_windows.end()) {
  227. did_misbehave("SetWindowOpacity: Bad window ID");
  228. return;
  229. }
  230. it->value->set_opacity(opacity);
  231. }
  232. void ClientConnection::set_wallpaper(String const& path)
  233. {
  234. Compositor::the().set_wallpaper(path, [&](bool success) {
  235. async_set_wallpaper_finished(success);
  236. });
  237. }
  238. void ClientConnection::set_background_color(String const& background_color)
  239. {
  240. Compositor::the().set_background_color(background_color);
  241. }
  242. void ClientConnection::set_wallpaper_mode(String const& mode)
  243. {
  244. Compositor::the().set_wallpaper_mode(mode);
  245. }
  246. Messages::WindowServer::GetWallpaperResponse ClientConnection::get_wallpaper()
  247. {
  248. return Compositor::the().wallpaper_path();
  249. }
  250. Messages::WindowServer::SetScreenLayoutResponse ClientConnection::set_screen_layout(ScreenLayout const& screen_layout, bool save)
  251. {
  252. String error_msg;
  253. bool success = WindowManager::the().set_screen_layout(ScreenLayout(screen_layout), save, error_msg);
  254. return { success, move(error_msg) };
  255. }
  256. Messages::WindowServer::GetScreenLayoutResponse ClientConnection::get_screen_layout()
  257. {
  258. return { WindowManager::the().get_screen_layout() };
  259. }
  260. Messages::WindowServer::SaveScreenLayoutResponse ClientConnection::save_screen_layout()
  261. {
  262. String error_msg;
  263. bool success = WindowManager::the().save_screen_layout(error_msg);
  264. return { success, move(error_msg) };
  265. }
  266. Messages::WindowServer::ApplyVirtualDesktopSettingsResponse ClientConnection::apply_virtual_desktop_settings(u32 rows, u32 columns, bool save)
  267. {
  268. if (rows == 0 || columns == 0 || rows > WindowManager::max_window_stack_rows || columns > WindowManager::max_window_stack_columns)
  269. return { false };
  270. return { WindowManager::the().apply_virtual_desktop_settings(rows, columns, save) };
  271. }
  272. Messages::WindowServer::GetVirtualDesktopSettingsResponse ClientConnection::get_virtual_desktop_settings()
  273. {
  274. auto& wm = WindowManager::the();
  275. return { (unsigned)wm.window_stack_rows(), (unsigned)wm.window_stack_columns(), WindowManager::max_window_stack_rows, WindowManager::max_window_stack_columns };
  276. }
  277. void ClientConnection::show_screen_numbers(bool show)
  278. {
  279. if (m_show_screen_number == show)
  280. return;
  281. m_show_screen_number = show;
  282. if (show)
  283. Compositor::the().increment_show_screen_number({});
  284. else
  285. Compositor::the().decrement_show_screen_number({});
  286. }
  287. void ClientConnection::set_window_title(i32 window_id, String const& title)
  288. {
  289. auto it = m_windows.find(window_id);
  290. if (it == m_windows.end()) {
  291. did_misbehave("SetWindowTitle: Bad window ID");
  292. return;
  293. }
  294. it->value->set_title(title);
  295. }
  296. Messages::WindowServer::GetWindowTitleResponse ClientConnection::get_window_title(i32 window_id)
  297. {
  298. auto it = m_windows.find(window_id);
  299. if (it == m_windows.end()) {
  300. did_misbehave("GetWindowTitle: Bad window ID");
  301. return nullptr;
  302. }
  303. return it->value->title();
  304. }
  305. Messages::WindowServer::IsMaximizedResponse ClientConnection::is_maximized(i32 window_id)
  306. {
  307. auto it = m_windows.find(window_id);
  308. if (it == m_windows.end()) {
  309. did_misbehave("IsMaximized: Bad window ID");
  310. return nullptr;
  311. }
  312. return it->value->is_maximized();
  313. }
  314. void ClientConnection::set_maximized(i32 window_id, bool maximized)
  315. {
  316. auto it = m_windows.find(window_id);
  317. if (it == m_windows.end()) {
  318. did_misbehave("SetMaximized: Bad window ID");
  319. return;
  320. }
  321. it->value->set_maximized(maximized);
  322. }
  323. void ClientConnection::set_window_icon_bitmap(i32 window_id, Gfx::ShareableBitmap const& icon)
  324. {
  325. auto it = m_windows.find(window_id);
  326. if (it == m_windows.end()) {
  327. did_misbehave("SetWindowIconBitmap: Bad window ID");
  328. return;
  329. }
  330. auto& window = *(*it).value;
  331. if (icon.is_valid()) {
  332. window.set_icon(*icon.bitmap());
  333. } else {
  334. window.set_default_icon();
  335. }
  336. window.frame().invalidate_titlebar();
  337. WindowManager::the().tell_wms_window_icon_changed(window);
  338. }
  339. Messages::WindowServer::SetWindowRectResponse ClientConnection::set_window_rect(i32 window_id, Gfx::IntRect const& rect)
  340. {
  341. auto it = m_windows.find(window_id);
  342. if (it == m_windows.end()) {
  343. did_misbehave("SetWindowRect: Bad window ID");
  344. return nullptr;
  345. }
  346. auto& window = *(*it).value;
  347. if (window.is_fullscreen()) {
  348. dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
  349. return nullptr;
  350. }
  351. if (rect.width() > INT16_MAX || rect.height() > INT16_MAX) {
  352. did_misbehave(String::formatted("SetWindowRect: Bad window sizing(width={}, height={}), dimension exceeds INT16_MAX", rect.width(), rect.height()).characters());
  353. return nullptr;
  354. }
  355. if (rect.location() != window.rect().location()) {
  356. window.set_default_positioned(false);
  357. }
  358. auto new_rect = rect;
  359. window.apply_minimum_size(new_rect);
  360. window.set_rect(new_rect);
  361. window.nudge_into_desktop(nullptr);
  362. window.request_update(window.rect());
  363. return window.rect();
  364. }
  365. Messages::WindowServer::GetWindowRectResponse ClientConnection::get_window_rect(i32 window_id)
  366. {
  367. auto it = m_windows.find(window_id);
  368. if (it == m_windows.end()) {
  369. did_misbehave("GetWindowRect: Bad window ID");
  370. return nullptr;
  371. }
  372. return it->value->rect();
  373. }
  374. void ClientConnection::set_window_minimum_size(i32 window_id, Gfx::IntSize const& size)
  375. {
  376. auto it = m_windows.find(window_id);
  377. if (it == m_windows.end()) {
  378. did_misbehave("SetWindowMinimumSize: Bad window ID");
  379. return;
  380. }
  381. auto& window = *(*it).value;
  382. if (window.is_fullscreen()) {
  383. dbgln("ClientConnection: Ignoring SetWindowMinimumSize request for fullscreen window");
  384. return;
  385. }
  386. window.set_minimum_size(size);
  387. if (window.width() < window.minimum_size().width() || window.height() < window.minimum_size().height()) {
  388. // New minimum size is larger than the current window size, resize accordingly.
  389. auto new_rect = window.rect();
  390. bool did_size_clamp = window.apply_minimum_size(new_rect);
  391. window.set_rect(new_rect);
  392. window.nudge_into_desktop(nullptr);
  393. window.request_update(window.rect());
  394. if (did_size_clamp)
  395. window.refresh_client_size();
  396. }
  397. }
  398. Messages::WindowServer::GetWindowMinimumSizeResponse ClientConnection::get_window_minimum_size(i32 window_id)
  399. {
  400. auto it = m_windows.find(window_id);
  401. if (it == m_windows.end()) {
  402. did_misbehave("GetWindowMinimumSize: Bad window ID");
  403. return nullptr;
  404. }
  405. return it->value->minimum_size();
  406. }
  407. Messages::WindowServer::GetAppletRectOnScreenResponse ClientConnection::get_applet_rect_on_screen(i32 window_id)
  408. {
  409. auto it = m_windows.find(window_id);
  410. if (it == m_windows.end()) {
  411. did_misbehave("GetAppletRectOnScreen: Bad window ID");
  412. return nullptr;
  413. }
  414. Gfx::IntRect applet_area_rect;
  415. if (auto* applet_area_window = AppletManager::the().window())
  416. applet_area_rect = applet_area_window->rect();
  417. return it->value->rect_in_applet_area().translated(applet_area_rect.location());
  418. }
  419. Window* ClientConnection::window_from_id(i32 window_id)
  420. {
  421. auto it = m_windows.find(window_id);
  422. if (it == m_windows.end())
  423. return nullptr;
  424. return it->value.ptr();
  425. }
  426. void ClientConnection::create_window(i32 window_id, Gfx::IntRect const& rect,
  427. bool auto_position, bool has_alpha_channel, bool modal, bool minimizable, bool resizable,
  428. bool fullscreen, bool frameless, bool forced_shadow, bool accessory, float opacity,
  429. float alpha_hit_threshold, Gfx::IntSize const& base_size, Gfx::IntSize const& size_increment,
  430. Gfx::IntSize const& minimum_size, Optional<Gfx::IntSize> const& resize_aspect_ratio, i32 type,
  431. String const& title, i32 parent_window_id, Gfx::IntRect const& launch_origin_rect)
  432. {
  433. Window* parent_window = nullptr;
  434. if (parent_window_id) {
  435. parent_window = window_from_id(parent_window_id);
  436. if (!parent_window) {
  437. did_misbehave("CreateWindow with bad parent_window_id");
  438. return;
  439. }
  440. }
  441. if (type < 0 || type >= (i32)WindowType::_Count) {
  442. did_misbehave("CreateWindow with a bad type");
  443. return;
  444. }
  445. if (m_windows.contains(window_id)) {
  446. did_misbehave("CreateWindow with already-used window ID");
  447. return;
  448. }
  449. auto window = Window::construct(*this, (WindowType)type, window_id, modal, minimizable, frameless, resizable, fullscreen, accessory, parent_window);
  450. window->set_forced_shadow(forced_shadow);
  451. if (!launch_origin_rect.is_empty())
  452. window->start_launch_animation(launch_origin_rect);
  453. window->set_has_alpha_channel(has_alpha_channel);
  454. window->set_title(title);
  455. if (!fullscreen) {
  456. Gfx::IntRect new_rect = rect;
  457. if (auto_position && window->is_movable()) {
  458. new_rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), rect.size() };
  459. window->set_default_positioned(true);
  460. }
  461. window->set_minimum_size(minimum_size);
  462. bool did_size_clamp = window->apply_minimum_size(new_rect);
  463. window->set_rect(new_rect);
  464. window->nudge_into_desktop(nullptr);
  465. if (did_size_clamp)
  466. window->refresh_client_size();
  467. }
  468. if (window->type() == WindowType::Desktop) {
  469. window->set_rect(Screen::bounding_rect());
  470. window->recalculate_rect();
  471. }
  472. window->set_opacity(opacity);
  473. window->set_alpha_hit_threshold(alpha_hit_threshold);
  474. window->set_size_increment(size_increment);
  475. window->set_base_size(base_size);
  476. if (resize_aspect_ratio.has_value() && !resize_aspect_ratio.value().is_null())
  477. window->set_resize_aspect_ratio(resize_aspect_ratio);
  478. window->invalidate(true, true);
  479. if (window->type() == WindowType::Applet)
  480. AppletManager::the().add_applet(*window);
  481. m_windows.set(window_id, move(window));
  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. VERIFY(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. VERIFY(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::Applet)
  499. AppletManager::the().remove_applet(window);
  500. window.destroy();
  501. remove_child(window);
  502. m_windows.remove(window.window_id());
  503. }
  504. Messages::WindowServer::DestroyWindowResponse ClientConnection::destroy_window(i32 window_id)
  505. {
  506. auto it = m_windows.find(window_id);
  507. if (it == m_windows.end()) {
  508. did_misbehave("DestroyWindow: Bad window ID");
  509. return nullptr;
  510. }
  511. auto& window = *(*it).value;
  512. Vector<i32> destroyed_window_ids;
  513. destroy_window(window, destroyed_window_ids);
  514. return 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. async_paint(window.window_id(), window.size(), rect_set.rects());
  522. }
  523. void ClientConnection::invalidate_rect(i32 window_id, Vector<Gfx::IntRect> const& rects, bool ignore_occlusion)
  524. {
  525. auto it = m_windows.find(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 < rects.size(); ++i)
  532. window.request_update(rects[i].intersected({ {}, window.size() }), ignore_occlusion);
  533. }
  534. void ClientConnection::did_finish_painting(i32 window_id, Vector<Gfx::IntRect> const& rects)
  535. {
  536. auto it = m_windows.find(window_id);
  537. if (it == m_windows.end()) {
  538. did_misbehave("DidFinishPainting: Bad window ID");
  539. return;
  540. }
  541. auto& window = *(*it).value;
  542. for (auto& rect : rects)
  543. window.invalidate(rect);
  544. if (window.has_alpha_channel() && window.alpha_hit_threshold() > 0.0f)
  545. WindowManager::the().reevaluate_hovered_window(&window);
  546. WindowSwitcher::the().refresh_if_needed();
  547. }
  548. void ClientConnection::set_window_backing_store(i32 window_id, [[maybe_unused]] i32 bpp,
  549. [[maybe_unused]] i32 pitch, IPC::File const& anon_file, i32 serial, bool has_alpha_channel,
  550. Gfx::IntSize const& size, bool flush_immediately)
  551. {
  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() == serial) {
  559. window.swap_backing_stores();
  560. } else {
  561. // FIXME: Plumb scale factor here eventually.
  562. Core::AnonymousBuffer buffer = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), pitch * size.height());
  563. if (!buffer.is_valid()) {
  564. did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store");
  565. return;
  566. }
  567. auto backing_store = Gfx::Bitmap::try_create_with_anonymous_buffer(
  568. has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
  569. move(buffer),
  570. size,
  571. 1,
  572. {});
  573. window.set_backing_store(move(backing_store), serial);
  574. }
  575. if (flush_immediately)
  576. window.invalidate(false);
  577. }
  578. void ClientConnection::set_global_mouse_tracking(bool enabled)
  579. {
  580. m_does_global_mouse_tracking = enabled;
  581. }
  582. void ClientConnection::set_window_cursor(i32 window_id, i32 cursor_type)
  583. {
  584. auto it = m_windows.find(window_id);
  585. if (it == m_windows.end()) {
  586. did_misbehave("SetWindowCursor: Bad window ID");
  587. return;
  588. }
  589. auto& window = *(*it).value;
  590. if (cursor_type < 0 || cursor_type >= (i32)Gfx::StandardCursor::__Count) {
  591. did_misbehave("SetWindowCursor: Bad cursor type");
  592. return;
  593. }
  594. window.set_cursor(Cursor::create((Gfx::StandardCursor)cursor_type));
  595. if (&window == WindowManager::the().hovered_window())
  596. Compositor::the().invalidate_cursor();
  597. }
  598. void ClientConnection::set_window_custom_cursor(i32 window_id, Gfx::ShareableBitmap const& cursor)
  599. {
  600. auto it = m_windows.find(window_id);
  601. if (it == m_windows.end()) {
  602. did_misbehave("SetWindowCustomCursor: Bad window ID");
  603. return;
  604. }
  605. auto& window = *(*it).value;
  606. if (!cursor.is_valid()) {
  607. did_misbehave("SetWindowCustomCursor: Bad cursor");
  608. return;
  609. }
  610. window.set_cursor(Cursor::create(*cursor.bitmap(), 1));
  611. Compositor::the().invalidate_cursor();
  612. }
  613. void ClientConnection::set_window_has_alpha_channel(i32 window_id, bool has_alpha_channel)
  614. {
  615. auto it = m_windows.find(window_id);
  616. if (it == m_windows.end()) {
  617. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  618. return;
  619. }
  620. it->value->set_has_alpha_channel(has_alpha_channel);
  621. }
  622. void ClientConnection::set_window_alpha_hit_threshold(i32 window_id, float threshold)
  623. {
  624. auto it = m_windows.find(window_id);
  625. if (it == m_windows.end()) {
  626. did_misbehave("SetWindowAlphaHitThreshold: Bad window ID");
  627. return;
  628. }
  629. it->value->set_alpha_hit_threshold(threshold);
  630. }
  631. void ClientConnection::start_window_resize(i32 window_id)
  632. {
  633. auto it = m_windows.find(window_id);
  634. if (it == m_windows.end()) {
  635. did_misbehave("WM_StartWindowResize: Bad window ID");
  636. return;
  637. }
  638. auto& window = *(*it).value;
  639. if (!window.is_resizable()) {
  640. dbgln("Client wants to start resizing a non-resizable window");
  641. return;
  642. }
  643. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  644. // Maybe the client should be allowed to specify what initiated this request?
  645. WindowManager::the().start_window_resize(window, ScreenInput::the().cursor_location(), MouseButton::Left);
  646. }
  647. Messages::WindowServer::StartDragResponse ClientConnection::start_drag(String const& text, HashMap<String, ByteBuffer> const& mime_data, Gfx::ShareableBitmap const& drag_bitmap)
  648. {
  649. auto& wm = WindowManager::the();
  650. if (wm.dnd_client())
  651. return false;
  652. wm.start_dnd_drag(*this, text, drag_bitmap.bitmap(), Core::MimeData::construct(mime_data));
  653. return true;
  654. }
  655. Messages::WindowServer::SetSystemThemeResponse ClientConnection::set_system_theme(String const& theme_path, String const& theme_name)
  656. {
  657. bool success = WindowManager::the().update_theme(theme_path, theme_name);
  658. return success;
  659. }
  660. Messages::WindowServer::GetSystemThemeResponse ClientConnection::get_system_theme()
  661. {
  662. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini");
  663. auto name = wm_config->read_entry("Theme", "Name");
  664. return name;
  665. }
  666. void ClientConnection::apply_cursor_theme(String const& name)
  667. {
  668. WindowManager::the().apply_cursor_theme(name);
  669. }
  670. Messages::WindowServer::GetCursorThemeResponse ClientConnection::get_cursor_theme()
  671. {
  672. auto config = Core::ConfigFile::open("/etc/WindowServer.ini");
  673. auto name = config->read_entry("Mouse", "CursorTheme");
  674. return name;
  675. }
  676. Messages::WindowServer::SetSystemFontsResponse ClientConnection::set_system_fonts(String const& default_font_query, String const& fixed_width_font_query)
  677. {
  678. if (!Gfx::FontDatabase::the().get_by_name(default_font_query)
  679. || !Gfx::FontDatabase::the().get_by_name(fixed_width_font_query)) {
  680. dbgln("Received unusable font queries: '{}' and '{}'", default_font_query, fixed_width_font_query);
  681. return false;
  682. }
  683. dbgln("Updating fonts: '{}' and '{}'", default_font_query, fixed_width_font_query);
  684. Gfx::FontDatabase::set_default_font_query(default_font_query);
  685. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  686. ClientConnection::for_each_client([&](auto& client) {
  687. client.async_update_system_fonts(default_font_query, fixed_width_font_query);
  688. });
  689. WindowManager::the().invalidate_after_theme_or_font_change();
  690. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini", Core::ConfigFile::AllowWriting::Yes);
  691. wm_config->write_entry("Fonts", "Default", default_font_query);
  692. wm_config->write_entry("Fonts", "FixedWidth", fixed_width_font_query);
  693. return true;
  694. }
  695. void ClientConnection::set_window_base_size_and_size_increment(i32 window_id, Gfx::IntSize const& base_size, Gfx::IntSize const& size_increment)
  696. {
  697. auto it = m_windows.find(window_id);
  698. if (it == m_windows.end()) {
  699. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  700. return;
  701. }
  702. auto& window = *it->value;
  703. window.set_base_size(base_size);
  704. window.set_size_increment(size_increment);
  705. }
  706. void ClientConnection::set_window_resize_aspect_ratio(i32 window_id, Optional<Gfx::IntSize> const& resize_aspect_ratio)
  707. {
  708. auto it = m_windows.find(window_id);
  709. if (it == m_windows.end()) {
  710. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  711. return;
  712. }
  713. auto& window = *it->value;
  714. window.set_resize_aspect_ratio(resize_aspect_ratio);
  715. }
  716. void ClientConnection::enable_display_link()
  717. {
  718. if (m_has_display_link)
  719. return;
  720. m_has_display_link = true;
  721. Compositor::the().increment_display_link_count({});
  722. }
  723. void ClientConnection::disable_display_link()
  724. {
  725. if (!m_has_display_link)
  726. return;
  727. m_has_display_link = false;
  728. Compositor::the().decrement_display_link_count({});
  729. }
  730. void ClientConnection::notify_display_link(Badge<Compositor>)
  731. {
  732. if (!m_has_display_link)
  733. return;
  734. async_display_link_notification();
  735. }
  736. void ClientConnection::set_window_progress(i32 window_id, Optional<i32> const& progress)
  737. {
  738. auto it = m_windows.find(window_id);
  739. if (it == m_windows.end()) {
  740. did_misbehave("SetWindowProgress with bad window ID");
  741. return;
  742. }
  743. it->value->set_progress(progress);
  744. }
  745. void ClientConnection::refresh_system_theme()
  746. {
  747. // Post the client an UpdateSystemTheme message to refresh its theme.
  748. async_update_system_theme(Gfx::current_system_theme_buffer());
  749. }
  750. void ClientConnection::pong()
  751. {
  752. m_ping_timer = nullptr;
  753. set_unresponsive(false);
  754. }
  755. void ClientConnection::set_global_cursor_position(Gfx::IntPoint const& position)
  756. {
  757. if (!Screen::main().rect().contains(position)) {
  758. did_misbehave("SetGlobalCursorPosition with bad position");
  759. return;
  760. }
  761. if (position != ScreenInput::the().cursor_location()) {
  762. ScreenInput::the().set_cursor_location(position);
  763. Compositor::the().invalidate_cursor();
  764. }
  765. }
  766. Messages::WindowServer::GetGlobalCursorPositionResponse ClientConnection::get_global_cursor_position()
  767. {
  768. return ScreenInput::the().cursor_location();
  769. }
  770. void ClientConnection::set_mouse_acceleration(float factor)
  771. {
  772. double dbl_factor = (double)factor;
  773. if (dbl_factor < mouse_accel_min || dbl_factor > mouse_accel_max) {
  774. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  775. return;
  776. }
  777. WindowManager::the().set_acceleration_factor(dbl_factor);
  778. }
  779. Messages::WindowServer::GetMouseAccelerationResponse ClientConnection::get_mouse_acceleration()
  780. {
  781. return ScreenInput::the().acceleration_factor();
  782. }
  783. void ClientConnection::set_scroll_step_size(u32 step_size)
  784. {
  785. if (step_size < scroll_step_size_min) {
  786. did_misbehave("SetScrollStepSize with bad scroll step size");
  787. return;
  788. }
  789. WindowManager::the().set_scroll_step_size(step_size);
  790. }
  791. Messages::WindowServer::GetScrollStepSizeResponse ClientConnection::get_scroll_step_size()
  792. {
  793. return ScreenInput::the().scroll_step_size();
  794. }
  795. void ClientConnection::set_double_click_speed(i32 speed)
  796. {
  797. if (speed < double_click_speed_min || speed > double_click_speed_max) {
  798. did_misbehave("SetDoubleClickSpeed with bad speed");
  799. return;
  800. }
  801. WindowManager::the().set_double_click_speed(speed);
  802. }
  803. Messages::WindowServer::GetDoubleClickSpeedResponse ClientConnection::get_double_click_speed()
  804. {
  805. return WindowManager::the().double_click_speed();
  806. }
  807. void ClientConnection::set_unresponsive(bool unresponsive)
  808. {
  809. if (m_unresponsive == unresponsive)
  810. return;
  811. m_unresponsive = unresponsive;
  812. for (auto& it : m_windows) {
  813. auto& window = *it.value;
  814. window.invalidate(true, true);
  815. if (unresponsive) {
  816. window.set_cursor_override(WindowManager::the().wait_cursor());
  817. } else {
  818. window.remove_cursor_override();
  819. }
  820. }
  821. Compositor::the().invalidate_cursor();
  822. }
  823. void ClientConnection::may_have_become_unresponsive()
  824. {
  825. async_ping();
  826. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  827. set_unresponsive(true);
  828. });
  829. m_ping_timer->start();
  830. }
  831. void ClientConnection::did_become_responsive()
  832. {
  833. set_unresponsive(false);
  834. }
  835. Messages::WindowServer::GetScreenBitmapResponse ClientConnection::get_screen_bitmap(Optional<Gfx::IntRect> const& rect, Optional<u32> const& screen_index)
  836. {
  837. if (screen_index.has_value()) {
  838. auto* screen = Screen::find_by_index(screen_index.value());
  839. if (!screen) {
  840. dbgln("get_screen_bitmap: Screen {} does not exist!", screen_index.value());
  841. return { {} };
  842. }
  843. if (rect.has_value()) {
  844. auto bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen).cropped(rect.value());
  845. return bitmap->to_shareable_bitmap();
  846. }
  847. auto& bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen);
  848. return bitmap.to_shareable_bitmap();
  849. }
  850. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  851. auto bitmap_size = rect.value_or(Screen::bounding_rect()).size();
  852. if (auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, bitmap_size, 1)) {
  853. Gfx::Painter painter(*bitmap);
  854. Screen::for_each([&](auto& screen) {
  855. auto screen_rect = screen.rect();
  856. if (rect.has_value() && !rect.value().intersects(screen_rect))
  857. return IterationDecision::Continue;
  858. auto src_rect = rect.has_value() ? rect.value().intersected(screen_rect) : screen_rect;
  859. VERIFY(Screen::bounding_rect().contains(src_rect));
  860. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  861. // TODO: painter does *not* support down-sampling!!!
  862. painter.blit(screen_rect.location(), screen_bitmap, src_rect.translated(-screen_rect.location()), 1.0f, false);
  863. return IterationDecision::Continue;
  864. });
  865. return bitmap->to_shareable_bitmap();
  866. }
  867. return { {} };
  868. }
  869. Messages::WindowServer::GetScreenBitmapAroundCursorResponse ClientConnection::get_screen_bitmap_around_cursor(Gfx::IntSize const& size)
  870. {
  871. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  872. auto cursor_location = ScreenInput::the().cursor_location();
  873. Gfx::Rect rect { cursor_location.x() - (size.width() / 2), cursor_location.y() - (size.height() / 2), size.width(), size.height() };
  874. // Recompose the screen to make sure the cursor is painted in the location we think it is.
  875. // FIXME: This is rather wasteful. We can probably think of a way to avoid this.
  876. Compositor::the().compose();
  877. // Check if we need to compose from multiple screens. If not we can take a fast path
  878. size_t intersecting_with_screens = 0;
  879. Screen::for_each([&](auto& screen) {
  880. if (rect.intersects(screen.rect()))
  881. intersecting_with_screens++;
  882. return IterationDecision::Continue;
  883. });
  884. if (intersecting_with_screens == 1) {
  885. auto& screen = Screen::closest_to_rect(rect);
  886. auto bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen).cropped(rect.translated(-screen.rect().location()));
  887. return bitmap->to_shareable_bitmap();
  888. }
  889. if (auto bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1)) {
  890. auto bounding_screen_src_rect = Screen::bounding_rect().intersected(rect);
  891. Gfx::Painter painter(*bitmap);
  892. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  893. auto cursor_rect = Compositor::the().current_cursor_rect();
  894. Screen::for_each([&](auto& screen) {
  895. auto screen_rect = screen.rect();
  896. auto src_rect = screen_rect.intersected(bounding_screen_src_rect);
  897. if (src_rect.is_empty())
  898. return IterationDecision ::Continue;
  899. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  900. auto from_rect = src_rect.translated(-screen_rect.location());
  901. auto target_location = rect.intersected(screen_rect).location().translated(-rect.location());
  902. // TODO: painter does *not* support down-sampling!!!
  903. painter.blit(target_location, screen_bitmap, from_rect, 1.0f, false);
  904. // Check if we are a screen that doesn't have the cursor but the cursor would
  905. // have normally been cut off (we don't draw portions of the cursor on a screen
  906. // that doesn't actually have the cursor). In that case we need to render the remaining
  907. // portion of the cursor on that screen's capture manually
  908. if (&screen != &screen_with_cursor) {
  909. auto screen_cursor_rect = cursor_rect.intersected(screen_rect);
  910. if (!screen_cursor_rect.is_empty()) {
  911. if (auto const* cursor_bitmap = Compositor::the().cursor_bitmap_for_screenshot({}, screen)) {
  912. auto src_rect = screen_cursor_rect.translated(-cursor_rect.location());
  913. auto cursor_target = cursor_rect.intersected(screen_rect).location().translated(-rect.location());
  914. // TODO: painter does *not* support down-sampling!!!
  915. painter.blit(cursor_target, *cursor_bitmap, src_rect);
  916. }
  917. }
  918. }
  919. return IterationDecision::Continue;
  920. });
  921. return bitmap->to_shareable_bitmap();
  922. }
  923. return { {} };
  924. }
  925. Messages::WindowServer::GetColorUnderCursorResponse ClientConnection::get_color_under_cursor()
  926. {
  927. // FIXME: Add a mechanism to get screen bitmap without cursor, so we don't have to do this
  928. // manual translation to avoid sampling the color on the actual cursor itself.
  929. auto cursor_location = ScreenInput::the().cursor_location().translated(-1, -1);
  930. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  931. auto color = Compositor::the().color_at_position({}, screen_with_cursor, cursor_location);
  932. return color;
  933. }
  934. Messages::WindowServer::IsWindowModifiedResponse ClientConnection::is_window_modified(i32 window_id)
  935. {
  936. auto it = m_windows.find(window_id);
  937. if (it == m_windows.end()) {
  938. did_misbehave("IsWindowModified: Bad window ID");
  939. return nullptr;
  940. }
  941. auto& window = *it->value;
  942. return window.is_modified();
  943. }
  944. Messages::WindowServer::GetDesktopDisplayScaleResponse ClientConnection::get_desktop_display_scale(u32 screen_index)
  945. {
  946. if (auto* screen = Screen::find_by_index(screen_index))
  947. return screen->scale_factor();
  948. dbgln("GetDesktopDisplayScale: Screen {} does not exist", screen_index);
  949. return 0;
  950. }
  951. void ClientConnection::set_window_modified(i32 window_id, bool modified)
  952. {
  953. auto it = m_windows.find(window_id);
  954. if (it == m_windows.end()) {
  955. did_misbehave("SetWindowModified: Bad window ID");
  956. return;
  957. }
  958. auto& window = *it->value;
  959. window.set_modified(modified);
  960. }
  961. void ClientConnection::set_flash_flush(bool enabled)
  962. {
  963. Compositor::the().set_flash_flush(enabled);
  964. }
  965. void ClientConnection::set_window_parent_from_client(i32 client_id, i32 parent_id, i32 child_id)
  966. {
  967. auto child_window = window_from_id(child_id);
  968. if (!child_window)
  969. did_misbehave("SetWindowParentFromClient: Bad child window ID");
  970. auto client_connection = from_client_id(client_id);
  971. if (!client_connection)
  972. did_misbehave("SetWindowParentFromClient: Bad client ID");
  973. auto parent_window = client_connection->window_from_id(parent_id);
  974. if (!parent_window)
  975. did_misbehave("SetWindowParentFromClient: Bad parent window ID");
  976. if (parent_window->is_stealable_by_client(this->client_id())) {
  977. child_window->set_parent_window(*parent_window);
  978. } else {
  979. did_misbehave("SetWindowParentFromClient: Window is not stealable");
  980. }
  981. }
  982. Messages::WindowServer::GetWindowRectFromClientResponse ClientConnection::get_window_rect_from_client(i32 client_id, i32 window_id)
  983. {
  984. auto client_connection = from_client_id(client_id);
  985. if (!client_connection)
  986. did_misbehave("GetWindowRectFromClient: Bad client ID");
  987. auto window = client_connection->window_from_id(window_id);
  988. if (!window)
  989. did_misbehave("GetWindowRectFromClient: Bad window ID");
  990. return window->rect();
  991. }
  992. void ClientConnection::add_window_stealing_for_client(i32 client_id, i32 window_id)
  993. {
  994. auto window = window_from_id(window_id);
  995. if (!window)
  996. did_misbehave("AddWindowStealingForClient: Bad window ID");
  997. if (!from_client_id(client_id))
  998. did_misbehave("AddWindowStealingForClient: Bad client ID");
  999. window->add_stealing_for_client(client_id);
  1000. }
  1001. void ClientConnection::remove_window_stealing_for_client(i32 client_id, i32 window_id)
  1002. {
  1003. auto window = window_from_id(window_id);
  1004. if (!window)
  1005. did_misbehave("RemoveWindowStealingForClient: Bad window ID");
  1006. // Don't check if the client exists, it may have died
  1007. window->remove_stealing_for_client(client_id);
  1008. }
  1009. void ClientConnection::remove_window_stealing(i32 window_id)
  1010. {
  1011. auto window = window_from_id(window_id);
  1012. if (!window)
  1013. did_misbehave("RemoveWindowStealing: Bad window ID");
  1014. window->remove_all_stealing();
  1015. }
  1016. }