WSWindowManager.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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->laddr().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. Color metadata_color(204, 204, 204);
  157. char buffer[64];
  158. ksprintf(buffer, "%d:%d", window.pid(), window.window_id());
  159. m_back_painter->draw_text(titleBarTitleRect, buffer, Painter::TextAlignment::CenterRight, metadata_color);
  160. }
  161. void WSWindowManager::add_window(WSWindow& window)
  162. {
  163. LOCKER(m_lock);
  164. m_windows.set(&window);
  165. m_windows_in_order.append(&window);
  166. if (!active_window())
  167. set_active_window(&window);
  168. }
  169. void WSWindowManager::move_to_front(WSWindow& window)
  170. {
  171. LOCKER(m_lock);
  172. if (m_windows_in_order.tail() != &window)
  173. invalidate(window);
  174. m_windows_in_order.remove(&window);
  175. m_windows_in_order.append(&window);
  176. }
  177. void WSWindowManager::remove_window(WSWindow& window)
  178. {
  179. LOCKER(m_lock);
  180. if (!m_windows.contains(&window))
  181. return;
  182. invalidate(window);
  183. m_windows.remove(&window);
  184. m_windows_in_order.remove(&window);
  185. if (!active_window() && !m_windows.is_empty())
  186. set_active_window(*m_windows.begin());
  187. }
  188. void WSWindowManager::notify_title_changed(WSWindow& window)
  189. {
  190. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  191. invalidate(outer_window_rect(window.rect()));
  192. }
  193. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  194. {
  195. 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());
  196. ASSERT_INTERRUPTS_ENABLED();
  197. invalidate(outer_window_rect(old_rect));
  198. invalidate(outer_window_rect(new_rect));
  199. }
  200. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  201. {
  202. if (event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left) {
  203. #ifdef DRAG_DEBUG
  204. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  205. #endif
  206. m_drag_window = window.makeWeakPtr();;
  207. m_drag_origin = event.position();
  208. m_drag_window_origin = window.position();
  209. m_drag_start_rect = outer_window_rect(window.rect());
  210. window.set_is_being_dragged(true);
  211. invalidate(window);
  212. return;
  213. }
  214. }
  215. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  216. {
  217. if (event.type() == WSEvent::MouseUp && event.button() == MouseButton::Left) {
  218. if (m_drag_window) {
  219. #ifdef DRAG_DEBUG
  220. printf("[WM] Finish dragging WSWindow{%p}\n", m_dragWindow.ptr());
  221. #endif
  222. invalidate(*m_drag_window);
  223. m_drag_window->set_is_being_dragged(false);
  224. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  225. m_drag_window = nullptr;
  226. return;
  227. }
  228. }
  229. if (event.type() == WSEvent::MouseMove) {
  230. if (m_drag_window) {
  231. auto old_window_rect = m_drag_window->rect();
  232. Point pos = m_drag_window_origin;
  233. #ifdef DRAG_DEBUG
  234. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_dragOrigin.x(), m_dragOrigin.y(), event.x(), event.y());
  235. #endif
  236. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  237. m_drag_window->set_position_without_repaint(pos);
  238. invalidate(outer_window_rect(old_window_rect));
  239. invalidate(outer_window_rect(m_drag_window->rect()));
  240. return;
  241. }
  242. }
  243. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  244. if (title_bar_rect(window->rect()).contains(event.position())) {
  245. if (event.type() == WSEvent::MouseDown) {
  246. move_to_front(*window);
  247. set_active_window(window);
  248. }
  249. handle_titlebar_mouse_event(*window, event);
  250. return;
  251. }
  252. if (window->rect().contains(event.position())) {
  253. if (event.type() == WSEvent::MouseDown) {
  254. move_to_front(*window);
  255. set_active_window(window);
  256. }
  257. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  258. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  259. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  260. window->event(*local_event);
  261. return;
  262. }
  263. }
  264. }
  265. void WSWindowManager::compose()
  266. {
  267. LOCKER(m_lock);
  268. auto dirty_rects = move(m_dirty_rects);
  269. #ifdef DEBUG_COUNTERS
  270. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  271. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  272. #endif
  273. auto any_window_contains_rect = [this] (const Rect& r) {
  274. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  275. if (outer_window_rect(window->rect()).contains(r))
  276. return true;
  277. }
  278. return false;
  279. };
  280. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  281. auto window_rect = outer_window_rect(window.rect());
  282. for (auto& dirty_rect : dirty_rects) {
  283. if (dirty_rect.intersects(window_rect))
  284. return true;
  285. }
  286. return false;
  287. };
  288. for (auto& dirty_rect : dirty_rects) {
  289. if (any_window_contains_rect(dirty_rect)) {
  290. continue;
  291. }
  292. //dbgprintf("Repaint root %d,%d %dx%d\n", dirty_rect.x(), dirty_rect.y(), dirty_rect.width(), dirty_rect.height());
  293. m_back_painter->fill_rect(dirty_rect, Color(0, 72, 96));
  294. }
  295. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  296. WSWindowLocker locker(*window);
  297. RetainPtr<GraphicsBitmap> backing = window->backing();
  298. if (!backing)
  299. continue;
  300. if (!any_dirty_rect_intersects_window(*window))
  301. continue;
  302. for (auto& dirty_rect : dirty_rects) {
  303. m_back_painter->set_clip_rect(dirty_rect);
  304. paint_window_frame(*window);
  305. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  306. if (dirty_rect_in_window_coordinates.is_empty())
  307. continue;
  308. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  309. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  310. auto dst = window->position();
  311. dst.move_by(dirty_rect_in_window_coordinates.location());
  312. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  313. m_back_painter->clear_clip_rect();
  314. }
  315. m_back_painter->clear_clip_rect();
  316. }
  317. for (auto& r : dirty_rects)
  318. flush(r);
  319. draw_cursor();
  320. }
  321. void WSWindowManager::draw_cursor()
  322. {
  323. ASSERT_INTERRUPTS_ENABLED();
  324. LOCKER(m_lock);
  325. auto cursor_location = m_screen.cursor_location();
  326. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  327. flush(m_last_cursor_rect.united(cursor_rect));
  328. Color inner_color = Color::White;
  329. Color outer_color = Color::Black;
  330. if (m_screen.left_mouse_button_pressed())
  331. swap(inner_color, outer_color);
  332. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  333. m_front_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  334. m_last_cursor_rect = cursor_rect;
  335. }
  336. void WSWindowManager::event(WSEvent& event)
  337. {
  338. ASSERT_INTERRUPTS_ENABLED();
  339. LOCKER(m_lock);
  340. if (event.is_mouse_event())
  341. return process_mouse_event(static_cast<WSMouseEvent&>(event));
  342. if (event.is_key_event()) {
  343. // FIXME: This is a good place to hook key events globally. :)
  344. if (m_active_window)
  345. return m_active_window->event(event);
  346. return;
  347. }
  348. if (event.type() == WSEvent::WM_Compose) {
  349. m_pending_compose_event = false;
  350. compose();
  351. return;
  352. }
  353. }
  354. void WSWindowManager::set_active_window(WSWindow* window)
  355. {
  356. LOCKER(m_lock);
  357. if (window == m_active_window.ptr())
  358. return;
  359. if (auto* previously_active_window = m_active_window.ptr()) {
  360. WSEventLoop::the().post_event(previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
  361. invalidate(*previously_active_window);
  362. }
  363. m_active_window = window->makeWeakPtr();
  364. if (m_active_window) {
  365. WSEventLoop::the().post_event(m_active_window.ptr(), make<WSEvent>(WSEvent::WindowActivated));
  366. invalidate(*m_active_window);
  367. }
  368. }
  369. void WSWindowManager::invalidate()
  370. {
  371. LOCKER(m_lock);
  372. m_dirty_rects.clear_with_capacity();
  373. m_dirty_rects.append(m_screen_rect);
  374. }
  375. void WSWindowManager::invalidate(const Rect& a_rect)
  376. {
  377. LOCKER(m_lock);
  378. auto rect = Rect::intersection(a_rect, m_screen_rect);
  379. if (rect.is_empty())
  380. return;
  381. for (auto& r : m_dirty_rects) {
  382. if (r.contains(rect))
  383. return;
  384. if (r.intersects(rect)) {
  385. // Unite with the existing dirty rect.
  386. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  387. r = r.united(rect);
  388. return;
  389. }
  390. }
  391. m_dirty_rects.append(rect);
  392. if (!m_pending_compose_event) {
  393. ASSERT_INTERRUPTS_ENABLED();
  394. WSEventLoop::the().post_event(this, make<WSEvent>(WSEvent::WM_Compose));
  395. m_pending_compose_event = true;
  396. }
  397. }
  398. void WSWindowManager::invalidate(const WSWindow& window)
  399. {
  400. ASSERT_INTERRUPTS_ENABLED();
  401. LOCKER(m_lock);
  402. invalidate(outer_window_rect(window.rect()));
  403. }
  404. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  405. {
  406. if (rect.is_empty()) {
  407. invalidate(window);
  408. return;
  409. }
  410. ASSERT_INTERRUPTS_ENABLED();
  411. LOCKER(m_lock);
  412. auto outer_rect = outer_window_rect(window.rect());
  413. auto inner_rect = rect;
  414. inner_rect.move_by(window.position());
  415. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  416. inner_rect.intersect(outer_rect);
  417. invalidate(inner_rect);
  418. }
  419. void WSWindowManager::flush(const Rect& a_rect)
  420. {
  421. auto rect = Rect::intersection(a_rect, m_screen_rect);
  422. #ifdef DEBUG_COUNTERS
  423. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  424. #endif
  425. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  426. const RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  427. size_t pitch = m_back_bitmap->pitch();
  428. if (m_flash_flush)
  429. m_front_painter->fill_rect(rect, Color::Yellow);
  430. for (int y = 0; y < rect.height(); ++y) {
  431. fast_dword_copy(front_ptr, back_ptr, rect.width());
  432. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  433. back_ptr = (const RGBA32*)((const byte*)back_ptr + pitch);
  434. }
  435. }