ClientConnection.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. MenuManager::the().close_all_menus_from_client({}, *this);
  84. auto windows = move(m_windows);
  85. for (auto& window : windows) {
  86. window.value->detach_client({});
  87. if (window.value->type() == WindowType::MenuApplet)
  88. AppletManager::the().remove_applet(window.value);
  89. }
  90. }
  91. void ClientConnection::die()
  92. {
  93. deferred_invoke([this](auto&) {
  94. s_connections->remove(client_id());
  95. });
  96. }
  97. void ClientConnection::notify_about_new_screen_rect(const Gfx::Rect& rect)
  98. {
  99. post_message(Messages::WindowClient::ScreenRectChanged(rect));
  100. }
  101. void ClientConnection::notify_about_clipboard_contents_changed()
  102. {
  103. post_message(Messages::WindowClient::ClipboardContentsChanged(Clipboard::the().data_type()));
  104. }
  105. OwnPtr<Messages::WindowServer::CreateMenubarResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenubar&)
  106. {
  107. int menubar_id = m_next_menubar_id++;
  108. auto menubar = make<MenuBar>(*this, menubar_id);
  109. m_menubars.set(menubar_id, move(menubar));
  110. return make<Messages::WindowServer::CreateMenubarResponse>(menubar_id);
  111. }
  112. OwnPtr<Messages::WindowServer::DestroyMenubarResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenubar& message)
  113. {
  114. int menubar_id = message.menubar_id();
  115. auto it = m_menubars.find(menubar_id);
  116. if (it == m_menubars.end()) {
  117. did_misbehave("DestroyMenubar: Bad menubar ID");
  118. return nullptr;
  119. }
  120. auto& menubar = *(*it).value;
  121. MenuManager::the().close_menubar(menubar);
  122. m_menubars.remove(it);
  123. return make<Messages::WindowServer::DestroyMenubarResponse>();
  124. }
  125. OwnPtr<Messages::WindowServer::CreateMenuResponse> ClientConnection::handle(const Messages::WindowServer::CreateMenu& message)
  126. {
  127. int menu_id = m_next_menu_id++;
  128. auto menu = Menu::construct(this, menu_id, message.menu_title());
  129. m_menus.set(menu_id, move(menu));
  130. return make<Messages::WindowServer::CreateMenuResponse>(menu_id);
  131. }
  132. OwnPtr<Messages::WindowServer::DestroyMenuResponse> ClientConnection::handle(const Messages::WindowServer::DestroyMenu& message)
  133. {
  134. int menu_id = message.menu_id();
  135. auto it = m_menus.find(menu_id);
  136. if (it == m_menus.end()) {
  137. did_misbehave("DestroyMenu: Bad menu ID");
  138. return nullptr;
  139. }
  140. auto& menu = *(*it).value;
  141. menu.close();
  142. m_menus.remove(it);
  143. remove_child(menu);
  144. return make<Messages::WindowServer::DestroyMenuResponse>();
  145. }
  146. OwnPtr<Messages::WindowServer::SetApplicationMenubarResponse> ClientConnection::handle(const Messages::WindowServer::SetApplicationMenubar& message)
  147. {
  148. int menubar_id = message.menubar_id();
  149. auto it = m_menubars.find(menubar_id);
  150. if (it == m_menubars.end()) {
  151. did_misbehave("SetApplicationMenubar: Bad menubar ID");
  152. return nullptr;
  153. }
  154. auto& menubar = *(*it).value;
  155. m_app_menubar = menubar.make_weak_ptr();
  156. WindowManager::the().notify_client_changed_app_menubar(*this);
  157. return make<Messages::WindowServer::SetApplicationMenubarResponse>();
  158. }
  159. OwnPtr<Messages::WindowServer::AddMenuToMenubarResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuToMenubar& message)
  160. {
  161. int menubar_id = message.menubar_id();
  162. int menu_id = message.menu_id();
  163. auto it = m_menubars.find(menubar_id);
  164. auto jt = m_menus.find(menu_id);
  165. if (it == m_menubars.end()) {
  166. did_misbehave("AddMenuToMenubar: Bad menubar ID");
  167. return nullptr;
  168. }
  169. if (jt == m_menus.end()) {
  170. did_misbehave("AddMenuToMenubar: Bad menu ID");
  171. return nullptr;
  172. }
  173. auto& menubar = *(*it).value;
  174. auto& menu = *(*jt).value;
  175. menubar.add_menu(menu);
  176. return make<Messages::WindowServer::AddMenuToMenubarResponse>();
  177. }
  178. OwnPtr<Messages::WindowServer::AddMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::AddMenuItem& message)
  179. {
  180. int menu_id = message.menu_id();
  181. unsigned identifier = message.identifier();
  182. auto it = m_menus.find(menu_id);
  183. if (it == m_menus.end()) {
  184. dbg() << "AddMenuItem: Bad menu ID: " << menu_id;
  185. return nullptr;
  186. }
  187. auto& menu = *(*it).value;
  188. auto menu_item = make<MenuItem>(menu, identifier, message.text(), message.shortcut(), message.enabled(), message.checkable(), message.checked());
  189. if (message.icon_buffer_id() != -1) {
  190. auto icon_buffer = SharedBuffer::create_from_shbuf_id(message.icon_buffer_id());
  191. if (!icon_buffer)
  192. return nullptr;
  193. // FIXME: Verify that the icon buffer can accomodate a 16x16 bitmap view.
  194. auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, icon_buffer.release_nonnull(), { 16, 16 });
  195. menu_item->set_icon(shared_icon);
  196. }
  197. menu_item->set_submenu_id(message.submenu_id());
  198. menu_item->set_exclusive(message.exclusive());
  199. menu.add_item(move(menu_item));
  200. return make<Messages::WindowServer::AddMenuItemResponse>();
  201. }
  202. OwnPtr<Messages::WindowServer::PopupMenuResponse> ClientConnection::handle(const Messages::WindowServer::PopupMenu& message)
  203. {
  204. int menu_id = message.menu_id();
  205. auto position = message.screen_position();
  206. auto it = m_menus.find(menu_id);
  207. if (it == m_menus.end()) {
  208. did_misbehave("PopupMenu: Bad menu ID");
  209. return nullptr;
  210. }
  211. auto& menu = *(*it).value;
  212. menu.popup(position);
  213. return make<Messages::WindowServer::PopupMenuResponse>();
  214. }
  215. OwnPtr<Messages::WindowServer::DismissMenuResponse> ClientConnection::handle(const Messages::WindowServer::DismissMenu& message)
  216. {
  217. int menu_id = message.menu_id();
  218. auto it = m_menus.find(menu_id);
  219. if (it == m_menus.end()) {
  220. did_misbehave("DismissMenu: Bad menu ID");
  221. return nullptr;
  222. }
  223. auto& menu = *(*it).value;
  224. menu.close();
  225. return make<Messages::WindowServer::DismissMenuResponse>();
  226. }
  227. OwnPtr<Messages::WindowServer::UpdateMenuItemResponse> ClientConnection::handle(const Messages::WindowServer::UpdateMenuItem& message)
  228. {
  229. int menu_id = message.menu_id();
  230. auto it = m_menus.find(menu_id);
  231. if (it == m_menus.end()) {
  232. did_misbehave("UpdateMenuItem: Bad menu ID");
  233. return nullptr;
  234. }
  235. auto& menu = *(*it).value;
  236. auto* menu_item = menu.item_with_identifier(message.identifier());
  237. if (!menu_item) {
  238. did_misbehave("UpdateMenuItem: Bad menu item identifier");
  239. return nullptr;
  240. }
  241. menu_item->set_text(message.text());
  242. menu_item->set_shortcut_text(message.shortcut());
  243. menu_item->set_enabled(message.enabled());
  244. menu_item->set_checkable(message.checkable());
  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_backgound_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::SetWindowIconBitmapResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowIconBitmap& message)
  335. {
  336. auto it = m_windows.find(message.window_id());
  337. if (it == m_windows.end()) {
  338. did_misbehave("SetWindowIconBitmap: Bad window ID");
  339. return nullptr;
  340. }
  341. auto& window = *(*it).value;
  342. if (message.icon().is_valid()) {
  343. window.set_icon(*message.icon().bitmap());
  344. } else {
  345. window.set_default_icon();
  346. }
  347. window.frame().invalidate_title_bar();
  348. WindowManager::the().tell_wm_listeners_window_icon_changed(window);
  349. return make<Messages::WindowServer::SetWindowIconBitmapResponse>();
  350. }
  351. OwnPtr<Messages::WindowServer::SetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowRect& message)
  352. {
  353. int window_id = message.window_id();
  354. auto it = m_windows.find(window_id);
  355. if (it == m_windows.end()) {
  356. did_misbehave("SetWindowRect: Bad window ID");
  357. return nullptr;
  358. }
  359. auto& window = *(*it).value;
  360. if (window.is_fullscreen()) {
  361. dbg() << "ClientConnection: Ignoring SetWindowRect request for fullscreen window";
  362. return nullptr;
  363. }
  364. auto normalized_rect = normalize_window_rect(message.rect(), window.type());
  365. window.set_rect(normalized_rect);
  366. window.request_update(normalized_rect);
  367. return make<Messages::WindowServer::SetWindowRectResponse>(normalized_rect);
  368. }
  369. OwnPtr<Messages::WindowServer::GetWindowRectResponse> ClientConnection::handle(const Messages::WindowServer::GetWindowRect& message)
  370. {
  371. int window_id = message.window_id();
  372. auto it = m_windows.find(window_id);
  373. if (it == m_windows.end()) {
  374. did_misbehave("GetWindowRect: Bad window ID");
  375. return nullptr;
  376. }
  377. return make<Messages::WindowServer::GetWindowRectResponse>(it->value->rect());
  378. }
  379. OwnPtr<Messages::WindowServer::SetClipboardContentsResponse> ClientConnection::handle(const Messages::WindowServer::SetClipboardContents& message)
  380. {
  381. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  382. if (!shared_buffer) {
  383. did_misbehave("SetClipboardContents: Bad shared buffer ID");
  384. return nullptr;
  385. }
  386. Clipboard::the().set_data(*shared_buffer, message.content_size(), message.content_type());
  387. return make<Messages::WindowServer::SetClipboardContentsResponse>();
  388. }
  389. OwnPtr<Messages::WindowServer::GetClipboardContentsResponse> ClientConnection::handle(const Messages::WindowServer::GetClipboardContents&)
  390. {
  391. auto& clipboard = Clipboard::the();
  392. i32 shbuf_id = -1;
  393. if (clipboard.size()) {
  394. // FIXME: Optimize case where an app is copy/pasting within itself.
  395. // We can just reuse the SharedBuffer then, since it will have the same peer PID.
  396. // It would be even nicer if a SharedBuffer could have an arbitrary number of clients..
  397. RefPtr<SharedBuffer> shared_buffer = SharedBuffer::create_with_size(clipboard.size());
  398. ASSERT(shared_buffer);
  399. memcpy(shared_buffer->data(), clipboard.data(), clipboard.size());
  400. shared_buffer->seal();
  401. shared_buffer->share_with(client_pid());
  402. shbuf_id = shared_buffer->shbuf_id();
  403. // FIXME: This is a workaround for the fact that SharedBuffers will go away if neither side is retaining them.
  404. // After we respond to GetClipboardContents, we have to wait for the client to ref the buffer on his side.
  405. m_last_sent_clipboard_content = move(shared_buffer);
  406. }
  407. return make<Messages::WindowServer::GetClipboardContentsResponse>(shbuf_id, clipboard.size(), clipboard.data_type());
  408. }
  409. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  410. {
  411. int window_id = m_next_window_id++;
  412. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.resizable(), message.fullscreen());
  413. window->set_has_alpha_channel(message.has_alpha_channel());
  414. window->set_title(message.title());
  415. if (!message.fullscreen()) {
  416. auto normalized_rect = normalize_window_rect(message.rect(), window->type());
  417. window->set_rect(normalized_rect);
  418. }
  419. if (window->type() == WindowType::Desktop) {
  420. window->set_rect(WindowManager::the().desktop_rect());
  421. window->recalculate_rect();
  422. }
  423. window->set_show_titlebar(message.show_titlebar());
  424. window->set_opacity(message.opacity());
  425. window->set_size_increment(message.size_increment());
  426. window->set_base_size(message.base_size());
  427. window->invalidate();
  428. if (window->type() == WindowType::MenuApplet)
  429. AppletManager::the().add_applet(*window);
  430. m_windows.set(window_id, move(window));
  431. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  432. }
  433. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  434. {
  435. auto it = m_windows.find(message.window_id());
  436. if (it == m_windows.end()) {
  437. did_misbehave("DestroyWindow: Bad window ID");
  438. return nullptr;
  439. }
  440. auto& window = *(*it).value;
  441. if (window.type() == WindowType::MenuApplet)
  442. AppletManager::the().remove_applet(window);
  443. WindowManager::the().invalidate(window);
  444. remove_child(window);
  445. ASSERT(it->value.ptr() == &window);
  446. m_windows.remove(message.window_id());
  447. return make<Messages::WindowServer::DestroyWindowResponse>();
  448. }
  449. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  450. {
  451. auto rect_set = window.take_pending_paint_rects();
  452. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  453. return;
  454. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  455. }
  456. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  457. {
  458. auto it = m_windows.find(message.window_id());
  459. if (it == m_windows.end()) {
  460. did_misbehave("InvalidateRect: Bad window ID");
  461. return;
  462. }
  463. auto& window = *(*it).value;
  464. for (size_t i = 0; i < message.rects().size(); ++i)
  465. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  466. }
  467. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  468. {
  469. int window_id = message.window_id();
  470. auto it = m_windows.find(window_id);
  471. if (it == m_windows.end()) {
  472. did_misbehave("DidFinishPainting: Bad window ID");
  473. return;
  474. }
  475. auto& window = *(*it).value;
  476. for (auto& rect : message.rects())
  477. WindowManager::the().invalidate(window, rect);
  478. WindowSwitcher::the().refresh_if_needed();
  479. }
  480. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  481. {
  482. int window_id = message.window_id();
  483. auto it = m_windows.find(window_id);
  484. if (it == m_windows.end()) {
  485. did_misbehave("SetWindowBackingStore: Bad window ID");
  486. return nullptr;
  487. }
  488. auto& window = *(*it).value;
  489. if (window.last_backing_store() && window.last_backing_store()->shbuf_id() == message.shbuf_id()) {
  490. window.swap_backing_stores();
  491. } else {
  492. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  493. if (!shared_buffer)
  494. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  495. auto backing_store = Gfx::Bitmap::create_with_shared_buffer(
  496. message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
  497. *shared_buffer,
  498. message.size());
  499. window.set_backing_store(move(backing_store));
  500. }
  501. if (message.flush_immediately())
  502. window.invalidate();
  503. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  504. }
  505. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  506. {
  507. int window_id = message.window_id();
  508. auto it = m_windows.find(window_id);
  509. if (it == m_windows.end()) {
  510. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  511. return nullptr;
  512. }
  513. it->value->set_global_cursor_tracking_enabled(message.enabled());
  514. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  515. }
  516. OwnPtr<Messages::WindowServer::SetWindowOverrideCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOverrideCursor& message)
  517. {
  518. auto it = m_windows.find(message.window_id());
  519. if (it == m_windows.end()) {
  520. did_misbehave("SetWindowOverrideCursor: Bad window ID");
  521. return nullptr;
  522. }
  523. auto& window = *(*it).value;
  524. window.set_override_cursor(Cursor::create((StandardCursor)message.cursor_type()));
  525. return make<Messages::WindowServer::SetWindowOverrideCursorResponse>();
  526. }
  527. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  528. {
  529. auto it = m_windows.find(message.window_id());
  530. if (it == m_windows.end()) {
  531. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  532. return nullptr;
  533. }
  534. it->value->set_has_alpha_channel(message.has_alpha_channel());
  535. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  536. }
  537. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  538. {
  539. auto* client = ClientConnection::from_client_id(message.client_id());
  540. if (!client) {
  541. did_misbehave("WM_SetActiveWindow: Bad client ID");
  542. return;
  543. }
  544. auto it = client->m_windows.find(message.window_id());
  545. if (it == client->m_windows.end()) {
  546. did_misbehave("WM_SetActiveWindow: Bad window ID");
  547. return;
  548. }
  549. auto& window = *(*it).value;
  550. window.set_minimized(false);
  551. WindowManager::the().move_to_front_and_make_active(window);
  552. }
  553. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  554. {
  555. auto* client = ClientConnection::from_client_id(message.client_id());
  556. if (!client) {
  557. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  558. return;
  559. }
  560. auto it = client->m_windows.find(message.window_id());
  561. if (it == client->m_windows.end()) {
  562. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  563. return;
  564. }
  565. auto& window = *(*it).value;
  566. window.popup_window_menu(message.screen_position());
  567. }
  568. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  569. {
  570. auto* client = ClientConnection::from_client_id(request.client_id());
  571. if (!client) {
  572. did_misbehave("WM_StartWindowResize: Bad client ID");
  573. return;
  574. }
  575. auto it = client->m_windows.find(request.window_id());
  576. if (it == client->m_windows.end()) {
  577. did_misbehave("WM_StartWindowResize: Bad window ID");
  578. return;
  579. }
  580. auto& window = *(*it).value;
  581. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  582. // Maybe the client should be allowed to specify what initiated this request?
  583. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  584. }
  585. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  586. {
  587. auto* client = ClientConnection::from_client_id(message.client_id());
  588. if (!client) {
  589. did_misbehave("WM_SetWindowMinimized: 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_SetWindowMinimized: Bad window ID");
  595. return;
  596. }
  597. auto& window = *(*it).value;
  598. window.set_minimized(message.minimized());
  599. }
  600. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  601. {
  602. return make<Messages::WindowServer::GreetResponse>(client_id(), Screen::the().rect(), Gfx::current_system_theme_buffer_id());
  603. }
  604. bool ClientConnection::is_showing_modal_window() const
  605. {
  606. for (auto& it : m_windows) {
  607. auto& window = *it.value;
  608. if (window.is_visible() && window.is_modal())
  609. return true;
  610. }
  611. return false;
  612. }
  613. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  614. {
  615. auto* client = ClientConnection::from_client_id(message.client_id());
  616. if (!client) {
  617. did_misbehave("WM_SetWindowTaskbarRect: Bad client ID");
  618. return;
  619. }
  620. auto it = client->m_windows.find(message.window_id());
  621. if (it == client->m_windows.end()) {
  622. did_misbehave("WM_SetWindowTaskbarRect: Bad window ID");
  623. return;
  624. }
  625. auto& window = *(*it).value;
  626. window.set_taskbar_rect(message.rect());
  627. }
  628. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  629. {
  630. auto& wm = WindowManager::the();
  631. if (wm.dnd_client())
  632. return make<Messages::WindowServer::StartDragResponse>(false);
  633. RefPtr<Gfx::Bitmap> bitmap;
  634. if (message.bitmap_id() != -1) {
  635. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.bitmap_id());
  636. ssize_t size_in_bytes = message.bitmap_size().area() * sizeof(Gfx::RGBA32);
  637. if (size_in_bytes > shared_buffer->size()) {
  638. did_misbehave("SetAppletBackingStore: Shared buffer is too small for applet size");
  639. return nullptr;
  640. }
  641. bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, message.bitmap_size());
  642. }
  643. wm.start_dnd_drag(*this, message.text(), bitmap, message.data_type(), message.data());
  644. return make<Messages::WindowServer::StartDragResponse>(true);
  645. }
  646. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  647. {
  648. auto it = m_menus.find(message.menu_id());
  649. if (it == m_menus.end()) {
  650. did_misbehave("SetSystemMenu called with invalid menu ID");
  651. return nullptr;
  652. }
  653. auto& menu = it->value;
  654. MenuManager::the().set_system_menu(menu);
  655. return make<Messages::WindowServer::SetSystemMenuResponse>();
  656. }
  657. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  658. {
  659. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  660. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  661. }
  662. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  663. {
  664. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  665. auto name = wm_config->read_entry("Theme", "Name");
  666. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  667. }
  668. void ClientConnection::boost()
  669. {
  670. // FIXME: Re-enable this when we have a solution for boosting.
  671. #if 0
  672. if (set_process_boost(client_pid(), 10) < 0)
  673. perror("boost: set_process_boost");
  674. #endif
  675. }
  676. void ClientConnection::deboost()
  677. {
  678. // FIXME: Re-enable this when we have a solution for boosting.
  679. #if 0
  680. if (set_process_boost(client_pid(), 0) < 0)
  681. perror("deboost: set_process_boost");
  682. #endif
  683. }
  684. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  685. {
  686. auto it = m_windows.find(message.window_id());
  687. if (it == m_windows.end()) {
  688. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  689. return nullptr;
  690. }
  691. auto& window = *it->value;
  692. window.set_base_size(message.base_size());
  693. window.set_size_increment(message.size_increment());
  694. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  695. }
  696. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  697. {
  698. m_has_display_link = true;
  699. }
  700. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  701. {
  702. m_has_display_link = false;
  703. }
  704. void ClientConnection::notify_display_link(Badge<Compositor>)
  705. {
  706. if (!m_has_display_link)
  707. return;
  708. post_message(Messages::WindowClient::DisplayLinkNotification());
  709. }
  710. }