WSWindowManager.cpp 19 KB

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