WSWindowManager.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  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(WSMouseEvent(event.type(), event.position().translated(-window->position()), event.buttons(), event.button(), event.modifiers()));
  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. if (!window.frame().rect().contains(event.position()))
  567. return IterationDecision::Continue;
  568. // First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
  569. // In those cases, the event is swallowed by the window manager.
  570. if (window.type() == WSWindowType::Normal) {
  571. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  572. start_window_drag(window, event);
  573. return IterationDecision::Abort;
  574. }
  575. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  576. start_window_resize(window, event);
  577. return IterationDecision::Abort;
  578. }
  579. }
  580. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  581. if (window.rect().contains(event.position())) {
  582. if (window.type() == WSWindowType::Normal && event.type() == WSMessage::MouseDown)
  583. move_to_front_and_make_active(window);
  584. event_window = &window;
  585. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window))
  586. window.on_message(WSMouseEvent(event.type(), event.position().translated(-window.position()), event.buttons(), event.button(), event.modifiers()));
  587. return IterationDecision::Abort;
  588. }
  589. // We are hitting the frame, pass the event along to WSWindowFrame.
  590. window.frame().on_mouse_event(event);
  591. return IterationDecision::Abort;
  592. });
  593. }
  594. void WSWindowManager::compose()
  595. {
  596. auto dirty_rects = move(m_dirty_rects);
  597. dirty_rects.add(Rect::intersection(m_last_cursor_rect, m_screen_rect));
  598. dirty_rects.add(Rect::intersection(current_cursor_rect(), m_screen_rect));
  599. #ifdef DEBUG_COUNTERS
  600. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  601. #endif
  602. auto any_opaque_window_contains_rect = [this] (const Rect& r) {
  603. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  604. if (!window->is_visible())
  605. continue;
  606. if (window->opacity() < 1.0f)
  607. continue;
  608. if (window->has_alpha_channel()) {
  609. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  610. // Maybe there's some way we could know this?
  611. continue;
  612. }
  613. if (window->frame().rect().contains(r))
  614. return true;
  615. }
  616. return false;
  617. };
  618. auto any_opaque_window_above_this_one_contains_rect = [this] (const WSWindow& a_window, const Rect& rect) -> bool {
  619. bool found = false;
  620. bool checking = false;
  621. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  622. if (&window == &a_window) {
  623. checking = true;
  624. return IterationDecision::Continue;
  625. }
  626. if (!checking)
  627. return IterationDecision::Continue;
  628. if (!window.is_visible())
  629. return IterationDecision::Continue;;
  630. if (window.opacity() < 1.0f)
  631. return IterationDecision::Continue;;
  632. if (window.has_alpha_channel())
  633. return IterationDecision::Continue;;
  634. if (window.frame().rect().contains(rect)) {
  635. found = true;
  636. return IterationDecision::Abort;
  637. }
  638. return IterationDecision::Continue;
  639. });
  640. return found;
  641. };
  642. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  643. auto window_frame_rect = window.frame().rect();
  644. for (auto& dirty_rect : dirty_rects.rects()) {
  645. if (dirty_rect.intersects(window_frame_rect))
  646. return true;
  647. }
  648. return false;
  649. };
  650. for (auto& dirty_rect : dirty_rects.rects()) {
  651. if (any_opaque_window_contains_rect(dirty_rect))
  652. continue;
  653. if (!m_wallpaper)
  654. m_back_painter->fill_rect(dirty_rect, m_background_color);
  655. else
  656. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  657. }
  658. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  659. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  660. if (!any_dirty_rect_intersects_window(window))
  661. return IterationDecision::Continue;
  662. PainterStateSaver saver(*m_back_painter);
  663. m_back_painter->add_clip_rect(window.frame().rect());
  664. for (auto& dirty_rect : dirty_rects.rects()) {
  665. if (any_opaque_window_above_this_one_contains_rect(window, dirty_rect))
  666. continue;
  667. PainterStateSaver saver(*m_back_painter);
  668. m_back_painter->add_clip_rect(dirty_rect);
  669. window.frame().paint(*m_back_painter);
  670. if (!backing_store)
  671. continue;
  672. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  673. if (dirty_rect_in_window_coordinates.is_empty())
  674. continue;
  675. dirty_rect_in_window_coordinates.move_by(-window.position());
  676. auto dst = window.position();
  677. dst.move_by(dirty_rect_in_window_coordinates.location());
  678. if (window.opacity() == 1.0f)
  679. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates);
  680. else
  681. m_back_painter->blit_with_opacity(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  682. }
  683. return IterationDecision::Continue;
  684. });
  685. draw_menubar();
  686. draw_cursor();
  687. if (m_flash_flush) {
  688. for (auto& rect : dirty_rects.rects())
  689. m_front_painter->fill_rect(rect, Color::Yellow);
  690. }
  691. flip_buffers();
  692. for (auto& r : dirty_rects.rects())
  693. flush(r);
  694. }
  695. Rect WSWindowManager::current_cursor_rect() const
  696. {
  697. return { m_screen.cursor_location().translated(-active_cursor().hotspot()), active_cursor().size() };
  698. }
  699. void WSWindowManager::invalidate_cursor()
  700. {
  701. invalidate(current_cursor_rect());
  702. }
  703. Rect WSWindowManager::menubar_rect() const
  704. {
  705. return { 0, 0, m_screen_rect.width(), 18 };
  706. }
  707. void WSWindowManager::draw_menubar()
  708. {
  709. auto menubar_rect = this->menubar_rect();
  710. m_back_painter->fill_rect(menubar_rect, Color::LightGray);
  711. m_back_painter->draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::White);
  712. int index = 0;
  713. for_each_active_menubar_menu([&] (WSMenu& menu) {
  714. Color text_color = Color::Black;
  715. if (&menu == current_menu()) {
  716. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  717. text_color = Color::White;
  718. }
  719. m_back_painter->draw_text(
  720. menu.text_rect_in_menubar(),
  721. menu.name(),
  722. index == 1 ? app_menu_font() : menu_font(),
  723. TextAlignment::CenterLeft,
  724. text_color
  725. );
  726. ++index;
  727. return true;
  728. });
  729. int username_width = Font::default_bold_font().width(m_username);
  730. Rect username_rect {
  731. menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  732. menubar_rect.y(),
  733. username_width,
  734. menubar_rect.height()
  735. };
  736. m_back_painter->draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  737. time_t now = time(nullptr);
  738. auto* tm = localtime(&now);
  739. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  740. tm->tm_year + 1900,
  741. tm->tm_mon + 1,
  742. tm->tm_mday,
  743. tm->tm_hour,
  744. tm->tm_min,
  745. tm->tm_sec);
  746. int time_width = font().width(time_text);
  747. Rect time_rect {
  748. username_rect.left() - menubar_menu_margin() / 2 - time_width,
  749. menubar_rect.y(),
  750. time_width,
  751. menubar_rect.height()
  752. };
  753. m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
  754. 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 };
  755. m_back_painter->fill_rect(cpu_rect, Color::Black);
  756. int i = m_cpu_history.capacity() - m_cpu_history.size();
  757. for (auto cpu_usage : m_cpu_history) {
  758. m_back_painter->draw_line(
  759. { cpu_rect.x() + i, cpu_rect.bottom() },
  760. { cpu_rect.x() + i, (int)(cpu_rect.y() + (cpu_rect.height() - (cpu_usage * (float)cpu_rect.height()))) },
  761. Color::from_rgb(0xaa6d4b)
  762. );
  763. ++i;
  764. }
  765. }
  766. void WSWindowManager::draw_window_switcher()
  767. {
  768. if (m_switcher.is_visible())
  769. m_switcher.draw();
  770. }
  771. void WSWindowManager::draw_cursor()
  772. {
  773. Rect cursor_rect = current_cursor_rect();
  774. Color inner_color = Color::White;
  775. Color outer_color = Color::Black;
  776. if (m_screen.mouse_button_state() & (unsigned)MouseButton::Left)
  777. swap(inner_color, outer_color);
  778. m_back_painter->blit(cursor_rect.location(), active_cursor().bitmap(), active_cursor().rect());
  779. m_last_cursor_rect = cursor_rect;
  780. }
  781. void WSWindowManager::on_message(const WSMessage& message)
  782. {
  783. if (message.is_mouse_event()) {
  784. WSWindow* event_window = nullptr;
  785. process_mouse_event(static_cast<const WSMouseEvent&>(message), event_window);
  786. set_hovered_window(event_window);
  787. return;
  788. }
  789. if (message.is_key_event()) {
  790. auto& key_event = static_cast<const WSKeyEvent&>(message);
  791. m_keyboard_modifiers = key_event.modifiers();
  792. if (key_event.type() == WSMessage::KeyDown && key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab)
  793. m_switcher.show();
  794. if (m_switcher.is_visible()) {
  795. m_switcher.on_key_event(key_event);
  796. return;
  797. }
  798. if (m_active_window)
  799. return m_active_window->on_message(message);
  800. return;
  801. }
  802. if (message.type() == WSMessage::WM_DeferredCompose) {
  803. m_pending_compose_event = false;
  804. compose();
  805. return;
  806. }
  807. }
  808. void WSWindowManager::set_highlight_window(WSWindow* window)
  809. {
  810. if (window == m_highlight_window.ptr())
  811. return;
  812. if (auto* previous_highlight_window = m_highlight_window.ptr())
  813. invalidate(*previous_highlight_window);
  814. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  815. if (m_highlight_window)
  816. invalidate(*m_highlight_window);
  817. }
  818. void WSWindowManager::set_active_window(WSWindow* window)
  819. {
  820. if (window && window->is_blocked_by_modal_window())
  821. return;
  822. if (window->type() != WSWindowType::Normal) {
  823. dbgprintf("WSWindowManager: Attempted to make a non-normal window active.\n");
  824. return;
  825. }
  826. if (window == m_active_window.ptr())
  827. return;
  828. auto* previously_active_window = m_active_window.ptr();
  829. if (previously_active_window) {
  830. WSMessageLoop::the().post_message(*previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  831. invalidate(*previously_active_window);
  832. }
  833. m_active_window = window->make_weak_ptr();
  834. if (m_active_window) {
  835. WSMessageLoop::the().post_message(*m_active_window, make<WSMessage>(WSMessage::WindowActivated));
  836. invalidate(*m_active_window);
  837. auto* client = window->client();
  838. ASSERT(client);
  839. set_current_menubar(client->app_menubar());
  840. if (previously_active_window)
  841. tell_wm_listeners_window_state_changed(*previously_active_window);
  842. tell_wm_listeners_window_state_changed(*m_active_window);
  843. }
  844. }
  845. void WSWindowManager::set_hovered_window(WSWindow* window)
  846. {
  847. if (m_hovered_window.ptr() == window)
  848. return;
  849. if (m_hovered_window)
  850. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowLeft));
  851. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  852. if (m_hovered_window)
  853. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowEntered));
  854. }
  855. void WSWindowManager::invalidate()
  856. {
  857. m_dirty_rects.clear_with_capacity();
  858. invalidate(m_screen_rect);
  859. }
  860. void WSWindowManager::recompose_immediately()
  861. {
  862. m_dirty_rects.clear_with_capacity();
  863. invalidate(m_screen_rect, false);
  864. }
  865. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  866. {
  867. auto rect = Rect::intersection(a_rect, m_screen_rect);
  868. if (rect.is_empty())
  869. return;
  870. m_dirty_rects.add(rect);
  871. if (should_schedule_compose_event && !m_pending_compose_event) {
  872. WSMessageLoop::the().post_message(*this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  873. m_pending_compose_event = true;
  874. }
  875. }
  876. void WSWindowManager::invalidate(const WSWindow& window)
  877. {
  878. invalidate(window.frame().rect());
  879. }
  880. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  881. {
  882. if (rect.is_empty()) {
  883. invalidate(window);
  884. return;
  885. }
  886. auto outer_rect = window.frame().rect();
  887. auto inner_rect = rect;
  888. inner_rect.move_by(window.position());
  889. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  890. inner_rect.intersect(outer_rect);
  891. invalidate(inner_rect);
  892. }
  893. void WSWindowManager::flush(const Rect& a_rect)
  894. {
  895. auto rect = Rect::intersection(a_rect, m_screen_rect);
  896. #ifdef DEBUG_COUNTERS
  897. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  898. #endif
  899. const RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  900. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  901. size_t pitch = m_back_bitmap->pitch();
  902. for (int y = 0; y < rect.height(); ++y) {
  903. fast_dword_copy(back_ptr, front_ptr, rect.width());
  904. front_ptr = (const RGBA32*)((const byte*)front_ptr + pitch);
  905. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  906. }
  907. }
  908. void WSWindowManager::close_menu(WSMenu& menu)
  909. {
  910. if (current_menu() == &menu)
  911. close_current_menu();
  912. }
  913. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  914. {
  915. if (current_menubar() == &menubar)
  916. set_current_menubar(nullptr);
  917. }
  918. const WSClientConnection* WSWindowManager::active_client() const
  919. {
  920. if (m_active_window)
  921. return m_active_window->client();
  922. return nullptr;
  923. }
  924. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  925. {
  926. if (active_client() == &client)
  927. set_current_menubar(client.app_menubar());
  928. invalidate(menubar_rect());
  929. }
  930. const WSCursor& WSWindowManager::active_cursor() const
  931. {
  932. if (m_drag_window)
  933. return *m_move_cursor;
  934. if (m_resize_window) {
  935. switch (m_resize_direction) {
  936. case ResizeDirection::Up:
  937. case ResizeDirection::Down:
  938. return *m_resize_vertically_cursor;
  939. case ResizeDirection::Left:
  940. case ResizeDirection::Right:
  941. return *m_resize_horizontally_cursor;
  942. case ResizeDirection::UpLeft:
  943. case ResizeDirection::DownRight:
  944. return *m_resize_diagonally_tlbr_cursor;
  945. case ResizeDirection::UpRight:
  946. case ResizeDirection::DownLeft:
  947. return *m_resize_diagonally_bltr_cursor;
  948. case ResizeDirection::None:
  949. ASSERT_NOT_REACHED();
  950. }
  951. }
  952. if (m_hovered_window && m_hovered_window->override_cursor())
  953. return *m_hovered_window->override_cursor();
  954. return *m_arrow_cursor;
  955. }