WSWindowManager.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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/ProcFS.h>
  8. #include <SharedGraphics/Font.h>
  9. #include <SharedGraphics/Painter.h>
  10. #include <SharedGraphics/CharacterBitmap.h>
  11. #include <AK/StdLibExtras.h>
  12. #include <Kernel/BochsVGADevice.h>
  13. #include <LibC/errno_numbers.h>
  14. #include "WSMenu.h"
  15. #include "WSMenuBar.h"
  16. #include "WSMenuItem.h"
  17. //#define DEBUG_COUNTERS
  18. //#define DEBUG_WID_IN_TITLE_BAR
  19. static const int window_titlebar_height = 16;
  20. static inline Rect title_bar_rect(const Rect& window)
  21. {
  22. return {
  23. window.x() - 1,
  24. window.y() - window_titlebar_height,
  25. window.width() + 2,
  26. window_titlebar_height
  27. };
  28. }
  29. static inline Rect title_bar_text_rect(const Rect& window)
  30. {
  31. auto titlebar_rect = title_bar_rect(window);
  32. return {
  33. titlebar_rect.x() + 2,
  34. titlebar_rect.y(),
  35. titlebar_rect.width() - 4,
  36. titlebar_rect.height()
  37. };
  38. }
  39. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  40. {
  41. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  42. int close_button_margin = 1;
  43. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  44. return Rect {
  45. titlebar_inner_rect.right() - close_button_size + 1,
  46. titlebar_inner_rect.top() + close_button_margin,
  47. close_button_size,
  48. close_button_size - 1
  49. };
  50. }
  51. static inline Rect border_window_rect(const Rect& window)
  52. {
  53. auto titlebar_rect = title_bar_rect(window);
  54. return { titlebar_rect.x() - 1,
  55. titlebar_rect.y() - 1,
  56. titlebar_rect.width() + 2,
  57. window_titlebar_height + window.height() + 3
  58. };
  59. }
  60. static inline Rect outer_window_rect(const Rect& window)
  61. {
  62. auto rect = border_window_rect(window);
  63. rect.inflate(2, 2);
  64. return rect;
  65. }
  66. static WSWindowManager* s_the;
  67. WSWindowManager& WSWindowManager::the()
  68. {
  69. if (!s_the)
  70. s_the = new WSWindowManager;
  71. return *s_the;
  72. }
  73. static const char* cursor_bitmap_inner_ascii = {
  74. " # "
  75. " ## "
  76. " ### "
  77. " #### "
  78. " ##### "
  79. " ###### "
  80. " ####### "
  81. " ######## "
  82. " ######### "
  83. " ########## "
  84. " ###### "
  85. " ## ## "
  86. " # ## "
  87. " ## "
  88. " ## "
  89. " ## "
  90. " "
  91. };
  92. static const char* cursor_bitmap_outer_ascii = {
  93. "## "
  94. "# # "
  95. "# # "
  96. "# # "
  97. "# # "
  98. "# # "
  99. "# # "
  100. "# # "
  101. "# # "
  102. "# # "
  103. "# #### "
  104. "# ## # "
  105. "# # # # "
  106. "## # # "
  107. " # # "
  108. " # # "
  109. " ## "
  110. };
  111. void WSWindowManager::flip_buffers()
  112. {
  113. swap(m_front_bitmap, m_back_bitmap);
  114. swap(m_front_painter, m_back_painter);
  115. if (m_buffers_are_flipped)
  116. BochsVGADevice::the().set_y_offset(0);
  117. else
  118. BochsVGADevice::the().set_y_offset(m_screen_rect.height());
  119. m_buffers_are_flipped = !m_buffers_are_flipped;
  120. }
  121. WSWindowManager::WSWindowManager()
  122. : m_screen(WSScreen::the())
  123. , m_screen_rect(m_screen.rect())
  124. , m_lock("WSWindowManager")
  125. , m_flash_flush(false)
  126. {
  127. #ifndef DEBUG_COUNTERS
  128. (void)m_compose_count;
  129. (void)m_flush_count;
  130. #endif
  131. auto size = m_screen_rect.size();
  132. m_front_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(0));
  133. m_back_bitmap = GraphicsBitmap::create_wrapper(size, m_screen.scanline(size.height()));
  134. m_front_painter = make<Painter>(*m_front_bitmap);
  135. m_back_painter = make<Painter>(*m_back_bitmap);
  136. m_font = Font::default_font();
  137. m_front_painter->set_font(font());
  138. m_back_painter->set_font(font());
  139. m_background_color = Color(50, 50, 50);
  140. m_active_window_border_color = Color(110, 34, 9);
  141. m_active_window_border_color2 = Color(244, 202, 158);
  142. m_active_window_title_color = Color::White;
  143. m_inactive_window_border_color = Color(128, 128, 128);
  144. m_inactive_window_border_color2 = Color(192, 192, 192);
  145. m_inactive_window_title_color = Color(213, 208, 199);
  146. m_dragging_window_border_color = Color(161, 50, 13);
  147. m_dragging_window_border_color2 = Color(250, 220, 187);
  148. m_dragging_window_title_color = Color::White;
  149. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  150. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  151. {
  152. LOCKER(m_wallpaper_path.lock());
  153. m_wallpaper_path.resource() = "/res/wallpapers/cool.rgb";
  154. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  155. }
  156. ProcFS::the().add_sys_bool("wm_flash_flush", m_flash_flush);
  157. ProcFS::the().add_sys_string("wm_wallpaper", m_wallpaper_path, [this] {
  158. LOCKER(m_wallpaper_path.lock());
  159. m_wallpaper = GraphicsBitmap::load_from_file(m_wallpaper_path.resource(), m_screen_rect.size());
  160. invalidate(m_screen_rect);
  161. });
  162. m_menu_selection_color = Color(0x84351a);
  163. {
  164. byte system_menu_name[] = { 0xfc, 0 };
  165. m_system_menu = make<WSMenu>(*current, -1, String((const char*)system_menu_name));
  166. m_system_menu->add_item(make<WSMenuItem>(0, "Launch Terminal"));
  167. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  168. m_system_menu->add_item(make<WSMenuItem>(1, "Hello again"));
  169. m_system_menu->add_item(make<WSMenuItem>(2, "To all my friends"));
  170. m_system_menu->add_item(make<WSMenuItem>(3, "Together we can play some rock&roll"));
  171. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  172. m_system_menu->add_item(make<WSMenuItem>(4, "About..."));
  173. m_system_menu->on_item_activation = [] (WSMenuItem& item) {
  174. if (item.identifier() == 0) {
  175. int error;
  176. Process::create_user_process("/bin/Terminal", 100, 100, current->pid(), error);
  177. return;
  178. }
  179. kprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  180. };
  181. }
  182. // NOTE: This ensures that the system menu has the correct dimensions.
  183. set_current_menubar(nullptr);
  184. invalidate();
  185. compose();
  186. }
  187. WSWindowManager::~WSWindowManager()
  188. {
  189. }
  190. template<typename Callback>
  191. void WSWindowManager::for_each_active_menubar_menu(Callback callback)
  192. {
  193. callback(*m_system_menu);
  194. if (m_current_menubar)
  195. m_current_menubar->for_each_menu(callback);
  196. }
  197. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  198. {
  199. LOCKER(m_lock);
  200. m_current_menubar = menubar;
  201. dbgprintf("[WM] Current menubar is now %p\n", menubar);
  202. int menu_margin = 16;
  203. Point next_menu_location { menu_margin / 2, 3 };
  204. for_each_active_menubar_menu([&] (WSMenu& menu) {
  205. int text_width = font().width(menu.name());
  206. menu.set_rect_in_menubar({ next_menu_location.x() - menu_margin / 2, 0, text_width + menu_margin, menubar_rect().height() - 1 });
  207. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, font().glyph_height() } });
  208. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  209. return true;
  210. });
  211. invalidate();
  212. }
  213. static const char* s_close_button_bitmap_data = {
  214. "## ##"
  215. "### ###"
  216. " ###### "
  217. " #### "
  218. " ## "
  219. " #### "
  220. " ###### "
  221. "### ###"
  222. "## ##"
  223. };
  224. static CharacterBitmap* s_close_button_bitmap;
  225. static const int s_close_button_bitmap_width = 8;
  226. static const int s_close_button_bitmap_height = 9;
  227. void WSWindowManager::paint_window_frame(WSWindow& window)
  228. {
  229. LOCKER(m_lock);
  230. //printf("[WM] paint_window_frame {%p}, rect: %d,%d %dx%d\n", &window, window.rect().x(), window.rect().y(), window.rect().width(), window.rect().height());
  231. if (window.type() == WSWindowType::Menu) {
  232. m_back_painter->draw_rect(window.rect().inflated(2, 2), Color::LightGray);
  233. return;
  234. }
  235. auto titlebar_rect = title_bar_rect(window.rect());
  236. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  237. auto outer_rect = outer_window_rect(window.rect());
  238. auto border_rect = border_window_rect(window.rect());
  239. auto close_button_rect = close_button_rect_for_window(window.rect());
  240. auto titlebar_title_rect = titlebar_inner_rect;
  241. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  242. Rect inner_border_rect {
  243. window.x() - 1,
  244. window.y() - 1,
  245. window.width() + 2,
  246. window.height() + 2
  247. };
  248. Color title_color;
  249. Color border_color;
  250. Color border_color2;
  251. Color middle_border_color;
  252. if (&window == m_drag_window.ptr()) {
  253. border_color = m_dragging_window_border_color;
  254. border_color2 = m_dragging_window_border_color2;
  255. title_color = m_dragging_window_title_color;
  256. middle_border_color = Color::White;
  257. } else if (&window == m_active_window.ptr()) {
  258. border_color = m_active_window_border_color;
  259. border_color2 = m_active_window_border_color2;
  260. title_color = m_active_window_title_color;
  261. middle_border_color = Color::MidGray;
  262. } else {
  263. border_color = m_inactive_window_border_color;
  264. border_color2 = m_inactive_window_border_color2;
  265. title_color = m_inactive_window_title_color;
  266. middle_border_color = Color::MidGray;
  267. }
  268. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  269. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  270. m_back_painter->draw_line({ titlebar_title_rect.right() + 4, titlebar_inner_rect.y() + i }, { close_button_rect.left() - 3, titlebar_inner_rect.y() + i }, border_color);
  271. }
  272. m_back_painter->draw_rect(border_rect, middle_border_color);
  273. m_back_painter->draw_rect(outer_rect, border_color);
  274. m_back_painter->draw_rect(inner_border_rect, border_color);
  275. m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
  276. if (!s_close_button_bitmap)
  277. s_close_button_bitmap = CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
  278. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  279. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  280. auto x_location = close_button_rect.center();
  281. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  282. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  283. #ifdef DEBUG_WID_IN_TITLE_BAR
  284. Color metadata_color(96, 96, 96);
  285. m_back_painter->draw_text(
  286. titlebar_inner_rect,
  287. String::format("%d:%d", window.pid(), window.window_id()),
  288. TextAlignment::CenterRight,
  289. metadata_color
  290. );
  291. #endif
  292. }
  293. void WSWindowManager::add_window(WSWindow& window)
  294. {
  295. LOCKER(m_lock);
  296. m_windows.set(&window);
  297. m_windows_in_order.append(&window);
  298. if (!active_window())
  299. set_active_window(&window);
  300. }
  301. void WSWindowManager::move_to_front(WSWindow& window)
  302. {
  303. LOCKER(m_lock);
  304. if (m_windows_in_order.tail() != &window)
  305. invalidate(window);
  306. m_windows_in_order.remove(&window);
  307. m_windows_in_order.append(&window);
  308. }
  309. void WSWindowManager::remove_window(WSWindow& window)
  310. {
  311. LOCKER(m_lock);
  312. if (!m_windows.contains(&window))
  313. return;
  314. invalidate(window);
  315. m_windows.remove(&window);
  316. m_windows_in_order.remove(&window);
  317. if (!active_window() && !m_windows.is_empty())
  318. set_active_window(*m_windows.begin());
  319. }
  320. void WSWindowManager::notify_title_changed(WSWindow& window)
  321. {
  322. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  323. invalidate(outer_window_rect(window.rect()));
  324. }
  325. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  326. {
  327. 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());
  328. ASSERT_INTERRUPTS_ENABLED();
  329. invalidate(outer_window_rect(old_rect));
  330. invalidate(outer_window_rect(new_rect));
  331. }
  332. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, WSMouseEvent& event)
  333. {
  334. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu;
  335. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  336. bool should_open_menu = &menu != m_current_menu && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  337. if (should_open_menu) {
  338. if (m_current_menu == &menu)
  339. return;
  340. close_current_menu();
  341. auto& menu_window = menu.ensure_menu_window();
  342. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  343. menu_window.set_visible(true);
  344. m_current_menu = &menu;
  345. return;
  346. }
  347. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  348. close_current_menu();
  349. return;
  350. }
  351. }
  352. void WSWindowManager::close_current_menu()
  353. {
  354. if (m_current_menu && m_current_menu->menu_window())
  355. m_current_menu->menu_window()->set_visible(false);
  356. m_current_menu = nullptr;
  357. }
  358. void WSWindowManager::handle_menubar_mouse_event(WSMenuBar& menu, WSMouseEvent& event)
  359. {
  360. for_each_active_menubar_menu([&] (WSMenu& menu) {
  361. if (menu.rect_in_menubar().contains(event.position())) {
  362. handle_menu_mouse_event(menu, event);
  363. return false;
  364. }
  365. return true;
  366. });
  367. }
  368. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  369. {
  370. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  371. #ifdef DRAG_DEBUG
  372. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  373. #endif
  374. m_drag_window = window.make_weak_ptr();;
  375. m_drag_origin = event.position();
  376. m_drag_window_origin = window.position();
  377. m_drag_start_rect = outer_window_rect(window.rect());
  378. window.set_is_being_dragged(true);
  379. invalidate(window);
  380. return;
  381. }
  382. }
  383. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  384. {
  385. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  386. WSMessage message(WSMessage::WindowCloseRequest);
  387. window.on_message(message);
  388. return;
  389. }
  390. }
  391. void WSWindowManager::process_mouse_event(WSMouseEvent& event)
  392. {
  393. LOCKER(m_lock);
  394. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  395. if (m_drag_window) {
  396. #ifdef DRAG_DEBUG
  397. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  398. #endif
  399. invalidate(*m_drag_window);
  400. m_drag_window->set_is_being_dragged(false);
  401. m_drag_end_rect = outer_window_rect(m_drag_window->rect());
  402. m_drag_window = nullptr;
  403. return;
  404. }
  405. }
  406. if (event.type() == WSMessage::MouseMove) {
  407. if (m_drag_window) {
  408. auto old_window_rect = m_drag_window->rect();
  409. Point pos = m_drag_window_origin;
  410. #ifdef DRAG_DEBUG
  411. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  412. #endif
  413. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  414. m_drag_window->set_position_without_repaint(pos);
  415. invalidate(outer_window_rect(old_window_rect));
  416. invalidate(outer_window_rect(m_drag_window->rect()));
  417. return;
  418. }
  419. }
  420. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  421. if (!window->global_cursor_tracking())
  422. continue;
  423. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  424. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  425. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  426. window->on_message(*local_event);
  427. }
  428. if (menubar_rect().contains(event.position())) {
  429. handle_menubar_mouse_event(*m_current_menubar, event);
  430. return;
  431. }
  432. if (m_current_menu) {
  433. bool event_is_inside_current_menu = m_current_menu->menu_window()->rect().contains(event.position());
  434. if (!event_is_inside_current_menu) {
  435. if (m_current_menu->hovered_item())
  436. m_current_menu->clear_hovered_item();
  437. if (event.type() == WSMessage::MouseDown || event.type() == WSMessage::MouseUp)
  438. close_current_menu();
  439. }
  440. }
  441. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  442. if (!window->is_visible())
  443. continue;
  444. if (title_bar_rect(window->rect()).contains(event.position())) {
  445. if (event.type() == WSMessage::MouseDown) {
  446. move_to_front(*window);
  447. set_active_window(window);
  448. }
  449. if (close_button_rect_for_window(window->rect()).contains(event.position())) {
  450. handle_close_button_mouse_event(*window, event);
  451. return;
  452. }
  453. handle_titlebar_mouse_event(*window, event);
  454. return;
  455. }
  456. if (window->rect().contains(event.position())) {
  457. if (event.type() == WSMessage::MouseDown) {
  458. move_to_front(*window);
  459. set_active_window(window);
  460. }
  461. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  462. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  463. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  464. window->on_message(*local_event);
  465. return;
  466. }
  467. }
  468. }
  469. void WSWindowManager::compose()
  470. {
  471. LOCKER(m_lock);
  472. auto dirty_rects = move(m_dirty_rects);
  473. auto cursor_location = m_screen.cursor_location();
  474. dirty_rects.append(m_last_cursor_rect);
  475. dirty_rects.append({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  476. #ifdef DEBUG_COUNTERS
  477. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.size());
  478. dbgprintf("kmalloc stats: alloc:%u free:%u eternal:%u\n", sum_alloc, sum_free, kmalloc_sum_eternal);
  479. #endif
  480. auto any_window_contains_rect = [this] (const Rect& r) {
  481. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  482. if (!window->is_visible())
  483. continue;
  484. if (outer_window_rect(window->rect()).contains(r))
  485. return true;
  486. }
  487. return false;
  488. };
  489. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  490. auto window_rect = outer_window_rect(window.rect());
  491. for (auto& dirty_rect : dirty_rects) {
  492. if (dirty_rect.intersects(window_rect))
  493. return true;
  494. }
  495. return false;
  496. };
  497. for (auto& dirty_rect : dirty_rects) {
  498. if (any_window_contains_rect(dirty_rect)) {
  499. continue;
  500. }
  501. LOCKER(m_wallpaper_path.lock());
  502. if (!m_wallpaper)
  503. m_back_painter->fill_rect(dirty_rect, m_background_color);
  504. else
  505. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  506. }
  507. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  508. if (!window->is_visible())
  509. continue;
  510. WSWindowLocker locker(*window);
  511. RetainPtr<GraphicsBitmap> backing = window->backing();
  512. if (!backing)
  513. continue;
  514. if (!any_dirty_rect_intersects_window(*window))
  515. continue;
  516. for (auto& dirty_rect : dirty_rects) {
  517. m_back_painter->set_clip_rect(dirty_rect);
  518. paint_window_frame(*window);
  519. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window->rect());
  520. if (dirty_rect_in_window_coordinates.is_empty())
  521. continue;
  522. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window->x());
  523. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window->y());
  524. auto dst = window->position();
  525. dst.move_by(dirty_rect_in_window_coordinates.location());
  526. m_back_painter->blit(dst, *backing, dirty_rect_in_window_coordinates);
  527. m_back_painter->clear_clip_rect();
  528. }
  529. m_back_painter->clear_clip_rect();
  530. }
  531. draw_menubar();
  532. draw_cursor();
  533. if (m_flash_flush.lock_and_copy()) {
  534. for (auto& rect : dirty_rects)
  535. m_front_painter->fill_rect(rect, Color::Yellow);
  536. }
  537. flip_buffers();
  538. for (auto& r : dirty_rects)
  539. flush(r);
  540. }
  541. void WSWindowManager::invalidate_cursor()
  542. {
  543. LOCKER(m_lock);
  544. auto cursor_location = m_screen.cursor_location();
  545. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  546. invalidate(cursor_rect);
  547. }
  548. Rect WSWindowManager::menubar_rect() const
  549. {
  550. return { 0, 0, m_screen_rect.width(), 16 };
  551. }
  552. void WSWindowManager::draw_menubar()
  553. {
  554. m_back_painter->fill_rect(menubar_rect(), Color::LightGray);
  555. m_back_painter->draw_line({ 0, menubar_rect().bottom() }, { menubar_rect().right(), menubar_rect().bottom() }, Color::White);
  556. for_each_active_menubar_menu([&] (WSMenu& menu) {
  557. Color text_color = Color::Black;
  558. if (&menu == m_current_menu) {
  559. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  560. text_color = Color::White;
  561. }
  562. m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
  563. return true;
  564. });
  565. }
  566. void WSWindowManager::draw_cursor()
  567. {
  568. ASSERT_INTERRUPTS_ENABLED();
  569. LOCKER(m_lock);
  570. auto cursor_location = m_screen.cursor_location();
  571. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  572. Color inner_color = Color::White;
  573. Color outer_color = Color::Black;
  574. if (m_screen.left_mouse_button_pressed())
  575. swap(inner_color, outer_color);
  576. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  577. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  578. m_last_cursor_rect = cursor_rect;
  579. }
  580. void WSWindowManager::on_message(WSMessage& message)
  581. {
  582. ASSERT_INTERRUPTS_ENABLED();
  583. LOCKER(m_lock);
  584. if (message.is_mouse_event())
  585. return process_mouse_event(static_cast<WSMouseEvent&>(message));
  586. if (message.is_key_event()) {
  587. // FIXME: This is a good place to hook key events globally. :)
  588. if (m_active_window)
  589. return m_active_window->on_message(message);
  590. return;
  591. }
  592. if (message.type() == WSMessage::WM_DeferredCompose) {
  593. m_pending_compose_event = false;
  594. compose();
  595. return;
  596. }
  597. }
  598. void WSWindowManager::set_active_window(WSWindow* window)
  599. {
  600. LOCKER(m_lock);
  601. if (window == m_active_window.ptr())
  602. return;
  603. if (auto* previously_active_window = m_active_window.ptr()) {
  604. WSMessageLoop::the().post_message(previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  605. invalidate(*previously_active_window);
  606. }
  607. m_active_window = window->make_weak_ptr();
  608. if (m_active_window) {
  609. WSMessageLoop::the().post_message(m_active_window.ptr(), make<WSMessage>(WSMessage::WindowActivated));
  610. invalidate(*m_active_window);
  611. }
  612. }
  613. void WSWindowManager::invalidate()
  614. {
  615. LOCKER(m_lock);
  616. m_dirty_rects.clear_with_capacity();
  617. invalidate(m_screen_rect);
  618. }
  619. void WSWindowManager::invalidate(const Rect& a_rect)
  620. {
  621. LOCKER(m_lock);
  622. auto rect = Rect::intersection(a_rect, m_screen_rect);
  623. if (rect.is_empty())
  624. return;
  625. for (auto& r : m_dirty_rects) {
  626. if (r.contains(rect))
  627. return;
  628. if (r.intersects(rect)) {
  629. // Unite with the existing dirty rect.
  630. // FIXME: It would be much nicer to compute the exact rects needing repaint.
  631. r = r.united(rect);
  632. return;
  633. }
  634. }
  635. m_dirty_rects.append(rect);
  636. if (!m_pending_compose_event) {
  637. ASSERT_INTERRUPTS_ENABLED();
  638. WSMessageLoop::the().post_message(this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  639. m_pending_compose_event = true;
  640. }
  641. }
  642. void WSWindowManager::invalidate(const WSWindow& window)
  643. {
  644. ASSERT_INTERRUPTS_ENABLED();
  645. LOCKER(m_lock);
  646. invalidate(outer_window_rect(window.rect()));
  647. }
  648. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  649. {
  650. if (rect.is_empty()) {
  651. invalidate(window);
  652. return;
  653. }
  654. ASSERT_INTERRUPTS_ENABLED();
  655. LOCKER(m_lock);
  656. auto outer_rect = outer_window_rect(window.rect());
  657. auto inner_rect = rect;
  658. inner_rect.move_by(window.position());
  659. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  660. inner_rect.intersect(outer_rect);
  661. invalidate(inner_rect);
  662. }
  663. void WSWindowManager::flush(const Rect& a_rect)
  664. {
  665. auto rect = Rect::intersection(a_rect, m_screen_rect);
  666. #ifdef DEBUG_COUNTERS
  667. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  668. #endif
  669. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  670. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  671. size_t pitch = m_back_bitmap->pitch();
  672. for (int y = 0; y < rect.height(); ++y) {
  673. fast_dword_copy(back_ptr, front_ptr, rect.width());
  674. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  675. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  676. }
  677. }
  678. void WSWindowManager::close_menu(WSMenu& menu)
  679. {
  680. LOCKER(m_lock);
  681. if (m_current_menu == &menu)
  682. close_current_menu();
  683. }
  684. WSMenu& WSWindowManager::create_menu(String&& name)
  685. {
  686. LOCKER(m_lock);
  687. int menu_id = m_next_menu_id++;
  688. auto menu = make<WSMenu>(*current, menu_id, move(name));
  689. auto* menu_ptr = menu.ptr();
  690. m_menus.set(menu_id, move(menu));
  691. return *menu_ptr;
  692. }
  693. int WSWindowManager::api$menubar_create()
  694. {
  695. LOCKER(m_lock);
  696. int menubar_id = m_next_menubar_id++;
  697. auto menubar = make<WSMenuBar>(menubar_id, *current);
  698. m_menubars.set(menubar_id, move(menubar));
  699. return menubar_id;
  700. }
  701. int WSWindowManager::api$menubar_destroy(int menubar_id)
  702. {
  703. LOCKER(m_lock);
  704. auto it = m_menubars.find(menubar_id);
  705. if (it == m_menubars.end())
  706. return -EBADMENUBAR;
  707. auto& menubar = *(*it).value;
  708. if (&menubar == m_current_menubar)
  709. set_current_menubar(nullptr);
  710. m_menubars.remove(it);
  711. return 0;
  712. }
  713. int WSWindowManager::api$menubar_add_menu(int menubar_id, int menu_id)
  714. {
  715. LOCKER(m_lock);
  716. auto it = m_menubars.find(menubar_id);
  717. if (it == m_menubars.end())
  718. return -EBADMENUBAR;
  719. auto& menubar = *(*it).value;
  720. auto jt = m_menus.find(menu_id);
  721. if (jt == m_menus.end())
  722. return -EBADMENU;
  723. auto& menu = *(*jt).value;
  724. menubar.add_menu(&menu);
  725. return 0;
  726. }
  727. int WSWindowManager::api$menu_create(String&& name)
  728. {
  729. LOCKER(m_lock);
  730. return create_menu(move(name)).menu_id();
  731. }
  732. int WSWindowManager::api$menu_destroy(int menu_id)
  733. {
  734. LOCKER(m_lock);
  735. auto it = m_menus.find(menu_id);
  736. if (it == m_menus.end())
  737. return -EBADMENU;
  738. auto& menu = *(*it).value;
  739. close_menu(menu);
  740. m_menus.remove(it);
  741. return 0;
  742. }
  743. int WSWindowManager::api$menu_add_separator(int menu_id)
  744. {
  745. LOCKER(m_lock);
  746. auto it = m_menus.find(menu_id);
  747. if (it == m_menus.end())
  748. return -EBADMENU;
  749. auto& menu = *(*it).value;
  750. menu.add_item(make<WSMenuItem>(WSMenuItem::Separator));
  751. return 0;
  752. }
  753. int WSWindowManager::api$menu_add_item(int menu_id, unsigned identifier, String&& text)
  754. {
  755. LOCKER(m_lock);
  756. auto it = m_menus.find(menu_id);
  757. if (it == m_menus.end())
  758. return -EBADMENU;
  759. auto& menu = *(*it).value;
  760. menu.add_item(make<WSMenuItem>(identifier, move(text)));
  761. return 0;
  762. }
  763. int WSWindowManager::api$app_set_menubar(int menubar_id)
  764. {
  765. LOCKER(m_lock);
  766. auto it = m_menubars.find(menubar_id);
  767. if (it == m_menubars.end())
  768. return -EBADMENUBAR;
  769. auto& menubar = *(*it).value;
  770. if (&menubar == m_current_menubar)
  771. return 0;
  772. set_current_menubar(&menubar);
  773. // FIXME: Maybe leave the system menu even if the app menu changes?
  774. close_current_menu();
  775. invalidate();
  776. return 0;
  777. }
  778. void WSWindowManager::destroy_all_menus(Process& process)
  779. {
  780. LOCKER(m_lock);
  781. Vector<int> menu_ids;
  782. bool should_close_current_menu = false;
  783. for (auto& it : m_menus) {
  784. if (it.value->process() == &process)
  785. menu_ids.append(it.value->menu_id());
  786. if (m_current_menu == it.value.ptr())
  787. should_close_current_menu = true;
  788. }
  789. if (should_close_current_menu)
  790. close_current_menu();
  791. for (int menu_id : menu_ids)
  792. m_menus.remove(menu_id);
  793. Vector<int> menubar_ids;
  794. bool should_close_current_menubar = false;
  795. for (auto& it : m_menubars) {
  796. if (it.value->process() == &process)
  797. menubar_ids.append(it.value->menubar_id());
  798. if (m_current_menubar == it.value.ptr())
  799. should_close_current_menubar = true;
  800. }
  801. if (should_close_current_menubar)
  802. set_current_menubar(nullptr);
  803. for (int menubar_id : menubar_ids)
  804. m_menubars.remove(menubar_id);
  805. }