ClientConnection.cpp 42 KB

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