WSWindowManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. //printf("[WM] paintWindowFrame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  126. auto titleBarRect = titleBarRectForWindow(window.rect());
  127. auto titleBarTitleRect = titleBarTitleRectForWindow(window.rect());
  128. auto outerRect = outerRectForWindow(window.rect());
  129. auto borderRect = borderRectForWindow(window.rect());
  130. Rect inner_border_rect {
  131. window.x() - 1,
  132. window.y() - 1,
  133. window.width() + 2,
  134. window.height() + 2
  135. };
  136. auto titleColor = &window == activeWindow() ? m_activeWindowTitleColor : m_inactiveWindowTitleColor;
  137. auto borderColor = &window == activeWindow() ? m_activeWindowBorderColor : m_inactiveWindowBorderColor;
  138. m_back_painter->draw_rect(borderRect, Color::MidGray);
  139. m_back_painter->draw_rect(outerRect, borderColor);
  140. m_back_painter->fill_rect(titleBarRect, borderColor);
  141. m_back_painter->draw_rect(inner_border_rect, borderColor);
  142. m_back_painter->draw_text(titleBarTitleRect, window.title(), Painter::TextAlignment::CenterLeft, titleColor);
  143. }
  144. void WSWindowManager::addWindow(WSWindow& window)
  145. {
  146. m_windows.set(&window);
  147. m_windows_in_order.append(&window);
  148. if (!activeWindow())
  149. setActiveWindow(&window);
  150. }
  151. void WSWindowManager::move_to_front(WSWindow& window)
  152. {
  153. m_windows_in_order.remove(&window);
  154. m_windows_in_order.append(&window);
  155. }
  156. void WSWindowManager::removeWindow(WSWindow& window)
  157. {
  158. if (!m_windows.contains(&window))
  159. return;
  160. invalidate(window);
  161. m_windows.remove(&window);
  162. m_windows_in_order.remove(&window);
  163. if (!activeWindow() && !m_windows.is_empty())
  164. setActiveWindow(*m_windows.begin());
  165. }
  166. void WSWindowManager::notifyTitleChanged(WSWindow& window)
  167. {
  168. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  169. }
  170. void WSWindowManager::notifyRectChanged(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  171. {
  172. 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());
  173. ASSERT_INTERRUPTS_ENABLED();
  174. LOCKER(m_lock);
  175. invalidate(outerRectForWindow(old_rect));
  176. invalidate(outerRectForWindow(new_rect));
  177. }
  178. void WSWindowManager::handleTitleBarMouseEvent(WSWindow& window, MouseEvent& event)
  179. {
  180. if (event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left) {
  181. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  182. m_dragWindow = window.makeWeakPtr();;
  183. m_dragOrigin = event.position();
  184. m_dragWindowOrigin = window.position();
  185. m_dragStartRect = outerRectForWindow(window.rect());
  186. window.set_is_being_dragged(true);
  187. return;
  188. }
  189. }
  190. void WSWindowManager::processMouseEvent(MouseEvent& event)
  191. {
  192. if (event.type() == WSEvent::MouseUp && event.button() == MouseButton::Left) {
  193. if (m_dragWindow) {
  194. printf("[WM] Finish dragging WSWindow{%p}\n", m_dragWindow.ptr());
  195. invalidate(m_dragStartRect);
  196. invalidate(*m_dragWindow);
  197. m_dragWindow->set_is_being_dragged(false);
  198. m_dragEndRect = outerRectForWindow(m_dragWindow->rect());
  199. m_dragWindow = nullptr;
  200. return;
  201. }
  202. }
  203. if (event.type() == WSEvent::MouseMove) {
  204. if (m_dragWindow) {
  205. auto old_window_rect = m_dragWindow->rect();
  206. Point pos = m_dragWindowOrigin;
  207. printf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_dragOrigin.x(), m_dragOrigin.y(), event.x(), event.y());
  208. pos.move_by(event.x() - m_dragOrigin.x(), event.y() - m_dragOrigin.y());
  209. m_dragWindow->set_position_without_repaint(pos);
  210. invalidate(outerRectForWindow(old_window_rect));
  211. invalidate(outerRectForWindow(m_dragWindow->rect()));
  212. return;
  213. }
  214. }
  215. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  216. if (titleBarRectForWindow(window->rect()).contains(event.position())) {
  217. if (event.type() == WSEvent::MouseDown) {
  218. move_to_front(*window);
  219. setActiveWindow(window);
  220. }
  221. handleTitleBarMouseEvent(*window, event);
  222. return;
  223. }
  224. if (window->rect().contains(event.position())) {
  225. if (event.type() == WSEvent::MouseDown) {
  226. move_to_front(*window);
  227. setActiveWindow(window);
  228. }
  229. // FIXME: Re-use the existing event instead of crafting a new one?
  230. auto localEvent = make<MouseEvent>(event.type(), event.x() - window->rect().x(), event.y() - window->rect().y(), event.button());
  231. window->event(*localEvent);
  232. return;
  233. }
  234. }
  235. }
  236. void WSWindowManager::compose()
  237. {
  238. auto invalidated_rects = move(m_invalidated_rects);
  239. #ifdef DEBUG_COUNTERS
  240. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_recompose_count, invalidated_rects.size());
  241. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  242. #endif
  243. auto any_window_contains_rect = [this] (const Rect& r) {
  244. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  245. if (outerRectForWindow(window->rect()).contains(r))
  246. return true;
  247. }
  248. return false;
  249. };
  250. for (auto& r : invalidated_rects) {
  251. if (any_window_contains_rect(r))
  252. continue;
  253. //dbgprintf("Repaint root %d,%d %dx%d\n", r.x(), r.y(), r.width(), r.height());
  254. m_back_painter->fill_rect(r, Color(0, 72, 96));
  255. }
  256. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  257. if (!window->backing())
  258. continue;
  259. paintWindowFrame(*window);
  260. m_back_painter->blit(window->position(), *window->backing());
  261. }
  262. for (auto& r : invalidated_rects)
  263. flush(r);
  264. draw_cursor();
  265. }
  266. void WSWindowManager::draw_cursor()
  267. {
  268. ASSERT_INTERRUPTS_ENABLED();
  269. LOCKER(m_lock);
  270. auto cursor_location = m_framebuffer.cursor_location();
  271. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  272. flush(m_last_cursor_rect.united(cursor_rect));
  273. Color inner_color = Color::White;
  274. Color outer_color = Color::Black;
  275. if (m_framebuffer.left_mouse_button_pressed())
  276. swap(inner_color, outer_color);
  277. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  278. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  279. m_last_cursor_rect = cursor_rect;
  280. }
  281. void WSWindowManager::event(WSEvent& event)
  282. {
  283. ASSERT_INTERRUPTS_ENABLED();
  284. LOCKER(m_lock);
  285. if (event.isMouseEvent())
  286. return processMouseEvent(static_cast<MouseEvent&>(event));
  287. if (event.isKeyEvent()) {
  288. // FIXME: This is a good place to hook key events globally. :)
  289. if (m_activeWindow)
  290. return m_activeWindow->event(event);
  291. return;
  292. }
  293. if (event.type() == WSEvent::WM_Compose) {
  294. m_pending_compose_event = false;
  295. compose();
  296. return;
  297. }
  298. }
  299. void WSWindowManager::setActiveWindow(WSWindow* window)
  300. {
  301. if (window == m_activeWindow.ptr())
  302. return;
  303. if (auto* previously_active_window = m_activeWindow.ptr())
  304. invalidate(*previously_active_window);
  305. m_activeWindow = window->makeWeakPtr();
  306. if (m_activeWindow)
  307. invalidate(*m_activeWindow);
  308. }
  309. void WSWindowManager::invalidate()
  310. {
  311. m_invalidated_rects.clear_with_capacity();
  312. m_invalidated_rects.append(m_screen_rect);
  313. }
  314. void WSWindowManager::invalidate(const Rect& a_rect)
  315. {
  316. auto rect = Rect::intersection(a_rect, m_screen_rect);
  317. if (rect.is_empty())
  318. return;
  319. for (auto& r : m_invalidated_rects) {
  320. if (r.contains(rect))
  321. return;
  322. if (r.intersects(rect)) {
  323. // Unite with the existing dirty rect.
  324. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  325. r = r.united(rect);
  326. return;
  327. }
  328. }
  329. m_invalidated_rects.append(rect);
  330. if (!m_pending_compose_event) {
  331. ASSERT_INTERRUPTS_ENABLED();
  332. WSEventLoop::the().post_event(this, make<WSEvent>(WSEvent::WM_Compose));
  333. m_pending_compose_event = true;
  334. }
  335. }
  336. void WSWindowManager::invalidate(const WSWindow& window)
  337. {
  338. ASSERT_INTERRUPTS_ENABLED();
  339. LOCKER(m_lock);
  340. invalidate(outerRectForWindow(window.rect()));
  341. }
  342. void WSWindowManager::flush(const Rect& a_rect)
  343. {
  344. auto rect = Rect::intersection(a_rect, m_screen_rect);
  345. #ifdef DEBUG_COUNTERS
  346. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  347. #endif
  348. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  349. const RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  350. size_t pitch = m_back_bitmap->pitch();
  351. #ifdef DEBUG_FLUSH_YELLOW
  352. m_front_painter->fill_rect(rect, Color::Yellow);
  353. #endif
  354. for (int y = 0; y < rect.height(); ++y) {
  355. fast_dword_copy(front_ptr, back_ptr, rect.width());
  356. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  357. back_ptr = (const RGBA32*)((const byte*)back_ptr + pitch);
  358. }
  359. }