WSWindow.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "WSWindow.h"
  2. #include "WSEvent.h"
  3. #include "WSEventLoop.h"
  4. #include "WSScreen.h"
  5. #include "WSWindowManager.h"
  6. #include <WindowServer/WSClientConnection.h>
  7. #include <WindowServer/WindowClientEndpoint.h>
  8. static String default_window_icon_path()
  9. {
  10. return "/res/icons/16x16/window.png";
  11. }
  12. static GraphicsBitmap& default_window_icon()
  13. {
  14. static GraphicsBitmap* s_icon;
  15. if (!s_icon)
  16. s_icon = GraphicsBitmap::load_from_file(default_window_icon_path()).leak_ref();
  17. return *s_icon;
  18. }
  19. WSWindow::WSWindow(CObject& parent, WSWindowType type)
  20. : CObject(&parent)
  21. , m_type(type)
  22. , m_icon(default_window_icon())
  23. , m_frame(*this)
  24. {
  25. WSWindowManager::the().add_window(*this);
  26. }
  27. WSWindow::WSWindow(WSClientConnection& client, WSWindowType window_type, int window_id, bool modal, bool resizable, bool fullscreen)
  28. : CObject(&client)
  29. , m_client(&client)
  30. , m_type(window_type)
  31. , m_modal(modal)
  32. , m_resizable(resizable)
  33. , m_fullscreen(fullscreen)
  34. , m_window_id(window_id)
  35. , m_icon(default_window_icon())
  36. , m_frame(*this)
  37. {
  38. // FIXME: This should not be hard-coded here.
  39. if (m_type == WSWindowType::Taskbar) {
  40. m_wm_event_mask = WSWMEventMask::WindowStateChanges | WSWMEventMask::WindowRemovals | WSWMEventMask::WindowIconChanges;
  41. m_listens_to_wm_events = true;
  42. }
  43. WSWindowManager::the().add_window(*this);
  44. }
  45. WSWindow::~WSWindow()
  46. {
  47. WSWindowManager::the().remove_window(*this);
  48. }
  49. void WSWindow::set_title(const String& title)
  50. {
  51. if (m_title == title)
  52. return;
  53. m_title = title;
  54. WSWindowManager::the().notify_title_changed(*this);
  55. }
  56. void WSWindow::set_rect(const Rect& rect)
  57. {
  58. Rect old_rect;
  59. if (m_rect == rect)
  60. return;
  61. old_rect = m_rect;
  62. m_rect = rect;
  63. if (!m_client && (!m_backing_store || old_rect.size() != rect.size())) {
  64. m_backing_store = GraphicsBitmap::create(GraphicsBitmap::Format::RGB32, m_rect.size());
  65. }
  66. m_frame.notify_window_rect_changed(old_rect, rect);
  67. }
  68. void WSWindow::handle_mouse_event(const WSMouseEvent& event)
  69. {
  70. set_automatic_cursor_tracking_enabled(event.buttons() != 0);
  71. switch (event.type()) {
  72. case WSEvent::MouseMove:
  73. m_client->post_message(WindowClient::MouseMove(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  74. break;
  75. case WSEvent::MouseDown:
  76. m_client->post_message(WindowClient::MouseDown(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  77. break;
  78. case WSEvent::MouseDoubleClick:
  79. m_client->post_message(WindowClient::MouseDoubleClick(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  80. break;
  81. case WSEvent::MouseUp:
  82. m_client->post_message(WindowClient::MouseUp(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  83. break;
  84. case WSEvent::MouseWheel:
  85. m_client->post_message(WindowClient::MouseWheel(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta()));
  86. break;
  87. default:
  88. ASSERT_NOT_REACHED();
  89. }
  90. }
  91. void WSWindow::set_minimized(bool minimized)
  92. {
  93. if (m_minimized == minimized)
  94. return;
  95. m_minimized = minimized;
  96. if (!minimized)
  97. request_update({ {}, size() });
  98. invalidate();
  99. WSWindowManager::the().notify_minimization_state_changed(*this);
  100. }
  101. void WSWindow::set_maximized(bool maximized)
  102. {
  103. if (m_maximized == maximized)
  104. return;
  105. m_maximized = maximized;
  106. auto old_rect = m_rect;
  107. if (maximized) {
  108. m_unmaximized_rect = m_rect;
  109. set_rect(WSWindowManager::the().maximized_window_rect(*this));
  110. } else {
  111. set_rect(m_unmaximized_rect);
  112. }
  113. m_frame.did_set_maximized({}, maximized);
  114. CEventLoop::current().post_event(*this, make<WSResizeEvent>(old_rect, m_rect));
  115. }
  116. void WSWindow::event(CEvent& event)
  117. {
  118. if (!m_client) {
  119. ASSERT(parent());
  120. event.ignore();
  121. return;
  122. }
  123. if (is_blocked_by_modal_window())
  124. return;
  125. if (static_cast<WSEvent&>(event).is_mouse_event())
  126. return handle_mouse_event(static_cast<const WSMouseEvent&>(event));
  127. switch (event.type()) {
  128. case WSEvent::WindowEntered:
  129. m_client->post_message(WindowClient::WindowEntered(m_window_id));
  130. break;
  131. case WSEvent::WindowLeft:
  132. m_client->post_message(WindowClient::WindowLeft(m_window_id));
  133. break;
  134. case WSEvent::KeyDown:
  135. m_client->post_message(
  136. WindowClient::KeyDown(m_window_id,
  137. (u8) static_cast<const WSKeyEvent&>(event).character(),
  138. (u32) static_cast<const WSKeyEvent&>(event).key(),
  139. static_cast<const WSKeyEvent&>(event).modifiers()));
  140. break;
  141. case WSEvent::KeyUp:
  142. m_client->post_message(
  143. WindowClient::KeyUp(m_window_id,
  144. (u8) static_cast<const WSKeyEvent&>(event).character(),
  145. (u32) static_cast<const WSKeyEvent&>(event).key(),
  146. static_cast<const WSKeyEvent&>(event).modifiers()));
  147. break;
  148. case WSEvent::WindowActivated:
  149. m_client->post_message(WindowClient::WindowActivated(m_window_id));
  150. break;
  151. case WSEvent::WindowDeactivated:
  152. m_client->post_message(WindowClient::WindowDeactivated(m_window_id));
  153. break;
  154. case WSEvent::WindowCloseRequest:
  155. m_client->post_message(WindowClient::WindowCloseRequest(m_window_id));
  156. break;
  157. case WSEvent::WindowResized:
  158. m_client->post_message(
  159. WindowClient::WindowResized(
  160. m_window_id,
  161. static_cast<const WSResizeEvent&>(event).old_rect(),
  162. static_cast<const WSResizeEvent&>(event).rect()));
  163. break;
  164. case WSEvent::WM_WindowRemoved: {
  165. auto& removed_event = static_cast<const WSWMWindowRemovedEvent&>(event);
  166. m_client->post_message(WindowClient::WM_WindowRemoved(
  167. removed_event.client_id(),
  168. removed_event.window_id()));
  169. break;
  170. }
  171. case WSEvent::WM_WindowStateChanged: {
  172. auto& changed_event = static_cast<const WSWMWindowStateChangedEvent&>(event);
  173. m_client->post_message(WindowClient::WM_WindowStateChanged(
  174. changed_event.client_id(),
  175. changed_event.window_id(),
  176. changed_event.is_active(),
  177. changed_event.is_minimized(),
  178. (i32)(changed_event.window_type()),
  179. changed_event.title(),
  180. changed_event.rect()));
  181. break;
  182. }
  183. case WSEvent::WM_WindowIconBitmapChanged: {
  184. auto& changed_event = static_cast<const WSWMWindowIconBitmapChangedEvent&>(event);
  185. // FIXME: Perhaps we should update the bitmap sharing list somewhere else instead?
  186. dbg() << "WindowServer: Sharing icon buffer " << changed_event.icon_buffer_id() << " with PID " << client()->client_pid();
  187. if (share_buffer_with(changed_event.icon_buffer_id(), m_client->client_pid()) < 0) {
  188. ASSERT_NOT_REACHED();
  189. }
  190. m_client->post_message(
  191. WindowClient::WM_WindowIconBitmapChanged(
  192. changed_event.client_id(),
  193. changed_event.window_id(),
  194. changed_event.icon_buffer_id(),
  195. changed_event.icon_size()));
  196. break;
  197. }
  198. case WSEvent::WM_WindowRectChanged: {
  199. auto& changed_event = static_cast<const WSWMWindowRectChangedEvent&>(event);
  200. m_client->post_message(
  201. WindowClient::WM_WindowRectChanged(
  202. changed_event.client_id(),
  203. changed_event.window_id(),
  204. changed_event.rect()));
  205. break;
  206. }
  207. default:
  208. break;
  209. }
  210. }
  211. void WSWindow::set_global_cursor_tracking_enabled(bool enabled)
  212. {
  213. m_global_cursor_tracking_enabled = enabled;
  214. }
  215. void WSWindow::set_visible(bool b)
  216. {
  217. if (m_visible == b)
  218. return;
  219. m_visible = b;
  220. invalidate();
  221. }
  222. void WSWindow::invalidate()
  223. {
  224. WSWindowManager::the().invalidate(*this);
  225. }
  226. bool WSWindow::is_active() const
  227. {
  228. return WSWindowManager::the().active_window() == this;
  229. }
  230. bool WSWindow::is_blocked_by_modal_window() const
  231. {
  232. return !is_modal() && client() && client()->is_showing_modal_window();
  233. }
  234. void WSWindow::set_default_icon()
  235. {
  236. m_icon = default_window_icon();
  237. }
  238. void WSWindow::request_update(const Rect& rect)
  239. {
  240. if (m_pending_paint_rects.is_empty()) {
  241. deferred_invoke([this](auto&) {
  242. client()->post_paint_message(*this);
  243. });
  244. }
  245. m_pending_paint_rects.add(rect);
  246. }
  247. void WSWindow::popup_window_menu(const Point& position)
  248. {
  249. if (!m_window_menu) {
  250. m_window_menu = WSMenu::construct(nullptr, -1, "(Window Menu)");
  251. m_window_menu->add_item(make<WSMenuItem>(*m_window_menu, 1, "Minimize"));
  252. m_window_menu->add_item(make<WSMenuItem>(*m_window_menu, 2, "Unminimize"));
  253. m_window_menu->add_item(make<WSMenuItem>(*m_window_menu, WSMenuItem::Type::Separator));
  254. m_window_menu->add_item(make<WSMenuItem>(*m_window_menu, 3, "Close"));
  255. m_window_menu->on_item_activation = [&](auto& item) {
  256. switch (item.identifier()) {
  257. case 1:
  258. set_minimized(true);
  259. break;
  260. case 2:
  261. set_minimized(false);
  262. break;
  263. case 3:
  264. request_close();
  265. break;
  266. }
  267. };
  268. }
  269. m_window_menu->popup(position);
  270. }
  271. void WSWindow::request_close()
  272. {
  273. WSEvent close_request(WSEvent::WindowCloseRequest);
  274. event(close_request);
  275. }
  276. void WSWindow::set_fullscreen(bool fullscreen)
  277. {
  278. if (m_fullscreen == fullscreen)
  279. return;
  280. m_fullscreen = fullscreen;
  281. Rect new_window_rect = m_rect;
  282. if (m_fullscreen) {
  283. m_saved_nonfullscreen_rect = m_rect;
  284. new_window_rect = WSScreen::the().rect();
  285. } else if (!m_saved_nonfullscreen_rect.is_empty()) {
  286. new_window_rect = m_saved_nonfullscreen_rect;
  287. }
  288. CEventLoop::current().post_event(*this, make<WSResizeEvent>(m_rect, new_window_rect));
  289. set_rect(new_window_rect);
  290. }