WSWindowManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSEventLoop.h"
  5. #include "WSFrameBuffer.h"
  6. #include "Process.h"
  7. #include "MemoryManager.h"
  8. #include <Widgets/Painter.h>
  9. #include <Widgets/CharacterBitmap.h>
  10. #include <AK/StdLibExtras.h>
  11. //#define DEBUG_FLUSH_YELLOW
  12. //#define DEBUG_COUNTERS
  13. static const int windowTitleBarHeight = 16;
  14. static inline Rect titleBarRectForWindow(const Rect& window)
  15. {
  16. return {
  17. window.x() - 1,
  18. window.y() - windowTitleBarHeight,
  19. window.width() + 2,
  20. windowTitleBarHeight
  21. };
  22. }
  23. static inline Rect titleBarTitleRectForWindow(const Rect& window)
  24. {
  25. auto titleBarRect = titleBarRectForWindow(window);
  26. return {
  27. titleBarRect.x() + 2,
  28. titleBarRect.y(),
  29. titleBarRect.width() - 4,
  30. titleBarRect.height()
  31. };
  32. }
  33. static inline Rect borderRectForWindow(const Rect& window)
  34. {
  35. auto titleBarRect = titleBarRectForWindow(window);
  36. return { titleBarRect.x() - 1,
  37. titleBarRect.y() - 1,
  38. titleBarRect.width() + 2,
  39. windowTitleBarHeight + window.height() + 3
  40. };
  41. }
  42. static inline Rect outerRectForWindow(const Rect& window)
  43. {
  44. auto rect = borderRectForWindow(window);
  45. rect.inflate(2, 2);
  46. return rect;
  47. }
  48. static WSWindowManager* s_the_window_manager;
  49. WSWindowManager& WSWindowManager::the()
  50. {
  51. if (!s_the_window_manager)
  52. s_the_window_manager = new WSWindowManager;
  53. return *s_the_window_manager;
  54. }
  55. void WSWindowManager::initialize()
  56. {
  57. s_the_window_manager = nullptr;
  58. }
  59. static const char* cursor_bitmap_inner_ascii = {
  60. " # "
  61. " ## "
  62. " ### "
  63. " #### "
  64. " ##### "
  65. " ###### "
  66. " ####### "
  67. " ######## "
  68. " ######### "
  69. " ########## "
  70. " ###### "
  71. " ## ## "
  72. " # ## "
  73. " ## "
  74. " ## "
  75. " ## "
  76. " "
  77. };
  78. static const char* cursor_bitmap_outer_ascii = {
  79. "## "
  80. "# # "
  81. "# # "
  82. "# # "
  83. "# # "
  84. "# # "
  85. "# # "
  86. "# # "
  87. "# # "
  88. "# # "
  89. "# #### "
  90. "# ## # "
  91. "# # # # "
  92. "## # # "
  93. " # # "
  94. " # # "
  95. " ## "
  96. };
  97. WSWindowManager::WSWindowManager()
  98. : m_framebuffer(WSFrameBuffer::the())
  99. , m_screen_rect(m_framebuffer.rect())
  100. {
  101. #ifndef DEBUG_COUNTERS
  102. (void)m_recompose_count;
  103. (void)m_flush_count;
  104. #endif
  105. auto size = m_screen_rect.size();
  106. m_front_bitmap = GraphicsBitmap::create_wrapper(size, m_framebuffer.scanline(0));
  107. auto* region = current->allocate_region(LinearAddress(), size.width() * size.height() * sizeof(RGBA32), "BackBitmap", true, true, true);
  108. m_back_bitmap = GraphicsBitmap::create_wrapper(m_screen_rect.size(), (RGBA32*)region->linearAddress.get());
  109. m_front_painter = make<Painter>(*m_front_bitmap);
  110. m_back_painter = make<Painter>(*m_back_bitmap);
  111. m_activeWindowBorderColor = Color(0, 64, 192);
  112. m_activeWindowTitleColor = Color::White;
  113. m_inactiveWindowBorderColor = Color(64, 64, 64);
  114. m_inactiveWindowTitleColor = Color::White;
  115. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  116. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  117. invalidate();
  118. compose();
  119. }
  120. WSWindowManager::~WSWindowManager()
  121. {
  122. }
  123. void WSWindowManager::paintWindowFrame(WSWindow& window)
  124. {
  125. LOCKER(m_lock);
  126. //printf("[WM] paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  127. auto titleBarRect = titleBarRectForWindow(window.rect());
  128. auto titleBarTitleRect = titleBarTitleRectForWindow(window.rect());
  129. auto outerRect = outerRectForWindow(window.rect());
  130. auto borderRect = borderRectForWindow(window.rect());
  131. Rect inner_border_rect {
  132. window.x() - 1,
  133. window.y() - 1,
  134. window.width() + 2,
  135. window.height() + 2
  136. };
  137. auto titleColor = &window == activeWindow() ? m_activeWindowTitleColor : m_inactiveWindowTitleColor;
  138. auto borderColor = &window == activeWindow() ? m_activeWindowBorderColor : m_inactiveWindowBorderColor;
  139. m_back_painter->fill_rect(titleBarRect, borderColor);
  140. m_back_painter->draw_rect(borderRect, Color::MidGray);
  141. m_back_painter->draw_rect(outerRect, borderColor);
  142. m_back_painter->draw_rect(inner_border_rect, borderColor);
  143. m_back_painter->draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, titleColor);
  144. }
  145. void WSWindowManager::addWindow(WSWindow& window)
  146. {
  147. LOCKER(m_lock);
  148. m_windows.set(&window);
  149. m_windows_in_order.append(&window);
  150. if (!activeWindow())
  151. set_active_window(&window);
  152. }
  153. void WSWindowManager::move_to_front(WSWindow& window)
  154. {
  155. LOCKER(m_lock);
  156. m_windows_in_order.remove(&window);
  157. m_windows_in_order.append(&window);
  158. }
  159. void WSWindowManager::removeWindow(WSWindow& window)
  160. {
  161. LOCKER(m_lock);
  162. if (!m_windows.contains(&window))
  163. return;
  164. invalidate(window);
  165. m_windows.remove(&window);
  166. m_windows_in_order.remove(&window);
  167. if (!activeWindow() && !m_windows.is_empty())
  168. set_active_window(*m_windows.begin());
  169. }
  170. void WSWindowManager::notifyTitleChanged(WSWindow& window)
  171. {
  172. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  173. }
  174. void WSWindowManager::notifyRectChanged(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  175. {
  176. 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());
  177. ASSERT_INTERRUPTS_ENABLED();
  178. LOCKER(m_lock);
  179. invalidate(outerRectForWindow(old_rect));
  180. invalidate(outerRectForWindow(new_rect));
  181. }
  182. void WSWindowManager::handleTitleBarMouseEvent(WSWindow& window, MouseEvent& event)
  183. {
  184. if (event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left) {
  185. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  186. m_dragWindow = window.makeWeakPtr();;
  187. m_dragOrigin = event.position();
  188. m_dragWindowOrigin = window.position();
  189. m_dragStartRect = outerRectForWindow(window.rect());
  190. window.set_is_being_dragged(true);
  191. return;
  192. }
  193. }
  194. void WSWindowManager::processMouseEvent(MouseEvent& event)
  195. {
  196. if (event.type() == WSEvent::MouseUp && event.button() == MouseButton::Left) {
  197. if (m_dragWindow) {
  198. printf("[WM] Finish dragging WSWindow{%p}\n", m_dragWindow.ptr());
  199. invalidate(m_dragStartRect);
  200. invalidate(*m_dragWindow);
  201. m_dragWindow->set_is_being_dragged(false);
  202. m_dragEndRect = outerRectForWindow(m_dragWindow->rect());
  203. m_dragWindow = nullptr;
  204. return;
  205. }
  206. }
  207. if (event.type() == WSEvent::MouseMove) {
  208. if (m_dragWindow) {
  209. auto old_window_rect = m_dragWindow->rect();
  210. Point pos = m_dragWindowOrigin;
  211. printf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_dragOrigin.x(), m_dragOrigin.y(), event.x(), event.y());
  212. pos.move_by(event.x() - m_dragOrigin.x(), event.y() - m_dragOrigin.y());
  213. m_dragWindow->set_position_without_repaint(pos);
  214. invalidate(outerRectForWindow(old_window_rect));
  215. invalidate(outerRectForWindow(m_dragWindow->rect()));
  216. return;
  217. }
  218. }
  219. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  220. if (titleBarRectForWindow(window->rect()).contains(event.position())) {
  221. if (event.type() == WSEvent::MouseDown) {
  222. move_to_front(*window);
  223. set_active_window(window);
  224. }
  225. handleTitleBarMouseEvent(*window, event);
  226. return;
  227. }
  228. if (window->rect().contains(event.position())) {
  229. if (event.type() == WSEvent::MouseDown) {
  230. move_to_front(*window);
  231. set_active_window(window);
  232. }
  233. // FIXME: Re-use the existing event instead of crafting a new one?
  234. auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
  235. window->event(*localEvent);
  236. return;
  237. }
  238. }
  239. }
  240. void WSWindowManager::compose()
  241. {
  242. LOCKER(m_lock);
  243. auto invalidated_rects = move(m_invalidated_rects);
  244. #ifdef DEBUG_COUNTERS
  245. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_recompose_count, invalidated_rects.size());
  246. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  247. #endif
  248. auto any_window_contains_rect = [this] (const Rect& r) {
  249. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  250. if (outerRectForWindow(window->rect()).contains(r))
  251. return true;
  252. }
  253. return false;
  254. };
  255. auto any_dirty_rect_intersects_window = [&invalidated_rects] (const WSWindow& window) {
  256. auto window_rect = outerRectForWindow(window.rect());
  257. for (auto& dirty_rect : invalidated_rects) {
  258. if (dirty_rect.intersects(window_rect))
  259. return true;
  260. }
  261. return false;
  262. };
  263. for (auto& r : invalidated_rects) {
  264. if (any_window_contains_rect(r))
  265. continue;
  266. //dbgprintf("Repaint root %d,%d %dx%d\n", r.x(), r.y(), r.width(), r.height());
  267. m_back_painter->fill_rect(r, Color(0, 72, 96));
  268. }
  269. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  270. if (!window->backing())
  271. continue;
  272. if (!any_dirty_rect_intersects_window(*window))
  273. continue;
  274. paintWindowFrame(*window);
  275. m_back_painter->blit(window->position(), *window->backing());
  276. }
  277. for (auto& r : invalidated_rects)
  278. flush(r);
  279. draw_cursor();
  280. }
  281. void WSWindowManager::draw_cursor()
  282. {
  283. ASSERT_INTERRUPTS_ENABLED();
  284. LOCKER(m_lock);
  285. auto cursor_location = m_framebuffer.cursor_location();
  286. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  287. flush(m_last_cursor_rect.united(cursor_rect));
  288. Color inner_color = Color::White;
  289. Color outer_color = Color::Black;
  290. if (m_framebuffer.left_mouse_button_pressed())
  291. swap(inner_color, outer_color);
  292. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  293. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  294. m_last_cursor_rect = cursor_rect;
  295. }
  296. void WSWindowManager::event(WSEvent& event)
  297. {
  298. ASSERT_INTERRUPTS_ENABLED();
  299. LOCKER(m_lock);
  300. if (event.isMouseEvent())
  301. return processMouseEvent(static_cast<MouseEvent&>(event));
  302. if (event.isKeyEvent()) {
  303. // FIXME: This is a good place to hook key events globally. :)
  304. if (m_active_window)
  305. return m_active_window->event(event);
  306. return;
  307. }
  308. if (event.type() == WSEvent::WM_Compose) {
  309. m_pending_compose_event = false;
  310. compose();
  311. return;
  312. }
  313. }
  314. void WSWindowManager::set_active_window(WSWindow* window)
  315. {
  316. LOCKER(m_lock);
  317. if (window == m_active_window.ptr())
  318. return;
  319. if (auto* previously_active_window = m_active_window.ptr()) {
  320. WSEventLoop::the().post_event(previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
  321. invalidate(*previously_active_window);
  322. }
  323. m_active_window = window->makeWeakPtr();
  324. if (m_active_window) {
  325. WSEventLoop::the().post_event(m_active_window.ptr(), make<WSEvent>(WSEvent::WindowActivated));
  326. invalidate(*m_active_window);
  327. }
  328. }
  329. void WSWindowManager::invalidate()
  330. {
  331. LOCKER(m_lock);
  332. m_invalidated_rects.clear_with_capacity();
  333. m_invalidated_rects.append(m_screen_rect);
  334. }
  335. void WSWindowManager::invalidate(const Rect& a_rect)
  336. {
  337. LOCKER(m_lock);
  338. auto rect = Rect::intersection(a_rect, m_screen_rect);
  339. if (rect.is_empty())
  340. return;
  341. for (auto& r : m_invalidated_rects) {
  342. if (r.contains(rect))
  343. return;
  344. if (r.intersects(rect)) {
  345. // Unite with the existing dirty rect.
  346. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  347. r = r.united(rect);
  348. return;
  349. }
  350. }
  351. m_invalidated_rects.append(rect);
  352. if (!m_pending_compose_event) {
  353. ASSERT_INTERRUPTS_ENABLED();
  354. WSEventLoop::the().post_event(this, make<WSEvent>(WSEvent::WM_Compose));
  355. m_pending_compose_event = true;
  356. }
  357. }
  358. void WSWindowManager::invalidate(const WSWindow& window)
  359. {
  360. ASSERT_INTERRUPTS_ENABLED();
  361. LOCKER(m_lock);
  362. invalidate(outerRectForWindow(window.rect()));
  363. }
  364. void WSWindowManager::flush(const Rect& a_rect)
  365. {
  366. auto rect = Rect::intersection(a_rect, m_screen_rect);
  367. #ifdef DEBUG_COUNTERS
  368. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  369. #endif
  370. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  371. const RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  372. size_t pitch = m_back_bitmap->pitch();
  373. #ifdef DEBUG_FLUSH_YELLOW
  374. m_front_painter->fill_rect(rect, Color::Yellow);
  375. #endif
  376. for (int y = 0; y < rect.height(); ++y) {
  377. fast_dword_copy(front_ptr, back_ptr, rect.width());
  378. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  379. back_ptr = (const RGBA32*)((const byte*)back_ptr + pitch);
  380. }
  381. }