ClientConnection.cpp 35 KB

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