ClientConnection.cpp 31 KB

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