WSClientConnection.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. #include <LibC/SharedBuffer.h>
  2. #include <LibDraw/GraphicsBitmap.h>
  3. #include <LibDraw/SystemTheme.h>
  4. #include <SharedBuffer.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. if (window->type() == WSWindowType::MenuApplet)
  373. WSWindowManager::the().menu_manager().add_applet(*window);
  374. m_windows.set(window_id, move(window));
  375. return make<WindowServer::CreateWindowResponse>(window_id);
  376. }
  377. OwnPtr<WindowServer::DestroyWindowResponse> WSClientConnection::handle(const WindowServer::DestroyWindow& message)
  378. {
  379. auto it = m_windows.find(message.window_id());
  380. if (it == m_windows.end()) {
  381. did_misbehave("DestroyWindow: Bad window ID");
  382. return nullptr;
  383. }
  384. auto& window = *(*it).value;
  385. if (window.type() == WSWindowType::MenuApplet)
  386. WSWindowManager::the().menu_manager().remove_applet(window);
  387. WSWindowManager::the().invalidate(window);
  388. remove_child(window);
  389. ASSERT(it->value.ptr() == &window);
  390. m_windows.remove(message.window_id());
  391. return make<WindowServer::DestroyWindowResponse>();
  392. }
  393. void WSClientConnection::post_paint_message(WSWindow& window)
  394. {
  395. auto rect_set = window.take_pending_paint_rects();
  396. if (window.is_minimized())
  397. return;
  398. Vector<Rect> rects;
  399. rects.ensure_capacity(rect_set.size());
  400. for (auto& r : rect_set.rects()) {
  401. rects.append(r);
  402. }
  403. post_message(WindowClient::Paint(window.window_id(), window.size(), rects));
  404. }
  405. void WSClientConnection::handle(const WindowServer::InvalidateRect& message)
  406. {
  407. auto it = m_windows.find(message.window_id());
  408. if (it == m_windows.end()) {
  409. did_misbehave("InvalidateRect: Bad window ID");
  410. return;
  411. }
  412. auto& window = *(*it).value;
  413. for (int i = 0; i < message.rects().size(); ++i)
  414. window.request_update(message.rects()[i].intersected({ {}, window.size() }));
  415. }
  416. void WSClientConnection::handle(const WindowServer::DidFinishPainting& message)
  417. {
  418. int window_id = message.window_id();
  419. auto it = m_windows.find(window_id);
  420. if (it == m_windows.end()) {
  421. did_misbehave("DidFinishPainting: Bad window ID");
  422. return;
  423. }
  424. auto& window = *(*it).value;
  425. for (auto& rect : message.rects())
  426. WSWindowManager::the().invalidate(window, rect);
  427. WSWindowSwitcher::the().refresh_if_needed();
  428. }
  429. OwnPtr<WindowServer::SetWindowBackingStoreResponse> WSClientConnection::handle(const WindowServer::SetWindowBackingStore& message)
  430. {
  431. int window_id = message.window_id();
  432. auto it = m_windows.find(window_id);
  433. if (it == m_windows.end()) {
  434. did_misbehave("SetWindowBackingStore: Bad window ID");
  435. return nullptr;
  436. }
  437. auto& window = *(*it).value;
  438. if (window.last_backing_store() && window.last_backing_store()->shared_buffer_id() == message.shared_buffer_id()) {
  439. window.swap_backing_stores();
  440. } else {
  441. auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(message.shared_buffer_id());
  442. if (!shared_buffer)
  443. return make<WindowServer::SetWindowBackingStoreResponse>();
  444. auto backing_store = GraphicsBitmap::create_with_shared_buffer(
  445. message.has_alpha_channel() ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32,
  446. *shared_buffer,
  447. message.size());
  448. window.set_backing_store(move(backing_store));
  449. }
  450. if (message.flush_immediately())
  451. window.invalidate();
  452. return make<WindowServer::SetWindowBackingStoreResponse>();
  453. }
  454. OwnPtr<WindowServer::SetGlobalCursorTrackingResponse> WSClientConnection::handle(const WindowServer::SetGlobalCursorTracking& 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("SetGlobalCursorTracking: Bad window ID");
  460. return nullptr;
  461. }
  462. it->value->set_global_cursor_tracking_enabled(message.enabled());
  463. return make<WindowServer::SetGlobalCursorTrackingResponse>();
  464. }
  465. OwnPtr<WindowServer::SetWindowOverrideCursorResponse> WSClientConnection::handle(const WindowServer::SetWindowOverrideCursor& message)
  466. {
  467. auto it = m_windows.find(message.window_id());
  468. if (it == m_windows.end()) {
  469. did_misbehave("SetWindowOverrideCursor: Bad window ID");
  470. return nullptr;
  471. }
  472. auto& window = *(*it).value;
  473. window.set_override_cursor(WSCursor::create((WSStandardCursor)message.cursor_type()));
  474. return make<WindowServer::SetWindowOverrideCursorResponse>();
  475. }
  476. OwnPtr<WindowServer::SetWindowHasAlphaChannelResponse> WSClientConnection::handle(const WindowServer::SetWindowHasAlphaChannel& message)
  477. {
  478. auto it = m_windows.find(message.window_id());
  479. if (it == m_windows.end()) {
  480. did_misbehave("SetWindowHasAlphaChannel: Bad window ID");
  481. return nullptr;
  482. }
  483. it->value->set_has_alpha_channel(message.has_alpha_channel());
  484. return make<WindowServer::SetWindowHasAlphaChannelResponse>();
  485. }
  486. void WSClientConnection::handle(const WindowServer::WM_SetActiveWindow& message)
  487. {
  488. auto* client = WSClientConnection::from_client_id(message.client_id());
  489. if (!client) {
  490. did_misbehave("WM_SetActiveWindow: Bad client ID");
  491. return;
  492. }
  493. auto it = client->m_windows.find(message.window_id());
  494. if (it == client->m_windows.end()) {
  495. did_misbehave("WM_SetActiveWindow: Bad window ID");
  496. return;
  497. }
  498. auto& window = *(*it).value;
  499. window.set_minimized(false);
  500. WSWindowManager::the().move_to_front_and_make_active(window);
  501. }
  502. void WSClientConnection::handle(const WindowServer::WM_PopupWindowMenu& message)
  503. {
  504. auto* client = WSClientConnection::from_client_id(message.client_id());
  505. if (!client) {
  506. did_misbehave("WM_PopupWindowMenu: Bad client ID");
  507. return;
  508. }
  509. auto it = client->m_windows.find(message.window_id());
  510. if (it == client->m_windows.end()) {
  511. did_misbehave("WM_PopupWindowMenu: Bad window ID");
  512. return;
  513. }
  514. auto& window = *(*it).value;
  515. window.popup_window_menu(message.screen_position());
  516. }
  517. void WSClientConnection::handle(const WindowServer::WM_StartWindowResize& request)
  518. {
  519. auto* client = WSClientConnection::from_client_id(request.client_id());
  520. if (!client) {
  521. did_misbehave("WM_StartWindowResize: Bad client ID");
  522. return;
  523. }
  524. auto it = client->m_windows.find(request.window_id());
  525. if (it == client->m_windows.end()) {
  526. did_misbehave("WM_StartWindowResize: Bad window ID");
  527. return;
  528. }
  529. auto& window = *(*it).value;
  530. // FIXME: We are cheating a bit here by using the current cursor location and hard-coding the left button.
  531. // Maybe the client should be allowed to specify what initiated this request?
  532. WSWindowManager::the().start_window_resize(window, WSScreen::the().cursor_location(), MouseButton::Left);
  533. }
  534. void WSClientConnection::handle(const WindowServer::WM_SetWindowMinimized& message)
  535. {
  536. auto* client = WSClientConnection::from_client_id(message.client_id());
  537. if (!client) {
  538. did_misbehave("WM_SetWindowMinimized: Bad client ID");
  539. return;
  540. }
  541. auto it = client->m_windows.find(message.window_id());
  542. if (it == client->m_windows.end()) {
  543. did_misbehave("WM_SetWindowMinimized: Bad window ID");
  544. return;
  545. }
  546. auto& window = *(*it).value;
  547. window.set_minimized(message.minimized());
  548. }
  549. OwnPtr<WindowServer::GreetResponse> WSClientConnection::handle(const WindowServer::Greet&)
  550. {
  551. return make<WindowServer::GreetResponse>(client_id(), WSScreen::the().rect(), current_system_theme_buffer_id());
  552. }
  553. bool WSClientConnection::is_showing_modal_window() const
  554. {
  555. for (auto& it : m_windows) {
  556. auto& window = *it.value;
  557. if (window.is_visible() && window.is_modal())
  558. return true;
  559. }
  560. return false;
  561. }
  562. void WSClientConnection::handle(const WindowServer::WM_SetWindowTaskbarRect& message)
  563. {
  564. auto* client = WSClientConnection::from_client_id(message.client_id());
  565. if (!client) {
  566. did_misbehave("WM_SetWindowTaskbarRect: Bad client ID");
  567. return;
  568. }
  569. auto it = client->m_windows.find(message.window_id());
  570. if (it == client->m_windows.end()) {
  571. did_misbehave("WM_SetWindowTaskbarRect: Bad window ID");
  572. return;
  573. }
  574. auto& window = *(*it).value;
  575. window.set_taskbar_rect(message.rect());
  576. }
  577. OwnPtr<WindowServer::StartDragResponse> WSClientConnection::handle(const WindowServer::StartDrag& message)
  578. {
  579. auto& wm = WSWindowManager::the();
  580. if (wm.dnd_client())
  581. return make<WindowServer::StartDragResponse>(false);
  582. RefPtr<GraphicsBitmap> bitmap;
  583. if (message.bitmap_id() != -1) {
  584. auto shared_buffer = SharedBuffer::create_from_shared_buffer_id(message.bitmap_id());
  585. ssize_t size_in_bytes = message.bitmap_size().area() * sizeof(RGBA32);
  586. if (size_in_bytes > shared_buffer->size()) {
  587. did_misbehave("SetAppletBackingStore: Shared buffer is too small for applet size");
  588. return nullptr;
  589. }
  590. bitmap = GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *shared_buffer, message.bitmap_size());
  591. }
  592. wm.start_dnd_drag(*this, message.text(), bitmap, message.data_type(), message.data());
  593. return make<WindowServer::StartDragResponse>(true);
  594. }