Implement finding BLE connections in chrome://proximity-auth.
[chromium-blink-merge.git] / ash / touch / touchscreen_util.cc
blob3661398113ce4ffb2f32241bd29816f7b104a4a3
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/touch/touchscreen_util.h"
7 #include <set>
9 #include "base/logging.h"
10 #include "ui/events/devices/input_device.h"
12 namespace ash {
14 void AssociateTouchscreens(std::vector<DisplayInfo>* displays,
15 const std::vector<ui::TouchscreenDevice>& devices) {
16 DisplayInfo* internal_state = NULL;
17 for (size_t i = 0; i < displays->size(); ++i) {
18 DisplayInfo* state = &(*displays)[i];
19 if (gfx::Display::IsInternalDisplayId(state->id()) &&
20 !state->GetNativeModeSize().IsEmpty() &&
21 state->touch_support() == gfx::Display::TOUCH_SUPPORT_UNKNOWN) {
22 DCHECK(!internal_state);
23 internal_state = state;
25 state->ClearInputDevices();
28 std::set<int> no_match_touchscreen;
29 for (size_t i = 0; i < devices.size(); ++i) {
30 if (devices[i].type == ui::InputDeviceType::INPUT_DEVICE_INTERNAL) {
31 // Don't try and map internal touchscreens to external displays.
32 if (!internal_state)
33 continue;
34 VLOG(2) << "Found internal device for display " << internal_state->id()
35 << " with device id " << devices[i].id << " size "
36 << devices[i].size.ToString();
37 internal_state->AddInputDevice(devices[i].id);
38 internal_state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
39 continue;
42 bool found_mapping = false;
43 for (size_t j = 0; j < displays->size(); ++j) {
44 DisplayInfo* state = &(*displays)[j];
45 if (state == internal_state)
46 continue;
48 const gfx::Size native_size = state->GetNativeModeSize();
49 if (state->touch_support() == gfx::Display::TOUCH_SUPPORT_AVAILABLE ||
50 native_size.IsEmpty())
51 continue;
53 // Allow 1 pixel difference between screen and touchscreen
54 // resolutions. Because in some cases for monitor resolution
55 // 1024x768 touchscreen's resolution would be 1024x768, but for
56 // some 1023x767. It really depends on touchscreen's firmware
57 // configuration.
58 if (std::abs(native_size.width() - devices[i].size.width()) <= 1 &&
59 std::abs(native_size.height() - devices[i].size.height()) <= 1) {
60 state->AddInputDevice(devices[i].id);
61 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
63 VLOG(2) << "Found touchscreen for display " << state->id()
64 << " with device id " << devices[i].id << " size "
65 << devices[i].size.ToString();
66 found_mapping = true;
67 break;
71 if (!found_mapping) {
72 no_match_touchscreen.insert(devices[i].id);
73 VLOG(2) << "No matching display for device id " << devices[i].id
74 << " size " << devices[i].size.ToString();
78 // Sometimes we can't find a matching screen for the touchscreen, e.g.
79 // due to the touchscreen's reporting range having no correlation with the
80 // screen's resolution. In this case, we arbitrarily assign unmatched
81 // touchscreens to unmatched screens.
82 for (std::set<int>::iterator it = no_match_touchscreen.begin();
83 it != no_match_touchscreen.end();
84 ++it) {
85 for (size_t i = 0; i < displays->size(); ++i) {
86 DisplayInfo* state = &(*displays)[i];
87 if (!gfx::Display::IsInternalDisplayId(state->id()) &&
88 !state->GetNativeModeSize().IsEmpty() &&
89 state->touch_support() == gfx::Display::TOUCH_SUPPORT_UNKNOWN) {
90 state->AddInputDevice(*it);
91 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
92 VLOG(2) << "Arbitrarily matching touchscreen " << *it << " to display "
93 << state->id();
94 break;
100 } // namespace ash