ConnectionFromClient.cpp 49 KB

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