~gerboland/qtmir/desktophintenvvar

« back to all changes in this revision

Viewing changes to src/platforms/mirserver/miral/persist_display_config.cpp

Iteration 0 of miral::PersistDisplayConfig. This does nothing yet (and breaks nothing in the process). This MP creates a place (miral-prototypes) to build prototype miral features and sketches out what will need to be implemented for PersistDisplayConfig. (LP: #1644189)

Approved by: Nick Dedekind, Unity8 CI Bot

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2016 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3,
 
6
 * as published by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Alan Griffiths <alan@octopull.co.uk>
 
17
 */
 
18
 
 
19
#include "persist_display_config.h"
 
20
 
 
21
#include <mir/graphics/display_configuration_policy.h>
 
22
#include <mir/server.h>
 
23
#include <mir/version.h>
 
24
 
 
25
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 26, 0)
 
26
#include <mir/graphics/display_configuration_observer.h>
 
27
#include <mir/observer_registrar.h>
 
28
#endif
 
29
 
 
30
namespace mg = mir::graphics;
 
31
 
 
32
namespace
 
33
{
 
34
struct PersistDisplayConfigPolicy
 
35
{
 
36
    PersistDisplayConfigPolicy() = default;
 
37
    virtual ~PersistDisplayConfigPolicy() = default;
 
38
    PersistDisplayConfigPolicy(PersistDisplayConfigPolicy const&) = delete;
 
39
    auto operator=(PersistDisplayConfigPolicy const&) -> PersistDisplayConfigPolicy& = delete;
 
40
 
 
41
    void apply_to(mg::DisplayConfiguration& conf, mg::DisplayConfigurationPolicy& default_policy);
 
42
    void save_config(mg::DisplayConfiguration const& base_conf);
 
43
};
 
44
 
 
45
struct DisplayConfigurationPolicyAdapter : mg::DisplayConfigurationPolicy
 
46
{
 
47
    DisplayConfigurationPolicyAdapter(
 
48
        std::shared_ptr<PersistDisplayConfigPolicy> const& self,
 
49
        std::shared_ptr<mg::DisplayConfigurationPolicy> const& wrapped) :
 
50
        self{self}, default_policy{wrapped}
 
51
    {}
 
52
 
 
53
    void apply_to(mg::DisplayConfiguration& conf) override
 
54
    {
 
55
        self->apply_to(conf, *default_policy);
 
56
    }
 
57
 
 
58
    std::shared_ptr<PersistDisplayConfigPolicy> const self;
 
59
    std::shared_ptr<mg::DisplayConfigurationPolicy> const default_policy;
 
60
};
 
61
 
 
62
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 26, 0)
 
63
struct DisplayConfigurationObserver : mg::DisplayConfigurationObserver
 
64
{
 
65
    void initial_configuration(std::shared_ptr<mg::DisplayConfiguration const> const& /*config*/) override {}
 
66
 
 
67
    void configuration_applied(std::shared_ptr<mg::DisplayConfiguration const> const& /*config*/) override {}
 
68
 
 
69
    void configuration_failed(
 
70
        std::shared_ptr<mg::DisplayConfiguration const> const& /*attempted*/,
 
71
        std::exception const& /*error*/) override {}
 
72
 
 
73
    void catastrophic_configuration_error(
 
74
        std::shared_ptr<mg::DisplayConfiguration const> const& /*failed_fallback*/,
 
75
        std::exception const& /*error*/) override {}
 
76
};
 
77
#else
 
78
struct DisplayConfigurationObserver { };
 
79
#endif
 
80
}
 
81
 
 
82
struct miral::PersistDisplayConfig::Self : PersistDisplayConfigPolicy, DisplayConfigurationObserver
 
83
{
 
84
    Self() = default;
 
85
    Self(DisplayConfigurationPolicyWrapper const& custom_wrapper) :
 
86
        custom_wrapper{custom_wrapper} {}
 
87
 
 
88
    DisplayConfigurationPolicyWrapper const custom_wrapper =
 
89
        [](const std::shared_ptr<mg::DisplayConfigurationPolicy> &wrapped) { return wrapped; };
 
90
 
 
91
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 26, 0)
 
92
    void base_configuration_updated(std::shared_ptr<mg::DisplayConfiguration const> const& base_config) override
 
93
    {
 
94
        save_config(*base_config);
 
95
    }
 
96
 
 
97
    void session_configuration_applied(std::shared_ptr<mir::frontend::Session> const&,
 
98
                                       std::shared_ptr<mg::DisplayConfiguration> const&){}
 
99
    void session_configuration_removed(std::shared_ptr<mir::frontend::Session> const&)  {}
 
100
#endif
 
101
};
 
102
 
 
103
miral::PersistDisplayConfig::PersistDisplayConfig() :
 
104
    self{std::make_shared<Self>()}
 
105
{
 
106
}
 
107
 
 
108
miral::PersistDisplayConfig::PersistDisplayConfig(DisplayConfigurationPolicyWrapper const& custom_wrapper) :
 
109
    self{std::make_shared<Self>(custom_wrapper)}
 
110
{
 
111
}
 
112
 
 
113
miral::PersistDisplayConfig::~PersistDisplayConfig() = default;
 
114
 
 
115
miral::PersistDisplayConfig::PersistDisplayConfig(PersistDisplayConfig const&) = default;
 
116
 
 
117
auto miral::PersistDisplayConfig::operator=(PersistDisplayConfig const&) -> PersistDisplayConfig& = default;
 
118
 
 
119
void miral::PersistDisplayConfig::operator()(mir::Server& server)
 
120
{
 
121
    server.wrap_display_configuration_policy(
 
122
        [this](std::shared_ptr<mg::DisplayConfigurationPolicy> const& wrapped)
 
123
        -> std::shared_ptr<mg::DisplayConfigurationPolicy>
 
124
        {
 
125
            return std::make_shared<DisplayConfigurationPolicyAdapter>(self, self->custom_wrapper(wrapped));
 
126
        });
 
127
 
 
128
#if MIR_SERVER_VERSION >= MIR_VERSION_NUMBER(0, 26, 0)
 
129
    server.add_init_callback([this, &server]
 
130
        { server.the_display_configuration_observer_registrar()->register_interest(self); });
 
131
#else
 
132
    // Up to Mir-0.25 detecting changes to the base display config is only possible client-side
 
133
    // (and gives a different configuration API)
 
134
    // If we decide implementing this is necessary for earlier Mir versions then this is where to plumb it in.
 
135
    (void)&PersistDisplayConfigPolicy::save_config; // Fake "using" the function for now
 
136
#endif
 
137
}
 
138
 
 
139
void PersistDisplayConfigPolicy::apply_to(
 
140
    mg::DisplayConfiguration& conf,
 
141
    mg::DisplayConfigurationPolicy& default_policy)
 
142
{
 
143
    // TODO if the h/w profile (by some definition) has changed, then apply corresponding saved config (if any).
 
144
    // TODO Otherwise...
 
145
    default_policy.apply_to(conf);
 
146
}
 
147
 
 
148
void PersistDisplayConfigPolicy::save_config(mg::DisplayConfiguration const& /*base_conf*/)
 
149
{
 
150
    // TODO save display config options against the h/w profile
 
151
}