ClientConnection.cpp 37 KB

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