ClientConnection.cpp 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Badge.h>
  27. #include <LibGfx/Bitmap.h>
  28. #include <LibGfx/StandardCursor.h>
  29. #include <LibGfx/SystemTheme.h>
  30. #include <WindowServer/AppletManager.h>
  31. #include <WindowServer/ClientConnection.h>
  32. #include <WindowServer/Compositor.h>
  33. #include <WindowServer/Menu.h>
  34. #include <WindowServer/MenuItem.h>
  35. #include <WindowServer/Menubar.h>
  36. #include <WindowServer/Screen.h>
  37. #include <WindowServer/Window.h>
  38. #include <WindowServer/WindowClientEndpoint.h>
  39. #include <WindowServer/WindowManager.h>
  40. #include <WindowServer/WindowSwitcher.h>
  41. #include <errno.h>
  42. #include <serenity.h>
  43. #include <stdio.h>
  44. #include <unistd.h>
  45. namespace WindowServer {
  46. HashMap<int, NonnullRefPtr<ClientConnection>>* s_connections;
  47. void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
  48. {
  49. if (!s_connections)
  50. return;
  51. for (auto& it : *s_connections) {
  52. callback(*it.value);
  53. }
  54. }
  55. ClientConnection* ClientConnection::from_client_id(int client_id)
  56. {
  57. if (!s_connections)
  58. return nullptr;
  59. auto it = s_connections->find(client_id);
  60. if (it == s_connections->end())
  61. return nullptr;
  62. return (*it).value.ptr();
  63. }
  64. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  65. : IPC::ClientConnection<WindowClientEndpoint, WindowServerEndpoint>(*this, move(client_socket), client_id)
  66. {
  67. if (!s_connections)
  68. s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;
  69. s_connections->set(client_id, *this);
  70. }
  71. ClientConnection::~ClientConnection()
  72. {
  73. if (m_has_display_link)
  74. Compositor::the().decrement_display_link_count({});
  75. MenuManager::the().close_all_menus_from_client({}, *this);
  76. auto windows = move(m_windows);
  77. for (auto& window : windows) {
  78. window.value->detach_client({});
  79. if (window.value->type() == WindowType::Applet)
  80. AppletManager::the().remove_applet(window.value);
  81. }
  82. }
  83. void ClientConnection::die()
  84. {
  85. deferred_invoke([this](auto&) {
  86. s_connections->remove(client_id());
  87. });
  88. }
  89. void ClientConnection::notify_about_new_screen_rect(const Gfx::IntRect& rect)
  90. {
  91. post_message(Messages::WindowClient::ScreenRectChanged(rect));
  92. }
  93. OwnPtr<Messages::WindowServer::CreateMenubarResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenubar&)
  94. {
  95. int menubar_id = m_next_menubar_id++;
  96. auto menubar = Menubar::create(*this, menubar_id);
  97. m_menubars.set(menubar_id, move(menubar));
  98. return make<Messages::WindowServer::CreateMenubarResponse>(menubar_id);
  99. }
  100. OwnPtr<Messages::WindowServer::DestroyMenubarResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenubar& message)
  101. {
  102. int menubar_id = message.menubar_id();
  103. auto it = m_menubars.find(menubar_id);
  104. if (it == m_menubars.end()) {
  105. did_misbehave("DestroyMenubar: Bad menubar ID");
  106. return {};
  107. }
  108. m_menubars.remove(it);
  109. return make<Messages::WindowServer::DestroyMenubarResponse>();
  110. }
  111. OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
  112. {
  113. int menu_id = m_next_menu_id++;
  114. auto menu = Menu::construct(this, menu_id, message.menu_title());
  115. m_menus.set(menu_id, move(menu));
  116. return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
  117. }
  118. OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
  119. {
  120. int menu_id = message.menu_id();
  121. auto it = m_menus.find(menu_id);
  122. if (it == m_menus.end()) {
  123. did_misbehave("DestroyMenu: Bad menu ID");
  124. return {};
  125. }
  126. auto& menu = *(*it).value;
  127. menu.close();
  128. m_menus.remove(it);
  129. remove_child(menu);
  130. return make<Messages::WindowServer::DestroyMenuResponse>();
  131. }
  132. OwnPtr<Messages::WindowServer::SetWindowMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowMenubar& message)
  133. {
  134. RefPtr<Window> window;
  135. {
  136. auto it = m_windows.find(message.window_id());
  137. if (it == m_windows.end()) {
  138. did_misbehave("SetWindowMenubar: Bad window ID");
  139. return {};
  140. }
  141. window = it->value;
  142. }
  143. RefPtr<Menubar> menubar;
  144. if (message.menubar_id() != -1) {
  145. auto it = m_menubars.find(message.menubar_id());
  146. if (it == m_menubars.end()) {
  147. did_misbehave("SetWindowMenubar: Bad menubar ID");
  148. return {};
  149. }
  150. menubar = *(*it).value;
  151. }
  152. window->set_menubar(menubar);
  153. return make<Messages::WindowServer::SetWindowMenubarResponse>();
  154. }
  155. OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
  156. {
  157. int menubar_id = message.menubar_id();
  158. int menu_id = message.menu_id();
  159. auto it = m_menubars.find(menubar_id);
  160. auto jt = m_menus.find(menu_id);
  161. if (it == m_menubars.end()) {
  162. did_misbehave("AddMenuToMenubar: Bad menubar ID");
  163. return {};
  164. }
  165. if (jt == m_menus.end()) {
  166. did_misbehave("AddMenuToMenubar: Bad menu ID");
  167. return {};
  168. }
  169. auto& menubar = *(*it).value;
  170. auto& menu = *(*jt).value;
  171. menubar.add_menu(menu);
  172. return make<Messages::WindowServer::AddMenuToMenubarResponse>();
  173. }
  174. OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
  175. {
  176. int menu_id = message.menu_id();
  177. unsigned identifier = message.identifier();
  178. auto it = m_menus.find(menu_id);
  179. if (it == m_menus.end()) {
  180. dbgln("AddMenuItem: Bad menu ID: {}", menu_id);
  181. return {};
  182. }
  183. auto& menu = *(*it).value;
  184. auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
  185. if (message.is_default())
  186. menu_item->set_default(true);
  187. menu_item->set_icon(message.icon().bitmap());
  188. menu_item->set_submenu_id(message.submenu_id());
  189. menu_item->set_exclusive(message.exclusive());
  190. menu.add_item(move(menu_item));
  191. return make<Messages::WindowServer::AddMenuItemResponse>();
  192. }
  193. OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
  194. {
  195. int menu_id = message.menu_id();
  196. auto position = message.screen_position();
  197. auto it = m_menus.find(menu_id);
  198. if (it == m_menus.end()) {
  199. did_misbehave("PopupMenu: Bad menu ID");
  200. return {};
  201. }
  202. auto& menu = *(*it).value;
  203. menu.popup(position);
  204. return make<Messages::WindowServer::PopupMenuResponse>();
  205. }
  206. OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
  207. {
  208. int menu_id = message.menu_id();
  209. auto it = m_menus.find(menu_id);
  210. if (it == m_menus.end()) {
  211. did_misbehave("DismissMenu: Bad menu ID");
  212. return {};
  213. }
  214. auto& menu = *(*it).value;
  215. menu.close();
  216. return make<Messages::WindowServer::DismissMenuResponse>();
  217. }
  218. OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
  219. {
  220. int menu_id = message.menu_id();
  221. auto it = m_menus.find(menu_id);
  222. if (it == m_menus.end()) {
  223. did_misbehave("UpdateMenuItem: Bad menu ID");
  224. return {};
  225. }
  226. auto& menu = *(*it).value;
  227. auto* menu_item = menu.item_with_identifier(message.identifier());
  228. if (!menu_item) {
  229. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  230. return {};
  231. }
  232. menu_item->set_text(message.text());
  233. menu_item->set_shortcut_text(message.shortcut());
  234. menu_item->set_enabled(message.enabled());
  235. menu_item->set_checkable(message.checkable());
  236. menu_item->set_default(message.is_default());
  237. if (message.checkable())
  238. menu_item->set_checked(message.checked());
  239. return make<Messages::WindowServer::UpdateMenuItemResponse>();
  240. }
  241. OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuSeparator& message)
  242. {
  243. int menu_id = message.menu_id();
  244. auto it = m_menus.find(menu_id);
  245. if (it == m_menus.end()) {
  246. did_misbehave("AddMenuSeparator: Bad menu ID");
  247. return {};
  248. }
  249. auto& menu = *(*it).value;
  250. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  251. return make<Messages::WindowServer::AddMenuSeparatorResponse>();
  252. }
  253. OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> ClientConnection::handle(const Messages::WindowServer::MoveWindowToFront& message)
  254. {
  255. auto it = m_windows.find(message.window_id());
  256. if (it == m_windows.end()) {
  257. did_misbehave("MoveWindowToFront: Bad window ID");
  258. return {};
  259. }
  260. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  261. return make<Messages::WindowServer::MoveWindowToFrontResponse>();
  262. }
  263. OwnPtr<Messages::WindowServer::SetFullscreenResponse> ClientConnection::handle(const Messages::WindowServer::SetFullscreen& message)
  264. {
  265. auto it = m_windows.find(message.window_id());
  266. if (it == m_windows.end()) {
  267. did_misbehave("SetFullscreen: Bad window ID");
  268. return {};
  269. }
  270. it->value->set_fullscreen(message.fullscreen());
  271. return make<Messages::WindowServer::SetFullscreenResponse>();
  272. }
  273. OwnPtr<Messages::WindowServer::SetFramelessResponse> ClientConnection::handle(const Messages::WindowServer::SetFrameless& message)
  274. {
  275. auto it = m_windows.find(message.window_id());
  276. if (it == m_windows.end()) {
  277. did_misbehave("SetFrameless: Bad window ID");
  278. return {};
  279. }
  280. it->value->set_frameless(message.frameless());
  281. WindowManager::the().tell_wm_listeners_window_state_changed(*it->value);
  282. return make<Messages::WindowServer::SetFramelessResponse>();
  283. }
  284. OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOpacity& message)
  285. {
  286. auto it = m_windows.find(message.window_id());
  287. if (it == m_windows.end()) {
  288. did_misbehave("SetWindowOpacity: Bad window ID");
  289. return {};
  290. }
  291. it->value->set_opacity(message.opacity());
  292. return make<Messages::WindowServer::SetWindowOpacityResponse>();
  293. }
  294. void ClientConnection::handle(const Messages::WindowServer::AsyncSetWallpaper& message)
  295. {
  296. Compositor::the().set_wallpaper(message.path(), [&](bool success) {
  297. post_message(Messages::WindowClient::AsyncSetWallpaperFinished(success));
  298. });
  299. }
  300. OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> ClientConnection::handle(const Messages::WindowServer::SetBackgroundColor& message)
  301. {
  302. Compositor::the().set_background_color(message.background_color());
  303. return make<Messages::WindowServer::SetBackgroundColorResponse>();
  304. }
  305. OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> ClientConnection::handle(const Messages::WindowServer::SetWallpaperMode& message)
  306. {
  307. Compositor::the().set_wallpaper_mode(message.mode());
  308. return make<Messages::WindowServer::SetWallpaperModeResponse>();
  309. }
  310. OwnPtr<Messages::WindowServer::GetWallpaperResponse> ClientConnection::handle(const Messages::WindowServer::GetWallpaper&)
  311. {
  312. return make<Messages::WindowServer::GetWallpaperResponse>(Compositor::the().wallpaper_path());
  313. }
  314. OwnPtr<Messages::WindowServer::SetResolutionResponse> ClientConnection::handle(const Messages::WindowServer::SetResolution& message)
  315. {
  316. return make<Messages::WindowServer::SetResolutionResponse>(WindowManager::the().set_resolution(message.resolution().width(), message.resolution().height(), message.scale_factor()), WindowManager::the().resolution(), WindowManager::the().scale_factor());
  317. }
  318. OwnPtr<Messages::WindowServer::SetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowTitle& message)
  319. {
  320. auto it = m_windows.find(message.window_id());
  321. if (it == m_windows.end()) {
  322. did_misbehave("SetWindowTitle: Bad window ID");
  323. return {};
  324. }
  325. it->value->set_title(message.title());
  326. return make<Messages::WindowServer::SetWindowTitleResponse>();
  327. }
  328. OwnPtr<Messages::WindowServer::GetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowTitle& message)
  329. {
  330. auto it = m_windows.find(message.window_id());
  331. if (it == m_windows.end()) {
  332. did_misbehave("GetWindowTitle: Bad window ID");
  333. return {};
  334. }
  335. return make<Messages::WindowServer::GetWindowTitleResponse>(it->value->title());
  336. }
  337. OwnPtr<Messages::WindowServer::IsMaximizedResponse> ClientConnection::handle(const Messages::WindowServer::IsMaximized& message)
  338. {
  339. auto it = m_windows.find(message.window_id());
  340. if (it == m_windows.end()) {
  341. did_misbehave("IsMaximized: Bad window ID");
  342. return {};
  343. }
  344. return make<Messages::WindowServer::IsMaximizedResponse>(it->value->is_maximized());
  345. }
  346. OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
  347. {
  348. auto it = m_windows.find(message.window_id());
  349. if (it == m_windows.end()) {
  350. did_misbehave("SetWindowIconBitmap: Bad window ID");
  351. return {};
  352. }
  353. auto& window = *(*it).value;
  354. if (message.icon().is_valid()) {
  355. window.set_icon(*message.icon().bitmap());
  356. } else {
  357. window.set_default_icon();
  358. }
  359. window.frame().invalidate_title_bar();
  360. WindowManager::the().tell_wm_listeners_window_icon_changed(window);
  361. return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
  362. }
  363. OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
  364. {
  365. int window_id = message.window_id();
  366. auto it = m_windows.find(window_id);
  367. if (it == m_windows.end()) {
  368. did_misbehave("SetWindowRect: Bad window ID");
  369. return {};
  370. }
  371. auto& window = *(*it).value;
  372. if (window.is_fullscreen()) {
  373. dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
  374. return {};
  375. }
  376. if (message.rect().location() != window.rect().location()) {
  377. window.set_default_positioned(false);
  378. }
  379. auto rect = message.rect();
  380. window.apply_minimum_size(rect);
  381. window.set_rect(rect);
  382. window.nudge_into_desktop();
  383. window.request_update(window.rect());
  384. return make<Messages::WindowServer::SetWindowRectResponse>(window.rect());
  385. }
  386. OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& message)
  387. {
  388. int window_id = message.window_id();
  389. auto it = m_windows.find(window_id);
  390. if (it == m_windows.end()) {
  391. did_misbehave("GetWindowRect: Bad window ID");
  392. return {};
  393. }
  394. return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
  395. }
  396. OwnPtr<Messages::WindowServer::SetWindowMinimumSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowMinimumSize& message)
  397. {
  398. int window_id = message.window_id();
  399. auto it = m_windows.find(window_id);
  400. if (it == m_windows.end()) {
  401. did_misbehave("SetWindowMinimumSize: Bad window ID");
  402. return {};
  403. }
  404. auto& window = *(*it).value;
  405. if (window.is_fullscreen()) {
  406. dbgln("ClientConnection: Ignoring SetWindowMinimumSize request for fullscreen window");
  407. return {};
  408. }
  409. window.set_minimum_size(message.size());
  410. if (window.width() < window.minimum_size().width() || window.height() < window.minimum_size().height()) {
  411. // New minimum size is larger than the current window size, resize accordingly.
  412. auto new_rect = window.rect();
  413. bool did_size_clamp = window.apply_minimum_size(new_rect);
  414. window.set_rect(new_rect);
  415. window.nudge_into_desktop();
  416. window.request_update(window.rect());
  417. if (did_size_clamp)
  418. window.refresh_client_size();
  419. }
  420. return make<Messages::WindowServer::SetWindowMinimumSizeResponse>();
  421. }
  422. OwnPtr<Messages::WindowServer::GetWindowMinimumSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowMinimumSize& message)
  423. {
  424. int window_id = message.window_id();
  425. auto it = m_windows.find(window_id);
  426. if (it == m_windows.end()) {
  427. did_misbehave("GetWindowMinimumSize: Bad window ID");
  428. return {};
  429. }
  430. return make<Messages::WindowServer::GetWindowMinimumSizeResponse>(it->value->minimum_size());
  431. }
  432. OwnPtr<Messages::WindowServer::GetAppletRectOnScreenResponse> ClientConnection::handle(const Messages::WindowServer::GetAppletRectOnScreen& message)
  433. {
  434. int window_id = message.window_id();
  435. auto it = m_windows.find(window_id);
  436. if (it == m_windows.end()) {
  437. did_misbehave("GetAppletRectOnScreen: Bad window ID");
  438. return {};
  439. }
  440. Gfx::IntRect applet_area_rect;
  441. if (auto* applet_area_window = AppletManager::the().window())
  442. applet_area_rect = applet_area_window->rect();
  443. return make<Messages::WindowServer::GetAppletRectOnScreenResponse>(it->value->rect_in_applet_area().translated(applet_area_rect.location()));
  444. }
  445. Window* ClientConnection::window_from_id(i32 window_id)
  446. {
  447. auto it = m_windows.find(window_id);
  448. if (it == m_windows.end())
  449. return nullptr;
  450. return it->value.ptr();
  451. }
  452. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  453. {
  454. Window* parent_window = nullptr;
  455. if (message.parent_window_id()) {
  456. parent_window = window_from_id(message.parent_window_id());
  457. if (!parent_window) {
  458. did_misbehave("CreateWindow with bad parent_window_id");
  459. return {};
  460. }
  461. }
  462. int window_id = m_next_window_id++;
  463. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.frameless(), message.resizable(), message.fullscreen(), message.accessory(), parent_window);
  464. window->set_has_alpha_channel(message.has_alpha_channel());
  465. window->set_title(message.title());
  466. if (!message.fullscreen()) {
  467. auto rect = message.rect();
  468. if (message.auto_position() && window->is_movable()) {
  469. rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), message.rect().size() };
  470. window->set_default_positioned(true);
  471. }
  472. window->set_minimum_size(message.minimum_size());
  473. bool did_size_clamp = window->apply_minimum_size(rect);
  474. window->set_rect(rect);
  475. window->nudge_into_desktop();
  476. if (did_size_clamp)
  477. window->refresh_client_size();
  478. }
  479. if (window->type() == WindowType::Desktop) {
  480. window->set_rect(WindowManager::the().desktop_rect());
  481. window->recalculate_rect();
  482. }
  483. window->set_opacity(message.opacity());
  484. window->set_alpha_hit_threshold(message.alpha_hit_threshold());
  485. window->set_size_increment(message.size_increment());
  486. window->set_base_size(message.base_size());
  487. window->set_resize_aspect_ratio(message.resize_aspect_ratio());
  488. window->invalidate(true, true);
  489. if (window->type() == WindowType::Applet)
  490. AppletManager::the().add_applet(*window);
  491. m_windows.set(window_id, move(window));
  492. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  493. }
  494. void ClientConnection::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  495. {
  496. for (auto& child_window : window.child_windows()) {
  497. if (!child_window)
  498. continue;
  499. VERIFY(child_window->window_id() != window.window_id());
  500. destroy_window(*child_window, destroyed_window_ids);
  501. }
  502. for (auto& accessory_window : window.accessory_windows()) {
  503. if (!accessory_window)
  504. continue;
  505. VERIFY(accessory_window->window_id() != window.window_id());
  506. destroy_window(*accessory_window, destroyed_window_ids);
  507. }
  508. destroyed_window_ids.append(window.window_id());
  509. if (window.type() == WindowType::Applet)
  510. AppletManager::the().remove_applet(window);
  511. window.destroy();
  512. remove_child(window);
  513. m_windows.remove(window.window_id());
  514. }
  515. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  516. {
  517. auto it = m_windows.find(message.window_id());
  518. if (it == m_windows.end()) {
  519. did_misbehave("DestroyWindow: Bad window ID");
  520. return {};
  521. }
  522. auto& window = *(*it).value;
  523. Vector<i32> destroyed_window_ids;
  524. destroy_window(window, destroyed_window_ids);
  525. return make<Messages::WindowServer::DestroyWindowResponse>(destroyed_window_ids);
  526. }
  527. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  528. {
  529. auto rect_set = window.take_pending_paint_rects();
  530. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  531. return;
  532. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  533. }
  534. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  535. {
  536. auto it = m_windows.find(message.window_id());
  537. if (it == m_windows.end()) {
  538. did_misbehave("InvalidateRect: Bad window ID");
  539. return;
  540. }
  541. auto& window = *(*it).value;
  542. for (size_t i = 0; i < message.rects().size(); ++i)
  543. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  544. }
  545. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  546. {
  547. int window_id = message.window_id();
  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 : message.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. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  561. {
  562. int window_id = message.window_id();
  563. auto it = m_windows.find(window_id);
  564. if (it == m_windows.end()) {
  565. did_misbehave("SetWindowBackingStore: Bad window ID");
  566. return {};
  567. }
  568. auto& window = *(*it).value;
  569. if (window.last_backing_store() && window.last_backing_store_serial() == message.serial()) {
  570. window.swap_backing_stores();
  571. } else {
  572. // FIXME: Plumb scale factor here eventually.
  573. auto backing_store = Gfx::Bitmap::create_with_anon_fd(
  574. message.has_alpha_channel() ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
  575. message.anon_file().take_fd(),
  576. message.size(),
  577. 1,
  578. {},
  579. Gfx::Bitmap::ShouldCloseAnonymousFile::Yes);
  580. window.set_backing_store(move(backing_store), message.serial());
  581. }
  582. if (message.flush_immediately())
  583. window.invalidate(false);
  584. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  585. }
  586. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  587. {
  588. int window_id = message.window_id();
  589. auto it = m_windows.find(window_id);
  590. if (it == m_windows.end()) {
  591. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  592. return {};
  593. }
  594. it->value->set_global_cursor_tracking_enabled(message.enabled());
  595. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  596. }
  597. OwnPtr<Messages::WindowServer::SetWindowCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCursor& message)
  598. {
  599. auto it = m_windows.find(message.window_id());
  600. if (it == m_windows.end()) {
  601. did_misbehave("SetWindowCursor: Bad window ID");
  602. return {};
  603. }
  604. auto& window = *(*it).value;
  605. if (message.cursor_type() < 0 || message.cursor_type() >= (i32)Gfx::StandardCursor::__Count) {
  606. did_misbehave("SetWindowCursor: Bad cursor type");
  607. return {};
  608. }
  609. window.set_cursor(Cursor::create((Gfx::StandardCursor)message.cursor_type()));
  610. if (&window == WindowManager::the().hovered_window())
  611. Compositor::the().invalidate_cursor();
  612. return make<Messages::WindowServer::SetWindowCursorResponse>();
  613. }
  614. OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomCursor& message)
  615. {
  616. auto it = m_windows.find(message.window_id());
  617. if (it == m_windows.end()) {
  618. did_misbehave("SetWindowCustomCursor: Bad window ID");
  619. return {};
  620. }
  621. auto& window = *(*it).value;
  622. if (!message.cursor().is_valid()) {
  623. did_misbehave("SetWindowCustomCursor: Bad cursor");
  624. return {};
  625. }
  626. window.set_cursor(Cursor::create(*message.cursor().bitmap()));
  627. Compositor::the().invalidate_cursor();
  628. return make<Messages::WindowServer::SetWindowCustomCursorResponse>();
  629. }
  630. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  631. {
  632. auto it = m_windows.find(message.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(message.has_alpha_channel());
  638. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  639. }
  640. OwnPtr<Messages::WindowServer::SetWindowAlphaHitThresholdResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowAlphaHitThreshold& message)
  641. {
  642. auto it = m_windows.find(message.window_id());
  643. if (it == m_windows.end()) {
  644. did_misbehave("SetWindowAlphaHitThreshold: Bad window ID");
  645. return {};
  646. }
  647. it->value->set_alpha_hit_threshold(message.threshold());
  648. return make<Messages::WindowServer::SetWindowAlphaHitThresholdResponse>();
  649. }
  650. OwnPtr<Messages::WindowServer::WM_SetAppletAreaPositionResponse> ClientConnection::handle(const Messages::WindowServer::WM_SetAppletAreaPosition& message)
  651. {
  652. AppletManager::the().set_position(message.position());
  653. return make<Messages::WindowServer::WM_SetAppletAreaPositionResponse>();
  654. }
  655. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  656. {
  657. auto* client = ClientConnection::from_client_id(message.client_id());
  658. if (!client) {
  659. did_misbehave("WM_SetActiveWindow: Bad client ID");
  660. return;
  661. }
  662. auto it = client->m_windows.find(message.window_id());
  663. if (it == client->m_windows.end()) {
  664. did_misbehave("WM_SetActiveWindow: Bad window ID");
  665. return;
  666. }
  667. auto& window = *(*it).value;
  668. WindowManager::the().minimize_windows(window, false);
  669. WindowManager::the().move_to_front_and_make_active(window);
  670. }
  671. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  672. {
  673. auto* client = ClientConnection::from_client_id(message.client_id());
  674. if (!client) {
  675. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  676. return;
  677. }
  678. auto it = client->m_windows.find(message.window_id());
  679. if (it == client->m_windows.end()) {
  680. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  681. return;
  682. }
  683. auto& window = *(*it).value;
  684. if (auto* modal_window = window.blocking_modal_window()) {
  685. modal_window->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  686. } else {
  687. window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  688. }
  689. }
  690. void ClientConnection::handle(const Messages::WindowServer::StartWindowResize& request)
  691. {
  692. auto it = m_windows.find(request.window_id());
  693. if (it == m_windows.end()) {
  694. did_misbehave("WM_StartWindowResize: Bad window ID");
  695. return;
  696. }
  697. auto& window = *(*it).value;
  698. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  699. // Maybe the client should be allowed to specify what initiated this request?
  700. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  701. }
  702. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  703. {
  704. auto* client = ClientConnection::from_client_id(request.client_id());
  705. if (!client) {
  706. did_misbehave("WM_StartWindowResize: Bad client ID");
  707. return;
  708. }
  709. auto it = client->m_windows.find(request.window_id());
  710. if (it == client->m_windows.end()) {
  711. did_misbehave("WM_StartWindowResize: Bad window ID");
  712. return;
  713. }
  714. auto& window = *(*it).value;
  715. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  716. // Maybe the client should be allowed to specify what initiated this request?
  717. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  718. }
  719. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  720. {
  721. auto* client = ClientConnection::from_client_id(message.client_id());
  722. if (!client) {
  723. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  724. return;
  725. }
  726. auto it = client->m_windows.find(message.window_id());
  727. if (it == client->m_windows.end()) {
  728. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  729. return;
  730. }
  731. auto& window = *(*it).value;
  732. WindowManager::the().minimize_windows(window, message.minimized());
  733. }
  734. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  735. {
  736. return make<Messages::WindowServer::GreetResponse>(Screen::the().rect(), Gfx::current_system_theme_buffer());
  737. }
  738. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  739. {
  740. // Because the Taskbar (which should be the only user of this API) does not own the
  741. // window or the client id, there is a possibility that it may send this message for
  742. // a window or client that may have been destroyed already. This is not an error,
  743. // and we should not call did_misbehave() for either.
  744. auto* client = ClientConnection::from_client_id(message.client_id());
  745. if (!client)
  746. return;
  747. auto it = client->m_windows.find(message.window_id());
  748. if (it == client->m_windows.end())
  749. return;
  750. auto& window = *(*it).value;
  751. window.set_taskbar_rect(message.rect());
  752. }
  753. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  754. {
  755. auto& wm = WindowManager::the();
  756. if (wm.dnd_client())
  757. return make<Messages::WindowServer::StartDragResponse>(false);
  758. wm.start_dnd_drag(*this, message.text(), message.drag_bitmap().bitmap(), Core::MimeData::construct(message.mime_data()));
  759. return make<Messages::WindowServer::StartDragResponse>(true);
  760. }
  761. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  762. {
  763. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  764. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  765. }
  766. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  767. {
  768. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  769. auto name = wm_config->read_entry("Theme", "Name");
  770. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  771. }
  772. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  773. {
  774. auto it = m_windows.find(message.window_id());
  775. if (it == m_windows.end()) {
  776. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  777. return {};
  778. }
  779. auto& window = *it->value;
  780. window.set_base_size(message.base_size());
  781. window.set_size_increment(message.size_increment());
  782. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  783. }
  784. OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowResizeAspectRatio& message)
  785. {
  786. auto it = m_windows.find(message.window_id());
  787. if (it == m_windows.end()) {
  788. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  789. return {};
  790. }
  791. auto& window = *it->value;
  792. window.set_resize_aspect_ratio(message.resize_aspect_ratio());
  793. return make<Messages::WindowServer::SetWindowResizeAspectRatioResponse>();
  794. }
  795. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  796. {
  797. if (m_has_display_link)
  798. return;
  799. m_has_display_link = true;
  800. Compositor::the().increment_display_link_count({});
  801. }
  802. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  803. {
  804. if (!m_has_display_link)
  805. return;
  806. m_has_display_link = false;
  807. Compositor::the().decrement_display_link_count({});
  808. }
  809. void ClientConnection::notify_display_link(Badge<Compositor>)
  810. {
  811. if (!m_has_display_link)
  812. return;
  813. post_message(Messages::WindowClient::DisplayLinkNotification());
  814. }
  815. void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
  816. {
  817. auto it = m_windows.find(message.window_id());
  818. if (it == m_windows.end()) {
  819. did_misbehave("SetWindowProgress with bad window ID");
  820. return;
  821. }
  822. it->value->set_progress(message.progress());
  823. }
  824. void ClientConnection::handle(const Messages::WindowServer::RefreshSystemTheme&)
  825. {
  826. // Post the client an UpdateSystemTheme message to refresh its theme.
  827. post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  828. }
  829. void ClientConnection::handle(const Messages::WindowServer::Pong&)
  830. {
  831. m_ping_timer = nullptr;
  832. set_unresponsive(false);
  833. }
  834. OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> ClientConnection::handle(const Messages::WindowServer::GetGlobalCursorPosition&)
  835. {
  836. return make<Messages::WindowServer::GetGlobalCursorPositionResponse>(Screen::the().cursor_location());
  837. }
  838. OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::SetMouseAcceleration& message)
  839. {
  840. double factor = message.factor();
  841. if (factor < mouse_accel_min || factor > mouse_accel_max) {
  842. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  843. return {};
  844. }
  845. WindowManager::the().set_acceleration_factor(factor);
  846. return make<Messages::WindowServer::SetMouseAccelerationResponse>();
  847. }
  848. OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::GetMouseAcceleration&)
  849. {
  850. return make<Messages::WindowServer::GetMouseAccelerationResponse>(Screen::the().acceleration_factor());
  851. }
  852. OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetScrollStepSize& message)
  853. {
  854. if (message.step_size() < scroll_step_size_min) {
  855. did_misbehave("SetScrollStepSize with bad scroll step size");
  856. return {};
  857. }
  858. WindowManager::the().set_scroll_step_size(message.step_size());
  859. return make<Messages::WindowServer::SetScrollStepSizeResponse>();
  860. }
  861. OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetScrollStepSize&)
  862. {
  863. return make<Messages::WindowServer::GetScrollStepSizeResponse>(Screen::the().scroll_step_size());
  864. }
  865. OwnPtr<Messages::WindowServer::SetDoubleClickSpeedResponse> ClientConnection::handle(const Messages::WindowServer::SetDoubleClickSpeed& message)
  866. {
  867. if (message.speed() < double_click_speed_min || message.speed() > double_click_speed_max) {
  868. did_misbehave("SetDoubleClickSpeed with bad speed");
  869. return {};
  870. }
  871. WindowManager::the().set_double_click_speed(message.speed());
  872. return make<Messages::WindowServer::SetDoubleClickSpeedResponse>();
  873. }
  874. OwnPtr<Messages::WindowServer::GetDoubleClickSpeedResponse> ClientConnection::handle(const Messages::WindowServer::GetDoubleClickSpeed&)
  875. {
  876. return make<Messages::WindowServer::GetDoubleClickSpeedResponse>(WindowManager::the().double_click_speed());
  877. }
  878. void ClientConnection::set_unresponsive(bool unresponsive)
  879. {
  880. if (m_unresponsive == unresponsive)
  881. return;
  882. m_unresponsive = unresponsive;
  883. for (auto& it : m_windows) {
  884. auto& window = *it.value;
  885. window.invalidate();
  886. if (unresponsive) {
  887. window.set_cursor_override(WindowManager::the().wait_cursor());
  888. } else {
  889. window.remove_cursor_override();
  890. }
  891. }
  892. Compositor::the().invalidate_cursor();
  893. }
  894. void ClientConnection::may_have_become_unresponsive()
  895. {
  896. post_message(Messages::WindowClient::Ping());
  897. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  898. set_unresponsive(true);
  899. });
  900. m_ping_timer->start();
  901. }
  902. void ClientConnection::did_become_responsive()
  903. {
  904. set_unresponsive(false);
  905. }
  906. OwnPtr<Messages::WindowServer::GetScreenBitmapResponse> ClientConnection::handle(const Messages::WindowServer::GetScreenBitmap&)
  907. {
  908. auto& bitmap = Compositor::the().front_bitmap_for_screenshot({});
  909. return make<Messages::WindowServer::GetScreenBitmapResponse>(bitmap.to_shareable_bitmap());
  910. }
  911. }