WSClientConnection.cpp 25 KB

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