WSWindowManager.cpp 17 KB

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