~3v1n0/unity/accelerators-controller-trigger-fix

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2013 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Marco Trevisan <marco.trevisan@canonical.com>
 */

#include "DecorationsInputMixer.h"

namespace unity
{
namespace decoration
{

InputMixer::InputMixer()
  : mouse_down_(false)
  , recheck_owner_(false)
{}

void InputMixer::PushToFront(Item::Ptr const& item)
{
  if (!item)
    return;

  auto it = std::find(items_.begin(), items_.end(), item);
  if (it != items_.end())
    items_.erase(it);

  items_.push_front(item);
}

void InputMixer::PushToBack(Item::Ptr const& item)
{
  if (!item)
    return;

  auto it = std::find(items_.begin(), items_.end(), item);
  if (it != items_.end())
    items_.erase(it);

  items_.push_back(item);
}

void InputMixer::Remove(Item::Ptr const& item)
{
  if (item == last_mouse_owner_)
    UnsetMouseOwner();

  auto it = std::find(items_.begin(), items_.end(), item);
  if (it != items_.end())
    items_.erase(it);
}

Item::List const& InputMixer::Items() const
{
  return items_;
}

void InputMixer::ForceMouseOwnerCheck()
{
  if (!mouse_down_)
  {
    UpdateMouseOwner(CompPoint(pointerX, pointerY));
  }
  else
  {
    recheck_owner_ = true;
  }
}

Item::Ptr InputMixer::GetMatchingItemRecursive(Item::List const& items, CompPoint const& point)
{
  for (auto const& item : items)
  {
    if (item && item->visible() && item->Geometry().contains(point))
    {
      if (!item->IsContainer())
        return item->sensitive() ? item : nullptr;

      auto const& container = std::static_pointer_cast<BasicContainer>(item);
      auto const& child = GetMatchingItemRecursive(container->Items(), point);

      if (child)
        return child;
    }
  }

  return nullptr;
}

Item::Ptr InputMixer::GetMatchingItem(CompPoint const& point)
{
  return GetMatchingItemRecursive(items_, point);
}

void InputMixer::UpdateMouseOwner(CompPoint const& point)
{
  if (Item::Ptr const& item = GetMatchingItem(point))
  {
    if (item != last_mouse_owner_)
    {
      UnsetMouseOwner();
      last_mouse_owner_ = item;
      item->mouse_owner = true;
    }
  }
  else
  {
    UnsetMouseOwner();
  }
}

void InputMixer::UnsetMouseOwner()
{
  if (!last_mouse_owner_)
    return;

  last_mouse_owner_->mouse_owner = false;
  last_mouse_owner_ = nullptr;
}

void InputMixer::UngrabPointer()
{
  mouse_down_ = false;
  UnsetMouseOwner();
}

Item::Ptr const& InputMixer::GetMouseOwner() const
{
  return last_mouse_owner_;
};

void InputMixer::EnterEvent(CompPoint const& point)
{
  if (!mouse_down_)
    UpdateMouseOwner(point);
}

void InputMixer::LeaveEvent(CompPoint const& point)
{
  if (!mouse_down_)
    UnsetMouseOwner();
}

void InputMixer::MotionEvent(CompPoint const& point, Time timestamp)
{
  if (!mouse_down_)
    UpdateMouseOwner(point);

  if (last_mouse_owner_)
    last_mouse_owner_->MotionEvent(point, timestamp);
}

void InputMixer::ButtonDownEvent(CompPoint const& point, unsigned button, Time timestamp)
{
  mouse_down_ = true;

  if (last_mouse_owner_)
    last_mouse_owner_->ButtonDownEvent(point, button, timestamp);
}

void InputMixer::ButtonUpEvent(CompPoint const& point, unsigned button, Time timestamp)
{
  mouse_down_ = false;

  if (last_mouse_owner_)
  {
    // This event might cause the LastMouseOwner to be deleted, so we protect using a weak_ptr
    Item::WeakPtr weak_last_mouse_owner(last_mouse_owner_);
    last_mouse_owner_->ButtonUpEvent(point, button, timestamp);

    if (weak_last_mouse_owner && !last_mouse_owner_->Geometry().contains(point))
    {
      UpdateMouseOwner(point);
    }
    else if (recheck_owner_)
    {
      recheck_owner_ = false;
      UpdateMouseOwner(point);
    }
  }
}

} // decoration namespace
} // unity namespace