ConnectionFromClient.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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/Compositor.h>
  12. #include <WindowServer/ConnectionFromClient.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<ConnectionFromClient>>* s_connections;
  25. void ConnectionFromClient::for_each_client(Function<void(ConnectionFromClient&)> callback)
  26. {
  27. if (!s_connections)
  28. return;
  29. for (auto& it : *s_connections) {
  30. callback(*it.value);
  31. }
  32. }
  33. ConnectionFromClient* ConnectionFromClient::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. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
  43. : IPC::ConnectionFromClient<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
  44. {
  45. if (!s_connections)
  46. s_connections = new HashMap<int, NonnullRefPtr<ConnectionFromClient>>;
  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(), Gfx::FontDatabase::window_title_font_query(), client_id);
  50. }
  51. ConnectionFromClient::~ConnectionFromClient()
  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 ConnectionFromClient::die()
  69. {
  70. deferred_invoke([this] {
  71. s_connections->remove(client_id());
  72. });
  73. }
  74. void ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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 ConnectionFromClient::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, Gfx::ShareableBitmap const& icon)
  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_icon(icon.bitmap());
  167. menu_item->set_text(text);
  168. menu_item->set_shortcut_text(shortcut);
  169. menu_item->set_enabled(enabled);
  170. menu_item->set_checkable(checkable);
  171. menu_item->set_default(is_default);
  172. if (checkable)
  173. menu_item->set_checked(checked);
  174. }
  175. void ConnectionFromClient::remove_menu_item(i32 menu_id, i32 identifier)
  176. {
  177. auto it = m_menus.find(menu_id);
  178. if (it == m_menus.end()) {
  179. did_misbehave("RemoveMenuItem: Bad menu ID");
  180. return;
  181. }
  182. auto& menu = *(*it).value;
  183. if (!menu.remove_item_with_identifier(identifier))
  184. did_misbehave("RemoveMenuItem: Bad menu item identifier");
  185. }
  186. void ConnectionFromClient::flash_menubar_menu(i32 window_id, i32 menu_id)
  187. {
  188. auto itw = m_windows.find(window_id);
  189. if (itw == m_windows.end()) {
  190. did_misbehave("FlashMenubarMenu: Bad window ID");
  191. return;
  192. }
  193. auto& window = *(*itw).value;
  194. auto itm = m_menus.find(menu_id);
  195. if (itm == m_menus.end()) {
  196. did_misbehave("FlashMenubarMenu: Bad menu ID");
  197. return;
  198. }
  199. auto& menu = *(*itm).value;
  200. if (window.menubar().flash_menu(&menu)) {
  201. window.frame().invalidate_menubar();
  202. if (m_flashed_menu_timer && m_flashed_menu_timer->is_active()) {
  203. m_flashed_menu_timer->on_timeout();
  204. m_flashed_menu_timer->stop();
  205. }
  206. m_flashed_menu_timer = Core::Timer::create_single_shot(75, [weak_window = window.make_weak_ptr<Window>()]() mutable {
  207. if (!weak_window)
  208. return;
  209. weak_window->menubar().flash_menu(nullptr);
  210. weak_window->frame().invalidate_menubar();
  211. });
  212. m_flashed_menu_timer->start();
  213. } else if (m_flashed_menu_timer) {
  214. m_flashed_menu_timer->restart();
  215. }
  216. }
  217. void ConnectionFromClient::add_menu_separator(i32 menu_id)
  218. {
  219. auto it = m_menus.find(menu_id);
  220. if (it == m_menus.end()) {
  221. did_misbehave("AddMenuSeparator: Bad menu ID");
  222. return;
  223. }
  224. auto& menu = *(*it).value;
  225. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  226. }
  227. void ConnectionFromClient::move_window_to_front(i32 window_id)
  228. {
  229. auto it = m_windows.find(window_id);
  230. if (it == m_windows.end()) {
  231. did_misbehave("MoveWindowToFront: Bad window ID");
  232. return;
  233. }
  234. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  235. }
  236. void ConnectionFromClient::set_fullscreen(i32 window_id, bool fullscreen)
  237. {
  238. auto it = m_windows.find(window_id);
  239. if (it == m_windows.end()) {
  240. did_misbehave("SetFullscreen: Bad window ID");
  241. return;
  242. }
  243. it->value->set_fullscreen(fullscreen);
  244. }
  245. void ConnectionFromClient::set_frameless(i32 window_id, bool frameless)
  246. {
  247. auto it = m_windows.find(window_id);
  248. if (it == m_windows.end()) {
  249. did_misbehave("SetFrameless: Bad window ID");
  250. return;
  251. }
  252. it->value->set_frameless(frameless);
  253. WindowManager::the().tell_wms_window_state_changed(*it->value);
  254. }
  255. void ConnectionFromClient::set_forced_shadow(i32 window_id, bool shadow)
  256. {
  257. auto it = m_windows.find(window_id);
  258. if (it == m_windows.end()) {
  259. did_misbehave("SetForcedShadow: Bad window ID");
  260. return;
  261. }
  262. it->value->set_forced_shadow(shadow);
  263. it->value->invalidate();
  264. Compositor::the().invalidate_occlusions();
  265. }
  266. void ConnectionFromClient::set_window_opacity(i32 window_id, float opacity)
  267. {
  268. auto it = m_windows.find(window_id);
  269. if (it == m_windows.end()) {
  270. did_misbehave("SetWindowOpacity: Bad window ID");
  271. return;
  272. }
  273. it->value->set_opacity(opacity);
  274. }
  275. void ConnectionFromClient::set_wallpaper(Gfx::ShareableBitmap const& bitmap)
  276. {
  277. Compositor::the().set_wallpaper(bitmap.bitmap());
  278. async_set_wallpaper_finished(true);
  279. }
  280. void ConnectionFromClient::set_background_color(String const& background_color)
  281. {
  282. Compositor::the().set_background_color(background_color);
  283. }
  284. void ConnectionFromClient::set_wallpaper_mode(String const& mode)
  285. {
  286. Compositor::the().set_wallpaper_mode(mode);
  287. }
  288. Messages::WindowServer::GetWallpaperResponse ConnectionFromClient::get_wallpaper()
  289. {
  290. return Compositor::the().wallpaper_bitmap()->to_shareable_bitmap();
  291. }
  292. Messages::WindowServer::SetScreenLayoutResponse ConnectionFromClient::set_screen_layout(ScreenLayout const& screen_layout, bool save)
  293. {
  294. String error_msg;
  295. bool success = WindowManager::the().set_screen_layout(ScreenLayout(screen_layout), save, error_msg);
  296. return { success, move(error_msg) };
  297. }
  298. Messages::WindowServer::GetScreenLayoutResponse ConnectionFromClient::get_screen_layout()
  299. {
  300. return { WindowManager::the().get_screen_layout() };
  301. }
  302. Messages::WindowServer::SaveScreenLayoutResponse ConnectionFromClient::save_screen_layout()
  303. {
  304. String error_msg;
  305. bool success = WindowManager::the().save_screen_layout(error_msg);
  306. return { success, move(error_msg) };
  307. }
  308. Messages::WindowServer::ApplyWorkspaceSettingsResponse ConnectionFromClient::apply_workspace_settings(u32 rows, u32 columns, bool save)
  309. {
  310. if (rows == 0 || columns == 0 || rows > WindowManager::max_window_stack_rows || columns > WindowManager::max_window_stack_columns)
  311. return { false };
  312. return { WindowManager::the().apply_workspace_settings(rows, columns, save) };
  313. }
  314. Messages::WindowServer::GetWorkspaceSettingsResponse ConnectionFromClient::get_workspace_settings()
  315. {
  316. auto& wm = WindowManager::the();
  317. return { (unsigned)wm.window_stack_rows(), (unsigned)wm.window_stack_columns(), WindowManager::max_window_stack_rows, WindowManager::max_window_stack_columns };
  318. }
  319. void ConnectionFromClient::show_screen_numbers(bool show)
  320. {
  321. if (m_show_screen_number == show)
  322. return;
  323. m_show_screen_number = show;
  324. if (show)
  325. Compositor::the().increment_show_screen_number({});
  326. else
  327. Compositor::the().decrement_show_screen_number({});
  328. }
  329. void ConnectionFromClient::set_window_title(i32 window_id, String const& title)
  330. {
  331. auto it = m_windows.find(window_id);
  332. if (it == m_windows.end()) {
  333. did_misbehave("SetWindowTitle: Bad window ID");
  334. return;
  335. }
  336. it->value->set_title(title);
  337. }
  338. Messages::WindowServer::GetWindowTitleResponse ConnectionFromClient::get_window_title(i32 window_id)
  339. {
  340. auto it = m_windows.find(window_id);
  341. if (it == m_windows.end()) {
  342. did_misbehave("GetWindowTitle: Bad window ID");
  343. return nullptr;
  344. }
  345. return it->value->title();
  346. }
  347. Messages::WindowServer::IsMaximizedResponse ConnectionFromClient::is_maximized(i32 window_id)
  348. {
  349. auto it = m_windows.find(window_id);
  350. if (it == m_windows.end()) {
  351. did_misbehave("IsMaximized: Bad window ID");
  352. return nullptr;
  353. }
  354. return it->value->is_maximized();
  355. }
  356. void ConnectionFromClient::set_maximized(i32 window_id, bool maximized)
  357. {
  358. auto it = m_windows.find(window_id);
  359. if (it == m_windows.end()) {
  360. did_misbehave("SetMaximized: Bad window ID");
  361. return;
  362. }
  363. it->value->set_maximized(maximized);
  364. }
  365. void ConnectionFromClient::set_window_icon_bitmap(i32 window_id, Gfx::ShareableBitmap const& icon)
  366. {
  367. auto it = m_windows.find(window_id);
  368. if (it == m_windows.end()) {
  369. did_misbehave("SetWindowIconBitmap: Bad window ID");
  370. return;
  371. }
  372. auto& window = *(*it).value;
  373. if (icon.is_valid()) {
  374. window.set_icon(*icon.bitmap());
  375. } else {
  376. window.set_default_icon();
  377. }
  378. window.frame().invalidate_titlebar();
  379. WindowManager::the().tell_wms_window_icon_changed(window);
  380. }
  381. Messages::WindowServer::SetWindowRectResponse ConnectionFromClient::set_window_rect(i32 window_id, Gfx::IntRect const& rect)
  382. {
  383. auto it = m_windows.find(window_id);
  384. if (it == m_windows.end()) {
  385. did_misbehave("SetWindowRect: Bad window ID");
  386. return nullptr;
  387. }
  388. auto& window = *(*it).value;
  389. if (window.is_fullscreen()) {
  390. dbgln("ConnectionFromClient: Ignoring SetWindowRect request for fullscreen window");
  391. return nullptr;
  392. }
  393. if (rect.width() > INT16_MAX || rect.height() > INT16_MAX) {
  394. did_misbehave(String::formatted("SetWindowRect: Bad window sizing(width={}, height={}), dimension exceeds INT16_MAX", rect.width(), rect.height()).characters());
  395. return nullptr;
  396. }
  397. if (rect.location() != window.rect().location()) {
  398. window.set_default_positioned(false);
  399. }
  400. auto new_rect = rect;
  401. window.apply_minimum_size(new_rect);
  402. window.set_rect(new_rect);
  403. window.nudge_into_desktop(nullptr);
  404. window.request_update(window.rect());
  405. return window.rect();
  406. }
  407. Messages::WindowServer::GetWindowRectResponse ConnectionFromClient::get_window_rect(i32 window_id)
  408. {
  409. auto it = m_windows.find(window_id);
  410. if (it == m_windows.end()) {
  411. did_misbehave("GetWindowRect: Bad window ID");
  412. return nullptr;
  413. }
  414. return it->value->rect();
  415. }
  416. static Gfx::IntSize calculate_minimum_size_for_window(Window const& window)
  417. {
  418. // NOTE: Windows with a title bar have a minimum size enforced by the system,
  419. // because we want to always keep their title buttons accessible.
  420. if (window.type() == WindowType::Normal || window.type() == WindowType::ToolWindow) {
  421. auto palette = WindowManager::the().palette();
  422. int required_width = 0;
  423. // Padding on left and right of window title content.
  424. // FIXME: This seems like it should be defined in the theme.
  425. required_width += 2 + 2;
  426. // App icon
  427. required_width += 16;
  428. // Padding between icon and buttons
  429. required_width += 2;
  430. // Close button
  431. required_width += palette.window_title_button_width();
  432. // Maximize button
  433. if (window.is_resizable())
  434. required_width += palette.window_title_button_width();
  435. // Minimize button
  436. if (window.is_minimizable())
  437. required_width += palette.window_title_button_width();
  438. return { required_width, 0 };
  439. }
  440. return { 0, 0 };
  441. }
  442. void ConnectionFromClient::set_window_minimum_size(i32 window_id, Gfx::IntSize const& size)
  443. {
  444. auto it = m_windows.find(window_id);
  445. if (it == m_windows.end()) {
  446. did_misbehave("SetWindowMinimumSize: Bad window ID");
  447. return;
  448. }
  449. auto& window = *(*it).value;
  450. if (window.is_fullscreen()) {
  451. dbgln("ConnectionFromClient: Ignoring SetWindowMinimumSize request for fullscreen window");
  452. return;
  453. }
  454. auto system_window_minimum_size = calculate_minimum_size_for_window(window);
  455. window.set_minimum_size({ max(size.width(), system_window_minimum_size.width()),
  456. max(size.height(), system_window_minimum_size.height()) });
  457. if (window.width() < window.minimum_size().width() || window.height() < window.minimum_size().height()) {
  458. // New minimum size is larger than the current window size, resize accordingly.
  459. auto new_rect = window.rect();
  460. bool did_size_clamp = window.apply_minimum_size(new_rect);
  461. window.set_rect(new_rect);
  462. window.nudge_into_desktop(nullptr);
  463. window.request_update(window.rect());
  464. if (did_size_clamp)
  465. window.refresh_client_size();
  466. }
  467. }
  468. Messages::WindowServer::GetWindowMinimumSizeResponse ConnectionFromClient::get_window_minimum_size(i32 window_id)
  469. {
  470. auto it = m_windows.find(window_id);
  471. if (it == m_windows.end()) {
  472. did_misbehave("GetWindowMinimumSize: Bad window ID");
  473. return nullptr;
  474. }
  475. return it->value->minimum_size();
  476. }
  477. Messages::WindowServer::GetAppletRectOnScreenResponse ConnectionFromClient::get_applet_rect_on_screen(i32 window_id)
  478. {
  479. auto it = m_windows.find(window_id);
  480. if (it == m_windows.end()) {
  481. did_misbehave("GetAppletRectOnScreen: Bad window ID");
  482. return nullptr;
  483. }
  484. Gfx::IntRect applet_area_rect;
  485. if (auto* applet_area_window = AppletManager::the().window())
  486. applet_area_rect = applet_area_window->rect();
  487. return it->value->rect_in_applet_area().translated(applet_area_rect.location());
  488. }
  489. Window* ConnectionFromClient::window_from_id(i32 window_id)
  490. {
  491. auto it = m_windows.find(window_id);
  492. if (it == m_windows.end())
  493. return nullptr;
  494. return it->value.ptr();
  495. }
  496. void ConnectionFromClient::create_window(i32 window_id, Gfx::IntRect const& rect,
  497. bool auto_position, bool has_alpha_channel, bool modal, bool minimizable, bool closeable, bool resizable,
  498. bool fullscreen, bool frameless, bool forced_shadow, bool accessory, float opacity,
  499. float alpha_hit_threshold, Gfx::IntSize const& base_size, Gfx::IntSize const& size_increment,
  500. Gfx::IntSize const& minimum_size, Optional<Gfx::IntSize> const& resize_aspect_ratio, i32 type,
  501. String const& title, i32 parent_window_id, Gfx::IntRect const& launch_origin_rect)
  502. {
  503. Window* parent_window = nullptr;
  504. if (parent_window_id) {
  505. parent_window = window_from_id(parent_window_id);
  506. if (!parent_window) {
  507. did_misbehave("CreateWindow with bad parent_window_id");
  508. return;
  509. }
  510. }
  511. if (type < 0 || type >= (i32)WindowType::_Count) {
  512. did_misbehave("CreateWindow with a bad type");
  513. return;
  514. }
  515. if (m_windows.contains(window_id)) {
  516. did_misbehave("CreateWindow with already-used window ID");
  517. return;
  518. }
  519. auto window = Window::construct(*this, (WindowType)type, window_id, modal, minimizable, closeable, frameless, resizable, fullscreen, accessory, parent_window);
  520. window->set_forced_shadow(forced_shadow);
  521. if (!launch_origin_rect.is_empty())
  522. window->start_launch_animation(launch_origin_rect);
  523. window->set_has_alpha_channel(has_alpha_channel);
  524. window->set_title(title);
  525. if (!fullscreen) {
  526. Gfx::IntRect new_rect = rect;
  527. if (auto_position && window->is_movable()) {
  528. new_rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), rect.size() };
  529. window->set_default_positioned(true);
  530. }
  531. auto system_window_minimum_size = calculate_minimum_size_for_window(window);
  532. window->set_minimum_size({ max(minimum_size.width(), system_window_minimum_size.width()),
  533. max(minimum_size.height(), system_window_minimum_size.height()) });
  534. bool did_size_clamp = window->apply_minimum_size(new_rect);
  535. window->set_rect(new_rect);
  536. window->nudge_into_desktop(nullptr);
  537. if (did_size_clamp)
  538. window->refresh_client_size();
  539. }
  540. if (window->type() == WindowType::Desktop) {
  541. window->set_rect(Screen::bounding_rect());
  542. window->recalculate_rect();
  543. }
  544. window->set_opacity(opacity);
  545. window->set_alpha_hit_threshold(alpha_hit_threshold);
  546. window->set_size_increment(size_increment);
  547. window->set_base_size(base_size);
  548. if (resize_aspect_ratio.has_value() && !resize_aspect_ratio.value().is_null())
  549. window->set_resize_aspect_ratio(resize_aspect_ratio);
  550. window->invalidate(true, true);
  551. if (window->type() == WindowType::Applet)
  552. AppletManager::the().add_applet(*window);
  553. m_windows.set(window_id, move(window));
  554. }
  555. void ConnectionFromClient::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  556. {
  557. for (auto& child_window : window.child_windows()) {
  558. if (!child_window)
  559. continue;
  560. VERIFY(child_window->window_id() != window.window_id());
  561. destroy_window(*child_window, destroyed_window_ids);
  562. }
  563. for (auto& accessory_window : window.accessory_windows()) {
  564. if (!accessory_window)
  565. continue;
  566. VERIFY(accessory_window->window_id() != window.window_id());
  567. destroy_window(*accessory_window, destroyed_window_ids);
  568. }
  569. destroyed_window_ids.append(window.window_id());
  570. if (window.type() == WindowType::Applet)
  571. AppletManager::the().remove_applet(window);
  572. window.destroy();
  573. remove_child(window);
  574. m_windows.remove(window.window_id());
  575. }
  576. Messages::WindowServer::DestroyWindowResponse ConnectionFromClient::destroy_window(i32 window_id)
  577. {
  578. auto it = m_windows.find(window_id);
  579. if (it == m_windows.end()) {
  580. did_misbehave("DestroyWindow: Bad window ID");
  581. return nullptr;
  582. }
  583. auto& window = *(*it).value;
  584. Vector<i32> destroyed_window_ids;
  585. destroy_window(window, destroyed_window_ids);
  586. return destroyed_window_ids;
  587. }
  588. void ConnectionFromClient::post_paint_message(Window& window, bool ignore_occlusion)
  589. {
  590. auto rect_set = window.take_pending_paint_rects();
  591. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  592. return;
  593. async_paint(window.window_id(), window.size(), rect_set.rects());
  594. }
  595. void ConnectionFromClient::invalidate_rect(i32 window_id, Vector<Gfx::IntRect> const& rects, bool ignore_occlusion)
  596. {
  597. auto it = m_windows.find(window_id);
  598. if (it == m_windows.end()) {
  599. did_misbehave("InvalidateRect: Bad window ID");
  600. return;
  601. }
  602. auto& window = *(*it).value;
  603. for (size_t i = 0; i < rects.size(); ++i)
  604. window.request_update(rects[i].intersected({ {}, window.size() }), ignore_occlusion);
  605. }
  606. void ConnectionFromClient::did_finish_painting(i32 window_id, Vector<Gfx::IntRect> const& rects)
  607. {
  608. auto it = m_windows.find(window_id);
  609. if (it == m_windows.end()) {
  610. did_misbehave("DidFinishPainting: Bad window ID");
  611. return;
  612. }
  613. auto& window = *(*it).value;
  614. for (auto& rect : rects)
  615. window.invalidate(rect);
  616. if (window.has_alpha_channel() && window.alpha_hit_threshold() > 0.0f)
  617. WindowManager::the().reevaluate_hover_state_for_window(&window);
  618. WindowSwitcher::the().refresh_if_needed();
  619. }
  620. void ConnectionFromClient::set_window_backing_store(i32 window_id, [[maybe_unused]] i32 bpp,
  621. [[maybe_unused]] i32 pitch, IPC::File const& anon_file, i32 serial, bool has_alpha_channel,
  622. Gfx::IntSize const& size, bool flush_immediately)
  623. {
  624. auto it = m_windows.find(window_id);
  625. if (it == m_windows.end()) {
  626. did_misbehave("SetWindowBackingStore: Bad window ID");
  627. return;
  628. }
  629. auto& window = *(*it).value;
  630. if (window.last_backing_store() && window.last_backing_store_serial() == serial) {
  631. window.swap_backing_stores();
  632. } else {
  633. // FIXME: Plumb scale factor here eventually.
  634. auto buffer_or_error = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), pitch * size.height());
  635. if (buffer_or_error.is_error()) {
  636. did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store");
  637. return;
  638. }
  639. auto backing_store_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(
  640. has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
  641. buffer_or_error.release_value(),
  642. size,
  643. 1,
  644. {});
  645. if (backing_store_or_error.is_error()) {
  646. did_misbehave("");
  647. }
  648. window.set_backing_store(backing_store_or_error.release_value(), serial);
  649. }
  650. if (flush_immediately)
  651. window.invalidate(false);
  652. }
  653. void ConnectionFromClient::set_global_mouse_tracking(bool enabled)
  654. {
  655. m_does_global_mouse_tracking = enabled;
  656. }
  657. void ConnectionFromClient::set_window_cursor(i32 window_id, i32 cursor_type)
  658. {
  659. auto it = m_windows.find(window_id);
  660. if (it == m_windows.end()) {
  661. did_misbehave("SetWindowCursor: Bad window ID");
  662. return;
  663. }
  664. auto& window = *(*it).value;
  665. if (cursor_type < 0 || cursor_type >= (i32)Gfx::StandardCursor::__Count) {
  666. did_misbehave("SetWindowCursor: Bad cursor type");
  667. return;
  668. }
  669. window.set_cursor(Cursor::create((Gfx::StandardCursor)cursor_type));
  670. if (&window == WindowManager::the().hovered_window())
  671. Compositor::the().invalidate_cursor();
  672. }
  673. void ConnectionFromClient::set_window_custom_cursor(i32 window_id, Gfx::ShareableBitmap const& cursor)
  674. {
  675. auto it = m_windows.find(window_id);
  676. if (it == m_windows.end()) {
  677. did_misbehave("SetWindowCustomCursor: Bad window ID");
  678. return;
  679. }
  680. auto& window = *(*it).value;
  681. if (!cursor.is_valid()) {
  682. did_misbehave("SetWindowCustomCursor: Bad cursor");
  683. return;
  684. }
  685. window.set_cursor(Cursor::create(*cursor.bitmap(), 1));
  686. Compositor::the().invalidate_cursor();
  687. }
  688. void ConnectionFromClient::set_window_has_alpha_channel(i32 window_id, bool has_alpha_channel)
  689. {
  690. auto it = m_windows.find(window_id);
  691. if (it == m_windows.end()) {
  692. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  693. return;
  694. }
  695. it->value->set_has_alpha_channel(has_alpha_channel);
  696. }
  697. void ConnectionFromClient::set_window_alpha_hit_threshold(i32 window_id, float threshold)
  698. {
  699. auto it = m_windows.find(window_id);
  700. if (it == m_windows.end()) {
  701. did_misbehave("SetWindowAlphaHitThreshold: Bad window ID");
  702. return;
  703. }
  704. it->value->set_alpha_hit_threshold(threshold);
  705. }
  706. void ConnectionFromClient::start_window_resize(i32 window_id)
  707. {
  708. auto it = m_windows.find(window_id);
  709. if (it == m_windows.end()) {
  710. did_misbehave("WM_StartWindowResize: Bad window ID");
  711. return;
  712. }
  713. auto& window = *(*it).value;
  714. if (!window.is_resizable()) {
  715. dbgln("Client wants to start resizing a non-resizable window");
  716. return;
  717. }
  718. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  719. // Maybe the client should be allowed to specify what initiated this request?
  720. WindowManager::the().start_window_resize(window, ScreenInput::the().cursor_location(), MouseButton::Primary);
  721. }
  722. Messages::WindowServer::StartDragResponse ConnectionFromClient::start_drag(String const& text, HashMap<String, ByteBuffer> const& mime_data, Gfx::ShareableBitmap const& drag_bitmap)
  723. {
  724. auto& wm = WindowManager::the();
  725. if (wm.dnd_client())
  726. return false;
  727. wm.start_dnd_drag(*this, text, drag_bitmap.bitmap(), Core::MimeData::construct(mime_data));
  728. return true;
  729. }
  730. Messages::WindowServer::SetSystemThemeResponse ConnectionFromClient::set_system_theme(String const& theme_path, String const& theme_name, bool keep_desktop_background)
  731. {
  732. bool success = WindowManager::the().update_theme(theme_path, theme_name, keep_desktop_background);
  733. return success;
  734. }
  735. Messages::WindowServer::GetSystemThemeResponse ConnectionFromClient::get_system_theme()
  736. {
  737. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  738. auto name = wm_config->read_entry("Theme", "Name");
  739. return name;
  740. }
  741. Messages::WindowServer::SetSystemThemeOverrideResponse ConnectionFromClient::set_system_theme_override(Core::AnonymousBuffer const& theme_override)
  742. {
  743. bool success = WindowManager::the().set_theme_override(theme_override);
  744. return success;
  745. }
  746. Messages::WindowServer::GetSystemThemeOverrideResponse ConnectionFromClient::get_system_theme_override()
  747. {
  748. return WindowManager::the().get_theme_override();
  749. }
  750. void ConnectionFromClient::clear_system_theme_override()
  751. {
  752. WindowManager::the().clear_theme_override();
  753. }
  754. Messages::WindowServer::IsSystemThemeOverriddenResponse ConnectionFromClient::is_system_theme_overridden()
  755. {
  756. return WindowManager::the().is_theme_overridden();
  757. }
  758. void ConnectionFromClient::apply_cursor_theme(String const& name)
  759. {
  760. WindowManager::the().apply_cursor_theme(name);
  761. }
  762. void ConnectionFromClient::set_cursor_highlight_radius(int radius)
  763. {
  764. WindowManager::the().set_cursor_highlight_radius(radius);
  765. }
  766. Messages::WindowServer::GetCursorHighlightRadiusResponse ConnectionFromClient::get_cursor_highlight_radius()
  767. {
  768. return WindowManager::the().cursor_highlight_radius();
  769. }
  770. void ConnectionFromClient::set_cursor_highlight_color(Gfx::Color const& color)
  771. {
  772. WindowManager::the().set_cursor_highlight_color(color);
  773. }
  774. Messages::WindowServer::GetCursorHighlightColorResponse ConnectionFromClient::get_cursor_highlight_color()
  775. {
  776. return WindowManager::the().cursor_highlight_color();
  777. }
  778. Messages::WindowServer::GetCursorThemeResponse ConnectionFromClient::get_cursor_theme()
  779. {
  780. auto config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  781. auto name = config->read_entry("Mouse", "CursorTheme");
  782. return name;
  783. }
  784. Messages::WindowServer::SetSystemFontsResponse ConnectionFromClient::set_system_fonts(String const& default_font_query, String const& fixed_width_font_query, String const& window_title_font_query)
  785. {
  786. if (!Gfx::FontDatabase::the().get_by_name(default_font_query)
  787. || !Gfx::FontDatabase::the().get_by_name(fixed_width_font_query)) {
  788. dbgln("Received unusable font queries: '{}' and '{}'", default_font_query, fixed_width_font_query);
  789. return false;
  790. }
  791. dbgln("Updating fonts: '{}' and '{}'", default_font_query, fixed_width_font_query);
  792. Gfx::FontDatabase::set_default_font_query(default_font_query);
  793. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  794. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  795. ConnectionFromClient::for_each_client([&](auto& client) {
  796. client.async_update_system_fonts(default_font_query, fixed_width_font_query, window_title_font_query);
  797. });
  798. WindowManager::the().invalidate_after_theme_or_font_change();
  799. auto wm_config_or_error = Core::ConfigFile::open("/etc/WindowServer.ini", Core::ConfigFile::AllowWriting::Yes);
  800. if (wm_config_or_error.is_error()) {
  801. dbgln("Unable to open WindowServer.ini to set system fonts: {}", wm_config_or_error.error());
  802. return false;
  803. }
  804. auto wm_config = wm_config_or_error.release_value();
  805. wm_config->write_entry("Fonts", "Default", default_font_query);
  806. wm_config->write_entry("Fonts", "FixedWidth", fixed_width_font_query);
  807. wm_config->write_entry("Fonts", "WindowTitle", window_title_font_query);
  808. return true;
  809. }
  810. void ConnectionFromClient::set_window_base_size_and_size_increment(i32 window_id, Gfx::IntSize const& base_size, Gfx::IntSize const& size_increment)
  811. {
  812. auto it = m_windows.find(window_id);
  813. if (it == m_windows.end()) {
  814. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  815. return;
  816. }
  817. auto& window = *it->value;
  818. window.set_base_size(base_size);
  819. window.set_size_increment(size_increment);
  820. }
  821. void ConnectionFromClient::set_window_resize_aspect_ratio(i32 window_id, Optional<Gfx::IntSize> const& resize_aspect_ratio)
  822. {
  823. auto it = m_windows.find(window_id);
  824. if (it == m_windows.end()) {
  825. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  826. return;
  827. }
  828. auto& window = *it->value;
  829. window.set_resize_aspect_ratio(resize_aspect_ratio);
  830. }
  831. void ConnectionFromClient::enable_display_link()
  832. {
  833. if (m_has_display_link)
  834. return;
  835. m_has_display_link = true;
  836. Compositor::the().increment_display_link_count({});
  837. }
  838. void ConnectionFromClient::disable_display_link()
  839. {
  840. if (!m_has_display_link)
  841. return;
  842. m_has_display_link = false;
  843. Compositor::the().decrement_display_link_count({});
  844. }
  845. void ConnectionFromClient::notify_display_link(Badge<Compositor>)
  846. {
  847. if (!m_has_display_link)
  848. return;
  849. async_display_link_notification();
  850. }
  851. void ConnectionFromClient::set_window_progress(i32 window_id, Optional<i32> const& progress)
  852. {
  853. auto it = m_windows.find(window_id);
  854. if (it == m_windows.end()) {
  855. did_misbehave("SetWindowProgress with bad window ID");
  856. return;
  857. }
  858. it->value->set_progress(progress);
  859. }
  860. void ConnectionFromClient::refresh_system_theme()
  861. {
  862. // Post the client an UpdateSystemTheme message to refresh its theme.
  863. async_update_system_theme(Gfx::current_system_theme_buffer());
  864. }
  865. void ConnectionFromClient::pong()
  866. {
  867. m_ping_timer = nullptr;
  868. set_unresponsive(false);
  869. }
  870. void ConnectionFromClient::set_global_cursor_position(Gfx::IntPoint const& position)
  871. {
  872. if (!Screen::main().rect().contains(position)) {
  873. did_misbehave("SetGlobalCursorPosition with bad position");
  874. return;
  875. }
  876. if (position != ScreenInput::the().cursor_location()) {
  877. ScreenInput::the().set_cursor_location(position);
  878. Compositor::the().invalidate_cursor();
  879. }
  880. }
  881. Messages::WindowServer::GetGlobalCursorPositionResponse ConnectionFromClient::get_global_cursor_position()
  882. {
  883. return ScreenInput::the().cursor_location();
  884. }
  885. void ConnectionFromClient::set_mouse_acceleration(float factor)
  886. {
  887. double dbl_factor = (double)factor;
  888. if (dbl_factor < mouse_accel_min || dbl_factor > mouse_accel_max) {
  889. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  890. return;
  891. }
  892. WindowManager::the().set_acceleration_factor(dbl_factor);
  893. }
  894. Messages::WindowServer::GetMouseAccelerationResponse ConnectionFromClient::get_mouse_acceleration()
  895. {
  896. return ScreenInput::the().acceleration_factor();
  897. }
  898. void ConnectionFromClient::set_scroll_step_size(u32 step_size)
  899. {
  900. if (step_size < scroll_step_size_min) {
  901. did_misbehave("SetScrollStepSize with bad scroll step size");
  902. return;
  903. }
  904. WindowManager::the().set_scroll_step_size(step_size);
  905. }
  906. Messages::WindowServer::GetScrollStepSizeResponse ConnectionFromClient::get_scroll_step_size()
  907. {
  908. return ScreenInput::the().scroll_step_size();
  909. }
  910. void ConnectionFromClient::set_double_click_speed(i32 speed)
  911. {
  912. if (speed < double_click_speed_min || speed > double_click_speed_max) {
  913. did_misbehave("SetDoubleClickSpeed with bad speed");
  914. return;
  915. }
  916. WindowManager::the().set_double_click_speed(speed);
  917. }
  918. Messages::WindowServer::GetDoubleClickSpeedResponse ConnectionFromClient::get_double_click_speed()
  919. {
  920. return WindowManager::the().double_click_speed();
  921. }
  922. void ConnectionFromClient::set_buttons_switched(bool switched)
  923. {
  924. WindowManager::the().set_buttons_switched(switched);
  925. }
  926. Messages::WindowServer::GetButtonsSwitchedResponse ConnectionFromClient::get_buttons_switched()
  927. {
  928. return WindowManager::the().get_buttons_switched();
  929. }
  930. void ConnectionFromClient::set_unresponsive(bool unresponsive)
  931. {
  932. if (m_unresponsive == unresponsive)
  933. return;
  934. m_unresponsive = unresponsive;
  935. for (auto& it : m_windows) {
  936. auto& window = *it.value;
  937. window.invalidate(true, true);
  938. if (unresponsive) {
  939. window.set_cursor_override(WindowManager::the().wait_cursor());
  940. } else {
  941. window.remove_cursor_override();
  942. }
  943. }
  944. Compositor::the().invalidate_cursor();
  945. }
  946. void ConnectionFromClient::may_have_become_unresponsive()
  947. {
  948. async_ping();
  949. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  950. set_unresponsive(true);
  951. });
  952. m_ping_timer->start();
  953. }
  954. void ConnectionFromClient::did_become_responsive()
  955. {
  956. set_unresponsive(false);
  957. }
  958. Messages::WindowServer::GetScreenBitmapResponse ConnectionFromClient::get_screen_bitmap(Optional<Gfx::IntRect> const& rect, Optional<u32> const& screen_index)
  959. {
  960. if (screen_index.has_value()) {
  961. auto* screen = Screen::find_by_index(screen_index.value());
  962. if (!screen) {
  963. dbgln("get_screen_bitmap: Screen {} does not exist!", screen_index.value());
  964. return { Gfx::ShareableBitmap() };
  965. }
  966. if (rect.has_value()) {
  967. auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, *screen).cropped(rect.value());
  968. if (bitmap_or_error.is_error()) {
  969. dbgln("get_screen_bitmap: Failed to crop screenshot: {}", bitmap_or_error.error());
  970. return { Gfx::ShareableBitmap() };
  971. }
  972. return bitmap_or_error.release_value()->to_shareable_bitmap();
  973. }
  974. auto& bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen);
  975. return bitmap.to_shareable_bitmap();
  976. }
  977. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  978. auto bitmap_size = rect.value_or(Screen::bounding_rect()).size();
  979. if (auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, bitmap_size, 1); !bitmap_or_error.is_error()) {
  980. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  981. Gfx::Painter painter(*bitmap);
  982. Screen::for_each([&](auto& screen) {
  983. auto screen_rect = screen.rect();
  984. if (rect.has_value() && !rect.value().intersects(screen_rect))
  985. return IterationDecision::Continue;
  986. auto src_rect = rect.has_value() ? rect.value().intersected(screen_rect) : screen_rect;
  987. VERIFY(Screen::bounding_rect().contains(src_rect));
  988. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  989. // TODO: painter does *not* support down-sampling!!!
  990. painter.blit(screen_rect.location(), screen_bitmap, src_rect.translated(-screen_rect.location()), 1.0f, false);
  991. return IterationDecision::Continue;
  992. });
  993. return bitmap->to_shareable_bitmap();
  994. }
  995. return { Gfx::ShareableBitmap() };
  996. }
  997. Messages::WindowServer::GetScreenBitmapAroundCursorResponse ConnectionFromClient::get_screen_bitmap_around_cursor(Gfx::IntSize const& size)
  998. {
  999. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  1000. auto cursor_location = ScreenInput::the().cursor_location();
  1001. Gfx::Rect rect { cursor_location.x() - (size.width() / 2), cursor_location.y() - (size.height() / 2), size.width(), size.height() };
  1002. // Recompose the screen to make sure the cursor is painted in the location we think it is.
  1003. // FIXME: This is rather wasteful. We can probably think of a way to avoid this.
  1004. Compositor::the().compose();
  1005. // Check if we need to compose from multiple screens. If not we can take a fast path
  1006. size_t intersecting_with_screens = 0;
  1007. Screen::for_each([&](auto& screen) {
  1008. if (rect.intersects(screen.rect()))
  1009. intersecting_with_screens++;
  1010. return IterationDecision::Continue;
  1011. });
  1012. auto screen_scale_factor = ScreenInput::the().cursor_location_screen().scale_factor();
  1013. if (intersecting_with_screens == 1) {
  1014. auto& screen = Screen::closest_to_rect(rect);
  1015. auto crop_rect = rect.translated(-screen.rect().location()) * screen_scale_factor;
  1016. auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, screen).cropped(crop_rect);
  1017. if (bitmap_or_error.is_error()) {
  1018. dbgln("get_screen_bitmap_around_cursor: Failed to crop screenshot: {}", bitmap_or_error.error());
  1019. return { {} };
  1020. }
  1021. return bitmap_or_error.release_value()->to_shareable_bitmap();
  1022. }
  1023. if (auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1); !bitmap_or_error.is_error()) {
  1024. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  1025. auto bounding_screen_src_rect = Screen::bounding_rect().intersected(rect);
  1026. Gfx::Painter painter(*bitmap);
  1027. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  1028. auto cursor_rect = Compositor::the().current_cursor_rect();
  1029. Screen::for_each([&](auto& screen) {
  1030. auto screen_rect = screen.rect();
  1031. auto src_rect = screen_rect.intersected(bounding_screen_src_rect);
  1032. if (src_rect.is_empty())
  1033. return IterationDecision ::Continue;
  1034. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  1035. // TODO: Add scaling support for multiple screens
  1036. auto from_rect = src_rect.translated(-screen_rect.location());
  1037. auto target_location = rect.intersected(screen_rect).location().translated(-rect.location());
  1038. // TODO: painter does *not* support down-sampling!!!
  1039. painter.blit(target_location, screen_bitmap, from_rect, 1.0f, false);
  1040. // Check if we are a screen that doesn't have the cursor but the cursor would
  1041. // have normally been cut off (we don't draw portions of the cursor on a screen
  1042. // that doesn't actually have the cursor). In that case we need to render the remaining
  1043. // portion of the cursor on that screen's capture manually
  1044. if (&screen != &screen_with_cursor) {
  1045. auto screen_cursor_rect = cursor_rect.intersected(screen_rect);
  1046. if (!screen_cursor_rect.is_empty()) {
  1047. if (auto const* cursor_bitmap = Compositor::the().cursor_bitmap_for_screenshot({}, screen)) {
  1048. auto src_rect = screen_cursor_rect.translated(-cursor_rect.location());
  1049. auto cursor_target = cursor_rect.intersected(screen_rect).location().translated(-rect.location());
  1050. // TODO: painter does *not* support down-sampling!!!
  1051. painter.blit(cursor_target, *cursor_bitmap, src_rect);
  1052. }
  1053. }
  1054. }
  1055. return IterationDecision::Continue;
  1056. });
  1057. return bitmap->to_shareable_bitmap();
  1058. }
  1059. return { {} };
  1060. }
  1061. Messages::WindowServer::GetColorUnderCursorResponse ConnectionFromClient::get_color_under_cursor()
  1062. {
  1063. auto screen_scale_factor = ScreenInput::the().cursor_location_screen().scale_factor();
  1064. // FIXME: Add a mechanism to get screen bitmap without cursor, so we don't have to do this
  1065. // manual translation to avoid sampling the color on the actual cursor itself.
  1066. auto cursor_location = (ScreenInput::the().cursor_location() * screen_scale_factor).translated(-1, -1);
  1067. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  1068. auto scaled_screen_rect = screen_with_cursor.rect() * screen_scale_factor;
  1069. if (!scaled_screen_rect.contains(cursor_location))
  1070. return Optional<Color> {};
  1071. return { Compositor::the().color_at_position({}, screen_with_cursor, cursor_location) };
  1072. }
  1073. Messages::WindowServer::IsWindowModifiedResponse ConnectionFromClient::is_window_modified(i32 window_id)
  1074. {
  1075. auto it = m_windows.find(window_id);
  1076. if (it == m_windows.end()) {
  1077. did_misbehave("IsWindowModified: Bad window ID");
  1078. return nullptr;
  1079. }
  1080. auto& window = *it->value;
  1081. return window.is_modified();
  1082. }
  1083. Messages::WindowServer::GetDesktopDisplayScaleResponse ConnectionFromClient::get_desktop_display_scale(u32 screen_index)
  1084. {
  1085. if (auto* screen = Screen::find_by_index(screen_index))
  1086. return screen->scale_factor();
  1087. dbgln("GetDesktopDisplayScale: Screen {} does not exist", screen_index);
  1088. return 0;
  1089. }
  1090. void ConnectionFromClient::set_window_modified(i32 window_id, bool modified)
  1091. {
  1092. auto it = m_windows.find(window_id);
  1093. if (it == m_windows.end()) {
  1094. did_misbehave("SetWindowModified: Bad window ID");
  1095. return;
  1096. }
  1097. auto& window = *it->value;
  1098. window.set_modified(modified);
  1099. }
  1100. void ConnectionFromClient::set_flash_flush(bool enabled)
  1101. {
  1102. Compositor::the().set_flash_flush(enabled);
  1103. }
  1104. void ConnectionFromClient::set_window_parent_from_client(i32 client_id, i32 parent_id, i32 child_id)
  1105. {
  1106. auto child_window = window_from_id(child_id);
  1107. if (!child_window)
  1108. did_misbehave("SetWindowParentFromClient: Bad child window ID");
  1109. auto client_connection = from_client_id(client_id);
  1110. if (!client_connection)
  1111. did_misbehave("SetWindowParentFromClient: Bad client ID");
  1112. auto parent_window = client_connection->window_from_id(parent_id);
  1113. if (!parent_window)
  1114. did_misbehave("SetWindowParentFromClient: Bad parent window ID");
  1115. if (parent_window->is_stealable_by_client(this->client_id())) {
  1116. child_window->set_parent_window(*parent_window);
  1117. } else {
  1118. did_misbehave("SetWindowParentFromClient: Window is not stealable");
  1119. }
  1120. }
  1121. Messages::WindowServer::GetWindowRectFromClientResponse ConnectionFromClient::get_window_rect_from_client(i32 client_id, i32 window_id)
  1122. {
  1123. auto client_connection = from_client_id(client_id);
  1124. if (!client_connection)
  1125. did_misbehave("GetWindowRectFromClient: Bad client ID");
  1126. auto window = client_connection->window_from_id(window_id);
  1127. if (!window)
  1128. did_misbehave("GetWindowRectFromClient: Bad window ID");
  1129. return window->rect();
  1130. }
  1131. void ConnectionFromClient::add_window_stealing_for_client(i32 client_id, i32 window_id)
  1132. {
  1133. auto window = window_from_id(window_id);
  1134. if (!window)
  1135. did_misbehave("AddWindowStealingForClient: Bad window ID");
  1136. if (!from_client_id(client_id))
  1137. did_misbehave("AddWindowStealingForClient: Bad client ID");
  1138. window->add_stealing_for_client(client_id);
  1139. }
  1140. void ConnectionFromClient::remove_window_stealing_for_client(i32 client_id, i32 window_id)
  1141. {
  1142. auto window = window_from_id(window_id);
  1143. if (!window)
  1144. did_misbehave("RemoveWindowStealingForClient: Bad window ID");
  1145. // Don't check if the client exists, it may have died
  1146. window->remove_stealing_for_client(client_id);
  1147. }
  1148. void ConnectionFromClient::remove_window_stealing(i32 window_id)
  1149. {
  1150. auto window = window_from_id(window_id);
  1151. if (!window)
  1152. did_misbehave("RemoveWindowStealing: Bad window ID");
  1153. window->remove_all_stealing();
  1154. }
  1155. void ConnectionFromClient::notify_about_theme_change()
  1156. {
  1157. // Recalculate minimum size for each window, using the new theme metrics.
  1158. // FIXME: We only ever increase the minimum size, which means that if you go from a theme with large buttons
  1159. // (eg Basalt) to one with smaller buttons (eg Default) then the minimum size will remain large. This
  1160. // only happens with pre-existing windows, and it's unlikely that you will ever have windows that are
  1161. // so small, so it's probably fine, but it is technically a bug. :^)
  1162. for_each_window([](auto& window) -> IterationDecision {
  1163. auto system_window_minimum_size = calculate_minimum_size_for_window(window);
  1164. auto old_minimum_size = window.minimum_size();
  1165. auto new_rect = window.rect();
  1166. window.set_minimum_size({ max(old_minimum_size.width(), system_window_minimum_size.width()),
  1167. max(old_minimum_size.height(), system_window_minimum_size.height()) });
  1168. if (window.apply_minimum_size(new_rect)) {
  1169. window.set_rect(new_rect);
  1170. window.refresh_client_size();
  1171. }
  1172. return IterationDecision::Continue;
  1173. });
  1174. async_update_system_theme(Gfx::current_system_theme_buffer());
  1175. }
  1176. }