ConnectionFromClient.cpp 48 KB

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