ConnectionFromClient.cpp 43 KB

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