~alan-griffiths/mir/knee-jerk-mir_surface_state_automatic

« back to all changes in this revision

Viewing changes to src/frontend/registration_order_focus_selection_strategy.cpp

  • Committer: Kevin DuBois
  • Date: 2012-11-13 01:36:29 UTC
  • mfrom: (245 trunk)
  • mto: This revision was merged to the branch mainline in revision 246.
  • Revision ID: kevin.dubois@canonical.com-20121113013629-q4496w4mp5e33auk
merge in base branch. move the demo clients to a new directory, examples/

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 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: Robert Carr <robert.carr@canonical.com>
 
17
 */
 
18
 
 
19
#include "mir/frontend/registration_order_focus_selection_strategy.h"
 
20
#include "mir/frontend/application_session.h"
 
21
#include "mir/frontend/application_session_container.h"
 
22
 
 
23
#include <memory>
 
24
#include <cassert>
 
25
#include <algorithm>
 
26
 
 
27
 
 
28
namespace mf = mir::frontend;
 
29
 
 
30
mf::RegistrationOrderFocusSelectionStrategy::RegistrationOrderFocusSelectionStrategy(std::shared_ptr<mf::ApplicationSessionContainer> const& app_container) :
 
31
  app_container(app_container)
 
32
{
 
33
 
 
34
}
 
35
 
 
36
std::weak_ptr<mf::ApplicationSession> mf::RegistrationOrderFocusSelectionStrategy::next_focus_app(std::shared_ptr<mf::ApplicationSession> const& focused_app)
 
37
{
 
38
    auto it = app_container->iterator();
 
39
 
 
40
    if (focused_app == NULL)
 
41
    {
 
42
        return **it;
 
43
    }
 
44
 
 
45
    bool found = false;
 
46
 
 
47
    while (it->is_valid())
 
48
    {
 
49
        auto stacked_app = **it;
 
50
        
 
51
        if (found) return stacked_app;
 
52
 
 
53
        if (stacked_app == focused_app)
 
54
        {
 
55
            found = true;
 
56
        }
 
57
        it->advance();
 
58
    }
 
59
    it->reset();
 
60
    // This is programmer error if not found...
 
61
    // later we can make this use LockingIterators?
 
62
    assert(found);
 
63
 
 
64
    return **it;
 
65
}
 
66
 
 
67
std::weak_ptr<mf::ApplicationSession> mf::RegistrationOrderFocusSelectionStrategy::previous_focus_app(std::shared_ptr<mf::ApplicationSession> const& focused_app)
 
68
{
 
69
    auto it = app_container->iterator();
 
70
    
 
71
    if (focused_app == NULL)
 
72
    {
 
73
        return **it;
 
74
    }
 
75
    
 
76
    std::weak_ptr<mf::ApplicationSession> last_app = **it;
 
77
 
 
78
    it->advance();
 
79
    while (it->is_valid())
 
80
    {
 
81
        auto stacked_app = **it;
 
82
        
 
83
        if (stacked_app == focused_app)
 
84
        {
 
85
            return last_app;
 
86
        }
 
87
        last_app = stacked_app;
 
88
        it->advance();
 
89
    }
 
90
    // If we didn't focus_app it was the first app so we happily return the last app
 
91
    return last_app;
 
92
}