WSWindowManager.cpp 14 KB

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