ClientConnection.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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_backgound_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. OwnPtr<Messages::WindowServer::CreateWindowResponse> ClientConnection::handle(const Messages::WindowServer::CreateWindow& message)
  412. {
  413. int window_id = m_next_window_id++;
  414. auto window = Window::construct(*this, (WindowType)message.type(), window_id, message.modal(), message.minimizable(), message.resizable(), message.fullscreen());
  415. window->set_has_alpha_channel(message.has_alpha_channel());
  416. window->set_title(message.title());
  417. if (!message.fullscreen()) {
  418. auto normalized_rect = normalize_window_rect(message.rect(), window->type());
  419. window->set_rect(normalized_rect);
  420. }
  421. if (window->type() == WindowType::Desktop) {
  422. window->set_rect(WindowManager::the().desktop_rect());
  423. window->recalculate_rect();
  424. }
  425. window->set_show_titlebar(message.show_titlebar());
  426. window->set_opacity(message.opacity());
  427. window->set_size_increment(message.size_increment());
  428. window->set_base_size(message.base_size());
  429. window->invalidate();
  430. if (window->type() == WindowType::MenuApplet)
  431. AppletManager::the().add_applet(*window);
  432. m_windows.set(window_id, move(window));
  433. return make<Messages::WindowServer::CreateWindowResponse>(window_id);
  434. }
  435. OwnPtr<Messages::WindowServer::DestroyWindowResponse> ClientConnection::handle(const Messages::WindowServer::DestroyWindow& message)
  436. {
  437. auto it = m_windows.find(message.window_id());
  438. if (it == m_windows.end()) {
  439. did_misbehave("DestroyWindow: Bad window ID");
  440. return nullptr;
  441. }
  442. auto& window = *(*it).value;
  443. if (window.type() == WindowType::MenuApplet)
  444. AppletManager::the().remove_applet(window);
  445. WindowManager::the().invalidate(window);
  446. remove_child(window);
  447. ASSERT(it->value.ptr() == &window);
  448. m_windows.remove(message.window_id());
  449. return make<Messages::WindowServer::DestroyWindowResponse>();
  450. }
  451. void ClientConnection::post_paint_message(Window& window, bool ignore_occlusion)
  452. {
  453. auto rect_set = window.take_pending_paint_rects();
  454. if (window.is_minimized() || (!ignore_occlusion && window.is_occluded()))
  455. return;
  456. post_message(Messages::WindowClient::Paint(window.window_id(), window.size(), rect_set.rects()));
  457. }
  458. void ClientConnection::handle(const Messages::WindowServer::InvalidateRect& message)
  459. {
  460. auto it = m_windows.find(message.window_id());
  461. if (it == m_windows.end()) {
  462. did_misbehave("InvalidateRect: Bad window ID");
  463. return;
  464. }
  465. auto& window = *(*it).value;
  466. for (size_t i = 0; i < message.rects().size(); ++i)
  467. window.request_update(message.rects()[i].intersected({ {}, window.size() }), message.ignore_occlusion());
  468. }
  469. void ClientConnection::handle(const Messages::WindowServer::DidFinishPainting& message)
  470. {
  471. int window_id = message.window_id();
  472. auto it = m_windows.find(window_id);
  473. if (it == m_windows.end()) {
  474. did_misbehave("DidFinishPainting: Bad window ID");
  475. return;
  476. }
  477. auto& window = *(*it).value;
  478. for (auto& rect : message.rects())
  479. WindowManager::the().invalidate(window, rect);
  480. WindowSwitcher::the().refresh_if_needed();
  481. }
  482. OwnPtr<Messages::WindowServer::SetWindowBackingStoreResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBackingStore& message)
  483. {
  484. int window_id = message.window_id();
  485. auto it = m_windows.find(window_id);
  486. if (it == m_windows.end()) {
  487. did_misbehave("SetWindowBackingStore: Bad window ID");
  488. return nullptr;
  489. }
  490. auto& window = *(*it).value;
  491. if (window.last_backing_store() && window.last_backing_store()->shbuf_id() == message.shbuf_id()) {
  492. window.swap_backing_stores();
  493. } else {
  494. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.shbuf_id());
  495. if (!shared_buffer)
  496. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  497. auto backing_store = Gfx::Bitmap::create_with_shared_buffer(
  498. message.has_alpha_channel() ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32,
  499. *shared_buffer,
  500. message.size());
  501. window.set_backing_store(move(backing_store));
  502. }
  503. if (message.flush_immediately())
  504. window.invalidate();
  505. return make<Messages::WindowServer::SetWindowBackingStoreResponse>();
  506. }
  507. OwnPtr<Messages::WindowServer::SetGlobalCursorTrackingResponse> ClientConnection::handle(const Messages::WindowServer::SetGlobalCursorTracking& message)
  508. {
  509. int window_id = message.window_id();
  510. auto it = m_windows.find(window_id);
  511. if (it == m_windows.end()) {
  512. did_misbehave("SetGlobalCursorTracking: Bad window ID");
  513. return nullptr;
  514. }
  515. it->value->set_global_cursor_tracking_enabled(message.enabled());
  516. return make<Messages::WindowServer::SetGlobalCursorTrackingResponse>();
  517. }
  518. OwnPtr<Messages::WindowServer::SetWindowOverrideCursorResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowOverrideCursor& message)
  519. {
  520. auto it = m_windows.find(message.window_id());
  521. if (it == m_windows.end()) {
  522. did_misbehave("SetWindowOverrideCursor: Bad window ID");
  523. return nullptr;
  524. }
  525. auto& window = *(*it).value;
  526. window.set_override_cursor(Cursor::create((StandardCursor)message.cursor_type()));
  527. return make<Messages::WindowServer::SetWindowOverrideCursorResponse>();
  528. }
  529. OwnPtr<Messages::WindowServer::SetWindowHasAlphaChannelResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowHasAlphaChannel& message)
  530. {
  531. auto it = m_windows.find(message.window_id());
  532. if (it == m_windows.end()) {
  533. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  534. return nullptr;
  535. }
  536. it->value->set_has_alpha_channel(message.has_alpha_channel());
  537. return make<Messages::WindowServer::SetWindowHasAlphaChannelResponse>();
  538. }
  539. void ClientConnection::handle(const Messages::WindowServer::WM_SetActiveWindow& message)
  540. {
  541. auto* client = ClientConnection::from_client_id(message.client_id());
  542. if (!client) {
  543. did_misbehave("WM_SetActiveWindow: Bad client ID");
  544. return;
  545. }
  546. auto it = client->m_windows.find(message.window_id());
  547. if (it == client->m_windows.end()) {
  548. did_misbehave("WM_SetActiveWindow: Bad window ID");
  549. return;
  550. }
  551. auto& window = *(*it).value;
  552. window.set_minimized(false);
  553. WindowManager::the().move_to_front_and_make_active(window);
  554. }
  555. void ClientConnection::handle(const Messages::WindowServer::WM_PopupWindowMenu& message)
  556. {
  557. auto* client = ClientConnection::from_client_id(message.client_id());
  558. if (!client) {
  559. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  560. return;
  561. }
  562. auto it = client->m_windows.find(message.window_id());
  563. if (it == client->m_windows.end()) {
  564. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  565. return;
  566. }
  567. auto& window = *(*it).value;
  568. window.popup_window_menu(message.screen_position());
  569. }
  570. void ClientConnection::handle(const Messages::WindowServer::WM_StartWindowResize& request)
  571. {
  572. auto* client = ClientConnection::from_client_id(request.client_id());
  573. if (!client) {
  574. did_misbehave("WM_StartWindowResize: Bad client ID");
  575. return;
  576. }
  577. auto it = client->m_windows.find(request.window_id());
  578. if (it == client->m_windows.end()) {
  579. did_misbehave("WM_StartWindowResize: Bad window ID");
  580. return;
  581. }
  582. auto& window = *(*it).value;
  583. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  584. // Maybe the client should be allowed to specify what initiated this request?
  585. WindowManager::the().start_window_resize(window, Screen::the().cursor_location(), MouseButton::Left);
  586. }
  587. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowMinimized& message)
  588. {
  589. auto* client = ClientConnection::from_client_id(message.client_id());
  590. if (!client) {
  591. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  592. return;
  593. }
  594. auto it = client->m_windows.find(message.window_id());
  595. if (it == client->m_windows.end()) {
  596. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  597. return;
  598. }
  599. auto& window = *(*it).value;
  600. window.set_minimized(message.minimized());
  601. }
  602. OwnPtr<Messages::WindowServer::GreetResponse> ClientConnection::handle(const Messages::WindowServer::Greet&)
  603. {
  604. return make<Messages::WindowServer::GreetResponse>(client_id(), Screen::the().rect(), Gfx::current_system_theme_buffer_id());
  605. }
  606. bool ClientConnection::is_showing_modal_window() const
  607. {
  608. for (auto& it : m_windows) {
  609. auto& window = *it.value;
  610. if (window.is_visible() && window.is_modal())
  611. return true;
  612. }
  613. return false;
  614. }
  615. void ClientConnection::handle(const Messages::WindowServer::WM_SetWindowTaskbarRect& message)
  616. {
  617. auto* client = ClientConnection::from_client_id(message.client_id());
  618. if (!client) {
  619. did_misbehave("WM_SetWindowTaskbarRect: Bad client ID");
  620. return;
  621. }
  622. auto it = client->m_windows.find(message.window_id());
  623. if (it == client->m_windows.end()) {
  624. did_misbehave("WM_SetWindowTaskbarRect: Bad window ID");
  625. return;
  626. }
  627. auto& window = *(*it).value;
  628. window.set_taskbar_rect(message.rect());
  629. }
  630. OwnPtr<Messages::WindowServer::StartDragResponse> ClientConnection::handle(const Messages::WindowServer::StartDrag& message)
  631. {
  632. auto& wm = WindowManager::the();
  633. if (wm.dnd_client())
  634. return make<Messages::WindowServer::StartDragResponse>(false);
  635. RefPtr<Gfx::Bitmap> bitmap;
  636. if (message.bitmap_id() != -1) {
  637. auto shared_buffer = SharedBuffer::create_from_shbuf_id(message.bitmap_id());
  638. ssize_t size_in_bytes = message.bitmap_size().area() * sizeof(Gfx::RGBA32);
  639. if (size_in_bytes > shared_buffer->size()) {
  640. did_misbehave("SetAppletBackingStore: Shared buffer is too small for applet size");
  641. return nullptr;
  642. }
  643. bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, message.bitmap_size());
  644. }
  645. wm.start_dnd_drag(*this, message.text(), bitmap, message.data_type(), message.data());
  646. return make<Messages::WindowServer::StartDragResponse>(true);
  647. }
  648. OwnPtr<Messages::WindowServer::SetSystemMenuResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemMenu& message)
  649. {
  650. auto it = m_menus.find(message.menu_id());
  651. if (it == m_menus.end()) {
  652. did_misbehave("SetSystemMenu called with invalid menu ID");
  653. return nullptr;
  654. }
  655. auto& menu = it->value;
  656. MenuManager::the().set_system_menu(menu);
  657. return make<Messages::WindowServer::SetSystemMenuResponse>();
  658. }
  659. OwnPtr<Messages::WindowServer::SetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::SetSystemTheme& message)
  660. {
  661. bool success = WindowManager::the().update_theme(message.theme_path(), message.theme_name());
  662. return make<Messages::WindowServer::SetSystemThemeResponse>(success);
  663. }
  664. OwnPtr<Messages::WindowServer::GetSystemThemeResponse> ClientConnection::handle(const Messages::WindowServer::GetSystemTheme&)
  665. {
  666. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  667. auto name = wm_config->read_entry("Theme", "Name");
  668. return make<Messages::WindowServer::GetSystemThemeResponse>(name);
  669. }
  670. void ClientConnection::boost()
  671. {
  672. // FIXME: Re-enable this when we have a solution for boosting.
  673. #if 0
  674. if (set_process_boost(client_pid(), 10) < 0)
  675. perror("boost: set_process_boost");
  676. #endif
  677. }
  678. void ClientConnection::deboost()
  679. {
  680. // FIXME: Re-enable this when we have a solution for boosting.
  681. #if 0
  682. if (set_process_boost(client_pid(), 0) < 0)
  683. perror("deboost: set_process_boost");
  684. #endif
  685. }
  686. OwnPtr<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse> ClientConnection::handle(const Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement& message)
  687. {
  688. auto it = m_windows.find(message.window_id());
  689. if (it == m_windows.end()) {
  690. did_misbehave("SetWindowBaseSizeAndSizeIncrementResponse: Bad window ID");
  691. return nullptr;
  692. }
  693. auto& window = *it->value;
  694. window.set_base_size(message.base_size());
  695. window.set_size_increment(message.size_increment());
  696. return make<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrementResponse>();
  697. }
  698. void ClientConnection::handle(const Messages::WindowServer::EnableDisplayLink&)
  699. {
  700. if (m_has_display_link)
  701. return;
  702. m_has_display_link = true;
  703. Compositor::the().increment_display_link_count({});
  704. }
  705. void ClientConnection::handle(const Messages::WindowServer::DisableDisplayLink&)
  706. {
  707. if (!m_has_display_link)
  708. return;
  709. m_has_display_link = false;
  710. Compositor::the().decrement_display_link_count({});
  711. }
  712. void ClientConnection::notify_display_link(Badge<Compositor>)
  713. {
  714. if (!m_has_display_link)
  715. return;
  716. post_message(Messages::WindowClient::DisplayLinkNotification());
  717. }
  718. }