Desktop.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Badge.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <LibGUI/ConnectionToWindowServer.h>
  10. #include <LibGUI/Desktop.h>
  11. #include <string.h>
  12. // Including this after to avoid LibIPC errors
  13. #include <LibConfig/Client.h>
  14. namespace GUI {
  15. Desktop& Desktop::the()
  16. {
  17. static Desktop s_the;
  18. return s_the;
  19. }
  20. void Desktop::did_receive_screen_rects(Badge<ConnectionToWindowServer>, Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index, unsigned workspace_rows, unsigned workspace_columns)
  21. {
  22. m_main_screen_index = main_screen_index;
  23. m_rects = rects;
  24. if (!m_rects.is_empty()) {
  25. m_bounding_rect = m_rects[0];
  26. for (size_t i = 1; i < m_rects.size(); i++)
  27. m_bounding_rect = m_bounding_rect.united(m_rects[i]);
  28. } else {
  29. m_bounding_rect = {};
  30. }
  31. m_workspace_rows = workspace_rows;
  32. m_workspace_columns = workspace_columns;
  33. for (auto& callback : m_receive_rects_callbacks)
  34. callback(*this);
  35. }
  36. void Desktop::set_background_color(StringView background_color)
  37. {
  38. ConnectionToWindowServer::the().async_set_background_color(background_color);
  39. }
  40. void Desktop::set_wallpaper_mode(StringView mode)
  41. {
  42. ConnectionToWindowServer::the().async_set_wallpaper_mode(mode);
  43. }
  44. String Desktop::wallpaper_path() const
  45. {
  46. return Config::read_string("WindowManager", "Background", "Wallpaper");
  47. }
  48. RefPtr<Gfx::Bitmap> Desktop::wallpaper_bitmap() const
  49. {
  50. return ConnectionToWindowServer::the().get_wallpaper().bitmap();
  51. }
  52. bool Desktop::set_wallpaper(RefPtr<Gfx::Bitmap> wallpaper_bitmap, Optional<String> path)
  53. {
  54. if (m_is_setting_desktop_wallpaper)
  55. return false;
  56. TemporaryChange is_setting_desktop_wallpaper_change(m_is_setting_desktop_wallpaper, true);
  57. ConnectionToWindowServer::the().async_set_wallpaper(wallpaper_bitmap ? wallpaper_bitmap->to_shareable_bitmap() : Gfx::ShareableBitmap {});
  58. auto ret_val = ConnectionToWindowServer::the().wait_for_specific_message<Messages::WindowClient::SetWallpaperFinished>()->success();
  59. if (ret_val && path.has_value()) {
  60. dbgln("Saving wallpaper path '{}' to ConfigServer", *path);
  61. Config::write_string("WindowManager", "Background", "Wallpaper", *path);
  62. }
  63. return ret_val;
  64. }
  65. }