WSWindowManager.cpp 39 KB

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