GWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #include <AK/HashMap.h>
  2. #include <AK/JsonObject.h>
  3. #include <AK/NeverDestroyed.h>
  4. #include <LibC/SharedBuffer.h>
  5. #include <LibC/stdio.h>
  6. #include <LibC/stdlib.h>
  7. #include <LibC/unistd.h>
  8. #include <LibDraw/GraphicsBitmap.h>
  9. #include <LibGUI/GApplication.h>
  10. #include <LibGUI/GEvent.h>
  11. #include <LibGUI/GPainter.h>
  12. #include <LibGUI/GWidget.h>
  13. #include <LibGUI/GWindow.h>
  14. #include <LibGUI/GWindowServerConnection.h>
  15. //#define UPDATE_COALESCING_DEBUG
  16. static NeverDestroyed<HashTable<GWindow*>> all_windows;
  17. static NeverDestroyed<HashMap<int, GWindow*>> reified_windows;
  18. GWindow* GWindow::from_window_id(int window_id)
  19. {
  20. auto it = reified_windows->find(window_id);
  21. if (it != reified_windows->end())
  22. return (*it).value;
  23. return nullptr;
  24. }
  25. GWindow::GWindow(CObject* parent)
  26. : CObject(parent)
  27. {
  28. all_windows->set(this);
  29. m_rect_when_windowless = { 100, 400, 140, 140 };
  30. m_title_when_windowless = "GWindow";
  31. }
  32. GWindow::~GWindow()
  33. {
  34. all_windows->remove(this);
  35. hide();
  36. }
  37. void GWindow::close()
  38. {
  39. hide();
  40. }
  41. void GWindow::move_to_front()
  42. {
  43. if (!m_window_id)
  44. return;
  45. GWindowServerConnection::the().send_sync<WindowServer::MoveWindowToFront>(m_window_id);
  46. }
  47. void GWindow::show()
  48. {
  49. if (m_window_id)
  50. return;
  51. auto response = GWindowServerConnection::the().send_sync<WindowServer::CreateWindow>(
  52. m_rect_when_windowless,
  53. m_has_alpha_channel,
  54. m_modal,
  55. m_resizable,
  56. m_fullscreen,
  57. m_show_titlebar,
  58. m_opacity_when_windowless,
  59. m_background_color,
  60. m_base_size,
  61. m_size_increment,
  62. (i32)m_window_type,
  63. m_title_when_windowless);
  64. m_window_id = response->window_id();
  65. apply_icon();
  66. reified_windows->set(m_window_id, this);
  67. GApplication::the().did_create_window({});
  68. update();
  69. }
  70. void GWindow::hide()
  71. {
  72. if (!m_window_id)
  73. return;
  74. reified_windows->remove(m_window_id);
  75. GWindowServerConnection::the().send_sync<WindowServer::DestroyWindow>(m_window_id);
  76. m_window_id = 0;
  77. m_pending_paint_event_rects.clear();
  78. m_back_bitmap = nullptr;
  79. m_front_bitmap = nullptr;
  80. bool app_has_visible_windows = false;
  81. for (auto& window : *all_windows) {
  82. if (window->is_visible()) {
  83. app_has_visible_windows = true;
  84. break;
  85. }
  86. }
  87. if (!app_has_visible_windows)
  88. GApplication::the().did_delete_last_window({});
  89. }
  90. void GWindow::set_title(const StringView& title)
  91. {
  92. m_title_when_windowless = title;
  93. if (!m_window_id)
  94. return;
  95. GWindowServerConnection::the().send_sync<WindowServer::SetWindowTitle>(m_window_id, title);
  96. }
  97. String GWindow::title() const
  98. {
  99. if (!m_window_id)
  100. return m_title_when_windowless;
  101. return GWindowServerConnection::the().send_sync<WindowServer::GetWindowTitle>(m_window_id)->title();
  102. }
  103. Rect GWindow::rect() const
  104. {
  105. if (!m_window_id)
  106. return m_rect_when_windowless;
  107. return GWindowServerConnection::the().send_sync<WindowServer::GetWindowRect>(m_window_id)->rect();
  108. }
  109. void GWindow::set_rect(const Rect& a_rect)
  110. {
  111. m_rect_when_windowless = a_rect;
  112. if (!m_window_id) {
  113. if (m_main_widget)
  114. m_main_widget->resize(m_rect_when_windowless.size());
  115. return;
  116. }
  117. GWindowServerConnection::the().send_sync<WindowServer::SetWindowRect>(m_window_id, a_rect);
  118. if (m_back_bitmap && m_back_bitmap->size() != a_rect.size())
  119. m_back_bitmap = nullptr;
  120. if (m_front_bitmap && m_front_bitmap->size() != a_rect.size())
  121. m_front_bitmap = nullptr;
  122. if (m_main_widget)
  123. m_main_widget->resize(a_rect.size());
  124. }
  125. void GWindow::set_window_type(GWindowType window_type)
  126. {
  127. m_window_type = window_type;
  128. }
  129. void GWindow::set_override_cursor(GStandardCursor cursor)
  130. {
  131. if (!m_window_id)
  132. return;
  133. GWindowServerConnection::the().send_sync<WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor);
  134. }
  135. void GWindow::event(CEvent& event)
  136. {
  137. if (event.type() == GEvent::Drop) {
  138. auto& drop_event = static_cast<GDropEvent&>(event);
  139. if (!m_main_widget)
  140. return;
  141. auto result = m_main_widget->hit_test(drop_event.position());
  142. auto local_event = make<GDropEvent>(result.local_position, drop_event.text(), drop_event.data_type(), drop_event.data());
  143. ASSERT(result.widget);
  144. return result.widget->dispatch_event(*local_event, this);
  145. }
  146. if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseDoubleClick || event.type() == GEvent::MouseMove || event.type() == GEvent::MouseWheel) {
  147. auto& mouse_event = static_cast<GMouseEvent&>(event);
  148. if (m_global_cursor_tracking_widget) {
  149. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  150. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  151. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  152. m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
  153. return;
  154. }
  155. if (m_automatic_cursor_tracking_widget) {
  156. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  157. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  158. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  159. m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
  160. if (mouse_event.buttons() == 0)
  161. m_automatic_cursor_tracking_widget = nullptr;
  162. return;
  163. }
  164. if (!m_main_widget)
  165. return;
  166. auto result = m_main_widget->hit_test(mouse_event.position());
  167. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  168. ASSERT(result.widget);
  169. set_hovered_widget(result.widget);
  170. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  171. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  172. if (result.widget != m_global_cursor_tracking_widget.ptr())
  173. return result.widget->dispatch_event(*local_event, this);
  174. return;
  175. }
  176. if (event.type() == GEvent::MultiPaint) {
  177. if (!m_window_id)
  178. return;
  179. if (!m_main_widget)
  180. return;
  181. auto& paint_event = static_cast<GMultiPaintEvent&>(event);
  182. auto rects = paint_event.rects();
  183. ASSERT(!rects.is_empty());
  184. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  185. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  186. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  187. // effort on painting into an undersized bitmap that will be thrown away anyway.
  188. m_back_bitmap = nullptr;
  189. }
  190. bool created_new_backing_store = !m_back_bitmap;
  191. if (!m_back_bitmap) {
  192. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  193. } else if (m_double_buffering_enabled) {
  194. bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile();
  195. if (!still_has_pixels) {
  196. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  197. created_new_backing_store = true;
  198. }
  199. }
  200. auto rect = rects.first();
  201. if (rect.is_empty() || created_new_backing_store) {
  202. rects.clear();
  203. rects.append({ {}, paint_event.window_size() });
  204. }
  205. for (auto& rect : rects)
  206. m_main_widget->dispatch_event(*make<GPaintEvent>(rect), this);
  207. if (m_double_buffering_enabled)
  208. flip(rects);
  209. else if (created_new_backing_store)
  210. set_current_backing_bitmap(*m_back_bitmap, true);
  211. if (m_window_id) {
  212. Vector<Rect> rects_to_send;
  213. for (auto& r : rects)
  214. rects_to_send.append(r);
  215. GWindowServerConnection::the().post_message(WindowServer::DidFinishPainting(m_window_id, rects_to_send));
  216. }
  217. return;
  218. }
  219. if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
  220. if (m_focused_widget)
  221. return m_focused_widget->dispatch_event(event, this);
  222. if (m_main_widget)
  223. return m_main_widget->dispatch_event(event, this);
  224. return;
  225. }
  226. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  227. m_is_active = event.type() == GEvent::WindowBecameActive;
  228. if (m_main_widget)
  229. m_main_widget->dispatch_event(event, this);
  230. if (m_focused_widget)
  231. m_focused_widget->update();
  232. return;
  233. }
  234. if (event.type() == GEvent::WindowCloseRequest) {
  235. if (on_close_request) {
  236. if (on_close_request() == GWindow::CloseRequestDecision::StayOpen)
  237. return;
  238. }
  239. close();
  240. return;
  241. }
  242. if (event.type() == GEvent::WindowLeft) {
  243. set_hovered_widget(nullptr);
  244. return;
  245. }
  246. if (event.type() == GEvent::Resize) {
  247. auto new_size = static_cast<GResizeEvent&>(event).size();
  248. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  249. m_back_bitmap = nullptr;
  250. if (!m_pending_paint_event_rects.is_empty()) {
  251. m_pending_paint_event_rects.clear_with_capacity();
  252. m_pending_paint_event_rects.append({ {}, new_size });
  253. }
  254. m_rect_when_windowless = { {}, new_size };
  255. m_main_widget->set_relative_rect({ {}, new_size });
  256. return;
  257. }
  258. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  259. return wm_event(static_cast<GWMEvent&>(event));
  260. CObject::event(event);
  261. }
  262. bool GWindow::is_visible() const
  263. {
  264. return m_window_id != 0;
  265. }
  266. void GWindow::update(const Rect& a_rect)
  267. {
  268. if (!m_window_id)
  269. return;
  270. for (auto& pending_rect : m_pending_paint_event_rects) {
  271. if (pending_rect.contains(a_rect)) {
  272. #ifdef UPDATE_COALESCING_DEBUG
  273. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  274. #endif
  275. return;
  276. }
  277. }
  278. if (m_pending_paint_event_rects.is_empty()) {
  279. deferred_invoke([this](auto&) {
  280. auto rects = move(m_pending_paint_event_rects);
  281. if (rects.is_empty())
  282. return;
  283. Vector<Rect> rects_to_send;
  284. for (auto& r : rects)
  285. rects_to_send.append(r);
  286. GWindowServerConnection::the().post_message(WindowServer::InvalidateRect(m_window_id, rects_to_send));
  287. });
  288. }
  289. m_pending_paint_event_rects.append(a_rect);
  290. }
  291. void GWindow::set_main_widget(GWidget* widget)
  292. {
  293. if (m_main_widget == widget)
  294. return;
  295. if (m_main_widget)
  296. remove_child(*m_main_widget);
  297. m_main_widget = widget;
  298. if (m_main_widget) {
  299. add_child(*widget);
  300. auto new_window_rect = rect();
  301. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  302. new_window_rect.set_width(m_main_widget->preferred_size().width());
  303. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  304. new_window_rect.set_height(m_main_widget->preferred_size().height());
  305. set_rect(new_window_rect);
  306. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  307. m_main_widget->set_window(this);
  308. if (m_main_widget->accepts_focus())
  309. m_main_widget->set_focus(true);
  310. }
  311. update();
  312. }
  313. void GWindow::set_focused_widget(GWidget* widget)
  314. {
  315. if (m_focused_widget == widget)
  316. return;
  317. if (m_focused_widget) {
  318. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  319. m_focused_widget->update();
  320. }
  321. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  322. if (m_focused_widget) {
  323. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  324. m_focused_widget->update();
  325. }
  326. }
  327. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  328. {
  329. if (widget == m_global_cursor_tracking_widget)
  330. return;
  331. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  332. }
  333. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  334. {
  335. if (widget == m_automatic_cursor_tracking_widget)
  336. return;
  337. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  338. }
  339. void GWindow::set_has_alpha_channel(bool value)
  340. {
  341. if (m_has_alpha_channel == value)
  342. return;
  343. m_has_alpha_channel = value;
  344. if (!m_window_id)
  345. return;
  346. m_pending_paint_event_rects.clear();
  347. m_back_bitmap = nullptr;
  348. m_front_bitmap = nullptr;
  349. GWindowServerConnection::the().send_sync<WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  350. update();
  351. }
  352. void GWindow::set_double_buffering_enabled(bool value)
  353. {
  354. ASSERT(!m_window_id);
  355. m_double_buffering_enabled = value;
  356. }
  357. void GWindow::set_opacity(float opacity)
  358. {
  359. m_opacity_when_windowless = opacity;
  360. if (!m_window_id)
  361. return;
  362. GWindowServerConnection::the().send_sync<WindowServer::SetWindowOpacity>(m_window_id, opacity);
  363. }
  364. void GWindow::set_hovered_widget(GWidget* widget)
  365. {
  366. if (widget == m_hovered_widget)
  367. return;
  368. if (m_hovered_widget)
  369. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  370. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  371. if (m_hovered_widget)
  372. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  373. }
  374. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  375. {
  376. GWindowServerConnection::the().send_sync<WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shared_buffer_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  377. }
  378. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  379. {
  380. swap(m_front_bitmap, m_back_bitmap);
  381. set_current_backing_bitmap(*m_front_bitmap);
  382. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  383. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  384. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  385. m_back_bitmap->shared_buffer()->set_volatile();
  386. return;
  387. }
  388. // Copy whatever was painted from the front to the back.
  389. Painter painter(*m_back_bitmap);
  390. for (auto& dirty_rect : dirty_rects)
  391. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  392. m_back_bitmap->shared_buffer()->set_volatile();
  393. }
  394. NonnullRefPtr<GraphicsBitmap> GWindow::create_shared_bitmap(GraphicsBitmap::Format format, const Size& size)
  395. {
  396. ASSERT(GWindowServerConnection::the().server_pid());
  397. ASSERT(!size.is_empty());
  398. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  399. size_t size_in_bytes = size.height() * pitch;
  400. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  401. ASSERT(shared_buffer);
  402. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  403. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  404. }
  405. NonnullRefPtr<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  406. {
  407. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  408. return create_shared_bitmap(format, size);
  409. }
  410. void GWindow::set_modal(bool modal)
  411. {
  412. ASSERT(!m_window_id);
  413. m_modal = modal;
  414. }
  415. void GWindow::wm_event(GWMEvent&)
  416. {
  417. }
  418. void GWindow::set_icon(const GraphicsBitmap* icon)
  419. {
  420. if (m_icon == icon)
  421. return;
  422. m_icon = create_shared_bitmap(GraphicsBitmap::Format::RGBA32, icon->size());
  423. {
  424. GPainter painter(*m_icon);
  425. painter.blit({ 0, 0 }, *icon, icon->rect());
  426. }
  427. apply_icon();
  428. }
  429. void GWindow::apply_icon()
  430. {
  431. if (!m_icon)
  432. return;
  433. if (!m_window_id)
  434. return;
  435. int rc = seal_shared_buffer(m_icon->shared_buffer_id());
  436. ASSERT(rc == 0);
  437. rc = share_buffer_globally(m_icon->shared_buffer_id());
  438. ASSERT(rc == 0);
  439. static bool has_set_process_icon;
  440. if (!has_set_process_icon)
  441. set_process_icon(m_icon->shared_buffer_id());
  442. GWindowServerConnection::the().send_sync<WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shared_buffer_id(), m_icon->size());
  443. }
  444. void GWindow::start_wm_resize()
  445. {
  446. GWindowServerConnection::the().post_message(WindowServer::WM_StartWindowResize(GWindowServerConnection::the().my_client_id(), m_window_id));
  447. }
  448. Vector<GWidget*> GWindow::focusable_widgets() const
  449. {
  450. if (!m_main_widget)
  451. return {};
  452. Vector<GWidget*> collected_widgets;
  453. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  454. if (widget.accepts_focus())
  455. collected_widgets.append(&widget);
  456. widget.for_each_child_widget([&](auto& child) {
  457. if (!child.is_visible())
  458. return IterationDecision::Continue;
  459. if (!child.is_enabled())
  460. return IterationDecision::Continue;
  461. collect_focusable_widgets(child);
  462. return IterationDecision::Continue;
  463. });
  464. };
  465. collect_focusable_widgets(const_cast<GWidget&>(*m_main_widget));
  466. return collected_widgets;
  467. }
  468. void GWindow::save_to(AK::JsonObject& json)
  469. {
  470. json.set("title", title());
  471. json.set("visible", is_visible());
  472. json.set("active", is_active());
  473. json.set("resizable", is_resizable());
  474. json.set("fullscreen", is_fullscreen());
  475. json.set("rect", rect().to_string());
  476. json.set("base_size", base_size().to_string());
  477. json.set("size_increment", size_increment().to_string());
  478. CObject::save_to(json);
  479. }
  480. void GWindow::set_fullscreen(bool fullscreen)
  481. {
  482. if (m_fullscreen == fullscreen)
  483. return;
  484. m_fullscreen = fullscreen;
  485. if (!m_window_id)
  486. return;
  487. GWindowServerConnection::the().send_sync<WindowServer::SetFullscreen>(m_window_id, fullscreen);
  488. }
  489. void GWindow::schedule_relayout()
  490. {
  491. if (m_layout_pending)
  492. return;
  493. m_layout_pending = true;
  494. deferred_invoke([this](auto&) {
  495. if (main_widget())
  496. main_widget()->do_layout();
  497. update();
  498. m_layout_pending = false;
  499. });
  500. }
  501. void GWindow::update_all_windows(Badge<GWindowServerConnection>)
  502. {
  503. for (auto* window : *all_windows) {
  504. window->update();
  505. }
  506. }
  507. void GWindow::notify_state_changed(Badge<GWindowServerConnection>, bool minimized)
  508. {
  509. // When double buffering is enabled, minimization means we can mark the front bitmap volatile (in addition to the back bitmap.)
  510. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  511. RefPtr<GraphicsBitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap;
  512. if (!bitmap)
  513. return;
  514. if (minimized) {
  515. bitmap->shared_buffer()->set_volatile();
  516. } else {
  517. if (!bitmap->shared_buffer()->set_nonvolatile())
  518. bitmap = nullptr;
  519. }
  520. }