ClientConnection.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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/MenuBar.h>
  35. #include <WindowServer/MenuItem.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::MenuApplet)
  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. auto& menubar = *(*it).value;
  109. MenuManager::the().close_menubar(menubar);
  110. m_menubars.remove(it);
  111. return make<Messages::WindowServer::DestroyMenubarResponse>();
  112. }
  113. OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
  114. {
  115. int menu_id = m_next_menu_id++;
  116. auto menu = Menu::construct(this, menu_id, message.menu_title());
  117. m_menus.set(menu_id, move(menu));
  118. return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
  119. }
  120. OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
  121. {
  122. int menu_id = message.menu_id();
  123. auto it = m_menus.find(menu_id);
  124. if (it == m_menus.end()) {
  125. did_misbehave("DestroyMenu: Bad menu ID");
  126. return {};
  127. }
  128. auto& menu = *(*it).value;
  129. menu.close();
  130. m_menus.remove(it);
  131. remove_child(menu);
  132. return make<Messages::WindowServer::DestroyMenuResponse>();
  133. }
  134. OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetApplicationMenubar& message)
  135. {
  136. int menubar_id = message.menubar_id();
  137. auto it = m_menubars.find(menubar_id);
  138. if (it == m_menubars.end()) {
  139. did_misbehave("SetApplicationMenubar: Bad menubar ID");
  140. return {};
  141. }
  142. auto& menubar = *(*it).value;
  143. m_app_menubar = menubar.make_weak_ptr();
  144. WindowManager::the().notify_client_changed_app_menubar(*this);
  145. return make<Messages::WindowServer::SetApplicationMenubarResponse>();
  146. }
  147. OwnPtr<Messages::WindowServer::SetWindowMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowMenubar& message)
  148. {
  149. RefPtr<Window> window;
  150. {
  151. auto it = m_windows.find(message.window_id());
  152. if (it == m_windows.end()) {
  153. did_misbehave("SetWindowMenubar: Bad window ID");
  154. return {};
  155. }
  156. window = it->value;
  157. }
  158. RefPtr<MenuBar> menubar;
  159. if (message.menubar_id() != -1) {
  160. auto it = m_menubars.find(message.menubar_id());
  161. if (it == m_menubars.end()) {
  162. did_misbehave("SetWindowMenubar: Bad menubar ID");
  163. return {};
  164. }
  165. menubar = *(*it).value;
  166. }
  167. window->set_menubar(menubar);
  168. return make<Messages::WindowServer::SetWindowMenubarResponse>();
  169. }
  170. OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
  171. {
  172. int menubar_id = message.menubar_id();
  173. int menu_id = message.menu_id();
  174. auto it = m_menubars.find(menubar_id);
  175. auto jt = m_menus.find(menu_id);
  176. if (it == m_menubars.end()) {
  177. did_misbehave("AddMenuToMenubar: Bad menubar ID");
  178. return {};
  179. }
  180. if (jt == m_menus.end()) {
  181. did_misbehave("AddMenuToMenubar: Bad menu ID");
  182. return {};
  183. }
  184. auto& menubar = *(*it).value;
  185. auto& menu = *(*jt).value;
  186. menubar.add_menu(menu);
  187. return make<Messages::WindowServer::AddMenuToMenubarResponse>();
  188. }
  189. OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
  190. {
  191. int menu_id = message.menu_id();
  192. unsigned identifier = message.identifier();
  193. auto it = m_menus.find(menu_id);
  194. if (it == m_menus.end()) {
  195. dbgln("AddMenuItem: Bad menu ID: {}", menu_id);
  196. return {};
  197. }
  198. auto& menu = *(*it).value;
  199. auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
  200. if (message.is_default())
  201. menu_item->set_default(true);
  202. menu_item->set_icon(message.icon().bitmap());
  203. menu_item->set_submenu_id(message.submenu_id());
  204. menu_item->set_exclusive(message.exclusive());
  205. menu.add_item(move(menu_item));
  206. return make<Messages::WindowServer::AddMenuItemResponse>();
  207. }
  208. OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
  209. {
  210. int menu_id = message.menu_id();
  211. auto position = message.screen_position();
  212. auto it = m_menus.find(menu_id);
  213. if (it == m_menus.end()) {
  214. did_misbehave("PopupMenu: Bad menu ID");
  215. return {};
  216. }
  217. auto& menu = *(*it).value;
  218. menu.popup(position);
  219. return make<Messages::WindowServer::PopupMenuResponse>();
  220. }
  221. OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
  222. {
  223. int menu_id = message.menu_id();
  224. auto it = m_menus.find(menu_id);
  225. if (it == m_menus.end()) {
  226. did_misbehave("DismissMenu: Bad menu ID");
  227. return {};
  228. }
  229. auto& menu = *(*it).value;
  230. menu.close();
  231. return make<Messages::WindowServer::DismissMenuResponse>();
  232. }
  233. OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
  234. {
  235. int menu_id = message.menu_id();
  236. auto it = m_menus.find(menu_id);
  237. if (it == m_menus.end()) {
  238. did_misbehave("UpdateMenuItem: Bad menu ID");
  239. return {};
  240. }
  241. auto& menu = *(*it).value;
  242. auto* menu_item = menu.item_with_identifier(message.identifier());
  243. if (!menu_item) {
  244. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  245. return {};
  246. }
  247. menu_item->set_text(message.text());
  248. menu_item->set_shortcut_text(message.shortcut());
  249. menu_item->set_enabled(message.enabled());
  250. menu_item->set_checkable(message.checkable());
  251. menu_item->set_default(message.is_default());
  252. if (message.checkable())
  253. menu_item->set_checked(message.checked());
  254. return make<Messages::WindowServer::UpdateMenuItemResponse>();
  255. }
  256. OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuSeparator& message)
  257. {
  258. int menu_id = message.menu_id();
  259. auto it = m_menus.find(menu_id);
  260. if (it == m_menus.end()) {
  261. did_misbehave("AddMenuSeparator: Bad menu ID");
  262. return {};
  263. }
  264. auto& menu = *(*it).value;
  265. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  266. return make<Messages::WindowServer::AddMenuSeparatorResponse>();
  267. }
  268. OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> ClientConnection::handle(const Messages::WindowServer::MoveWindowToFront& message)
  269. {
  270. auto it = m_windows.find(message.window_id());
  271. if (it == m_windows.end()) {
  272. did_misbehave("MoveWindowToFront: Bad window ID");
  273. return {};
  274. }
  275. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  276. return make<Messages::WindowServer::MoveWindowToFrontResponse>();
  277. }
  278. OwnPtr<Messages::WindowServer::SetFullscreenResponse> ClientConnection::handle(const Messages::WindowServer::SetFullscreen& message)
  279. {
  280. auto it = m_windows.find(message.window_id());
  281. if (it == m_windows.end()) {
  282. did_misbehave("SetFullscreen: Bad window ID");
  283. return {};
  284. }
  285. it->value->set_fullscreen(message.fullscreen());
  286. return make<Messages::WindowServer::SetFullscreenResponse>();
  287. }
  288. OwnPtr<Messages::WindowServer::SetFramelessResponse> ClientConnection::handle(const Messages::WindowServer::SetFrameless& message)
  289. {
  290. auto it = m_windows.find(message.window_id());
  291. if (it == m_windows.end()) {
  292. did_misbehave("SetFrameless: Bad window ID");
  293. return {};
  294. }
  295. it->value->set_frameless(message.frameless());
  296. WindowManager::the().tell_wm_listeners_window_state_changed(*it->value);
  297. return make<Messages::WindowServer::SetFramelessResponse>();
  298. }
  299. OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOpacity& message)
  300. {
  301. auto it = m_windows.find(message.window_id());
  302. if (it == m_windows.end()) {
  303. did_misbehave("SetWindowOpacity: Bad window ID");
  304. return {};
  305. }
  306. it->value->set_opacity(message.opacity());
  307. return make<Messages::WindowServer::SetWindowOpacityResponse>();
  308. }
  309. void ClientConnection::handle(const Messages::WindowServer::AsyncSetWallpaper& message)
  310. {
  311. Compositor::the().set_wallpaper(message.path(), [&](bool success) {
  312. post_message(Messages::WindowClient::AsyncSetWallpaperFinished(success));
  313. });
  314. }
  315. OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> ClientConnection::handle(const Messages::WindowServer::SetBackgroundColor& message)
  316. {
  317. Compositor::the().set_background_color(message.background_color());
  318. return make<Messages::WindowServer::SetBackgroundColorResponse>();
  319. }
  320. OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> ClientConnection::handle(const Messages::WindowServer::SetWallpaperMode& message)
  321. {
  322. Compositor::the().set_wallpaper_mode(message.mode());
  323. return make<Messages::WindowServer::SetWallpaperModeResponse>();
  324. }
  325. OwnPtr<Messages::WindowServer::GetWallpaperResponse> ClientConnection::handle(const Messages::WindowServer::GetWallpaper&)
  326. {
  327. return make<Messages::WindowServer::GetWallpaperResponse>(Compositor::the().wallpaper_path());
  328. }
  329. OwnPtr<Messages::WindowServer::SetResolutionResponse> ClientConnection::handle(const Messages::WindowServer::SetResolution& message)
  330. {
  331. 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());
  332. }
  333. OwnPtr<Messages::WindowServer::SetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowTitle& message)
  334. {
  335. auto it = m_windows.find(message.window_id());
  336. if (it == m_windows.end()) {
  337. did_misbehave("SetWindowTitle: Bad window ID");
  338. return {};
  339. }
  340. it->value->set_title(message.title());
  341. return make<Messages::WindowServer::SetWindowTitleResponse>();
  342. }
  343. OwnPtr<Messages::WindowServer::GetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowTitle& message)
  344. {
  345. auto it = m_windows.find(message.window_id());
  346. if (it == m_windows.end()) {
  347. did_misbehave("GetWindowTitle: Bad window ID");
  348. return {};
  349. }
  350. return make<Messages::WindowServer::GetWindowTitleResponse>(it->value->title());
  351. }
  352. OwnPtr<Messages::WindowServer::IsMaximizedResponse> ClientConnection::handle(const Messages::WindowServer::IsMaximized& message)
  353. {
  354. auto it = m_windows.find(message.window_id());
  355. if (it == m_windows.end()) {
  356. did_misbehave("IsMaximized: Bad window ID");
  357. return {};
  358. }
  359. return make<Messages::WindowServer::IsMaximizedResponse>(it->value->is_maximized());
  360. }
  361. OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
  362. {
  363. auto it = m_windows.find(message.window_id());
  364. if (it == m_windows.end()) {
  365. did_misbehave("SetWindowIconBitmap: Bad window ID");
  366. return {};
  367. }
  368. auto& window = *(*it).value;
  369. if (message.icon().is_valid()) {
  370. window.set_icon(*message.icon().bitmap());
  371. } else {
  372. window.set_default_icon();
  373. }
  374. window.frame().invalidate_title_bar();
  375. WindowManager::the().tell_wm_listeners_window_icon_changed(window);
  376. return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
  377. }
  378. OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
  379. {
  380. int window_id = message.window_id();
  381. auto it = m_windows.find(window_id);
  382. if (it == m_windows.end()) {
  383. did_misbehave("SetWindowRect: Bad window ID");
  384. return {};
  385. }
  386. auto& window = *(*it).value;
  387. if (window.is_fullscreen()) {
  388. dbgln("ClientConnection: Ignoring SetWindowRect request for fullscreen window");
  389. return {};
  390. }
  391. if (message.rect().location() != window.rect().location()) {
  392. window.set_default_positioned(false);
  393. }
  394. auto rect = message.rect();
  395. window.apply_minimum_size(rect);
  396. window.set_rect(rect);
  397. window.nudge_into_desktop();
  398. window.request_update(window.rect());
  399. return make<Messages::WindowServer::SetWindowRectResponse>(window.rect());
  400. }
  401. OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& message)
  402. {
  403. int window_id = message.window_id();
  404. auto it = m_windows.find(window_id);
  405. if (it == m_windows.end()) {
  406. did_misbehave("GetWindowRect: Bad window ID");
  407. return {};
  408. }
  409. return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
  410. }
  411. OwnPtr<Messages::WindowServer::SetWindowMinimumSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowMinimumSize& message)
  412. {
  413. int window_id = message.window_id();
  414. auto it = m_windows.find(window_id);
  415. if (it == m_windows.end()) {
  416. did_misbehave("SetWindowMinimumSize: Bad window ID");
  417. return {};
  418. }
  419. auto& window = *(*it).value;
  420. if (window.is_fullscreen()) {
  421. dbgln("ClientConnection: Ignoring SetWindowMinimumSize request for fullscreen window");
  422. return {};
  423. }
  424. window.set_minimum_size(message.size());
  425. if (window.width() < window.minimum_size().width() || window.height() < window.minimum_size().height()) {
  426. // New minimum size is larger than the current window size, resize accordingly.
  427. auto new_rect = window.rect();
  428. bool did_size_clamp = window.apply_minimum_size(new_rect);
  429. window.set_rect(new_rect);
  430. window.nudge_into_desktop();
  431. window.request_update(window.rect());
  432. if (did_size_clamp)
  433. window.refresh_client_size();
  434. }
  435. return make<Messages::WindowServer::SetWindowMinimumSizeResponse>();
  436. }
  437. OwnPtr<Messages::WindowServer::GetWindowMinimumSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowMinimumSize& message)
  438. {
  439. int window_id = message.window_id();
  440. auto it = m_windows.find(window_id);
  441. if (it == m_windows.end()) {
  442. did_misbehave("GetWindowMinimumSize: Bad window ID");
  443. return {};
  444. }
  445. return make<Messages::WindowServer::GetWindowMinimumSizeResponse>(it->value->minimum_size());
  446. }
  447. OwnPtr<Messages::WindowServer::GetWindowRectInMenubarResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRectInMenubar& message)
  448. {
  449. int window_id = message.window_id();
  450. auto it = m_windows.find(window_id);
  451. if (it == m_windows.end()) {
  452. did_misbehave("GetWindowRectInMenubar: Bad window ID");
  453. return {};
  454. }
  455. return make<Messages::WindowServer::GetWindowRectInMenubarResponse>(it->value->rect_in_menubar());
  456. }
  457. Window* ClientConnection::window_from_id(i32 window_id)
  458. {
  459. auto it = m_windows.find(window_id);
  460. if (it == m_windows.end())
  461. return nullptr;
  462. return it->value.ptr();
  463. }
  464. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  465. {
  466. Window* parent_window = nullptr;
  467. if (message.parent_window_id()) {
  468. parent_window = window_from_id(message.parent_window_id());
  469. if (!parent_window) {
  470. did_misbehave("CreateWindow with bad parent_window_id");
  471. return {};
  472. }
  473. }
  474. int window_id = m_next_window_id++;
  475. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.frameless(), message.resizable(), message.fullscreen(), message.accessory(), parent_window);
  476. window->set_has_alpha_channel(message.has_alpha_channel());
  477. window->set_title(message.title());
  478. if (!message.fullscreen()) {
  479. auto rect = message.rect();
  480. if (message.auto_position() && window->is_movable()) {
  481. rect = { WindowManager::the().get_recommended_window_position({ 100, 100 }), message.rect().size() };
  482. window->set_default_positioned(true);
  483. }
  484. window->set_minimum_size(message.minimum_size());
  485. bool did_size_clamp = window->apply_minimum_size(rect);
  486. window->set_rect(rect);
  487. window->nudge_into_desktop();
  488. if (did_size_clamp)
  489. window->refresh_client_size();
  490. }
  491. if (window->type() == WindowType::Desktop) {
  492. window->set_rect(WindowManager::the().desktop_rect());
  493. window->recalculate_rect();
  494. }
  495. window->set_opacity(message.opacity());
  496. window->set_alpha_hit_threshold(message.alpha_hit_threshold());
  497. window->set_size_increment(message.size_increment());
  498. window->set_base_size(message.base_size());
  499. window->set_resize_aspect_ratio(message.resize_aspect_ratio());
  500. window->invalidate(true, true);
  501. if (window->type() == WindowType::MenuApplet)
  502. AppletManager::the().add_applet(*window);
  503. m_windows.set(window_id, move(window));
  504. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  505. }
  506. void ClientConnection::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  507. {
  508. for (auto& child_window : window.child_windows()) {
  509. if (!child_window)
  510. continue;
  511. VERIFY(child_window->window_id() != window.window_id());
  512. destroy_window(*child_window, destroyed_window_ids);
  513. }
  514. for (auto& accessory_window : window.accessory_windows()) {
  515. if (!accessory_window)
  516. continue;
  517. VERIFY(accessory_window->window_id() != window.window_id());
  518. destroy_window(*accessory_window, destroyed_window_ids);
  519. }
  520. destroyed_window_ids.append(window.window_id());
  521. if (window.type() == WindowType::MenuApplet)
  522. AppletManager::the().remove_applet(window);
  523. window.destroy();
  524. remove_child(window);
  525. m_windows.remove(window.window_id());
  526. }
  527. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  528. {
  529. auto it = m_windows.find(message.window_id());
  530. if (it == m_windows.end()) {
  531. did_misbehave("DestroyWindow: Bad window ID");
  532. return {};
  533. }
  534. auto& window = *(*it).value;
  535. Vector<i32> destroyed_window_ids;
  536. destroy_window(window, destroyed_window_ids);
  537. return make<Messages::WindowServer::DestroyWindowResponse>(destroyed_window_ids);
  538. }
  539. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  540. {
  541. auto rect_set = window.take_pending_paint_rects();
  542. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  543. return;
  544. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  545. }
  546. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  547. {
  548. auto it = m_windows.find(message.window_id());
  549. if (it == m_windows.end()) {
  550. did_misbehave("InvalidateRect: Bad window ID");
  551. return;
  552. }
  553. auto& window = *(*it).value;
  554. for (size_t i = 0; i < message.rects().size(); ++i)
  555. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  556. }
  557. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  558. {
  559. int window_id = message.window_id();
  560. auto it = m_windows.find(window_id);
  561. if (it == m_windows.end()) {
  562. did_misbehave("DidFinishPainting: Bad window ID");
  563. return;
  564. }
  565. auto& window = *(*it).value;
  566. for (auto& rect : message.rects())
  567. window.invalidate(rect);
  568. if (window.has_alpha_channel() && window.alpha_hit_threshold() > 0.0)
  569. WindowManager::the().reevaluate_hovered_window(&window);
  570. WindowSwitcher::the().refresh_if_needed();
  571. }
  572. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  573. {
  574. int window_id = message.window_id();
  575. auto it = m_windows.find(window_id);
  576. if (it == m_windows.end()) {
  577. did_misbehave("SetWindowBackingStore: Bad window ID");
  578. return {};
  579. }
  580. auto& window = *(*it).value;
  581. if (window.last_backing_store() && window.last_backing_store_serial() == message.serial()) {
  582. window.swap_backing_stores();
  583. } else {
  584. // FIXME: Plumb scale factor here eventually.
  585. auto backing_store = Gfx::Bitmap::create_with_anon_fd(
  586. message.has_alpha_channel() ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888,
  587. message.anon_file().take_fd(),
  588. message.size(),
  589. 1,
  590. {},
  591. Gfx::Bitmap::ShouldCloseAnonymousFile::Yes);
  592. window.set_backing_store(move(backing_store), message.serial());
  593. }
  594. if (message.flush_immediately())
  595. window.invalidate(false);
  596. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  597. }
  598. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  599. {
  600. int window_id = message.window_id();
  601. auto it = m_windows.find(window_id);
  602. if (it == m_windows.end()) {
  603. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  604. return {};
  605. }
  606. it->value->set_global_cursor_tracking_enabled(message.enabled());
  607. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  608. }
  609. OwnPtr<Messages::WindowServer::SetWindowCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCursor& message)
  610. {
  611. auto it = m_windows.find(message.window_id());
  612. if (it == m_windows.end()) {
  613. did_misbehave("SetWindowCursor: Bad window ID");
  614. return {};
  615. }
  616. auto& window = *(*it).value;
  617. if (message.cursor_type() < 0 || message.cursor_type() >= (i32)Gfx::StandardCursor::__Count) {
  618. did_misbehave("SetWindowCursor: Bad cursor type");
  619. return {};
  620. }
  621. window.set_cursor(Cursor::create((Gfx::StandardCursor)message.cursor_type()));
  622. if (&window == WindowManager::the().hovered_window())
  623. Compositor::the().invalidate_cursor();
  624. return make<Messages::WindowServer::SetWindowCursorResponse>();
  625. }
  626. OwnPtr<Messages::WindowServer::SetWindowCustomCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomCursor& message)
  627. {
  628. auto it = m_windows.find(message.window_id());
  629. if (it == m_windows.end()) {
  630. did_misbehave("SetWindowCustomCursor: Bad window ID");
  631. return {};
  632. }
  633. auto& window = *(*it).value;
  634. if (!message.cursor().is_valid()) {
  635. did_misbehave("SetWindowCustomCursor: Bad cursor");
  636. return {};
  637. }
  638. window.set_cursor(Cursor::create(*message.cursor().bitmap()));
  639. Compositor::the().invalidate_cursor();
  640. return make<Messages::WindowServer::SetWindowCustomCursorResponse>();
  641. }
  642. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  643. {
  644. auto it = m_windows.find(message.window_id());
  645. if (it == m_windows.end()) {
  646. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  647. return {};
  648. }
  649. it->value->set_has_alpha_channel(message.has_alpha_channel());
  650. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  651. }
  652. OwnPtr<Messages::WindowServer::SetWindowAlphaHitThresholdResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowAlphaHitThreshold& message)
  653. {
  654. auto it = m_windows.find(message.window_id());
  655. if (it == m_windows.end()) {
  656. did_misbehave("SetWindowAlphaHitThreshold: Bad window ID");
  657. return {};
  658. }
  659. it->value->set_alpha_hit_threshold(message.threshold());
  660. return make<Messages::WindowServer::SetWindowAlphaHitThresholdResponse>();
  661. }
  662. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  663. {
  664. auto* client = ClientConnection::from_client_id(message.client_id());
  665. if (!client) {
  666. did_misbehave("WM_SetActiveWindow: Bad client ID");
  667. return;
  668. }
  669. auto it = client->m_windows.find(message.window_id());
  670. if (it == client->m_windows.end()) {
  671. did_misbehave("WM_SetActiveWindow: Bad window ID");
  672. return;
  673. }
  674. auto& window = *(*it).value;
  675. WindowManager::the().minimize_windows(window, false);
  676. WindowManager::the().move_to_front_and_make_active(window);
  677. }
  678. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  679. {
  680. auto* client = ClientConnection::from_client_id(message.client_id());
  681. if (!client) {
  682. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  683. return;
  684. }
  685. auto it = client->m_windows.find(message.window_id());
  686. if (it == client->m_windows.end()) {
  687. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  688. return;
  689. }
  690. auto& window = *(*it).value;
  691. if (auto* modal_window = window.blocking_modal_window()) {
  692. modal_window->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  693. } else {
  694. window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  695. }
  696. }
  697. void ClientConnection::handle(const Messages::WindowServer::StartWindowResize& request)
  698. {
  699. auto it = m_windows.find(request.window_id());
  700. if (it == m_windows.end()) {
  701. did_misbehave("WM_StartWindowResize: Bad window ID");
  702. return;
  703. }
  704. auto& window = *(*it).value;
  705. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  706. // Maybe the client should be allowed to specify what initiated this request?
  707. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  708. }
  709. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  710. {
  711. auto* client = ClientConnection::from_client_id(request.client_id());
  712. if (!client) {
  713. did_misbehave("WM_StartWindowResize: Bad client ID");
  714. return;
  715. }
  716. auto it = client->m_windows.find(request.window_id());
  717. if (it == client->m_windows.end()) {
  718. did_misbehave("WM_StartWindowResize: Bad window ID");
  719. return;
  720. }
  721. auto& window = *(*it).value;
  722. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  723. // Maybe the client should be allowed to specify what initiated this request?
  724. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  725. }
  726. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  727. {
  728. auto* client = ClientConnection::from_client_id(message.client_id());
  729. if (!client) {
  730. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  731. return;
  732. }
  733. auto it = client->m_windows.find(message.window_id());
  734. if (it == client->m_windows.end()) {
  735. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  736. return;
  737. }
  738. auto& window = *(*it).value;
  739. WindowManager::the().minimize_windows(window, message.minimized());
  740. }
  741. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  742. {
  743. return make<Messages::WindowServer::GreetResponse>(Screen::the().rect(), Gfx::current_system_theme_buffer());
  744. }
  745. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  746. {
  747. // Because the Taskbar (which should be the only user of this API) does not own the
  748. // window or the client id, there is a possibility that it may send this message for
  749. // a window or client that may have been destroyed already. This is not an error,
  750. // and we should not call did_misbehave() for either.
  751. auto* client = ClientConnection::from_client_id(message.client_id());
  752. if (!client)
  753. return;
  754. auto it = client->m_windows.find(message.window_id());
  755. if (it == client->m_windows.end())
  756. return;
  757. auto& window = *(*it).value;
  758. window.set_taskbar_rect(message.rect());
  759. }
  760. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  761. {
  762. auto& wm = WindowManager::the();
  763. if (wm.dnd_client())
  764. return make<Messages::WindowServer::StartDragResponse>(false);
  765. wm.start_dnd_drag(*this, message.text(), message.drag_bitmap().bitmap(), Core::MimeData::construct(message.mime_data()));
  766. return make<Messages::WindowServer::StartDragResponse>(true);
  767. }
  768. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  769. {
  770. auto it = m_menus.find(message.menu_id());
  771. if (it == m_menus.end()) {
  772. did_misbehave("SetSystemMenu called with invalid menu ID");
  773. return {};
  774. }
  775. auto& menu = it->value;
  776. MenuManager::the().set_system_menu(menu);
  777. return make<Messages::WindowServer::SetSystemMenuResponse>();
  778. }
  779. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  780. {
  781. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  782. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  783. }
  784. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  785. {
  786. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  787. auto name = wm_config->read_entry("Theme", "Name");
  788. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  789. }
  790. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  791. {
  792. auto it = m_windows.find(message.window_id());
  793. if (it == m_windows.end()) {
  794. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  795. return {};
  796. }
  797. auto& window = *it->value;
  798. window.set_base_size(message.base_size());
  799. window.set_size_increment(message.size_increment());
  800. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  801. }
  802. OwnPtr<Messages::WindowServer::SetWindowResizeAspectRatioResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowResizeAspectRatio& message)
  803. {
  804. auto it = m_windows.find(message.window_id());
  805. if (it == m_windows.end()) {
  806. did_misbehave("SetWindowResizeAspectRatioResponse: Bad window ID");
  807. return {};
  808. }
  809. auto& window = *it->value;
  810. window.set_resize_aspect_ratio(message.resize_aspect_ratio());
  811. return make<Messages::WindowServer::SetWindowResizeAspectRatioResponse>();
  812. }
  813. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  814. {
  815. if (m_has_display_link)
  816. return;
  817. m_has_display_link = true;
  818. Compositor::the().increment_display_link_count({});
  819. }
  820. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  821. {
  822. if (!m_has_display_link)
  823. return;
  824. m_has_display_link = false;
  825. Compositor::the().decrement_display_link_count({});
  826. }
  827. void ClientConnection::notify_display_link(Badge<Compositor>)
  828. {
  829. if (!m_has_display_link)
  830. return;
  831. post_message(Messages::WindowClient::DisplayLinkNotification());
  832. }
  833. void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
  834. {
  835. auto it = m_windows.find(message.window_id());
  836. if (it == m_windows.end()) {
  837. did_misbehave("SetWindowProgress with bad window ID");
  838. return;
  839. }
  840. it->value->set_progress(message.progress());
  841. }
  842. void ClientConnection::handle(const Messages::WindowServer::RefreshSystemTheme&)
  843. {
  844. // Post the client an UpdateSystemTheme message to refresh its theme.
  845. post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  846. }
  847. void ClientConnection::handle(const Messages::WindowServer::Pong&)
  848. {
  849. m_ping_timer = nullptr;
  850. set_unresponsive(false);
  851. }
  852. OwnPtr<Messages::WindowServer::GetGlobalCursorPositionResponse> ClientConnection::handle(const Messages::WindowServer::GetGlobalCursorPosition&)
  853. {
  854. return make<Messages::WindowServer::GetGlobalCursorPositionResponse>(Screen::the().cursor_location());
  855. }
  856. OwnPtr<Messages::WindowServer::SetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::SetMouseAcceleration& message)
  857. {
  858. if (message.factor() < mouse_accel_min || message.factor() > mouse_accel_max) {
  859. did_misbehave("SetMouseAcceleration with bad acceleration factor");
  860. return {};
  861. }
  862. WindowManager::the().set_acceleration_factor(message.factor());
  863. return make<Messages::WindowServer::SetMouseAccelerationResponse>();
  864. }
  865. OwnPtr<Messages::WindowServer::GetMouseAccelerationResponse> ClientConnection::handle(const Messages::WindowServer::GetMouseAcceleration&)
  866. {
  867. return make<Messages::WindowServer::GetMouseAccelerationResponse>(Screen::the().acceleration_factor());
  868. }
  869. OwnPtr<Messages::WindowServer::SetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::SetScrollStepSize& message)
  870. {
  871. if (message.step_size() < scroll_step_size_min) {
  872. did_misbehave("SetScrollStepSize with bad scroll step size");
  873. return {};
  874. }
  875. WindowManager::the().set_scroll_step_size(message.step_size());
  876. return make<Messages::WindowServer::SetScrollStepSizeResponse>();
  877. }
  878. OwnPtr<Messages::WindowServer::GetScrollStepSizeResponse> ClientConnection::handle(const Messages::WindowServer::GetScrollStepSize&)
  879. {
  880. return make<Messages::WindowServer::GetScrollStepSizeResponse>(Screen::the().scroll_step_size());
  881. }
  882. void ClientConnection::set_unresponsive(bool unresponsive)
  883. {
  884. if (m_unresponsive == unresponsive)
  885. return;
  886. m_unresponsive = unresponsive;
  887. for (auto& it : m_windows) {
  888. auto& window = *it.value;
  889. window.invalidate();
  890. if (unresponsive) {
  891. window.set_cursor_override(WindowManager::the().wait_cursor());
  892. } else {
  893. window.remove_cursor_override();
  894. }
  895. }
  896. Compositor::the().invalidate_cursor();
  897. }
  898. void ClientConnection::may_have_become_unresponsive()
  899. {
  900. post_message(Messages::WindowClient::Ping());
  901. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  902. set_unresponsive(true);
  903. });
  904. m_ping_timer->start();
  905. }
  906. void ClientConnection::did_become_responsive()
  907. {
  908. set_unresponsive(false);
  909. }
  910. }