ClientConnection.cpp 28 KB

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