WSWindowManager.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include "Process.h"
  6. #include "MemoryManager.h"
  7. #include <Kernel/ProcFS.h>
  8. #include <SharedGraphics/Font.h>
  9. #include <SharedGraphics/Painter.h>
  10. #include <SharedGraphics/CharacterBitmap.h>
  11. #include <AK/StdLibExtras.h>
  12. //#define DEBUG_COUNTERS
  13. //#define DEBUG_WID_IN_TITLE_BAR
  14. static const int window_titlebar_height = 16;
  15. static inline Rect title_bar_rect(const Rect& window)
  16. {
  17. return {
  18. window.x() - 1,
  19. window.y() - window_titlebar_height,
  20. window.width() + 2,
  21. window_titlebar_height
  22. };
  23. }
  24. static inline Rect title_bar_text_rect(const Rect& window)
  25. {
  26. auto titlebar_rect = title_bar_rect(window);
  27. return {
  28. titlebar_rect.x() + 2,
  29. titlebar_rect.y(),
  30. titlebar_rect.width() - 4,
  31. titlebar_rect.height()
  32. };
  33. }
  34. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  35. {
  36. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  37. int close_button_margin = 1;
  38. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  39. return Rect {
  40. titlebar_inner_rect.right() - close_button_size + 1,
  41. titlebar_inner_rect.top() + close_button_margin,
  42. close_button_size,
  43. close_button_size - 1
  44. };
  45. }
  46. static inline Rect border_window_rect(const Rect& window)
  47. {
  48. auto titlebar_rect = title_bar_rect(window);
  49. return { titlebar_rect.x() - 1,
  50. titlebar_rect.y() - 1,
  51. titlebar_rect.width() + 2,
  52. window_titlebar_height + window.height() + 3
  53. };
  54. }
  55. static inline Rect outer_window_rect(const Rect& window)
  56. {
  57. auto rect = border_window_rect(window);
  58. rect.inflate(2, 2);
  59. return rect;
  60. }
  61. static WSWindowManager* s_the;
  62. WSWindowManager& WSWindowManager::the()
  63. {
  64. if (!s_the)
  65. s_the = new WSWindowManager;
  66. return *s_the;
  67. }
  68. void WSWindowManager::initialize()
  69. {
  70. s_the = nullptr;
  71. }
  72. static const char* cursor_bitmap_inner_ascii = {
  73. " # "
  74. " ## "
  75. " ### "
  76. " #### "
  77. " ##### "
  78. " ###### "
  79. " ####### "
  80. " ######## "
  81. " ######### "
  82. " ########## "
  83. " ###### "
  84. " ## ## "
  85. " # ## "
  86. " ## "
  87. " ## "
  88. " ## "
  89. " "
  90. };
  91. static const char* cursor_bitmap_outer_ascii = {
  92. "## "
  93. "# # "
  94. "# # "
  95. "# # "
  96. "# # "
  97. "# # "
  98. "# # "
  99. "# # "
  100. "# # "
  101. "# # "
  102. "# #### "
  103. "# ## # "
  104. "# # # # "
  105. "## # # "
  106. " # # "
  107. " # # "
  108. " ## "
  109. };
  110. WSWindowManager::WSWindowManager()
  111. : m_screen(WSScreen::the())
  112. , m_screen_rect(m_screen.rect())
  113. {
  114. #ifndef DEBUG_COUNTERS
  115. (void)m_compose_count;
  116. (void)m_flush_count;
  117. #endif
  118. auto size = m_screen_rect.size();
  119. m_front_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(0));
  120. auto* region = current->allocate_region(LinearAddress(), size.width() * size.height() * sizeof(RGBA32), "BackBitmap", true, true, true);
  121. m_back_bitmap = GraphicsBitmap::create_wrapper(m_screen_rect.size(), (RGBA32*)region->laddr().get());
  122. m_front_painter = make<Painter>(*m_front_bitmap);
  123. m_back_painter = make<Painter>(*m_back_bitmap);
  124. m_font = Font::default_font();
  125. m_front_painter->set_font(font());
  126. m_back_painter->set_font(font());
  127. m_background_color = Color(50, 50, 50);
  128. m_active_window_border_color = Color(110, 34, 9);
  129. m_active_window_border_color2 = Color(244, 202, 158);
  130. m_active_window_title_color = Color::White;
  131. m_inactive_window_border_color = Color(128, 128, 128);
  132. m_inactive_window_border_color2 = Color(192, 192, 192);
  133. m_inactive_window_title_color = Color(213, 208, 199);
  134. m_dragging_window_border_color = Color(161, 50, 13);
  135. m_dragging_window_border_color2 = Color(250, 220, 187);
  136. m_dragging_window_title_color = Color::White;
  137. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  138. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  139. ProcFS::the().add_sys_bool("wm_flash_flush", &m_flash_flush);
  140. invalidate();
  141. compose();
  142. }
  143. WSWindowManager::~WSWindowManager()
  144. {
  145. }
  146. static const char* s_close_button_bitmap_data = {
  147. "## ##"
  148. "### ###"
  149. " ###### "
  150. " #### "
  151. " ## "
  152. " #### "
  153. " ###### "
  154. "### ###"
  155. "## ##"
  156. };
  157. static CharacterBitmap* s_close_button_bitmap;
  158. static const int s_close_button_bitmap_width = 8;
  159. static const int s_close_button_bitmap_height = 9;
  160. void WSWindowManager::paint_window_frame(WSWindow& window)
  161. {
  162. LOCKER(m_lock);
  163. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  164. auto titlebar_rect = title_bar_rect(window.rect());
  165. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  166. auto outer_rect = outer_window_rect(window.rect());
  167. auto border_rect = border_window_rect(window.rect());
  168. auto close_button_rect = close_button_rect_for_window(window.rect());
  169. auto titlebar_title_rect = titlebar_inner_rect;
  170. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  171. Rect inner_border_rect {
  172. window.x() - 1,
  173. window.y() - 1,
  174. window.width() + 2,
  175. window.height() + 2
  176. };
  177. Color title_color;
  178. Color border_color;
  179. Color border_color2;
  180. Color middle_border_color;
  181. if (&window == m_drag_window.ptr()) {
  182. border_color = m_dragging_window_border_color;
  183. border_color2 = m_dragging_window_border_color2;
  184. title_color = m_dragging_window_title_color;
  185. middle_border_color = Color::White;
  186. } else if (&window == m_active_window.ptr()) {
  187. border_color = m_active_window_border_color;
  188. border_color2 = m_active_window_border_color2;
  189. title_color = m_active_window_title_color;
  190. middle_border_color = Color::MidGray;
  191. } else {
  192. border_color = m_inactive_window_border_color;
  193. border_color2 = m_inactive_window_border_color2;
  194. title_color = m_inactive_window_title_color;
  195. middle_border_color = Color::MidGray;
  196. }
  197. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  198. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  199. m_back_painter->draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  200. }
  201. m_back_painter->draw_rect(border_rect, middle_border_color);
  202. m_back_painter->draw_rect(outer_rect, border_color);
  203. m_back_painter->draw_rect(inner_border_rect, border_color);
  204. m_back_painter->draw_text(titlebar_title_rect, window.title(), Painter::TextAlignment::CenterLeft, title_color);
  205. if (!s_close_button_bitmap)
  206. s_close_button_bitmap = CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  207. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  208. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  209. auto x_location = close_button_rect.center();
  210. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  211. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  212. #ifdef DEBUG_WID_IN_TITLE_BAR
  213. Color metadata_color(96, 96, 96);
  214. m_back_painter->draw_text(
  215. titlebar_inner_rect,
  216. String::format("%d:%d", window.pid(), window.window_id()),
  217. Painter::TextAlignment::CenterRight,
  218. metadata_color
  219. );
  220. #endif
  221. }
  222. void WSWindowManager::add_window(WSWindow& window)
  223. {
  224. LOCKER(m_lock);
  225. m_windows.set(&window);
  226. m_windows_in_order.append(&window);
  227. if (!active_window())
  228. set_active_window(&window);
  229. }
  230. void WSWindowManager::move_to_front(WSWindow& window)
  231. {
  232. LOCKER(m_lock);
  233. if (m_windows_in_order.tail() != &window)
  234. invalidate(window);
  235. m_windows_in_order.remove(&window);
  236. m_windows_in_order.append(&window);
  237. }
  238. void WSWindowManager::remove_window(WSWindow& window)
  239. {
  240. LOCKER(m_lock);
  241. if (!m_windows.contains(&window))
  242. return;
  243. invalidate(window);
  244. m_windows.remove(&window);
  245. m_windows_in_order.remove(&window);
  246. if (!active_window() && !m_windows.is_empty())
  247. set_active_window(*m_windows.begin());
  248. }
  249. void WSWindowManager::notify_title_changed(WSWindow& window)
  250. {
  251. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  252. invalidate(outer_window_rect(window.rect()));
  253. }
  254. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  255. {
  256. printf("[WM] WSWindow %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, old_rect.x(), old_rect.y(), old_rect.width(), old_rect.height(), new_rect.x(), new_rect.y(), new_rect.width(), new_rect.height());
  257. ASSERT_INTERRUPTS_ENABLED();
  258. invalidate(outer_window_rect(old_rect));
  259. invalidate(outer_window_rect(new_rect));
  260. }
  261. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  262. {
  263. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  264. #ifdef DRAG_DEBUG
  265. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  266. #endif
  267. m_drag_window = window.make_weak_ptr();;
  268. m_drag_origin = event.position();
  269. m_drag_window_origin = window.position();
  270. m_drag_start_rect = outer_window_rect(window.rect());
  271. window.set_is_being_dragged(true);
  272. invalidate(window);
  273. return;
  274. }
  275. }
  276. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  277. {
  278. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  279. WSMessage message(WSMessage::WindowCloseRequest);
  280. window.on_message(message);
  281. return;
  282. }
  283. }
  284. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  285. {
  286. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  287. if (m_drag_window) {
  288. #ifdef DRAG_DEBUG
  289. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  290. #endif
  291. invalidate(*m_drag_window);
  292. m_drag_window->set_is_being_dragged(false);
  293. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  294. m_drag_window = nullptr;
  295. return;
  296. }
  297. }
  298. if (event.type() == WSMessage::MouseMove) {
  299. if (m_drag_window) {
  300. auto old_window_rect = m_drag_window->rect();
  301. Point pos = m_drag_window_origin;
  302. #ifdef DRAG_DEBUG
  303. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  304. #endif
  305. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  306. m_drag_window->set_position_without_repaint(pos);
  307. invalidate(outer_window_rect(old_window_rect));
  308. invalidate(outer_window_rect(m_drag_window->rect()));
  309. return;
  310. }
  311. }
  312. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  313. if (!window->global_cursor_tracking())
  314. continue;
  315. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  316. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  317. window->on_message(*local_event);
  318. }
  319. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  320. if (title_bar_rect(window->rect()).contains(event.position())) {
  321. if (event.type() == WSMessage::MouseDown) {
  322. move_to_front(*window);
  323. set_active_window(window);
  324. }
  325. if (close_button_rect_for_window(window->rect()).contains(event.position())) {
  326. handle_close_button_mouse_event(*window, event);
  327. return;
  328. }
  329. handle_titlebar_mouse_event(*window, event);
  330. return;
  331. }
  332. if (window->rect().contains(event.position())) {
  333. if (event.type() == WSMessage::MouseDown) {
  334. move_to_front(*window);
  335. set_active_window(window);
  336. }
  337. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  338. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  339. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  340. window->on_message(*local_event);
  341. return;
  342. }
  343. }
  344. }
  345. void WSWindowManager::compose()
  346. {
  347. LOCKER(m_lock);
  348. auto dirty_rects = move(m_dirty_rects);
  349. #ifdef DEBUG_COUNTERS
  350. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  351. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  352. #endif
  353. auto any_window_contains_rect = [this] (const Rect& r) {
  354. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  355. if (outer_window_rect(window->rect()).contains(r))
  356. return true;
  357. }
  358. return false;
  359. };
  360. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  361. auto window_rect = outer_window_rect(window.rect());
  362. for (auto& dirty_rect : dirty_rects) {
  363. if (dirty_rect.intersects(window_rect))
  364. return true;
  365. }
  366. return false;
  367. };
  368. for (auto& dirty_rect : dirty_rects) {
  369. if (any_window_contains_rect(dirty_rect)) {
  370. continue;
  371. }
  372. //dbgprintf("Repaint root %d,%d %dx%d\n", dirty_rect.x(), dirty_rect.y(), dirty_rect.width(), dirty_rect.height());
  373. m_back_painter->fill_rect(dirty_rect, m_background_color);
  374. }
  375. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  376. WSWindowLocker locker(*window);
  377. RetainPtr<GraphicsBitmap> backing = window->backing();
  378. if (!backing)
  379. continue;
  380. if (!any_dirty_rect_intersects_window(*window))
  381. continue;
  382. for (auto& dirty_rect : dirty_rects) {
  383. m_back_painter->set_clip_rect(dirty_rect);
  384. paint_window_frame(*window);
  385. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  386. if (dirty_rect_in_window_coordinates.is_empty())
  387. continue;
  388. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  389. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  390. auto dst = window->position();
  391. dst.move_by(dirty_rect_in_window_coordinates.location());
  392. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  393. m_back_painter->clear_clip_rect();
  394. }
  395. m_back_painter->clear_clip_rect();
  396. }
  397. for (auto& r : dirty_rects)
  398. flush(r);
  399. draw_cursor();
  400. }
  401. void WSWindowManager::draw_cursor()
  402. {
  403. ASSERT_INTERRUPTS_ENABLED();
  404. LOCKER(m_lock);
  405. auto cursor_location = m_screen.cursor_location();
  406. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  407. flush(m_last_cursor_rect.united(cursor_rect));
  408. Color inner_color = Color::White;
  409. Color outer_color = Color::Black;
  410. if (m_screen.left_mouse_button_pressed())
  411. swap(inner_color, outer_color);
  412. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  413. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  414. m_last_cursor_rect = cursor_rect;
  415. }
  416. void WSWindowManager::on_message(WSMessage& message)
  417. {
  418. ASSERT_INTERRUPTS_ENABLED();
  419. LOCKER(m_lock);
  420. if (message.is_mouse_event())
  421. return process_mouse_event(static_cast<WSMouseEvent&>(message));
  422. if (message.is_key_event()) {
  423. // FIXME: This is a good place to hook key events globally. :)
  424. if (m_active_window)
  425. return m_active_window->on_message(message);
  426. return;
  427. }
  428. if (message.type() == WSMessage::WM_DeferredCompose) {
  429. m_pending_compose_event = false;
  430. compose();
  431. return;
  432. }
  433. }
  434. void WSWindowManager::set_active_window(WSWindow* window)
  435. {
  436. LOCKER(m_lock);
  437. if (window == m_active_window.ptr())
  438. return;
  439. if (auto* previously_active_window = m_active_window.ptr()) {
  440. WSMessageLoop::the().post_message(previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  441. invalidate(*previously_active_window);
  442. }
  443. m_active_window = window->make_weak_ptr();
  444. if (m_active_window) {
  445. WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
  446. invalidate(*m_active_window);
  447. }
  448. }
  449. void WSWindowManager::invalidate()
  450. {
  451. LOCKER(m_lock);
  452. m_dirty_rects.clear_with_capacity();
  453. m_dirty_rects.append(m_screen_rect);
  454. }
  455. void WSWindowManager::invalidate(const Rect& a_rect)
  456. {
  457. LOCKER(m_lock);
  458. auto rect = Rect::intersection(a_rect, m_screen_rect);
  459. if (rect.is_empty())
  460. return;
  461. for (auto& r : m_dirty_rects) {
  462. if (r.contains(rect))
  463. return;
  464. if (r.intersects(rect)) {
  465. // Unite with the existing dirty rect.
  466. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  467. r = r.united(rect);
  468. return;
  469. }
  470. }
  471. m_dirty_rects.append(rect);
  472. if (!m_pending_compose_event) {
  473. ASSERT_INTERRUPTS_ENABLED();
  474. WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  475. m_pending_compose_event = true;
  476. }
  477. }
  478. void WSWindowManager::invalidate(const WSWindow& window)
  479. {
  480. ASSERT_INTERRUPTS_ENABLED();
  481. LOCKER(m_lock);
  482. invalidate(outer_window_rect(window.rect()));
  483. }
  484. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  485. {
  486. if (rect.is_empty()) {
  487. invalidate(window);
  488. return;
  489. }
  490. ASSERT_INTERRUPTS_ENABLED();
  491. LOCKER(m_lock);
  492. auto outer_rect = outer_window_rect(window.rect());
  493. auto inner_rect = rect;
  494. inner_rect.move_by(window.position());
  495. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  496. inner_rect.intersect(outer_rect);
  497. invalidate(inner_rect);
  498. }
  499. void WSWindowManager::flush(const Rect& a_rect)
  500. {
  501. auto rect = Rect::intersection(a_rect, m_screen_rect);
  502. #ifdef DEBUG_COUNTERS
  503. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  504. #endif
  505. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  506. const RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  507. size_t pitch = m_back_bitmap->pitch();
  508. if (m_flash_flush)
  509. m_front_painter->fill_rect(rect, Color::Yellow);
  510. for (int y = 0; y < rect.height(); ++y) {
  511. fast_dword_copy(front_ptr, back_ptr, rect.width());
  512. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  513. back_ptr = (const RGBA32*)((const byte*)back_ptr + pitch);
  514. }
  515. }