Serenity Operating System
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
8#include <AK/Badge.h>
9#include <AK/TemporaryChange.h>
10#include <LibConfig/Client.h>
11#include <LibGUI/ConnectionToWindowServer.h>
12#include <LibGUI/Desktop.h>
13#include <string.h>
14
15namespace GUI {
16
17Desktop& Desktop::the()
18{
19 static Desktop s_the;
20 return s_the;
21}
22
23void Desktop::did_receive_screen_rects(Badge<ConnectionToWindowServer>, Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index, unsigned workspace_rows, unsigned workspace_columns)
24{
25 m_main_screen_index = main_screen_index;
26 m_rects = rects;
27 if (!m_rects.is_empty()) {
28 m_bounding_rect = m_rects[0];
29 for (size_t i = 1; i < m_rects.size(); i++)
30 m_bounding_rect = m_bounding_rect.united(m_rects[i]);
31 } else {
32 m_bounding_rect = {};
33 }
34
35 m_workspace_rows = workspace_rows;
36 m_workspace_columns = workspace_columns;
37
38 for (auto& callback : m_receive_rects_callbacks)
39 callback(*this);
40}
41
42void Desktop::set_background_color(StringView background_color)
43{
44 ConnectionToWindowServer::the().async_set_background_color(background_color);
45}
46
47void Desktop::set_wallpaper_mode(StringView mode)
48{
49 ConnectionToWindowServer::the().async_set_wallpaper_mode(mode);
50}
51
52DeprecatedString Desktop::wallpaper_path() const
53{
54 return Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv);
55}
56
57RefPtr<Gfx::Bitmap> Desktop::wallpaper_bitmap() const
58{
59 return ConnectionToWindowServer::the().get_wallpaper().bitmap();
60}
61
62bool Desktop::set_wallpaper(RefPtr<Gfx::Bitmap const> wallpaper_bitmap, Optional<DeprecatedString> path)
63{
64 if (m_is_setting_desktop_wallpaper)
65 return false;
66
67 TemporaryChange is_setting_desktop_wallpaper_change(m_is_setting_desktop_wallpaper, true);
68 auto result = ConnectionToWindowServer::the().set_wallpaper(wallpaper_bitmap ? wallpaper_bitmap->to_shareable_bitmap() : Gfx::ShareableBitmap {});
69
70 if (result && path.has_value()) {
71 dbgln("Saving wallpaper path '{}' to ConfigServer", *path);
72 Config::write_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, *path);
73 }
74
75 return result;
76}
77
78}