~ci-train-bot/unity-system-compositor/unity-system-compositor-ubuntu-zesty-2649

« back to all changes in this revision

Viewing changes to src/session_switcher.cpp

  • Committer: Alexandros Frantzis
  • Date: 2014-07-09 06:58:05 UTC
  • mto: This revision was merged to the branch mainline in revision 157.
  • Revision ID: alexandros.frantzis@canonical.com-20140709065805-12e3vzvcxdf8fhrr
First steps of refactoring to improve code comprehensibility and testability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2014 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License version 3 as
 
6
 * 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: Alexandros Frantzis <alexandros.frantzis@canonical.com>
 
17
 */
 
18
 
 
19
#include "session_switcher.h"
 
20
#include "spinner.h"
 
21
 
 
22
usc::SessionSwitcher::SessionSwitcher(std::shared_ptr<Spinner> const& spinner)
 
23
    : spinner_process{spinner},
 
24
      booting{true}
 
25
{
 
26
}
 
27
 
 
28
void usc::SessionSwitcher::add(std::shared_ptr<Session> const& session, pid_t pid)
 
29
{
 
30
    std::lock_guard<std::mutex> lock{mutex};
 
31
 
 
32
    if (pid == spinner_process->pid())
 
33
        spinner_name = session->name();
 
34
 
 
35
    sessions[session->name()] = SessionInfo(session);
 
36
    update_displayed_sessions();
 
37
}
 
38
 
 
39
void usc::SessionSwitcher::remove(std::string const& name)
 
40
{
 
41
    std::lock_guard<std::mutex> lock{mutex};
 
42
 
 
43
    if (name == spinner_name)
 
44
        spinner_name = "";
 
45
 
 
46
    sessions.erase(name);
 
47
    update_displayed_sessions();
 
48
}
 
49
 
 
50
void usc::SessionSwitcher::set_active_session(std::string const& name)
 
51
{
 
52
    std::lock_guard<std::mutex> lock{mutex};
 
53
 
 
54
    active_name = name;
 
55
    update_displayed_sessions();
 
56
}
 
57
 
 
58
void usc::SessionSwitcher::set_next_session(std::string const& name)
 
59
{
 
60
    std::lock_guard<std::mutex> lock{mutex};
 
61
 
 
62
    next_name = name;
 
63
    update_displayed_sessions();
 
64
}
 
65
 
 
66
void usc::SessionSwitcher::mark_ready(mir::scene::Session const* scene_session)
 
67
{
 
68
    std::lock_guard<std::mutex> lock{mutex};
 
69
 
 
70
    for (auto& pair : sessions)
 
71
    {
 
72
        if (pair.second.session && pair.second.session->corresponds_to(scene_session))
 
73
            pair.second.ready = true;
 
74
    }
 
75
 
 
76
    update_displayed_sessions();
 
77
}
 
78
 
 
79
void usc::SessionSwitcher::update_displayed_sessions()
 
80
{
 
81
    hide_uninteresting_sessions();
 
82
 
 
83
    bool show_spinner = false;
 
84
    ShowMode show_spinner_mode{ShowMode::as_next};
 
85
    bool const allowed_to_display_active =
 
86
        is_session_ready_for_display(next_name) ||
 
87
        !is_session_expected_to_become_ready(next_name) ||
 
88
        !booting;
 
89
 
 
90
    if (allowed_to_display_active && is_session_ready_for_display(active_name))
 
91
    {
 
92
        show_session(active_name, ShowMode::as_active);
 
93
        booting = false;
 
94
    }
 
95
    else if (is_session_expected_to_become_ready(active_name))
 
96
    {
 
97
        show_spinner = true;
 
98
        show_spinner_mode = ShowMode::as_active;
 
99
    }
 
100
 
 
101
    if (!show_spinner)
 
102
    {
 
103
        if (is_session_ready_for_display(next_name))
 
104
        {
 
105
            show_session(next_name, ShowMode::as_next);
 
106
        }
 
107
        else if (is_session_expected_to_become_ready(next_name))
 
108
        {
 
109
            show_spinner = true;
 
110
            show_spinner_mode = ShowMode::as_next;
 
111
        }
 
112
    }
 
113
    else if (is_session_ready_for_display(next_name))
 
114
    {
 
115
        hide_session(next_name);
 
116
    }
 
117
 
 
118
    if (show_spinner)
 
119
        ensure_spinner_will_be_shown(show_spinner_mode);
 
120
    else
 
121
        ensure_spinner_is_not_running();
 
122
}
 
123
 
 
124
void usc::SessionSwitcher::hide_uninteresting_sessions()
 
125
{
 
126
    for (auto const& pair : sessions)
 
127
    {
 
128
        if (pair.second.session->name() != active_name &&
 
129
            pair.second.session->name() != next_name)
 
130
        {
 
131
            pair.second.session->hide();
 
132
        }
 
133
    }
 
134
}
 
135
 
 
136
bool usc::SessionSwitcher::is_session_ready_for_display(std::string const& name)
 
137
{
 
138
    auto const iter = sessions.find(name);
 
139
    if (iter == sessions.end())
 
140
        return false;
 
141
 
 
142
    return iter->second.session && iter->second.ready;
 
143
}
 
144
 
 
145
bool usc::SessionSwitcher::is_session_expected_to_become_ready(std::string const& name)
 
146
{
 
147
    return !name.empty();
 
148
}
 
149
 
 
150
void usc::SessionSwitcher::show_session(
 
151
    std::string const& name,
 
152
    ShowMode show_mode)
 
153
{
 
154
    auto& session = sessions[name].session;
 
155
 
 
156
    if (show_mode == ShowMode::as_active)
 
157
        session->raise_and_focus();
 
158
 
 
159
    session->show();
 
160
}
 
161
 
 
162
void usc::SessionSwitcher::hide_session(std::string const& name)
 
163
{
 
164
    auto& session = sessions[name].session;
 
165
    session->hide();
 
166
}
 
167
 
 
168
void usc::SessionSwitcher::ensure_spinner_will_be_shown(ShowMode show_mode)
 
169
{
 
170
    auto const iter = sessions.find(spinner_name);
 
171
    if (iter == sessions.end())
 
172
    {
 
173
        spinner_process->ensure_running();
 
174
    }
 
175
    else
 
176
    {
 
177
        if (show_mode == ShowMode::as_active)
 
178
            iter->second.session->raise_and_focus();
 
179
        iter->second.session->show();
 
180
    }
 
181
}
 
182
 
 
183
void usc::SessionSwitcher::ensure_spinner_is_not_running()
 
184
{
 
185
    spinner_process->kill();
 
186
}