Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ash / touch / touchscreen_util.cc
blob8242c4ba4d06eb5cce4c8e62a4d241fd42d91da6
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 VLOG(2) << "Found internal device for display " << internal_state->id()
32 << " with device id " << devices[i].id << " size "
33 << devices[i].size.ToString();
34 internal_state->AddInputDevice(devices[i].id);
35 internal_state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
36 continue;
39 bool found_mapping = false;
40 for (size_t j = 0; j < displays->size(); ++j) {
41 DisplayInfo* state = &(*displays)[j];
42 if (state == internal_state)
43 continue;
45 const gfx::Size native_size = state->GetNativeModeSize();
46 if (state->touch_support() == gfx::Display::TOUCH_SUPPORT_AVAILABLE ||
47 native_size.IsEmpty())
48 continue;
50 // Allow 1 pixel difference between screen and touchscreen
51 // resolutions. Because in some cases for monitor resolution
52 // 1024x768 touchscreen's resolution would be 1024x768, but for
53 // some 1023x767. It really depends on touchscreen's firmware
54 // configuration.
55 if (std::abs(native_size.width() - devices[i].size.width()) <= 1 &&
56 std::abs(native_size.height() - devices[i].size.height()) <= 1) {
57 state->AddInputDevice(devices[i].id);
58 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
60 VLOG(2) << "Found touchscreen for display " << state->id()
61 << " with device id " << devices[i].id << " size "
62 << devices[i].size.ToString();
63 found_mapping = true;
64 break;
68 if (!found_mapping) {
69 no_match_touchscreen.insert(devices[i].id);
70 VLOG(2) << "No matching display for device id " << devices[i].id
71 << " size " << devices[i].size.ToString();
75 // Sometimes we can't find a matching screen for the touchscreen, e.g.
76 // due to the touchscreen's reporting range having no correlation with the
77 // screen's resolution. In this case, we arbitrarily assign unmatched
78 // touchscreens to unmatched screens.
79 for (std::set<int>::iterator it = no_match_touchscreen.begin();
80 it != no_match_touchscreen.end();
81 ++it) {
82 for (size_t i = 0; i < displays->size(); ++i) {
83 DisplayInfo* state = &(*displays)[i];
84 if (!gfx::Display::IsInternalDisplayId(state->id()) &&
85 !state->GetNativeModeSize().IsEmpty() &&
86 state->touch_support() == gfx::Display::TOUCH_SUPPORT_UNKNOWN) {
87 state->AddInputDevice(*it);
88 state->set_touch_support(gfx::Display::TOUCH_SUPPORT_AVAILABLE);
89 VLOG(2) << "Arbitrarily matching touchscreen " << *it << " to display "
90 << state->id();
91 break;
97 } // namespace ash