~lukas-kde/miral/shellchrome-windowinfo

« back to all changes in this revision

Viewing changes to miral-qt/src/platforms/mirserver/mirdisplayconfigurationpolicy.cpp

  • Committer: Larry Price
  • Date: 2016-09-13 16:19:29 UTC
  • mto: (330.4.1 miral)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: larry.price@canonical.com-20160913161929-vs9ka1capmljq1es
Removing miral-qt from release branch, updating copyright file, and adding GPL3 license to root dir

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2016 Canonical, Ltd.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it under
5
 
 * the terms of the GNU Lesser General Public License version 3, as published by
6
 
 * the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
 
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
 
 * Lesser General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU Lesser General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 */
16
 
 
17
 
#include "mirdisplayconfigurationpolicy.h"
18
 
 
19
 
#include <mir/graphics/display_configuration_policy.h>
20
 
#include <mir/graphics/display_configuration.h>
21
 
#include <mir/geometry/point.h>
22
 
#include <mir/server.h>
23
 
 
24
 
#include <qglobal.h>
25
 
#include <QByteArray>
26
 
 
27
 
namespace mg = mir::graphics;
28
 
 
29
 
#define ENV_GRID_UNIT_PX "GRID_UNIT_PX"
30
 
#define DEFAULT_GRID_UNIT_PX 8
31
 
 
32
 
namespace {
33
 
class MirDisplayConfigurationPolicy : public mir::graphics::DisplayConfigurationPolicy
34
 
{
35
 
public:
36
 
    MirDisplayConfigurationPolicy(const std::shared_ptr<mir::graphics::DisplayConfigurationPolicy> &wrapped);
37
 
 
38
 
    void apply_to(mir::graphics::DisplayConfiguration &conf) override;
39
 
 
40
 
private:
41
 
    const std::shared_ptr<mir::graphics::DisplayConfigurationPolicy> m_wrapped;
42
 
    float m_defaultScale;
43
 
};
44
 
 
45
 
static float getenvFloat(const char* name, float defaultValue)
46
 
{
47
 
    QByteArray stringValue = qgetenv(name);
48
 
    bool ok;
49
 
    float value = stringValue.toFloat(&ok);
50
 
    return ok ? value : defaultValue;
51
 
}
52
 
 
53
 
MirDisplayConfigurationPolicy::MirDisplayConfigurationPolicy(
54
 
        const std::shared_ptr<mir::graphics::DisplayConfigurationPolicy> &wrapped)
55
 
    : m_wrapped(wrapped)
56
 
{
57
 
    float gridUnit = DEFAULT_GRID_UNIT_PX;
58
 
    if (qEnvironmentVariableIsSet(ENV_GRID_UNIT_PX)) {
59
 
        gridUnit = getenvFloat(ENV_GRID_UNIT_PX, DEFAULT_GRID_UNIT_PX);
60
 
    }
61
 
    m_defaultScale = gridUnit / DEFAULT_GRID_UNIT_PX;
62
 
}
63
 
 
64
 
void MirDisplayConfigurationPolicy::apply_to(mg::DisplayConfiguration &conf)
65
 
{
66
 
    int nextTopLeftPosition = 0;
67
 
 
68
 
    m_wrapped->apply_to(conf);
69
 
 
70
 
    //TODO: scan through saved configurations and select matching one to apply
71
 
 
72
 
    // We want to apply a particular display config policy when connecting an external display
73
 
    // to a phone/tablet. We don't have a reliable way to distinguish a phone/tablet display
74
 
    // from a laptop display as yet.
75
 
    //
76
 
    // Best we can do currently is guess that LVDS panel implies a phone/tablet
77
 
    bool phoneDetected = false;
78
 
    int screenCount = 0;
79
 
    conf.for_each_output(
80
 
        [&](const mg::DisplayConfigurationOutput &output )
81
 
        {
82
 
            if (output.connected && output.used) {
83
 
                screenCount++;
84
 
 
85
 
                if (output.type == mg::DisplayConfigurationOutputType::lvds) {
86
 
                    phoneDetected = true;
87
 
                }
88
 
            }
89
 
        });
90
 
 
91
 
    conf.for_each_output(
92
 
        [&](mg::UserDisplayConfigurationOutput &output)
93
 
        {
94
 
            if (!output.connected || !output.used) {
95
 
                return;
96
 
            }
97
 
 
98
 
            output.top_left = mir::geometry::Point{nextTopLeftPosition, 0};
99
 
            nextTopLeftPosition += output.modes[output.current_mode_index].size.width.as_int();
100
 
 
101
 
            if (phoneDetected) {
102
 
                if (screenCount == 1 || output.type == mg::DisplayConfigurationOutputType::lvds) {
103
 
                    output.form_factor = mir_form_factor_phone;
104
 
                    output.scale = m_defaultScale;
105
 
                } else { // screenCount > 1 && output.type != lvds
106
 
                    output.form_factor = mir_form_factor_monitor;
107
 
                    output.scale = 1;
108
 
                }
109
 
            } else { // desktop
110
 
                output.form_factor = mir_form_factor_monitor;
111
 
                output.scale = m_defaultScale; // probably 1 on desktop anyway.
112
 
            }
113
 
        });
114
 
}
115
 
} //namespace
116
 
 
117
 
void qtmir::setDisplayConfigurationPolicy(mir::Server& server)
118
 
{
119
 
    server.wrap_display_configuration_policy(
120
 
        [](const std::shared_ptr<mg::DisplayConfigurationPolicy> &wrapped)
121
 
            -> std::shared_ptr<mg::DisplayConfigurationPolicy>
122
 
            {
123
 
                return std::make_shared<MirDisplayConfigurationPolicy>(wrapped);
124
 
            });
125
 
 
126
 
}