WSWindowManager.cpp 38 KB

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