GWindow.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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()
  267. {
  268. update({ 0, 0, width(), height() });
  269. }
  270. void GWindow::update(const Rect& a_rect)
  271. {
  272. if (!m_window_id)
  273. return;
  274. for (auto& pending_rect : m_pending_paint_event_rects) {
  275. if (pending_rect.contains(a_rect)) {
  276. #ifdef UPDATE_COALESCING_DEBUG
  277. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  278. #endif
  279. return;
  280. }
  281. }
  282. if (m_pending_paint_event_rects.is_empty()) {
  283. deferred_invoke([this](auto&) {
  284. auto rects = move(m_pending_paint_event_rects);
  285. if (rects.is_empty())
  286. return;
  287. Vector<Rect> rects_to_send;
  288. for (auto& r : rects)
  289. rects_to_send.append(r);
  290. GWindowServerConnection::the().post_message(WindowServer::InvalidateRect(m_window_id, rects_to_send));
  291. });
  292. }
  293. m_pending_paint_event_rects.append(a_rect);
  294. }
  295. void GWindow::set_main_widget(GWidget* widget)
  296. {
  297. if (m_main_widget == widget)
  298. return;
  299. if (m_main_widget)
  300. remove_child(*m_main_widget);
  301. m_main_widget = widget;
  302. if (m_main_widget) {
  303. add_child(*widget);
  304. auto new_window_rect = rect();
  305. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  306. new_window_rect.set_width(m_main_widget->preferred_size().width());
  307. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  308. new_window_rect.set_height(m_main_widget->preferred_size().height());
  309. set_rect(new_window_rect);
  310. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  311. m_main_widget->set_window(this);
  312. if (m_main_widget->accepts_focus())
  313. m_main_widget->set_focus(true);
  314. }
  315. update();
  316. }
  317. void GWindow::set_focused_widget(GWidget* widget)
  318. {
  319. if (m_focused_widget == widget)
  320. return;
  321. if (m_focused_widget) {
  322. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  323. m_focused_widget->update();
  324. }
  325. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  326. if (m_focused_widget) {
  327. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  328. m_focused_widget->update();
  329. }
  330. }
  331. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  332. {
  333. if (widget == m_global_cursor_tracking_widget)
  334. return;
  335. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  336. }
  337. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  338. {
  339. if (widget == m_automatic_cursor_tracking_widget)
  340. return;
  341. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  342. }
  343. void GWindow::set_has_alpha_channel(bool value)
  344. {
  345. if (m_has_alpha_channel == value)
  346. return;
  347. m_has_alpha_channel = value;
  348. if (!m_window_id)
  349. return;
  350. m_pending_paint_event_rects.clear();
  351. m_back_bitmap = nullptr;
  352. m_front_bitmap = nullptr;
  353. GWindowServerConnection::the().send_sync<WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  354. update();
  355. }
  356. void GWindow::set_double_buffering_enabled(bool value)
  357. {
  358. ASSERT(!m_window_id);
  359. m_double_buffering_enabled = value;
  360. }
  361. void GWindow::set_opacity(float opacity)
  362. {
  363. m_opacity_when_windowless = opacity;
  364. if (!m_window_id)
  365. return;
  366. GWindowServerConnection::the().send_sync<WindowServer::SetWindowOpacity>(m_window_id, opacity);
  367. }
  368. void GWindow::set_hovered_widget(GWidget* widget)
  369. {
  370. if (widget == m_hovered_widget)
  371. return;
  372. if (m_hovered_widget)
  373. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  374. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  375. if (m_hovered_widget)
  376. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  377. }
  378. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  379. {
  380. GWindowServerConnection::the().send_sync<WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shared_buffer_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  381. }
  382. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  383. {
  384. swap(m_front_bitmap, m_back_bitmap);
  385. set_current_backing_bitmap(*m_front_bitmap);
  386. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  387. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  388. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  389. m_back_bitmap->shared_buffer()->set_volatile();
  390. return;
  391. }
  392. // Copy whatever was painted from the front to the back.
  393. Painter painter(*m_back_bitmap);
  394. for (auto& dirty_rect : dirty_rects)
  395. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  396. m_back_bitmap->shared_buffer()->set_volatile();
  397. }
  398. NonnullRefPtr<GraphicsBitmap> GWindow::create_shared_bitmap(GraphicsBitmap::Format format, const Size& size)
  399. {
  400. ASSERT(GWindowServerConnection::the().server_pid());
  401. ASSERT(!size.is_empty());
  402. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  403. size_t size_in_bytes = size.height() * pitch;
  404. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  405. ASSERT(shared_buffer);
  406. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  407. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  408. }
  409. NonnullRefPtr<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  410. {
  411. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  412. return create_shared_bitmap(format, size);
  413. }
  414. void GWindow::set_modal(bool modal)
  415. {
  416. ASSERT(!m_window_id);
  417. m_modal = modal;
  418. }
  419. void GWindow::wm_event(GWMEvent&)
  420. {
  421. }
  422. void GWindow::set_icon(const GraphicsBitmap* icon)
  423. {
  424. if (m_icon == icon)
  425. return;
  426. m_icon = create_shared_bitmap(GraphicsBitmap::Format::RGBA32, icon->size());
  427. {
  428. GPainter painter(*m_icon);
  429. painter.blit({ 0, 0 }, *icon, icon->rect());
  430. }
  431. apply_icon();
  432. }
  433. void GWindow::apply_icon()
  434. {
  435. if (!m_icon)
  436. return;
  437. if (!m_window_id)
  438. return;
  439. int rc = seal_shared_buffer(m_icon->shared_buffer_id());
  440. ASSERT(rc == 0);
  441. rc = share_buffer_globally(m_icon->shared_buffer_id());
  442. ASSERT(rc == 0);
  443. static bool has_set_process_icon;
  444. if (!has_set_process_icon)
  445. set_process_icon(m_icon->shared_buffer_id());
  446. GWindowServerConnection::the().send_sync<WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shared_buffer_id(), m_icon->size());
  447. }
  448. void GWindow::start_wm_resize()
  449. {
  450. GWindowServerConnection::the().post_message(WindowServer::WM_StartWindowResize(GWindowServerConnection::the().my_client_id(), m_window_id));
  451. }
  452. Vector<GWidget*> GWindow::focusable_widgets() const
  453. {
  454. if (!m_main_widget)
  455. return {};
  456. Vector<GWidget*> collected_widgets;
  457. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  458. if (widget.accepts_focus())
  459. collected_widgets.append(&widget);
  460. widget.for_each_child_widget([&](auto& child) {
  461. if (!child.is_visible())
  462. return IterationDecision::Continue;
  463. if (!child.is_enabled())
  464. return IterationDecision::Continue;
  465. collect_focusable_widgets(child);
  466. return IterationDecision::Continue;
  467. });
  468. };
  469. collect_focusable_widgets(const_cast<GWidget&>(*m_main_widget));
  470. return collected_widgets;
  471. }
  472. void GWindow::save_to(AK::JsonObject& json)
  473. {
  474. json.set("title", title());
  475. json.set("visible", is_visible());
  476. json.set("active", is_active());
  477. json.set("resizable", is_resizable());
  478. json.set("fullscreen", is_fullscreen());
  479. json.set("rect", rect().to_string());
  480. json.set("base_size", base_size().to_string());
  481. json.set("size_increment", size_increment().to_string());
  482. CObject::save_to(json);
  483. }
  484. void GWindow::set_fullscreen(bool fullscreen)
  485. {
  486. if (m_fullscreen == fullscreen)
  487. return;
  488. m_fullscreen = fullscreen;
  489. if (!m_window_id)
  490. return;
  491. GWindowServerConnection::the().send_sync<WindowServer::SetFullscreen>(m_window_id, fullscreen);
  492. }
  493. void GWindow::schedule_relayout()
  494. {
  495. if (m_layout_pending)
  496. return;
  497. m_layout_pending = true;
  498. deferred_invoke([this](auto&) {
  499. if (main_widget())
  500. main_widget()->do_layout();
  501. update();
  502. m_layout_pending = false;
  503. });
  504. }
  505. void GWindow::update_all_windows(Badge<GWindowServerConnection>)
  506. {
  507. for (auto* window : *all_windows) {
  508. window->update();
  509. }
  510. }
  511. void GWindow::notify_state_changed(Badge<GWindowServerConnection>, bool minimized, bool occluded)
  512. {
  513. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  514. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  515. RefPtr<GraphicsBitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap;
  516. if (!bitmap)
  517. return;
  518. if (minimized || occluded) {
  519. bitmap->shared_buffer()->set_volatile();
  520. } else {
  521. if (!bitmap->shared_buffer()->set_nonvolatile()) {
  522. bitmap = nullptr;
  523. update();
  524. }
  525. }
  526. }