ConnectionFromClient.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Badge.h>
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibGfx/StandardCursor.h>
  9. #include <LibGfx/SystemTheme.h>
  10. #include <WindowServer/AppletManager.h>
  11. #include <WindowServer/Compositor.h>
  12. #include <WindowServer/ConnectionFromClient.h>
  13. #include <WindowServer/Menu.h>
  14. #include <WindowServer/MenuItem.h>
  15. #include <WindowServer/Screen.h>
  16. #include <WindowServer/Window.h>
  17. #include <WindowServer/WindowClientEndpoint.h>
  18. #include <WindowServer/WindowManager.h>
  19. #include <WindowServer/WindowSwitcher.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <unistd.h>
  23. namespace WindowServer {
  24. HashMap<int, NonnullRefPtr<ConnectionFromClient>>* s_connections;
  25. void ConnectionFromClient::for_each_client(Function<void(ConnectionFromClient&)> callback)
  26. {
  27. if (!s_connections)
  28. return;
  29. for (auto& it : *s_connections) {
  30. callback(*it.value);
  31. }
  32. }
  33. ConnectionFromClient* ConnectionFromClient::from_client_id(int client_id)
  34. {
  35. if (!s_connections)
  36. return nullptr;
  37. auto it = s_connections->find(client_id);
  38. if (it == s_connections->end())
  39. return nullptr;
  40. return (*it).value.ptr();
  41. }
  42. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> client_socket, int client_id)
  43. : IPC::ConnectionFromClient<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
  44. {
  45. if (!s_connections)
  46. s_connections = new HashMap<int, NonnullRefPtr<ConnectionFromClient>>;
  47. s_connections->set(client_id, *this);
  48. auto& wm = WindowManager::the();
  49. async_fast_greet(Screen::rects(), Screen::main().index(), wm.window_stack_rows(), wm.window_stack_columns(), Gfx::current_system_theme_buffer(), Gfx::FontDatabase::default_font_query(), Gfx::FontDatabase::fixed_width_font_query(), Gfx::FontDatabase::window_title_font_query(), wm.system_effects().effects(), client_id);
  50. }
  51. ConnectionFromClient::~ConnectionFromClient()
  52. {
  53. auto& wm = WindowManager::the();
  54. if (wm.dnd_client() == this)
  55. wm.end_dnd_drag();
  56. if (m_has_display_link)
  57. Compositor::the().decrement_display_link_count({});
  58. MenuManager::the().close_all_menus_from_client({}, *this);
  59. auto windows = move(m_windows);
  60. for (auto& window : windows) {
  61. window.value->detach_client({});
  62. if (window.value->type() == WindowType::Applet)
  63. AppletManager::the().remove_applet(window.value);
  64. }
  65. if (m_show_screen_number)
  66. Compositor::the().decrement_show_screen_number({});
  67. }
  68. void ConnectionFromClient::die()
  69. {
  70. deferred_invoke([this] {
  71. s_connections->remove(client_id());
  72. });
  73. }
  74. void ConnectionFromClient::notify_about_new_screen_rects()
  75. {
  76. auto& wm = WindowManager::the();
  77. async_screen_rects_changed(Screen::rects(), Screen::main().index(), wm.window_stack_rows(), wm.window_stack_columns());
  78. }
  79. void ConnectionFromClient::create_menu(i32 menu_id, DeprecatedString const& menu_title)
  80. {
  81. auto menu = Menu::construct(this, menu_id, menu_title);
  82. m_menus.set(menu_id, move(menu));
  83. }
  84. void ConnectionFromClient::destroy_menu(i32 menu_id)
  85. {
  86. auto it = m_menus.find(menu_id);
  87. if (it == m_menus.end()) {
  88. did_misbehave("DestroyMenu: Bad menu ID");
  89. return;
  90. }
  91. auto& menu = *(*it).value;
  92. menu.close();
  93. m_menus.remove(it);
  94. remove_child(menu);
  95. }
  96. void ConnectionFromClient::add_menu(i32 window_id, i32 menu_id)
  97. {
  98. auto it = m_windows.find(window_id);
  99. auto jt = m_menus.find(menu_id);
  100. if (it == m_windows.end()) {
  101. did_misbehave("AddMenu: Bad window ID");
  102. return;
  103. }
  104. if (jt == m_menus.end()) {
  105. did_misbehave("AddMenu: Bad menu ID");
  106. return;
  107. }
  108. auto& window = *(*it).value;
  109. auto& menu = *(*jt).value;
  110. window.add_menu(menu);
  111. }
  112. void ConnectionFromClient::add_menu_item(i32 menu_id, i32 identifier, i32 submenu_id,
  113. DeprecatedString const& text, bool enabled, bool checkable, bool checked, bool is_default,
  114. DeprecatedString const& shortcut, Gfx::ShareableBitmap const& icon, bool exclusive)
  115. {
  116. auto it = m_menus.find(menu_id);
  117. if (it == m_menus.end()) {
  118. dbgln("AddMenuItem: Bad menu ID: {}", menu_id);
  119. return;
  120. }
  121. auto& menu = *(*it).value;
  122. auto menu_item = make<MenuItem>(menu, identifier, text, shortcut, enabled, checkable, checked);
  123. if (is_default)
  124. menu_item->set_default(true);
  125. menu_item->set_icon(icon.bitmap());
  126. menu_item->set_submenu_id(submenu_id);
  127. menu_item->set_exclusive(exclusive);
  128. menu.add_item(move(menu_item));
  129. }
  130. void ConnectionFromClient::popup_menu(i32 menu_id, Gfx::IntPoint 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. DeprecatedString const& text, bool enabled, bool checkable, bool checked, bool is_default,
  156. DeprecatedString 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(DeprecatedString const& background_color)
  284. {
  285. Compositor::the().set_background_color(background_color);
  286. }
  287. void ConnectionFromClient::set_wallpaper_mode(DeprecatedString 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. DeprecatedString 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. DeprecatedString 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, DeprecatedString 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(DeprecatedString::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 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 base_size, Gfx::IntSize size_increment,
  521. Gfx::IntSize minimum_size, Optional<Gfx::IntSize> const& resize_aspect_ratio, i32 type, i32 mode,
  522. DeprecatedString 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. if (auto* blocker = parent_window->blocking_modal_window(); blocker && mode == (i32)WindowMode::Blocking) {
  532. did_misbehave("CreateWindow with illegal mode: reciprocally blocked");
  533. return;
  534. }
  535. }
  536. if (type < 0 || type >= (i32)WindowType::_Count) {
  537. did_misbehave("CreateWindow with a bad type");
  538. return;
  539. }
  540. if (mode < 0 || mode >= (i32)WindowMode::_Count) {
  541. did_misbehave("CreateWindow with a bad mode");
  542. return;
  543. }
  544. if (m_windows.contains(window_id)) {
  545. did_misbehave("CreateWindow with already-used window ID");
  546. return;
  547. }
  548. auto window = Window::construct(*this, (WindowType)type, (WindowMode)mode, window_id, minimizable, closeable, frameless, resizable, fullscreen, parent_window);
  549. window->set_forced_shadow(forced_shadow);
  550. if (!launch_origin_rect.is_empty())
  551. window->start_launch_animation(launch_origin_rect);
  552. window->set_has_alpha_channel(has_alpha_channel);
  553. window->set_title(title);
  554. if (!fullscreen) {
  555. Gfx::IntRect new_rect = rect;
  556. if (auto_position && window->is_movable()) {
  557. new_rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), rect.size() };
  558. window->set_default_positioned(true);
  559. }
  560. auto system_window_minimum_size = calculate_minimum_size_for_window(window);
  561. window->set_minimum_size({ max(minimum_size.width(), system_window_minimum_size.width()),
  562. max(minimum_size.height(), system_window_minimum_size.height()) });
  563. bool did_size_clamp = window->apply_minimum_size(new_rect);
  564. window->set_rect(new_rect);
  565. if (did_size_clamp)
  566. window->refresh_client_size();
  567. }
  568. if (window->type() == WindowType::Desktop) {
  569. window->set_rect(Screen::bounding_rect());
  570. window->recalculate_rect();
  571. }
  572. window->set_opacity(opacity);
  573. window->set_alpha_hit_threshold(alpha_hit_threshold);
  574. window->set_size_increment(size_increment);
  575. window->set_base_size(base_size);
  576. if (resize_aspect_ratio.has_value() && !resize_aspect_ratio.value().is_null())
  577. window->set_resize_aspect_ratio(resize_aspect_ratio);
  578. window->invalidate(true, true);
  579. if (window->type() == WindowType::Applet)
  580. AppletManager::the().add_applet(*window);
  581. m_windows.set(window_id, move(window));
  582. }
  583. void ConnectionFromClient::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  584. {
  585. for (auto& child_window : window.child_windows()) {
  586. if (!child_window)
  587. continue;
  588. VERIFY(child_window->window_id() != window.window_id());
  589. destroy_window(*child_window, destroyed_window_ids);
  590. }
  591. destroyed_window_ids.append(window.window_id());
  592. if (window.type() == WindowType::Applet)
  593. AppletManager::the().remove_applet(window);
  594. window.destroy();
  595. remove_child(window);
  596. m_windows.remove(window.window_id());
  597. }
  598. Messages::WindowServer::DestroyWindowResponse ConnectionFromClient::destroy_window(i32 window_id)
  599. {
  600. auto it = m_windows.find(window_id);
  601. if (it == m_windows.end()) {
  602. did_misbehave("DestroyWindow: Bad window ID");
  603. return nullptr;
  604. }
  605. auto& window = *(*it).value;
  606. Vector<i32> destroyed_window_ids;
  607. destroy_window(window, destroyed_window_ids);
  608. return destroyed_window_ids;
  609. }
  610. void ConnectionFromClient::post_paint_message(Window& window, bool ignore_occlusion)
  611. {
  612. auto rect_set = window.take_pending_paint_rects();
  613. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  614. return;
  615. async_paint(window.window_id(), window.size(), rect_set.rects());
  616. }
  617. void ConnectionFromClient::invalidate_rect(i32 window_id, Vector<Gfx::IntRect> const& rects, bool ignore_occlusion)
  618. {
  619. auto it = m_windows.find(window_id);
  620. if (it == m_windows.end()) {
  621. did_misbehave("InvalidateRect: Bad window ID");
  622. return;
  623. }
  624. auto& window = *(*it).value;
  625. for (size_t i = 0; i < rects.size(); ++i)
  626. window.request_update(rects[i].intersected(Gfx::Rect { {}, window.size() }), ignore_occlusion);
  627. }
  628. void ConnectionFromClient::did_finish_painting(i32 window_id, Vector<Gfx::IntRect> const& rects)
  629. {
  630. auto it = m_windows.find(window_id);
  631. if (it == m_windows.end()) {
  632. did_misbehave("DidFinishPainting: Bad window ID");
  633. return;
  634. }
  635. auto& window = *(*it).value;
  636. for (auto& rect : rects)
  637. window.invalidate(rect);
  638. if (window.has_alpha_channel() && window.alpha_hit_threshold() > 0.0f)
  639. WindowManager::the().reevaluate_hover_state_for_window(&window);
  640. WindowSwitcher::the().refresh_if_needed();
  641. }
  642. void ConnectionFromClient::set_window_backing_store(i32 window_id, [[maybe_unused]] i32 bpp,
  643. [[maybe_unused]] i32 pitch, IPC::File const& anon_file, i32 serial, bool has_alpha_channel,
  644. Gfx::IntSize size, bool flush_immediately)
  645. {
  646. auto it = m_windows.find(window_id);
  647. if (it == m_windows.end()) {
  648. did_misbehave("SetWindowBackingStore: Bad window ID");
  649. return;
  650. }
  651. auto& window = *(*it).value;
  652. if (window.last_backing_store() && window.last_backing_store_serial() == serial) {
  653. window.swap_backing_stores();
  654. } else {
  655. // FIXME: Plumb scale factor here eventually.
  656. auto buffer_or_error = Core::AnonymousBuffer::create_from_anon_fd(anon_file.take_fd(), pitch * size.height());
  657. if (buffer_or_error.is_error()) {
  658. did_misbehave("SetWindowBackingStore: Failed to create anonymous buffer for window backing store");
  659. return;
  660. }
  661. auto backing_store_or_error = Gfx::Bitmap::try_create_with_anonymous_buffer(
  662. has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
  663. buffer_or_error.release_value(),
  664. size,
  665. 1,
  666. {});
  667. if (backing_store_or_error.is_error()) {
  668. did_misbehave("");
  669. }
  670. window.set_backing_store(backing_store_or_error.release_value(), serial);
  671. }
  672. if (flush_immediately)
  673. window.invalidate(false);
  674. }
  675. void ConnectionFromClient::set_global_mouse_tracking(bool enabled)
  676. {
  677. m_does_global_mouse_tracking = enabled;
  678. }
  679. void ConnectionFromClient::set_window_cursor(i32 window_id, i32 cursor_type)
  680. {
  681. auto it = m_windows.find(window_id);
  682. if (it == m_windows.end()) {
  683. did_misbehave("SetWindowCursor: Bad window ID");
  684. return;
  685. }
  686. auto& window = *(*it).value;
  687. if (cursor_type < 0 || cursor_type >= (i32)Gfx::StandardCursor::__Count) {
  688. did_misbehave("SetWindowCursor: Bad cursor type");
  689. return;
  690. }
  691. window.set_cursor(Cursor::create((Gfx::StandardCursor)cursor_type));
  692. if (&window == WindowManager::the().hovered_window())
  693. Compositor::the().invalidate_cursor();
  694. }
  695. void ConnectionFromClient::set_window_custom_cursor(i32 window_id, Gfx::ShareableBitmap const& cursor)
  696. {
  697. auto it = m_windows.find(window_id);
  698. if (it == m_windows.end()) {
  699. did_misbehave("SetWindowCustomCursor: Bad window ID");
  700. return;
  701. }
  702. auto& window = *(*it).value;
  703. if (!cursor.is_valid()) {
  704. did_misbehave("SetWindowCustomCursor: Bad cursor");
  705. return;
  706. }
  707. window.set_cursor(Cursor::create(*cursor.bitmap(), 1));
  708. Compositor::the().invalidate_cursor();
  709. }
  710. void ConnectionFromClient::set_window_has_alpha_channel(i32 window_id, bool has_alpha_channel)
  711. {
  712. auto it = m_windows.find(window_id);
  713. if (it == m_windows.end()) {
  714. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  715. return;
  716. }
  717. it->value->set_has_alpha_channel(has_alpha_channel);
  718. }
  719. void ConnectionFromClient::set_window_alpha_hit_threshold(i32 window_id, float threshold)
  720. {
  721. auto it = m_windows.find(window_id);
  722. if (it == m_windows.end()) {
  723. did_misbehave("SetWindowAlphaHitThreshold: Bad window ID");
  724. return;
  725. }
  726. it->value->set_alpha_hit_threshold(threshold);
  727. }
  728. void ConnectionFromClient::start_window_resize(i32 window_id, i32 resize_direction)
  729. {
  730. auto it = m_windows.find(window_id);
  731. if (it == m_windows.end()) {
  732. did_misbehave("WM_StartWindowResize: Bad window ID");
  733. return;
  734. }
  735. if (resize_direction < 0 || resize_direction >= (i32)ResizeDirection::__Count) {
  736. did_misbehave("WM_StartWindowResize: Bad resize direction");
  737. return;
  738. }
  739. auto& window = *(*it).value;
  740. if (!window.is_resizable()) {
  741. dbgln("Client wants to start resizing a non-resizable window");
  742. return;
  743. }
  744. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  745. // Maybe the client should be allowed to specify what initiated this request?
  746. WindowManager::the().start_window_resize(window, ScreenInput::the().cursor_location(), MouseButton::Primary, (ResizeDirection)resize_direction);
  747. }
  748. Messages::WindowServer::StartDragResponse ConnectionFromClient::start_drag(DeprecatedString const& text, HashMap<DeprecatedString, ByteBuffer> const& mime_data, Gfx::ShareableBitmap const& drag_bitmap)
  749. {
  750. auto& wm = WindowManager::the();
  751. if (wm.dnd_client() || !(wm.last_processed_buttons() & MouseButton::Primary))
  752. return false;
  753. wm.start_dnd_drag(*this, text, drag_bitmap.bitmap(), Core::MimeData::construct(mime_data));
  754. return true;
  755. }
  756. void ConnectionFromClient::set_accepts_drag(bool accepts)
  757. {
  758. auto& wm = WindowManager::the();
  759. VERIFY(wm.dnd_client());
  760. wm.set_accepts_drag(accepts);
  761. }
  762. Messages::WindowServer::SetSystemThemeResponse ConnectionFromClient::set_system_theme(DeprecatedString const& theme_path, DeprecatedString const& theme_name, bool keep_desktop_background)
  763. {
  764. bool success = WindowManager::the().update_theme(theme_path, theme_name, keep_desktop_background);
  765. return success;
  766. }
  767. Messages::WindowServer::GetSystemThemeResponse ConnectionFromClient::get_system_theme()
  768. {
  769. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  770. auto name = wm_config->read_entry("Theme", "Name");
  771. return name;
  772. }
  773. Messages::WindowServer::SetSystemThemeOverrideResponse ConnectionFromClient::set_system_theme_override(Core::AnonymousBuffer const& theme_override)
  774. {
  775. bool success = WindowManager::the().set_theme_override(theme_override);
  776. return success;
  777. }
  778. Messages::WindowServer::GetSystemThemeOverrideResponse ConnectionFromClient::get_system_theme_override()
  779. {
  780. return WindowManager::the().get_theme_override();
  781. }
  782. void ConnectionFromClient::clear_system_theme_override()
  783. {
  784. WindowManager::the().clear_theme_override();
  785. }
  786. Messages::WindowServer::IsSystemThemeOverriddenResponse ConnectionFromClient::is_system_theme_overridden()
  787. {
  788. return WindowManager::the().is_theme_overridden();
  789. }
  790. void ConnectionFromClient::apply_cursor_theme(DeprecatedString const& name)
  791. {
  792. WindowManager::the().apply_cursor_theme(name);
  793. }
  794. void ConnectionFromClient::set_cursor_highlight_radius(int radius)
  795. {
  796. WindowManager::the().set_cursor_highlight_radius(radius);
  797. }
  798. Messages::WindowServer::GetCursorHighlightRadiusResponse ConnectionFromClient::get_cursor_highlight_radius()
  799. {
  800. return WindowManager::the().cursor_highlight_radius();
  801. }
  802. void ConnectionFromClient::set_cursor_highlight_color(Gfx::Color color)
  803. {
  804. WindowManager::the().set_cursor_highlight_color(color);
  805. }
  806. Messages::WindowServer::GetCursorHighlightColorResponse ConnectionFromClient::get_cursor_highlight_color()
  807. {
  808. return WindowManager::the().cursor_highlight_color();
  809. }
  810. Messages::WindowServer::GetCursorThemeResponse ConnectionFromClient::get_cursor_theme()
  811. {
  812. auto config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  813. auto name = config->read_entry("Mouse", "CursorTheme");
  814. return name;
  815. }
  816. Messages::WindowServer::SetSystemFontsResponse ConnectionFromClient::set_system_fonts(DeprecatedString const& default_font_query, DeprecatedString const& fixed_width_font_query, DeprecatedString const& window_title_font_query)
  817. {
  818. if (!Gfx::FontDatabase::the().get_by_name(default_font_query)
  819. || !Gfx::FontDatabase::the().get_by_name(fixed_width_font_query)) {
  820. dbgln("Received unusable font queries: '{}' and '{}'", default_font_query, fixed_width_font_query);
  821. return false;
  822. }
  823. dbgln("Updating fonts: '{}' and '{}'", default_font_query, fixed_width_font_query);
  824. Gfx::FontDatabase::set_default_font_query(default_font_query);
  825. Gfx::FontDatabase::set_fixed_width_font_query(fixed_width_font_query);
  826. Gfx::FontDatabase::set_window_title_font_query(window_title_font_query);
  827. ConnectionFromClient::for_each_client([&](auto& client) {
  828. client.async_update_system_fonts(default_font_query, fixed_width_font_query, window_title_font_query);
  829. });
  830. WindowManager::the().invalidate_after_theme_or_font_change();
  831. auto wm_config_or_error = Core::ConfigFile::open("/etc/WindowServer.ini", Core::ConfigFile::AllowWriting::Yes);
  832. if (wm_config_or_error.is_error()) {
  833. dbgln("Unable to open WindowServer.ini to set system fonts: {}", wm_config_or_error.error());
  834. return false;
  835. }
  836. auto wm_config = wm_config_or_error.release_value();
  837. wm_config->write_entry("Fonts", "Default", default_font_query);
  838. wm_config->write_entry("Fonts", "FixedWidth", fixed_width_font_query);
  839. wm_config->write_entry("Fonts", "WindowTitle", window_title_font_query);
  840. return true;
  841. }
  842. void ConnectionFromClient::set_system_effects(Vector<bool> const& effects, u8 geometry)
  843. {
  844. WindowManager::the().apply_system_effects(effects, static_cast<ShowGeometry>(geometry));
  845. ConnectionFromClient::for_each_client([&](auto& client) {
  846. client.async_update_system_effects(effects);
  847. });
  848. }
  849. void ConnectionFromClient::set_window_base_size_and_size_increment(i32 window_id, Gfx::IntSize base_size, Gfx::IntSize size_increment)
  850. {
  851. auto it = m_windows.find(window_id);
  852. if (it == m_windows.end()) {
  853. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  854. return;
  855. }
  856. auto& window = *it->value;
  857. window.set_base_size(base_size);
  858. window.set_size_increment(size_increment);
  859. }
  860. void ConnectionFromClient::set_window_resize_aspect_ratio(i32 window_id, Optional<Gfx::IntSize> const& resize_aspect_ratio)
  861. {
  862. auto it = m_windows.find(window_id);
  863. if (it == m_windows.end()) {
  864. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  865. return;
  866. }
  867. auto& window = *it->value;
  868. window.set_resize_aspect_ratio(resize_aspect_ratio);
  869. }
  870. void ConnectionFromClient::enable_display_link()
  871. {
  872. if (m_has_display_link)
  873. return;
  874. m_has_display_link = true;
  875. Compositor::the().increment_display_link_count({});
  876. }
  877. void ConnectionFromClient::disable_display_link()
  878. {
  879. if (!m_has_display_link)
  880. return;
  881. m_has_display_link = false;
  882. Compositor::the().decrement_display_link_count({});
  883. }
  884. void ConnectionFromClient::notify_display_link(Badge<Compositor>)
  885. {
  886. if (!m_has_display_link)
  887. return;
  888. async_display_link_notification();
  889. }
  890. void ConnectionFromClient::set_window_progress(i32 window_id, Optional<i32> const& progress)
  891. {
  892. auto it = m_windows.find(window_id);
  893. if (it == m_windows.end()) {
  894. did_misbehave("SetWindowProgress with bad window ID");
  895. return;
  896. }
  897. it->value->set_progress(progress);
  898. }
  899. void ConnectionFromClient::refresh_system_theme()
  900. {
  901. // Post the client an UpdateSystemTheme message to refresh its theme.
  902. async_update_system_theme(Gfx::current_system_theme_buffer());
  903. }
  904. void ConnectionFromClient::pong()
  905. {
  906. m_ping_timer = nullptr;
  907. set_unresponsive(false);
  908. }
  909. void ConnectionFromClient::set_global_cursor_position(Gfx::IntPoint position)
  910. {
  911. if (!Screen::main().rect().contains(position)) {
  912. did_misbehave("SetGlobalCursorPosition with bad position");
  913. return;
  914. }
  915. if (position != ScreenInput::the().cursor_location()) {
  916. ScreenInput::the().set_cursor_location(position);
  917. Compositor::the().invalidate_cursor();
  918. }
  919. }
  920. Messages::WindowServer::GetGlobalCursorPositionResponse ConnectionFromClient::get_global_cursor_position()
  921. {
  922. return ScreenInput::the().cursor_location();
  923. }
  924. void ConnectionFromClient::set_mouse_acceleration(float factor)
  925. {
  926. double dbl_factor = (double)factor;
  927. if (dbl_factor < mouse_accel_min || dbl_factor > mouse_accel_max) {
  928. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  929. return;
  930. }
  931. WindowManager::the().set_acceleration_factor(dbl_factor);
  932. }
  933. Messages::WindowServer::GetMouseAccelerationResponse ConnectionFromClient::get_mouse_acceleration()
  934. {
  935. return ScreenInput::the().acceleration_factor();
  936. }
  937. void ConnectionFromClient::set_scroll_step_size(u32 step_size)
  938. {
  939. if (step_size < scroll_step_size_min) {
  940. did_misbehave("SetScrollStepSize with bad scroll step size");
  941. return;
  942. }
  943. WindowManager::the().set_scroll_step_size(step_size);
  944. }
  945. Messages::WindowServer::GetScrollStepSizeResponse ConnectionFromClient::get_scroll_step_size()
  946. {
  947. return ScreenInput::the().scroll_step_size();
  948. }
  949. void ConnectionFromClient::set_double_click_speed(i32 speed)
  950. {
  951. if (speed < double_click_speed_min || speed > double_click_speed_max) {
  952. did_misbehave("SetDoubleClickSpeed with bad speed");
  953. return;
  954. }
  955. WindowManager::the().set_double_click_speed(speed);
  956. }
  957. Messages::WindowServer::GetDoubleClickSpeedResponse ConnectionFromClient::get_double_click_speed()
  958. {
  959. return WindowManager::the().double_click_speed();
  960. }
  961. void ConnectionFromClient::set_buttons_switched(bool switched)
  962. {
  963. WindowManager::the().set_buttons_switched(switched);
  964. }
  965. Messages::WindowServer::GetButtonsSwitchedResponse ConnectionFromClient::get_buttons_switched()
  966. {
  967. return WindowManager::the().get_buttons_switched();
  968. }
  969. void ConnectionFromClient::set_natural_scroll(bool inverted)
  970. {
  971. WindowManager::the().set_natural_scroll(inverted);
  972. }
  973. Messages::WindowServer::IsNaturalScrollResponse ConnectionFromClient::is_natural_scroll()
  974. {
  975. return WindowManager::the().is_natural_scroll();
  976. }
  977. void ConnectionFromClient::set_unresponsive(bool unresponsive)
  978. {
  979. if (m_unresponsive == unresponsive)
  980. return;
  981. m_unresponsive = unresponsive;
  982. for (auto& it : m_windows) {
  983. auto& window = *it.value;
  984. window.invalidate(true, true);
  985. if (unresponsive) {
  986. window.set_cursor_override(WindowManager::the().wait_cursor());
  987. } else {
  988. window.remove_cursor_override();
  989. }
  990. }
  991. Compositor::the().invalidate_cursor();
  992. }
  993. void ConnectionFromClient::may_have_become_unresponsive()
  994. {
  995. async_ping();
  996. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  997. set_unresponsive(true);
  998. });
  999. m_ping_timer->start();
  1000. }
  1001. void ConnectionFromClient::did_become_responsive()
  1002. {
  1003. set_unresponsive(false);
  1004. }
  1005. Messages::WindowServer::GetScreenBitmapResponse ConnectionFromClient::get_screen_bitmap(Optional<Gfx::IntRect> const& rect, Optional<u32> const& screen_index)
  1006. {
  1007. if (screen_index.has_value()) {
  1008. auto* screen = Screen::find_by_index(screen_index.value());
  1009. if (!screen) {
  1010. dbgln("get_screen_bitmap: Screen {} does not exist!", screen_index.value());
  1011. return { Gfx::ShareableBitmap() };
  1012. }
  1013. if (rect.has_value()) {
  1014. auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, *screen).cropped(rect.value());
  1015. if (bitmap_or_error.is_error()) {
  1016. dbgln("get_screen_bitmap: Failed to crop screenshot: {}", bitmap_or_error.error());
  1017. return { Gfx::ShareableBitmap() };
  1018. }
  1019. return bitmap_or_error.release_value()->to_shareable_bitmap();
  1020. }
  1021. auto& bitmap = Compositor::the().front_bitmap_for_screenshot({}, *screen);
  1022. return bitmap.to_shareable_bitmap();
  1023. }
  1024. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  1025. auto bitmap_size = rect.value_or(Screen::bounding_rect()).size();
  1026. if (auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, bitmap_size, 1); !bitmap_or_error.is_error()) {
  1027. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  1028. Gfx::Painter painter(*bitmap);
  1029. Screen::for_each([&](auto& screen) {
  1030. auto screen_rect = screen.rect();
  1031. if (rect.has_value() && !rect.value().intersects(screen_rect))
  1032. return IterationDecision::Continue;
  1033. auto src_rect = rect.has_value() ? rect.value().intersected(screen_rect) : screen_rect;
  1034. VERIFY(Screen::bounding_rect().contains(src_rect));
  1035. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  1036. // TODO: painter does *not* support down-sampling!!!
  1037. painter.blit(screen_rect.location(), screen_bitmap, src_rect.translated(-screen_rect.location()), 1.0f, false);
  1038. return IterationDecision::Continue;
  1039. });
  1040. return bitmap->to_shareable_bitmap();
  1041. }
  1042. return { Gfx::ShareableBitmap() };
  1043. }
  1044. Messages::WindowServer::GetScreenBitmapAroundCursorResponse ConnectionFromClient::get_screen_bitmap_around_cursor(Gfx::IntSize size)
  1045. {
  1046. // TODO: Mixed scale setups at what scale? Lowest? Highest? Configurable?
  1047. auto cursor_location = ScreenInput::the().cursor_location();
  1048. Gfx::Rect rect { cursor_location.x() - (size.width() / 2), cursor_location.y() - (size.height() / 2), size.width(), size.height() };
  1049. // Recompose the screen to make sure the cursor is painted in the location we think it is.
  1050. // FIXME: This is rather wasteful. We can probably think of a way to avoid this.
  1051. Compositor::the().compose();
  1052. // Check if we need to compose from multiple screens. If not we can take a fast path
  1053. size_t intersecting_with_screens = 0;
  1054. Screen::for_each([&](auto& screen) {
  1055. if (rect.intersects(screen.rect()))
  1056. intersecting_with_screens++;
  1057. return IterationDecision::Continue;
  1058. });
  1059. auto screen_scale_factor = ScreenInput::the().cursor_location_screen().scale_factor();
  1060. if (intersecting_with_screens == 1) {
  1061. auto& screen = Screen::closest_to_rect(rect);
  1062. auto crop_rect = rect.translated(-screen.rect().location()) * screen_scale_factor;
  1063. auto bitmap_or_error = Compositor::the().front_bitmap_for_screenshot({}, screen).cropped(crop_rect);
  1064. if (bitmap_or_error.is_error()) {
  1065. dbgln("get_screen_bitmap_around_cursor: Failed to crop screenshot: {}", bitmap_or_error.error());
  1066. return { {} };
  1067. }
  1068. return bitmap_or_error.release_value()->to_shareable_bitmap();
  1069. }
  1070. if (auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, rect.size(), 1); !bitmap_or_error.is_error()) {
  1071. auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  1072. auto bounding_screen_src_rect = Screen::bounding_rect().intersected(rect);
  1073. Gfx::Painter painter(*bitmap);
  1074. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  1075. auto cursor_rect = Compositor::the().current_cursor_rect();
  1076. Screen::for_each([&](auto& screen) {
  1077. auto screen_rect = screen.rect();
  1078. auto src_rect = screen_rect.intersected(bounding_screen_src_rect);
  1079. if (src_rect.is_empty())
  1080. return IterationDecision ::Continue;
  1081. auto& screen_bitmap = Compositor::the().front_bitmap_for_screenshot({}, screen);
  1082. // TODO: Add scaling support for multiple screens
  1083. auto from_rect = src_rect.translated(-screen_rect.location());
  1084. auto target_location = rect.intersected(screen_rect).location().translated(-rect.location());
  1085. // TODO: painter does *not* support down-sampling!!!
  1086. painter.blit(target_location, screen_bitmap, from_rect, 1.0f, false);
  1087. // Check if we are a screen that doesn't have the cursor but the cursor would
  1088. // have normally been cut off (we don't draw portions of the cursor on a screen
  1089. // that doesn't actually have the cursor). In that case we need to render the remaining
  1090. // portion of the cursor on that screen's capture manually
  1091. if (&screen != &screen_with_cursor) {
  1092. auto screen_cursor_rect = cursor_rect.intersected(screen_rect);
  1093. if (!screen_cursor_rect.is_empty()) {
  1094. if (auto const* cursor_bitmap = Compositor::the().cursor_bitmap_for_screenshot({}, screen)) {
  1095. auto src_rect = screen_cursor_rect.translated(-cursor_rect.location());
  1096. auto cursor_target = cursor_rect.intersected(screen_rect).location().translated(-rect.location());
  1097. // TODO: painter does *not* support down-sampling!!!
  1098. painter.blit(cursor_target, *cursor_bitmap, src_rect);
  1099. }
  1100. }
  1101. }
  1102. return IterationDecision::Continue;
  1103. });
  1104. return bitmap->to_shareable_bitmap();
  1105. }
  1106. return { {} };
  1107. }
  1108. Messages::WindowServer::GetColorUnderCursorResponse ConnectionFromClient::get_color_under_cursor()
  1109. {
  1110. auto screen_scale_factor = ScreenInput::the().cursor_location_screen().scale_factor();
  1111. // FIXME: Add a mechanism to get screen bitmap without cursor, so we don't have to do this
  1112. // manual translation to avoid sampling the color on the actual cursor itself.
  1113. auto cursor_location = (ScreenInput::the().cursor_location() * screen_scale_factor).translated(-1, -1);
  1114. auto& screen_with_cursor = ScreenInput::the().cursor_location_screen();
  1115. auto scaled_screen_rect = screen_with_cursor.rect() * screen_scale_factor;
  1116. if (!scaled_screen_rect.contains(cursor_location))
  1117. return Optional<Color> {};
  1118. return { Compositor::the().color_at_position({}, screen_with_cursor, cursor_location) };
  1119. }
  1120. Messages::WindowServer::IsWindowModifiedResponse ConnectionFromClient::is_window_modified(i32 window_id)
  1121. {
  1122. auto it = m_windows.find(window_id);
  1123. if (it == m_windows.end()) {
  1124. did_misbehave("IsWindowModified: Bad window ID");
  1125. return nullptr;
  1126. }
  1127. auto& window = *it->value;
  1128. return window.is_modified();
  1129. }
  1130. Messages::WindowServer::GetDesktopDisplayScaleResponse ConnectionFromClient::get_desktop_display_scale(u32 screen_index)
  1131. {
  1132. if (auto* screen = Screen::find_by_index(screen_index))
  1133. return screen->scale_factor();
  1134. dbgln("GetDesktopDisplayScale: Screen {} does not exist", screen_index);
  1135. return 0;
  1136. }
  1137. void ConnectionFromClient::set_window_modified(i32 window_id, bool modified)
  1138. {
  1139. auto it = m_windows.find(window_id);
  1140. if (it == m_windows.end()) {
  1141. did_misbehave("SetWindowModified: Bad window ID");
  1142. return;
  1143. }
  1144. auto& window = *it->value;
  1145. window.set_modified(modified);
  1146. }
  1147. void ConnectionFromClient::set_flash_flush(bool enabled)
  1148. {
  1149. Compositor::the().set_flash_flush(enabled);
  1150. }
  1151. void ConnectionFromClient::set_window_parent_from_client(i32 client_id, i32 parent_id, i32 child_id)
  1152. {
  1153. auto* child_window = window_from_id(child_id);
  1154. if (!child_window) {
  1155. did_misbehave("SetWindowParentFromClient: Bad child window ID");
  1156. return;
  1157. }
  1158. auto* client_connection = from_client_id(client_id);
  1159. if (!client_connection) {
  1160. did_misbehave("SetWindowParentFromClient: Bad client ID");
  1161. return;
  1162. }
  1163. auto* parent_window = client_connection->window_from_id(parent_id);
  1164. if (!parent_window) {
  1165. did_misbehave("SetWindowParentFromClient: Bad parent window ID");
  1166. return;
  1167. }
  1168. if (parent_window->is_stealable_by_client(this->client_id())) {
  1169. child_window->set_parent_window(*parent_window);
  1170. } else {
  1171. did_misbehave("SetWindowParentFromClient: Window is not stealable");
  1172. }
  1173. }
  1174. Messages::WindowServer::GetWindowRectFromClientResponse ConnectionFromClient::get_window_rect_from_client(i32 client_id, i32 window_id)
  1175. {
  1176. auto* client_connection = from_client_id(client_id);
  1177. if (!client_connection) {
  1178. did_misbehave("GetWindowRectFromClient: Bad client ID");
  1179. return { Gfx::IntRect() };
  1180. }
  1181. auto* window = client_connection->window_from_id(window_id);
  1182. if (!window) {
  1183. did_misbehave("GetWindowRectFromClient: Bad window ID");
  1184. return { Gfx::IntRect() };
  1185. }
  1186. return window->rect();
  1187. }
  1188. void ConnectionFromClient::add_window_stealing_for_client(i32 client_id, i32 window_id)
  1189. {
  1190. auto* window = window_from_id(window_id);
  1191. if (!window) {
  1192. did_misbehave("AddWindowStealingForClient: Bad window ID");
  1193. return;
  1194. }
  1195. if (!from_client_id(client_id)) {
  1196. did_misbehave("AddWindowStealingForClient: Bad client ID");
  1197. return;
  1198. }
  1199. window->add_stealing_for_client(client_id);
  1200. }
  1201. void ConnectionFromClient::remove_window_stealing_for_client(i32 client_id, i32 window_id)
  1202. {
  1203. auto* window = window_from_id(window_id);
  1204. if (!window) {
  1205. did_misbehave("RemoveWindowStealingForClient: Bad window ID");
  1206. return;
  1207. }
  1208. // Don't check if the client exists, it may have died
  1209. window->remove_stealing_for_client(client_id);
  1210. }
  1211. void ConnectionFromClient::remove_window_stealing(i32 window_id)
  1212. {
  1213. auto* window = window_from_id(window_id);
  1214. if (!window) {
  1215. did_misbehave("RemoveWindowStealing: Bad window ID");
  1216. return;
  1217. }
  1218. window->remove_all_stealing();
  1219. }
  1220. void ConnectionFromClient::set_always_on_top(i32 window_id, bool always_on_top)
  1221. {
  1222. auto* window = window_from_id(window_id);
  1223. if (!window) {
  1224. did_misbehave("SetAlwaysOnTop: Bad window ID");
  1225. return;
  1226. }
  1227. window->set_always_on_top(always_on_top);
  1228. }
  1229. void ConnectionFromClient::notify_about_theme_change()
  1230. {
  1231. // Recalculate minimum size for each window, using the new theme metrics.
  1232. // FIXME: We only ever increase the minimum size, which means that if you go from a theme with large buttons
  1233. // (eg Basalt) to one with smaller buttons (eg Default) then the minimum size will remain large. This
  1234. // only happens with pre-existing windows, and it's unlikely that you will ever have windows that are
  1235. // so small, so it's probably fine, but it is technically a bug. :^)
  1236. for_each_window([](auto& window) -> IterationDecision {
  1237. auto system_window_minimum_size = calculate_minimum_size_for_window(window);
  1238. auto old_minimum_size = window.minimum_size();
  1239. auto new_rect = window.rect();
  1240. window.set_minimum_size({ max(old_minimum_size.width(), system_window_minimum_size.width()),
  1241. max(old_minimum_size.height(), system_window_minimum_size.height()) });
  1242. if (window.apply_minimum_size(new_rect)) {
  1243. window.set_rect(new_rect);
  1244. window.refresh_client_size();
  1245. }
  1246. return IterationDecision::Continue;
  1247. });
  1248. async_update_system_theme(Gfx::current_system_theme_buffer());
  1249. }
  1250. }