ConnectionFromClient.cpp 51 KB

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