ConnectionFromClient.cpp 50 KB

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