ClientConnection.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 <AK/SharedBuffer.h>
  28. #include <LibGfx/Bitmap.h>
  29. #include <LibGfx/SystemTheme.h>
  30. #include <WindowServer/AppletManager.h>
  31. #include <WindowServer/ClientConnection.h>
  32. #include <WindowServer/Compositor.h>
  33. #include <WindowServer/EventLoop.h>
  34. #include <WindowServer/Menu.h>
  35. #include <WindowServer/MenuBar.h>
  36. #include <WindowServer/MenuItem.h>
  37. #include <WindowServer/Screen.h>
  38. #include <WindowServer/Window.h>
  39. #include <WindowServer/WindowClientEndpoint.h>
  40. #include <WindowServer/WindowManager.h>
  41. #include <WindowServer/WindowSwitcher.h>
  42. #include <errno.h>
  43. #include <serenity.h>
  44. #include <stdio.h>
  45. #include <unistd.h>
  46. namespace WindowServer {
  47. HashMap<int, NonnullRefPtr<ClientConnection>>* s_connections;
  48. static Gfx::IntRect normalize_window_rect(Gfx::IntRect rect, WindowType window_type)
  49. {
  50. auto min_size = 1;
  51. if (window_type == WindowType::Normal)
  52. min_size = 50;
  53. Gfx::IntRect normalized_rect = { rect.x(), rect.y(), max(rect.width(), min_size), max(rect.height(), min_size) };
  54. return normalized_rect;
  55. }
  56. void ClientConnection::for_each_client(Function<void(ClientConnection&)> callback)
  57. {
  58. if (!s_connections)
  59. return;
  60. for (auto& it : *s_connections) {
  61. callback(*it.value);
  62. }
  63. }
  64. ClientConnection* ClientConnection::from_client_id(int client_id)
  65. {
  66. if (!s_connections)
  67. return nullptr;
  68. auto it = s_connections->find(client_id);
  69. if (it == s_connections->end())
  70. return nullptr;
  71. return (*it).value.ptr();
  72. }
  73. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  74. : IPC::ClientConnection<WindowServerEndpoint>(*this, move(client_socket), client_id)
  75. {
  76. if (!s_connections)
  77. s_connections = new HashMap<int, NonnullRefPtr<ClientConnection>>;
  78. s_connections->set(client_id, *this);
  79. }
  80. ClientConnection::~ClientConnection()
  81. {
  82. if (m_has_display_link)
  83. Compositor::the().decrement_display_link_count({});
  84. MenuManager::the().close_all_menus_from_client({}, *this);
  85. auto windows = move(m_windows);
  86. for (auto& window : windows) {
  87. window.value->detach_client({});
  88. if (window.value->type() == WindowType::MenuApplet)
  89. AppletManager::the().remove_applet(window.value);
  90. }
  91. }
  92. void ClientConnection::die()
  93. {
  94. deferred_invoke([this](auto&) {
  95. s_connections->remove(client_id());
  96. });
  97. }
  98. void ClientConnection::notify_about_new_screen_rect(const Gfx::IntRect& rect)
  99. {
  100. post_message(Messages::WindowClient::ScreenRectChanged(rect));
  101. }
  102. OwnPtr<Messages::WindowServer::CreateMenubarResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenubar&)
  103. {
  104. int menubar_id = m_next_menubar_id++;
  105. auto menubar = make<MenuBar>(*this, menubar_id);
  106. m_menubars.set(menubar_id, move(menubar));
  107. return make<Messages::WindowServer::CreateMenubarResponse>(menubar_id);
  108. }
  109. OwnPtr<Messages::WindowServer::DestroyMenubarResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenubar& message)
  110. {
  111. int menubar_id = message.menubar_id();
  112. auto it = m_menubars.find(menubar_id);
  113. if (it == m_menubars.end()) {
  114. did_misbehave("DestroyMenubar: Bad menubar ID");
  115. return nullptr;
  116. }
  117. auto& menubar = *(*it).value;
  118. MenuManager::the().close_menubar(menubar);
  119. m_menubars.remove(it);
  120. return make<Messages::WindowServer::DestroyMenubarResponse>();
  121. }
  122. OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
  123. {
  124. int menu_id = m_next_menu_id++;
  125. auto menu = Menu::construct(this, menu_id, message.menu_title());
  126. m_menus.set(menu_id, move(menu));
  127. return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
  128. }
  129. OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
  130. {
  131. int menu_id = message.menu_id();
  132. auto it = m_menus.find(menu_id);
  133. if (it == m_menus.end()) {
  134. did_misbehave("DestroyMenu: Bad menu ID");
  135. return nullptr;
  136. }
  137. auto& menu = *(*it).value;
  138. menu.close();
  139. m_menus.remove(it);
  140. remove_child(menu);
  141. return make<Messages::WindowServer::DestroyMenuResponse>();
  142. }
  143. OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetApplicationMenubar& message)
  144. {
  145. int menubar_id = message.menubar_id();
  146. auto it = m_menubars.find(menubar_id);
  147. if (it == m_menubars.end()) {
  148. did_misbehave("SetApplicationMenubar: Bad menubar ID");
  149. return nullptr;
  150. }
  151. auto& menubar = *(*it).value;
  152. m_app_menubar = menubar.make_weak_ptr();
  153. WindowManager::the().notify_client_changed_app_menubar(*this);
  154. return make<Messages::WindowServer::SetApplicationMenubarResponse>();
  155. }
  156. OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
  157. {
  158. int menubar_id = message.menubar_id();
  159. int menu_id = message.menu_id();
  160. auto it = m_menubars.find(menubar_id);
  161. auto jt = m_menus.find(menu_id);
  162. if (it == m_menubars.end()) {
  163. did_misbehave("AddMenuToMenubar: Bad menubar ID");
  164. return nullptr;
  165. }
  166. if (jt == m_menus.end()) {
  167. did_misbehave("AddMenuToMenubar: Bad menu ID");
  168. return nullptr;
  169. }
  170. auto& menubar = *(*it).value;
  171. auto& menu = *(*jt).value;
  172. menubar.add_menu(menu);
  173. return make<Messages::WindowServer::AddMenuToMenubarResponse>();
  174. }
  175. OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
  176. {
  177. int menu_id = message.menu_id();
  178. unsigned identifier = message.identifier();
  179. auto it = m_menus.find(menu_id);
  180. if (it == m_menus.end()) {
  181. dbg() << "AddMenuItem: Bad menu ID: " << menu_id;
  182. return nullptr;
  183. }
  184. auto& menu = *(*it).value;
  185. auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
  186. if (message.is_default())
  187. menu_item->set_default(true);
  188. if (message.icon_buffer_id() != -1) {
  189. auto icon_buffer = SharedBuffer::create_from_shbuf_id(message.icon_buffer_id());
  190. if (!icon_buffer)
  191. return nullptr;
  192. // FIXME: Verify that the icon buffer can accomodate a 16x16 bitmap view.
  193. auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, icon_buffer.release_nonnull(), { 16, 16 });
  194. menu_item->set_icon(shared_icon);
  195. }
  196. menu_item->set_submenu_id(message.submenu_id());
  197. menu_item->set_exclusive(message.exclusive());
  198. menu.add_item(move(menu_item));
  199. return make<Messages::WindowServer::AddMenuItemResponse>();
  200. }
  201. OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
  202. {
  203. int menu_id = message.menu_id();
  204. auto position = message.screen_position();
  205. auto it = m_menus.find(menu_id);
  206. if (it == m_menus.end()) {
  207. did_misbehave("PopupMenu: Bad menu ID");
  208. return nullptr;
  209. }
  210. auto& menu = *(*it).value;
  211. menu.popup(position);
  212. return make<Messages::WindowServer::PopupMenuResponse>();
  213. }
  214. OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
  215. {
  216. int menu_id = message.menu_id();
  217. auto it = m_menus.find(menu_id);
  218. if (it == m_menus.end()) {
  219. did_misbehave("DismissMenu: Bad menu ID");
  220. return nullptr;
  221. }
  222. auto& menu = *(*it).value;
  223. menu.close();
  224. return make<Messages::WindowServer::DismissMenuResponse>();
  225. }
  226. OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
  227. {
  228. int menu_id = message.menu_id();
  229. auto it = m_menus.find(menu_id);
  230. if (it == m_menus.end()) {
  231. did_misbehave("UpdateMenuItem: Bad menu ID");
  232. return nullptr;
  233. }
  234. auto& menu = *(*it).value;
  235. auto* menu_item = menu.item_with_identifier(message.identifier());
  236. if (!menu_item) {
  237. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  238. return nullptr;
  239. }
  240. menu_item->set_text(message.text());
  241. menu_item->set_shortcut_text(message.shortcut());
  242. menu_item->set_enabled(message.enabled());
  243. menu_item->set_checkable(message.checkable());
  244. menu_item->set_default(message.is_default());
  245. if (message.checkable())
  246. menu_item->set_checked(message.checked());
  247. return make<Messages::WindowServer::UpdateMenuItemResponse>();
  248. }
  249. OwnPtr<Messages::WindowServer::AddMenuSeparatorResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuSeparator& message)
  250. {
  251. int menu_id = message.menu_id();
  252. auto it = m_menus.find(menu_id);
  253. if (it == m_menus.end()) {
  254. did_misbehave("AddMenuSeparator: Bad menu ID");
  255. return nullptr;
  256. }
  257. auto& menu = *(*it).value;
  258. menu.add_item(make<MenuItem>(menu, MenuItem::Separator));
  259. return make<Messages::WindowServer::AddMenuSeparatorResponse>();
  260. }
  261. OwnPtr<Messages::WindowServer::MoveWindowToFrontResponse> ClientConnection::handle(const Messages::WindowServer::MoveWindowToFront& message)
  262. {
  263. auto it = m_windows.find(message.window_id());
  264. if (it == m_windows.end()) {
  265. did_misbehave("MoveWindowToFront: Bad window ID");
  266. return nullptr;
  267. }
  268. WindowManager::the().move_to_front_and_make_active(*(*it).value);
  269. return make<Messages::WindowServer::MoveWindowToFrontResponse>();
  270. }
  271. OwnPtr<Messages::WindowServer::SetFullscreenResponse> ClientConnection::handle(const Messages::WindowServer::SetFullscreen& message)
  272. {
  273. auto it = m_windows.find(message.window_id());
  274. if (it == m_windows.end()) {
  275. did_misbehave("SetFullscreen: Bad window ID");
  276. return nullptr;
  277. }
  278. it->value->set_fullscreen(message.fullscreen());
  279. return make<Messages::WindowServer::SetFullscreenResponse>();
  280. }
  281. OwnPtr<Messages::WindowServer::SetWindowOpacityResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOpacity& message)
  282. {
  283. auto it = m_windows.find(message.window_id());
  284. if (it == m_windows.end()) {
  285. did_misbehave("SetWindowOpacity: Bad window ID");
  286. return nullptr;
  287. }
  288. it->value->set_opacity(message.opacity());
  289. return make<Messages::WindowServer::SetWindowOpacityResponse>();
  290. }
  291. void ClientConnection::handle(const Messages::WindowServer::AsyncSetWallpaper& message)
  292. {
  293. Compositor::the().set_wallpaper(message.path(), [&](bool success) {
  294. post_message(Messages::WindowClient::AsyncSetWallpaperFinished(success));
  295. });
  296. }
  297. OwnPtr<Messages::WindowServer::SetBackgroundColorResponse> ClientConnection::handle(const Messages::WindowServer::SetBackgroundColor& message)
  298. {
  299. Compositor::the().set_background_color(message.background_color());
  300. return make<Messages::WindowServer::SetBackgroundColorResponse>();
  301. }
  302. OwnPtr<Messages::WindowServer::SetWallpaperModeResponse> ClientConnection::handle(const Messages::WindowServer::SetWallpaperMode& message)
  303. {
  304. Compositor::the().set_wallpaper_mode(message.mode());
  305. return make<Messages::WindowServer::SetWallpaperModeResponse>();
  306. }
  307. OwnPtr<Messages::WindowServer::GetWallpaperResponse> ClientConnection::handle(const Messages::WindowServer::GetWallpaper&)
  308. {
  309. return make<Messages::WindowServer::GetWallpaperResponse>(Compositor::the().wallpaper_path());
  310. }
  311. OwnPtr<Messages::WindowServer::SetResolutionResponse> ClientConnection::handle(const Messages::WindowServer::SetResolution& message)
  312. {
  313. return make<Messages::WindowServer::SetResolutionResponse>(WindowManager::the().set_resolution(message.resolution().width(), message.resolution().height()), WindowManager::the().resolution());
  314. }
  315. OwnPtr<Messages::WindowServer::SetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowTitle& message)
  316. {
  317. auto it = m_windows.find(message.window_id());
  318. if (it == m_windows.end()) {
  319. did_misbehave("SetWindowTitle: Bad window ID");
  320. return nullptr;
  321. }
  322. it->value->set_title(message.title());
  323. return make<Messages::WindowServer::SetWindowTitleResponse>();
  324. }
  325. OwnPtr<Messages::WindowServer::GetWindowTitleResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowTitle& message)
  326. {
  327. auto it = m_windows.find(message.window_id());
  328. if (it == m_windows.end()) {
  329. did_misbehave("GetWindowTitle: Bad window ID");
  330. return nullptr;
  331. }
  332. return make<Messages::WindowServer::GetWindowTitleResponse>(it->value->title());
  333. }
  334. OwnPtr<Messages::WindowServer::IsMaximizedResponse> ClientConnection::handle(const Messages::WindowServer::IsMaximized& message)
  335. {
  336. auto it = m_windows.find(message.window_id());
  337. if (it == m_windows.end()) {
  338. did_misbehave("IsMaximized: Bad window ID");
  339. return nullptr;
  340. }
  341. return make<Messages::WindowServer::IsMaximizedResponse>(it->value->is_minimized());
  342. }
  343. OwnPtr<Messages::WindowServer::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
  344. {
  345. auto it = m_windows.find(message.window_id());
  346. if (it == m_windows.end()) {
  347. did_misbehave("SetWindowIconBitmap: Bad window ID");
  348. return nullptr;
  349. }
  350. auto& window = *(*it).value;
  351. if (message.icon().is_valid()) {
  352. window.set_icon(*message.icon().bitmap());
  353. } else {
  354. window.set_default_icon();
  355. }
  356. window.frame().invalidate_title_bar();
  357. WindowManager::the().tell_wm_listeners_window_icon_changed(window);
  358. return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
  359. }
  360. OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
  361. {
  362. int window_id = message.window_id();
  363. auto it = m_windows.find(window_id);
  364. if (it == m_windows.end()) {
  365. did_misbehave("SetWindowRect: Bad window ID");
  366. return nullptr;
  367. }
  368. auto& window = *(*it).value;
  369. if (window.is_fullscreen()) {
  370. dbg() << "ClientConnection: Ignoring SetWindowRect request for fullscreen window";
  371. return nullptr;
  372. }
  373. auto normalized_rect = normalize_window_rect(message.rect(), window.type());
  374. window.set_rect(normalized_rect);
  375. window.request_update(normalized_rect);
  376. return make<Messages::WindowServer::SetWindowRectResponse>(normalized_rect);
  377. }
  378. OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& 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("GetWindowRect: Bad window ID");
  384. return nullptr;
  385. }
  386. return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
  387. }
  388. Window* ClientConnection::window_from_id(i32 window_id)
  389. {
  390. auto it = m_windows.find(window_id);
  391. if (it == m_windows.end())
  392. return nullptr;
  393. return it->value.ptr();
  394. }
  395. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  396. {
  397. Window* parent_window = nullptr;
  398. if (message.parent_window_id()) {
  399. parent_window = window_from_id(message.parent_window_id());
  400. if (!parent_window) {
  401. did_misbehave("CreateWindow with bad parent_window_id");
  402. return nullptr;
  403. }
  404. }
  405. int window_id = m_next_window_id++;
  406. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.frameless(), message.resizable(), message.fullscreen(), message.accessory(), parent_window);
  407. window->set_has_alpha_channel(message.has_alpha_channel());
  408. window->set_title(message.title());
  409. if (!message.fullscreen()) {
  410. auto normalized_rect = normalize_window_rect(message.rect(), window->type());
  411. window->set_rect(normalized_rect);
  412. }
  413. if (window->type() == WindowType::Desktop) {
  414. window->set_rect(WindowManager::the().desktop_rect());
  415. window->recalculate_rect();
  416. }
  417. window->set_opacity(message.opacity());
  418. window->set_size_increment(message.size_increment());
  419. window->set_base_size(message.base_size());
  420. window->invalidate();
  421. if (window->type() == WindowType::MenuApplet)
  422. AppletManager::the().add_applet(*window);
  423. m_windows.set(window_id, move(window));
  424. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  425. }
  426. void ClientConnection::destroy_window(Window& window, Vector<i32>& destroyed_window_ids)
  427. {
  428. for (auto& child_window : window.child_windows()) {
  429. if (!child_window)
  430. continue;
  431. ASSERT(child_window->window_id() != window.window_id());
  432. destroy_window(*child_window, destroyed_window_ids);
  433. }
  434. destroyed_window_ids.append(window.window_id());
  435. if (window.type() == WindowType::MenuApplet)
  436. AppletManager::the().remove_applet(window);
  437. window.invalidate();
  438. remove_child(window);
  439. m_windows.remove(window.window_id());
  440. }
  441. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  442. {
  443. auto it = m_windows.find(message.window_id());
  444. if (it == m_windows.end()) {
  445. did_misbehave("DestroyWindow: Bad window ID");
  446. return nullptr;
  447. }
  448. auto& window = *(*it).value;
  449. Vector<i32> destroyed_window_ids;
  450. destroy_window(window, destroyed_window_ids);
  451. return make<Messages::WindowServer::DestroyWindowResponse>(destroyed_window_ids);
  452. }
  453. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  454. {
  455. auto rect_set = window.take_pending_paint_rects();
  456. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  457. return;
  458. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  459. }
  460. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  461. {
  462. auto it = m_windows.find(message.window_id());
  463. if (it == m_windows.end()) {
  464. did_misbehave("InvalidateRect: Bad window ID");
  465. return;
  466. }
  467. auto& window = *(*it).value;
  468. for (size_t i = 0; i < message.rects().size(); ++i)
  469. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  470. }
  471. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  472. {
  473. int window_id = message.window_id();
  474. auto it = m_windows.find(window_id);
  475. if (it == m_windows.end()) {
  476. did_misbehave("DidFinishPainting: Bad window ID");
  477. return;
  478. }
  479. auto& window = *(*it).value;
  480. for (auto& rect : message.rects())
  481. window.invalidate(rect);
  482. WindowSwitcher::the().refresh_if_needed();
  483. }
  484. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  485. {
  486. int window_id = message.window_id();
  487. auto it = m_windows.find(window_id);
  488. if (it == m_windows.end()) {
  489. did_misbehave("SetWindowBackingStore: Bad window ID");
  490. return nullptr;
  491. }
  492. auto& window = *(*it).value;
  493. if (window.last_backing_store() && window.last_backing_store()->shbuf_id() == message.shbuf_id()) {
  494. window.swap_backing_stores();
  495. } else {
  496. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  497. if (!shared_buffer)
  498. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  499. auto backing_store = Gfx::Bitmap::create_with_shared_buffer(
  500. message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
  501. *shared_buffer,
  502. message.size());
  503. window.set_backing_store(move(backing_store));
  504. }
  505. if (message.flush_immediately())
  506. window.invalidate();
  507. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  508. }
  509. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  510. {
  511. int window_id = message.window_id();
  512. auto it = m_windows.find(window_id);
  513. if (it == m_windows.end()) {
  514. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  515. return nullptr;
  516. }
  517. it->value->set_global_cursor_tracking_enabled(message.enabled());
  518. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  519. }
  520. OwnPtr<Messages::WindowServer::SetWindowOverrideCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOverrideCursor& message)
  521. {
  522. auto it = m_windows.find(message.window_id());
  523. if (it == m_windows.end()) {
  524. did_misbehave("SetWindowOverrideCursor: Bad window ID");
  525. return nullptr;
  526. }
  527. auto& window = *(*it).value;
  528. window.set_override_cursor(Cursor::create((StandardCursor)message.cursor_type()));
  529. Compositor::the().invalidate_cursor();
  530. return make<Messages::WindowServer::SetWindowOverrideCursorResponse>();
  531. }
  532. OwnPtr<Messages::WindowServer::SetWindowCustomOverrideCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowCustomOverrideCursor& message)
  533. {
  534. auto it = m_windows.find(message.window_id());
  535. if (it == m_windows.end()) {
  536. did_misbehave("SetWindowCustomOverrideCursor: Bad window ID");
  537. return nullptr;
  538. }
  539. auto& window = *(*it).value;
  540. if (!message.cursor().is_valid()) {
  541. did_misbehave("SetWindowCustomOverrideCursor: Bad cursor");
  542. return nullptr;
  543. }
  544. window.set_override_cursor(Cursor::create(*message.cursor().bitmap()));
  545. Compositor::the().invalidate_cursor();
  546. return make<Messages::WindowServer::SetWindowCustomOverrideCursorResponse>();
  547. }
  548. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  549. {
  550. auto it = m_windows.find(message.window_id());
  551. if (it == m_windows.end()) {
  552. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  553. return nullptr;
  554. }
  555. it->value->set_has_alpha_channel(message.has_alpha_channel());
  556. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  557. }
  558. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  559. {
  560. auto* client = ClientConnection::from_client_id(message.client_id());
  561. if (!client) {
  562. did_misbehave("WM_SetActiveWindow: Bad client ID");
  563. return;
  564. }
  565. auto it = client->m_windows.find(message.window_id());
  566. if (it == client->m_windows.end()) {
  567. did_misbehave("WM_SetActiveWindow: Bad window ID");
  568. return;
  569. }
  570. auto& window = *(*it).value;
  571. WindowManager::the().minimize_windows(window, false);
  572. WindowManager::the().move_to_front_and_make_active(window);
  573. }
  574. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  575. {
  576. auto* client = ClientConnection::from_client_id(message.client_id());
  577. if (!client) {
  578. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  579. return;
  580. }
  581. auto it = client->m_windows.find(message.window_id());
  582. if (it == client->m_windows.end()) {
  583. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  584. return;
  585. }
  586. auto& window = *(*it).value;
  587. if (auto* blocked_by_modal = window.is_blocked_by_modal_window()) {
  588. blocked_by_modal->popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  589. } else {
  590. window.popup_window_menu(message.screen_position(), WindowMenuDefaultAction::BasedOnWindowState);
  591. }
  592. }
  593. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  594. {
  595. auto* client = ClientConnection::from_client_id(request.client_id());
  596. if (!client) {
  597. did_misbehave("WM_StartWindowResize: Bad client ID");
  598. return;
  599. }
  600. auto it = client->m_windows.find(request.window_id());
  601. if (it == client->m_windows.end()) {
  602. did_misbehave("WM_StartWindowResize: Bad window ID");
  603. return;
  604. }
  605. auto& window = *(*it).value;
  606. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  607. // Maybe the client should be allowed to specify what initiated this request?
  608. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  609. }
  610. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  611. {
  612. auto* client = ClientConnection::from_client_id(message.client_id());
  613. if (!client) {
  614. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  615. return;
  616. }
  617. auto it = client->m_windows.find(message.window_id());
  618. if (it == client->m_windows.end()) {
  619. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  620. return;
  621. }
  622. auto& window = *(*it).value;
  623. WindowManager::the().minimize_windows(window, message.minimized());
  624. }
  625. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  626. {
  627. return make<Messages::WindowServer::GreetResponse>(client_id(), Screen::the().rect(), Gfx::current_system_theme_buffer_id());
  628. }
  629. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  630. {
  631. auto* client = ClientConnection::from_client_id(message.client_id());
  632. if (!client) {
  633. did_misbehave("WM_SetWindowTaskbarRect: Bad client ID");
  634. return;
  635. }
  636. auto it = client->m_windows.find(message.window_id());
  637. if (it == client->m_windows.end()) {
  638. did_misbehave("WM_SetWindowTaskbarRect: Bad window ID");
  639. return;
  640. }
  641. auto& window = *(*it).value;
  642. window.set_taskbar_rect(message.rect());
  643. }
  644. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  645. {
  646. auto& wm = WindowManager::the();
  647. if (wm.dnd_client())
  648. return make<Messages::WindowServer::StartDragResponse>(false);
  649. RefPtr<Gfx::Bitmap> bitmap;
  650. if (message.bitmap_id() != -1) {
  651. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.bitmap_id());
  652. ssize_t size_in_bytes = message.bitmap_size().area() * sizeof(Gfx::RGBA32);
  653. if (size_in_bytes > shared_buffer->size()) {
  654. did_misbehave("SetAppletBackingStore: Shared buffer is too small for applet size");
  655. return nullptr;
  656. }
  657. bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, message.bitmap_size());
  658. }
  659. wm.start_dnd_drag(*this, message.text(), bitmap, message.data_type(), message.data());
  660. return make<Messages::WindowServer::StartDragResponse>(true);
  661. }
  662. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  663. {
  664. auto it = m_menus.find(message.menu_id());
  665. if (it == m_menus.end()) {
  666. did_misbehave("SetSystemMenu called with invalid menu ID");
  667. return nullptr;
  668. }
  669. auto& menu = it->value;
  670. MenuManager::the().set_system_menu(menu);
  671. return make<Messages::WindowServer::SetSystemMenuResponse>();
  672. }
  673. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  674. {
  675. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  676. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  677. }
  678. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  679. {
  680. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  681. auto name = wm_config->read_entry("Theme", "Name");
  682. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  683. }
  684. void ClientConnection::boost()
  685. {
  686. // FIXME: Re-enable this when we have a solution for boosting.
  687. #if 0
  688. if (set_process_boost(client_pid(), 10) < 0)
  689. perror("boost: set_process_boost");
  690. #endif
  691. }
  692. void ClientConnection::deboost()
  693. {
  694. // FIXME: Re-enable this when we have a solution for boosting.
  695. #if 0
  696. if (set_process_boost(client_pid(), 0) < 0)
  697. perror("deboost: set_process_boost");
  698. #endif
  699. }
  700. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  701. {
  702. auto it = m_windows.find(message.window_id());
  703. if (it == m_windows.end()) {
  704. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  705. return nullptr;
  706. }
  707. auto& window = *it->value;
  708. window.set_base_size(message.base_size());
  709. window.set_size_increment(message.size_increment());
  710. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  711. }
  712. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  713. {
  714. if (m_has_display_link)
  715. return;
  716. m_has_display_link = true;
  717. Compositor::the().increment_display_link_count({});
  718. }
  719. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  720. {
  721. if (!m_has_display_link)
  722. return;
  723. m_has_display_link = false;
  724. Compositor::the().decrement_display_link_count({});
  725. }
  726. void ClientConnection::notify_display_link(Badge<Compositor>)
  727. {
  728. if (!m_has_display_link)
  729. return;
  730. post_message(Messages::WindowClient::DisplayLinkNotification());
  731. }
  732. void ClientConnection::handle(const Messages::WindowServer::SetWindowProgress& message)
  733. {
  734. auto it = m_windows.find(message.window_id());
  735. if (it == m_windows.end()) {
  736. did_misbehave("SetWindowProgress with bad window ID");
  737. return;
  738. }
  739. it->value->set_progress(message.progress());
  740. }
  741. void ClientConnection::handle(const Messages::WindowServer::Pong&)
  742. {
  743. m_ping_timer = nullptr;
  744. set_unresponsive(false);
  745. }
  746. void ClientConnection::set_unresponsive(bool unresponsive)
  747. {
  748. if (m_unresponsive == unresponsive)
  749. return;
  750. m_unresponsive = unresponsive;
  751. for (auto& it : m_windows) {
  752. auto& window = *it.value;
  753. window.invalidate();
  754. if (unresponsive)
  755. window.set_override_cursor(WindowManager::the().wait_cursor());
  756. }
  757. Compositor::the().invalidate_cursor();
  758. }
  759. void ClientConnection::may_have_become_unresponsive()
  760. {
  761. post_message(Messages::WindowClient::Ping());
  762. m_ping_timer = Core::Timer::create_single_shot(1000, [this] {
  763. set_unresponsive(true);
  764. });
  765. }
  766. void ClientConnection::did_become_responsive()
  767. {
  768. set_unresponsive(false);
  769. }
  770. }