WSWindowManager.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include <SharedGraphics/Font.h>
  6. #include <SharedGraphics/Painter.h>
  7. #include <SharedGraphics/CharacterBitmap.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <LibC/errno_numbers.h>
  10. #include "WSMenu.h"
  11. #include "WSMenuBar.h"
  12. #include "WSMenuItem.h"
  13. #include <WindowServer/WSClientConnection.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #ifdef KERNEL
  18. #include <Kernel/ProcFS.h>
  19. #endif
  20. //#define DEBUG_COUNTERS
  21. //#define DEBUG_WID_IN_TITLE_BAR
  22. #define RESIZE_DEBUG
  23. #define USE_WALLPAPER
  24. static const int window_titlebar_height = 16;
  25. static inline Rect menu_window_rect(const Rect& rect)
  26. {
  27. return rect.inflated(2, 2);
  28. }
  29. static inline Rect title_bar_rect(const Rect& window)
  30. {
  31. return {
  32. window.x() - 1,
  33. window.y() - window_titlebar_height,
  34. window.width() + 2,
  35. window_titlebar_height
  36. };
  37. }
  38. static inline Rect title_bar_text_rect(const Rect& window)
  39. {
  40. auto titlebar_rect = title_bar_rect(window);
  41. return {
  42. titlebar_rect.x() + 2,
  43. titlebar_rect.y(),
  44. titlebar_rect.width() - 4,
  45. titlebar_rect.height()
  46. };
  47. }
  48. static inline Rect close_button_rect_for_window(const Rect& window_rect)
  49. {
  50. auto titlebar_inner_rect = title_bar_text_rect(window_rect);
  51. int close_button_margin = 1;
  52. int close_button_size = titlebar_inner_rect.height() - close_button_margin * 2;
  53. return Rect {
  54. titlebar_inner_rect.right() - close_button_size + 1,
  55. titlebar_inner_rect.top() + close_button_margin,
  56. close_button_size,
  57. close_button_size - 1
  58. };
  59. }
  60. static inline Rect border_window_rect(const Rect& window)
  61. {
  62. auto titlebar_rect = title_bar_rect(window);
  63. return { titlebar_rect.x() - 1,
  64. titlebar_rect.y() - 1,
  65. titlebar_rect.width() + 2,
  66. window_titlebar_height + window.height() + 3
  67. };
  68. }
  69. static inline Rect outer_window_rect(const Rect& window)
  70. {
  71. auto rect = border_window_rect(window);
  72. rect.inflate(2, 2);
  73. return rect;
  74. }
  75. static WSWindowManager* s_the;
  76. WSWindowManager& WSWindowManager::the()
  77. {
  78. ASSERT(s_the);
  79. return *s_the;
  80. }
  81. static const char* cursor_bitmap_inner_ascii = {
  82. " # "
  83. " ## "
  84. " ### "
  85. " #### "
  86. " ##### "
  87. " ###### "
  88. " ####### "
  89. " ######## "
  90. " ######### "
  91. " ########## "
  92. " ###### "
  93. " ## ## "
  94. " # ## "
  95. " ## "
  96. " ## "
  97. " ## "
  98. " "
  99. };
  100. static const char* cursor_bitmap_outer_ascii = {
  101. "## "
  102. "# # "
  103. "# # "
  104. "# # "
  105. "# # "
  106. "# # "
  107. "# # "
  108. "# # "
  109. "# # "
  110. "# # "
  111. "# #### "
  112. "# ## # "
  113. "# # # # "
  114. "## # # "
  115. " # # "
  116. " # # "
  117. " ## "
  118. };
  119. void WSWindowManager::flip_buffers()
  120. {
  121. swap(m_front_bitmap, m_back_bitmap);
  122. swap(m_front_painter, m_back_painter);
  123. int new_y_offset = m_buffers_are_flipped ? 0 : m_screen_rect.height();
  124. WSScreen::the().set_y_offset(new_y_offset);
  125. m_buffers_are_flipped = !m_buffers_are_flipped;
  126. }
  127. WSWindowManager::WSWindowManager()
  128. : m_screen(WSScreen::the())
  129. , m_screen_rect(m_screen.rect())
  130. , m_flash_flush(false)
  131. {
  132. s_the = this;
  133. #ifndef DEBUG_COUNTERS
  134. (void)m_compose_count;
  135. (void)m_flush_count;
  136. #endif
  137. auto size = m_screen_rect.size();
  138. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(0));
  139. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(size.height()));
  140. m_front_painter = make<Painter>(*m_front_bitmap);
  141. m_back_painter = make<Painter>(*m_back_bitmap);
  142. m_font = Font::default_font();
  143. m_front_painter->set_font(font());
  144. m_back_painter->set_font(font());
  145. m_background_color = Color(50, 50, 50);
  146. m_active_window_border_color = Color(110, 34, 9);
  147. m_active_window_border_color2 = Color(244, 202, 158);
  148. m_active_window_title_color = Color::White;
  149. m_inactive_window_border_color = Color(128, 128, 128);
  150. m_inactive_window_border_color2 = Color(192, 192, 192);
  151. m_inactive_window_title_color = Color(213, 208, 199);
  152. m_dragging_window_border_color = Color(161, 50, 13);
  153. m_dragging_window_border_color2 = Color(250, 220, 187);
  154. m_dragging_window_title_color = Color::White;
  155. m_cursor_bitmap_inner = CharacterBitmap::create_from_ascii(cursor_bitmap_inner_ascii, 12, 17);
  156. m_cursor_bitmap_outer = CharacterBitmap::create_from_ascii(cursor_bitmap_outer_ascii, 12, 17);
  157. #ifdef USE_WALLPAPER
  158. m_wallpaper_path = "/res/wallpapers/cool.rgb";
  159. m_wallpaper = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, m_wallpaper_path, { 1024, 768 });
  160. #endif
  161. #ifdef KERNEL
  162. ProcFS::the().add_sys_bool("wm_flash_flush", m_flash_flush);
  163. ProcFS::the().add_sys_string("wm_wallpaper", m_wallpaper_path, [this] {
  164. m_wallpaper = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, m_wallpaper_path, m_screen_rect.size());
  165. invalidate(m_screen_rect);
  166. });
  167. #endif
  168. m_menu_selection_color = Color::from_rgb(0x84351a);
  169. {
  170. byte system_menu_name[] = { 0xf8, 0 };
  171. m_system_menu = make<WSMenu>(nullptr, -1, String((const char*)system_menu_name));
  172. m_system_menu->add_item(make<WSMenuItem>(0, "Launch Terminal"));
  173. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  174. m_system_menu->add_item(make<WSMenuItem>(1, "640x480"));
  175. m_system_menu->add_item(make<WSMenuItem>(2, "800x600"));
  176. m_system_menu->add_item(make<WSMenuItem>(3, "1024x768"));
  177. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  178. m_system_menu->add_item(make<WSMenuItem>(4, "About..."));
  179. m_system_menu->on_item_activation = [this] (WSMenuItem& item) {
  180. if (item.identifier() == 0) {
  181. if (fork() == 0) {
  182. execl("/bin/Terminal", "/bin/Terminal", nullptr);
  183. ASSERT_NOT_REACHED();
  184. }
  185. return;
  186. }
  187. switch (item.identifier()) {
  188. case 1: set_resolution(640, 480); break;
  189. case 2: set_resolution(800, 600); break;
  190. case 3: set_resolution(1024, 768); break;
  191. }
  192. if (item.identifier() == 4) {
  193. if (fork() == 0) {
  194. execl("/bin/About", "/bin/About", nullptr);
  195. ASSERT_NOT_REACHED();
  196. }
  197. return;
  198. }
  199. dbgprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  200. };
  201. }
  202. // NOTE: This ensures that the system menu has the correct dimensions.
  203. set_current_menubar(nullptr);
  204. WSMessageLoop::the().start_timer(300, [this] {
  205. static time_t last_update_time;
  206. time_t now = time(nullptr);
  207. if (now != last_update_time) {
  208. tick_clock();
  209. last_update_time = now;
  210. }
  211. });
  212. invalidate();
  213. compose();
  214. }
  215. WSWindowManager::~WSWindowManager()
  216. {
  217. }
  218. static void get_cpu_usage(unsigned& busy, unsigned& idle)
  219. {
  220. busy = 0;
  221. idle = 0;
  222. FILE* fp = fopen("/proc/all", "r");
  223. if (!fp) {
  224. perror("failed to open /proc/all");
  225. exit(1);
  226. }
  227. for (;;) {
  228. char buf[BUFSIZ];
  229. char* ptr = fgets(buf, sizeof(buf), fp);
  230. if (!ptr)
  231. break;
  232. auto parts = String(buf, Chomp).split(',');
  233. if (parts.size() < 17)
  234. break;
  235. bool ok;
  236. pid_t pid = parts[0].to_uint(ok);
  237. ASSERT(ok);
  238. unsigned nsched = parts[1].to_uint(ok);
  239. ASSERT(ok);
  240. if (pid == 0)
  241. idle += nsched;
  242. else
  243. busy += nsched;
  244. }
  245. int rc = fclose(fp);
  246. ASSERT(rc == 0);
  247. }
  248. void WSWindowManager::tick_clock()
  249. {
  250. static unsigned last_busy;
  251. static unsigned last_idle;
  252. unsigned busy;
  253. unsigned idle;
  254. get_cpu_usage(busy, idle);
  255. unsigned busy_diff = busy - last_busy;
  256. unsigned idle_diff = idle - last_idle;
  257. last_busy = busy;
  258. last_idle = idle;
  259. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  260. m_cpu_history.enqueue(cpu);
  261. invalidate(menubar_rect());
  262. }
  263. void WSWindowManager::set_resolution(int width, int height)
  264. {
  265. if (m_screen_rect.width() == width && m_screen_rect.height() == height)
  266. return;
  267. m_screen.set_resolution(width, height);
  268. m_screen_rect = m_screen.rect();
  269. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(0));
  270. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(height));
  271. m_front_painter = make<Painter>(*m_front_bitmap);
  272. m_back_painter = make<Painter>(*m_back_bitmap);
  273. m_buffers_are_flipped = false;
  274. invalidate();
  275. compose();
  276. }
  277. template<typename Callback>
  278. void WSWindowManager::for_each_active_menubar_menu(Callback callback)
  279. {
  280. callback(*m_system_menu);
  281. if (m_current_menubar)
  282. m_current_menubar->for_each_menu(callback);
  283. }
  284. int WSWindowManager::menubar_menu_margin() const
  285. {
  286. return 16;
  287. }
  288. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  289. {
  290. if (menubar)
  291. m_current_menubar = menubar->make_weak_ptr();
  292. else
  293. m_current_menubar = nullptr;
  294. dbgprintf("[WM] Current menubar is now %p\n", menubar);
  295. Point next_menu_location { menubar_menu_margin() / 2, 3 };
  296. for_each_active_menubar_menu([&] (WSMenu& menu) {
  297. int text_width = font().width(menu.name());
  298. menu.set_rect_in_menubar({ next_menu_location.x() - menubar_menu_margin() / 2, 0, text_width + menubar_menu_margin(), menubar_rect().height() - 1 });
  299. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, font().glyph_height() } });
  300. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  301. return true;
  302. });
  303. invalidate(menubar_rect());
  304. }
  305. static const char* s_close_button_bitmap_data = {
  306. "## ##"
  307. "### ###"
  308. " ###### "
  309. " #### "
  310. " ## "
  311. " #### "
  312. " ###### "
  313. "### ###"
  314. "## ##"
  315. };
  316. static CharacterBitmap* s_close_button_bitmap;
  317. static const int s_close_button_bitmap_width = 8;
  318. static const int s_close_button_bitmap_height = 9;
  319. void WSWindowManager::paint_window_frame(WSWindow& window)
  320. {
  321. //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());
  322. if (window.type() == WSWindowType::Menu) {
  323. m_back_painter->draw_rect(menu_window_rect(window.rect()), Color::LightGray);
  324. return;
  325. }
  326. auto titlebar_rect = title_bar_rect(window.rect());
  327. auto titlebar_inner_rect = title_bar_text_rect(window.rect());
  328. auto outer_rect = outer_window_rect(window.rect());
  329. auto border_rect = border_window_rect(window.rect());
  330. auto close_button_rect = close_button_rect_for_window(window.rect());
  331. auto titlebar_title_rect = titlebar_inner_rect;
  332. titlebar_title_rect.set_width(font().glyph_width() * window.title().length());
  333. Rect inner_border_rect {
  334. window.x() - 1,
  335. window.y() - 1,
  336. window.width() + 2,
  337. window.height() + 2
  338. };
  339. Color title_color;
  340. Color border_color;
  341. Color border_color2;
  342. Color middle_border_color;
  343. if (&window == m_drag_window.ptr()) {
  344. border_color = m_dragging_window_border_color;
  345. border_color2 = m_dragging_window_border_color2;
  346. title_color = m_dragging_window_title_color;
  347. middle_border_color = Color::White;
  348. } else if (&window == m_active_window.ptr()) {
  349. border_color = m_active_window_border_color;
  350. border_color2 = m_active_window_border_color2;
  351. title_color = m_active_window_title_color;
  352. middle_border_color = Color::MidGray;
  353. } else {
  354. border_color = m_inactive_window_border_color;
  355. border_color2 = m_inactive_window_border_color2;
  356. title_color = m_inactive_window_title_color;
  357. middle_border_color = Color::MidGray;
  358. }
  359. m_back_painter->fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
  360. for (int i = 2; i <= titlebar_inner_rect.height() - 4; i += 2) {
  361. 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);
  362. }
  363. m_back_painter->draw_rect(border_rect, middle_border_color);
  364. m_back_painter->draw_rect(outer_rect, border_color);
  365. m_back_painter->draw_rect(inner_border_rect, border_color);
  366. m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
  367. if (!s_close_button_bitmap)
  368. 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();
  369. m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);
  370. m_back_painter->draw_rect(close_button_rect, Color::DarkGray, true);
  371. auto x_location = close_button_rect.center();
  372. x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));
  373. m_back_painter->draw_bitmap(x_location, *s_close_button_bitmap, Color::Black);
  374. #ifdef DEBUG_WID_IN_TITLE_BAR
  375. Color metadata_color(96, 96, 96);
  376. m_back_painter->draw_text(
  377. titlebar_inner_rect,
  378. String::format("%d:%d", window.pid(), window.window_id()),
  379. TextAlignment::CenterRight,
  380. metadata_color
  381. );
  382. #endif
  383. }
  384. void WSWindowManager::add_window(WSWindow& window)
  385. {
  386. m_windows.set(&window);
  387. m_windows_in_order.append(&window);
  388. if (!active_window())
  389. set_active_window(&window);
  390. }
  391. void WSWindowManager::move_to_front(WSWindow& window)
  392. {
  393. if (m_windows_in_order.tail() != &window)
  394. invalidate(window);
  395. m_windows_in_order.remove(&window);
  396. m_windows_in_order.append(&window);
  397. }
  398. void WSWindowManager::remove_window(WSWindow& window)
  399. {
  400. if (!m_windows.contains(&window))
  401. return;
  402. invalidate(window);
  403. m_windows.remove(&window);
  404. m_windows_in_order.remove(&window);
  405. if (!active_window() && !m_windows.is_empty())
  406. set_active_window(*m_windows.begin());
  407. }
  408. void WSWindowManager::notify_title_changed(WSWindow& window)
  409. {
  410. printf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  411. invalidate(outer_window_rect(window.rect()));
  412. }
  413. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  414. {
  415. 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());
  416. invalidate(outer_window_rect(old_rect));
  417. invalidate(outer_window_rect(new_rect));
  418. }
  419. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, WSMouseEvent& event)
  420. {
  421. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu;
  422. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  423. bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  424. if (should_open_menu) {
  425. if (current_menu() == &menu)
  426. return;
  427. close_current_menu();
  428. if (!menu.is_empty()) {
  429. auto& menu_window = menu.ensure_menu_window();
  430. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  431. menu_window.set_visible(true);
  432. }
  433. m_current_menu = menu.make_weak_ptr();
  434. return;
  435. }
  436. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  437. close_current_menu();
  438. return;
  439. }
  440. }
  441. void WSWindowManager::close_current_menu()
  442. {
  443. if (m_current_menu && m_current_menu->menu_window())
  444. m_current_menu->menu_window()->set_visible(false);
  445. m_current_menu = nullptr;
  446. }
  447. void WSWindowManager::handle_menubar_mouse_event(WSMouseEvent& event)
  448. {
  449. for_each_active_menubar_menu([&] (WSMenu& menu) {
  450. if (menu.rect_in_menubar().contains(event.position())) {
  451. handle_menu_mouse_event(menu, event);
  452. return false;
  453. }
  454. return true;
  455. });
  456. }
  457. void WSWindowManager::handle_titlebar_mouse_event(WSWindow& window, WSMouseEvent& event)
  458. {
  459. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  460. #ifdef DRAG_DEBUG
  461. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  462. #endif
  463. m_drag_window = window.make_weak_ptr();;
  464. m_drag_origin = event.position();
  465. m_drag_window_origin = window.position();
  466. invalidate(window);
  467. return;
  468. }
  469. }
  470. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  471. {
  472. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  473. WSMessage message(WSMessage::WindowCloseRequest);
  474. window.on_message(message);
  475. return;
  476. }
  477. }
  478. void WSWindowManager::start_window_resize(WSWindow& window, WSMouseEvent& event)
  479. {
  480. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  481. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  482. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  483. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  484. };
  485. Rect window_rect = window.rect();
  486. int window_relative_x = event.x() - window_rect.x();
  487. int window_relative_y = event.y() - window_rect.y();
  488. int hot_area_row = window_relative_y / (window_rect.height() / 3);
  489. int hot_area_column = window_relative_x / (window_rect.width() / 3);
  490. ASSERT(hot_area_row >= 0 && hot_area_row <= 2);
  491. ASSERT(hot_area_column >= 0 && hot_area_column <= 2);
  492. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  493. if (m_resize_direction == ResizeDirection::None) {
  494. ASSERT(!m_resize_window);
  495. return;
  496. }
  497. #ifdef RESIZE_DEBUG
  498. printf("[WM] Begin resizing WSWindow{%p}\n", &window);
  499. #endif
  500. m_resize_window = window.make_weak_ptr();;
  501. m_resize_origin = event.position();
  502. m_resize_window_original_rect = window.rect();
  503. m_resize_window->set_has_painted_since_last_resize(true);
  504. invalidate(window);
  505. }
  506. void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& event_window)
  507. {
  508. event_window = nullptr;
  509. if (m_drag_window) {
  510. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  511. #ifdef DRAG_DEBUG
  512. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  513. #endif
  514. invalidate(*m_drag_window);
  515. m_drag_window = nullptr;
  516. return;
  517. }
  518. if (event.type() == WSMessage::MouseMove) {
  519. auto old_window_rect = m_drag_window->rect();
  520. Point pos = m_drag_window_origin;
  521. #ifdef DRAG_DEBUG
  522. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  523. #endif
  524. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  525. m_drag_window->set_position_without_repaint(pos);
  526. invalidate(outer_window_rect(old_window_rect));
  527. invalidate(outer_window_rect(m_drag_window->rect()));
  528. return;
  529. }
  530. }
  531. if (m_resize_window) {
  532. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Right) {
  533. #ifdef RESIZE_DEBUG
  534. printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr());
  535. #endif
  536. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  537. invalidate(*m_resize_window);
  538. m_resize_window = nullptr;
  539. return;
  540. }
  541. if (event.type() == WSMessage::MouseMove) {
  542. auto old_rect = m_resize_window->rect();
  543. int diff_x = event.x() - m_resize_origin.x();
  544. int diff_y = event.y() - m_resize_origin.y();
  545. int change_x = 0;
  546. int change_y = 0;
  547. int change_w = 0;
  548. int change_h = 0;
  549. switch (m_resize_direction) {
  550. case ResizeDirection::DownRight:
  551. change_w = diff_x;
  552. change_h = diff_y;
  553. break;
  554. case ResizeDirection::Right:
  555. change_w = diff_x;
  556. break;
  557. case ResizeDirection::UpRight:
  558. change_w = diff_x;
  559. change_y = diff_y;
  560. change_h = -diff_y;
  561. break;
  562. case ResizeDirection::Up:
  563. change_y = diff_y;
  564. change_h = -diff_y;
  565. break;
  566. case ResizeDirection::UpLeft:
  567. change_x = diff_x;
  568. change_w = -diff_x;
  569. change_y = diff_y;
  570. change_h = -diff_y;
  571. break;
  572. case ResizeDirection::Left:
  573. change_x = diff_x;
  574. change_w = -diff_x;
  575. break;
  576. case ResizeDirection::DownLeft:
  577. change_x = diff_x;
  578. change_w = -diff_x;
  579. change_h = diff_y;
  580. break;
  581. case ResizeDirection::Down:
  582. change_h = diff_y;
  583. break;
  584. default:
  585. ASSERT_NOT_REACHED();
  586. }
  587. auto new_rect = m_resize_window_original_rect;
  588. Size minimum_size { 50, 50 };
  589. new_rect.set_x(new_rect.x() + change_x);
  590. new_rect.set_y(new_rect.y() + change_y);
  591. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  592. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  593. if (!m_resize_window->size_increment().is_null()) {
  594. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  595. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  596. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  597. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  598. }
  599. if (m_resize_window->rect() == new_rect)
  600. return;
  601. #ifdef RESIZE_DEBUG
  602. dbgprintf("[WM] Resizing [original: %s] now: %s\n",
  603. m_resize_window_original_rect.to_string().characters(),
  604. new_rect.to_string().characters());
  605. #endif
  606. m_resize_window->set_rect(new_rect);
  607. if (m_resize_window->has_painted_since_last_resize()) {
  608. m_resize_window->set_has_painted_since_last_resize(false);
  609. dbgprintf("I'm gonna wait for %s\n", new_rect.to_string().characters());
  610. m_resize_window->set_last_lazy_resize_rect(new_rect);
  611. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
  612. }
  613. return;
  614. }
  615. }
  616. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  617. if (!window->global_cursor_tracking())
  618. continue;
  619. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  620. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  621. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  622. window->on_message(*local_event);
  623. }
  624. if (menubar_rect().contains(event.position())) {
  625. handle_menubar_mouse_event(event);
  626. return;
  627. }
  628. if (m_current_menu && m_current_menu->menu_window()) {
  629. bool event_is_inside_current_menu = m_current_menu->menu_window()->rect().contains(event.position());
  630. if (!event_is_inside_current_menu) {
  631. if (m_current_menu->hovered_item())
  632. m_current_menu->clear_hovered_item();
  633. if (event.type() == WSMessage::MouseDown || event.type() == WSMessage::MouseUp)
  634. close_current_menu();
  635. }
  636. }
  637. for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
  638. if (window.type() != WSWindowType::Menu && title_bar_rect(window.rect()).contains(event.position())) {
  639. if (event.type() == WSMessage::MouseDown) {
  640. move_to_front(window);
  641. set_active_window(&window);
  642. }
  643. if (close_button_rect_for_window(window.rect()).contains(event.position())) {
  644. handle_close_button_mouse_event(window, event);
  645. return IterationDecision::Abort;
  646. }
  647. handle_titlebar_mouse_event(window, event);
  648. return IterationDecision::Abort;
  649. }
  650. if (window.rect().contains(event.position())) {
  651. if (window.type() != WSWindowType::Menu && event.type() == WSMessage::MouseDown) {
  652. move_to_front(window);
  653. set_active_window(&window);
  654. }
  655. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Right) {
  656. start_window_resize(window, event);
  657. return IterationDecision::Abort;
  658. }
  659. event_window = &window;
  660. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  661. Point position { event.x() - window.rect().x(), event.y() - window.rect().y() };
  662. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button());
  663. window.on_message(*local_event);
  664. return IterationDecision::Abort;
  665. }
  666. return IterationDecision::Continue;
  667. });
  668. }
  669. template<typename Callback>
  670. IterationDecision WSWindowManager::for_each_visible_window_of_type_from_back_to_front(WSWindowType type, Callback callback)
  671. {
  672. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  673. if (!window->is_visible())
  674. continue;
  675. if (window->type() != type)
  676. continue;
  677. if (callback(*window) == IterationDecision::Abort)
  678. return IterationDecision::Abort;
  679. }
  680. return IterationDecision::Continue;
  681. }
  682. template<typename Callback>
  683. IterationDecision WSWindowManager::for_each_visible_window_from_back_to_front(Callback callback)
  684. {
  685. if (for_each_visible_window_of_type_from_back_to_front(WSWindowType::Normal, callback) == IterationDecision::Abort)
  686. return IterationDecision::Abort;
  687. return for_each_visible_window_of_type_from_back_to_front(WSWindowType::Menu, callback);
  688. }
  689. template<typename Callback>
  690. IterationDecision WSWindowManager::for_each_visible_window_of_type_from_front_to_back(WSWindowType type, Callback callback)
  691. {
  692. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  693. if (!window->is_visible())
  694. continue;
  695. if (window->type() != type)
  696. continue;
  697. if (callback(*window) == IterationDecision::Abort)
  698. return IterationDecision::Abort;
  699. }
  700. return IterationDecision::Continue;
  701. }
  702. template<typename Callback>
  703. IterationDecision WSWindowManager::for_each_visible_window_from_front_to_back(Callback callback)
  704. {
  705. if (for_each_visible_window_of_type_from_front_to_back(WSWindowType::Menu, callback) == IterationDecision::Abort)
  706. return IterationDecision::Abort;
  707. return for_each_visible_window_of_type_from_front_to_back(WSWindowType::Normal, callback);
  708. }
  709. void WSWindowManager::compose()
  710. {
  711. auto dirty_rects = move(m_dirty_rects);
  712. auto cursor_location = m_screen.cursor_location();
  713. dirty_rects.add(m_last_cursor_rect);
  714. dirty_rects.add({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  715. #ifdef DEBUG_COUNTERS
  716. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  717. #endif
  718. auto any_opaque_window_contains_rect = [this] (const Rect& r) {
  719. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  720. if (!window->is_visible())
  721. continue;
  722. if (window->opacity() < 1.0f)
  723. continue;
  724. if (window->has_alpha_channel()) {
  725. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  726. // Maybe there's some way we could know this?
  727. continue;
  728. }
  729. if (outer_window_rect(window->rect()).contains(r))
  730. return true;
  731. }
  732. return false;
  733. };
  734. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  735. auto window_rect = outer_window_rect(window.rect());
  736. for (auto& dirty_rect : dirty_rects.rects()) {
  737. if (dirty_rect.intersects(window_rect))
  738. return true;
  739. }
  740. return false;
  741. };
  742. for (auto& dirty_rect : dirty_rects.rects()) {
  743. if (any_opaque_window_contains_rect(dirty_rect))
  744. continue;
  745. if (!m_wallpaper)
  746. m_back_painter->fill_rect(dirty_rect, m_background_color);
  747. else
  748. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  749. }
  750. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  751. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  752. if (!backing_store)
  753. return IterationDecision::Continue;
  754. if (!any_dirty_rect_intersects_window(window))
  755. return IterationDecision::Continue;
  756. for (auto& dirty_rect : dirty_rects.rects()) {
  757. m_back_painter->set_clip_rect(dirty_rect);
  758. paint_window_frame(window);
  759. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  760. if (dirty_rect_in_window_coordinates.is_empty())
  761. continue;
  762. dirty_rect_in_window_coordinates.set_x(dirty_rect_in_window_coordinates.x() - window.x());
  763. dirty_rect_in_window_coordinates.set_y(dirty_rect_in_window_coordinates.y() - window.y());
  764. auto dst = window.position();
  765. dst.move_by(dirty_rect_in_window_coordinates.location());
  766. if (window.opacity() == 1.0f)
  767. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates);
  768. else
  769. m_back_painter->blit_with_opacity(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  770. m_back_painter->clear_clip_rect();
  771. }
  772. m_back_painter->clear_clip_rect();
  773. return IterationDecision::Continue;
  774. });
  775. draw_menubar();
  776. draw_cursor();
  777. if (m_flash_flush) {
  778. for (auto& rect : dirty_rects.rects())
  779. m_front_painter->fill_rect(rect, Color::Yellow);
  780. }
  781. flip_buffers();
  782. for (auto& r : dirty_rects.rects())
  783. flush(r);
  784. }
  785. void WSWindowManager::invalidate_cursor()
  786. {
  787. auto cursor_location = m_screen.cursor_location();
  788. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  789. invalidate(cursor_rect);
  790. }
  791. Rect WSWindowManager::menubar_rect() const
  792. {
  793. return { 0, 0, m_screen_rect.width(), 16 };
  794. }
  795. void WSWindowManager::draw_menubar()
  796. {
  797. m_back_painter->fill_rect(menubar_rect(), Color::LightGray);
  798. m_back_painter->draw_line({ 0, menubar_rect().bottom() }, { menubar_rect().right(), menubar_rect().bottom() }, Color::White);
  799. for_each_active_menubar_menu([&] (WSMenu& menu) {
  800. Color text_color = Color::Black;
  801. if (&menu == current_menu()) {
  802. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  803. text_color = Color::White;
  804. }
  805. m_back_painter->draw_text(menu.text_rect_in_menubar(), menu.name(), TextAlignment::CenterLeft, text_color);
  806. return true;
  807. });
  808. time_t now = time(nullptr);
  809. auto* tm = localtime(&now);
  810. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u\n",
  811. tm->tm_year + 1900,
  812. tm->tm_mon + 1,
  813. tm->tm_mday,
  814. tm->tm_hour,
  815. tm->tm_min,
  816. tm->tm_sec);
  817. auto time_rect = menubar_rect().translated(-(menubar_menu_margin() / 2), 0);
  818. m_back_painter->draw_text(time_rect, time_text, TextAlignment::CenterRight, Color::Black);
  819. Rect cpu_rect { time_rect.right() - font().glyph_width() * time_text.length() - (int)m_cpu_history.capacity() - 10, time_rect.y() + 1, (int)m_cpu_history.capacity(), time_rect.height() - 2 };
  820. m_back_painter->fill_rect(cpu_rect, Color::Black);
  821. int i = m_cpu_history.capacity() - m_cpu_history.size();
  822. for (auto cpu_usage : m_cpu_history) {
  823. m_back_painter->draw_line(
  824. { cpu_rect.x() + i, cpu_rect.bottom() },
  825. { cpu_rect.x() + i, (int)(cpu_rect.y() + (cpu_rect.height() - (cpu_usage * (float)cpu_rect.height()))) },
  826. Color(0, 200, 0)
  827. );
  828. ++i;
  829. }
  830. }
  831. void WSWindowManager::draw_cursor()
  832. {
  833. auto cursor_location = m_screen.cursor_location();
  834. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  835. Color inner_color = Color::White;
  836. Color outer_color = Color::Black;
  837. if (m_screen.left_mouse_button_pressed())
  838. swap(inner_color, outer_color);
  839. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  840. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  841. m_last_cursor_rect = cursor_rect;
  842. }
  843. void WSWindowManager::on_message(WSMessage& message)
  844. {
  845. if (message.is_mouse_event()) {
  846. WSWindow* event_window = nullptr;
  847. process_mouse_event(static_cast<WSMouseEvent&>(message), event_window);
  848. set_hovered_window(event_window);
  849. return;
  850. }
  851. if (message.is_key_event()) {
  852. // FIXME: This is a good place to hook key events globally. :)
  853. if (m_active_window)
  854. return m_active_window->on_message(message);
  855. return;
  856. }
  857. if (message.type() == WSMessage::WM_DeferredCompose) {
  858. m_pending_compose_event = false;
  859. compose();
  860. return;
  861. }
  862. }
  863. void WSWindowManager::set_active_window(WSWindow* window)
  864. {
  865. if (window->type() == WSWindowType::Menu) {
  866. dbgprintf("WSWindowManager: Attempted to make a menu window active.\n");
  867. return;
  868. }
  869. if (window == m_active_window.ptr())
  870. return;
  871. if (auto* previously_active_window = m_active_window.ptr()) {
  872. WSMessageLoop::the().post_message(*previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  873. invalidate(*previously_active_window);
  874. }
  875. m_active_window = window->make_weak_ptr();
  876. if (m_active_window) {
  877. WSMessageLoop::the().post_message(*m_active_window, make<WSMessage>(WSMessage::WindowActivated));
  878. invalidate(*m_active_window);
  879. auto* client = window->client();
  880. ASSERT(client);
  881. set_current_menubar(client->app_menubar());
  882. }
  883. }
  884. void WSWindowManager::set_hovered_window(WSWindow* window)
  885. {
  886. if (m_hovered_window.ptr() == window)
  887. return;
  888. if (m_hovered_window)
  889. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowLeft));
  890. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  891. if (m_hovered_window)
  892. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowEntered));
  893. }
  894. void WSWindowManager::invalidate()
  895. {
  896. m_dirty_rects.clear_with_capacity();
  897. invalidate(m_screen_rect);
  898. }
  899. void WSWindowManager::recompose_immediately()
  900. {
  901. m_dirty_rects.clear_with_capacity();
  902. invalidate(m_screen_rect, false);
  903. }
  904. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  905. {
  906. auto rect = Rect::intersection(a_rect, m_screen_rect);
  907. if (rect.is_empty())
  908. return;
  909. m_dirty_rects.add(rect);
  910. if (should_schedule_compose_event && !m_pending_compose_event) {
  911. WSMessageLoop::the().post_message(*this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  912. m_pending_compose_event = true;
  913. }
  914. }
  915. void WSWindowManager::invalidate(const WSWindow& window)
  916. {
  917. if (window.type() == WSWindowType::Menu) {
  918. invalidate(menu_window_rect(window.rect()));
  919. return;
  920. }
  921. if (window.type() == WSWindowType::Normal) {
  922. invalidate(outer_window_rect(window.rect()));
  923. return;
  924. }
  925. ASSERT_NOT_REACHED();
  926. }
  927. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  928. {
  929. if (rect.is_empty()) {
  930. invalidate(window);
  931. return;
  932. }
  933. auto outer_rect = outer_window_rect(window.rect());
  934. auto inner_rect = rect;
  935. inner_rect.move_by(window.position());
  936. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  937. inner_rect.intersect(outer_rect);
  938. invalidate(inner_rect);
  939. }
  940. void WSWindowManager::flush(const Rect& a_rect)
  941. {
  942. auto rect = Rect::intersection(a_rect, m_screen_rect);
  943. #ifdef DEBUG_COUNTERS
  944. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  945. #endif
  946. RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  947. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  948. size_t pitch = m_back_bitmap->pitch();
  949. for (int y = 0; y < rect.height(); ++y) {
  950. fast_dword_copy(back_ptr, front_ptr, rect.width());
  951. front_ptr = (RGBA32*)((byte*)front_ptr + pitch);
  952. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  953. }
  954. }
  955. void WSWindowManager::close_menu(WSMenu& menu)
  956. {
  957. if (current_menu() == &menu)
  958. close_current_menu();
  959. }
  960. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  961. {
  962. if (current_menubar() == &menubar)
  963. set_current_menubar(nullptr);
  964. }
  965. const WSClientConnection* WSWindowManager::active_client() const
  966. {
  967. if (m_active_window)
  968. return m_active_window->client();
  969. return nullptr;
  970. }
  971. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  972. {
  973. if (active_client() == &client)
  974. set_current_menubar(client.app_menubar());
  975. invalidate(menubar_rect());
  976. }