ConnectionFromClient.cpp 47 KB

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